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(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)
95 if (matches(*argv
, "pop") == 0) {
96 if (check_double_action(action
, *argv
))
98 action
= TCA_MPLS_ACT_POP
;
99 } else if (matches(*argv
, "push") == 0) {
100 if (check_double_action(action
, *argv
))
102 action
= TCA_MPLS_ACT_PUSH
;
103 } else if (matches(*argv
, "modify") == 0) {
104 if (check_double_action(action
, *argv
))
106 action
= TCA_MPLS_ACT_MODIFY
;
107 } else if (matches(*argv
, "mac_push") == 0) {
108 if (check_double_action(action
, *argv
))
110 action
= TCA_MPLS_ACT_MAC_PUSH
;
111 } else if (matches(*argv
, "dec_ttl") == 0) {
112 if (check_double_action(action
, *argv
))
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",
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",
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",
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",
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",
148 if (ll_proto_a2n(&proto
, *argv
))
149 invarg("protocol is invalid", *argv
);
150 } else if (matches(*argv
, "help") == 0) {
160 incomplete_command();
162 parse_action_control_dflt(&argc
, &argv
, &parm
.action
,
166 if (matches(*argv
, "index") == 0) {
168 if (get_u32(&parm
.index
, *argv
, 10))
169 invarg("illegal index", *argv
);
174 if (action
== TCA_MPLS_ACT_PUSH
&& label
== 0xffffffff)
177 if ((action
== TCA_MPLS_ACT_PUSH
|| action
== TCA_MPLS_ACT_MAC_PUSH
) &&
179 proto
!= htons(ETH_P_MPLS_UC
) && proto
!= htons(ETH_P_MPLS_MC
)) {
181 "invalid %spush protocol \"0x%04x\" - use mpls_(uc|mc)\n",
182 action
== TCA_MPLS_ACT_MAC_PUSH
? "mac_" : "",
187 if (action
== TCA_MPLS_ACT_POP
&& !proto
)
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
));
196 addattr_l(n
, MAX_MSG
, TCA_MPLS_PROTO
, &proto
, sizeof(proto
));
198 addattr8(n
, MAX_MSG
, TCA_MPLS_TC
, tc
);
200 addattr8(n
, MAX_MSG
, TCA_MPLS_TTL
, ttl
);
202 addattr8(n
, MAX_MSG
, TCA_MPLS_BOS
, bos
);
203 addattr_nest_end(n
, tail
);
210 static int print_mpls(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
212 struct rtattr
*tb
[TCA_MPLS_MAX
+ 1];
213 struct tc_mpls
*parm
;
217 print_string(PRINT_ANY
, "kind", "%s ", "mpls");
221 parse_rtattr_nested(tb
, TCA_MPLS_MAX
, arg
);
223 if (!tb
[TCA_MPLS_PARMS
]) {
224 fprintf(stderr
, "[NULL mpls parameters]\n");
227 parm
= RTA_DATA(tb
[TCA_MPLS_PARMS
]);
229 print_string(PRINT_ANY
, "mpls_action", " %s",
230 action_names
[parm
->m_action
]);
232 switch (parm
->m_action
) {
233 case TCA_MPLS_ACT_POP
:
234 if (tb
[TCA_MPLS_PROTO
]) {
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
)));
242 case TCA_MPLS_ACT_PUSH
:
243 case TCA_MPLS_ACT_MAC_PUSH
:
244 if (tb
[TCA_MPLS_PROTO
]) {
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
)));
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
);
257 if (tb
[TCA_MPLS_TC
]) {
258 val
= rta_getattr_u8(tb
[TCA_MPLS_TC
]);
259 print_uint(PRINT_ANY
, "tc", " tc %u", val
);
261 if (tb
[TCA_MPLS_BOS
]) {
262 val
= rta_getattr_u8(tb
[TCA_MPLS_BOS
]);
263 print_uint(PRINT_ANY
, "bos", " bos %u", val
);
265 if (tb
[TCA_MPLS_TTL
]) {
266 val
= rta_getattr_u8(tb
[TCA_MPLS_TTL
]);
267 print_uint(PRINT_ANY
, "ttl", " ttl %u", val
);
271 print_action_control(f
, " ", parm
->action
, "");
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
);
279 if (tb
[TCA_MPLS_TM
]) {
280 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_MPLS_TM
]);
291 struct action_util mpls_action_util
= {
293 .parse_aopt
= parse_mpls
,
294 .print_aopt
= print_mpls
,