1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Bridge netlink control interface
4 *
5 * Authors:
6 * Stephen Hemminger <shemminger@osdl.org>
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/etherdevice.h>
12 #include <net/rtnetlink.h>
13 #include <net/net_namespace.h>
14 #include <net/sock.h>
15 #include <uapi/linux/if_bridge.h>
16
17 #include "br_private.h"
18 #include "br_private_stp.h"
19 #include "br_private_cfm.h"
20 #include "br_private_tunnel.h"
21 #include "br_private_mcast_eht.h"
22
__get_num_vlan_infos(struct net_bridge_vlan_group * vg,u32 filter_mask)23 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
24 u32 filter_mask)
25 {
26 struct net_bridge_vlan *v;
27 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
28 u16 flags, pvid;
29 int num_vlans = 0;
30
31 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
32 return 0;
33
34 pvid = br_get_pvid(vg);
35 /* Count number of vlan infos */
36 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
37 flags = 0;
38 /* only a context, bridge vlan not activated */
39 if (!br_vlan_should_use(v))
40 continue;
41 if (v->vid == pvid)
42 flags |= BRIDGE_VLAN_INFO_PVID;
43
44 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
45 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
46
47 if (vid_range_start == 0) {
48 goto initvars;
49 } else if ((v->vid - vid_range_end) == 1 &&
50 flags == vid_range_flags) {
51 vid_range_end = v->vid;
52 continue;
53 } else {
54 if ((vid_range_end - vid_range_start) > 0)
55 num_vlans += 2;
56 else
57 num_vlans += 1;
58 }
59 initvars:
60 vid_range_start = v->vid;
61 vid_range_end = v->vid;
62 vid_range_flags = flags;
63 }
64
65 if (vid_range_start != 0) {
66 if ((vid_range_end - vid_range_start) > 0)
67 num_vlans += 2;
68 else
69 num_vlans += 1;
70 }
71
72 return num_vlans;
73 }
74
br_get_num_vlan_infos(struct net_bridge_vlan_group * vg,u32 filter_mask)75 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
76 u32 filter_mask)
77 {
78 int num_vlans;
79
80 if (!vg)
81 return 0;
82
83 if (filter_mask & RTEXT_FILTER_BRVLAN)
84 return vg->num_vlans;
85
86 rcu_read_lock();
87 num_vlans = __get_num_vlan_infos(vg, filter_mask);
88 rcu_read_unlock();
89
90 return num_vlans;
91 }
92
br_get_link_af_size_filtered(const struct net_device * dev,u32 filter_mask)93 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
94 u32 filter_mask)
95 {
96 struct net_bridge_vlan_group *vg = NULL;
97 struct net_bridge_port *p = NULL;
98 struct net_bridge *br = NULL;
99 u32 num_cfm_peer_mep_infos;
100 u32 num_cfm_mep_infos;
101 size_t vinfo_sz = 0;
102 int num_vlan_infos;
103
104 rcu_read_lock();
105 if (netif_is_bridge_port(dev)) {
106 p = br_port_get_check_rcu(dev);
107 if (p)
108 vg = nbp_vlan_group_rcu(p);
109 } else if (netif_is_bridge_master(dev)) {
110 br = netdev_priv(dev);
111 vg = br_vlan_group_rcu(br);
112 }
113 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
114 rcu_read_unlock();
115
116 if (p && (p->flags & BR_VLAN_TUNNEL))
117 vinfo_sz += br_get_vlan_tunnel_info_size(vg);
118
119 /* Each VLAN is returned in bridge_vlan_info along with flags */
120 vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
121
122 if (p && vg && (filter_mask & RTEXT_FILTER_MST))
123 vinfo_sz += br_mst_info_size(vg);
124
125 if (!(filter_mask & RTEXT_FILTER_CFM_STATUS))
126 return vinfo_sz;
127
128 if (!br)
129 return vinfo_sz;
130
131 /* CFM status info must be added */
132 br_cfm_mep_count(br, &num_cfm_mep_infos);
133 br_cfm_peer_mep_count(br, &num_cfm_peer_mep_infos);
134
135 vinfo_sz += nla_total_size(0); /* IFLA_BRIDGE_CFM */
136 /* For each status struct the MEP instance (u32) is added */
137 /* MEP instance (u32) + br_cfm_mep_status */
138 vinfo_sz += num_cfm_mep_infos *
139 /*IFLA_BRIDGE_CFM_MEP_STATUS_INSTANCE */
140 (nla_total_size(sizeof(u32))
141 /* IFLA_BRIDGE_CFM_MEP_STATUS_OPCODE_UNEXP_SEEN */
142 + nla_total_size(sizeof(u32))
143 /* IFLA_BRIDGE_CFM_MEP_STATUS_VERSION_UNEXP_SEEN */
144 + nla_total_size(sizeof(u32))
145 /* IFLA_BRIDGE_CFM_MEP_STATUS_RX_LEVEL_LOW_SEEN */
146 + nla_total_size(sizeof(u32)));
147 /* MEP instance (u32) + br_cfm_cc_peer_status */
148 vinfo_sz += num_cfm_peer_mep_infos *
149 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE */
150 (nla_total_size(sizeof(u32))
151 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PEER_MEPID */
152 + nla_total_size(sizeof(u32))
153 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_CCM_DEFECT */
154 + nla_total_size(sizeof(u32))
155 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_RDI */
156 + nla_total_size(sizeof(u32))
157 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PORT_TLV_VALUE */
158 + nla_total_size(sizeof(u8))
159 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_IF_TLV_VALUE */
160 + nla_total_size(sizeof(u8))
161 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEEN */
162 + nla_total_size(sizeof(u32))
163 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_TLV_SEEN */
164 + nla_total_size(sizeof(u32))
165 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEQ_UNEXP_SEEN */
166 + nla_total_size(sizeof(u32)));
167
168 return vinfo_sz;
169 }
170
br_port_info_size(void)171 static inline size_t br_port_info_size(void)
172 {
173 return nla_total_size(1) /* IFLA_BRPORT_STATE */
174 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
175 + nla_total_size(4) /* IFLA_BRPORT_COST */
176 + nla_total_size(1) /* IFLA_BRPORT_MODE */
177 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
178 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
179 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
180 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
181 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
182 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
183 + nla_total_size(1) /* IFLA_BRPORT_MCAST_FLOOD */
184 + nla_total_size(1) /* IFLA_BRPORT_BCAST_FLOOD */
185 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
186 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
187 + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */
188 + nla_total_size(1) /* IFLA_BRPORT_NEIGH_SUPPRESS */
189 + nla_total_size(1) /* IFLA_BRPORT_ISOLATED */
190 + nla_total_size(1) /* IFLA_BRPORT_LOCKED */
191 + nla_total_size(1) /* IFLA_BRPORT_MAB */
192 + nla_total_size(1) /* IFLA_BRPORT_NEIGH_VLAN_SUPPRESS */
193 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
194 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
195 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
196 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
197 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
198 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
199 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
200 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
201 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
202 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
203 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
204 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
205 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
206 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_N_GROUPS */
207 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_MAX_GROUPS */
208 #endif
209 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_GROUP_FWD_MASK */
210 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_RING_OPEN */
211 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_IN_OPEN */
212 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT */
213 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_CNT */
214 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_BACKUP_NHID */
215 + 0;
216 }
217
br_nlmsg_size(struct net_device * dev,u32 filter_mask)218 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
219 {
220 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
221 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
222 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
223 + nla_total_size(4) /* IFLA_MASTER */
224 + nla_total_size(4) /* IFLA_MTU */
225 + nla_total_size(4) /* IFLA_LINK */
226 + nla_total_size(1) /* IFLA_OPERSTATE */
227 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
228 + nla_total_size(br_get_link_af_size_filtered(dev,
229 filter_mask)) /* IFLA_AF_SPEC */
230 + nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */
231 }
232
br_port_fill_attrs(struct sk_buff * skb,const struct net_bridge_port * p)233 static int br_port_fill_attrs(struct sk_buff *skb,
234 const struct net_bridge_port *p)
235 {
236 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
237 struct net_bridge_port *backup_p;
238 u64 timerval;
239
240 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
241 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
242 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
243 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
244 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
245 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
246 !!(p->flags & BR_ROOT_BLOCK)) ||
247 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
248 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
249 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
250 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
251 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
252 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
253 !!(p->flags & BR_FLOOD)) ||
254 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
255 !!(p->flags & BR_MCAST_FLOOD)) ||
256 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
257 !!(p->flags & BR_BCAST_FLOOD)) ||
258 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
259 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
260 !!(p->flags & BR_PROXYARP_WIFI)) ||
261 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
262 &p->designated_root) ||
263 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
264 &p->designated_bridge) ||
265 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
266 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
267 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
268 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
269 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
270 p->topology_change_ack) ||
271 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
272 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
273 BR_VLAN_TUNNEL)) ||
274 nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
275 nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
276 !!(p->flags & BR_NEIGH_SUPPRESS)) ||
277 nla_put_u8(skb, IFLA_BRPORT_MRP_RING_OPEN, !!(p->flags &
278 BR_MRP_LOST_CONT)) ||
279 nla_put_u8(skb, IFLA_BRPORT_MRP_IN_OPEN,
280 !!(p->flags & BR_MRP_LOST_IN_CONT)) ||
281 nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)) ||
282 nla_put_u8(skb, IFLA_BRPORT_LOCKED, !!(p->flags & BR_PORT_LOCKED)) ||
283 nla_put_u8(skb, IFLA_BRPORT_MAB, !!(p->flags & BR_PORT_MAB)) ||
284 nla_put_u8(skb, IFLA_BRPORT_NEIGH_VLAN_SUPPRESS,
285 !!(p->flags & BR_NEIGH_VLAN_SUPPRESS)))
286 return -EMSGSIZE;
287
288 timerval = br_timer_value(&p->message_age_timer);
289 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
290 IFLA_BRPORT_PAD))
291 return -EMSGSIZE;
292 timerval = br_timer_value(&p->forward_delay_timer);
293 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
294 IFLA_BRPORT_PAD))
295 return -EMSGSIZE;
296 timerval = br_timer_value(&p->hold_timer);
297 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
298 IFLA_BRPORT_PAD))
299 return -EMSGSIZE;
300
301 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
302 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
303 p->multicast_ctx.multicast_router) ||
304 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT,
305 p->multicast_eht_hosts_limit) ||
306 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_CNT,
307 p->multicast_eht_hosts_cnt) ||
308 nla_put_u32(skb, IFLA_BRPORT_MCAST_N_GROUPS,
309 br_multicast_ngroups_get(&p->multicast_ctx)) ||
310 nla_put_u32(skb, IFLA_BRPORT_MCAST_MAX_GROUPS,
311 br_multicast_ngroups_get_max(&p->multicast_ctx)))
312 return -EMSGSIZE;
313 #endif
314
315 /* we might be called only with br->lock */
316 rcu_read_lock();
317 backup_p = rcu_dereference(p->backup_port);
318 if (backup_p)
319 nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT,
320 backup_p->dev->ifindex);
321 rcu_read_unlock();
322
323 if (p->backup_nhid &&
324 nla_put_u32(skb, IFLA_BRPORT_BACKUP_NHID, p->backup_nhid))
325 return -EMSGSIZE;
326
327 return 0;
328 }
329
br_fill_ifvlaninfo_range(struct sk_buff * skb,u16 vid_start,u16 vid_end,u16 flags)330 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
331 u16 vid_end, u16 flags)
332 {
333 struct bridge_vlan_info vinfo;
334
335 if ((vid_end - vid_start) > 0) {
336 /* add range to skb */
337 vinfo.vid = vid_start;
338 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
339 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
340 sizeof(vinfo), &vinfo))
341 goto nla_put_failure;
342
343 vinfo.vid = vid_end;
344 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
345 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
346 sizeof(vinfo), &vinfo))
347 goto nla_put_failure;
348 } else {
349 vinfo.vid = vid_start;
350 vinfo.flags = flags;
351 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
352 sizeof(vinfo), &vinfo))
353 goto nla_put_failure;
354 }
355
356 return 0;
357
358 nla_put_failure:
359 return -EMSGSIZE;
360 }
361
br_fill_ifvlaninfo_compressed(struct sk_buff * skb,struct net_bridge_vlan_group * vg)362 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
363 struct net_bridge_vlan_group *vg)
364 {
365 struct net_bridge_vlan *v;
366 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
367 u16 flags, pvid;
368 int err = 0;
369
370 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
371 * and mark vlan info with begin and end flags
372 * if vlaninfo represents a range
373 */
374 pvid = br_get_pvid(vg);
375 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
376 flags = 0;
377 if (!br_vlan_should_use(v))
378 continue;
379 if (v->vid == pvid)
380 flags |= BRIDGE_VLAN_INFO_PVID;
381
382 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
383 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
384
385 if (vid_range_start == 0) {
386 goto initvars;
387 } else if ((v->vid - vid_range_end) == 1 &&
388 flags == vid_range_flags) {
389 vid_range_end = v->vid;
390 continue;
391 } else {
392 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
393 vid_range_end,
394 vid_range_flags);
395 if (err)
396 return err;
397 }
398
399 initvars:
400 vid_range_start = v->vid;
401 vid_range_end = v->vid;
402 vid_range_flags = flags;
403 }
404
405 if (vid_range_start != 0) {
406 /* Call it once more to send any left over vlans */
407 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
408 vid_range_end,
409 vid_range_flags);
410 if (err)
411 return err;
412 }
413
414 return 0;
415 }
416
br_fill_ifvlaninfo(struct sk_buff * skb,struct net_bridge_vlan_group * vg)417 static int br_fill_ifvlaninfo(struct sk_buff *skb,
418 struct net_bridge_vlan_group *vg)
419 {
420 struct bridge_vlan_info vinfo;
421 struct net_bridge_vlan *v;
422 u16 pvid;
423
424 pvid = br_get_pvid(vg);
425 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
426 if (!br_vlan_should_use(v))
427 continue;
428
429 vinfo.vid = v->vid;
430 vinfo.flags = 0;
431 if (v->vid == pvid)
432 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
433
434 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
435 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
436
437 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
438 sizeof(vinfo), &vinfo))
439 goto nla_put_failure;
440 }
441
442 return 0;
443
444 nla_put_failure:
445 return -EMSGSIZE;
446 }
447
448 /*
449 * Create one netlink message for one interface
450 * Contains port and master info as well as carrier and bridge state.
451 */
br_fill_ifinfo(struct sk_buff * skb,const struct net_bridge_port * port,u32 pid,u32 seq,int event,unsigned int flags,u32 filter_mask,const struct net_device * dev,bool getlink)452 static int br_fill_ifinfo(struct sk_buff *skb,
453 const struct net_bridge_port *port,
454 u32 pid, u32 seq, int event, unsigned int flags,
455 u32 filter_mask, const struct net_device *dev,
456 bool getlink)
457 {
458 u8 operstate = netif_running(dev) ? READ_ONCE(dev->operstate) :
459 IF_OPER_DOWN;
460 struct nlattr *af = NULL;
461 struct net_bridge *br;
462 struct ifinfomsg *hdr;
463 struct nlmsghdr *nlh;
464
465 if (port)
466 br = port->br;
467 else
468 br = netdev_priv(dev);
469
470 br_debug(br, "br_fill_info event %d port %s master %s\n",
471 event, dev->name, br->dev->name);
472
473 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
474 if (nlh == NULL)
475 return -EMSGSIZE;
476
477 hdr = nlmsg_data(nlh);
478 hdr->ifi_family = AF_BRIDGE;
479 hdr->__ifi_pad = 0;
480 hdr->ifi_type = dev->type;
481 hdr->ifi_index = dev->ifindex;
482 hdr->ifi_flags = dev_get_flags(dev);
483 hdr->ifi_change = 0;
484
485 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
486 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
487 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
488 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
489 (dev->addr_len &&
490 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
491 (dev->ifindex != dev_get_iflink(dev) &&
492 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
493 goto nla_put_failure;
494
495 if (event == RTM_NEWLINK && port) {
496 struct nlattr *nest;
497
498 nest = nla_nest_start(skb, IFLA_PROTINFO);
499 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
500 goto nla_put_failure;
501 nla_nest_end(skb, nest);
502 }
503
504 if (filter_mask & (RTEXT_FILTER_BRVLAN |
505 RTEXT_FILTER_BRVLAN_COMPRESSED |
506 RTEXT_FILTER_MRP |
507 RTEXT_FILTER_CFM_CONFIG |
508 RTEXT_FILTER_CFM_STATUS |
509 RTEXT_FILTER_MST)) {
510 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
511 if (!af)
512 goto nla_put_failure;
513 }
514
515 /* Check if the VID information is requested */
516 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
517 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
518 struct net_bridge_vlan_group *vg;
519 int err;
520
521 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
522 rcu_read_lock();
523 if (port)
524 vg = nbp_vlan_group_rcu(port);
525 else
526 vg = br_vlan_group_rcu(br);
527
528 if (!vg || !vg->num_vlans) {
529 rcu_read_unlock();
530 goto done;
531 }
532 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
533 err = br_fill_ifvlaninfo_compressed(skb, vg);
534 else
535 err = br_fill_ifvlaninfo(skb, vg);
536
537 if (port && (port->flags & BR_VLAN_TUNNEL))
538 err = br_fill_vlan_tunnel_info(skb, vg);
539 rcu_read_unlock();
540 if (err)
541 goto nla_put_failure;
542 }
543
544 if (filter_mask & RTEXT_FILTER_MRP) {
545 int err;
546
547 if (!br_mrp_enabled(br) || port)
548 goto done;
549
550 rcu_read_lock();
551 err = br_mrp_fill_info(skb, br);
552 rcu_read_unlock();
553
554 if (err)
555 goto nla_put_failure;
556 }
557
558 if (filter_mask & (RTEXT_FILTER_CFM_CONFIG | RTEXT_FILTER_CFM_STATUS)) {
559 struct nlattr *cfm_nest = NULL;
560 int err;
561
562 if (!br_cfm_created(br) || port)
563 goto done;
564
565 cfm_nest = nla_nest_start(skb, IFLA_BRIDGE_CFM);
566 if (!cfm_nest)
567 goto nla_put_failure;
568
569 if (filter_mask & RTEXT_FILTER_CFM_CONFIG) {
570 rcu_read_lock();
571 err = br_cfm_config_fill_info(skb, br);
572 rcu_read_unlock();
573 if (err)
574 goto nla_put_failure;
575 }
576
577 if (filter_mask & RTEXT_FILTER_CFM_STATUS) {
578 rcu_read_lock();
579 err = br_cfm_status_fill_info(skb, br, getlink);
580 rcu_read_unlock();
581 if (err)
582 goto nla_put_failure;
583 }
584
585 nla_nest_end(skb, cfm_nest);
586 }
587
588 if ((filter_mask & RTEXT_FILTER_MST) &&
589 br_opt_get(br, BROPT_MST_ENABLED) && port) {
590 const struct net_bridge_vlan_group *vg = nbp_vlan_group(port);
591 struct nlattr *mst_nest;
592 int err;
593
594 if (!vg || !vg->num_vlans)
595 goto done;
596
597 mst_nest = nla_nest_start(skb, IFLA_BRIDGE_MST);
598 if (!mst_nest)
599 goto nla_put_failure;
600
601 err = br_mst_fill_info(skb, vg);
602 if (err)
603 goto nla_put_failure;
604
605 nla_nest_end(skb, mst_nest);
606 }
607
608 done:
609 if (af) {
610 if (nlmsg_get_pos(skb) - (void *)af > nla_attr_size(0))
611 nla_nest_end(skb, af);
612 else
613 nla_nest_cancel(skb, af);
614 }
615
616 nlmsg_end(skb, nlh);
617 return 0;
618
619 nla_put_failure:
620 nlmsg_cancel(skb, nlh);
621 return -EMSGSIZE;
622 }
623
br_info_notify(int event,const struct net_bridge * br,const struct net_bridge_port * port,u32 filter)624 void br_info_notify(int event, const struct net_bridge *br,
625 const struct net_bridge_port *port, u32 filter)
626 {
627 struct net_device *dev;
628 struct sk_buff *skb;
629 int err = -ENOBUFS;
630 struct net *net;
631 u16 port_no = 0;
632
633 if (WARN_ON(!port && !br))
634 return;
635
636 if (port) {
637 dev = port->dev;
638 br = port->br;
639 port_no = port->port_no;
640 } else {
641 dev = br->dev;
642 }
643
644 net = dev_net(dev);
645 br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
646
647 skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
648 if (skb == NULL)
649 goto errout;
650
651 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev, false);
652 if (err < 0) {
653 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
654 WARN_ON(err == -EMSGSIZE);
655 kfree_skb(skb);
656 goto errout;
657 }
658 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
659 return;
660 errout:
661 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
662 }
663
664 /* Notify listeners of a change in bridge or port information */
br_ifinfo_notify(int event,const struct net_bridge * br,const struct net_bridge_port * port)665 void br_ifinfo_notify(int event, const struct net_bridge *br,
666 const struct net_bridge_port *port)
667 {
668 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
669
670 br_info_notify(event, br, port, filter);
671 }
672
673 /*
674 * Dump information about all ports, in response to GETLINK
675 */
br_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)676 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
677 struct net_device *dev, u32 filter_mask, int nlflags)
678 {
679 struct net_bridge_port *port = br_port_get_rtnl(dev);
680
681 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
682 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) &&
683 !(filter_mask & RTEXT_FILTER_MRP) &&
684 !(filter_mask & RTEXT_FILTER_CFM_CONFIG) &&
685 !(filter_mask & RTEXT_FILTER_CFM_STATUS))
686 return 0;
687
688 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
689 filter_mask, dev, true);
690 }
691
br_vlan_info(struct net_bridge * br,struct net_bridge_port * p,int cmd,struct bridge_vlan_info * vinfo,bool * changed,struct netlink_ext_ack * extack)692 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
693 int cmd, struct bridge_vlan_info *vinfo, bool *changed,
694 struct netlink_ext_ack *extack)
695 {
696 bool curr_change;
697 int err = 0;
698
699 switch (cmd) {
700 case RTM_SETLINK:
701 if (p) {
702 /* if the MASTER flag is set this will act on the global
703 * per-VLAN entry as well
704 */
705 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
706 &curr_change, extack);
707 } else {
708 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
709 err = br_vlan_add(br, vinfo->vid, vinfo->flags,
710 &curr_change, extack);
711 }
712 if (curr_change)
713 *changed = true;
714 break;
715
716 case RTM_DELLINK:
717 if (p) {
718 if (!nbp_vlan_delete(p, vinfo->vid))
719 *changed = true;
720
721 if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
722 !br_vlan_delete(p->br, vinfo->vid))
723 *changed = true;
724 } else if (!br_vlan_delete(br, vinfo->vid)) {
725 *changed = true;
726 }
727 break;
728 }
729
730 return err;
731 }
732
br_process_vlan_info(struct net_bridge * br,struct net_bridge_port * p,int cmd,struct bridge_vlan_info * vinfo_curr,struct bridge_vlan_info ** vinfo_last,bool * changed,struct netlink_ext_ack * extack)733 int br_process_vlan_info(struct net_bridge *br,
734 struct net_bridge_port *p, int cmd,
735 struct bridge_vlan_info *vinfo_curr,
736 struct bridge_vlan_info **vinfo_last,
737 bool *changed,
738 struct netlink_ext_ack *extack)
739 {
740 int err, rtm_cmd;
741
742 if (!br_vlan_valid_id(vinfo_curr->vid, extack))
743 return -EINVAL;
744
745 /* needed for vlan-only NEWVLAN/DELVLAN notifications */
746 rtm_cmd = br_afspec_cmd_to_rtm(cmd);
747
748 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
749 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
750 return -EINVAL;
751 *vinfo_last = vinfo_curr;
752 return 0;
753 }
754
755 if (*vinfo_last) {
756 struct bridge_vlan_info tmp_vinfo;
757 int v, v_change_start = 0;
758
759 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
760 return -EINVAL;
761
762 memcpy(&tmp_vinfo, *vinfo_last,
763 sizeof(struct bridge_vlan_info));
764 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
765 bool curr_change = false;
766
767 tmp_vinfo.vid = v;
768 err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
769 extack);
770 if (err)
771 break;
772 if (curr_change) {
773 *changed = curr_change;
774 if (!v_change_start)
775 v_change_start = v;
776 } else {
777 /* nothing to notify yet */
778 if (!v_change_start)
779 continue;
780 br_vlan_notify(br, p, v_change_start,
781 v - 1, rtm_cmd);
782 v_change_start = 0;
783 }
784 cond_resched();
785 }
786 /* v_change_start is set only if the last/whole range changed */
787 if (v_change_start)
788 br_vlan_notify(br, p, v_change_start,
789 v - 1, rtm_cmd);
790
791 *vinfo_last = NULL;
792
793 return err;
794 }
795
796 err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
797 if (*changed)
798 br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
799
800 return err;
801 }
802
br_afspec(struct net_bridge * br,struct net_bridge_port * p,struct nlattr * af_spec,int cmd,bool * changed,struct netlink_ext_ack * extack)803 static int br_afspec(struct net_bridge *br,
804 struct net_bridge_port *p,
805 struct nlattr *af_spec,
806 int cmd, bool *changed,
807 struct netlink_ext_ack *extack)
808 {
809 struct bridge_vlan_info *vinfo_curr = NULL;
810 struct bridge_vlan_info *vinfo_last = NULL;
811 struct nlattr *attr;
812 struct vtunnel_info tinfo_last = {};
813 struct vtunnel_info tinfo_curr = {};
814 int err = 0, rem;
815
816 nla_for_each_nested(attr, af_spec, rem) {
817 err = 0;
818 switch (nla_type(attr)) {
819 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
820 if (!p || !(p->flags & BR_VLAN_TUNNEL))
821 return -EINVAL;
822 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
823 if (err)
824 return err;
825 err = br_process_vlan_tunnel_info(br, p, cmd,
826 &tinfo_curr,
827 &tinfo_last,
828 changed);
829 if (err)
830 return err;
831 break;
832 case IFLA_BRIDGE_VLAN_INFO:
833 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
834 return -EINVAL;
835 vinfo_curr = nla_data(attr);
836 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
837 &vinfo_last, changed,
838 extack);
839 if (err)
840 return err;
841 break;
842 case IFLA_BRIDGE_MRP:
843 err = br_mrp_parse(br, p, attr, cmd, extack);
844 if (err)
845 return err;
846 break;
847 case IFLA_BRIDGE_CFM:
848 err = br_cfm_parse(br, p, attr, cmd, extack);
849 if (err)
850 return err;
851 break;
852 case IFLA_BRIDGE_MST:
853 if (!p) {
854 NL_SET_ERR_MSG(extack,
855 "MST states can only be set on bridge ports");
856 return -EINVAL;
857 }
858
859 if (cmd != RTM_SETLINK) {
860 NL_SET_ERR_MSG(extack,
861 "MST states can only be set through RTM_SETLINK");
862 return -EINVAL;
863 }
864
865 err = br_mst_process(p, attr, extack);
866 if (err)
867 return err;
868 break;
869 }
870 }
871
872 return err;
873 }
874
875 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
876 [IFLA_BRPORT_UNSPEC] = { .strict_start_type =
877 IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT + 1 },
878 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
879 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
880 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
881 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
882 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
883 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
884 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
885 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
886 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
887 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
888 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
889 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
890 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
891 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
892 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
893 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
894 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
895 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
896 [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 },
897 [IFLA_BRPORT_LOCKED] = { .type = NLA_U8 },
898 [IFLA_BRPORT_MAB] = { .type = NLA_U8 },
899 [IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
900 [IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT] = { .type = NLA_U32 },
901 [IFLA_BRPORT_MCAST_N_GROUPS] = { .type = NLA_REJECT },
902 [IFLA_BRPORT_MCAST_MAX_GROUPS] = { .type = NLA_U32 },
903 [IFLA_BRPORT_NEIGH_VLAN_SUPPRESS] = NLA_POLICY_MAX(NLA_U8, 1),
904 [IFLA_BRPORT_BACKUP_NHID] = { .type = NLA_U32 },
905 };
906
907 /* Change the state of the port and notify spanning tree */
br_set_port_state(struct net_bridge_port * p,u8 state)908 static int br_set_port_state(struct net_bridge_port *p, u8 state)
909 {
910 if (state > BR_STATE_BLOCKING)
911 return -EINVAL;
912
913 /* if kernel STP is running, don't allow changes */
914 if (p->br->stp_enabled == BR_KERNEL_STP)
915 return -EBUSY;
916
917 /* if device is not up, change is not allowed
918 * if link is not present, only allowable state is disabled
919 */
920 if (!netif_running(p->dev) ||
921 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
922 return -ENETDOWN;
923
924 br_set_state(p, state);
925 br_port_state_selection(p->br);
926 return 0;
927 }
928
929 /* Set/clear or port flags based on attribute */
br_set_port_flag(struct net_bridge_port * p,struct nlattr * tb[],int attrtype,unsigned long mask)930 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
931 int attrtype, unsigned long mask)
932 {
933 if (!tb[attrtype])
934 return;
935
936 if (nla_get_u8(tb[attrtype]))
937 p->flags |= mask;
938 else
939 p->flags &= ~mask;
940 }
941
942 /* Process bridge protocol info on port */
br_setport(struct net_bridge_port * p,struct nlattr * tb[],struct netlink_ext_ack * extack)943 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[],
944 struct netlink_ext_ack *extack)
945 {
946 unsigned long old_flags, changed_mask;
947 bool br_vlan_tunnel_old;
948 int err;
949
950 old_flags = p->flags;
951 br_vlan_tunnel_old = (old_flags & BR_VLAN_TUNNEL) ? true : false;
952
953 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
954 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
955 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE,
956 BR_MULTICAST_FAST_LEAVE);
957 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
958 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
959 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
960 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
961 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST,
962 BR_MULTICAST_TO_UNICAST);
963 br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
964 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
965 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
966 br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
967 br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS, BR_NEIGH_SUPPRESS);
968 br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
969 br_set_port_flag(p, tb, IFLA_BRPORT_LOCKED, BR_PORT_LOCKED);
970 br_set_port_flag(p, tb, IFLA_BRPORT_MAB, BR_PORT_MAB);
971 br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_VLAN_SUPPRESS,
972 BR_NEIGH_VLAN_SUPPRESS);
973
974 if ((p->flags & BR_PORT_MAB) &&
975 (!(p->flags & BR_PORT_LOCKED) || !(p->flags & BR_LEARNING))) {
976 NL_SET_ERR_MSG(extack, "Bridge port must be locked and have learning enabled when MAB is enabled");
977 p->flags = old_flags;
978 return -EINVAL;
979 } else if (!(p->flags & BR_PORT_MAB) && (old_flags & BR_PORT_MAB)) {
980 struct net_bridge_fdb_flush_desc desc = {
981 .flags = BIT(BR_FDB_LOCKED),
982 .flags_mask = BIT(BR_FDB_LOCKED),
983 .port_ifindex = p->dev->ifindex,
984 };
985
986 br_fdb_flush(p->br, &desc);
987 }
988
989 changed_mask = old_flags ^ p->flags;
990
991 err = br_switchdev_set_port_flag(p, p->flags, changed_mask, extack);
992 if (err) {
993 p->flags = old_flags;
994 return err;
995 }
996
997 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
998 nbp_vlan_tunnel_info_flush(p);
999
1000 br_port_flags_change(p, changed_mask);
1001
1002 if (tb[IFLA_BRPORT_COST]) {
1003 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
1004 if (err)
1005 return err;
1006 }
1007
1008 if (tb[IFLA_BRPORT_PRIORITY]) {
1009 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
1010 if (err)
1011 return err;
1012 }
1013
1014 if (tb[IFLA_BRPORT_STATE]) {
1015 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
1016 if (err)
1017 return err;
1018 }
1019
1020 if (tb[IFLA_BRPORT_FLUSH])
1021 br_fdb_delete_by_port(p->br, p, 0, 0);
1022
1023 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1024 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
1025 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
1026
1027 err = br_multicast_set_port_router(&p->multicast_ctx,
1028 mcast_router);
1029 if (err)
1030 return err;
1031 }
1032
1033 if (tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]) {
1034 u32 hlimit;
1035
1036 hlimit = nla_get_u32(tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]);
1037 err = br_multicast_eht_set_hosts_limit(p, hlimit);
1038 if (err)
1039 return err;
1040 }
1041
1042 if (tb[IFLA_BRPORT_MCAST_MAX_GROUPS]) {
1043 u32 max_groups;
1044
1045 max_groups = nla_get_u32(tb[IFLA_BRPORT_MCAST_MAX_GROUPS]);
1046 br_multicast_ngroups_set_max(&p->multicast_ctx, max_groups);
1047 }
1048 #endif
1049
1050 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
1051 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
1052
1053 if (fwd_mask & BR_GROUPFWD_MACPAUSE)
1054 return -EINVAL;
1055 p->group_fwd_mask = fwd_mask;
1056 }
1057
1058 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
1059 struct net_device *backup_dev = NULL;
1060 u32 backup_ifindex;
1061
1062 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
1063 if (backup_ifindex) {
1064 backup_dev = __dev_get_by_index(dev_net(p->dev),
1065 backup_ifindex);
1066 if (!backup_dev)
1067 return -ENOENT;
1068 }
1069
1070 err = nbp_backup_change(p, backup_dev);
1071 if (err)
1072 return err;
1073 }
1074
1075 if (tb[IFLA_BRPORT_BACKUP_NHID]) {
1076 u32 backup_nhid = nla_get_u32(tb[IFLA_BRPORT_BACKUP_NHID]);
1077
1078 WRITE_ONCE(p->backup_nhid, backup_nhid);
1079 }
1080
1081 return 0;
1082 }
1083
1084 /* Change state and parameters on port. */
br_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)1085 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
1086 struct netlink_ext_ack *extack)
1087 {
1088 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1089 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
1090 struct net_bridge_port *p;
1091 struct nlattr *protinfo;
1092 struct nlattr *afspec;
1093 bool changed = false;
1094 int err = 0;
1095
1096 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
1097 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1098 if (!protinfo && !afspec)
1099 return 0;
1100
1101 p = br_port_get_rtnl(dev);
1102 /* We want to accept dev as bridge itself if the AF_SPEC
1103 * is set to see if someone is setting vlan info on the bridge
1104 */
1105 if (!p && !afspec)
1106 return -EINVAL;
1107
1108 if (p && protinfo) {
1109 if (protinfo->nla_type & NLA_F_NESTED) {
1110 err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
1111 protinfo,
1112 br_port_policy,
1113 NULL);
1114 if (err)
1115 return err;
1116
1117 spin_lock_bh(&p->br->lock);
1118 err = br_setport(p, tb, extack);
1119 spin_unlock_bh(&p->br->lock);
1120 } else {
1121 /* Binary compatibility with old RSTP */
1122 if (nla_len(protinfo) < sizeof(u8))
1123 return -EINVAL;
1124
1125 spin_lock_bh(&p->br->lock);
1126 err = br_set_port_state(p, nla_get_u8(protinfo));
1127 spin_unlock_bh(&p->br->lock);
1128 }
1129 if (err)
1130 goto out;
1131 changed = true;
1132 }
1133
1134 if (afspec)
1135 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
1136
1137 if (changed)
1138 br_ifinfo_notify(RTM_NEWLINK, br, p);
1139 out:
1140 return err;
1141 }
1142
1143 /* Delete port information */
br_dellink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags)1144 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
1145 {
1146 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1147 struct net_bridge_port *p;
1148 struct nlattr *afspec;
1149 bool changed = false;
1150 int err = 0;
1151
1152 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1153 if (!afspec)
1154 return 0;
1155
1156 p = br_port_get_rtnl(dev);
1157 /* We want to accept dev as bridge itself as well */
1158 if (!p && !netif_is_bridge_master(dev))
1159 return -EINVAL;
1160
1161 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
1162 if (changed)
1163 /* Send RTM_NEWLINK because userspace
1164 * expects RTM_NEWLINK for vlan dels
1165 */
1166 br_ifinfo_notify(RTM_NEWLINK, br, p);
1167
1168 return err;
1169 }
1170
br_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1171 static int br_validate(struct nlattr *tb[], struct nlattr *data[],
1172 struct netlink_ext_ack *extack)
1173 {
1174 if (tb[IFLA_ADDRESS]) {
1175 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1176 return -EINVAL;
1177 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1178 return -EADDRNOTAVAIL;
1179 }
1180
1181 if (!data)
1182 return 0;
1183
1184 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1185 if (data[IFLA_BR_VLAN_PROTOCOL] &&
1186 !eth_type_vlan(nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])))
1187 return -EPROTONOSUPPORT;
1188
1189 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1190 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1191
1192 if (defpvid >= VLAN_VID_MASK)
1193 return -EINVAL;
1194 }
1195 #endif
1196
1197 return 0;
1198 }
1199
br_port_slave_changelink(struct net_device * brdev,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1200 static int br_port_slave_changelink(struct net_device *brdev,
1201 struct net_device *dev,
1202 struct nlattr *tb[],
1203 struct nlattr *data[],
1204 struct netlink_ext_ack *extack)
1205 {
1206 struct net_bridge *br = netdev_priv(brdev);
1207 int ret;
1208
1209 if (!data)
1210 return 0;
1211
1212 spin_lock_bh(&br->lock);
1213 ret = br_setport(br_port_get_rtnl(dev), data, extack);
1214 spin_unlock_bh(&br->lock);
1215
1216 return ret;
1217 }
1218
br_port_fill_slave_info(struct sk_buff * skb,const struct net_device * brdev,const struct net_device * dev)1219 static int br_port_fill_slave_info(struct sk_buff *skb,
1220 const struct net_device *brdev,
1221 const struct net_device *dev)
1222 {
1223 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1224 }
1225
br_port_get_slave_size(const struct net_device * brdev,const struct net_device * dev)1226 static size_t br_port_get_slave_size(const struct net_device *brdev,
1227 const struct net_device *dev)
1228 {
1229 return br_port_info_size();
1230 }
1231
1232 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1233 [IFLA_BR_UNSPEC] = { .strict_start_type =
1234 IFLA_BR_FDB_N_LEARNED },
1235 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
1236 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
1237 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
1238 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1239 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1240 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
1241 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
1242 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
1243 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
1244 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1245 .len = ETH_ALEN },
1246 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
1247 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
1248 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
1249 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
1250 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
1251 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
1252 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
1253 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
1254 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1255 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1256 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1257 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1258 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1259 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
1260 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1261 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1262 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
1263 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
1264 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
1265 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
1266 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
1267 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
1268 [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
1269 [IFLA_BR_MULTI_BOOLOPT] =
1270 NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
1271 [IFLA_BR_FDB_N_LEARNED] = { .type = NLA_REJECT },
1272 [IFLA_BR_FDB_MAX_LEARNED] = { .type = NLA_U32 },
1273 };
1274
br_changelink(struct net_device * brdev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1275 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
1276 struct nlattr *data[],
1277 struct netlink_ext_ack *extack)
1278 {
1279 struct net_bridge *br = netdev_priv(brdev);
1280 int err;
1281
1282 if (!data)
1283 return 0;
1284
1285 if (data[IFLA_BR_FORWARD_DELAY]) {
1286 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1287 if (err)
1288 return err;
1289 }
1290
1291 if (data[IFLA_BR_HELLO_TIME]) {
1292 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1293 if (err)
1294 return err;
1295 }
1296
1297 if (data[IFLA_BR_MAX_AGE]) {
1298 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1299 if (err)
1300 return err;
1301 }
1302
1303 if (data[IFLA_BR_AGEING_TIME]) {
1304 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1305 if (err)
1306 return err;
1307 }
1308
1309 if (data[IFLA_BR_STP_STATE]) {
1310 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1311
1312 err = br_stp_set_enabled(br, stp_enabled, extack);
1313 if (err)
1314 return err;
1315 }
1316
1317 if (data[IFLA_BR_PRIORITY]) {
1318 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1319
1320 br_stp_set_bridge_priority(br, priority);
1321 }
1322
1323 if (data[IFLA_BR_VLAN_FILTERING]) {
1324 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1325
1326 err = br_vlan_filter_toggle(br, vlan_filter, extack);
1327 if (err)
1328 return err;
1329 }
1330
1331 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1332 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1333 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1334
1335 err = __br_vlan_set_proto(br, vlan_proto, extack);
1336 if (err)
1337 return err;
1338 }
1339
1340 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1341 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1342
1343 err = __br_vlan_set_default_pvid(br, defpvid, extack);
1344 if (err)
1345 return err;
1346 }
1347
1348 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1349 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1350
1351 err = br_vlan_set_stats(br, vlan_stats);
1352 if (err)
1353 return err;
1354 }
1355
1356 if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1357 __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1358
1359 err = br_vlan_set_stats_per_port(br, per_port);
1360 if (err)
1361 return err;
1362 }
1363 #endif
1364
1365 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1366 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1367
1368 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1369 return -EINVAL;
1370 br->group_fwd_mask = fwd_mask;
1371 }
1372
1373 if (data[IFLA_BR_GROUP_ADDR]) {
1374 u8 new_addr[ETH_ALEN];
1375
1376 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1377 return -EINVAL;
1378 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1379 if (!is_link_local_ether_addr(new_addr))
1380 return -EINVAL;
1381 if (new_addr[5] == 1 || /* 802.3x Pause address */
1382 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1383 new_addr[5] == 3) /* 802.1X PAE address */
1384 return -EINVAL;
1385 spin_lock_bh(&br->lock);
1386 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1387 spin_unlock_bh(&br->lock);
1388 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
1389 br_recalculate_fwd_mask(br);
1390 }
1391
1392 if (data[IFLA_BR_FDB_FLUSH]) {
1393 struct net_bridge_fdb_flush_desc desc = {
1394 .flags_mask = BIT(BR_FDB_STATIC)
1395 };
1396
1397 br_fdb_flush(br, &desc);
1398 }
1399
1400 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1401 if (data[IFLA_BR_MCAST_ROUTER]) {
1402 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1403
1404 err = br_multicast_set_router(&br->multicast_ctx,
1405 multicast_router);
1406 if (err)
1407 return err;
1408 }
1409
1410 if (data[IFLA_BR_MCAST_SNOOPING]) {
1411 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1412
1413 err = br_multicast_toggle(br, mcast_snooping, extack);
1414 if (err)
1415 return err;
1416 }
1417
1418 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1419 u8 val;
1420
1421 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1422 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
1423 }
1424
1425 if (data[IFLA_BR_MCAST_QUERIER]) {
1426 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1427
1428 err = br_multicast_set_querier(&br->multicast_ctx,
1429 mcast_querier);
1430 if (err)
1431 return err;
1432 }
1433
1434 if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1435 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1436 RHT_ELASTICITY);
1437
1438 if (data[IFLA_BR_MCAST_HASH_MAX])
1439 br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1440
1441 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1442 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1443
1444 br->multicast_ctx.multicast_last_member_count = val;
1445 }
1446
1447 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1448 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1449
1450 br->multicast_ctx.multicast_startup_query_count = val;
1451 }
1452
1453 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1454 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1455
1456 br->multicast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val);
1457 }
1458
1459 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1460 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1461
1462 br->multicast_ctx.multicast_membership_interval = clock_t_to_jiffies(val);
1463 }
1464
1465 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1466 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1467
1468 br->multicast_ctx.multicast_querier_interval = clock_t_to_jiffies(val);
1469 }
1470
1471 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1472 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1473
1474 br_multicast_set_query_intvl(&br->multicast_ctx, val);
1475 }
1476
1477 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1478 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1479
1480 br->multicast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val);
1481 }
1482
1483 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1484 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1485
1486 br_multicast_set_startup_query_intvl(&br->multicast_ctx, val);
1487 }
1488
1489 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1490 __u8 mcast_stats;
1491
1492 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1493 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
1494 }
1495
1496 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1497 __u8 igmp_version;
1498
1499 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1500 err = br_multicast_set_igmp_version(&br->multicast_ctx,
1501 igmp_version);
1502 if (err)
1503 return err;
1504 }
1505
1506 #if IS_ENABLED(CONFIG_IPV6)
1507 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1508 __u8 mld_version;
1509
1510 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1511 err = br_multicast_set_mld_version(&br->multicast_ctx,
1512 mld_version);
1513 if (err)
1514 return err;
1515 }
1516 #endif
1517 #endif
1518 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1519 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1520 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1521
1522 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
1523 }
1524
1525 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1526 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1527
1528 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
1529 }
1530
1531 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1532 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1533
1534 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
1535 }
1536 #endif
1537
1538 if (data[IFLA_BR_MULTI_BOOLOPT]) {
1539 struct br_boolopt_multi *bm;
1540
1541 bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1542 err = br_boolopt_multi_toggle(br, bm, extack);
1543 if (err)
1544 return err;
1545 }
1546
1547 if (data[IFLA_BR_FDB_MAX_LEARNED]) {
1548 u32 val = nla_get_u32(data[IFLA_BR_FDB_MAX_LEARNED]);
1549
1550 WRITE_ONCE(br->fdb_max_learned, val);
1551 }
1552
1553 return 0;
1554 }
1555
br_dev_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1556 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1557 struct nlattr *tb[], struct nlattr *data[],
1558 struct netlink_ext_ack *extack)
1559 {
1560 struct net_bridge *br = netdev_priv(dev);
1561 int err;
1562
1563 err = register_netdevice(dev);
1564 if (err)
1565 return err;
1566
1567 if (tb[IFLA_ADDRESS]) {
1568 spin_lock_bh(&br->lock);
1569 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1570 spin_unlock_bh(&br->lock);
1571 }
1572
1573 err = br_changelink(dev, tb, data, extack);
1574 if (err)
1575 br_dev_delete(dev, NULL);
1576
1577 return err;
1578 }
1579
br_get_size(const struct net_device * brdev)1580 static size_t br_get_size(const struct net_device *brdev)
1581 {
1582 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1583 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1584 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
1585 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1586 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1587 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
1588 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
1589 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1590 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1591 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
1592 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
1593 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_PER_PORT */
1594 #endif
1595 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
1596 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
1597 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
1598 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
1599 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
1600 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1601 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1602 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1603 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1604 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1605 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1606 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
1607 nla_total_size(sizeof(u32)) + /* IFLA_BR_FDB_N_LEARNED */
1608 nla_total_size(sizeof(u32)) + /* IFLA_BR_FDB_MAX_LEARNED */
1609 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1610 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
1611 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
1612 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1613 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
1614 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
1615 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1616 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1617 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1618 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1619 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1620 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1621 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1622 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1623 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1624 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1625 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
1626 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
1627 br_multicast_querier_state_size() + /* IFLA_BR_MCAST_QUERIER_STATE */
1628 #endif
1629 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1630 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1631 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1632 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1633 #endif
1634 nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
1635 0;
1636 }
1637
br_fill_info(struct sk_buff * skb,const struct net_device * brdev)1638 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1639 {
1640 struct net_bridge *br = netdev_priv(brdev);
1641 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1642 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1643 u32 age_time = jiffies_to_clock_t(br->max_age);
1644 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1645 u32 stp_enabled = br->stp_enabled;
1646 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1647 u8 vlan_enabled = br_vlan_enabled(br->dev);
1648 struct br_boolopt_multi bm;
1649 u64 clockval;
1650
1651 clockval = br_timer_value(&br->hello_timer);
1652 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1653 return -EMSGSIZE;
1654 clockval = br_timer_value(&br->tcn_timer);
1655 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1656 return -EMSGSIZE;
1657 clockval = br_timer_value(&br->topology_change_timer);
1658 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1659 IFLA_BR_PAD))
1660 return -EMSGSIZE;
1661 clockval = br_timer_value(&br->gc_work.timer);
1662 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1663 return -EMSGSIZE;
1664
1665 br_boolopt_multi_get(br, &bm);
1666 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1667 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1668 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1669 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1670 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1671 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1672 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1673 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1674 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1675 &br->bridge_id) ||
1676 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1677 &br->designated_root) ||
1678 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1679 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1680 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1681 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1682 br->topology_change_detected) ||
1683 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1684 nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm) ||
1685 nla_put_u32(skb, IFLA_BR_FDB_N_LEARNED,
1686 atomic_read(&br->fdb_n_learned)) ||
1687 nla_put_u32(skb, IFLA_BR_FDB_MAX_LEARNED, br->fdb_max_learned))
1688 return -EMSGSIZE;
1689
1690 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1691 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1692 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1693 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
1694 br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1695 nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
1696 br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
1697 return -EMSGSIZE;
1698 #endif
1699 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1700 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER,
1701 br->multicast_ctx.multicast_router) ||
1702 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1703 br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
1704 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1705 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1706 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1707 br->multicast_ctx.multicast_querier) ||
1708 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1709 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
1710 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
1711 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1712 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1713 br->multicast_ctx.multicast_last_member_count) ||
1714 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1715 br->multicast_ctx.multicast_startup_query_count) ||
1716 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1717 br->multicast_ctx.multicast_igmp_version) ||
1718 br_multicast_dump_querier_state(skb, &br->multicast_ctx,
1719 IFLA_BR_MCAST_QUERIER_STATE))
1720 return -EMSGSIZE;
1721 #if IS_ENABLED(CONFIG_IPV6)
1722 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1723 br->multicast_ctx.multicast_mld_version))
1724 return -EMSGSIZE;
1725 #endif
1726 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_last_member_interval);
1727 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1728 IFLA_BR_PAD))
1729 return -EMSGSIZE;
1730 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_membership_interval);
1731 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1732 IFLA_BR_PAD))
1733 return -EMSGSIZE;
1734 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_querier_interval);
1735 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1736 IFLA_BR_PAD))
1737 return -EMSGSIZE;
1738 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_interval);
1739 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1740 IFLA_BR_PAD))
1741 return -EMSGSIZE;
1742 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_response_interval);
1743 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1744 IFLA_BR_PAD))
1745 return -EMSGSIZE;
1746 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_startup_query_interval);
1747 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1748 IFLA_BR_PAD))
1749 return -EMSGSIZE;
1750 #endif
1751 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1752 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1753 br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
1754 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1755 br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
1756 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1757 br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
1758 return -EMSGSIZE;
1759 #endif
1760
1761 return 0;
1762 }
1763
br_get_linkxstats_size(const struct net_device * dev,int attr)1764 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1765 {
1766 struct net_bridge_port *p = NULL;
1767 struct net_bridge_vlan_group *vg;
1768 struct net_bridge_vlan *v;
1769 struct net_bridge *br;
1770 int numvls = 0;
1771
1772 switch (attr) {
1773 case IFLA_STATS_LINK_XSTATS:
1774 br = netdev_priv(dev);
1775 vg = br_vlan_group(br);
1776 break;
1777 case IFLA_STATS_LINK_XSTATS_SLAVE:
1778 p = br_port_get_rtnl(dev);
1779 if (!p)
1780 return 0;
1781 vg = nbp_vlan_group(p);
1782 break;
1783 default:
1784 return 0;
1785 }
1786
1787 if (vg) {
1788 /* we need to count all, even placeholder entries */
1789 list_for_each_entry(v, &vg->vlan_list, vlist)
1790 numvls++;
1791 }
1792
1793 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1794 nla_total_size_64bit(sizeof(struct br_mcast_stats)) +
1795 (p ? nla_total_size_64bit(sizeof(p->stp_xstats)) : 0) +
1796 nla_total_size(0);
1797 }
1798
br_fill_linkxstats(struct sk_buff * skb,const struct net_device * dev,int * prividx,int attr)1799 static int br_fill_linkxstats(struct sk_buff *skb,
1800 const struct net_device *dev,
1801 int *prividx, int attr)
1802 {
1803 struct nlattr *nla __maybe_unused;
1804 struct net_bridge_port *p = NULL;
1805 struct net_bridge_vlan_group *vg;
1806 struct net_bridge_vlan *v;
1807 struct net_bridge *br;
1808 struct nlattr *nest;
1809 int vl_idx = 0;
1810
1811 switch (attr) {
1812 case IFLA_STATS_LINK_XSTATS:
1813 br = netdev_priv(dev);
1814 vg = br_vlan_group(br);
1815 break;
1816 case IFLA_STATS_LINK_XSTATS_SLAVE:
1817 p = br_port_get_rtnl(dev);
1818 if (!p)
1819 return 0;
1820 br = p->br;
1821 vg = nbp_vlan_group(p);
1822 break;
1823 default:
1824 return -EINVAL;
1825 }
1826
1827 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
1828 if (!nest)
1829 return -EMSGSIZE;
1830
1831 if (vg) {
1832 u16 pvid;
1833
1834 pvid = br_get_pvid(vg);
1835 list_for_each_entry(v, &vg->vlan_list, vlist) {
1836 struct bridge_vlan_xstats vxi;
1837 struct pcpu_sw_netstats stats;
1838
1839 if (++vl_idx < *prividx)
1840 continue;
1841 memset(&vxi, 0, sizeof(vxi));
1842 vxi.vid = v->vid;
1843 vxi.flags = v->flags;
1844 if (v->vid == pvid)
1845 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1846 br_vlan_get_stats(v, &stats);
1847 vxi.rx_bytes = u64_stats_read(&stats.rx_bytes);
1848 vxi.rx_packets = u64_stats_read(&stats.rx_packets);
1849 vxi.tx_bytes = u64_stats_read(&stats.tx_bytes);
1850 vxi.tx_packets = u64_stats_read(&stats.tx_packets);
1851
1852 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1853 goto nla_put_failure;
1854 }
1855 }
1856
1857 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1858 if (++vl_idx >= *prividx) {
1859 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1860 sizeof(struct br_mcast_stats),
1861 BRIDGE_XSTATS_PAD);
1862 if (!nla)
1863 goto nla_put_failure;
1864 br_multicast_get_stats(br, p, nla_data(nla));
1865 }
1866 #endif
1867
1868 if (p) {
1869 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1870 sizeof(p->stp_xstats),
1871 BRIDGE_XSTATS_PAD);
1872 if (!nla)
1873 goto nla_put_failure;
1874
1875 spin_lock_bh(&br->lock);
1876 memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1877 spin_unlock_bh(&br->lock);
1878 }
1879
1880 nla_nest_end(skb, nest);
1881 *prividx = 0;
1882
1883 return 0;
1884
1885 nla_put_failure:
1886 nla_nest_end(skb, nest);
1887 *prividx = vl_idx;
1888
1889 return -EMSGSIZE;
1890 }
1891
1892 static struct rtnl_af_ops br_af_ops __read_mostly = {
1893 .family = AF_BRIDGE,
1894 .get_link_af_size = br_get_link_af_size_filtered,
1895 };
1896
1897 struct rtnl_link_ops br_link_ops __read_mostly = {
1898 .kind = "bridge",
1899 .priv_size = sizeof(struct net_bridge),
1900 .setup = br_dev_setup,
1901 .maxtype = IFLA_BR_MAX,
1902 .policy = br_policy,
1903 .validate = br_validate,
1904 .newlink = br_dev_newlink,
1905 .changelink = br_changelink,
1906 .dellink = br_dev_delete,
1907 .get_size = br_get_size,
1908 .fill_info = br_fill_info,
1909 .fill_linkxstats = br_fill_linkxstats,
1910 .get_linkxstats_size = br_get_linkxstats_size,
1911
1912 .slave_maxtype = IFLA_BRPORT_MAX,
1913 .slave_policy = br_port_policy,
1914 .slave_changelink = br_port_slave_changelink,
1915 .get_slave_size = br_port_get_slave_size,
1916 .fill_slave_info = br_port_fill_slave_info,
1917 };
1918
br_netlink_init(void)1919 int __init br_netlink_init(void)
1920 {
1921 int err;
1922
1923 err = br_vlan_rtnl_init();
1924 if (err)
1925 goto out;
1926
1927 err = rtnl_af_register(&br_af_ops);
1928 if (err)
1929 goto out_vlan;
1930
1931 err = rtnl_link_register(&br_link_ops);
1932 if (err)
1933 goto out_af;
1934
1935 return 0;
1936
1937 out_af:
1938 rtnl_af_unregister(&br_af_ops);
1939 out_vlan:
1940 br_vlan_rtnl_uninit();
1941 out:
1942 return err;
1943 }
1944
br_netlink_fini(void)1945 void br_netlink_fini(void)
1946 {
1947 br_vlan_rtnl_uninit();
1948 rtnl_af_unregister(&br_af_ops);
1949 rtnl_link_unregister(&br_link_ops);
1950 }
1951