blob: f163dad6049c46f1e0abce8aa6f558f39aece332 [file] [log] [blame]
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +09001/*
2 * Copyright (C)2006 USAGI/WIDE Project
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003 *
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +09004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Stephen Hemmingerae665a52006-12-05 10:10:22 -08008 *
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +09009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Stephen Hemmingerae665a52006-12-05 10:10:22 -080013 *
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090014 * You should have received a copy of the GNU General Public License
Stephen Hemminger4d98ab02013-12-06 15:05:07 -080015 * along with this program; if not, see <http://www.gnu.org/licenses>.
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090016 */
17/*
18 * split from ip_tunnel.c
19 */
20/*
21 * Author:
22 * Masahide NAKAMURA @USAGI
23 */
24
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
Alexandre Cassen3979ef92010-04-04 22:40:14 +020028#include <errno.h>
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090029#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/ioctl.h>
32#include <netinet/in.h>
33#include <linux/if.h>
34#include <linux/ip.h>
35#include <linux/if_tunnel.h>
36
37#include "utils.h"
38#include "tunnel.h"
39
40const char *tnl_strproto(__u8 proto)
41{
42 static char buf[16];
43
44 switch (proto) {
45 case IPPROTO_IPIP:
46 strcpy(buf, "ip");
47 break;
48 case IPPROTO_GRE:
49 strcpy(buf, "gre");
50 break;
51 case IPPROTO_IPV6:
52 strcpy(buf, "ipv6");
53 break;
YOSHIFUJI Hideaki / 吉藤英明0b959b02007-10-12 16:51:22 +090054 case 0:
55 strcpy(buf, "any");
56 break;
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090057 default:
58 strcpy(buf, "unknown");
59 break;
60 }
61
62 return buf;
63}
64
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090065int tnl_get_ioctl(const char *basedev, void *p)
66{
67 struct ifreq ifr;
68 int fd;
69 int err;
70
71 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
72 ifr.ifr_ifru.ifru_data = (void*)p;
73 fd = socket(preferred_family, SOCK_DGRAM, 0);
74 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
75 if (err)
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +000076 fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev,
Stephen Hemmingerea71bea2010-11-28 10:35:28 -080077 strerror(errno));
78
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090079 close(fd);
80 return err;
81}
82
83int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
84{
85 struct ifreq ifr;
86 int fd;
87 int err;
88
89 if (cmd == SIOCCHGTUNNEL && name[0])
90 strncpy(ifr.ifr_name, name, IFNAMSIZ);
91 else
92 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
93 ifr.ifr_ifru.ifru_data = p;
94 fd = socket(preferred_family, SOCK_DGRAM, 0);
95 err = ioctl(fd, cmd, &ifr);
96 if (err)
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +000097 fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name,
Stephen Hemmingerea71bea2010-11-28 10:35:28 -080098 strerror(errno));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +090099 close(fd);
100 return err;
101}
102
103int tnl_del_ioctl(const char *basedev, const char *name, void *p)
104{
105 struct ifreq ifr;
106 int fd;
107 int err;
108
109 if (name[0])
110 strncpy(ifr.ifr_name, name, IFNAMSIZ);
111 else
112 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800113
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900114 ifr.ifr_ifru.ifru_data = p;
115 fd = socket(preferred_family, SOCK_DGRAM, 0);
116 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
117 if (err)
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000118 fprintf(stderr, "delete tunnel \"%s\" failed: %s\n",
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800119 ifr.ifr_name, strerror(errno));
Masahide NAKAMURAd9bd1bd2006-11-24 12:27:03 +0900120 close(fd);
121 return err;
122}
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200123
Stephen Hemminger06125192014-02-17 10:55:31 -0800124static int tnl_gen_ioctl(int cmd, const char *name,
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800125 void *p, int skiperr)
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200126{
127 struct ifreq ifr;
128 int fd;
129 int err;
130
131 strncpy(ifr.ifr_name, name, IFNAMSIZ);
132 ifr.ifr_ifru.ifru_data = p;
133 fd = socket(preferred_family, SOCK_DGRAM, 0);
134 err = ioctl(fd, cmd, &ifr);
Alexandre Cassen3979ef92010-04-04 22:40:14 +0200135 if (err && errno != skiperr)
Stephen Hemmingerea71bea2010-11-28 10:35:28 -0800136 fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
137 cmd, strerror(errno));
Sascha Hlusiaka07e9912009-05-04 01:44:47 +0200138 close(fd);
139 return err;
140}
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000141
142int tnl_prl_ioctl(int cmd, const char *name, void *p)
143{
Alexandre Cassen3979ef92010-04-04 22:40:14 +0200144 return tnl_gen_ioctl(cmd, name, p, -1);
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000145}
146
147int tnl_6rd_ioctl(int cmd, const char *name, void *p)
148{
Alexandre Cassen3979ef92010-04-04 22:40:14 +0200149 return tnl_gen_ioctl(cmd, name, p, -1);
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000150}
151
152int tnl_ioctl_get_6rd(const char *name, void *p)
153{
Alexandre Cassen3979ef92010-04-04 22:40:14 +0200154 return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL);
Alexandre Cassenb88215c2009-12-16 02:38:29 +0000155}