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