iplink_vlan.c revision 5c302d518f10e67ddab9e44207a0c878214ed389
1/*
2 * iplink_vlan.c	VLAN device support
3 *
4 *              This program is free software; you can redistribute it and/or
5 *              modify it under the terms of the GNU General Public License
6 *              as published by the Free Software Foundation; either version
7 *              2 of the License, or (at your option) any later version.
8 *
9 * Authors:     Patrick McHardy <kaber@trash.net>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <linux/if_vlan.h>
16
17#include "rt_names.h"
18#include "utils.h"
19#include "ip_common.h"
20
21static void explain(void)
22{
23	fprintf(stderr,
24		"Usage: ... vlan id VLANID [ FLAG-LIST ]\n"
25		"                          [ ingress-qos-map QOS-MAP ] [ egress-qos-map QOS-MAP ]\n"
26		"\n"
27		"VLANID := 0-4095\n"
28		"FLAG-LIST := [ FLAG-LIST ] FLAG\n"
29		"FLAG := [ reorder_hdr { on | off } ]\n"
30		"QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
31		"QOS-MAPPING := FROM:TO\n"
32	);
33}
34
35static int on_off(char *msg)
36{
37	fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\"\n", msg);
38	return -1;
39}
40
41static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
42			      int attrtype)
43{
44	int argc = *argcp;
45	char **argv = *argvp;
46	struct ifla_vlan_qos_mapping m;
47	struct rtattr *tail;
48
49	tail = NLMSG_TAIL(n);
50	addattr_l(n, 1024, attrtype, NULL, 0);
51
52	while (argc > 0) {
53		char *colon = strchr(*argv, ':');
54
55		if (!colon)
56			break;
57		*colon = '\0';
58
59		if (get_u32(&m.from, *argv, 0))
60			return 1;
61		if (get_u32(&m.to, colon + 1, 0))
62			return 1;
63		argc--, argv++;
64
65		addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
66	}
67
68	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *)tail;
69
70	*argcp = argc;
71	*argvp = argv;
72	return 0;
73}
74
75static int vlan_parse_opt(struct link_util *lu, int argc, char **argv,
76			  struct nlmsghdr *n)
77{
78	struct ifla_vlan_flags flags = { 0 };
79	__u16 id;
80
81	while (argc > 0) {
82		if (matches(*argv, "id") == 0) {
83			NEXT_ARG();
84			if (get_u16(&id, *argv, 0))
85				invarg("id is invalid", *argv);
86			addattr_l(n, 1024, IFLA_VLAN_ID, &id, 2);
87		} else if (matches(*argv, "reorder_hdr") == 0) {
88			NEXT_ARG();
89			flags.mask |= VLAN_FLAG_REORDER_HDR;
90			if (strcmp(*argv, "on") == 0)
91				flags.flags |= VLAN_FLAG_REORDER_HDR;
92			else if (strcmp(*argv, "off") == 0)
93				flags.flags &= ~VLAN_FLAG_REORDER_HDR;
94			else
95				return on_off("reorder_hdr");
96		} else if (matches(*argv, "ingress-qos-map") == 0) {
97			NEXT_ARG();
98			if (vlan_parse_qos_map(&argc, &argv, n,
99					       IFLA_VLAN_INGRESS_QOS))
100				invarg("invalid ingress-qos-map", *argv);
101			continue;
102		} else if (matches(*argv, "egress-qos-map") == 0) {
103			NEXT_ARG();
104			if (vlan_parse_qos_map(&argc, &argv, n,
105					       IFLA_VLAN_EGRESS_QOS))
106				invarg("invalid egress-qos-map", *argv);
107			continue;
108		} else if (matches(*argv, "help") == 0) {
109			explain();
110			return -1;
111		} else {
112			fprintf(stderr, "vlan: what is \"%s\"?\n", *argv);
113			explain();
114			return -1;
115		}
116		argc--, argv++;
117	}
118
119	if (flags.mask)
120		addattr_l(n, 1024, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
121
122	return 0;
123}
124
125static void vlan_print_map(FILE *f, char *name, struct rtattr *attr)
126{
127	struct ifla_vlan_qos_mapping *m;
128	struct rtattr *i;
129	int rem;
130
131	fprintf(f, "\n      %s { ", name);
132
133	rem = RTA_PAYLOAD(attr);
134	for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
135		m = RTA_DATA(i);
136		fprintf(f, "%u:%u ", m->from, m->to);
137	}
138	fprintf(f, "} ");
139}
140
141static void vlan_print_flags(FILE *fp, __u32 flags)
142{
143	fprintf(fp, "<");
144#define _PF(f)	if (flags & VLAN_FLAG_##f) { \
145			flags &= ~ VLAN_FLAG_##f; \
146			fprintf(fp, #f "%s", flags ? "," : ""); \
147		}
148	_PF(REORDER_HDR);
149#undef _PF
150	if (flags)
151		fprintf(fp, "%x", flags);
152	fprintf(fp, "> ");
153}
154
155static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
156{
157	struct ifla_vlan_flags *flags;
158	if (!tb)
159		return;
160
161	if (!tb[IFLA_VLAN_ID] ||
162	    RTA_PAYLOAD(tb[IFLA_VLAN_ID]) < sizeof(__u16))
163		return;
164
165	fprintf(f, "id %u ", *(__u16 *)RTA_DATA(tb[IFLA_VLAN_ID]));
166
167	if (tb[IFLA_VLAN_FLAGS]) {
168		if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags))
169			return;
170		flags = RTA_DATA(tb[IFLA_VLAN_FLAGS]);
171		vlan_print_flags(f, flags->flags);
172	}
173	if (tb[IFLA_VLAN_INGRESS_QOS])
174		vlan_print_map(f, "ingress-qos-map", tb[IFLA_VLAN_INGRESS_QOS]);
175	if (tb[IFLA_VLAN_EGRESS_QOS])
176		vlan_print_map(f, "egress-qos-map", tb[IFLA_VLAN_EGRESS_QOS]);
177}
178
179struct link_util vlan_link_util = {
180	.id		= "vlan",
181	.maxattr	= IFLA_VLAN_MAX,
182	.parse_opt	= vlan_parse_opt,
183	.print_opt	= vlan_print_opt,
184};
185