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_write(int fd
, const uint8_t *p
, size_t l
)
37 ssize_t written
=write(fd
,p
,l
);
41 } else if (iswouldblock(errno
)) {
42 lg_perror(0,"slip",0,M_ERR
,errno
,"write() (packet(s) lost)");
45 fatal_perror("slip_stuff: write()");
49 assert((size_t)written
<=l
);
55 static void slip_stuff(struct slip
*st
, struct buffer_if
*buf
, int fd
)
57 uint8_t txbuf
[DEFAULT_BUFSIZE
];
63 /* There's probably a much more efficient way of implementing this */
65 for (i
=buf
->start
; i
<(buf
->start
+buf
->size
); i
++) {
69 txbuf
[j
++]=SLIP_ESCEND
;
73 txbuf
[j
++]=SLIP_ESCESC
;
79 if ((j
+2)>DEFAULT_BUFSIZE
) {
80 slip_write(fd
,txbuf
,j
);
85 slip_write(fd
,txbuf
,j
);
89 static void slip_unstuff(struct slip
*st
, uint8_t *buf
, uint32_t l
)
93 BUF_ASSERT_USED(st
->buff
);
96 enum { OUTPUT_END
= 256, OUTPUT_NOTHING
= 257 };
99 buffer_init(st
->buff
,calculate_max_start_pad());
101 if (st
->pending_esc
) {
102 st
->pending_esc
=False
;
111 if (!st
->ignoring_packet
) {
112 Message(M_WARNING
, "userv_afterpoll: bad SLIP escape"
113 " character, dropping packet\n");
115 st
->ignoring_packet
=True
;
116 outputchr
=OUTPUT_NOTHING
;
122 outputchr
=OUTPUT_END
;
125 st
->pending_esc
=True
;
126 outputchr
=OUTPUT_NOTHING
;
134 if (st
->ignoring_packet
) {
135 if (outputchr
== OUTPUT_END
) {
136 st
->ignoring_packet
=False
;
140 if (outputchr
== OUTPUT_END
) {
141 if (st
->buff
->size
>0) {
142 st
->netlink_to_tunnel(&st
->nl
,st
->buff
);
143 BUF_ALLOC(st
->buff
,"userv_afterpoll");
146 } else if (outputchr
!= OUTPUT_NOTHING
) {
147 if (buf_remaining_space(st
->buff
)) {
148 buf_append_uint8(st
->buff
,outputchr
);
150 Message(M_WARNING
, "userv_afterpoll: dropping overlong"
152 st
->ignoring_packet
=True
;
159 static void slip_init(struct slip
*st
, struct cloc loc
, dict_t
*dict
,
160 cstring_t name
, netlink_deliver_fn
*to_host
)
162 st
->netlink_to_tunnel
=
163 netlink_init(&st
->nl
,st
,loc
,dict
,
164 "netlink-userv-ipif",NULL
,to_host
);
165 st
->buff
=find_cl_if(dict
,"buffer",CL_BUFFER
,True
,"name",loc
);
166 BUF_ALLOC(st
->buff
,"slip_init");
167 st
->pending_esc
=False
;
168 st
->ignoring_packet
=False
;
171 /* Connection to the kernel through userv-ipif */
175 int txfd
; /* We transmit to userv */
176 int rxfd
; /* We receive from userv */
177 cstring_t userv_path
;
178 cstring_t service_user
;
179 cstring_t service_name
;
181 bool_t expecting_userv_exit
;
184 static int userv_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
187 struct userv
*st
=sst
;
190 BEFOREPOLL_WANT_FDS(2);
192 fds
[0].events
=0; /* Might want to pick up POLLOUT sometime */
194 fds
[1].events
=POLLIN
;
196 BEFOREPOLL_WANT_FDS(0);
201 static void userv_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
)
203 struct userv
*st
=sst
;
204 uint8_t rxbuf
[DEFAULT_BUFSIZE
];
209 if (fds
[1].revents
&POLLERR
) {
210 Message(M_ERR
,"%s: userv_afterpoll: POLLERR!\n",st
->slip
.nl
.name
);
212 if (fds
[1].revents
&POLLIN
) {
213 l
=read(st
->rxfd
,rxbuf
,DEFAULT_BUFSIZE
);
215 if (errno
!=EINTR
&& !iswouldblock(errno
))
216 fatal_perror("%s: userv_afterpoll: read(rxfd)",
219 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
221 } else slip_unstuff(&st
->slip
,rxbuf
,l
);
225 /* Send buf to the kernel. Free buf before returning. */
226 static void userv_deliver_to_kernel(void *sst
, struct buffer_if
*buf
)
228 struct userv
*st
=sst
;
230 if (buf
->size
> st
->slip
.nl
.mtu
) {
231 Message(M_ERR
,"%s: packet of size %"PRIu32
" exceeds mtu %"PRIu32
":"
232 " cannot be injected into kernel, dropped\n",
233 st
->slip
.nl
.name
, buf
->size
, st
->slip
.nl
.mtu
);
238 slip_stuff(&st
->slip
,buf
,st
->txfd
);
241 static void userv_userv_callback(void *sst
, pid_t pid
, int status
)
243 struct userv
*st
=sst
;
246 Message(M_WARNING
,"userv_callback called unexpectedly with pid %d "
247 "(expected %d)\n",pid
,st
->pid
);
250 if (!(st
->expecting_userv_exit
&&
252 (WIFSIGNALED(status
) && WTERMSIG(status
)==SIGTERM
)))) {
253 lg_exitstatus(0,st
->slip
.nl
.name
,0,
254 st
->expecting_userv_exit ? M_WARNING
: M_FATAL
,
260 struct userv_entry_rec
{
265 /* XXX perhaps we should collect and log stderr? */
268 static void userv_entry(void *sst
)
270 struct userv_entry_rec
*st
=sst
;
276 execvp(st
->path
,(char *const*)st
->argv
);
277 perror("userv-entry: execvp()");
281 static void userv_invoke_userv(struct userv
*st
)
283 struct userv_entry_rec er
[1];
288 struct netlink_client
*r
;
289 struct ipset
*allnets
;
290 struct subnet_list
*snets
;
295 fatal("userv_invoke_userv: already running");
298 /* This is where we actually invoke userv - all the networks we'll
299 be using should already have been registered. */
302 snprintf(addrs
,sizeof(addrs
),"%s,%s,%d,slip",
303 ipaddr_to_string(st
->slip
.nl
.local_address
),
304 ipaddr_to_string(st
->slip
.nl
.secnet_address
),st
->slip
.nl
.mtu
);
307 for (r
=st
->slip
.nl
.clients
; r
; r
=r
->next
) {
308 if (r
->link_quality
> LINK_QUALITY_UNUSED
) {
311 nan
=ipset_union(allnets
,r
->networks
);
316 snets
=ipset_to_subnet_list(allnets
);
318 nets
=safe_malloc(20*snets
->entries
,"userv_invoke_userv:nets");
320 for (i
=0; i
<snets
->entries
; i
++) {
321 s
=subnet_to_string(snets
->list
[i
]);
325 nets
[strlen(nets
)-1]=0;
326 subnet_list_free(snets
);
328 Message(M_INFO
,"%s: about to invoke: %s %s %s %s %s\n",st
->slip
.nl
.name
,
329 st
->userv_path
,st
->service_user
,st
->service_name
,addrs
,nets
);
331 st
->slip
.pending_esc
=False
;
334 pipe_cloexec(c_stdin
);
335 pipe_cloexec(c_stdout
);
337 st
->rxfd
=c_stdout
[0];
341 /* The arguments are:
345 local-addr,secnet-addr,mtu,protocol
347 const char *er_argv
[6];
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
);
362 Message(M_INFO
,"%s: userv-ipif pid is %d\n",st
->slip
.nl
.name
,st
->pid
);
363 /* Read a single character from the pipe to confirm userv-ipif is
364 running. If we get a SIGCHLD at this point then we'll get EINTR. */
365 if ((nread
=read(st
->rxfd
,&confirm
,1))!=1) {
367 Message(M_WARNING
,"%s: read of confirmation byte was "
368 "interrupted\n",st
->slip
.nl
.name
);
371 fatal_perror("%s: error reading confirmation byte",
374 fatal("%s: unexpected EOF instead of confirmation byte"
375 " - userv ipif failed?", st
->slip
.nl
.name
);
379 if (confirm
!=SLIP_END
) {
380 fatal("%s: bad confirmation byte %d from userv-ipif",
381 st
->slip
.nl
.name
,confirm
);
384 setnonblock(st
->txfd
);
385 setnonblock(st
->rxfd
);
388 static void userv_kill_userv(struct userv
*st
)
391 kill(-st
->pid
,SIGTERM
);
392 st
->expecting_userv_exit
=True
;
396 static void userv_phase_hook(void *sst
, uint32_t newphase
)
398 struct userv
*st
=sst
;
399 /* We must wait until signal processing has started before forking
401 if (newphase
==PHASE_RUN
) {
402 userv_invoke_userv(st
);
403 /* Register for poll() */
404 register_for_poll(st
, userv_beforepoll
, userv_afterpoll
,
407 if (newphase
==PHASE_SHUTDOWN
) {
408 userv_kill_userv(st
);
412 static list_t
*userv_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
419 st
=safe_malloc(sizeof(*st
),"userv_apply");
421 /* First parameter must be a dict */
422 item
=list_elem(args
,0);
423 if (!item
|| item
->type
!=t_dict
)
424 cfgfatal(loc
,"userv-ipif","parameter must be a dictionary\n");
426 dict
=item
->data
.dict
;
428 slip_init(&st
->slip
,loc
,dict
,"netlink-userv-ipif",
429 userv_deliver_to_kernel
);
431 st
->userv_path
=dict_read_string(dict
,"userv-path",False
,"userv-netlink",
433 st
->service_user
=dict_read_string(dict
,"service-user",False
,
434 "userv-netlink",loc
);
435 st
->service_name
=dict_read_string(dict
,"service-name",False
,
436 "userv-netlink",loc
);
437 if (!st
->userv_path
) st
->userv_path
="userv";
438 if (!st
->service_user
) st
->service_user
="root";
439 if (!st
->service_name
) st
->service_name
="ipif";
440 st
->rxfd
=-1; st
->txfd
=-1;
442 st
->expecting_userv_exit
=False
;
443 add_hook(PHASE_RUN
,userv_phase_hook
,st
);
444 add_hook(PHASE_SHUTDOWN
,userv_phase_hook
,st
);
446 return new_closure(&st
->slip
.nl
.cl
);
449 void slip_module(dict_t
*dict
)
451 add_closure(dict
,"userv-ipif",userv_apply
);