libbb: add strncpy_IFNAMSIZ
[oweals/busybox.git] / networking / libiproute / iptunnel.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * iptunnel.c          "ip tunnel"
4  *
5  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6  *
7  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8  *
9  *
10  * Changes:
11  *
12  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13  * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
14  * Phil Karn <karn@ka9q.ampr.org>       990408: "pmtudisc" flag
15  */
16
17 #include <netinet/ip.h>
18 #include <net/if.h>
19 #include <net/if_arp.h>
20 #include <asm/types.h>
21 #ifndef __constant_htons
22 #define __constant_htons htons
23 #endif
24 #include <linux/if_tunnel.h>
25
26 #include "ip_common.h"  /* #include "libbb.h" is inside */
27 #include "rt_names.h"
28 #include "utils.h"
29
30
31 /* Dies on error */
32 static int do_ioctl_get_ifindex(char *dev)
33 {
34         struct ifreq ifr;
35         int fd;
36
37         strncpy_IFNAMSIZ(ifr.ifr_name, dev);
38         fd = xsocket(AF_INET, SOCK_DGRAM, 0);
39         xioctl(fd, SIOCGIFINDEX, &ifr);
40         close(fd);
41         return ifr.ifr_ifindex;
42 }
43
44 static int do_ioctl_get_iftype(char *dev)
45 {
46         struct ifreq ifr;
47         int fd;
48         int err;
49
50         strncpy_IFNAMSIZ(ifr.ifr_name, dev);
51         fd = xsocket(AF_INET, SOCK_DGRAM, 0);
52         err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
53         close(fd);
54         return err ? -1 : ifr.ifr_addr.sa_family;
55 }
56
57 static char *do_ioctl_get_ifname(int idx)
58 {
59         struct ifreq ifr;
60         int fd;
61         int err;
62
63         ifr.ifr_ifindex = idx;
64         fd = xsocket(AF_INET, SOCK_DGRAM, 0);
65         err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
66         close(fd);
67         return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
68 }
69
70 static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
71 {
72         struct ifreq ifr;
73         int fd;
74         int err;
75
76         strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
77         ifr.ifr_ifru.ifru_data = (void*)p;
78         fd = xsocket(AF_INET, SOCK_DGRAM, 0);
79         err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
80         close(fd);
81         return err;
82 }
83
84 /* Dies on error, otherwise returns 0 */
85 static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
86 {
87         struct ifreq ifr;
88         int fd;
89
90         if (cmd == SIOCCHGTUNNEL && p->name[0]) {
91                 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
92         } else {
93                 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
94         }
95         ifr.ifr_ifru.ifru_data = (void*)p;
96         fd = xsocket(AF_INET, SOCK_DGRAM, 0);
97 #if ENABLE_IOCTL_HEX2STR_ERROR
98         /* #define magic will turn ioctl# into string */
99         if (cmd == SIOCCHGTUNNEL)
100                 xioctl(fd, SIOCCHGTUNNEL, &ifr);
101         else
102                 xioctl(fd, SIOCADDTUNNEL, &ifr);
103 #else
104         xioctl(fd, cmd, &ifr);
105 #endif
106         close(fd);
107         return 0;
108 }
109
110 /* Dies on error, otherwise returns 0 */
111 static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
112 {
113         struct ifreq ifr;
114         int fd;
115
116         if (p->name[0]) {
117                 strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
118         } else {
119                 strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
120         }
121         ifr.ifr_ifru.ifru_data = (void*)p;
122         fd = xsocket(AF_INET, SOCK_DGRAM, 0);
123         xioctl(fd, SIOCDELTUNNEL, &ifr);
124         close(fd);
125         return 0;
126 }
127
128 /* Dies on error */
129 static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
130 {
131         static const char keywords[] ALIGN1 =
132                 "mode\0""ipip\0""ip/ip\0""gre\0""gre/ip\0""sit\0""ipv6/ip\0"
133                 "key\0""ikey\0""okey\0""seq\0""iseq\0""oseq\0"
134                 "csum\0""icsum\0""ocsum\0""nopmtudisc\0""pmtudisc\0"
135                 "remote\0""any\0""local\0""dev\0"
136                 "ttl\0""inherit\0""tos\0""dsfield\0"
137                 "name\0";
138         enum {
139                 ARG_mode, ARG_ipip, ARG_ip_ip, ARG_gre, ARG_gre_ip, ARG_sit, ARG_ip6_ip,
140                 ARG_key, ARG_ikey, ARG_okey, ARG_seq, ARG_iseq, ARG_oseq,
141                 ARG_csum, ARG_icsum, ARG_ocsum, ARG_nopmtudisc, ARG_pmtudisc,
142                 ARG_remote, ARG_any, ARG_local, ARG_dev,
143                 ARG_ttl, ARG_inherit, ARG_tos, ARG_dsfield,
144                 ARG_name
145         };
146         int count = 0;
147         char medium[IFNAMSIZ];
148         int key;
149
150         memset(p, 0, sizeof(*p));
151         medium[0] = '\0';
152
153         p->iph.version = 4;
154         p->iph.ihl = 5;
155 #ifndef IP_DF
156 #define IP_DF 0x4000  /* Flag: "Don't Fragment" */
157 #endif
158         p->iph.frag_off = htons(IP_DF);
159
160         while (*argv) {
161                 key = index_in_strings(keywords, *argv);
162                 if (key == ARG_mode) {
163                         NEXT_ARG();
164                         key = index_in_strings(keywords, *argv);
165                         if (key == ARG_ipip ||
166                             key == ARG_ip_ip) {
167                                 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
168                                         bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
169                                 }
170                                 p->iph.protocol = IPPROTO_IPIP;
171                         } else if (key == ARG_gre ||
172                                    key == ARG_gre_ip) {
173                                 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
174                                         bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
175                                 }
176                                 p->iph.protocol = IPPROTO_GRE;
177                         } else if (key == ARG_sit ||
178                                    key == ARG_ip6_ip) {
179                                 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
180                                         bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one");
181                                 }
182                                 p->iph.protocol = IPPROTO_IPV6;
183                         } else {
184                                 bb_error_msg_and_die("%s tunnel mode", "cannot guess");
185                         }
186                 } else if (key == ARG_key) {
187                         unsigned uval;
188                         NEXT_ARG();
189                         p->i_flags |= GRE_KEY;
190                         p->o_flags |= GRE_KEY;
191                         if (strchr(*argv, '.'))
192                                 p->i_key = p->o_key = get_addr32(*argv);
193                         else {
194                                 if (get_unsigned(&uval, *argv, 0) < 0) {
195                                         invarg(*argv, "key");
196                                 }
197                                 p->i_key = p->o_key = htonl(uval);
198                         }
199                 } else if (key == ARG_ikey) {
200                         unsigned uval;
201                         NEXT_ARG();
202                         p->i_flags |= GRE_KEY;
203                         if (strchr(*argv, '.'))
204                                 p->o_key = get_addr32(*argv);
205                         else {
206                                 if (get_unsigned(&uval, *argv, 0) < 0) {
207                                         invarg(*argv, "ikey");
208                                 }
209                                 p->i_key = htonl(uval);
210                         }
211                 } else if (key == ARG_okey) {
212                         unsigned uval;
213                         NEXT_ARG();
214                         p->o_flags |= GRE_KEY;
215                         if (strchr(*argv, '.'))
216                                 p->o_key = get_addr32(*argv);
217                         else {
218                                 if (get_unsigned(&uval, *argv, 0) < 0) {
219                                         invarg(*argv, "okey");
220                                 }
221                                 p->o_key = htonl(uval);
222                         }
223                 } else if (key == ARG_seq) {
224                         p->i_flags |= GRE_SEQ;
225                         p->o_flags |= GRE_SEQ;
226                 } else if (key == ARG_iseq) {
227                         p->i_flags |= GRE_SEQ;
228                 } else if (key == ARG_oseq) {
229                         p->o_flags |= GRE_SEQ;
230                 } else if (key == ARG_csum) {
231                         p->i_flags |= GRE_CSUM;
232                         p->o_flags |= GRE_CSUM;
233                 } else if (key == ARG_icsum) {
234                         p->i_flags |= GRE_CSUM;
235                 } else if (key == ARG_ocsum) {
236                         p->o_flags |= GRE_CSUM;
237                 } else if (key == ARG_nopmtudisc) {
238                         p->iph.frag_off = 0;
239                 } else if (key == ARG_pmtudisc) {
240                         p->iph.frag_off = htons(IP_DF);
241                 } else if (key == ARG_remote) {
242                         NEXT_ARG();
243                         key = index_in_strings(keywords, *argv);
244                         if (key != ARG_any)
245                                 p->iph.daddr = get_addr32(*argv);
246                 } else if (key == ARG_local) {
247                         NEXT_ARG();
248                         key = index_in_strings(keywords, *argv);
249                         if (key != ARG_any)
250                                 p->iph.saddr = get_addr32(*argv);
251                 } else if (key == ARG_dev) {
252                         NEXT_ARG();
253                         strncpy_IFNAMSIZ(medium, *argv);
254                 } else if (key == ARG_ttl) {
255                         unsigned uval;
256                         NEXT_ARG();
257                         key = index_in_strings(keywords, *argv);
258                         if (key != ARG_inherit) {
259                                 if (get_unsigned(&uval, *argv, 0))
260                                         invarg(*argv, "TTL");
261                                 if (uval > 255)
262                                         invarg(*argv, "TTL must be <=255");
263                                 p->iph.ttl = uval;
264                         }
265                 } else if (key == ARG_tos ||
266                            key == ARG_dsfield) {
267                         uint32_t uval;
268                         NEXT_ARG();
269                         key = index_in_strings(keywords, *argv);
270                         if (key != ARG_inherit) {
271                                 if (rtnl_dsfield_a2n(&uval, *argv))
272                                         invarg(*argv, "TOS");
273                                 p->iph.tos = uval;
274                         } else
275                                 p->iph.tos = 1;
276                 } else {
277                         if (key == ARG_name) {
278                                 NEXT_ARG();
279                         }
280                         if (p->name[0])
281                                 duparg2("name", *argv);
282                         strncpy_IFNAMSIZ(p->name, *argv);
283                         if (cmd == SIOCCHGTUNNEL && count == 0) {
284                                 struct ip_tunnel_parm old_p;
285                                 memset(&old_p, 0, sizeof(old_p));
286                                 if (do_get_ioctl(*argv, &old_p))
287                                         exit(EXIT_FAILURE);
288                                 *p = old_p;
289                         }
290                 }
291                 count++;
292                 argv++;
293         }
294
295         if (p->iph.protocol == 0) {
296                 if (memcmp(p->name, "gre", 3) == 0)
297                         p->iph.protocol = IPPROTO_GRE;
298                 else if (memcmp(p->name, "ipip", 4) == 0)
299                         p->iph.protocol = IPPROTO_IPIP;
300                 else if (memcmp(p->name, "sit", 3) == 0)
301                         p->iph.protocol = IPPROTO_IPV6;
302         }
303
304         if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
305                 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
306                         bb_error_msg_and_die("keys are not allowed with ipip and sit");
307                 }
308         }
309
310         if (medium[0]) {
311                 p->link = do_ioctl_get_ifindex(medium);
312         }
313
314         if (p->i_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
315                 p->i_key = p->iph.daddr;
316                 p->i_flags |= GRE_KEY;
317         }
318         if (p->o_key == 0 && IN_MULTICAST(ntohl(p->iph.daddr))) {
319                 p->o_key = p->iph.daddr;
320                 p->o_flags |= GRE_KEY;
321         }
322         if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
323                 bb_error_msg_and_die("broadcast tunnel requires a source address");
324         }
325 }
326
327 /* Return value becomes exitcode. It's okay to not return at all */
328 static int do_add(int cmd, char **argv)
329 {
330         struct ip_tunnel_parm p;
331
332         parse_args(argv, cmd, &p);
333
334         if (p.iph.ttl && p.iph.frag_off == 0) {
335                 bb_error_msg_and_die("ttl != 0 and noptmudisc are incompatible");
336         }
337
338         switch (p.iph.protocol) {
339         case IPPROTO_IPIP:
340                 return do_add_ioctl(cmd, "tunl0", &p);
341         case IPPROTO_GRE:
342                 return do_add_ioctl(cmd, "gre0", &p);
343         case IPPROTO_IPV6:
344                 return do_add_ioctl(cmd, "sit0", &p);
345         default:
346                 bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
347         }
348 }
349
350 /* Return value becomes exitcode. It's okay to not return at all */
351 static int do_del(char **argv)
352 {
353         struct ip_tunnel_parm p;
354
355         parse_args(argv, SIOCDELTUNNEL, &p);
356
357         switch (p.iph.protocol) {
358         case IPPROTO_IPIP:
359                 return do_del_ioctl("tunl0", &p);
360         case IPPROTO_GRE:
361                 return do_del_ioctl("gre0", &p);
362         case IPPROTO_IPV6:
363                 return do_del_ioctl("sit0", &p);
364         default:
365                 return do_del_ioctl(p.name, &p);
366         }
367 }
368
369 static void print_tunnel(struct ip_tunnel_parm *p)
370 {
371         char s1[256];
372         char s2[256];
373         char s3[64];
374         char s4[64];
375
376         format_host(AF_INET, 4, &p->iph.daddr, s1, sizeof(s1));
377         format_host(AF_INET, 4, &p->iph.saddr, s2, sizeof(s2));
378         inet_ntop(AF_INET, &p->i_key, s3, sizeof(s3));
379         inet_ntop(AF_INET, &p->o_key, s4, sizeof(s4));
380
381         printf("%s: %s/ip  remote %s  local %s ",
382                p->name,
383                p->iph.protocol == IPPROTO_IPIP ? "ip" :
384                (p->iph.protocol == IPPROTO_GRE ? "gre" :
385                 (p->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")),
386                p->iph.daddr ? s1 : "any", p->iph.saddr ? s2 : "any");
387         if (p->link) {
388                 char *n = do_ioctl_get_ifname(p->link);
389                 if (n) {
390                         printf(" dev %s ", n);
391                         free(n);
392                 }
393         }
394         if (p->iph.ttl)
395                 printf(" ttl %d ", p->iph.ttl);
396         else
397                 printf(" ttl inherit ");
398         if (p->iph.tos) {
399                 SPRINT_BUF(b1);
400                 printf(" tos");
401                 if (p->iph.tos & 1)
402                         printf(" inherit");
403                 if (p->iph.tos & ~1)
404                         printf("%c%s ", p->iph.tos & 1 ? '/' : ' ',
405                                rtnl_dsfield_n2a(p->iph.tos & ~1, b1, sizeof(b1)));
406         }
407         if (!(p->iph.frag_off & htons(IP_DF)))
408                 printf(" nopmtudisc");
409
410         if ((p->i_flags & GRE_KEY) && (p->o_flags & GRE_KEY) && p->o_key == p->i_key)
411                 printf(" key %s", s3);
412         else if ((p->i_flags | p->o_flags) & GRE_KEY) {
413                 if (p->i_flags & GRE_KEY)
414                         printf(" ikey %s ", s3);
415                 if (p->o_flags & GRE_KEY)
416                         printf(" okey %s ", s4);
417         }
418
419         if (p->i_flags & GRE_SEQ)
420                 printf("%c  Drop packets out of sequence.\n", _SL_);
421         if (p->i_flags & GRE_CSUM)
422                 printf("%c  Checksum in received packet is required.", _SL_);
423         if (p->o_flags & GRE_SEQ)
424                 printf("%c  Sequence packets on output.", _SL_);
425         if (p->o_flags & GRE_CSUM)
426                 printf("%c  Checksum output packets.", _SL_);
427 }
428
429 static void do_tunnels_list(struct ip_tunnel_parm *p)
430 {
431         char name[IFNAMSIZ];
432         unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
433                 rx_fifo, rx_frame,
434                 tx_bytes, tx_packets, tx_errs, tx_drops,
435                 tx_fifo, tx_colls, tx_carrier, rx_multi;
436         int type;
437         struct ip_tunnel_parm p1;
438         char buf[512];
439         FILE *fp = fopen_or_warn("/proc/net/dev", "r");
440
441         if (fp == NULL) {
442                 return;
443         }
444         /* skip headers */
445         fgets(buf, sizeof(buf), fp);
446         fgets(buf, sizeof(buf), fp);
447
448         while (fgets(buf, sizeof(buf), fp) != NULL) {
449                 char *ptr;
450
451                 /*buf[sizeof(buf) - 1] = 0; - fgets is safe anyway */
452                 ptr = strchr(buf, ':');
453                 if (ptr == NULL ||
454                     (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
455                         bb_error_msg("wrong format of /proc/net/dev");
456                         return;
457                 }
458                 if (sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
459                            &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
460                            &rx_fifo, &rx_frame, &rx_multi,
461                            &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
462                            &tx_fifo, &tx_colls, &tx_carrier) != 14)
463                         continue;
464                 if (p->name[0] && strcmp(p->name, name))
465                         continue;
466                 type = do_ioctl_get_iftype(name);
467                 if (type == -1) {
468                         bb_error_msg("cannot get type of [%s]", name);
469                         continue;
470                 }
471                 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
472                         continue;
473                 memset(&p1, 0, sizeof(p1));
474                 if (do_get_ioctl(name, &p1))
475                         continue;
476                 if ((p->link && p1.link != p->link) ||
477                     (p->name[0] && strcmp(p1.name, p->name)) ||
478                     (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
479                     (p->iph.saddr && p1.iph.saddr != p->iph.saddr) ||
480                     (p->i_key && p1.i_key != p->i_key))
481                         continue;
482                 print_tunnel(&p1);
483                 bb_putchar('\n');
484         }
485 }
486
487 /* Return value becomes exitcode. It's okay to not return at all */
488 static int do_show(char **argv)
489 {
490         int err;
491         struct ip_tunnel_parm p;
492
493         parse_args(argv, SIOCGETTUNNEL, &p);
494
495         switch (p.iph.protocol) {
496         case IPPROTO_IPIP:
497                 err = do_get_ioctl(p.name[0] ? p.name : "tunl0", &p);
498                 break;
499         case IPPROTO_GRE:
500                 err = do_get_ioctl(p.name[0] ? p.name : "gre0", &p);
501                 break;
502         case IPPROTO_IPV6:
503                 err = do_get_ioctl(p.name[0] ? p.name : "sit0", &p);
504                 break;
505         default:
506                 do_tunnels_list(&p);
507                 return 0;
508         }
509         if (err)
510                 return -1;
511
512         print_tunnel(&p);
513         bb_putchar('\n');
514         return 0;
515 }
516
517 /* Return value becomes exitcode. It's okay to not return at all */
518 int do_iptunnel(char **argv)
519 {
520         static const char keywords[] ALIGN1 =
521                 "add\0""change\0""delete\0""show\0""list\0""lst\0";
522         enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
523         int key;
524
525         if (*argv) {
526                 key = index_in_substrings(keywords, *argv);
527                 if (key < 0)
528                         bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
529                 argv++;
530                 if (key == ARG_add)
531                         return do_add(SIOCADDTUNNEL, argv);
532                 if (key == ARG_change)
533                         return do_add(SIOCCHGTUNNEL, argv);
534                 if (key == ARG_del)
535                         return do_del(argv);
536         }
537         return do_show(argv);
538 }