]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iproute_lwtunnel.c
utils: make rt_addr_n2a() non-reentrant by default
[mirror_iproute2.git] / ip / iproute_lwtunnel.c
1 /*
2 * iproute_lwtunnel.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
10 * Thomas Graf <tgraf@suug.ch>
11 *
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <linux/ila.h>
20 #include <linux/lwtunnel.h>
21 #include <linux/mpls_iptunnel.h>
22 #include <errno.h>
23
24 #include "rt_names.h"
25 #include "utils.h"
26 #include "iproute_lwtunnel.h"
27
28 static int read_encap_type(const char *name)
29 {
30 if (strcmp(name, "mpls") == 0)
31 return LWTUNNEL_ENCAP_MPLS;
32 else if (strcmp(name, "ip") == 0)
33 return LWTUNNEL_ENCAP_IP;
34 else if (strcmp(name, "ip6") == 0)
35 return LWTUNNEL_ENCAP_IP6;
36 else if (strcmp(name, "ila") == 0)
37 return LWTUNNEL_ENCAP_ILA;
38 else
39 return LWTUNNEL_ENCAP_NONE;
40 }
41
42 static const char *format_encap_type(int type)
43 {
44 switch (type) {
45 case LWTUNNEL_ENCAP_MPLS:
46 return "mpls";
47 case LWTUNNEL_ENCAP_IP:
48 return "ip";
49 case LWTUNNEL_ENCAP_IP6:
50 return "ip6";
51 case LWTUNNEL_ENCAP_ILA:
52 return "ila";
53 default:
54 return "unknown";
55 }
56 }
57
58 static void print_encap_mpls(FILE *fp, struct rtattr *encap)
59 {
60 struct rtattr *tb[MPLS_IPTUNNEL_MAX+1];
61
62 parse_rtattr_nested(tb, MPLS_IPTUNNEL_MAX, encap);
63
64 if (tb[MPLS_IPTUNNEL_DST])
65 fprintf(fp, " %s ", format_host(AF_MPLS,
66 RTA_PAYLOAD(tb[MPLS_IPTUNNEL_DST]),
67 RTA_DATA(tb[MPLS_IPTUNNEL_DST])));
68 }
69
70 static void print_encap_ip(FILE *fp, struct rtattr *encap)
71 {
72 struct rtattr *tb[LWTUNNEL_IP_MAX+1];
73
74 parse_rtattr_nested(tb, LWTUNNEL_IP_MAX, encap);
75
76 if (tb[LWTUNNEL_IP_ID])
77 fprintf(fp, "id %llu ", ntohll(rta_getattr_u64(tb[LWTUNNEL_IP_ID])));
78
79 if (tb[LWTUNNEL_IP_SRC])
80 fprintf(fp, "src %s ",
81 rt_addr_n2a(AF_INET,
82 RTA_PAYLOAD(tb[LWTUNNEL_IP_SRC]),
83 RTA_DATA(tb[LWTUNNEL_IP_SRC])));
84
85 if (tb[LWTUNNEL_IP_DST])
86 fprintf(fp, "dst %s ",
87 rt_addr_n2a(AF_INET,
88 RTA_PAYLOAD(tb[LWTUNNEL_IP_DST]),
89 RTA_DATA(tb[LWTUNNEL_IP_DST])));
90
91 if (tb[LWTUNNEL_IP_TTL])
92 fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL]));
93
94 if (tb[LWTUNNEL_IP_TOS])
95 fprintf(fp, "tos %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TOS]));
96 }
97
98 static void print_encap_ila(FILE *fp, struct rtattr *encap)
99 {
100 struct rtattr *tb[ILA_ATTR_MAX+1];
101
102 parse_rtattr_nested(tb, ILA_ATTR_MAX, encap);
103
104 if (tb[ILA_ATTR_LOCATOR]) {
105 char abuf[ADDR64_BUF_SIZE];
106
107 addr64_n2a(*(__u64 *)RTA_DATA(tb[ILA_ATTR_LOCATOR]),
108 abuf, sizeof(abuf));
109 fprintf(fp, " %s ", abuf);
110 }
111 }
112
113 static void print_encap_ip6(FILE *fp, struct rtattr *encap)
114 {
115 struct rtattr *tb[LWTUNNEL_IP6_MAX+1];
116 char abuf[256];
117
118 parse_rtattr_nested(tb, LWTUNNEL_IP6_MAX, encap);
119
120 if (tb[LWTUNNEL_IP6_ID])
121 fprintf(fp, "id %llu ", ntohll(rta_getattr_u64(tb[LWTUNNEL_IP6_ID])));
122
123 if (tb[LWTUNNEL_IP6_SRC])
124 fprintf(fp, "src %s ",
125 rt_addr_n2a(AF_INET6,
126 RTA_PAYLOAD(tb[LWTUNNEL_IP6_SRC]),
127 RTA_DATA(tb[LWTUNNEL_IP6_SRC]),
128 abuf, sizeof(abuf)));
129
130 if (tb[LWTUNNEL_IP6_DST])
131 fprintf(fp, "dst %s ",
132 rt_addr_n2a(AF_INET6,
133 RTA_PAYLOAD(tb[LWTUNNEL_IP6_DST]),
134 RTA_DATA(tb[LWTUNNEL_IP6_DST]),
135 abuf, sizeof(abuf)));
136
137 if (tb[LWTUNNEL_IP6_HOPLIMIT])
138 fprintf(fp, "hoplimit %d ", rta_getattr_u8(tb[LWTUNNEL_IP6_HOPLIMIT]));
139
140 if (tb[LWTUNNEL_IP6_TC])
141 fprintf(fp, "tc %d ", rta_getattr_u8(tb[LWTUNNEL_IP6_TC]));
142 }
143
144 void lwt_print_encap(FILE *fp, struct rtattr *encap_type,
145 struct rtattr *encap)
146 {
147 int et;
148
149 if (!encap_type)
150 return;
151
152 et = rta_getattr_u16(encap_type);
153
154 fprintf(fp, " encap %s ", format_encap_type(et));
155
156 switch (et) {
157 case LWTUNNEL_ENCAP_MPLS:
158 print_encap_mpls(fp, encap);
159 break;
160 case LWTUNNEL_ENCAP_IP:
161 print_encap_ip(fp, encap);
162 break;
163 case LWTUNNEL_ENCAP_ILA:
164 print_encap_ila(fp, encap);
165 break;
166 case LWTUNNEL_ENCAP_IP6:
167 print_encap_ip6(fp, encap);
168 break;
169 }
170 }
171
172 static int parse_encap_mpls(struct rtattr *rta, size_t len, int *argcp, char ***argvp)
173 {
174 inet_prefix addr;
175 int argc = *argcp;
176 char **argv = *argvp;
177
178 if (get_addr(&addr, *argv, AF_MPLS)) {
179 fprintf(stderr, "Error: an inet address is expected rather than \"%s\".\n", *argv);
180 exit(1);
181 }
182
183 rta_addattr_l(rta, len, MPLS_IPTUNNEL_DST, &addr.data,
184 addr.bytelen);
185
186 *argcp = argc;
187 *argvp = argv;
188
189 return 0;
190 }
191
192 static int parse_encap_ip(struct rtattr *rta, size_t len, int *argcp, char ***argvp)
193 {
194 int id_ok = 0, dst_ok = 0, tos_ok = 0, ttl_ok = 0;
195 char **argv = *argvp;
196 int argc = *argcp;
197
198 while (argc > 0) {
199 if (strcmp(*argv, "id") == 0) {
200 __u64 id;
201
202 NEXT_ARG();
203 if (id_ok++)
204 duparg2("id", *argv);
205 if (get_u64(&id, *argv, 0))
206 invarg("\"id\" value is invalid\n", *argv);
207 rta_addattr64(rta, len, LWTUNNEL_IP_ID, htonll(id));
208 } else if (strcmp(*argv, "dst") == 0) {
209 inet_prefix addr;
210
211 NEXT_ARG();
212 if (dst_ok++)
213 duparg2("dst", *argv);
214 get_addr(&addr, *argv, AF_INET);
215 rta_addattr_l(rta, len, LWTUNNEL_IP_DST, &addr.data, addr.bytelen);
216 } else if (strcmp(*argv, "tos") == 0) {
217 __u32 tos;
218
219 NEXT_ARG();
220 if (tos_ok++)
221 duparg2("tos", *argv);
222 if (rtnl_dsfield_a2n(&tos, *argv))
223 invarg("\"tos\" value is invalid\n", *argv);
224 rta_addattr8(rta, len, LWTUNNEL_IP_TOS, tos);
225 } else if (strcmp(*argv, "ttl") == 0) {
226 __u8 ttl;
227
228 NEXT_ARG();
229 if (ttl_ok++)
230 duparg2("ttl", *argv);
231 if (get_u8(&ttl, *argv, 0))
232 invarg("\"ttl\" value is invalid\n", *argv);
233 rta_addattr8(rta, len, LWTUNNEL_IP_TTL, ttl);
234 } else {
235 break;
236 }
237 argc--; argv++;
238 }
239
240 /* argv is currently the first unparsed argument,
241 * but the lwt_parse_encap() caller will move to the next,
242 * so step back */
243 *argcp = argc + 1;
244 *argvp = argv - 1;
245
246 return 0;
247 }
248
249 static int parse_encap_ila(struct rtattr *rta, size_t len,
250 int *argcp, char ***argvp)
251 {
252 __u64 locator;
253 int argc = *argcp;
254 char **argv = *argvp;
255
256 if (get_addr64(&locator, *argv) < 0) {
257 fprintf(stderr, "Bad locator: %s\n", *argv);
258 exit(1);
259 }
260
261 rta_addattr64(rta, 1024, ILA_ATTR_LOCATOR, locator);
262
263 *argcp = argc;
264 *argvp = argv;
265
266 return 0;
267 }
268
269 static int parse_encap_ip6(struct rtattr *rta, size_t len, int *argcp, char ***argvp)
270 {
271 int id_ok = 0, dst_ok = 0, tos_ok = 0, ttl_ok = 0;
272 char **argv = *argvp;
273 int argc = *argcp;
274
275 while (argc > 0) {
276 if (strcmp(*argv, "id") == 0) {
277 __u64 id;
278
279 NEXT_ARG();
280 if (id_ok++)
281 duparg2("id", *argv);
282 if (get_u64(&id, *argv, 0))
283 invarg("\"id\" value is invalid\n", *argv);
284 rta_addattr64(rta, len, LWTUNNEL_IP6_ID, htonll(id));
285 } else if (strcmp(*argv, "dst") == 0) {
286 inet_prefix addr;
287
288 NEXT_ARG();
289 if (dst_ok++)
290 duparg2("dst", *argv);
291 get_addr(&addr, *argv, AF_INET6);
292 rta_addattr_l(rta, len, LWTUNNEL_IP6_DST, &addr.data, addr.bytelen);
293 } else if (strcmp(*argv, "tc") == 0) {
294 __u32 tc;
295
296 NEXT_ARG();
297 if (tos_ok++)
298 duparg2("tc", *argv);
299 if (rtnl_dsfield_a2n(&tc, *argv))
300 invarg("\"tc\" value is invalid\n", *argv);
301 rta_addattr8(rta, len, LWTUNNEL_IP6_TC, tc);
302 } else if (strcmp(*argv, "hoplimit") == 0) {
303 __u8 hoplimit;
304
305 NEXT_ARG();
306 if (ttl_ok++)
307 duparg2("hoplimit", *argv);
308 if (get_u8(&hoplimit, *argv, 0))
309 invarg("\"hoplimit\" value is invalid\n", *argv);
310 rta_addattr8(rta, len, LWTUNNEL_IP6_HOPLIMIT, hoplimit);
311 } else {
312 break;
313 }
314 argc--; argv++;
315 }
316
317 /* argv is currently the first unparsed argument,
318 * but the lwt_parse_encap() caller will move to the next,
319 * so step back */
320 *argcp = argc + 1;
321 *argvp = argv - 1;
322
323 return 0;
324 }
325
326 int lwt_parse_encap(struct rtattr *rta, size_t len, int *argcp, char ***argvp)
327 {
328 struct rtattr *nest;
329 int argc = *argcp;
330 char **argv = *argvp;
331 __u16 type;
332
333 NEXT_ARG();
334 type = read_encap_type(*argv);
335 if (!type)
336 invarg("\"encap type\" value is invalid\n", *argv);
337
338 NEXT_ARG();
339 if (argc <= 1) {
340 fprintf(stderr, "Error: unexpected end of line after \"encap\"\n");
341 exit(-1);
342 }
343
344 nest = rta_nest(rta, 1024, RTA_ENCAP);
345 switch (type) {
346 case LWTUNNEL_ENCAP_MPLS:
347 parse_encap_mpls(rta, len, &argc, &argv);
348 break;
349 case LWTUNNEL_ENCAP_IP:
350 parse_encap_ip(rta, len, &argc, &argv);
351 break;
352 case LWTUNNEL_ENCAP_ILA:
353 parse_encap_ila(rta, len, &argc, &argv);
354 break;
355 case LWTUNNEL_ENCAP_IP6:
356 parse_encap_ip6(rta, len, &argc, &argv);
357 break;
358 default:
359 fprintf(stderr, "Error: unsupported encap type\n");
360 break;
361 }
362 rta_nest_end(rta, nest);
363
364 rta_addattr16(rta, 1024, RTA_ENCAP_TYPE, type);
365
366 *argcp = argc;
367 *argvp = argv;
368
369 return 0;
370 }