]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/bridge/br_netlink.c
scsi: pmcraid: remove redundant check to see if request_size is less than zero
[karo-tx-linux.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 #include "br_private_tunnel.h"
24
25 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
26                                 u32 filter_mask)
27 {
28         struct net_bridge_vlan *v;
29         u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
30         u16 flags, pvid;
31         int num_vlans = 0;
32
33         if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
34                 return 0;
35
36         pvid = br_get_pvid(vg);
37         /* Count number of vlan infos */
38         list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
39                 flags = 0;
40                 /* only a context, bridge vlan not activated */
41                 if (!br_vlan_should_use(v))
42                         continue;
43                 if (v->vid == pvid)
44                         flags |= BRIDGE_VLAN_INFO_PVID;
45
46                 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
47                         flags |= BRIDGE_VLAN_INFO_UNTAGGED;
48
49                 if (vid_range_start == 0) {
50                         goto initvars;
51                 } else if ((v->vid - vid_range_end) == 1 &&
52                         flags == vid_range_flags) {
53                         vid_range_end = v->vid;
54                         continue;
55                 } else {
56                         if ((vid_range_end - vid_range_start) > 0)
57                                 num_vlans += 2;
58                         else
59                                 num_vlans += 1;
60                 }
61 initvars:
62                 vid_range_start = v->vid;
63                 vid_range_end = v->vid;
64                 vid_range_flags = flags;
65         }
66
67         if (vid_range_start != 0) {
68                 if ((vid_range_end - vid_range_start) > 0)
69                         num_vlans += 2;
70                 else
71                         num_vlans += 1;
72         }
73
74         return num_vlans;
75 }
76
77 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
78                                  u32 filter_mask)
79 {
80         int num_vlans;
81
82         if (!vg)
83                 return 0;
84
85         if (filter_mask & RTEXT_FILTER_BRVLAN)
86                 return vg->num_vlans;
87
88         rcu_read_lock();
89         num_vlans = __get_num_vlan_infos(vg, filter_mask);
90         rcu_read_unlock();
91
92         return num_vlans;
93 }
94
95 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
96                                            u32 filter_mask)
97 {
98         struct net_bridge_vlan_group *vg = NULL;
99         struct net_bridge_port *p = NULL;
100         struct net_bridge *br;
101         int num_vlan_infos;
102         size_t vinfo_sz = 0;
103
104         rcu_read_lock();
105         if (br_port_exists(dev)) {
106                 p = br_port_get_rcu(dev);
107                 vg = nbp_vlan_group_rcu(p);
108         } else if (dev->priv_flags & IFF_EBRIDGE) {
109                 br = netdev_priv(dev);
110                 vg = br_vlan_group_rcu(br);
111         }
112         num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
113         rcu_read_unlock();
114
115         if (p && (p->flags & BR_VLAN_TUNNEL))
116                 vinfo_sz += br_get_vlan_tunnel_info_size(vg);
117
118         /* Each VLAN is returned in bridge_vlan_info along with flags */
119         vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
120
121         return vinfo_sz;
122 }
123
124 static inline size_t br_port_info_size(void)
125 {
126         return nla_total_size(1)        /* IFLA_BRPORT_STATE  */
127                 + nla_total_size(2)     /* IFLA_BRPORT_PRIORITY */
128                 + nla_total_size(4)     /* IFLA_BRPORT_COST */
129                 + nla_total_size(1)     /* IFLA_BRPORT_MODE */
130                 + nla_total_size(1)     /* IFLA_BRPORT_GUARD */
131                 + nla_total_size(1)     /* IFLA_BRPORT_PROTECT */
132                 + nla_total_size(1)     /* IFLA_BRPORT_FAST_LEAVE */
133                 + nla_total_size(1)     /* IFLA_BRPORT_MCAST_TO_UCAST */
134                 + nla_total_size(1)     /* IFLA_BRPORT_LEARNING */
135                 + nla_total_size(1)     /* IFLA_BRPORT_UNICAST_FLOOD */
136                 + nla_total_size(1)     /* IFLA_BRPORT_PROXYARP */
137                 + nla_total_size(1)     /* IFLA_BRPORT_PROXYARP_WIFI */
138                 + nla_total_size(1)     /* IFLA_BRPORT_VLAN_TUNNEL */
139                 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
140                 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
141                 + nla_total_size(sizeof(u16))   /* IFLA_BRPORT_DESIGNATED_PORT */
142                 + nla_total_size(sizeof(u16))   /* IFLA_BRPORT_DESIGNATED_COST */
143                 + nla_total_size(sizeof(u16))   /* IFLA_BRPORT_ID */
144                 + nla_total_size(sizeof(u16))   /* IFLA_BRPORT_NO */
145                 + nla_total_size(sizeof(u8))    /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
146                 + nla_total_size(sizeof(u8))    /* IFLA_BRPORT_CONFIG_PENDING */
147                 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
148                 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
149                 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
150 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
151                 + nla_total_size(sizeof(u8))    /* IFLA_BRPORT_MULTICAST_ROUTER */
152 #endif
153                 + 0;
154 }
155
156 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
157 {
158         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
159                 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
160                 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
161                 + nla_total_size(4) /* IFLA_MASTER */
162                 + nla_total_size(4) /* IFLA_MTU */
163                 + nla_total_size(4) /* IFLA_LINK */
164                 + nla_total_size(1) /* IFLA_OPERSTATE */
165                 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
166                 + nla_total_size(br_get_link_af_size_filtered(dev,
167                                  filter_mask)); /* IFLA_AF_SPEC */
168 }
169
170 static int br_port_fill_attrs(struct sk_buff *skb,
171                               const struct net_bridge_port *p)
172 {
173         u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
174         u64 timerval;
175
176         if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
177             nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
178             nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
179             nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
180             nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
181             nla_put_u8(skb, IFLA_BRPORT_PROTECT,
182                        !!(p->flags & BR_ROOT_BLOCK)) ||
183             nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
184                        !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
185             nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
186                        !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
187             nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
188             nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
189                        !!(p->flags & BR_FLOOD)) ||
190             nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
191                        !!(p->flags & BR_MCAST_FLOOD)) ||
192             nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
193                        !!(p->flags & BR_BCAST_FLOOD)) ||
194             nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
195             nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
196                        !!(p->flags & BR_PROXYARP_WIFI)) ||
197             nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
198                     &p->designated_root) ||
199             nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
200                     &p->designated_bridge) ||
201             nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
202             nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
203             nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
204             nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
205             nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
206                        p->topology_change_ack) ||
207             nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
208             nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
209                                                         BR_VLAN_TUNNEL)))
210                 return -EMSGSIZE;
211
212         timerval = br_timer_value(&p->message_age_timer);
213         if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
214                               IFLA_BRPORT_PAD))
215                 return -EMSGSIZE;
216         timerval = br_timer_value(&p->forward_delay_timer);
217         if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
218                               IFLA_BRPORT_PAD))
219                 return -EMSGSIZE;
220         timerval = br_timer_value(&p->hold_timer);
221         if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
222                               IFLA_BRPORT_PAD))
223                 return -EMSGSIZE;
224
225 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
226         if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
227                        p->multicast_router))
228                 return -EMSGSIZE;
229 #endif
230
231         return 0;
232 }
233
234 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
235                                     u16 vid_end, u16 flags)
236 {
237         struct  bridge_vlan_info vinfo;
238
239         if ((vid_end - vid_start) > 0) {
240                 /* add range to skb */
241                 vinfo.vid = vid_start;
242                 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
243                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
244                             sizeof(vinfo), &vinfo))
245                         goto nla_put_failure;
246
247                 vinfo.vid = vid_end;
248                 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
249                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
250                             sizeof(vinfo), &vinfo))
251                         goto nla_put_failure;
252         } else {
253                 vinfo.vid = vid_start;
254                 vinfo.flags = flags;
255                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
256                             sizeof(vinfo), &vinfo))
257                         goto nla_put_failure;
258         }
259
260         return 0;
261
262 nla_put_failure:
263         return -EMSGSIZE;
264 }
265
266 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
267                                          struct net_bridge_vlan_group *vg)
268 {
269         struct net_bridge_vlan *v;
270         u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
271         u16 flags, pvid;
272         int err = 0;
273
274         /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
275          * and mark vlan info with begin and end flags
276          * if vlaninfo represents a range
277          */
278         pvid = br_get_pvid(vg);
279         list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
280                 flags = 0;
281                 if (!br_vlan_should_use(v))
282                         continue;
283                 if (v->vid == pvid)
284                         flags |= BRIDGE_VLAN_INFO_PVID;
285
286                 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
287                         flags |= BRIDGE_VLAN_INFO_UNTAGGED;
288
289                 if (vid_range_start == 0) {
290                         goto initvars;
291                 } else if ((v->vid - vid_range_end) == 1 &&
292                         flags == vid_range_flags) {
293                         vid_range_end = v->vid;
294                         continue;
295                 } else {
296                         err = br_fill_ifvlaninfo_range(skb, vid_range_start,
297                                                        vid_range_end,
298                                                        vid_range_flags);
299                         if (err)
300                                 return err;
301                 }
302
303 initvars:
304                 vid_range_start = v->vid;
305                 vid_range_end = v->vid;
306                 vid_range_flags = flags;
307         }
308
309         if (vid_range_start != 0) {
310                 /* Call it once more to send any left over vlans */
311                 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
312                                                vid_range_end,
313                                                vid_range_flags);
314                 if (err)
315                         return err;
316         }
317
318         return 0;
319 }
320
321 static int br_fill_ifvlaninfo(struct sk_buff *skb,
322                               struct net_bridge_vlan_group *vg)
323 {
324         struct bridge_vlan_info vinfo;
325         struct net_bridge_vlan *v;
326         u16 pvid;
327
328         pvid = br_get_pvid(vg);
329         list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
330                 if (!br_vlan_should_use(v))
331                         continue;
332
333                 vinfo.vid = v->vid;
334                 vinfo.flags = 0;
335                 if (v->vid == pvid)
336                         vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
337
338                 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
339                         vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
340
341                 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
342                             sizeof(vinfo), &vinfo))
343                         goto nla_put_failure;
344         }
345
346         return 0;
347
348 nla_put_failure:
349         return -EMSGSIZE;
350 }
351
352 /*
353  * Create one netlink message for one interface
354  * Contains port and master info as well as carrier and bridge state.
355  */
356 static int br_fill_ifinfo(struct sk_buff *skb,
357                           struct net_bridge_port *port,
358                           u32 pid, u32 seq, int event, unsigned int flags,
359                           u32 filter_mask, const struct net_device *dev)
360 {
361         struct net_bridge *br;
362         struct ifinfomsg *hdr;
363         struct nlmsghdr *nlh;
364         u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
365
366         if (port)
367                 br = port->br;
368         else
369                 br = netdev_priv(dev);
370
371         br_debug(br, "br_fill_info event %d port %s master %s\n",
372                      event, dev->name, br->dev->name);
373
374         nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
375         if (nlh == NULL)
376                 return -EMSGSIZE;
377
378         hdr = nlmsg_data(nlh);
379         hdr->ifi_family = AF_BRIDGE;
380         hdr->__ifi_pad = 0;
381         hdr->ifi_type = dev->type;
382         hdr->ifi_index = dev->ifindex;
383         hdr->ifi_flags = dev_get_flags(dev);
384         hdr->ifi_change = 0;
385
386         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
387             nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
388             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
389             nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
390             (dev->addr_len &&
391              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
392             (dev->ifindex != dev_get_iflink(dev) &&
393              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
394                 goto nla_put_failure;
395
396         if (event == RTM_NEWLINK && port) {
397                 struct nlattr *nest
398                         = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
399
400                 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
401                         goto nla_put_failure;
402                 nla_nest_end(skb, nest);
403         }
404
405         /* Check if  the VID information is requested */
406         if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
407             (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
408                 struct net_bridge_vlan_group *vg;
409                 struct nlattr *af;
410                 int err;
411
412                 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
413                 rcu_read_lock();
414                 if (port)
415                         vg = nbp_vlan_group_rcu(port);
416                 else
417                         vg = br_vlan_group_rcu(br);
418
419                 if (!vg || !vg->num_vlans) {
420                         rcu_read_unlock();
421                         goto done;
422                 }
423                 af = nla_nest_start(skb, IFLA_AF_SPEC);
424                 if (!af) {
425                         rcu_read_unlock();
426                         goto nla_put_failure;
427                 }
428                 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
429                         err = br_fill_ifvlaninfo_compressed(skb, vg);
430                 else
431                         err = br_fill_ifvlaninfo(skb, vg);
432
433                 if (port && (port->flags & BR_VLAN_TUNNEL))
434                         err = br_fill_vlan_tunnel_info(skb, vg);
435                 rcu_read_unlock();
436                 if (err)
437                         goto nla_put_failure;
438                 nla_nest_end(skb, af);
439         }
440
441 done:
442         nlmsg_end(skb, nlh);
443         return 0;
444
445 nla_put_failure:
446         nlmsg_cancel(skb, nlh);
447         return -EMSGSIZE;
448 }
449
450 /*
451  * Notify listeners of a change in port information
452  */
453 void br_ifinfo_notify(int event, struct net_bridge_port *port)
454 {
455         struct net *net;
456         struct sk_buff *skb;
457         int err = -ENOBUFS;
458         u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
459
460         if (!port)
461                 return;
462
463         net = dev_net(port->dev);
464         br_debug(port->br, "port %u(%s) event %d\n",
465                  (unsigned int)port->port_no, port->dev->name, event);
466
467         skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
468         if (skb == NULL)
469                 goto errout;
470
471         err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
472         if (err < 0) {
473                 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
474                 WARN_ON(err == -EMSGSIZE);
475                 kfree_skb(skb);
476                 goto errout;
477         }
478         rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
479         return;
480 errout:
481         rtnl_set_sk_err(net, RTNLGRP_LINK, err);
482 }
483
484
485 /*
486  * Dump information about all ports, in response to GETLINK
487  */
488 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
489                struct net_device *dev, u32 filter_mask, int nlflags)
490 {
491         struct net_bridge_port *port = br_port_get_rtnl(dev);
492
493         if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
494             !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
495                 return 0;
496
497         return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
498                               filter_mask, dev);
499 }
500
501 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
502                         int cmd, struct bridge_vlan_info *vinfo)
503 {
504         int err = 0;
505
506         switch (cmd) {
507         case RTM_SETLINK:
508                 if (p) {
509                         /* if the MASTER flag is set this will act on the global
510                          * per-VLAN entry as well
511                          */
512                         err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
513                         if (err)
514                                 break;
515                 } else {
516                         vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
517                         err = br_vlan_add(br, vinfo->vid, vinfo->flags);
518                 }
519                 break;
520
521         case RTM_DELLINK:
522                 if (p) {
523                         nbp_vlan_delete(p, vinfo->vid);
524                         if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
525                                 br_vlan_delete(p->br, vinfo->vid);
526                 } else {
527                         br_vlan_delete(br, vinfo->vid);
528                 }
529                 break;
530         }
531
532         return err;
533 }
534
535 static int br_process_vlan_info(struct net_bridge *br,
536                                 struct net_bridge_port *p, int cmd,
537                                 struct bridge_vlan_info *vinfo_curr,
538                                 struct bridge_vlan_info **vinfo_last)
539 {
540         if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK)
541                 return -EINVAL;
542
543         if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
544                 /* check if we are already processing a range */
545                 if (*vinfo_last)
546                         return -EINVAL;
547                 *vinfo_last = vinfo_curr;
548                 /* don't allow range of pvids */
549                 if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
550                         return -EINVAL;
551                 return 0;
552         }
553
554         if (*vinfo_last) {
555                 struct bridge_vlan_info tmp_vinfo;
556                 int v, err;
557
558                 if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END))
559                         return -EINVAL;
560
561                 if (vinfo_curr->vid <= (*vinfo_last)->vid)
562                         return -EINVAL;
563
564                 memcpy(&tmp_vinfo, *vinfo_last,
565                        sizeof(struct bridge_vlan_info));
566                 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
567                         tmp_vinfo.vid = v;
568                         err = br_vlan_info(br, p, cmd, &tmp_vinfo);
569                         if (err)
570                                 break;
571                 }
572                 *vinfo_last = NULL;
573
574                 return 0;
575         }
576
577         return br_vlan_info(br, p, cmd, vinfo_curr);
578 }
579
580 static int br_afspec(struct net_bridge *br,
581                      struct net_bridge_port *p,
582                      struct nlattr *af_spec,
583                      int cmd)
584 {
585         struct bridge_vlan_info *vinfo_curr = NULL;
586         struct bridge_vlan_info *vinfo_last = NULL;
587         struct nlattr *attr;
588         struct vtunnel_info tinfo_last = {};
589         struct vtunnel_info tinfo_curr = {};
590         int err = 0, rem;
591
592         nla_for_each_nested(attr, af_spec, rem) {
593                 err = 0;
594                 switch (nla_type(attr)) {
595                 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
596                         if (!(p->flags & BR_VLAN_TUNNEL))
597                                 return -EINVAL;
598                         err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
599                         if (err)
600                                 return err;
601                         err = br_process_vlan_tunnel_info(br, p, cmd,
602                                                           &tinfo_curr,
603                                                           &tinfo_last);
604                         if (err)
605                                 return err;
606                         break;
607                 case IFLA_BRIDGE_VLAN_INFO:
608                         if (nla_len(attr) != sizeof(struct bridge_vlan_info))
609                                 return -EINVAL;
610                         vinfo_curr = nla_data(attr);
611                         err = br_process_vlan_info(br, p, cmd, vinfo_curr,
612                                                    &vinfo_last);
613                         if (err)
614                                 return err;
615                         break;
616                 }
617         }
618
619         return err;
620 }
621
622 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
623         [IFLA_BRPORT_STATE]     = { .type = NLA_U8 },
624         [IFLA_BRPORT_COST]      = { .type = NLA_U32 },
625         [IFLA_BRPORT_PRIORITY]  = { .type = NLA_U16 },
626         [IFLA_BRPORT_MODE]      = { .type = NLA_U8 },
627         [IFLA_BRPORT_GUARD]     = { .type = NLA_U8 },
628         [IFLA_BRPORT_PROTECT]   = { .type = NLA_U8 },
629         [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
630         [IFLA_BRPORT_LEARNING]  = { .type = NLA_U8 },
631         [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
632         [IFLA_BRPORT_PROXYARP]  = { .type = NLA_U8 },
633         [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
634         [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
635         [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
636 };
637
638 /* Change the state of the port and notify spanning tree */
639 static int br_set_port_state(struct net_bridge_port *p, u8 state)
640 {
641         if (state > BR_STATE_BLOCKING)
642                 return -EINVAL;
643
644         /* if kernel STP is running, don't allow changes */
645         if (p->br->stp_enabled == BR_KERNEL_STP)
646                 return -EBUSY;
647
648         /* if device is not up, change is not allowed
649          * if link is not present, only allowable state is disabled
650          */
651         if (!netif_running(p->dev) ||
652             (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
653                 return -ENETDOWN;
654
655         br_set_state(p, state);
656         br_port_state_selection(p->br);
657         return 0;
658 }
659
660 /* Set/clear or port flags based on attribute */
661 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
662                            int attrtype, unsigned long mask)
663 {
664         if (tb[attrtype]) {
665                 u8 flag = nla_get_u8(tb[attrtype]);
666                 if (flag)
667                         p->flags |= mask;
668                 else
669                         p->flags &= ~mask;
670         }
671 }
672
673 /* Process bridge protocol info on port */
674 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
675 {
676         unsigned long old_flags = p->flags;
677         bool br_vlan_tunnel_old = false;
678         int err;
679
680         br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
681         br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
682         br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
683         br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
684         br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
685         br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
686         br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
687         br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
688         br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
689         br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
690         br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
691
692         br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
693         br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
694         if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
695                 nbp_vlan_tunnel_info_flush(p);
696
697         if (tb[IFLA_BRPORT_COST]) {
698                 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
699                 if (err)
700                         return err;
701         }
702
703         if (tb[IFLA_BRPORT_PRIORITY]) {
704                 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
705                 if (err)
706                         return err;
707         }
708
709         if (tb[IFLA_BRPORT_STATE]) {
710                 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
711                 if (err)
712                         return err;
713         }
714
715         if (tb[IFLA_BRPORT_FLUSH])
716                 br_fdb_delete_by_port(p->br, p, 0, 0);
717
718 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
719         if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
720                 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
721
722                 err = br_multicast_set_port_router(p, mcast_router);
723                 if (err)
724                         return err;
725         }
726 #endif
727         br_port_flags_change(p, old_flags ^ p->flags);
728         return 0;
729 }
730
731 /* Change state and parameters on port. */
732 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
733 {
734         struct nlattr *protinfo;
735         struct nlattr *afspec;
736         struct net_bridge_port *p;
737         struct nlattr *tb[IFLA_BRPORT_MAX + 1];
738         int err = 0;
739
740         protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
741         afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
742         if (!protinfo && !afspec)
743                 return 0;
744
745         p = br_port_get_rtnl(dev);
746         /* We want to accept dev as bridge itself if the AF_SPEC
747          * is set to see if someone is setting vlan info on the bridge
748          */
749         if (!p && !afspec)
750                 return -EINVAL;
751
752         if (p && protinfo) {
753                 if (protinfo->nla_type & NLA_F_NESTED) {
754                         err = nla_parse_nested(tb, IFLA_BRPORT_MAX, protinfo,
755                                                br_port_policy, NULL);
756                         if (err)
757                                 return err;
758
759                         spin_lock_bh(&p->br->lock);
760                         err = br_setport(p, tb);
761                         spin_unlock_bh(&p->br->lock);
762                 } else {
763                         /* Binary compatibility with old RSTP */
764                         if (nla_len(protinfo) < sizeof(u8))
765                                 return -EINVAL;
766
767                         spin_lock_bh(&p->br->lock);
768                         err = br_set_port_state(p, nla_get_u8(protinfo));
769                         spin_unlock_bh(&p->br->lock);
770                 }
771                 if (err)
772                         goto out;
773         }
774
775         if (afspec) {
776                 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
777                                 afspec, RTM_SETLINK);
778         }
779
780         if (err == 0)
781                 br_ifinfo_notify(RTM_NEWLINK, p);
782 out:
783         return err;
784 }
785
786 /* Delete port information */
787 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
788 {
789         struct nlattr *afspec;
790         struct net_bridge_port *p;
791         int err = 0;
792
793         afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
794         if (!afspec)
795                 return 0;
796
797         p = br_port_get_rtnl(dev);
798         /* We want to accept dev as bridge itself as well */
799         if (!p && !(dev->priv_flags & IFF_EBRIDGE))
800                 return -EINVAL;
801
802         err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
803                         afspec, RTM_DELLINK);
804         if (err == 0)
805                 /* Send RTM_NEWLINK because userspace
806                  * expects RTM_NEWLINK for vlan dels
807                  */
808                 br_ifinfo_notify(RTM_NEWLINK, p);
809
810         return err;
811 }
812 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
813 {
814         if (tb[IFLA_ADDRESS]) {
815                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
816                         return -EINVAL;
817                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
818                         return -EADDRNOTAVAIL;
819         }
820
821         if (!data)
822                 return 0;
823
824 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
825         if (data[IFLA_BR_VLAN_PROTOCOL]) {
826                 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
827                 case htons(ETH_P_8021Q):
828                 case htons(ETH_P_8021AD):
829                         break;
830                 default:
831                         return -EPROTONOSUPPORT;
832                 }
833         }
834 #endif
835
836         return 0;
837 }
838
839 static int br_port_slave_changelink(struct net_device *brdev,
840                                     struct net_device *dev,
841                                     struct nlattr *tb[],
842                                     struct nlattr *data[])
843 {
844         struct net_bridge *br = netdev_priv(brdev);
845         int ret;
846
847         if (!data)
848                 return 0;
849
850         spin_lock_bh(&br->lock);
851         ret = br_setport(br_port_get_rtnl(dev), data);
852         spin_unlock_bh(&br->lock);
853
854         return ret;
855 }
856
857 static int br_port_fill_slave_info(struct sk_buff *skb,
858                                    const struct net_device *brdev,
859                                    const struct net_device *dev)
860 {
861         return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
862 }
863
864 static size_t br_port_get_slave_size(const struct net_device *brdev,
865                                      const struct net_device *dev)
866 {
867         return br_port_info_size();
868 }
869
870 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
871         [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
872         [IFLA_BR_HELLO_TIME]    = { .type = NLA_U32 },
873         [IFLA_BR_MAX_AGE]       = { .type = NLA_U32 },
874         [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
875         [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
876         [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
877         [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
878         [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
879         [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
880         [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
881                                  .len  = ETH_ALEN },
882         [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
883         [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
884         [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
885         [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
886         [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
887         [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
888         [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
889         [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
890         [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
891         [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
892         [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
893         [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
894         [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
895         [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
896         [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
897         [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
898         [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
899         [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
900         [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
901         [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
902         [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
903         [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
904 };
905
906 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
907                          struct nlattr *data[])
908 {
909         struct net_bridge *br = netdev_priv(brdev);
910         int err;
911
912         if (!data)
913                 return 0;
914
915         if (data[IFLA_BR_FORWARD_DELAY]) {
916                 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
917                 if (err)
918                         return err;
919         }
920
921         if (data[IFLA_BR_HELLO_TIME]) {
922                 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
923                 if (err)
924                         return err;
925         }
926
927         if (data[IFLA_BR_MAX_AGE]) {
928                 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
929                 if (err)
930                         return err;
931         }
932
933         if (data[IFLA_BR_AGEING_TIME]) {
934                 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
935                 if (err)
936                         return err;
937         }
938
939         if (data[IFLA_BR_STP_STATE]) {
940                 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
941
942                 br_stp_set_enabled(br, stp_enabled);
943         }
944
945         if (data[IFLA_BR_PRIORITY]) {
946                 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
947
948                 br_stp_set_bridge_priority(br, priority);
949         }
950
951         if (data[IFLA_BR_VLAN_FILTERING]) {
952                 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
953
954                 err = __br_vlan_filter_toggle(br, vlan_filter);
955                 if (err)
956                         return err;
957         }
958
959 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
960         if (data[IFLA_BR_VLAN_PROTOCOL]) {
961                 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
962
963                 err = __br_vlan_set_proto(br, vlan_proto);
964                 if (err)
965                         return err;
966         }
967
968         if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
969                 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
970
971                 err = __br_vlan_set_default_pvid(br, defpvid);
972                 if (err)
973                         return err;
974         }
975
976         if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
977                 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
978
979                 err = br_vlan_set_stats(br, vlan_stats);
980                 if (err)
981                         return err;
982         }
983 #endif
984
985         if (data[IFLA_BR_GROUP_FWD_MASK]) {
986                 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
987
988                 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
989                         return -EINVAL;
990                 br->group_fwd_mask = fwd_mask;
991         }
992
993         if (data[IFLA_BR_GROUP_ADDR]) {
994                 u8 new_addr[ETH_ALEN];
995
996                 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
997                         return -EINVAL;
998                 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
999                 if (!is_link_local_ether_addr(new_addr))
1000                         return -EINVAL;
1001                 if (new_addr[5] == 1 ||         /* 802.3x Pause address */
1002                     new_addr[5] == 2 ||         /* 802.3ad Slow protocols */
1003                     new_addr[5] == 3)           /* 802.1X PAE address */
1004                         return -EINVAL;
1005                 spin_lock_bh(&br->lock);
1006                 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1007                 spin_unlock_bh(&br->lock);
1008                 br->group_addr_set = true;
1009                 br_recalculate_fwd_mask(br);
1010         }
1011
1012         if (data[IFLA_BR_FDB_FLUSH])
1013                 br_fdb_flush(br);
1014
1015 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1016         if (data[IFLA_BR_MCAST_ROUTER]) {
1017                 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1018
1019                 err = br_multicast_set_router(br, multicast_router);
1020                 if (err)
1021                         return err;
1022         }
1023
1024         if (data[IFLA_BR_MCAST_SNOOPING]) {
1025                 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1026
1027                 err = br_multicast_toggle(br, mcast_snooping);
1028                 if (err)
1029                         return err;
1030         }
1031
1032         if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1033                 u8 val;
1034
1035                 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1036                 br->multicast_query_use_ifaddr = !!val;
1037         }
1038
1039         if (data[IFLA_BR_MCAST_QUERIER]) {
1040                 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1041
1042                 err = br_multicast_set_querier(br, mcast_querier);
1043                 if (err)
1044                         return err;
1045         }
1046
1047         if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
1048                 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
1049
1050                 br->hash_elasticity = val;
1051         }
1052
1053         if (data[IFLA_BR_MCAST_HASH_MAX]) {
1054                 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1055
1056                 err = br_multicast_set_hash_max(br, hash_max);
1057                 if (err)
1058                         return err;
1059         }
1060
1061         if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1062                 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1063
1064                 br->multicast_last_member_count = val;
1065         }
1066
1067         if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1068                 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1069
1070                 br->multicast_startup_query_count = val;
1071         }
1072
1073         if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1074                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1075
1076                 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1077         }
1078
1079         if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1080                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1081
1082                 br->multicast_membership_interval = clock_t_to_jiffies(val);
1083         }
1084
1085         if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1086                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1087
1088                 br->multicast_querier_interval = clock_t_to_jiffies(val);
1089         }
1090
1091         if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1092                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1093
1094                 br->multicast_query_interval = clock_t_to_jiffies(val);
1095         }
1096
1097         if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1098                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1099
1100                 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1101         }
1102
1103         if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1104                 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1105
1106                 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1107         }
1108
1109         if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1110                 __u8 mcast_stats;
1111
1112                 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1113                 br->multicast_stats_enabled = !!mcast_stats;
1114         }
1115
1116         if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1117                 __u8 igmp_version;
1118
1119                 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1120                 err = br_multicast_set_igmp_version(br, igmp_version);
1121                 if (err)
1122                         return err;
1123         }
1124
1125 #if IS_ENABLED(CONFIG_IPV6)
1126         if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1127                 __u8 mld_version;
1128
1129                 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1130                 err = br_multicast_set_mld_version(br, mld_version);
1131                 if (err)
1132                         return err;
1133         }
1134 #endif
1135 #endif
1136 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1137         if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1138                 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1139
1140                 br->nf_call_iptables = val ? true : false;
1141         }
1142
1143         if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1144                 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1145
1146                 br->nf_call_ip6tables = val ? true : false;
1147         }
1148
1149         if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1150                 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1151
1152                 br->nf_call_arptables = val ? true : false;
1153         }
1154 #endif
1155
1156         return 0;
1157 }
1158
1159 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1160                           struct nlattr *tb[], struct nlattr *data[])
1161 {
1162         struct net_bridge *br = netdev_priv(dev);
1163         int err;
1164
1165         if (tb[IFLA_ADDRESS]) {
1166                 spin_lock_bh(&br->lock);
1167                 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1168                 spin_unlock_bh(&br->lock);
1169         }
1170
1171         err = register_netdevice(dev);
1172         if (err)
1173                 return err;
1174
1175         err = br_changelink(dev, tb, data);
1176         if (err)
1177                 unregister_netdevice(dev);
1178         return err;
1179 }
1180
1181 static size_t br_get_size(const struct net_device *brdev)
1182 {
1183         return nla_total_size(sizeof(u32)) +    /* IFLA_BR_FORWARD_DELAY  */
1184                nla_total_size(sizeof(u32)) +    /* IFLA_BR_HELLO_TIME */
1185                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MAX_AGE */
1186                nla_total_size(sizeof(u32)) +    /* IFLA_BR_AGEING_TIME */
1187                nla_total_size(sizeof(u32)) +    /* IFLA_BR_STP_STATE */
1188                nla_total_size(sizeof(u16)) +    /* IFLA_BR_PRIORITY */
1189                nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_FILTERING */
1190 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1191                nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1192                nla_total_size(sizeof(u16)) +    /* IFLA_BR_VLAN_DEFAULT_PVID */
1193                nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_STATS_ENABLED */
1194 #endif
1195                nla_total_size(sizeof(u16)) +    /* IFLA_BR_GROUP_FWD_MASK */
1196                nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_ROOT_ID */
1197                nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_BRIDGE_ID */
1198                nla_total_size(sizeof(u16)) +    /* IFLA_BR_ROOT_PORT */
1199                nla_total_size(sizeof(u32)) +    /* IFLA_BR_ROOT_PATH_COST */
1200                nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE */
1201                nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1202                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1203                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1204                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1205                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1206                nla_total_size(ETH_ALEN) +       /* IFLA_BR_GROUP_ADDR */
1207 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1208                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_ROUTER */
1209                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_SNOOPING */
1210                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1211                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERIER */
1212                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_STATS_ENABLED */
1213                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_ELASTICITY */
1214                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_MAX */
1215                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1216                nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1217                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1218                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1219                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1220                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1221                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1222                nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1223                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_IGMP_VERSION */
1224                nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_MLD_VERSION */
1225 #endif
1226 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1227                nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IPTABLES */
1228                nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IP6TABLES */
1229                nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_ARPTABLES */
1230 #endif
1231                0;
1232 }
1233
1234 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1235 {
1236         struct net_bridge *br = netdev_priv(brdev);
1237         u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1238         u32 hello_time = jiffies_to_clock_t(br->hello_time);
1239         u32 age_time = jiffies_to_clock_t(br->max_age);
1240         u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1241         u32 stp_enabled = br->stp_enabled;
1242         u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1243         u8 vlan_enabled = br_vlan_enabled(br);
1244         u64 clockval;
1245
1246         clockval = br_timer_value(&br->hello_timer);
1247         if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1248                 return -EMSGSIZE;
1249         clockval = br_timer_value(&br->tcn_timer);
1250         if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1251                 return -EMSGSIZE;
1252         clockval = br_timer_value(&br->topology_change_timer);
1253         if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1254                               IFLA_BR_PAD))
1255                 return -EMSGSIZE;
1256         clockval = br_timer_value(&br->gc_work.timer);
1257         if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1258                 return -EMSGSIZE;
1259
1260         if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1261             nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1262             nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1263             nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1264             nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1265             nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1266             nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1267             nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1268             nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1269                     &br->bridge_id) ||
1270             nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1271                     &br->designated_root) ||
1272             nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1273             nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1274             nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1275             nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1276                        br->topology_change_detected) ||
1277             nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
1278                 return -EMSGSIZE;
1279
1280 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1281         if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1282             nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1283             nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
1284                 return -EMSGSIZE;
1285 #endif
1286 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1287         if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1288             nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1289             nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1290                        br->multicast_query_use_ifaddr) ||
1291             nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1292             nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1293                        br->multicast_stats_enabled) ||
1294             nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
1295                         br->hash_elasticity) ||
1296             nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1297             nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1298                         br->multicast_last_member_count) ||
1299             nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1300                         br->multicast_startup_query_count) ||
1301             nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1302                        br->multicast_igmp_version))
1303                 return -EMSGSIZE;
1304 #if IS_ENABLED(CONFIG_IPV6)
1305         if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1306                        br->multicast_mld_version))
1307                 return -EMSGSIZE;
1308 #endif
1309         clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1310         if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1311                               IFLA_BR_PAD))
1312                 return -EMSGSIZE;
1313         clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1314         if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1315                               IFLA_BR_PAD))
1316                 return -EMSGSIZE;
1317         clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1318         if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1319                               IFLA_BR_PAD))
1320                 return -EMSGSIZE;
1321         clockval = jiffies_to_clock_t(br->multicast_query_interval);
1322         if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1323                               IFLA_BR_PAD))
1324                 return -EMSGSIZE;
1325         clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1326         if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1327                               IFLA_BR_PAD))
1328                 return -EMSGSIZE;
1329         clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1330         if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1331                               IFLA_BR_PAD))
1332                 return -EMSGSIZE;
1333 #endif
1334 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1335         if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1336                        br->nf_call_iptables ? 1 : 0) ||
1337             nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1338                        br->nf_call_ip6tables ? 1 : 0) ||
1339             nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1340                        br->nf_call_arptables ? 1 : 0))
1341                 return -EMSGSIZE;
1342 #endif
1343
1344         return 0;
1345 }
1346
1347 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1348 {
1349         struct net_bridge_port *p = NULL;
1350         struct net_bridge_vlan_group *vg;
1351         struct net_bridge_vlan *v;
1352         struct net_bridge *br;
1353         int numvls = 0;
1354
1355         switch (attr) {
1356         case IFLA_STATS_LINK_XSTATS:
1357                 br = netdev_priv(dev);
1358                 vg = br_vlan_group(br);
1359                 break;
1360         case IFLA_STATS_LINK_XSTATS_SLAVE:
1361                 p = br_port_get_rtnl(dev);
1362                 if (!p)
1363                         return 0;
1364                 br = p->br;
1365                 vg = nbp_vlan_group(p);
1366                 break;
1367         default:
1368                 return 0;
1369         }
1370
1371         if (vg) {
1372                 /* we need to count all, even placeholder entries */
1373                 list_for_each_entry(v, &vg->vlan_list, vlist)
1374                         numvls++;
1375         }
1376
1377         return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1378                nla_total_size(sizeof(struct br_mcast_stats)) +
1379                nla_total_size(0);
1380 }
1381
1382 static int br_fill_linkxstats(struct sk_buff *skb,
1383                               const struct net_device *dev,
1384                               int *prividx, int attr)
1385 {
1386         struct nlattr *nla __maybe_unused;
1387         struct net_bridge_port *p = NULL;
1388         struct net_bridge_vlan_group *vg;
1389         struct net_bridge_vlan *v;
1390         struct net_bridge *br;
1391         struct nlattr *nest;
1392         int vl_idx = 0;
1393
1394         switch (attr) {
1395         case IFLA_STATS_LINK_XSTATS:
1396                 br = netdev_priv(dev);
1397                 vg = br_vlan_group(br);
1398                 break;
1399         case IFLA_STATS_LINK_XSTATS_SLAVE:
1400                 p = br_port_get_rtnl(dev);
1401                 if (!p)
1402                         return 0;
1403                 br = p->br;
1404                 vg = nbp_vlan_group(p);
1405                 break;
1406         default:
1407                 return -EINVAL;
1408         }
1409
1410         nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
1411         if (!nest)
1412                 return -EMSGSIZE;
1413
1414         if (vg) {
1415                 u16 pvid;
1416
1417                 pvid = br_get_pvid(vg);
1418                 list_for_each_entry(v, &vg->vlan_list, vlist) {
1419                         struct bridge_vlan_xstats vxi;
1420                         struct br_vlan_stats stats;
1421
1422                         if (++vl_idx < *prividx)
1423                                 continue;
1424                         memset(&vxi, 0, sizeof(vxi));
1425                         vxi.vid = v->vid;
1426                         vxi.flags = v->flags;
1427                         if (v->vid == pvid)
1428                                 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1429                         br_vlan_get_stats(v, &stats);
1430                         vxi.rx_bytes = stats.rx_bytes;
1431                         vxi.rx_packets = stats.rx_packets;
1432                         vxi.tx_bytes = stats.tx_bytes;
1433                         vxi.tx_packets = stats.tx_packets;
1434
1435                         if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1436                                 goto nla_put_failure;
1437                 }
1438         }
1439
1440 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1441         if (++vl_idx >= *prividx) {
1442                 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1443                                         sizeof(struct br_mcast_stats),
1444                                         BRIDGE_XSTATS_PAD);
1445                 if (!nla)
1446                         goto nla_put_failure;
1447                 br_multicast_get_stats(br, p, nla_data(nla));
1448         }
1449 #endif
1450         nla_nest_end(skb, nest);
1451         *prividx = 0;
1452
1453         return 0;
1454
1455 nla_put_failure:
1456         nla_nest_end(skb, nest);
1457         *prividx = vl_idx;
1458
1459         return -EMSGSIZE;
1460 }
1461
1462 static struct rtnl_af_ops br_af_ops __read_mostly = {
1463         .family                 = AF_BRIDGE,
1464         .get_link_af_size       = br_get_link_af_size_filtered,
1465 };
1466
1467 struct rtnl_link_ops br_link_ops __read_mostly = {
1468         .kind                   = "bridge",
1469         .priv_size              = sizeof(struct net_bridge),
1470         .setup                  = br_dev_setup,
1471         .maxtype                = IFLA_BR_MAX,
1472         .policy                 = br_policy,
1473         .validate               = br_validate,
1474         .newlink                = br_dev_newlink,
1475         .changelink             = br_changelink,
1476         .dellink                = br_dev_delete,
1477         .get_size               = br_get_size,
1478         .fill_info              = br_fill_info,
1479         .fill_linkxstats        = br_fill_linkxstats,
1480         .get_linkxstats_size    = br_get_linkxstats_size,
1481
1482         .slave_maxtype          = IFLA_BRPORT_MAX,
1483         .slave_policy           = br_port_policy,
1484         .slave_changelink       = br_port_slave_changelink,
1485         .get_slave_size         = br_port_get_slave_size,
1486         .fill_slave_info        = br_port_fill_slave_info,
1487 };
1488
1489 int __init br_netlink_init(void)
1490 {
1491         int err;
1492
1493         br_mdb_init();
1494         rtnl_af_register(&br_af_ops);
1495
1496         err = rtnl_link_register(&br_link_ops);
1497         if (err)
1498                 goto out_af;
1499
1500         return 0;
1501
1502 out_af:
1503         rtnl_af_unregister(&br_af_ops);
1504         br_mdb_uninit();
1505         return err;
1506 }
1507
1508 void br_netlink_fini(void)
1509 {
1510         br_mdb_uninit();
1511         rtnl_af_unregister(&br_af_ops);
1512         rtnl_link_unregister(&br_link_ops);
1513 }