blob: ec2b58a09f76381b75179f38e438e190027a5102 [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 */
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700141 + nla_total_size(1) /* IFLA_BRPORT_NEIGH_SUPPRESS */
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300142 + nla_total_size(1) /* IFLA_BRPORT_ISOLATED */
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200143 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200144 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200145 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
146 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200147 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
148 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200149 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
150 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200151 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
152 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
153 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200154#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
155 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
156#endif
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300157 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_GROUP_FWD_MASK */
stephen hemminger25c71c72012-11-13 07:53:05 +0000158 + 0;
159}
160
Roopa Prabhufed0a152015-02-25 23:55:40 -0800161static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800162{
163 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000164 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
165 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
166 + nla_total_size(4) /* IFLA_MASTER */
167 + nla_total_size(4) /* IFLA_MTU */
168 + nla_total_size(4) /* IFLA_LINK */
169 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800170 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800171 + nla_total_size(br_get_link_af_size_filtered(dev,
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300172 filter_mask)) /* IFLA_AF_SPEC */
173 + nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */
stephen hemminger25c71c72012-11-13 07:53:05 +0000174}
175
176static int br_port_fill_attrs(struct sk_buff *skb,
177 const struct net_bridge_port *p)
178{
179 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300180 struct net_bridge_port *backup_p;
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200181 u64 timerval;
stephen hemminger25c71c72012-11-13 07:53:05 +0000182
183 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
184 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
185 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000186 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000187 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200188 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
189 !!(p->flags & BR_ROOT_BLOCK)) ||
190 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
191 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100192 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
193 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400194 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200195 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
196 !!(p->flags & BR_FLOOD)) ||
197 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
198 !!(p->flags & BR_MCAST_FLOOD)) ||
Mike Manning99f906e2017-04-26 14:48:09 +0100199 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
200 !!(p->flags & BR_BCAST_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200201 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
202 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200203 !!(p->flags & BR_PROXYARP_WIFI)) ||
204 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200205 &p->designated_root) ||
206 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200207 &p->designated_bridge) ||
208 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200209 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
210 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200211 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
212 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
213 p->topology_change_ack) ||
Roopa Prabhuefa53562017-01-31 22:59:54 -0800214 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
215 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300216 BR_VLAN_TUNNEL)) ||
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700217 nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
218 nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300219 !!(p->flags & BR_NEIGH_SUPPRESS)) ||
220 nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000221 return -EMSGSIZE;
222
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200223 timerval = br_timer_value(&p->message_age_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200224 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
225 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200226 return -EMSGSIZE;
227 timerval = br_timer_value(&p->forward_delay_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200228 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
229 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200230 return -EMSGSIZE;
231 timerval = br_timer_value(&p->hold_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200232 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
233 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200234 return -EMSGSIZE;
235
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200236#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
237 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
238 p->multicast_router))
239 return -EMSGSIZE;
240#endif
241
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300242 /* we might be called only with br->lock */
243 rcu_read_lock();
244 backup_p = rcu_dereference(p->backup_port);
245 if (backup_p)
246 nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT,
247 backup_p->dev->ifindex);
248 rcu_read_unlock();
249
stephen hemminger25c71c72012-11-13 07:53:05 +0000250 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800251}
252
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800253static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
254 u16 vid_end, u16 flags)
255{
256 struct bridge_vlan_info vinfo;
257
258 if ((vid_end - vid_start) > 0) {
259 /* add range to skb */
260 vinfo.vid = vid_start;
261 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
262 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
263 sizeof(vinfo), &vinfo))
264 goto nla_put_failure;
265
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800266 vinfo.vid = vid_end;
267 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
268 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
269 sizeof(vinfo), &vinfo))
270 goto nla_put_failure;
271 } else {
272 vinfo.vid = vid_start;
273 vinfo.flags = flags;
274 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
275 sizeof(vinfo), &vinfo))
276 goto nla_put_failure;
277 }
278
279 return 0;
280
281nla_put_failure:
282 return -EMSGSIZE;
283}
284
285static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200286 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800287{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200288 struct net_bridge_vlan *v;
289 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200290 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800291 int err = 0;
292
293 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
294 * and mark vlan info with begin and end flags
295 * if vlaninfo represents a range
296 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200297 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200298 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800299 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200300 if (!br_vlan_should_use(v))
301 continue;
302 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800303 flags |= BRIDGE_VLAN_INFO_PVID;
304
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200305 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800306 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
307
308 if (vid_range_start == 0) {
309 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200310 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800311 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200312 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800313 continue;
314 } else {
315 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
316 vid_range_end,
317 vid_range_flags);
318 if (err)
319 return err;
320 }
321
322initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200323 vid_range_start = v->vid;
324 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800325 vid_range_flags = flags;
326 }
327
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800328 if (vid_range_start != 0) {
329 /* Call it once more to send any left over vlans */
330 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
331 vid_range_end,
332 vid_range_flags);
333 if (err)
334 return err;
335 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800336
337 return 0;
338}
339
340static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200341 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800342{
343 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200344 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200345 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800346
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200347 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200348 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200349 if (!br_vlan_should_use(v))
350 continue;
351
352 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800353 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800355 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
356
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200357 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800358 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
359
360 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
361 sizeof(vinfo), &vinfo))
362 goto nla_put_failure;
363 }
364
365 return 0;
366
367nla_put_failure:
368 return -EMSGSIZE;
369}
370
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700371/*
372 * Create one netlink message for one interface
373 * Contains port and master info as well as carrier and bridge state.
374 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000375static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200376 const struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000377 u32 pid, u32 seq, int event, unsigned int flags,
378 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700379{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200380 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200381 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800382 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700383 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700384
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000385 if (port)
386 br = port->br;
387 else
388 br = netdev_priv(dev);
389
stephen hemminger28a16c92010-05-10 09:31:09 +0000390 br_debug(br, "br_fill_info event %d port %s master %s\n",
391 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700392
Thomas Graf74685962006-11-20 16:20:22 -0800393 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
394 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800395 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700396
Thomas Graf74685962006-11-20 16:20:22 -0800397 hdr = nlmsg_data(nlh);
398 hdr->ifi_family = AF_BRIDGE;
399 hdr->__ifi_pad = 0;
400 hdr->ifi_type = dev->type;
401 hdr->ifi_index = dev->ifindex;
402 hdr->ifi_flags = dev_get_flags(dev);
403 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700404
David S. Miller2eb812e2012-04-01 20:49:54 -0400405 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
406 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
407 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
408 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
409 (dev->addr_len &&
410 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200411 (dev->ifindex != dev_get_iflink(dev) &&
412 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400413 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000414
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000415 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000416 struct nlattr *nest
417 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
418
419 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
420 goto nla_put_failure;
421 nla_nest_end(skb, nest);
422 }
423
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000424 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800425 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
426 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200427 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800428 struct nlattr *af;
429 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000430
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200431 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
432 rcu_read_lock();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200433 if (port)
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200434 vg = nbp_vlan_group_rcu(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200435 else
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200436 vg = br_vlan_group_rcu(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000437
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200438 if (!vg || !vg->num_vlans) {
439 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000440 goto done;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200441 }
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000442 af = nla_nest_start(skb, IFLA_AF_SPEC);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200443 if (!af) {
444 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000445 goto nla_put_failure;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200446 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800447 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200448 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800449 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200450 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800451
452 if (port && (port->flags & BR_VLAN_TUNNEL))
453 err = br_fill_vlan_tunnel_info(skb, vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200454 rcu_read_unlock();
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800455 if (err)
456 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000457 nla_nest_end(skb, af);
458 }
459
460done:
Johannes Berg053c0952015-01-16 22:09:00 +0100461 nlmsg_end(skb, nlh);
462 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700463
Thomas Graf74685962006-11-20 16:20:22 -0800464nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800465 nlmsg_cancel(skb, nlh);
466 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700467}
468
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200469/* Notify listeners of a change in bridge or port information */
470void br_ifinfo_notify(int event, const struct net_bridge *br,
471 const struct net_bridge_port *port)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700472{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200473 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
474 struct net_device *dev;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700475 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700476 int err = -ENOBUFS;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200477 struct net *net;
478 u16 port_no = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700479
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200480 if (WARN_ON(!port && !br))
Vlad Yasevich407af322013-02-13 12:00:12 +0000481 return;
482
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200483 if (port) {
484 dev = port->dev;
485 br = port->br;
486 port_no = port->port_no;
487 } else {
488 dev = br->dev;
489 }
stephen hemminger28a16c92010-05-10 09:31:09 +0000490
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200491 net = dev_net(dev);
492 br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
493
494 skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700495 if (skb == NULL)
496 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700497
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200498 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800499 if (err < 0) {
500 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
501 WARN_ON(err == -EMSGSIZE);
502 kfree_skb(skb);
503 goto errout;
504 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800505 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
506 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700507errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800508 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700509}
510
511/*
512 * Dump information about all ports, in response to GETLINK
513 */
John Fastabende5a55a82012-10-24 08:12:57 +0000514int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200515 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700516{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800517 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700518
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800519 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
520 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300521 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000522
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200523 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300524 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700525}
526
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800527static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300528 int cmd, struct bridge_vlan_info *vinfo, bool *changed)
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800529{
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300530 bool curr_change;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800531 int err = 0;
532
533 switch (cmd) {
534 case RTM_SETLINK:
535 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200536 /* if the MASTER flag is set this will act on the global
537 * per-VLAN entry as well
538 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300539 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
540 &curr_change);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800541 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200542 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300543 err = br_vlan_add(br, vinfo->vid, vinfo->flags,
544 &curr_change);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800545 }
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300546 if (curr_change)
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300547 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800548 break;
549
550 case RTM_DELLINK:
551 if (p) {
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300552 if (!nbp_vlan_delete(p, vinfo->vid))
553 *changed = true;
554
555 if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
556 !br_vlan_delete(p->br, vinfo->vid))
557 *changed = true;
558 } else if (!br_vlan_delete(br, vinfo->vid)) {
559 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800560 }
561 break;
562 }
563
564 return err;
565}
Vlad Yasevich407af322013-02-13 12:00:12 +0000566
Roopa Prabhuefa53562017-01-31 22:59:54 -0800567static int br_process_vlan_info(struct net_bridge *br,
568 struct net_bridge_port *p, int cmd,
569 struct bridge_vlan_info *vinfo_curr,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300570 struct bridge_vlan_info **vinfo_last,
571 bool *changed)
Roopa Prabhuefa53562017-01-31 22:59:54 -0800572{
573 if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK)
574 return -EINVAL;
575
576 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
577 /* check if we are already processing a range */
578 if (*vinfo_last)
579 return -EINVAL;
580 *vinfo_last = vinfo_curr;
581 /* don't allow range of pvids */
582 if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
583 return -EINVAL;
584 return 0;
585 }
586
587 if (*vinfo_last) {
588 struct bridge_vlan_info tmp_vinfo;
589 int v, err;
590
591 if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END))
592 return -EINVAL;
593
594 if (vinfo_curr->vid <= (*vinfo_last)->vid)
595 return -EINVAL;
596
597 memcpy(&tmp_vinfo, *vinfo_last,
598 sizeof(struct bridge_vlan_info));
599 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
600 tmp_vinfo.vid = v;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300601 err = br_vlan_info(br, p, cmd, &tmp_vinfo, changed);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800602 if (err)
603 break;
604 }
605 *vinfo_last = NULL;
606
Nikolay Aleksandrov66c54512017-10-19 20:17:32 +0300607 return err;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800608 }
609
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300610 return br_vlan_info(br, p, cmd, vinfo_curr, changed);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800611}
612
Vlad Yasevich407af322013-02-13 12:00:12 +0000613static int br_afspec(struct net_bridge *br,
614 struct net_bridge_port *p,
615 struct nlattr *af_spec,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300616 int cmd, bool *changed)
Vlad Yasevich407af322013-02-13 12:00:12 +0000617{
Roopa Prabhuefa53562017-01-31 22:59:54 -0800618 struct bridge_vlan_info *vinfo_curr = NULL;
619 struct bridge_vlan_info *vinfo_last = NULL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800620 struct nlattr *attr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800621 struct vtunnel_info tinfo_last = {};
622 struct vtunnel_info tinfo_curr = {};
623 int err = 0, rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000624
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800625 nla_for_each_nested(attr, af_spec, rem) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800626 err = 0;
627 switch (nla_type(attr)) {
628 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
Nikolay Aleksandrov1020ce32017-06-06 01:26:24 +0300629 if (!p || !(p->flags & BR_VLAN_TUNNEL))
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800630 return -EINVAL;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800631 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
632 if (err)
633 return err;
634 err = br_process_vlan_tunnel_info(br, p, cmd,
635 &tinfo_curr,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300636 &tinfo_last,
637 changed);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800638 if (err)
639 return err;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800640 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800641 case IFLA_BRIDGE_VLAN_INFO:
642 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
643 return -EINVAL;
644 vinfo_curr = nla_data(attr);
645 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300646 &vinfo_last, changed);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800647 if (err)
648 return err;
649 break;
650 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000651 }
652
653 return err;
654}
655
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200656static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000657 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
658 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
659 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
660 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000661 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000662 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100663 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400664 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400665 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200666 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200667 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200668 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100669 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
Tobias Klauser90512472017-05-05 16:36:53 +0200670 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
671 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrovfbec4432017-11-13 18:30:55 +0200672 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300673 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov1a3fbd32017-10-30 12:56:33 +0200674 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300675 [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 },
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300676 [IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000677};
678
679/* Change the state of the port and notify spanning tree */
680static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700681{
stephen hemminger25c71c72012-11-13 07:53:05 +0000682 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000683 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700684
685 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700686 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700687 return -EBUSY;
688
stephen hemminger576eb622012-12-28 18:15:22 +0000689 /* if device is not up, change is not allowed
690 * if link is not present, only allowable state is disabled
691 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000692 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000693 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700694 return -ENETDOWN;
695
Florian Fainelli775dd692014-09-30 16:13:19 -0700696 br_set_state(p, state);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000697 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700698 return 0;
699}
700
stephen hemminger25c71c72012-11-13 07:53:05 +0000701/* Set/clear or port flags based on attribute */
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200702static int br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
703 int attrtype, unsigned long mask)
stephen hemminger25c71c72012-11-13 07:53:05 +0000704{
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200705 unsigned long flags;
706 int err;
707
708 if (!tb[attrtype])
709 return 0;
710
711 if (nla_get_u8(tb[attrtype]))
712 flags = p->flags | mask;
713 else
714 flags = p->flags & ~mask;
715
716 err = br_switchdev_set_port_flag(p, flags, mask);
717 if (err)
718 return err;
719
720 p->flags = flags;
721 return 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000722}
723
724/* Process bridge protocol info on port */
725static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
726{
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400727 unsigned long old_flags = p->flags;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800728 bool br_vlan_tunnel_old = false;
729 int err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000730
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200731 err = br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
732 if (err)
733 return err;
734
735 err = br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
736 if (err)
737 return err;
738
739 err = br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
740 if (err)
741 return err;
742
743 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
744 if (err)
745 return err;
746
747 err = br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
748 if (err)
749 return err;
750
751 err = br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
752 if (err)
753 return err;
754
755 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
756 if (err)
757 return err;
758
759 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
760 if (err)
761 return err;
762
763 err = br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
764 if (err)
765 return err;
766
767 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
768 if (err)
769 return err;
770
771 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
772 if (err)
773 return err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000774
Roopa Prabhuefa53562017-01-31 22:59:54 -0800775 br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200776 err = br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
777 if (err)
778 return err;
779
Roopa Prabhuefa53562017-01-31 22:59:54 -0800780 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
781 nbp_vlan_tunnel_info_flush(p);
782
stephen hemminger25c71c72012-11-13 07:53:05 +0000783 if (tb[IFLA_BRPORT_COST]) {
784 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
785 if (err)
786 return err;
787 }
788
789 if (tb[IFLA_BRPORT_PRIORITY]) {
790 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
791 if (err)
792 return err;
793 }
794
795 if (tb[IFLA_BRPORT_STATE]) {
796 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
797 if (err)
798 return err;
799 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400800
Nikolay Aleksandrov9b0c6e42015-10-06 14:12:01 +0200801 if (tb[IFLA_BRPORT_FLUSH])
802 br_fdb_delete_by_port(p->br, p, 0, 0);
803
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200804#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
805 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
806 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
807
808 err = br_multicast_set_port_router(p, mcast_router);
809 if (err)
810 return err;
811 }
812#endif
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300813
814 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
815 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
816
817 if (fwd_mask & BR_GROUPFWD_MACPAUSE)
818 return -EINVAL;
819 p->group_fwd_mask = fwd_mask;
820 }
821
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700822 err = br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS,
823 BR_NEIGH_SUPPRESS);
824 if (err)
825 return err;
826
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300827 err = br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
828 if (err)
829 return err;
830
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300831 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
832 struct net_device *backup_dev = NULL;
833 u32 backup_ifindex;
834
835 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
836 if (backup_ifindex) {
837 backup_dev = __dev_get_by_index(dev_net(p->dev),
838 backup_ifindex);
839 if (!backup_dev)
840 return -ENOENT;
841 }
842
843 err = nbp_backup_change(p, backup_dev);
844 if (err)
845 return err;
846 }
847
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400848 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000849 return 0;
850}
851
852/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800853int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000854{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200855 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
856 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
857 struct net_bridge_port *p;
stephen hemminger25c71c72012-11-13 07:53:05 +0000858 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000859 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300860 bool changed = false;
Scott Feldman41c498b2015-05-10 09:47:59 -0700861 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000862
Hong zhi guoc60ee672013-03-28 06:21:22 +0000863 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
864 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000865 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000866 return 0;
867
868 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000869 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800870 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000871 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000872 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000873 return -EINVAL;
874
Vlad Yasevich407af322013-02-13 12:00:12 +0000875 if (p && protinfo) {
876 if (protinfo->nla_type & NLA_F_NESTED) {
Johannes Bergfceb6432017-04-12 14:34:07 +0200877 err = nla_parse_nested(tb, IFLA_BRPORT_MAX, protinfo,
878 br_port_policy, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +0000879 if (err)
880 return err;
881
882 spin_lock_bh(&p->br->lock);
883 err = br_setport(p, tb);
884 spin_unlock_bh(&p->br->lock);
885 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800886 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000887 if (nla_len(protinfo) < sizeof(u8))
888 return -EINVAL;
889
890 spin_lock_bh(&p->br->lock);
891 err = br_set_port_state(p, nla_get_u8(protinfo));
892 spin_unlock_bh(&p->br->lock);
893 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000894 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000895 goto out;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300896 changed = true;
Vlad Yasevich407af322013-02-13 12:00:12 +0000897 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000898
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200899 if (afspec)
900 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed);
stephen hemminger25c71c72012-11-13 07:53:05 +0000901
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300902 if (changed)
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200903 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000904out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000905 return err;
906}
907
Vlad Yasevich407af322013-02-13 12:00:12 +0000908/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800909int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000910{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200911 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000912 struct net_bridge_port *p;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200913 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300914 bool changed = false;
Scott Feldman85080252015-05-10 09:48:03 -0700915 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000916
Hong zhi guoc60ee672013-03-28 06:21:22 +0000917 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000918 if (!afspec)
919 return 0;
920
921 p = br_port_get_rtnl(dev);
922 /* We want to accept dev as bridge itself as well */
923 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
924 return -EINVAL;
925
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200926 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed);
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300927 if (changed)
Roopa Prabhu02dba432015-01-14 20:02:25 -0800928 /* Send RTM_NEWLINK because userspace
929 * expects RTM_NEWLINK for vlan dels
930 */
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200931 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000932
933 return err;
934}
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200935
936static int br_validate(struct nlattr *tb[], struct nlattr *data[],
937 struct netlink_ext_ack *extack)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700938{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000939 if (tb[IFLA_ADDRESS]) {
940 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
941 return -EINVAL;
942 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
943 return -EADDRNOTAVAIL;
944 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700945
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900946 if (!data)
947 return 0;
948
949#ifdef CONFIG_BRIDGE_VLAN_FILTERING
950 if (data[IFLA_BR_VLAN_PROTOCOL]) {
951 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
952 case htons(ETH_P_8021Q):
953 case htons(ETH_P_8021AD):
954 break;
955 default:
956 return -EPROTONOSUPPORT;
957 }
958 }
Tobias Jungela2858602017-05-17 09:29:12 +0200959
960 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
961 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
962
963 if (defpvid >= VLAN_VID_MASK)
964 return -EINVAL;
965 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900966#endif
967
Thomas Graf32fe21c2007-03-22 11:59:03 -0700968 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700969}
970
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200971static int br_port_slave_changelink(struct net_device *brdev,
972 struct net_device *dev,
973 struct nlattr *tb[],
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +0200974 struct nlattr *data[],
975 struct netlink_ext_ack *extack)
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200976{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200977 struct net_bridge *br = netdev_priv(brdev);
978 int ret;
979
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200980 if (!data)
981 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200982
983 spin_lock_bh(&br->lock);
984 ret = br_setport(br_port_get_rtnl(dev), data);
985 spin_unlock_bh(&br->lock);
986
987 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200988}
989
Jiri Pirkoced82832014-09-05 15:51:29 +0200990static int br_port_fill_slave_info(struct sk_buff *skb,
991 const struct net_device *brdev,
992 const struct net_device *dev)
993{
994 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
995}
996
997static size_t br_port_get_slave_size(const struct net_device *brdev,
998 const struct net_device *dev)
999{
1000 return br_port_info_size();
1001}
1002
Jiri Pirko13323512014-09-05 15:51:32 +02001003static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1004 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
1005 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
1006 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +01001007 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1008 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1009 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001010 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001011 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001012 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001013 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1014 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001015 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001016 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001017 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001018 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001019 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001020 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001021 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001022 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001023 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1024 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1025 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1026 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1027 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1028 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001029 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1030 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1031 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001032 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001033 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001034 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001035 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001036 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
Jiri Pirko13323512014-09-05 15:51:32 +02001037};
1038
1039static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001040 struct nlattr *data[],
1041 struct netlink_ext_ack *extack)
Jiri Pirko13323512014-09-05 15:51:32 +02001042{
1043 struct net_bridge *br = netdev_priv(brdev);
1044 int err;
1045
1046 if (!data)
1047 return 0;
1048
1049 if (data[IFLA_BR_FORWARD_DELAY]) {
1050 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1051 if (err)
1052 return err;
1053 }
1054
1055 if (data[IFLA_BR_HELLO_TIME]) {
1056 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1057 if (err)
1058 return err;
1059 }
1060
1061 if (data[IFLA_BR_MAX_AGE]) {
1062 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1063 if (err)
1064 return err;
1065 }
1066
Jörg Thalheimaf615762015-03-18 10:06:58 +01001067 if (data[IFLA_BR_AGEING_TIME]) {
Scott Feldmanc62987b2015-10-08 19:23:19 -07001068 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1069 if (err)
1070 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +01001071 }
1072
1073 if (data[IFLA_BR_STP_STATE]) {
1074 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1075
1076 br_stp_set_enabled(br, stp_enabled);
1077 }
1078
1079 if (data[IFLA_BR_PRIORITY]) {
1080 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1081
1082 br_stp_set_bridge_priority(br, priority);
1083 }
1084
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001085 if (data[IFLA_BR_VLAN_FILTERING]) {
1086 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1087
1088 err = __br_vlan_filter_toggle(br, vlan_filter);
1089 if (err)
1090 return err;
1091 }
1092
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001093#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1094 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1095 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1096
1097 err = __br_vlan_set_proto(br, vlan_proto);
1098 if (err)
1099 return err;
1100 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001101
1102 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1103 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1104
1105 err = __br_vlan_set_default_pvid(br, defpvid);
1106 if (err)
1107 return err;
1108 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001109
1110 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1111 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1112
1113 err = br_vlan_set_stats(br, vlan_stats);
1114 if (err)
1115 return err;
1116 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001117#endif
1118
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001119 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1120 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1121
1122 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1123 return -EINVAL;
1124 br->group_fwd_mask = fwd_mask;
1125 }
1126
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001127 if (data[IFLA_BR_GROUP_ADDR]) {
1128 u8 new_addr[ETH_ALEN];
1129
1130 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1131 return -EINVAL;
1132 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1133 if (!is_link_local_ether_addr(new_addr))
1134 return -EINVAL;
1135 if (new_addr[5] == 1 || /* 802.3x Pause address */
1136 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1137 new_addr[5] == 3) /* 802.1X PAE address */
1138 return -EINVAL;
1139 spin_lock_bh(&br->lock);
1140 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1141 spin_unlock_bh(&br->lock);
1142 br->group_addr_set = true;
1143 br_recalculate_fwd_mask(br);
1144 }
1145
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +02001146 if (data[IFLA_BR_FDB_FLUSH])
1147 br_fdb_flush(br);
1148
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001149#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1150 if (data[IFLA_BR_MCAST_ROUTER]) {
1151 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1152
1153 err = br_multicast_set_router(br, multicast_router);
1154 if (err)
1155 return err;
1156 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001157
1158 if (data[IFLA_BR_MCAST_SNOOPING]) {
1159 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1160
1161 err = br_multicast_toggle(br, mcast_snooping);
1162 if (err)
1163 return err;
1164 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001165
1166 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1167 u8 val;
1168
1169 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1170 br->multicast_query_use_ifaddr = !!val;
1171 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001172
1173 if (data[IFLA_BR_MCAST_QUERIER]) {
1174 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1175
1176 err = br_multicast_set_querier(br, mcast_querier);
1177 if (err)
1178 return err;
1179 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001180
1181 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
1182 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
1183
1184 br->hash_elasticity = val;
1185 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001186
1187 if (data[IFLA_BR_MCAST_HASH_MAX]) {
1188 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1189
1190 err = br_multicast_set_hash_max(br, hash_max);
1191 if (err)
1192 return err;
1193 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001194
1195 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1196 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1197
1198 br->multicast_last_member_count = val;
1199 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001200
1201 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1202 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1203
1204 br->multicast_startup_query_count = val;
1205 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001206
1207 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1208 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1209
1210 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1211 }
1212
1213 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1214 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1215
1216 br->multicast_membership_interval = clock_t_to_jiffies(val);
1217 }
1218
1219 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1220 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1221
1222 br->multicast_querier_interval = clock_t_to_jiffies(val);
1223 }
1224
1225 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1226 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1227
1228 br->multicast_query_interval = clock_t_to_jiffies(val);
1229 }
1230
1231 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1232 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1233
1234 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1235 }
1236
1237 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1238 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1239
1240 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1241 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001242
1243 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1244 __u8 mcast_stats;
1245
1246 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1247 br->multicast_stats_enabled = !!mcast_stats;
1248 }
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001249
1250 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1251 __u8 igmp_version;
1252
1253 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1254 err = br_multicast_set_igmp_version(br, igmp_version);
1255 if (err)
1256 return err;
1257 }
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001258
1259#if IS_ENABLED(CONFIG_IPV6)
1260 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1261 __u8 mld_version;
1262
1263 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1264 err = br_multicast_set_mld_version(br, mld_version);
1265 if (err)
1266 return err;
1267 }
1268#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001269#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001270#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1271 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1272 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1273
1274 br->nf_call_iptables = val ? true : false;
1275 }
1276
1277 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1278 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1279
1280 br->nf_call_ip6tables = val ? true : false;
1281 }
1282
1283 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1284 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1285
1286 br->nf_call_arptables = val ? true : false;
1287 }
1288#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001289
Jiri Pirko13323512014-09-05 15:51:32 +02001290 return 0;
1291}
1292
Ivan Vecerab6677442017-01-20 18:12:17 +01001293static int br_dev_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001294 struct nlattr *tb[], struct nlattr *data[],
1295 struct netlink_ext_ack *extack)
Ivan Vecerab6677442017-01-20 18:12:17 +01001296{
1297 struct net_bridge *br = netdev_priv(dev);
1298 int err;
1299
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001300 err = register_netdevice(dev);
1301 if (err)
1302 return err;
1303
Ivan Vecerab6677442017-01-20 18:12:17 +01001304 if (tb[IFLA_ADDRESS]) {
1305 spin_lock_bh(&br->lock);
1306 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1307 spin_unlock_bh(&br->lock);
1308 }
1309
Matthias Schifferad744b22017-06-25 23:56:00 +02001310 err = br_changelink(dev, tb, data, extack);
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001311 if (err)
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001312 br_dev_delete(dev, NULL);
1313
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001314 return err;
Ivan Vecerab6677442017-01-20 18:12:17 +01001315}
1316
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001317static size_t br_get_size(const struct net_device *brdev)
1318{
1319 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1320 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1321 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001322 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1323 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1324 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001325 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001326#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1327 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001328 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001329 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001330#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001331 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001332 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001333 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001334 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001335 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001336 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1337 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001338 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1339 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1340 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1341 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001342 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001343#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1344 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001345 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001346 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001347 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001348 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001349 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1350 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1351 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1352 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001353 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1354 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1355 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1356 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1357 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1358 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001359 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001360 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001361#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001362#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1363 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1364 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1365 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1366#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001367 0;
1368}
1369
1370static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1371{
1372 struct net_bridge *br = netdev_priv(brdev);
1373 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1374 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1375 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001376 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1377 u32 stp_enabled = br->stp_enabled;
1378 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Ido Schimmel1f514452017-05-26 08:37:23 +02001379 u8 vlan_enabled = br_vlan_enabled(br->dev);
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001380 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001381
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001382 clockval = br_timer_value(&br->hello_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001383 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001384 return -EMSGSIZE;
1385 clockval = br_timer_value(&br->tcn_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001386 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001387 return -EMSGSIZE;
1388 clockval = br_timer_value(&br->topology_change_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001389 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1390 IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001391 return -EMSGSIZE;
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +01001392 clockval = br_timer_value(&br->gc_work.timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001393 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001394 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001395
1396 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1397 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001398 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1399 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1400 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001401 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001402 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001403 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1404 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1405 &br->bridge_id) ||
1406 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1407 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001408 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001409 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1410 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1411 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001412 br->topology_change_detected) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001413 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001414 return -EMSGSIZE;
1415
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001416#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001417 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001418 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1419 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001420 return -EMSGSIZE;
1421#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001422#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001423 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001424 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1425 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001426 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001427 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001428 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1429 br->multicast_stats_enabled) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001430 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001431 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001432 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1433 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001434 br->multicast_last_member_count) ||
1435 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001436 br->multicast_startup_query_count) ||
1437 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1438 br->multicast_igmp_version))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001439 return -EMSGSIZE;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001440#if IS_ENABLED(CONFIG_IPV6)
1441 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1442 br->multicast_mld_version))
1443 return -EMSGSIZE;
1444#endif
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001445 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001446 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1447 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001448 return -EMSGSIZE;
1449 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001450 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1451 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001452 return -EMSGSIZE;
1453 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001454 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1455 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001456 return -EMSGSIZE;
1457 clockval = jiffies_to_clock_t(br->multicast_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001458 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1459 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001460 return -EMSGSIZE;
1461 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001462 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1463 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001464 return -EMSGSIZE;
1465 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001466 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1467 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001468 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001469#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001470#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1471 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1472 br->nf_call_iptables ? 1 : 0) ||
1473 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1474 br->nf_call_ip6tables ? 1 : 0) ||
1475 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1476 br->nf_call_arptables ? 1 : 0))
1477 return -EMSGSIZE;
1478#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001479
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001480 return 0;
1481}
1482
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001483static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001484{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001485 struct net_bridge_port *p = NULL;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001486 struct net_bridge_vlan_group *vg;
1487 struct net_bridge_vlan *v;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001488 struct net_bridge *br;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001489 int numvls = 0;
1490
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001491 switch (attr) {
1492 case IFLA_STATS_LINK_XSTATS:
1493 br = netdev_priv(dev);
1494 vg = br_vlan_group(br);
1495 break;
1496 case IFLA_STATS_LINK_XSTATS_SLAVE:
1497 p = br_port_get_rtnl(dev);
1498 if (!p)
1499 return 0;
1500 br = p->br;
1501 vg = nbp_vlan_group(p);
1502 break;
1503 default:
1504 return 0;
1505 }
1506
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001507 if (vg) {
1508 /* we need to count all, even placeholder entries */
1509 list_for_each_entry(v, &vg->vlan_list, vlist)
1510 numvls++;
1511 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001512
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001513 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001514 nla_total_size(sizeof(struct br_mcast_stats)) +
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001515 nla_total_size(0);
1516}
1517
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001518static int br_fill_linkxstats(struct sk_buff *skb,
1519 const struct net_device *dev,
1520 int *prividx, int attr)
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001521{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001522 struct nlattr *nla __maybe_unused;
1523 struct net_bridge_port *p = NULL;
1524 struct net_bridge_vlan_group *vg;
1525 struct net_bridge_vlan *v;
1526 struct net_bridge *br;
1527 struct nlattr *nest;
1528 int vl_idx = 0;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001529
1530 switch (attr) {
1531 case IFLA_STATS_LINK_XSTATS:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001532 br = netdev_priv(dev);
1533 vg = br_vlan_group(br);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001534 break;
1535 case IFLA_STATS_LINK_XSTATS_SLAVE:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001536 p = br_port_get_rtnl(dev);
1537 if (!p)
1538 return 0;
1539 br = p->br;
1540 vg = nbp_vlan_group(p);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001541 break;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001542 default:
1543 return -EINVAL;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001544 }
1545
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001546 nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
1547 if (!nest)
1548 return -EMSGSIZE;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001549
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001550 if (vg) {
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001551 u16 pvid;
1552
1553 pvid = br_get_pvid(vg);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001554 list_for_each_entry(v, &vg->vlan_list, vlist) {
1555 struct bridge_vlan_xstats vxi;
1556 struct br_vlan_stats stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001557
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001558 if (++vl_idx < *prividx)
1559 continue;
1560 memset(&vxi, 0, sizeof(vxi));
1561 vxi.vid = v->vid;
Nikolay Aleksandrov61ba1a22016-08-17 12:53:10 +02001562 vxi.flags = v->flags;
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001563 if (v->vid == pvid)
1564 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001565 br_vlan_get_stats(v, &stats);
1566 vxi.rx_bytes = stats.rx_bytes;
1567 vxi.rx_packets = stats.rx_packets;
1568 vxi.tx_bytes = stats.tx_bytes;
1569 vxi.tx_packets = stats.tx_packets;
1570
1571 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1572 goto nla_put_failure;
1573 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001574 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001575
1576#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1577 if (++vl_idx >= *prividx) {
1578 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1579 sizeof(struct br_mcast_stats),
1580 BRIDGE_XSTATS_PAD);
1581 if (!nla)
1582 goto nla_put_failure;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001583 br_multicast_get_stats(br, p, nla_data(nla));
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001584 }
1585#endif
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001586 nla_nest_end(skb, nest);
1587 *prividx = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001588
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001589 return 0;
1590
1591nla_put_failure:
1592 nla_nest_end(skb, nest);
1593 *prividx = vl_idx;
1594
1595 return -EMSGSIZE;
1596}
Roopa Prabhufed0a152015-02-25 23:55:40 -08001597
Daniel Borkmann207895fd32015-01-29 12:15:03 +01001598static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001599 .family = AF_BRIDGE,
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001600 .get_link_af_size = br_get_link_af_size_filtered,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001601};
1602
stephen hemminger149ddd82012-06-26 05:48:45 +00001603struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001604 .kind = "bridge",
1605 .priv_size = sizeof(struct net_bridge),
1606 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001607 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001608 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001609 .validate = br_validate,
1610 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001611 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001612 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001613 .get_size = br_get_size,
1614 .fill_info = br_fill_info,
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001615 .fill_linkxstats = br_fill_linkxstats,
1616 .get_linkxstats_size = br_get_linkxstats_size,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001617
1618 .slave_maxtype = IFLA_BRPORT_MAX,
1619 .slave_policy = br_port_policy,
1620 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001621 .get_slave_size = br_port_get_slave_size,
1622 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001623};
1624
1625int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001626{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001627 int err;
1628
1629 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001630 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001631
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001632 err = rtnl_link_register(&br_link_ops);
1633 if (err)
1634 goto out_af;
1635
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001636 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001637
1638out_af:
1639 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001640 br_mdb_uninit();
1641 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001642}
1643
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001644void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001645{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001646 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001647 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001648 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001649}