summaryrefslogtreecommitdiffstats
path: root/ip/iplink_macvlan.c
blob: 05e6bc77c8c31ad558df8e71980e418d5c64b171 (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
/*
 * iplink_macvlan.c	macvlan/macvtap device support
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 *
 * Authors:     Patrick McHardy <kaber@trash.net>
 *		Arnd Bergmann <arnd@arndb.de>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if_link.h>
#include <linux/if_ether.h>

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

#define pfx_err(lu, ...) {               \
	fprintf(stderr, "%s: ", lu->id); \
	fprintf(stderr, __VA_ARGS__);    \
	fprintf(stderr, "\n");           \
}

static void print_explain(struct link_util *lu, FILE *f)
{
	fprintf(f,
		"Usage: ... %s mode MODE [flag MODE_FLAG] MODE_OPTS [bcqueuelen BC_QUEUE_LEN]\n"
		"\n"
		"MODE: private | vepa | bridge | passthru | source\n"
		"MODE_FLAG: null | nopromisc | nodst\n"
		"MODE_OPTS: for mode \"source\":\n"
		"\tmacaddr { { add | del } <macaddr> | set [ <macaddr> [ <macaddr>  ... ] ] | flush }\n"
		"BC_QUEUE_LEN: Length of the rx queue for broadcast/multicast: [0-4294967295]\n",
		lu->id
	);
}

static void explain(struct link_util *lu)
{
	print_explain(lu, stderr);
}


static int mode_arg(const char *arg)
{
	fprintf(stderr,
		"Error: argument of \"mode\" must be \"private\", \"vepa\", \"bridge\", \"passthru\" or \"source\", not \"%s\"\n",
		arg);
	return -1;
}

static int flag_arg(const char *arg)
{
	fprintf(stderr,
		"Error: argument of \"flag\" must be \"nopromisc\", \"nodst\" or \"null\", not \"%s\"\n",
		arg);
	return -1;
}

static int bc_queue_len_arg(const char *arg)
{
	fprintf(stderr,
		"Error: argument of \"bcqueuelen\" must be a positive integer [0-4294967295], not \"%s\"\n",
		arg);
	return -1;
}

static int macvlan_parse_opt(struct link_util *lu, int argc, char **argv,
			  struct nlmsghdr *n)
{
	__u32 mode = 0;
	__u16 flags = 0;
	__u32 mac_mode = 0;
	int has_flags = 0;
	char mac[ETH_ALEN];
	struct rtattr *nmac;

	while (argc > 0) {
		if (matches(*argv, "mode") == 0) {
			NEXT_ARG();

			if (strcmp(*argv, "private") == 0)
				mode = MACVLAN_MODE_PRIVATE;
			else if (strcmp(*argv, "vepa") == 0)
				mode = MACVLAN_MODE_VEPA;
			else if (strcmp(*argv, "bridge") == 0)
				mode = MACVLAN_MODE_BRIDGE;
			else if (strcmp(*argv, "passthru") == 0)
				mode = MACVLAN_MODE_PASSTHRU;
			else if (strcmp(*argv, "source") == 0)
				mode = MACVLAN_MODE_SOURCE;
			else
				return mode_arg(*argv);
		} else if (matches(*argv, "flag") == 0) {
			NEXT_ARG();

			if (strcmp(*argv, "nopromisc") == 0)
				flags |= MACVLAN_FLAG_NOPROMISC;
			else if (strcmp(*argv, "nodst") == 0)
				flags |= MACVLAN_FLAG_NODST;
			else if (strcmp(*argv, "null") == 0)
				flags |= 0;
			else
				return flag_arg(*argv);

			has_flags = 1;

		} else if (matches(*argv, "macaddr") == 0) {
			NEXT_ARG();

			if (strcmp(*argv, "add") == 0) {
				mac_mode = MACVLAN_MACADDR_ADD;
			} else if (strcmp(*argv, "del") == 0) {
				mac_mode = MACVLAN_MACADDR_DEL;
			} else if (strcmp(*argv, "set") == 0) {
				mac_mode = MACVLAN_MACADDR_SET;
			} else if (strcmp(*argv, "flush") == 0) {
				mac_mode = MACVLAN_MACADDR_FLUSH;
			} else {
				explain(lu);
				return -1;
			}

			addattr32(n, 1024, IFLA_MACVLAN_MACADDR_MODE, mac_mode);

			if (mac_mode == MACVLAN_MACADDR_ADD ||
			    mac_mode == MACVLAN_MACADDR_DEL) {
				NEXT_ARG();

				if (ll_addr_a2n(mac, sizeof(mac),
						*argv) != ETH_ALEN)
					return -1;

				addattr_l(n, 1024, IFLA_MACVLAN_MACADDR, &mac,
					  ETH_ALEN);
			}

			if (mac_mode == MACVLAN_MACADDR_SET) {
				nmac = addattr_nest(n, 1024,
						    IFLA_MACVLAN_MACADDR_DATA);
				while (NEXT_ARG_OK()) {
					NEXT_ARG_FWD();

					if (ll_addr_a2n(mac, sizeof(mac),
							*argv) != ETH_ALEN) {
						PREV_ARG();
						break;
					}

					addattr_l(n, 1024, IFLA_MACVLAN_MACADDR,
						  &mac, ETH_ALEN);
				}
				addattr_nest_end(n, nmac);
			}
		} else if (matches(*argv, "nopromisc") == 0) {
			flags |= MACVLAN_FLAG_NOPROMISC;
			has_flags = 1;
		} else if (matches(*argv, "nodst") == 0) {
			flags |= MACVLAN_FLAG_NODST;
			has_flags = 1;
		} else if (matches(*argv, "bcqueuelen") == 0) {
			__u32 bc_queue_len;
			NEXT_ARG();

			if (get_u32(&bc_queue_len, *argv, 0)) {
				return bc_queue_len_arg(*argv);
			}
			addattr32(n, 1024, IFLA_MACVLAN_BC_QUEUE_LEN, bc_queue_len);
		} else if (matches(*argv, "help") == 0) {
			explain(lu);
			return -1;
		} else {
			pfx_err(lu, "unknown option \"%s\"?", *argv);
			explain(lu);
			return -1;
		}
		argc--, argv++;
	}

	if (mode)
		addattr32(n, 1024, IFLA_MACVLAN_MODE, mode);

	if (has_flags) {
		if (flags & MACVLAN_FLAG_NOPROMISC &&
		    mode != MACVLAN_MODE_PASSTHRU) {
			pfx_err(lu, "nopromisc flag only valid in passthru mode");
			explain(lu);
			return -1;
		}
		addattr16(n, 1024, IFLA_MACVLAN_FLAGS, flags);
	}
	return 0;
}

static void macvlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
	__u32 mode;
	__u16 flags;
	__u32 count;
	unsigned char *addr;
	int len;
	struct rtattr *rta;

	if (!tb)
		return;

	if (!tb[IFLA_MACVLAN_MODE] ||
	    RTA_PAYLOAD(tb[IFLA_MACVLAN_MODE]) < sizeof(__u32))
		return;

	mode = rta_getattr_u32(tb[IFLA_MACVLAN_MODE]);
	print_string(PRINT_ANY,
		     "mode",
		     "mode %s ",
		     mode == MACVLAN_MODE_PRIVATE ? "private"
		     : mode == MACVLAN_MODE_VEPA    ? "vepa"
		     : mode == MACVLAN_MODE_BRIDGE  ? "bridge"
		     : mode == MACVLAN_MODE_PASSTHRU  ? "passthru"
		     : mode == MACVLAN_MODE_SOURCE  ? "source"
		     :				 "unknown");

	if (!tb[IFLA_MACVLAN_FLAGS] ||
	    RTA_PAYLOAD(tb[IFLA_MACVLAN_FLAGS]) < sizeof(__u16))
		flags = 0;
	else
		flags = rta_getattr_u16(tb[IFLA_MACVLAN_FLAGS]);

	if (flags & MACVLAN_FLAG_NOPROMISC)
		print_bool(PRINT_ANY, "nopromisc", "nopromisc ", true);

	if (flags & MACVLAN_FLAG_NODST)
		print_bool(PRINT_ANY, "nodst", "nodst ", true);

	if (tb[IFLA_MACVLAN_BC_QUEUE_LEN] &&
		RTA_PAYLOAD(tb[IFLA_MACVLAN_BC_QUEUE_LEN]) >= sizeof(__u32)) {
		__u32 bc_queue_len = rta_getattr_u32(tb[IFLA_MACVLAN_BC_QUEUE_LEN]);
		print_luint(PRINT_ANY, "bcqueuelen", "bcqueuelen %lu ", bc_queue_len);
	}

	if (tb[IFLA_MACVLAN_BC_QUEUE_LEN_USED] &&
		RTA_PAYLOAD(tb[IFLA_MACVLAN_BC_QUEUE_LEN_USED]) >= sizeof(__u32)) {
		__u32 bc_queue_len = rta_getattr_u32(tb[IFLA_MACVLAN_BC_QUEUE_LEN_USED]);
		print_luint(PRINT_ANY, "usedbcqueuelen", "usedbcqueuelen %lu ", bc_queue_len);
	}

	/* in source mode, there are more options to print */

	if (mode != MACVLAN_MODE_SOURCE)
		return;

	if (!tb[IFLA_MACVLAN_MACADDR_COUNT] ||
	    RTA_PAYLOAD(tb[IFLA_MACVLAN_MACADDR_COUNT]) < sizeof(__u32))
		return;

	count = rta_getattr_u32(tb[IFLA_MACVLAN_MACADDR_COUNT]);
	print_int(PRINT_ANY, "macaddr_count", "remotes (%d) ", count);

	if (!tb[IFLA_MACVLAN_MACADDR_DATA])
		return;

	rta = RTA_DATA(tb[IFLA_MACVLAN_MACADDR_DATA]);
	len = RTA_PAYLOAD(tb[IFLA_MACVLAN_MACADDR_DATA]);

	open_json_array(PRINT_JSON, "macaddr_data");
	for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
		if (rta->rta_type != IFLA_MACVLAN_MACADDR ||
		    RTA_PAYLOAD(rta) < 6)
			continue;
		addr = RTA_DATA(rta);
		if (is_json_context()) {
			SPRINT_BUF(b1);

			snprintf(b1, sizeof(b1),
				 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", addr[0],
				 addr[1], addr[2], addr[3], addr[4], addr[5]);
			print_string(PRINT_JSON, NULL, NULL, b1);
		} else {
			fprintf(f, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x ", addr[0],
				addr[1], addr[2], addr[3], addr[4], addr[5]);
		}
	}
	close_json_array(PRINT_JSON, NULL);
}

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

struct link_util macvlan_link_util = {
	.id		= "macvlan",
	.maxattr	= IFLA_MACVLAN_MAX,
	.parse_opt	= macvlan_parse_opt,
	.print_opt	= macvlan_print_opt,
	.print_help	= macvlan_print_help,
};

struct link_util macvtap_link_util = {
	.id		= "macvtap",
	.maxattr	= IFLA_MACVLAN_MAX,
	.parse_opt	= macvlan_parse_opt,
	.print_opt	= macvlan_print_opt,
	.print_help	= macvlan_print_help,
};