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