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