2 * lib/route/link/bridge.c AF_BRIDGE link support
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2010-2013 Thomas Graf <tgraf@suug.ch>
14 * @defgroup bridge Bridging
20 #include <netlink-private/netlink.h>
21 #include <netlink/netlink.h>
22 #include <netlink/attr.h>
23 #include <netlink/route/rtnl.h>
24 #include <netlink/route/link/bridge.h>
25 #include <netlink-private/route/link/api.h>
26 #include <linux/if_bridge.h>
29 #define BRIDGE_ATTR_PORT_STATE (1 << 0)
30 #define BRIDGE_ATTR_PRIORITY (1 << 1)
31 #define BRIDGE_ATTR_COST (1 << 2)
32 #define BRIDGE_ATTR_FLAGS (1 << 3)
34 #define PRIV_FLAG_NEW_ATTRS (1 << 0)
39 uint8_t b_priv_flags; /* internal flags */
43 uint32_t b_flags_mask;
44 uint32_t ce_mask; /* HACK to support attr macros */
47 static struct rtnl_link_af_ops bridge_ops;
49 #define IS_BRIDGE_LINK_ASSERT(link) \
50 if (!rtnl_link_is_bridge(link)) { \
51 APPBUG("A function was expecting a link object of type bridge."); \
52 return -NLE_OPNOTSUPP; \
55 static inline struct bridge_data *bridge_data(struct rtnl_link *link)
57 return rtnl_link_af_data(link, &bridge_ops);
60 static void *bridge_alloc(struct rtnl_link *link)
62 return calloc(1, sizeof(struct bridge_data));
65 static void *bridge_clone(struct rtnl_link *link, void *data)
67 struct bridge_data *bd;
69 if ((bd = bridge_alloc(link)))
70 memcpy(bd, data, sizeof(*bd));
75 static void bridge_free(struct rtnl_link *link, void *data)
80 static struct nla_policy br_attrs_policy[IFLA_BRPORT_MAX+1] = {
81 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
82 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
83 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
84 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
85 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
86 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
87 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
90 static void check_flag(struct rtnl_link *link, struct nlattr *attrs[],
93 if (attrs[type] && nla_get_u8(attrs[type]))
94 rtnl_link_bridge_set_flags(link, flag);
97 static int bridge_parse_protinfo(struct rtnl_link *link, struct nlattr *attr,
100 struct bridge_data *bd = data;
101 struct nlattr *br_attrs[IFLA_BRPORT_MAX+1];
104 /* Backwards compatibility */
105 if (!nla_is_nested(attr)) {
106 if (nla_len(attr) < 1)
109 bd->b_port_state = nla_get_u8(attr);
110 bd->ce_mask |= BRIDGE_ATTR_PORT_STATE;
115 if ((err = nla_parse_nested(br_attrs, IFLA_BRPORT_MAX, attr,
116 br_attrs_policy)) < 0)
119 bd->b_priv_flags |= PRIV_FLAG_NEW_ATTRS;
121 if (br_attrs[IFLA_BRPORT_STATE]) {
122 bd->b_port_state = nla_get_u8(br_attrs[IFLA_BRPORT_STATE]);
123 bd->ce_mask |= BRIDGE_ATTR_PORT_STATE;
126 if (br_attrs[IFLA_BRPORT_PRIORITY]) {
127 bd->b_priority = nla_get_u16(br_attrs[IFLA_BRPORT_PRIORITY]);
128 bd->ce_mask |= BRIDGE_ATTR_PRIORITY;
131 if (br_attrs[IFLA_BRPORT_COST]) {
132 bd->b_cost = nla_get_u32(br_attrs[IFLA_BRPORT_COST]);
133 bd->ce_mask |= BRIDGE_ATTR_COST;
136 check_flag(link, br_attrs, IFLA_BRPORT_MODE, RTNL_BRIDGE_HAIRPIN_MODE);
137 check_flag(link, br_attrs, IFLA_BRPORT_GUARD, RTNL_BRIDGE_BPDU_GUARD);
138 check_flag(link, br_attrs, IFLA_BRPORT_PROTECT, RTNL_BRIDGE_ROOT_BLOCK);
139 check_flag(link, br_attrs, IFLA_BRPORT_FAST_LEAVE, RTNL_BRIDGE_FAST_LEAVE);
144 static void bridge_dump_details(struct rtnl_link *link,
145 struct nl_dump_params *p, void *data)
147 struct bridge_data *bd = data;
149 nl_dump_line(p, " bridge: ");
151 if (bd->ce_mask & BRIDGE_ATTR_PORT_STATE)
152 nl_dump(p, "port-state %u ", bd->b_port_state);
154 if (bd->ce_mask & BRIDGE_ATTR_PRIORITY)
155 nl_dump(p, "prio %u ", bd->b_priority);
157 if (bd->ce_mask & BRIDGE_ATTR_COST)
158 nl_dump(p, "cost %u ", bd->b_cost);
163 static int bridge_compare(struct rtnl_link *_a, struct rtnl_link *_b,
164 int family, uint32_t attrs, int flags)
166 struct bridge_data *a = bridge_data(_a);
167 struct bridge_data *b = bridge_data(_b);
170 #define BRIDGE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, BRIDGE_ATTR_##ATTR, a, b, EXPR)
171 diff |= BRIDGE_DIFF(PORT_STATE, a->b_port_state != b->b_port_state);
172 diff |= BRIDGE_DIFF(PRIORITY, a->b_priority != b->b_priority);
173 diff |= BRIDGE_DIFF(COST, a->b_cost != b->b_cost);
175 if (flags & LOOSE_COMPARISON)
176 diff |= BRIDGE_DIFF(FLAGS,
177 (a->b_flags ^ b->b_flags) & b->b_flags_mask);
179 diff |= BRIDGE_DIFF(FLAGS, a->b_flags != b->b_flags);
187 * Allocate link object of type bridge
189 * @return Allocated link object or NULL.
191 struct rtnl_link *rtnl_link_bridge_alloc(void)
193 struct rtnl_link *link;
196 if (!(link = rtnl_link_alloc()))
199 if ((err = rtnl_link_set_type(link, "bridge")) < 0) {
208 * Check if a link is a bridge
209 * @arg link Link object
211 * @return 1 if the link is a bridge, 0 otherwise.
213 int rtnl_link_is_bridge(struct rtnl_link *link)
215 return link->l_family == AF_BRIDGE &&
216 link->l_af_ops == &bridge_ops;
220 * Check if bridge has extended information
221 * @arg link Link object of type bridge
223 * Checks if the bridge object has been constructed based on
224 * information that is only available in newer kernels. This
225 * affectes the following functions:
226 * - rtnl_link_bridge_get_cost()
227 * - rtnl_link_bridge_get_priority()
228 * - rtnl_link_bridge_get_flags()
230 * @return 1 if extended information is available, otherwise 0 is returned.
232 int rtnl_link_bridge_has_ext_info(struct rtnl_link *link)
234 struct bridge_data *bd;
236 if (!rtnl_link_is_bridge(link))
239 bd = bridge_data(link);
240 return !!(bd->b_priv_flags & PRIV_FLAG_NEW_ATTRS);
244 * Set Spanning Tree Protocol (STP) port state
245 * @arg link Link object of type bridge
246 * @arg state New STP port state
248 * The value of state must be one of the following:
249 * - BR_STATE_DISABLED
250 * - BR_STATE_LISTENING
251 * - BR_STATE_LEARNING
252 * - BR_STATE_FORWARDING
253 * - BR_STATE_BLOCKING
255 * @see rtnl_link_bridge_get_port_state()
257 * @return 0 on success or a negative error code.
258 * @retval -NLE_OPNOTSUPP Link is not a bridge
259 * @retval -NLE_INVAL Invalid state value (0..BR_STATE_BLOCKING)
261 int rtnl_link_bridge_set_port_state(struct rtnl_link *link, uint8_t state)
263 struct bridge_data *bd = bridge_data(link);
265 IS_BRIDGE_LINK_ASSERT(link);
267 if (state > BR_STATE_BLOCKING)
270 bd->b_port_state = state;
271 bd->ce_mask |= BRIDGE_ATTR_PORT_STATE;
277 * Get Spanning Tree Protocol (STP) port state
278 * @arg link Link object of type bridge
280 * @see rtnl_link_bridge_set_port_state()
282 * @return The STP port state or a negative error code.
283 * @retval -NLE_OPNOTSUPP Link is not a bridge
285 int rtnl_link_bridge_get_port_state(struct rtnl_link *link)
287 struct bridge_data *bd = bridge_data(link);
289 IS_BRIDGE_LINK_ASSERT(link);
291 return bd->b_port_state;
296 * @arg link Link object of type bridge
297 * @arg prio Bridge priority
299 * @see rtnl_link_bridge_get_priority()
301 * @return 0 on success or a negative error code.
302 * @retval -NLE_OPNOTSUPP Link is not a bridge
304 int rtnl_link_bridge_set_priority(struct rtnl_link *link, uint16_t prio)
306 struct bridge_data *bd = bridge_data(link);
308 IS_BRIDGE_LINK_ASSERT(link);
310 bd->b_priority = prio;
311 bd->ce_mask |= BRIDGE_ATTR_PRIORITY;
318 * @arg link Link object of type bridge
320 * @see rtnl_link_bridge_set_priority()
322 * @return 0 on success or a negative error code.
323 * @retval -NLE_OPNOTSUPP Link is not a bridge
325 int rtnl_link_bridge_get_priority(struct rtnl_link *link)
327 struct bridge_data *bd = bridge_data(link);
329 IS_BRIDGE_LINK_ASSERT(link);
331 return bd->b_priority;
335 * Set Spanning Tree Protocol (STP) path cost
336 * @arg link Link object of type bridge
337 * @arg cost New STP path cost value
339 * @see rtnl_link_bridge_get_cost()
341 * @return The bridge priority or a negative error code.
342 * @retval -NLE_OPNOTSUPP Link is not a bridge
344 int rtnl_link_bridge_set_cost(struct rtnl_link *link, uint32_t cost)
346 struct bridge_data *bd = bridge_data(link);
348 IS_BRIDGE_LINK_ASSERT(link);
351 bd->ce_mask |= BRIDGE_ATTR_COST;
357 * Get Spanning Tree Protocol (STP) path cost
358 * @arg link Link object of type bridge
359 * @arg cost Pointer to store STP cost value
361 * @see rtnl_link_bridge_set_cost()
363 * @return 0 on success or a negative error code.
364 * @retval -NLE_OPNOTSUPP Link is not a bridge
365 * @retval -NLE_INVAL `cost` is not a valid pointer
367 int rtnl_link_bridge_get_cost(struct rtnl_link *link, uint32_t *cost)
369 struct bridge_data *bd = bridge_data(link);
371 IS_BRIDGE_LINK_ASSERT(link);
383 * @arg link Link object of type bridge
384 * @arg flags Bridging flags to unset
386 * @see rtnl_link_bridge_set_flags()
387 * @see rtnl_link_bridge_get_flags()
389 * @return 0 on success or a negative error code.
390 * @retval -NLE_OPNOTSUPP Link is not a bridge
392 int rtnl_link_bridge_unset_flags(struct rtnl_link *link, unsigned int flags)
394 struct bridge_data *bd = bridge_data(link);
396 IS_BRIDGE_LINK_ASSERT(link);
398 bd->b_flags_mask |= flags;
399 bd->b_flags &= ~flags;
400 bd->ce_mask |= BRIDGE_ATTR_FLAGS;
407 * @arg link Link object of type bridge
408 * @arg flags Bridging flags to set
411 * - RTNL_BRIDGE_HAIRPIN_MODE
412 * - RTNL_BRIDGE_BPDU_GUARD
413 * - RTNL_BRIDGE_ROOT_BLOCK
414 * - RTNL_BRIDGE_FAST_LEAVE
416 * @see rtnl_link_bridge_unset_flags()
417 * @see rtnl_link_bridge_get_flags()
419 * @return 0 on success or a negative error code.
420 * @retval -NLE_OPNOTSUPP Link is not a bridge
422 int rtnl_link_bridge_set_flags(struct rtnl_link *link, unsigned int flags)
424 struct bridge_data *bd = bridge_data(link);
426 IS_BRIDGE_LINK_ASSERT(link);
428 bd->b_flags_mask |= flags;
429 bd->b_flags |= flags;
430 bd->ce_mask |= BRIDGE_ATTR_FLAGS;
437 * @arg link Link object of type bridge
439 * @see rtnl_link_bridge_set_flags()
440 * @see rtnl_link_bridge_unset_flags()
442 * @return Flags or a negative error code.
443 * @retval -NLE_OPNOTSUPP Link is not a bridge
445 int rtnl_link_bridge_get_flags(struct rtnl_link *link)
447 struct bridge_data *bd = bridge_data(link);
449 IS_BRIDGE_LINK_ASSERT(link);
454 static struct rtnl_link_af_ops bridge_ops = {
455 .ao_family = AF_BRIDGE,
456 .ao_alloc = &bridge_alloc,
457 .ao_clone = &bridge_clone,
458 .ao_free = &bridge_free,
459 .ao_parse_protinfo = &bridge_parse_protinfo,
460 .ao_dump[NL_DUMP_DETAILS] = &bridge_dump_details,
461 .ao_compare = &bridge_compare,
464 static void __init bridge_init(void)
466 rtnl_link_af_register(&bridge_ops);
469 static void __exit bridge_exit(void)
471 rtnl_link_af_unregister(&bridge_ops);