]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_mpls.c
Merge branch 'main' into next
[mirror_iproute2.git] / tc / m_mpls.c
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
15 static 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",
20 [TCA_MPLS_ACT_MAC_PUSH] = "mac_push",
21 };
22
23 static void explain(void)
24 {
25 fprintf(stderr,
26 "Usage: mpls pop [ protocol MPLS_PROTO ] [CONTROL]\n"
27 " mpls push [ protocol MPLS_PROTO ] [ label MPLS_LABEL ] [ tc MPLS_TC ]\n"
28 " [ ttl MPLS_TTL ] [ bos MPLS_BOS ] [CONTROL]\n"
29 " mpls mac_push [ protocol MPLS_PROTO ] [ label MPLS_LABEL ] [ tc MPLS_TC ]\n"
30 " [ ttl MPLS_TTL ] [ bos MPLS_BOS ] [CONTROL]\n"
31 " mpls modify [ label MPLS_LABEL ] [ tc MPLS_TC ] [ ttl MPLS_TTL ]\n"
32 " [ bos MPLS_BOS ] [CONTROL]\n"
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"
35 " with default: mpls_uc\n"
36 " CONTROL := reclassify | pipe | drop | continue | pass |\n"
37 " goto chain <CHAIN_INDEX>\n");
38 }
39
40 static void usage(void)
41 {
42 explain();
43 exit(-1);
44 }
45
46 static bool can_modify_mpls_fields(unsigned int action)
47 {
48 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH ||
49 action == TCA_MPLS_ACT_MODIFY;
50 }
51
52 static bool can_set_ethtype(unsigned int action)
53 {
54 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH ||
55 action == TCA_MPLS_ACT_POP;
56 }
57
58 static bool is_valid_label(__u32 label)
59 {
60 return label <= 0xfffff;
61 }
62
63 static 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
75 static int parse_mpls(struct action_util *a, int *argc_p, char ***argv_p,
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
94 while (argc > 0) {
95 if (matches(*argv, "pop") == 0) {
96 if (check_double_action(action, *argv))
97 return -1;
98 action = TCA_MPLS_ACT_POP;
99 } else if (matches(*argv, "push") == 0) {
100 if (check_double_action(action, *argv))
101 return -1;
102 action = TCA_MPLS_ACT_PUSH;
103 } else if (matches(*argv, "modify") == 0) {
104 if (check_double_action(action, *argv))
105 return -1;
106 action = TCA_MPLS_ACT_MODIFY;
107 } else if (matches(*argv, "mac_push") == 0) {
108 if (check_double_action(action, *argv))
109 return -1;
110 action = TCA_MPLS_ACT_MAC_PUSH;
111 } else if (matches(*argv, "dec_ttl") == 0) {
112 if (check_double_action(action, *argv))
113 return -1;
114 action = TCA_MPLS_ACT_DEC_TTL;
115 } else if (matches(*argv, "label") == 0) {
116 if (!can_modify_mpls_fields(action))
117 invarg("only valid for push, mac_push and modify",
118 *argv);
119 NEXT_ARG();
120 if (get_u32(&label, *argv, 0) || !is_valid_label(label))
121 invarg("label must be <=0xFFFFF", *argv);
122 } else if (matches(*argv, "tc") == 0) {
123 if (!can_modify_mpls_fields(action))
124 invarg("only valid for push, mac_push and modify",
125 *argv);
126 NEXT_ARG();
127 if (get_u8(&tc, *argv, 0) || (tc & ~0x7))
128 invarg("tc field is 3 bits max", *argv);
129 } else if (matches(*argv, "ttl") == 0) {
130 if (!can_modify_mpls_fields(action))
131 invarg("only valid for push, mac_push and modify",
132 *argv);
133 NEXT_ARG();
134 if (get_u8(&ttl, *argv, 0) || !ttl)
135 invarg("ttl must be >0 and <=255", *argv);
136 } else if (matches(*argv, "bos") == 0) {
137 if (!can_modify_mpls_fields(action))
138 invarg("only valid for push, mac_push and modify",
139 *argv);
140 NEXT_ARG();
141 if (get_u8(&bos, *argv, 0) || (bos & ~0x1))
142 invarg("bos must be 0 or 1", *argv);
143 } else if (matches(*argv, "protocol") == 0) {
144 if (!can_set_ethtype(action))
145 invarg("only valid for push, mac_push and pop",
146 *argv);
147 NEXT_ARG();
148 if (ll_proto_a2n(&proto, *argv))
149 invarg("protocol is invalid", *argv);
150 } else if (matches(*argv, "help") == 0) {
151 usage();
152 } else {
153 break;
154 }
155
156 NEXT_ARG_FWD();
157 }
158
159 if (!action)
160 incomplete_command();
161
162 parse_action_control_dflt(&argc, &argv, &parm.action,
163 false, TC_ACT_PIPE);
164
165 if (argc) {
166 if (matches(*argv, "index") == 0) {
167 NEXT_ARG();
168 if (get_u32(&parm.index, *argv, 10))
169 invarg("illegal index", *argv);
170 NEXT_ARG_FWD();
171 }
172 }
173
174 if (action == TCA_MPLS_ACT_PUSH && label == 0xffffffff)
175 missarg("label");
176
177 if ((action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH) &&
178 proto &&
179 proto != htons(ETH_P_MPLS_UC) && proto != htons(ETH_P_MPLS_MC)) {
180 fprintf(stderr,
181 "invalid %spush protocol \"0x%04x\" - use mpls_(uc|mc)\n",
182 action == TCA_MPLS_ACT_MAC_PUSH ? "mac_" : "",
183 ntohs(proto));
184 return -1;
185 }
186
187 if (action == TCA_MPLS_ACT_POP && !proto)
188 missarg("protocol");
189
190 parm.m_action = action;
191 tail = addattr_nest(n, MAX_MSG, tca_id | NLA_F_NESTED);
192 addattr_l(n, MAX_MSG, TCA_MPLS_PARMS, &parm, sizeof(parm));
193 if (label != 0xffffffff)
194 addattr_l(n, MAX_MSG, TCA_MPLS_LABEL, &label, sizeof(label));
195 if (proto)
196 addattr_l(n, MAX_MSG, TCA_MPLS_PROTO, &proto, sizeof(proto));
197 if (tc != 0xff)
198 addattr8(n, MAX_MSG, TCA_MPLS_TC, tc);
199 if (ttl)
200 addattr8(n, MAX_MSG, TCA_MPLS_TTL, ttl);
201 if (bos != 0xff)
202 addattr8(n, MAX_MSG, TCA_MPLS_BOS, bos);
203 addattr_nest_end(n, tail);
204
205 *argc_p = argc;
206 *argv_p = argv;
207 return 0;
208 }
209
210 static int print_mpls(struct action_util *au, FILE *f, struct rtattr *arg)
211 {
212 struct rtattr *tb[TCA_MPLS_MAX + 1];
213 struct tc_mpls *parm;
214 SPRINT_BUF(b1);
215 __u32 val;
216
217 print_string(PRINT_ANY, "kind", "%s ", "mpls");
218 if (!arg)
219 return 0;
220
221 parse_rtattr_nested(tb, TCA_MPLS_MAX, arg);
222
223 if (!tb[TCA_MPLS_PARMS]) {
224 fprintf(stderr, "[NULL mpls parameters]\n");
225 return -1;
226 }
227 parm = RTA_DATA(tb[TCA_MPLS_PARMS]);
228
229 print_string(PRINT_ANY, "mpls_action", " %s",
230 action_names[parm->m_action]);
231
232 switch (parm->m_action) {
233 case TCA_MPLS_ACT_POP:
234 if (tb[TCA_MPLS_PROTO]) {
235 __u16 proto;
236
237 proto = rta_getattr_u16(tb[TCA_MPLS_PROTO]);
238 print_string(PRINT_ANY, "protocol", " protocol %s",
239 ll_proto_n2a(proto, b1, sizeof(b1)));
240 }
241 break;
242 case TCA_MPLS_ACT_PUSH:
243 case TCA_MPLS_ACT_MAC_PUSH:
244 if (tb[TCA_MPLS_PROTO]) {
245 __u16 proto;
246
247 proto = rta_getattr_u16(tb[TCA_MPLS_PROTO]);
248 print_string(PRINT_ANY, "protocol", " protocol %s",
249 ll_proto_n2a(proto, b1, sizeof(b1)));
250 }
251 /* Fallthrough */
252 case TCA_MPLS_ACT_MODIFY:
253 if (tb[TCA_MPLS_LABEL]) {
254 val = rta_getattr_u32(tb[TCA_MPLS_LABEL]);
255 print_uint(PRINT_ANY, "label", " label %u", val);
256 }
257 if (tb[TCA_MPLS_TC]) {
258 val = rta_getattr_u8(tb[TCA_MPLS_TC]);
259 print_uint(PRINT_ANY, "tc", " tc %u", val);
260 }
261 if (tb[TCA_MPLS_BOS]) {
262 val = rta_getattr_u8(tb[TCA_MPLS_BOS]);
263 print_uint(PRINT_ANY, "bos", " bos %u", val);
264 }
265 if (tb[TCA_MPLS_TTL]) {
266 val = rta_getattr_u8(tb[TCA_MPLS_TTL]);
267 print_uint(PRINT_ANY, "ttl", " ttl %u", val);
268 }
269 break;
270 }
271 print_action_control(f, " ", parm->action, "");
272
273 print_nl();
274 print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
275 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
276 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
277
278 if (show_stats) {
279 if (tb[TCA_MPLS_TM]) {
280 struct tcf_t *tm = RTA_DATA(tb[TCA_MPLS_TM]);
281
282 print_tm(f, tm);
283 }
284 }
285
286 print_nl();
287
288 return 0;
289 }
290
291 struct action_util mpls_action_util = {
292 .id = "mpls",
293 .parse_aopt = parse_mpls,
294 .print_aopt = print_mpls,
295 };