Commit | Line | Data |
---|---|---|
9d3a4132 SE |
1 | #include "secnet.h" |
2 | #include "util.h" | |
3 | #include "netlink.h" | |
4 | #include <stdio.h> | |
5 | #include <string.h> | |
6 | #include <unistd.h> | |
7 | #include <fcntl.h> | |
8 | #include <sys/ioctl.h> | |
9 | ||
10 | #ifdef HAVE_LINUX_IF_H | |
11 | #include <linux/if.h> | |
12 | #include <linux/if_tun.h> | |
13 | #endif | |
14 | ||
c6f79b17 | 15 | /* Where do we find if_tun on other platforms? */ |
9d3a4132 SE |
16 | |
17 | /* Connection to the kernel through the universal TUN/TAP driver */ | |
18 | ||
19 | struct tun { | |
20 | struct netlink nl; | |
21 | int fd; | |
22 | string_t device_path; | |
23 | string_t interface_name; | |
24 | string_t ifconfig_path; | |
25 | string_t route_path; | |
26 | bool_t tun_old; | |
27 | bool_t search_for_if; /* Applies to tun-old only */ | |
28 | struct buffer_if *buff; /* We receive packets into here | |
29 | and send them to the netlink code. */ | |
30 | netlink_deliver_fn *netlink_to_tunnel; | |
b2a56f7c | 31 | uint32_t local_address; /* host interface address */ |
9d3a4132 SE |
32 | }; |
33 | ||
34 | static int tun_beforepoll(void *sst, struct pollfd *fds, int *nfds_io, | |
35 | int *timeout_io, const struct timeval *tv_now, | |
36 | uint64_t *now) | |
37 | { | |
38 | struct tun *st=sst; | |
39 | *nfds_io=1; | |
40 | fds[0].fd=st->fd; | |
41 | fds[0].events=POLLIN|POLLERR|POLLHUP; | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static void tun_afterpoll(void *sst, struct pollfd *fds, int nfds, | |
46 | const struct timeval *tv_now, uint64_t *now) | |
47 | { | |
48 | struct tun *st=sst; | |
49 | int l; | |
50 | ||
7138d0c5 | 51 | if (nfds==0) return; |
9d3a4132 SE |
52 | if (fds[0].revents&POLLERR) { |
53 | printf("tun_afterpoll: hup!\n"); | |
54 | } | |
55 | if (fds[0].revents&POLLIN) { | |
56 | BUF_ALLOC(st->buff,"tun_afterpoll"); | |
57 | buffer_init(st->buff,st->nl.max_start_pad); | |
58 | l=read(st->fd,st->buff->start,st->buff->len-st->nl.max_start_pad); | |
59 | if (l<0) { | |
60 | fatal_perror("tun_afterpoll: read()"); | |
61 | } | |
62 | if (l==0) { | |
63 | fatal("tun_afterpoll: read()=0; device gone away?\n"); | |
64 | } | |
65 | if (l>0) { | |
66 | st->buff->size=l; | |
469fd1d9 | 67 | st->netlink_to_tunnel(&st->nl,st->buff); |
9d3a4132 SE |
68 | BUF_ASSERT_FREE(st->buff); |
69 | } | |
70 | } | |
71 | } | |
72 | ||
469fd1d9 | 73 | static void tun_deliver_to_kernel(void *sst, struct buffer_if *buf) |
9d3a4132 SE |
74 | { |
75 | struct tun *st=sst; | |
76 | ||
77 | BUF_ASSERT_USED(buf); | |
c6f79b17 SE |
78 | /* No error checking, because we'd just throw the packet away |
79 | anyway if it didn't work. */ | |
9d3a4132 SE |
80 | write(st->fd,buf->start,buf->size); |
81 | BUF_FREE(buf); | |
82 | } | |
83 | ||
84 | static bool_t tun_set_route(void *sst, struct netlink_route *route) | |
85 | { | |
86 | struct tun *st=sst; | |
87 | string_t network, mask, secnetaddr; | |
88 | ||
89 | if (route->up != route->kup) { | |
90 | network=ipaddr_to_string(route->net.prefix); | |
91 | mask=ipaddr_to_string(route->net.mask); | |
92 | secnetaddr=ipaddr_to_string(st->nl.secnet_address); | |
93 | Message(M_INFO,"%s: %s route %s/%d %s kernel routing table\n", | |
94 | st->nl.name,route->up?"adding":"deleting",network, | |
95 | route->net.len,route->up?"to":"from"); | |
96 | sys_cmd(st->route_path,"route",route->up?"add":"del","-net",network, | |
97 | "netmask",mask,"gw",secnetaddr,(char *)0); | |
98 | free(network); free(mask); free(secnetaddr); | |
99 | route->kup=route->up; | |
100 | return True; | |
101 | } | |
102 | return False; | |
103 | } | |
104 | ||
105 | static void tun_phase_hook(void *sst, uint32_t newphase) | |
106 | { | |
107 | struct tun *st=sst; | |
108 | string_t hostaddr,secnetaddr; | |
109 | uint8_t mtu[6]; | |
110 | string_t network,mask; | |
111 | struct netlink_route *r; | |
112 | int i; | |
113 | ||
114 | if (st->tun_old) { | |
115 | if (st->search_for_if) { | |
116 | string_t dname; | |
117 | int i; | |
118 | ||
119 | /* ASSERT st->interface_name */ | |
120 | dname=safe_malloc(strlen(st->device_path)+4,"tun_old_apply"); | |
121 | st->interface_name=safe_malloc(8,"tun_phase_hook"); | |
122 | ||
123 | for (i=0; i<255; i++) { | |
124 | sprintf(dname,"%s%d",st->device_path,i); | |
125 | if ((st->fd=open(dname,O_RDWR))>0) { | |
126 | sprintf(st->interface_name,"tun%d",i); | |
127 | Message(M_INFO,"%s: allocated network interface %s " | |
128 | "through %s\n",st->nl.name,st->interface_name, | |
129 | dname); | |
130 | break; | |
131 | } | |
132 | } | |
133 | if (st->fd==-1) { | |
134 | fatal("%s: unable to open any TUN device (%s...)\n", | |
135 | st->nl.name,st->device_path); | |
136 | } | |
137 | } else { | |
138 | st->fd=open(st->device_path,O_RDWR); | |
139 | if (st->fd==-1) { | |
140 | fatal_perror("%s: unable to open TUN device file %s", | |
141 | st->nl.name,st->device_path); | |
142 | } | |
143 | } | |
144 | } else { | |
145 | #ifdef HAVE_LINUX_IF_H | |
146 | struct ifreq ifr; | |
147 | ||
148 | /* New TUN interface: open the device, then do ioctl TUNSETIFF | |
149 | to set or find out the network interface name. */ | |
150 | st->fd=open(st->device_path,O_RDWR); | |
151 | if (st->fd==-1) { | |
152 | fatal_perror("%s: can't open device file %s",st->nl.name, | |
153 | st->device_path); | |
154 | } | |
155 | memset(&ifr,0,sizeof(ifr)); | |
156 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Just send/receive IP packets, | |
157 | no extra headers */ | |
158 | if (st->interface_name) | |
159 | strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ); | |
b2a56f7c | 160 | Message(M_DEBUG,"%s: about to ioctl(TUNSETIFF)...\n",st->nl.name); |
9d3a4132 SE |
161 | if (ioctl(st->fd,TUNSETIFF,&ifr)<0) { |
162 | fatal_perror("%s: ioctl(TUNSETIFF)",st->nl.name); | |
163 | } | |
164 | if (!st->interface_name) { | |
165 | st->interface_name=safe_malloc(strlen(ifr.ifr_name)+1,"tun_apply"); | |
166 | strcpy(st->interface_name,ifr.ifr_name); | |
167 | Message(M_INFO,"%s: allocated network interface %s\n",st->nl.name, | |
168 | st->interface_name); | |
169 | } | |
170 | #else | |
171 | fatal("netlink.c:tun_phase_hook:!tun_old unexpected\n"); | |
172 | #endif /* HAVE_LINUX_IF_H */ | |
173 | } | |
174 | /* All the networks we'll be using have been registered. Invoke ifconfig | |
175 | to set the TUN device's address, and route to add routes to all | |
176 | our networks. */ | |
177 | ||
b2a56f7c | 178 | hostaddr=ipaddr_to_string(st->local_address); |
9d3a4132 SE |
179 | secnetaddr=ipaddr_to_string(st->nl.secnet_address); |
180 | snprintf(mtu,6,"%d",st->nl.mtu); | |
181 | mtu[5]=0; | |
182 | ||
183 | sys_cmd(st->ifconfig_path,"ifconfig",st->interface_name, | |
184 | hostaddr,"netmask","255.255.255.255","-broadcast", | |
185 | "pointopoint",secnetaddr,"mtu",mtu,"up",(char *)0); | |
186 | ||
187 | r=st->nl.routes; | |
188 | for (i=0; i<st->nl.n_routes; i++) { | |
189 | if (r[i].up && !r[i].kup) { | |
190 | network=ipaddr_to_string(r[i].net.prefix); | |
191 | mask=ipaddr_to_string(r[i].net.mask); | |
192 | sys_cmd(st->route_path,"route","add","-net",network, | |
193 | "netmask",mask,"gw",secnetaddr,(char *)0); | |
194 | r[i].kup=True; | |
195 | } | |
196 | } | |
197 | ||
198 | /* Register for poll() */ | |
199 | register_for_poll(st, tun_beforepoll, tun_afterpoll, 1, st->nl.name); | |
200 | } | |
201 | ||
202 | #ifdef HAVE_LINUX_IF_H | |
203 | static list_t *tun_apply(closure_t *self, struct cloc loc, dict_t *context, | |
204 | list_t *args) | |
205 | { | |
206 | struct tun *st; | |
207 | item_t *item; | |
208 | dict_t *dict; | |
209 | ||
210 | st=safe_malloc(sizeof(*st),"tun_apply"); | |
211 | ||
212 | /* First parameter must be a dict */ | |
213 | item=list_elem(args,0); | |
214 | if (!item || item->type!=t_dict) | |
215 | cfgfatal(loc,"tun","parameter must be a dictionary\n"); | |
216 | ||
217 | dict=item->data.dict; | |
218 | ||
219 | st->netlink_to_tunnel= | |
220 | netlink_init(&st->nl,st,loc,dict, | |
221 | "netlink-tun",tun_set_route,tun_deliver_to_kernel); | |
222 | ||
223 | st->tun_old=False; | |
224 | st->device_path=dict_read_string(dict,"device",False,"tun-netlink",loc); | |
225 | st->interface_name=dict_read_string(dict,"interface",False, | |
226 | "tun-netlink",loc); | |
227 | st->ifconfig_path=dict_read_string(dict,"ifconfig-path", | |
228 | False,"tun-netlink",loc); | |
229 | st->route_path=dict_read_string(dict,"route-path", | |
230 | False,"tun-netlink",loc); | |
231 | ||
232 | if (!st->device_path) st->device_path="/dev/net/tun"; | |
233 | if (!st->ifconfig_path) st->ifconfig_path="ifconfig"; | |
234 | if (!st->route_path) st->route_path="route"; | |
235 | st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"tun-netlink",loc); | |
794f2398 | 236 | st->local_address=string_item_to_ipaddr( |
b2a56f7c | 237 | dict_find_item(dict,"local-address", True, "netlink", loc),"netlink"); |
9d3a4132 SE |
238 | |
239 | add_hook(PHASE_GETRESOURCES,tun_phase_hook,st); | |
240 | ||
241 | return new_closure(&st->nl.cl); | |
242 | } | |
243 | #endif /* HAVE_LINUX_IF_H */ | |
244 | ||
245 | static list_t *tun_old_apply(closure_t *self, struct cloc loc, dict_t *context, | |
246 | list_t *args) | |
247 | { | |
248 | struct tun *st; | |
249 | item_t *item; | |
250 | dict_t *dict; | |
251 | ||
252 | st=safe_malloc(sizeof(*st),"tun_old_apply"); | |
253 | ||
9d3a4132 SE |
254 | /* First parameter must be a dict */ |
255 | item=list_elem(args,0); | |
256 | if (!item || item->type!=t_dict) | |
257 | cfgfatal(loc,"tun","parameter must be a dictionary\n"); | |
258 | ||
259 | dict=item->data.dict; | |
260 | ||
261 | st->netlink_to_tunnel= | |
262 | netlink_init(&st->nl,st,loc,dict, | |
469fd1d9 | 263 | "netlink-tun",tun_set_route,tun_deliver_to_kernel); |
9d3a4132 SE |
264 | |
265 | st->tun_old=True; | |
266 | st->device_path=dict_read_string(dict,"device",False,"tun-netlink",loc); | |
267 | st->interface_name=dict_read_string(dict,"interface",False, | |
268 | "tun-netlink",loc); | |
269 | st->search_for_if=dict_read_bool(dict,"interface-search",False, | |
270 | "tun-netlink",loc,st->device_path==NULL); | |
271 | st->ifconfig_path=dict_read_string(dict,"ifconfig-path",False, | |
272 | "tun-netlink",loc); | |
273 | st->route_path=dict_read_string(dict,"route-path",False,"tun-netlink",loc); | |
274 | ||
275 | if (!st->device_path) st->device_path="/dev/tun"; | |
276 | if (!st->ifconfig_path) st->ifconfig_path="ifconfig"; | |
277 | if (!st->route_path) st->route_path="route"; | |
278 | st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"tun-netlink",loc); | |
794f2398 | 279 | st->local_address=string_item_to_ipaddr( |
b2a56f7c | 280 | dict_find_item(dict,"local-address", True, "netlink", loc),"netlink"); |
9d3a4132 SE |
281 | |
282 | /* Old TUN interface: the network interface name depends on which | |
283 | /dev/tunX file we open. If 'interface-search' is set to true, treat | |
284 | 'device' as the prefix and try numbers from 0--255. If it's set | |
285 | to false, treat 'device' as the whole name, and require than an | |
286 | appropriate interface name be specified. */ | |
287 | if (st->search_for_if && st->interface_name) { | |
288 | cfgfatal(loc,"tun-old","you may not specify an interface name " | |
289 | "in interface-search mode\n"); | |
290 | } | |
291 | if (!st->search_for_if && !st->interface_name) { | |
292 | cfgfatal(loc,"tun-old","you must specify an interface name " | |
293 | "when you explicitly specify a TUN device file\n"); | |
294 | } | |
295 | ||
296 | ||
297 | add_hook(PHASE_GETRESOURCES,tun_phase_hook,st); | |
298 | ||
299 | return new_closure(&st->nl.cl); | |
300 | } | |
301 | ||
302 | init_module tun_module; | |
303 | void tun_module(dict_t *dict) | |
304 | { | |
305 | #ifdef HAVE_LINUX_IF_H | |
306 | add_closure(dict,"tun",tun_apply); | |
307 | #endif | |
308 | add_closure(dict,"tun-old",tun_old_apply); | |
309 | } |