summaryrefslogtreecommitdiffstats
path: root/ip/iplink_bridge_slave.c
blob: 3821923b5da5cd8028ceed89f50ee72d0ab428e0 (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * iplink_bridge_slave.c	Bridge slave device support
 *
 * Authors:     Jiri Pirko <jiri@resnulli.us>
 */

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if_link.h>
#include <linux/if_bridge.h>

#include "rt_names.h"
#include "utils.h"
#include "ip_common.h"

static void print_explain(FILE *f)
{
	fprintf(f,
		"Usage: ... bridge_slave [ fdb_flush ]\n"
		"			[ state STATE ]\n"
		"			[ priority PRIO ]\n"
		"			[ cost COST ]\n"
		"			[ guard {on | off} ]\n"
		"			[ hairpin {on | off} ]\n"
		"			[ fastleave {on | off} ]\n"
		"			[ root_block {on | off} ]\n"
		"			[ learning {on | off} ]\n"
		"			[ flood {on | off} ]\n"
		"			[ proxy_arp {on | off} ]\n"
		"			[ proxy_arp_wifi {on | off} ]\n"
		"			[ mcast_router MULTICAST_ROUTER ]\n"
		"			[ mcast_fast_leave {on | off} ]\n"
		"			[ mcast_flood {on | off} ]\n"
		"			[ bcast_flood {on | off} ]\n"
		"			[ mcast_to_unicast {on | off} ]\n"
		"			[ group_fwd_mask MASK ]\n"
		"			[ neigh_suppress {on | off} ]\n"
		"			[ neigh_vlan_suppress {on | off} ]\n"
		"			[ vlan_tunnel {on | off} ]\n"
		"			[ isolated {on | off} ]\n"
		"			[ locked {on | off} ]\n"
		"			[ mab {on | off} ]\n"
		"			[ backup_port DEVICE ] [ nobackup_port ]\n"
		"			[ backup_nhid NHID ]\n"
	);
}

static void explain(void)
{
	print_explain(stderr);
}

static const char *port_states[] = {
	[BR_STATE_DISABLED] = "disabled",
	[BR_STATE_LISTENING] = "listening",
	[BR_STATE_LEARNING] = "learning",
	[BR_STATE_FORWARDING] = "forwarding",
	[BR_STATE_BLOCKING] = "blocking",
};

static const char *fwd_mask_tbl[16] = {
	[0]	= "stp",
	[2]	= "lacp",
	[14]	= "lldp"
};

static void print_portstate(FILE *f, __u8 state)
{
	if (state <= BR_STATE_BLOCKING)
		print_string(PRINT_ANY,
			     "state",
			     "state %s ",
			     port_states[state]);
	else
		print_int(PRINT_ANY, "state_index", "state (%d) ", state);
}

static void _print_timer(FILE *f, const char *attr, struct rtattr *timer)
{
	struct timeval tv;

	__jiffies_to_tv(&tv, rta_getattr_u64(timer));
	if (is_json_context()) {
		json_writer_t *jw = get_json_writer();

		jsonw_name(jw, attr);
		jsonw_printf(jw, "%i.%.2i",
			     (int)tv.tv_sec, (int)tv.tv_usec / 10000);
	} else {
		fprintf(f, "%s %4i.%.2i ", attr, (int)tv.tv_sec,
			(int)tv.tv_usec / 10000);
	}
}

static void _bitmask2str(__u16 bitmask, char *dst, size_t dst_size,
			 const char **tbl)
{
	int len, i;

	for (i = 0, len = 0; bitmask; i++, bitmask >>= 1) {
		int n;

		if (bitmask & 0x1) {
			if (tbl[i])
				n = snprintf(dst + len, dst_size - len, "%s,",
						tbl[i]);
			else
				n = snprintf(dst + len, dst_size - len, "0x%x,",
						(1 << i));

			if (n < 0 || n >= dst_size - len)
				break;

			len += n;
		}
	}

	if (!len)
		snprintf(dst, dst_size, "0x0");
	else
		dst[len - 1] = 0;
}

static void bridge_slave_print_opt(struct link_util *lu, FILE *f,
				   struct rtattr *tb[])
{
	if (!tb)
		return;

	if (tb[IFLA_BRPORT_STATE])
		print_portstate(f, rta_getattr_u8(tb[IFLA_BRPORT_STATE]));

	if (tb[IFLA_BRPORT_PRIORITY])
		print_int(PRINT_ANY,
			  "priority",
			  "priority %d ",
			  rta_getattr_u16(tb[IFLA_BRPORT_PRIORITY]));

	if (tb[IFLA_BRPORT_COST])
		print_int(PRINT_ANY,
			  "cost",
			  "cost %d ",
			  rta_getattr_u32(tb[IFLA_BRPORT_COST]));

	if (tb[IFLA_BRPORT_MODE])
		print_on_off(PRINT_ANY, "hairpin", "hairpin %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_MODE]));

	if (tb[IFLA_BRPORT_GUARD])
		print_on_off(PRINT_ANY, "guard", "guard %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_GUARD]));

	if (tb[IFLA_BRPORT_PROTECT])
		print_on_off(PRINT_ANY, "root_block", "root_block %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_PROTECT]));

	if (tb[IFLA_BRPORT_FAST_LEAVE])
		print_on_off(PRINT_ANY, "fastleave", "fastleave %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]));

	if (tb[IFLA_BRPORT_LEARNING])
		print_on_off(PRINT_ANY, "learning", "learning %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_LEARNING]));

	if (tb[IFLA_BRPORT_UNICAST_FLOOD])
		print_on_off(PRINT_ANY, "flood", "flood %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_UNICAST_FLOOD]));

	if (tb[IFLA_BRPORT_ID])
		print_0xhex(PRINT_ANY, "id", "port_id %#llx ",
			    rta_getattr_u16(tb[IFLA_BRPORT_ID]));

	if (tb[IFLA_BRPORT_NO])
		print_0xhex(PRINT_ANY, "no", "port_no %#llx ",
			   rta_getattr_u16(tb[IFLA_BRPORT_NO]));

	if (tb[IFLA_BRPORT_DESIGNATED_PORT])
		print_uint(PRINT_ANY,
			   "designated_port",
			   "designated_port %u ",
			   rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_PORT]));

	if (tb[IFLA_BRPORT_DESIGNATED_COST])
		print_uint(PRINT_ANY,
			   "designated_cost",
			   "designated_cost %u ",
			   rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_COST]));

	if (tb[IFLA_BRPORT_BRIDGE_ID]) {
		char bridge_id[32];

		br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_BRIDGE_ID]),
				  bridge_id, sizeof(bridge_id));
		print_string(PRINT_ANY,
			     "bridge_id",
			     "designated_bridge %s ",
			     bridge_id);
	}

	if (tb[IFLA_BRPORT_ROOT_ID]) {
		char root_id[32];

		br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_ROOT_ID]),
				  root_id, sizeof(root_id));
		print_string(PRINT_ANY,
			     "root_id",
			     "designated_root %s ", root_id);
	}

	if (tb[IFLA_BRPORT_HOLD_TIMER])
		_print_timer(f, "hold_timer", tb[IFLA_BRPORT_HOLD_TIMER]);

	if (tb[IFLA_BRPORT_MESSAGE_AGE_TIMER])
		_print_timer(f, "message_age_timer",
			     tb[IFLA_BRPORT_MESSAGE_AGE_TIMER]);

	if (tb[IFLA_BRPORT_FORWARD_DELAY_TIMER])
		_print_timer(f, "forward_delay_timer",
			     tb[IFLA_BRPORT_FORWARD_DELAY_TIMER]);

	if (tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK])
		print_uint(PRINT_ANY,
			   "topology_change_ack",
			   "topology_change_ack %u ",
			   rta_getattr_u8(tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK]));

	if (tb[IFLA_BRPORT_CONFIG_PENDING])
		print_uint(PRINT_ANY,
			   "config_pending",
			   "config_pending %u ",
			   rta_getattr_u8(tb[IFLA_BRPORT_CONFIG_PENDING]));

	if (tb[IFLA_BRPORT_PROXYARP])
		print_on_off(PRINT_ANY, "proxy_arp", "proxy_arp %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP]));

	if (tb[IFLA_BRPORT_PROXYARP_WIFI])
		print_on_off(PRINT_ANY, "proxy_arp_wifi", "proxy_arp_wifi %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP_WIFI]));

	if (tb[IFLA_BRPORT_MULTICAST_ROUTER])
		print_uint(PRINT_ANY,
			   "multicast_router",
			   "mcast_router %u ",
			   rta_getattr_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]));

	if (tb[IFLA_BRPORT_FAST_LEAVE])
		// not printing any json here because
		// we already printed fast_leave before
		print_string(PRINT_FP,
			     NULL,
			     "mcast_fast_leave %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]) ? "on" : "off");

	if (tb[IFLA_BRPORT_MCAST_FLOOD])
		print_on_off(PRINT_ANY, "mcast_flood", "mcast_flood %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_MCAST_FLOOD]));

	if (tb[IFLA_BRPORT_BCAST_FLOOD])
		print_on_off(PRINT_ANY, "bcast_flood", "bcast_flood %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_BCAST_FLOOD]));

	if (tb[IFLA_BRPORT_MCAST_TO_UCAST])
		print_on_off(PRINT_ANY, "mcast_to_unicast", "mcast_to_unicast %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_MCAST_TO_UCAST]));

	if (tb[IFLA_BRPORT_NEIGH_SUPPRESS])
		print_on_off(PRINT_ANY, "neigh_suppress", "neigh_suppress %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_NEIGH_SUPPRESS]));

	if (tb[IFLA_BRPORT_NEIGH_VLAN_SUPPRESS])
		print_on_off(PRINT_ANY, "neigh_vlan_suppress",
			     "neigh_vlan_suppress %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_NEIGH_VLAN_SUPPRESS]));

	if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
		char convbuf[256];
		__u16 fwd_mask;

		fwd_mask = rta_getattr_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
		print_0xhex(PRINT_ANY, "group_fwd_mask",
			    "group_fwd_mask %#llx ", fwd_mask);
		_bitmask2str(fwd_mask, convbuf, sizeof(convbuf), fwd_mask_tbl);
		print_string(PRINT_ANY, "group_fwd_mask_str",
			     "group_fwd_mask_str %s ", convbuf);
	}

	if (tb[IFLA_BRPORT_VLAN_TUNNEL])
		print_on_off(PRINT_ANY, "vlan_tunnel", "vlan_tunnel %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_VLAN_TUNNEL]));

	if (tb[IFLA_BRPORT_ISOLATED])
		print_on_off(PRINT_ANY, "isolated", "isolated %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_ISOLATED]));

	if (tb[IFLA_BRPORT_LOCKED])
		print_on_off(PRINT_ANY, "locked", "locked %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_LOCKED]));

	if (tb[IFLA_BRPORT_MAB])
		print_on_off(PRINT_ANY, "mab", "mab %s ",
			     rta_getattr_u8(tb[IFLA_BRPORT_MAB]));

	if (tb[IFLA_BRPORT_BACKUP_PORT]) {
		int backup_p = rta_getattr_u32(tb[IFLA_BRPORT_BACKUP_PORT]);

		print_string(PRINT_ANY, "backup_port", "backup_port %s ",
			     ll_index_to_name(backup_p));
	}

	if (tb[IFLA_BRPORT_BACKUP_NHID])
		print_uint(PRINT_ANY, "backup_nhid", "backup_nhid %u ",
			   rta_getattr_u32(tb[IFLA_BRPORT_BACKUP_NHID]));
}

static void bridge_slave_parse_on_off(char *arg_name, char *arg_val,
				      struct nlmsghdr *n, int type)
{
	int ret;
	__u8 val = parse_on_off(arg_name, arg_val, &ret);

	if (ret)
		exit(1);
	addattr8(n, 1024, type, val);
}

static int bridge_slave_parse_opt(struct link_util *lu, int argc, char **argv,
				  struct nlmsghdr *n)
{
	__u8 state;
	__u16 priority;
	__u32 cost;

	while (argc > 0) {
		if (matches(*argv, "fdb_flush") == 0) {
			addattr(n, 1024, IFLA_BRPORT_FLUSH);
		} else if (matches(*argv, "state") == 0) {
			NEXT_ARG();
			if (get_u8(&state, *argv, 0))
				invarg("state is invalid", *argv);
			addattr8(n, 1024, IFLA_BRPORT_STATE, state);
		} else if (matches(*argv, "priority") == 0) {
			NEXT_ARG();
			if (get_u16(&priority, *argv, 0))
				invarg("priority is invalid", *argv);
			addattr16(n, 1024, IFLA_BRPORT_PRIORITY, priority);
		} else if (matches(*argv, "cost") == 0) {
			NEXT_ARG();
			if (get_u32(&cost, *argv, 0))
				invarg("cost is invalid", *argv);
			addattr32(n, 1024, IFLA_BRPORT_COST, cost);
		} else if (matches(*argv, "hairpin") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("hairpin", *argv, n,
						  IFLA_BRPORT_MODE);
		} else if (matches(*argv, "guard") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("guard", *argv, n,
						  IFLA_BRPORT_GUARD);
		} else if (matches(*argv, "root_block") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("root_block", *argv, n,
						  IFLA_BRPORT_PROTECT);
		} else if (matches(*argv, "fastleave") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("fastleave", *argv, n,
						  IFLA_BRPORT_FAST_LEAVE);
		} else if (matches(*argv, "learning") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("learning", *argv, n,
						  IFLA_BRPORT_LEARNING);
		} else if (matches(*argv, "flood") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("flood", *argv, n,
						  IFLA_BRPORT_UNICAST_FLOOD);
		} else if (matches(*argv, "mcast_flood") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("mcast_flood", *argv, n,
						  IFLA_BRPORT_MCAST_FLOOD);
		} else if (matches(*argv, "bcast_flood") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("bcast_flood", *argv, n,
						  IFLA_BRPORT_BCAST_FLOOD);
		} else if (matches(*argv, "mcast_to_unicast") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("mcast_to_unicast", *argv, n,
						  IFLA_BRPORT_MCAST_TO_UCAST);
		} else if (matches(*argv, "proxy_arp") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("proxy_arp", *argv, n,
						  IFLA_BRPORT_PROXYARP);
		} else if (matches(*argv, "proxy_arp_wifi") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("proxy_arp_wifi", *argv, n,
						  IFLA_BRPORT_PROXYARP_WIFI);
		} else if (matches(*argv, "mcast_router") == 0) {
			__u8 mcast_router;

			NEXT_ARG();
			if (get_u8(&mcast_router, *argv, 0))
				invarg("invalid mcast_router", *argv);
			addattr8(n, 1024, IFLA_BRPORT_MULTICAST_ROUTER,
				 mcast_router);
		} else if (matches(*argv, "mcast_fast_leave") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("mcast_fast_leave", *argv, n,
						  IFLA_BRPORT_FAST_LEAVE);
		} else if (matches(*argv, "neigh_suppress") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("neigh_suppress", *argv, n,
						  IFLA_BRPORT_NEIGH_SUPPRESS);
		} else if (strcmp(*argv, "neigh_vlan_suppress") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("neigh_vlan_suppress", *argv,
						  n, IFLA_BRPORT_NEIGH_VLAN_SUPPRESS);
		} else if (matches(*argv, "group_fwd_mask") == 0) {
			__u16 mask;

			NEXT_ARG();
			if (get_u16(&mask, *argv, 0))
				invarg("invalid group_fwd_mask", *argv);
			addattr16(n, 1024, IFLA_BRPORT_GROUP_FWD_MASK, mask);
		} else if (matches(*argv, "vlan_tunnel") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("vlan_tunnel", *argv, n,
						  IFLA_BRPORT_VLAN_TUNNEL);
		} else if (matches(*argv, "isolated") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("isolated", *argv, n,
						  IFLA_BRPORT_ISOLATED);
		} else if (matches(*argv, "locked") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("locked", *argv, n,
						  IFLA_BRPORT_LOCKED);
		} else if (strcmp(*argv, "mab") == 0) {
			NEXT_ARG();
			bridge_slave_parse_on_off("mab", *argv, n,
						  IFLA_BRPORT_MAB);
		} else if (matches(*argv, "backup_port") == 0) {
			int ifindex;

			NEXT_ARG();
			ifindex = ll_name_to_index(*argv);
			if (!ifindex)
				invarg("Device does not exist\n", *argv);
			addattr32(n, 1024, IFLA_BRPORT_BACKUP_PORT, ifindex);
		} else if (matches(*argv, "nobackup_port") == 0) {
			addattr32(n, 1024, IFLA_BRPORT_BACKUP_PORT, 0);
		} else if (strcmp(*argv, "backup_nhid") == 0) {
			__u32 backup_nhid;

			NEXT_ARG();
			if (get_u32(&backup_nhid, *argv, 0))
				invarg("backup_nhid is invalid", *argv);
			addattr32(n, 1024, IFLA_BRPORT_BACKUP_NHID,
				  backup_nhid);
		} else if (matches(*argv, "help") == 0) {
			explain();
			return -1;
		} else {
			fprintf(stderr, "bridge_slave: unknown option \"%s\"?\n",
				*argv);
			explain();
			return -1;
		}
		argc--, argv++;
	}

	return 0;
}

static void bridge_slave_print_help(struct link_util *lu, int argc, char **argv,
		FILE *f)
{
	print_explain(f);
}

struct link_util bridge_slave_link_util = {
	.id		= "bridge_slave",
	.maxattr	= IFLA_BRPORT_MAX,
	.print_opt	= bridge_slave_print_opt,
	.parse_opt	= bridge_slave_parse_opt,
	.print_help     = bridge_slave_print_help,
	.parse_ifla_xstats = bridge_parse_xstats,
	.print_ifla_xstats = bridge_print_xstats,
};