blob: 37e4ec2953b2dd75c54d6caa62a3ad21f4b4439d [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"
Roopa Prabhuefa53562017-01-31 22:59:54 -080023#include "br_private_tunnel.h"
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070024
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020025static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020026 u32 filter_mask)
Roopa Prabhufed0a152015-02-25 23:55:40 -080027{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020028 struct net_bridge_vlan *v;
29 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020030 u16 flags, pvid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080031 int num_vlans = 0;
32
Roopa Prabhufed0a152015-02-25 23:55:40 -080033 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
34 return 0;
35
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020036 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020037 /* Count number of vlan infos */
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020038 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhufed0a152015-02-25 23:55:40 -080039 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020040 /* only a context, bridge vlan not activated */
41 if (!br_vlan_should_use(v))
42 continue;
43 if (v->vid == pvid)
Roopa Prabhufed0a152015-02-25 23:55:40 -080044 flags |= BRIDGE_VLAN_INFO_PVID;
45
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020046 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhufed0a152015-02-25 23:55:40 -080047 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
48
49 if (vid_range_start == 0) {
50 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020051 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhufed0a152015-02-25 23:55:40 -080052 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020053 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080054 continue;
55 } else {
56 if ((vid_range_end - vid_range_start) > 0)
57 num_vlans += 2;
58 else
59 num_vlans += 1;
60 }
61initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020062 vid_range_start = v->vid;
63 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080064 vid_range_flags = flags;
65 }
66
67 if (vid_range_start != 0) {
68 if ((vid_range_end - vid_range_start) > 0)
69 num_vlans += 2;
70 else
71 num_vlans += 1;
72 }
73
74 return num_vlans;
75}
76
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020077static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020078 u32 filter_mask)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020079{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020080 int num_vlans;
81
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020082 if (!vg)
83 return 0;
84
85 if (filter_mask & RTEXT_FILTER_BRVLAN)
86 return vg->num_vlans;
87
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020088 rcu_read_lock();
89 num_vlans = __get_num_vlan_infos(vg, filter_mask);
90 rcu_read_unlock();
91
92 return num_vlans;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020093}
94
Roopa Prabhufed0a152015-02-25 23:55:40 -080095static size_t br_get_link_af_size_filtered(const struct net_device *dev,
96 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080097{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020098 struct net_bridge_vlan_group *vg = NULL;
Roopa Prabhuefa53562017-01-31 22:59:54 -080099 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200100 struct net_bridge *br;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800101 int num_vlan_infos;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800102 size_t vinfo_sz = 0;
Roopa Prabhub7853d72015-02-21 20:21:51 -0800103
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100104 rcu_read_lock();
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200105 if (br_port_exists(dev)) {
106 p = br_port_get_rcu(dev);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200107 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200108 } else if (dev->priv_flags & IFF_EBRIDGE) {
109 br = netdev_priv(dev);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200110 vg = br_vlan_group_rcu(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200111 }
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200112 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100113 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -0800114
Roopa Prabhuefa53562017-01-31 22:59:54 -0800115 if (p && (p->flags & BR_VLAN_TUNNEL))
116 vinfo_sz += br_get_vlan_tunnel_info_size(vg);
117
Roopa Prabhub7853d72015-02-21 20:21:51 -0800118 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhuefa53562017-01-31 22:59:54 -0800119 vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
120
121 return vinfo_sz;
Roopa Prabhub7853d72015-02-21 20:21:51 -0800122}
123
stephen hemminger25c71c72012-11-13 07:53:05 +0000124static inline size_t br_port_info_size(void)
125{
126 return nla_total_size(1) /* IFLA_BRPORT_STATE */
127 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
128 + nla_total_size(4) /* IFLA_BRPORT_COST */
129 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000130 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000131 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000132 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100133 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400134 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400135 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Tobias Klauser90512472017-05-05 16:36:53 +0200136 + nla_total_size(1) /* IFLA_BRPORT_MCAST_FLOOD */
137 + nla_total_size(1) /* IFLA_BRPORT_BCAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200138 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200139 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
Roopa Prabhuefa53562017-01-31 22:59:54 -0800140 + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200141 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200142 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200143 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
144 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200145 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
146 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200147 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
148 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200149 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
150 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
151 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200152#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
153 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
154#endif
stephen hemminger25c71c72012-11-13 07:53:05 +0000155 + 0;
156}
157
Roopa Prabhufed0a152015-02-25 23:55:40 -0800158static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800159{
160 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000161 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
162 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
163 + nla_total_size(4) /* IFLA_MASTER */
164 + nla_total_size(4) /* IFLA_MTU */
165 + nla_total_size(4) /* IFLA_LINK */
166 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800167 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800168 + nla_total_size(br_get_link_af_size_filtered(dev,
169 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000170}
171
172static int br_port_fill_attrs(struct sk_buff *skb,
173 const struct net_bridge_port *p)
174{
175 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200176 u64 timerval;
stephen hemminger25c71c72012-11-13 07:53:05 +0000177
178 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
179 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
180 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000181 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000182 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200183 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
184 !!(p->flags & BR_ROOT_BLOCK)) ||
185 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
186 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100187 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
188 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400189 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200190 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
191 !!(p->flags & BR_FLOOD)) ||
192 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
193 !!(p->flags & BR_MCAST_FLOOD)) ||
Mike Manning99f906e2017-04-26 14:48:09 +0100194 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
195 !!(p->flags & BR_BCAST_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200196 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
197 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200198 !!(p->flags & BR_PROXYARP_WIFI)) ||
199 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200200 &p->designated_root) ||
201 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200202 &p->designated_bridge) ||
203 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200204 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
205 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200206 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
207 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
208 p->topology_change_ack) ||
Roopa Prabhuefa53562017-01-31 22:59:54 -0800209 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
210 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
211 BR_VLAN_TUNNEL)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000212 return -EMSGSIZE;
213
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200214 timerval = br_timer_value(&p->message_age_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200215 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
216 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200217 return -EMSGSIZE;
218 timerval = br_timer_value(&p->forward_delay_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200219 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
220 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200221 return -EMSGSIZE;
222 timerval = br_timer_value(&p->hold_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200223 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
224 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200225 return -EMSGSIZE;
226
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200227#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
228 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
229 p->multicast_router))
230 return -EMSGSIZE;
231#endif
232
stephen hemminger25c71c72012-11-13 07:53:05 +0000233 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800234}
235
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800236static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
237 u16 vid_end, u16 flags)
238{
239 struct bridge_vlan_info vinfo;
240
241 if ((vid_end - vid_start) > 0) {
242 /* add range to skb */
243 vinfo.vid = vid_start;
244 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
245 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
246 sizeof(vinfo), &vinfo))
247 goto nla_put_failure;
248
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800249 vinfo.vid = vid_end;
250 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
251 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
252 sizeof(vinfo), &vinfo))
253 goto nla_put_failure;
254 } else {
255 vinfo.vid = vid_start;
256 vinfo.flags = flags;
257 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
258 sizeof(vinfo), &vinfo))
259 goto nla_put_failure;
260 }
261
262 return 0;
263
264nla_put_failure:
265 return -EMSGSIZE;
266}
267
268static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200269 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800270{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200271 struct net_bridge_vlan *v;
272 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200273 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800274 int err = 0;
275
276 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
277 * and mark vlan info with begin and end flags
278 * if vlaninfo represents a range
279 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200280 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200281 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800282 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200283 if (!br_vlan_should_use(v))
284 continue;
285 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800286 flags |= BRIDGE_VLAN_INFO_PVID;
287
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200288 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800289 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
290
291 if (vid_range_start == 0) {
292 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200293 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800294 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200295 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800296 continue;
297 } else {
298 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
299 vid_range_end,
300 vid_range_flags);
301 if (err)
302 return err;
303 }
304
305initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200306 vid_range_start = v->vid;
307 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800308 vid_range_flags = flags;
309 }
310
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800311 if (vid_range_start != 0) {
312 /* Call it once more to send any left over vlans */
313 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
314 vid_range_end,
315 vid_range_flags);
316 if (err)
317 return err;
318 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800319
320 return 0;
321}
322
323static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200324 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800325{
326 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200327 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200328 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800329
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200330 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200331 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200332 if (!br_vlan_should_use(v))
333 continue;
334
335 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800336 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200337 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800338 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
339
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200340 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800341 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
342
343 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
344 sizeof(vinfo), &vinfo))
345 goto nla_put_failure;
346 }
347
348 return 0;
349
350nla_put_failure:
351 return -EMSGSIZE;
352}
353
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700354/*
355 * Create one netlink message for one interface
356 * Contains port and master info as well as carrier and bridge state.
357 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000358static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200359 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000360 u32 pid, u32 seq, int event, unsigned int flags,
361 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700362{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200363 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800364 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700365 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700366 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700367
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000368 if (port)
369 br = port->br;
370 else
371 br = netdev_priv(dev);
372
stephen hemminger28a16c92010-05-10 09:31:09 +0000373 br_debug(br, "br_fill_info event %d port %s master %s\n",
374 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700375
Thomas Graf74685962006-11-20 16:20:22 -0800376 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
377 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800378 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700379
Thomas Graf74685962006-11-20 16:20:22 -0800380 hdr = nlmsg_data(nlh);
381 hdr->ifi_family = AF_BRIDGE;
382 hdr->__ifi_pad = 0;
383 hdr->ifi_type = dev->type;
384 hdr->ifi_index = dev->ifindex;
385 hdr->ifi_flags = dev_get_flags(dev);
386 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700387
David S. Miller2eb812e2012-04-01 20:49:54 -0400388 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
389 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
390 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
391 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
392 (dev->addr_len &&
393 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200394 (dev->ifindex != dev_get_iflink(dev) &&
395 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400396 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000397
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000398 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000399 struct nlattr *nest
400 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
401
402 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
403 goto nla_put_failure;
404 nla_nest_end(skb, nest);
405 }
406
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000407 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800408 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
409 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200410 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800411 struct nlattr *af;
412 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000413
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200414 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
415 rcu_read_lock();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200416 if (port)
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200417 vg = nbp_vlan_group_rcu(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200418 else
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200419 vg = br_vlan_group_rcu(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000420
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200421 if (!vg || !vg->num_vlans) {
422 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000423 goto done;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200424 }
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000425 af = nla_nest_start(skb, IFLA_AF_SPEC);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200426 if (!af) {
427 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000428 goto nla_put_failure;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200429 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800430 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200431 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800432 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200433 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800434
435 if (port && (port->flags & BR_VLAN_TUNNEL))
436 err = br_fill_vlan_tunnel_info(skb, vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200437 rcu_read_unlock();
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800438 if (err)
439 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000440 nla_nest_end(skb, af);
441 }
442
443done:
Johannes Berg053c0952015-01-16 22:09:00 +0100444 nlmsg_end(skb, nlh);
445 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700446
Thomas Graf74685962006-11-20 16:20:22 -0800447nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800448 nlmsg_cancel(skb, nlh);
449 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700450}
451
452/*
453 * Notify listeners of a change in port information
454 */
455void br_ifinfo_notify(int event, struct net_bridge_port *port)
456{
Vlad Yasevich407af322013-02-13 12:00:12 +0000457 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700458 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700459 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800460 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700461
Vlad Yasevich407af322013-02-13 12:00:12 +0000462 if (!port)
463 return;
464
465 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000466 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000467 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000468
Roopa Prabhufed0a152015-02-25 23:55:40 -0800469 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700470 if (skb == NULL)
471 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700472
Roopa Prabhufed0a152015-02-25 23:55:40 -0800473 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800474 if (err < 0) {
475 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
476 WARN_ON(err == -EMSGSIZE);
477 kfree_skb(skb);
478 goto errout;
479 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800480 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
481 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700482errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800483 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700484}
485
Vlad Yasevich407af322013-02-13 12:00:12 +0000486
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700487/*
488 * Dump information about all ports, in response to GETLINK
489 */
John Fastabende5a55a82012-10-24 08:12:57 +0000490int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200491 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700492{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800493 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700494
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800495 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
496 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300497 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000498
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200499 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300500 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700501}
502
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800503static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
504 int cmd, struct bridge_vlan_info *vinfo)
505{
506 int err = 0;
507
508 switch (cmd) {
509 case RTM_SETLINK:
510 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200511 /* if the MASTER flag is set this will act on the global
512 * per-VLAN entry as well
513 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800514 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
515 if (err)
516 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800517 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200518 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800519 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
520 }
521 break;
522
523 case RTM_DELLINK:
524 if (p) {
525 nbp_vlan_delete(p, vinfo->vid);
526 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
527 br_vlan_delete(p->br, vinfo->vid);
528 } else {
529 br_vlan_delete(br, vinfo->vid);
530 }
531 break;
532 }
533
534 return err;
535}
Vlad Yasevich407af322013-02-13 12:00:12 +0000536
Roopa Prabhuefa53562017-01-31 22:59:54 -0800537static int br_process_vlan_info(struct net_bridge *br,
538 struct net_bridge_port *p, int cmd,
539 struct bridge_vlan_info *vinfo_curr,
540 struct bridge_vlan_info **vinfo_last)
541{
542 if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK)
543 return -EINVAL;
544
545 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
546 /* check if we are already processing a range */
547 if (*vinfo_last)
548 return -EINVAL;
549 *vinfo_last = vinfo_curr;
550 /* don't allow range of pvids */
551 if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
552 return -EINVAL;
553 return 0;
554 }
555
556 if (*vinfo_last) {
557 struct bridge_vlan_info tmp_vinfo;
558 int v, err;
559
560 if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END))
561 return -EINVAL;
562
563 if (vinfo_curr->vid <= (*vinfo_last)->vid)
564 return -EINVAL;
565
566 memcpy(&tmp_vinfo, *vinfo_last,
567 sizeof(struct bridge_vlan_info));
568 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
569 tmp_vinfo.vid = v;
570 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
571 if (err)
572 break;
573 }
574 *vinfo_last = NULL;
575
576 return 0;
577 }
578
579 return br_vlan_info(br, p, cmd, vinfo_curr);
580}
581
Vlad Yasevich407af322013-02-13 12:00:12 +0000582static int br_afspec(struct net_bridge *br,
583 struct net_bridge_port *p,
584 struct nlattr *af_spec,
585 int cmd)
586{
Roopa Prabhuefa53562017-01-31 22:59:54 -0800587 struct bridge_vlan_info *vinfo_curr = NULL;
588 struct bridge_vlan_info *vinfo_last = NULL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800589 struct nlattr *attr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800590 struct vtunnel_info tinfo_last = {};
591 struct vtunnel_info tinfo_curr = {};
592 int err = 0, rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000593
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800594 nla_for_each_nested(attr, af_spec, rem) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800595 err = 0;
596 switch (nla_type(attr)) {
597 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
Nikolay Aleksandrov1020ce32017-06-06 01:26:24 +0300598 if (!p || !(p->flags & BR_VLAN_TUNNEL))
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800599 return -EINVAL;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800600 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
601 if (err)
602 return err;
603 err = br_process_vlan_tunnel_info(br, p, cmd,
604 &tinfo_curr,
605 &tinfo_last);
606 if (err)
607 return err;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800608 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800609 case IFLA_BRIDGE_VLAN_INFO:
610 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
611 return -EINVAL;
612 vinfo_curr = nla_data(attr);
613 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
614 &vinfo_last);
615 if (err)
616 return err;
617 break;
618 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000619 }
620
621 return err;
622}
623
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200624static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000625 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
626 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
627 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
628 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000629 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000630 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100631 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400632 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400633 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200634 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200635 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200636 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100637 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
Tobias Klauser90512472017-05-05 16:36:53 +0200638 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
639 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000640};
641
642/* Change the state of the port and notify spanning tree */
643static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700644{
stephen hemminger25c71c72012-11-13 07:53:05 +0000645 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000646 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700647
648 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700649 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700650 return -EBUSY;
651
stephen hemminger576eb622012-12-28 18:15:22 +0000652 /* if device is not up, change is not allowed
653 * if link is not present, only allowable state is disabled
654 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000655 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000656 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700657 return -ENETDOWN;
658
Florian Fainelli775dd692014-09-30 16:13:19 -0700659 br_set_state(p, state);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000660 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700661 return 0;
662}
663
stephen hemminger25c71c72012-11-13 07:53:05 +0000664/* Set/clear or port flags based on attribute */
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200665static int br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
666 int attrtype, unsigned long mask)
stephen hemminger25c71c72012-11-13 07:53:05 +0000667{
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200668 unsigned long flags;
669 int err;
670
671 if (!tb[attrtype])
672 return 0;
673
674 if (nla_get_u8(tb[attrtype]))
675 flags = p->flags | mask;
676 else
677 flags = p->flags & ~mask;
678
679 err = br_switchdev_set_port_flag(p, flags, mask);
680 if (err)
681 return err;
682
683 p->flags = flags;
684 return 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000685}
686
687/* Process bridge protocol info on port */
688static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
689{
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400690 unsigned long old_flags = p->flags;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800691 bool br_vlan_tunnel_old = false;
692 int err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000693
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200694 err = br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
695 if (err)
696 return err;
697
698 err = br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
699 if (err)
700 return err;
701
702 err = br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
703 if (err)
704 return err;
705
706 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
707 if (err)
708 return err;
709
710 err = br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
711 if (err)
712 return err;
713
714 err = br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
715 if (err)
716 return err;
717
718 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
719 if (err)
720 return err;
721
722 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
723 if (err)
724 return err;
725
726 err = br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
727 if (err)
728 return err;
729
730 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
731 if (err)
732 return err;
733
734 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
735 if (err)
736 return err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000737
Roopa Prabhuefa53562017-01-31 22:59:54 -0800738 br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200739 err = br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
740 if (err)
741 return err;
742
Roopa Prabhuefa53562017-01-31 22:59:54 -0800743 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
744 nbp_vlan_tunnel_info_flush(p);
745
stephen hemminger25c71c72012-11-13 07:53:05 +0000746 if (tb[IFLA_BRPORT_COST]) {
747 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
748 if (err)
749 return err;
750 }
751
752 if (tb[IFLA_BRPORT_PRIORITY]) {
753 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
754 if (err)
755 return err;
756 }
757
758 if (tb[IFLA_BRPORT_STATE]) {
759 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
760 if (err)
761 return err;
762 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400763
Nikolay Aleksandrov9b0c6e42015-10-06 14:12:01 +0200764 if (tb[IFLA_BRPORT_FLUSH])
765 br_fdb_delete_by_port(p->br, p, 0, 0);
766
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200767#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
768 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
769 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
770
771 err = br_multicast_set_port_router(p, mcast_router);
772 if (err)
773 return err;
774 }
775#endif
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400776 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000777 return 0;
778}
779
780/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800781int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000782{
stephen hemminger25c71c72012-11-13 07:53:05 +0000783 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000784 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000785 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000786 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700787 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000788
Hong zhi guoc60ee672013-03-28 06:21:22 +0000789 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
790 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000791 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000792 return 0;
793
794 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000795 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800796 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000797 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000798 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000799 return -EINVAL;
800
Vlad Yasevich407af322013-02-13 12:00:12 +0000801 if (p && protinfo) {
802 if (protinfo->nla_type & NLA_F_NESTED) {
Johannes Bergfceb6432017-04-12 14:34:07 +0200803 err = nla_parse_nested(tb, IFLA_BRPORT_MAX, protinfo,
804 br_port_policy, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +0000805 if (err)
806 return err;
807
808 spin_lock_bh(&p->br->lock);
809 err = br_setport(p, tb);
810 spin_unlock_bh(&p->br->lock);
811 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800812 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000813 if (nla_len(protinfo) < sizeof(u8))
814 return -EINVAL;
815
816 spin_lock_bh(&p->br->lock);
817 err = br_set_port_state(p, nla_get_u8(protinfo));
818 spin_unlock_bh(&p->br->lock);
819 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000820 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000821 goto out;
822 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000823
Vlad Yasevich407af322013-02-13 12:00:12 +0000824 if (afspec) {
825 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
826 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000827 }
828
829 if (err == 0)
830 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000831out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000832 return err;
833}
834
Vlad Yasevich407af322013-02-13 12:00:12 +0000835/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800836int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000837{
Vlad Yasevich407af322013-02-13 12:00:12 +0000838 struct nlattr *afspec;
839 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700840 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000841
Hong zhi guoc60ee672013-03-28 06:21:22 +0000842 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000843 if (!afspec)
844 return 0;
845
846 p = br_port_get_rtnl(dev);
847 /* We want to accept dev as bridge itself as well */
848 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
849 return -EINVAL;
850
851 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
852 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800853 if (err == 0)
854 /* Send RTM_NEWLINK because userspace
855 * expects RTM_NEWLINK for vlan dels
856 */
857 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000858
859 return err;
860}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000861static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700862{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000863 if (tb[IFLA_ADDRESS]) {
864 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
865 return -EINVAL;
866 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
867 return -EADDRNOTAVAIL;
868 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700869
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900870 if (!data)
871 return 0;
872
873#ifdef CONFIG_BRIDGE_VLAN_FILTERING
874 if (data[IFLA_BR_VLAN_PROTOCOL]) {
875 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
876 case htons(ETH_P_8021Q):
877 case htons(ETH_P_8021AD):
878 break;
879 default:
880 return -EPROTONOSUPPORT;
881 }
882 }
Tobias Jungela2858602017-05-17 09:29:12 +0200883
884 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
885 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
886
887 if (defpvid >= VLAN_VID_MASK)
888 return -EINVAL;
889 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900890#endif
891
Thomas Graf32fe21c2007-03-22 11:59:03 -0700892 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700893}
894
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200895static int br_port_slave_changelink(struct net_device *brdev,
896 struct net_device *dev,
897 struct nlattr *tb[],
898 struct nlattr *data[])
899{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200900 struct net_bridge *br = netdev_priv(brdev);
901 int ret;
902
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200903 if (!data)
904 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200905
906 spin_lock_bh(&br->lock);
907 ret = br_setport(br_port_get_rtnl(dev), data);
908 spin_unlock_bh(&br->lock);
909
910 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200911}
912
Jiri Pirkoced82832014-09-05 15:51:29 +0200913static int br_port_fill_slave_info(struct sk_buff *skb,
914 const struct net_device *brdev,
915 const struct net_device *dev)
916{
917 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
918}
919
920static size_t br_port_get_slave_size(const struct net_device *brdev,
921 const struct net_device *dev)
922{
923 return br_port_info_size();
924}
925
Jiri Pirko13323512014-09-05 15:51:32 +0200926static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
927 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
928 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
929 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100930 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
931 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
932 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300933 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900934 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200935 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200936 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
937 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200938 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200939 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200940 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200941 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200942 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200943 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200944 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200945 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200946 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
947 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
948 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
949 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
950 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
951 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200952 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
953 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
954 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200955 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200956 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200957 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100958 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100959 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
Jiri Pirko13323512014-09-05 15:51:32 +0200960};
961
962static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +0200963 struct nlattr *data[],
964 struct netlink_ext_ack *extack)
Jiri Pirko13323512014-09-05 15:51:32 +0200965{
966 struct net_bridge *br = netdev_priv(brdev);
967 int err;
968
969 if (!data)
970 return 0;
971
972 if (data[IFLA_BR_FORWARD_DELAY]) {
973 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
974 if (err)
975 return err;
976 }
977
978 if (data[IFLA_BR_HELLO_TIME]) {
979 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
980 if (err)
981 return err;
982 }
983
984 if (data[IFLA_BR_MAX_AGE]) {
985 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
986 if (err)
987 return err;
988 }
989
Jörg Thalheimaf615762015-03-18 10:06:58 +0100990 if (data[IFLA_BR_AGEING_TIME]) {
Scott Feldmanc62987b2015-10-08 19:23:19 -0700991 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
992 if (err)
993 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +0100994 }
995
996 if (data[IFLA_BR_STP_STATE]) {
997 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
998
999 br_stp_set_enabled(br, stp_enabled);
1000 }
1001
1002 if (data[IFLA_BR_PRIORITY]) {
1003 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1004
1005 br_stp_set_bridge_priority(br, priority);
1006 }
1007
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001008 if (data[IFLA_BR_VLAN_FILTERING]) {
1009 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1010
1011 err = __br_vlan_filter_toggle(br, vlan_filter);
1012 if (err)
1013 return err;
1014 }
1015
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001016#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1017 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1018 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1019
1020 err = __br_vlan_set_proto(br, vlan_proto);
1021 if (err)
1022 return err;
1023 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001024
1025 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1026 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1027
1028 err = __br_vlan_set_default_pvid(br, defpvid);
1029 if (err)
1030 return err;
1031 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001032
1033 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1034 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1035
1036 err = br_vlan_set_stats(br, vlan_stats);
1037 if (err)
1038 return err;
1039 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001040#endif
1041
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001042 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1043 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1044
1045 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1046 return -EINVAL;
1047 br->group_fwd_mask = fwd_mask;
1048 }
1049
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001050 if (data[IFLA_BR_GROUP_ADDR]) {
1051 u8 new_addr[ETH_ALEN];
1052
1053 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1054 return -EINVAL;
1055 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1056 if (!is_link_local_ether_addr(new_addr))
1057 return -EINVAL;
1058 if (new_addr[5] == 1 || /* 802.3x Pause address */
1059 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1060 new_addr[5] == 3) /* 802.1X PAE address */
1061 return -EINVAL;
1062 spin_lock_bh(&br->lock);
1063 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1064 spin_unlock_bh(&br->lock);
1065 br->group_addr_set = true;
1066 br_recalculate_fwd_mask(br);
1067 }
1068
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +02001069 if (data[IFLA_BR_FDB_FLUSH])
1070 br_fdb_flush(br);
1071
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001072#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1073 if (data[IFLA_BR_MCAST_ROUTER]) {
1074 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1075
1076 err = br_multicast_set_router(br, multicast_router);
1077 if (err)
1078 return err;
1079 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001080
1081 if (data[IFLA_BR_MCAST_SNOOPING]) {
1082 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1083
1084 err = br_multicast_toggle(br, mcast_snooping);
1085 if (err)
1086 return err;
1087 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001088
1089 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1090 u8 val;
1091
1092 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1093 br->multicast_query_use_ifaddr = !!val;
1094 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001095
1096 if (data[IFLA_BR_MCAST_QUERIER]) {
1097 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1098
1099 err = br_multicast_set_querier(br, mcast_querier);
1100 if (err)
1101 return err;
1102 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001103
1104 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
1105 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
1106
1107 br->hash_elasticity = val;
1108 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001109
1110 if (data[IFLA_BR_MCAST_HASH_MAX]) {
1111 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1112
1113 err = br_multicast_set_hash_max(br, hash_max);
1114 if (err)
1115 return err;
1116 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001117
1118 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1119 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1120
1121 br->multicast_last_member_count = val;
1122 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001123
1124 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1125 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1126
1127 br->multicast_startup_query_count = val;
1128 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001129
1130 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1131 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1132
1133 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1134 }
1135
1136 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1137 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1138
1139 br->multicast_membership_interval = clock_t_to_jiffies(val);
1140 }
1141
1142 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1143 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1144
1145 br->multicast_querier_interval = clock_t_to_jiffies(val);
1146 }
1147
1148 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1149 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1150
1151 br->multicast_query_interval = clock_t_to_jiffies(val);
1152 }
1153
1154 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1155 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1156
1157 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1158 }
1159
1160 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1161 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1162
1163 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1164 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001165
1166 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1167 __u8 mcast_stats;
1168
1169 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1170 br->multicast_stats_enabled = !!mcast_stats;
1171 }
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001172
1173 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1174 __u8 igmp_version;
1175
1176 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1177 err = br_multicast_set_igmp_version(br, igmp_version);
1178 if (err)
1179 return err;
1180 }
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001181
1182#if IS_ENABLED(CONFIG_IPV6)
1183 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1184 __u8 mld_version;
1185
1186 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1187 err = br_multicast_set_mld_version(br, mld_version);
1188 if (err)
1189 return err;
1190 }
1191#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001192#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001193#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1194 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1195 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1196
1197 br->nf_call_iptables = val ? true : false;
1198 }
1199
1200 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1201 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1202
1203 br->nf_call_ip6tables = val ? true : false;
1204 }
1205
1206 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1207 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1208
1209 br->nf_call_arptables = val ? true : false;
1210 }
1211#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001212
Jiri Pirko13323512014-09-05 15:51:32 +02001213 return 0;
1214}
1215
Ivan Vecerab6677442017-01-20 18:12:17 +01001216static int br_dev_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001217 struct nlattr *tb[], struct nlattr *data[],
1218 struct netlink_ext_ack *extack)
Ivan Vecerab6677442017-01-20 18:12:17 +01001219{
1220 struct net_bridge *br = netdev_priv(dev);
1221 int err;
1222
1223 if (tb[IFLA_ADDRESS]) {
1224 spin_lock_bh(&br->lock);
1225 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1226 spin_unlock_bh(&br->lock);
1227 }
1228
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001229 err = register_netdevice(dev);
Ivan Vecerab6677442017-01-20 18:12:17 +01001230 if (err)
1231 return err;
1232
Matthias Schifferad744b22017-06-25 23:56:00 +02001233 err = br_changelink(dev, tb, data, extack);
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001234 if (err)
1235 unregister_netdevice(dev);
1236 return err;
Ivan Vecerab6677442017-01-20 18:12:17 +01001237}
1238
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001239static size_t br_get_size(const struct net_device *brdev)
1240{
1241 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1242 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1243 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001244 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1245 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1246 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001247 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001248#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1249 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001250 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001251 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001252#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001253 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001254 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001255 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001256 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001257 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001258 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1259 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001260 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1261 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1262 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1263 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001264 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001265#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1266 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001267 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001268 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001269 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001270 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001271 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1272 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1273 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1274 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001275 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1276 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1277 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1278 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1279 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1280 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001281 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001282 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001283#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001284#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1285 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1286 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1287 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1288#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001289 0;
1290}
1291
1292static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1293{
1294 struct net_bridge *br = netdev_priv(brdev);
1295 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1296 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1297 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001298 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1299 u32 stp_enabled = br->stp_enabled;
1300 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Ido Schimmel1f514452017-05-26 08:37:23 +02001301 u8 vlan_enabled = br_vlan_enabled(br->dev);
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001302 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001303
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001304 clockval = br_timer_value(&br->hello_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001305 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001306 return -EMSGSIZE;
1307 clockval = br_timer_value(&br->tcn_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001308 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001309 return -EMSGSIZE;
1310 clockval = br_timer_value(&br->topology_change_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001311 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1312 IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001313 return -EMSGSIZE;
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +01001314 clockval = br_timer_value(&br->gc_work.timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001315 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001316 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001317
1318 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1319 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001320 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1321 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1322 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001323 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001324 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001325 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1326 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1327 &br->bridge_id) ||
1328 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1329 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001330 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001331 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1332 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1333 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001334 br->topology_change_detected) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001335 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001336 return -EMSGSIZE;
1337
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001338#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001339 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001340 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1341 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001342 return -EMSGSIZE;
1343#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001344#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001345 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001346 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1347 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001348 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001349 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001350 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1351 br->multicast_stats_enabled) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001352 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001353 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001354 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1355 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001356 br->multicast_last_member_count) ||
1357 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001358 br->multicast_startup_query_count) ||
1359 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1360 br->multicast_igmp_version))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001361 return -EMSGSIZE;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001362#if IS_ENABLED(CONFIG_IPV6)
1363 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1364 br->multicast_mld_version))
1365 return -EMSGSIZE;
1366#endif
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001367 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001368 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1369 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001370 return -EMSGSIZE;
1371 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001372 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1373 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001374 return -EMSGSIZE;
1375 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001376 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1377 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001378 return -EMSGSIZE;
1379 clockval = jiffies_to_clock_t(br->multicast_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001380 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1381 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001382 return -EMSGSIZE;
1383 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001384 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1385 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001386 return -EMSGSIZE;
1387 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001388 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1389 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001390 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001391#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001392#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1393 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1394 br->nf_call_iptables ? 1 : 0) ||
1395 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1396 br->nf_call_ip6tables ? 1 : 0) ||
1397 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1398 br->nf_call_arptables ? 1 : 0))
1399 return -EMSGSIZE;
1400#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001401
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001402 return 0;
1403}
1404
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001405static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001406{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001407 struct net_bridge_port *p = NULL;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001408 struct net_bridge_vlan_group *vg;
1409 struct net_bridge_vlan *v;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001410 struct net_bridge *br;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001411 int numvls = 0;
1412
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001413 switch (attr) {
1414 case IFLA_STATS_LINK_XSTATS:
1415 br = netdev_priv(dev);
1416 vg = br_vlan_group(br);
1417 break;
1418 case IFLA_STATS_LINK_XSTATS_SLAVE:
1419 p = br_port_get_rtnl(dev);
1420 if (!p)
1421 return 0;
1422 br = p->br;
1423 vg = nbp_vlan_group(p);
1424 break;
1425 default:
1426 return 0;
1427 }
1428
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001429 if (vg) {
1430 /* we need to count all, even placeholder entries */
1431 list_for_each_entry(v, &vg->vlan_list, vlist)
1432 numvls++;
1433 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001434
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001435 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001436 nla_total_size(sizeof(struct br_mcast_stats)) +
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001437 nla_total_size(0);
1438}
1439
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001440static int br_fill_linkxstats(struct sk_buff *skb,
1441 const struct net_device *dev,
1442 int *prividx, int attr)
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001443{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001444 struct nlattr *nla __maybe_unused;
1445 struct net_bridge_port *p = NULL;
1446 struct net_bridge_vlan_group *vg;
1447 struct net_bridge_vlan *v;
1448 struct net_bridge *br;
1449 struct nlattr *nest;
1450 int vl_idx = 0;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001451
1452 switch (attr) {
1453 case IFLA_STATS_LINK_XSTATS:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001454 br = netdev_priv(dev);
1455 vg = br_vlan_group(br);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001456 break;
1457 case IFLA_STATS_LINK_XSTATS_SLAVE:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001458 p = br_port_get_rtnl(dev);
1459 if (!p)
1460 return 0;
1461 br = p->br;
1462 vg = nbp_vlan_group(p);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001463 break;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001464 default:
1465 return -EINVAL;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001466 }
1467
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001468 nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
1469 if (!nest)
1470 return -EMSGSIZE;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001471
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001472 if (vg) {
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001473 u16 pvid;
1474
1475 pvid = br_get_pvid(vg);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001476 list_for_each_entry(v, &vg->vlan_list, vlist) {
1477 struct bridge_vlan_xstats vxi;
1478 struct br_vlan_stats stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001479
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001480 if (++vl_idx < *prividx)
1481 continue;
1482 memset(&vxi, 0, sizeof(vxi));
1483 vxi.vid = v->vid;
Nikolay Aleksandrov61ba1a22016-08-17 12:53:10 +02001484 vxi.flags = v->flags;
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001485 if (v->vid == pvid)
1486 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001487 br_vlan_get_stats(v, &stats);
1488 vxi.rx_bytes = stats.rx_bytes;
1489 vxi.rx_packets = stats.rx_packets;
1490 vxi.tx_bytes = stats.tx_bytes;
1491 vxi.tx_packets = stats.tx_packets;
1492
1493 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1494 goto nla_put_failure;
1495 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001496 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001497
1498#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1499 if (++vl_idx >= *prividx) {
1500 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1501 sizeof(struct br_mcast_stats),
1502 BRIDGE_XSTATS_PAD);
1503 if (!nla)
1504 goto nla_put_failure;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001505 br_multicast_get_stats(br, p, nla_data(nla));
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001506 }
1507#endif
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001508 nla_nest_end(skb, nest);
1509 *prividx = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001510
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001511 return 0;
1512
1513nla_put_failure:
1514 nla_nest_end(skb, nest);
1515 *prividx = vl_idx;
1516
1517 return -EMSGSIZE;
1518}
Roopa Prabhufed0a152015-02-25 23:55:40 -08001519
Daniel Borkmann207895fd32015-01-29 12:15:03 +01001520static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001521 .family = AF_BRIDGE,
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001522 .get_link_af_size = br_get_link_af_size_filtered,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001523};
1524
stephen hemminger149ddd82012-06-26 05:48:45 +00001525struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001526 .kind = "bridge",
1527 .priv_size = sizeof(struct net_bridge),
1528 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001529 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001530 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001531 .validate = br_validate,
1532 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001533 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001534 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001535 .get_size = br_get_size,
1536 .fill_info = br_fill_info,
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001537 .fill_linkxstats = br_fill_linkxstats,
1538 .get_linkxstats_size = br_get_linkxstats_size,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001539
1540 .slave_maxtype = IFLA_BRPORT_MAX,
1541 .slave_policy = br_port_policy,
1542 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001543 .get_slave_size = br_port_get_slave_size,
1544 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001545};
1546
1547int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001548{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001549 int err;
1550
1551 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001552 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001553
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001554 err = rtnl_link_register(&br_link_ops);
1555 if (err)
1556 goto out_af;
1557
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001558 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001559
1560out_af:
1561 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001562 br_mdb_uninit();
1563 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001564}
1565
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001566void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001567{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001568 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001569 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001570 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001571}