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 | 29 | netlink_deliver_fn *netlink_to_tunnel; |
042a8da9 SE |
30 | }; |
31 | ||
32 | /* Generic SLIP mangling code */ | |
33 | ||
34 | static void slip_stuff(struct slip *st, struct buffer_if *buf, int fd) | |
35 | { | |
36 | uint8_t txbuf[DEFAULT_BUFSIZE]; | |
37 | uint8_t *i; | |
1caa23ff | 38 | int32_t j=0; |
042a8da9 SE |
39 | |
40 | BUF_ASSERT_USED(buf); | |
41 | ||
d3fe100d | 42 | /* There's probably a much more efficient way of implementing this */ |
042a8da9 SE |
43 | txbuf[j++]=SLIP_END; |
44 | for (i=buf->start; i<(buf->start+buf->size); i++) { | |
45 | switch (*i) { | |
46 | case SLIP_END: | |
47 | txbuf[j++]=SLIP_ESC; | |
48 | txbuf[j++]=SLIP_ESCEND; | |
49 | break; | |
50 | case SLIP_ESC: | |
51 | txbuf[j++]=SLIP_ESC; | |
52 | txbuf[j++]=SLIP_ESCESC; | |
53 | break; | |
54 | default: | |
55 | txbuf[j++]=*i; | |
56 | break; | |
57 | } | |
58 | if ((j+2)>DEFAULT_BUFSIZE) { | |
59 | if (write(fd,txbuf,j)<0) { | |
60 | fatal_perror("slip_stuff: write()"); | |
61 | } | |
62 | j=0; | |
63 | } | |
64 | } | |
65 | txbuf[j++]=SLIP_END; | |
66 | if (write(fd,txbuf,j)<0) { | |
67 | fatal_perror("slip_stuff: write()"); | |
68 | } | |
69 | BUF_FREE(buf); | |
70 | } | |
71 | ||
72 | static void slip_unstuff(struct slip *st, uint8_t *buf, uint32_t l) | |
73 | { | |
74 | uint32_t i; | |
75 | ||
042a8da9 SE |
76 | BUF_ASSERT_USED(st->buff); |
77 | for (i=0; i<l; i++) { | |
e8a0782f ST |
78 | int outputchr; |
79 | enum { OUTPUT_END = 256, OUTPUT_NOTHING = 257 }; | |
80 | ||
a28d65a5 IJ |
81 | if (!st->buff->size) |
82 | buffer_init(st->buff,calculate_max_start_pad()); | |
83 | ||
042a8da9 SE |
84 | if (st->pending_esc) { |
85 | st->pending_esc=False; | |
86 | switch(buf[i]) { | |
87 | case SLIP_ESCEND: | |
e8a0782f | 88 | outputchr=SLIP_END; |
042a8da9 SE |
89 | break; |
90 | case SLIP_ESCESC: | |
e8a0782f | 91 | outputchr=SLIP_ESC; |
042a8da9 SE |
92 | break; |
93 | default: | |
e8a0782f ST |
94 | if (!st->ignoring_packet) { |
95 | Message(M_WARNING, "userv_afterpoll: bad SLIP escape" | |
96 | " character, dropping packet\n"); | |
97 | } | |
98 | st->ignoring_packet=True; | |
99 | outputchr=OUTPUT_NOTHING; | |
100 | break; | |
042a8da9 SE |
101 | } |
102 | } else { | |
103 | switch (buf[i]) { | |
104 | case SLIP_END: | |
e8a0782f | 105 | outputchr=OUTPUT_END; |
042a8da9 SE |
106 | break; |
107 | case SLIP_ESC: | |
108 | st->pending_esc=True; | |
e8a0782f | 109 | outputchr=OUTPUT_NOTHING; |
042a8da9 SE |
110 | break; |
111 | default: | |
e8a0782f | 112 | outputchr=buf[i]; |
042a8da9 SE |
113 | break; |
114 | } | |
115 | } | |
e8a0782f ST |
116 | |
117 | if (st->ignoring_packet) { | |
118 | if (outputchr == OUTPUT_END) { | |
119 | st->ignoring_packet=False; | |
a28d65a5 | 120 | st->buff->size=0; |
e8a0782f ST |
121 | } |
122 | } else { | |
123 | if (outputchr == OUTPUT_END) { | |
124 | if (st->buff->size>0) { | |
125 | st->netlink_to_tunnel(&st->nl,st->buff); | |
126 | BUF_ALLOC(st->buff,"userv_afterpoll"); | |
127 | } | |
a28d65a5 | 128 | st->buff->size=0; |
e8a0782f | 129 | } else if (outputchr != OUTPUT_NOTHING) { |
92795040 | 130 | if (buf_remaining_space(st->buff)) { |
993db2a6 | 131 | buf_append_uint8(st->buff,outputchr); |
e8a0782f ST |
132 | } else { |
133 | Message(M_WARNING, "userv_afterpoll: dropping overlong" | |
134 | " SLIP packet\n"); | |
135 | st->ignoring_packet=True; | |
136 | } | |
137 | } | |
138 | } | |
042a8da9 SE |
139 | } |
140 | } | |
141 | ||
142 | static void slip_init(struct slip *st, struct cloc loc, dict_t *dict, | |
fe5e9cc4 | 143 | cstring_t name, netlink_deliver_fn *to_host) |
042a8da9 SE |
144 | { |
145 | st->netlink_to_tunnel= | |
146 | netlink_init(&st->nl,st,loc,dict, | |
147 | "netlink-userv-ipif",NULL,to_host); | |
148 | st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"name",loc); | |
042a8da9 SE |
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) { | |
ee697dd9 | 173 | BEFOREPOLL_WANT_FDS(2); |
042a8da9 | 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 | 178 | } else { |
ee697dd9 | 179 | BEFOREPOLL_WANT_FDS(0); |
042a8da9 | 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 | |
32240a83 IJ |
213 | if (buf->size > st->slip.nl.mtu) { |
214 | Message(M_ERR,"%s: packet of size %"PRIu32" exceeds mtu %"PRIu32":" | |
215 | " cannot be injected into kernel, dropped\n", | |
216 | st->slip.nl.name, buf->size, st->slip.nl.mtu); | |
217 | BUF_FREE(buf); | |
218 | return; | |
219 | } | |
220 | ||
042a8da9 SE |
221 | slip_stuff(&st->slip,buf,st->txfd); |
222 | } | |
9d3a4132 | 223 | |
042a8da9 SE |
224 | static void userv_userv_callback(void *sst, pid_t pid, int status) |
225 | { | |
226 | struct userv *st=sst; | |
227 | ||
228 | if (pid!=st->pid) { | |
229 | Message(M_WARNING,"userv_callback called unexpectedly with pid %d " | |
230 | "(expected %d)\n",pid,st->pid); | |
231 | return; | |
9d3a4132 | 232 | } |
042a8da9 | 233 | if (!st->expecting_userv_exit) { |
4ac7fd3f IJ |
234 | lg_exitstatus(0,st->slip.nl.name,0, |
235 | st->expecting_userv_exit ? M_WARNING : M_FATAL, | |
236 | status,"userv"); | |
9d3a4132 | 237 | } |
042a8da9 | 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 | 255 | |
042a8da9 | 256 | setsid(); |
fe5e9cc4 SE |
257 | /* XXX We really should strdup() all of argv[] but because we'll just |
258 | exit anyway if execvp() fails it doesn't seem worth bothering. */ | |
259 | execvp(st->path,(char *const*)st->argv); | |
042a8da9 SE |
260 | perror("userv-entry: execvp()"); |
261 | exit(1); | |
262 | } | |
263 | ||
264 | static void userv_invoke_userv(struct userv *st) | |
265 | { | |
f665113d | 266 | struct userv_entry_rec er[1]; |
9d3a4132 SE |
267 | int c_stdin[2]; |
268 | int c_stdout[2]; | |
9d3a4132 SE |
269 | string_t nets; |
270 | string_t s; | |
d3fe100d SE |
271 | struct netlink_client *r; |
272 | struct ipset *allnets; | |
794f2398 | 273 | struct subnet_list *snets; |
4f5e39ec | 274 | int i, nread; |
042a8da9 SE |
275 | uint8_t confirm; |
276 | ||
277 | if (st->pid) { | |
4f5e39ec | 278 | fatal("userv_invoke_userv: already running"); |
042a8da9 | 279 | } |
9d3a4132 SE |
280 | |
281 | /* This is where we actually invoke userv - all the networks we'll | |
282 | be using should already have been registered. */ | |
283 | ||
f665113d IJ |
284 | char addrs[512]; |
285 | snprintf(addrs,sizeof(addrs),"%s,%s,%d,slip", | |
091433c6 | 286 | ipaddr_to_string(st->slip.nl.local_address), |
042a8da9 | 287 | ipaddr_to_string(st->slip.nl.secnet_address),st->slip.nl.mtu); |
9d3a4132 | 288 | |
d3fe100d SE |
289 | allnets=ipset_new(); |
290 | for (r=st->slip.nl.clients; r; r=r->next) { | |
f208b9a9 IJ |
291 | if (r->link_quality > LINK_QUALITY_UNUSED) { |
292 | struct ipset *nan; | |
293 | r->kup=True; | |
294 | nan=ipset_union(allnets,r->networks); | |
295 | ipset_free(allnets); | |
296 | allnets=nan; | |
297 | } | |
9d3a4132 | 298 | } |
d3fe100d SE |
299 | snets=ipset_to_subnet_list(allnets); |
300 | ipset_free(allnets); | |
794f2398 SE |
301 | nets=safe_malloc(20*snets->entries,"userv_invoke_userv:nets"); |
302 | *nets=0; | |
303 | for (i=0; i<snets->entries; i++) { | |
304 | s=subnet_to_string(snets->list[i]); | |
305 | strcat(nets,s); | |
306 | strcat(nets,","); | |
794f2398 | 307 | } |
9d3a4132 | 308 | nets[strlen(nets)-1]=0; |
794f2398 | 309 | subnet_list_free(snets); |
9d3a4132 | 310 | |
042a8da9 | 311 | Message(M_INFO,"%s: about to invoke: %s %s %s %s %s\n",st->slip.nl.name, |
9d3a4132 SE |
312 | st->userv_path,st->service_user,st->service_name,addrs,nets); |
313 | ||
042a8da9 | 314 | st->slip.pending_esc=False; |
9d3a4132 SE |
315 | |
316 | /* Invoke userv */ | |
6a06198c IJ |
317 | pipe_cloexec(c_stdin); |
318 | pipe_cloexec(c_stdout); | |
9d3a4132 SE |
319 | st->txfd=c_stdin[1]; |
320 | st->rxfd=c_stdout[0]; | |
321 | ||
469fd1d9 SE |
322 | er->in=c_stdin[0]; |
323 | er->out=c_stdout[1]; | |
042a8da9 SE |
324 | /* The arguments are: |
325 | userv | |
326 | service-user | |
327 | service-name | |
328 | local-addr,secnet-addr,mtu,protocol | |
329 | route1,route2,... */ | |
f665113d IJ |
330 | const char *er_argv[6]; |
331 | er->argv=er_argv; | |
042a8da9 SE |
332 | er->argv[0]=st->userv_path; |
333 | er->argv[1]=st->service_user; | |
334 | er->argv[2]=st->service_name; | |
335 | er->argv[3]=addrs; | |
336 | er->argv[4]=nets; | |
337 | er->argv[5]=NULL; | |
338 | er->path=st->userv_path; | |
339 | ||
340 | st->pid=makesubproc(userv_entry, userv_userv_callback, | |
341 | er, st, st->slip.nl.name); | |
469fd1d9 SE |
342 | close(er->in); |
343 | close(er->out); | |
042a8da9 SE |
344 | free(nets); |
345 | Message(M_INFO,"%s: userv-ipif pid is %d\n",st->slip.nl.name,st->pid); | |
346 | /* Read a single character from the pipe to confirm userv-ipif is | |
347 | running. If we get a SIGCHLD at this point then we'll get EINTR. */ | |
4f5e39ec | 348 | if ((nread=read(st->rxfd,&confirm,1))!=1) { |
042a8da9 SE |
349 | if (errno==EINTR) { |
350 | Message(M_WARNING,"%s: read of confirmation byte was " | |
351 | "interrupted\n",st->slip.nl.name); | |
352 | } else { | |
4f5e39ec SE |
353 | if (nread<0) { |
354 | fatal_perror("%s: error reading confirmation byte", | |
355 | st->slip.nl.name); | |
356 | } else { | |
357 | fatal("%s: unexpected EOF instead of confirmation byte" | |
358 | " - userv ipif failed?", st->slip.nl.name); | |
359 | } | |
042a8da9 SE |
360 | } |
361 | } else { | |
362 | if (confirm!=SLIP_END) { | |
4f5e39ec | 363 | fatal("%s: bad confirmation byte %d from userv-ipif", |
042a8da9 SE |
364 | st->slip.nl.name,confirm); |
365 | } | |
9d3a4132 | 366 | } |
042a8da9 SE |
367 | } |
368 | ||
369 | static void userv_kill_userv(struct userv *st) | |
370 | { | |
371 | if (st->pid) { | |
372 | kill(-st->pid,SIGTERM); | |
373 | st->expecting_userv_exit=True; | |
374 | } | |
375 | } | |
376 | ||
377 | static void userv_phase_hook(void *sst, uint32_t newphase) | |
378 | { | |
379 | struct userv *st=sst; | |
380 | /* We must wait until signal processing has started before forking | |
381 | userv */ | |
382 | if (newphase==PHASE_RUN) { | |
383 | userv_invoke_userv(st); | |
384 | /* Register for poll() */ | |
32fc582f | 385 | register_for_poll(st, userv_beforepoll, userv_afterpoll, |
042a8da9 SE |
386 | st->slip.nl.name); |
387 | } | |
388 | if (newphase==PHASE_SHUTDOWN) { | |
389 | userv_kill_userv(st); | |
9d3a4132 | 390 | } |
9d3a4132 SE |
391 | } |
392 | ||
393 | static list_t *userv_apply(closure_t *self, struct cloc loc, dict_t *context, | |
394 | list_t *args) | |
395 | { | |
396 | struct userv *st; | |
397 | item_t *item; | |
398 | dict_t *dict; | |
399 | ||
400 | st=safe_malloc(sizeof(*st),"userv_apply"); | |
401 | ||
402 | /* First parameter must be a dict */ | |
403 | item=list_elem(args,0); | |
404 | if (!item || item->type!=t_dict) | |
405 | cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n"); | |
406 | ||
407 | dict=item->data.dict; | |
408 | ||
042a8da9 SE |
409 | slip_init(&st->slip,loc,dict,"netlink-userv-ipif", |
410 | userv_deliver_to_kernel); | |
9d3a4132 SE |
411 | |
412 | st->userv_path=dict_read_string(dict,"userv-path",False,"userv-netlink", | |
413 | loc); | |
414 | st->service_user=dict_read_string(dict,"service-user",False, | |
415 | "userv-netlink",loc); | |
416 | st->service_name=dict_read_string(dict,"service-name",False, | |
417 | "userv-netlink",loc); | |
418 | if (!st->userv_path) st->userv_path="userv"; | |
419 | if (!st->service_user) st->service_user="root"; | |
420 | if (!st->service_name) st->service_name="ipif"; | |
9d3a4132 | 421 | st->rxfd=-1; st->txfd=-1; |
042a8da9 SE |
422 | st->pid=0; |
423 | st->expecting_userv_exit=False; | |
424 | add_hook(PHASE_RUN,userv_phase_hook,st); | |
425 | add_hook(PHASE_SHUTDOWN,userv_phase_hook,st); | |
9d3a4132 | 426 | |
042a8da9 | 427 | return new_closure(&st->slip.nl.cl); |
9d3a4132 SE |
428 | } |
429 | ||
9d3a4132 SE |
430 | void slip_module(dict_t *dict) |
431 | { | |
432 | add_closure(dict,"userv-ipif",userv_apply); | |
9d3a4132 | 433 | } |