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 | |
25c71c75 | 24 | static inline size_t br_port_info_size(void) |
25 | { | |
26 | return nla_total_size(1) /* IFLA_BRPORT_STATE */ | |
27 | + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */ | |
28 | + nla_total_size(4) /* IFLA_BRPORT_COST */ | |
29 | + nla_total_size(1) /* IFLA_BRPORT_MODE */ | |
a2e01a65 | 30 | + nla_total_size(1) /* IFLA_BRPORT_GUARD */ |
1007dd1a | 31 | + nla_total_size(1) /* IFLA_BRPORT_PROTECT */ |
3da889b6 | 32 | + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */ |
9ba18891 | 33 | + nla_total_size(1) /* IFLA_BRPORT_LEARNING */ |
867a5943 | 34 | + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */ |
25c71c75 | 35 | + 0; |
36 | } | |
37 | ||
339bf98f TG |
38 | static inline size_t br_nlmsg_size(void) |
39 | { | |
40 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
25c71c75 | 41 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
42 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
43 | + nla_total_size(4) /* IFLA_MASTER */ | |
44 | + nla_total_size(4) /* IFLA_MTU */ | |
45 | + nla_total_size(4) /* IFLA_LINK */ | |
46 | + nla_total_size(1) /* IFLA_OPERSTATE */ | |
47 | + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */ | |
48 | } | |
49 | ||
50 | static int br_port_fill_attrs(struct sk_buff *skb, | |
51 | const struct net_bridge_port *p) | |
52 | { | |
53 | u8 mode = !!(p->flags & BR_HAIRPIN_MODE); | |
54 | ||
55 | if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) || | |
56 | nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) || | |
57 | nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) || | |
a2e01a65 | 58 | nla_put_u8(skb, IFLA_BRPORT_MODE, mode) || |
1007dd1a | 59 | nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) || |
c2d3babf | 60 | nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) || |
9ba18891 | 61 | nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) || |
867a5943 | 62 | nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) || |
95850116 KP |
63 | nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) || |
64 | nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP))) | |
25c71c75 | 65 | return -EMSGSIZE; |
66 | ||
67 | return 0; | |
339bf98f TG |
68 | } |
69 | ||
36cd0ffb RP |
70 | static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start, |
71 | u16 vid_end, u16 flags) | |
72 | { | |
73 | struct bridge_vlan_info vinfo; | |
74 | ||
75 | if ((vid_end - vid_start) > 0) { | |
76 | /* add range to skb */ | |
77 | vinfo.vid = vid_start; | |
78 | vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN; | |
79 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, | |
80 | sizeof(vinfo), &vinfo)) | |
81 | goto nla_put_failure; | |
82 | ||
83 | vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN; | |
84 | ||
85 | vinfo.vid = vid_end; | |
86 | vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END; | |
87 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, | |
88 | sizeof(vinfo), &vinfo)) | |
89 | goto nla_put_failure; | |
90 | } else { | |
91 | vinfo.vid = vid_start; | |
92 | vinfo.flags = flags; | |
93 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, | |
94 | sizeof(vinfo), &vinfo)) | |
95 | goto nla_put_failure; | |
96 | } | |
97 | ||
98 | return 0; | |
99 | ||
100 | nla_put_failure: | |
101 | return -EMSGSIZE; | |
102 | } | |
103 | ||
104 | static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb, | |
105 | const struct net_port_vlans *pv) | |
106 | { | |
107 | u16 vid_range_start = 0, vid_range_end = 0; | |
108 | u16 vid_range_flags; | |
109 | u16 pvid, vid, flags; | |
110 | int err = 0; | |
111 | ||
112 | /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan | |
113 | * and mark vlan info with begin and end flags | |
114 | * if vlaninfo represents a range | |
115 | */ | |
116 | pvid = br_get_pvid(pv); | |
117 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { | |
118 | flags = 0; | |
119 | if (vid == pvid) | |
120 | flags |= BRIDGE_VLAN_INFO_PVID; | |
121 | ||
122 | if (test_bit(vid, pv->untagged_bitmap)) | |
123 | flags |= BRIDGE_VLAN_INFO_UNTAGGED; | |
124 | ||
125 | if (vid_range_start == 0) { | |
126 | goto initvars; | |
127 | } else if ((vid - vid_range_end) == 1 && | |
128 | flags == vid_range_flags) { | |
129 | vid_range_end = vid; | |
130 | continue; | |
131 | } else { | |
132 | err = br_fill_ifvlaninfo_range(skb, vid_range_start, | |
133 | vid_range_end, | |
134 | vid_range_flags); | |
135 | if (err) | |
136 | return err; | |
137 | } | |
138 | ||
139 | initvars: | |
140 | vid_range_start = vid; | |
141 | vid_range_end = vid; | |
142 | vid_range_flags = flags; | |
143 | } | |
144 | ||
145 | /* Call it once more to send any left over vlans */ | |
146 | err = br_fill_ifvlaninfo_range(skb, vid_range_start, | |
147 | vid_range_end, | |
148 | vid_range_flags); | |
149 | if (err) | |
150 | return err; | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
155 | static int br_fill_ifvlaninfo(struct sk_buff *skb, | |
156 | const struct net_port_vlans *pv) | |
157 | { | |
158 | struct bridge_vlan_info vinfo; | |
159 | u16 pvid, vid; | |
160 | ||
161 | pvid = br_get_pvid(pv); | |
162 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { | |
163 | vinfo.vid = vid; | |
164 | vinfo.flags = 0; | |
165 | if (vid == pvid) | |
166 | vinfo.flags |= BRIDGE_VLAN_INFO_PVID; | |
167 | ||
168 | if (test_bit(vid, pv->untagged_bitmap)) | |
169 | vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED; | |
170 | ||
171 | if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO, | |
172 | sizeof(vinfo), &vinfo)) | |
173 | goto nla_put_failure; | |
174 | } | |
175 | ||
176 | return 0; | |
177 | ||
178 | nla_put_failure: | |
179 | return -EMSGSIZE; | |
180 | } | |
181 | ||
11dc1f36 SH |
182 | /* |
183 | * Create one netlink message for one interface | |
184 | * Contains port and master info as well as carrier and bridge state. | |
185 | */ | |
6cbdceeb VY |
186 | static int br_fill_ifinfo(struct sk_buff *skb, |
187 | const struct net_bridge_port *port, | |
188 | u32 pid, u32 seq, int event, unsigned int flags, | |
189 | u32 filter_mask, const struct net_device *dev) | |
11dc1f36 | 190 | { |
6cbdceeb | 191 | const struct net_bridge *br; |
74685962 | 192 | struct ifinfomsg *hdr; |
11dc1f36 | 193 | struct nlmsghdr *nlh; |
11dc1f36 | 194 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
11dc1f36 | 195 | |
6cbdceeb VY |
196 | if (port) |
197 | br = port->br; | |
198 | else | |
199 | br = netdev_priv(dev); | |
200 | ||
28a16c97 | 201 | br_debug(br, "br_fill_info event %d port %s master %s\n", |
202 | event, dev->name, br->dev->name); | |
11dc1f36 | 203 | |
74685962 TG |
204 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); |
205 | if (nlh == NULL) | |
26932566 | 206 | return -EMSGSIZE; |
11dc1f36 | 207 | |
74685962 TG |
208 | hdr = nlmsg_data(nlh); |
209 | hdr->ifi_family = AF_BRIDGE; | |
210 | hdr->__ifi_pad = 0; | |
211 | hdr->ifi_type = dev->type; | |
212 | hdr->ifi_index = dev->ifindex; | |
213 | hdr->ifi_flags = dev_get_flags(dev); | |
214 | hdr->ifi_change = 0; | |
11dc1f36 | 215 | |
2eb812e6 DM |
216 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
217 | nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) || | |
218 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
219 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || | |
220 | (dev->addr_len && | |
221 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || | |
222 | (dev->ifindex != dev->iflink && | |
25c71c75 | 223 | nla_put_u32(skb, IFLA_LINK, dev->iflink))) |
2eb812e6 | 224 | goto nla_put_failure; |
25c71c75 | 225 | |
6cbdceeb | 226 | if (event == RTM_NEWLINK && port) { |
25c71c75 | 227 | struct nlattr *nest |
228 | = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); | |
229 | ||
230 | if (nest == NULL || br_port_fill_attrs(skb, port) < 0) | |
231 | goto nla_put_failure; | |
232 | nla_nest_end(skb, nest); | |
233 | } | |
234 | ||
6cbdceeb | 235 | /* Check if the VID information is requested */ |
36cd0ffb RP |
236 | if ((filter_mask & RTEXT_FILTER_BRVLAN) || |
237 | (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) { | |
6cbdceeb | 238 | const struct net_port_vlans *pv; |
36cd0ffb RP |
239 | struct nlattr *af; |
240 | int err; | |
6cbdceeb VY |
241 | |
242 | if (port) | |
243 | pv = nbp_get_vlan_info(port); | |
244 | else | |
245 | pv = br_get_vlan_info(br); | |
246 | ||
ef40b7ef | 247 | if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) |
6cbdceeb VY |
248 | goto done; |
249 | ||
250 | af = nla_nest_start(skb, IFLA_AF_SPEC); | |
251 | if (!af) | |
252 | goto nla_put_failure; | |
253 | ||
36cd0ffb RP |
254 | if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) |
255 | err = br_fill_ifvlaninfo_compressed(skb, pv); | |
256 | else | |
257 | err = br_fill_ifvlaninfo(skb, pv); | |
258 | if (err) | |
259 | goto nla_put_failure; | |
6cbdceeb VY |
260 | nla_nest_end(skb, af); |
261 | } | |
262 | ||
263 | done: | |
74685962 | 264 | return nlmsg_end(skb, nlh); |
11dc1f36 | 265 | |
74685962 | 266 | nla_put_failure: |
26932566 PM |
267 | nlmsg_cancel(skb, nlh); |
268 | return -EMSGSIZE; | |
11dc1f36 SH |
269 | } |
270 | ||
271 | /* | |
272 | * Notify listeners of a change in port information | |
273 | */ | |
274 | void br_ifinfo_notify(int event, struct net_bridge_port *port) | |
275 | { | |
407af329 | 276 | struct net *net; |
11dc1f36 | 277 | struct sk_buff *skb; |
280a306c | 278 | int err = -ENOBUFS; |
11dc1f36 | 279 | |
407af329 VY |
280 | if (!port) |
281 | return; | |
282 | ||
283 | net = dev_net(port->dev); | |
28a16c97 | 284 | br_debug(port->br, "port %u(%s) event %d\n", |
95c96174 | 285 | (unsigned int)port->port_no, port->dev->name, event); |
28a16c97 | 286 | |
339bf98f | 287 | skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC); |
280a306c TG |
288 | if (skb == NULL) |
289 | goto errout; | |
290 | ||
6cbdceeb | 291 | err = br_fill_ifinfo(skb, port, 0, 0, event, 0, 0, port->dev); |
26932566 PM |
292 | if (err < 0) { |
293 | /* -EMSGSIZE implies BUG in br_nlmsg_size() */ | |
294 | WARN_ON(err == -EMSGSIZE); | |
295 | kfree_skb(skb); | |
296 | goto errout; | |
297 | } | |
1ce85fe4 PNA |
298 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
299 | return; | |
280a306c | 300 | errout: |
87e823b3 | 301 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
11dc1f36 SH |
302 | } |
303 | ||
407af329 | 304 | |
11dc1f36 SH |
305 | /* |
306 | * Dump information about all ports, in response to GETLINK | |
307 | */ | |
e5a55a89 | 308 | int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
6cbdceeb | 309 | struct net_device *dev, u32 filter_mask) |
11dc1f36 | 310 | { |
e5a55a89 | 311 | int err = 0; |
1fb1754a | 312 | struct net_bridge_port *port = br_port_get_rtnl(dev); |
e5a55a89 | 313 | |
36cd0ffb RP |
314 | if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) && |
315 | !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) | |
e5a55a89 | 316 | goto out; |
11dc1f36 | 317 | |
6cbdceeb VY |
318 | err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI, |
319 | filter_mask, dev); | |
e5a55a89 JF |
320 | out: |
321 | return err; | |
11dc1f36 SH |
322 | } |
323 | ||
bdced7ef RP |
324 | static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p, |
325 | int cmd, struct bridge_vlan_info *vinfo) | |
326 | { | |
327 | int err = 0; | |
328 | ||
329 | switch (cmd) { | |
330 | case RTM_SETLINK: | |
331 | if (p) { | |
332 | err = nbp_vlan_add(p, vinfo->vid, vinfo->flags); | |
333 | if (err) | |
334 | break; | |
335 | ||
336 | if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER) | |
337 | err = br_vlan_add(p->br, vinfo->vid, | |
338 | vinfo->flags); | |
339 | } else { | |
340 | err = br_vlan_add(br, vinfo->vid, vinfo->flags); | |
341 | } | |
342 | break; | |
343 | ||
344 | case RTM_DELLINK: | |
345 | if (p) { | |
346 | nbp_vlan_delete(p, vinfo->vid); | |
347 | if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER) | |
348 | br_vlan_delete(p->br, vinfo->vid); | |
349 | } else { | |
350 | br_vlan_delete(br, vinfo->vid); | |
351 | } | |
352 | break; | |
353 | } | |
354 | ||
355 | return err; | |
356 | } | |
407af329 VY |
357 | |
358 | static int br_afspec(struct net_bridge *br, | |
359 | struct net_bridge_port *p, | |
360 | struct nlattr *af_spec, | |
361 | int cmd) | |
362 | { | |
bdced7ef RP |
363 | struct bridge_vlan_info *vinfo_start = NULL; |
364 | struct bridge_vlan_info *vinfo = NULL; | |
365 | struct nlattr *attr; | |
407af329 | 366 | int err = 0; |
bdced7ef | 367 | int rem; |
407af329 | 368 | |
bdced7ef RP |
369 | nla_for_each_nested(attr, af_spec, rem) { |
370 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) | |
371 | continue; | |
372 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) | |
373 | return -EINVAL; | |
374 | vinfo = nla_data(attr); | |
375 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { | |
376 | if (vinfo_start) | |
377 | return -EINVAL; | |
378 | vinfo_start = vinfo; | |
379 | continue; | |
380 | } | |
407af329 | 381 | |
bdced7ef RP |
382 | if (vinfo_start) { |
383 | struct bridge_vlan_info tmp_vinfo; | |
384 | int v; | |
407af329 | 385 | |
bdced7ef RP |
386 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END)) |
387 | return -EINVAL; | |
407af329 | 388 | |
bdced7ef RP |
389 | if (vinfo->vid <= vinfo_start->vid) |
390 | return -EINVAL; | |
391 | ||
392 | memcpy(&tmp_vinfo, vinfo_start, | |
393 | sizeof(struct bridge_vlan_info)); | |
407af329 | 394 | |
bdced7ef RP |
395 | for (v = vinfo_start->vid; v <= vinfo->vid; v++) { |
396 | tmp_vinfo.vid = v; | |
397 | err = br_vlan_info(br, p, cmd, &tmp_vinfo); | |
407af329 VY |
398 | if (err) |
399 | break; | |
bdced7ef RP |
400 | } |
401 | vinfo_start = NULL; | |
402 | } else { | |
403 | err = br_vlan_info(br, p, cmd, vinfo); | |
407af329 | 404 | } |
bdced7ef RP |
405 | if (err) |
406 | break; | |
407af329 VY |
407 | } |
408 | ||
409 | return err; | |
410 | } | |
411 | ||
3ac636b8 | 412 | static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = { |
25c71c75 | 413 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, |
414 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, | |
415 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, | |
416 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, | |
a2e01a65 | 417 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
1007dd1a | 418 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
6f705d8c | 419 | [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 }, |
9ba18891 | 420 | [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, |
867a5943 | 421 | [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, |
25c71c75 | 422 | }; |
423 | ||
424 | /* Change the state of the port and notify spanning tree */ | |
425 | static int br_set_port_state(struct net_bridge_port *p, u8 state) | |
426 | { | |
427 | if (state > BR_STATE_BLOCKING) | |
428 | return -EINVAL; | |
429 | ||
430 | /* if kernel STP is running, don't allow changes */ | |
431 | if (p->br->stp_enabled == BR_KERNEL_STP) | |
432 | return -EBUSY; | |
433 | ||
576eb625 | 434 | /* if device is not up, change is not allowed |
435 | * if link is not present, only allowable state is disabled | |
436 | */ | |
25c71c75 | 437 | if (!netif_running(p->dev) || |
576eb625 | 438 | (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED)) |
25c71c75 | 439 | return -ENETDOWN; |
440 | ||
775dd692 | 441 | br_set_state(p, state); |
25c71c75 | 442 | br_log_state(p); |
443 | br_port_state_selection(p->br); | |
444 | return 0; | |
445 | } | |
446 | ||
447 | /* Set/clear or port flags based on attribute */ | |
448 | static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[], | |
449 | int attrtype, unsigned long mask) | |
450 | { | |
451 | if (tb[attrtype]) { | |
452 | u8 flag = nla_get_u8(tb[attrtype]); | |
453 | if (flag) | |
454 | p->flags |= mask; | |
455 | else | |
456 | p->flags &= ~mask; | |
457 | } | |
458 | } | |
459 | ||
460 | /* Process bridge protocol info on port */ | |
461 | static int br_setport(struct net_bridge_port *p, struct nlattr *tb[]) | |
462 | { | |
463 | int err; | |
e028e4b8 | 464 | unsigned long old_flags = p->flags; |
25c71c75 | 465 | |
466 | br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE); | |
a2e01a65 | 467 | br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD); |
c2d3babf | 468 | br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE); |
3d84fa98 | 469 | br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK); |
9ba18891 | 470 | br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING); |
867a5943 | 471 | br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD); |
95850116 | 472 | br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP); |
25c71c75 | 473 | |
474 | if (tb[IFLA_BRPORT_COST]) { | |
475 | err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST])); | |
476 | if (err) | |
477 | return err; | |
478 | } | |
479 | ||
480 | if (tb[IFLA_BRPORT_PRIORITY]) { | |
481 | err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY])); | |
482 | if (err) | |
483 | return err; | |
484 | } | |
485 | ||
486 | if (tb[IFLA_BRPORT_STATE]) { | |
487 | err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE])); | |
488 | if (err) | |
489 | return err; | |
490 | } | |
e028e4b8 VY |
491 | |
492 | br_port_flags_change(p, old_flags ^ p->flags); | |
25c71c75 | 493 | return 0; |
494 | } | |
495 | ||
496 | /* Change state and parameters on port. */ | |
e5a55a89 | 497 | int br_setlink(struct net_device *dev, struct nlmsghdr *nlh) |
11dc1f36 | 498 | { |
74685962 | 499 | struct nlattr *protinfo; |
407af329 | 500 | struct nlattr *afspec; |
11dc1f36 | 501 | struct net_bridge_port *p; |
2062cc20 | 502 | struct nlattr *tb[IFLA_BRPORT_MAX + 1]; |
7b99a993 | 503 | int err = 0; |
11dc1f36 | 504 | |
c60ee67f H |
505 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO); |
506 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); | |
407af329 | 507 | if (!protinfo && !afspec) |
25c71c75 | 508 | return 0; |
11dc1f36 | 509 | |
ec1e5610 | 510 | p = br_port_get_rtnl(dev); |
407af329 | 511 | /* We want to accept dev as bridge itself if the AF_SPEC |
8e3bff96 | 512 | * is set to see if someone is setting vlan info on the bridge |
407af329 | 513 | */ |
7b99a993 | 514 | if (!p && !afspec) |
b5ed54e9 | 515 | return -EINVAL; |
11dc1f36 | 516 | |
407af329 VY |
517 | if (p && protinfo) { |
518 | if (protinfo->nla_type & NLA_F_NESTED) { | |
519 | err = nla_parse_nested(tb, IFLA_BRPORT_MAX, | |
3ac636b8 | 520 | protinfo, br_port_policy); |
407af329 VY |
521 | if (err) |
522 | return err; | |
523 | ||
524 | spin_lock_bh(&p->br->lock); | |
525 | err = br_setport(p, tb); | |
526 | spin_unlock_bh(&p->br->lock); | |
527 | } else { | |
8e3bff96 | 528 | /* Binary compatibility with old RSTP */ |
407af329 VY |
529 | if (nla_len(protinfo) < sizeof(u8)) |
530 | return -EINVAL; | |
531 | ||
532 | spin_lock_bh(&p->br->lock); | |
533 | err = br_set_port_state(p, nla_get_u8(protinfo)); | |
534 | spin_unlock_bh(&p->br->lock); | |
535 | } | |
25c71c75 | 536 | if (err) |
407af329 VY |
537 | goto out; |
538 | } | |
11dc1f36 | 539 | |
407af329 VY |
540 | if (afspec) { |
541 | err = br_afspec((struct net_bridge *)netdev_priv(dev), p, | |
542 | afspec, RTM_SETLINK); | |
25c71c75 | 543 | } |
b03b6dd5 | 544 | |
25c71c75 | 545 | if (err == 0) |
546 | br_ifinfo_notify(RTM_NEWLINK, p); | |
b03b6dd5 | 547 | |
407af329 | 548 | out: |
25c71c75 | 549 | return err; |
11dc1f36 SH |
550 | } |
551 | ||
407af329 VY |
552 | /* Delete port information */ |
553 | int br_dellink(struct net_device *dev, struct nlmsghdr *nlh) | |
554 | { | |
407af329 VY |
555 | struct nlattr *afspec; |
556 | struct net_bridge_port *p; | |
557 | int err; | |
558 | ||
c60ee67f | 559 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
407af329 VY |
560 | if (!afspec) |
561 | return 0; | |
562 | ||
563 | p = br_port_get_rtnl(dev); | |
564 | /* We want to accept dev as bridge itself as well */ | |
565 | if (!p && !(dev->priv_flags & IFF_EBRIDGE)) | |
566 | return -EINVAL; | |
567 | ||
568 | err = br_afspec((struct net_bridge *)netdev_priv(dev), p, | |
569 | afspec, RTM_DELLINK); | |
570 | ||
571 | return err; | |
572 | } | |
bb900b27 | 573 | static int br_validate(struct nlattr *tb[], struct nlattr *data[]) |
574 | { | |
575 | if (tb[IFLA_ADDRESS]) { | |
576 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
577 | return -EINVAL; | |
578 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
579 | return -EADDRNOTAVAIL; | |
580 | } | |
581 | ||
582 | return 0; | |
583 | } | |
584 | ||
30313a3d TM |
585 | static int br_dev_newlink(struct net *src_net, struct net_device *dev, |
586 | struct nlattr *tb[], struct nlattr *data[]) | |
587 | { | |
588 | struct net_bridge *br = netdev_priv(dev); | |
589 | ||
590 | if (tb[IFLA_ADDRESS]) { | |
591 | spin_lock_bh(&br->lock); | |
592 | br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS])); | |
593 | spin_unlock_bh(&br->lock); | |
594 | } | |
595 | ||
596 | return register_netdevice(dev); | |
597 | } | |
598 | ||
3ac636b8 JP |
599 | static int br_port_slave_changelink(struct net_device *brdev, |
600 | struct net_device *dev, | |
601 | struct nlattr *tb[], | |
602 | struct nlattr *data[]) | |
603 | { | |
604 | if (!data) | |
605 | return 0; | |
606 | return br_setport(br_port_get_rtnl(dev), data); | |
607 | } | |
608 | ||
ced8283f JP |
609 | static int br_port_fill_slave_info(struct sk_buff *skb, |
610 | const struct net_device *brdev, | |
611 | const struct net_device *dev) | |
612 | { | |
613 | return br_port_fill_attrs(skb, br_port_get_rtnl(dev)); | |
614 | } | |
615 | ||
616 | static size_t br_port_get_slave_size(const struct net_device *brdev, | |
617 | const struct net_device *dev) | |
618 | { | |
619 | return br_port_info_size(); | |
620 | } | |
621 | ||
13323516 JP |
622 | static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = { |
623 | [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 }, | |
624 | [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 }, | |
625 | [IFLA_BR_MAX_AGE] = { .type = NLA_U32 }, | |
626 | }; | |
627 | ||
628 | static int br_changelink(struct net_device *brdev, struct nlattr *tb[], | |
629 | struct nlattr *data[]) | |
630 | { | |
631 | struct net_bridge *br = netdev_priv(brdev); | |
632 | int err; | |
633 | ||
634 | if (!data) | |
635 | return 0; | |
636 | ||
637 | if (data[IFLA_BR_FORWARD_DELAY]) { | |
638 | err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY])); | |
639 | if (err) | |
640 | return err; | |
641 | } | |
642 | ||
643 | if (data[IFLA_BR_HELLO_TIME]) { | |
644 | err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME])); | |
645 | if (err) | |
646 | return err; | |
647 | } | |
648 | ||
649 | if (data[IFLA_BR_MAX_AGE]) { | |
650 | err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE])); | |
651 | if (err) | |
652 | return err; | |
653 | } | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
e5c3ea5c JP |
658 | static size_t br_get_size(const struct net_device *brdev) |
659 | { | |
660 | return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */ | |
661 | nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */ | |
662 | nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */ | |
663 | 0; | |
664 | } | |
665 | ||
666 | static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev) | |
667 | { | |
668 | struct net_bridge *br = netdev_priv(brdev); | |
669 | u32 forward_delay = jiffies_to_clock_t(br->forward_delay); | |
670 | u32 hello_time = jiffies_to_clock_t(br->hello_time); | |
671 | u32 age_time = jiffies_to_clock_t(br->max_age); | |
672 | ||
673 | if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) || | |
674 | nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) || | |
675 | nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time)) | |
676 | return -EMSGSIZE; | |
677 | ||
678 | return 0; | |
679 | } | |
680 | ||
6cbdceeb VY |
681 | static size_t br_get_link_af_size(const struct net_device *dev) |
682 | { | |
683 | struct net_port_vlans *pv; | |
684 | ||
685 | if (br_port_exists(dev)) | |
1fb1754a | 686 | pv = nbp_get_vlan_info(br_port_get_rtnl(dev)); |
6cbdceeb VY |
687 | else if (dev->priv_flags & IFF_EBRIDGE) |
688 | pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev)); | |
689 | else | |
690 | return 0; | |
691 | ||
692 | if (!pv) | |
693 | return 0; | |
694 | ||
695 | /* Each VLAN is returned in bridge_vlan_info along with flags */ | |
696 | return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info)); | |
697 | } | |
698 | ||
15004cab | 699 | static struct rtnl_af_ops br_af_ops = { |
6cbdceeb VY |
700 | .family = AF_BRIDGE, |
701 | .get_link_af_size = br_get_link_af_size, | |
702 | }; | |
703 | ||
149ddd83 | 704 | struct rtnl_link_ops br_link_ops __read_mostly = { |
ced8283f JP |
705 | .kind = "bridge", |
706 | .priv_size = sizeof(struct net_bridge), | |
707 | .setup = br_dev_setup, | |
13323516 JP |
708 | .maxtype = IFLA_BRPORT_MAX, |
709 | .policy = br_policy, | |
ced8283f JP |
710 | .validate = br_validate, |
711 | .newlink = br_dev_newlink, | |
13323516 | 712 | .changelink = br_changelink, |
ced8283f | 713 | .dellink = br_dev_delete, |
e5c3ea5c JP |
714 | .get_size = br_get_size, |
715 | .fill_info = br_fill_info, | |
3ac636b8 JP |
716 | |
717 | .slave_maxtype = IFLA_BRPORT_MAX, | |
718 | .slave_policy = br_port_policy, | |
719 | .slave_changelink = br_port_slave_changelink, | |
ced8283f JP |
720 | .get_slave_size = br_port_get_slave_size, |
721 | .fill_slave_info = br_port_fill_slave_info, | |
bb900b27 | 722 | }; |
11dc1f36 | 723 | |
32fe21c0 | 724 | int __init br_netlink_init(void) |
11dc1f36 | 725 | { |
3ec8e9f0 VY |
726 | int err; |
727 | ||
728 | br_mdb_init(); | |
3678a9d8 | 729 | rtnl_af_register(&br_af_ops); |
3ec8e9f0 | 730 | |
6cbdceeb VY |
731 | err = rtnl_link_register(&br_link_ops); |
732 | if (err) | |
733 | goto out_af; | |
734 | ||
3ec8e9f0 | 735 | return 0; |
6cbdceeb VY |
736 | |
737 | out_af: | |
738 | rtnl_af_unregister(&br_af_ops); | |
3ec8e9f0 VY |
739 | br_mdb_uninit(); |
740 | return err; | |
11dc1f36 SH |
741 | } |
742 | ||
34666d46 | 743 | void br_netlink_fini(void) |
11dc1f36 | 744 | { |
3ec8e9f0 | 745 | br_mdb_uninit(); |
6cbdceeb | 746 | rtnl_af_unregister(&br_af_ops); |
bb900b27 | 747 | rtnl_link_unregister(&br_link_ops); |
11dc1f36 | 748 | } |