blob: ea748c93a07f1dcd988cc18130d629d6a3556d98 [file] [log] [blame]
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000015#include <linux/etherdevice.h>
Thomas Graf32fe21c2007-03-22 11:59:03 -070016#include <net/rtnetlink.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070017#include <net/net_namespace.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110018#include <net/sock.h>
Vlad Yasevich407af322013-02-13 12:00:12 +000019#include <uapi/linux/if_bridge.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000020
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070021#include "br_private.h"
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +000022#include "br_private_stp.h"
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070023
Roopa Prabhufed0a152015-02-25 23:55:40 -080024static int br_get_num_vlan_infos(const struct net_port_vlans *pv,
25 u32 filter_mask)
26{
27 u16 vid_range_start = 0, vid_range_end = 0;
28 u16 vid_range_flags = 0;
29 u16 pvid, vid, flags;
30 int num_vlans = 0;
31
32 if (filter_mask & RTEXT_FILTER_BRVLAN)
33 return pv->num_vlans;
34
35 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
36 return 0;
37
38 /* Count number of vlan info's
39 */
40 pvid = br_get_pvid(pv);
41 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
42 flags = 0;
43 if (vid == pvid)
44 flags |= BRIDGE_VLAN_INFO_PVID;
45
46 if (test_bit(vid, pv->untagged_bitmap))
47 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
48
49 if (vid_range_start == 0) {
50 goto initvars;
51 } else if ((vid - vid_range_end) == 1 &&
52 flags == vid_range_flags) {
53 vid_range_end = vid;
54 continue;
55 } else {
56 if ((vid_range_end - vid_range_start) > 0)
57 num_vlans += 2;
58 else
59 num_vlans += 1;
60 }
61initvars:
62 vid_range_start = vid;
63 vid_range_end = vid;
64 vid_range_flags = flags;
65 }
66
67 if (vid_range_start != 0) {
68 if ((vid_range_end - vid_range_start) > 0)
69 num_vlans += 2;
70 else
71 num_vlans += 1;
72 }
73
74 return num_vlans;
75}
76
77static size_t br_get_link_af_size_filtered(const struct net_device *dev,
78 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080079{
80 struct net_port_vlans *pv;
Roopa Prabhufed0a152015-02-25 23:55:40 -080081 int num_vlan_infos;
Roopa Prabhub7853d72015-02-21 20:21:51 -080082
Johannes Berg2f56f6b2015-03-03 16:02:16 +010083 rcu_read_lock();
Roopa Prabhub7853d72015-02-21 20:21:51 -080084 if (br_port_exists(dev))
Johannes Berg2f56f6b2015-03-03 16:02:16 +010085 pv = nbp_get_vlan_info(br_port_get_rcu(dev));
Roopa Prabhub7853d72015-02-21 20:21:51 -080086 else if (dev->priv_flags & IFF_EBRIDGE)
87 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
88 else
Johannes Berg2f56f6b2015-03-03 16:02:16 +010089 pv = NULL;
90 if (pv)
91 num_vlan_infos = br_get_num_vlan_infos(pv, filter_mask);
92 else
93 num_vlan_infos = 0;
94 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -080095
Roopa Prabhufed0a152015-02-25 23:55:40 -080096 if (!num_vlan_infos)
97 return 0;
98
Roopa Prabhub7853d72015-02-21 20:21:51 -080099 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800100 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhub7853d72015-02-21 20:21:51 -0800101}
102
stephen hemminger25c71c72012-11-13 07:53:05 +0000103static inline size_t br_port_info_size(void)
104{
105 return nla_total_size(1) /* IFLA_BRPORT_STATE */
106 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
107 + nla_total_size(4) /* IFLA_BRPORT_COST */
108 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000109 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000110 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000111 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400112 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400113 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200114 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200115 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
stephen hemminger25c71c72012-11-13 07:53:05 +0000116 + 0;
117}
118
Roopa Prabhufed0a152015-02-25 23:55:40 -0800119static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800120{
121 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000122 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
123 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
124 + nla_total_size(4) /* IFLA_MASTER */
125 + nla_total_size(4) /* IFLA_MTU */
126 + nla_total_size(4) /* IFLA_LINK */
127 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800128 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800129 + nla_total_size(br_get_link_af_size_filtered(dev,
130 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000131}
132
133static int br_port_fill_attrs(struct sk_buff *skb,
134 const struct net_bridge_port *p)
135{
136 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
137
138 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
139 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
140 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000141 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000142 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500143 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400144 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400145 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700146 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200147 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
148 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
149 !!(p->flags & BR_PROXYARP_WIFI)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000150 return -EMSGSIZE;
151
152 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800153}
154
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800155static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
156 u16 vid_end, u16 flags)
157{
158 struct bridge_vlan_info vinfo;
159
160 if ((vid_end - vid_start) > 0) {
161 /* add range to skb */
162 vinfo.vid = vid_start;
163 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
164 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
165 sizeof(vinfo), &vinfo))
166 goto nla_put_failure;
167
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800168 vinfo.vid = vid_end;
169 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
170 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
171 sizeof(vinfo), &vinfo))
172 goto nla_put_failure;
173 } else {
174 vinfo.vid = vid_start;
175 vinfo.flags = flags;
176 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
177 sizeof(vinfo), &vinfo))
178 goto nla_put_failure;
179 }
180
181 return 0;
182
183nla_put_failure:
184 return -EMSGSIZE;
185}
186
187static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
188 const struct net_port_vlans *pv)
189{
190 u16 vid_range_start = 0, vid_range_end = 0;
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800191 u16 vid_range_flags = 0;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800192 u16 pvid, vid, flags;
193 int err = 0;
194
195 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
196 * and mark vlan info with begin and end flags
197 * if vlaninfo represents a range
198 */
199 pvid = br_get_pvid(pv);
200 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
201 flags = 0;
202 if (vid == pvid)
203 flags |= BRIDGE_VLAN_INFO_PVID;
204
205 if (test_bit(vid, pv->untagged_bitmap))
206 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
207
208 if (vid_range_start == 0) {
209 goto initvars;
210 } else if ((vid - vid_range_end) == 1 &&
211 flags == vid_range_flags) {
212 vid_range_end = vid;
213 continue;
214 } else {
215 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
216 vid_range_end,
217 vid_range_flags);
218 if (err)
219 return err;
220 }
221
222initvars:
223 vid_range_start = vid;
224 vid_range_end = vid;
225 vid_range_flags = flags;
226 }
227
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800228 if (vid_range_start != 0) {
229 /* Call it once more to send any left over vlans */
230 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
231 vid_range_end,
232 vid_range_flags);
233 if (err)
234 return err;
235 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800236
237 return 0;
238}
239
240static int br_fill_ifvlaninfo(struct sk_buff *skb,
241 const struct net_port_vlans *pv)
242{
243 struct bridge_vlan_info vinfo;
244 u16 pvid, vid;
245
246 pvid = br_get_pvid(pv);
247 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
248 vinfo.vid = vid;
249 vinfo.flags = 0;
250 if (vid == pvid)
251 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
252
253 if (test_bit(vid, pv->untagged_bitmap))
254 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
255
256 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
257 sizeof(vinfo), &vinfo))
258 goto nla_put_failure;
259 }
260
261 return 0;
262
263nla_put_failure:
264 return -EMSGSIZE;
265}
266
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700267/*
268 * Create one netlink message for one interface
269 * Contains port and master info as well as carrier and bridge state.
270 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000271static int br_fill_ifinfo(struct sk_buff *skb,
272 const struct net_bridge_port *port,
273 u32 pid, u32 seq, int event, unsigned int flags,
274 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700275{
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000276 const struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800277 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700278 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700279 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700280
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000281 if (port)
282 br = port->br;
283 else
284 br = netdev_priv(dev);
285
stephen hemminger28a16c92010-05-10 09:31:09 +0000286 br_debug(br, "br_fill_info event %d port %s master %s\n",
287 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700288
Thomas Graf74685962006-11-20 16:20:22 -0800289 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
290 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800291 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700292
Thomas Graf74685962006-11-20 16:20:22 -0800293 hdr = nlmsg_data(nlh);
294 hdr->ifi_family = AF_BRIDGE;
295 hdr->__ifi_pad = 0;
296 hdr->ifi_type = dev->type;
297 hdr->ifi_index = dev->ifindex;
298 hdr->ifi_flags = dev_get_flags(dev);
299 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700300
David S. Miller2eb812e2012-04-01 20:49:54 -0400301 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
302 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
303 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
304 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
305 (dev->addr_len &&
306 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200307 (dev->ifindex != dev_get_iflink(dev) &&
308 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400309 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000310
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000311 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000312 struct nlattr *nest
313 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
314
315 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
316 goto nla_put_failure;
317 nla_nest_end(skb, nest);
318 }
319
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000320 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800321 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
322 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000323 const struct net_port_vlans *pv;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800324 struct nlattr *af;
325 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000326
327 if (port)
328 pv = nbp_get_vlan_info(port);
329 else
330 pv = br_get_vlan_info(br);
331
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900332 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID))
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000333 goto done;
334
335 af = nla_nest_start(skb, IFLA_AF_SPEC);
336 if (!af)
337 goto nla_put_failure;
338
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800339 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
340 err = br_fill_ifvlaninfo_compressed(skb, pv);
341 else
342 err = br_fill_ifvlaninfo(skb, pv);
343 if (err)
344 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000345 nla_nest_end(skb, af);
346 }
347
348done:
Johannes Berg053c0952015-01-16 22:09:00 +0100349 nlmsg_end(skb, nlh);
350 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700351
Thomas Graf74685962006-11-20 16:20:22 -0800352nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800353 nlmsg_cancel(skb, nlh);
354 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700355}
356
357/*
358 * Notify listeners of a change in port information
359 */
360void br_ifinfo_notify(int event, struct net_bridge_port *port)
361{
Vlad Yasevich407af322013-02-13 12:00:12 +0000362 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700363 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700364 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800365 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700366
Vlad Yasevich407af322013-02-13 12:00:12 +0000367 if (!port)
368 return;
369
370 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000371 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000372 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000373
Roopa Prabhufed0a152015-02-25 23:55:40 -0800374 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700375 if (skb == NULL)
376 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700377
Roopa Prabhufed0a152015-02-25 23:55:40 -0800378 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800379 if (err < 0) {
380 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
381 WARN_ON(err == -EMSGSIZE);
382 kfree_skb(skb);
383 goto errout;
384 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800385 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
386 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700387errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800388 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700389}
390
Vlad Yasevich407af322013-02-13 12:00:12 +0000391
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700392/*
393 * Dump information about all ports, in response to GETLINK
394 */
John Fastabende5a55a82012-10-24 08:12:57 +0000395int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200396 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700397{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800398 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700399
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800400 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
401 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300402 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000403
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200404 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300405 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700406}
407
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800408static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
409 int cmd, struct bridge_vlan_info *vinfo)
410{
411 int err = 0;
412
413 switch (cmd) {
414 case RTM_SETLINK:
415 if (p) {
416 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
417 if (err)
418 break;
419
420 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
421 err = br_vlan_add(p->br, vinfo->vid,
422 vinfo->flags);
423 } else {
424 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
425 }
426 break;
427
428 case RTM_DELLINK:
429 if (p) {
430 nbp_vlan_delete(p, vinfo->vid);
431 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
432 br_vlan_delete(p->br, vinfo->vid);
433 } else {
434 br_vlan_delete(br, vinfo->vid);
435 }
436 break;
437 }
438
439 return err;
440}
Vlad Yasevich407af322013-02-13 12:00:12 +0000441
442static int br_afspec(struct net_bridge *br,
443 struct net_bridge_port *p,
444 struct nlattr *af_spec,
445 int cmd)
446{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800447 struct bridge_vlan_info *vinfo_start = NULL;
448 struct bridge_vlan_info *vinfo = NULL;
449 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000450 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800451 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000452
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800453 nla_for_each_nested(attr, af_spec, rem) {
454 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
455 continue;
456 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000457 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800458 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700459 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
460 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800461 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
462 if (vinfo_start)
463 return -EINVAL;
464 vinfo_start = vinfo;
465 continue;
466 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000467
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800468 if (vinfo_start) {
469 struct bridge_vlan_info tmp_vinfo;
470 int v;
471
472 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
473 return -EINVAL;
474
475 if (vinfo->vid <= vinfo_start->vid)
476 return -EINVAL;
477
478 memcpy(&tmp_vinfo, vinfo_start,
479 sizeof(struct bridge_vlan_info));
480
481 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
482 tmp_vinfo.vid = v;
483 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000484 if (err)
485 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800486 }
487 vinfo_start = NULL;
488 } else {
489 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000490 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800491 if (err)
492 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000493 }
494
495 return err;
496}
497
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200498static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000499 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
500 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
501 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
502 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000503 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000504 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100505 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400506 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400507 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200508 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200509 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000510};
511
512/* Change the state of the port and notify spanning tree */
513static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700514{
stephen hemminger25c71c72012-11-13 07:53:05 +0000515 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000516 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700517
518 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700519 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700520 return -EBUSY;
521
stephen hemminger576eb622012-12-28 18:15:22 +0000522 /* if device is not up, change is not allowed
523 * if link is not present, only allowable state is disabled
524 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000525 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000526 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700527 return -ENETDOWN;
528
Florian Fainelli775dd692014-09-30 16:13:19 -0700529 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700530 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000531 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700532 return 0;
533}
534
stephen hemminger25c71c72012-11-13 07:53:05 +0000535/* Set/clear or port flags based on attribute */
536static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
537 int attrtype, unsigned long mask)
538{
539 if (tb[attrtype]) {
540 u8 flag = nla_get_u8(tb[attrtype]);
541 if (flag)
542 p->flags |= mask;
543 else
544 p->flags &= ~mask;
545 }
546}
547
548/* Process bridge protocol info on port */
549static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
550{
551 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400552 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000553
554 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000555 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500556 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000557 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400558 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400559 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700560 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200561 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000562
563 if (tb[IFLA_BRPORT_COST]) {
564 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
565 if (err)
566 return err;
567 }
568
569 if (tb[IFLA_BRPORT_PRIORITY]) {
570 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
571 if (err)
572 return err;
573 }
574
575 if (tb[IFLA_BRPORT_STATE]) {
576 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
577 if (err)
578 return err;
579 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400580
581 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000582 return 0;
583}
584
585/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800586int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000587{
stephen hemminger25c71c72012-11-13 07:53:05 +0000588 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000589 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000590 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000591 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700592 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000593
Hong zhi guoc60ee672013-03-28 06:21:22 +0000594 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
595 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000596 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000597 return 0;
598
599 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000600 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800601 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000602 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000603 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000604 return -EINVAL;
605
Vlad Yasevich407af322013-02-13 12:00:12 +0000606 if (p && protinfo) {
607 if (protinfo->nla_type & NLA_F_NESTED) {
608 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200609 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000610 if (err)
611 return err;
612
613 spin_lock_bh(&p->br->lock);
614 err = br_setport(p, tb);
615 spin_unlock_bh(&p->br->lock);
616 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800617 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000618 if (nla_len(protinfo) < sizeof(u8))
619 return -EINVAL;
620
621 spin_lock_bh(&p->br->lock);
622 err = br_set_port_state(p, nla_get_u8(protinfo));
623 spin_unlock_bh(&p->br->lock);
624 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000625 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000626 goto out;
627 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000628
Vlad Yasevich407af322013-02-13 12:00:12 +0000629 if (afspec) {
630 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
631 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000632 }
633
634 if (err == 0)
635 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000636out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000637 return err;
638}
639
Vlad Yasevich407af322013-02-13 12:00:12 +0000640/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800641int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000642{
Vlad Yasevich407af322013-02-13 12:00:12 +0000643 struct nlattr *afspec;
644 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700645 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000646
Hong zhi guoc60ee672013-03-28 06:21:22 +0000647 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000648 if (!afspec)
649 return 0;
650
651 p = br_port_get_rtnl(dev);
652 /* We want to accept dev as bridge itself as well */
653 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
654 return -EINVAL;
655
656 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
657 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800658 if (err == 0)
659 /* Send RTM_NEWLINK because userspace
660 * expects RTM_NEWLINK for vlan dels
661 */
662 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000663
664 return err;
665}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000666static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700667{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000668 if (tb[IFLA_ADDRESS]) {
669 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
670 return -EINVAL;
671 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
672 return -EADDRNOTAVAIL;
673 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700674
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900675 if (!data)
676 return 0;
677
678#ifdef CONFIG_BRIDGE_VLAN_FILTERING
679 if (data[IFLA_BR_VLAN_PROTOCOL]) {
680 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
681 case htons(ETH_P_8021Q):
682 case htons(ETH_P_8021AD):
683 break;
684 default:
685 return -EPROTONOSUPPORT;
686 }
687 }
688#endif
689
Thomas Graf32fe21c2007-03-22 11:59:03 -0700690 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700691}
692
Toshiaki Makita30313a32014-04-25 17:01:18 +0900693static int br_dev_newlink(struct net *src_net, struct net_device *dev,
694 struct nlattr *tb[], struct nlattr *data[])
695{
696 struct net_bridge *br = netdev_priv(dev);
697
698 if (tb[IFLA_ADDRESS]) {
699 spin_lock_bh(&br->lock);
700 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
701 spin_unlock_bh(&br->lock);
702 }
703
704 return register_netdevice(dev);
705}
706
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200707static int br_port_slave_changelink(struct net_device *brdev,
708 struct net_device *dev,
709 struct nlattr *tb[],
710 struct nlattr *data[])
711{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200712 struct net_bridge *br = netdev_priv(brdev);
713 int ret;
714
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200715 if (!data)
716 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200717
718 spin_lock_bh(&br->lock);
719 ret = br_setport(br_port_get_rtnl(dev), data);
720 spin_unlock_bh(&br->lock);
721
722 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200723}
724
Jiri Pirkoced82832014-09-05 15:51:29 +0200725static int br_port_fill_slave_info(struct sk_buff *skb,
726 const struct net_device *brdev,
727 const struct net_device *dev)
728{
729 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
730}
731
732static size_t br_port_get_slave_size(const struct net_device *brdev,
733 const struct net_device *dev)
734{
735 return br_port_info_size();
736}
737
Jiri Pirko13323512014-09-05 15:51:32 +0200738static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
739 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
740 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
741 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100742 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
743 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
744 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300745 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900746 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Jiri Pirko13323512014-09-05 15:51:32 +0200747};
748
749static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
750 struct nlattr *data[])
751{
752 struct net_bridge *br = netdev_priv(brdev);
753 int err;
754
755 if (!data)
756 return 0;
757
758 if (data[IFLA_BR_FORWARD_DELAY]) {
759 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
760 if (err)
761 return err;
762 }
763
764 if (data[IFLA_BR_HELLO_TIME]) {
765 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
766 if (err)
767 return err;
768 }
769
770 if (data[IFLA_BR_MAX_AGE]) {
771 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
772 if (err)
773 return err;
774 }
775
Jörg Thalheimaf615762015-03-18 10:06:58 +0100776 if (data[IFLA_BR_AGEING_TIME]) {
777 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
778
779 br->ageing_time = clock_t_to_jiffies(ageing_time);
780 }
781
782 if (data[IFLA_BR_STP_STATE]) {
783 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
784
785 br_stp_set_enabled(br, stp_enabled);
786 }
787
788 if (data[IFLA_BR_PRIORITY]) {
789 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
790
791 br_stp_set_bridge_priority(br, priority);
792 }
793
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300794 if (data[IFLA_BR_VLAN_FILTERING]) {
795 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
796
797 err = __br_vlan_filter_toggle(br, vlan_filter);
798 if (err)
799 return err;
800 }
801
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900802#ifdef CONFIG_BRIDGE_VLAN_FILTERING
803 if (data[IFLA_BR_VLAN_PROTOCOL]) {
804 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
805
806 err = __br_vlan_set_proto(br, vlan_proto);
807 if (err)
808 return err;
809 }
810#endif
811
Jiri Pirko13323512014-09-05 15:51:32 +0200812 return 0;
813}
814
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200815static size_t br_get_size(const struct net_device *brdev)
816{
817 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
818 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
819 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +0100820 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
821 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
822 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300823 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900824#ifdef CONFIG_BRIDGE_VLAN_FILTERING
825 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
826#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200827 0;
828}
829
830static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
831{
832 struct net_bridge *br = netdev_priv(brdev);
833 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
834 u32 hello_time = jiffies_to_clock_t(br->hello_time);
835 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +0100836 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
837 u32 stp_enabled = br->stp_enabled;
838 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300839 u8 vlan_enabled = br_vlan_enabled(br);
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200840
841 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
842 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +0100843 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
844 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
845 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300846 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
847 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200848 return -EMSGSIZE;
849
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900850#ifdef CONFIG_BRIDGE_VLAN_FILTERING
851 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto))
852 return -EMSGSIZE;
853#endif
854
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200855 return 0;
856}
857
Roopa Prabhufed0a152015-02-25 23:55:40 -0800858static size_t br_get_link_af_size(const struct net_device *dev)
859{
860 struct net_port_vlans *pv;
861
862 if (br_port_exists(dev))
863 pv = nbp_get_vlan_info(br_port_get_rtnl(dev));
864 else if (dev->priv_flags & IFF_EBRIDGE)
865 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
866 else
867 return 0;
868
869 if (!pv)
870 return 0;
871
872 /* Each VLAN is returned in bridge_vlan_info along with flags */
873 return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
874}
875
Daniel Borkmann207895f2015-01-29 12:15:03 +0100876static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000877 .family = AF_BRIDGE,
878 .get_link_af_size = br_get_link_af_size,
879};
880
stephen hemminger149ddd82012-06-26 05:48:45 +0000881struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +0200882 .kind = "bridge",
883 .priv_size = sizeof(struct net_bridge),
884 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -0700885 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +0200886 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +0200887 .validate = br_validate,
888 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +0200889 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200890 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200891 .get_size = br_get_size,
892 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200893
894 .slave_maxtype = IFLA_BRPORT_MAX,
895 .slave_policy = br_port_policy,
896 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200897 .get_slave_size = br_port_get_slave_size,
898 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +0000899};
900
901int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700902{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000903 int err;
904
905 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800906 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000907
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000908 err = rtnl_link_register(&br_link_ops);
909 if (err)
910 goto out_af;
911
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000912 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000913
914out_af:
915 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000916 br_mdb_uninit();
917 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700918}
919
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200920void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +0000921{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000922 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000923 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000924 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000925}