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