Commit | Line | Data |
---|---|---|
9d3a4132 SE |
1 | /* When dealing with SLIP (to a pty, or ipif) we have separate rx, tx |
2 | and client buffers. When receiving we may read() any amount, not | |
3 | just whole packets. When transmitting we need to bytestuff anyway, | |
4 | and may be part-way through receiving. */ | |
5 | ||
6 | #include "secnet.h" | |
7 | #include "util.h" | |
8 | #include "netlink.h" | |
042a8da9 | 9 | #include "process.h" |
993db2a6 | 10 | #include "unaligned.h" |
9d3a4132 SE |
11 | #include <stdio.h> |
12 | #include <string.h> | |
13 | #include <unistd.h> | |
042a8da9 SE |
14 | #include <errno.h> |
15 | #include <fcntl.h> | |
9d3a4132 SE |
16 | |
17 | #define SLIP_END 192 | |
18 | #define SLIP_ESC 219 | |
19 | #define SLIP_ESCEND 220 | |
20 | #define SLIP_ESCESC 221 | |
21 | ||
042a8da9 SE |
22 | struct slip { |
23 | struct netlink nl; | |
24 | struct buffer_if *buff; /* We unstuff received packets into here | |
25 | and send them to the netlink code. */ | |
26 | bool_t pending_esc; | |
e8a0782f ST |
27 | bool_t ignoring_packet; /* If this packet was corrupt or overlong, |
28 | we ignore everything up to the next END */ | |
042a8da9 SE |
29 | netlink_deliver_fn *netlink_to_tunnel; |
30 | uint32_t local_address; | |
31 | }; | |
32 | ||
33 | /* Generic SLIP mangling code */ | |
34 | ||
35 | static void slip_stuff(struct slip *st, struct buffer_if *buf, int fd) | |
36 | { | |
37 | uint8_t txbuf[DEFAULT_BUFSIZE]; | |
38 | uint8_t *i; | |
1caa23ff | 39 | int32_t j=0; |
042a8da9 SE |
40 | |
41 | BUF_ASSERT_USED(buf); | |
42 | ||
d3fe100d | 43 | /* There's probably a much more efficient way of implementing this */ |
042a8da9 SE |
44 | txbuf[j++]=SLIP_END; |
45 | for (i=buf->start; i<(buf->start+buf->size); i++) { | |
46 | switch (*i) { | |
47 | case SLIP_END: | |
48 | txbuf[j++]=SLIP_ESC; | |
49 | txbuf[j++]=SLIP_ESCEND; | |
50 | break; | |
51 | case SLIP_ESC: | |
52 | txbuf[j++]=SLIP_ESC; | |
53 | txbuf[j++]=SLIP_ESCESC; | |
54 | break; | |
55 | default: | |
56 | txbuf[j++]=*i; | |
57 | break; | |
58 | } | |
59 | if ((j+2)>DEFAULT_BUFSIZE) { | |
60 | if (write(fd,txbuf,j)<0) { | |
61 | fatal_perror("slip_stuff: write()"); | |
62 | } | |
63 | j=0; | |
64 | } | |
65 | } | |
66 | txbuf[j++]=SLIP_END; | |
67 | if (write(fd,txbuf,j)<0) { | |
68 | fatal_perror("slip_stuff: write()"); | |
69 | } | |
70 | BUF_FREE(buf); | |
71 | } | |
72 | ||
73 | static void slip_unstuff(struct slip *st, uint8_t *buf, uint32_t l) | |
74 | { | |
75 | uint32_t i; | |
76 | ||
042a8da9 SE |
77 | BUF_ASSERT_USED(st->buff); |
78 | for (i=0; i<l; i++) { | |
e8a0782f ST |
79 | int outputchr; |
80 | enum { OUTPUT_END = 256, OUTPUT_NOTHING = 257 }; | |
81 | ||
a28d65a5 IJ |
82 | if (!st->buff->size) |
83 | buffer_init(st->buff,calculate_max_start_pad()); | |
84 | ||
042a8da9 SE |
85 | if (st->pending_esc) { |
86 | st->pending_esc=False; | |
87 | switch(buf[i]) { | |
88 | case SLIP_ESCEND: | |
e8a0782f | 89 | outputchr=SLIP_END; |
042a8da9 SE |
90 | break; |
91 | case SLIP_ESCESC: | |
e8a0782f | 92 | outputchr=SLIP_ESC; |
042a8da9 SE |
93 | break; |
94 | default: | |
e8a0782f ST |
95 | if (!st->ignoring_packet) { |
96 | Message(M_WARNING, "userv_afterpoll: bad SLIP escape" | |
97 | " character, dropping packet\n"); | |
98 | } | |
99 | st->ignoring_packet=True; | |
100 | outputchr=OUTPUT_NOTHING; | |
101 | break; | |
042a8da9 SE |
102 | } |
103 | } else { | |
104 | switch (buf[i]) { | |
105 | case SLIP_END: | |
e8a0782f | 106 | outputchr=OUTPUT_END; |
042a8da9 SE |
107 | break; |
108 | case SLIP_ESC: | |
109 | st->pending_esc=True; | |
e8a0782f | 110 | outputchr=OUTPUT_NOTHING; |
042a8da9 SE |
111 | break; |
112 | default: | |
e8a0782f | 113 | outputchr=buf[i]; |
042a8da9 SE |
114 | break; |
115 | } | |
116 | } | |
e8a0782f ST |
117 | |
118 | if (st->ignoring_packet) { | |
119 | if (outputchr == OUTPUT_END) { | |
120 | st->ignoring_packet=False; | |
a28d65a5 | 121 | st->buff->size=0; |
e8a0782f ST |
122 | } |
123 | } else { | |
124 | if (outputchr == OUTPUT_END) { | |
125 | if (st->buff->size>0) { | |
126 | st->netlink_to_tunnel(&st->nl,st->buff); | |
127 | BUF_ALLOC(st->buff,"userv_afterpoll"); | |
128 | } | |
a28d65a5 | 129 | st->buff->size=0; |
e8a0782f ST |
130 | } else if (outputchr != OUTPUT_NOTHING) { |
131 | if (st->buff->size < st->buff->len) { | |
993db2a6 | 132 | buf_append_uint8(st->buff,outputchr); |
e8a0782f ST |
133 | } else { |
134 | Message(M_WARNING, "userv_afterpoll: dropping overlong" | |
135 | " SLIP packet\n"); | |
136 | st->ignoring_packet=True; | |
137 | } | |
138 | } | |
139 | } | |
042a8da9 SE |
140 | } |
141 | } | |
142 | ||
143 | static void slip_init(struct slip *st, struct cloc loc, dict_t *dict, | |
fe5e9cc4 | 144 | cstring_t name, netlink_deliver_fn *to_host) |
042a8da9 SE |
145 | { |
146 | st->netlink_to_tunnel= | |
147 | netlink_init(&st->nl,st,loc,dict, | |
148 | "netlink-userv-ipif",NULL,to_host); | |
149 | st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"name",loc); | |
794f2398 | 150 | st->local_address=string_item_to_ipaddr( |
042a8da9 SE |
151 | dict_find_item(dict,"local-address", True, name, loc),"netlink"); |
152 | BUF_ALLOC(st->buff,"slip_init"); | |
153 | st->pending_esc=False; | |
e8a0782f | 154 | st->ignoring_packet=False; |
042a8da9 SE |
155 | } |
156 | ||
9d3a4132 SE |
157 | /* Connection to the kernel through userv-ipif */ |
158 | ||
159 | struct userv { | |
042a8da9 | 160 | struct slip slip; |
9d3a4132 SE |
161 | int txfd; /* We transmit to userv */ |
162 | int rxfd; /* We receive from userv */ | |
fe5e9cc4 SE |
163 | cstring_t userv_path; |
164 | cstring_t service_user; | |
165 | cstring_t service_name; | |
042a8da9 SE |
166 | pid_t pid; |
167 | bool_t expecting_userv_exit; | |
9d3a4132 SE |
168 | }; |
169 | ||
170 | static int userv_beforepoll(void *sst, struct pollfd *fds, int *nfds_io, | |
90a39563 | 171 | int *timeout_io) |
9d3a4132 SE |
172 | { |
173 | struct userv *st=sst; | |
042a8da9 SE |
174 | |
175 | if (st->rxfd!=-1) { | |
176 | *nfds_io=2; | |
177 | fds[0].fd=st->txfd; | |
fe5e9cc4 | 178 | fds[0].events=0; /* Might want to pick up POLLOUT sometime */ |
042a8da9 | 179 | fds[1].fd=st->rxfd; |
fe5e9cc4 | 180 | fds[1].events=POLLIN; |
042a8da9 SE |
181 | } else { |
182 | *nfds_io=0; | |
183 | } | |
9d3a4132 SE |
184 | return 0; |
185 | } | |
186 | ||
90a39563 | 187 | static void userv_afterpoll(void *sst, struct pollfd *fds, int nfds) |
9d3a4132 SE |
188 | { |
189 | struct userv *st=sst; | |
190 | uint8_t rxbuf[DEFAULT_BUFSIZE]; | |
042a8da9 SE |
191 | int l; |
192 | ||
193 | if (nfds==0) return; | |
9d3a4132 SE |
194 | |
195 | if (fds[1].revents&POLLERR) { | |
469fd1d9 | 196 | Message(M_ERR,"%s: userv_afterpoll: POLLERR!\n",st->slip.nl.name); |
9d3a4132 SE |
197 | } |
198 | if (fds[1].revents&POLLIN) { | |
199 | l=read(st->rxfd,rxbuf,DEFAULT_BUFSIZE); | |
200 | if (l<0) { | |
042a8da9 SE |
201 | if (errno!=EINTR) |
202 | fatal_perror("%s: userv_afterpoll: read(rxfd)", | |
203 | st->slip.nl.name); | |
204 | } else if (l==0) { | |
4f5e39ec | 205 | fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?", |
042a8da9 SE |
206 | st->slip.nl.name); |
207 | } else slip_unstuff(&st->slip,rxbuf,l); | |
9d3a4132 SE |
208 | } |
209 | } | |
210 | ||
211 | /* Send buf to the kernel. Free buf before returning. */ | |
469fd1d9 | 212 | static void userv_deliver_to_kernel(void *sst, struct buffer_if *buf) |
9d3a4132 SE |
213 | { |
214 | struct userv *st=sst; | |
9d3a4132 | 215 | |
32240a83 IJ |
216 | if (buf->size > st->slip.nl.mtu) { |
217 | Message(M_ERR,"%s: packet of size %"PRIu32" exceeds mtu %"PRIu32":" | |
218 | " cannot be injected into kernel, dropped\n", | |
219 | st->slip.nl.name, buf->size, st->slip.nl.mtu); | |
220 | BUF_FREE(buf); | |
221 | return; | |
222 | } | |
223 | ||
042a8da9 SE |
224 | slip_stuff(&st->slip,buf,st->txfd); |
225 | } | |
9d3a4132 | 226 | |
042a8da9 SE |
227 | static void userv_userv_callback(void *sst, pid_t pid, int status) |
228 | { | |
229 | struct userv *st=sst; | |
230 | ||
231 | if (pid!=st->pid) { | |
232 | Message(M_WARNING,"userv_callback called unexpectedly with pid %d " | |
233 | "(expected %d)\n",pid,st->pid); | |
234 | return; | |
9d3a4132 | 235 | } |
042a8da9 SE |
236 | if (!st->expecting_userv_exit) { |
237 | if (WIFEXITED(status)) { | |
4f5e39ec | 238 | fatal("%s: userv exited unexpectedly with status %d", |
042a8da9 SE |
239 | st->slip.nl.name,WEXITSTATUS(status)); |
240 | } else if (WIFSIGNALED(status)) { | |
4f5e39ec | 241 | fatal("%s: userv exited unexpectedly: uncaught signal %d", |
042a8da9 SE |
242 | st->slip.nl.name,WTERMSIG(status)); |
243 | } else { | |
779837e1 IJ |
244 | fatal("%s: userv stopped unexpectedly", |
245 | st->slip.nl.name); | |
042a8da9 | 246 | } |
9d3a4132 | 247 | } |
042a8da9 SE |
248 | Message(M_WARNING,"%s: userv subprocess died with status %d\n", |
249 | st->slip.nl.name,WEXITSTATUS(status)); | |
250 | st->pid=0; | |
9d3a4132 SE |
251 | } |
252 | ||
042a8da9 | 253 | struct userv_entry_rec { |
fe5e9cc4 SE |
254 | cstring_t path; |
255 | const char **argv; | |
469fd1d9 SE |
256 | int in; |
257 | int out; | |
042a8da9 SE |
258 | /* XXX perhaps we should collect and log stderr? */ |
259 | }; | |
260 | ||
261 | static void userv_entry(void *sst) | |
9d3a4132 | 262 | { |
042a8da9 SE |
263 | struct userv_entry_rec *st=sst; |
264 | ||
469fd1d9 SE |
265 | dup2(st->in,0); |
266 | dup2(st->out,1); | |
042a8da9 SE |
267 | |
268 | /* XXX close all other fds */ | |
269 | setsid(); | |
fe5e9cc4 SE |
270 | /* XXX We really should strdup() all of argv[] but because we'll just |
271 | exit anyway if execvp() fails it doesn't seem worth bothering. */ | |
272 | execvp(st->path,(char *const*)st->argv); | |
042a8da9 SE |
273 | perror("userv-entry: execvp()"); |
274 | exit(1); | |
275 | } | |
276 | ||
277 | static void userv_invoke_userv(struct userv *st) | |
278 | { | |
279 | struct userv_entry_rec *er; | |
9d3a4132 SE |
280 | int c_stdin[2]; |
281 | int c_stdout[2]; | |
282 | string_t addrs; | |
283 | string_t nets; | |
284 | string_t s; | |
d3fe100d SE |
285 | struct netlink_client *r; |
286 | struct ipset *allnets; | |
794f2398 | 287 | struct subnet_list *snets; |
4f5e39ec | 288 | int i, nread; |
042a8da9 SE |
289 | uint8_t confirm; |
290 | ||
291 | if (st->pid) { | |
4f5e39ec | 292 | fatal("userv_invoke_userv: already running"); |
042a8da9 | 293 | } |
9d3a4132 SE |
294 | |
295 | /* This is where we actually invoke userv - all the networks we'll | |
296 | be using should already have been registered. */ | |
297 | ||
042a8da9 SE |
298 | addrs=safe_malloc(512,"userv_invoke_userv:addrs"); |
299 | snprintf(addrs,512,"%s,%s,%d,slip", | |
300 | ipaddr_to_string(st->slip.local_address), | |
301 | ipaddr_to_string(st->slip.nl.secnet_address),st->slip.nl.mtu); | |
9d3a4132 | 302 | |
d3fe100d SE |
303 | allnets=ipset_new(); |
304 | for (r=st->slip.nl.clients; r; r=r->next) { | |
f208b9a9 IJ |
305 | if (r->link_quality > LINK_QUALITY_UNUSED) { |
306 | struct ipset *nan; | |
307 | r->kup=True; | |
308 | nan=ipset_union(allnets,r->networks); | |
309 | ipset_free(allnets); | |
310 | allnets=nan; | |
311 | } | |
9d3a4132 | 312 | } |
d3fe100d SE |
313 | snets=ipset_to_subnet_list(allnets); |
314 | ipset_free(allnets); | |
794f2398 SE |
315 | nets=safe_malloc(20*snets->entries,"userv_invoke_userv:nets"); |
316 | *nets=0; | |
317 | for (i=0; i<snets->entries; i++) { | |
318 | s=subnet_to_string(snets->list[i]); | |
319 | strcat(nets,s); | |
320 | strcat(nets,","); | |
321 | free(s); | |
322 | } | |
9d3a4132 | 323 | nets[strlen(nets)-1]=0; |
794f2398 | 324 | subnet_list_free(snets); |
9d3a4132 | 325 | |
042a8da9 | 326 | Message(M_INFO,"%s: about to invoke: %s %s %s %s %s\n",st->slip.nl.name, |
9d3a4132 SE |
327 | st->userv_path,st->service_user,st->service_name,addrs,nets); |
328 | ||
042a8da9 | 329 | st->slip.pending_esc=False; |
9d3a4132 SE |
330 | |
331 | /* Invoke userv */ | |
332 | if (pipe(c_stdin)!=0) { | |
042a8da9 | 333 | fatal_perror("userv_invoke_userv: pipe(c_stdin)"); |
9d3a4132 SE |
334 | } |
335 | if (pipe(c_stdout)!=0) { | |
042a8da9 | 336 | fatal_perror("userv_invoke_userv: pipe(c_stdout)"); |
9d3a4132 SE |
337 | } |
338 | st->txfd=c_stdin[1]; | |
339 | st->rxfd=c_stdout[0]; | |
340 | ||
042a8da9 SE |
341 | er=safe_malloc(sizeof(*r),"userv_invoke_userv: er"); |
342 | ||
469fd1d9 SE |
343 | er->in=c_stdin[0]; |
344 | er->out=c_stdout[1]; | |
042a8da9 SE |
345 | /* The arguments are: |
346 | userv | |
347 | service-user | |
348 | service-name | |
349 | local-addr,secnet-addr,mtu,protocol | |
350 | route1,route2,... */ | |
351 | er->argv=safe_malloc(sizeof(*er->argv)*6,"userv_invoke_userv:argv"); | |
352 | er->argv[0]=st->userv_path; | |
353 | er->argv[1]=st->service_user; | |
354 | er->argv[2]=st->service_name; | |
355 | er->argv[3]=addrs; | |
356 | er->argv[4]=nets; | |
357 | er->argv[5]=NULL; | |
358 | er->path=st->userv_path; | |
359 | ||
360 | st->pid=makesubproc(userv_entry, userv_userv_callback, | |
361 | er, st, st->slip.nl.name); | |
469fd1d9 SE |
362 | close(er->in); |
363 | close(er->out); | |
042a8da9 SE |
364 | free(er->argv); |
365 | free(er); | |
366 | free(addrs); | |
367 | free(nets); | |
368 | Message(M_INFO,"%s: userv-ipif pid is %d\n",st->slip.nl.name,st->pid); | |
369 | /* Read a single character from the pipe to confirm userv-ipif is | |
370 | running. If we get a SIGCHLD at this point then we'll get EINTR. */ | |
4f5e39ec | 371 | if ((nread=read(st->rxfd,&confirm,1))!=1) { |
042a8da9 SE |
372 | if (errno==EINTR) { |
373 | Message(M_WARNING,"%s: read of confirmation byte was " | |
374 | "interrupted\n",st->slip.nl.name); | |
375 | } else { | |
4f5e39ec SE |
376 | if (nread<0) { |
377 | fatal_perror("%s: error reading confirmation byte", | |
378 | st->slip.nl.name); | |
379 | } else { | |
380 | fatal("%s: unexpected EOF instead of confirmation byte" | |
381 | " - userv ipif failed?", st->slip.nl.name); | |
382 | } | |
042a8da9 SE |
383 | } |
384 | } else { | |
385 | if (confirm!=SLIP_END) { | |
4f5e39ec | 386 | fatal("%s: bad confirmation byte %d from userv-ipif", |
042a8da9 SE |
387 | st->slip.nl.name,confirm); |
388 | } | |
9d3a4132 | 389 | } |
042a8da9 SE |
390 | } |
391 | ||
392 | static void userv_kill_userv(struct userv *st) | |
393 | { | |
394 | if (st->pid) { | |
395 | kill(-st->pid,SIGTERM); | |
396 | st->expecting_userv_exit=True; | |
397 | } | |
398 | } | |
399 | ||
400 | static void userv_phase_hook(void *sst, uint32_t newphase) | |
401 | { | |
402 | struct userv *st=sst; | |
403 | /* We must wait until signal processing has started before forking | |
404 | userv */ | |
405 | if (newphase==PHASE_RUN) { | |
406 | userv_invoke_userv(st); | |
407 | /* Register for poll() */ | |
408 | register_for_poll(st, userv_beforepoll, userv_afterpoll, 2, | |
409 | st->slip.nl.name); | |
410 | } | |
411 | if (newphase==PHASE_SHUTDOWN) { | |
412 | userv_kill_userv(st); | |
9d3a4132 | 413 | } |
9d3a4132 SE |
414 | } |
415 | ||
416 | static list_t *userv_apply(closure_t *self, struct cloc loc, dict_t *context, | |
417 | list_t *args) | |
418 | { | |
419 | struct userv *st; | |
420 | item_t *item; | |
421 | dict_t *dict; | |
422 | ||
423 | st=safe_malloc(sizeof(*st),"userv_apply"); | |
424 | ||
425 | /* First parameter must be a dict */ | |
426 | item=list_elem(args,0); | |
427 | if (!item || item->type!=t_dict) | |
428 | cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n"); | |
429 | ||
430 | dict=item->data.dict; | |
431 | ||
042a8da9 SE |
432 | slip_init(&st->slip,loc,dict,"netlink-userv-ipif", |
433 | userv_deliver_to_kernel); | |
9d3a4132 SE |
434 | |
435 | st->userv_path=dict_read_string(dict,"userv-path",False,"userv-netlink", | |
436 | loc); | |
437 | st->service_user=dict_read_string(dict,"service-user",False, | |
438 | "userv-netlink",loc); | |
439 | st->service_name=dict_read_string(dict,"service-name",False, | |
440 | "userv-netlink",loc); | |
441 | if (!st->userv_path) st->userv_path="userv"; | |
442 | if (!st->service_user) st->service_user="root"; | |
443 | if (!st->service_name) st->service_name="ipif"; | |
9d3a4132 | 444 | st->rxfd=-1; st->txfd=-1; |
042a8da9 SE |
445 | st->pid=0; |
446 | st->expecting_userv_exit=False; | |
447 | add_hook(PHASE_RUN,userv_phase_hook,st); | |
448 | add_hook(PHASE_SHUTDOWN,userv_phase_hook,st); | |
9d3a4132 | 449 | |
042a8da9 | 450 | return new_closure(&st->slip.nl.cl); |
9d3a4132 SE |
451 | } |
452 | ||
9d3a4132 SE |
453 | void slip_module(dict_t *dict) |
454 | { | |
455 | add_closure(dict,"userv-ipif",userv_apply); | |
9d3a4132 | 456 | } |