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