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