]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/tunnel.c
tunnel: decode ESP tunnel type
[mirror_iproute2.git] / ip / tunnel.c
1 /*
2 * Copyright (C)2006 USAGI/WIDE Project
3 *
4 * 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.
8 *
9 * 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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses>.
16 */
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>
28 #include <errno.h>
29 #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
40 const 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;
54 case IPPROTO_ESP:
55 strcpy(buf, "esp");
56 break;
57 case 0:
58 strcpy(buf, "any");
59 break;
60 default:
61 strcpy(buf, "unknown");
62 break;
63 }
64
65 return buf;
66 }
67
68 int tnl_get_ioctl(const char *basedev, void *p)
69 {
70 struct ifreq ifr;
71 int fd;
72 int err;
73
74 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
75 ifr.ifr_ifru.ifru_data = (void*)p;
76 fd = socket(preferred_family, SOCK_DGRAM, 0);
77 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
78 if (err)
79 fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev,
80 strerror(errno));
81
82 close(fd);
83 return err;
84 }
85
86 int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
87 {
88 struct ifreq ifr;
89 int fd;
90 int err;
91
92 if (cmd == SIOCCHGTUNNEL && name[0])
93 strncpy(ifr.ifr_name, name, IFNAMSIZ);
94 else
95 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
96 ifr.ifr_ifru.ifru_data = p;
97 fd = socket(preferred_family, SOCK_DGRAM, 0);
98 err = ioctl(fd, cmd, &ifr);
99 if (err)
100 fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name,
101 strerror(errno));
102 close(fd);
103 return err;
104 }
105
106 int tnl_del_ioctl(const char *basedev, const char *name, void *p)
107 {
108 struct ifreq ifr;
109 int fd;
110 int err;
111
112 if (name[0])
113 strncpy(ifr.ifr_name, name, IFNAMSIZ);
114 else
115 strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
116
117 ifr.ifr_ifru.ifru_data = p;
118 fd = socket(preferred_family, SOCK_DGRAM, 0);
119 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
120 if (err)
121 fprintf(stderr, "delete tunnel \"%s\" failed: %s\n",
122 ifr.ifr_name, strerror(errno));
123 close(fd);
124 return err;
125 }
126
127 static int tnl_gen_ioctl(int cmd, const char *name,
128 void *p, int skiperr)
129 {
130 struct ifreq ifr;
131 int fd;
132 int err;
133
134 strncpy(ifr.ifr_name, name, IFNAMSIZ);
135 ifr.ifr_ifru.ifru_data = p;
136 fd = socket(preferred_family, SOCK_DGRAM, 0);
137 err = ioctl(fd, cmd, &ifr);
138 if (err && errno != skiperr)
139 fprintf(stderr, "%s: ioctl %x failed: %s\n", name,
140 cmd, strerror(errno));
141 close(fd);
142 return err;
143 }
144
145 int tnl_prl_ioctl(int cmd, const char *name, void *p)
146 {
147 return tnl_gen_ioctl(cmd, name, p, -1);
148 }
149
150 int tnl_6rd_ioctl(int cmd, const char *name, void *p)
151 {
152 return tnl_gen_ioctl(cmd, name, p, -1);
153 }
154
155 int tnl_ioctl_get_6rd(const char *name, void *p)
156 {
157 return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL);
158 }