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. */
10 #include "unaligned.h"
19 #define SLIP_ESCEND 220
20 #define SLIP_ESCESC 221
24 struct buffer_if
*buff
; /* We unstuff received packets into here
25 and send them to the netlink code. */
27 bool_t ignoring_packet
; /* If this packet was corrupt or overlong,
28 we ignore everything up to the next END */
29 netlink_deliver_fn
*netlink_to_tunnel
;
32 /* Generic SLIP mangling code */
34 static void slip_stuff(struct slip
*st
, struct buffer_if
*buf
, int fd
)
36 uint8_t txbuf
[DEFAULT_BUFSIZE
];
42 /* There's probably a much more efficient way of implementing this */
44 for (i
=buf
->start
; i
<(buf
->start
+buf
->size
); i
++) {
48 txbuf
[j
++]=SLIP_ESCEND
;
52 txbuf
[j
++]=SLIP_ESCESC
;
58 if ((j
+2)>DEFAULT_BUFSIZE
) {
59 if (write(fd
,txbuf
,j
)<0) {
60 fatal_perror("slip_stuff: write()");
66 if (write(fd
,txbuf
,j
)<0) {
67 fatal_perror("slip_stuff: write()");
72 static void slip_unstuff(struct slip
*st
, uint8_t *buf
, uint32_t l
)
76 BUF_ASSERT_USED(st
->buff
);
79 enum { OUTPUT_END
= 256, OUTPUT_NOTHING
= 257 };
82 buffer_init(st
->buff
,calculate_max_start_pad());
84 if (st
->pending_esc
) {
85 st
->pending_esc
=False
;
94 if (!st
->ignoring_packet
) {
95 Message(M_WARNING
, "userv_afterpoll: bad SLIP escape"
96 " character, dropping packet\n");
98 st
->ignoring_packet
=True
;
99 outputchr
=OUTPUT_NOTHING
;
105 outputchr
=OUTPUT_END
;
108 st
->pending_esc
=True
;
109 outputchr
=OUTPUT_NOTHING
;
117 if (st
->ignoring_packet
) {
118 if (outputchr
== OUTPUT_END
) {
119 st
->ignoring_packet
=False
;
123 if (outputchr
== OUTPUT_END
) {
124 if (st
->buff
->size
>0) {
125 st
->netlink_to_tunnel(&st
->nl
,st
->buff
);
126 BUF_ALLOC(st
->buff
,"userv_afterpoll");
129 } else if (outputchr
!= OUTPUT_NOTHING
) {
130 if (st
->buff
->size
< st
->buff
->len
) {
131 buf_append_uint8(st
->buff
,outputchr
);
133 Message(M_WARNING
, "userv_afterpoll: dropping overlong"
135 st
->ignoring_packet
=True
;
142 static void slip_init(struct slip
*st
, struct cloc loc
, dict_t
*dict
,
143 cstring_t name
, netlink_deliver_fn
*to_host
)
145 st
->netlink_to_tunnel
=
146 netlink_init(&st
->nl
,st
,loc
,dict
,
147 "netlink-userv-ipif",NULL
,to_host
);
148 st
->buff
=find_cl_if(dict
,"buffer",CL_BUFFER
,True
,"name",loc
);
149 BUF_ALLOC(st
->buff
,"slip_init");
150 st
->pending_esc
=False
;
151 st
->ignoring_packet
=False
;
154 /* Connection to the kernel through userv-ipif */
158 int txfd
; /* We transmit to userv */
159 int rxfd
; /* We receive from userv */
160 cstring_t userv_path
;
161 cstring_t service_user
;
162 cstring_t service_name
;
164 bool_t expecting_userv_exit
;
167 static int userv_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
170 struct userv
*st
=sst
;
175 fds
[0].events
=0; /* Might want to pick up POLLOUT sometime */
177 fds
[1].events
=POLLIN
;
184 static void userv_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
)
186 struct userv
*st
=sst
;
187 uint8_t rxbuf
[DEFAULT_BUFSIZE
];
192 if (fds
[1].revents
&POLLERR
) {
193 Message(M_ERR
,"%s: userv_afterpoll: POLLERR!\n",st
->slip
.nl
.name
);
195 if (fds
[1].revents
&POLLIN
) {
196 l
=read(st
->rxfd
,rxbuf
,DEFAULT_BUFSIZE
);
199 fatal_perror("%s: userv_afterpoll: read(rxfd)",
202 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
204 } else slip_unstuff(&st
->slip
,rxbuf
,l
);
208 /* Send buf to the kernel. Free buf before returning. */
209 static void userv_deliver_to_kernel(void *sst
, struct buffer_if
*buf
)
211 struct userv
*st
=sst
;
213 if (buf
->size
> st
->slip
.nl
.mtu
) {
214 Message(M_ERR
,"%s: packet of size %"PRIu32
" exceeds mtu %"PRIu32
":"
215 " cannot be injected into kernel, dropped\n",
216 st
->slip
.nl
.name
, buf
->size
, st
->slip
.nl
.mtu
);
221 slip_stuff(&st
->slip
,buf
,st
->txfd
);
224 static void userv_userv_callback(void *sst
, pid_t pid
, int status
)
226 struct userv
*st
=sst
;
229 Message(M_WARNING
,"userv_callback called unexpectedly with pid %d "
230 "(expected %d)\n",pid
,st
->pid
);
233 if (!st
->expecting_userv_exit
) {
234 if (WIFEXITED(status
)) {
235 fatal("%s: userv exited unexpectedly with status %d",
236 st
->slip
.nl
.name
,WEXITSTATUS(status
));
237 } else if (WIFSIGNALED(status
)) {
238 fatal("%s: userv exited unexpectedly: uncaught signal %d",
239 st
->slip
.nl
.name
,WTERMSIG(status
));
241 fatal("%s: userv stopped unexpectedly",
245 Message(M_WARNING
,"%s: userv subprocess died with status %d\n",
246 st
->slip
.nl
.name
,WEXITSTATUS(status
));
250 struct userv_entry_rec
{
255 /* XXX perhaps we should collect and log stderr? */
258 static void userv_entry(void *sst
)
260 struct userv_entry_rec
*st
=sst
;
265 /* XXX close all other fds */
267 /* XXX We really should strdup() all of argv[] but because we'll just
268 exit anyway if execvp() fails it doesn't seem worth bothering. */
269 execvp(st
->path
,(char *const*)st
->argv
);
270 perror("userv-entry: execvp()");
274 static void userv_invoke_userv(struct userv
*st
)
276 struct userv_entry_rec
*er
;
282 struct netlink_client
*r
;
283 struct ipset
*allnets
;
284 struct subnet_list
*snets
;
289 fatal("userv_invoke_userv: already running");
292 /* This is where we actually invoke userv - all the networks we'll
293 be using should already have been registered. */
295 addrs
=safe_malloc(512,"userv_invoke_userv:addrs");
296 snprintf(addrs
,512,"%s,%s,%d,slip",
297 ipaddr_to_string(st
->slip
.nl
.local_address
),
298 ipaddr_to_string(st
->slip
.nl
.secnet_address
),st
->slip
.nl
.mtu
);
301 for (r
=st
->slip
.nl
.clients
; r
; r
=r
->next
) {
302 if (r
->link_quality
> LINK_QUALITY_UNUSED
) {
305 nan
=ipset_union(allnets
,r
->networks
);
310 snets
=ipset_to_subnet_list(allnets
);
312 nets
=safe_malloc(20*snets
->entries
,"userv_invoke_userv:nets");
314 for (i
=0; i
<snets
->entries
; i
++) {
315 s
=subnet_to_string(snets
->list
[i
]);
320 nets
[strlen(nets
)-1]=0;
321 subnet_list_free(snets
);
323 Message(M_INFO
,"%s: about to invoke: %s %s %s %s %s\n",st
->slip
.nl
.name
,
324 st
->userv_path
,st
->service_user
,st
->service_name
,addrs
,nets
);
326 st
->slip
.pending_esc
=False
;
329 if (pipe(c_stdin
)!=0) {
330 fatal_perror("userv_invoke_userv: pipe(c_stdin)");
332 if (pipe(c_stdout
)!=0) {
333 fatal_perror("userv_invoke_userv: pipe(c_stdout)");
336 st
->rxfd
=c_stdout
[0];
338 er
=safe_malloc(sizeof(*r
),"userv_invoke_userv: er");
342 /* The arguments are:
346 local-addr,secnet-addr,mtu,protocol
348 er
->argv
=safe_malloc(sizeof(*er
->argv
)*6,"userv_invoke_userv:argv");
349 er
->argv
[0]=st
->userv_path
;
350 er
->argv
[1]=st
->service_user
;
351 er
->argv
[2]=st
->service_name
;
355 er
->path
=st
->userv_path
;
357 st
->pid
=makesubproc(userv_entry
, userv_userv_callback
,
358 er
, st
, st
->slip
.nl
.name
);
365 Message(M_INFO
,"%s: userv-ipif pid is %d\n",st
->slip
.nl
.name
,st
->pid
);
366 /* Read a single character from the pipe to confirm userv-ipif is
367 running. If we get a SIGCHLD at this point then we'll get EINTR. */
368 if ((nread
=read(st
->rxfd
,&confirm
,1))!=1) {
370 Message(M_WARNING
,"%s: read of confirmation byte was "
371 "interrupted\n",st
->slip
.nl
.name
);
374 fatal_perror("%s: error reading confirmation byte",
377 fatal("%s: unexpected EOF instead of confirmation byte"
378 " - userv ipif failed?", st
->slip
.nl
.name
);
382 if (confirm
!=SLIP_END
) {
383 fatal("%s: bad confirmation byte %d from userv-ipif",
384 st
->slip
.nl
.name
,confirm
);
389 static void userv_kill_userv(struct userv
*st
)
392 kill(-st
->pid
,SIGTERM
);
393 st
->expecting_userv_exit
=True
;
397 static void userv_phase_hook(void *sst
, uint32_t newphase
)
399 struct userv
*st
=sst
;
400 /* We must wait until signal processing has started before forking
402 if (newphase
==PHASE_RUN
) {
403 userv_invoke_userv(st
);
404 /* Register for poll() */
405 register_for_poll(st
, userv_beforepoll
, userv_afterpoll
, 2,
408 if (newphase
==PHASE_SHUTDOWN
) {
409 userv_kill_userv(st
);
413 static list_t
*userv_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
420 st
=safe_malloc(sizeof(*st
),"userv_apply");
422 /* First parameter must be a dict */
423 item
=list_elem(args
,0);
424 if (!item
|| item
->type
!=t_dict
)
425 cfgfatal(loc
,"userv-ipif","parameter must be a dictionary\n");
427 dict
=item
->data
.dict
;
429 slip_init(&st
->slip
,loc
,dict
,"netlink-userv-ipif",
430 userv_deliver_to_kernel
);
432 st
->userv_path
=dict_read_string(dict
,"userv-path",False
,"userv-netlink",
434 st
->service_user
=dict_read_string(dict
,"service-user",False
,
435 "userv-netlink",loc
);
436 st
->service_name
=dict_read_string(dict
,"service-name",False
,
437 "userv-netlink",loc
);
438 if (!st
->userv_path
) st
->userv_path
="userv";
439 if (!st
->service_user
) st
->service_user
="root";
440 if (!st
->service_name
) st
->service_name
="ipif";
441 st
->rxfd
=-1; st
->txfd
=-1;
443 st
->expecting_userv_exit
=False
;
444 add_hook(PHASE_RUN
,userv_phase_hook
,st
);
445 add_hook(PHASE_SHUTDOWN
,userv_phase_hook
,st
);
447 return new_closure(&st
->slip
.nl
.cl
);
450 void slip_module(dict_t
*dict
)
452 add_closure(dict
,"userv-ipif",userv_apply
);