bridge: netlink: export port's designated cost and port
[firefly-linux-kernel-4.4.55.git] / net / bridge / br_netlink.c
1 /*
2  *      Bridge netlink control interface
3  *
4  *      Authors:
5  *      Stephen Hemminger               <shemminger@osdl.org>
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/etherdevice.h>
16 #include <net/rtnetlink.h>
17 #include <net/net_namespace.h>
18 #include <net/sock.h>
19 #include <uapi/linux/if_bridge.h>
20
21 #include "br_private.h"
22 #include "br_private_stp.h"
23
24 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
25                                 u32 filter_mask)
26 {
27         struct net_bridge_vlan *v;
28         u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
29         u16 flags, pvid;
30         int num_vlans = 0;
31
32         if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
33                 return 0;
34
35         pvid = br_get_pvid(vg);
36         /* Count number of vlan infos */
37         list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
38                 flags = 0;
39                 /* only a context, bridge vlan not activated */
40                 if (!br_vlan_should_use(v))
41                         continue;
42                 if (v->vid == pvid)
43                         flags |= BRIDGE_VLAN_INFO_PVID;
44
45                 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
46                         flags |= BRIDGE_VLAN_INFO_UNTAGGED;
47
48                 if (vid_range_start == 0) {
49                         goto initvars;
50                 } else if ((v->vid - vid_range_end) == 1 &&
51                         flags == vid_range_flags) {
52                         vid_range_end = v->vid;
53                         continue;
54                 } else {
55                         if ((vid_range_end - vid_range_start) > 0)
56                                 num_vlans += 2;
57                         else
58                                 num_vlans += 1;
59                 }
60 initvars:
61                 vid_range_start = v->vid;
62                 vid_range_end = v->vid;
63                 vid_range_flags = flags;
64         }
65
66         if (vid_range_start != 0) {
67                 if ((vid_range_end - vid_range_start) > 0)
68                         num_vlans += 2;
69                 else
70                         num_vlans += 1;
71         }
72
73         return num_vlans;
74 }
75
76 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
77                                  u32 filter_mask)
78 {
79         int num_vlans;
80
81         if (!vg)
82                 return 0;
83
84         if (filter_mask & RTEXT_FILTER_BRVLAN)
85                 return vg->num_vlans;
86
87         rcu_read_lock();
88         num_vlans = __get_num_vlan_infos(vg, filter_mask);
89         rcu_read_unlock();
90
91         return num_vlans;
92 }
93
94 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
95                                            u32 filter_mask)
96 {
97         struct net_bridge_vlan_group *vg = NULL;
98         struct net_bridge_port *p;
99         struct net_bridge *br;
100         int num_vlan_infos;
101
102         rcu_read_lock();
103         if (br_port_exists(dev)) {
104                 p = br_port_get_rcu(dev);
105                 vg = nbp_vlan_group(p);
106         } else if (dev->priv_flags & IFF_EBRIDGE) {
107                 br = netdev_priv(dev);
108                 vg = br_vlan_group(br);
109         }
110         num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
111         rcu_read_unlock();
112
113         /* Each VLAN is returned in bridge_vlan_info along with flags */
114         return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
115 }
116
117 static inline size_t br_port_info_size(void)
118 {
119         return nla_total_size(1)        /* IFLA_BRPORT_STATE  */
120                 + nla_total_size(2)     /* IFLA_BRPORT_PRIORITY */
121                 + nla_total_size(4)     /* IFLA_BRPORT_COST */
122                 + nla_total_size(1)     /* IFLA_BRPORT_MODE */
123                 + nla_total_size(1)     /* IFLA_BRPORT_GUARD */
124                 + nla_total_size(1)     /* IFLA_BRPORT_PROTECT */
125                 + nla_total_size(1)     /* IFLA_BRPORT_FAST_LEAVE */
126                 + nla_total_size(1)     /* IFLA_BRPORT_LEARNING */
127                 + nla_total_size(1)     /* IFLA_BRPORT_UNICAST_FLOOD */
128                 + nla_total_size(1)     /* IFLA_BRPORT_PROXYARP */
129                 + nla_total_size(1)     /* IFLA_BRPORT_PROXYARP_WIFI */
130                 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
131                 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
132                 + nla_total_size(sizeof(u16))   /* IFLA_BRPORT_DESIGNATED_PORT */
133                 + nla_total_size(sizeof(u16))   /* IFLA_BRPORT_DESIGNATED_COST */
134                 + 0;
135 }
136
137 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
138 {
139         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
140                 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
141                 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
142                 + nla_total_size(4) /* IFLA_MASTER */
143                 + nla_total_size(4) /* IFLA_MTU */
144                 + nla_total_size(4) /* IFLA_LINK */
145                 + nla_total_size(1) /* IFLA_OPERSTATE */
146                 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
147                 + nla_total_size(br_get_link_af_size_filtered(dev,
148                                  filter_mask)); /* IFLA_AF_SPEC */
149 }
150
151 static int br_port_fill_attrs(struct sk_buff *skb,
152                               const struct net_bridge_port *p)
153 {
154         u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
155
156         if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
157             nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
158             nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
159             nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
160             nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
161             nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
162             nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
163             nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
164             nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
165             nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
166             nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
167                        !!(p->flags & BR_PROXYARP_WIFI)) ||
168             nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
169                     &p->designated_root) ||
170             nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
171                     &p->designated_bridge) ||
172             nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
173             nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost))
174                 return -EMSGSIZE;
175
176         return 0;
177 }
178
179 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
180                                     u16 vid_end, u16 flags)
181 {
182         struct  bridge_vlan_info vinfo;
183
184         if ((vid_end - vid_start) > 0) {
185                 /* add range to skb */
186                 vinfo.vid = vid_start;
187                 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
188                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
189                             sizeof(vinfo), &vinfo))
190                         goto nla_put_failure;
191
192                 vinfo.vid = vid_end;
193                 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
194                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
195                             sizeof(vinfo), &vinfo))
196                         goto nla_put_failure;
197         } else {
198                 vinfo.vid = vid_start;
199                 vinfo.flags = flags;
200                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
201                             sizeof(vinfo), &vinfo))
202                         goto nla_put_failure;
203         }
204
205         return 0;
206
207 nla_put_failure:
208         return -EMSGSIZE;
209 }
210
211 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
212                                          struct net_bridge_vlan_group *vg)
213 {
214         struct net_bridge_vlan *v;
215         u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
216         u16 flags, pvid;
217         int err = 0;
218
219         /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
220          * and mark vlan info with begin and end flags
221          * if vlaninfo represents a range
222          */
223         pvid = br_get_pvid(vg);
224         list_for_each_entry(v, &vg->vlan_list, vlist) {
225                 flags = 0;
226                 if (!br_vlan_should_use(v))
227                         continue;
228                 if (v->vid == pvid)
229                         flags |= BRIDGE_VLAN_INFO_PVID;
230
231                 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
232                         flags |= BRIDGE_VLAN_INFO_UNTAGGED;
233
234                 if (vid_range_start == 0) {
235                         goto initvars;
236                 } else if ((v->vid - vid_range_end) == 1 &&
237                         flags == vid_range_flags) {
238                         vid_range_end = v->vid;
239                         continue;
240                 } else {
241                         err = br_fill_ifvlaninfo_range(skb, vid_range_start,
242                                                        vid_range_end,
243                                                        vid_range_flags);
244                         if (err)
245                                 return err;
246                 }
247
248 initvars:
249                 vid_range_start = v->vid;
250                 vid_range_end = v->vid;
251                 vid_range_flags = flags;
252         }
253
254         if (vid_range_start != 0) {
255                 /* Call it once more to send any left over vlans */
256                 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
257                                                vid_range_end,
258                                                vid_range_flags);
259                 if (err)
260                         return err;
261         }
262
263         return 0;
264 }
265
266 static int br_fill_ifvlaninfo(struct sk_buff *skb,
267                               struct net_bridge_vlan_group *vg)
268 {
269         struct bridge_vlan_info vinfo;
270         struct net_bridge_vlan *v;
271         u16 pvid;
272
273         pvid = br_get_pvid(vg);
274         list_for_each_entry(v, &vg->vlan_list, vlist) {
275                 if (!br_vlan_should_use(v))
276                         continue;
277
278                 vinfo.vid = v->vid;
279                 vinfo.flags = 0;
280                 if (v->vid == pvid)
281                         vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
282
283                 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
284                         vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
285
286                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
287                             sizeof(vinfo), &vinfo))
288                         goto nla_put_failure;
289         }
290
291         return 0;
292
293 nla_put_failure:
294         return -EMSGSIZE;
295 }
296
297 /*
298  * Create one netlink message for one interface
299  * Contains port and master info as well as carrier and bridge state.
300  */
301 static int br_fill_ifinfo(struct sk_buff *skb,
302                           struct net_bridge_port *port,
303                           u32 pid, u32 seq, int event, unsigned int flags,
304                           u32 filter_mask, const struct net_device *dev)
305 {
306         struct net_bridge *br;
307         struct ifinfomsg *hdr;
308         struct nlmsghdr *nlh;
309         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
310
311         if (port)
312                 br = port->br;
313         else
314                 br = netdev_priv(dev);
315
316         br_debug(br, "br_fill_info event %d port %s master %s\n",
317                      event, dev->name, br->dev->name);
318
319         nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
320         if (nlh == NULL)
321                 return -EMSGSIZE;
322
323         hdr = nlmsg_data(nlh);
324         hdr->ifi_family = AF_BRIDGE;
325         hdr->__ifi_pad = 0;
326         hdr->ifi_type = dev->type;
327         hdr->ifi_index = dev->ifindex;
328         hdr->ifi_flags = dev_get_flags(dev);
329         hdr->ifi_change = 0;
330
331         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
332             nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
333             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
334             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
335             (dev->addr_len &&
336              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
337             (dev->ifindex != dev_get_iflink(dev) &&
338              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
339                 goto nla_put_failure;
340
341         if (event == RTM_NEWLINK && port) {
342                 struct nlattr *nest
343                         = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
344
345                 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
346                         goto nla_put_failure;
347                 nla_nest_end(skb, nest);
348         }
349
350         /* Check if  the VID information is requested */
351         if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
352             (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
353                 struct net_bridge_vlan_group *vg;
354                 struct nlattr *af;
355                 int err;
356
357                 if (port)
358                         vg = nbp_vlan_group(port);
359                 else
360                         vg = br_vlan_group(br);
361
362                 if (!vg || !vg->num_vlans)
363                         goto done;
364
365                 af = nla_nest_start(skb, IFLA_AF_SPEC);
366                 if (!af)
367                         goto nla_put_failure;
368
369                 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
370                         err = br_fill_ifvlaninfo_compressed(skb, vg);
371                 else
372                         err = br_fill_ifvlaninfo(skb, vg);
373                 if (err)
374                         goto nla_put_failure;
375                 nla_nest_end(skb, af);
376         }
377
378 done:
379         nlmsg_end(skb, nlh);
380         return 0;
381
382 nla_put_failure:
383         nlmsg_cancel(skb, nlh);
384         return -EMSGSIZE;
385 }
386
387 /*
388  * Notify listeners of a change in port information
389  */
390 void br_ifinfo_notify(int event, struct net_bridge_port *port)
391 {
392         struct net *net;
393         struct sk_buff *skb;
394         int err = -ENOBUFS;
395         u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
396
397         if (!port)
398                 return;
399
400         net = dev_net(port->dev);
401         br_debug(port->br, "port %u(%s) event %d\n",
402                  (unsigned int)port->port_no, port->dev->name, event);
403
404         skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
405         if (skb == NULL)
406                 goto errout;
407
408         err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
409         if (err < 0) {
410                 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
411                 WARN_ON(err == -EMSGSIZE);
412                 kfree_skb(skb);
413                 goto errout;
414         }
415         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
416         return;
417 errout:
418         rtnl_set_sk_err(net, RTNLGRP_LINK, err);
419 }
420
421
422 /*
423  * Dump information about all ports, in response to GETLINK
424  */
425 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
426                struct net_device *dev, u32 filter_mask, int nlflags)
427 {
428         struct net_bridge_port *port = br_port_get_rtnl(dev);
429
430         if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
431             !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
432                 return 0;
433
434         return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
435                               filter_mask, dev);
436 }
437
438 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
439                         int cmd, struct bridge_vlan_info *vinfo)
440 {
441         int err = 0;
442
443         switch (cmd) {
444         case RTM_SETLINK:
445                 if (p) {
446                         /* if the MASTER flag is set this will act on the global
447                          * per-VLAN entry as well
448                          */
449                         err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
450                         if (err)
451                                 break;
452                 } else {
453                         vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
454                         err = br_vlan_add(br, vinfo->vid, vinfo->flags);
455                 }
456                 break;
457
458         case RTM_DELLINK:
459                 if (p) {
460                         nbp_vlan_delete(p, vinfo->vid);
461                         if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
462                                 br_vlan_delete(p->br, vinfo->vid);
463                 } else {
464                         br_vlan_delete(br, vinfo->vid);
465                 }
466                 break;
467         }
468
469         return err;
470 }
471
472 static int br_afspec(struct net_bridge *br,
473                      struct net_bridge_port *p,
474                      struct nlattr *af_spec,
475                      int cmd)
476 {
477         struct bridge_vlan_info *vinfo_start = NULL;
478         struct bridge_vlan_info *vinfo = NULL;
479         struct nlattr *attr;
480         int err = 0;
481         int rem;
482
483         nla_for_each_nested(attr, af_spec, rem) {
484                 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
485                         continue;
486                 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
487                         return -EINVAL;
488                 vinfo = nla_data(attr);
489                 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
490                         return -EINVAL;
491                 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
492                         if (vinfo_start)
493                                 return -EINVAL;
494                         vinfo_start = vinfo;
495                         continue;
496                 }
497
498                 if (vinfo_start) {
499                         struct bridge_vlan_info tmp_vinfo;
500                         int v;
501
502                         if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
503                                 return -EINVAL;
504
505                         if (vinfo->vid <= vinfo_start->vid)
506                                 return -EINVAL;
507
508                         memcpy(&tmp_vinfo, vinfo_start,
509                                sizeof(struct bridge_vlan_info));
510
511                         for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
512                                 tmp_vinfo.vid = v;
513                                 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
514                                 if (err)
515                                         break;
516                         }
517                         vinfo_start = NULL;
518                 } else {
519                         err = br_vlan_info(br, p, cmd, vinfo);
520                 }
521                 if (err)
522                         break;
523         }
524
525         return err;
526 }
527
528 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
529         [IFLA_BRPORT_STATE]     = { .type = NLA_U8 },
530         [IFLA_BRPORT_COST]      = { .type = NLA_U32 },
531         [IFLA_BRPORT_PRIORITY]  = { .type = NLA_U16 },
532         [IFLA_BRPORT_MODE]      = { .type = NLA_U8 },
533         [IFLA_BRPORT_GUARD]     = { .type = NLA_U8 },
534         [IFLA_BRPORT_PROTECT]   = { .type = NLA_U8 },
535         [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
536         [IFLA_BRPORT_LEARNING]  = { .type = NLA_U8 },
537         [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
538         [IFLA_BRPORT_PROXYARP]  = { .type = NLA_U8 },
539         [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
540 };
541
542 /* Change the state of the port and notify spanning tree */
543 static int br_set_port_state(struct net_bridge_port *p, u8 state)
544 {
545         if (state > BR_STATE_BLOCKING)
546                 return -EINVAL;
547
548         /* if kernel STP is running, don't allow changes */
549         if (p->br->stp_enabled == BR_KERNEL_STP)
550                 return -EBUSY;
551
552         /* if device is not up, change is not allowed
553          * if link is not present, only allowable state is disabled
554          */
555         if (!netif_running(p->dev) ||
556             (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
557                 return -ENETDOWN;
558
559         br_set_state(p, state);
560         br_log_state(p);
561         br_port_state_selection(p->br);
562         return 0;
563 }
564
565 /* Set/clear or port flags based on attribute */
566 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
567                            int attrtype, unsigned long mask)
568 {
569         if (tb[attrtype]) {
570                 u8 flag = nla_get_u8(tb[attrtype]);
571                 if (flag)
572                         p->flags |= mask;
573                 else
574                         p->flags &= ~mask;
575         }
576 }
577
578 /* Process bridge protocol info on port */
579 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
580 {
581         int err;
582         unsigned long old_flags = p->flags;
583
584         br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
585         br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
586         br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
587         br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
588         br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
589         br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
590         br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
591         br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
592
593         if (tb[IFLA_BRPORT_COST]) {
594                 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
595                 if (err)
596                         return err;
597         }
598
599         if (tb[IFLA_BRPORT_PRIORITY]) {
600                 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
601                 if (err)
602                         return err;
603         }
604
605         if (tb[IFLA_BRPORT_STATE]) {
606                 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
607                 if (err)
608                         return err;
609         }
610
611         br_port_flags_change(p, old_flags ^ p->flags);
612         return 0;
613 }
614
615 /* Change state and parameters on port. */
616 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
617 {
618         struct nlattr *protinfo;
619         struct nlattr *afspec;
620         struct net_bridge_port *p;
621         struct nlattr *tb[IFLA_BRPORT_MAX + 1];
622         int err = 0;
623
624         protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
625         afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
626         if (!protinfo && !afspec)
627                 return 0;
628
629         p = br_port_get_rtnl(dev);
630         /* We want to accept dev as bridge itself if the AF_SPEC
631          * is set to see if someone is setting vlan info on the bridge
632          */
633         if (!p && !afspec)
634                 return -EINVAL;
635
636         if (p && protinfo) {
637                 if (protinfo->nla_type & NLA_F_NESTED) {
638                         err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
639                                                protinfo, br_port_policy);
640                         if (err)
641                                 return err;
642
643                         spin_lock_bh(&p->br->lock);
644                         err = br_setport(p, tb);
645                         spin_unlock_bh(&p->br->lock);
646                 } else {
647                         /* Binary compatibility with old RSTP */
648                         if (nla_len(protinfo) < sizeof(u8))
649                                 return -EINVAL;
650
651                         spin_lock_bh(&p->br->lock);
652                         err = br_set_port_state(p, nla_get_u8(protinfo));
653                         spin_unlock_bh(&p->br->lock);
654                 }
655                 if (err)
656                         goto out;
657         }
658
659         if (afspec) {
660                 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
661                                 afspec, RTM_SETLINK);
662         }
663
664         if (err == 0)
665                 br_ifinfo_notify(RTM_NEWLINK, p);
666 out:
667         return err;
668 }
669
670 /* Delete port information */
671 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
672 {
673         struct nlattr *afspec;
674         struct net_bridge_port *p;
675         int err = 0;
676
677         afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
678         if (!afspec)
679                 return 0;
680
681         p = br_port_get_rtnl(dev);
682         /* We want to accept dev as bridge itself as well */
683         if (!p && !(dev->priv_flags & IFF_EBRIDGE))
684                 return -EINVAL;
685
686         err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
687                         afspec, RTM_DELLINK);
688         if (err == 0)
689                 /* Send RTM_NEWLINK because userspace
690                  * expects RTM_NEWLINK for vlan dels
691                  */
692                 br_ifinfo_notify(RTM_NEWLINK, p);
693
694         return err;
695 }
696 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
697 {
698         if (tb[IFLA_ADDRESS]) {
699                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
700                         return -EINVAL;
701                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
702                         return -EADDRNOTAVAIL;
703         }
704
705         if (!data)
706                 return 0;
707
708 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
709         if (data[IFLA_BR_VLAN_PROTOCOL]) {
710                 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
711                 case htons(ETH_P_8021Q):
712                 case htons(ETH_P_8021AD):
713                         break;
714                 default:
715                         return -EPROTONOSUPPORT;
716                 }
717         }
718 #endif
719
720         return 0;
721 }
722
723 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
724                           struct nlattr *tb[], struct nlattr *data[])
725 {
726         struct net_bridge *br = netdev_priv(dev);
727
728         if (tb[IFLA_ADDRESS]) {
729                 spin_lock_bh(&br->lock);
730                 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
731                 spin_unlock_bh(&br->lock);
732         }
733
734         return register_netdevice(dev);
735 }
736
737 static int br_port_slave_changelink(struct net_device *brdev,
738                                     struct net_device *dev,
739                                     struct nlattr *tb[],
740                                     struct nlattr *data[])
741 {
742         struct net_bridge *br = netdev_priv(brdev);
743         int ret;
744
745         if (!data)
746                 return 0;
747
748         spin_lock_bh(&br->lock);
749         ret = br_setport(br_port_get_rtnl(dev), data);
750         spin_unlock_bh(&br->lock);
751
752         return ret;
753 }
754
755 static int br_port_fill_slave_info(struct sk_buff *skb,
756                                    const struct net_device *brdev,
757                                    const struct net_device *dev)
758 {
759         return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
760 }
761
762 static size_t br_port_get_slave_size(const struct net_device *brdev,
763                                      const struct net_device *dev)
764 {
765         return br_port_info_size();
766 }
767
768 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
769         [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
770         [IFLA_BR_HELLO_TIME]    = { .type = NLA_U32 },
771         [IFLA_BR_MAX_AGE]       = { .type = NLA_U32 },
772         [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
773         [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
774         [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
775         [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
776         [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
777         [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
778         [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
779                                  .len  = ETH_ALEN },
780         [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
781         [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
782         [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
783         [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
784         [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
785         [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
786         [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
787         [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
788         [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
789         [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
790         [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
791         [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
792         [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
793         [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
794         [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
795         [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
796         [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
797         [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
798 };
799
800 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
801                          struct nlattr *data[])
802 {
803         struct net_bridge *br = netdev_priv(brdev);
804         int err;
805
806         if (!data)
807                 return 0;
808
809         if (data[IFLA_BR_FORWARD_DELAY]) {
810                 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
811                 if (err)
812                         return err;
813         }
814
815         if (data[IFLA_BR_HELLO_TIME]) {
816                 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
817                 if (err)
818                         return err;
819         }
820
821         if (data[IFLA_BR_MAX_AGE]) {
822                 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
823                 if (err)
824                         return err;
825         }
826
827         if (data[IFLA_BR_AGEING_TIME]) {
828                 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
829
830                 br->ageing_time = clock_t_to_jiffies(ageing_time);
831         }
832
833         if (data[IFLA_BR_STP_STATE]) {
834                 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
835
836                 br_stp_set_enabled(br, stp_enabled);
837         }
838
839         if (data[IFLA_BR_PRIORITY]) {
840                 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
841
842                 br_stp_set_bridge_priority(br, priority);
843         }
844
845         if (data[IFLA_BR_VLAN_FILTERING]) {
846                 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
847
848                 err = __br_vlan_filter_toggle(br, vlan_filter);
849                 if (err)
850                         return err;
851         }
852
853 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
854         if (data[IFLA_BR_VLAN_PROTOCOL]) {
855                 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
856
857                 err = __br_vlan_set_proto(br, vlan_proto);
858                 if (err)
859                         return err;
860         }
861
862         if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
863                 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
864
865                 err = __br_vlan_set_default_pvid(br, defpvid);
866                 if (err)
867                         return err;
868         }
869 #endif
870
871         if (data[IFLA_BR_GROUP_FWD_MASK]) {
872                 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
873
874                 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
875                         return -EINVAL;
876                 br->group_fwd_mask = fwd_mask;
877         }
878
879         if (data[IFLA_BR_GROUP_ADDR]) {
880                 u8 new_addr[ETH_ALEN];
881
882                 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
883                         return -EINVAL;
884                 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
885                 if (!is_link_local_ether_addr(new_addr))
886                         return -EINVAL;
887                 if (new_addr[5] == 1 ||         /* 802.3x Pause address */
888                     new_addr[5] == 2 ||         /* 802.3ad Slow protocols */
889                     new_addr[5] == 3)           /* 802.1X PAE address */
890                         return -EINVAL;
891                 spin_lock_bh(&br->lock);
892                 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
893                 spin_unlock_bh(&br->lock);
894                 br->group_addr_set = true;
895                 br_recalculate_fwd_mask(br);
896         }
897
898         if (data[IFLA_BR_FDB_FLUSH])
899                 br_fdb_flush(br);
900
901 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
902         if (data[IFLA_BR_MCAST_ROUTER]) {
903                 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
904
905                 err = br_multicast_set_router(br, multicast_router);
906                 if (err)
907                         return err;
908         }
909
910         if (data[IFLA_BR_MCAST_SNOOPING]) {
911                 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
912
913                 err = br_multicast_toggle(br, mcast_snooping);
914                 if (err)
915                         return err;
916         }
917
918         if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
919                 u8 val;
920
921                 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
922                 br->multicast_query_use_ifaddr = !!val;
923         }
924
925         if (data[IFLA_BR_MCAST_QUERIER]) {
926                 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
927
928                 err = br_multicast_set_querier(br, mcast_querier);
929                 if (err)
930                         return err;
931         }
932
933         if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
934                 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
935
936                 br->hash_elasticity = val;
937         }
938
939         if (data[IFLA_BR_MCAST_HASH_MAX]) {
940                 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
941
942                 err = br_multicast_set_hash_max(br, hash_max);
943                 if (err)
944                         return err;
945         }
946
947         if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
948                 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
949
950                 br->multicast_last_member_count = val;
951         }
952
953         if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
954                 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
955
956                 br->multicast_startup_query_count = val;
957         }
958
959         if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
960                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
961
962                 br->multicast_last_member_interval = clock_t_to_jiffies(val);
963         }
964
965         if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
966                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
967
968                 br->multicast_membership_interval = clock_t_to_jiffies(val);
969         }
970
971         if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
972                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
973
974                 br->multicast_querier_interval = clock_t_to_jiffies(val);
975         }
976
977         if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
978                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
979
980                 br->multicast_query_interval = clock_t_to_jiffies(val);
981         }
982
983         if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
984                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
985
986                 br->multicast_query_response_interval = clock_t_to_jiffies(val);
987         }
988
989         if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
990                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
991
992                 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
993         }
994 #endif
995 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
996         if (data[IFLA_BR_NF_CALL_IPTABLES]) {
997                 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
998
999                 br->nf_call_iptables = val ? true : false;
1000         }
1001
1002         if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1003                 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1004
1005                 br->nf_call_ip6tables = val ? true : false;
1006         }
1007
1008         if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1009                 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1010
1011                 br->nf_call_arptables = val ? true : false;
1012         }
1013 #endif
1014
1015         return 0;
1016 }
1017
1018 static size_t br_get_size(const struct net_device *brdev)
1019 {
1020         return nla_total_size(sizeof(u32)) +    /* IFLA_BR_FORWARD_DELAY  */
1021                nla_total_size(sizeof(u32)) +    /* IFLA_BR_HELLO_TIME */
1022                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MAX_AGE */
1023                nla_total_size(sizeof(u32)) +    /* IFLA_BR_AGEING_TIME */
1024                nla_total_size(sizeof(u32)) +    /* IFLA_BR_STP_STATE */
1025                nla_total_size(sizeof(u16)) +    /* IFLA_BR_PRIORITY */
1026                nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_FILTERING */
1027 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1028                nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1029                nla_total_size(sizeof(u16)) +    /* IFLA_BR_VLAN_DEFAULT_PVID */
1030 #endif
1031                nla_total_size(sizeof(u16)) +    /* IFLA_BR_GROUP_FWD_MASK */
1032                nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_ROOT_ID */
1033                nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_BRIDGE_ID */
1034                nla_total_size(sizeof(u16)) +    /* IFLA_BR_ROOT_PORT */
1035                nla_total_size(sizeof(u32)) +    /* IFLA_BR_ROOT_PATH_COST */
1036                nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE */
1037                nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1038                nla_total_size(sizeof(u64)) +    /* IFLA_BR_HELLO_TIMER */
1039                nla_total_size(sizeof(u64)) +    /* IFLA_BR_TCN_TIMER */
1040                nla_total_size(sizeof(u64)) +    /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1041                nla_total_size(sizeof(u64)) +    /* IFLA_BR_GC_TIMER */
1042                nla_total_size(ETH_ALEN) +       /* IFLA_BR_GROUP_ADDR */
1043 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1044                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_ROUTER */
1045                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_SNOOPING */
1046                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1047                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERIER */
1048                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_ELASTICITY */
1049                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_MAX */
1050                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1051                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1052                nla_total_size(sizeof(u64)) +    /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1053                nla_total_size(sizeof(u64)) +    /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1054                nla_total_size(sizeof(u64)) +    /* IFLA_BR_MCAST_QUERIER_INTVL */
1055                nla_total_size(sizeof(u64)) +    /* IFLA_BR_MCAST_QUERY_INTVL */
1056                nla_total_size(sizeof(u64)) +    /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1057                nla_total_size(sizeof(u64)) +    /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1058 #endif
1059 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1060                nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IPTABLES */
1061                nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IP6TABLES */
1062                nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_ARPTABLES */
1063 #endif
1064                0;
1065 }
1066
1067 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1068 {
1069         struct net_bridge *br = netdev_priv(brdev);
1070         u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1071         u32 hello_time = jiffies_to_clock_t(br->hello_time);
1072         u32 age_time = jiffies_to_clock_t(br->max_age);
1073         u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1074         u32 stp_enabled = br->stp_enabled;
1075         u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1076         u8 vlan_enabled = br_vlan_enabled(br);
1077         u64 clockval;
1078
1079         clockval = br_timer_value(&br->hello_timer);
1080         if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
1081                 return -EMSGSIZE;
1082         clockval = br_timer_value(&br->tcn_timer);
1083         if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
1084                 return -EMSGSIZE;
1085         clockval = br_timer_value(&br->topology_change_timer);
1086         if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
1087                 return -EMSGSIZE;
1088         clockval = br_timer_value(&br->gc_timer);
1089         if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
1090                 return -EMSGSIZE;
1091
1092         if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1093             nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1094             nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1095             nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1096             nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1097             nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1098             nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1099             nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1100             nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1101                     &br->bridge_id) ||
1102             nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1103                     &br->designated_root) ||
1104             nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1105             nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1106             nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1107             nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1108                        br->topology_change_detected) ||
1109             nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
1110                 return -EMSGSIZE;
1111
1112 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1113         if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1114             nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
1115                 return -EMSGSIZE;
1116 #endif
1117 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1118         if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1119             nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1120             nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1121                        br->multicast_query_use_ifaddr) ||
1122             nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1123             nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
1124                         br->hash_elasticity) ||
1125             nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1126             nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1127                         br->multicast_last_member_count) ||
1128             nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1129                         br->multicast_startup_query_count))
1130                 return -EMSGSIZE;
1131
1132         clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1133         if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1134                 return -EMSGSIZE;
1135         clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1136         if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1137                 return -EMSGSIZE;
1138         clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1139         if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1140                 return -EMSGSIZE;
1141         clockval = jiffies_to_clock_t(br->multicast_query_interval);
1142         if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1143                 return -EMSGSIZE;
1144         clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1145         if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1146                 return -EMSGSIZE;
1147         clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1148         if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1149                 return -EMSGSIZE;
1150 #endif
1151 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1152         if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1153                        br->nf_call_iptables ? 1 : 0) ||
1154             nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1155                        br->nf_call_ip6tables ? 1 : 0) ||
1156             nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1157                        br->nf_call_arptables ? 1 : 0))
1158                 return -EMSGSIZE;
1159 #endif
1160
1161         return 0;
1162 }
1163
1164 static size_t br_get_link_af_size(const struct net_device *dev)
1165 {
1166         struct net_bridge_port *p;
1167         struct net_bridge *br;
1168         int num_vlans = 0;
1169
1170         if (br_port_exists(dev)) {
1171                 p = br_port_get_rtnl(dev);
1172                 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
1173                                                   RTEXT_FILTER_BRVLAN);
1174         } else if (dev->priv_flags & IFF_EBRIDGE) {
1175                 br = netdev_priv(dev);
1176                 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
1177                                                   RTEXT_FILTER_BRVLAN);
1178         }
1179
1180         /* Each VLAN is returned in bridge_vlan_info along with flags */
1181         return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
1182 }
1183
1184 static struct rtnl_af_ops br_af_ops __read_mostly = {
1185         .family                 = AF_BRIDGE,
1186         .get_link_af_size       = br_get_link_af_size,
1187 };
1188
1189 struct rtnl_link_ops br_link_ops __read_mostly = {
1190         .kind                   = "bridge",
1191         .priv_size              = sizeof(struct net_bridge),
1192         .setup                  = br_dev_setup,
1193         .maxtype                = IFLA_BR_MAX,
1194         .policy                 = br_policy,
1195         .validate               = br_validate,
1196         .newlink                = br_dev_newlink,
1197         .changelink             = br_changelink,
1198         .dellink                = br_dev_delete,
1199         .get_size               = br_get_size,
1200         .fill_info              = br_fill_info,
1201
1202         .slave_maxtype          = IFLA_BRPORT_MAX,
1203         .slave_policy           = br_port_policy,
1204         .slave_changelink       = br_port_slave_changelink,
1205         .get_slave_size         = br_port_get_slave_size,
1206         .fill_slave_info        = br_port_fill_slave_info,
1207 };
1208
1209 int __init br_netlink_init(void)
1210 {
1211         int err;
1212
1213         br_mdb_init();
1214         rtnl_af_register(&br_af_ops);
1215
1216         err = rtnl_link_register(&br_link_ops);
1217         if (err)
1218                 goto out_af;
1219
1220         return 0;
1221
1222 out_af:
1223         rtnl_af_unregister(&br_af_ops);
1224         br_mdb_uninit();
1225         return err;
1226 }
1227
1228 void br_netlink_fini(void)
1229 {
1230         br_mdb_uninit();
1231         rtnl_af_unregister(&br_af_ops);
1232         rtnl_link_unregister(&br_link_ops);
1233 }