blob: 678d22747044f62748ce9c43891069b46c2a56cd [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 */
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 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000134 + 0;
135}
136
Roopa Prabhufed0a152015-02-25 23:55:40 -0800137static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800138{
139 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000140 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
141 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
142 + nla_total_size(4) /* IFLA_MASTER */
143 + nla_total_size(4) /* IFLA_MTU */
144 + nla_total_size(4) /* IFLA_LINK */
145 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800146 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800147 + nla_total_size(br_get_link_af_size_filtered(dev,
148 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000149}
150
151static int br_port_fill_attrs(struct sk_buff *skb,
152 const struct net_bridge_port *p)
153{
154 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
155
156 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
157 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
158 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000159 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000160 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500161 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400162 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400163 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700164 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200165 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
166 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200167 !!(p->flags & BR_PROXYARP_WIFI)) ||
168 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200169 &p->designated_root) ||
170 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200171 &p->designated_bridge) ||
172 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
173 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost))
stephen hemminger25c71c72012-11-13 07:53:05 +0000174 return -EMSGSIZE;
175
176 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800177}
178
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800179static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
180 u16 vid_end, u16 flags)
181{
182 struct bridge_vlan_info vinfo;
183
184 if ((vid_end - vid_start) > 0) {
185 /* add range to skb */
186 vinfo.vid = vid_start;
187 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
188 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
189 sizeof(vinfo), &vinfo))
190 goto nla_put_failure;
191
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800192 vinfo.vid = vid_end;
193 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
194 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
195 sizeof(vinfo), &vinfo))
196 goto nla_put_failure;
197 } else {
198 vinfo.vid = vid_start;
199 vinfo.flags = flags;
200 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
201 sizeof(vinfo), &vinfo))
202 goto nla_put_failure;
203 }
204
205 return 0;
206
207nla_put_failure:
208 return -EMSGSIZE;
209}
210
211static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200212 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800213{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200214 struct net_bridge_vlan *v;
215 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200216 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800217 int err = 0;
218
219 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
220 * and mark vlan info with begin and end flags
221 * if vlaninfo represents a range
222 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200223 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200224 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800225 flags = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200226 if (!br_vlan_should_use(v))
227 continue;
228 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800229 flags |= BRIDGE_VLAN_INFO_PVID;
230
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200231 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800232 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
233
234 if (vid_range_start == 0) {
235 goto initvars;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200236 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800237 flags == vid_range_flags) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200238 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800239 continue;
240 } else {
241 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
242 vid_range_end,
243 vid_range_flags);
244 if (err)
245 return err;
246 }
247
248initvars:
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200249 vid_range_start = v->vid;
250 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800251 vid_range_flags = flags;
252 }
253
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800254 if (vid_range_start != 0) {
255 /* Call it once more to send any left over vlans */
256 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
257 vid_range_end,
258 vid_range_flags);
259 if (err)
260 return err;
261 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800262
263 return 0;
264}
265
266static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200267 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800268{
269 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200270 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200271 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800272
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200273 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200274 list_for_each_entry(v, &vg->vlan_list, vlist) {
275 if (!br_vlan_should_use(v))
276 continue;
277
278 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800279 vinfo.flags = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200280 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800281 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
282
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200283 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800284 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
285
286 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
287 sizeof(vinfo), &vinfo))
288 goto nla_put_failure;
289 }
290
291 return 0;
292
293nla_put_failure:
294 return -EMSGSIZE;
295}
296
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700297/*
298 * Create one netlink message for one interface
299 * Contains port and master info as well as carrier and bridge state.
300 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000301static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200302 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000303 u32 pid, u32 seq, int event, unsigned int flags,
304 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700305{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200306 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800307 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700308 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700309 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700310
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000311 if (port)
312 br = port->br;
313 else
314 br = netdev_priv(dev);
315
stephen hemminger28a16c92010-05-10 09:31:09 +0000316 br_debug(br, "br_fill_info event %d port %s master %s\n",
317 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700318
Thomas Graf74685962006-11-20 16:20:22 -0800319 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
320 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800321 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700322
Thomas Graf74685962006-11-20 16:20:22 -0800323 hdr = nlmsg_data(nlh);
324 hdr->ifi_family = AF_BRIDGE;
325 hdr->__ifi_pad = 0;
326 hdr->ifi_type = dev->type;
327 hdr->ifi_index = dev->ifindex;
328 hdr->ifi_flags = dev_get_flags(dev);
329 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700330
David S. Miller2eb812e2012-04-01 20:49:54 -0400331 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
332 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
333 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
334 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
335 (dev->addr_len &&
336 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200337 (dev->ifindex != dev_get_iflink(dev) &&
338 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400339 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000340
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000341 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000342 struct nlattr *nest
343 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
344
345 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
346 goto nla_put_failure;
347 nla_nest_end(skb, nest);
348 }
349
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000350 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800351 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
352 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200353 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800354 struct nlattr *af;
355 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000356
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200357 if (port)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200358 vg = nbp_vlan_group(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200359 else
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200360 vg = br_vlan_group(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000361
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200362 if (!vg || !vg->num_vlans)
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000363 goto done;
364
365 af = nla_nest_start(skb, IFLA_AF_SPEC);
366 if (!af)
367 goto nla_put_failure;
368
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800369 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200370 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800371 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200372 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800373 if (err)
374 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000375 nla_nest_end(skb, af);
376 }
377
378done:
Johannes Berg053c0952015-01-16 22:09:00 +0100379 nlmsg_end(skb, nlh);
380 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700381
Thomas Graf74685962006-11-20 16:20:22 -0800382nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800383 nlmsg_cancel(skb, nlh);
384 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700385}
386
387/*
388 * Notify listeners of a change in port information
389 */
390void br_ifinfo_notify(int event, struct net_bridge_port *port)
391{
Vlad Yasevich407af322013-02-13 12:00:12 +0000392 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700393 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700394 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800395 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700396
Vlad Yasevich407af322013-02-13 12:00:12 +0000397 if (!port)
398 return;
399
400 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000401 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000402 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000403
Roopa Prabhufed0a152015-02-25 23:55:40 -0800404 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700405 if (skb == NULL)
406 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700407
Roopa Prabhufed0a152015-02-25 23:55:40 -0800408 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800409 if (err < 0) {
410 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
411 WARN_ON(err == -EMSGSIZE);
412 kfree_skb(skb);
413 goto errout;
414 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800415 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
416 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700417errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800418 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700419}
420
Vlad Yasevich407af322013-02-13 12:00:12 +0000421
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700422/*
423 * Dump information about all ports, in response to GETLINK
424 */
John Fastabende5a55a82012-10-24 08:12:57 +0000425int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200426 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700427{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800428 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700429
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800430 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
431 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300432 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000433
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200434 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300435 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700436}
437
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800438static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
439 int cmd, struct bridge_vlan_info *vinfo)
440{
441 int err = 0;
442
443 switch (cmd) {
444 case RTM_SETLINK:
445 if (p) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200446 /* if the MASTER flag is set this will act on the global
447 * per-VLAN entry as well
448 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800449 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
450 if (err)
451 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800452 } else {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200453 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800454 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
455 }
456 break;
457
458 case RTM_DELLINK:
459 if (p) {
460 nbp_vlan_delete(p, vinfo->vid);
461 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
462 br_vlan_delete(p->br, vinfo->vid);
463 } else {
464 br_vlan_delete(br, vinfo->vid);
465 }
466 break;
467 }
468
469 return err;
470}
Vlad Yasevich407af322013-02-13 12:00:12 +0000471
472static int br_afspec(struct net_bridge *br,
473 struct net_bridge_port *p,
474 struct nlattr *af_spec,
475 int cmd)
476{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800477 struct bridge_vlan_info *vinfo_start = NULL;
478 struct bridge_vlan_info *vinfo = NULL;
479 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000480 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800481 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000482
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800483 nla_for_each_nested(attr, af_spec, rem) {
484 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
485 continue;
486 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000487 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800488 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700489 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
490 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800491 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
492 if (vinfo_start)
493 return -EINVAL;
494 vinfo_start = vinfo;
495 continue;
496 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000497
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800498 if (vinfo_start) {
499 struct bridge_vlan_info tmp_vinfo;
500 int v;
501
502 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
503 return -EINVAL;
504
505 if (vinfo->vid <= vinfo_start->vid)
506 return -EINVAL;
507
508 memcpy(&tmp_vinfo, vinfo_start,
509 sizeof(struct bridge_vlan_info));
510
511 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
512 tmp_vinfo.vid = v;
513 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000514 if (err)
515 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800516 }
517 vinfo_start = NULL;
518 } else {
519 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000520 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800521 if (err)
522 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000523 }
524
525 return err;
526}
527
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200528static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000529 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
530 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
531 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
532 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000533 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000534 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100535 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400536 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400537 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200538 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200539 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000540};
541
542/* Change the state of the port and notify spanning tree */
543static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700544{
stephen hemminger25c71c72012-11-13 07:53:05 +0000545 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000546 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700547
548 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700549 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700550 return -EBUSY;
551
stephen hemminger576eb622012-12-28 18:15:22 +0000552 /* if device is not up, change is not allowed
553 * if link is not present, only allowable state is disabled
554 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000555 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000556 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700557 return -ENETDOWN;
558
Florian Fainelli775dd692014-09-30 16:13:19 -0700559 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700560 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000561 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700562 return 0;
563}
564
stephen hemminger25c71c72012-11-13 07:53:05 +0000565/* Set/clear or port flags based on attribute */
566static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
567 int attrtype, unsigned long mask)
568{
569 if (tb[attrtype]) {
570 u8 flag = nla_get_u8(tb[attrtype]);
571 if (flag)
572 p->flags |= mask;
573 else
574 p->flags &= ~mask;
575 }
576}
577
578/* Process bridge protocol info on port */
579static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
580{
581 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400582 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000583
584 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000585 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500586 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000587 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400588 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400589 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700590 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200591 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000592
593 if (tb[IFLA_BRPORT_COST]) {
594 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
595 if (err)
596 return err;
597 }
598
599 if (tb[IFLA_BRPORT_PRIORITY]) {
600 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
601 if (err)
602 return err;
603 }
604
605 if (tb[IFLA_BRPORT_STATE]) {
606 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
607 if (err)
608 return err;
609 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400610
611 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000612 return 0;
613}
614
615/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800616int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000617{
stephen hemminger25c71c72012-11-13 07:53:05 +0000618 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000619 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000620 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000621 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700622 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000623
Hong zhi guoc60ee672013-03-28 06:21:22 +0000624 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
625 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000626 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000627 return 0;
628
629 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000630 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800631 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000632 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000633 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000634 return -EINVAL;
635
Vlad Yasevich407af322013-02-13 12:00:12 +0000636 if (p && protinfo) {
637 if (protinfo->nla_type & NLA_F_NESTED) {
638 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200639 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000640 if (err)
641 return err;
642
643 spin_lock_bh(&p->br->lock);
644 err = br_setport(p, tb);
645 spin_unlock_bh(&p->br->lock);
646 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800647 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000648 if (nla_len(protinfo) < sizeof(u8))
649 return -EINVAL;
650
651 spin_lock_bh(&p->br->lock);
652 err = br_set_port_state(p, nla_get_u8(protinfo));
653 spin_unlock_bh(&p->br->lock);
654 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000655 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000656 goto out;
657 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000658
Vlad Yasevich407af322013-02-13 12:00:12 +0000659 if (afspec) {
660 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
661 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000662 }
663
664 if (err == 0)
665 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000666out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000667 return err;
668}
669
Vlad Yasevich407af322013-02-13 12:00:12 +0000670/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800671int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000672{
Vlad Yasevich407af322013-02-13 12:00:12 +0000673 struct nlattr *afspec;
674 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700675 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000676
Hong zhi guoc60ee672013-03-28 06:21:22 +0000677 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000678 if (!afspec)
679 return 0;
680
681 p = br_port_get_rtnl(dev);
682 /* We want to accept dev as bridge itself as well */
683 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
684 return -EINVAL;
685
686 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
687 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800688 if (err == 0)
689 /* Send RTM_NEWLINK because userspace
690 * expects RTM_NEWLINK for vlan dels
691 */
692 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000693
694 return err;
695}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000696static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700697{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000698 if (tb[IFLA_ADDRESS]) {
699 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
700 return -EINVAL;
701 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
702 return -EADDRNOTAVAIL;
703 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700704
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900705 if (!data)
706 return 0;
707
708#ifdef CONFIG_BRIDGE_VLAN_FILTERING
709 if (data[IFLA_BR_VLAN_PROTOCOL]) {
710 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
711 case htons(ETH_P_8021Q):
712 case htons(ETH_P_8021AD):
713 break;
714 default:
715 return -EPROTONOSUPPORT;
716 }
717 }
718#endif
719
Thomas Graf32fe21c2007-03-22 11:59:03 -0700720 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700721}
722
Toshiaki Makita30313a32014-04-25 17:01:18 +0900723static int br_dev_newlink(struct net *src_net, struct net_device *dev,
724 struct nlattr *tb[], struct nlattr *data[])
725{
726 struct net_bridge *br = netdev_priv(dev);
727
728 if (tb[IFLA_ADDRESS]) {
729 spin_lock_bh(&br->lock);
730 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
731 spin_unlock_bh(&br->lock);
732 }
733
734 return register_netdevice(dev);
735}
736
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200737static int br_port_slave_changelink(struct net_device *brdev,
738 struct net_device *dev,
739 struct nlattr *tb[],
740 struct nlattr *data[])
741{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200742 struct net_bridge *br = netdev_priv(brdev);
743 int ret;
744
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200745 if (!data)
746 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200747
748 spin_lock_bh(&br->lock);
749 ret = br_setport(br_port_get_rtnl(dev), data);
750 spin_unlock_bh(&br->lock);
751
752 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200753}
754
Jiri Pirkoced82832014-09-05 15:51:29 +0200755static int br_port_fill_slave_info(struct sk_buff *skb,
756 const struct net_device *brdev,
757 const struct net_device *dev)
758{
759 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
760}
761
762static size_t br_port_get_slave_size(const struct net_device *brdev,
763 const struct net_device *dev)
764{
765 return br_port_info_size();
766}
767
Jiri Pirko13323512014-09-05 15:51:32 +0200768static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
769 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
770 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
771 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100772 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
773 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
774 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300775 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900776 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200777 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200778 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
779 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200780 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200781 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200782 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200783 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200784 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200785 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200786 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200787 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200788 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
789 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
790 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
791 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
792 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
793 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200794 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
795 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
796 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200797 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Jiri Pirko13323512014-09-05 15:51:32 +0200798};
799
800static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
801 struct nlattr *data[])
802{
803 struct net_bridge *br = netdev_priv(brdev);
804 int err;
805
806 if (!data)
807 return 0;
808
809 if (data[IFLA_BR_FORWARD_DELAY]) {
810 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
811 if (err)
812 return err;
813 }
814
815 if (data[IFLA_BR_HELLO_TIME]) {
816 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
817 if (err)
818 return err;
819 }
820
821 if (data[IFLA_BR_MAX_AGE]) {
822 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
823 if (err)
824 return err;
825 }
826
Jörg Thalheimaf615762015-03-18 10:06:58 +0100827 if (data[IFLA_BR_AGEING_TIME]) {
828 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
829
830 br->ageing_time = clock_t_to_jiffies(ageing_time);
831 }
832
833 if (data[IFLA_BR_STP_STATE]) {
834 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
835
836 br_stp_set_enabled(br, stp_enabled);
837 }
838
839 if (data[IFLA_BR_PRIORITY]) {
840 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
841
842 br_stp_set_bridge_priority(br, priority);
843 }
844
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300845 if (data[IFLA_BR_VLAN_FILTERING]) {
846 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
847
848 err = __br_vlan_filter_toggle(br, vlan_filter);
849 if (err)
850 return err;
851 }
852
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900853#ifdef CONFIG_BRIDGE_VLAN_FILTERING
854 if (data[IFLA_BR_VLAN_PROTOCOL]) {
855 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
856
857 err = __br_vlan_set_proto(br, vlan_proto);
858 if (err)
859 return err;
860 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200861
862 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
863 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
864
865 err = __br_vlan_set_default_pvid(br, defpvid);
866 if (err)
867 return err;
868 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900869#endif
870
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200871 if (data[IFLA_BR_GROUP_FWD_MASK]) {
872 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
873
874 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
875 return -EINVAL;
876 br->group_fwd_mask = fwd_mask;
877 }
878
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200879 if (data[IFLA_BR_GROUP_ADDR]) {
880 u8 new_addr[ETH_ALEN];
881
882 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
883 return -EINVAL;
884 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
885 if (!is_link_local_ether_addr(new_addr))
886 return -EINVAL;
887 if (new_addr[5] == 1 || /* 802.3x Pause address */
888 new_addr[5] == 2 || /* 802.3ad Slow protocols */
889 new_addr[5] == 3) /* 802.1X PAE address */
890 return -EINVAL;
891 spin_lock_bh(&br->lock);
892 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
893 spin_unlock_bh(&br->lock);
894 br->group_addr_set = true;
895 br_recalculate_fwd_mask(br);
896 }
897
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +0200898 if (data[IFLA_BR_FDB_FLUSH])
899 br_fdb_flush(br);
900
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200901#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
902 if (data[IFLA_BR_MCAST_ROUTER]) {
903 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
904
905 err = br_multicast_set_router(br, multicast_router);
906 if (err)
907 return err;
908 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200909
910 if (data[IFLA_BR_MCAST_SNOOPING]) {
911 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
912
913 err = br_multicast_toggle(br, mcast_snooping);
914 if (err)
915 return err;
916 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200917
918 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
919 u8 val;
920
921 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
922 br->multicast_query_use_ifaddr = !!val;
923 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200924
925 if (data[IFLA_BR_MCAST_QUERIER]) {
926 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
927
928 err = br_multicast_set_querier(br, mcast_querier);
929 if (err)
930 return err;
931 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200932
933 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
934 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
935
936 br->hash_elasticity = val;
937 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200938
939 if (data[IFLA_BR_MCAST_HASH_MAX]) {
940 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
941
942 err = br_multicast_set_hash_max(br, hash_max);
943 if (err)
944 return err;
945 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200946
947 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
948 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
949
950 br->multicast_last_member_count = val;
951 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200952
953 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
954 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
955
956 br->multicast_startup_query_count = val;
957 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200958
959 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
960 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
961
962 br->multicast_last_member_interval = clock_t_to_jiffies(val);
963 }
964
965 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
966 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
967
968 br->multicast_membership_interval = clock_t_to_jiffies(val);
969 }
970
971 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
972 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
973
974 br->multicast_querier_interval = clock_t_to_jiffies(val);
975 }
976
977 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
978 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
979
980 br->multicast_query_interval = clock_t_to_jiffies(val);
981 }
982
983 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
984 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
985
986 br->multicast_query_response_interval = clock_t_to_jiffies(val);
987 }
988
989 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
990 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
991
992 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
993 }
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200994#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200995#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
996 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
997 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
998
999 br->nf_call_iptables = val ? true : false;
1000 }
1001
1002 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1003 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1004
1005 br->nf_call_ip6tables = val ? true : false;
1006 }
1007
1008 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1009 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1010
1011 br->nf_call_arptables = val ? true : false;
1012 }
1013#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001014
Jiri Pirko13323512014-09-05 15:51:32 +02001015 return 0;
1016}
1017
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001018static size_t br_get_size(const struct net_device *brdev)
1019{
1020 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1021 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1022 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001023 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1024 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1025 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001026 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001027#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1028 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001029 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001030#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001031 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001032 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001033 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001034 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001035 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001036 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1037 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001038 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1039 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1040 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1041 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001042 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001043#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1044 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001045 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001046 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001047 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001048 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1049 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1050 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1051 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001052 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1053 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1054 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1055 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1056 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1057 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001058#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001059#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1060 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1061 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1062 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1063#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001064 0;
1065}
1066
1067static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1068{
1069 struct net_bridge *br = netdev_priv(brdev);
1070 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1071 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1072 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001073 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1074 u32 stp_enabled = br->stp_enabled;
1075 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001076 u8 vlan_enabled = br_vlan_enabled(br);
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001077 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001078
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001079 clockval = br_timer_value(&br->hello_timer);
1080 if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
1081 return -EMSGSIZE;
1082 clockval = br_timer_value(&br->tcn_timer);
1083 if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
1084 return -EMSGSIZE;
1085 clockval = br_timer_value(&br->topology_change_timer);
1086 if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
1087 return -EMSGSIZE;
1088 clockval = br_timer_value(&br->gc_timer);
1089 if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
1090 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001091
1092 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1093 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001094 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1095 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1096 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001097 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001098 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001099 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1100 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1101 &br->bridge_id) ||
1102 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1103 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001104 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001105 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1106 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1107 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001108 br->topology_change_detected) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001109 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001110 return -EMSGSIZE;
1111
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001112#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001113 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1114 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001115 return -EMSGSIZE;
1116#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001117#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001118 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001119 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1120 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001121 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001122 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1123 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001124 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001125 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1126 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001127 br->multicast_last_member_count) ||
1128 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1129 br->multicast_startup_query_count))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001130 return -EMSGSIZE;
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001131
1132 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1133 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1134 return -EMSGSIZE;
1135 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1136 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1137 return -EMSGSIZE;
1138 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1139 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1140 return -EMSGSIZE;
1141 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1142 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1143 return -EMSGSIZE;
1144 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1145 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1146 return -EMSGSIZE;
1147 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1148 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1149 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001150#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001151#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1152 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1153 br->nf_call_iptables ? 1 : 0) ||
1154 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1155 br->nf_call_ip6tables ? 1 : 0) ||
1156 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1157 br->nf_call_arptables ? 1 : 0))
1158 return -EMSGSIZE;
1159#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001160
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001161 return 0;
1162}
1163
Roopa Prabhufed0a152015-02-25 23:55:40 -08001164static size_t br_get_link_af_size(const struct net_device *dev)
1165{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001166 struct net_bridge_port *p;
1167 struct net_bridge *br;
1168 int num_vlans = 0;
Roopa Prabhufed0a152015-02-25 23:55:40 -08001169
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001170 if (br_port_exists(dev)) {
1171 p = br_port_get_rtnl(dev);
1172 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001173 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001174 } else if (dev->priv_flags & IFF_EBRIDGE) {
1175 br = netdev_priv(dev);
1176 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001177 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001178 }
Roopa Prabhufed0a152015-02-25 23:55:40 -08001179
1180 /* Each VLAN is returned in bridge_vlan_info along with flags */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001181 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhufed0a152015-02-25 23:55:40 -08001182}
1183
Daniel Borkmann207895f2015-01-29 12:15:03 +01001184static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001185 .family = AF_BRIDGE,
1186 .get_link_af_size = br_get_link_af_size,
1187};
1188
stephen hemminger149ddd82012-06-26 05:48:45 +00001189struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001190 .kind = "bridge",
1191 .priv_size = sizeof(struct net_bridge),
1192 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001193 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001194 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001195 .validate = br_validate,
1196 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001197 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001198 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001199 .get_size = br_get_size,
1200 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001201
1202 .slave_maxtype = IFLA_BRPORT_MAX,
1203 .slave_policy = br_port_policy,
1204 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001205 .get_slave_size = br_port_get_slave_size,
1206 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001207};
1208
1209int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001210{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001211 int err;
1212
1213 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001214 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001215
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001216 err = rtnl_link_register(&br_link_ops);
1217 if (err)
1218 goto out_af;
1219
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001220 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001221
1222out_af:
1223 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001224 br_mdb_uninit();
1225 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001226}
1227
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001228void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001229{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001230 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001231 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001232 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001233}