/src/ntp-dev/ntpd/ntp_io.c
Line | Count | Source |
1 | | /* |
2 | | * ntp_io.c - input/output routines for ntpd. The socket-opening code |
3 | | * was shamelessly stolen from ntpd. |
4 | | */ |
5 | | |
6 | | #ifdef HAVE_CONFIG_H |
7 | | # include <config.h> |
8 | | #endif |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <signal.h> |
12 | | #ifdef HAVE_FNMATCH_H |
13 | | # include <fnmatch.h> |
14 | | # if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE) |
15 | | # define FNM_CASEFOLD FNM_IGNORECASE |
16 | | # endif |
17 | | #endif |
18 | | #ifdef HAVE_SYS_PARAM_H |
19 | | # include <sys/param.h> |
20 | | #endif |
21 | | #ifdef HAVE_SYS_IOCTL_H |
22 | | # include <sys/ioctl.h> |
23 | | #endif |
24 | | #ifdef HAVE_SYS_SOCKIO_H /* UXPV: SIOC* #defines (Frank Vance <fvance@waii.com>) */ |
25 | | # include <sys/sockio.h> |
26 | | #endif |
27 | | #ifdef HAVE_SYS_UIO_H |
28 | | # include <sys/uio.h> |
29 | | #endif |
30 | | |
31 | | #include "ntp_machine.h" |
32 | | #include "ntpd.h" |
33 | | #include "ntp_io.h" |
34 | | #include "iosignal.h" |
35 | | #include "ntp_lists.h" |
36 | | #include "ntp_refclock.h" |
37 | | #include "ntp_stdlib.h" |
38 | | #include "ntp_worker.h" |
39 | | #include "ntp_request.h" |
40 | | #include "ntp_assert.h" |
41 | | #include "timevalops.h" |
42 | | #include "timespecops.h" |
43 | | #include "ntpd-opts.h" |
44 | | |
45 | | /* Don't include ISC's version of IPv6 variables and structures */ |
46 | | #define ISC_IPV6_H 1 |
47 | | #include <isc/mem.h> |
48 | | #include <isc/interfaceiter.h> |
49 | | #include <isc/netaddr.h> |
50 | | #include <isc/result.h> |
51 | | #include <isc/sockaddr.h> |
52 | | |
53 | | #ifdef SIM |
54 | | #include "ntpsim.h" |
55 | | #endif |
56 | | |
57 | | #ifdef HAS_ROUTING_SOCKET |
58 | | # include <net/route.h> |
59 | | # ifdef HAVE_RTNETLINK |
60 | | # include <linux/rtnetlink.h> |
61 | | # endif |
62 | | #endif |
63 | | |
64 | | /* |
65 | | * setsockopt does not always have the same arg declaration |
66 | | * across all platforms. If it's not defined we make it empty |
67 | | */ |
68 | | |
69 | | #ifndef SETSOCKOPT_ARG_CAST |
70 | | #define SETSOCKOPT_ARG_CAST |
71 | | #endif |
72 | | |
73 | | extern int listen_to_virtual_ips; |
74 | | |
75 | | #ifndef IPTOS_DSCP_EF |
76 | | #define IPTOS_DSCP_EF 0xb8 |
77 | | #endif |
78 | | int qos = IPTOS_DSCP_EF; /* QoS RFC3246 */ |
79 | | |
80 | | #ifdef LEAP_SMEAR |
81 | | /* TODO burnicki: This should be moved to ntp_timer.c, but if we do so |
82 | | * we get a linker error. Since we're running out of time before the leap |
83 | | * second occurs, we let it here where it just works. |
84 | | */ |
85 | | int leap_smear_intv; |
86 | | #endif |
87 | | |
88 | | /* |
89 | | * NIC rule entry |
90 | | */ |
91 | | typedef struct nic_rule_tag nic_rule; |
92 | | |
93 | | struct nic_rule_tag { |
94 | | nic_rule * next; |
95 | | nic_rule_action action; |
96 | | nic_rule_match match_type; |
97 | | char * if_name; |
98 | | sockaddr_u addr; |
99 | | int prefixlen; |
100 | | }; |
101 | | |
102 | | /* |
103 | | * NIC rule listhead. Entries are added at the head so that the first |
104 | | * match in the list is the last matching rule specified. |
105 | | */ |
106 | | nic_rule *nic_rule_list; |
107 | | |
108 | | |
109 | | #if defined(SO_BINTIME) && defined(SCM_BINTIME) && defined(CMSG_FIRSTHDR) |
110 | | # define HAVE_PACKET_TIMESTAMP |
111 | | # define HAVE_BINTIME |
112 | | # ifdef BINTIME_CTLMSGBUF_SIZE |
113 | | # define CMSG_BUFSIZE BINTIME_CTLMSGBUF_SIZE |
114 | | # else |
115 | | # define CMSG_BUFSIZE 1536 /* moderate default */ |
116 | | # endif |
117 | | #elif defined(SO_TIMESTAMPNS) && defined(SCM_TIMESTAMPNS) && defined(CMSG_FIRSTHDR) |
118 | | # define HAVE_PACKET_TIMESTAMP |
119 | | # define HAVE_TIMESTAMPNS |
120 | | # ifdef TIMESTAMPNS_CTLMSGBUF_SIZE |
121 | | # define CMSG_BUFSIZE TIMESTAMPNS_CTLMSGBUF_SIZE |
122 | | # else |
123 | | # define CMSG_BUFSIZE 1536 /* moderate default */ |
124 | | # endif |
125 | | #elif defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP) && defined(CMSG_FIRSTHDR) |
126 | | # define HAVE_PACKET_TIMESTAMP |
127 | | # define HAVE_TIMESTAMP |
128 | | # ifdef TIMESTAMP_CTLMSGBUF_SIZE |
129 | | # define CMSG_BUFSIZE TIMESTAMP_CTLMSGBUF_SIZE |
130 | | # else |
131 | | # define CMSG_BUFSIZE 1536 /* moderate default */ |
132 | | # endif |
133 | | #else |
134 | | /* fill in for old/other timestamp interfaces */ |
135 | | #endif |
136 | | |
137 | | #if defined(SYS_WINNT) |
138 | | #include "win32_io.h" |
139 | | #include <isc/win32os.h> |
140 | | #endif |
141 | | |
142 | | /* |
143 | | * We do asynchronous input using the SIGIO facility. A number of |
144 | | * recvbuf buffers are preallocated for input. In the signal |
145 | | * handler we poll to see which sockets are ready and read the |
146 | | * packets from them into the recvbuf's along with a time stamp and |
147 | | * an indication of the source host and the interface it was received |
148 | | * through. This allows us to get as accurate receive time stamps |
149 | | * as possible independent of other processing going on. |
150 | | * |
151 | | * We watch the number of recvbufs available to the signal handler |
152 | | * and allocate more when this number drops below the low water |
153 | | * mark. If the signal handler should run out of buffers in the |
154 | | * interim it will drop incoming frames, the idea being that it is |
155 | | * better to drop a packet than to be inaccurate. |
156 | | */ |
157 | | |
158 | | |
159 | | /* |
160 | | * Other statistics of possible interest |
161 | | */ |
162 | | volatile u_long packets_dropped; /* total number of packets dropped on reception */ |
163 | | volatile u_long packets_ignored; /* packets received on wild card interface */ |
164 | | volatile u_long packets_received; /* total number of packets received */ |
165 | | u_long packets_sent; /* total number of packets sent */ |
166 | | u_long packets_notsent; /* total number of packets which couldn't be sent */ |
167 | | |
168 | | volatile u_long handler_calls; /* number of calls to interrupt handler */ |
169 | | volatile u_long handler_pkts; /* number of pkts received by handler */ |
170 | | u_long io_timereset; /* time counters were reset */ |
171 | | |
172 | | /* |
173 | | * Interface stuff |
174 | | */ |
175 | | endpt * any_interface; /* wildcard ipv4 interface */ |
176 | | endpt * any6_interface; /* wildcard ipv6 interface */ |
177 | | endpt * loopback_interface; /* loopback ipv4 interface */ |
178 | | |
179 | | static isc_boolean_t broadcast_client_enabled; |
180 | | u_int sys_ifnum; /* next .ifnum to assign */ |
181 | | int ninterfaces; /* Total number of interfaces */ |
182 | | |
183 | | int no_periodic_scan; /* network endpoint scans */ |
184 | | int scan_addrs_once; /* because dropped privs */ |
185 | | int nonlocal_v4_addr_up; /* should we try IPv4 pool? */ |
186 | | int nonlocal_v6_addr_up; /* should we try IPv6 pool? */ |
187 | | |
188 | | #ifdef REFCLOCK |
189 | | /* |
190 | | * Refclock stuff. We keep a chain of structures with data concerning |
191 | | * the guys we are doing I/O for. |
192 | | */ |
193 | | static struct refclockio *refio; |
194 | | #endif /* REFCLOCK */ |
195 | | |
196 | | /* |
197 | | * File descriptor masks etc. for call to select |
198 | | * Not needed for I/O Completion Ports or anything outside this file |
199 | | */ |
200 | | static fd_set activefds; |
201 | | static int maxactivefd; |
202 | | |
203 | | /* |
204 | | * bit alternating value to detect verified interfaces during an update cycle |
205 | | */ |
206 | | static u_short sys_interphase = 0; |
207 | | |
208 | | static endpt * new_interface(endpt *); |
209 | | static void add_interface(endpt *); |
210 | | static int update_interfaces(u_short, interface_receiver_t, |
211 | | void *); |
212 | | static void remove_interface(endpt *); |
213 | | static endpt * create_interface(u_short, endpt *); |
214 | | |
215 | | static inline int is_wildcard_addr(const sockaddr_u *psau); |
216 | | |
217 | | /* |
218 | | * Multicast functions |
219 | | */ |
220 | | static isc_boolean_t addr_ismulticast (sockaddr_u *); |
221 | | static isc_boolean_t is_anycast (sockaddr_u *, |
222 | | const char *); |
223 | | |
224 | | /* |
225 | | * Not all platforms support multicast |
226 | | */ |
227 | | #ifdef MCAST |
228 | | static isc_boolean_t socket_multicast_enable (endpt *, sockaddr_u *); |
229 | | static isc_boolean_t socket_multicast_disable(endpt *, sockaddr_u *); |
230 | | #endif |
231 | | |
232 | | #ifdef DEBUG |
233 | | static void interface_dump (const endpt *); |
234 | | static void print_interface (const endpt *, const char *, const char *); |
235 | 8 | #define DPRINT_INTERFACE(level, args) do { if (debug >= (level)) { print_interface args; } } while (0) |
236 | | #else |
237 | | #define DPRINT_INTERFACE(level, args) do {} while (0) |
238 | | #endif |
239 | | |
240 | | typedef struct vsock vsock_t; |
241 | | enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE }; |
242 | | |
243 | | struct vsock { |
244 | | vsock_t * link; |
245 | | SOCKET fd; |
246 | | enum desc_type type; |
247 | | }; |
248 | | |
249 | | vsock_t *fd_list; |
250 | | |
251 | | #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) |
252 | | /* |
253 | | * async notification processing (e. g. routing sockets) |
254 | | */ |
255 | | /* |
256 | | * support for receiving data on fd that is not a refclock or a socket |
257 | | * like e. g. routing sockets |
258 | | */ |
259 | | struct asyncio_reader { |
260 | | struct asyncio_reader *link; /* the list this is being kept in */ |
261 | | SOCKET fd; /* fd to be read */ |
262 | | void *data; /* possibly local data */ |
263 | | void (*receiver)(struct asyncio_reader *); /* input handler */ |
264 | | }; |
265 | | |
266 | | struct asyncio_reader *asyncio_reader_list; |
267 | | |
268 | | static void delete_asyncio_reader (struct asyncio_reader *); |
269 | | static struct asyncio_reader *new_asyncio_reader (void); |
270 | | static void add_asyncio_reader (struct asyncio_reader *, enum desc_type); |
271 | | static void remove_asyncio_reader (struct asyncio_reader *); |
272 | | |
273 | | #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */ |
274 | | |
275 | | static void init_async_notifications (void); |
276 | | |
277 | | static int addr_eqprefix (const sockaddr_u *, const sockaddr_u *, |
278 | | int); |
279 | | static int addr_samesubnet (const sockaddr_u *, const sockaddr_u *, |
280 | | const sockaddr_u *, const sockaddr_u *); |
281 | | static int create_sockets (u_short); |
282 | | static SOCKET open_socket (sockaddr_u *, int, int, endpt *); |
283 | | static void set_reuseaddr (int); |
284 | | static isc_boolean_t socket_broadcast_enable (endpt *, SOCKET, sockaddr_u *); |
285 | | |
286 | | #if !defined(HAVE_IO_COMPLETION_PORT) && !defined(HAVE_SIGNALED_IO) |
287 | | static char * fdbits (int, const fd_set *); |
288 | | #endif |
289 | | #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES |
290 | | static isc_boolean_t socket_broadcast_disable (endpt *, sockaddr_u *); |
291 | | #endif |
292 | | |
293 | | typedef struct remaddr remaddr_t; |
294 | | |
295 | | struct remaddr { |
296 | | remaddr_t * link; |
297 | | sockaddr_u addr; |
298 | | endpt * ep; |
299 | | }; |
300 | | |
301 | | remaddr_t * remoteaddr_list; |
302 | | endpt * ep_list; /* complete endpt list */ |
303 | | endpt * mc4_list; /* IPv4 mcast-capable unicast endpts */ |
304 | | endpt * mc6_list; /* IPv6 mcast-capable unicast endpts */ |
305 | | |
306 | | static endpt * wildipv4; |
307 | | static endpt * wildipv6; |
308 | | |
309 | 1 | #define RFC3927_ADDR 0xa9fe0000 /* 169.254. */ |
310 | 1 | #define RFC3927_MASK 0xffff0000 |
311 | | #define IS_AUTOCONF(addr4) \ |
312 | 1 | ((SRCADR(addr4) & RFC3927_MASK) == RFC3927_ADDR) |
313 | | |
314 | | #ifdef SYS_WINNT |
315 | | int accept_wildcard_if_for_winnt; |
316 | | #else |
317 | | const int accept_wildcard_if_for_winnt = FALSE; |
318 | 1 | #define init_io_completion_port() do {} while (FALSE) |
319 | | #endif |
320 | | |
321 | | static void add_fd_to_list (SOCKET, enum desc_type); |
322 | | static endpt * find_addr_in_list (sockaddr_u *); |
323 | | static endpt * find_flagged_addr_in_list(sockaddr_u *, u_int32); |
324 | | static void delete_addr_from_list (sockaddr_u *); |
325 | | static void delete_interface_from_list(endpt *); |
326 | | static void close_and_delete_fd_from_list(SOCKET, endpt *); |
327 | | static void add_addr_to_list (sockaddr_u *, endpt *); |
328 | | static void create_wildcards (u_short); |
329 | | static endpt * findlocalinterface (sockaddr_u *, int, int); |
330 | | static endpt * findclosestinterface (sockaddr_u *, int); |
331 | | #ifdef DEBUG |
332 | | static const char * action_text (nic_rule_action); |
333 | | #endif |
334 | | static nic_rule_action interface_action(char *, sockaddr_u *, u_int32); |
335 | | static void convert_isc_if (isc_interface_t *, |
336 | | endpt *, u_short); |
337 | | static void calc_addr_distance(sockaddr_u *, |
338 | | const sockaddr_u *, |
339 | | const sockaddr_u *); |
340 | | static int cmp_addr_distance(const sockaddr_u *, |
341 | | const sockaddr_u *); |
342 | | |
343 | | /* |
344 | | * Routines to read the ntp packets |
345 | | */ |
346 | | #if !defined(HAVE_IO_COMPLETION_PORT) |
347 | | static inline int read_network_packet (SOCKET, endpt *, l_fp); |
348 | | static void ntpd_addremove_io_fd (int, int, int); |
349 | | static void input_handler_scan (const l_fp*, const fd_set*); |
350 | | static int/*BOOL*/ sanitize_fdset (int errc); |
351 | | #ifdef REFCLOCK |
352 | | static inline int read_refclock_packet (SOCKET, struct refclockio *, l_fp); |
353 | | #endif |
354 | | #ifdef HAVE_SIGNALED_IO |
355 | | static void input_handler (l_fp*); |
356 | | #endif |
357 | | #endif |
358 | | |
359 | | |
360 | | #ifndef HAVE_IO_COMPLETION_PORT |
361 | | void |
362 | | maintain_activefds( |
363 | | int fd, |
364 | | int closing |
365 | | ) |
366 | 5 | { |
367 | 5 | int i; |
368 | | |
369 | 5 | if (fd < 0 || fd >= FD_SETSIZE) { |
370 | 0 | msyslog(LOG_ERR, |
371 | 0 | "Too many sockets in use, FD_SETSIZE %d exceeded by fd %d", |
372 | 0 | FD_SETSIZE, fd); |
373 | 0 | exit(1); |
374 | 0 | } |
375 | | |
376 | 5 | if (!closing) { |
377 | 5 | FD_SET(fd, &activefds); |
378 | 5 | maxactivefd = max(fd, maxactivefd); |
379 | 5 | } else { |
380 | 0 | FD_CLR(fd, &activefds); |
381 | 0 | if (maxactivefd && fd == maxactivefd) { |
382 | 0 | for (i = maxactivefd - 1; i >= 0; i--) |
383 | 0 | if (FD_ISSET(i, &activefds)) { |
384 | 0 | maxactivefd = i; |
385 | 0 | break; |
386 | 0 | } |
387 | 0 | INSIST(fd != maxactivefd); |
388 | 0 | } |
389 | 0 | } |
390 | 5 | } |
391 | | #endif /* !HAVE_IO_COMPLETION_PORT */ |
392 | | |
393 | | |
394 | | #ifdef DEBUG_TIMING |
395 | | /* |
396 | | * collect timing information for various processing |
397 | | * paths. currently we only pass them on to the file |
398 | | * for later processing. this could also do histogram |
399 | | * based analysis in other to reduce the load (and skew) |
400 | | * dur to the file output |
401 | | */ |
402 | | void |
403 | | collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts) |
404 | | { |
405 | | char buf[256]; |
406 | | |
407 | | snprintf(buf, sizeof(buf), "%s %d %s %s", |
408 | | (rb != NULL) |
409 | | ? ((rb->dstadr != NULL) |
410 | | ? stoa(&rb->recv_srcadr) |
411 | | : "-REFCLOCK-") |
412 | | : "-", |
413 | | count, lfptoa(dts, 9), tag); |
414 | | record_timing_stats(buf); |
415 | | } |
416 | | #endif |
417 | | |
418 | | /* |
419 | | * About dynamic interfaces, sockets, reception and more... |
420 | | * |
421 | | * the code solves following tasks: |
422 | | * |
423 | | * - keep a current list of active interfaces in order |
424 | | * to bind to to the interface address on NTP_PORT so that |
425 | | * all wild and specific bindings for NTP_PORT are taken by ntpd |
426 | | * to avoid other daemons messing with the time or sockets. |
427 | | * - all interfaces keep a list of peers that are referencing |
428 | | * the interface in order to quickly re-assign the peers to |
429 | | * new interface in case an interface is deleted (=> gone from system or |
430 | | * down) |
431 | | * - have a preconfigured socket ready with the right local address |
432 | | * for transmission and reception |
433 | | * - have an address list for all destination addresses used within ntpd |
434 | | * to find the "right" preconfigured socket. |
435 | | * - facilitate updating the internal interface list with respect to |
436 | | * the current kernel state |
437 | | * |
438 | | * special issues: |
439 | | * |
440 | | * - mapping of multicast addresses to the interface affected is not always |
441 | | * one to one - especially on hosts with multiple interfaces |
442 | | * the code here currently allocates a separate interface entry for those |
443 | | * multicast addresses |
444 | | * iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF) |
445 | | * in case of failure the multicast address is bound to an existing interface. |
446 | | * - on some systems it is perfectly legal to assign the same address to |
447 | | * multiple interfaces. Therefore this code does not keep a list of interfaces |
448 | | * but a list of interfaces that represent a unique address as determined by the kernel |
449 | | * by the procedure in findlocalinterface. Thus it is perfectly legal to see only |
450 | | * one representative of a group of real interfaces if they share the same address. |
451 | | * |
452 | | * Frank Kardel 20050910 |
453 | | */ |
454 | | |
455 | | /* |
456 | | * init_io - initialize I/O module. |
457 | | */ |
458 | | void |
459 | | init_io(void) |
460 | 1 | { |
461 | | /* Init buffer free list and stat counters */ |
462 | 1 | init_recvbuff(RECV_INIT); |
463 | | /* update interface every 5 minutes as default */ |
464 | 1 | endpt_scan_period = 301; |
465 | | |
466 | 1 | #ifdef WORK_PIPE |
467 | 1 | addremove_io_fd = &ntpd_addremove_io_fd; |
468 | 1 | #endif |
469 | | |
470 | 1 | init_io_completion_port(); |
471 | | #if defined(HAVE_SIGNALED_IO) |
472 | | (void) set_signal(input_handler); |
473 | | #endif |
474 | 1 | } |
475 | | |
476 | | |
477 | | static void |
478 | | ntpd_addremove_io_fd( |
479 | | int fd, |
480 | | int is_pipe, |
481 | | int remove_it |
482 | | ) |
483 | 0 | { |
484 | 0 | UNUSED_ARG(is_pipe); |
485 | |
|
486 | | #ifdef HAVE_SIGNALED_IO |
487 | | if (!remove_it) |
488 | | init_socket_sig(fd); |
489 | | #endif /* not HAVE_SIGNALED_IO */ |
490 | |
|
491 | 0 | maintain_activefds(fd, remove_it); |
492 | 0 | } |
493 | | |
494 | | |
495 | | /* |
496 | | * io_open_sockets - call socket creation routine |
497 | | */ |
498 | | void |
499 | | io_open_sockets(void) |
500 | 1 | { |
501 | 1 | static int already_opened; |
502 | | |
503 | 1 | if (already_opened || HAVE_OPT( SAVECONFIGQUIT )) |
504 | 0 | return; |
505 | | |
506 | 1 | already_opened = 1; |
507 | | |
508 | | /* |
509 | | * Create the sockets |
510 | | */ |
511 | 1 | BLOCKIO(); |
512 | 1 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
513 | 1 | create_sockets(4123); |
514 | | #else |
515 | | create_sockets(NTP_PORT); |
516 | | #endif |
517 | 1 | UNBLOCKIO(); |
518 | | |
519 | 1 | init_async_notifications(); |
520 | | |
521 | 1 | DPRINTF(3, ("io_open_sockets: maxactivefd %d\n", maxactivefd)); |
522 | 1 | } |
523 | | |
524 | | |
525 | | #ifdef DEBUG |
526 | | /* |
527 | | * function to dump the contents of the interface structure |
528 | | * for debugging use only. |
529 | | * We face a dilemma here -- sockets are FDs under POSIX and |
530 | | * actually HANDLES under Windows. So we use '%lld' as format |
531 | | * and cast the value to 'long long'; this should not hurt |
532 | | * with UNIX-like systems and does not truncate values on Win64. |
533 | | */ |
534 | | void |
535 | | interface_dump(const endpt *itf) |
536 | 0 | { |
537 | 0 | printf("Dumping interface: %p\n", itf); |
538 | 0 | printf("fd = %lld\n", (long long)itf->fd); |
539 | 0 | printf("bfd = %lld\n", (long long)itf->bfd); |
540 | 0 | printf("sin = %s,\n", stoa(&itf->sin)); |
541 | 0 | printf("bcast = %s,\n", stoa(&itf->bcast)); |
542 | 0 | printf("mask = %s,\n", stoa(&itf->mask)); |
543 | 0 | printf("name = %s\n", itf->name); |
544 | 0 | printf("flags = 0x%08x\n", itf->flags); |
545 | 0 | printf("last_ttl = %d\n", itf->last_ttl); |
546 | 0 | printf("addr_refid = %08x\n", itf->addr_refid); |
547 | 0 | printf("num_mcast = %d\n", itf->num_mcast); |
548 | 0 | printf("received = %ld\n", itf->received); |
549 | 0 | printf("sent = %ld\n", itf->sent); |
550 | 0 | printf("notsent = %ld\n", itf->notsent); |
551 | 0 | printf("ifindex = %u\n", itf->ifindex); |
552 | 0 | printf("peercnt = %u\n", itf->peercnt); |
553 | 0 | printf("phase = %u\n", itf->phase); |
554 | 0 | } |
555 | | |
556 | | |
557 | | /* |
558 | | * print_interface - helper to output debug information |
559 | | */ |
560 | | static void |
561 | | print_interface(const endpt *iface, const char *pfx, const char *sfx) |
562 | 0 | { |
563 | 0 | printf("%sinterface #%d: fd=%lld, bfd=%lld, name=%s, flags=0x%x, ifindex=%u, sin=%s", |
564 | 0 | pfx, |
565 | 0 | iface->ifnum, |
566 | 0 | (long long)iface->fd, |
567 | 0 | (long long)iface->bfd, |
568 | 0 | iface->name, |
569 | 0 | iface->flags, |
570 | 0 | iface->ifindex, |
571 | 0 | stoa(&iface->sin)); |
572 | 0 | if (AF_INET == iface->family) { |
573 | 0 | if (iface->flags & INT_BROADCAST) |
574 | 0 | printf(", bcast=%s", stoa(&iface->bcast)); |
575 | 0 | printf(", mask=%s", stoa(&iface->mask)); |
576 | 0 | } |
577 | 0 | printf(", %s:%s", |
578 | 0 | (iface->ignore_packets) |
579 | 0 | ? "Disabled" |
580 | 0 | : "Enabled", |
581 | 0 | sfx); |
582 | 0 | if (debug > 4) /* in-depth debugging only */ |
583 | 0 | interface_dump(iface); |
584 | 0 | } |
585 | | #endif |
586 | | |
587 | | #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) |
588 | | /* |
589 | | * create an asyncio_reader structure |
590 | | */ |
591 | | static struct asyncio_reader * |
592 | | new_asyncio_reader(void) |
593 | 1 | { |
594 | 1 | struct asyncio_reader *reader; |
595 | | |
596 | 1 | reader = emalloc_zero(sizeof(*reader)); |
597 | 1 | reader->fd = INVALID_SOCKET; |
598 | | |
599 | 1 | return reader; |
600 | 1 | } |
601 | | |
602 | | /* |
603 | | * delete a reader |
604 | | */ |
605 | | static void |
606 | | delete_asyncio_reader( |
607 | | struct asyncio_reader *reader |
608 | | ) |
609 | 0 | { |
610 | 0 | free(reader); |
611 | 0 | } |
612 | | |
613 | | /* |
614 | | * add asynchio_reader |
615 | | */ |
616 | | static void |
617 | | add_asyncio_reader( |
618 | | struct asyncio_reader * reader, |
619 | | enum desc_type type) |
620 | 1 | { |
621 | 1 | LINK_SLIST(asyncio_reader_list, reader, link); |
622 | 1 | add_fd_to_list(reader->fd, type); |
623 | 1 | } |
624 | | |
625 | | /* |
626 | | * remove asyncio_reader |
627 | | */ |
628 | | static void |
629 | | remove_asyncio_reader( |
630 | | struct asyncio_reader *reader |
631 | | ) |
632 | 0 | { |
633 | 0 | struct asyncio_reader *unlinked; |
634 | |
|
635 | 0 | UNLINK_SLIST(unlinked, asyncio_reader_list, reader, link, |
636 | 0 | struct asyncio_reader); |
637 | |
|
638 | 0 | if (reader->fd != INVALID_SOCKET) { |
639 | 0 | close_and_delete_fd_from_list(reader->fd, NULL); |
640 | 0 | } |
641 | 0 | reader->fd = INVALID_SOCKET; |
642 | 0 | } |
643 | | #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */ |
644 | | |
645 | | |
646 | | /* compare two sockaddr prefixes */ |
647 | | static int |
648 | | addr_eqprefix( |
649 | | const sockaddr_u * a, |
650 | | const sockaddr_u * b, |
651 | | int prefixlen |
652 | | ) |
653 | 0 | { |
654 | 0 | isc_netaddr_t isc_a; |
655 | 0 | isc_netaddr_t isc_b; |
656 | 0 | isc_sockaddr_t isc_sa; |
657 | |
|
658 | 0 | ZERO(isc_sa); |
659 | 0 | memcpy(&isc_sa.type, a, min(sizeof(isc_sa.type), sizeof(*a))); |
660 | 0 | isc_netaddr_fromsockaddr(&isc_a, &isc_sa); |
661 | |
|
662 | 0 | ZERO(isc_sa); |
663 | 0 | memcpy(&isc_sa.type, b, min(sizeof(isc_sa.type), sizeof(*b))); |
664 | 0 | isc_netaddr_fromsockaddr(&isc_b, &isc_sa); |
665 | |
|
666 | 0 | return (int)isc_netaddr_eqprefix(&isc_a, &isc_b, |
667 | 0 | (u_int)prefixlen); |
668 | 0 | } |
669 | | |
670 | | |
671 | | static int |
672 | | addr_samesubnet( |
673 | | const sockaddr_u * a, |
674 | | const sockaddr_u * a_mask, |
675 | | const sockaddr_u * b, |
676 | | const sockaddr_u * b_mask |
677 | | ) |
678 | 0 | { |
679 | 0 | const u_int32 * pa; |
680 | 0 | const u_int32 * pa_limit; |
681 | 0 | const u_int32 * pb; |
682 | 0 | const u_int32 * pm; |
683 | 0 | size_t loops; |
684 | |
|
685 | 0 | REQUIRE(AF(a) == AF(a_mask)); |
686 | 0 | REQUIRE(AF(b) == AF(b_mask)); |
687 | | /* |
688 | | * With address and mask families verified to match, comparing |
689 | | * the masks also validates the address's families match. |
690 | | */ |
691 | 0 | if (!SOCK_EQ(a_mask, b_mask)) |
692 | 0 | return FALSE; |
693 | | |
694 | 0 | if (IS_IPV6(a)) { |
695 | 0 | loops = sizeof(NSRCADR6(a)) / sizeof(*pa); |
696 | 0 | pa = (const void *)&NSRCADR6(a); |
697 | 0 | pb = (const void *)&NSRCADR6(b); |
698 | 0 | pm = (const void *)&NSRCADR6(a_mask); |
699 | 0 | } else { |
700 | 0 | loops = sizeof(NSRCADR(a)) / sizeof(*pa); |
701 | 0 | pa = (const void *)&NSRCADR(a); |
702 | 0 | pb = (const void *)&NSRCADR(b); |
703 | 0 | pm = (const void *)&NSRCADR(a_mask); |
704 | 0 | } |
705 | 0 | for (pa_limit = pa + loops; pa < pa_limit; pa++, pb++, pm++) |
706 | 0 | if ((*pa & *pm) != (*pb & *pm)) |
707 | 0 | return FALSE; |
708 | | |
709 | 0 | return TRUE; |
710 | 0 | } |
711 | | |
712 | | |
713 | | /* |
714 | | * interface list enumerator - visitor pattern |
715 | | */ |
716 | | void |
717 | | interface_enumerate( |
718 | | interface_receiver_t receiver, |
719 | | void * data |
720 | | ) |
721 | 2.21k | { |
722 | 2.21k | interface_info_t ifi; |
723 | | |
724 | 2.21k | ifi.action = IFS_EXISTS; |
725 | 11.0k | for (ifi.ep = ep_list; ifi.ep != NULL; ifi.ep = ifi.ep->elink) |
726 | 8.86k | (*receiver)(data, &ifi); |
727 | 2.21k | } |
728 | | |
729 | | /* |
730 | | * do standard initialization of interface structure |
731 | | */ |
732 | | static inline void |
733 | | init_interface( |
734 | | endpt *ep |
735 | | ) |
736 | 2 | { |
737 | 2 | ZERO(*ep); |
738 | 2 | ep->fd = INVALID_SOCKET; |
739 | 2 | ep->bfd = INVALID_SOCKET; |
740 | 2 | ep->phase = sys_interphase; |
741 | 2 | } |
742 | | |
743 | | |
744 | | /* |
745 | | * create new interface structure initialize from |
746 | | * template structure or via standard initialization |
747 | | * function |
748 | | */ |
749 | | static endpt * |
750 | | new_interface( |
751 | | endpt *protot |
752 | | ) |
753 | 4 | { |
754 | 4 | endpt * iface; |
755 | | |
756 | 4 | iface = emalloc(sizeof(*iface)); |
757 | 4 | if (NULL == protot) { |
758 | 2 | ZERO(*iface); |
759 | 2 | } else { |
760 | 2 | memcpy(iface, protot, sizeof(*iface)); |
761 | 2 | } |
762 | | /* count every new instance of an interface in the system */ |
763 | 4 | iface->ifnum = sys_ifnum++; |
764 | 4 | iface->starttime = current_time; |
765 | | |
766 | | # ifdef HAVE_IO_COMPLETION_PORT |
767 | | if (!io_completion_port_add_interface(iface)) { |
768 | | msyslog(LOG_EMERG, "cannot register interface with IO engine -- will exit now"); |
769 | | exit(1); |
770 | | } |
771 | | # endif |
772 | 4 | return iface; |
773 | 4 | } |
774 | | |
775 | | |
776 | | /* |
777 | | * return interface storage into free memory pool |
778 | | */ |
779 | | static void |
780 | | delete_interface( |
781 | | endpt *ep |
782 | | ) |
783 | 0 | { |
784 | | # ifdef HAVE_IO_COMPLETION_PORT |
785 | | io_completion_port_remove_interface(ep); |
786 | | # endif |
787 | 0 | free(ep); |
788 | 0 | } |
789 | | |
790 | | |
791 | | /* |
792 | | * link interface into list of known interfaces |
793 | | */ |
794 | | static void |
795 | | add_interface( |
796 | | endpt * ep |
797 | | ) |
798 | 4 | { |
799 | 4 | endpt ** pmclisthead; |
800 | 4 | endpt * scan; |
801 | 4 | endpt * scan_next; |
802 | 4 | int same_subnet; |
803 | 4 | int rc; |
804 | | |
805 | | /* Calculate the refid */ |
806 | 4 | ep->addr_refid = addr2refid(&ep->sin); |
807 | | # ifdef WORDS_BIGENDIAN |
808 | | if (IS_IPV6(&ep->sin)) { |
809 | | ep->old_refid = BYTESWAP32(ep->addr_refid); |
810 | | } |
811 | | # endif |
812 | | /* link at tail so ntpdc -c ifstats index increases each row */ |
813 | 4 | LINK_TAIL_SLIST(ep_list, ep, elink, endpt); |
814 | 4 | ninterfaces++; |
815 | 4 | #ifdef MCAST |
816 | | /* the rest is for enabled multicast-capable addresses only */ |
817 | 4 | if (ep->ignore_packets || !(INT_MULTICAST & ep->flags) || |
818 | 1 | INT_LOOPBACK & ep->flags) |
819 | 3 | return; |
820 | | # ifndef INCLUDE_IPV6_MULTICAST_SUPPORT |
821 | | if (AF_INET6 == ep->family) |
822 | | return; |
823 | | # endif |
824 | 1 | pmclisthead = (AF_INET == ep->family) |
825 | 1 | ? &mc4_list |
826 | 1 | : &mc6_list; |
827 | | |
828 | | /* |
829 | | * If we have multiple global addresses from the same prefix |
830 | | * on the same network interface, multicast from one. |
831 | | */ |
832 | 1 | for (scan = *pmclisthead; scan != NULL; scan = scan_next) { |
833 | 0 | scan_next = scan->mclink; |
834 | 0 | if ( ep->family != scan->family |
835 | 0 | || ep->ifindex != scan->ifindex) { |
836 | 0 | continue; |
837 | 0 | } |
838 | 0 | same_subnet = addr_samesubnet(&ep->sin, &ep->mask, |
839 | 0 | &scan->sin, &scan->mask); |
840 | 0 | if (same_subnet) { |
841 | 0 | DPRINTF(4, ("did not add %s to multicast-capable list" |
842 | 0 | "which already has %s\n", |
843 | 0 | stoa(&ep->sin), stoa(&scan->sin))); |
844 | 0 | return; |
845 | 0 | } |
846 | 0 | } |
847 | 1 | LINK_SLIST(*pmclisthead, ep, mclink); |
848 | 1 | if (INVALID_SOCKET == ep->fd) |
849 | 0 | return; |
850 | | |
851 | | /* |
852 | | * select the local address from which to send to multicast. |
853 | | */ |
854 | 1 | switch (AF(&ep->sin)) { |
855 | | |
856 | 1 | case AF_INET : |
857 | 1 | rc = setsockopt(ep->fd, IPPROTO_IP, |
858 | 1 | IP_MULTICAST_IF, |
859 | 1 | (void *)&NSRCADR(&ep->sin), |
860 | 1 | sizeof(NSRCADR(&ep->sin))); |
861 | 1 | if (rc) |
862 | 0 | msyslog(LOG_ERR, |
863 | 0 | "setsockopt IP_MULTICAST_IF %s fails: %m", |
864 | 0 | stoa(&ep->sin)); |
865 | 1 | break; |
866 | | |
867 | 0 | # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT |
868 | 0 | case AF_INET6 : |
869 | 0 | rc = setsockopt(ep->fd, IPPROTO_IPV6, |
870 | 0 | IPV6_MULTICAST_IF, |
871 | 0 | (void *)&ep->ifindex, |
872 | 0 | sizeof(ep->ifindex)); |
873 | | /* do not complain if bound addr scope is ifindex */ |
874 | 0 | if (rc && ep->ifindex != SCOPE(&ep->sin)) |
875 | 0 | msyslog(LOG_ERR, |
876 | 0 | "setsockopt IPV6_MULTICAST_IF %u for %s fails: %m", |
877 | 0 | ep->ifindex, stoa(&ep->sin)); |
878 | 0 | break; |
879 | 1 | # endif |
880 | 1 | } |
881 | 1 | #endif /* MCAST */ |
882 | 1 | } |
883 | | |
884 | | |
885 | | /* |
886 | | * remove interface from known interface list and clean up |
887 | | * associated resources |
888 | | */ |
889 | | static void |
890 | | remove_interface( |
891 | | endpt * ep |
892 | | ) |
893 | 0 | { |
894 | 0 | endpt * unlinked; |
895 | 0 | endpt ** pmclisthead; |
896 | 0 | sockaddr_u resmask; |
897 | 0 | int/*BOOL*/ success; |
898 | |
|
899 | 0 | UNLINK_SLIST(unlinked, ep_list, ep, elink, endpt); |
900 | 0 | if (!ep->ignore_packets && INT_MULTICAST & ep->flags) { |
901 | 0 | pmclisthead = (AF_INET == ep->family) |
902 | 0 | ? &mc4_list |
903 | 0 | : &mc6_list; |
904 | 0 | UNLINK_SLIST(unlinked, *pmclisthead, ep, mclink, endpt); |
905 | 0 | DPRINTF(4, ("%s %s IPv%s multicast-capable unicast local address list\n", |
906 | 0 | stoa(&ep->sin), |
907 | 0 | (unlinked != NULL) |
908 | 0 | ? "removed from" |
909 | 0 | : "not found on", |
910 | 0 | (AF_INET == ep->family) |
911 | 0 | ? "4" |
912 | 0 | : "6")); |
913 | 0 | } |
914 | 0 | delete_interface_from_list(ep); |
915 | |
|
916 | 0 | if (ep->fd != INVALID_SOCKET) { |
917 | 0 | msyslog(LOG_INFO, |
918 | 0 | "Deleting %d %s, [%s]:%hd, stats:" |
919 | 0 | " received=%ld, sent=%ld, dropped=%ld," |
920 | 0 | " active_time=%ld secs", |
921 | 0 | ep->ifnum, |
922 | 0 | ep->name, |
923 | 0 | stoa(&ep->sin), |
924 | 0 | SRCPORT(&ep->sin), |
925 | 0 | ep->received, |
926 | 0 | ep->sent, |
927 | 0 | ep->notsent, |
928 | 0 | current_time - ep->starttime); |
929 | 0 | close_and_delete_fd_from_list(ep->fd, ep); |
930 | 0 | ep->fd = INVALID_SOCKET; |
931 | 0 | } |
932 | |
|
933 | 0 | if (ep->bfd != INVALID_SOCKET) { |
934 | 0 | msyslog(LOG_INFO, |
935 | 0 | "stop listening for broadcasts to %s on interface #%d %s", |
936 | 0 | stoa(&ep->bcast), ep->ifnum, ep->name); |
937 | 0 | close_and_delete_fd_from_list(ep->bfd, ep); |
938 | 0 | ep->bfd = INVALID_SOCKET; |
939 | 0 | } |
940 | | # ifdef HAVE_IO_COMPLETION_PORT |
941 | | io_completion_port_remove_interface(ep); |
942 | | # endif |
943 | |
|
944 | 0 | ninterfaces--; |
945 | 0 | mon_clearinterface(ep); |
946 | | |
947 | | /* remove restrict interface entry */ |
948 | 0 | SET_HOSTMASK(&resmask, AF(&ep->sin)); |
949 | 0 | success = hack_restrict(RESTRICT_REMOVEIF, &ep->sin, &resmask, 0, |
950 | 0 | RESM_NTPONLY | RESM_INTERFACE, 0, 0); |
951 | 0 | if (!success) { |
952 | 0 | msyslog(LOG_ERR, |
953 | 0 | "unable to remove self-restriction for %s", |
954 | 0 | stoa(&ep->sin)); |
955 | 0 | } |
956 | |
|
957 | 0 | } |
958 | | |
959 | | |
960 | | static void |
961 | | log_listen_address( |
962 | | endpt * ep |
963 | | ) |
964 | 4 | { |
965 | 4 | msyslog(LOG_INFO, "%s on %d %s %s", |
966 | 4 | (ep->ignore_packets) |
967 | 4 | ? "Listen and drop" |
968 | 4 | : "Listen normally", |
969 | 4 | ep->ifnum, |
970 | 4 | ep->name, |
971 | 4 | sptoa(&ep->sin)); |
972 | 4 | } |
973 | | |
974 | | |
975 | | static void |
976 | | create_wildcards( |
977 | | u_short port |
978 | | ) |
979 | 1 | { |
980 | 1 | int v4wild; |
981 | 1 | #ifdef INCLUDE_IPV6_SUPPORT |
982 | 1 | int v6wild; |
983 | 1 | #endif |
984 | 1 | sockaddr_u wildaddr; |
985 | 1 | nic_rule_action action; |
986 | 1 | endpt * wildif; |
987 | | |
988 | | /* |
989 | | * silence "potentially uninitialized" warnings from VC9 |
990 | | * failing to follow the logic. Ideally action could remain |
991 | | * uninitialized, and the memset be the first statement under |
992 | | * the first if (v4wild). |
993 | | */ |
994 | 1 | action = ACTION_LISTEN; |
995 | 1 | ZERO(wildaddr); |
996 | | |
997 | 1 | #ifdef INCLUDE_IPV6_SUPPORT |
998 | | /* |
999 | | * create pseudo-interface with wildcard IPv6 address |
1000 | | */ |
1001 | 1 | v6wild = ipv6_works; |
1002 | 1 | if (v6wild) { |
1003 | | /* set wildaddr to the v6 wildcard address :: */ |
1004 | 1 | ZERO(wildaddr); |
1005 | 1 | AF(&wildaddr) = AF_INET6; |
1006 | 1 | SET_ADDR6N(&wildaddr, in6addr_any); |
1007 | 1 | SET_PORT(&wildaddr, port); |
1008 | 1 | SET_SCOPE(&wildaddr, 0); |
1009 | | |
1010 | | /* check for interface/nic rules affecting the wildcard */ |
1011 | 1 | action = interface_action(NULL, &wildaddr, 0); |
1012 | 1 | v6wild = (ACTION_IGNORE != action); |
1013 | 1 | } |
1014 | 1 | if (v6wild) { |
1015 | 1 | wildif = new_interface(NULL); |
1016 | | |
1017 | 1 | strlcpy(wildif->name, "v6wildcard", sizeof(wildif->name)); |
1018 | 1 | memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin)); |
1019 | 1 | wildif->family = AF_INET6; |
1020 | 1 | AF(&wildif->mask) = AF_INET6; |
1021 | 1 | SET_ONESMASK(&wildif->mask); |
1022 | | |
1023 | 1 | wildif->flags = INT_UP | INT_WILDCARD; |
1024 | 1 | wildif->ignore_packets = (ACTION_DROP == action); |
1025 | | |
1026 | 1 | wildif->fd = open_socket(&wildif->sin, 0, 1, wildif); |
1027 | | |
1028 | 1 | if (wildif->fd != INVALID_SOCKET) { |
1029 | 1 | wildipv6 = wildif; |
1030 | 1 | any6_interface = wildif; |
1031 | 1 | add_addr_to_list(&wildif->sin, wildif); |
1032 | 1 | add_interface(wildif); |
1033 | 1 | log_listen_address(wildif); |
1034 | 1 | } else { |
1035 | 0 | msyslog(LOG_ERR, |
1036 | 0 | "unable to bind to wildcard address %s - another process may be running - EXITING", |
1037 | 0 | stoa(&wildif->sin)); |
1038 | 0 | exit(1); |
1039 | 0 | } |
1040 | 1 | DPRINT_INTERFACE(2, (wildif, "created ", "\n")); |
1041 | 1 | } |
1042 | 1 | #endif |
1043 | | |
1044 | | /* |
1045 | | * create pseudo-interface with wildcard IPv4 address |
1046 | | */ |
1047 | 1 | v4wild = ipv4_works; |
1048 | 1 | if (v4wild) { |
1049 | | /* set wildaddr to the v4 wildcard address 0.0.0.0 */ |
1050 | 1 | AF(&wildaddr) = AF_INET; |
1051 | 1 | SET_ADDR4N(&wildaddr, INADDR_ANY); |
1052 | 1 | SET_PORT(&wildaddr, port); |
1053 | | |
1054 | | /* check for interface/nic rules affecting the wildcard */ |
1055 | 1 | action = interface_action(NULL, &wildaddr, 0); |
1056 | 1 | v4wild = (ACTION_IGNORE != action); |
1057 | 1 | } |
1058 | 1 | if (v4wild) { |
1059 | 1 | wildif = new_interface(NULL); |
1060 | | |
1061 | 1 | strlcpy(wildif->name, "v4wildcard", sizeof(wildif->name)); |
1062 | 1 | memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin)); |
1063 | 1 | wildif->family = AF_INET; |
1064 | 1 | AF(&wildif->mask) = AF_INET; |
1065 | 1 | SET_ONESMASK(&wildif->mask); |
1066 | | |
1067 | 1 | wildif->flags = INT_BROADCAST | INT_UP | INT_WILDCARD; |
1068 | 1 | wildif->ignore_packets = (ACTION_DROP == action); |
1069 | 1 | #if defined(MCAST) |
1070 | | /* |
1071 | | * enable multicast reception on the broadcast socket |
1072 | | */ |
1073 | 1 | AF(&wildif->bcast) = AF_INET; |
1074 | 1 | SET_ADDR4N(&wildif->bcast, INADDR_ANY); |
1075 | 1 | SET_PORT(&wildif->bcast, port); |
1076 | 1 | #endif /* MCAST */ |
1077 | 1 | wildif->fd = open_socket(&wildif->sin, 0, 1, wildif); |
1078 | | |
1079 | 1 | if (wildif->fd != INVALID_SOCKET) { |
1080 | 1 | wildipv4 = wildif; |
1081 | 1 | any_interface = wildif; |
1082 | | |
1083 | 1 | add_addr_to_list(&wildif->sin, wildif); |
1084 | 1 | add_interface(wildif); |
1085 | 1 | log_listen_address(wildif); |
1086 | 1 | } else { |
1087 | 0 | msyslog(LOG_ERR, |
1088 | 0 | "unable to bind to wildcard address %s - another process may be running - EXITING", |
1089 | 0 | stoa(&wildif->sin)); |
1090 | 0 | exit(1); |
1091 | 0 | } |
1092 | 1 | DPRINT_INTERFACE(2, (wildif, "created ", "\n")); |
1093 | 1 | } |
1094 | 1 | } |
1095 | | |
1096 | | |
1097 | | /* |
1098 | | * add_nic_rule() -- insert a rule entry at the head of nic_rule_list. |
1099 | | */ |
1100 | | void |
1101 | | add_nic_rule( |
1102 | | nic_rule_match match_type, |
1103 | | const char * if_name, /* interface name or numeric address */ |
1104 | | int prefixlen, |
1105 | | nic_rule_action action |
1106 | | ) |
1107 | 0 | { |
1108 | 0 | nic_rule * rule; |
1109 | 0 | isc_boolean_t is_ip; |
1110 | |
|
1111 | 0 | rule = emalloc_zero(sizeof(*rule)); |
1112 | 0 | rule->match_type = match_type; |
1113 | 0 | rule->prefixlen = prefixlen; |
1114 | 0 | rule->action = action; |
1115 | |
|
1116 | 0 | if (MATCH_IFNAME == match_type) { |
1117 | 0 | REQUIRE(NULL != if_name); |
1118 | 0 | rule->if_name = estrdup(if_name); |
1119 | 0 | } else if (MATCH_IFADDR == match_type) { |
1120 | 0 | REQUIRE(NULL != if_name); |
1121 | | /* set rule->addr */ |
1122 | 0 | is_ip = sau_from_string(if_name, AF_UNSPEC, &rule->addr); |
1123 | 0 | REQUIRE(is_ip); |
1124 | 0 | } else |
1125 | 0 | REQUIRE(NULL == if_name); |
1126 | | |
1127 | 0 | LINK_SLIST(nic_rule_list, rule, next); |
1128 | 0 | } |
1129 | | |
1130 | | |
1131 | | #ifdef DEBUG |
1132 | | static const char * |
1133 | | action_text( |
1134 | | nic_rule_action action |
1135 | | ) |
1136 | 0 | { |
1137 | 0 | const char *t; |
1138 | |
|
1139 | 0 | switch (action) { |
1140 | | |
1141 | 0 | default: |
1142 | 0 | t = "ERROR"; /* quiet uninit warning */ |
1143 | 0 | DPRINTF(1, ("fatal: unknown nic_rule_action %d\n", |
1144 | 0 | action)); |
1145 | 0 | ENSURE(0); |
1146 | 0 | break; |
1147 | | |
1148 | 0 | case ACTION_LISTEN: |
1149 | 0 | t = "listen"; |
1150 | 0 | break; |
1151 | | |
1152 | 0 | case ACTION_IGNORE: |
1153 | 0 | t = "ignore"; |
1154 | 0 | break; |
1155 | | |
1156 | 0 | case ACTION_DROP: |
1157 | 0 | t = "drop"; |
1158 | 0 | break; |
1159 | 0 | } |
1160 | | |
1161 | 0 | return t; |
1162 | 0 | } |
1163 | | #endif /* DEBUG */ |
1164 | | |
1165 | | |
1166 | | static nic_rule_action |
1167 | | interface_action( |
1168 | | char * if_name, |
1169 | | sockaddr_u * if_addr, |
1170 | | u_int32 if_flags |
1171 | | ) |
1172 | 4 | { |
1173 | 4 | nic_rule * rule; |
1174 | 4 | int isloopback; |
1175 | 4 | int iswildcard; |
1176 | | |
1177 | 4 | DPRINTF(4, ("interface_action: interface %s ", |
1178 | 4 | (if_name != NULL) ? if_name : "wildcard")); |
1179 | | |
1180 | 4 | iswildcard = is_wildcard_addr(if_addr); |
1181 | 4 | isloopback = !!(INT_LOOPBACK & if_flags); |
1182 | | |
1183 | | /* |
1184 | | * Find any matching NIC rule from --interface / -I or ntp.conf |
1185 | | * interface/nic rules. |
1186 | | */ |
1187 | 4 | for (rule = nic_rule_list; rule != NULL; rule = rule->next) { |
1188 | |
|
1189 | 0 | switch (rule->match_type) { |
1190 | | |
1191 | 0 | case MATCH_ALL: |
1192 | | /* loopback and wildcard excluded from "all" */ |
1193 | 0 | if (isloopback || iswildcard) |
1194 | 0 | break; |
1195 | 0 | DPRINTF(4, ("nic all %s\n", |
1196 | 0 | action_text(rule->action))); |
1197 | 0 | return rule->action; |
1198 | | |
1199 | 0 | case MATCH_IPV4: |
1200 | 0 | if (IS_IPV4(if_addr)) { |
1201 | 0 | DPRINTF(4, ("nic ipv4 %s\n", |
1202 | 0 | action_text(rule->action))); |
1203 | 0 | return rule->action; |
1204 | 0 | } |
1205 | 0 | break; |
1206 | | |
1207 | 0 | case MATCH_IPV6: |
1208 | 0 | if (IS_IPV6(if_addr)) { |
1209 | 0 | DPRINTF(4, ("nic ipv6 %s\n", |
1210 | 0 | action_text(rule->action))); |
1211 | 0 | return rule->action; |
1212 | 0 | } |
1213 | 0 | break; |
1214 | | |
1215 | 0 | case MATCH_WILDCARD: |
1216 | 0 | if (iswildcard) { |
1217 | 0 | DPRINTF(4, ("nic wildcard %s\n", |
1218 | 0 | action_text(rule->action))); |
1219 | 0 | return rule->action; |
1220 | 0 | } |
1221 | 0 | break; |
1222 | | |
1223 | 0 | case MATCH_IFADDR: |
1224 | 0 | if (rule->prefixlen != -1) { |
1225 | 0 | if (addr_eqprefix(if_addr, &rule->addr, |
1226 | 0 | rule->prefixlen)) { |
1227 | |
|
1228 | 0 | DPRINTF(4, ("subnet address match - %s\n", |
1229 | 0 | action_text(rule->action))); |
1230 | 0 | return rule->action; |
1231 | 0 | } |
1232 | 0 | } else |
1233 | 0 | if (SOCK_EQ(if_addr, &rule->addr)) { |
1234 | |
|
1235 | 0 | DPRINTF(4, ("address match - %s\n", |
1236 | 0 | action_text(rule->action))); |
1237 | 0 | return rule->action; |
1238 | 0 | } |
1239 | 0 | break; |
1240 | | |
1241 | 0 | case MATCH_IFNAME: |
1242 | 0 | if (if_name != NULL |
1243 | 0 | #if defined(HAVE_FNMATCH) && defined(FNM_CASEFOLD) |
1244 | 0 | && !fnmatch(rule->if_name, if_name, FNM_CASEFOLD) |
1245 | | #else |
1246 | | && !strcasecmp(if_name, rule->if_name) |
1247 | | #endif |
1248 | 0 | ) { |
1249 | |
|
1250 | 0 | DPRINTF(4, ("interface name match - %s\n", |
1251 | 0 | action_text(rule->action))); |
1252 | 0 | return rule->action; |
1253 | 0 | } |
1254 | 0 | break; |
1255 | 0 | } |
1256 | 0 | } |
1257 | | |
1258 | | /* |
1259 | | * Unless explicitly disabled such as with "nic ignore ::1" |
1260 | | * listen on loopback addresses. Since ntpq and ntpdc query |
1261 | | * "localhost" by default, which typically resolves to ::1 and |
1262 | | * 127.0.0.1, it's useful to default to listening on both. |
1263 | | */ |
1264 | 4 | if (isloopback) { |
1265 | 1 | DPRINTF(4, ("default loopback listen\n")); |
1266 | 1 | return ACTION_LISTEN; |
1267 | 1 | } |
1268 | | |
1269 | | /* |
1270 | | * Treat wildcard addresses specially. If there is no explicit |
1271 | | * "nic ... wildcard" or "nic ... 0.0.0.0" or "nic ... ::" rule |
1272 | | * default to drop. |
1273 | | */ |
1274 | 3 | if (iswildcard) { |
1275 | 2 | DPRINTF(4, ("default wildcard drop\n")); |
1276 | 2 | return ACTION_DROP; |
1277 | 2 | } |
1278 | | |
1279 | | /* |
1280 | | * Check for "virtual IP" (colon in the interface name) after |
1281 | | * the rules so that "ntpd --interface eth0:1 -novirtualips" |
1282 | | * does indeed listen on eth0:1's addresses. |
1283 | | */ |
1284 | 1 | if (!listen_to_virtual_ips && if_name != NULL |
1285 | 0 | && (strchr(if_name, ':') != NULL)) { |
1286 | |
|
1287 | 0 | DPRINTF(4, ("virtual ip - ignore\n")); |
1288 | 0 | return ACTION_IGNORE; |
1289 | 0 | } |
1290 | | |
1291 | | /* |
1292 | | * If there are no --interface/-I command-line options and no |
1293 | | * interface/nic rules in ntp.conf, the default action is to |
1294 | | * listen. In the presence of rules from either, the default |
1295 | | * is to ignore. This implements ntpd's traditional listen- |
1296 | | * every default with no interface listen configuration, and |
1297 | | * ensures a single -I eth0 or "nic listen eth0" means do not |
1298 | | * listen on any other addresses. |
1299 | | */ |
1300 | 1 | if (NULL == nic_rule_list) { |
1301 | 1 | DPRINTF(4, ("default listen\n")); |
1302 | 1 | return ACTION_LISTEN; |
1303 | 1 | } |
1304 | | |
1305 | 0 | DPRINTF(4, ("implicit ignore\n")); |
1306 | 0 | return ACTION_IGNORE; |
1307 | 1 | } |
1308 | | |
1309 | | |
1310 | | static void |
1311 | | convert_isc_if( |
1312 | | isc_interface_t *isc_if, |
1313 | | endpt *itf, |
1314 | | u_short port |
1315 | | ) |
1316 | 2 | { |
1317 | 2 | strlcpy(itf->name, isc_if->name, sizeof(itf->name)); |
1318 | 2 | itf->ifindex = isc_if->ifindex; |
1319 | 2 | itf->family = (u_short)isc_if->af; |
1320 | 2 | AF(&itf->sin) = itf->family; |
1321 | 2 | AF(&itf->mask) = itf->family; |
1322 | 2 | AF(&itf->bcast) = itf->family; |
1323 | 2 | SET_PORT(&itf->sin, port); |
1324 | 2 | SET_PORT(&itf->mask, port); |
1325 | 2 | SET_PORT(&itf->bcast, port); |
1326 | | |
1327 | 2 | if (IS_IPV4(&itf->sin)) { |
1328 | 2 | NSRCADR(&itf->sin) = isc_if->address.type.in.s_addr; |
1329 | 2 | NSRCADR(&itf->mask) = isc_if->netmask.type.in.s_addr; |
1330 | | |
1331 | 2 | if (isc_if->flags & INTERFACE_F_BROADCAST) { |
1332 | 1 | itf->flags |= INT_BROADCAST; |
1333 | 1 | NSRCADR(&itf->bcast) = |
1334 | 1 | isc_if->broadcast.type.in.s_addr; |
1335 | 1 | } |
1336 | 2 | } |
1337 | 0 | #ifdef INCLUDE_IPV6_SUPPORT |
1338 | 0 | else if (IS_IPV6(&itf->sin)) { |
1339 | 0 | SET_ADDR6N(&itf->sin, isc_if->address.type.in6); |
1340 | 0 | SET_ADDR6N(&itf->mask, isc_if->netmask.type.in6); |
1341 | |
|
1342 | 0 | SET_SCOPE(&itf->sin, isc_if->address.zone); |
1343 | 0 | } |
1344 | 2 | #endif /* INCLUDE_IPV6_SUPPORT */ |
1345 | | |
1346 | | |
1347 | | /* Process the rest of the flags */ |
1348 | | |
1349 | 2 | itf->flags |= |
1350 | 2 | ((INTERFACE_F_UP & isc_if->flags) |
1351 | 2 | ? INT_UP : 0) |
1352 | 2 | | ((INTERFACE_F_LOOPBACK & isc_if->flags) |
1353 | 2 | ? INT_LOOPBACK : 0) |
1354 | 2 | | ((INTERFACE_F_POINTTOPOINT & isc_if->flags) |
1355 | 2 | ? INT_PPP : 0) |
1356 | 2 | | ((INTERFACE_F_MULTICAST & isc_if->flags) |
1357 | 2 | ? INT_MULTICAST : 0) |
1358 | 2 | | ((INTERFACE_F_PRIVACY & isc_if->flags) |
1359 | 2 | ? INT_PRIVACY : 0) |
1360 | 2 | ; |
1361 | | |
1362 | | /* |
1363 | | * Clear the loopback flag if the address is not localhost. |
1364 | | * http://bugs.ntp.org/1683 |
1365 | | */ |
1366 | 2 | if ((INT_LOOPBACK & itf->flags) && !IS_LOOPBACK_ADDR(&itf->sin)) { |
1367 | 0 | itf->flags &= ~INT_LOOPBACK; |
1368 | 0 | } |
1369 | 2 | } |
1370 | | |
1371 | | |
1372 | | /* |
1373 | | * refresh_interface |
1374 | | * |
1375 | | * some OSes have been observed to keep |
1376 | | * cached routes even when more specific routes |
1377 | | * become available. |
1378 | | * this can be mitigated by re-binding |
1379 | | * the socket. |
1380 | | */ |
1381 | | static int |
1382 | | refresh_interface( |
1383 | | endpt * iface |
1384 | | ) |
1385 | 0 | { |
1386 | | #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES |
1387 | | if (iface->fd != INVALID_SOCKET) { |
1388 | | int bcast = (iface->flags & INT_BCASTXMIT) != 0; |
1389 | | /* as we forcibly close() the socket remove the |
1390 | | broadcast permission indication */ |
1391 | | if (bcast) |
1392 | | socket_broadcast_disable(iface, &iface->sin); |
1393 | | |
1394 | | close_and_delete_fd_from_list(iface->fd); |
1395 | | |
1396 | | /* create new socket picking up a new first hop binding |
1397 | | at connect() time */ |
1398 | | iface->fd = open_socket(&iface->sin, |
1399 | | bcast, 0, iface); |
1400 | | /* |
1401 | | * reset TTL indication so TTL is is set again |
1402 | | * next time around |
1403 | | */ |
1404 | | iface->last_ttl = 0; |
1405 | | return (iface->fd != INVALID_SOCKET); |
1406 | | } else |
1407 | | return 0; /* invalid sockets are not refreshable */ |
1408 | | #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */ |
1409 | 0 | return (iface->fd != INVALID_SOCKET); |
1410 | 0 | #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */ |
1411 | 0 | } |
1412 | | |
1413 | | /* |
1414 | | * interface_update - externally callable update function |
1415 | | */ |
1416 | | void |
1417 | | interface_update( |
1418 | | interface_receiver_t receiver, |
1419 | | void * data |
1420 | | ) |
1421 | 0 | { |
1422 | 0 | int new_interface_found; |
1423 | |
|
1424 | 0 | if (scan_addrs_once) { |
1425 | 0 | return; |
1426 | 0 | } |
1427 | 0 | BLOCKIO(); |
1428 | 0 | new_interface_found = update_interfaces(NTP_PORT, receiver, data); |
1429 | 0 | UNBLOCKIO(); |
1430 | |
|
1431 | 0 | if (!new_interface_found) { |
1432 | 0 | return; |
1433 | 0 | } |
1434 | 0 | #ifdef DEBUG |
1435 | 0 | msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver"); |
1436 | 0 | #endif |
1437 | 0 | interrupt_worker_sleep(); |
1438 | 0 | } |
1439 | | |
1440 | | |
1441 | | /* |
1442 | | * sau_from_netaddr() - convert network address on-wire formats. |
1443 | | * Convert from libisc's isc_netaddr_t to NTP's sockaddr_u |
1444 | | */ |
1445 | | void |
1446 | | sau_from_netaddr( |
1447 | | sockaddr_u *psau, |
1448 | | const isc_netaddr_t *pna |
1449 | | ) |
1450 | 0 | { |
1451 | 0 | ZERO_SOCK(psau); |
1452 | 0 | AF(psau) = (u_short)pna->family; |
1453 | 0 | switch (pna->family) { |
1454 | | |
1455 | 0 | case AF_INET: |
1456 | 0 | psau->sa4.sin_addr = pna->type.in; |
1457 | 0 | break; |
1458 | | |
1459 | 0 | case AF_INET6: |
1460 | 0 | psau->sa6.sin6_addr = pna->type.in6; |
1461 | 0 | break; |
1462 | 0 | } |
1463 | 0 | } |
1464 | | |
1465 | | |
1466 | | static int |
1467 | | is_wildcard_addr( |
1468 | | const sockaddr_u *psau |
1469 | | ) |
1470 | 14 | { |
1471 | 14 | if (IS_IPV4(psau) && !NSRCADR(psau)) |
1472 | 3 | return 1; |
1473 | | |
1474 | 11 | #ifdef INCLUDE_IPV6_SUPPORT |
1475 | 11 | if (IS_IPV6(psau) && S_ADDR6_EQ(psau, &in6addr_any)) |
1476 | 3 | return 1; |
1477 | 8 | #endif |
1478 | | |
1479 | 8 | return 0; |
1480 | 11 | } |
1481 | | |
1482 | | |
1483 | | isc_boolean_t |
1484 | | is_linklocal( |
1485 | | sockaddr_u * psau |
1486 | | ) |
1487 | 1 | { |
1488 | 1 | struct in6_addr * p6addr; |
1489 | | |
1490 | 1 | if (IS_IPV6(psau)) { |
1491 | 0 | p6addr = &psau->sa6.sin6_addr; |
1492 | 0 | if (IN6_IS_ADDR_LINKLOCAL(p6addr)) { |
1493 | 0 | return TRUE; |
1494 | 0 | } |
1495 | 1 | } else if (IS_IPV4(psau)) { |
1496 | | /* autoconf are link-local 169.254.0.0/16 */ |
1497 | 1 | if (IS_AUTOCONF(psau)) { |
1498 | 0 | return TRUE; |
1499 | 0 | } |
1500 | 1 | } |
1501 | 1 | return FALSE; |
1502 | 1 | } |
1503 | | |
1504 | | |
1505 | | #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND |
1506 | | /* |
1507 | | * enable/disable re-use of wildcard address socket |
1508 | | */ |
1509 | | static void |
1510 | | set_wildcard_reuse( |
1511 | | u_short family, |
1512 | | int on |
1513 | | ) |
1514 | 4 | { |
1515 | 4 | endpt *any; |
1516 | 4 | SOCKET fd = INVALID_SOCKET; |
1517 | | |
1518 | 4 | any = ANY_INTERFACE_BYFAM(family); |
1519 | 4 | if (any != NULL) |
1520 | 4 | fd = any->fd; |
1521 | | |
1522 | 4 | if (fd != INVALID_SOCKET) { |
1523 | 4 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
1524 | 4 | (void *)&on, sizeof(on))) |
1525 | 0 | msyslog(LOG_ERR, |
1526 | 0 | "set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m", |
1527 | 0 | on ? "on" : "off"); |
1528 | | |
1529 | 4 | DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n", |
1530 | 4 | on ? "on" : "off", |
1531 | 4 | stoa(&any->sin))); |
1532 | 4 | } |
1533 | 4 | } |
1534 | | #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */ |
1535 | | |
1536 | | static isc_boolean_t |
1537 | | check_flags( |
1538 | | sockaddr_u *psau, |
1539 | | const char *name, |
1540 | | u_int32 flags |
1541 | | ) |
1542 | 2 | { |
1543 | | #if defined(SIOCGIFAFLAG_IN) |
1544 | | struct ifreq ifr; |
1545 | | int fd; |
1546 | | |
1547 | | if (psau->sa.sa_family != AF_INET) |
1548 | | return ISC_FALSE; |
1549 | | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
1550 | | return ISC_FALSE; |
1551 | | ZERO(ifr); |
1552 | | memcpy(&ifr.ifr_addr, &psau->sa, sizeof(ifr.ifr_addr)); |
1553 | | strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
1554 | | if (ioctl(fd, SIOCGIFAFLAG_IN, &ifr) < 0) { |
1555 | | close(fd); |
1556 | | return ISC_FALSE; |
1557 | | } |
1558 | | close(fd); |
1559 | | if ((ifr.ifr_addrflags & flags) != 0) |
1560 | | return ISC_TRUE; |
1561 | | #endif /* SIOCGIFAFLAG_IN */ |
1562 | 2 | return ISC_FALSE; |
1563 | 2 | } |
1564 | | |
1565 | | static isc_boolean_t |
1566 | | check_flags6( |
1567 | | sockaddr_u *psau, |
1568 | | const char *name, |
1569 | | u_int32 flags6 |
1570 | | ) |
1571 | 0 | { |
1572 | | #if defined(INCLUDE_IPV6_SUPPORT) && defined(SIOCGIFAFLAG_IN6) |
1573 | | struct in6_ifreq ifr6; |
1574 | | int fd; |
1575 | | |
1576 | | if (psau->sa.sa_family != AF_INET6) |
1577 | | return ISC_FALSE; |
1578 | | if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) |
1579 | | return ISC_FALSE; |
1580 | | ZERO(ifr6); |
1581 | | memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr)); |
1582 | | strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); |
1583 | | if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) { |
1584 | | close(fd); |
1585 | | return ISC_FALSE; |
1586 | | } |
1587 | | close(fd); |
1588 | | if ((ifr6.ifr_ifru.ifru_flags6 & flags6) != 0) |
1589 | | return ISC_TRUE; |
1590 | | #endif /* INCLUDE_IPV6_SUPPORT && SIOCGIFAFLAG_IN6 */ |
1591 | 0 | return ISC_FALSE; |
1592 | 0 | } |
1593 | | |
1594 | | static isc_boolean_t |
1595 | | is_anycast( |
1596 | | sockaddr_u *psau, |
1597 | | const char *name |
1598 | | ) |
1599 | 2 | { |
1600 | | #ifdef IN6_IFF_ANYCAST |
1601 | | return check_flags6(psau, name, IN6_IFF_ANYCAST); |
1602 | | #else |
1603 | 2 | return ISC_FALSE; |
1604 | 2 | #endif |
1605 | 2 | } |
1606 | | |
1607 | | static isc_boolean_t |
1608 | | is_valid( |
1609 | | sockaddr_u *psau, |
1610 | | const char *name |
1611 | | ) |
1612 | 2 | { |
1613 | 2 | u_int32 flags; |
1614 | | |
1615 | 2 | flags = 0; |
1616 | 2 | switch (psau->sa.sa_family) { |
1617 | 2 | case AF_INET: |
1618 | | #ifdef IN_IFF_DETACHED |
1619 | | flags |= IN_IFF_DETACHED; |
1620 | | #endif |
1621 | | #ifdef IN_IFF_TENTATIVE |
1622 | | flags |= IN_IFF_TENTATIVE; |
1623 | | #endif |
1624 | 2 | return check_flags(psau, name, flags) ? ISC_FALSE : ISC_TRUE; |
1625 | 0 | case AF_INET6: |
1626 | | #ifdef IN6_IFF_DEPARTED |
1627 | | flags |= IN6_IFF_DEPARTED; |
1628 | | #endif |
1629 | | #ifdef IN6_IFF_DETACHED |
1630 | | flags |= IN6_IFF_DETACHED; |
1631 | | #endif |
1632 | | #ifdef IN6_IFF_TENTATIVE |
1633 | | flags |= IN6_IFF_TENTATIVE; |
1634 | | #endif |
1635 | 0 | return check_flags6(psau, name, flags) ? ISC_FALSE : ISC_TRUE; |
1636 | 0 | default: |
1637 | 0 | return ISC_FALSE; |
1638 | 2 | } |
1639 | 2 | } |
1640 | | |
1641 | | |
1642 | | /* rename an endpt for an address used on multiple local interfaces */ |
1643 | | static void |
1644 | | rename_multi_iface_endpt( |
1645 | | endpt * target, |
1646 | | endpt * const other |
1647 | | ) |
1648 | 0 | { |
1649 | 0 | char new_name[sizeof(target->name)]; |
1650 | 0 | int rc; |
1651 | |
|
1652 | 0 | rc = snprintf(new_name, sizeof(new_name), "%s,%s", |
1653 | 0 | target->name, other->name); |
1654 | 0 | if (rc < sizeof(new_name)) { |
1655 | 0 | strlcpy(target->name, new_name, sizeof(target->name)); |
1656 | 0 | } else { |
1657 | 0 | msyslog(LOG_INFO, "%s on %s & %s -> *multiple*", |
1658 | 0 | stoa(&target->sin), target->name, other->name); |
1659 | 0 | strlcpy(target->name, "*multiple*", sizeof(target->name)); |
1660 | 0 | } |
1661 | 0 | } |
1662 | | |
1663 | | |
1664 | | /* |
1665 | | * update_interface strategy |
1666 | | * |
1667 | | * toggle configuration phase |
1668 | | * |
1669 | | * Phase 1a: |
1670 | | * forall currently existing interfaces |
1671 | | * if address is known: |
1672 | | * drop socket - rebind again |
1673 | | * |
1674 | | * if address is NOT known: |
1675 | | * Add address to list of new addresses |
1676 | | * |
1677 | | * Phase 1b: |
1678 | | * Scan the list of new addresses marking IPv6 link-local addresses |
1679 | | * which also have a global v6 address using the same OS ifindex. |
1680 | | * Attempt to create a new interface entry |
1681 | | * |
1682 | | * Phase 2: |
1683 | | * forall currently known non MCAST and WILDCARD interfaces |
1684 | | * if interface does not match configuration phase (not seen in phase 1): |
1685 | | * remove interface from known interface list |
1686 | | * forall peers associated with this interface |
1687 | | * disconnect peer from this interface |
1688 | | * |
1689 | | * Phase 3: |
1690 | | * attempt to re-assign interfaces to peers |
1691 | | * |
1692 | | */ |
1693 | | |
1694 | | static int |
1695 | | update_interfaces( |
1696 | | u_short port, |
1697 | | interface_receiver_t receiver, |
1698 | | void * data |
1699 | | ) |
1700 | 1 | { |
1701 | 1 | isc_mem_t * mctx = (void *)-1; |
1702 | 1 | interface_info_t ifi; |
1703 | 1 | isc_interfaceiter_t * iter; |
1704 | 1 | isc_result_t result; |
1705 | 1 | isc_interface_t isc_if; |
1706 | 1 | int new_interface_found; |
1707 | 1 | unsigned int family; |
1708 | 1 | endpt enumep; |
1709 | 1 | endpt * ep; |
1710 | 1 | endpt * next_ep; |
1711 | 1 | endpt * newaddrs; |
1712 | 1 | endpt * newaddrs_tail; |
1713 | 1 | endpt * ep2; |
1714 | | |
1715 | 1 | DPRINTF(3, ("update_interfaces(%d)\n", port)); |
1716 | | |
1717 | | /* |
1718 | | * phase 1a - scan OS local addresses |
1719 | | * - update those that ntpd already knows |
1720 | | * - build a list of newly-discovered addresses. |
1721 | | */ |
1722 | | |
1723 | 1 | new_interface_found = FALSE; |
1724 | 1 | nonlocal_v4_addr_up = nonlocal_v6_addr_up = FALSE; |
1725 | 1 | iter = NULL; |
1726 | 1 | newaddrs = newaddrs_tail = NULL; |
1727 | 1 | result = isc_interfaceiter_create(mctx, &iter); |
1728 | | |
1729 | 1 | if (result != ISC_R_SUCCESS) |
1730 | 0 | return 0; |
1731 | | |
1732 | | /* |
1733 | | * Toggle system interface scan phase to find untouched |
1734 | | * interfaces to be deleted. |
1735 | | */ |
1736 | 1 | sys_interphase ^= 0x1; |
1737 | | |
1738 | 1 | for (result = isc_interfaceiter_first(iter); |
1739 | 3 | ISC_R_SUCCESS == result; |
1740 | 2 | result = isc_interfaceiter_next(iter)) { |
1741 | | |
1742 | 2 | result = isc_interfaceiter_current(iter, &isc_if); |
1743 | | |
1744 | 2 | if (result != ISC_R_SUCCESS) { |
1745 | 0 | break; |
1746 | 0 | } |
1747 | | /* See if we have a valid family to use */ |
1748 | 2 | family = isc_if.address.family; |
1749 | 2 | if (AF_INET != family && AF_INET6 != family) |
1750 | 0 | continue; |
1751 | 2 | if (AF_INET == family && !ipv4_works) |
1752 | 0 | continue; |
1753 | 2 | if (AF_INET6 == family && !ipv6_works) |
1754 | 0 | continue; |
1755 | | |
1756 | | /* create prototype */ |
1757 | 2 | init_interface(&enumep); |
1758 | | |
1759 | 2 | convert_isc_if(&isc_if, &enumep, port); |
1760 | | |
1761 | 2 | DPRINT_INTERFACE(4, (&enumep, "examining ", "\n")); |
1762 | | |
1763 | | #ifdef SYS_WINNT |
1764 | | /* |
1765 | | * https://bugs.ntp.org/3932 Ignore teredo IFs in Windows ntpd |
1766 | | */ |
1767 | | static const char szTeredo[] = "Teredo Tunneling Pseudo-Interfa"; |
1768 | | if (!memcmp(szTeredo, enumep.name, |
1769 | | min(sizeof szTeredo, sizeof enumep.name))) { |
1770 | | continue; |
1771 | | } |
1772 | | #endif |
1773 | | |
1774 | | /* |
1775 | | * Check if and how we are going to use the interface. |
1776 | | */ |
1777 | 2 | switch (interface_action(enumep.name, &enumep.sin, |
1778 | 2 | enumep.flags)) { |
1779 | | |
1780 | 0 | case ACTION_IGNORE: |
1781 | 0 | DPRINTF(4, ("ignoring interface %s (%s) - by nic rules\n", |
1782 | 0 | enumep.name, stoa(&enumep.sin))); |
1783 | 0 | continue; |
1784 | | |
1785 | 2 | case ACTION_LISTEN: |
1786 | 2 | DPRINTF(4, ("listen interface %s (%s) - by nic rules\n", |
1787 | 2 | enumep.name, stoa(&enumep.sin))); |
1788 | 2 | enumep.ignore_packets = ISC_FALSE; |
1789 | 2 | break; |
1790 | | |
1791 | 0 | case ACTION_DROP: |
1792 | 0 | DPRINTF(4, ("drop on interface %s (%s) - by nic rules\n", |
1793 | 0 | enumep.name, stoa(&enumep.sin))); |
1794 | 0 | enumep.ignore_packets = ISC_TRUE; |
1795 | 0 | break; |
1796 | 2 | } |
1797 | | |
1798 | | /* interfaces must be UP to be usable */ |
1799 | 2 | if (!(enumep.flags & INT_UP)) { |
1800 | 0 | DPRINTF(4, ("skipping interface %s (%s) - DOWN\n", |
1801 | 0 | enumep.name, stoa(&enumep.sin))); |
1802 | 0 | continue; |
1803 | 0 | } |
1804 | | |
1805 | | /* |
1806 | | * skip any interfaces UP and bound to a wildcard |
1807 | | * address - some dhcp clients produce that in the |
1808 | | * wild |
1809 | | */ |
1810 | 2 | if (is_wildcard_addr(&enumep.sin)) |
1811 | 0 | continue; |
1812 | | |
1813 | 2 | if (is_anycast(&enumep.sin, isc_if.name)) |
1814 | 0 | continue; |
1815 | | |
1816 | | /* |
1817 | | * skip any address that is an invalid state to be used |
1818 | | */ |
1819 | 2 | if (!is_valid(&enumep.sin, isc_if.name)) |
1820 | 0 | continue; |
1821 | | |
1822 | | /* |
1823 | | * Keep track of having non-linklocal connectivity |
1824 | | * for IPv4 and IPv6 so we don't solicit pool hosts |
1825 | | * when it can't work. |
1826 | | */ |
1827 | 2 | if ( !(INT_LOOPBACK & enumep.flags) |
1828 | 1 | && !is_linklocal(&enumep.sin)) { |
1829 | 1 | if (IS_IPV6(&enumep.sin)) { |
1830 | 0 | nonlocal_v6_addr_up = TRUE; |
1831 | 1 | } else { |
1832 | 1 | nonlocal_v4_addr_up = TRUE; |
1833 | 1 | } |
1834 | 1 | } |
1835 | | /* |
1836 | | * Map to local *address* in order to use a single endpt |
1837 | | * even if a local address appears on two or more network |
1838 | | * interfaces. |
1839 | | */ |
1840 | 2 | ep = getinterface(&enumep.sin, INT_WILDCARD); |
1841 | | |
1842 | 2 | if (NULL != ep) { |
1843 | 0 | if (!refresh_interface(ep)) { |
1844 | | /* |
1845 | | * This endpt will be deleted in phase 2 |
1846 | | * because it will not be marked current. |
1847 | | */ |
1848 | 0 | continue; |
1849 | 0 | } |
1850 | 2 | } else { |
1851 | 2 | ep = emalloc(sizeof(*ep)); |
1852 | 2 | memcpy(ep, &enumep, sizeof(*ep)); |
1853 | 2 | if (NULL != newaddrs_tail) { |
1854 | 1 | newaddrs_tail->elink = ep; |
1855 | 1 | newaddrs_tail = ep; |
1856 | 1 | } else { |
1857 | 1 | newaddrs_tail = newaddrs = ep; |
1858 | 1 | } |
1859 | 2 | continue; |
1860 | 2 | } |
1861 | | |
1862 | 0 | if (ep->phase == sys_interphase) { |
1863 | | /* |
1864 | | * The local address is already bound because it is |
1865 | | * configured on more than one network interface. |
1866 | | * Change the endpt name to reflect that. |
1867 | | */ |
1868 | 0 | rename_multi_iface_endpt(ep, &enumep); |
1869 | 0 | } else { |
1870 | | /* |
1871 | | * On a new round we reset the name so the |
1872 | | * interface name shows up again if this address |
1873 | | * is no longer shared. We reset ignore_packets |
1874 | | * from the new prototype to respect any runtime |
1875 | | * changes to the nic AKA listen rules. |
1876 | | */ |
1877 | 0 | strlcpy(ep->name, enumep.name, sizeof(ep->name)); |
1878 | 0 | ep->ignore_packets = enumep.ignore_packets; |
1879 | 0 | } |
1880 | |
|
1881 | 0 | DPRINT_INTERFACE(4, (ep, "updating ", " present\n")); |
1882 | |
|
1883 | 0 | if (ep->ignore_packets != enumep.ignore_packets) { |
1884 | | /* |
1885 | | * We have conflicting configurations for the |
1886 | | * address. This can happen with |
1887 | | * -I <interfacename> on the command line for an |
1888 | | * interface that shares its address with other |
1889 | | * interfaces. We cannot disambiguate incoming |
1890 | | * packets delivered to this socket without extra |
1891 | | * syscalls/features. Note this is an unusual |
1892 | | * configuration where several interfaces share |
1893 | | * an address but filtering via interface name is |
1894 | | * attempted. We resolve the config conflict by |
1895 | | * disabling the processing of received packets. |
1896 | | * This leads to no service on the address where |
1897 | | * the conflict occurs. |
1898 | | */ |
1899 | 0 | msyslog(LOG_WARNING, |
1900 | 0 | "conflicting listen configuration between" |
1901 | 0 | " %s and %s for %s, disabled", |
1902 | 0 | enumep.name, ep->name, stoa(&enumep.sin)); |
1903 | |
|
1904 | 0 | ep->ignore_packets = TRUE; |
1905 | 0 | } |
1906 | | |
1907 | | /* |
1908 | | * found existing and up to date interface - |
1909 | | * mark present. |
1910 | | */ |
1911 | 0 | ep->phase = sys_interphase; |
1912 | |
|
1913 | 0 | ifi.action = IFS_EXISTS; |
1914 | 0 | ifi.ep = ep; |
1915 | 0 | if (receiver != NULL) { |
1916 | 0 | (*receiver)(data, &ifi); |
1917 | 0 | } |
1918 | 0 | } |
1919 | | |
1920 | 1 | isc_interfaceiter_destroy(&iter); |
1921 | | |
1922 | | /* |
1923 | | * Phase 1b |
1924 | | */ |
1925 | 3 | for (ep = newaddrs; ep != NULL; ep = ep->elink) { |
1926 | 2 | if (IS_IPV6(&ep->sin) && is_linklocal(&ep->sin)) { |
1927 | 0 | for (ep2 = newaddrs; ep2 != NULL; ep2 = ep2->elink) { |
1928 | 0 | if ( IS_IPV6(&ep2->sin) |
1929 | 0 | && ep != ep2 |
1930 | 0 | && !is_linklocal(&ep2->sin)) { |
1931 | |
|
1932 | 0 | ep->flags |= INT_LL_OF_GLOB; |
1933 | 0 | break; |
1934 | 0 | } |
1935 | 0 | } |
1936 | 0 | } |
1937 | 2 | } |
1938 | 3 | for (ep2 = newaddrs; ep2 != NULL; ep2 = next_ep) { |
1939 | 2 | next_ep = ep2->elink; |
1940 | 2 | ep2->elink = NULL; |
1941 | 2 | ep = create_interface(port, ep2); |
1942 | 2 | if (ep != NULL) { |
1943 | 2 | if (receiver != NULL) { |
1944 | 0 | ifi.action = IFS_CREATED; |
1945 | 0 | ifi.ep = ep; |
1946 | 0 | (*receiver)(data, &ifi); |
1947 | 0 | } |
1948 | 2 | new_interface_found = TRUE; |
1949 | 2 | DPRINT_INTERFACE(3, |
1950 | 2 | (ep, "updating ", " new - created\n")); |
1951 | 2 | } else { |
1952 | | /* |
1953 | | * The only reason create_interface() returns NULL is |
1954 | | * failure to bind the local address. If there are |
1955 | | * two network interfaces which both use the same |
1956 | | * address, we can be here because we've just bound |
1957 | | * that address earlier in this newaddrs loop. In |
1958 | | * that case the failure to bind is expected and not |
1959 | | * an error, as evidenced by the local address |
1960 | | * already appearing in our list of bound endpoints. |
1961 | | */ |
1962 | 0 | ep = getinterface(&ep2->sin, INT_WILDCARD); |
1963 | 0 | if (NULL != ep) { |
1964 | 0 | rename_multi_iface_endpt(ep2, ep); |
1965 | 0 | } else { |
1966 | 0 | DPRINT_INTERFACE(3, |
1967 | 0 | (ep2, "updating ", " new - FAILED")); |
1968 | 0 | msyslog(LOG_ERR, "unable to listen on %s %s", |
1969 | 0 | ep2->name, sptoa(&ep2->sin)); |
1970 | 0 | } |
1971 | 0 | } |
1972 | | /* we are done with the prototype ep2 either way */ |
1973 | 2 | free(ep2); |
1974 | 2 | } |
1975 | | |
1976 | | /* |
1977 | | * phase 2 - delete gone interfaces - reassigning peers to |
1978 | | * other interfaces |
1979 | | */ |
1980 | 5 | for (ep = ep_list; ep != NULL; ep = next_ep) { |
1981 | 4 | next_ep = ep->elink; |
1982 | | |
1983 | | /* |
1984 | | * if phase does not match sys_phase this interface was |
1985 | | * not enumerated during the last interface scan - so it |
1986 | | * is gone and will be deleted here unless it did not |
1987 | | * originate from interface enumeration (INT_WILDCARD, |
1988 | | * INT_MCASTIF). |
1989 | | */ |
1990 | 4 | if (((INT_WILDCARD | INT_MCASTIF) & ep->flags) || |
1991 | 2 | ep->phase == sys_interphase) |
1992 | 4 | continue; |
1993 | | |
1994 | 0 | DPRINT_INTERFACE(3, (ep, "updating ", |
1995 | 0 | "GONE - deleting\n")); |
1996 | 0 | remove_interface(ep); |
1997 | |
|
1998 | 0 | if (receiver != NULL) { |
1999 | 0 | ifi.action = IFS_DELETED; |
2000 | 0 | ifi.ep = ep; |
2001 | 0 | (*receiver)(data, &ifi); |
2002 | 0 | } |
2003 | | /* disconnect peers from deleted endpt. */ |
2004 | 0 | while (ep->peers != NULL) { |
2005 | 0 | set_peerdstadr(ep->peers, NULL); |
2006 | 0 | } |
2007 | | /* |
2008 | | * update globals in case we lose |
2009 | | * a loopback interface |
2010 | | */ |
2011 | 0 | if (ep == loopback_interface) { |
2012 | 0 | loopback_interface = NULL; |
2013 | 0 | } |
2014 | 0 | delete_interface(ep); |
2015 | 0 | } |
2016 | | |
2017 | | /* |
2018 | | * phase 3 - re-configure as the world has possibly changed |
2019 | | * |
2020 | | * never ever make this conditional again - it is needed to track |
2021 | | * routing updates. see bug #2506 |
2022 | | */ |
2023 | 1 | refresh_all_peerinterfaces(); |
2024 | | |
2025 | 1 | if (sys_bclient) { |
2026 | 0 | io_setbclient(); |
2027 | 0 | } |
2028 | 1 | #ifdef MCAST |
2029 | | /* |
2030 | | * Check multicast interfaces and try to join multicast groups if |
2031 | | * not joined yet. |
2032 | | */ |
2033 | 5 | for (ep = ep_list; ep != NULL; ep = ep->elink) { |
2034 | 4 | remaddr_t *entry; |
2035 | | |
2036 | 4 | if (!(INT_MCASTIF & ep->flags) || (INT_MCASTOPEN & ep->flags)) { |
2037 | 4 | continue; |
2038 | 4 | } |
2039 | | /* Find remote address that was linked to this interface */ |
2040 | 0 | for (entry = remoteaddr_list; |
2041 | 0 | entry != NULL; |
2042 | 0 | entry = entry->link) { |
2043 | 0 | if (entry->ep == ep) { |
2044 | 0 | if (socket_multicast_enable(ep, &entry->addr)) { |
2045 | 0 | msyslog(LOG_INFO, |
2046 | 0 | "Joined %s socket to multicast group %s", |
2047 | 0 | stoa(&ep->sin), |
2048 | 0 | stoa(&entry->addr)); |
2049 | 0 | } |
2050 | 0 | break; |
2051 | 0 | } |
2052 | 0 | } |
2053 | 0 | } |
2054 | 1 | #endif /* MCAST */ |
2055 | | |
2056 | 1 | return new_interface_found; |
2057 | 1 | } |
2058 | | |
2059 | | |
2060 | | /* |
2061 | | * create_sockets - create a socket for each interface plus a default |
2062 | | * socket for when we don't know where to send |
2063 | | */ |
2064 | | static int |
2065 | | create_sockets( |
2066 | | u_short port |
2067 | | ) |
2068 | 1 | { |
2069 | 1 | #ifndef HAVE_IO_COMPLETION_PORT |
2070 | | /* |
2071 | | * I/O Completion Ports don't care about the select and FD_SET |
2072 | | */ |
2073 | 1 | maxactivefd = 0; |
2074 | 1 | FD_ZERO(&activefds); |
2075 | 1 | #endif |
2076 | | |
2077 | 1 | DPRINTF(2, ("create_sockets(%d)\n", port)); |
2078 | | |
2079 | 1 | create_wildcards(port); |
2080 | | |
2081 | 1 | update_interfaces(port, NULL, NULL); |
2082 | | |
2083 | | /* |
2084 | | * Now that we have opened all the sockets, turn off the reuse |
2085 | | * flag for security. |
2086 | | */ |
2087 | 1 | set_reuseaddr(0); |
2088 | | |
2089 | 1 | DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces)); |
2090 | | |
2091 | 1 | return ninterfaces; |
2092 | 1 | } |
2093 | | |
2094 | | /* |
2095 | | * create_interface - create a new interface for a given prototype |
2096 | | * binding the socket. |
2097 | | */ |
2098 | | static endpt * |
2099 | | create_interface( |
2100 | | u_short port, |
2101 | | endpt * protot |
2102 | | ) |
2103 | 2 | { |
2104 | 2 | sockaddr_u resmask; |
2105 | 2 | endpt * iface; |
2106 | 2 | int/*BOOL*/ success; |
2107 | | #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET) |
2108 | | remaddr_t * entry; |
2109 | | remaddr_t * next_entry; |
2110 | | #endif |
2111 | 2 | DPRINTF(2, ("create_interface(%s)\n", sptoa(&protot->sin))); |
2112 | | |
2113 | | /* build an interface */ |
2114 | 2 | iface = new_interface(protot); |
2115 | | |
2116 | | /* |
2117 | | * create socket |
2118 | | */ |
2119 | 2 | iface->fd = open_socket(&iface->sin, 0, 0, iface); |
2120 | | |
2121 | 2 | if (iface->fd != INVALID_SOCKET) |
2122 | 2 | log_listen_address(iface); |
2123 | | |
2124 | 2 | if ((INT_BROADCAST & iface->flags) |
2125 | 1 | && iface->bfd != INVALID_SOCKET) |
2126 | 0 | msyslog(LOG_INFO, "Listening on broadcast address %s", |
2127 | 0 | sptoa(&iface->bcast)); |
2128 | | |
2129 | 2 | if (INVALID_SOCKET == iface->fd |
2130 | 0 | && INVALID_SOCKET == iface->bfd) { |
2131 | 0 | DPRINTF(2, ("unable to create socket on %s (%d) for %s", |
2132 | 0 | iface->name, |
2133 | 0 | iface->ifnum, |
2134 | 0 | sptoa(&iface->sin))); |
2135 | 0 | delete_interface(iface); |
2136 | 0 | return NULL; |
2137 | 0 | } |
2138 | | |
2139 | | /* |
2140 | | * Blacklist our own addresses, no use talking to ourself |
2141 | | */ |
2142 | 2 | SET_HOSTMASK(&resmask, AF(&iface->sin)); |
2143 | 2 | success = hack_restrict(RESTRICT_FLAGS, &iface->sin, &resmask, |
2144 | 2 | -4, RESM_NTPONLY | RESM_INTERFACE, |
2145 | 2 | RES_IGNORE, 0); |
2146 | 2 | if (!success) { |
2147 | 0 | msyslog(LOG_ERR, |
2148 | 0 | "unable to self-restrict %s", stoa(&iface->sin)); |
2149 | 0 | } |
2150 | | |
2151 | | /* |
2152 | | * set globals with the first found |
2153 | | * loopback interface of the appropriate class |
2154 | | */ |
2155 | 2 | if (NULL == loopback_interface && AF_INET == iface->family |
2156 | 1 | && (INT_LOOPBACK & iface->flags)) |
2157 | 1 | loopback_interface = iface; |
2158 | | |
2159 | | /* |
2160 | | * put into our interface list |
2161 | | */ |
2162 | 2 | add_addr_to_list(&iface->sin, iface); |
2163 | 2 | add_interface(iface); |
2164 | | |
2165 | | #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET) |
2166 | | /* |
2167 | | * Join any previously-configured compatible multicast groups. |
2168 | | */ |
2169 | | if (INT_MULTICAST & iface->flags && |
2170 | | !((INT_LOOPBACK | INT_WILDCARD) & iface->flags) && |
2171 | | !iface->ignore_packets) { |
2172 | | for (entry = remoteaddr_list; |
2173 | | entry != NULL; |
2174 | | entry = next_entry) { |
2175 | | next_entry = entry->link; |
2176 | | if (AF(&iface->sin) != AF(&entry->addr) || |
2177 | | !IS_MCAST(&entry->addr)) |
2178 | | continue; |
2179 | | if (socket_multicast_enable(iface, |
2180 | | &entry->addr)) |
2181 | | msyslog(LOG_INFO, |
2182 | | "Joined %s socket to multicast group %s", |
2183 | | stoa(&iface->sin), |
2184 | | stoa(&entry->addr)); |
2185 | | else |
2186 | | msyslog(LOG_ERR, |
2187 | | "Failed to join %s socket to multicast group %s", |
2188 | | stoa(&iface->sin), |
2189 | | stoa(&entry->addr)); |
2190 | | } |
2191 | | } |
2192 | | #endif /* MCAST && MCAST_NONEWSOCKET */ |
2193 | | |
2194 | 2 | DPRINT_INTERFACE(2, (iface, "created ", "\n")); |
2195 | 2 | return iface; |
2196 | 2 | } |
2197 | | |
2198 | | |
2199 | | #ifdef DEBUG |
2200 | | const char * |
2201 | | iflags_str( |
2202 | | u_int32 iflags |
2203 | | ) |
2204 | 0 | { |
2205 | 0 | const size_t sz = LIB_BUFLENGTH; |
2206 | 0 | char * ifs; |
2207 | |
|
2208 | 0 | LIB_GETBUF(ifs); |
2209 | 0 | ifs[0] = '\0'; |
2210 | |
|
2211 | 0 | if (iflags & INT_UP) { |
2212 | 0 | CLEAR_BIT_IF_DEBUG(INT_UP, iflags); |
2213 | 0 | append_flagstr(ifs, sz, "up"); |
2214 | 0 | } |
2215 | |
|
2216 | 0 | if (iflags & INT_PPP) { |
2217 | 0 | CLEAR_BIT_IF_DEBUG(INT_PPP, iflags); |
2218 | 0 | append_flagstr(ifs, sz, "ppp"); |
2219 | 0 | } |
2220 | |
|
2221 | 0 | if (iflags & INT_LOOPBACK) { |
2222 | 0 | CLEAR_BIT_IF_DEBUG(INT_LOOPBACK, iflags); |
2223 | 0 | append_flagstr(ifs, sz, "loopback"); |
2224 | 0 | } |
2225 | |
|
2226 | 0 | if (iflags & INT_BROADCAST) { |
2227 | 0 | CLEAR_BIT_IF_DEBUG(INT_BROADCAST, iflags); |
2228 | 0 | append_flagstr(ifs, sz, "broadcast"); |
2229 | 0 | } |
2230 | |
|
2231 | 0 | if (iflags & INT_MULTICAST) { |
2232 | 0 | CLEAR_BIT_IF_DEBUG(INT_MULTICAST, iflags); |
2233 | 0 | append_flagstr(ifs, sz, "multicast"); |
2234 | 0 | } |
2235 | |
|
2236 | 0 | if (iflags & INT_BCASTOPEN) { |
2237 | 0 | CLEAR_BIT_IF_DEBUG(INT_BCASTOPEN, iflags); |
2238 | 0 | append_flagstr(ifs, sz, "bcastopen"); |
2239 | 0 | } |
2240 | |
|
2241 | 0 | if (iflags & INT_MCASTOPEN) { |
2242 | 0 | CLEAR_BIT_IF_DEBUG(INT_MCASTOPEN, iflags); |
2243 | 0 | append_flagstr(ifs, sz, "mcastopen"); |
2244 | 0 | } |
2245 | |
|
2246 | 0 | if (iflags & INT_WILDCARD) { |
2247 | 0 | CLEAR_BIT_IF_DEBUG(INT_WILDCARD, iflags); |
2248 | 0 | append_flagstr(ifs, sz, "wildcard"); |
2249 | 0 | } |
2250 | |
|
2251 | 0 | if (iflags & INT_MCASTIF) { |
2252 | 0 | CLEAR_BIT_IF_DEBUG(INT_MCASTIF, iflags); |
2253 | 0 | append_flagstr(ifs, sz, "mcastif"); |
2254 | 0 | } |
2255 | |
|
2256 | 0 | if (iflags & INT_PRIVACY) { |
2257 | 0 | CLEAR_BIT_IF_DEBUG(INT_PRIVACY, iflags); |
2258 | 0 | append_flagstr(ifs, sz, "IPv6privacy"); |
2259 | 0 | } |
2260 | |
|
2261 | 0 | if (iflags & INT_BCASTXMIT) { |
2262 | 0 | CLEAR_BIT_IF_DEBUG(INT_BCASTXMIT, iflags); |
2263 | 0 | append_flagstr(ifs, sz, "bcastxmit"); |
2264 | 0 | } |
2265 | |
|
2266 | 0 | if (iflags & INT_LL_OF_GLOB) { |
2267 | 0 | CLEAR_BIT_IF_DEBUG(INT_LL_OF_GLOB, iflags); |
2268 | 0 | append_flagstr(ifs, sz, "linklocal-w-global"); |
2269 | 0 | } |
2270 | |
|
2271 | 0 | DEBUG_INVARIANT(!iflags); |
2272 | | |
2273 | 0 | return ifs; |
2274 | 0 | } |
2275 | | #endif /* DEBUG */ |
2276 | | |
2277 | | |
2278 | | #ifdef SO_EXCLUSIVEADDRUSE |
2279 | | static void |
2280 | | set_excladdruse( |
2281 | | SOCKET fd |
2282 | | ) |
2283 | | { |
2284 | | int one = 1; |
2285 | | int failed; |
2286 | | #ifdef SYS_WINNT |
2287 | | DWORD err; |
2288 | | #endif |
2289 | | |
2290 | | failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, |
2291 | | (void *)&one, sizeof(one)); |
2292 | | |
2293 | | if (!failed) |
2294 | | return; |
2295 | | |
2296 | | #ifdef SYS_WINNT |
2297 | | /* |
2298 | | * Prior to Windows XP setting SO_EXCLUSIVEADDRUSE can fail with |
2299 | | * error WSAINVAL depending on service pack level and whether |
2300 | | * the user account is in the Administrators group. Do not |
2301 | | * complain if it fails that way on versions prior to XP (5.1). |
2302 | | */ |
2303 | | err = GetLastError(); |
2304 | | |
2305 | | if (isc_win32os_versioncheck(5, 1, 0, 0) < 0 /* < 5.1/XP */ |
2306 | | && WSAEINVAL == err) |
2307 | | return; |
2308 | | |
2309 | | SetLastError(err); |
2310 | | #endif |
2311 | | msyslog(LOG_ERR, |
2312 | | "setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m", |
2313 | | (int)fd); |
2314 | | } |
2315 | | #endif /* SO_EXCLUSIVEADDRUSE */ |
2316 | | |
2317 | | |
2318 | | /* |
2319 | | * set_reuseaddr() - set/clear REUSEADDR on all sockets |
2320 | | * NB possible hole - should we be doing this on broadcast |
2321 | | * fd's also? |
2322 | | */ |
2323 | | static void |
2324 | | set_reuseaddr( |
2325 | | int flag |
2326 | | ) |
2327 | 1 | { |
2328 | 1 | #ifndef SO_EXCLUSIVEADDRUSE |
2329 | 1 | endpt *ep; |
2330 | | |
2331 | 5 | for (ep = ep_list; ep != NULL; ep = ep->elink) { |
2332 | 4 | if (ep->flags & INT_WILDCARD) |
2333 | 2 | continue; |
2334 | | |
2335 | | /* |
2336 | | * if ep->fd is INVALID_SOCKET, we might have a adapter |
2337 | | * configured but not present |
2338 | | */ |
2339 | 2 | DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n", |
2340 | 2 | ep->name, stoa(&ep->sin), |
2341 | 2 | flag ? "on" : "off")); |
2342 | | |
2343 | 2 | if (ep->fd != INVALID_SOCKET) { |
2344 | 2 | if (setsockopt(ep->fd, SOL_SOCKET, SO_REUSEADDR, |
2345 | 2 | (void *)&flag, sizeof(flag))) { |
2346 | 0 | msyslog(LOG_ERR, "set_reuseaddr: setsockopt(%s, SO_REUSEADDR, %s) failed: %m", |
2347 | 0 | stoa(&ep->sin), flag ? "on" : "off"); |
2348 | 0 | } |
2349 | 2 | } |
2350 | 2 | } |
2351 | 1 | #endif /* ! SO_EXCLUSIVEADDRUSE */ |
2352 | 1 | } |
2353 | | |
2354 | | /* |
2355 | | * This is just a wrapper around an internal function so we can |
2356 | | * make other changes as necessary later on |
2357 | | */ |
2358 | | void |
2359 | | enable_broadcast( |
2360 | | endpt * iface, |
2361 | | sockaddr_u * baddr |
2362 | | ) |
2363 | 0 | { |
2364 | 0 | #ifdef OPEN_BCAST_SOCKET |
2365 | 0 | socket_broadcast_enable(iface, iface->fd, baddr); |
2366 | 0 | #endif |
2367 | 0 | } |
2368 | | |
2369 | | #ifdef OPEN_BCAST_SOCKET |
2370 | | /* |
2371 | | * Enable a broadcast address to a given socket |
2372 | | * The socket is in the ep_list all we need to do is enable |
2373 | | * broadcasting. It is not this function's job to select the socket |
2374 | | */ |
2375 | | static isc_boolean_t |
2376 | | socket_broadcast_enable( |
2377 | | endpt * iface, |
2378 | | SOCKET fd, |
2379 | | sockaddr_u * baddr |
2380 | | ) |
2381 | 0 | { |
2382 | 0 | #ifdef SO_BROADCAST |
2383 | 0 | int on = 1; |
2384 | |
|
2385 | 0 | if (IS_IPV4(baddr)) { |
2386 | | /* if this interface can support broadcast, set SO_BROADCAST */ |
2387 | 0 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
2388 | 0 | (void *)&on, sizeof(on))) |
2389 | 0 | msyslog(LOG_ERR, |
2390 | 0 | "setsockopt(SO_BROADCAST) enable failure on address %s: %m", |
2391 | 0 | stoa(baddr)); |
2392 | 0 | else |
2393 | 0 | DPRINTF(2, ("Broadcast enabled on socket %d for address %s\n", |
2394 | 0 | fd, stoa(baddr))); |
2395 | 0 | } |
2396 | 0 | iface->flags |= INT_BCASTXMIT; |
2397 | 0 | return ISC_TRUE; |
2398 | | #else |
2399 | | return ISC_FALSE; |
2400 | | #endif /* SO_BROADCAST */ |
2401 | 0 | } |
2402 | | |
2403 | | #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES |
2404 | | /* |
2405 | | * Remove a broadcast address from a given socket |
2406 | | * The socket is in the ep_list all we need to do is disable |
2407 | | * broadcasting. It is not this function's job to select the socket |
2408 | | */ |
2409 | | static isc_boolean_t |
2410 | | socket_broadcast_disable( |
2411 | | endpt * iface, |
2412 | | sockaddr_u * baddr |
2413 | | ) |
2414 | | { |
2415 | | #ifdef SO_BROADCAST |
2416 | | int off = 0; /* This seems to be OK as an int */ |
2417 | | |
2418 | | if (IS_IPV4(baddr) && setsockopt(iface->fd, SOL_SOCKET, |
2419 | | SO_BROADCAST, (void *)&off, sizeof(off))) |
2420 | | msyslog(LOG_ERR, |
2421 | | "setsockopt(SO_BROADCAST) disable failure on address %s: %m", |
2422 | | stoa(baddr)); |
2423 | | |
2424 | | iface->flags &= ~INT_BCASTXMIT; |
2425 | | return ISC_TRUE; |
2426 | | #else |
2427 | | return ISC_FALSE; |
2428 | | #endif /* SO_BROADCAST */ |
2429 | | } |
2430 | | #endif /* OS_MISSES_SPECIFIC_ROUTE_UPDATES */ |
2431 | | |
2432 | | #endif /* OPEN_BCAST_SOCKET */ |
2433 | | |
2434 | | |
2435 | | /* |
2436 | | * Check to see if the address is a multicast address |
2437 | | */ |
2438 | | static isc_boolean_t |
2439 | | addr_ismulticast( |
2440 | | sockaddr_u *maddr |
2441 | | ) |
2442 | 0 | { |
2443 | 0 | isc_boolean_t result; |
2444 | |
|
2445 | | #ifndef INCLUDE_IPV6_MULTICAST_SUPPORT |
2446 | | /* |
2447 | | * If we don't have IPV6 support any IPV6 addr is not multicast |
2448 | | */ |
2449 | | if (IS_IPV6(maddr)) |
2450 | | result = ISC_FALSE; |
2451 | | else |
2452 | | #endif |
2453 | 0 | result = IS_MCAST(maddr); |
2454 | |
|
2455 | 0 | if (!result) |
2456 | 0 | DPRINTF(4, ("address %s is not multicast\n", |
2457 | 0 | stoa(maddr))); |
2458 | |
|
2459 | 0 | return result; |
2460 | 0 | } |
2461 | | |
2462 | | /* |
2463 | | * Multicast servers need to set the appropriate Multicast interface |
2464 | | * socket option in order for it to know which interface to use for |
2465 | | * send the multicast packet. |
2466 | | */ |
2467 | | void |
2468 | | enable_multicast_if( |
2469 | | endpt * iface, |
2470 | | sockaddr_u * maddr |
2471 | | ) |
2472 | 0 | { |
2473 | 0 | #ifdef MCAST |
2474 | 0 | #ifdef IP_MULTICAST_LOOP |
2475 | 0 | TYPEOF_IP_MULTICAST_LOOP off = 0; |
2476 | 0 | #endif |
2477 | 0 | #if defined(INCLUDE_IPV6_MULTICAST_SUPPORT) && defined(IPV6_MULTICAST_LOOP) |
2478 | 0 | u_int off6 = 0; |
2479 | 0 | #endif |
2480 | |
|
2481 | 0 | REQUIRE(AF(maddr) == AF(&iface->sin)); |
2482 | | |
2483 | 0 | switch (AF(&iface->sin)) { |
2484 | | |
2485 | 0 | case AF_INET: |
2486 | 0 | #ifdef IP_MULTICAST_LOOP |
2487 | | /* |
2488 | | * Don't send back to itself, but allow failure to set |
2489 | | */ |
2490 | 0 | if (setsockopt(iface->fd, IPPROTO_IP, |
2491 | 0 | IP_MULTICAST_LOOP, |
2492 | 0 | (void *)&off, |
2493 | 0 | sizeof(off))) { |
2494 | |
|
2495 | 0 | msyslog(LOG_ERR, |
2496 | 0 | "setsockopt IP_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s", |
2497 | 0 | iface->fd, stoa(&iface->sin), |
2498 | 0 | stoa(maddr)); |
2499 | 0 | } |
2500 | 0 | #endif |
2501 | 0 | break; |
2502 | | |
2503 | 0 | case AF_INET6: |
2504 | 0 | #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT |
2505 | 0 | #ifdef IPV6_MULTICAST_LOOP |
2506 | | /* |
2507 | | * Don't send back to itself, but allow failure to set |
2508 | | */ |
2509 | 0 | if (setsockopt(iface->fd, IPPROTO_IPV6, |
2510 | 0 | IPV6_MULTICAST_LOOP, |
2511 | 0 | (void *) &off6, sizeof(off6))) { |
2512 | |
|
2513 | 0 | msyslog(LOG_ERR, |
2514 | 0 | "setsockopt IPV6_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s", |
2515 | 0 | iface->fd, stoa(&iface->sin), |
2516 | 0 | stoa(maddr)); |
2517 | 0 | } |
2518 | 0 | #endif |
2519 | 0 | break; |
2520 | | #else |
2521 | | return; |
2522 | | #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */ |
2523 | 0 | } |
2524 | 0 | return; |
2525 | 0 | #endif |
2526 | 0 | } |
2527 | | |
2528 | | /* |
2529 | | * Add a multicast address to a given socket |
2530 | | * The socket is in the ep_list all we need to do is enable |
2531 | | * multicasting. It is not this function's job to select the socket |
2532 | | */ |
2533 | | #if defined(MCAST) |
2534 | | static isc_boolean_t |
2535 | | socket_multicast_enable( |
2536 | | endpt * iface, |
2537 | | sockaddr_u * maddr |
2538 | | ) |
2539 | 0 | { |
2540 | 0 | struct ip_mreq mreq; |
2541 | 0 | # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT |
2542 | 0 | struct ipv6_mreq mreq6; |
2543 | 0 | # endif |
2544 | 0 | switch (AF(maddr)) { |
2545 | | |
2546 | 0 | case AF_INET: |
2547 | 0 | ZERO(mreq); |
2548 | 0 | mreq.imr_multiaddr = SOCK_ADDR4(maddr); |
2549 | 0 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
2550 | 0 | if (setsockopt(iface->fd, |
2551 | 0 | IPPROTO_IP, |
2552 | 0 | IP_ADD_MEMBERSHIP, |
2553 | 0 | (void *)&mreq, |
2554 | 0 | sizeof(mreq))) { |
2555 | 0 | DPRINTF(2, ( |
2556 | 0 | "setsockopt IP_ADD_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)", |
2557 | 0 | iface->fd, stoa(&iface->sin), |
2558 | 0 | mreq.imr_multiaddr.s_addr, |
2559 | 0 | mreq.imr_interface.s_addr, |
2560 | 0 | stoa(maddr))); |
2561 | 0 | return ISC_FALSE; |
2562 | 0 | } |
2563 | 0 | DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n", |
2564 | 0 | iface->fd, stoa(&iface->sin), |
2565 | 0 | mreq.imr_multiaddr.s_addr, |
2566 | 0 | mreq.imr_interface.s_addr, stoa(maddr))); |
2567 | 0 | break; |
2568 | | |
2569 | 0 | case AF_INET6: |
2570 | 0 | # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT |
2571 | | /* |
2572 | | * Enable reception of multicast packets. |
2573 | | * If the address is link-local we can get the |
2574 | | * interface index from the scope id. Don't do this |
2575 | | * for other types of multicast addresses. For now let |
2576 | | * the kernel figure it out. |
2577 | | */ |
2578 | 0 | ZERO(mreq6); |
2579 | 0 | mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr); |
2580 | 0 | mreq6.ipv6mr_interface = iface->ifindex; |
2581 | |
|
2582 | 0 | if (setsockopt(iface->fd, IPPROTO_IPV6, |
2583 | 0 | IPV6_JOIN_GROUP, (void *)&mreq6, |
2584 | 0 | sizeof(mreq6))) { |
2585 | 0 | DPRINTF(2, ( |
2586 | 0 | "setsockopt IPV6_JOIN_GROUP failed: %m on socket %d, addr %s for interface %u (%s)", |
2587 | 0 | iface->fd, stoa(&iface->sin), |
2588 | 0 | mreq6.ipv6mr_interface, stoa(maddr))); |
2589 | 0 | return ISC_FALSE; |
2590 | 0 | } |
2591 | 0 | DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %u (%s)\n", |
2592 | 0 | iface->fd, stoa(&iface->sin), |
2593 | 0 | mreq6.ipv6mr_interface, stoa(maddr))); |
2594 | | # else |
2595 | | return ISC_FALSE; |
2596 | | # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */ |
2597 | 0 | } |
2598 | 0 | iface->flags |= INT_MCASTOPEN; |
2599 | 0 | iface->num_mcast++; |
2600 | |
|
2601 | 0 | return ISC_TRUE; |
2602 | 0 | } |
2603 | | #endif /* MCAST */ |
2604 | | |
2605 | | |
2606 | | /* |
2607 | | * Remove a multicast address from a given socket |
2608 | | * The socket is in the ep_list all we need to do is disable |
2609 | | * multicasting. It is not this function's job to select the socket |
2610 | | */ |
2611 | | #ifdef MCAST |
2612 | | static isc_boolean_t |
2613 | | socket_multicast_disable( |
2614 | | endpt * iface, |
2615 | | sockaddr_u * maddr |
2616 | | ) |
2617 | 0 | { |
2618 | 0 | # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT |
2619 | 0 | struct ipv6_mreq mreq6; |
2620 | 0 | # endif |
2621 | 0 | struct ip_mreq mreq; |
2622 | |
|
2623 | 0 | if (find_addr_in_list(maddr) == NULL) { |
2624 | 0 | DPRINTF(4, ("socket_multicast_disable(%s): not found\n", |
2625 | 0 | stoa(maddr))); |
2626 | 0 | return ISC_TRUE; |
2627 | 0 | } |
2628 | | |
2629 | 0 | switch (AF(maddr)) { |
2630 | | |
2631 | 0 | case AF_INET: |
2632 | 0 | ZERO(mreq); |
2633 | 0 | mreq.imr_multiaddr = SOCK_ADDR4(maddr); |
2634 | 0 | mreq.imr_interface = SOCK_ADDR4(&iface->sin); |
2635 | 0 | if (setsockopt(iface->fd, IPPROTO_IP, |
2636 | 0 | IP_DROP_MEMBERSHIP, (void *)&mreq, |
2637 | 0 | sizeof(mreq))) { |
2638 | |
|
2639 | 0 | msyslog(LOG_ERR, |
2640 | 0 | "setsockopt IP_DROP_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)", |
2641 | 0 | iface->fd, stoa(&iface->sin), |
2642 | 0 | SRCADR(maddr), SRCADR(&iface->sin), |
2643 | 0 | stoa(maddr)); |
2644 | 0 | return ISC_FALSE; |
2645 | 0 | } |
2646 | 0 | break; |
2647 | 0 | case AF_INET6: |
2648 | 0 | # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT |
2649 | | /* |
2650 | | * Disable reception of multicast packets |
2651 | | * If the address is link-local we can get the |
2652 | | * interface index from the scope id. Don't do this |
2653 | | * for other types of multicast addresses. For now let |
2654 | | * the kernel figure it out. |
2655 | | */ |
2656 | 0 | ZERO(mreq6); |
2657 | 0 | mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr); |
2658 | 0 | mreq6.ipv6mr_interface = iface->ifindex; |
2659 | |
|
2660 | 0 | if (setsockopt(iface->fd, IPPROTO_IPV6, |
2661 | 0 | IPV6_LEAVE_GROUP, (void *)&mreq6, |
2662 | 0 | sizeof(mreq6))) { |
2663 | |
|
2664 | 0 | msyslog(LOG_ERR, |
2665 | 0 | "setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d (%s)", |
2666 | 0 | iface->fd, stoa(&iface->sin), |
2667 | 0 | iface->ifindex, stoa(maddr)); |
2668 | 0 | return ISC_FALSE; |
2669 | 0 | } |
2670 | 0 | break; |
2671 | | # else |
2672 | | return ISC_FALSE; |
2673 | | # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */ |
2674 | 0 | } |
2675 | | |
2676 | 0 | iface->num_mcast--; |
2677 | 0 | if (iface->num_mcast <= 0) { |
2678 | 0 | iface->flags &= ~INT_MCASTOPEN; |
2679 | 0 | } |
2680 | 0 | return ISC_TRUE; |
2681 | 0 | } |
2682 | | #endif /* MCAST */ |
2683 | | |
2684 | | |
2685 | | /* |
2686 | | * io_setbclient - open the broadcast client sockets |
2687 | | */ |
2688 | | void |
2689 | | io_setbclient(void) |
2690 | 0 | { |
2691 | 0 | #ifdef OPEN_BCAST_SOCKET |
2692 | 0 | endpt * ep; |
2693 | 0 | unsigned int nif, ni4; |
2694 | |
|
2695 | 0 | nif = ni4 = 0; |
2696 | 0 | set_reuseaddr(1); |
2697 | |
|
2698 | 0 | for (ep = ep_list; ep != NULL; ep = ep->elink) { |
2699 | | /* count IPv4 interfaces. Needed later to decide |
2700 | | * if we should log an error or not. |
2701 | | */ |
2702 | 0 | if (AF_INET == ep->family) { |
2703 | 0 | ++ni4; |
2704 | 0 | } |
2705 | | |
2706 | 0 | if (ep->flags & (INT_WILDCARD | INT_LOOPBACK)) |
2707 | 0 | continue; |
2708 | | |
2709 | | /* use only allowed addresses */ |
2710 | 0 | if (ep->ignore_packets) |
2711 | 0 | continue; |
2712 | | |
2713 | | /* Need a broadcast-capable interface */ |
2714 | 0 | if (!(ep->flags & INT_BROADCAST)) |
2715 | 0 | continue; |
2716 | | |
2717 | | /* Only IPv4 addresses are valid for broadcast */ |
2718 | 0 | REQUIRE(IS_IPV4(&ep->bcast)); |
2719 | | |
2720 | | /* Do we already have the broadcast address open? */ |
2721 | 0 | if (ep->flags & INT_BCASTOPEN) { |
2722 | | /* |
2723 | | * account for already open interfaces to avoid |
2724 | | * misleading warning below |
2725 | | */ |
2726 | 0 | nif++; |
2727 | 0 | continue; |
2728 | 0 | } |
2729 | | |
2730 | | /* |
2731 | | * Try to open the broadcast address |
2732 | | */ |
2733 | 0 | ep->family = AF_INET; |
2734 | 0 | ep->bfd = open_socket(&ep->bcast, 1, 0, ep); |
2735 | | |
2736 | | /* |
2737 | | * If we succeeded then we use it otherwise enable |
2738 | | * broadcast on the interface address |
2739 | | */ |
2740 | 0 | if (ep->bfd != INVALID_SOCKET) { |
2741 | 0 | nif++; |
2742 | 0 | ep->flags |= INT_BCASTOPEN; |
2743 | 0 | msyslog(LOG_INFO, |
2744 | 0 | "Listen for broadcasts to %s on interface #%d %s", |
2745 | 0 | stoa(&ep->bcast), ep->ifnum, ep->name); |
2746 | 0 | } else switch (errno) { |
2747 | | /* Silently ignore EADDRINUSE as we probably |
2748 | | * opened the socket already for an address in |
2749 | | * the same network */ |
2750 | 0 | case EADDRINUSE: |
2751 | | /* Some systems cannot bind a socket to a broadcast |
2752 | | * address, as that is not a valid host address. */ |
2753 | 0 | case EADDRNOTAVAIL: |
2754 | | # ifdef SYS_WINNT /*TODO: use for other systems, too? */ |
2755 | | /* avoid recurrence here -- if we already have a |
2756 | | * regular socket, it's quite useless to try this |
2757 | | * again. |
2758 | | */ |
2759 | | if (ep->fd != INVALID_SOCKET) { |
2760 | | ep->flags |= INT_BCASTOPEN; |
2761 | | nif++; |
2762 | | } |
2763 | | # endif |
2764 | 0 | break; |
2765 | | |
2766 | 0 | default: |
2767 | 0 | msyslog(LOG_INFO, |
2768 | 0 | "failed to listen for broadcasts to %s on interface #%d %s", |
2769 | 0 | stoa(&ep->bcast), ep->ifnum, ep->name); |
2770 | 0 | break; |
2771 | 0 | } |
2772 | 0 | } |
2773 | 0 | set_reuseaddr(0); |
2774 | 0 | if (nif != 0) { |
2775 | 0 | broadcast_client_enabled = ISC_TRUE; |
2776 | 0 | DPRINTF(1, ("io_setbclient: listening to %d broadcast addresses\n", nif)); |
2777 | 0 | } else { |
2778 | 0 | broadcast_client_enabled = ISC_FALSE; |
2779 | | /* This is expected when having only IPv6 interfaces |
2780 | | * and no IPv4 interfaces at all. We suppress the error |
2781 | | * log in that case... everything else should work! |
2782 | | */ |
2783 | 0 | if (ni4) { |
2784 | 0 | msyslog(LOG_ERR, |
2785 | 0 | "Unable to listen for broadcasts, no broadcast interfaces available"); |
2786 | 0 | } |
2787 | 0 | } |
2788 | | #else |
2789 | | msyslog(LOG_ERR, |
2790 | | "io_setbclient: Broadcast Client disabled by build"); |
2791 | | #endif /* OPEN_BCAST_SOCKET */ |
2792 | 0 | } |
2793 | | |
2794 | | |
2795 | | /* |
2796 | | * io_unsetbclient - close the broadcast client sockets |
2797 | | */ |
2798 | | void |
2799 | | io_unsetbclient(void) |
2800 | 0 | { |
2801 | 0 | endpt *ep; |
2802 | |
|
2803 | 0 | for (ep = ep_list; ep != NULL; ep = ep->elink) { |
2804 | 0 | if (INT_WILDCARD & ep->flags) |
2805 | 0 | continue; |
2806 | 0 | if (!(INT_BCASTOPEN & ep->flags)) |
2807 | 0 | continue; |
2808 | | |
2809 | 0 | if (ep->bfd != INVALID_SOCKET) { |
2810 | | /* destroy broadcast listening socket */ |
2811 | 0 | msyslog(LOG_INFO, |
2812 | 0 | "stop listening for broadcasts to %s on interface #%d %s", |
2813 | 0 | stoa(&ep->bcast), ep->ifnum, ep->name); |
2814 | 0 | close_and_delete_fd_from_list(ep->bfd, ep); |
2815 | 0 | ep->bfd = INVALID_SOCKET; |
2816 | 0 | } |
2817 | 0 | ep->flags &= ~INT_BCASTOPEN; |
2818 | 0 | } |
2819 | 0 | broadcast_client_enabled = ISC_FALSE; |
2820 | 0 | } |
2821 | | |
2822 | | |
2823 | | /* |
2824 | | * io_multicast_add() - add multicast group address |
2825 | | */ |
2826 | | void |
2827 | | io_multicast_add( |
2828 | | sockaddr_u *addr |
2829 | | ) |
2830 | 0 | { |
2831 | 0 | #ifdef MCAST |
2832 | 0 | endpt * ep; |
2833 | 0 | endpt * one_ep; |
2834 | | |
2835 | | /* |
2836 | | * Check to see if this is a multicast address |
2837 | | */ |
2838 | 0 | if (!addr_ismulticast(addr)) |
2839 | 0 | return; |
2840 | | |
2841 | | /* If we already have it we can just return */ |
2842 | 0 | if (NULL != find_flagged_addr_in_list(addr, INT_MCASTOPEN)) { |
2843 | 0 | return; |
2844 | 0 | } |
2845 | | |
2846 | 0 | # ifndef MULTICAST_NONEWSOCKET |
2847 | 0 | ep = new_interface(NULL); |
2848 | | |
2849 | | /* |
2850 | | * Open a new socket for the multicast address |
2851 | | */ |
2852 | 0 | ep->sin = *addr; |
2853 | 0 | SET_PORT(&ep->sin, NTP_PORT); |
2854 | 0 | ep->family = AF(&ep->sin); |
2855 | 0 | AF(&ep->mask) = ep->family; |
2856 | 0 | SET_ONESMASK(&ep->mask); |
2857 | |
|
2858 | 0 | set_reuseaddr(1); |
2859 | 0 | ep->bfd = INVALID_SOCKET; |
2860 | 0 | ep->fd = open_socket(&ep->sin, 0, 0, ep); |
2861 | 0 | if (ep->fd != INVALID_SOCKET) { |
2862 | 0 | ep->ignore_packets = ISC_FALSE; |
2863 | 0 | ep->flags |= INT_MCASTIF; |
2864 | 0 | ep->ifindex = SCOPE(addr); |
2865 | |
|
2866 | 0 | strlcpy(ep->name, "multicast", sizeof(ep->name)); |
2867 | 0 | DPRINT_INTERFACE(2, (ep, "multicast add ", "\n")); |
2868 | 0 | add_interface(ep); |
2869 | 0 | log_listen_address(ep); |
2870 | 0 | } else { |
2871 | | /* bind failed, re-use wildcard interface */ |
2872 | 0 | delete_interface(ep); |
2873 | |
|
2874 | 0 | if (IS_IPV4(addr)) |
2875 | 0 | ep = wildipv4; |
2876 | 0 | else if (IS_IPV6(addr)) |
2877 | 0 | ep = wildipv6; |
2878 | 0 | else |
2879 | 0 | ep = NULL; |
2880 | |
|
2881 | 0 | if (ep != NULL) { |
2882 | | /* HACK ! -- stuff in an address */ |
2883 | | /* because we don't bind addr? DH */ |
2884 | 0 | ep->bcast = *addr; |
2885 | 0 | msyslog(LOG_ERR, |
2886 | 0 | "multicast address %s using wildcard interface #%d %s", |
2887 | 0 | stoa(addr), ep->ifnum, ep->name); |
2888 | 0 | } else { |
2889 | 0 | msyslog(LOG_ERR, |
2890 | 0 | "No multicast socket available to use for address %s", |
2891 | 0 | stoa(addr)); |
2892 | 0 | return; |
2893 | 0 | } |
2894 | 0 | } |
2895 | 0 | { /* in place of the { following for in #else clause */ |
2896 | 0 | one_ep = ep; |
2897 | | # else /* MULTICAST_NONEWSOCKET follows */ |
2898 | | /* |
2899 | | * For the case where we can't use a separate socket (Windows) |
2900 | | * join each applicable endpoint socket to the group address. |
2901 | | */ |
2902 | | if (IS_IPV4(addr)) |
2903 | | one_ep = wildipv4; |
2904 | | else |
2905 | | one_ep = wildipv6; |
2906 | | for (ep = ep_list; ep != NULL; ep = ep->elink) { |
2907 | | if (ep->ignore_packets || AF(&ep->sin) != AF(addr) || |
2908 | | !(INT_MULTICAST & ep->flags) || |
2909 | | (INT_LOOPBACK | INT_WILDCARD) & ep->flags) |
2910 | | continue; |
2911 | | one_ep = ep; |
2912 | | # endif /* MULTICAST_NONEWSOCKET */ |
2913 | 0 | if (socket_multicast_enable(ep, addr)) |
2914 | 0 | msyslog(LOG_INFO, |
2915 | 0 | "Joined %s socket to multicast group %s", |
2916 | 0 | stoa(&ep->sin), |
2917 | 0 | stoa(addr)); |
2918 | 0 | } |
2919 | |
|
2920 | 0 | add_addr_to_list(addr, one_ep); |
2921 | | #else /* !MCAST follows*/ |
2922 | | msyslog(LOG_ERR, |
2923 | | "Can not add multicast address %s: no multicast support", |
2924 | | stoa(addr)); |
2925 | | #endif |
2926 | 0 | return; |
2927 | 0 | } |
2928 | | |
2929 | | |
2930 | | /* |
2931 | | * io_multicast_del() - delete multicast group address |
2932 | | */ |
2933 | | void |
2934 | | io_multicast_del( |
2935 | | sockaddr_u * addr |
2936 | | ) |
2937 | 0 | { |
2938 | 0 | #ifdef MCAST |
2939 | 0 | endpt *iface; |
2940 | | |
2941 | | /* |
2942 | | * Check to see if this is a multicast address |
2943 | | */ |
2944 | 0 | if (!addr_ismulticast(addr)) { |
2945 | 0 | msyslog(LOG_ERR, "invalid multicast address %s", |
2946 | 0 | stoa(addr)); |
2947 | 0 | return; |
2948 | 0 | } |
2949 | | |
2950 | | /* |
2951 | | * Disable reception of multicast packets |
2952 | | */ |
2953 | 0 | while ((iface = find_flagged_addr_in_list(addr, INT_MCASTOPEN)) |
2954 | 0 | != NULL) |
2955 | 0 | socket_multicast_disable(iface, addr); |
2956 | |
|
2957 | 0 | delete_addr_from_list(addr); |
2958 | |
|
2959 | | #else /* not MCAST */ |
2960 | | msyslog(LOG_ERR, |
2961 | | "Can not delete multicast address %s: no multicast support", |
2962 | | stoa(addr)); |
2963 | | #endif /* not MCAST */ |
2964 | 0 | } |
2965 | | |
2966 | | |
2967 | | /* |
2968 | | * open_socket - open a socket, returning the file descriptor |
2969 | | */ |
2970 | | |
2971 | | static SOCKET |
2972 | | open_socket( |
2973 | | sockaddr_u * addr, |
2974 | | int bcast, |
2975 | | int turn_off_reuse, |
2976 | | endpt * interf |
2977 | | ) |
2978 | 4 | { |
2979 | 4 | SOCKET fd; |
2980 | 4 | int errval; |
2981 | | /* |
2982 | | * int is OK for REUSEADR per |
2983 | | * http://www.kohala.com/start/mcast.api.txt |
2984 | | */ |
2985 | 4 | int on = 1; |
2986 | 4 | int off = 0; |
2987 | 4 | #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND |
2988 | 4 | int saved_errno; |
2989 | 4 | #endif |
2990 | | |
2991 | 4 | if (IS_IPV6(addr) && !ipv6_works) |
2992 | 0 | return INVALID_SOCKET; |
2993 | | |
2994 | | /* create a datagram (UDP) socket */ |
2995 | 4 | fd = socket(AF(addr), SOCK_DGRAM, 0); |
2996 | 4 | if (INVALID_SOCKET == fd) { |
2997 | 0 | errval = socket_errno(); |
2998 | 0 | msyslog(LOG_ERR, |
2999 | 0 | "socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m", |
3000 | 0 | IS_IPV6(addr) ? "6" : "", stoa(addr)); |
3001 | |
|
3002 | 0 | if (errval == EPROTONOSUPPORT || |
3003 | 0 | errval == EAFNOSUPPORT || |
3004 | 0 | errval == EPFNOSUPPORT) |
3005 | 0 | return (INVALID_SOCKET); |
3006 | | |
3007 | 0 | msyslog(LOG_ERR, |
3008 | 0 | "unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - exiting", |
3009 | 0 | errno); |
3010 | 0 | exit(1); |
3011 | 0 | } |
3012 | | |
3013 | | #ifdef SYS_WINNT |
3014 | | connection_reset_fix(fd, addr); |
3015 | | #endif |
3016 | | /* |
3017 | | * Fixup the file descriptor for some systems |
3018 | | * See bug #530 for details of the issue. |
3019 | | */ |
3020 | 4 | fd = move_fd(fd); |
3021 | | |
3022 | | /* |
3023 | | * set SO_REUSEADDR since we will be binding the same port |
3024 | | * number on each interface according to turn_off_reuse. |
3025 | | * This is undesirable on Windows versions starting with |
3026 | | * Windows XP (numeric version 5.1). |
3027 | | */ |
3028 | | #ifdef SYS_WINNT |
3029 | | if (isc_win32os_versioncheck(5, 1, 0, 0) < 0) /* before 5.1 */ |
3030 | | #endif |
3031 | 4 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
3032 | 4 | (void *)((turn_off_reuse) |
3033 | 4 | ? &off |
3034 | 4 | : &on), |
3035 | 4 | sizeof(on))) { |
3036 | |
|
3037 | 0 | msyslog(LOG_ERR, |
3038 | 0 | "setsockopt SO_REUSEADDR %s fails for address %s: %m", |
3039 | 0 | (turn_off_reuse) |
3040 | 0 | ? "off" |
3041 | 0 | : "on", |
3042 | 0 | stoa(addr)); |
3043 | 0 | closesocket(fd); |
3044 | 0 | return INVALID_SOCKET; |
3045 | 0 | } |
3046 | | #ifdef SO_EXCLUSIVEADDRUSE |
3047 | | /* |
3048 | | * setting SO_EXCLUSIVEADDRUSE on the wildcard we open |
3049 | | * first will cause more specific binds to fail. |
3050 | | */ |
3051 | | if (!(interf->flags & INT_WILDCARD)) |
3052 | | set_excladdruse(fd); |
3053 | | #endif |
3054 | | |
3055 | | /* |
3056 | | * IPv4 specific options go here |
3057 | | */ |
3058 | 4 | if (IS_IPV4(addr)) { |
3059 | 3 | #if defined(IPPROTO_IP) && defined(IP_TOS) |
3060 | 3 | if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void *)&qos, |
3061 | 3 | sizeof(qos))) |
3062 | 0 | msyslog(LOG_ERR, |
3063 | 0 | "setsockopt IP_TOS (%02x) fails on address %s: %m", |
3064 | 0 | qos, stoa(addr)); |
3065 | 3 | #endif /* IPPROTO_IP && IP_TOS */ |
3066 | 3 | if (bcast) |
3067 | 0 | socket_broadcast_enable(interf, fd, addr); |
3068 | 3 | } |
3069 | | |
3070 | | /* |
3071 | | * IPv6 specific options go here |
3072 | | */ |
3073 | 4 | if (IS_IPV6(addr)) { |
3074 | 1 | #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS) |
3075 | 1 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, (void *)&qos, |
3076 | 1 | sizeof(qos))) |
3077 | 0 | msyslog(LOG_ERR, |
3078 | 0 | "setsockopt IPV6_TCLASS (%02x) fails on address %s: %m", |
3079 | 0 | qos, stoa(addr)); |
3080 | 1 | #endif /* IPPROTO_IPV6 && IPV6_TCLASS */ |
3081 | 1 | #ifdef IPV6_V6ONLY |
3082 | 1 | if (isc_net_probe_ipv6only() == ISC_R_SUCCESS |
3083 | 1 | && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, |
3084 | 1 | (void *)&on, sizeof(on))) |
3085 | 0 | msyslog(LOG_ERR, |
3086 | 0 | "setsockopt IPV6_V6ONLY on fails on address %s: %m", |
3087 | 0 | stoa(addr)); |
3088 | 1 | #endif |
3089 | | #ifdef IPV6_BINDV6ONLY |
3090 | | if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY, |
3091 | | (void *)&on, sizeof(on))) |
3092 | | msyslog(LOG_ERR, |
3093 | | "setsockopt IPV6_BINDV6ONLY on fails on address %s: %m", |
3094 | | stoa(addr)); |
3095 | | #endif |
3096 | 1 | } |
3097 | | |
3098 | 4 | #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND |
3099 | | /* |
3100 | | * some OSes don't allow binding to more specific |
3101 | | * addresses if a wildcard address already bound |
3102 | | * to the port and SO_REUSEADDR is not set |
3103 | | */ |
3104 | 4 | if (!is_wildcard_addr(addr)) { |
3105 | 2 | set_wildcard_reuse(AF(addr), 1); |
3106 | 2 | } |
3107 | 4 | #endif |
3108 | | |
3109 | | /* |
3110 | | * bind the local address. |
3111 | | */ |
3112 | 4 | errval = bind(fd, &addr->sa, SOCKLEN(addr)); |
3113 | | |
3114 | 4 | #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND |
3115 | 4 | if (!is_wildcard_addr(addr)) { |
3116 | 2 | saved_errno = errno; /* save for DPRINTF just below */ |
3117 | 2 | set_wildcard_reuse(AF(addr), 0); |
3118 | 2 | errno = saved_errno; |
3119 | 2 | } |
3120 | 4 | #endif |
3121 | | |
3122 | 4 | if (errval < 0) { |
3123 | | /* |
3124 | | * Don't syslog this, as bind() failing is not always |
3125 | | * unexpected. It can happen for wildcard addresses and |
3126 | | * broadcast addresses (where turn_off_reuse is 0) and |
3127 | | * when attempting to bind an address we've already |
3128 | | * bound due to more than one interface using the same |
3129 | | * local address. Leave it to callers to report errors. |
3130 | | */ |
3131 | 0 | DPRINTF(1,("bind(%d) %s%s flags 0x%x failed: %m", |
3132 | 0 | fd, sptoa(addr), |
3133 | 0 | IS_MCAST(addr) ? " (multicast)" : "", |
3134 | 0 | interf->flags)); |
3135 | 0 | closesocket(fd); |
3136 | |
|
3137 | 0 | return INVALID_SOCKET; |
3138 | 0 | } |
3139 | | |
3140 | | #ifdef HAVE_TIMESTAMP |
3141 | | { |
3142 | | if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, |
3143 | | (void *)&on, sizeof(on))) |
3144 | | msyslog(LOG_DEBUG, |
3145 | | "setsockopt SO_TIMESTAMP on fails on address %s: %m", |
3146 | | stoa(addr)); |
3147 | | else |
3148 | | DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n", |
3149 | | fd, stoa(addr))); |
3150 | | } |
3151 | | #endif |
3152 | 4 | #ifdef HAVE_TIMESTAMPNS |
3153 | 4 | { |
3154 | 4 | if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, |
3155 | 4 | (void *)&on, sizeof(on))) |
3156 | 0 | msyslog(LOG_DEBUG, |
3157 | 0 | "setsockopt SO_TIMESTAMPNS on fails on address %s: %m", |
3158 | 0 | stoa(addr)); |
3159 | 4 | else |
3160 | 4 | DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n", |
3161 | 4 | fd, stoa(addr))); |
3162 | 4 | } |
3163 | 4 | #endif |
3164 | | #ifdef HAVE_BINTIME |
3165 | | { |
3166 | | if (setsockopt(fd, SOL_SOCKET, SO_BINTIME, |
3167 | | (void *)&on, sizeof(on))) |
3168 | | msyslog(LOG_DEBUG, |
3169 | | "setsockopt SO_BINTIME on fails on address %s: %m", |
3170 | | stoa(addr)); |
3171 | | else |
3172 | | DPRINTF(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n", |
3173 | | fd, stoa(addr))); |
3174 | | } |
3175 | | #endif |
3176 | | |
3177 | 4 | DPRINTF(4, ("bind(%d) addr %s, flags 0x%x\n", |
3178 | 4 | fd, sptoa(addr), interf->flags)); |
3179 | | |
3180 | 4 | make_socket_nonblocking(fd); |
3181 | | |
3182 | | #ifdef HAVE_SIGNALED_IO |
3183 | | init_socket_sig(fd); |
3184 | | #endif /* not HAVE_SIGNALED_IO */ |
3185 | | |
3186 | 4 | add_fd_to_list(fd, FD_TYPE_SOCKET); |
3187 | | |
3188 | 4 | #if !defined(SYS_WINNT) && !defined(VMS) |
3189 | 4 | DPRINTF(4, ("flags for fd %d: 0x%x\n", fd, |
3190 | 4 | fcntl(fd, F_GETFL, 0))); |
3191 | 4 | #endif /* SYS_WINNT || VMS */ |
3192 | | |
3193 | | #if defined(HAVE_IO_COMPLETION_PORT) |
3194 | | /* |
3195 | | * Add the socket to the completion port |
3196 | | */ |
3197 | | if (!io_completion_port_add_socket(fd, interf, bcast)) { |
3198 | | msyslog(LOG_ERR, "unable to set up io completion port - EXITING"); |
3199 | | exit(1); |
3200 | | } |
3201 | | #endif |
3202 | 4 | return fd; |
3203 | 4 | } |
3204 | | |
3205 | | |
3206 | | |
3207 | | /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */ |
3208 | | /* |
3209 | | * sendpkt - send a packet to the specified destination from the given endpt |
3210 | | * except for multicast, which may be sent from several addresses. |
3211 | | */ |
3212 | | void |
3213 | | sendpkt( |
3214 | | sockaddr_u * dest, |
3215 | | endpt * ep, |
3216 | | int ttl, |
3217 | | struct pkt * pkt, |
3218 | | int len |
3219 | | ) |
3220 | 0 | { |
3221 | 0 | endpt * src; |
3222 | 0 | int ismcast; |
3223 | 0 | int cc; |
3224 | 0 | int rc; |
3225 | 0 | u_char cttl; |
3226 | 0 | l_fp fp_zero = { { 0 }, 0 }; |
3227 | 0 | l_fp org, rec, xmt; |
3228 | | |
3229 | 0 | ismcast = IS_MCAST(dest); |
3230 | 0 | if (!ismcast) { |
3231 | 0 | src = ep; |
3232 | 0 | } else { |
3233 | | #ifndef MCAST |
3234 | | return; |
3235 | | #endif |
3236 | 0 | src = (IS_IPV4(dest)) |
3237 | 0 | ? mc4_list |
3238 | 0 | : mc6_list; |
3239 | 0 | } |
3240 | |
|
3241 | 0 | if (NULL == src) { |
3242 | | /* |
3243 | | * unbound peer - drop request and wait for better |
3244 | | * network conditions |
3245 | | */ |
3246 | 0 | DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n", |
3247 | 0 | ismcast ? "\tMCAST\t***** " : "", |
3248 | 0 | stoa(dest), ttl, len)); |
3249 | 0 | return; |
3250 | 0 | } |
3251 | | |
3252 | 0 | do { |
3253 | 0 | if (ismcast && INT_LL_OF_GLOB & src->flags) { |
3254 | | /* avoid duplicate multicasts on same IPv6 net */ |
3255 | 0 | goto loop; |
3256 | 0 | } |
3257 | 0 | DPRINTF(2, ("%ssendpkt(%d, dst=%s, src=%s, ttl=%d, len=%d)\n", |
3258 | 0 | ismcast ? "\tMCAST\t***** " : "", src->fd, |
3259 | 0 | stoa(dest), stoa(&src->sin), ttl, len)); |
3260 | 0 | #ifdef MCAST |
3261 | 0 | if (ismcast && ttl > 0 && ttl != src->last_ttl) { |
3262 | | /* |
3263 | | * set the multicast ttl for outgoing packets |
3264 | | */ |
3265 | 0 | switch (AF(&src->sin)) { |
3266 | | |
3267 | 0 | case AF_INET : |
3268 | 0 | cttl = (u_char)ttl; |
3269 | 0 | rc = setsockopt(src->fd, IPPROTO_IP, |
3270 | 0 | IP_MULTICAST_TTL, |
3271 | 0 | (void *)&cttl, |
3272 | 0 | sizeof(cttl)); |
3273 | 0 | break; |
3274 | | |
3275 | 0 | # ifdef INCLUDE_IPV6_SUPPORT |
3276 | 0 | case AF_INET6 : |
3277 | 0 | rc = setsockopt(src->fd, IPPROTO_IPV6, |
3278 | 0 | IPV6_MULTICAST_HOPS, |
3279 | 0 | (void *)&ttl, |
3280 | 0 | sizeof(ttl)); |
3281 | 0 | break; |
3282 | 0 | # endif /* INCLUDE_IPV6_SUPPORT */ |
3283 | | |
3284 | 0 | default: |
3285 | 0 | rc = 0; |
3286 | 0 | } |
3287 | | |
3288 | 0 | if (!rc) |
3289 | 0 | src->last_ttl = ttl; |
3290 | 0 | else |
3291 | 0 | msyslog(LOG_ERR, |
3292 | 0 | "setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m", |
3293 | 0 | stoa(&src->sin)); |
3294 | 0 | } |
3295 | 0 | #endif /* MCAST */ |
3296 | | |
3297 | | #ifdef SIM |
3298 | | cc = simulate_server(dest, src, pkt); |
3299 | | #elif defined(HAVE_IO_COMPLETION_PORT) |
3300 | | cc = io_completion_port_sendto(src, src->fd, pkt, |
3301 | | (size_t)len, dest); |
3302 | | #else |
3303 | 0 | cc = sendto(src->fd, (char *)pkt, (u_int)len, 0, |
3304 | 0 | &dest->sa, SOCKLEN(dest)); |
3305 | 0 | #endif |
3306 | 0 | if (cc == -1) { |
3307 | 0 | src->notsent++; |
3308 | 0 | packets_notsent++; |
3309 | 0 | } else { |
3310 | 0 | src->sent++; |
3311 | 0 | packets_sent++; |
3312 | 0 | } |
3313 | 0 | loop: |
3314 | 0 | if (ismcast) |
3315 | 0 | src = src->mclink; |
3316 | 0 | } while (ismcast && src != NULL); |
3317 | | |
3318 | | /* HMS: pkt->rootdisp is usually random here */ |
3319 | 0 | NTOHL_FP(&pkt->org, &org); |
3320 | 0 | NTOHL_FP(&pkt->rec, &rec); |
3321 | 0 | NTOHL_FP(&pkt->xmt, &xmt); |
3322 | 0 | record_raw_stats(src ? &src->sin : NULL, dest, |
3323 | 0 | &org, &rec, &xmt, &fp_zero, |
3324 | 0 | PKT_LEAP(pkt->li_vn_mode), |
3325 | 0 | PKT_VERSION(pkt->li_vn_mode), |
3326 | 0 | PKT_MODE(pkt->li_vn_mode), |
3327 | 0 | pkt->stratum, |
3328 | 0 | pkt->ppoll, pkt->precision, |
3329 | 0 | FPTOD(NTOHS_FP(pkt->rootdelay)), |
3330 | 0 | FPTOD(NTOHS_FP(pkt->rootdisp)), pkt->refid, |
3331 | 0 | len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten); |
3332 | 0 | } |
3333 | | |
3334 | | |
3335 | | #if !defined(HAVE_IO_COMPLETION_PORT) |
3336 | | #if !defined(HAVE_SIGNALED_IO) |
3337 | | /* |
3338 | | * fdbits - generate ascii representation of fd_set (FAU debug support) |
3339 | | * HFDF format - highest fd first. |
3340 | | */ |
3341 | | static char * |
3342 | | fdbits( |
3343 | | int count, |
3344 | | const fd_set* set |
3345 | | ) |
3346 | 0 | { |
3347 | 0 | static char buffer[256]; |
3348 | 0 | char * buf = buffer; |
3349 | |
|
3350 | 0 | count = min(count, sizeof(buffer) - 1); |
3351 | |
|
3352 | 0 | while (count >= 0) { |
3353 | 0 | *buf++ = FD_ISSET(count, set) ? '#' : '-'; |
3354 | 0 | count--; |
3355 | 0 | } |
3356 | 0 | *buf = '\0'; |
3357 | |
|
3358 | 0 | return buffer; |
3359 | 0 | } |
3360 | | #endif |
3361 | | |
3362 | | #ifdef REFCLOCK |
3363 | | /* |
3364 | | * Routine to read the refclock packets for a specific interface |
3365 | | * Return the number of bytes read. That way we know if we should |
3366 | | * read it again or go on to the next one if no bytes returned |
3367 | | */ |
3368 | | static inline int |
3369 | | read_refclock_packet( |
3370 | | SOCKET fd, |
3371 | | struct refclockio * rp, |
3372 | | l_fp ts |
3373 | | ) |
3374 | 0 | { |
3375 | 0 | u_int read_count; |
3376 | 0 | int buflen; |
3377 | 0 | int saved_errno; |
3378 | 0 | int consumed; |
3379 | 0 | struct recvbuf * rb; |
3380 | |
|
3381 | 0 | rb = get_free_recv_buffer(TRUE); |
3382 | |
|
3383 | 0 | if (NULL == rb) { |
3384 | | /* |
3385 | | * No buffer space available - just drop the 'packet'. |
3386 | | * Since this is a non-blocking character stream we read |
3387 | | * all data that we can. |
3388 | | * |
3389 | | * ...hmmmm... what about "tcflush(fd,TCIFLUSH)" here?!? |
3390 | | */ |
3391 | 0 | char buf[128]; |
3392 | 0 | do |
3393 | 0 | buflen = read(fd, buf, sizeof(buf)); |
3394 | 0 | while (buflen > 0); |
3395 | 0 | packets_dropped++; |
3396 | 0 | return (buflen); |
3397 | 0 | } |
3398 | | |
3399 | | /* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead |
3400 | | * to buffer overrun and memory corruption |
3401 | | */ |
3402 | 0 | if (rp->datalen <= 0 || (size_t)rp->datalen > sizeof(rb->recv_space)) |
3403 | 0 | read_count = sizeof(rb->recv_space); |
3404 | 0 | else |
3405 | 0 | read_count = (u_int)rp->datalen; |
3406 | 0 | do { |
3407 | 0 | buflen = read(fd, (char *)&rb->recv_space, read_count); |
3408 | 0 | } while (buflen < 0 && EINTR == errno); |
3409 | |
|
3410 | 0 | if (buflen <= 0) { |
3411 | 0 | saved_errno = errno; |
3412 | 0 | freerecvbuf(rb); |
3413 | 0 | errno = saved_errno; |
3414 | 0 | return buflen; |
3415 | 0 | } |
3416 | | |
3417 | | /* |
3418 | | * Got one. Mark how and when it got here, |
3419 | | * put it on the full list and do bookkeeping. |
3420 | | */ |
3421 | 0 | rb->recv_length = buflen; |
3422 | 0 | rb->recv_peer = rp->srcclock; |
3423 | 0 | rb->dstadr = NULL; |
3424 | 0 | rb->fd = fd; |
3425 | 0 | rb->recv_time = ts; |
3426 | 0 | rb->receiver = rp->clock_recv; |
3427 | |
|
3428 | 0 | consumed = indicate_refclock_packet(rp, rb); |
3429 | 0 | if (!consumed) { |
3430 | 0 | rp->recvcount++; |
3431 | 0 | packets_received++; |
3432 | 0 | } |
3433 | |
|
3434 | 0 | return buflen; |
3435 | 0 | } |
3436 | | #endif /* REFCLOCK */ |
3437 | | |
3438 | | |
3439 | | #ifdef HAVE_PACKET_TIMESTAMP |
3440 | | /* |
3441 | | * extract timestamps from control message buffer |
3442 | | */ |
3443 | | static l_fp |
3444 | | fetch_timestamp( |
3445 | | struct recvbuf * rb, |
3446 | | struct msghdr * msghdr, |
3447 | | l_fp ts |
3448 | | ) |
3449 | 0 | { |
3450 | 0 | struct cmsghdr * cmsghdr; |
3451 | 0 | unsigned long ticks; |
3452 | 0 | double fuzz; |
3453 | 0 | l_fp lfpfuzz; |
3454 | 0 | l_fp nts; |
3455 | | #ifdef DEBUG_TIMING |
3456 | | l_fp dts; |
3457 | | #endif |
3458 | |
|
3459 | 0 | cmsghdr = CMSG_FIRSTHDR(msghdr); |
3460 | 0 | while (cmsghdr != NULL) { |
3461 | 0 | switch (cmsghdr->cmsg_type) |
3462 | 0 | { |
3463 | | #ifdef HAVE_BINTIME |
3464 | | case SCM_BINTIME: |
3465 | | #endif /* HAVE_BINTIME */ |
3466 | 0 | #ifdef HAVE_TIMESTAMPNS |
3467 | 0 | case SCM_TIMESTAMPNS: |
3468 | 0 | #endif /* HAVE_TIMESTAMPNS */ |
3469 | | #ifdef HAVE_TIMESTAMP |
3470 | | case SCM_TIMESTAMP: |
3471 | | #endif /* HAVE_TIMESTAMP */ |
3472 | 0 | #if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP) |
3473 | 0 | switch (cmsghdr->cmsg_type) |
3474 | 0 | { |
3475 | | #ifdef HAVE_BINTIME |
3476 | | case SCM_BINTIME: |
3477 | | { |
3478 | | struct bintime pbt; |
3479 | | memcpy(&pbt, CMSG_DATA(cmsghdr), sizeof(pbt)); |
3480 | | /* |
3481 | | * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf |
3482 | | */ |
3483 | | nts.l_i = pbt.sec + JAN_1970; |
3484 | | nts.l_uf = (u_int32)(pbt.frac >> 32); |
3485 | | if (sys_tick > measured_tick && |
3486 | | sys_tick > 1e-9) { |
3487 | | ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC)); |
3488 | | nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC)); |
3489 | | } |
3490 | | DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n", |
3491 | | (long)pbt.sec, (u_long)((nts.l_uf / FRAC) * 1e9))); |
3492 | | } |
3493 | | break; |
3494 | | #endif /* HAVE_BINTIME */ |
3495 | 0 | #ifdef HAVE_TIMESTAMPNS |
3496 | 0 | case SCM_TIMESTAMPNS: |
3497 | 0 | { |
3498 | 0 | struct timespec pts; |
3499 | 0 | memcpy(&pts, CMSG_DATA(cmsghdr), sizeof(pts)); |
3500 | 0 | if (sys_tick > measured_tick && |
3501 | 0 | sys_tick > 1e-9) { |
3502 | 0 | ticks = (unsigned long)((pts.tv_nsec * 1e-9) / |
3503 | 0 | sys_tick); |
3504 | 0 | pts.tv_nsec = (long)(ticks * 1e9 * |
3505 | 0 | sys_tick); |
3506 | 0 | } |
3507 | 0 | DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n", |
3508 | 0 | pts.tv_sec, pts.tv_nsec)); |
3509 | 0 | nts = tspec_stamp_to_lfp(pts); |
3510 | 0 | } |
3511 | 0 | break; |
3512 | 0 | #endif /* HAVE_TIMESTAMPNS */ |
3513 | | #ifdef HAVE_TIMESTAMP |
3514 | | case SCM_TIMESTAMP: |
3515 | | { |
3516 | | struct timeval ptv; |
3517 | | memcpy(&ptv, CMSG_DATA(cmsghdr), sizeof(ptv)); |
3518 | | if (sys_tick > measured_tick && |
3519 | | sys_tick > 1e-6) { |
3520 | | ticks = (unsigned long)((ptv.tv_usec * 1e-6) / |
3521 | | sys_tick); |
3522 | | ptv.tv_usec = (long)(ticks * 1e6 * |
3523 | | sys_tick); |
3524 | | } |
3525 | | DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n", |
3526 | | (intmax_t)ptv.tv_sec, (long)ptv.tv_usec)); |
3527 | | nts = tval_stamp_to_lfp(ptv); |
3528 | | } |
3529 | | break; |
3530 | | #endif /* HAVE_TIMESTAMP */ |
3531 | 0 | } |
3532 | 0 | fuzz = ntp_uurandom() * sys_fuzz; |
3533 | 0 | DTOLFP(fuzz, &lfpfuzz); |
3534 | 0 | L_ADD(&nts, &lfpfuzz); |
3535 | | #ifdef DEBUG_TIMING |
3536 | | dts = ts; |
3537 | | L_SUB(&dts, &nts); |
3538 | | collect_timing(rb, "input processing delay", 1, |
3539 | | &dts); |
3540 | | DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n", |
3541 | | lfptoa(&dts, 9))); |
3542 | | #endif /* DEBUG_TIMING */ |
3543 | 0 | ts = nts; /* network time stamp */ |
3544 | 0 | break; |
3545 | 0 | #endif /* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */ |
3546 | | |
3547 | 0 | default: |
3548 | 0 | DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n", |
3549 | 0 | cmsghdr->cmsg_type)); |
3550 | 0 | } |
3551 | 0 | cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr); |
3552 | 0 | } |
3553 | 0 | return ts; |
3554 | 0 | } |
3555 | | #endif /* HAVE_PACKET_TIMESTAMP */ |
3556 | | |
3557 | | |
3558 | | /* |
3559 | | * Routine to read the network NTP packets for a specific interface |
3560 | | * Return the number of bytes read. That way we know if we should |
3561 | | * read it again or go on to the next one if no bytes returned |
3562 | | */ |
3563 | | static inline int |
3564 | | read_network_packet( |
3565 | | SOCKET fd, |
3566 | | endpt * itf, |
3567 | | l_fp ts |
3568 | | ) |
3569 | 0 | { |
3570 | 0 | GETSOCKNAME_SOCKLEN_TYPE fromlen; |
3571 | 0 | int buflen; |
3572 | 0 | register struct recvbuf *rb; |
3573 | 0 | #ifdef HAVE_PACKET_TIMESTAMP |
3574 | 0 | struct msghdr msghdr; |
3575 | 0 | struct iovec iovec; |
3576 | 0 | char control[CMSG_BUFSIZE]; |
3577 | 0 | #endif |
3578 | | |
3579 | | /* |
3580 | | * Get a buffer and read the frame. If we haven't got a buffer, |
3581 | | * or this is received on a disallowed socket, just dump the |
3582 | | * packet. |
3583 | | */ |
3584 | |
|
3585 | 0 | rb = itf->ignore_packets ? NULL : get_free_recv_buffer(FALSE); |
3586 | 0 | if (NULL == rb) { |
3587 | | /* A partial read on a UDP socket truncates the data and |
3588 | | * removes the message from the queue. So there's no |
3589 | | * need to have a full buffer here on the stack. |
3590 | | */ |
3591 | 0 | char buf[16]; |
3592 | 0 | sockaddr_u from; |
3593 | |
|
3594 | 0 | if (rb != NULL) |
3595 | 0 | freerecvbuf(rb); |
3596 | |
|
3597 | 0 | fromlen = sizeof(from); |
3598 | 0 | buflen = recvfrom(fd, buf, sizeof(buf), 0, |
3599 | 0 | &from.sa, &fromlen); |
3600 | 0 | DPRINTF(4, ("%s on (%lu) fd=%d from %s\n", |
3601 | 0 | (itf->ignore_packets) |
3602 | 0 | ? "ignore" |
3603 | 0 | : "drop", |
3604 | 0 | free_recvbuffs(), fd, stoa(&from))); |
3605 | 0 | if (itf->ignore_packets) |
3606 | 0 | packets_ignored++; |
3607 | 0 | else |
3608 | 0 | packets_dropped++; |
3609 | 0 | return (buflen); |
3610 | 0 | } |
3611 | | |
3612 | 0 | fromlen = sizeof(rb->recv_srcadr); |
3613 | |
|
3614 | | #ifndef HAVE_PACKET_TIMESTAMP |
3615 | | rb->recv_length = recvfrom(fd, (char *)&rb->recv_space, |
3616 | | sizeof(rb->recv_space), 0, |
3617 | | &rb->recv_srcadr.sa, &fromlen); |
3618 | | #else |
3619 | 0 | iovec.iov_base = &rb->recv_space; |
3620 | 0 | iovec.iov_len = sizeof(rb->recv_space); |
3621 | 0 | msghdr.msg_name = &rb->recv_srcadr; |
3622 | 0 | msghdr.msg_namelen = fromlen; |
3623 | 0 | msghdr.msg_iov = &iovec; |
3624 | 0 | msghdr.msg_iovlen = 1; |
3625 | 0 | msghdr.msg_control = (void *)&control; |
3626 | 0 | msghdr.msg_controllen = sizeof(control); |
3627 | 0 | msghdr.msg_flags = 0; |
3628 | 0 | rb->recv_length = recvmsg(fd, &msghdr, 0); |
3629 | 0 | #endif |
3630 | |
|
3631 | 0 | buflen = rb->recv_length; |
3632 | |
|
3633 | 0 | if (buflen == 0 || (buflen == -1 && |
3634 | 0 | (EWOULDBLOCK == errno |
3635 | 0 | #ifdef EAGAIN |
3636 | 0 | || EAGAIN == errno |
3637 | 0 | #endif |
3638 | 0 | ))) { |
3639 | 0 | freerecvbuf(rb); |
3640 | 0 | return (buflen); |
3641 | 0 | } else if (buflen < 0) { |
3642 | 0 | msyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m", |
3643 | 0 | stoa(&rb->recv_srcadr), fd); |
3644 | 0 | DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n", |
3645 | 0 | fd)); |
3646 | 0 | freerecvbuf(rb); |
3647 | 0 | return (buflen); |
3648 | 0 | } |
3649 | | |
3650 | 0 | DPRINTF(3, ("read_network_packet: fd=%d length %d from %s\n", |
3651 | 0 | fd, buflen, stoa(&rb->recv_srcadr))); |
3652 | |
|
3653 | 0 | #ifdef ENABLE_BUG3020_FIX |
3654 | 0 | if (ISREFCLOCKADR(&rb->recv_srcadr)) { |
3655 | 0 | msyslog(LOG_ERR, "recvfrom(%s) fd=%d: refclock srcadr on a network interface!", |
3656 | 0 | stoa(&rb->recv_srcadr), fd); |
3657 | 0 | DPRINTF(1, ("read_network_packet: fd=%d dropped (refclock srcadr))\n", |
3658 | 0 | fd)); |
3659 | 0 | packets_dropped++; |
3660 | 0 | freerecvbuf(rb); |
3661 | 0 | return (buflen); |
3662 | 0 | } |
3663 | 0 | #endif |
3664 | | |
3665 | | /* |
3666 | | ** Bug 2672: Some OSes (MacOSX and Linux) don't block spoofed ::1 |
3667 | | */ |
3668 | | |
3669 | 0 | if ( IS_IPV6(&rb->recv_srcadr) |
3670 | 0 | && IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr)) |
3671 | 0 | && !(INT_LOOPBACK & itf->flags)) { |
3672 | |
|
3673 | 0 | packets_dropped++; |
3674 | 0 | DPRINTF(2, ("DROPPING pkt with spoofed ::1 source on %s\n", latoa(itf))); |
3675 | 0 | freerecvbuf(rb); |
3676 | 0 | return -1; |
3677 | 0 | } |
3678 | | |
3679 | | /* |
3680 | | * Got one. Mark how and when it got here, |
3681 | | * put it on the full list and do bookkeeping. |
3682 | | */ |
3683 | 0 | rb->dstadr = itf; |
3684 | 0 | rb->fd = fd; |
3685 | 0 | #ifdef HAVE_PACKET_TIMESTAMP |
3686 | | /* pick up a network time stamp if possible */ |
3687 | 0 | ts = fetch_timestamp(rb, &msghdr, ts); |
3688 | 0 | #endif |
3689 | 0 | rb->recv_time = ts; |
3690 | 0 | rb->receiver = receive; |
3691 | |
|
3692 | 0 | add_full_recv_buffer(rb); |
3693 | |
|
3694 | 0 | itf->received++; |
3695 | 0 | packets_received++; |
3696 | 0 | return (buflen); |
3697 | 0 | } |
3698 | | |
3699 | | /* |
3700 | | * attempt to handle io (select()/signaled IO) |
3701 | | */ |
3702 | | void |
3703 | | io_handler(void) |
3704 | 0 | { |
3705 | 0 | # ifndef HAVE_SIGNALED_IO |
3706 | 0 | fd_set rdfdes; |
3707 | 0 | int nfound; |
3708 | | |
3709 | | /* |
3710 | | * Use select() on all on all input fd's for unlimited |
3711 | | * time. select() will terminate on SIGALARM or on the |
3712 | | * reception of input. Using select() means we can't do |
3713 | | * robust signal handling and we get a potential race |
3714 | | * between checking for alarms and doing the select(). |
3715 | | * Mostly harmless, I think. |
3716 | | */ |
3717 | | /* |
3718 | | * On VMS, I suspect that select() can't be interrupted |
3719 | | * by a "signal" either, so I take the easy way out and |
3720 | | * have select() time out after one second. |
3721 | | * System clock updates really aren't time-critical, |
3722 | | * and - lacking a hardware reference clock - I have |
3723 | | * yet to learn about anything else that is. |
3724 | | */ |
3725 | 0 | ++handler_calls; |
3726 | 0 | rdfdes = activefds; |
3727 | 0 | # if !defined(VMS) && !defined(SYS_VXWORKS) |
3728 | 0 | nfound = select(maxactivefd + 1, &rdfdes, NULL, |
3729 | 0 | NULL, NULL); |
3730 | | # else /* VMS, VxWorks */ |
3731 | | /* make select() wake up after one second */ |
3732 | | { |
3733 | | struct timeval t1; |
3734 | | t1.tv_sec = 1; |
3735 | | t1.tv_usec = 0; |
3736 | | nfound = select(maxactivefd + 1, |
3737 | | &rdfdes, NULL, NULL, |
3738 | | &t1); |
3739 | | } |
3740 | | # endif /* VMS, VxWorks */ |
3741 | 0 | if (nfound < 0 && sanitize_fdset(errno)) { |
3742 | 0 | struct timeval t1; |
3743 | 0 | t1.tv_sec = 0; |
3744 | 0 | t1.tv_usec = 0; |
3745 | 0 | rdfdes = activefds; |
3746 | 0 | nfound = select(maxactivefd + 1, |
3747 | 0 | &rdfdes, NULL, NULL, |
3748 | 0 | &t1); |
3749 | 0 | } |
3750 | |
|
3751 | 0 | if (nfound > 0) { |
3752 | 0 | l_fp ts; |
3753 | |
|
3754 | 0 | get_systime(&ts); |
3755 | |
|
3756 | 0 | input_handler_scan(&ts, &rdfdes); |
3757 | 0 | } else if (nfound == -1 && errno != EINTR) { |
3758 | 0 | msyslog(LOG_ERR, "select() error: %m"); |
3759 | 0 | } |
3760 | | # else /* HAVE_SIGNALED_IO */ |
3761 | | wait_for_signal(); |
3762 | | # endif /* HAVE_SIGNALED_IO */ |
3763 | 0 | } |
3764 | | |
3765 | | #ifdef HAVE_SIGNALED_IO |
3766 | | /* |
3767 | | * input_handler - receive packets asynchronously |
3768 | | * |
3769 | | * ALWAYS IN SIGNAL HANDLER CONTEXT -- only async-safe functions allowed! |
3770 | | */ |
3771 | | static RETSIGTYPE |
3772 | | input_handler( |
3773 | | l_fp * cts |
3774 | | ) |
3775 | | { |
3776 | | int n; |
3777 | | struct timeval tvzero; |
3778 | | fd_set fds; |
3779 | | |
3780 | | ++handler_calls; |
3781 | | |
3782 | | /* |
3783 | | * Do a poll to see who has data |
3784 | | */ |
3785 | | |
3786 | | fds = activefds; |
3787 | | tvzero.tv_sec = tvzero.tv_usec = 0; |
3788 | | |
3789 | | n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero); |
3790 | | if (n < 0 && sanitize_fdset(errno)) { |
3791 | | fds = activefds; |
3792 | | tvzero.tv_sec = tvzero.tv_usec = 0; |
3793 | | n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero); |
3794 | | } |
3795 | | if (n > 0) |
3796 | | input_handler_scan(cts, &fds); |
3797 | | } |
3798 | | #endif /* HAVE_SIGNALED_IO */ |
3799 | | |
3800 | | |
3801 | | /* |
3802 | | * Try to sanitize the global FD set |
3803 | | * |
3804 | | * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise |
3805 | | */ |
3806 | | static int/*BOOL*/ |
3807 | | sanitize_fdset( |
3808 | | int errc |
3809 | | ) |
3810 | 0 | { |
3811 | 0 | int j, b, maxscan; |
3812 | |
|
3813 | 0 | # ifndef HAVE_SIGNALED_IO |
3814 | | /* |
3815 | | * extended FAU debugging output |
3816 | | */ |
3817 | 0 | if (errc != EINTR) { |
3818 | 0 | msyslog(LOG_ERR, |
3819 | 0 | "select(%d, %s, 0L, 0L, &0.0) error: %m", |
3820 | 0 | maxactivefd + 1, |
3821 | 0 | fdbits(maxactivefd, &activefds)); |
3822 | 0 | } |
3823 | 0 | # endif |
3824 | | |
3825 | 0 | if (errc != EBADF) |
3826 | 0 | return FALSE; |
3827 | | |
3828 | | /* if we have oviously bad FDs, try to sanitize the FD set. */ |
3829 | 0 | for (j = 0, maxscan = 0; j <= maxactivefd; j++) { |
3830 | 0 | if (FD_ISSET(j, &activefds)) { |
3831 | 0 | if (-1 != read(j, &b, 0)) { |
3832 | 0 | maxscan = j; |
3833 | 0 | continue; |
3834 | 0 | } |
3835 | 0 | # ifndef HAVE_SIGNALED_IO |
3836 | 0 | msyslog(LOG_ERR, |
3837 | 0 | "Removing bad file descriptor %d from select set", |
3838 | 0 | j); |
3839 | 0 | # endif |
3840 | 0 | FD_CLR(j, &activefds); |
3841 | 0 | } |
3842 | 0 | } |
3843 | 0 | if (maxactivefd != maxscan) |
3844 | 0 | maxactivefd = maxscan; |
3845 | 0 | return TRUE; |
3846 | 0 | } |
3847 | | |
3848 | | /* |
3849 | | * scan the known FDs (clocks, servers, ...) for presence in a 'fd_set'. |
3850 | | * |
3851 | | * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise |
3852 | | */ |
3853 | | static void |
3854 | | input_handler_scan( |
3855 | | const l_fp * cts, |
3856 | | const fd_set * pfds |
3857 | | ) |
3858 | 0 | { |
3859 | 0 | int buflen; |
3860 | 0 | u_int idx; |
3861 | 0 | int doing; |
3862 | 0 | SOCKET fd; |
3863 | 0 | blocking_child *c; |
3864 | 0 | l_fp ts; /* Timestamp at BOselect() gob */ |
3865 | |
|
3866 | | #if defined(DEBUG_TIMING) |
3867 | | l_fp ts_e; /* Timestamp at EOselect() gob */ |
3868 | | #endif |
3869 | 0 | endpt * ep; |
3870 | 0 | #ifdef REFCLOCK |
3871 | 0 | struct refclockio *rp; |
3872 | 0 | int saved_errno; |
3873 | 0 | const char * clk; |
3874 | 0 | #endif |
3875 | 0 | #ifdef HAS_ROUTING_SOCKET |
3876 | 0 | struct asyncio_reader * asyncio_reader; |
3877 | 0 | struct asyncio_reader * next_asyncio_reader; |
3878 | 0 | #endif |
3879 | |
|
3880 | 0 | ++handler_pkts; |
3881 | 0 | ts = *cts; |
3882 | |
|
3883 | 0 | #ifdef REFCLOCK |
3884 | | /* |
3885 | | * Check out the reference clocks first, if any |
3886 | | */ |
3887 | | |
3888 | 0 | for (rp = refio; rp != NULL; rp = rp->next) { |
3889 | 0 | fd = rp->fd; |
3890 | | |
3891 | 0 | if (!FD_ISSET(fd, pfds)) |
3892 | 0 | continue; |
3893 | 0 | buflen = read_refclock_packet(fd, rp, ts); |
3894 | | /* |
3895 | | * The first read must succeed after select() indicates |
3896 | | * readability, or we've reached a permanent EOF. |
3897 | | * http://bugs.ntp.org/1732 reported ntpd munching CPU |
3898 | | * after a USB GPS was unplugged because select was |
3899 | | * indicating EOF but ntpd didn't remove the descriptor |
3900 | | * from the activefds set. |
3901 | | */ |
3902 | 0 | if (buflen < 0 && EAGAIN != errno) { |
3903 | 0 | saved_errno = errno; |
3904 | 0 | clk = refnumtoa(&rp->srcclock->srcadr); |
3905 | 0 | errno = saved_errno; |
3906 | 0 | msyslog(LOG_ERR, "%s read: %m", clk); |
3907 | 0 | maintain_activefds(fd, TRUE); |
3908 | 0 | } else if (0 == buflen) { |
3909 | 0 | clk = refnumtoa(&rp->srcclock->srcadr); |
3910 | 0 | msyslog(LOG_ERR, "%s read EOF", clk); |
3911 | 0 | maintain_activefds(fd, TRUE); |
3912 | 0 | } else { |
3913 | | /* drain any remaining refclock input */ |
3914 | 0 | do { |
3915 | 0 | buflen = read_refclock_packet(fd, rp, ts); |
3916 | 0 | } while (buflen > 0); |
3917 | 0 | } |
3918 | 0 | } |
3919 | 0 | #endif /* REFCLOCK */ |
3920 | | |
3921 | | /* |
3922 | | * Loop through the interfaces looking for data to read. |
3923 | | */ |
3924 | 0 | for (ep = ep_list; ep != NULL; ep = ep->elink) { |
3925 | 0 | for (doing = 0; doing < 2; doing++) { |
3926 | 0 | if (!doing) { |
3927 | 0 | fd = ep->fd; |
3928 | 0 | } else { |
3929 | 0 | if (!(ep->flags & INT_BCASTOPEN)) |
3930 | 0 | break; |
3931 | 0 | fd = ep->bfd; |
3932 | 0 | } |
3933 | 0 | if (fd < 0) |
3934 | 0 | continue; |
3935 | 0 | if (FD_ISSET(fd, pfds)) |
3936 | 0 | do { |
3937 | 0 | buflen = read_network_packet( |
3938 | 0 | fd, ep, ts); |
3939 | 0 | } while (buflen > 0); |
3940 | | /* Check more interfaces */ |
3941 | 0 | } |
3942 | 0 | } |
3943 | |
|
3944 | 0 | #ifdef HAS_ROUTING_SOCKET |
3945 | | /* |
3946 | | * scan list of asyncio readers - currently only used for routing sockets |
3947 | | */ |
3948 | 0 | asyncio_reader = asyncio_reader_list; |
3949 | |
|
3950 | 0 | while (asyncio_reader != NULL) { |
3951 | | /* callback may unlink and free asyncio_reader */ |
3952 | 0 | next_asyncio_reader = asyncio_reader->link; |
3953 | 0 | if (FD_ISSET(asyncio_reader->fd, pfds)) |
3954 | 0 | (*asyncio_reader->receiver)(asyncio_reader); |
3955 | 0 | asyncio_reader = next_asyncio_reader; |
3956 | 0 | } |
3957 | 0 | #endif /* HAS_ROUTING_SOCKET */ |
3958 | | |
3959 | | /* |
3960 | | * Check for a response from a blocking child |
3961 | | */ |
3962 | 0 | for (idx = 0; idx < blocking_children_alloc; idx++) { |
3963 | 0 | c = blocking_children[idx]; |
3964 | 0 | if (NULL == c || -1 == c->resp_read_pipe) |
3965 | 0 | continue; |
3966 | 0 | if (FD_ISSET(c->resp_read_pipe, pfds)) { |
3967 | 0 | ++c->resp_ready_seen; |
3968 | 0 | ++blocking_child_ready_seen; |
3969 | 0 | } |
3970 | 0 | } |
3971 | | |
3972 | | /* We've done our work */ |
3973 | | #if defined(DEBUG_TIMING) |
3974 | | get_systime(&ts_e); |
3975 | | /* |
3976 | | * (ts_e - ts) is the amount of time we spent |
3977 | | * processing this gob of file descriptors. Log |
3978 | | * it. |
3979 | | */ |
3980 | | L_SUB(&ts_e, &ts); |
3981 | | collect_timing(NULL, "input handler", 1, &ts_e); |
3982 | | if (debug > 3) |
3983 | | msyslog(LOG_DEBUG, |
3984 | | "input_handler: Processed a gob of fd's in %s msec", |
3985 | | lfptoms(&ts_e, 6)); |
3986 | | #endif /* DEBUG_TIMING */ |
3987 | 0 | } |
3988 | | #endif /* !HAVE_IO_COMPLETION_PORT */ |
3989 | | |
3990 | | /* |
3991 | | * find an interface suitable for the src address |
3992 | | */ |
3993 | | endpt * |
3994 | | select_peerinterface( |
3995 | | struct peer * peer, |
3996 | | sockaddr_u * srcadr, |
3997 | | endpt * dstadr |
3998 | | ) |
3999 | 0 | { |
4000 | 0 | endpt *ep; |
4001 | 0 | #ifndef SIM |
4002 | 0 | endpt *wild; |
4003 | |
|
4004 | 0 | wild = ANY_INTERFACE_CHOOSE(srcadr); |
4005 | | |
4006 | | /* |
4007 | | * Initialize the peer structure and dance the interface jig. |
4008 | | * Reference clocks step the loopback waltz, the others |
4009 | | * squaredance around the interface list looking for a buddy. If |
4010 | | * the dance peters out, there is always the wildcard interface. |
4011 | | * This might happen in some systems and would preclude proper |
4012 | | * operation with public key cryptography. |
4013 | | */ |
4014 | 0 | if (ISREFCLOCKADR(srcadr)) { |
4015 | 0 | ep = loopback_interface; |
4016 | 0 | } else if (peer->cast_flags & |
4017 | 0 | (MDF_BCLNT | MDF_ACAST | MDF_MCAST | MDF_BCAST)) { |
4018 | 0 | ep = findbcastinter(srcadr); |
4019 | 0 | if (ep != NULL) |
4020 | 0 | DPRINTF(4, ("Found *-cast interface %s for address %s\n", |
4021 | 0 | stoa(&ep->sin), stoa(srcadr))); |
4022 | 0 | else |
4023 | 0 | DPRINTF(4, ("No *-cast local address found for address %s\n", |
4024 | 0 | stoa(srcadr))); |
4025 | 0 | } else { |
4026 | 0 | ep = dstadr; |
4027 | 0 | if (NULL == ep) { |
4028 | 0 | ep = wild; |
4029 | 0 | } |
4030 | 0 | } |
4031 | | /* |
4032 | | * If it is a multicast address, findbcastinter() may not find |
4033 | | * it. For unicast, we get to find the interface when dstadr is |
4034 | | * given to us as the wildcard (ANY_INTERFACE_CHOOSE). Either |
4035 | | * way, try a little harder. |
4036 | | */ |
4037 | 0 | if (wild == ep) { |
4038 | 0 | ep = findinterface(srcadr); |
4039 | 0 | } |
4040 | | /* |
4041 | | * we do not bind to the wildcard interfaces for output |
4042 | | * as our (network) source address would be undefined and |
4043 | | * crypto will not work without knowing the own transmit address |
4044 | | */ |
4045 | 0 | if (ep != NULL && (INT_WILDCARD & ep->flags)) { |
4046 | 0 | if (!accept_wildcard_if_for_winnt) { |
4047 | 0 | ep = NULL; |
4048 | 0 | } |
4049 | 0 | } |
4050 | | #else /* SIM follows */ |
4051 | | ep = loopback_interface; |
4052 | | #endif |
4053 | |
|
4054 | 0 | return ep; |
4055 | 0 | } |
4056 | | |
4057 | | |
4058 | | /* |
4059 | | * findinterface - find local interface corresponding to address |
4060 | | */ |
4061 | | endpt * |
4062 | | findinterface( |
4063 | | sockaddr_u *addr |
4064 | | ) |
4065 | 0 | { |
4066 | 0 | endpt *iface; |
4067 | |
|
4068 | 0 | iface = findlocalinterface(addr, INT_WILDCARD, 0); |
4069 | |
|
4070 | 0 | if (NULL == iface) { |
4071 | 0 | DPRINTF(4, ("Found no interface for address %s - returning wildcard\n", |
4072 | 0 | stoa(addr))); |
4073 | |
|
4074 | 0 | iface = ANY_INTERFACE_CHOOSE(addr); |
4075 | 0 | } else |
4076 | 0 | DPRINTF(4, ("Found interface #%d %s for address %s\n", |
4077 | 0 | iface->ifnum, iface->name, stoa(addr))); |
4078 | |
|
4079 | 0 | return iface; |
4080 | 0 | } |
4081 | | |
4082 | | /* |
4083 | | * findlocalinterface - find local interface corresponding to addr, |
4084 | | * which does not have any of flags set. If bcast is nonzero, addr is |
4085 | | * a broadcast address. |
4086 | | * |
4087 | | * This code attempts to find the local sending address for an outgoing |
4088 | | * address by connecting a new socket to destinationaddress:NTP_PORT |
4089 | | * and reading the sockname of the resulting connect. |
4090 | | * the complicated sequence simulates the routing table lookup |
4091 | | * for to first hop without duplicating any of the routing logic into |
4092 | | * ntpd. preferably we would have used an API call - but its not there - |
4093 | | * so this is the best we can do here short of duplicating to entire routing |
4094 | | * logic in ntpd which would be a silly and really unportable thing to do. |
4095 | | * |
4096 | | */ |
4097 | | static endpt * |
4098 | | findlocalinterface( |
4099 | | sockaddr_u * addr, |
4100 | | int flags, |
4101 | | int bcast |
4102 | | ) |
4103 | 0 | { |
4104 | 0 | GETSOCKNAME_SOCKLEN_TYPE sockaddrlen; |
4105 | 0 | endpt * iface; |
4106 | 0 | sockaddr_u saddr; |
4107 | 0 | SOCKET s; |
4108 | 0 | int rtn; |
4109 | 0 | int on; |
4110 | |
|
4111 | 0 | DPRINTF(4, ("Finding interface for addr %s in list of addresses\n", |
4112 | 0 | stoa(addr))); |
4113 | | |
4114 | | /* [Bug 3437] The prototype POOL peer can be AF_UNSPEC. |
4115 | | * This is bound to fail, but on the way to nowhere it |
4116 | | * triggers a security incident on SELinux. |
4117 | | * |
4118 | | * Checking the condition and failing early is probably good |
4119 | | * advice, and even saves us some syscalls in that case. |
4120 | | * Thanks to Miroslav Lichvar for finding this. |
4121 | | */ |
4122 | 0 | if (AF_UNSPEC == AF(addr)) { |
4123 | 0 | return NULL; |
4124 | 0 | } |
4125 | 0 | s = socket(AF(addr), SOCK_DGRAM, 0); |
4126 | 0 | if (INVALID_SOCKET == s) { |
4127 | 0 | return NULL; |
4128 | 0 | } |
4129 | | /* |
4130 | | * If we are looking for broadcast interface we need to set this |
4131 | | * socket to allow broadcast |
4132 | | */ |
4133 | 0 | if (bcast) { |
4134 | 0 | on = 1; |
4135 | 0 | if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET, |
4136 | 0 | SO_BROADCAST, |
4137 | 0 | (void *)&on, |
4138 | 0 | sizeof(on))) { |
4139 | 0 | closesocket(s); |
4140 | 0 | return NULL; |
4141 | 0 | } |
4142 | 0 | } |
4143 | | |
4144 | 0 | rtn = connect(s, &addr->sa, SOCKLEN(addr)); |
4145 | 0 | if (SOCKET_ERROR == rtn) { |
4146 | 0 | closesocket(s); |
4147 | 0 | return NULL; |
4148 | 0 | } |
4149 | | |
4150 | 0 | sockaddrlen = sizeof(saddr); |
4151 | 0 | rtn = getsockname(s, &saddr.sa, &sockaddrlen); |
4152 | 0 | closesocket(s); |
4153 | 0 | if (SOCKET_ERROR == rtn) |
4154 | 0 | return NULL; |
4155 | | |
4156 | 0 | DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n", |
4157 | 0 | stoa(addr), stoa(&saddr))); |
4158 | |
|
4159 | 0 | iface = getinterface(&saddr, flags); |
4160 | | |
4161 | | /* |
4162 | | * if we didn't find an exact match on saddr, find the closest |
4163 | | * available local address. This handles the case of the |
4164 | | * address suggested by the kernel being excluded by nic rules |
4165 | | * or the user's -I and -L options to ntpd. |
4166 | | * See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683 |
4167 | | * for more background. |
4168 | | */ |
4169 | 0 | if (NULL == iface || iface->ignore_packets) { |
4170 | 0 | iface = findclosestinterface(&saddr, |
4171 | 0 | flags | INT_LOOPBACK); |
4172 | 0 | } |
4173 | | /* |
4174 | | * Don't select an interface which will ignore replies, or one |
4175 | | * dedicated to multicast receive. |
4176 | | */ |
4177 | 0 | if ( iface != NULL |
4178 | 0 | && (iface->ignore_packets || (INT_MCASTIF & iface->flags))) { |
4179 | 0 | iface = NULL; |
4180 | 0 | } |
4181 | 0 | return iface; |
4182 | 0 | } |
4183 | | |
4184 | | |
4185 | | /* |
4186 | | * findclosestinterface |
4187 | | * |
4188 | | * If there are -I/--interface or -L/novirtualips command-line options, |
4189 | | * or "nic" or "interface" rules in ntp.conf, findlocalinterface() may |
4190 | | * find the kernel's preferred local address for a given peer address is |
4191 | | * administratively unavailable to ntpd, and punt to this routine's more |
4192 | | * expensive search. |
4193 | | * |
4194 | | * Find the numerically closest local address to the one connect() |
4195 | | * suggested. This matches an address on the same subnet first, as |
4196 | | * needed by Bug 1184, and provides a consistent choice if there are |
4197 | | * multiple feasible local addresses, regardless of the order ntpd |
4198 | | * enumerated them. |
4199 | | */ |
4200 | | endpt * |
4201 | | findclosestinterface( |
4202 | | sockaddr_u * addr, |
4203 | | int flags |
4204 | | ) |
4205 | 0 | { |
4206 | 0 | endpt * ep; |
4207 | 0 | endpt * winner; |
4208 | 0 | sockaddr_u addr_dist; |
4209 | 0 | sockaddr_u min_dist; |
4210 | |
|
4211 | 0 | ZERO_SOCK(&min_dist); |
4212 | 0 | winner = NULL; |
4213 | |
|
4214 | 0 | for (ep = ep_list; ep != NULL; ep = ep->elink) { |
4215 | 0 | if (ep->ignore_packets || |
4216 | 0 | AF(addr) != ep->family || |
4217 | 0 | flags & ep->flags) |
4218 | 0 | continue; |
4219 | | |
4220 | 0 | calc_addr_distance(&addr_dist, addr, &ep->sin); |
4221 | 0 | if (NULL == winner || |
4222 | 0 | -1 == cmp_addr_distance(&addr_dist, &min_dist)) { |
4223 | 0 | min_dist = addr_dist; |
4224 | 0 | winner = ep; |
4225 | 0 | } |
4226 | 0 | } |
4227 | 0 | if (NULL == winner) |
4228 | 0 | DPRINTF(4, ("findclosestinterface(%s) failed\n", |
4229 | 0 | stoa(addr))); |
4230 | 0 | else |
4231 | 0 | DPRINTF(4, ("findclosestinterface(%s) -> %s\n", |
4232 | 0 | stoa(addr), stoa(&winner->sin))); |
4233 | |
|
4234 | 0 | return winner; |
4235 | 0 | } |
4236 | | |
4237 | | |
4238 | | /* |
4239 | | * calc_addr_distance - calculate the distance between two addresses, |
4240 | | * the absolute value of the difference between |
4241 | | * the addresses numerically, stored as an address. |
4242 | | */ |
4243 | | static void |
4244 | | calc_addr_distance( |
4245 | | sockaddr_u * dist, |
4246 | | const sockaddr_u * a1, |
4247 | | const sockaddr_u * a2 |
4248 | | ) |
4249 | 0 | { |
4250 | 0 | u_char * pdist; |
4251 | 0 | const u_char * p1; |
4252 | 0 | const u_char * p2; |
4253 | 0 | size_t cb; |
4254 | 0 | int different; |
4255 | 0 | int a1_greater; |
4256 | 0 | u_int u; |
4257 | |
|
4258 | 0 | REQUIRE(AF(a1) == AF(a2)); |
4259 | | |
4260 | 0 | ZERO_SOCK(dist); |
4261 | 0 | AF(dist) = AF(a1); |
4262 | |
|
4263 | 0 | if (IS_IPV4(a1)) { |
4264 | 0 | pdist = ( u_char *)&NSRCADR(dist); |
4265 | 0 | p1 = (const u_char *)&NSRCADR(a1); |
4266 | 0 | p2 = (const u_char *)&NSRCADR(a2); |
4267 | 0 | } else { |
4268 | 0 | pdist = ( u_char *)&NSRCADR(dist); |
4269 | 0 | p1 = (const u_char *)&NSRCADR(a1); |
4270 | 0 | p2 = (const u_char *)&NSRCADR(a2); |
4271 | 0 | } |
4272 | 0 | cb = SIZEOF_INADDR(AF(dist)); |
4273 | 0 | different = FALSE; |
4274 | 0 | a1_greater = FALSE; |
4275 | 0 | for (u = 0; u < cb; u++) { |
4276 | 0 | if (!different && p1[u] != p2[u]) { |
4277 | 0 | a1_greater = (p1[u] > p2[u]); |
4278 | 0 | different = TRUE; |
4279 | 0 | } |
4280 | 0 | if (a1_greater) { |
4281 | 0 | pdist[u] = p1[u] - p2[u]; |
4282 | 0 | } else { |
4283 | 0 | pdist[u] = p2[u] - p1[u]; |
4284 | 0 | } |
4285 | 0 | } |
4286 | 0 | } |
4287 | | |
4288 | | |
4289 | | /* |
4290 | | * cmp_addr_distance - compare two address distances, returning -1, 0, |
4291 | | * 1 to indicate their relationship. |
4292 | | */ |
4293 | | static int |
4294 | | cmp_addr_distance( |
4295 | | const sockaddr_u * d1, |
4296 | | const sockaddr_u * d2 |
4297 | | ) |
4298 | 0 | { |
4299 | 0 | int i; |
4300 | |
|
4301 | 0 | REQUIRE(AF(d1) == AF(d2)); |
4302 | | |
4303 | 0 | if (IS_IPV4(d1)) { |
4304 | 0 | if (SRCADR(d1) < SRCADR(d2)) |
4305 | 0 | return -1; |
4306 | 0 | else if (SRCADR(d1) == SRCADR(d2)) |
4307 | 0 | return 0; |
4308 | 0 | else |
4309 | 0 | return 1; |
4310 | 0 | } |
4311 | | |
4312 | 0 | for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) { |
4313 | 0 | if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i]) |
4314 | 0 | return -1; |
4315 | 0 | else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i]) |
4316 | 0 | return 1; |
4317 | 0 | } |
4318 | | |
4319 | 0 | return 0; |
4320 | 0 | } |
4321 | | |
4322 | | |
4323 | | |
4324 | | /* |
4325 | | * fetch an interface structure the matches the |
4326 | | * address and has the given flags NOT set |
4327 | | */ |
4328 | | endpt * |
4329 | | getinterface( |
4330 | | sockaddr_u * addr, |
4331 | | u_int32 flags |
4332 | | ) |
4333 | 2 | { |
4334 | 2 | endpt *iface; |
4335 | | |
4336 | 2 | iface = find_addr_in_list(addr); |
4337 | | |
4338 | 2 | if (iface != NULL && (iface->flags & flags)) |
4339 | 0 | iface = NULL; |
4340 | | |
4341 | 2 | return iface; |
4342 | 2 | } |
4343 | | |
4344 | | |
4345 | | /* |
4346 | | * findbcastinter - find broadcast interface corresponding to address |
4347 | | */ |
4348 | | endpt * |
4349 | | findbcastinter( |
4350 | | sockaddr_u *addr |
4351 | | ) |
4352 | 0 | { |
4353 | 0 | endpt * iface; |
4354 | |
|
4355 | 0 | iface = NULL; |
4356 | 0 | #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT)) |
4357 | 0 | DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n", |
4358 | 0 | stoa(addr))); |
4359 | |
|
4360 | 0 | iface = findlocalinterface(addr, INT_LOOPBACK | INT_WILDCARD, |
4361 | 0 | 1); |
4362 | 0 | if (iface != NULL) { |
4363 | 0 | DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n", |
4364 | 0 | iface->ifnum, iface->name)); |
4365 | 0 | return iface; |
4366 | 0 | } |
4367 | | |
4368 | | /* |
4369 | | * plan B - try to find something reasonable in our lists in |
4370 | | * case kernel lookup doesn't help |
4371 | | */ |
4372 | 0 | for (iface = ep_list; iface != NULL; iface = iface->elink) { |
4373 | 0 | if (iface->flags & INT_WILDCARD) |
4374 | 0 | continue; |
4375 | | |
4376 | | /* Don't bother with ignored interfaces */ |
4377 | 0 | if (iface->ignore_packets) |
4378 | 0 | continue; |
4379 | | |
4380 | | /* |
4381 | | * First look if this is the correct family |
4382 | | */ |
4383 | 0 | if(AF(&iface->sin) != AF(addr)) |
4384 | 0 | continue; |
4385 | | |
4386 | | /* Skip the loopback addresses */ |
4387 | 0 | if (iface->flags & INT_LOOPBACK) |
4388 | 0 | continue; |
4389 | | |
4390 | | /* |
4391 | | * If we are looking to match a multicast address and |
4392 | | * this interface is one... |
4393 | | */ |
4394 | 0 | if (addr_ismulticast(addr) |
4395 | 0 | && (iface->flags & INT_MULTICAST)) { |
4396 | 0 | #ifdef INCLUDE_IPV6_SUPPORT |
4397 | | /* |
4398 | | * ...it is the winner unless we're looking for |
4399 | | * an interface to use for link-local multicast |
4400 | | * and its address is not link-local. |
4401 | | */ |
4402 | 0 | if (IS_IPV6(addr) |
4403 | 0 | && IN6_IS_ADDR_MC_LINKLOCAL(PSOCK_ADDR6(addr)) |
4404 | 0 | && !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin))) |
4405 | 0 | continue; |
4406 | 0 | #endif |
4407 | 0 | break; |
4408 | 0 | } |
4409 | | |
4410 | | /* |
4411 | | * We match only those interfaces marked as |
4412 | | * broadcastable and either the explicit broadcast |
4413 | | * address or the network portion of the IP address. |
4414 | | * Sloppy. |
4415 | | */ |
4416 | 0 | if (IS_IPV4(addr)) { |
4417 | 0 | if (SOCK_EQ(&iface->bcast, addr)) |
4418 | 0 | break; |
4419 | | |
4420 | 0 | if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask)) |
4421 | 0 | == (NSRCADR(addr) & NSRCADR(&iface->mask))) |
4422 | 0 | break; |
4423 | 0 | } |
4424 | 0 | #ifdef INCLUDE_IPV6_SUPPORT |
4425 | 0 | else if (IS_IPV6(addr)) { |
4426 | 0 | if (SOCK_EQ(&iface->bcast, addr)) |
4427 | 0 | break; |
4428 | | |
4429 | 0 | if (SOCK_EQ(netof(&iface->sin), netof(addr))) |
4430 | 0 | break; |
4431 | 0 | } |
4432 | 0 | #endif |
4433 | 0 | } |
4434 | 0 | #endif /* SIOCGIFCONF */ |
4435 | 0 | if (NULL == iface) { |
4436 | 0 | DPRINTF(4, ("No bcast interface found for %s\n", |
4437 | 0 | stoa(addr))); |
4438 | 0 | iface = ANY_INTERFACE_CHOOSE(addr); |
4439 | 0 | } else { |
4440 | 0 | DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n", |
4441 | 0 | iface->ifnum, iface->name)); |
4442 | 0 | } |
4443 | |
|
4444 | 0 | return iface; |
4445 | 0 | } |
4446 | | |
4447 | | |
4448 | | /* |
4449 | | * io_clr_stats - clear I/O module statistics |
4450 | | */ |
4451 | | void |
4452 | | io_clr_stats(void) |
4453 | 0 | { |
4454 | 0 | packets_dropped = 0; |
4455 | 0 | packets_ignored = 0; |
4456 | 0 | packets_received = 0; |
4457 | 0 | packets_sent = 0; |
4458 | 0 | packets_notsent = 0; |
4459 | |
|
4460 | 0 | handler_calls = 0; |
4461 | 0 | handler_pkts = 0; |
4462 | 0 | io_timereset = current_time; |
4463 | 0 | } |
4464 | | |
4465 | | |
4466 | | #ifdef REFCLOCK |
4467 | | /* |
4468 | | * io_addclock - add a reference clock to the list and arrange that we |
4469 | | * get SIGIO interrupts from it. |
4470 | | */ |
4471 | | int |
4472 | | io_addclock( |
4473 | | struct refclockio *rio |
4474 | | ) |
4475 | 0 | { |
4476 | 0 | BLOCKIO(); |
4477 | | |
4478 | | /* |
4479 | | * Stuff the I/O structure in the list and mark the descriptor |
4480 | | * in use. There is a harmless (I hope) race condition here. |
4481 | | */ |
4482 | 0 | rio->active = TRUE; |
4483 | |
|
4484 | | # ifdef HAVE_SIGNALED_IO |
4485 | | if (init_clock_sig(rio)) { |
4486 | | UNBLOCKIO(); |
4487 | | return 0; |
4488 | | } |
4489 | | # elif defined(HAVE_IO_COMPLETION_PORT) |
4490 | | if (!io_completion_port_add_clock_io(rio)) { |
4491 | | UNBLOCKIO(); |
4492 | | return 0; |
4493 | | } |
4494 | | # endif |
4495 | | |
4496 | | /* |
4497 | | * enqueue |
4498 | | */ |
4499 | 0 | LINK_SLIST(refio, rio, next); |
4500 | | |
4501 | | /* |
4502 | | * register fd |
4503 | | */ |
4504 | 0 | add_fd_to_list(rio->fd, FD_TYPE_FILE); |
4505 | |
|
4506 | 0 | UNBLOCKIO(); |
4507 | 0 | return 1; |
4508 | 0 | } |
4509 | | |
4510 | | |
4511 | | /* |
4512 | | * io_closeclock - close the clock in the I/O structure given |
4513 | | */ |
4514 | | void |
4515 | | io_closeclock( |
4516 | | struct refclockio *rio |
4517 | | ) |
4518 | 0 | { |
4519 | 0 | struct refclockio *unlinked; |
4520 | |
|
4521 | 0 | BLOCKIO(); |
4522 | | |
4523 | | /* |
4524 | | * Remove structure from the list |
4525 | | */ |
4526 | 0 | rio->active = FALSE; |
4527 | 0 | UNLINK_SLIST(unlinked, refio, rio, next, struct refclockio); |
4528 | 0 | if (NULL != unlinked) { |
4529 | | /* Close the descriptor. The order of operations is |
4530 | | * important here in case of async / overlapped IO: |
4531 | | * only after we have removed the clock from the |
4532 | | * IO completion port we can be sure no further |
4533 | | * input is queued. So... |
4534 | | * - we first disable feeding to the queu by removing |
4535 | | * the clock from the IO engine |
4536 | | * - close the file (which brings down any IO on it) |
4537 | | * - clear the buffer from results for this fd |
4538 | | */ |
4539 | | # ifdef HAVE_IO_COMPLETION_PORT |
4540 | | io_completion_port_remove_clock_io(rio); |
4541 | | # endif |
4542 | 0 | close_and_delete_fd_from_list(rio->fd, NULL); |
4543 | 0 | purge_recv_buffers_for_fd(rio->fd); |
4544 | 0 | rio->fd = -1; |
4545 | 0 | } |
4546 | |
|
4547 | 0 | UNBLOCKIO(); |
4548 | 0 | } |
4549 | | #endif /* REFCLOCK */ |
4550 | | |
4551 | | |
4552 | | /* |
4553 | | * On NT a SOCKET is an unsigned int so we cannot possibly keep it in |
4554 | | * an array. So we use one of the ISC_LIST functions to hold the |
4555 | | * socket value and use that when we want to enumerate it. |
4556 | | * |
4557 | | * This routine is called by the forked intres child process to close |
4558 | | * all open sockets. On Windows there's no need as intres runs in |
4559 | | * the same process as a thread. |
4560 | | */ |
4561 | | #ifndef SYS_WINNT |
4562 | | void |
4563 | | kill_asyncio( |
4564 | | int startfd |
4565 | | ) |
4566 | 0 | { |
4567 | 0 | BLOCKIO(); |
4568 | | |
4569 | | /* |
4570 | | * In the child process we do not maintain activefds and |
4571 | | * maxactivefd. Zeroing maxactivefd disables code which |
4572 | | * maintains it in close_and_delete_fd_from_list(). |
4573 | | */ |
4574 | 0 | maxactivefd = 0; |
4575 | |
|
4576 | 0 | while (fd_list != NULL) |
4577 | 0 | close_and_delete_fd_from_list(fd_list->fd, NULL); |
4578 | |
|
4579 | 0 | UNBLOCKIO(); |
4580 | 0 | } |
4581 | | #endif /* !SYS_WINNT */ |
4582 | | |
4583 | | |
4584 | | /* |
4585 | | * Add and delete functions for the list of input file descriptors |
4586 | | */ |
4587 | | static void |
4588 | | add_fd_to_list( |
4589 | | SOCKET fd, |
4590 | | enum desc_type type |
4591 | | ) |
4592 | 5 | { |
4593 | 5 | vsock_t *lsock = emalloc(sizeof(*lsock)); |
4594 | | |
4595 | 5 | lsock->fd = fd; |
4596 | 5 | lsock->type = type; |
4597 | | |
4598 | 5 | LINK_SLIST(fd_list, lsock, link); |
4599 | 5 | maintain_activefds(fd, 0); |
4600 | 5 | } |
4601 | | |
4602 | | |
4603 | | static void |
4604 | | close_and_delete_fd_from_list( |
4605 | | SOCKET fd, |
4606 | | endpt *ep /* req. if fd is in struct endpt */ |
4607 | | ) |
4608 | 0 | { |
4609 | 0 | vsock_t *lsock; |
4610 | |
|
4611 | 0 | UNLINK_EXPR_SLIST(lsock, fd_list, fd == |
4612 | 0 | UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t); |
4613 | |
|
4614 | 0 | if (NULL == lsock) |
4615 | 0 | return; |
4616 | | |
4617 | 0 | switch (lsock->type) { |
4618 | | |
4619 | 0 | case FD_TYPE_SOCKET: |
4620 | | #ifdef HAVE_IO_COMPLETION_PORT |
4621 | | if (ep != NULL) { |
4622 | | io_completion_port_remove_socket(fd, ep); |
4623 | | } |
4624 | | #endif |
4625 | 0 | closesocket(lsock->fd); |
4626 | 0 | break; |
4627 | | |
4628 | 0 | case FD_TYPE_FILE: |
4629 | 0 | closeserial((int)lsock->fd); |
4630 | 0 | break; |
4631 | | |
4632 | 0 | default: |
4633 | 0 | msyslog(LOG_ERR, |
4634 | 0 | "internal error - illegal descriptor type %d - EXITING", |
4635 | 0 | (int)lsock->type); |
4636 | 0 | exit(1); |
4637 | 0 | } |
4638 | | |
4639 | 0 | free(lsock); |
4640 | | /* |
4641 | | * remove from activefds |
4642 | | */ |
4643 | 0 | maintain_activefds(fd, 1); |
4644 | 0 | } |
4645 | | |
4646 | | |
4647 | | static void |
4648 | | add_addr_to_list( |
4649 | | sockaddr_u * addr, |
4650 | | endpt * ep |
4651 | | ) |
4652 | 4 | { |
4653 | 4 | remaddr_t *laddr; |
4654 | | |
4655 | 4 | #ifdef DEBUG |
4656 | 4 | if (find_addr_in_list(addr) == NULL) { |
4657 | 4 | #endif |
4658 | | /* not there yet - add to list */ |
4659 | 4 | laddr = emalloc(sizeof(*laddr)); |
4660 | 4 | laddr->addr = *addr; |
4661 | 4 | laddr->ep = ep; |
4662 | | |
4663 | 4 | LINK_SLIST(remoteaddr_list, laddr, link); |
4664 | | |
4665 | 4 | DPRINTF(4, ("Added addr %s to list of addresses\n", |
4666 | 4 | stoa(addr))); |
4667 | 4 | #ifdef DEBUG |
4668 | 4 | } else |
4669 | 0 | DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n", |
4670 | 4 | stoa(addr))); |
4671 | 4 | #endif |
4672 | 4 | } |
4673 | | |
4674 | | |
4675 | | static void |
4676 | | delete_addr_from_list( |
4677 | | sockaddr_u *addr |
4678 | | ) |
4679 | 0 | { |
4680 | 0 | remaddr_t *unlinked; |
4681 | |
|
4682 | 0 | UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr, |
4683 | 0 | &(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t); |
4684 | |
|
4685 | 0 | if (unlinked != NULL) { |
4686 | 0 | DPRINTF(4, ("Deleted addr %s from list of addresses\n", |
4687 | 0 | stoa(addr))); |
4688 | 0 | free(unlinked); |
4689 | 0 | } |
4690 | 0 | } |
4691 | | |
4692 | | |
4693 | | static void |
4694 | | delete_interface_from_list( |
4695 | | endpt *iface |
4696 | | ) |
4697 | 0 | { |
4698 | 0 | remaddr_t *unlinked; |
4699 | |
|
4700 | 0 | for (;;) { |
4701 | 0 | UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface == |
4702 | 0 | UNLINK_EXPR_SLIST_CURRENT()->ep, link, |
4703 | 0 | remaddr_t); |
4704 | |
|
4705 | 0 | if (unlinked == NULL) |
4706 | 0 | break; |
4707 | 0 | DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n", |
4708 | 0 | stoa(&unlinked->addr), iface->ifnum, |
4709 | 0 | iface->name)); |
4710 | 0 | free(unlinked); |
4711 | 0 | } |
4712 | 0 | } |
4713 | | |
4714 | | |
4715 | | static endpt * |
4716 | | find_addr_in_list( |
4717 | | sockaddr_u *addr |
4718 | | ) |
4719 | 6 | { |
4720 | 6 | remaddr_t *entry; |
4721 | | |
4722 | 6 | DPRINTF(4, ("Searching for addr %s in list of addresses - ", |
4723 | 6 | stoa(addr))); |
4724 | | |
4725 | 6 | for (entry = remoteaddr_list; |
4726 | 16 | entry != NULL; |
4727 | 10 | entry = entry->link) |
4728 | 10 | if (SOCK_EQ(&entry->addr, addr)) { |
4729 | 0 | DPRINTF(4, ("FOUND\n")); |
4730 | 0 | return entry->ep; |
4731 | 0 | } |
4732 | | |
4733 | 6 | DPRINTF(4, ("NOT FOUND\n")); |
4734 | 6 | return NULL; |
4735 | 6 | } |
4736 | | |
4737 | | |
4738 | | /* |
4739 | | * Find the given address with the all given flags set in the list |
4740 | | */ |
4741 | | static endpt * |
4742 | | find_flagged_addr_in_list( |
4743 | | sockaddr_u * addr, |
4744 | | u_int32 flags |
4745 | | ) |
4746 | 0 | { |
4747 | 0 | remaddr_t *entry; |
4748 | |
|
4749 | 0 | DPRINTF(4, ("Finding addr %s with flags %d in list: ", |
4750 | 0 | stoa(addr), flags)); |
4751 | |
|
4752 | 0 | for (entry = remoteaddr_list; |
4753 | 0 | entry != NULL; |
4754 | 0 | entry = entry->link) |
4755 | | |
4756 | 0 | if (SOCK_EQ(&entry->addr, addr) |
4757 | 0 | && (entry->ep->flags & flags) == flags) { |
4758 | |
|
4759 | 0 | DPRINTF(4, ("FOUND\n")); |
4760 | 0 | return entry->ep; |
4761 | 0 | } |
4762 | | |
4763 | 0 | DPRINTF(4, ("NOT FOUND\n")); |
4764 | 0 | return NULL; |
4765 | 0 | } |
4766 | | |
4767 | | |
4768 | | const char * |
4769 | | localaddrtoa( |
4770 | | endpt *la |
4771 | | ) |
4772 | 0 | { |
4773 | 0 | return (NULL == la) |
4774 | 0 | ? "<null>" |
4775 | 0 | : stoa(&la->sin); |
4776 | 0 | } |
4777 | | |
4778 | | |
4779 | | #ifdef HAS_ROUTING_SOCKET |
4780 | | # ifndef UPDATE_GRACE |
4781 | 0 | # define UPDATE_GRACE 3 /* min. UPDATE_GRACE - 1 seconds before scanning */ |
4782 | | # endif |
4783 | | |
4784 | | static void |
4785 | | process_routing_msgs(struct asyncio_reader *reader) |
4786 | 0 | { |
4787 | 0 | static void * buffer; |
4788 | 0 | static size_t buffsz = 8192; |
4789 | 0 | int cnt, new, msg_type; |
4790 | 0 | socklen_t len; |
4791 | 0 | #ifdef HAVE_RTNETLINK |
4792 | 0 | struct nlmsghdr *nh; |
4793 | | #else |
4794 | | struct rt_msghdr rtm; |
4795 | | char *p; |
4796 | | char *endp; |
4797 | | #endif |
4798 | |
|
4799 | 0 | if (scan_addrs_once) { |
4800 | | /* |
4801 | | * discard ourselves if we are not needed any more |
4802 | | * usually happens when running unprivileged |
4803 | | */ |
4804 | 0 | goto disable; |
4805 | 0 | } |
4806 | | |
4807 | 0 | if (NULL == buffer) { |
4808 | 0 | buffer = emalloc(buffsz); |
4809 | 0 | } |
4810 | |
|
4811 | 0 | cnt = read(reader->fd, buffer, buffsz); |
4812 | |
|
4813 | 0 | if (cnt < 0) { |
4814 | 0 | if (errno == ENOBUFS) { |
4815 | | /* increase socket buffer by 25% */ |
4816 | 0 | len = sizeof cnt; |
4817 | 0 | if (0 > getsockopt(reader->fd, SOL_SOCKET, SO_RCVBUF, &cnt, &len) || |
4818 | 0 | sizeof cnt != len) { |
4819 | 0 | msyslog(LOG_ERR, |
4820 | 0 | "routing getsockopt SO_RCVBUF %u %u: %m - disabling", |
4821 | 0 | (u_int)cnt, (u_int)sizeof cnt); |
4822 | 0 | goto disable; |
4823 | 0 | } |
4824 | 0 | new = cnt + (cnt / 4); |
4825 | 0 | if (0 > setsockopt(reader->fd, SOL_SOCKET, SO_RCVBUF, &new, sizeof new)) { |
4826 | 0 | msyslog(LOG_ERR, |
4827 | 0 | "routing setsockopt SO_RCVBUF %d -> %d: %m - disabling", |
4828 | 0 | cnt, new); |
4829 | 0 | goto disable; |
4830 | 0 | } |
4831 | 0 | } else { |
4832 | 0 | msyslog(LOG_ERR, |
4833 | 0 | "routing socket reports: %m - disabling"); |
4834 | 0 | disable: |
4835 | 0 | remove_asyncio_reader(reader); |
4836 | 0 | delete_asyncio_reader(reader); |
4837 | 0 | return; |
4838 | 0 | } |
4839 | 0 | } |
4840 | | |
4841 | | /* |
4842 | | * process routing message |
4843 | | */ |
4844 | 0 | #ifdef HAVE_RTNETLINK |
4845 | 0 | for (nh = buffer; NLMSG_OK(nh, cnt); nh = NLMSG_NEXT(nh, cnt)) |
4846 | 0 | { |
4847 | 0 | msg_type = nh->nlmsg_type; |
4848 | | #else |
4849 | | for (p = buffer, endp = p + cnt; |
4850 | | (p + sizeof(struct rt_msghdr)) <= endp; |
4851 | | p += rtm.rtm_msglen) |
4852 | | { |
4853 | | memcpy(&rtm, p, sizeof(rtm)); |
4854 | | if (rtm.rtm_version != RTM_VERSION) { |
4855 | | msyslog(LOG_ERR, |
4856 | | "version mismatch (got %d - expected %d) on routing socket - disabling", |
4857 | | rtm.rtm_version, RTM_VERSION); |
4858 | | |
4859 | | remove_asyncio_reader(reader); |
4860 | | delete_asyncio_reader(reader); |
4861 | | return; |
4862 | | } |
4863 | | msg_type = rtm.rtm_type; |
4864 | | #endif /* !HAVE_RTNETLINK */ |
4865 | 0 | switch (msg_type) { |
4866 | 0 | #ifdef RTM_NEWADDR |
4867 | 0 | case RTM_NEWADDR: |
4868 | 0 | #endif |
4869 | 0 | #ifdef RTM_DELADDR |
4870 | 0 | case RTM_DELADDR: |
4871 | 0 | #endif |
4872 | | #ifdef RTM_ADD |
4873 | | case RTM_ADD: |
4874 | | #endif |
4875 | | #ifdef RTM_DELETE |
4876 | | case RTM_DELETE: |
4877 | | #endif |
4878 | | #ifdef RTM_REDIRECT |
4879 | | case RTM_REDIRECT: |
4880 | | #endif |
4881 | | #ifdef RTM_CHANGE |
4882 | | case RTM_CHANGE: |
4883 | | #endif |
4884 | | #ifdef RTM_LOSING |
4885 | | case RTM_LOSING: |
4886 | | #endif |
4887 | | #ifdef RTM_IFINFO |
4888 | | case RTM_IFINFO: |
4889 | | #endif |
4890 | | #ifdef RTM_IFANNOUNCE |
4891 | | case RTM_IFANNOUNCE: |
4892 | | #endif |
4893 | 0 | #ifdef RTM_NEWLINK |
4894 | 0 | case RTM_NEWLINK: |
4895 | 0 | #endif |
4896 | 0 | #ifdef RTM_DELLINK |
4897 | 0 | case RTM_DELLINK: |
4898 | 0 | #endif |
4899 | 0 | #ifdef RTM_NEWROUTE |
4900 | 0 | case RTM_NEWROUTE: |
4901 | 0 | #endif |
4902 | 0 | #ifdef RTM_DELROUTE |
4903 | 0 | case RTM_DELROUTE: |
4904 | 0 | #endif |
4905 | | /* |
4906 | | * we are keen on new and deleted addresses and |
4907 | | * if an interface goes up and down or routing |
4908 | | * changes |
4909 | | */ |
4910 | 0 | DPRINTF(3, ("routing message op = %d: scheduling interface update\n", |
4911 | 0 | msg_type)); |
4912 | 0 | endpt_scan_timer = UPDATE_GRACE + current_time; |
4913 | 0 | break; |
4914 | 0 | #ifdef HAVE_RTNETLINK |
4915 | 0 | case NLMSG_DONE: |
4916 | | /* end of multipart message */ |
4917 | 0 | return; |
4918 | 0 | #endif |
4919 | 0 | default: |
4920 | | /* |
4921 | | * the rest doesn't bother us. |
4922 | | */ |
4923 | 0 | DPRINTF(4, ("routing message op = %d: ignored\n", |
4924 | 0 | msg_type)); |
4925 | 0 | break; |
4926 | 0 | } |
4927 | 0 | } |
4928 | 0 | } |
4929 | | |
4930 | | /* |
4931 | | * set up routing notifications |
4932 | | */ |
4933 | | static void |
4934 | | init_async_notifications(void) |
4935 | 1 | { |
4936 | 1 | struct asyncio_reader *reader; |
4937 | 1 | #ifdef HAVE_RTNETLINK |
4938 | 1 | int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
4939 | 1 | struct sockaddr_nl sa; |
4940 | | #else |
4941 | | int fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC); |
4942 | | #endif |
4943 | 1 | if (fd < 0) { |
4944 | 0 | msyslog(LOG_ERR, |
4945 | 0 | "unable to open routing socket (%m) - using polled interface update"); |
4946 | 0 | return; |
4947 | 0 | } |
4948 | | |
4949 | 1 | fd = move_fd(fd); |
4950 | 1 | #ifdef HAVE_RTNETLINK |
4951 | 1 | ZERO(sa); |
4952 | 1 | sa.nl_family = PF_NETLINK; |
4953 | 1 | sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR |
4954 | 1 | | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE |
4955 | 1 | | RTMGRP_IPV4_MROUTE | RTMGRP_IPV6_ROUTE |
4956 | 1 | | RTMGRP_IPV6_MROUTE; |
4957 | 1 | if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) { |
4958 | 0 | msyslog(LOG_ERR, |
4959 | 0 | "bind failed on routing socket (%m) - using polled interface update"); |
4960 | 0 | return; |
4961 | 0 | } |
4962 | 1 | #endif |
4963 | 1 | make_socket_nonblocking(fd); |
4964 | | #if defined(HAVE_SIGNALED_IO) |
4965 | | init_socket_sig(fd); |
4966 | | #endif /* HAVE_SIGNALED_IO */ |
4967 | | |
4968 | 1 | reader = new_asyncio_reader(); |
4969 | | |
4970 | 1 | reader->fd = fd; |
4971 | 1 | reader->receiver = process_routing_msgs; |
4972 | | |
4973 | 1 | add_asyncio_reader(reader, FD_TYPE_SOCKET); |
4974 | | msyslog(LOG_INFO, |
4975 | 1 | "Listening on routing socket on fd #%d for interface updates", |
4976 | 1 | fd); |
4977 | 1 | } |
4978 | | #else |
4979 | | /* HAS_ROUTING_SOCKET not defined */ |
4980 | | static void |
4981 | | init_async_notifications(void) |
4982 | | { |
4983 | | } |
4984 | | #endif |