Import release 0.1.14
[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 uint32_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 string_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 string_t userv_path;
128 string_t service_user;
129 string_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, const struct timeval *tv_now,
136 uint64_t *now)
137 {
138 struct userv *st=sst;
139
140 if (st->rxfd!=-1) {
141 *nfds_io=2;
142 fds[0].fd=st->txfd;
143 fds[0].events=POLLERR; /* Might want to pick up POLLOUT sometime */
144 fds[1].fd=st->rxfd;
145 fds[1].events=POLLIN|POLLERR|POLLHUP;
146 } else {
147 *nfds_io=0;
148 }
149 return 0;
150 }
151
152 static 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];
157 int l;
158
159 if (nfds==0) return;
160
161 if (fds[1].revents&POLLERR) {
162 Message(M_ERR,"%s: userv_afterpoll: POLLERR!\n",st->slip.nl.name);
163 }
164 if (fds[1].revents&POLLIN) {
165 l=read(st->rxfd,rxbuf,DEFAULT_BUFSIZE);
166 if (l<0) {
167 if (errno!=EINTR)
168 fatal_perror("%s: userv_afterpoll: read(rxfd)",
169 st->slip.nl.name);
170 } else if (l==0) {
171 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
172 st->slip.nl.name);
173 } else slip_unstuff(&st->slip,rxbuf,l);
174 }
175 }
176
177 /* Send buf to the kernel. Free buf before returning. */
178 static void userv_deliver_to_kernel(void *sst, struct buffer_if *buf)
179 {
180 struct userv *st=sst;
181
182 slip_stuff(&st->slip,buf,st->txfd);
183 }
184
185 static 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;
193 }
194 if (!st->expecting_userv_exit) {
195 if (WIFEXITED(status)) {
196 fatal("%s: userv exited unexpectedly with status %d",
197 st->slip.nl.name,WEXITSTATUS(status));
198 } else if (WIFSIGNALED(status)) {
199 fatal("%s: userv exited unexpectedly: uncaught signal %d",
200 st->slip.nl.name,WTERMSIG(status));
201 } else {
202 fatal("%s: userv stopped unexpectedly");
203 }
204 }
205 Message(M_WARNING,"%s: userv subprocess died with status %d\n",
206 st->slip.nl.name,WEXITSTATUS(status));
207 st->pid=0;
208 }
209
210 struct userv_entry_rec {
211 string_t path;
212 char **argv;
213 int in;
214 int out;
215 /* XXX perhaps we should collect and log stderr? */
216 };
217
218 static void userv_entry(void *sst)
219 {
220 struct userv_entry_rec *st=sst;
221
222 dup2(st->in,0);
223 dup2(st->out,1);
224
225 /* XXX close all other fds */
226 setsid();
227 execvp(st->path,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->up) {
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 init_module slip_module;
409 void slip_module(dict_t *dict)
410 {
411 add_closure(dict,"userv-ipif",userv_apply);
412 }