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