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. */
18 #define SLIP_ESCEND 220
19 #define SLIP_ESCESC 221
23 struct buffer_if
*buff
; /* We unstuff received packets into here
24 and send them to the netlink code. */
26 bool_t ignoring_packet
; /* If this packet was corrupt or overlong,
27 we ignore everything up to the next END */
28 netlink_deliver_fn
*netlink_to_tunnel
;
29 uint32_t local_address
;
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 };
81 if (st
->pending_esc
) {
82 st
->pending_esc
=False
;
91 if (!st
->ignoring_packet
) {
92 Message(M_WARNING
, "userv_afterpoll: bad SLIP escape"
93 " character, dropping packet\n");
95 st
->ignoring_packet
=True
;
96 outputchr
=OUTPUT_NOTHING
;
102 outputchr
=OUTPUT_END
;
105 st
->pending_esc
=True
;
106 outputchr
=OUTPUT_NOTHING
;
114 if (st
->ignoring_packet
) {
115 if (outputchr
== OUTPUT_END
) {
116 st
->ignoring_packet
=False
;
117 buffer_init(st
->buff
,st
->nl
.max_start_pad
);
120 if (outputchr
== OUTPUT_END
) {
121 if (st
->buff
->size
>0) {
122 st
->netlink_to_tunnel(&st
->nl
,st
->buff
);
123 BUF_ALLOC(st
->buff
,"userv_afterpoll");
125 buffer_init(st
->buff
,st
->nl
.max_start_pad
);
126 } else if (outputchr
!= OUTPUT_NOTHING
) {
127 if (st
->buff
->size
< st
->buff
->len
) {
128 *(uint8_t *)buf_append(st
->buff
,1)=outputchr
;
130 Message(M_WARNING
, "userv_afterpoll: dropping overlong"
132 st
->ignoring_packet
=True
;
139 static void slip_init(struct slip
*st
, struct cloc loc
, dict_t
*dict
,
140 cstring_t name
, netlink_deliver_fn
*to_host
)
142 st
->netlink_to_tunnel
=
143 netlink_init(&st
->nl
,st
,loc
,dict
,
144 "netlink-userv-ipif",NULL
,to_host
);
145 st
->buff
=find_cl_if(dict
,"buffer",CL_BUFFER
,True
,"name",loc
);
146 st
->local_address
=string_item_to_ipaddr(
147 dict_find_item(dict
,"local-address", True
, name
, loc
),"netlink");
148 BUF_ALLOC(st
->buff
,"slip_init");
149 st
->pending_esc
=False
;
150 st
->ignoring_packet
=False
;
153 /* Connection to the kernel through userv-ipif */
157 int txfd
; /* We transmit to userv */
158 int rxfd
; /* We receive from userv */
159 cstring_t userv_path
;
160 cstring_t service_user
;
161 cstring_t service_name
;
163 bool_t expecting_userv_exit
;
166 static int userv_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
169 struct userv
*st
=sst
;
174 fds
[0].events
=0; /* Might want to pick up POLLOUT sometime */
176 fds
[1].events
=POLLIN
;
183 static void userv_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
)
185 struct userv
*st
=sst
;
186 uint8_t rxbuf
[DEFAULT_BUFSIZE
];
191 if (fds
[1].revents
&POLLERR
) {
192 Message(M_ERR
,"%s: userv_afterpoll: POLLERR!\n",st
->slip
.nl
.name
);
194 if (fds
[1].revents
&POLLIN
) {
195 l
=read(st
->rxfd
,rxbuf
,DEFAULT_BUFSIZE
);
198 fatal_perror("%s: userv_afterpoll: read(rxfd)",
201 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
203 } else slip_unstuff(&st
->slip
,rxbuf
,l
);
207 /* Send buf to the kernel. Free buf before returning. */
208 static void userv_deliver_to_kernel(void *sst
, struct buffer_if
*buf
)
210 struct userv
*st
=sst
;
212 slip_stuff(&st
->slip
,buf
,st
->txfd
);
215 static void userv_userv_callback(void *sst
, pid_t pid
, int status
)
217 struct userv
*st
=sst
;
220 Message(M_WARNING
,"userv_callback called unexpectedly with pid %d "
221 "(expected %d)\n",pid
,st
->pid
);
224 if (!st
->expecting_userv_exit
) {
225 if (WIFEXITED(status
)) {
226 fatal("%s: userv exited unexpectedly with status %d",
227 st
->slip
.nl
.name
,WEXITSTATUS(status
));
228 } else if (WIFSIGNALED(status
)) {
229 fatal("%s: userv exited unexpectedly: uncaught signal %d",
230 st
->slip
.nl
.name
,WTERMSIG(status
));
232 fatal("%s: userv stopped unexpectedly");
235 Message(M_WARNING
,"%s: userv subprocess died with status %d\n",
236 st
->slip
.nl
.name
,WEXITSTATUS(status
));
240 struct userv_entry_rec
{
245 /* XXX perhaps we should collect and log stderr? */
248 static void userv_entry(void *sst
)
250 struct userv_entry_rec
*st
=sst
;
255 /* XXX close all other fds */
257 /* XXX We really should strdup() all of argv[] but because we'll just
258 exit anyway if execvp() fails it doesn't seem worth bothering. */
259 execvp(st
->path
,(char *const*)st
->argv
);
260 perror("userv-entry: execvp()");
264 static void userv_invoke_userv(struct userv
*st
)
266 struct userv_entry_rec
*er
;
272 struct netlink_client
*r
;
273 struct ipset
*allnets
;
274 struct subnet_list
*snets
;
279 fatal("userv_invoke_userv: already running");
282 /* This is where we actually invoke userv - all the networks we'll
283 be using should already have been registered. */
285 addrs
=safe_malloc(512,"userv_invoke_userv:addrs");
286 snprintf(addrs
,512,"%s,%s,%d,slip",
287 ipaddr_to_string(st
->slip
.local_address
),
288 ipaddr_to_string(st
->slip
.nl
.secnet_address
),st
->slip
.nl
.mtu
);
291 for (r
=st
->slip
.nl
.clients
; r
; r
=r
->next
) {
292 if (r
->link_quality
> LINK_QUALITY_UNUSED
) {
295 nan
=ipset_union(allnets
,r
->networks
);
300 snets
=ipset_to_subnet_list(allnets
);
302 nets
=safe_malloc(20*snets
->entries
,"userv_invoke_userv:nets");
304 for (i
=0; i
<snets
->entries
; i
++) {
305 s
=subnet_to_string(snets
->list
[i
]);
310 nets
[strlen(nets
)-1]=0;
311 subnet_list_free(snets
);
313 Message(M_INFO
,"%s: about to invoke: %s %s %s %s %s\n",st
->slip
.nl
.name
,
314 st
->userv_path
,st
->service_user
,st
->service_name
,addrs
,nets
);
316 st
->slip
.pending_esc
=False
;
319 if (pipe(c_stdin
)!=0) {
320 fatal_perror("userv_invoke_userv: pipe(c_stdin)");
322 if (pipe(c_stdout
)!=0) {
323 fatal_perror("userv_invoke_userv: pipe(c_stdout)");
326 st
->rxfd
=c_stdout
[0];
328 er
=safe_malloc(sizeof(*r
),"userv_invoke_userv: er");
332 /* The arguments are:
336 local-addr,secnet-addr,mtu,protocol
338 er
->argv
=safe_malloc(sizeof(*er
->argv
)*6,"userv_invoke_userv:argv");
339 er
->argv
[0]=st
->userv_path
;
340 er
->argv
[1]=st
->service_user
;
341 er
->argv
[2]=st
->service_name
;
345 er
->path
=st
->userv_path
;
347 st
->pid
=makesubproc(userv_entry
, userv_userv_callback
,
348 er
, st
, st
->slip
.nl
.name
);
355 Message(M_INFO
,"%s: userv-ipif pid is %d\n",st
->slip
.nl
.name
,st
->pid
);
356 /* Read a single character from the pipe to confirm userv-ipif is
357 running. If we get a SIGCHLD at this point then we'll get EINTR. */
358 if ((nread
=read(st
->rxfd
,&confirm
,1))!=1) {
360 Message(M_WARNING
,"%s: read of confirmation byte was "
361 "interrupted\n",st
->slip
.nl
.name
);
364 fatal_perror("%s: error reading confirmation byte",
367 fatal("%s: unexpected EOF instead of confirmation byte"
368 " - userv ipif failed?", st
->slip
.nl
.name
);
372 if (confirm
!=SLIP_END
) {
373 fatal("%s: bad confirmation byte %d from userv-ipif",
374 st
->slip
.nl
.name
,confirm
);
379 static void userv_kill_userv(struct userv
*st
)
382 kill(-st
->pid
,SIGTERM
);
383 st
->expecting_userv_exit
=True
;
387 static void userv_phase_hook(void *sst
, uint32_t newphase
)
389 struct userv
*st
=sst
;
390 /* We must wait until signal processing has started before forking
392 if (newphase
==PHASE_RUN
) {
393 userv_invoke_userv(st
);
394 /* Register for poll() */
395 register_for_poll(st
, userv_beforepoll
, userv_afterpoll
, 2,
398 if (newphase
==PHASE_SHUTDOWN
) {
399 userv_kill_userv(st
);
403 static list_t
*userv_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
410 st
=safe_malloc(sizeof(*st
),"userv_apply");
412 /* First parameter must be a dict */
413 item
=list_elem(args
,0);
414 if (!item
|| item
->type
!=t_dict
)
415 cfgfatal(loc
,"userv-ipif","parameter must be a dictionary\n");
417 dict
=item
->data
.dict
;
419 slip_init(&st
->slip
,loc
,dict
,"netlink-userv-ipif",
420 userv_deliver_to_kernel
);
422 st
->userv_path
=dict_read_string(dict
,"userv-path",False
,"userv-netlink",
424 st
->service_user
=dict_read_string(dict
,"service-user",False
,
425 "userv-netlink",loc
);
426 st
->service_name
=dict_read_string(dict
,"service-name",False
,
427 "userv-netlink",loc
);
428 if (!st
->userv_path
) st
->userv_path
="userv";
429 if (!st
->service_user
) st
->service_user
="root";
430 if (!st
->service_name
) st
->service_name
="ipif";
431 st
->rxfd
=-1; st
->txfd
=-1;
433 st
->expecting_userv_exit
=False
;
434 add_hook(PHASE_RUN
,userv_phase_hook
,st
);
435 add_hook(PHASE_SHUTDOWN
,userv_phase_hook
,st
);
437 return new_closure(&st
->slip
.nl
.cl
);
440 void slip_module(dict_t
*dict
)
442 add_closure(dict
,"userv-ipif",userv_apply
);