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