blob: c2684461b616aaed07dd4f3b32c49c59e3383de7 [file] [log] [blame]
Jiri Pirko8b1c0212014-11-21 12:31:30 +01001/*
2 * m_vlan.c vlan manipulation module
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: Jiri Pirko <jiri@resnulli.us>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <string.h>
16#include <linux/if_ether.h>
17#include "utils.h"
18#include "rt_names.h"
19#include "tc_util.h"
20#include <linux/tc_act/tc_vlan.h>
21
22static void explain(void)
23{
24 fprintf(stderr, "Usage: vlan pop\n");
Phil Sutter51011da2016-03-22 15:48:38 +010025 fprintf(stderr, " vlan push [ protocol VLANPROTO ] id VLANID [CONTROL]\n");
Jiri Pirko8b1c0212014-11-21 12:31:30 +010026 fprintf(stderr, " VLANPROTO is one of 802.1Q or 802.1AD\n");
27 fprintf(stderr, " with default: 802.1Q\n");
Phil Sutter51011da2016-03-22 15:48:38 +010028 fprintf(stderr, " CONTROL := reclassify | pipe | drop | continue | pass\n");
Jiri Pirko8b1c0212014-11-21 12:31:30 +010029}
30
31static void usage(void)
32{
33 explain();
34 exit(-1);
35}
36
37static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
38 int tca_id, struct nlmsghdr *n)
39{
40 int argc = *argc_p;
41 char **argv = *argv_p;
42 struct rtattr *tail;
43 int action = 0;
44 __u16 id;
45 int id_set = 0;
46 __u16 proto;
47 int proto_set = 0;
48 struct tc_vlan parm = { 0 };
49
50 if (matches(*argv, "vlan") != 0)
51 return -1;
52
53 NEXT_ARG();
54
55 while (argc > 0) {
56 if (matches(*argv, "pop") == 0) {
57 if (action) {
58 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
59 *argv);
60 explain();
61 return -1;
62 }
63 action = TCA_VLAN_ACT_POP;
64 } else if (matches(*argv, "push") == 0) {
65 if (action) {
66 fprintf(stderr, "unexpected \"%s\" - action already specified\n",
67 *argv);
68 explain();
69 return -1;
70 }
71 action = TCA_VLAN_ACT_PUSH;
72 } else if (matches(*argv, "id") == 0) {
73 if (action != TCA_VLAN_ACT_PUSH) {
74 fprintf(stderr, "\"%s\" is only valid for push\n",
75 *argv);
76 explain();
77 return -1;
78 }
79 NEXT_ARG();
80 if (get_u16(&id, *argv, 0))
81 invarg("id is invalid", *argv);
82 id_set = 1;
83 } else if (matches(*argv, "protocol") == 0) {
84 if (action != TCA_VLAN_ACT_PUSH) {
85 fprintf(stderr, "\"%s\" is only valid for push\n",
86 *argv);
87 explain();
88 return -1;
89 }
90 NEXT_ARG();
91 if (ll_proto_a2n(&proto, *argv))
92 invarg("protocol is invalid", *argv);
93 proto_set = 1;
94 } else if (matches(*argv, "help") == 0) {
95 usage();
96 } else {
97 break;
98 }
99 argc--;
100 argv++;
101 }
102
103 parm.action = TC_ACT_PIPE;
104 if (argc) {
105 if (matches(*argv, "reclassify") == 0) {
106 parm.action = TC_ACT_RECLASSIFY;
Jamal Hadi Salim564663b2015-01-11 09:31:30 -0500107 argc--;
108 argv++;
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100109 } else if (matches(*argv, "pipe") == 0) {
110 parm.action = TC_ACT_PIPE;
Jamal Hadi Salim564663b2015-01-11 09:31:30 -0500111 argc--;
112 argv++;
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100113 } else if (matches(*argv, "drop") == 0 ||
114 matches(*argv, "shot") == 0) {
115 parm.action = TC_ACT_SHOT;
Jamal Hadi Salim564663b2015-01-11 09:31:30 -0500116 argc--;
117 argv++;
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100118 } else if (matches(*argv, "continue") == 0) {
119 parm.action = TC_ACT_UNSPEC;
Jamal Hadi Salim564663b2015-01-11 09:31:30 -0500120 argc--;
121 argv++;
Jamal Hadi Salim43726b72016-05-07 09:39:36 -0400122 } else if (matches(*argv, "pass") == 0 ||
123 matches(*argv, "ok") == 0) {
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100124 parm.action = TC_ACT_OK;
Jamal Hadi Salim564663b2015-01-11 09:31:30 -0500125 argc--;
126 argv++;
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100127 }
128 }
129
130 if (argc) {
131 if (matches(*argv, "index") == 0) {
132 NEXT_ARG();
133 if (get_u32(&parm.index, *argv, 10)) {
134 fprintf(stderr, "vlan: Illegal \"index\"\n");
135 return -1;
136 }
137 argc--;
138 argv++;
139 }
140 }
141
142 if (action == TCA_VLAN_ACT_PUSH && !id_set) {
143 fprintf(stderr, "id needs to be set for push\n");
144 explain();
145 return -1;
146 }
147
148 parm.v_action = action;
149 tail = NLMSG_TAIL(n);
150 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
151 addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm));
152 if (id_set)
153 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_ID, &id, 2);
154 if (proto_set) {
155 if (proto != htons(ETH_P_8021Q) &&
156 proto != htons(ETH_P_8021AD)) {
157 fprintf(stderr, "protocol not supported\n");
158 explain();
159 return -1;
160 }
161
162 addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PROTOCOL, &proto, 2);
163 }
164 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
165
166 *argc_p = argc;
167 *argv_p = argv;
168 return 0;
169}
170
171static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg)
172{
173 SPRINT_BUF(b1);
174 struct rtattr *tb[TCA_VLAN_MAX + 1];
175 __u16 val;
176 struct tc_vlan *parm;
177
178 if (arg == NULL)
179 return -1;
180
181 parse_rtattr_nested(tb, TCA_VLAN_MAX, arg);
182
183 if (!tb[TCA_VLAN_PARMS]) {
184 fprintf(f, "[NULL vlan parameters]");
185 return -1;
186 }
187 parm = RTA_DATA(tb[TCA_VLAN_PARMS]);
188
189 fprintf(f, " vlan");
190
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700191 switch (parm->v_action) {
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100192 case TCA_VLAN_ACT_POP:
193 fprintf(f, " pop");
194 break;
195 case TCA_VLAN_ACT_PUSH:
196 fprintf(f, " push");
197 if (tb[TCA_VLAN_PUSH_VLAN_ID]) {
198 val = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
199 fprintf(f, " id %u", val);
200 }
201 if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
202 fprintf(f, " protocol %s",
203 ll_proto_n2a(rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]),
204 b1, sizeof(b1)));
205 }
206 break;
207 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700208 fprintf(f, " %s", action_n2a(parm->action, b1, sizeof(b1)));
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100209
210 fprintf(f, "\n\t index %d ref %d bind %d", parm->index, parm->refcnt,
211 parm->bindcnt);
212
213 if (show_stats) {
214 if (tb[TCA_VLAN_TM]) {
215 struct tcf_t *tm = RTA_DATA(tb[TCA_VLAN_TM]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700216
Jiri Pirko8b1c0212014-11-21 12:31:30 +0100217 print_tm(f, tm);
218 }
219 }
220
221 fprintf(f, "\n ");
222
223 return 0;
224}
225
226struct action_util vlan_action_util = {
227 .id = "vlan",
228 .parse_aopt = parse_vlan,
229 .print_aopt = print_vlan,
230};