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. */
7 * This file is part of secnet.
8 * See README for full list of copyright holders.
10 * secnet is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * secnet is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * version 3 along with secnet; if not, see
22 * https://www.gnu.org/licenses/gpl.html.
29 #include "unaligned.h"
38 #define SLIP_ESCEND 220
39 #define SLIP_ESCESC 221
43 struct buffer_if *buff; /* We unstuff received packets into here
44 and send them to the netlink code. */
46 bool_t ignoring_packet; /* If this packet was corrupt or overlong,
47 we ignore everything up to the next END */
48 netlink_deliver_fn *netlink_to_tunnel;
51 /* Generic SLIP mangling code */
53 static void slip_write(int fd, const uint8_t *p, size_t l)
56 ssize_t written=write(fd,p,l);
60 } else if (iswouldblock(errno)) {
61 lg_perror(0,"slip",0,M_ERR,errno,"write() (packet(s) lost)");
64 fatal_perror("slip_stuff: write()");
68 assert((size_t)written<=l);
74 static void slip_stuff(struct slip *st, struct buffer_if *buf, int fd)
76 uint8_t txbuf[DEFAULT_BUFSIZE];
82 /* There's probably a much more efficient way of implementing this */
84 for (i=buf->start; i<(buf->start+buf->size); i++) {
88 txbuf[j++]=SLIP_ESCEND;
92 txbuf[j++]=SLIP_ESCESC;
98 if ((j+2)>DEFAULT_BUFSIZE) {
99 slip_write(fd,txbuf,j);
104 slip_write(fd,txbuf,j);
108 static void slip_unstuff(struct slip *st, uint8_t *buf, uint32_t l)
112 BUF_ASSERT_USED(st->buff);
113 for (i=0; i<l; i++) {
115 enum { OUTPUT_END = 256, OUTPUT_NOTHING = 257 };
118 buffer_init(st->buff,calculate_max_start_pad());
120 if (st->pending_esc) {
121 st->pending_esc=False;
130 if (!st->ignoring_packet) {
131 Message(M_WARNING, "userv_afterpoll: bad SLIP escape"
132 " character, dropping packet\n");
134 st->ignoring_packet=True;
135 outputchr=OUTPUT_NOTHING;
141 outputchr=OUTPUT_END;
144 st->pending_esc=True;
145 outputchr=OUTPUT_NOTHING;
153 if (st->ignoring_packet) {
154 if (outputchr == OUTPUT_END) {
155 st->ignoring_packet=False;
159 if (outputchr == OUTPUT_END) {
160 if (st->buff->size>0) {
161 st->netlink_to_tunnel(&st->nl,st->buff);
162 BUF_ALLOC(st->buff,"userv_afterpoll");
165 } else if (outputchr != OUTPUT_NOTHING) {
166 if (buf_remaining_space(st->buff)) {
167 buf_append_uint8(st->buff,outputchr);
169 Message(M_WARNING, "userv_afterpoll: dropping overlong"
171 st->ignoring_packet=True;
178 static void slip_init(struct slip *st, struct cloc loc, dict_t *dict,
179 cstring_t name, netlink_deliver_fn *to_host)
181 st->netlink_to_tunnel=
182 netlink_init(&st->nl,st,loc,dict,
183 "netlink-userv-ipif",NULL,to_host);
184 st->buff=find_cl_if(dict,"buffer",CL_BUFFER,True,"name",loc);
185 BUF_ALLOC(st->buff,"slip_init");
186 st->pending_esc=False;
187 st->ignoring_packet=False;
190 /* Connection to the kernel through userv-ipif */
194 int txfd; /* We transmit to userv */
195 int rxfd; /* We receive from userv */
196 cstring_t userv_path;
197 cstring_t service_user;
198 cstring_t service_name;
200 bool_t expecting_userv_exit;
203 static int userv_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
206 struct userv *st=sst;
209 BEFOREPOLL_WANT_FDS(2);
211 fds[0].events=0; /* Might want to pick up POLLOUT sometime */
213 fds[1].events=POLLIN;
215 BEFOREPOLL_WANT_FDS(0);
220 static void userv_afterpoll(void *sst, struct pollfd *fds, int nfds)
222 struct userv *st=sst;
223 uint8_t rxbuf[DEFAULT_BUFSIZE];
228 if (fds[1].revents&POLLERR) {
229 Message(M_ERR,"%s: userv_afterpoll: POLLERR!\n",st->slip.nl.name);
231 if (fds[1].revents&POLLIN) {
232 l=read(st->rxfd,rxbuf,DEFAULT_BUFSIZE);
234 if (errno!=EINTR && !iswouldblock(errno))
235 fatal_perror("%s: userv_afterpoll: read(rxfd)",
238 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
240 } else slip_unstuff(&st->slip,rxbuf,l);
244 /* Send buf to the kernel. Free buf before returning. */
245 static void userv_deliver_to_kernel(void *sst, struct buffer_if *buf)
247 struct userv *st=sst;
249 if (buf->size > st->slip.nl.mtu) {
250 Message(M_ERR,"%s: packet of size %"PRIu32" exceeds mtu %"PRIu32":"
251 " cannot be injected into kernel, dropped\n",
252 st->slip.nl.name, buf->size, st->slip.nl.mtu);
257 slip_stuff(&st->slip,buf,st->txfd);
260 static void userv_userv_callback(void *sst, pid_t pid, int status)
262 struct userv *st=sst;
265 Message(M_WARNING,"userv_callback called unexpectedly with pid %d "
266 "(expected %d)\n",pid,st->pid);
269 if (!(st->expecting_userv_exit &&
271 (WIFSIGNALED(status) && WTERMSIG(status)==SIGTERM)))) {
272 lg_exitstatus(0,st->slip.nl.name,0,
273 st->expecting_userv_exit ? M_WARNING : M_FATAL,
279 struct userv_entry_rec {
284 /* XXX perhaps we should collect and log stderr? */
287 static void userv_entry(void *sst)
289 struct userv_entry_rec *st=sst;
295 execvp(st->path,(char *const*)st->argv);
296 perror("userv-entry: execvp()");
300 static void userv_invoke_userv(struct userv *st)
302 struct userv_entry_rec er[1];
307 struct netlink_client *r;
308 struct ipset *allnets;
309 struct subnet_list *snets;
314 fatal("userv_invoke_userv: already running");
317 /* This is where we actually invoke userv - all the networks we'll
318 be using should already have been registered. */
321 snprintf(addrs,sizeof(addrs),"%s,%s,%d,slip",
322 ipaddr_to_string(st->slip.nl.local_address),
323 ipaddr_to_string(st->slip.nl.secnet_address),st->slip.nl.mtu);
326 for (r=st->slip.nl.clients; r; r=r->next) {
327 if (r->link_quality > LINK_QUALITY_UNUSED) {
330 nan=ipset_union(allnets,r->networks);
335 snets=ipset_to_subnet_list(allnets);
337 nets=safe_malloc(20*snets->entries,"userv_invoke_userv:nets");
339 for (i=0; i<snets->entries; i++) {
340 s=subnet_to_string(snets->list[i]);
344 nets[strlen(nets)-1]=0;
345 subnet_list_free(snets);
347 Message(M_INFO,"%s: about to invoke: %s %s %s %s %s\n",st->slip.nl.name,
348 st->userv_path,st->service_user,st->service_name,addrs,nets);
350 st->slip.pending_esc=False;
353 pipe_cloexec(c_stdin);
354 pipe_cloexec(c_stdout);
356 st->rxfd=c_stdout[0];
360 /* The arguments are:
364 local-addr,secnet-addr,mtu,protocol
366 const char *er_argv[6];
368 er->argv[0]=st->userv_path;
369 er->argv[1]=st->service_user;
370 er->argv[2]=st->service_name;
374 er->path=st->userv_path;
376 st->pid=makesubproc(userv_entry, userv_userv_callback,
377 er, st, st->slip.nl.name);
381 Message(M_INFO,"%s: userv-ipif pid is %d\n",st->slip.nl.name,st->pid);
382 /* Read a single character from the pipe to confirm userv-ipif is
383 running. If we get a SIGCHLD at this point then we'll get EINTR. */
384 if ((nread=read(st->rxfd,&confirm,1))!=1) {
386 Message(M_WARNING,"%s: read of confirmation byte was "
387 "interrupted\n",st->slip.nl.name);
390 fatal_perror("%s: error reading confirmation byte",
393 fatal("%s: unexpected EOF instead of confirmation byte"
394 " - userv ipif failed?", st->slip.nl.name);
398 if (confirm!=SLIP_END) {
399 fatal("%s: bad confirmation byte %d from userv-ipif",
400 st->slip.nl.name,confirm);
403 setnonblock(st->txfd);
404 setnonblock(st->rxfd);
406 add_hook(PHASE_CHILDPERSIST,childpersist_closefd_hook,&st->txfd);
407 add_hook(PHASE_CHILDPERSIST,childpersist_closefd_hook,&st->rxfd);
410 static void userv_kill_userv(struct userv *st)
413 kill(-st->pid,SIGTERM);
414 st->expecting_userv_exit=True;
418 static void userv_phase_hook(void *sst, uint32_t newphase)
420 struct userv *st=sst;
421 /* We must wait until signal processing has started before forking
423 if (newphase==PHASE_RUN) {
424 userv_invoke_userv(st);
425 /* Register for poll() */
426 register_for_poll(st, userv_beforepoll, userv_afterpoll,
429 if (newphase==PHASE_SHUTDOWN) {
430 userv_kill_userv(st);
434 static list_t *userv_apply(closure_t *self, struct cloc loc, dict_t *context,
443 /* First parameter must be a dict */
444 item=list_elem(args,0);
445 if (!item || item->type!=t_dict)
446 cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n");
448 dict=item->data.dict;
450 slip_init(&st->slip,loc,dict,"netlink-userv-ipif",
451 userv_deliver_to_kernel);
453 st->userv_path=dict_read_string(dict,"userv-path",False,"userv-netlink",
455 st->service_user=dict_read_string(dict,"service-user",False,
456 "userv-netlink",loc);
457 st->service_name=dict_read_string(dict,"service-name",False,
458 "userv-netlink",loc);
459 if (!st->userv_path) st->userv_path="userv";
460 if (!st->service_user) st->service_user="root";
461 if (!st->service_name) st->service_name="ipif";
462 st->rxfd=-1; st->txfd=-1;
464 st->expecting_userv_exit=False;
465 add_hook(PHASE_RUN,userv_phase_hook,st);
466 add_hook(PHASE_SHUTDOWN,userv_phase_hook,st);
468 return new_closure(&st->slip.nl.cl);
471 void slip_module(dict_t *dict)
473 add_closure(dict,"userv-ipif",userv_apply);