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 } ] [ gvrp { on | off } ]\n"
30		"        [ loose_binding { on | off } ]\n"
31		"QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
32		"QOS-MAPPING := FROM:TO\n"
33	);
34}
35
36static int on_off(char *msg)
37{
38	fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\"\n", msg);
39	return -1;
40}
41
42static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n,
43			      int attrtype)
44{
45	int argc = *argcp;
46	char **argv = *argvp;
47	struct ifla_vlan_qos_mapping m;
48	struct rtattr *tail;
49
50	tail = NLMSG_TAIL(n);
51	addattr_l(n, 1024, attrtype, NULL, 0);
52
53	while (argc > 0) {
54		char *colon = strchr(*argv, ':');
55
56		if (!colon)
57			break;
58		*colon = '\0';
59
60		if (get_u32(&m.from, *argv, 0))
61			return 1;
62		if (get_u32(&m.to, colon + 1, 0))
63			return 1;
64		argc--, argv++;
65
66		addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m));
67	}
68
69	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *)tail;
70
71	*argcp = argc;
72	*argvp = argv;
73	return 0;
74}
75
76static int vlan_parse_opt(struct link_util *lu, int argc, char **argv,
77			  struct nlmsghdr *n)
78{
79	struct ifla_vlan_flags flags = { 0 };
80	__u16 id;
81
82	while (argc > 0) {
83		if (matches(*argv, "id") == 0) {
84			NEXT_ARG();
85			if (get_u16(&id, *argv, 0))
86				invarg("id is invalid", *argv);
87			addattr_l(n, 1024, IFLA_VLAN_ID, &id, 2);
88		} else if (matches(*argv, "reorder_hdr") == 0) {
89			NEXT_ARG();
90			flags.mask |= VLAN_FLAG_REORDER_HDR;
91			if (strcmp(*argv, "on") == 0)
92				flags.flags |= VLAN_FLAG_REORDER_HDR;
93			else if (strcmp(*argv, "off") == 0)
94				flags.flags &= ~VLAN_FLAG_REORDER_HDR;
95			else
96				return on_off("reorder_hdr");
97		} else if (matches(*argv, "gvrp") == 0) {
98			NEXT_ARG();
99			flags.mask |= VLAN_FLAG_GVRP;
100			if (strcmp(*argv, "on") == 0)
101				flags.flags |= VLAN_FLAG_GVRP;
102			else if (strcmp(*argv, "off") == 0)
103				flags.flags &= ~VLAN_FLAG_GVRP;
104			else
105				return on_off("gvrp");
106		} else if (matches(*argv, "loose_binding") == 0) {
107			NEXT_ARG();
108			flags.mask |= VLAN_FLAG_LOOSE_BINDING;
109			if (strcmp(*argv, "on") == 0)
110				flags.flags |= VLAN_FLAG_LOOSE_BINDING;
111			else if (strcmp(*argv, "off") == 0)
112				flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
113			else
114				return on_off("loose_binding");
115		} else if (matches(*argv, "ingress-qos-map") == 0) {
116			NEXT_ARG();
117			if (vlan_parse_qos_map(&argc, &argv, n,
118					       IFLA_VLAN_INGRESS_QOS))
119				invarg("invalid ingress-qos-map", *argv);
120			continue;
121		} else if (matches(*argv, "egress-qos-map") == 0) {
122			NEXT_ARG();
123			if (vlan_parse_qos_map(&argc, &argv, n,
124					       IFLA_VLAN_EGRESS_QOS))
125				invarg("invalid egress-qos-map", *argv);
126			continue;
127		} else if (matches(*argv, "help") == 0) {
128			explain();
129			return -1;
130		} else {
131			fprintf(stderr, "vlan: what is \"%s\"?\n", *argv);
132			explain();
133			return -1;
134		}
135		argc--, argv++;
136	}
137
138	if (flags.mask)
139		addattr_l(n, 1024, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
140
141	return 0;
142}
143
144static void vlan_print_map(FILE *f, char *name, struct rtattr *attr)
145{
146	struct ifla_vlan_qos_mapping *m;
147	struct rtattr *i;
148	int rem;
149
150	fprintf(f, "\n      %s { ", name);
151
152	rem = RTA_PAYLOAD(attr);
153	for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
154		m = RTA_DATA(i);
155		fprintf(f, "%u:%u ", m->from, m->to);
156	}
157	fprintf(f, "} ");
158}
159
160static void vlan_print_flags(FILE *fp, __u32 flags)
161{
162	fprintf(fp, "<");
163#define _PF(f)	if (flags & VLAN_FLAG_##f) { \
164			flags &= ~ VLAN_FLAG_##f; \
165			fprintf(fp, #f "%s", flags ? "," : ""); \
166		}
167	_PF(REORDER_HDR);
168	_PF(GVRP);
169	_PF(LOOSE_BINDING);
170#undef _PF
171	if (flags)
172		fprintf(fp, "%x", flags);
173	fprintf(fp, "> ");
174}
175
176static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
177{
178	struct ifla_vlan_flags *flags;
179	if (!tb)
180		return;
181
182	if (!tb[IFLA_VLAN_ID] ||
183	    RTA_PAYLOAD(tb[IFLA_VLAN_ID]) < sizeof(__u16))
184		return;
185
186	fprintf(f, "id %u ", rta_getattr_u16(tb[IFLA_VLAN_ID]));
187
188	if (tb[IFLA_VLAN_FLAGS]) {
189		if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags))
190			return;
191		flags = RTA_DATA(tb[IFLA_VLAN_FLAGS]);
192		vlan_print_flags(f, flags->flags);
193	}
194	if (tb[IFLA_VLAN_INGRESS_QOS])
195		vlan_print_map(f, "ingress-qos-map", tb[IFLA_VLAN_INGRESS_QOS]);
196	if (tb[IFLA_VLAN_EGRESS_QOS])
197		vlan_print_map(f, "egress-qos-map", tb[IFLA_VLAN_EGRESS_QOS]);
198}
199
200struct link_util vlan_link_util = {
201	.id		= "vlan",
202	.maxattr	= IFLA_VLAN_MAX,
203	.parse_opt	= vlan_parse_opt,
204	.print_opt	= vlan_print_opt,
205};
206