]>
Commit | Line | Data |
---|---|---|
d9bd1bd9 MN |
1 | /* |
2 | * Copyright (C)2006 USAGI/WIDE Project | |
ae665a52 | 3 | * |
d9bd1bd9 MN |
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. | |
ae665a52 | 8 | * |
d9bd1bd9 MN |
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. | |
ae665a52 | 13 | * |
d9bd1bd9 | 14 | * You should have received a copy of the GNU General Public License |
4d98ab00 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
d9bd1bd9 MN |
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> | |
3979ef91 | 28 | #include <errno.h> |
d9bd1bd9 MN |
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; | |
0b959b0f YH |
54 | case 0: |
55 | strcpy(buf, "any"); | |
56 | break; | |
d9bd1bd9 MN |
57 | default: |
58 | strcpy(buf, "unknown"); | |
59 | break; | |
60 | } | |
61 | ||
62 | return buf; | |
63 | } | |
64 | ||
d9bd1bd9 MN |
65 | int 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) | |
14645ec2 | 76 | fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev, |
ea71beac SH |
77 | strerror(errno)); |
78 | ||
d9bd1bd9 MN |
79 | close(fd); |
80 | return err; | |
81 | } | |
82 | ||
83 | int 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) | |
14645ec2 | 97 | fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name, |
ea71beac | 98 | strerror(errno)); |
d9bd1bd9 MN |
99 | close(fd); |
100 | return err; | |
101 | } | |
102 | ||
103 | int 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); | |
ea71beac | 113 | |
d9bd1bd9 MN |
114 | ifr.ifr_ifru.ifru_data = p; |
115 | fd = socket(preferred_family, SOCK_DGRAM, 0); | |
116 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); | |
117 | if (err) | |
14645ec2 | 118 | fprintf(stderr, "delete tunnel \"%s\" failed: %s\n", |
ea71beac | 119 | ifr.ifr_name, strerror(errno)); |
d9bd1bd9 MN |
120 | close(fd); |
121 | return err; | |
122 | } | |
a07e9912 | 123 | |
0612519e | 124 | static int tnl_gen_ioctl(int cmd, const char *name, |
ea71beac | 125 | void *p, int skiperr) |
a07e9912 SH |
126 | { |
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); | |
3979ef91 | 135 | if (err && errno != skiperr) |
ea71beac SH |
136 | fprintf(stderr, "%s: ioctl %x failed: %s\n", name, |
137 | cmd, strerror(errno)); | |
a07e9912 SH |
138 | close(fd); |
139 | return err; | |
140 | } | |
b88215c4 AC |
141 | |
142 | int tnl_prl_ioctl(int cmd, const char *name, void *p) | |
143 | { | |
3979ef91 | 144 | return tnl_gen_ioctl(cmd, name, p, -1); |
b88215c4 AC |
145 | } |
146 | ||
147 | int tnl_6rd_ioctl(int cmd, const char *name, void *p) | |
148 | { | |
3979ef91 | 149 | return tnl_gen_ioctl(cmd, name, p, -1); |
b88215c4 AC |
150 | } |
151 | ||
152 | int tnl_ioctl_get_6rd(const char *name, void *p) | |
153 | { | |
3979ef91 | 154 | return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL); |
b88215c4 | 155 | } |