Commit | Line | Data |
---|---|---|
ce5c098f IJ |
1 | /* polypath |
2 | * send/receive module for secnet | |
3 | * for multi-route setups */ | |
4 | ||
5 | #include "secnet.h" | |
6 | #include "util.h" | |
7 | #include "unaligned.h" | |
8 | #include "comm-common.h" | |
9 | ||
10 | #include <adns.h> | |
11 | #include <ctype.h> | |
12 | ||
13 | #ifdef CONFIG_IPV6 | |
14 | ||
15 | static comm_sendmsg_fn polypath_sendmsg; | |
16 | ||
17 | struct interf { | |
18 | char *name; /* from malloc */ | |
19 | struct udpsocks socks; | |
20 | bool_t experienced_xmit_noaf[MAX_AF+1]; | |
21 | LIST_ENTRY(interf) entry; | |
22 | }; | |
23 | ||
24 | struct polypath { | |
25 | struct udpcommon uc; | |
26 | int max_interfs; | |
27 | const char *const *ifname_pats; | |
28 | const char *const *monitor_command; | |
29 | bool_t permit_loopback; | |
30 | LIST_HEAD(,interf) interfs; | |
31 | struct buffer_if lbuf; | |
32 | int monitor_fd; | |
33 | pid_t monitor_pid; | |
93cdea57 IJ |
34 | int privsep_incoming_fd; |
35 | int privsep_ipcsock_fd; | |
ce5c098f IJ |
36 | }; |
37 | ||
93cdea57 IJ |
38 | static void polypath_phase_shutdown(void *sst, uint32_t newphase); |
39 | ||
ce5c098f IJ |
40 | #define LG 0, st->uc.cc.cl.description, &st->uc.cc.loc |
41 | ||
42 | static const char *const default_loopback_ifname_pats[] = { | |
43 | "!lo", 0 | |
44 | }; | |
45 | static const char *const default_ifname_pats[] = { | |
46 | "!tun*","!tap*","!sl*","!userv*", "*", 0 | |
47 | }; | |
48 | ||
49 | static const char *const default_monitor_command[] = { | |
e61a41a4 IJ |
50 | #if __linux__ |
51 | DATAROOTDIR "/secnet/" "polypath-interface-monitor-linux", 0 | |
52 | #else | |
ce5c098f | 53 | 0 |
e61a41a4 | 54 | #endif |
ce5c098f IJ |
55 | }; |
56 | ||
57 | static const char *polypath_addr_to_string(void *commst, | |
58 | const struct comm_addr *ca) | |
59 | { | |
60 | static char sbuf[100]; | |
61 | ||
62 | snprintf(sbuf, sizeof(sbuf), "polypath:%s", | |
63 | iaddr_to_string(&ca->ia)); | |
64 | return sbuf; | |
65 | } | |
66 | ||
67 | static bool_t ifname_search_pats(struct polypath *st, struct cloc loc, | |
68 | const char *ifname, bool_t *want_io, | |
69 | const char *const *pats) { | |
70 | /* Returns True iff we found a list entry, in which case *want_io | |
71 | * is set to the sense of that entry. Otherwise *want_io is set | |
72 | * to the sense of the last entry, or unchanged if there were no pats. */ | |
73 | if (!pats) | |
74 | return False; | |
75 | const char *const *pati; | |
76 | for (pati=pats; *pati; pati++) { | |
77 | const char *pat=*pati; | |
78 | if (*pat=='!') { *want_io=False; pat++; } | |
79 | else if (*pat=='+') { *want_io=True; pat++; } | |
80 | else if (*pat=='*' || isalnum((unsigned char)*pat)) { *want_io=True; } | |
81 | else cfgfatal(loc,"polypath","invalid interface name pattern `%s'",pat); | |
82 | int match=fnmatch(pat,ifname,0); | |
83 | if (match==0) return True; | |
84 | if (match!=FNM_NOMATCH) | |
85 | cfgfatal(loc,"polypath","fnmatch failed! (pattern `%s')",pat); | |
86 | } | |
87 | return False; | |
88 | } | |
89 | ||
90 | static bool_t ifname_wanted(struct polypath *st, struct cloc loc, | |
91 | const char *ifname) { | |
92 | bool_t want=False; /* pretend an empty cfg ends with !<doesn'tmatch> */ | |
93 | if (ifname_search_pats(st,loc,ifname,&want, st->ifname_pats)) | |
94 | return want; | |
95 | if (want) /* last pattern was positive, do not search default */ | |
96 | return False; | |
97 | if (!st->permit_loopback && | |
98 | ifname_search_pats(st,loc,ifname,&want, default_loopback_ifname_pats)) | |
99 | return want; | |
100 | if (ifname_search_pats(st,loc,ifname,&want, default_ifname_pats)) | |
101 | return want; | |
102 | abort(); | |
103 | } | |
104 | ||
105 | static int polypath_beforepoll(void *state, struct pollfd *fds, int *nfds_io, | |
106 | int *timeout_io) | |
107 | { | |
108 | struct polypath *st=state; | |
109 | BEFOREPOLL_WANT_FDS(1); | |
110 | fds[0].fd=st->monitor_fd; | |
111 | fds[0].events=POLLIN; | |
112 | return 0; | |
113 | } | |
114 | ||
ce5c098f IJ |
115 | static inline bool_t matches32(uint32_t word, uint32_t prefix, int prefixlen) |
116 | { | |
117 | assert(prefixlen>0); | |
118 | assert(prefixlen<=32); | |
119 | uint32_t mask = ~(((uint32_t)1 << (32-prefixlen)) - 1); | |
120 | assert(!(prefix & ~mask)); | |
121 | return (word & mask) == prefix; | |
122 | } | |
123 | ||
70ad8e2c IJ |
124 | /* These macros expect |
125 | * bad_fn_type *const bad; | |
126 | * void *badctx; | |
127 | * and | |
128 | * out: | |
129 | */ | |
84dcd42d IJ |
130 | #define BAD(m) do{ bad(st,badctx,M_WARNING,m,0); goto out; }while(0) |
131 | #define BADE(m,ev) do{ bad(st,badctx,M_WARNING,m,ev); goto out; }while(0) | |
70ad8e2c | 132 | typedef void bad_fn_type(struct polypath *st, void *badctx, |
84dcd42d | 133 | int mclass, const char* m, int ev); |
70ad8e2c IJ |
134 | |
135 | typedef void polypath_ppml_callback_type(struct polypath *st, | |
136 | bad_fn_type *bad, void *badctx, | |
137 | bool_t add, const char *ifname, const char *ifaddr, | |
138 | const union iaddr *ia, int fd /* -1 if none yet */); | |
139 | ||
140 | struct ppml_bad_ctx { | |
141 | const char *orgl; | |
142 | char *undospace; | |
143 | }; | |
144 | ||
84dcd42d IJ |
145 | static void ppml_bad(struct polypath *st, void *badctx, |
146 | int mclass, const char *m, int ev) | |
70ad8e2c IJ |
147 | { |
148 | struct ppml_bad_ctx *bc=badctx; | |
149 | if (bc->undospace) | |
150 | *(bc->undospace)=' '; | |
84dcd42d | 151 | lg_perror(LG,mclass,ev, |
70ad8e2c IJ |
152 | "error processing polypath state change: %s" |
153 | " (while processing `%s')", | |
154 | m,bc->orgl); | |
155 | } | |
156 | ||
157 | static void polypath_process_monitor_line(struct polypath *st, char *orgl, | |
158 | polypath_ppml_callback_type *callback) | |
159 | /* always calls callback with fd==-1 */ | |
ce5c098f | 160 | { |
ce5c098f | 161 | struct udpcommon *uc=&st->uc; |
ce5c098f | 162 | char *l=orgl; |
70ad8e2c IJ |
163 | bad_fn_type (*const bad)=ppml_bad; |
164 | struct ppml_bad_ctx badctx[1]={{ | |
165 | .orgl=orgl, | |
166 | .undospace=0 | |
167 | }}; | |
ce5c098f IJ |
168 | |
169 | bool_t add; | |
170 | int c=*l++; | |
171 | if (c=='+') add=True; | |
172 | else if (c=='-') add=False; | |
173 | else BAD("bad +/-"); | |
174 | ||
175 | int proto; | |
176 | c=*l++; | |
177 | if (c=='4') proto=AF_INET; | |
178 | else if (c=='6') proto=AF_INET6; | |
179 | else BAD("bad proto"); | |
180 | ||
181 | char *space=strchr(l,' '); | |
182 | if (!space) BAD("no first space"); | |
183 | const char *ifname=space+1; | |
184 | ||
185 | space=strchr(ifname,' '); | |
186 | if (!space) BAD("no second space"); | |
187 | const char *ifaddr=space+1; | |
188 | *space=0; | |
70ad8e2c | 189 | badctx->undospace=space; |
ce5c098f IJ |
190 | |
191 | union iaddr ia; | |
192 | FILLZERO(ia); | |
193 | socklen_t salen=sizeof(ia); | |
194 | int r=adns_text2addr(ifaddr,uc->port, adns_qf_addrlit_ipv4_quadonly, | |
195 | &ia.sa, &salen); | |
196 | assert(r!=ENOSPC); | |
197 | if (r) BADE("adns_text2addr",r); | |
198 | if (ia.sa.sa_family!=proto) BAD("address family mismatch"); | |
199 | ||
200 | #define DONT(m) do{ \ | |
201 | if (add) \ | |
202 | lg_perror(LG,M_INFO,0,"ignoring %s [%s]: %s",ifname,ifaddr,m); \ | |
203 | goto out; \ | |
204 | }while(0) | |
205 | ||
206 | if (!ifname_wanted(st,st->uc.cc.loc,ifname)) | |
207 | DONT("unwanted interface name"); | |
208 | ||
209 | switch (ia.sa.sa_family) { | |
210 | case AF_INET6: { | |
211 | const struct in6_addr *i6=&ia.sin6.sin6_addr; | |
212 | #define DONTKIND(X,m) \ | |
213 | if (IN6_IS_ADDR_##X(i6)) DONT("IPv6 address is " m) | |
214 | DONTKIND(UNSPECIFIED, "unspecified"); | |
215 | DONTKIND(MULTICAST , "multicast" ); | |
216 | DONTKIND(LINKLOCAL , "link local" ); | |
217 | DONTKIND(SITELOCAL , "site local" ); | |
218 | DONTKIND(V4MAPPED , "v4-mapped" ); | |
219 | if (!st->permit_loopback) | |
220 | DONTKIND(LOOPBACK , "loopback" ); | |
221 | #undef DONTKIND | |
222 | #define DONTMASK(w7x,w6x,prefixlen,m) \ | |
223 | if (matches32(get_uint32(i6->s6_addr), \ | |
224 | ((uint32_t)0x##w7x << 16) | (uint32_t)0x##w6x, \ | |
225 | prefixlen)) \ | |
226 | DONT("IPv6 address is " m) | |
227 | DONTMASK( 100, 0, 8, "Discard-Only (RFC6666)"); | |
228 | DONTMASK(2001, 0, 23, "in IETF protocol block (RFC2928)"); | |
b073e347 | 229 | DONTMASK(fc00, 0, 7, "Uniqe Local unicast (RFC4193)"); |
ce5c098f IJ |
230 | #undef DONTMASK |
231 | break; | |
232 | } | |
233 | case AF_INET: { | |
234 | const uint32_t i4=htonl(ia.sin.sin_addr.s_addr); | |
235 | if (i4==INADDR_ANY) DONT("IPv4 address is any/unspecified"); | |
236 | if (i4==INADDR_BROADCAST) DONT("IPv4 address is all hosts broadcast"); | |
237 | #define DONTMASK(b3,b2,b1,b0,prefixlen,m) do{ \ | |
238 | const uint8_t prefixbytes[4] = { (b3),(b2),(b1),(b0) }; \ | |
239 | if (matches32(i4,get_uint32(prefixbytes),prefixlen)) \ | |
240 | DONT("IPv4 address is " m); \ | |
241 | }while(0) | |
242 | DONTMASK(169,254,0,0, 16, "link local"); | |
243 | DONTMASK(224, 0,0,0, 4, "multicast"); | |
244 | DONTMASK(192, 0,0,0, 24, "in IETF protocol block (RFC6890)"); | |
245 | DONTMASK(240, 0,0,0, 4, "in reserved addressing block (RFC1112)"); | |
246 | if (!st->permit_loopback) | |
247 | DONTMASK(127, 0,0,0, 8, "loopback"); | |
248 | #undef DONTMASK | |
249 | break; | |
250 | } | |
251 | default: | |
252 | abort(); | |
253 | } | |
254 | ||
70ad8e2c IJ |
255 | #undef DONT |
256 | ||
ce5c098f | 257 | /* OK, process it */ |
70ad8e2c IJ |
258 | callback(st, bad,badctx, add,ifname,ifaddr,&ia,-1); |
259 | ||
260 | out:; | |
261 | } | |
262 | ||
263 | static void dump_pria(struct polypath *st, const char *ifname) | |
264 | { | |
265 | #ifdef POLYPATH_DEBUG | |
266 | struct interf *interf; | |
267 | if (ifname) | |
268 | lg_perror(LG,M_DEBUG,0, "polypath record ifaddr `%s'",ifname); | |
269 | LIST_FOREACH(interf, &st->interfs, entry) { | |
270 | lg_perror(LG,M_DEBUG,0, " polypath interface `%s', nsocks=%d", | |
271 | interf->name, interf->socks.n_socks); | |
272 | int i; | |
273 | for (i=0; i<interf->socks.n_socks; i++) { | |
274 | struct udpsock *us=&interf->socks.socks[i]; | |
275 | lg_perror(LG,M_DEBUG,0, " polypath sock fd=%d addr=%s", | |
276 | us->fd, iaddr_to_string(&us->addr)); | |
277 | } | |
278 | } | |
279 | #endif | |
280 | } | |
281 | ||
282 | static bool_t polypath_make_socket(struct polypath *st, | |
283 | bad_fn_type *bad, void *badctx, | |
284 | struct udpsock *us, const char *ifname) | |
285 | /* on error exit has called bad; might leave us->fd as -1 */ | |
286 | { | |
287 | assert(us->fd==-1); | |
288 | ||
289 | bool_t ok=udp_make_socket(&st->uc,us,M_WARNING); | |
290 | if (!ok) BAD("unable to set up socket"); | |
291 | int r=setsockopt(us->fd,SOL_SOCKET,SO_BINDTODEVICE, | |
292 | ifname,strlen(ifname)+1); | |
293 | if (r) BADE("setsockopt(,,SO_BINDTODEVICE,)",errno); | |
294 | return True; | |
295 | ||
296 | out: | |
297 | return False; | |
298 | } | |
299 | ||
300 | static void polypath_record_ifaddr(struct polypath *st, | |
301 | bad_fn_type *bad, void *badctx, | |
302 | bool_t add, const char *ifname, | |
303 | const char *ifaddr, | |
304 | const union iaddr *ia, int fd) | |
305 | { | |
306 | struct udpcommon *uc=&st->uc; | |
307 | struct interf *interf=0; | |
308 | struct udpsock *us=0; | |
309 | ||
310 | dump_pria(st,ifname); | |
311 | ||
ce5c098f IJ |
312 | int n_ifs=0; |
313 | LIST_FOREACH(interf,&st->interfs,entry) { | |
314 | if (!strcmp(interf->name,ifname)) | |
315 | goto found_interf; | |
316 | n_ifs++; | |
317 | } | |
318 | /* not found */ | |
319 | if (n_ifs==st->max_interfs) BAD("too many interfaces"); | |
320 | interf=malloc(sizeof(*interf)); | |
321 | if (!interf) BADE("malloc for new interface",errno); | |
322 | interf->name=0; | |
323 | interf->socks.n_socks=0; | |
324 | FILLZERO(interf->experienced_xmit_noaf); | |
325 | LIST_INSERT_HEAD(&st->interfs,interf,entry); | |
ce5c098f | 326 | interf->name=strdup(ifname); |
c72d679d | 327 | udp_socks_register(&st->uc,&interf->socks,interf->name); |
ce5c098f IJ |
328 | if (!interf->name) BADE("strdup interface name",errno); |
329 | found_interf: | |
330 | ||
331 | if (add) { | |
332 | if (interf->socks.n_socks == UDP_MAX_SOCKETS) | |
333 | BAD("too many addresses on this interface"); | |
334 | struct udpsock *us=&interf->socks.socks[interf->socks.n_socks]; | |
335 | us->fd=-1; | |
70ad8e2c IJ |
336 | COPY_OBJ(us->addr,*ia); |
337 | if (fd<0) { | |
338 | bool_t ok=polypath_make_socket(st,bad,badctx, us,ifname); | |
339 | if (!ok) goto out; | |
340 | } else { | |
9c44ef13 IJ |
341 | bool_t ok=udp_import_socket(uc,us,M_WARNING,fd); |
342 | if (!ok) goto out; | |
70ad8e2c IJ |
343 | fd=-1; |
344 | } | |
ce5c098f | 345 | interf->socks.n_socks++; |
9c44ef13 IJ |
346 | lg_perror(LG,M_INFO,0,"using %s %s",ifname, |
347 | iaddr_to_string(&us->addr)); | |
ce5c098f | 348 | us=0; /* do not destroy this socket during `out' */ |
ce5c098f IJ |
349 | } else { |
350 | int i; | |
351 | for (i=0; i<interf->socks.n_socks; i++) | |
9c44ef13 | 352 | if (iaddr_equal(&interf->socks.socks[i].addr,ia,True)) |
ce5c098f | 353 | goto address_remove_found; |
c69b984e IJ |
354 | bad(st,badctx,M_DEBUG,"address to remove not found",0); |
355 | goto out; | |
ce5c098f | 356 | address_remove_found: |
9c44ef13 IJ |
357 | lg_perror(LG,M_INFO,0,"removed %s %s",ifname, |
358 | iaddr_to_string(&interf->socks.socks[i].addr)); | |
ce5c098f IJ |
359 | udp_destroy_socket(&st->uc,&interf->socks.socks[i]); |
360 | interf->socks.socks[i]= | |
361 | interf->socks.socks[--interf->socks.n_socks]; | |
362 | } | |
363 | ||
364 | out: | |
365 | if (us) | |
366 | udp_destroy_socket(uc,us); | |
70ad8e2c IJ |
367 | if (fd>=0) |
368 | close(fd); | |
ce5c098f IJ |
369 | if (interf && !interf->socks.n_socks) { |
370 | udp_socks_deregister(&st->uc,&interf->socks); | |
371 | LIST_REMOVE(interf,entry); | |
372 | free(interf->name); | |
373 | free(interf); | |
374 | } | |
375 | ||
70ad8e2c | 376 | dump_pria(st,0); |
ce5c098f IJ |
377 | } |
378 | ||
23ca1537 IJ |
379 | static void subproc_problem(struct polypath *st, |
380 | enum async_linebuf_result alr, const char *emsg) | |
ce5c098f | 381 | { |
ce5c098f | 382 | int status; |
ce5c098f IJ |
383 | assert(st->monitor_pid); |
384 | ||
385 | pid_t gotpid=waitpid(st->monitor_pid,&status,WNOHANG); | |
386 | if (gotpid==st->monitor_pid) { | |
387 | st->monitor_pid=0; | |
388 | lg_exitstatus(LG,M_FATAL,status,"interface monitor"); | |
389 | } else if (gotpid<0) | |
390 | lg_perror(LG,M_ERR,errno,"unable to reap interface monitor"); | |
391 | else | |
392 | assert(gotpid==0); | |
393 | ||
394 | if (alr==async_linebuf_eof) | |
395 | lg_perror(LG,M_FATAL,0,"unexpected EOF from interface monitor"); | |
396 | else | |
397 | lg_perror(LG,M_FATAL,0,"bad output from interface monitor: %s",emsg); | |
ce5c098f IJ |
398 | assert(!"not reached"); |
399 | } | |
400 | ||
93cdea57 | 401 | /* Used in non-privsep case, and in privsep child */ |
23ca1537 IJ |
402 | static void afterpoll_monitor(struct polypath *st, struct pollfd *fd, |
403 | polypath_ppml_callback_type *callback) | |
404 | { | |
405 | enum async_linebuf_result alr; | |
406 | const char *emsg; | |
407 | ||
408 | while ((alr=async_linebuf_read(fd,&st->lbuf,&emsg)) == async_linebuf_ok) | |
409 | polypath_process_monitor_line(st,st->lbuf.base,callback); | |
410 | ||
411 | if (alr==async_linebuf_nothing) | |
412 | return; | |
413 | ||
414 | subproc_problem(st,alr,emsg); | |
415 | } | |
416 | ||
93cdea57 | 417 | /* Used in non-privsep case only - glue for secnet main loop */ |
23ca1537 IJ |
418 | static void polypath_afterpoll_monitor(void *state, struct pollfd *fds, |
419 | int nfds) | |
420 | { | |
421 | struct polypath *st=state; | |
422 | if (nfds<1) return; | |
423 | afterpoll_monitor(st,fds,polypath_record_ifaddr); | |
424 | } | |
425 | ||
ce5c098f IJ |
426 | /* Actual udp packet sending work */ |
427 | static bool_t polypath_sendmsg(void *commst, struct buffer_if *buf, | |
428 | const struct comm_addr *dest) | |
429 | { | |
430 | struct polypath *st=commst; | |
431 | struct interf *interf; | |
432 | bool_t allreasonable=True; | |
433 | int af=dest->ia.sa.sa_family; | |
434 | ||
435 | LIST_FOREACH(interf,&st->interfs,entry) { | |
436 | int i; | |
437 | bool_t attempted=False, reasonable=False; | |
438 | for (i=0; i<interf->socks.n_socks; i++) { | |
439 | struct udpsock *us=&interf->socks.socks[i]; | |
440 | if (af != us->addr.sa.sa_family) | |
441 | continue; | |
442 | attempted=True; | |
443 | int r=sendto(us->fd,buf->start,buf->size, | |
444 | 0,&dest->ia.sa,iaddr_socklen(&dest->ia)); | |
c72d679d | 445 | udp_sock_experienced(0,&st->uc,&interf->socks,us, |
6c5889e6 | 446 | &dest->ia,af, r,errno); |
ce5c098f IJ |
447 | if (r>=0) { |
448 | reasonable=True; | |
449 | break; | |
450 | } | |
451 | if (!(errno==EAFNOSUPPORT || errno==ENETUNREACH)) | |
452 | reasonable=True; | |
453 | lg_perror(LG,M_DEBUG,errno,"%s [%s] xmit %"PRIu32" bytes to %s", | |
454 | interf->name,iaddr_to_string(&us->addr), | |
455 | buf->size,iaddr_to_string(&dest->ia)); | |
456 | } | |
457 | if (!attempted) | |
458 | if (!interf->experienced_xmit_noaf[af]++) | |
459 | lg_perror(LG,M_WARNING,0, | |
460 | "%s has no suitable address to transmit %s", | |
461 | interf->name, af_name(af)); | |
462 | allreasonable *= reasonable; | |
463 | } | |
464 | return allreasonable; | |
465 | } | |
466 | ||
93cdea57 | 467 | /* Non-privsep: called in (sole) child. Privsep: in grandchild. */ |
23ca1537 IJ |
468 | static void child_monitor(struct polypath *st, int childfd) |
469 | { | |
470 | dup2(childfd,1); | |
471 | execvp(st->monitor_command[0],(char**)st->monitor_command); | |
472 | fprintf(stderr,"secnet: cannot execute %s: %s\n", | |
473 | st->monitor_command[0], strerror(errno)); | |
474 | exit(-1); | |
475 | } | |
476 | ||
93cdea57 | 477 | /* General utility function. */ |
23ca1537 | 478 | static void start_subproc(struct polypath *st, void (*make_fdpair)(int[2]), |
83e03cac IJ |
479 | void (*child)(struct polypath *st, int childfd), |
480 | const char *desc) | |
ce5c098f | 481 | { |
ce5c098f IJ |
482 | int pfds[2]; |
483 | ||
484 | assert(!st->monitor_pid); | |
485 | assert(st->monitor_fd<0); | |
486 | ||
23ca1537 | 487 | make_fdpair(pfds); |
ce5c098f IJ |
488 | |
489 | pid_t pid=fork(); | |
490 | if (!pid) { | |
491 | afterfork(); | |
94a1d5fc | 492 | close(pfds[0]); |
23ca1537 IJ |
493 | child(st,pfds[1]); |
494 | abort(); | |
ce5c098f IJ |
495 | } |
496 | if (pid<0) | |
23ca1537 | 497 | fatal_perror("%s: failed to fork for interface monitoring", |
ce5c098f IJ |
498 | st->uc.cc.cl.description); |
499 | ||
500 | close(pfds[1]); | |
501 | st->monitor_pid=pid; | |
502 | st->monitor_fd=pfds[0]; | |
503 | setnonblock(st->monitor_fd); | |
83e03cac IJ |
504 | |
505 | lg_perror(LG,M_NOTICE,0, "%s: spawning %s [pid %ld]", | |
506 | st->uc.cc.cl.description, desc, (long)st->monitor_pid); | |
23ca1537 | 507 | } |
ce5c098f | 508 | |
93cdea57 | 509 | /* Non-privsep only: glue for forking the monitor, from the main loop */ |
23ca1537 IJ |
510 | static void polypath_phase_startmonitor(void *sst, uint32_t newphase) |
511 | { | |
512 | struct polypath *st=sst; | |
93cdea57 IJ |
513 | start_subproc(st,pipe_cloexec,child_monitor, |
514 | "interface monitor (no privsep)"); | |
23ca1537 IJ |
515 | register_for_poll(st,polypath_beforepoll, |
516 | polypath_afterpoll_monitor,"polypath"); | |
ce5c098f IJ |
517 | } |
518 | ||
93cdea57 IJ |
519 | /*----- Privsep-only: -----*/ |
520 | ||
521 | /* | |
522 | * We use two subprocesses, a child and a grandchild. These are | |
523 | * forked before secnet drops privilege. | |
524 | * | |
525 | * The grandchild is the same interface monitor helper script as used | |
526 | * in the non-privsep case. But its lines are read by the child | |
527 | * instead of by the main secnet. The child is responsible for | |
528 | * creating the actual socket (binding it, etc.). Each socket is | |
529 | * passed to secnet proper via fd passing, along with a data message | |
530 | * describing the interface name and address. The child does not | |
531 | * retain a list of current interfaces and addresses - it trusts the | |
532 | * interface monitor to get that right. secnet proper still maintains | |
533 | * that data structure. | |
534 | * | |
535 | * The result is that much of the non-privsep code can be reused, but | |
536 | * just plumbed together differently. | |
537 | * | |
538 | * The child does not retain the socket after passing it on. | |
539 | * Interface removals are handled similarly but without any fd. | |
540 | * | |
541 | * The result is that the configuration's limits on which interfaces | |
542 | * and ports secnet may use are enforced by the privileged child. | |
543 | */ | |
544 | ||
545 | struct privsep_mdata { | |
546 | bool_t add; | |
547 | char ifname[100]; | |
548 | union iaddr ia; | |
549 | }; | |
550 | ||
84dcd42d IJ |
551 | static void papp_bad(struct polypath *st, void *badctx, |
552 | int mclass, const char *m, int ev) | |
93cdea57 IJ |
553 | { |
554 | const struct privsep_mdata *mdata=(const void*)st->lbuf.start; | |
555 | const char *addr_str=badctx; | |
556 | ||
84dcd42d | 557 | lg_perror(LG,mclass,ev, |
93cdea57 IJ |
558 | "error processing polypath address change %s %s [%s]: %s", |
559 | mdata->add ? "+" : "-", | |
560 | mdata->ifname, addr_str, m); | |
561 | } | |
562 | ||
563 | static void polypath_afterpoll_privsep(void *state, struct pollfd *fds, | |
564 | int nfds) | |
565 | /* In secnet proper; receives messages from child. */ | |
566 | { | |
567 | struct polypath *st=state; | |
568 | ||
569 | if (nfds<1) return; | |
570 | ||
571 | int revents=fds[0].revents; | |
572 | ||
573 | const char *badbit=pollbadbit(revents); | |
574 | if (badbit) subproc_problem(st,async_linebuf_broken,badbit); | |
575 | ||
576 | if (!(revents & POLLIN)) return; | |
577 | ||
578 | for (;;) { | |
579 | if (st->lbuf.size==sizeof(struct privsep_mdata)) { | |
580 | const struct privsep_mdata *mdata=(const void*)st->lbuf.start; | |
581 | if (mdata->add && st->privsep_incoming_fd<0) | |
582 | fatal("polypath (privsep): got add message data but no fd"); | |
583 | if (!mdata->add && st->privsep_incoming_fd>=0) | |
584 | fatal("polypath (privsep): got remove message data with fd"); | |
585 | if (!memchr(mdata->ifname,0,sizeof(mdata->ifname))) | |
586 | fatal("polypath (privsep): got ifname with no terminating nul"); | |
587 | int af=mdata->ia.sa.sa_family; | |
588 | if (!(af==AF_INET6 || af==AF_INET)) | |
589 | fatal("polypath (privsep): got message data but bad AF %d",af); | |
590 | const char *addr_str=iaddr_to_string(&mdata->ia); | |
591 | polypath_record_ifaddr(st,papp_bad,(void*)addr_str, | |
592 | mdata->add,mdata->ifname,addr_str, | |
593 | &mdata->ia, st->privsep_incoming_fd); | |
594 | st->privsep_incoming_fd=-1; | |
595 | st->lbuf.size=0; | |
596 | } | |
597 | struct msghdr msg; | |
598 | int fd; | |
599 | size_t cmsgdatalen=sizeof(fd); | |
600 | char cmsg_control_buf[CMSG_SPACE(cmsgdatalen)]; | |
601 | struct iovec iov; | |
602 | ||
603 | FILLZERO(msg); | |
604 | msg.msg_iov=&iov; | |
605 | msg.msg_iovlen=1; | |
606 | ||
607 | iov.iov_base=st->lbuf.start+st->lbuf.size; | |
608 | iov.iov_len=sizeof(struct privsep_mdata)-st->lbuf.size; | |
609 | ||
610 | if (st->privsep_incoming_fd<0) { | |
611 | msg.msg_control=cmsg_control_buf; | |
612 | msg.msg_controllen=sizeof(cmsg_control_buf); | |
613 | } | |
614 | ||
615 | ssize_t got=recvmsg(st->monitor_fd,&msg,0); | |
616 | if (got<0) { | |
617 | if (errno==EINTR) continue; | |
618 | if (iswouldblock(errno)) break; | |
619 | fatal_perror("polypath (privsep): recvmsg failed"); | |
620 | } | |
621 | if (got==0) | |
622 | subproc_problem(st,async_linebuf_eof,0); | |
623 | ||
624 | st->lbuf.size+=got; | |
625 | ||
626 | if (msg.msg_controllen) { | |
627 | size_t cmsgdatalen=sizeof(st->privsep_incoming_fd); | |
628 | struct cmsghdr *h=CMSG_FIRSTHDR(&msg); | |
629 | if (!(st->privsep_incoming_fd==-1 && | |
630 | h && | |
631 | h->cmsg_level==SOL_SOCKET && | |
632 | h->cmsg_type==SCM_RIGHTS && | |
633 | h->cmsg_len==CMSG_LEN(cmsgdatalen) && | |
634 | !CMSG_NXTHDR(&msg,h))) | |
635 | subproc_problem(st,async_linebuf_broken,"bad cmsg"); | |
636 | memcpy(&st->privsep_incoming_fd,CMSG_DATA(h),cmsgdatalen); | |
637 | assert(st->privsep_incoming_fd>=0); | |
638 | } | |
639 | ||
640 | } | |
641 | } | |
642 | ||
643 | static void privsep_handle_ifaddr(struct polypath *st, | |
644 | bad_fn_type *bad, void *badctx, | |
645 | bool_t add, const char *ifname, | |
646 | const char *ifaddr, | |
647 | const union iaddr *ia, int fd_dummy) | |
648 | /* In child: handles discovered wanted interfaces, making sockets | |
649 | and sending them to secnet proper. */ | |
650 | { | |
651 | struct msghdr msg; | |
652 | struct iovec iov; | |
653 | struct udpsock us={ .fd=-1 }; | |
654 | size_t cmsgdatalen=sizeof(us.fd); | |
655 | char cmsg_control_buf[CMSG_SPACE(cmsgdatalen)]; | |
656 | ||
657 | assert(fd_dummy==-1); | |
658 | ||
659 | struct privsep_mdata mdata; | |
660 | FILLZERO(mdata); | |
661 | mdata.add=add; | |
662 | ||
663 | size_t l=strlen(ifname); | |
664 | if (l>=sizeof(mdata.ifname)) BAD("interface name too long"); | |
665 | strcpy(mdata.ifname,ifname); | |
666 | ||
667 | COPY_OBJ(mdata.ia,*ia); | |
668 | ||
669 | iov.iov_base=&mdata; | |
670 | iov.iov_len =sizeof(mdata); | |
671 | ||
672 | FILLZERO(msg); | |
673 | msg.msg_iov=&iov; | |
674 | msg.msg_iovlen=1; | |
675 | ||
676 | if (add) { | |
677 | COPY_OBJ(us.addr,*ia); | |
678 | bool_t ok=polypath_make_socket(st,bad,badctx,&us,ifname); | |
679 | if (!ok) goto out; | |
680 | ||
681 | msg.msg_control=cmsg_control_buf; | |
682 | msg.msg_controllen=sizeof(cmsg_control_buf); | |
683 | ||
684 | struct cmsghdr *h=CMSG_FIRSTHDR(&msg); | |
685 | h->cmsg_level=SOL_SOCKET; | |
686 | h->cmsg_type =SCM_RIGHTS; | |
687 | h->cmsg_len =CMSG_LEN(cmsgdatalen); | |
688 | memcpy(CMSG_DATA(h),&us.fd,cmsgdatalen); | |
689 | } | |
690 | ||
691 | while (iov.iov_len) { | |
692 | ssize_t got=sendmsg(st->privsep_ipcsock_fd,&msg,0); | |
693 | if (got<0) { | |
694 | if (errno!=EINTR) fatal_perror("polypath privsep sendmsg"); | |
695 | got=0; | |
696 | } else { | |
697 | assert(got>0); | |
698 | assert((size_t)got<=iov.iov_len); | |
699 | } | |
700 | iov.iov_base=(char*)iov.iov_base+got; | |
701 | iov.iov_len-=got; | |
702 | msg.msg_control=0; | |
703 | msg.msg_controllen=0; | |
704 | } | |
705 | ||
706 | out: | |
707 | if (us.fd>=0) close(us.fd); | |
708 | } | |
709 | ||
710 | static void child_privsep(struct polypath *st, int ipcsockfd) | |
711 | /* Privsep child main loop. */ | |
712 | { | |
713 | struct pollfd fds[2]; | |
714 | ||
715 | enter_phase(PHASE_CHILDPERSIST); | |
716 | ||
717 | st->privsep_ipcsock_fd=ipcsockfd; | |
718 | start_subproc(st,pipe_cloexec,child_monitor, | |
719 | "interface monitor (grandchild)"); | |
720 | for (;;) { | |
721 | int nfds=1; | |
722 | int r=polypath_beforepoll(st,fds,&nfds,0); | |
723 | assert(nfds==1); | |
724 | assert(!r); | |
725 | ||
726 | fds[1].fd=st->privsep_ipcsock_fd; | |
727 | fds[1].events=POLLIN; | |
728 | ||
729 | r=poll(fds,ARRAY_SIZE(fds),-1); | |
730 | ||
731 | if (r<0) { | |
732 | if (errno==EINTR) continue; | |
733 | fatal_perror("polypath privsep poll"); | |
734 | } | |
735 | if (fds[1].revents) { | |
736 | if (fds[1].revents & (POLLHUP|POLLIN)) { | |
737 | polypath_phase_shutdown(st,PHASE_SHUTDOWN); | |
738 | exit(0); | |
739 | } | |
740 | fatal("polypath privsep poll parent socket revents=%#x", | |
741 | fds[1].revents); | |
742 | } | |
743 | if (fds[0].revents & POLLNVAL) | |
744 | fatal("polypath privsep poll child socket POLLNVAL"); | |
745 | afterpoll_monitor(st,fds,privsep_handle_ifaddr); | |
746 | } | |
747 | } | |
748 | ||
749 | static void privsep_socketpair(int *fd) | |
750 | { | |
751 | int r=socketpair(AF_UNIX,SOCK_STREAM,0,fd); | |
752 | if (r) fatal_perror("socketpair(AF_UNIX,SOCK_STREAM,,)"); | |
753 | setcloexec(fd[0]); | |
754 | setcloexec(fd[1]); | |
755 | } | |
756 | ||
757 | static void polypath_phase_startprivsep(void *sst, uint32_t newphase) | |
758 | { | |
759 | struct polypath *st=sst; | |
760 | ||
761 | if (!will_droppriv()) { | |
762 | add_hook(PHASE_RUN, polypath_phase_startmonitor,st); | |
763 | return; | |
764 | } | |
765 | ||
766 | start_subproc(st,privsep_socketpair,child_privsep, | |
767 | "socket generator (privsep interface handler)"); | |
768 | ||
769 | BUF_FREE(&st->lbuf); | |
770 | buffer_destroy(&st->lbuf); | |
771 | buffer_new(&st->lbuf,sizeof(struct privsep_mdata)); | |
772 | BUF_ALLOC(&st->lbuf,"polypath mdata buf"); | |
773 | st->privsep_incoming_fd=-1; | |
774 | ||
775 | register_for_poll(st,polypath_beforepoll, | |
776 | polypath_afterpoll_privsep,"polypath"); | |
777 | } | |
778 | ||
ce5c098f IJ |
779 | static void polypath_phase_shutdown(void *sst, uint32_t newphase) |
780 | { | |
781 | struct polypath *st=sst; | |
782 | if (st->monitor_pid) { | |
783 | assert(st->monitor_pid>0); | |
784 | kill(st->monitor_pid,SIGTERM); | |
785 | } | |
786 | } | |
787 | ||
32654a31 IJ |
788 | static void polypath_phase_childpersist(void *sst, uint32_t newphase) |
789 | { | |
790 | struct polypath *st=sst; | |
791 | struct interf *interf; | |
792 | ||
793 | LIST_FOREACH(interf,&st->interfs,entry) | |
794 | udp_socks_childpersist(&st->uc,&interf->socks); | |
795 | } | |
796 | ||
70ad8e2c IJ |
797 | #undef BAD |
798 | #undef BADE | |
799 | ||
93cdea57 IJ |
800 | /*----- generic closure and module setup -----*/ |
801 | ||
ce5c098f IJ |
802 | static list_t *polypath_apply(closure_t *self, struct cloc loc, |
803 | dict_t *context, list_t *args) | |
804 | { | |
805 | struct polypath *st; | |
806 | ||
807 | COMM_APPLY(st,&st->uc.cc,polypath_,"polypath",loc); | |
808 | COMM_APPLY_STANDARD(st,&st->uc.cc,"polypath",args); | |
809 | UDP_APPLY_STANDARD(st,&st->uc,"polypath"); | |
810 | ||
811 | struct udpcommon *uc=&st->uc; | |
812 | struct commcommon *cc=&uc->cc; | |
813 | ||
814 | st->max_interfs=dict_read_number(d,"max-interfaces",False,"polypath",loc,3); | |
815 | ||
816 | st->ifname_pats=dict_read_string_array(d,"interfaces",False,"polypath", | |
817 | cc->loc,0); | |
818 | st->permit_loopback=0; /* ifname_wanted reads this */ | |
819 | ifname_wanted(st,st->uc.cc.loc," "); /* try to check each pattern */ | |
820 | ||
821 | st->monitor_command=dict_read_string_array(d,"monitor-command",False, | |
822 | "polypath",cc->loc, default_monitor_command); | |
823 | if (!st->monitor_command[0]) | |
824 | cfgfatal(loc,"polypath","no polypath interface monitor-command" | |
825 | " (polypath unsupported on this platform?)\n"); | |
826 | ||
827 | st->permit_loopback=dict_read_bool(d,"permit-loopback",False, | |
828 | "polypath",cc->loc,False); | |
829 | ||
830 | LIST_INIT(&st->interfs); | |
831 | buffer_new(&st->lbuf,ADNS_ADDR2TEXT_BUFLEN+100); | |
832 | BUF_ALLOC(&st->lbuf,"polypath lbuf"); | |
833 | ||
834 | st->monitor_fd=-1; | |
835 | st->monitor_pid=0; | |
836 | ||
93cdea57 IJ |
837 | add_hook(PHASE_GETRESOURCES, polypath_phase_startprivsep,st); |
838 | ||
ce5c098f | 839 | add_hook(PHASE_SHUTDOWN, polypath_phase_shutdown, st); |
32654a31 | 840 | add_hook(PHASE_CHILDPERSIST,polypath_phase_childpersist,st); |
ce5c098f IJ |
841 | |
842 | return new_closure(&cc->cl); | |
843 | } | |
844 | ||
845 | #endif /* CONFIG_IPV6 */ | |
846 | ||
847 | void polypath_module(dict_t *dict) | |
848 | { | |
849 | #ifdef CONFIG_IPV6 | |
850 | add_closure(dict,"polypath",polypath_apply); | |
851 | #endif /* CONFIG_IPV6 */ | |
852 | } |