ARM: rockchip: fix broken build
[linux/fpc-iii.git] / net / bridge / br_netlink.c
blob4d74a0639c4ccd040b6371665e6a74f8dca6f800
1 /*
2 * Bridge netlink control interface
4 * Authors:
5 * Stephen Hemminger <shemminger@osdl.org>
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.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/etherdevice.h>
16 #include <net/rtnetlink.h>
17 #include <net/net_namespace.h>
18 #include <net/sock.h>
19 #include <net/switchdev.h>
20 #include <uapi/linux/if_bridge.h>
22 #include "br_private.h"
23 #include "br_private_stp.h"
25 static int br_get_num_vlan_infos(const struct net_port_vlans *pv,
26 u32 filter_mask)
28 u16 vid_range_start = 0, vid_range_end = 0;
29 u16 vid_range_flags = 0;
30 u16 pvid, vid, flags;
31 int num_vlans = 0;
33 if (filter_mask & RTEXT_FILTER_BRVLAN)
34 return pv->num_vlans;
36 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
37 return 0;
39 /* Count number of vlan info's
41 pvid = br_get_pvid(pv);
42 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
43 flags = 0;
44 if (vid == pvid)
45 flags |= BRIDGE_VLAN_INFO_PVID;
47 if (test_bit(vid, pv->untagged_bitmap))
48 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
50 if (vid_range_start == 0) {
51 goto initvars;
52 } else if ((vid - vid_range_end) == 1 &&
53 flags == vid_range_flags) {
54 vid_range_end = vid;
55 continue;
56 } else {
57 if ((vid_range_end - vid_range_start) > 0)
58 num_vlans += 2;
59 else
60 num_vlans += 1;
62 initvars:
63 vid_range_start = vid;
64 vid_range_end = vid;
65 vid_range_flags = flags;
68 if (vid_range_start != 0) {
69 if ((vid_range_end - vid_range_start) > 0)
70 num_vlans += 2;
71 else
72 num_vlans += 1;
75 return num_vlans;
78 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
79 u32 filter_mask)
81 struct net_port_vlans *pv;
82 int num_vlan_infos;
84 rcu_read_lock();
85 if (br_port_exists(dev))
86 pv = nbp_get_vlan_info(br_port_get_rcu(dev));
87 else if (dev->priv_flags & IFF_EBRIDGE)
88 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
89 else
90 pv = NULL;
91 if (pv)
92 num_vlan_infos = br_get_num_vlan_infos(pv, filter_mask);
93 else
94 num_vlan_infos = 0;
95 rcu_read_unlock();
97 if (!num_vlan_infos)
98 return 0;
100 /* Each VLAN is returned in bridge_vlan_info along with flags */
101 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
104 static inline size_t br_port_info_size(void)
106 return nla_total_size(1) /* IFLA_BRPORT_STATE */
107 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
108 + nla_total_size(4) /* IFLA_BRPORT_COST */
109 + nla_total_size(1) /* IFLA_BRPORT_MODE */
110 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
111 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
112 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
113 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
114 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
115 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
116 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
117 + 0;
120 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
122 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
123 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
124 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
125 + nla_total_size(4) /* IFLA_MASTER */
126 + nla_total_size(4) /* IFLA_MTU */
127 + nla_total_size(4) /* IFLA_LINK */
128 + nla_total_size(1) /* IFLA_OPERSTATE */
129 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
130 + nla_total_size(br_get_link_af_size_filtered(dev,
131 filter_mask)); /* IFLA_AF_SPEC */
134 static int br_port_fill_attrs(struct sk_buff *skb,
135 const struct net_bridge_port *p)
137 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
139 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
140 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
141 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
142 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
143 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
144 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
145 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
146 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
147 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
148 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
149 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
150 !!(p->flags & BR_PROXYARP_WIFI)))
151 return -EMSGSIZE;
153 return 0;
156 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
157 u16 vid_end, u16 flags)
159 struct bridge_vlan_info vinfo;
161 if ((vid_end - vid_start) > 0) {
162 /* add range to skb */
163 vinfo.vid = vid_start;
164 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
165 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
166 sizeof(vinfo), &vinfo))
167 goto nla_put_failure;
169 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
171 vinfo.vid = vid_end;
172 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
173 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
174 sizeof(vinfo), &vinfo))
175 goto nla_put_failure;
176 } else {
177 vinfo.vid = vid_start;
178 vinfo.flags = flags;
179 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
180 sizeof(vinfo), &vinfo))
181 goto nla_put_failure;
184 return 0;
186 nla_put_failure:
187 return -EMSGSIZE;
190 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
191 const struct net_port_vlans *pv)
193 u16 vid_range_start = 0, vid_range_end = 0;
194 u16 vid_range_flags = 0;
195 u16 pvid, vid, flags;
196 int err = 0;
198 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
199 * and mark vlan info with begin and end flags
200 * if vlaninfo represents a range
202 pvid = br_get_pvid(pv);
203 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
204 flags = 0;
205 if (vid == pvid)
206 flags |= BRIDGE_VLAN_INFO_PVID;
208 if (test_bit(vid, pv->untagged_bitmap))
209 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
211 if (vid_range_start == 0) {
212 goto initvars;
213 } else if ((vid - vid_range_end) == 1 &&
214 flags == vid_range_flags) {
215 vid_range_end = vid;
216 continue;
217 } else {
218 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
219 vid_range_end,
220 vid_range_flags);
221 if (err)
222 return err;
225 initvars:
226 vid_range_start = vid;
227 vid_range_end = vid;
228 vid_range_flags = flags;
231 if (vid_range_start != 0) {
232 /* Call it once more to send any left over vlans */
233 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
234 vid_range_end,
235 vid_range_flags);
236 if (err)
237 return err;
240 return 0;
243 static int br_fill_ifvlaninfo(struct sk_buff *skb,
244 const struct net_port_vlans *pv)
246 struct bridge_vlan_info vinfo;
247 u16 pvid, vid;
249 pvid = br_get_pvid(pv);
250 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
251 vinfo.vid = vid;
252 vinfo.flags = 0;
253 if (vid == pvid)
254 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
256 if (test_bit(vid, pv->untagged_bitmap))
257 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
259 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
260 sizeof(vinfo), &vinfo))
261 goto nla_put_failure;
264 return 0;
266 nla_put_failure:
267 return -EMSGSIZE;
271 * Create one netlink message for one interface
272 * Contains port and master info as well as carrier and bridge state.
274 static int br_fill_ifinfo(struct sk_buff *skb,
275 const struct net_bridge_port *port,
276 u32 pid, u32 seq, int event, unsigned int flags,
277 u32 filter_mask, const struct net_device *dev)
279 const struct net_bridge *br;
280 struct ifinfomsg *hdr;
281 struct nlmsghdr *nlh;
282 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
284 if (port)
285 br = port->br;
286 else
287 br = netdev_priv(dev);
289 br_debug(br, "br_fill_info event %d port %s master %s\n",
290 event, dev->name, br->dev->name);
292 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
293 if (nlh == NULL)
294 return -EMSGSIZE;
296 hdr = nlmsg_data(nlh);
297 hdr->ifi_family = AF_BRIDGE;
298 hdr->__ifi_pad = 0;
299 hdr->ifi_type = dev->type;
300 hdr->ifi_index = dev->ifindex;
301 hdr->ifi_flags = dev_get_flags(dev);
302 hdr->ifi_change = 0;
304 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
305 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
306 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
307 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
308 (dev->addr_len &&
309 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
310 (dev->ifindex != dev_get_iflink(dev) &&
311 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
312 goto nla_put_failure;
314 if (event == RTM_NEWLINK && port) {
315 struct nlattr *nest
316 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
318 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
319 goto nla_put_failure;
320 nla_nest_end(skb, nest);
323 /* Check if the VID information is requested */
324 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
325 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
326 const struct net_port_vlans *pv;
327 struct nlattr *af;
328 int err;
330 if (port)
331 pv = nbp_get_vlan_info(port);
332 else
333 pv = br_get_vlan_info(br);
335 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID))
336 goto done;
338 af = nla_nest_start(skb, IFLA_AF_SPEC);
339 if (!af)
340 goto nla_put_failure;
342 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
343 err = br_fill_ifvlaninfo_compressed(skb, pv);
344 else
345 err = br_fill_ifvlaninfo(skb, pv);
346 if (err)
347 goto nla_put_failure;
348 nla_nest_end(skb, af);
351 done:
352 nlmsg_end(skb, nlh);
353 return 0;
355 nla_put_failure:
356 nlmsg_cancel(skb, nlh);
357 return -EMSGSIZE;
361 * Notify listeners of a change in port information
363 void br_ifinfo_notify(int event, struct net_bridge_port *port)
365 struct net *net;
366 struct sk_buff *skb;
367 int err = -ENOBUFS;
368 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
370 if (!port)
371 return;
373 net = dev_net(port->dev);
374 br_debug(port->br, "port %u(%s) event %d\n",
375 (unsigned int)port->port_no, port->dev->name, event);
377 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
378 if (skb == NULL)
379 goto errout;
381 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
382 if (err < 0) {
383 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
384 WARN_ON(err == -EMSGSIZE);
385 kfree_skb(skb);
386 goto errout;
388 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
389 return;
390 errout:
391 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
396 * Dump information about all ports, in response to GETLINK
398 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
399 struct net_device *dev, u32 filter_mask, int nlflags)
401 struct net_bridge_port *port = br_port_get_rtnl(dev);
403 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
404 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
405 return 0;
407 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
408 filter_mask, dev);
411 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
412 int cmd, struct bridge_vlan_info *vinfo)
414 int err = 0;
416 switch (cmd) {
417 case RTM_SETLINK:
418 if (p) {
419 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
420 if (err)
421 break;
423 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
424 err = br_vlan_add(p->br, vinfo->vid,
425 vinfo->flags);
426 } else {
427 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
429 break;
431 case RTM_DELLINK:
432 if (p) {
433 nbp_vlan_delete(p, vinfo->vid);
434 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
435 br_vlan_delete(p->br, vinfo->vid);
436 } else {
437 br_vlan_delete(br, vinfo->vid);
439 break;
442 return err;
445 static int br_afspec(struct net_bridge *br,
446 struct net_bridge_port *p,
447 struct nlattr *af_spec,
448 int cmd)
450 struct bridge_vlan_info *vinfo_start = NULL;
451 struct bridge_vlan_info *vinfo = NULL;
452 struct nlattr *attr;
453 int err = 0;
454 int rem;
456 nla_for_each_nested(attr, af_spec, rem) {
457 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
458 continue;
459 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
460 return -EINVAL;
461 vinfo = nla_data(attr);
462 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
463 return -EINVAL;
464 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
465 if (vinfo_start)
466 return -EINVAL;
467 vinfo_start = vinfo;
468 continue;
471 if (vinfo_start) {
472 struct bridge_vlan_info tmp_vinfo;
473 int v;
475 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
476 return -EINVAL;
478 if (vinfo->vid <= vinfo_start->vid)
479 return -EINVAL;
481 memcpy(&tmp_vinfo, vinfo_start,
482 sizeof(struct bridge_vlan_info));
484 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
485 tmp_vinfo.vid = v;
486 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
487 if (err)
488 break;
490 vinfo_start = NULL;
491 } else {
492 err = br_vlan_info(br, p, cmd, vinfo);
494 if (err)
495 break;
498 return err;
501 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
502 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
503 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
504 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
505 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
506 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
507 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
508 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
509 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
510 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
511 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
512 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
515 /* Change the state of the port and notify spanning tree */
516 static int br_set_port_state(struct net_bridge_port *p, u8 state)
518 if (state > BR_STATE_BLOCKING)
519 return -EINVAL;
521 /* if kernel STP is running, don't allow changes */
522 if (p->br->stp_enabled == BR_KERNEL_STP)
523 return -EBUSY;
525 /* if device is not up, change is not allowed
526 * if link is not present, only allowable state is disabled
528 if (!netif_running(p->dev) ||
529 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
530 return -ENETDOWN;
532 br_set_state(p, state);
533 br_log_state(p);
534 br_port_state_selection(p->br);
535 return 0;
538 /* Set/clear or port flags based on attribute */
539 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
540 int attrtype, unsigned long mask)
542 if (tb[attrtype]) {
543 u8 flag = nla_get_u8(tb[attrtype]);
544 if (flag)
545 p->flags |= mask;
546 else
547 p->flags &= ~mask;
551 /* Process bridge protocol info on port */
552 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
554 int err;
555 unsigned long old_flags = p->flags;
557 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
558 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
559 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
560 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
561 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
562 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
563 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
564 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
566 if (tb[IFLA_BRPORT_COST]) {
567 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
568 if (err)
569 return err;
572 if (tb[IFLA_BRPORT_PRIORITY]) {
573 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
574 if (err)
575 return err;
578 if (tb[IFLA_BRPORT_STATE]) {
579 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
580 if (err)
581 return err;
584 br_port_flags_change(p, old_flags ^ p->flags);
585 return 0;
588 /* Change state and parameters on port. */
589 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
591 struct nlattr *protinfo;
592 struct nlattr *afspec;
593 struct net_bridge_port *p;
594 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
595 int err = 0;
597 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
598 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
599 if (!protinfo && !afspec)
600 return 0;
602 p = br_port_get_rtnl(dev);
603 /* We want to accept dev as bridge itself if the AF_SPEC
604 * is set to see if someone is setting vlan info on the bridge
606 if (!p && !afspec)
607 return -EINVAL;
609 if (p && protinfo) {
610 if (protinfo->nla_type & NLA_F_NESTED) {
611 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
612 protinfo, br_port_policy);
613 if (err)
614 return err;
616 spin_lock_bh(&p->br->lock);
617 err = br_setport(p, tb);
618 spin_unlock_bh(&p->br->lock);
619 } else {
620 /* Binary compatibility with old RSTP */
621 if (nla_len(protinfo) < sizeof(u8))
622 return -EINVAL;
624 spin_lock_bh(&p->br->lock);
625 err = br_set_port_state(p, nla_get_u8(protinfo));
626 spin_unlock_bh(&p->br->lock);
628 if (err)
629 goto out;
632 if (afspec) {
633 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
634 afspec, RTM_SETLINK);
637 if (err == 0)
638 br_ifinfo_notify(RTM_NEWLINK, p);
639 out:
640 return err;
643 /* Delete port information */
644 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
646 struct nlattr *afspec;
647 struct net_bridge_port *p;
648 int err = 0;
650 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
651 if (!afspec)
652 return 0;
654 p = br_port_get_rtnl(dev);
655 /* We want to accept dev as bridge itself as well */
656 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
657 return -EINVAL;
659 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
660 afspec, RTM_DELLINK);
661 if (err == 0)
662 /* Send RTM_NEWLINK because userspace
663 * expects RTM_NEWLINK for vlan dels
665 br_ifinfo_notify(RTM_NEWLINK, p);
667 return err;
669 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
671 if (tb[IFLA_ADDRESS]) {
672 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
673 return -EINVAL;
674 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
675 return -EADDRNOTAVAIL;
678 return 0;
681 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
682 struct nlattr *tb[], struct nlattr *data[])
684 struct net_bridge *br = netdev_priv(dev);
686 if (tb[IFLA_ADDRESS]) {
687 spin_lock_bh(&br->lock);
688 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
689 spin_unlock_bh(&br->lock);
692 return register_netdevice(dev);
695 static int br_port_slave_changelink(struct net_device *brdev,
696 struct net_device *dev,
697 struct nlattr *tb[],
698 struct nlattr *data[])
700 struct net_bridge *br = netdev_priv(brdev);
701 int ret;
703 if (!data)
704 return 0;
706 spin_lock_bh(&br->lock);
707 ret = br_setport(br_port_get_rtnl(dev), data);
708 spin_unlock_bh(&br->lock);
710 return ret;
713 static int br_port_fill_slave_info(struct sk_buff *skb,
714 const struct net_device *brdev,
715 const struct net_device *dev)
717 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
720 static size_t br_port_get_slave_size(const struct net_device *brdev,
721 const struct net_device *dev)
723 return br_port_info_size();
726 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
727 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
728 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
729 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
730 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
731 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
732 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
735 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
736 struct nlattr *data[])
738 struct net_bridge *br = netdev_priv(brdev);
739 int err;
741 if (!data)
742 return 0;
744 if (data[IFLA_BR_FORWARD_DELAY]) {
745 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
746 if (err)
747 return err;
750 if (data[IFLA_BR_HELLO_TIME]) {
751 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
752 if (err)
753 return err;
756 if (data[IFLA_BR_MAX_AGE]) {
757 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
758 if (err)
759 return err;
762 if (data[IFLA_BR_AGEING_TIME]) {
763 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
765 br->ageing_time = clock_t_to_jiffies(ageing_time);
768 if (data[IFLA_BR_STP_STATE]) {
769 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
771 br_stp_set_enabled(br, stp_enabled);
774 if (data[IFLA_BR_PRIORITY]) {
775 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
777 br_stp_set_bridge_priority(br, priority);
780 return 0;
783 static size_t br_get_size(const struct net_device *brdev)
785 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
786 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
787 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
788 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
789 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
790 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
794 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
796 struct net_bridge *br = netdev_priv(brdev);
797 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
798 u32 hello_time = jiffies_to_clock_t(br->hello_time);
799 u32 age_time = jiffies_to_clock_t(br->max_age);
800 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
801 u32 stp_enabled = br->stp_enabled;
802 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
804 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
805 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
806 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
807 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
808 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
809 nla_put_u16(skb, IFLA_BR_PRIORITY, priority))
810 return -EMSGSIZE;
812 return 0;
815 static size_t br_get_link_af_size(const struct net_device *dev)
817 struct net_port_vlans *pv;
819 if (br_port_exists(dev))
820 pv = nbp_get_vlan_info(br_port_get_rtnl(dev));
821 else if (dev->priv_flags & IFF_EBRIDGE)
822 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
823 else
824 return 0;
826 if (!pv)
827 return 0;
829 /* Each VLAN is returned in bridge_vlan_info along with flags */
830 return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
833 static struct rtnl_af_ops br_af_ops __read_mostly = {
834 .family = AF_BRIDGE,
835 .get_link_af_size = br_get_link_af_size,
838 struct rtnl_link_ops br_link_ops __read_mostly = {
839 .kind = "bridge",
840 .priv_size = sizeof(struct net_bridge),
841 .setup = br_dev_setup,
842 .maxtype = IFLA_BRPORT_MAX,
843 .policy = br_policy,
844 .validate = br_validate,
845 .newlink = br_dev_newlink,
846 .changelink = br_changelink,
847 .dellink = br_dev_delete,
848 .get_size = br_get_size,
849 .fill_info = br_fill_info,
851 .slave_maxtype = IFLA_BRPORT_MAX,
852 .slave_policy = br_port_policy,
853 .slave_changelink = br_port_slave_changelink,
854 .get_slave_size = br_port_get_slave_size,
855 .fill_slave_info = br_port_fill_slave_info,
858 int __init br_netlink_init(void)
860 int err;
862 br_mdb_init();
863 rtnl_af_register(&br_af_ops);
865 err = rtnl_link_register(&br_link_ops);
866 if (err)
867 goto out_af;
869 return 0;
871 out_af:
872 rtnl_af_unregister(&br_af_ops);
873 br_mdb_uninit();
874 return err;
877 void br_netlink_fini(void)
879 br_mdb_uninit();
880 rtnl_af_unregister(&br_af_ops);
881 rtnl_link_unregister(&br_link_ops);