]>
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 */ |
25c71c75 | 32 | + 0; |
33 | } | |
34 | ||
339bf98f TG |
35 | static inline size_t br_nlmsg_size(void) |
36 | { | |
37 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
25c71c75 | 38 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
39 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
40 | + nla_total_size(4) /* IFLA_MASTER */ | |
41 | + nla_total_size(4) /* IFLA_MTU */ | |
42 | + nla_total_size(4) /* IFLA_LINK */ | |
43 | + nla_total_size(1) /* IFLA_OPERSTATE */ | |
44 | + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */ | |
45 | } | |
46 | ||
47 | static int br_port_fill_attrs(struct sk_buff *skb, | |
48 | const struct net_bridge_port *p) | |
49 | { | |
50 | u8 mode = !!(p->flags & BR_HAIRPIN_MODE); | |
51 | ||
52 | if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) || | |
53 | nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) || | |
54 | nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) || | |
a2e01a65 | 55 | nla_put_u8(skb, IFLA_BRPORT_MODE, mode) || |
1007dd1a | 56 | nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) || |
c2d3babf DM |
57 | nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) || |
58 | nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE))) | |
25c71c75 | 59 | return -EMSGSIZE; |
60 | ||
61 | return 0; | |
339bf98f TG |
62 | } |
63 | ||
11dc1f36 SH |
64 | /* |
65 | * Create one netlink message for one interface | |
66 | * Contains port and master info as well as carrier and bridge state. | |
67 | */ | |
68 | static int br_fill_ifinfo(struct sk_buff *skb, const struct net_bridge_port *port, | |
69 | u32 pid, u32 seq, int event, unsigned int flags) | |
70 | { | |
71 | const struct net_bridge *br = port->br; | |
72 | const struct net_device *dev = port->dev; | |
74685962 | 73 | struct ifinfomsg *hdr; |
11dc1f36 | 74 | struct nlmsghdr *nlh; |
11dc1f36 | 75 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
11dc1f36 | 76 | |
28a16c97 | 77 | br_debug(br, "br_fill_info event %d port %s master %s\n", |
78 | event, dev->name, br->dev->name); | |
11dc1f36 | 79 | |
74685962 TG |
80 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); |
81 | if (nlh == NULL) | |
26932566 | 82 | return -EMSGSIZE; |
11dc1f36 | 83 | |
74685962 TG |
84 | hdr = nlmsg_data(nlh); |
85 | hdr->ifi_family = AF_BRIDGE; | |
86 | hdr->__ifi_pad = 0; | |
87 | hdr->ifi_type = dev->type; | |
88 | hdr->ifi_index = dev->ifindex; | |
89 | hdr->ifi_flags = dev_get_flags(dev); | |
90 | hdr->ifi_change = 0; | |
11dc1f36 | 91 | |
2eb812e6 DM |
92 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
93 | nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) || | |
94 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
95 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || | |
96 | (dev->addr_len && | |
97 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || | |
98 | (dev->ifindex != dev->iflink && | |
25c71c75 | 99 | nla_put_u32(skb, IFLA_LINK, dev->iflink))) |
2eb812e6 | 100 | goto nla_put_failure; |
25c71c75 | 101 | |
102 | if (event == RTM_NEWLINK) { | |
103 | struct nlattr *nest | |
104 | = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); | |
105 | ||
106 | if (nest == NULL || br_port_fill_attrs(skb, port) < 0) | |
107 | goto nla_put_failure; | |
108 | nla_nest_end(skb, nest); | |
109 | } | |
110 | ||
74685962 | 111 | return nlmsg_end(skb, nlh); |
11dc1f36 | 112 | |
74685962 | 113 | nla_put_failure: |
26932566 PM |
114 | nlmsg_cancel(skb, nlh); |
115 | return -EMSGSIZE; | |
11dc1f36 SH |
116 | } |
117 | ||
118 | /* | |
119 | * Notify listeners of a change in port information | |
120 | */ | |
121 | void br_ifinfo_notify(int event, struct net_bridge_port *port) | |
122 | { | |
407af329 | 123 | struct net *net; |
11dc1f36 | 124 | struct sk_buff *skb; |
280a306c | 125 | int err = -ENOBUFS; |
11dc1f36 | 126 | |
407af329 VY |
127 | if (!port) |
128 | return; | |
129 | ||
130 | net = dev_net(port->dev); | |
28a16c97 | 131 | br_debug(port->br, "port %u(%s) event %d\n", |
95c96174 | 132 | (unsigned int)port->port_no, port->dev->name, event); |
28a16c97 | 133 | |
339bf98f | 134 | skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC); |
280a306c TG |
135 | if (skb == NULL) |
136 | goto errout; | |
137 | ||
138 | err = br_fill_ifinfo(skb, port, 0, 0, event, 0); | |
26932566 PM |
139 | if (err < 0) { |
140 | /* -EMSGSIZE implies BUG in br_nlmsg_size() */ | |
141 | WARN_ON(err == -EMSGSIZE); | |
142 | kfree_skb(skb); | |
143 | goto errout; | |
144 | } | |
1ce85fe4 PNA |
145 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
146 | return; | |
280a306c | 147 | errout: |
bea1b42e | 148 | if (err < 0) |
4aa678ba | 149 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
11dc1f36 SH |
150 | } |
151 | ||
407af329 | 152 | |
11dc1f36 SH |
153 | /* |
154 | * Dump information about all ports, in response to GETLINK | |
155 | */ | |
e5a55a89 JF |
156 | int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
157 | struct net_device *dev) | |
11dc1f36 | 158 | { |
e5a55a89 JF |
159 | int err = 0; |
160 | struct net_bridge_port *port = br_port_get_rcu(dev); | |
161 | ||
162 | /* not a bridge port */ | |
163 | if (!port) | |
164 | goto out; | |
11dc1f36 | 165 | |
e5a55a89 JF |
166 | err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI); |
167 | out: | |
168 | return err; | |
11dc1f36 SH |
169 | } |
170 | ||
407af329 VY |
171 | const struct nla_policy ifla_br_policy[IFLA_MAX+1] = { |
172 | [IFLA_BRIDGE_FLAGS] = { .type = NLA_U16 }, | |
173 | [IFLA_BRIDGE_MODE] = { .type = NLA_U16 }, | |
174 | [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY, | |
175 | .len = sizeof(struct bridge_vlan_info), }, | |
176 | }; | |
177 | ||
178 | static int br_afspec(struct net_bridge *br, | |
179 | struct net_bridge_port *p, | |
180 | struct nlattr *af_spec, | |
181 | int cmd) | |
182 | { | |
183 | struct nlattr *tb[IFLA_BRIDGE_MAX+1]; | |
184 | int err = 0; | |
185 | ||
186 | err = nla_parse_nested(tb, IFLA_BRIDGE_MAX, af_spec, ifla_br_policy); | |
187 | if (err) | |
188 | return err; | |
189 | ||
190 | if (tb[IFLA_BRIDGE_VLAN_INFO]) { | |
191 | struct bridge_vlan_info *vinfo; | |
192 | ||
193 | vinfo = nla_data(tb[IFLA_BRIDGE_VLAN_INFO]); | |
194 | ||
195 | if (vinfo->vid >= VLAN_N_VID) | |
196 | return -EINVAL; | |
197 | ||
198 | switch (cmd) { | |
199 | case RTM_SETLINK: | |
200 | if (p) { | |
201 | err = nbp_vlan_add(p, vinfo->vid); | |
202 | if (err) | |
203 | break; | |
204 | ||
205 | if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER) | |
206 | err = br_vlan_add(p->br, vinfo->vid); | |
207 | } else | |
208 | err = br_vlan_add(br, vinfo->vid); | |
209 | ||
210 | if (err) | |
211 | break; | |
212 | ||
213 | break; | |
214 | ||
215 | case RTM_DELLINK: | |
216 | if (p) { | |
217 | nbp_vlan_delete(p, vinfo->vid); | |
218 | if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER) | |
219 | br_vlan_delete(p->br, vinfo->vid); | |
220 | } else | |
221 | br_vlan_delete(br, vinfo->vid); | |
222 | break; | |
223 | } | |
224 | } | |
225 | ||
226 | return err; | |
227 | } | |
228 | ||
25c71c75 | 229 | static const struct nla_policy ifla_brport_policy[IFLA_BRPORT_MAX + 1] = { |
230 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, | |
231 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, | |
232 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, | |
233 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, | |
a2e01a65 | 234 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
1007dd1a | 235 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
25c71c75 | 236 | }; |
237 | ||
238 | /* Change the state of the port and notify spanning tree */ | |
239 | static int br_set_port_state(struct net_bridge_port *p, u8 state) | |
240 | { | |
241 | if (state > BR_STATE_BLOCKING) | |
242 | return -EINVAL; | |
243 | ||
244 | /* if kernel STP is running, don't allow changes */ | |
245 | if (p->br->stp_enabled == BR_KERNEL_STP) | |
246 | return -EBUSY; | |
247 | ||
576eb625 | 248 | /* if device is not up, change is not allowed |
249 | * if link is not present, only allowable state is disabled | |
250 | */ | |
25c71c75 | 251 | if (!netif_running(p->dev) || |
576eb625 | 252 | (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED)) |
25c71c75 | 253 | return -ENETDOWN; |
254 | ||
255 | p->state = state; | |
256 | br_log_state(p); | |
257 | br_port_state_selection(p->br); | |
258 | return 0; | |
259 | } | |
260 | ||
261 | /* Set/clear or port flags based on attribute */ | |
262 | static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[], | |
263 | int attrtype, unsigned long mask) | |
264 | { | |
265 | if (tb[attrtype]) { | |
266 | u8 flag = nla_get_u8(tb[attrtype]); | |
267 | if (flag) | |
268 | p->flags |= mask; | |
269 | else | |
270 | p->flags &= ~mask; | |
271 | } | |
272 | } | |
273 | ||
274 | /* Process bridge protocol info on port */ | |
275 | static int br_setport(struct net_bridge_port *p, struct nlattr *tb[]) | |
276 | { | |
277 | int err; | |
278 | ||
279 | br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE); | |
a2e01a65 | 280 | br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD); |
c2d3babf | 281 | br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE); |
25c71c75 | 282 | |
283 | if (tb[IFLA_BRPORT_COST]) { | |
284 | err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST])); | |
285 | if (err) | |
286 | return err; | |
287 | } | |
288 | ||
289 | if (tb[IFLA_BRPORT_PRIORITY]) { | |
290 | err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY])); | |
291 | if (err) | |
292 | return err; | |
293 | } | |
294 | ||
295 | if (tb[IFLA_BRPORT_STATE]) { | |
296 | err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE])); | |
297 | if (err) | |
298 | return err; | |
299 | } | |
300 | return 0; | |
301 | } | |
302 | ||
303 | /* Change state and parameters on port. */ | |
e5a55a89 | 304 | int br_setlink(struct net_device *dev, struct nlmsghdr *nlh) |
11dc1f36 | 305 | { |
74685962 TG |
306 | struct ifinfomsg *ifm; |
307 | struct nlattr *protinfo; | |
407af329 | 308 | struct nlattr *afspec; |
11dc1f36 | 309 | struct net_bridge_port *p; |
2062cc20 | 310 | struct nlattr *tb[IFLA_BRPORT_MAX + 1]; |
25c71c75 | 311 | int err; |
11dc1f36 | 312 | |
74685962 | 313 | ifm = nlmsg_data(nlh); |
11dc1f36 | 314 | |
74685962 | 315 | protinfo = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_PROTINFO); |
407af329 VY |
316 | afspec = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_AF_SPEC); |
317 | if (!protinfo && !afspec) | |
25c71c75 | 318 | return 0; |
11dc1f36 | 319 | |
ec1e5610 | 320 | p = br_port_get_rtnl(dev); |
407af329 VY |
321 | /* We want to accept dev as bridge itself if the AF_SPEC |
322 | * is set to see if someone is setting vlan info on the brigde | |
323 | */ | |
324 | if (!p && ((dev->priv_flags & IFF_EBRIDGE) && !afspec)) | |
b5ed54e9 | 325 | return -EINVAL; |
11dc1f36 | 326 | |
407af329 VY |
327 | if (p && protinfo) { |
328 | if (protinfo->nla_type & NLA_F_NESTED) { | |
329 | err = nla_parse_nested(tb, IFLA_BRPORT_MAX, | |
330 | protinfo, ifla_brport_policy); | |
331 | if (err) | |
332 | return err; | |
333 | ||
334 | spin_lock_bh(&p->br->lock); | |
335 | err = br_setport(p, tb); | |
336 | spin_unlock_bh(&p->br->lock); | |
337 | } else { | |
338 | /* Binary compatability with old RSTP */ | |
339 | if (nla_len(protinfo) < sizeof(u8)) | |
340 | return -EINVAL; | |
341 | ||
342 | spin_lock_bh(&p->br->lock); | |
343 | err = br_set_port_state(p, nla_get_u8(protinfo)); | |
344 | spin_unlock_bh(&p->br->lock); | |
345 | } | |
25c71c75 | 346 | if (err) |
407af329 VY |
347 | goto out; |
348 | } | |
11dc1f36 | 349 | |
407af329 VY |
350 | if (afspec) { |
351 | err = br_afspec((struct net_bridge *)netdev_priv(dev), p, | |
352 | afspec, RTM_SETLINK); | |
25c71c75 | 353 | } |
b03b6dd5 | 354 | |
25c71c75 | 355 | if (err == 0) |
356 | br_ifinfo_notify(RTM_NEWLINK, p); | |
b03b6dd5 | 357 | |
407af329 | 358 | out: |
25c71c75 | 359 | return err; |
11dc1f36 SH |
360 | } |
361 | ||
407af329 VY |
362 | /* Delete port information */ |
363 | int br_dellink(struct net_device *dev, struct nlmsghdr *nlh) | |
364 | { | |
365 | struct ifinfomsg *ifm; | |
366 | struct nlattr *afspec; | |
367 | struct net_bridge_port *p; | |
368 | int err; | |
369 | ||
370 | ifm = nlmsg_data(nlh); | |
371 | ||
372 | afspec = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_AF_SPEC); | |
373 | if (!afspec) | |
374 | return 0; | |
375 | ||
376 | p = br_port_get_rtnl(dev); | |
377 | /* We want to accept dev as bridge itself as well */ | |
378 | if (!p && !(dev->priv_flags & IFF_EBRIDGE)) | |
379 | return -EINVAL; | |
380 | ||
381 | err = br_afspec((struct net_bridge *)netdev_priv(dev), p, | |
382 | afspec, RTM_DELLINK); | |
383 | ||
384 | return err; | |
385 | } | |
bb900b27 | 386 | static int br_validate(struct nlattr *tb[], struct nlattr *data[]) |
387 | { | |
388 | if (tb[IFLA_ADDRESS]) { | |
389 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
390 | return -EINVAL; | |
391 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
392 | return -EADDRNOTAVAIL; | |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
149ddd83 | 398 | struct rtnl_link_ops br_link_ops __read_mostly = { |
bb900b27 | 399 | .kind = "bridge", |
400 | .priv_size = sizeof(struct net_bridge), | |
401 | .setup = br_dev_setup, | |
402 | .validate = br_validate, | |
1ce5cce8 | 403 | .dellink = br_dev_delete, |
bb900b27 | 404 | }; |
11dc1f36 | 405 | |
32fe21c0 | 406 | int __init br_netlink_init(void) |
11dc1f36 | 407 | { |
3ec8e9f0 VY |
408 | int err; |
409 | ||
410 | br_mdb_init(); | |
411 | err = rtnl_link_register(&br_link_ops); | |
412 | if (err) | |
413 | goto out; | |
414 | ||
415 | return 0; | |
416 | out: | |
417 | br_mdb_uninit(); | |
418 | return err; | |
11dc1f36 SH |
419 | } |
420 | ||
421 | void __exit br_netlink_fini(void) | |
422 | { | |
3ec8e9f0 | 423 | br_mdb_uninit(); |
bb900b27 | 424 | rtnl_link_unregister(&br_link_ops); |
11dc1f36 | 425 | } |