]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_mpls.c
tc_util: detect overflow in get_size
[mirror_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",
20};
21
22static void explain(void)
23{
24 fprintf(stderr,
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");
34}
35
36static void usage(void)
37{
38 explain();
39 exit(-1);
40}
41
42static bool can_modify_mpls_fields(unsigned int action)
43{
44 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MODIFY;
45}
46
47static bool can_modify_ethtype(unsigned int action)
48{
49 return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_POP;
50}
51
52static bool is_valid_label(__u32 label)
53{
54 return label <= 0xfffff;
55}
56
57static bool check_double_action(unsigned int action, const char *arg)
58{
59 if (!action)
60 return false;
61
62 fprintf(stderr,
63 "Error: got \"%s\" but action already set to \"%s\"\n",
64 arg, action_names[action]);
65 explain();
66 return true;
67}
68
69static int parse_mpls(struct action_util *a, int *argc_p, char ***argv_p,
70 int tca_id, struct nlmsghdr *n)
71{
72 struct tc_mpls parm = {};
73 __u32 label = 0xffffffff;
74 unsigned int action = 0;
75 char **argv = *argv_p;
76 struct rtattr *tail;
77 int argc = *argc_p;
78 __u16 proto = 0;
79 __u8 bos = 0xff;
80 __u8 tc = 0xff;
81 __u8 ttl = 0;
82
83 if (matches(*argv, "mpls") != 0)
84 return -1;
85
86 NEXT_ARG();
87
88 while (argc > 0) {
89 if (matches(*argv, "pop") == 0) {
90 if (check_double_action(action, *argv))
91 return -1;
92 action = TCA_MPLS_ACT_POP;
93 } else if (matches(*argv, "push") == 0) {
94 if (check_double_action(action, *argv))
95 return -1;
96 action = TCA_MPLS_ACT_PUSH;
97 } else if (matches(*argv, "modify") == 0) {
98 if (check_double_action(action, *argv))
99 return -1;
100 action = TCA_MPLS_ACT_MODIFY;
101 } else if (matches(*argv, "dec_ttl") == 0) {
102 if (check_double_action(action, *argv))
103 return -1;
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);
108 NEXT_ARG();
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);
114 NEXT_ARG();
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);
120 NEXT_ARG();
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);
126 NEXT_ARG();
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);
132 NEXT_ARG();
133 if (ll_proto_a2n(&proto, *argv))
134 invarg("protocol is invalid", *argv);
135 } else if (matches(*argv, "help") == 0) {
136 usage();
137 } else {
138 break;
139 }
140
141 NEXT_ARG_FWD();
142 }
143
144 if (!action)
145 incomplete_command();
146
147 parse_action_control_dflt(&argc, &argv, &parm.action,
148 false, TC_ACT_PIPE);
149
150 if (argc) {
151 if (matches(*argv, "index") == 0) {
152 NEXT_ARG();
153 if (get_u32(&parm.index, *argv, 10))
154 invarg("illegal index", *argv);
155 NEXT_ARG_FWD();
156 }
157 }
158
72cc0baf 159 if (action == TCA_MPLS_ACT_PUSH && label == 0xffffffff)
fb57b092
JH
160 missarg("label");
161
162 if (action == TCA_MPLS_ACT_PUSH && proto &&
163 proto != htons(ETH_P_MPLS_UC) && proto != htons(ETH_P_MPLS_MC)) {
164 fprintf(stderr,
165 "invalid push protocol \"0x%04x\" - use mpls_(uc|mc)\n",
166 ntohs(proto));
167 return -1;
168 }
169
170 if (action == TCA_MPLS_ACT_POP && !proto)
171 missarg("protocol");
172
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));
178 if (proto)
179 addattr_l(n, MAX_MSG, TCA_MPLS_PROTO, &proto, sizeof(proto));
180 if (tc != 0xff)
181 addattr8(n, MAX_MSG, TCA_MPLS_TC, tc);
182 if (ttl)
183 addattr8(n, MAX_MSG, TCA_MPLS_TTL, ttl);
184 if (bos != 0xff)
185 addattr8(n, MAX_MSG, TCA_MPLS_BOS, bos);
186 addattr_nest_end(n, tail);
187
188 *argc_p = argc;
189 *argv_p = argv;
190 return 0;
191}
192
193static int print_mpls(struct action_util *au, FILE *f, struct rtattr *arg)
194{
195 struct rtattr *tb[TCA_MPLS_MAX + 1];
196 struct tc_mpls *parm;
197 SPRINT_BUF(b1);
198 __u32 val;
199
200 if (!arg)
201 return -1;
202
203 parse_rtattr_nested(tb, TCA_MPLS_MAX, arg);
204
205 if (!tb[TCA_MPLS_PARMS]) {
206 fprintf(stderr, "[NULL mpls parameters]\n");
207 return -1;
208 }
209 parm = RTA_DATA(tb[TCA_MPLS_PARMS]);
210
211 print_string(PRINT_ANY, "kind", "%s ", "mpls");
212 print_string(PRINT_ANY, "mpls_action", " %s",
213 action_names[parm->m_action]);
214
215 switch (parm->m_action) {
216 case TCA_MPLS_ACT_POP:
217 if (tb[TCA_MPLS_PROTO]) {
218 __u16 proto;
219
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)));
223 }
224 break;
225 case TCA_MPLS_ACT_PUSH:
226 if (tb[TCA_MPLS_PROTO]) {
227 __u16 proto;
228
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)));
232 }
233 /* Fallthrough */
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);
238 }
239 if (tb[TCA_MPLS_TC]) {
240 val = rta_getattr_u8(tb[TCA_MPLS_TC]);
241 print_uint(PRINT_ANY, "tc", " tc %u", val);
242 }
243 if (tb[TCA_MPLS_BOS]) {
244 val = rta_getattr_u8(tb[TCA_MPLS_BOS]);
245 print_uint(PRINT_ANY, "bos", " bos %u", val);
246 }
247 if (tb[TCA_MPLS_TTL]) {
248 val = rta_getattr_u8(tb[TCA_MPLS_TTL]);
249 print_uint(PRINT_ANY, "ttl", " ttl %u", val);
250 }
251 break;
252 }
253 print_action_control(f, " ", parm->action, "");
254
7b0d424a
SH
255 print_nl();
256 print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
fb57b092
JH
257 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
258 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
259
260 if (show_stats) {
261 if (tb[TCA_MPLS_TM]) {
262 struct tcf_t *tm = RTA_DATA(tb[TCA_MPLS_TM]);
263
264 print_tm(f, tm);
265 }
266 }
267
268 print_string(PRINT_FP, NULL, "%s", _SL_);
269
270 return 0;
271}
272
273struct action_util mpls_action_util = {
274 .id = "mpls",
275 .parse_aopt = parse_mpls,
276 .print_aopt = print_mpls,
277};