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