2 * Bridge netlink control interface
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>
20 #include "br_private.h"
21 #include "br_private_stp.h"
23 static inline size_t br_port_info_size(void)
25 return nla_total_size(1) /* IFLA_BRPORT_STATE */
26 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
27 + nla_total_size(4) /* IFLA_BRPORT_COST */
28 + nla_total_size(1) /* IFLA_BRPORT_MODE */
29 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
30 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
34 static inline size_t br_nlmsg_size(void)
36 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
37 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
38 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
39 + nla_total_size(4) /* IFLA_MASTER */
40 + nla_total_size(4) /* IFLA_MTU */
41 + nla_total_size(4) /* IFLA_LINK */
42 + nla_total_size(1) /* IFLA_OPERSTATE */
43 + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */
46 static int br_port_fill_attrs(struct sk_buff
*skb
,
47 const struct net_bridge_port
*p
)
49 u8 mode
= !!(p
->flags
& BR_HAIRPIN_MODE
);
51 if (nla_put_u8(skb
, IFLA_BRPORT_STATE
, p
->state
) ||
52 nla_put_u16(skb
, IFLA_BRPORT_PRIORITY
, p
->priority
) ||
53 nla_put_u32(skb
, IFLA_BRPORT_COST
, p
->path_cost
) ||
54 nla_put_u8(skb
, IFLA_BRPORT_MODE
, mode
) ||
55 nla_put_u8(skb
, IFLA_BRPORT_GUARD
, !!(p
->flags
& BR_BPDU_GUARD
)) ||
56 nla_put_u8(skb
, IFLA_BRPORT_PROTECT
, !!(p
->flags
& BR_ROOT_BLOCK
)) ||
57 nla_put_u8(skb
, IFLA_BRPORT_FAST_LEAVE
, !!(p
->flags
& BR_MULTICAST_FAST_LEAVE
)))
64 * Create one netlink message for one interface
65 * Contains port and master info as well as carrier and bridge state.
67 static int br_fill_ifinfo(struct sk_buff
*skb
, const struct net_bridge_port
*port
,
68 u32 pid
, u32 seq
, int event
, unsigned int flags
)
70 const struct net_bridge
*br
= port
->br
;
71 const struct net_device
*dev
= port
->dev
;
72 struct ifinfomsg
*hdr
;
74 u8 operstate
= netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
;
76 br_debug(br
, "br_fill_info event %d port %s master %s\n",
77 event
, dev
->name
, br
->dev
->name
);
79 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*hdr
), flags
);
83 hdr
= nlmsg_data(nlh
);
84 hdr
->ifi_family
= AF_BRIDGE
;
86 hdr
->ifi_type
= dev
->type
;
87 hdr
->ifi_index
= dev
->ifindex
;
88 hdr
->ifi_flags
= dev_get_flags(dev
);
91 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
92 nla_put_u32(skb
, IFLA_MASTER
, br
->dev
->ifindex
) ||
93 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
94 nla_put_u8(skb
, IFLA_OPERSTATE
, operstate
) ||
96 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
97 (dev
->ifindex
!= dev
->iflink
&&
98 nla_put_u32(skb
, IFLA_LINK
, dev
->iflink
)))
101 if (event
== RTM_NEWLINK
) {
103 = nla_nest_start(skb
, IFLA_PROTINFO
| NLA_F_NESTED
);
105 if (nest
== NULL
|| br_port_fill_attrs(skb
, port
) < 0)
106 goto nla_put_failure
;
107 nla_nest_end(skb
, nest
);
110 return nlmsg_end(skb
, nlh
);
113 nlmsg_cancel(skb
, nlh
);
118 * Notify listeners of a change in port information
120 void br_ifinfo_notify(int event
, struct net_bridge_port
*port
)
122 struct net
*net
= dev_net(port
->dev
);
126 br_debug(port
->br
, "port %u(%s) event %d\n",
127 (unsigned int)port
->port_no
, port
->dev
->name
, event
);
129 skb
= nlmsg_new(br_nlmsg_size(), GFP_ATOMIC
);
133 err
= br_fill_ifinfo(skb
, port
, 0, 0, event
, 0);
135 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
136 WARN_ON(err
== -EMSGSIZE
);
140 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
144 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
148 * Dump information about all ports, in response to GETLINK
150 int br_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
151 struct net_device
*dev
)
154 struct net_bridge_port
*port
= br_port_get_rcu(dev
);
156 /* not a bridge port */
160 err
= br_fill_ifinfo(skb
, port
, pid
, seq
, RTM_NEWLINK
, NLM_F_MULTI
);
165 static const struct nla_policy ifla_brport_policy
[IFLA_BRPORT_MAX
+ 1] = {
166 [IFLA_BRPORT_STATE
] = { .type
= NLA_U8
},
167 [IFLA_BRPORT_COST
] = { .type
= NLA_U32
},
168 [IFLA_BRPORT_PRIORITY
] = { .type
= NLA_U16
},
169 [IFLA_BRPORT_MODE
] = { .type
= NLA_U8
},
170 [IFLA_BRPORT_GUARD
] = { .type
= NLA_U8
},
171 [IFLA_BRPORT_PROTECT
] = { .type
= NLA_U8
},
174 /* Change the state of the port and notify spanning tree */
175 static int br_set_port_state(struct net_bridge_port
*p
, u8 state
)
177 if (state
> BR_STATE_BLOCKING
)
180 /* if kernel STP is running, don't allow changes */
181 if (p
->br
->stp_enabled
== BR_KERNEL_STP
)
184 if (!netif_running(p
->dev
) ||
185 (!netif_carrier_ok(p
->dev
) && state
!= BR_STATE_DISABLED
))
190 br_port_state_selection(p
->br
);
194 /* Set/clear or port flags based on attribute */
195 static void br_set_port_flag(struct net_bridge_port
*p
, struct nlattr
*tb
[],
196 int attrtype
, unsigned long mask
)
199 u8 flag
= nla_get_u8(tb
[attrtype
]);
207 /* Process bridge protocol info on port */
208 static int br_setport(struct net_bridge_port
*p
, struct nlattr
*tb
[])
212 br_set_port_flag(p
, tb
, IFLA_BRPORT_MODE
, BR_HAIRPIN_MODE
);
213 br_set_port_flag(p
, tb
, IFLA_BRPORT_GUARD
, BR_BPDU_GUARD
);
214 br_set_port_flag(p
, tb
, IFLA_BRPORT_FAST_LEAVE
, BR_MULTICAST_FAST_LEAVE
);
216 if (tb
[IFLA_BRPORT_COST
]) {
217 err
= br_stp_set_path_cost(p
, nla_get_u32(tb
[IFLA_BRPORT_COST
]));
222 if (tb
[IFLA_BRPORT_PRIORITY
]) {
223 err
= br_stp_set_port_priority(p
, nla_get_u16(tb
[IFLA_BRPORT_PRIORITY
]));
228 if (tb
[IFLA_BRPORT_STATE
]) {
229 err
= br_set_port_state(p
, nla_get_u8(tb
[IFLA_BRPORT_STATE
]));
236 /* Change state and parameters on port. */
237 int br_setlink(struct net_device
*dev
, struct nlmsghdr
*nlh
)
239 struct ifinfomsg
*ifm
;
240 struct nlattr
*protinfo
;
241 struct net_bridge_port
*p
;
242 struct nlattr
*tb
[IFLA_BRPORT_MAX
+ 1];
245 ifm
= nlmsg_data(nlh
);
247 protinfo
= nlmsg_find_attr(nlh
, sizeof(*ifm
), IFLA_PROTINFO
);
251 p
= br_port_get_rtnl(dev
);
255 if (protinfo
->nla_type
& NLA_F_NESTED
) {
256 err
= nla_parse_nested(tb
, IFLA_BRPORT_MAX
,
257 protinfo
, ifla_brport_policy
);
261 spin_lock_bh(&p
->br
->lock
);
262 err
= br_setport(p
, tb
);
263 spin_unlock_bh(&p
->br
->lock
);
265 /* Binary compatability with old RSTP */
266 if (nla_len(protinfo
) < sizeof(u8
))
269 spin_lock_bh(&p
->br
->lock
);
270 err
= br_set_port_state(p
, nla_get_u8(protinfo
));
271 spin_unlock_bh(&p
->br
->lock
);
275 br_ifinfo_notify(RTM_NEWLINK
, p
);
280 static int br_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
282 if (tb
[IFLA_ADDRESS
]) {
283 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
285 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
286 return -EADDRNOTAVAIL
;
292 struct rtnl_link_ops br_link_ops __read_mostly
= {
294 .priv_size
= sizeof(struct net_bridge
),
295 .setup
= br_dev_setup
,
296 .validate
= br_validate
,
297 .dellink
= br_dev_delete
,
300 int __init
br_netlink_init(void)
305 err
= rtnl_link_register(&br_link_ops
);
315 void __exit
br_netlink_fini(void)
318 rtnl_link_unregister(&br_link_ops
);