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 netlink_deliver_fn
*netlink_to_tunnel
;
27 uint32_t local_address
;
30 /* Generic SLIP mangling code */
32 static void slip_stuff(struct slip
*st
, struct buffer_if
*buf
, int fd
)
34 uint8_t txbuf
[DEFAULT_BUFSIZE
];
40 /* XXX crunchy bytestuff code */
42 for (i
=buf
->start
; i
<(buf
->start
+buf
->size
); i
++) {
46 txbuf
[j
++]=SLIP_ESCEND
;
50 txbuf
[j
++]=SLIP_ESCESC
;
56 if ((j
+2)>DEFAULT_BUFSIZE
) {
57 if (write(fd
,txbuf
,j
)<0) {
58 fatal_perror("slip_stuff: write()");
64 if (write(fd
,txbuf
,j
)<0) {
65 fatal_perror("slip_stuff: write()");
70 static void slip_unstuff(struct slip
*st
, uint8_t *buf
, uint32_t l
)
74 /* XXX really crude unstuff code */
75 /* XXX check for buffer overflow */
76 BUF_ASSERT_USED(st
->buff
);
78 if (st
->pending_esc
) {
79 st
->pending_esc
=False
;
82 *(uint8_t *)buf_append(st
->buff
,1)=SLIP_END
;
85 *(uint8_t *)buf_append(st
->buff
,1)=SLIP_ESC
;
88 fatal("userv_afterpoll: bad SLIP escape character\n");
93 if (st
->buff
->size
>0) {
94 st
->netlink_to_tunnel(&st
->nl
,NULL
,
96 BUF_ALLOC(st
->buff
,"userv_afterpoll");
98 buffer_init(st
->buff
,st
->nl
.max_start_pad
);
101 st
->pending_esc
=True
;
104 *(uint8_t *)buf_append(st
->buff
,1)=buf
[i
];
111 static void slip_init(struct slip
*st
, struct cloc loc
, dict_t
*dict
,
112 string_t name
, netlink_deliver_fn
*to_host
)
114 st
->netlink_to_tunnel
=
115 netlink_init(&st
->nl
,st
,loc
,dict
,
116 "netlink-userv-ipif",NULL
,to_host
);
117 st
->buff
=find_cl_if(dict
,"buffer",CL_BUFFER
,True
,"name",loc
);
118 st
->local_address
=string_to_ipaddr(
119 dict_find_item(dict
,"local-address", True
, name
, loc
),"netlink");
120 BUF_ALLOC(st
->buff
,"slip_init");
121 st
->pending_esc
=False
;
124 /* Connection to the kernel through userv-ipif */
128 int txfd
; /* We transmit to userv */
129 int rxfd
; /* We receive from userv */
131 string_t service_user
;
132 string_t service_name
;
134 bool_t expecting_userv_exit
;
137 static int userv_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
138 int *timeout_io
, const struct timeval
*tv_now
,
141 struct userv
*st
=sst
;
146 fds
[0].events
=POLLERR
; /* Might want to pick up POLLOUT sometime */
148 fds
[1].events
=POLLIN
|POLLERR
|POLLHUP
;
155 static void userv_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
,
156 const struct timeval
*tv_now
, uint64_t *now
)
158 struct userv
*st
=sst
;
159 uint8_t rxbuf
[DEFAULT_BUFSIZE
];
164 if (fds
[1].revents
&POLLERR
) {
165 Message(M_ERROR
,"%s: userv_afterpoll: POLLERR!\n",st
->slip
.nl
.name
);
167 if (fds
[1].revents
&POLLIN
) {
168 l
=read(st
->rxfd
,rxbuf
,DEFAULT_BUFSIZE
);
171 fatal_perror("%s: userv_afterpoll: read(rxfd)",
174 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?\n",
176 } else slip_unstuff(&st
->slip
,rxbuf
,l
);
180 /* Send buf to the kernel. Free buf before returning. */
181 static void userv_deliver_to_kernel(void *sst
, void *cid
,
182 struct buffer_if
*buf
)
184 struct userv
*st
=sst
;
186 slip_stuff(&st
->slip
,buf
,st
->txfd
);
189 static void userv_userv_callback(void *sst
, pid_t pid
, int status
)
191 struct userv
*st
=sst
;
194 Message(M_WARNING
,"userv_callback called unexpectedly with pid %d "
195 "(expected %d)\n",pid
,st
->pid
);
198 if (!st
->expecting_userv_exit
) {
199 if (WIFEXITED(status
)) {
200 fatal("%s: userv exited unexpectedly with status %d\n",
201 st
->slip
.nl
.name
,WEXITSTATUS(status
));
202 } else if (WIFSIGNALED(status
)) {
203 fatal("%s: userv exited unexpectedly: uncaught signal %d\n",
204 st
->slip
.nl
.name
,WTERMSIG(status
));
206 fatal("%s: userv stopped unexpectedly\n");
209 Message(M_WARNING
,"%s: userv subprocess died with status %d\n",
210 st
->slip
.nl
.name
,WEXITSTATUS(status
));
214 struct userv_entry_rec
{
219 /* XXX perhaps we should collect and log stderr? */
222 static void userv_entry(void *sst
)
224 struct userv_entry_rec
*st
=sst
;
229 /* XXX close all other fds */
231 execvp(st
->path
,st
->argv
);
232 perror("userv-entry: execvp()");
236 static void userv_invoke_userv(struct userv
*st
)
238 struct userv_entry_rec
*er
;
244 struct netlink_route
*r
;
249 fatal("userv_invoke_userv: already running\n");
252 /* This is where we actually invoke userv - all the networks we'll
253 be using should already have been registered. */
255 addrs
=safe_malloc(512,"userv_invoke_userv:addrs");
256 snprintf(addrs
,512,"%s,%s,%d,slip",
257 ipaddr_to_string(st
->slip
.local_address
),
258 ipaddr_to_string(st
->slip
.nl
.secnet_address
),st
->slip
.nl
.mtu
);
260 nets
=safe_malloc(1024,"userv_invoke_userv:nets");
262 r
=st
->slip
.nl
.routes
;
263 for (i
=0; i
<st
->slip
.nl
.n_routes
; i
++) {
266 s
=subnet_to_string(&r
[i
].net
);
272 nets
[strlen(nets
)-1]=0;
274 Message(M_INFO
,"%s: about to invoke: %s %s %s %s %s\n",st
->slip
.nl
.name
,
275 st
->userv_path
,st
->service_user
,st
->service_name
,addrs
,nets
);
277 st
->slip
.pending_esc
=False
;
280 if (pipe(c_stdin
)!=0) {
281 fatal_perror("userv_invoke_userv: pipe(c_stdin)");
283 if (pipe(c_stdout
)!=0) {
284 fatal_perror("userv_invoke_userv: pipe(c_stdout)");
287 st
->rxfd
=c_stdout
[0];
289 er
=safe_malloc(sizeof(*r
),"userv_invoke_userv: er");
291 er
->stdin
=c_stdin
[0];
292 er
->stdout
=c_stdout
[1];
293 /* The arguments are:
297 local-addr,secnet-addr,mtu,protocol
299 er
->argv
=safe_malloc(sizeof(*er
->argv
)*6,"userv_invoke_userv:argv");
300 er
->argv
[0]=st
->userv_path
;
301 er
->argv
[1]=st
->service_user
;
302 er
->argv
[2]=st
->service_name
;
306 er
->path
=st
->userv_path
;
308 st
->pid
=makesubproc(userv_entry
, userv_userv_callback
,
309 er
, st
, st
->slip
.nl
.name
);
316 Message(M_INFO
,"%s: userv-ipif pid is %d\n",st
->slip
.nl
.name
,st
->pid
);
317 /* Read a single character from the pipe to confirm userv-ipif is
318 running. If we get a SIGCHLD at this point then we'll get EINTR. */
319 if (read(st
->rxfd
,&confirm
,1)!=1) {
321 Message(M_WARNING
,"%s: read of confirmation byte was "
322 "interrupted\n",st
->slip
.nl
.name
);
324 fatal_perror("%s: read() of confirmation byte",st
->slip
.nl
.name
);
327 if (confirm
!=SLIP_END
) {
328 fatal("%s: bad confirmation byte %d from userv-ipif\n",
329 st
->slip
.nl
.name
,confirm
);
332 /* Mark rxfd non-blocking */
333 if (fcntl(st
->rxfd
, F_SETFL
, fcntl(st
->rxfd
, F_GETFL
)|O_NONBLOCK
)==-1) {
334 fatal_perror("%s: fcntl(O_NONBLOCK)",st
->slip
.nl
.name
);
338 static void userv_kill_userv(struct userv
*st
)
341 kill(-st
->pid
,SIGTERM
);
342 st
->expecting_userv_exit
=True
;
346 static void userv_phase_hook(void *sst
, uint32_t newphase
)
348 struct userv
*st
=sst
;
349 /* We must wait until signal processing has started before forking
351 if (newphase
==PHASE_RUN
) {
352 userv_invoke_userv(st
);
353 /* Register for poll() */
354 register_for_poll(st
, userv_beforepoll
, userv_afterpoll
, 2,
357 if (newphase
==PHASE_SHUTDOWN
) {
358 userv_kill_userv(st
);
362 static list_t
*userv_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
369 st
=safe_malloc(sizeof(*st
),"userv_apply");
371 /* First parameter must be a dict */
372 item
=list_elem(args
,0);
373 if (!item
|| item
->type
!=t_dict
)
374 cfgfatal(loc
,"userv-ipif","parameter must be a dictionary\n");
376 dict
=item
->data
.dict
;
378 slip_init(&st
->slip
,loc
,dict
,"netlink-userv-ipif",
379 userv_deliver_to_kernel
);
381 st
->userv_path
=dict_read_string(dict
,"userv-path",False
,"userv-netlink",
383 st
->service_user
=dict_read_string(dict
,"service-user",False
,
384 "userv-netlink",loc
);
385 st
->service_name
=dict_read_string(dict
,"service-name",False
,
386 "userv-netlink",loc
);
387 if (!st
->userv_path
) st
->userv_path
="userv";
388 if (!st
->service_user
) st
->service_user
="root";
389 if (!st
->service_name
) st
->service_name
="ipif";
390 st
->rxfd
=-1; st
->txfd
=-1;
392 st
->expecting_userv_exit
=False
;
393 add_hook(PHASE_RUN
,userv_phase_hook
,st
);
394 add_hook(PHASE_SHUTDOWN
,userv_phase_hook
,st
);
396 return new_closure(&st
->slip
.nl
.cl
);
399 init_module slip_module
;
400 void slip_module(dict_t
*dict
)
402 add_closure(dict
,"userv-ipif",userv_apply
);
405 add_closure(dict
,"pty-slip",ptyslip_apply
);
406 add_closure(dict
,"slipd",slipd_apply
);