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",
20 [TCA_MPLS_ACT_MAC_PUSH
] = "mac_push",
23 static void explain(void)
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");
40 static void usage(void)
46 static bool can_modify_mpls_fields(unsigned int action
)
48 return action
== TCA_MPLS_ACT_PUSH
|| action
== TCA_MPLS_ACT_MAC_PUSH
||
49 action
== TCA_MPLS_ACT_MODIFY
;
52 static bool can_set_ethtype(unsigned int action
)
54 return action
== TCA_MPLS_ACT_PUSH
|| action
== TCA_MPLS_ACT_MAC_PUSH
||
55 action
== TCA_MPLS_ACT_POP
;
58 static bool is_valid_label(__u32 label
)
60 return label
<= 0xfffff;
63 static bool check_double_action(unsigned int action
, const char *arg
)
69 "Error: got \"%s\" but action already set to \"%s\"\n",
70 arg
, action_names
[action
]);
75 static int parse_mpls(const struct action_util
*a
, int *argc_p
, char ***argv_p
,
76 int tca_id
, struct nlmsghdr
*n
)
78 struct tc_mpls parm
= {};
79 __u32 label
= 0xffffffff;
80 unsigned int action
= 0;
81 char **argv
= *argv_p
;
89 if (matches(*argv
, "mpls") != 0)
94 if (strcmp(*argv
, "index") == 0)
98 if (matches(*argv
, "pop") == 0) {
99 if (check_double_action(action
, *argv
))
101 action
= TCA_MPLS_ACT_POP
;
102 } else if (matches(*argv
, "push") == 0) {
103 if (check_double_action(action
, *argv
))
105 action
= TCA_MPLS_ACT_PUSH
;
106 } else if (matches(*argv
, "modify") == 0) {
107 if (check_double_action(action
, *argv
))
109 action
= TCA_MPLS_ACT_MODIFY
;
110 } else if (matches(*argv
, "mac_push") == 0) {
111 if (check_double_action(action
, *argv
))
113 action
= TCA_MPLS_ACT_MAC_PUSH
;
114 } else if (matches(*argv
, "dec_ttl") == 0) {
115 if (check_double_action(action
, *argv
))
117 action
= TCA_MPLS_ACT_DEC_TTL
;
118 } else if (matches(*argv
, "label") == 0) {
119 if (!can_modify_mpls_fields(action
))
120 invarg("only valid for push, mac_push and modify",
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
))
127 invarg("only valid for push, mac_push and modify",
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
))
134 invarg("only valid for push, mac_push and modify",
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
))
141 invarg("only valid for push, mac_push and modify",
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) {
147 if (!can_set_ethtype(action
))
148 invarg("only valid for push, mac_push and pop",
151 if (ll_proto_a2n(&proto
, *argv
))
152 invarg("protocol is invalid", *argv
);
153 } else if (matches(*argv
, "help") == 0) {
163 incomplete_command();
165 parse_action_control_dflt(&argc
, &argv
, &parm
.action
,
169 if (matches(*argv
, "index") == 0) {
172 if (get_u32(&parm
.index
, *argv
, 10))
173 invarg("illegal index", *argv
);
178 if (action
== TCA_MPLS_ACT_PUSH
&& label
== 0xffffffff)
181 if ((action
== TCA_MPLS_ACT_PUSH
|| action
== TCA_MPLS_ACT_MAC_PUSH
) &&
183 proto
!= htons(ETH_P_MPLS_UC
) && proto
!= htons(ETH_P_MPLS_MC
)) {
185 "invalid %spush protocol \"0x%04x\" - use mpls_(uc|mc)\n",
186 action
== TCA_MPLS_ACT_MAC_PUSH
? "mac_" : "",
191 if (action
== TCA_MPLS_ACT_POP
&& !proto
)
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
));
200 addattr_l(n
, MAX_MSG
, TCA_MPLS_PROTO
, &proto
, sizeof(proto
));
202 addattr8(n
, MAX_MSG
, TCA_MPLS_TC
, tc
);
204 addattr8(n
, MAX_MSG
, TCA_MPLS_TTL
, ttl
);
206 addattr8(n
, MAX_MSG
, TCA_MPLS_BOS
, bos
);
207 addattr_nest_end(n
, tail
);
214 static int print_mpls(const struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
216 struct rtattr
*tb
[TCA_MPLS_MAX
+ 1];
217 struct tc_mpls
*parm
;
221 print_string(PRINT_ANY
, "kind", "%s ", "mpls");
225 parse_rtattr_nested(tb
, TCA_MPLS_MAX
, arg
);
227 if (!tb
[TCA_MPLS_PARMS
]) {
228 fprintf(stderr
, "[NULL mpls parameters]\n");
231 parm
= RTA_DATA(tb
[TCA_MPLS_PARMS
]);
233 print_string(PRINT_ANY
, "mpls_action", " %s",
234 action_names
[parm
->m_action
]);
236 switch (parm
->m_action
) {
237 case TCA_MPLS_ACT_POP
:
238 if (tb
[TCA_MPLS_PROTO
]) {
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
)));
246 case TCA_MPLS_ACT_PUSH
:
247 case TCA_MPLS_ACT_MAC_PUSH
:
248 if (tb
[TCA_MPLS_PROTO
]) {
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
)));
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
);
261 if (tb
[TCA_MPLS_TC
]) {
262 val
= rta_getattr_u8(tb
[TCA_MPLS_TC
]);
263 print_uint(PRINT_ANY
, "tc", " tc %u", val
);
265 if (tb
[TCA_MPLS_BOS
]) {
266 val
= rta_getattr_u8(tb
[TCA_MPLS_BOS
]);
267 print_uint(PRINT_ANY
, "bos", " bos %u", val
);
269 if (tb
[TCA_MPLS_TTL
]) {
270 val
= rta_getattr_u8(tb
[TCA_MPLS_TTL
]);
271 print_uint(PRINT_ANY
, "ttl", " ttl %u", val
);
275 print_action_control(f
, " ", parm
->action
, "");
278 print_uint(PRINT_ANY
, "index", "\t index %u", parm
->index
);
279 print_int(PRINT_ANY
, "ref", " ref %d", parm
->refcnt
);
280 print_int(PRINT_ANY
, "bind", " bind %d", parm
->bindcnt
);
283 if (tb
[TCA_MPLS_TM
]) {
284 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_MPLS_TM
]);
295 struct action_util mpls_action_util
= {
297 .parse_aopt
= parse_mpls
,
298 .print_aopt
= print_mpls
,