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> |
bb900b27 | 19 | |
11dc1f36 | 20 | #include "br_private.h" |
b03b6dd5 | 21 | #include "br_private_stp.h" |
11dc1f36 | 22 | |
25c71c75 | 23 | static inline size_t br_port_info_size(void) |
24 | { | |
25 | return nla_total_size(1) /* IFLA_BRPORT_STATE */ | |
26 | + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */ | |
27 | + nla_total_size(4) /* IFLA_BRPORT_COST */ | |
28 | + nla_total_size(1) /* IFLA_BRPORT_MODE */ | |
29 | + 0; | |
30 | } | |
31 | ||
339bf98f TG |
32 | static inline size_t br_nlmsg_size(void) |
33 | { | |
34 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
25c71c75 | 35 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
36 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
37 | + nla_total_size(4) /* IFLA_MASTER */ | |
38 | + nla_total_size(4) /* IFLA_MTU */ | |
39 | + nla_total_size(4) /* IFLA_LINK */ | |
40 | + nla_total_size(1) /* IFLA_OPERSTATE */ | |
41 | + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */ | |
42 | } | |
43 | ||
44 | static int br_port_fill_attrs(struct sk_buff *skb, | |
45 | const struct net_bridge_port *p) | |
46 | { | |
47 | u8 mode = !!(p->flags & BR_HAIRPIN_MODE); | |
48 | ||
49 | if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) || | |
50 | nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) || | |
51 | nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) || | |
52 | nla_put_u8(skb, IFLA_BRPORT_MODE, mode)) | |
53 | return -EMSGSIZE; | |
54 | ||
55 | return 0; | |
339bf98f TG |
56 | } |
57 | ||
11dc1f36 SH |
58 | /* |
59 | * Create one netlink message for one interface | |
60 | * Contains port and master info as well as carrier and bridge state. | |
61 | */ | |
62 | static int br_fill_ifinfo(struct sk_buff *skb, const struct net_bridge_port *port, | |
63 | u32 pid, u32 seq, int event, unsigned int flags) | |
64 | { | |
65 | const struct net_bridge *br = port->br; | |
66 | const struct net_device *dev = port->dev; | |
74685962 | 67 | struct ifinfomsg *hdr; |
11dc1f36 | 68 | struct nlmsghdr *nlh; |
11dc1f36 | 69 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
11dc1f36 | 70 | |
28a16c97 | 71 | br_debug(br, "br_fill_info event %d port %s master %s\n", |
72 | event, dev->name, br->dev->name); | |
11dc1f36 | 73 | |
74685962 TG |
74 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); |
75 | if (nlh == NULL) | |
26932566 | 76 | return -EMSGSIZE; |
11dc1f36 | 77 | |
74685962 TG |
78 | hdr = nlmsg_data(nlh); |
79 | hdr->ifi_family = AF_BRIDGE; | |
80 | hdr->__ifi_pad = 0; | |
81 | hdr->ifi_type = dev->type; | |
82 | hdr->ifi_index = dev->ifindex; | |
83 | hdr->ifi_flags = dev_get_flags(dev); | |
84 | hdr->ifi_change = 0; | |
11dc1f36 | 85 | |
2eb812e6 DM |
86 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
87 | nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) || | |
88 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || | |
89 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || | |
90 | (dev->addr_len && | |
91 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || | |
92 | (dev->ifindex != dev->iflink && | |
25c71c75 | 93 | nla_put_u32(skb, IFLA_LINK, dev->iflink))) |
2eb812e6 | 94 | goto nla_put_failure; |
25c71c75 | 95 | |
96 | if (event == RTM_NEWLINK) { | |
97 | struct nlattr *nest | |
98 | = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); | |
99 | ||
100 | if (nest == NULL || br_port_fill_attrs(skb, port) < 0) | |
101 | goto nla_put_failure; | |
102 | nla_nest_end(skb, nest); | |
103 | } | |
104 | ||
74685962 | 105 | return nlmsg_end(skb, nlh); |
11dc1f36 | 106 | |
74685962 | 107 | nla_put_failure: |
26932566 PM |
108 | nlmsg_cancel(skb, nlh); |
109 | return -EMSGSIZE; | |
11dc1f36 SH |
110 | } |
111 | ||
112 | /* | |
113 | * Notify listeners of a change in port information | |
114 | */ | |
115 | void br_ifinfo_notify(int event, struct net_bridge_port *port) | |
116 | { | |
4aa678ba | 117 | struct net *net = dev_net(port->dev); |
11dc1f36 | 118 | struct sk_buff *skb; |
280a306c | 119 | int err = -ENOBUFS; |
11dc1f36 | 120 | |
28a16c97 | 121 | br_debug(port->br, "port %u(%s) event %d\n", |
95c96174 | 122 | (unsigned int)port->port_no, port->dev->name, event); |
28a16c97 | 123 | |
339bf98f | 124 | skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC); |
280a306c TG |
125 | if (skb == NULL) |
126 | goto errout; | |
127 | ||
128 | err = br_fill_ifinfo(skb, port, 0, 0, event, 0); | |
26932566 PM |
129 | if (err < 0) { |
130 | /* -EMSGSIZE implies BUG in br_nlmsg_size() */ | |
131 | WARN_ON(err == -EMSGSIZE); | |
132 | kfree_skb(skb); | |
133 | goto errout; | |
134 | } | |
1ce85fe4 PNA |
135 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
136 | return; | |
280a306c | 137 | errout: |
bea1b42e | 138 | if (err < 0) |
4aa678ba | 139 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
11dc1f36 SH |
140 | } |
141 | ||
142 | /* | |
143 | * Dump information about all ports, in response to GETLINK | |
144 | */ | |
e5a55a89 JF |
145 | int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
146 | struct net_device *dev) | |
11dc1f36 | 147 | { |
e5a55a89 JF |
148 | int err = 0; |
149 | struct net_bridge_port *port = br_port_get_rcu(dev); | |
150 | ||
151 | /* not a bridge port */ | |
152 | if (!port) | |
153 | goto out; | |
11dc1f36 | 154 | |
e5a55a89 JF |
155 | err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI); |
156 | out: | |
157 | return err; | |
11dc1f36 SH |
158 | } |
159 | ||
25c71c75 | 160 | static const struct nla_policy ifla_brport_policy[IFLA_BRPORT_MAX + 1] = { |
161 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, | |
162 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, | |
163 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, | |
164 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, | |
165 | }; | |
166 | ||
167 | /* Change the state of the port and notify spanning tree */ | |
168 | static int br_set_port_state(struct net_bridge_port *p, u8 state) | |
169 | { | |
170 | if (state > BR_STATE_BLOCKING) | |
171 | return -EINVAL; | |
172 | ||
173 | /* if kernel STP is running, don't allow changes */ | |
174 | if (p->br->stp_enabled == BR_KERNEL_STP) | |
175 | return -EBUSY; | |
176 | ||
177 | if (!netif_running(p->dev) || | |
178 | (!netif_carrier_ok(p->dev) && state != BR_STATE_DISABLED)) | |
179 | return -ENETDOWN; | |
180 | ||
181 | p->state = state; | |
182 | br_log_state(p); | |
183 | br_port_state_selection(p->br); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | /* Set/clear or port flags based on attribute */ | |
188 | static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[], | |
189 | int attrtype, unsigned long mask) | |
190 | { | |
191 | if (tb[attrtype]) { | |
192 | u8 flag = nla_get_u8(tb[attrtype]); | |
193 | if (flag) | |
194 | p->flags |= mask; | |
195 | else | |
196 | p->flags &= ~mask; | |
197 | } | |
198 | } | |
199 | ||
200 | /* Process bridge protocol info on port */ | |
201 | static int br_setport(struct net_bridge_port *p, struct nlattr *tb[]) | |
202 | { | |
203 | int err; | |
204 | ||
205 | br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE); | |
206 | ||
207 | if (tb[IFLA_BRPORT_COST]) { | |
208 | err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST])); | |
209 | if (err) | |
210 | return err; | |
211 | } | |
212 | ||
213 | if (tb[IFLA_BRPORT_PRIORITY]) { | |
214 | err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY])); | |
215 | if (err) | |
216 | return err; | |
217 | } | |
218 | ||
219 | if (tb[IFLA_BRPORT_STATE]) { | |
220 | err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE])); | |
221 | if (err) | |
222 | return err; | |
223 | } | |
224 | return 0; | |
225 | } | |
226 | ||
227 | /* Change state and parameters on port. */ | |
e5a55a89 | 228 | int br_setlink(struct net_device *dev, struct nlmsghdr *nlh) |
11dc1f36 | 229 | { |
74685962 TG |
230 | struct ifinfomsg *ifm; |
231 | struct nlattr *protinfo; | |
11dc1f36 | 232 | struct net_bridge_port *p; |
25c71c75 | 233 | struct nlattr *tb[IFLA_BRPORT_MAX]; |
234 | int err; | |
11dc1f36 | 235 | |
74685962 | 236 | ifm = nlmsg_data(nlh); |
11dc1f36 | 237 | |
74685962 | 238 | protinfo = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_PROTINFO); |
25c71c75 | 239 | if (!protinfo) |
240 | return 0; | |
11dc1f36 | 241 | |
ec1e5610 | 242 | p = br_port_get_rtnl(dev); |
b5ed54e9 | 243 | if (!p) |
244 | return -EINVAL; | |
11dc1f36 | 245 | |
25c71c75 | 246 | if (protinfo->nla_type & NLA_F_NESTED) { |
247 | err = nla_parse_nested(tb, IFLA_BRPORT_MAX, | |
248 | protinfo, ifla_brport_policy); | |
249 | if (err) | |
250 | return err; | |
11dc1f36 | 251 | |
25c71c75 | 252 | spin_lock_bh(&p->br->lock); |
253 | err = br_setport(p, tb); | |
254 | spin_unlock_bh(&p->br->lock); | |
255 | } else { | |
256 | /* Binary compatability with old RSTP */ | |
257 | if (nla_len(protinfo) < sizeof(u8)) | |
258 | return -EINVAL; | |
11dc1f36 | 259 | |
25c71c75 | 260 | spin_lock_bh(&p->br->lock); |
261 | err = br_set_port_state(p, nla_get_u8(protinfo)); | |
262 | spin_unlock_bh(&p->br->lock); | |
263 | } | |
b03b6dd5 | 264 | |
25c71c75 | 265 | if (err == 0) |
266 | br_ifinfo_notify(RTM_NEWLINK, p); | |
b03b6dd5 | 267 | |
25c71c75 | 268 | return err; |
11dc1f36 SH |
269 | } |
270 | ||
bb900b27 | 271 | static int br_validate(struct nlattr *tb[], struct nlattr *data[]) |
272 | { | |
273 | if (tb[IFLA_ADDRESS]) { | |
274 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
275 | return -EINVAL; | |
276 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
277 | return -EADDRNOTAVAIL; | |
278 | } | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
149ddd83 | 283 | struct rtnl_link_ops br_link_ops __read_mostly = { |
bb900b27 | 284 | .kind = "bridge", |
285 | .priv_size = sizeof(struct net_bridge), | |
286 | .setup = br_dev_setup, | |
287 | .validate = br_validate, | |
1ce5cce8 | 288 | .dellink = br_dev_delete, |
bb900b27 | 289 | }; |
11dc1f36 | 290 | |
32fe21c0 | 291 | int __init br_netlink_init(void) |
11dc1f36 | 292 | { |
e5a55a89 | 293 | return rtnl_link_register(&br_link_ops); |
11dc1f36 SH |
294 | } |
295 | ||
296 | void __exit br_netlink_fini(void) | |
297 | { | |
bb900b27 | 298 | rtnl_link_unregister(&br_link_ops); |
32fe21c0 | 299 | rtnl_unregister_all(PF_BRIDGE); |
11dc1f36 | 300 | } |