site, transform: Do not initiate rekey when packets too much out of order
[secnet] / slip.c
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"
9 #include "process.h"
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <fcntl.h>
15
16 #define SLIP_END 192
17 #define SLIP_ESC 219
18 #define SLIP_ESCEND 220
19 #define SLIP_ESCESC 221
20
21 struct 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
32 static void slip_stuff(struct slip *st, struct buffer_if *buf, int fd)
33 {
34 uint8_t txbuf[DEFAULT_BUFSIZE];
35 uint8_t *i;
36 int32_t j=0;
37
38 BUF_ASSERT_USED(buf);
39
40 /* There's probably a much more efficient way of implementing this */
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
70 static void slip_unstuff(struct slip *st, uint8_t *buf, uint32_t l)
71 {
72 uint32_t i;
73
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:
86 fatal("userv_afterpoll: bad SLIP escape character");
87 }
88 } else {
89 switch (buf[i]) {
90 case SLIP_END:
91 if (st->buff->size>0) {
92 st->netlink_to_tunnel(&st->nl,st->buff);
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
108 static void slip_init(struct slip *st, struct cloc loc, dict_t *dict,
109 cstring_t name, netlink_deliver_fn *to_host)
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);
115 st->local_address=string_item_to_ipaddr(
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
121 /* Connection to the kernel through userv-ipif */
122
123 struct userv {
124 struct slip slip;
125 int txfd; /* We transmit to userv */
126 int rxfd; /* We receive from userv */
127 cstring_t userv_path;
128 cstring_t service_user;
129 cstring_t service_name;
130 pid_t pid;
131 bool_t expecting_userv_exit;
132 };
133
134 static int userv_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
135 int *timeout_io)
136 {
137 struct userv *st=sst;
138
139 if (st->rxfd!=-1) {
140 *nfds_io=2;
141 fds[0].fd=st->txfd;
142 fds[0].events=0; /* Might want to pick up POLLOUT sometime */
143 fds[1].fd=st->rxfd;
144 fds[1].events=POLLIN;
145 } else {
146 *nfds_io=0;
147 }
148 return 0;
149 }
150
151 static void userv_afterpoll(void *sst, struct pollfd *fds, int nfds)
152 {
153 struct userv *st=sst;
154 uint8_t rxbuf[DEFAULT_BUFSIZE];
155 int l;
156
157 if (nfds==0) return;
158
159 if (fds[1].revents&POLLERR) {
160 Message(M_ERR,"%s: userv_afterpoll: POLLERR!\n",st->slip.nl.name);
161 }
162 if (fds[1].revents&POLLIN) {
163 l=read(st->rxfd,rxbuf,DEFAULT_BUFSIZE);
164 if (l<0) {
165 if (errno!=EINTR)
166 fatal_perror("%s: userv_afterpoll: read(rxfd)",
167 st->slip.nl.name);
168 } else if (l==0) {
169 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
170 st->slip.nl.name);
171 } else slip_unstuff(&st->slip,rxbuf,l);
172 }
173 }
174
175 /* Send buf to the kernel. Free buf before returning. */
176 static void userv_deliver_to_kernel(void *sst, struct buffer_if *buf)
177 {
178 struct userv *st=sst;
179
180 slip_stuff(&st->slip,buf,st->txfd);
181 }
182
183 static 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;
191 }
192 if (!st->expecting_userv_exit) {
193 if (WIFEXITED(status)) {
194 fatal("%s: userv exited unexpectedly with status %d",
195 st->slip.nl.name,WEXITSTATUS(status));
196 } else if (WIFSIGNALED(status)) {
197 fatal("%s: userv exited unexpectedly: uncaught signal %d",
198 st->slip.nl.name,WTERMSIG(status));
199 } else {
200 fatal("%s: userv stopped unexpectedly");
201 }
202 }
203 Message(M_WARNING,"%s: userv subprocess died with status %d\n",
204 st->slip.nl.name,WEXITSTATUS(status));
205 st->pid=0;
206 }
207
208 struct userv_entry_rec {
209 cstring_t path;
210 const char **argv;
211 int in;
212 int out;
213 /* XXX perhaps we should collect and log stderr? */
214 };
215
216 static void userv_entry(void *sst)
217 {
218 struct userv_entry_rec *st=sst;
219
220 dup2(st->in,0);
221 dup2(st->out,1);
222
223 /* XXX close all other fds */
224 setsid();
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);
228 perror("userv-entry: execvp()");
229 exit(1);
230 }
231
232 static void userv_invoke_userv(struct userv *st)
233 {
234 struct userv_entry_rec *er;
235 int c_stdin[2];
236 int c_stdout[2];
237 string_t addrs;
238 string_t nets;
239 string_t s;
240 struct netlink_client *r;
241 struct ipset *allnets;
242 struct subnet_list *snets;
243 int i, nread;
244 uint8_t confirm;
245
246 if (st->pid) {
247 fatal("userv_invoke_userv: already running");
248 }
249
250 /* This is where we actually invoke userv - all the networks we'll
251 be using should already have been registered. */
252
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);
257
258 allnets=ipset_new();
259 for (r=st->slip.nl.clients; r; r=r->next) {
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 }
267 }
268 snets=ipset_to_subnet_list(allnets);
269 ipset_free(allnets);
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 }
278 nets[strlen(nets)-1]=0;
279 subnet_list_free(snets);
280
281 Message(M_INFO,"%s: about to invoke: %s %s %s %s %s\n",st->slip.nl.name,
282 st->userv_path,st->service_user,st->service_name,addrs,nets);
283
284 st->slip.pending_esc=False;
285
286 /* Invoke userv */
287 if (pipe(c_stdin)!=0) {
288 fatal_perror("userv_invoke_userv: pipe(c_stdin)");
289 }
290 if (pipe(c_stdout)!=0) {
291 fatal_perror("userv_invoke_userv: pipe(c_stdout)");
292 }
293 st->txfd=c_stdin[1];
294 st->rxfd=c_stdout[0];
295
296 er=safe_malloc(sizeof(*r),"userv_invoke_userv: er");
297
298 er->in=c_stdin[0];
299 er->out=c_stdout[1];
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);
317 close(er->in);
318 close(er->out);
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. */
326 if ((nread=read(st->rxfd,&confirm,1))!=1) {
327 if (errno==EINTR) {
328 Message(M_WARNING,"%s: read of confirmation byte was "
329 "interrupted\n",st->slip.nl.name);
330 } else {
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 }
338 }
339 } else {
340 if (confirm!=SLIP_END) {
341 fatal("%s: bad confirmation byte %d from userv-ipif",
342 st->slip.nl.name,confirm);
343 }
344 }
345 }
346
347 static 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
355 static 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);
368 }
369 }
370
371 static 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
387 slip_init(&st->slip,loc,dict,"netlink-userv-ipif",
388 userv_deliver_to_kernel);
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";
399 st->rxfd=-1; st->txfd=-1;
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);
404
405 return new_closure(&st->slip.nl.cl);
406 }
407
408 void slip_module(dict_t *dict)
409 {
410 add_closure(dict,"userv-ipif",userv_apply);
411 }