blob: e51332792c3c07e720cfa18b6bf34bc690d24196 [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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-09-25 19:00:11 +020078{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020079 int num_vlans;
80
Nikolay Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 Aleksandrov2594e9062015-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 */
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200130 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200131 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200132 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
133 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200134 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
135 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
stephen hemminger25c71c72012-11-13 07:53:05 +0000136 + 0;
137}
138
Roopa Prabhufed0a152015-02-25 23:55:40 -0800139static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800140{
141 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000142 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
143 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
144 + nla_total_size(4) /* IFLA_MASTER */
145 + nla_total_size(4) /* IFLA_MTU */
146 + nla_total_size(4) /* IFLA_LINK */
147 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800148 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800149 + nla_total_size(br_get_link_af_size_filtered(dev,
150 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000151}
152
153static int br_port_fill_attrs(struct sk_buff *skb,
154 const struct net_bridge_port *p)
155{
156 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
157
158 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
159 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
160 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000161 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000162 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500163 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400164 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400165 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700166 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200167 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
168 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200169 !!(p->flags & BR_PROXYARP_WIFI)) ||
170 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200171 &p->designated_root) ||
172 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200173 &p->designated_bridge) ||
174 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200175 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
176 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
177 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no))
stephen hemminger25c71c72012-11-13 07:53:05 +0000178 return -EMSGSIZE;
179
180 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800181}
182
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800183static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
184 u16 vid_end, u16 flags)
185{
186 struct bridge_vlan_info vinfo;
187
188 if ((vid_end - vid_start) > 0) {
189 /* add range to skb */
190 vinfo.vid = vid_start;
191 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
192 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
193 sizeof(vinfo), &vinfo))
194 goto nla_put_failure;
195
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800196 vinfo.vid = vid_end;
197 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
198 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
199 sizeof(vinfo), &vinfo))
200 goto nla_put_failure;
201 } else {
202 vinfo.vid = vid_start;
203 vinfo.flags = flags;
204 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
205 sizeof(vinfo), &vinfo))
206 goto nla_put_failure;
207 }
208
209 return 0;
210
211nla_put_failure:
212 return -EMSGSIZE;
213}
214
215static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200216 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800217{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200218 struct net_bridge_vlan *v;
219 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200220 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800221 int err = 0;
222
223 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
224 * and mark vlan info with begin and end flags
225 * if vlaninfo represents a range
226 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200227 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200228 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800229 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200230 if (!br_vlan_should_use(v))
231 continue;
232 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800233 flags |= BRIDGE_VLAN_INFO_PVID;
234
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200235 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800236 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
237
238 if (vid_range_start == 0) {
239 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200240 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800241 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200242 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800243 continue;
244 } else {
245 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
246 vid_range_end,
247 vid_range_flags);
248 if (err)
249 return err;
250 }
251
252initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200253 vid_range_start = v->vid;
254 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800255 vid_range_flags = flags;
256 }
257
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800258 if (vid_range_start != 0) {
259 /* Call it once more to send any left over vlans */
260 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
261 vid_range_end,
262 vid_range_flags);
263 if (err)
264 return err;
265 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800266
267 return 0;
268}
269
270static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200271 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800272{
273 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200274 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200275 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800276
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200277 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200278 list_for_each_entry(v, &vg->vlan_list, vlist) {
279 if (!br_vlan_should_use(v))
280 continue;
281
282 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800283 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200284 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800285 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
286
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200287 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800288 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
289
290 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
291 sizeof(vinfo), &vinfo))
292 goto nla_put_failure;
293 }
294
295 return 0;
296
297nla_put_failure:
298 return -EMSGSIZE;
299}
300
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700301/*
302 * Create one netlink message for one interface
303 * Contains port and master info as well as carrier and bridge state.
304 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000305static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200306 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000307 u32 pid, u32 seq, int event, unsigned int flags,
308 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700309{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200310 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800311 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700312 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700313 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700314
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000315 if (port)
316 br = port->br;
317 else
318 br = netdev_priv(dev);
319
stephen hemminger28a16c92010-05-10 09:31:09 +0000320 br_debug(br, "br_fill_info event %d port %s master %s\n",
321 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700322
Thomas Graf74685962006-11-20 16:20:22 -0800323 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
324 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800325 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700326
Thomas Graf74685962006-11-20 16:20:22 -0800327 hdr = nlmsg_data(nlh);
328 hdr->ifi_family = AF_BRIDGE;
329 hdr->__ifi_pad = 0;
330 hdr->ifi_type = dev->type;
331 hdr->ifi_index = dev->ifindex;
332 hdr->ifi_flags = dev_get_flags(dev);
333 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700334
David S. Miller2eb812e2012-04-01 20:49:54 -0400335 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
336 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
337 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
338 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
339 (dev->addr_len &&
340 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200341 (dev->ifindex != dev_get_iflink(dev) &&
342 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400343 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000344
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000345 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000346 struct nlattr *nest
347 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
348
349 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
350 goto nla_put_failure;
351 nla_nest_end(skb, nest);
352 }
353
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000354 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800355 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
356 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200357 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800358 struct nlattr *af;
359 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000360
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200361 if (port)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200362 vg = nbp_vlan_group(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200363 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200364 vg = br_vlan_group(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000365
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200366 if (!vg || !vg->num_vlans)
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000367 goto done;
368
369 af = nla_nest_start(skb, IFLA_AF_SPEC);
370 if (!af)
371 goto nla_put_failure;
372
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800373 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200374 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800375 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200376 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800377 if (err)
378 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000379 nla_nest_end(skb, af);
380 }
381
382done:
Johannes Berg053c0952015-01-16 22:09:00 +0100383 nlmsg_end(skb, nlh);
384 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700385
Thomas Graf74685962006-11-20 16:20:22 -0800386nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800387 nlmsg_cancel(skb, nlh);
388 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700389}
390
391/*
392 * Notify listeners of a change in port information
393 */
394void br_ifinfo_notify(int event, struct net_bridge_port *port)
395{
Vlad Yasevich407af322013-02-13 12:00:12 +0000396 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700397 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700398 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800399 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700400
Vlad Yasevich407af322013-02-13 12:00:12 +0000401 if (!port)
402 return;
403
404 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000405 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000406 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000407
Roopa Prabhufed0a152015-02-25 23:55:40 -0800408 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700409 if (skb == NULL)
410 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700411
Roopa Prabhufed0a152015-02-25 23:55:40 -0800412 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800413 if (err < 0) {
414 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
415 WARN_ON(err == -EMSGSIZE);
416 kfree_skb(skb);
417 goto errout;
418 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800419 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
420 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700421errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800422 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700423}
424
Vlad Yasevich407af322013-02-13 12:00:12 +0000425
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700426/*
427 * Dump information about all ports, in response to GETLINK
428 */
John Fastabende5a55a82012-10-24 08:12:57 +0000429int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200430 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700431{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800432 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700433
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800434 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
435 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300436 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000437
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200438 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300439 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700440}
441
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800442static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
443 int cmd, struct bridge_vlan_info *vinfo)
444{
445 int err = 0;
446
447 switch (cmd) {
448 case RTM_SETLINK:
449 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200450 /* if the MASTER flag is set this will act on the global
451 * per-VLAN entry as well
452 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800453 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
454 if (err)
455 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800456 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200457 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800458 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
459 }
460 break;
461
462 case RTM_DELLINK:
463 if (p) {
464 nbp_vlan_delete(p, vinfo->vid);
465 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
466 br_vlan_delete(p->br, vinfo->vid);
467 } else {
468 br_vlan_delete(br, vinfo->vid);
469 }
470 break;
471 }
472
473 return err;
474}
Vlad Yasevich407af322013-02-13 12:00:12 +0000475
476static int br_afspec(struct net_bridge *br,
477 struct net_bridge_port *p,
478 struct nlattr *af_spec,
479 int cmd)
480{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800481 struct bridge_vlan_info *vinfo_start = NULL;
482 struct bridge_vlan_info *vinfo = NULL;
483 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000484 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800485 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000486
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800487 nla_for_each_nested(attr, af_spec, rem) {
488 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
489 continue;
490 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000491 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800492 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700493 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
494 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800495 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
496 if (vinfo_start)
497 return -EINVAL;
498 vinfo_start = vinfo;
499 continue;
500 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000501
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800502 if (vinfo_start) {
503 struct bridge_vlan_info tmp_vinfo;
504 int v;
505
506 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
507 return -EINVAL;
508
509 if (vinfo->vid <= vinfo_start->vid)
510 return -EINVAL;
511
512 memcpy(&tmp_vinfo, vinfo_start,
513 sizeof(struct bridge_vlan_info));
514
515 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
516 tmp_vinfo.vid = v;
517 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000518 if (err)
519 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800520 }
521 vinfo_start = NULL;
522 } else {
523 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000524 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800525 if (err)
526 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000527 }
528
529 return err;
530}
531
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200532static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000533 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
534 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
535 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
536 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000537 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000538 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100539 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400540 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400541 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200542 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200543 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000544};
545
546/* Change the state of the port and notify spanning tree */
547static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700548{
stephen hemminger25c71c72012-11-13 07:53:05 +0000549 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000550 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700551
552 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700553 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700554 return -EBUSY;
555
stephen hemminger576eb622012-12-28 18:15:22 +0000556 /* if device is not up, change is not allowed
557 * if link is not present, only allowable state is disabled
558 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000559 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000560 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700561 return -ENETDOWN;
562
Florian Fainelli775dd692014-09-30 16:13:19 -0700563 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700564 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000565 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700566 return 0;
567}
568
stephen hemminger25c71c72012-11-13 07:53:05 +0000569/* Set/clear or port flags based on attribute */
570static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
571 int attrtype, unsigned long mask)
572{
573 if (tb[attrtype]) {
574 u8 flag = nla_get_u8(tb[attrtype]);
575 if (flag)
576 p->flags |= mask;
577 else
578 p->flags &= ~mask;
579 }
580}
581
582/* Process bridge protocol info on port */
583static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
584{
585 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400586 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000587
588 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000589 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500590 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000591 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400592 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400593 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700594 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200595 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000596
597 if (tb[IFLA_BRPORT_COST]) {
598 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
599 if (err)
600 return err;
601 }
602
603 if (tb[IFLA_BRPORT_PRIORITY]) {
604 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
605 if (err)
606 return err;
607 }
608
609 if (tb[IFLA_BRPORT_STATE]) {
610 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
611 if (err)
612 return err;
613 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400614
615 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000616 return 0;
617}
618
619/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800620int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000621{
stephen hemminger25c71c72012-11-13 07:53:05 +0000622 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000623 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000624 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000625 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700626 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000627
Hong zhi guoc60ee672013-03-28 06:21:22 +0000628 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
629 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000630 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000631 return 0;
632
633 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000634 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800635 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000636 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000637 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000638 return -EINVAL;
639
Vlad Yasevich407af322013-02-13 12:00:12 +0000640 if (p && protinfo) {
641 if (protinfo->nla_type & NLA_F_NESTED) {
642 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200643 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000644 if (err)
645 return err;
646
647 spin_lock_bh(&p->br->lock);
648 err = br_setport(p, tb);
649 spin_unlock_bh(&p->br->lock);
650 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800651 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000652 if (nla_len(protinfo) < sizeof(u8))
653 return -EINVAL;
654
655 spin_lock_bh(&p->br->lock);
656 err = br_set_port_state(p, nla_get_u8(protinfo));
657 spin_unlock_bh(&p->br->lock);
658 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000659 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000660 goto out;
661 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000662
Vlad Yasevich407af322013-02-13 12:00:12 +0000663 if (afspec) {
664 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
665 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000666 }
667
668 if (err == 0)
669 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000670out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000671 return err;
672}
673
Vlad Yasevich407af322013-02-13 12:00:12 +0000674/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800675int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000676{
Vlad Yasevich407af322013-02-13 12:00:12 +0000677 struct nlattr *afspec;
678 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700679 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000680
Hong zhi guoc60ee672013-03-28 06:21:22 +0000681 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000682 if (!afspec)
683 return 0;
684
685 p = br_port_get_rtnl(dev);
686 /* We want to accept dev as bridge itself as well */
687 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
688 return -EINVAL;
689
690 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
691 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800692 if (err == 0)
693 /* Send RTM_NEWLINK because userspace
694 * expects RTM_NEWLINK for vlan dels
695 */
696 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000697
698 return err;
699}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000700static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700701{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000702 if (tb[IFLA_ADDRESS]) {
703 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
704 return -EINVAL;
705 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
706 return -EADDRNOTAVAIL;
707 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700708
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900709 if (!data)
710 return 0;
711
712#ifdef CONFIG_BRIDGE_VLAN_FILTERING
713 if (data[IFLA_BR_VLAN_PROTOCOL]) {
714 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
715 case htons(ETH_P_8021Q):
716 case htons(ETH_P_8021AD):
717 break;
718 default:
719 return -EPROTONOSUPPORT;
720 }
721 }
722#endif
723
Thomas Graf32fe21c2007-03-22 11:59:03 -0700724 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700725}
726
Toshiaki Makita30313a32014-04-25 17:01:18 +0900727static int br_dev_newlink(struct net *src_net, struct net_device *dev,
728 struct nlattr *tb[], struct nlattr *data[])
729{
730 struct net_bridge *br = netdev_priv(dev);
731
732 if (tb[IFLA_ADDRESS]) {
733 spin_lock_bh(&br->lock);
734 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
735 spin_unlock_bh(&br->lock);
736 }
737
738 return register_netdevice(dev);
739}
740
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200741static int br_port_slave_changelink(struct net_device *brdev,
742 struct net_device *dev,
743 struct nlattr *tb[],
744 struct nlattr *data[])
745{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200746 struct net_bridge *br = netdev_priv(brdev);
747 int ret;
748
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200749 if (!data)
750 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200751
752 spin_lock_bh(&br->lock);
753 ret = br_setport(br_port_get_rtnl(dev), data);
754 spin_unlock_bh(&br->lock);
755
756 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200757}
758
Jiri Pirkoced82832014-09-05 15:51:29 +0200759static int br_port_fill_slave_info(struct sk_buff *skb,
760 const struct net_device *brdev,
761 const struct net_device *dev)
762{
763 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
764}
765
766static size_t br_port_get_slave_size(const struct net_device *brdev,
767 const struct net_device *dev)
768{
769 return br_port_info_size();
770}
771
Jiri Pirko13323512014-09-05 15:51:32 +0200772static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
773 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
774 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
775 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100776 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
777 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
778 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300779 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900780 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200781 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200782 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
783 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200784 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200785 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200786 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200787 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200788 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200789 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200790 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200791 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200792 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
793 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
794 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
795 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
796 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
797 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200798 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
799 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
800 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200801 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Jiri Pirko13323512014-09-05 15:51:32 +0200802};
803
804static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
805 struct nlattr *data[])
806{
807 struct net_bridge *br = netdev_priv(brdev);
808 int err;
809
810 if (!data)
811 return 0;
812
813 if (data[IFLA_BR_FORWARD_DELAY]) {
814 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
815 if (err)
816 return err;
817 }
818
819 if (data[IFLA_BR_HELLO_TIME]) {
820 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
821 if (err)
822 return err;
823 }
824
825 if (data[IFLA_BR_MAX_AGE]) {
826 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
827 if (err)
828 return err;
829 }
830
Jörg Thalheimaf615762015-03-18 10:06:58 +0100831 if (data[IFLA_BR_AGEING_TIME]) {
832 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
833
834 br->ageing_time = clock_t_to_jiffies(ageing_time);
835 }
836
837 if (data[IFLA_BR_STP_STATE]) {
838 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
839
840 br_stp_set_enabled(br, stp_enabled);
841 }
842
843 if (data[IFLA_BR_PRIORITY]) {
844 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
845
846 br_stp_set_bridge_priority(br, priority);
847 }
848
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300849 if (data[IFLA_BR_VLAN_FILTERING]) {
850 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
851
852 err = __br_vlan_filter_toggle(br, vlan_filter);
853 if (err)
854 return err;
855 }
856
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900857#ifdef CONFIG_BRIDGE_VLAN_FILTERING
858 if (data[IFLA_BR_VLAN_PROTOCOL]) {
859 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
860
861 err = __br_vlan_set_proto(br, vlan_proto);
862 if (err)
863 return err;
864 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200865
866 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
867 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
868
869 err = __br_vlan_set_default_pvid(br, defpvid);
870 if (err)
871 return err;
872 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900873#endif
874
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200875 if (data[IFLA_BR_GROUP_FWD_MASK]) {
876 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
877
878 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
879 return -EINVAL;
880 br->group_fwd_mask = fwd_mask;
881 }
882
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200883 if (data[IFLA_BR_GROUP_ADDR]) {
884 u8 new_addr[ETH_ALEN];
885
886 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
887 return -EINVAL;
888 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
889 if (!is_link_local_ether_addr(new_addr))
890 return -EINVAL;
891 if (new_addr[5] == 1 || /* 802.3x Pause address */
892 new_addr[5] == 2 || /* 802.3ad Slow protocols */
893 new_addr[5] == 3) /* 802.1X PAE address */
894 return -EINVAL;
895 spin_lock_bh(&br->lock);
896 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
897 spin_unlock_bh(&br->lock);
898 br->group_addr_set = true;
899 br_recalculate_fwd_mask(br);
900 }
901
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +0200902 if (data[IFLA_BR_FDB_FLUSH])
903 br_fdb_flush(br);
904
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200905#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
906 if (data[IFLA_BR_MCAST_ROUTER]) {
907 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
908
909 err = br_multicast_set_router(br, multicast_router);
910 if (err)
911 return err;
912 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200913
914 if (data[IFLA_BR_MCAST_SNOOPING]) {
915 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
916
917 err = br_multicast_toggle(br, mcast_snooping);
918 if (err)
919 return err;
920 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200921
922 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
923 u8 val;
924
925 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
926 br->multicast_query_use_ifaddr = !!val;
927 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200928
929 if (data[IFLA_BR_MCAST_QUERIER]) {
930 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
931
932 err = br_multicast_set_querier(br, mcast_querier);
933 if (err)
934 return err;
935 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200936
937 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
938 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
939
940 br->hash_elasticity = val;
941 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200942
943 if (data[IFLA_BR_MCAST_HASH_MAX]) {
944 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
945
946 err = br_multicast_set_hash_max(br, hash_max);
947 if (err)
948 return err;
949 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200950
951 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
952 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
953
954 br->multicast_last_member_count = val;
955 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200956
957 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
958 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
959
960 br->multicast_startup_query_count = val;
961 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200962
963 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
964 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
965
966 br->multicast_last_member_interval = clock_t_to_jiffies(val);
967 }
968
969 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
970 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
971
972 br->multicast_membership_interval = clock_t_to_jiffies(val);
973 }
974
975 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
976 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
977
978 br->multicast_querier_interval = clock_t_to_jiffies(val);
979 }
980
981 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
982 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
983
984 br->multicast_query_interval = clock_t_to_jiffies(val);
985 }
986
987 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
988 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
989
990 br->multicast_query_response_interval = clock_t_to_jiffies(val);
991 }
992
993 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
994 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
995
996 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
997 }
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200998#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200999#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1000 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1001 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1002
1003 br->nf_call_iptables = val ? true : false;
1004 }
1005
1006 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1007 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1008
1009 br->nf_call_ip6tables = val ? true : false;
1010 }
1011
1012 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1013 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1014
1015 br->nf_call_arptables = val ? true : false;
1016 }
1017#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001018
Jiri Pirko13323512014-09-05 15:51:32 +02001019 return 0;
1020}
1021
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001022static size_t br_get_size(const struct net_device *brdev)
1023{
1024 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1025 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1026 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001027 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1028 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1029 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001030 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001031#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1032 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001033 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001034#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001035 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001036 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001037 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001038 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001039 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001040 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1041 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001042 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1043 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1044 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1045 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001046 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001047#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1048 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001049 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001050 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001051 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001052 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1053 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1054 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1055 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001056 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1057 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1058 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1059 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1060 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1061 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001062#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001063#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1064 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1065 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1066 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1067#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001068 0;
1069}
1070
1071static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1072{
1073 struct net_bridge *br = netdev_priv(brdev);
1074 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1075 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1076 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001077 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1078 u32 stp_enabled = br->stp_enabled;
1079 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001080 u8 vlan_enabled = br_vlan_enabled(br);
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001081 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001082
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001083 clockval = br_timer_value(&br->hello_timer);
1084 if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
1085 return -EMSGSIZE;
1086 clockval = br_timer_value(&br->tcn_timer);
1087 if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
1088 return -EMSGSIZE;
1089 clockval = br_timer_value(&br->topology_change_timer);
1090 if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
1091 return -EMSGSIZE;
1092 clockval = br_timer_value(&br->gc_timer);
1093 if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
1094 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001095
1096 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1097 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001098 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1099 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1100 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001101 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001102 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001103 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1104 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1105 &br->bridge_id) ||
1106 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1107 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001108 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001109 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1110 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1111 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001112 br->topology_change_detected) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001113 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001114 return -EMSGSIZE;
1115
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001116#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001117 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1118 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001119 return -EMSGSIZE;
1120#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001121#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001122 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001123 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1124 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001125 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001126 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1127 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001128 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001129 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1130 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001131 br->multicast_last_member_count) ||
1132 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1133 br->multicast_startup_query_count))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001134 return -EMSGSIZE;
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001135
1136 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1137 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1138 return -EMSGSIZE;
1139 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1140 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1141 return -EMSGSIZE;
1142 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1143 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1144 return -EMSGSIZE;
1145 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1146 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1147 return -EMSGSIZE;
1148 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1149 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1150 return -EMSGSIZE;
1151 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1152 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1153 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001154#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001155#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1156 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1157 br->nf_call_iptables ? 1 : 0) ||
1158 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1159 br->nf_call_ip6tables ? 1 : 0) ||
1160 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1161 br->nf_call_arptables ? 1 : 0))
1162 return -EMSGSIZE;
1163#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001164
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001165 return 0;
1166}
1167
Roopa Prabhufed0a152015-02-25 23:55:40 -08001168static size_t br_get_link_af_size(const struct net_device *dev)
1169{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001170 struct net_bridge_port *p;
1171 struct net_bridge *br;
1172 int num_vlans = 0;
Roopa Prabhufed0a152015-02-25 23:55:40 -08001173
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001174 if (br_port_exists(dev)) {
1175 p = br_port_get_rtnl(dev);
1176 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001177 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001178 } else if (dev->priv_flags & IFF_EBRIDGE) {
1179 br = netdev_priv(dev);
1180 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001181 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001182 }
Roopa Prabhufed0a152015-02-25 23:55:40 -08001183
1184 /* Each VLAN is returned in bridge_vlan_info along with flags */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001185 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhufed0a152015-02-25 23:55:40 -08001186}
1187
Daniel Borkmann207895f2015-01-29 12:15:03 +01001188static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001189 .family = AF_BRIDGE,
1190 .get_link_af_size = br_get_link_af_size,
1191};
1192
stephen hemminger149ddd82012-06-26 05:48:45 +00001193struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001194 .kind = "bridge",
1195 .priv_size = sizeof(struct net_bridge),
1196 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001197 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001198 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001199 .validate = br_validate,
1200 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001201 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001202 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001203 .get_size = br_get_size,
1204 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001205
1206 .slave_maxtype = IFLA_BRPORT_MAX,
1207 .slave_policy = br_port_policy,
1208 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001209 .get_slave_size = br_port_get_slave_size,
1210 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001211};
1212
1213int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001214{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001215 int err;
1216
1217 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001218 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001219
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001220 err = rtnl_link_register(&br_link_ops);
1221 if (err)
1222 goto out_af;
1223
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001224 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001225
1226out_af:
1227 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001228 br_mdb_uninit();
1229 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001230}
1231
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001232void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001233{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001234 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001235 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001236 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001237}