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 /* There's probably a much more efficient way of implementing this */
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 BUF_ASSERT_USED(st
->buff
);
76 if (st
->pending_esc
) {
77 st
->pending_esc
=False
;
80 *(uint8_t *)buf_append(st
->buff
,1)=SLIP_END
;
83 *(uint8_t *)buf_append(st
->buff
,1)=SLIP_ESC
;
86 fatal("userv_afterpoll: bad SLIP escape character");
91 if (st
->buff
->size
>0) {
92 st
->netlink_to_tunnel(&st
->nl
,st
->buff
);
93 BUF_ALLOC(st
->buff
,"userv_afterpoll");
95 buffer_init(st
->buff
,st
->nl
.max_start_pad
);
101 *(uint8_t *)buf_append(st
->buff
,1)=buf
[i
];
108 static void slip_init(struct slip
*st
, struct cloc loc
, dict_t
*dict
,
109 cstring_t name
, netlink_deliver_fn
*to_host
)
111 st
->netlink_to_tunnel
=
112 netlink_init(&st
->nl
,st
,loc
,dict
,
113 "netlink-userv-ipif",NULL
,to_host
);
114 st
->buff
=find_cl_if(dict
,"buffer",CL_BUFFER
,True
,"name",loc
);
115 st
->local_address
=string_item_to_ipaddr(
116 dict_find_item(dict
,"local-address", True
, name
, loc
),"netlink");
117 BUF_ALLOC(st
->buff
,"slip_init");
118 st
->pending_esc
=False
;
121 /* Connection to the kernel through userv-ipif */
125 int txfd
; /* We transmit to userv */
126 int rxfd
; /* We receive from userv */
127 cstring_t userv_path
;
128 cstring_t service_user
;
129 cstring_t service_name
;
131 bool_t expecting_userv_exit
;
134 static int userv_beforepoll(void *sst
, struct pollfd
*fds
, int *nfds_io
,
135 int *timeout_io
, const struct timeval
*tv_now
,
138 struct userv
*st
=sst
;
143 fds
[0].events
=0; /* Might want to pick up POLLOUT sometime */
145 fds
[1].events
=POLLIN
;
152 static void userv_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
,
153 const struct timeval
*tv_now
, uint64_t *now
)
155 struct userv
*st
=sst
;
156 uint8_t rxbuf
[DEFAULT_BUFSIZE
];
161 if (fds
[1].revents
&POLLERR
) {
162 Message(M_ERR
,"%s: userv_afterpoll: POLLERR!\n",st
->slip
.nl
.name
);
164 if (fds
[1].revents
&POLLIN
) {
165 l
=read(st
->rxfd
,rxbuf
,DEFAULT_BUFSIZE
);
168 fatal_perror("%s: userv_afterpoll: read(rxfd)",
171 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
173 } else slip_unstuff(&st
->slip
,rxbuf
,l
);
177 /* Send buf to the kernel. Free buf before returning. */
178 static void userv_deliver_to_kernel(void *sst
, struct buffer_if
*buf
)
180 struct userv
*st
=sst
;
182 slip_stuff(&st
->slip
,buf
,st
->txfd
);
185 static void userv_userv_callback(void *sst
, pid_t pid
, int status
)
187 struct userv
*st
=sst
;
190 Message(M_WARNING
,"userv_callback called unexpectedly with pid %d "
191 "(expected %d)\n",pid
,st
->pid
);
194 if (!st
->expecting_userv_exit
) {
195 if (WIFEXITED(status
)) {
196 fatal("%s: userv exited unexpectedly with status %d",
197 st
->slip
.nl
.name
,WEXITSTATUS(status
));
198 } else if (WIFSIGNALED(status
)) {
199 fatal("%s: userv exited unexpectedly: uncaught signal %d",
200 st
->slip
.nl
.name
,WTERMSIG(status
));
202 fatal("%s: userv stopped unexpectedly");
205 Message(M_WARNING
,"%s: userv subprocess died with status %d\n",
206 st
->slip
.nl
.name
,WEXITSTATUS(status
));
210 struct userv_entry_rec
{
215 /* XXX perhaps we should collect and log stderr? */
218 static void userv_entry(void *sst
)
220 struct userv_entry_rec
*st
=sst
;
225 /* XXX close all other fds */
227 /* XXX We really should strdup() all of argv[] but because we'll just
228 exit anyway if execvp() fails it doesn't seem worth bothering. */
229 execvp(st
->path
,(char *const*)st
->argv
);
230 perror("userv-entry: execvp()");
234 static void userv_invoke_userv(struct userv
*st
)
236 struct userv_entry_rec
*er
;
242 struct netlink_client
*r
;
243 struct ipset
*allnets
;
244 struct subnet_list
*snets
;
249 fatal("userv_invoke_userv: already running");
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
);
261 for (r
=st
->slip
.nl
.clients
; r
; r
=r
->next
) {
265 nan
=ipset_union(allnets
,r
->networks
);
270 snets
=ipset_to_subnet_list(allnets
);
272 nets
=safe_malloc(20*snets
->entries
,"userv_invoke_userv:nets");
274 for (i
=0; i
<snets
->entries
; i
++) {
275 s
=subnet_to_string(snets
->list
[i
]);
280 nets
[strlen(nets
)-1]=0;
281 subnet_list_free(snets
);
283 Message(M_INFO
,"%s: about to invoke: %s %s %s %s %s\n",st
->slip
.nl
.name
,
284 st
->userv_path
,st
->service_user
,st
->service_name
,addrs
,nets
);
286 st
->slip
.pending_esc
=False
;
289 if (pipe(c_stdin
)!=0) {
290 fatal_perror("userv_invoke_userv: pipe(c_stdin)");
292 if (pipe(c_stdout
)!=0) {
293 fatal_perror("userv_invoke_userv: pipe(c_stdout)");
296 st
->rxfd
=c_stdout
[0];
298 er
=safe_malloc(sizeof(*r
),"userv_invoke_userv: er");
302 /* The arguments are:
306 local-addr,secnet-addr,mtu,protocol
308 er
->argv
=safe_malloc(sizeof(*er
->argv
)*6,"userv_invoke_userv:argv");
309 er
->argv
[0]=st
->userv_path
;
310 er
->argv
[1]=st
->service_user
;
311 er
->argv
[2]=st
->service_name
;
315 er
->path
=st
->userv_path
;
317 st
->pid
=makesubproc(userv_entry
, userv_userv_callback
,
318 er
, st
, st
->slip
.nl
.name
);
325 Message(M_INFO
,"%s: userv-ipif pid is %d\n",st
->slip
.nl
.name
,st
->pid
);
326 /* Read a single character from the pipe to confirm userv-ipif is
327 running. If we get a SIGCHLD at this point then we'll get EINTR. */
328 if ((nread
=read(st
->rxfd
,&confirm
,1))!=1) {
330 Message(M_WARNING
,"%s: read of confirmation byte was "
331 "interrupted\n",st
->slip
.nl
.name
);
334 fatal_perror("%s: error reading confirmation byte",
337 fatal("%s: unexpected EOF instead of confirmation byte"
338 " - userv ipif failed?", st
->slip
.nl
.name
);
342 if (confirm
!=SLIP_END
) {
343 fatal("%s: bad confirmation byte %d from userv-ipif",
344 st
->slip
.nl
.name
,confirm
);
349 static void userv_kill_userv(struct userv
*st
)
352 kill(-st
->pid
,SIGTERM
);
353 st
->expecting_userv_exit
=True
;
357 static void userv_phase_hook(void *sst
, uint32_t newphase
)
359 struct userv
*st
=sst
;
360 /* We must wait until signal processing has started before forking
362 if (newphase
==PHASE_RUN
) {
363 userv_invoke_userv(st
);
364 /* Register for poll() */
365 register_for_poll(st
, userv_beforepoll
, userv_afterpoll
, 2,
368 if (newphase
==PHASE_SHUTDOWN
) {
369 userv_kill_userv(st
);
373 static list_t
*userv_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
380 st
=safe_malloc(sizeof(*st
),"userv_apply");
382 /* First parameter must be a dict */
383 item
=list_elem(args
,0);
384 if (!item
|| item
->type
!=t_dict
)
385 cfgfatal(loc
,"userv-ipif","parameter must be a dictionary\n");
387 dict
=item
->data
.dict
;
389 slip_init(&st
->slip
,loc
,dict
,"netlink-userv-ipif",
390 userv_deliver_to_kernel
);
392 st
->userv_path
=dict_read_string(dict
,"userv-path",False
,"userv-netlink",
394 st
->service_user
=dict_read_string(dict
,"service-user",False
,
395 "userv-netlink",loc
);
396 st
->service_name
=dict_read_string(dict
,"service-name",False
,
397 "userv-netlink",loc
);
398 if (!st
->userv_path
) st
->userv_path
="userv";
399 if (!st
->service_user
) st
->service_user
="root";
400 if (!st
->service_name
) st
->service_name
="ipif";
401 st
->rxfd
=-1; st
->txfd
=-1;
403 st
->expecting_userv_exit
=False
;
404 add_hook(PHASE_RUN
,userv_phase_hook
,st
);
405 add_hook(PHASE_SHUTDOWN
,userv_phase_hook
,st
);
407 return new_closure(&st
->slip
.nl
.cl
);
410 void slip_module(dict_t
*dict
)
412 add_closure(dict
,"userv-ipif",userv_apply
);