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