blob: 8bcaa5171f351700183cf36ad50f88c62c99657d [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
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020024static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020025 u32 filter_mask)
Roopa Prabhufed0a152015-02-25 23:55:40 -080026{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020027 struct net_bridge_vlan *v;
28 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020029 u16 flags, pvid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080030 int num_vlans = 0;
31
Roopa Prabhufed0a152015-02-25 23:55:40 -080032 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
33 return 0;
34
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020035 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020036 /* Count number of vlan infos */
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020037 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhufed0a152015-02-25 23:55:40 -080038 flags = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020039 /* only a context, bridge vlan not activated */
40 if (!br_vlan_should_use(v))
41 continue;
42 if (v->vid == pvid)
Roopa Prabhufed0a152015-02-25 23:55:40 -080043 flags |= BRIDGE_VLAN_INFO_PVID;
44
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020045 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhufed0a152015-02-25 23:55:40 -080046 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
47
48 if (vid_range_start == 0) {
49 goto initvars;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020050 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhufed0a152015-02-25 23:55:40 -080051 flags == vid_range_flags) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020052 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080053 continue;
54 } else {
55 if ((vid_range_end - vid_range_start) > 0)
56 num_vlans += 2;
57 else
58 num_vlans += 1;
59 }
60initvars:
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020061 vid_range_start = v->vid;
62 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080063 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
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020076static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020077 u32 filter_mask)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020078{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020079 int num_vlans;
80
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020081 if (!vg)
82 return 0;
83
84 if (filter_mask & RTEXT_FILTER_BRVLAN)
85 return vg->num_vlans;
86
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020087 rcu_read_lock();
88 num_vlans = __get_num_vlan_infos(vg, filter_mask);
89 rcu_read_unlock();
90
91 return num_vlans;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020092}
93
Roopa Prabhufed0a152015-02-25 23:55:40 -080094static size_t br_get_link_af_size_filtered(const struct net_device *dev,
95 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080096{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020097 struct net_bridge_vlan_group *vg = NULL;
98 struct net_bridge_port *p;
99 struct net_bridge *br;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800100 int num_vlan_infos;
Roopa Prabhub7853d72015-02-21 20:21:51 -0800101
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100102 rcu_read_lock();
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200103 if (br_port_exists(dev)) {
104 p = br_port_get_rcu(dev);
105 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200106 } else if (dev->priv_flags & IFF_EBRIDGE) {
107 br = netdev_priv(dev);
108 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200109 }
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200110 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100111 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -0800112
Roopa Prabhub7853d72015-02-21 20:21:51 -0800113 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800114 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhub7853d72015-02-21 20:21:51 -0800115}
116
stephen hemminger25c71c72012-11-13 07:53:05 +0000117static 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 */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000123 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000124 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000125 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400126 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400127 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200128 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200129 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
stephen hemminger25c71c72012-11-13 07:53:05 +0000130 + 0;
131}
132
Roopa Prabhufed0a152015-02-25 23:55:40 -0800133static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800134{
135 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000136 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
137 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
138 + nla_total_size(4) /* IFLA_MASTER */
139 + nla_total_size(4) /* IFLA_MTU */
140 + nla_total_size(4) /* IFLA_LINK */
141 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800142 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800143 + nla_total_size(br_get_link_af_size_filtered(dev,
144 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000145}
146
147static int br_port_fill_attrs(struct sk_buff *skb,
148 const struct net_bridge_port *p)
149{
150 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
151
152 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
153 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
154 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000155 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000156 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500157 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400158 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400159 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700160 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200161 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
162 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
163 !!(p->flags & BR_PROXYARP_WIFI)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000164 return -EMSGSIZE;
165
166 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800167}
168
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800169static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
170 u16 vid_end, u16 flags)
171{
172 struct bridge_vlan_info vinfo;
173
174 if ((vid_end - vid_start) > 0) {
175 /* add range to skb */
176 vinfo.vid = vid_start;
177 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
178 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
179 sizeof(vinfo), &vinfo))
180 goto nla_put_failure;
181
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800182 vinfo.vid = vid_end;
183 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
184 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
185 sizeof(vinfo), &vinfo))
186 goto nla_put_failure;
187 } else {
188 vinfo.vid = vid_start;
189 vinfo.flags = flags;
190 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
191 sizeof(vinfo), &vinfo))
192 goto nla_put_failure;
193 }
194
195 return 0;
196
197nla_put_failure:
198 return -EMSGSIZE;
199}
200
201static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200202 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800203{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200204 struct net_bridge_vlan *v;
205 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200206 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800207 int err = 0;
208
209 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
210 * and mark vlan info with begin and end flags
211 * if vlaninfo represents a range
212 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200213 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200214 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800215 flags = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200216 if (!br_vlan_should_use(v))
217 continue;
218 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800219 flags |= BRIDGE_VLAN_INFO_PVID;
220
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200221 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800222 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
223
224 if (vid_range_start == 0) {
225 goto initvars;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200226 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800227 flags == vid_range_flags) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200228 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800229 continue;
230 } else {
231 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
232 vid_range_end,
233 vid_range_flags);
234 if (err)
235 return err;
236 }
237
238initvars:
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200239 vid_range_start = v->vid;
240 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800241 vid_range_flags = flags;
242 }
243
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800244 if (vid_range_start != 0) {
245 /* Call it once more to send any left over vlans */
246 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
247 vid_range_end,
248 vid_range_flags);
249 if (err)
250 return err;
251 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800252
253 return 0;
254}
255
256static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200257 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800258{
259 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200260 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200261 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800262
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200263 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200264 list_for_each_entry(v, &vg->vlan_list, vlist) {
265 if (!br_vlan_should_use(v))
266 continue;
267
268 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800269 vinfo.flags = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200270 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800271 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
272
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200273 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800274 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
275
276 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
277 sizeof(vinfo), &vinfo))
278 goto nla_put_failure;
279 }
280
281 return 0;
282
283nla_put_failure:
284 return -EMSGSIZE;
285}
286
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700287/*
288 * Create one netlink message for one interface
289 * Contains port and master info as well as carrier and bridge state.
290 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000291static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200292 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000293 u32 pid, u32 seq, int event, unsigned int flags,
294 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700295{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200296 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800297 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700298 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700299 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700300
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000301 if (port)
302 br = port->br;
303 else
304 br = netdev_priv(dev);
305
stephen hemminger28a16c92010-05-10 09:31:09 +0000306 br_debug(br, "br_fill_info event %d port %s master %s\n",
307 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700308
Thomas Graf74685962006-11-20 16:20:22 -0800309 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
310 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800311 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700312
Thomas Graf74685962006-11-20 16:20:22 -0800313 hdr = nlmsg_data(nlh);
314 hdr->ifi_family = AF_BRIDGE;
315 hdr->__ifi_pad = 0;
316 hdr->ifi_type = dev->type;
317 hdr->ifi_index = dev->ifindex;
318 hdr->ifi_flags = dev_get_flags(dev);
319 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700320
David S. Miller2eb812e2012-04-01 20:49:54 -0400321 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
322 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
323 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
324 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
325 (dev->addr_len &&
326 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200327 (dev->ifindex != dev_get_iflink(dev) &&
328 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400329 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000330
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000331 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000332 struct nlattr *nest
333 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
334
335 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
336 goto nla_put_failure;
337 nla_nest_end(skb, nest);
338 }
339
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000340 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800341 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
342 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200343 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800344 struct nlattr *af;
345 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000346
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200347 if (port)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200348 vg = nbp_vlan_group(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200349 else
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200350 vg = br_vlan_group(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000351
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200352 if (!vg || !vg->num_vlans)
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000353 goto done;
354
355 af = nla_nest_start(skb, IFLA_AF_SPEC);
356 if (!af)
357 goto nla_put_failure;
358
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800359 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200360 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800361 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200362 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800363 if (err)
364 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000365 nla_nest_end(skb, af);
366 }
367
368done:
Johannes Berg053c0952015-01-16 22:09:00 +0100369 nlmsg_end(skb, nlh);
370 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700371
Thomas Graf74685962006-11-20 16:20:22 -0800372nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800373 nlmsg_cancel(skb, nlh);
374 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700375}
376
377/*
378 * Notify listeners of a change in port information
379 */
380void br_ifinfo_notify(int event, struct net_bridge_port *port)
381{
Vlad Yasevich407af322013-02-13 12:00:12 +0000382 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700383 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700384 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800385 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700386
Vlad Yasevich407af322013-02-13 12:00:12 +0000387 if (!port)
388 return;
389
390 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000391 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000392 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000393
Roopa Prabhufed0a152015-02-25 23:55:40 -0800394 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700395 if (skb == NULL)
396 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700397
Roopa Prabhufed0a152015-02-25 23:55:40 -0800398 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800399 if (err < 0) {
400 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
401 WARN_ON(err == -EMSGSIZE);
402 kfree_skb(skb);
403 goto errout;
404 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800405 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
406 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700407errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800408 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700409}
410
Vlad Yasevich407af322013-02-13 12:00:12 +0000411
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700412/*
413 * Dump information about all ports, in response to GETLINK
414 */
John Fastabende5a55a82012-10-24 08:12:57 +0000415int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200416 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700417{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800418 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700419
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800420 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
421 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300422 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000423
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200424 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300425 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700426}
427
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800428static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
429 int cmd, struct bridge_vlan_info *vinfo)
430{
431 int err = 0;
432
433 switch (cmd) {
434 case RTM_SETLINK:
435 if (p) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200436 /* if the MASTER flag is set this will act on the global
437 * per-VLAN entry as well
438 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800439 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
440 if (err)
441 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800442 } else {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200443 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800444 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
445 }
446 break;
447
448 case RTM_DELLINK:
449 if (p) {
450 nbp_vlan_delete(p, vinfo->vid);
451 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
452 br_vlan_delete(p->br, vinfo->vid);
453 } else {
454 br_vlan_delete(br, vinfo->vid);
455 }
456 break;
457 }
458
459 return err;
460}
Vlad Yasevich407af322013-02-13 12:00:12 +0000461
462static int br_afspec(struct net_bridge *br,
463 struct net_bridge_port *p,
464 struct nlattr *af_spec,
465 int cmd)
466{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800467 struct bridge_vlan_info *vinfo_start = NULL;
468 struct bridge_vlan_info *vinfo = NULL;
469 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000470 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800471 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000472
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800473 nla_for_each_nested(attr, af_spec, rem) {
474 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
475 continue;
476 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000477 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800478 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700479 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
480 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800481 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
482 if (vinfo_start)
483 return -EINVAL;
484 vinfo_start = vinfo;
485 continue;
486 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000487
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800488 if (vinfo_start) {
489 struct bridge_vlan_info tmp_vinfo;
490 int v;
491
492 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
493 return -EINVAL;
494
495 if (vinfo->vid <= vinfo_start->vid)
496 return -EINVAL;
497
498 memcpy(&tmp_vinfo, vinfo_start,
499 sizeof(struct bridge_vlan_info));
500
501 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
502 tmp_vinfo.vid = v;
503 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000504 if (err)
505 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800506 }
507 vinfo_start = NULL;
508 } else {
509 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000510 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800511 if (err)
512 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000513 }
514
515 return err;
516}
517
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200518static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000519 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
520 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
521 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
522 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000523 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000524 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100525 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400526 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400527 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200528 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200529 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000530};
531
532/* Change the state of the port and notify spanning tree */
533static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700534{
stephen hemminger25c71c72012-11-13 07:53:05 +0000535 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000536 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700537
538 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700539 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700540 return -EBUSY;
541
stephen hemminger576eb622012-12-28 18:15:22 +0000542 /* if device is not up, change is not allowed
543 * if link is not present, only allowable state is disabled
544 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000545 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000546 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700547 return -ENETDOWN;
548
Florian Fainelli775dd692014-09-30 16:13:19 -0700549 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700550 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000551 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700552 return 0;
553}
554
stephen hemminger25c71c72012-11-13 07:53:05 +0000555/* Set/clear or port flags based on attribute */
556static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
557 int attrtype, unsigned long mask)
558{
559 if (tb[attrtype]) {
560 u8 flag = nla_get_u8(tb[attrtype]);
561 if (flag)
562 p->flags |= mask;
563 else
564 p->flags &= ~mask;
565 }
566}
567
568/* Process bridge protocol info on port */
569static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
570{
571 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400572 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000573
574 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000575 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500576 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000577 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400578 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400579 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700580 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200581 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000582
583 if (tb[IFLA_BRPORT_COST]) {
584 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
585 if (err)
586 return err;
587 }
588
589 if (tb[IFLA_BRPORT_PRIORITY]) {
590 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
591 if (err)
592 return err;
593 }
594
595 if (tb[IFLA_BRPORT_STATE]) {
596 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
597 if (err)
598 return err;
599 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400600
601 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000602 return 0;
603}
604
605/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800606int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000607{
stephen hemminger25c71c72012-11-13 07:53:05 +0000608 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000609 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000610 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000611 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700612 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000613
Hong zhi guoc60ee672013-03-28 06:21:22 +0000614 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
615 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000616 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000617 return 0;
618
619 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000620 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800621 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000622 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000623 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000624 return -EINVAL;
625
Vlad Yasevich407af322013-02-13 12:00:12 +0000626 if (p && protinfo) {
627 if (protinfo->nla_type & NLA_F_NESTED) {
628 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200629 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000630 if (err)
631 return err;
632
633 spin_lock_bh(&p->br->lock);
634 err = br_setport(p, tb);
635 spin_unlock_bh(&p->br->lock);
636 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800637 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000638 if (nla_len(protinfo) < sizeof(u8))
639 return -EINVAL;
640
641 spin_lock_bh(&p->br->lock);
642 err = br_set_port_state(p, nla_get_u8(protinfo));
643 spin_unlock_bh(&p->br->lock);
644 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000645 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000646 goto out;
647 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000648
Vlad Yasevich407af322013-02-13 12:00:12 +0000649 if (afspec) {
650 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
651 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000652 }
653
654 if (err == 0)
655 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000656out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000657 return err;
658}
659
Vlad Yasevich407af322013-02-13 12:00:12 +0000660/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800661int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000662{
Vlad Yasevich407af322013-02-13 12:00:12 +0000663 struct nlattr *afspec;
664 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700665 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000666
Hong zhi guoc60ee672013-03-28 06:21:22 +0000667 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000668 if (!afspec)
669 return 0;
670
671 p = br_port_get_rtnl(dev);
672 /* We want to accept dev as bridge itself as well */
673 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
674 return -EINVAL;
675
676 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
677 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800678 if (err == 0)
679 /* Send RTM_NEWLINK because userspace
680 * expects RTM_NEWLINK for vlan dels
681 */
682 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000683
684 return err;
685}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000686static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700687{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000688 if (tb[IFLA_ADDRESS]) {
689 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
690 return -EINVAL;
691 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
692 return -EADDRNOTAVAIL;
693 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700694
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900695 if (!data)
696 return 0;
697
698#ifdef CONFIG_BRIDGE_VLAN_FILTERING
699 if (data[IFLA_BR_VLAN_PROTOCOL]) {
700 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
701 case htons(ETH_P_8021Q):
702 case htons(ETH_P_8021AD):
703 break;
704 default:
705 return -EPROTONOSUPPORT;
706 }
707 }
708#endif
709
Thomas Graf32fe21c2007-03-22 11:59:03 -0700710 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700711}
712
Toshiaki Makita30313a32014-04-25 17:01:18 +0900713static int br_dev_newlink(struct net *src_net, struct net_device *dev,
714 struct nlattr *tb[], struct nlattr *data[])
715{
716 struct net_bridge *br = netdev_priv(dev);
717
718 if (tb[IFLA_ADDRESS]) {
719 spin_lock_bh(&br->lock);
720 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
721 spin_unlock_bh(&br->lock);
722 }
723
724 return register_netdevice(dev);
725}
726
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200727static int br_port_slave_changelink(struct net_device *brdev,
728 struct net_device *dev,
729 struct nlattr *tb[],
730 struct nlattr *data[])
731{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200732 struct net_bridge *br = netdev_priv(brdev);
733 int ret;
734
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200735 if (!data)
736 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200737
738 spin_lock_bh(&br->lock);
739 ret = br_setport(br_port_get_rtnl(dev), data);
740 spin_unlock_bh(&br->lock);
741
742 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200743}
744
Jiri Pirkoced82832014-09-05 15:51:29 +0200745static int br_port_fill_slave_info(struct sk_buff *skb,
746 const struct net_device *brdev,
747 const struct net_device *dev)
748{
749 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
750}
751
752static size_t br_port_get_slave_size(const struct net_device *brdev,
753 const struct net_device *dev)
754{
755 return br_port_info_size();
756}
757
Jiri Pirko13323512014-09-05 15:51:32 +0200758static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
759 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
760 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
761 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100762 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
763 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
764 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300765 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900766 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200767 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Jiri Pirko13323512014-09-05 15:51:32 +0200768};
769
770static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
771 struct nlattr *data[])
772{
773 struct net_bridge *br = netdev_priv(brdev);
774 int err;
775
776 if (!data)
777 return 0;
778
779 if (data[IFLA_BR_FORWARD_DELAY]) {
780 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
781 if (err)
782 return err;
783 }
784
785 if (data[IFLA_BR_HELLO_TIME]) {
786 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
787 if (err)
788 return err;
789 }
790
791 if (data[IFLA_BR_MAX_AGE]) {
792 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
793 if (err)
794 return err;
795 }
796
Jörg Thalheimaf615762015-03-18 10:06:58 +0100797 if (data[IFLA_BR_AGEING_TIME]) {
798 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
799
800 br->ageing_time = clock_t_to_jiffies(ageing_time);
801 }
802
803 if (data[IFLA_BR_STP_STATE]) {
804 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
805
806 br_stp_set_enabled(br, stp_enabled);
807 }
808
809 if (data[IFLA_BR_PRIORITY]) {
810 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
811
812 br_stp_set_bridge_priority(br, priority);
813 }
814
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300815 if (data[IFLA_BR_VLAN_FILTERING]) {
816 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
817
818 err = __br_vlan_filter_toggle(br, vlan_filter);
819 if (err)
820 return err;
821 }
822
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900823#ifdef CONFIG_BRIDGE_VLAN_FILTERING
824 if (data[IFLA_BR_VLAN_PROTOCOL]) {
825 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
826
827 err = __br_vlan_set_proto(br, vlan_proto);
828 if (err)
829 return err;
830 }
831#endif
832
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200833 if (data[IFLA_BR_GROUP_FWD_MASK]) {
834 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
835
836 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
837 return -EINVAL;
838 br->group_fwd_mask = fwd_mask;
839 }
840
Jiri Pirko13323512014-09-05 15:51:32 +0200841 return 0;
842}
843
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200844static size_t br_get_size(const struct net_device *brdev)
845{
846 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
847 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
848 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +0100849 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
850 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
851 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300852 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900853#ifdef CONFIG_BRIDGE_VLAN_FILTERING
854 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
855#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200856 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +0200857 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +0200858 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +0200859 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +0200860 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandroved416302015-10-04 14:23:33 +0200861 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
862 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200863 0;
864}
865
866static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
867{
868 struct net_bridge *br = netdev_priv(brdev);
869 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
870 u32 hello_time = jiffies_to_clock_t(br->hello_time);
871 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +0100872 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
873 u32 stp_enabled = br->stp_enabled;
874 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200875 u16 group_fwd_mask = br->group_fwd_mask;
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300876 u8 vlan_enabled = br_vlan_enabled(br);
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +0200877 struct ifla_bridge_id root_id, bridge_id;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +0200878
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +0200879 memset(&bridge_id, 0, sizeof(bridge_id));
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +0200880 memset(&root_id, 0, sizeof(root_id));
881 memcpy(root_id.prio, br->designated_root.prio, sizeof(root_id.prio));
882 memcpy(root_id.addr, br->designated_root.addr, sizeof(root_id.addr));
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +0200883 memcpy(bridge_id.prio, br->bridge_id.prio, sizeof(bridge_id.prio));
884 memcpy(bridge_id.addr, br->bridge_id.addr, sizeof(bridge_id.addr));
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200885
886 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
887 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +0100888 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
889 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
890 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300891 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200892 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +0200893 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, group_fwd_mask) ||
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +0200894 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(root_id), &root_id) ||
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +0200895 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(bridge_id), &bridge_id) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +0200896 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +0200897 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
898 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
899 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
900 br->topology_change_detected))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200901 return -EMSGSIZE;
902
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900903#ifdef CONFIG_BRIDGE_VLAN_FILTERING
904 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto))
905 return -EMSGSIZE;
906#endif
907
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200908 return 0;
909}
910
Roopa Prabhufed0a152015-02-25 23:55:40 -0800911static size_t br_get_link_af_size(const struct net_device *dev)
912{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200913 struct net_bridge_port *p;
914 struct net_bridge *br;
915 int num_vlans = 0;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800916
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200917 if (br_port_exists(dev)) {
918 p = br_port_get_rtnl(dev);
919 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200920 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200921 } else if (dev->priv_flags & IFF_EBRIDGE) {
922 br = netdev_priv(dev);
923 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200924 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200925 }
Roopa Prabhufed0a152015-02-25 23:55:40 -0800926
927 /* Each VLAN is returned in bridge_vlan_info along with flags */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200928 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhufed0a152015-02-25 23:55:40 -0800929}
930
Daniel Borkmann207895f2015-01-29 12:15:03 +0100931static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000932 .family = AF_BRIDGE,
933 .get_link_af_size = br_get_link_af_size,
934};
935
stephen hemminger149ddd82012-06-26 05:48:45 +0000936struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +0200937 .kind = "bridge",
938 .priv_size = sizeof(struct net_bridge),
939 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -0700940 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +0200941 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +0200942 .validate = br_validate,
943 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +0200944 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200945 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200946 .get_size = br_get_size,
947 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200948
949 .slave_maxtype = IFLA_BRPORT_MAX,
950 .slave_policy = br_port_policy,
951 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200952 .get_slave_size = br_port_get_slave_size,
953 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +0000954};
955
956int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700957{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000958 int err;
959
960 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800961 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000962
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000963 err = rtnl_link_register(&br_link_ops);
964 if (err)
965 goto out_af;
966
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000967 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000968
969out_af:
970 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000971 br_mdb_uninit();
972 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700973}
974
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200975void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +0000976{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000977 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000978 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000979 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000980}