site, transform: Do not initiate rekey when packets too much out of order
[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) {
f208b9a9
IJ
260 if (r->link_quality > LINK_QUALITY_UNUSED) {
261 struct ipset *nan;
262 r->kup=True;
263 nan=ipset_union(allnets,r->networks);
264 ipset_free(allnets);
265 allnets=nan;
266 }
9d3a4132 267 }
d3fe100d
SE
268 snets=ipset_to_subnet_list(allnets);
269 ipset_free(allnets);
794f2398
SE
270 nets=safe_malloc(20*snets->entries,"userv_invoke_userv:nets");
271 *nets=0;
272 for (i=0; i<snets->entries; i++) {
273 s=subnet_to_string(snets->list[i]);
274 strcat(nets,s);
275 strcat(nets,",");
276 free(s);
277 }
9d3a4132 278 nets[strlen(nets)-1]=0;
794f2398 279 subnet_list_free(snets);
9d3a4132 280
042a8da9 281 Message(M_INFO,"%s: about to invoke: %s %s %s %s %s\n",st->slip.nl.name,
9d3a4132
SE
282 st->userv_path,st->service_user,st->service_name,addrs,nets);
283
042a8da9 284 st->slip.pending_esc=False;
9d3a4132
SE
285
286 /* Invoke userv */
287 if (pipe(c_stdin)!=0) {
042a8da9 288 fatal_perror("userv_invoke_userv: pipe(c_stdin)");
9d3a4132
SE
289 }
290 if (pipe(c_stdout)!=0) {
042a8da9 291 fatal_perror("userv_invoke_userv: pipe(c_stdout)");
9d3a4132
SE
292 }
293 st->txfd=c_stdin[1];
294 st->rxfd=c_stdout[0];
295
042a8da9
SE
296 er=safe_malloc(sizeof(*r),"userv_invoke_userv: er");
297
469fd1d9
SE
298 er->in=c_stdin[0];
299 er->out=c_stdout[1];
042a8da9
SE
300 /* The arguments are:
301 userv
302 service-user
303 service-name
304 local-addr,secnet-addr,mtu,protocol
305 route1,route2,... */
306 er->argv=safe_malloc(sizeof(*er->argv)*6,"userv_invoke_userv:argv");
307 er->argv[0]=st->userv_path;
308 er->argv[1]=st->service_user;
309 er->argv[2]=st->service_name;
310 er->argv[3]=addrs;
311 er->argv[4]=nets;
312 er->argv[5]=NULL;
313 er->path=st->userv_path;
314
315 st->pid=makesubproc(userv_entry, userv_userv_callback,
316 er, st, st->slip.nl.name);
469fd1d9
SE
317 close(er->in);
318 close(er->out);
042a8da9
SE
319 free(er->argv);
320 free(er);
321 free(addrs);
322 free(nets);
323 Message(M_INFO,"%s: userv-ipif pid is %d\n",st->slip.nl.name,st->pid);
324 /* Read a single character from the pipe to confirm userv-ipif is
325 running. If we get a SIGCHLD at this point then we'll get EINTR. */
4f5e39ec 326 if ((nread=read(st->rxfd,&confirm,1))!=1) {
042a8da9
SE
327 if (errno==EINTR) {
328 Message(M_WARNING,"%s: read of confirmation byte was "
329 "interrupted\n",st->slip.nl.name);
330 } else {
4f5e39ec
SE
331 if (nread<0) {
332 fatal_perror("%s: error reading confirmation byte",
333 st->slip.nl.name);
334 } else {
335 fatal("%s: unexpected EOF instead of confirmation byte"
336 " - userv ipif failed?", st->slip.nl.name);
337 }
042a8da9
SE
338 }
339 } else {
340 if (confirm!=SLIP_END) {
4f5e39ec 341 fatal("%s: bad confirmation byte %d from userv-ipif",
042a8da9
SE
342 st->slip.nl.name,confirm);
343 }
9d3a4132 344 }
042a8da9
SE
345}
346
347static void userv_kill_userv(struct userv *st)
348{
349 if (st->pid) {
350 kill(-st->pid,SIGTERM);
351 st->expecting_userv_exit=True;
352 }
353}
354
355static void userv_phase_hook(void *sst, uint32_t newphase)
356{
357 struct userv *st=sst;
358 /* We must wait until signal processing has started before forking
359 userv */
360 if (newphase==PHASE_RUN) {
361 userv_invoke_userv(st);
362 /* Register for poll() */
363 register_for_poll(st, userv_beforepoll, userv_afterpoll, 2,
364 st->slip.nl.name);
365 }
366 if (newphase==PHASE_SHUTDOWN) {
367 userv_kill_userv(st);
9d3a4132 368 }
9d3a4132
SE
369}
370
371static list_t *userv_apply(closure_t *self, struct cloc loc, dict_t *context,
372 list_t *args)
373{
374 struct userv *st;
375 item_t *item;
376 dict_t *dict;
377
378 st=safe_malloc(sizeof(*st),"userv_apply");
379
380 /* First parameter must be a dict */
381 item=list_elem(args,0);
382 if (!item || item->type!=t_dict)
383 cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n");
384
385 dict=item->data.dict;
386
042a8da9
SE
387 slip_init(&st->slip,loc,dict,"netlink-userv-ipif",
388 userv_deliver_to_kernel);
9d3a4132
SE
389
390 st->userv_path=dict_read_string(dict,"userv-path",False,"userv-netlink",
391 loc);
392 st->service_user=dict_read_string(dict,"service-user",False,
393 "userv-netlink",loc);
394 st->service_name=dict_read_string(dict,"service-name",False,
395 "userv-netlink",loc);
396 if (!st->userv_path) st->userv_path="userv";
397 if (!st->service_user) st->service_user="root";
398 if (!st->service_name) st->service_name="ipif";
9d3a4132 399 st->rxfd=-1; st->txfd=-1;
042a8da9
SE
400 st->pid=0;
401 st->expecting_userv_exit=False;
402 add_hook(PHASE_RUN,userv_phase_hook,st);
403 add_hook(PHASE_SHUTDOWN,userv_phase_hook,st);
9d3a4132 404
042a8da9 405 return new_closure(&st->slip.nl.cl);
9d3a4132
SE
406}
407
9d3a4132
SE
408void slip_module(dict_t *dict)
409{
410 add_closure(dict,"userv-ipif",userv_apply);
9d3a4132 411}