1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Netronome Systems, Inc. */
4 #include <linux/if_ether.h>
5 #include <linux/tc_act/tc_mpls.h>
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",
22 static void explain(void)
25 "Usage: mpls pop [ protocol MPLS_PROTO ]\n"
26 " mpls push [ protocol MPLS_PROTO ] [ label MPLS_LABEL ] [ tc MPLS_TC ]\n"
27 " [ ttl MPLS_TTL ] [ bos MPLS_BOS ] [CONTROL]\n"
28 " mpls modify [ label MPLS_LABEL ] [ tc MPLS_TC ] [ ttl MPLS_TTL ] [CONTROL]\n"
29 " for pop MPLS_PROTO is next header of packet - e.g. ip or mpls_uc\n"
30 " for push MPLS_PROTO is one of mpls_uc or mpls_mc\n"
31 " with default: mpls_uc\n"
32 " CONTROL := reclassify | pipe | drop | continue | pass |\n"
33 " goto chain <CHAIN_INDEX>\n");
36 static void usage(void)
42 static bool can_modify_mpls_fields(unsigned int action)
44 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MODIFY;
47 static bool can_modify_ethtype(unsigned int action)
49 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_POP;
52 static bool is_valid_label(__u32 label)
54 return label <= 0xfffff;
57 static bool check_double_action(unsigned int action, const char *arg)
63 "Error: got \"%s\" but action already set to \"%s\"\n",
64 arg, action_names[action]);
69 static int parse_mpls(struct action_util *a, int *argc_p, char ***argv_p,
70 int tca_id, struct nlmsghdr *n)
72 struct tc_mpls parm = {};
73 __u32 label = 0xffffffff;
74 unsigned int action = 0;
75 char **argv = *argv_p;
83 if (matches(*argv, "mpls") != 0)
89 if (matches(*argv, "pop") == 0) {
90 if (check_double_action(action, *argv))
92 action = TCA_MPLS_ACT_POP;
93 } else if (matches(*argv, "push") == 0) {
94 if (check_double_action(action, *argv))
96 action = TCA_MPLS_ACT_PUSH;
97 } else if (matches(*argv, "modify") == 0) {
98 if (check_double_action(action, *argv))
100 action = TCA_MPLS_ACT_MODIFY;
101 } else if (matches(*argv, "dec_ttl") == 0) {
102 if (check_double_action(action, *argv))
104 action = TCA_MPLS_ACT_DEC_TTL;
105 } else if (matches(*argv, "label") == 0) {
106 if (!can_modify_mpls_fields(action))
107 invarg("only valid for push/modify", *argv);
109 if (get_u32(&label, *argv, 0) || !is_valid_label(label))
110 invarg("label must be <=0xFFFFF", *argv);
111 } else if (matches(*argv, "tc") == 0) {
112 if (!can_modify_mpls_fields(action))
113 invarg("only valid for push/modify", *argv);
115 if (get_u8(&tc, *argv, 0) || (tc & ~0x7))
116 invarg("tc field is 3 bits max", *argv);
117 } else if (matches(*argv, "ttl") == 0) {
118 if (!can_modify_mpls_fields(action))
119 invarg("only valid for push/modify", *argv);
121 if (get_u8(&ttl, *argv, 0) || !ttl)
122 invarg("ttl must be >0 and <=255", *argv);
123 } else if (matches(*argv, "bos") == 0) {
124 if (!can_modify_mpls_fields(action))
125 invarg("only valid for push/modify", *argv);
127 if (get_u8(&bos, *argv, 0) || (bos & ~0x1))
128 invarg("bos must be 0 or 1", *argv);
129 } else if (matches(*argv, "protocol") == 0) {
130 if (!can_modify_ethtype(action))
131 invarg("only valid for push/pop", *argv);
133 if (ll_proto_a2n(&proto, *argv))
134 invarg("protocol is invalid", *argv);
135 } else if (matches(*argv, "help") == 0) {
145 incomplete_command();
147 parse_action_control_dflt(&argc, &argv, &parm.action,
151 if (matches(*argv, "index") == 0) {
153 if (get_u32(&parm.index, *argv, 10))
154 invarg("illegal index", *argv);
159 if (action == TCA_MPLS_ACT_PUSH && !label)
162 if (action == TCA_MPLS_ACT_PUSH && proto &&
163 proto != htons(ETH_P_MPLS_UC) && proto != htons(ETH_P_MPLS_MC)) {
165 "invalid push protocol \"0x%04x\" - use mpls_(uc|mc)\n",
170 if (action == TCA_MPLS_ACT_POP && !proto)
173 parm.m_action = action;
174 tail = addattr_nest(n, MAX_MSG, tca_id | NLA_F_NESTED);
175 addattr_l(n, MAX_MSG, TCA_MPLS_PARMS, &parm, sizeof(parm));
176 if (label != 0xffffffff)
177 addattr_l(n, MAX_MSG, TCA_MPLS_LABEL, &label, sizeof(label));
179 addattr_l(n, MAX_MSG, TCA_MPLS_PROTO, &proto, sizeof(proto));
181 addattr8(n, MAX_MSG, TCA_MPLS_TC, tc);
183 addattr8(n, MAX_MSG, TCA_MPLS_TTL, ttl);
185 addattr8(n, MAX_MSG, TCA_MPLS_BOS, bos);
186 addattr_nest_end(n, tail);
193 static int print_mpls(struct action_util *au, FILE *f, struct rtattr *arg)
195 struct rtattr *tb[TCA_MPLS_MAX + 1];
196 struct tc_mpls *parm;
203 parse_rtattr_nested(tb, TCA_MPLS_MAX, arg);
205 if (!tb[TCA_MPLS_PARMS]) {
206 fprintf(stderr, "[NULL mpls parameters]\n");
209 parm = RTA_DATA(tb[TCA_MPLS_PARMS]);
211 print_string(PRINT_ANY, "kind", "%s ", "mpls");
212 print_string(PRINT_ANY, "mpls_action", " %s",
213 action_names[parm->m_action]);
215 switch (parm->m_action) {
216 case TCA_MPLS_ACT_POP:
217 if (tb[TCA_MPLS_PROTO]) {
220 proto = rta_getattr_u16(tb[TCA_MPLS_PROTO]);
221 print_string(PRINT_ANY, "protocol", " protocol %s",
222 ll_proto_n2a(proto, b1, sizeof(b1)));
225 case TCA_MPLS_ACT_PUSH:
226 if (tb[TCA_MPLS_PROTO]) {
229 proto = rta_getattr_u16(tb[TCA_MPLS_PROTO]);
230 print_string(PRINT_ANY, "protocol", " protocol %s",
231 ll_proto_n2a(proto, b1, sizeof(b1)));
234 case TCA_MPLS_ACT_MODIFY:
235 if (tb[TCA_MPLS_LABEL]) {
236 val = rta_getattr_u32(tb[TCA_MPLS_LABEL]);
237 print_uint(PRINT_ANY, "label", " label %u", val);
239 if (tb[TCA_MPLS_TC]) {
240 val = rta_getattr_u8(tb[TCA_MPLS_TC]);
241 print_uint(PRINT_ANY, "tc", " tc %u", val);
243 if (tb[TCA_MPLS_BOS]) {
244 val = rta_getattr_u8(tb[TCA_MPLS_BOS]);
245 print_uint(PRINT_ANY, "bos", " bos %u", val);
247 if (tb[TCA_MPLS_TTL]) {
248 val = rta_getattr_u8(tb[TCA_MPLS_TTL]);
249 print_uint(PRINT_ANY, "ttl", " ttl %u", val);
253 print_action_control(f, " ", parm->action, "");
255 print_uint(PRINT_ANY, "index", "\n\t index %u", parm->index);
256 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
257 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
260 if (tb[TCA_MPLS_TM]) {
261 struct tcf_t *tm = RTA_DATA(tb[TCA_MPLS_TM]);
267 print_string(PRINT_FP, NULL, "%s", _SL_);
272 struct action_util mpls_action_util = {
274 .parse_aopt = parse_mpls,
275 .print_aopt = print_mpls,