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
,
137 struct userv
*st
=sst
;
142 fds
[0].events
=0; /* Might want to pick up POLLOUT sometime */
144 fds
[1].events
=POLLIN
;
151 static void userv_afterpoll(void *sst
, struct pollfd
*fds
, int nfds
)
153 struct userv
*st
=sst
;
154 uint8_t rxbuf
[DEFAULT_BUFSIZE
];
159 if (fds
[1].revents
&POLLERR
) {
160 Message(M_ERR
,"%s: userv_afterpoll: POLLERR!\n",st
->slip
.nl
.name
);
162 if (fds
[1].revents
&POLLIN
) {
163 l
=read(st
->rxfd
,rxbuf
,DEFAULT_BUFSIZE
);
166 fatal_perror("%s: userv_afterpoll: read(rxfd)",
169 fatal("%s: userv_afterpoll: read(rxfd)=0; userv gone away?",
171 } else slip_unstuff(&st
->slip
,rxbuf
,l
);
175 /* Send buf to the kernel. Free buf before returning. */
176 static void userv_deliver_to_kernel(void *sst
, struct buffer_if
*buf
)
178 struct userv
*st
=sst
;
180 slip_stuff(&st
->slip
,buf
,st
->txfd
);
183 static void userv_userv_callback(void *sst
, pid_t pid
, int status
)
185 struct userv
*st
=sst
;
188 Message(M_WARNING
,"userv_callback called unexpectedly with pid %d "
189 "(expected %d)\n",pid
,st
->pid
);
192 if (!st
->expecting_userv_exit
) {
193 if (WIFEXITED(status
)) {
194 fatal("%s: userv exited unexpectedly with status %d",
195 st
->slip
.nl
.name
,WEXITSTATUS(status
));
196 } else if (WIFSIGNALED(status
)) {
197 fatal("%s: userv exited unexpectedly: uncaught signal %d",
198 st
->slip
.nl
.name
,WTERMSIG(status
));
200 fatal("%s: userv stopped unexpectedly");
203 Message(M_WARNING
,"%s: userv subprocess died with status %d\n",
204 st
->slip
.nl
.name
,WEXITSTATUS(status
));
208 struct userv_entry_rec
{
213 /* XXX perhaps we should collect and log stderr? */
216 static void userv_entry(void *sst
)
218 struct userv_entry_rec
*st
=sst
;
223 /* XXX close all other fds */
225 /* XXX We really should strdup() all of argv[] but because we'll just
226 exit anyway if execvp() fails it doesn't seem worth bothering. */
227 execvp(st
->path
,(char *const*)st
->argv
);
228 perror("userv-entry: execvp()");
232 static void userv_invoke_userv(struct userv
*st
)
234 struct userv_entry_rec
*er
;
240 struct netlink_client
*r
;
241 struct ipset
*allnets
;
242 struct subnet_list
*snets
;
247 fatal("userv_invoke_userv: already running");
250 /* This is where we actually invoke userv - all the networks we'll
251 be using should already have been registered. */
253 addrs
=safe_malloc(512,"userv_invoke_userv:addrs");
254 snprintf(addrs
,512,"%s,%s,%d,slip",
255 ipaddr_to_string(st
->slip
.local_address
),
256 ipaddr_to_string(st
->slip
.nl
.secnet_address
),st
->slip
.nl
.mtu
);
259 for (r
=st
->slip
.nl
.clients
; r
; r
=r
->next
) {
260 if (r
->link_quality
> LINK_QUALITY_UNUSED
) {
263 nan
=ipset_union(allnets
,r
->networks
);
268 snets
=ipset_to_subnet_list(allnets
);
270 nets
=safe_malloc(20*snets
->entries
,"userv_invoke_userv:nets");
272 for (i
=0; i
<snets
->entries
; i
++) {
273 s
=subnet_to_string(snets
->list
[i
]);
278 nets
[strlen(nets
)-1]=0;
279 subnet_list_free(snets
);
281 Message(M_INFO
,"%s: about to invoke: %s %s %s %s %s\n",st
->slip
.nl
.name
,
282 st
->userv_path
,st
->service_user
,st
->service_name
,addrs
,nets
);
284 st
->slip
.pending_esc
=False
;
287 if (pipe(c_stdin
)!=0) {
288 fatal_perror("userv_invoke_userv: pipe(c_stdin)");
290 if (pipe(c_stdout
)!=0) {
291 fatal_perror("userv_invoke_userv: pipe(c_stdout)");
294 st
->rxfd
=c_stdout
[0];
296 er
=safe_malloc(sizeof(*r
),"userv_invoke_userv: er");
300 /* The arguments are:
304 local-addr,secnet-addr,mtu,protocol
306 er
->argv
=safe_malloc(sizeof(*er
->argv
)*6,"userv_invoke_userv:argv");
307 er
->argv
[0]=st
->userv_path
;
308 er
->argv
[1]=st
->service_user
;
309 er
->argv
[2]=st
->service_name
;
313 er
->path
=st
->userv_path
;
315 st
->pid
=makesubproc(userv_entry
, userv_userv_callback
,
316 er
, st
, st
->slip
.nl
.name
);
323 Message(M_INFO
,"%s: userv-ipif pid is %d\n",st
->slip
.nl
.name
,st
->pid
);
324 /* Read a single character from the pipe to confirm userv-ipif is
325 running. If we get a SIGCHLD at this point then we'll get EINTR. */
326 if ((nread
=read(st
->rxfd
,&confirm
,1))!=1) {
328 Message(M_WARNING
,"%s: read of confirmation byte was "
329 "interrupted\n",st
->slip
.nl
.name
);
332 fatal_perror("%s: error reading confirmation byte",
335 fatal("%s: unexpected EOF instead of confirmation byte"
336 " - userv ipif failed?", st
->slip
.nl
.name
);
340 if (confirm
!=SLIP_END
) {
341 fatal("%s: bad confirmation byte %d from userv-ipif",
342 st
->slip
.nl
.name
,confirm
);
347 static void userv_kill_userv(struct userv
*st
)
350 kill(-st
->pid
,SIGTERM
);
351 st
->expecting_userv_exit
=True
;
355 static void userv_phase_hook(void *sst
, uint32_t newphase
)
357 struct userv
*st
=sst
;
358 /* We must wait until signal processing has started before forking
360 if (newphase
==PHASE_RUN
) {
361 userv_invoke_userv(st
);
362 /* Register for poll() */
363 register_for_poll(st
, userv_beforepoll
, userv_afterpoll
, 2,
366 if (newphase
==PHASE_SHUTDOWN
) {
367 userv_kill_userv(st
);
371 static list_t
*userv_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
378 st
=safe_malloc(sizeof(*st
),"userv_apply");
380 /* First parameter must be a dict */
381 item
=list_elem(args
,0);
382 if (!item
|| item
->type
!=t_dict
)
383 cfgfatal(loc
,"userv-ipif","parameter must be a dictionary\n");
385 dict
=item
->data
.dict
;
387 slip_init(&st
->slip
,loc
,dict
,"netlink-userv-ipif",
388 userv_deliver_to_kernel
);
390 st
->userv_path
=dict_read_string(dict
,"userv-path",False
,"userv-netlink",
392 st
->service_user
=dict_read_string(dict
,"service-user",False
,
393 "userv-netlink",loc
);
394 st
->service_name
=dict_read_string(dict
,"service-name",False
,
395 "userv-netlink",loc
);
396 if (!st
->userv_path
) st
->userv_path
="userv";
397 if (!st
->service_user
) st
->service_user
="root";
398 if (!st
->service_name
) st
->service_name
="ipif";
399 st
->rxfd
=-1; st
->txfd
=-1;
401 st
->expecting_userv_exit
=False
;
402 add_hook(PHASE_RUN
,userv_phase_hook
,st
);
403 add_hook(PHASE_SHUTDOWN
,userv_phase_hook
,st
);
405 return new_closure(&st
->slip
.nl
.cl
);
408 void slip_module(dict_t
*dict
)
410 add_closure(dict
,"userv-ipif",userv_apply
);