1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
/*
* lib/route/link/bridge.c AF_BRIDGE link support
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2010-2013 Thomas Graf <tgraf@suug.ch>
*/
/**
* @ingroup link
* @defgroup bridge Bridging
*
* @details
* @{
*/
#include <netlink-private/netlink.h>
#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/link/bridge.h>
#include <netlink-private/route/link/api.h>
#include <linux/if_bridge.h>
/** @cond SKIP */
#define BRIDGE_ATTR_PORT_STATE (1 << 0)
#define BRIDGE_ATTR_PRIORITY (1 << 1)
#define BRIDGE_ATTR_COST (1 << 2)
#define BRIDGE_ATTR_FLAGS (1 << 3)
#define PRIV_FLAG_NEW_ATTRS (1 << 0)
struct bridge_data
{
uint8_t b_port_state;
uint8_t b_priv_flags; /* internal flags */
uint16_t b_priority;
uint32_t b_cost;
uint32_t b_flags;
uint32_t b_flags_mask;
uint32_t ce_mask; /* HACK to support attr macros */
};
static struct rtnl_link_af_ops bridge_ops;
#define IS_BRIDGE_LINK_ASSERT(link) \
if (!rtnl_link_is_bridge(link)) { \
APPBUG("A function was expecting a link object of type bridge."); \
return -NLE_OPNOTSUPP; \
}
static inline struct bridge_data *bridge_data(struct rtnl_link *link)
{
return rtnl_link_af_data(link, &bridge_ops);
}
static void *bridge_alloc(struct rtnl_link *link)
{
return calloc(1, sizeof(struct bridge_data));
}
static void *bridge_clone(struct rtnl_link *link, void *data)
{
struct bridge_data *bd;
if ((bd = bridge_alloc(link)))
memcpy(bd, data, sizeof(*bd));
return bd;
}
static void bridge_free(struct rtnl_link *link, void *data)
{
free(data);
}
static struct nla_policy br_attrs_policy[IFLA_BRPORT_MAX+1] = {
[IFLA_BRPORT_STATE] = { .type = NLA_U8 },
[IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
[IFLA_BRPORT_COST] = { .type = NLA_U32 },
[IFLA_BRPORT_MODE] = { .type = NLA_U8 },
[IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
[IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
[IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
};
static void check_flag(struct rtnl_link *link, struct nlattr *attrs[],
int type, int flag)
{
if (attrs[type] && nla_get_u8(attrs[type]))
rtnl_link_bridge_set_flags(link, flag);
}
static int bridge_parse_protinfo(struct rtnl_link *link, struct nlattr *attr,
void *data)
{
struct bridge_data *bd = data;
struct nlattr *br_attrs[IFLA_BRPORT_MAX+1];
int err;
/* Backwards compatibility */
if (!nla_is_nested(attr)) {
if (nla_len(attr) < 1)
return -NLE_RANGE;
bd->b_port_state = nla_get_u8(attr);
bd->ce_mask |= BRIDGE_ATTR_PORT_STATE;
return 0;
}
if ((err = nla_parse_nested(br_attrs, IFLA_BRPORT_MAX, attr,
br_attrs_policy)) < 0)
return err;
bd->b_priv_flags |= PRIV_FLAG_NEW_ATTRS;
if (br_attrs[IFLA_BRPORT_STATE]) {
bd->b_port_state = nla_get_u8(br_attrs[IFLA_BRPORT_STATE]);
bd->ce_mask |= BRIDGE_ATTR_PORT_STATE;
}
if (br_attrs[IFLA_BRPORT_PRIORITY]) {
bd->b_priority = nla_get_u16(br_attrs[IFLA_BRPORT_PRIORITY]);
bd->ce_mask |= BRIDGE_ATTR_PRIORITY;
}
if (br_attrs[IFLA_BRPORT_COST]) {
bd->b_cost = nla_get_u32(br_attrs[IFLA_BRPORT_COST]);
bd->ce_mask |= BRIDGE_ATTR_COST;
}
check_flag(link, br_attrs, IFLA_BRPORT_MODE, RTNL_BRIDGE_HAIRPIN_MODE);
check_flag(link, br_attrs, IFLA_BRPORT_GUARD, RTNL_BRIDGE_BPDU_GUARD);
check_flag(link, br_attrs, IFLA_BRPORT_PROTECT, RTNL_BRIDGE_ROOT_BLOCK);
check_flag(link, br_attrs, IFLA_BRPORT_FAST_LEAVE, RTNL_BRIDGE_FAST_LEAVE);
return 0;
}
static void bridge_dump_details(struct rtnl_link *link,
struct nl_dump_params *p, void *data)
{
struct bridge_data *bd = data;
nl_dump_line(p, " bridge: ");
if (bd->ce_mask & BRIDGE_ATTR_PORT_STATE)
nl_dump(p, "port-state %u ", bd->b_port_state);
if (bd->ce_mask & BRIDGE_ATTR_PRIORITY)
nl_dump(p, "prio %u ", bd->b_priority);
if (bd->ce_mask & BRIDGE_ATTR_COST)
nl_dump(p, "cost %u ", bd->b_cost);
nl_dump(p, "\n");
}
static int bridge_compare(struct rtnl_link *_a, struct rtnl_link *_b,
int family, uint32_t attrs, int flags)
{
struct bridge_data *a = bridge_data(_a);
struct bridge_data *b = bridge_data(_b);
int diff = 0;
#define BRIDGE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, BRIDGE_ATTR_##ATTR, a, b, EXPR)
diff |= BRIDGE_DIFF(PORT_STATE, a->b_port_state != b->b_port_state);
diff |= BRIDGE_DIFF(PRIORITY, a->b_priority != b->b_priority);
diff |= BRIDGE_DIFF(COST, a->b_cost != b->b_cost);
if (flags & LOOSE_COMPARISON)
diff |= BRIDGE_DIFF(FLAGS,
(a->b_flags ^ b->b_flags) & b->b_flags_mask);
else
diff |= BRIDGE_DIFF(FLAGS, a->b_flags != b->b_flags);
#undef BRIDGE_DIFF
return diff;
}
/** @endcond */
/**
* Allocate link object of type bridge
*
* @return Allocated link object or NULL.
*/
struct rtnl_link *rtnl_link_bridge_alloc(void)
{
struct rtnl_link *link;
int err;
if (!(link = rtnl_link_alloc()))
return NULL;
if ((err = rtnl_link_set_type(link, "bridge")) < 0) {
rtnl_link_put(link);
return NULL;
}
return link;
}
/**
* Create a new kernel bridge device
* @arg sk netlink socket
* @arg name name of the bridge device or NULL
*
* Creates a new bridge device in the kernel. If no name is
* provided, the kernel will automatically pick a name of the
* form "type%d" (e.g. bridge0, vlan1, etc.)
*
* @return 0 on success or a negative error code
*/
int rtnl_link_bridge_add(struct nl_sock *sk, const char *name)
{
int err;
struct rtnl_link *link;
if (!(link = rtnl_link_bridge_alloc()))
return -NLE_NOMEM;
if(name)
rtnl_link_set_name(link, name);
err = rtnl_link_add(sk, link, NLM_F_CREATE);
rtnl_link_put(link);
return err;
}
/**
* Check if a link is a bridge
* @arg link Link object
*
* @return 1 if the link is a bridge, 0 otherwise.
*/
int rtnl_link_is_bridge(struct rtnl_link *link)
{
return link->l_family == AF_BRIDGE &&
link->l_af_ops == &bridge_ops;
}
/**
* Check if bridge has extended information
* @arg link Link object of type bridge
*
* Checks if the bridge object has been constructed based on
* information that is only available in newer kernels. This
* affectes the following functions:
* - rtnl_link_bridge_get_cost()
* - rtnl_link_bridge_get_priority()
* - rtnl_link_bridge_get_flags()
*
* @return 1 if extended information is available, otherwise 0 is returned.
*/
int rtnl_link_bridge_has_ext_info(struct rtnl_link *link)
{
struct bridge_data *bd;
if (!rtnl_link_is_bridge(link))
return 0;
bd = bridge_data(link);
return !!(bd->b_priv_flags & PRIV_FLAG_NEW_ATTRS);
}
/**
* Set Spanning Tree Protocol (STP) port state
* @arg link Link object of type bridge
* @arg state New STP port state
*
* The value of state must be one of the following:
* - BR_STATE_DISABLED
* - BR_STATE_LISTENING
* - BR_STATE_LEARNING
* - BR_STATE_FORWARDING
* - BR_STATE_BLOCKING
*
* @see rtnl_link_bridge_get_port_state()
*
* @return 0 on success or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
* @retval -NLE_INVAL Invalid state value (0..BR_STATE_BLOCKING)
*/
int rtnl_link_bridge_set_port_state(struct rtnl_link *link, uint8_t state)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
if (state > BR_STATE_BLOCKING)
return -NLE_INVAL;
bd->b_port_state = state;
bd->ce_mask |= BRIDGE_ATTR_PORT_STATE;
return 0;
}
/**
* Get Spanning Tree Protocol (STP) port state
* @arg link Link object of type bridge
*
* @see rtnl_link_bridge_set_port_state()
*
* @return The STP port state or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
*/
int rtnl_link_bridge_get_port_state(struct rtnl_link *link)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
return bd->b_port_state;
}
/**
* Set priority
* @arg link Link object of type bridge
* @arg prio Bridge priority
*
* @see rtnl_link_bridge_get_priority()
*
* @return 0 on success or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
*/
int rtnl_link_bridge_set_priority(struct rtnl_link *link, uint16_t prio)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
bd->b_priority = prio;
bd->ce_mask |= BRIDGE_ATTR_PRIORITY;
return 0;
}
/**
* Get priority
* @arg link Link object of type bridge
*
* @see rtnl_link_bridge_set_priority()
*
* @return 0 on success or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
*/
int rtnl_link_bridge_get_priority(struct rtnl_link *link)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
return bd->b_priority;
}
/**
* Set Spanning Tree Protocol (STP) path cost
* @arg link Link object of type bridge
* @arg cost New STP path cost value
*
* @see rtnl_link_bridge_get_cost()
*
* @return The bridge priority or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
*/
int rtnl_link_bridge_set_cost(struct rtnl_link *link, uint32_t cost)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
bd->b_cost = cost;
bd->ce_mask |= BRIDGE_ATTR_COST;
return 0;
}
/**
* Get Spanning Tree Protocol (STP) path cost
* @arg link Link object of type bridge
* @arg cost Pointer to store STP cost value
*
* @see rtnl_link_bridge_set_cost()
*
* @return 0 on success or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
* @retval -NLE_INVAL `cost` is not a valid pointer
*/
int rtnl_link_bridge_get_cost(struct rtnl_link *link, uint32_t *cost)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
if (!cost)
return -NLE_INVAL;
*cost = bd->b_cost;
return 0;
}
/**
* Unset flags
* @arg link Link object of type bridge
* @arg flags Bridging flags to unset
*
* @see rtnl_link_bridge_set_flags()
* @see rtnl_link_bridge_get_flags()
*
* @return 0 on success or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
*/
int rtnl_link_bridge_unset_flags(struct rtnl_link *link, unsigned int flags)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
bd->b_flags_mask |= flags;
bd->b_flags &= ~flags;
bd->ce_mask |= BRIDGE_ATTR_FLAGS;
return 0;
}
/**
* Set flags
* @arg link Link object of type bridge
* @arg flags Bridging flags to set
*
* Valid flags are:
* - RTNL_BRIDGE_HAIRPIN_MODE
* - RTNL_BRIDGE_BPDU_GUARD
* - RTNL_BRIDGE_ROOT_BLOCK
* - RTNL_BRIDGE_FAST_LEAVE
*
* @see rtnl_link_bridge_unset_flags()
* @see rtnl_link_bridge_get_flags()
*
* @return 0 on success or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
*/
int rtnl_link_bridge_set_flags(struct rtnl_link *link, unsigned int flags)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
bd->b_flags_mask |= flags;
bd->b_flags |= flags;
bd->ce_mask |= BRIDGE_ATTR_FLAGS;
return 0;
}
/**
* Get flags
* @arg link Link object of type bridge
*
* @see rtnl_link_bridge_set_flags()
* @see rtnl_link_bridge_unset_flags()
*
* @return Flags or a negative error code.
* @retval -NLE_OPNOTSUPP Link is not a bridge
*/
int rtnl_link_bridge_get_flags(struct rtnl_link *link)
{
struct bridge_data *bd = bridge_data(link);
IS_BRIDGE_LINK_ASSERT(link);
return bd->b_flags;
}
static const struct trans_tbl bridge_flags[] = {
__ADD(RTNL_BRIDGE_HAIRPIN_MODE, hairpin_mode),
__ADD(RTNL_BRIDGE_BPDU_GUARD, bpdu_guard),
__ADD(RTNL_BRIDGE_ROOT_BLOCK, root_block),
__ADD(RTNL_BRIDGE_FAST_LEAVE, fast_leave),
};
/**
* @name Flag Translation
* @{
*/
char *rtnl_link_bridge_flags2str(int flags, char *buf, size_t len)
{
return __flags2str(flags, buf, len, bridge_flags, ARRAY_SIZE(bridge_flags));
}
int rtnl_link_bridge_str2flags(const char *name)
{
return __str2flags(name, bridge_flags, ARRAY_SIZE(bridge_flags));
}
/** @} */
static struct rtnl_link_af_ops bridge_ops = {
.ao_family = AF_BRIDGE,
.ao_alloc = &bridge_alloc,
.ao_clone = &bridge_clone,
.ao_free = &bridge_free,
.ao_parse_protinfo = &bridge_parse_protinfo,
.ao_dump[NL_DUMP_DETAILS] = &bridge_dump_details,
.ao_compare = &bridge_compare,
};
static void __init bridge_init(void)
{
rtnl_link_af_register(&bridge_ops);
}
static void __exit bridge_exit(void)
{
rtnl_link_af_unregister(&bridge_ops);
}
/** @} */
|