]> git.ipfire.org Git - thirdparty/iproute2.git/blame - tc/m_mpls.c
tc: make action_util arg const
[thirdparty/iproute2.git] / tc / m_mpls.c
CommitLineData
fb57b092
JH
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2019 Netronome Systems, Inc. */
3
4#include <linux/if_ether.h>
5#include <linux/tc_act/tc_mpls.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11#include "utils.h"
12#include "rt_names.h"
13#include "tc_util.h"
14
15static const char * const action_names[] = {
16 [TCA_MPLS_ACT_POP] = "pop",
17 [TCA_MPLS_ACT_PUSH] = "push",
18 [TCA_MPLS_ACT_MODIFY] = "modify",
19 [TCA_MPLS_ACT_DEC_TTL] = "dec_ttl",
02a261b5 20 [TCA_MPLS_ACT_MAC_PUSH] = "mac_push",
fb57b092
JH
21};
22
23static void explain(void)
24{
25 fprintf(stderr,
8682f588 26 "Usage: mpls pop [ protocol MPLS_PROTO ] [CONTROL]\n"
fb57b092
JH
27 " mpls push [ protocol MPLS_PROTO ] [ label MPLS_LABEL ] [ tc MPLS_TC ]\n"
28 " [ ttl MPLS_TTL ] [ bos MPLS_BOS ] [CONTROL]\n"
02a261b5
GN
29 " mpls mac_push [ protocol MPLS_PROTO ] [ label MPLS_LABEL ] [ tc MPLS_TC ]\n"
30 " [ ttl MPLS_TTL ] [ bos MPLS_BOS ] [CONTROL]\n"
8682f588
GN
31 " mpls modify [ label MPLS_LABEL ] [ tc MPLS_TC ] [ ttl MPLS_TTL ]\n"
32 " [ bos MPLS_BOS ] [CONTROL]\n"
02a261b5
GN
33 " for pop, MPLS_PROTO is next header of packet - e.g. ip or mpls_uc\n"
34 " for push and mac_push, MPLS_PROTO is one of mpls_uc or mpls_mc\n"
fb57b092
JH
35 " with default: mpls_uc\n"
36 " CONTROL := reclassify | pipe | drop | continue | pass |\n"
37 " goto chain <CHAIN_INDEX>\n");
38}
39
40static void usage(void)
41{
42 explain();
43 exit(-1);
44}
45
46static bool can_modify_mpls_fields(unsigned int action)
47{
02a261b5
GN
48 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH ||
49 action == TCA_MPLS_ACT_MODIFY;
fb57b092
JH
50}
51
02a261b5 52static bool can_set_ethtype(unsigned int action)
fb57b092 53{
02a261b5
GN
54 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH ||
55 action == TCA_MPLS_ACT_POP;
fb57b092
JH
56}
57
58static bool is_valid_label(__u32 label)
59{
60 return label <= 0xfffff;
61}
62
63static bool check_double_action(unsigned int action, const char *arg)
64{
65 if (!action)
66 return false;
67
68 fprintf(stderr,
69 "Error: got \"%s\" but action already set to \"%s\"\n",
70 arg, action_names[action]);
71 explain();
72 return true;
73}
74
38b0e6c1 75static int parse_mpls(const struct action_util *a, int *argc_p, char ***argv_p,
fb57b092
JH
76 int tca_id, struct nlmsghdr *n)
77{
78 struct tc_mpls parm = {};
79 __u32 label = 0xffffffff;
80 unsigned int action = 0;
81 char **argv = *argv_p;
82 struct rtattr *tail;
83 int argc = *argc_p;
84 __u16 proto = 0;
85 __u8 bos = 0xff;
86 __u8 tc = 0xff;
87 __u8 ttl = 0;
88
89 if (matches(*argv, "mpls") != 0)
90 return -1;
91
92 NEXT_ARG();
93
af6fd6b8
PT
94 if (strcmp(*argv, "index") == 0)
95 goto skip_args;
96
fb57b092
JH
97 while (argc > 0) {
98 if (matches(*argv, "pop") == 0) {
99 if (check_double_action(action, *argv))
100 return -1;
101 action = TCA_MPLS_ACT_POP;
102 } else if (matches(*argv, "push") == 0) {
103 if (check_double_action(action, *argv))
104 return -1;
105 action = TCA_MPLS_ACT_PUSH;
106 } else if (matches(*argv, "modify") == 0) {
107 if (check_double_action(action, *argv))
108 return -1;
109 action = TCA_MPLS_ACT_MODIFY;
f1298d76
GN
110 } else if (matches(*argv, "mac_push") == 0) {
111 if (check_double_action(action, *argv))
112 return -1;
113 action = TCA_MPLS_ACT_MAC_PUSH;
fb57b092
JH
114 } else if (matches(*argv, "dec_ttl") == 0) {
115 if (check_double_action(action, *argv))
116 return -1;
117 action = TCA_MPLS_ACT_DEC_TTL;
118 } else if (matches(*argv, "label") == 0) {
119 if (!can_modify_mpls_fields(action))
02a261b5
GN
120 invarg("only valid for push, mac_push and modify",
121 *argv);
fb57b092
JH
122 NEXT_ARG();
123 if (get_u32(&label, *argv, 0) || !is_valid_label(label))
124 invarg("label must be <=0xFFFFF", *argv);
125 } else if (matches(*argv, "tc") == 0) {
126 if (!can_modify_mpls_fields(action))
02a261b5
GN
127 invarg("only valid for push, mac_push and modify",
128 *argv);
fb57b092
JH
129 NEXT_ARG();
130 if (get_u8(&tc, *argv, 0) || (tc & ~0x7))
131 invarg("tc field is 3 bits max", *argv);
132 } else if (matches(*argv, "ttl") == 0) {
133 if (!can_modify_mpls_fields(action))
02a261b5
GN
134 invarg("only valid for push, mac_push and modify",
135 *argv);
fb57b092
JH
136 NEXT_ARG();
137 if (get_u8(&ttl, *argv, 0) || !ttl)
138 invarg("ttl must be >0 and <=255", *argv);
139 } else if (matches(*argv, "bos") == 0) {
140 if (!can_modify_mpls_fields(action))
02a261b5
GN
141 invarg("only valid for push, mac_push and modify",
142 *argv);
fb57b092
JH
143 NEXT_ARG();
144 if (get_u8(&bos, *argv, 0) || (bos & ~0x1))
145 invarg("bos must be 0 or 1", *argv);
146 } else if (matches(*argv, "protocol") == 0) {
02a261b5
GN
147 if (!can_set_ethtype(action))
148 invarg("only valid for push, mac_push and pop",
149 *argv);
fb57b092
JH
150 NEXT_ARG();
151 if (ll_proto_a2n(&proto, *argv))
152 invarg("protocol is invalid", *argv);
153 } else if (matches(*argv, "help") == 0) {
154 usage();
155 } else {
156 break;
157 }
158
159 NEXT_ARG_FWD();
160 }
161
162 if (!action)
163 incomplete_command();
164
165 parse_action_control_dflt(&argc, &argv, &parm.action,
166 false, TC_ACT_PIPE);
167
168 if (argc) {
169 if (matches(*argv, "index") == 0) {
af6fd6b8 170skip_args:
fb57b092
JH
171 NEXT_ARG();
172 if (get_u32(&parm.index, *argv, 10))
173 invarg("illegal index", *argv);
174 NEXT_ARG_FWD();
175 }
176 }
177
72cc0baf 178 if (action == TCA_MPLS_ACT_PUSH && label == 0xffffffff)
fb57b092
JH
179 missarg("label");
180
02a261b5
GN
181 if ((action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH) &&
182 proto &&
fb57b092
JH
183 proto != htons(ETH_P_MPLS_UC) && proto != htons(ETH_P_MPLS_MC)) {
184 fprintf(stderr,
02a261b5
GN
185 "invalid %spush protocol \"0x%04x\" - use mpls_(uc|mc)\n",
186 action == TCA_MPLS_ACT_MAC_PUSH ? "mac_" : "",
fb57b092
JH
187 ntohs(proto));
188 return -1;
189 }
190
191 if (action == TCA_MPLS_ACT_POP && !proto)
192 missarg("protocol");
193
194 parm.m_action = action;
195 tail = addattr_nest(n, MAX_MSG, tca_id | NLA_F_NESTED);
196 addattr_l(n, MAX_MSG, TCA_MPLS_PARMS, &parm, sizeof(parm));
197 if (label != 0xffffffff)
198 addattr_l(n, MAX_MSG, TCA_MPLS_LABEL, &label, sizeof(label));
199 if (proto)
200 addattr_l(n, MAX_MSG, TCA_MPLS_PROTO, &proto, sizeof(proto));
201 if (tc != 0xff)
202 addattr8(n, MAX_MSG, TCA_MPLS_TC, tc);
203 if (ttl)
204 addattr8(n, MAX_MSG, TCA_MPLS_TTL, ttl);
205 if (bos != 0xff)
206 addattr8(n, MAX_MSG, TCA_MPLS_BOS, bos);
207 addattr_nest_end(n, tail);
208
209 *argc_p = argc;
210 *argv_p = argv;
211 return 0;
212}
213
38b0e6c1 214static int print_mpls(const struct action_util *au, FILE *f, struct rtattr *arg)
fb57b092
JH
215{
216 struct rtattr *tb[TCA_MPLS_MAX + 1];
217 struct tc_mpls *parm;
218 SPRINT_BUF(b1);
219 __u32 val;
220
a99ebeee 221 print_string(PRINT_ANY, "kind", "%s ", "mpls");
fb57b092 222 if (!arg)
a99ebeee 223 return 0;
fb57b092
JH
224
225 parse_rtattr_nested(tb, TCA_MPLS_MAX, arg);
226
227 if (!tb[TCA_MPLS_PARMS]) {
228 fprintf(stderr, "[NULL mpls parameters]\n");
229 return -1;
230 }
231 parm = RTA_DATA(tb[TCA_MPLS_PARMS]);
232
fb57b092
JH
233 print_string(PRINT_ANY, "mpls_action", " %s",
234 action_names[parm->m_action]);
235
236 switch (parm->m_action) {
237 case TCA_MPLS_ACT_POP:
238 if (tb[TCA_MPLS_PROTO]) {
239 __u16 proto;
240
241 proto = rta_getattr_u16(tb[TCA_MPLS_PROTO]);
242 print_string(PRINT_ANY, "protocol", " protocol %s",
243 ll_proto_n2a(proto, b1, sizeof(b1)));
244 }
245 break;
246 case TCA_MPLS_ACT_PUSH:
02a261b5 247 case TCA_MPLS_ACT_MAC_PUSH:
fb57b092
JH
248 if (tb[TCA_MPLS_PROTO]) {
249 __u16 proto;
250
251 proto = rta_getattr_u16(tb[TCA_MPLS_PROTO]);
252 print_string(PRINT_ANY, "protocol", " protocol %s",
253 ll_proto_n2a(proto, b1, sizeof(b1)));
254 }
255 /* Fallthrough */
256 case TCA_MPLS_ACT_MODIFY:
257 if (tb[TCA_MPLS_LABEL]) {
258 val = rta_getattr_u32(tb[TCA_MPLS_LABEL]);
259 print_uint(PRINT_ANY, "label", " label %u", val);
260 }
261 if (tb[TCA_MPLS_TC]) {
262 val = rta_getattr_u8(tb[TCA_MPLS_TC]);
263 print_uint(PRINT_ANY, "tc", " tc %u", val);
264 }
265 if (tb[TCA_MPLS_BOS]) {
266 val = rta_getattr_u8(tb[TCA_MPLS_BOS]);
267 print_uint(PRINT_ANY, "bos", " bos %u", val);
268 }
269 if (tb[TCA_MPLS_TTL]) {
270 val = rta_getattr_u8(tb[TCA_MPLS_TTL]);
271 print_uint(PRINT_ANY, "ttl", " ttl %u", val);
272 }
273 break;
274 }
275 print_action_control(f, " ", parm->action, "");
276
7b0d424a
SH
277 print_nl();
278 print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
fb57b092
JH
279 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
280 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
281
282 if (show_stats) {
283 if (tb[TCA_MPLS_TM]) {
284 struct tcf_t *tm = RTA_DATA(tb[TCA_MPLS_TM]);
285
286 print_tm(f, tm);
287 }
288 }
289
0501fe73 290 print_nl();
fb57b092
JH
291
292 return 0;
293}
294
295struct action_util mpls_action_util = {
296 .id = "mpls",
297 .parse_aopt = parse_mpls,
298 .print_aopt = print_mpls,
299};