Tweaks
[iproute2.git] / ip / iplink_vlan.c
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
21 static 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                 "QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n"
31                 "QOS-MAPPING := FROM:TO\n"
32         );
33 }
34
35 static 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
41 static 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
75 static 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, "gvrp") == 0) {
97                         NEXT_ARG();
98                         flags.mask |= VLAN_FLAG_GVRP;
99                         if (strcmp(*argv, "on") == 0)
100                                 flags.flags |= VLAN_FLAG_GVRP;
101                         else if (strcmp(*argv, "off") == 0)
102                                 flags.flags &= ~VLAN_FLAG_GVRP;
103                         else
104                                 return on_off("gvrp");
105                 } else if (matches(*argv, "ingress-qos-map") == 0) {
106                         NEXT_ARG();
107                         if (vlan_parse_qos_map(&argc, &argv, n,
108                                                IFLA_VLAN_INGRESS_QOS))
109                                 invarg("invalid ingress-qos-map", *argv);
110                         continue;
111                 } else if (matches(*argv, "egress-qos-map") == 0) {
112                         NEXT_ARG();
113                         if (vlan_parse_qos_map(&argc, &argv, n,
114                                                IFLA_VLAN_EGRESS_QOS))
115                                 invarg("invalid egress-qos-map", *argv);
116                         continue;
117                 } else if (matches(*argv, "help") == 0) {
118                         explain();
119                         return -1;
120                 } else {
121                         fprintf(stderr, "vlan: what is \"%s\"?\n", *argv);
122                         explain();
123                         return -1;
124                 }
125                 argc--, argv++;
126         }
127
128         if (flags.mask)
129                 addattr_l(n, 1024, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
130
131         return 0;
132 }
133
134 static void vlan_print_map(FILE *f, char *name, struct rtattr *attr)
135 {
136         struct ifla_vlan_qos_mapping *m;
137         struct rtattr *i;
138         int rem;
139
140         fprintf(f, "\n      %s { ", name);
141
142         rem = RTA_PAYLOAD(attr);
143         for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
144                 m = RTA_DATA(i);
145                 fprintf(f, "%u:%u ", m->from, m->to);
146         }
147         fprintf(f, "} ");
148 }
149
150 static void vlan_print_flags(FILE *fp, __u32 flags)
151 {
152         fprintf(fp, "<");
153 #define _PF(f)  if (flags & VLAN_FLAG_##f) { \
154                         flags &= ~ VLAN_FLAG_##f; \
155                         fprintf(fp, #f "%s", flags ? "," : ""); \
156                 }
157         _PF(REORDER_HDR);
158         _PF(GVRP);
159 #undef _PF
160         if (flags)
161                 fprintf(fp, "%x", flags);
162         fprintf(fp, "> ");
163 }
164
165 static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
166 {
167         struct ifla_vlan_flags *flags;
168         if (!tb)
169                 return;
170
171         if (!tb[IFLA_VLAN_ID] ||
172             RTA_PAYLOAD(tb[IFLA_VLAN_ID]) < sizeof(__u16))
173                 return;
174
175         fprintf(f, "id %u ", *(__u16 *)RTA_DATA(tb[IFLA_VLAN_ID]));
176
177         if (tb[IFLA_VLAN_FLAGS]) {
178                 if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags))
179                         return;
180                 flags = RTA_DATA(tb[IFLA_VLAN_FLAGS]);
181                 vlan_print_flags(f, flags->flags);
182         }
183         if (tb[IFLA_VLAN_INGRESS_QOS])
184                 vlan_print_map(f, "ingress-qos-map", tb[IFLA_VLAN_INGRESS_QOS]);
185         if (tb[IFLA_VLAN_EGRESS_QOS])
186                 vlan_print_map(f, "egress-qos-map", tb[IFLA_VLAN_EGRESS_QOS]);
187 }
188
189 struct link_util vlan_link_util = {
190         .id             = "vlan",
191         .maxattr        = IFLA_VLAN_MAX,
192         .parse_opt      = vlan_parse_opt,
193         .print_opt      = vlan_print_opt,
194 };