PageRenderTime 53ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/ntp/ntpd/ntp_io.c

https://bitbucket.org/freebsd/freebsd-head/
C | 4029 lines | 2744 code | 465 blank | 820 comment | 567 complexity | f6b7746f9e2b27cfdbf19b0d937c3239 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * ntp_io.c - input/output routines for ntpd. The socket-opening code
  3. * was shamelessly stolen from ntpd.
  4. */
  5. #ifdef HAVE_CONFIG_H
  6. # include <config.h>
  7. #endif
  8. #include "ntp_machine.h"
  9. #include "ntpd.h"
  10. #include "ntp_io.h"
  11. #include "iosignal.h"
  12. #include "ntp_refclock.h"
  13. #include "ntp_stdlib.h"
  14. #include "ntp_request.h"
  15. #include "ntp.h"
  16. #include "ntp_unixtime.h"
  17. /* Don't include ISC's version of IPv6 variables and structures */
  18. #define ISC_IPV6_H 1
  19. #include <isc/interfaceiter.h>
  20. #include <isc/list.h>
  21. #include <isc/result.h>
  22. #ifdef SIM
  23. #include "ntpsim.h"
  24. #endif
  25. #include <stdio.h>
  26. #include <signal.h>
  27. #ifdef HAVE_SYS_PARAM_H
  28. # include <sys/param.h>
  29. #endif /* HAVE_SYS_PARAM_H */
  30. #ifdef HAVE_SYS_IOCTL_H
  31. # include <sys/ioctl.h>
  32. #endif
  33. #ifdef HAVE_SYS_SOCKIO_H /* UXPV: SIOC* #defines (Frank Vance <fvance@waii.com>) */
  34. # include <sys/sockio.h>
  35. #endif
  36. #ifdef HAVE_SYS_UIO_H
  37. # include <sys/uio.h>
  38. #endif
  39. /*
  40. * setsockopt does not always have the same arg declaration
  41. * across all platforms. If it's not defined we make it empty
  42. */
  43. #ifndef SETSOCKOPT_ARG_CAST
  44. #define SETSOCKOPT_ARG_CAST
  45. #endif
  46. /*
  47. * Set up some macros to look for IPv6 and IPv6 multicast
  48. */
  49. #if defined(ISC_PLATFORM_HAVEIPV6) && !defined(DISABLE_IPV6)
  50. #define INCLUDE_IPV6_SUPPORT
  51. #if defined(INCLUDE_IPV6_SUPPORT) && defined(IPV6_JOIN_GROUP) && defined(IPV6_LEAVE_GROUP)
  52. #define INCLUDE_IPV6_MULTICAST_SUPPORT
  53. #endif /* IPV6 Multicast Support */
  54. #endif /* IPv6 Support */
  55. #ifdef INCLUDE_IPV6_SUPPORT
  56. #include <netinet/in.h>
  57. #include <net/if_var.h>
  58. #include <netinet/in_var.h>
  59. #endif /* !INCLUDE_IPV6_SUPPORT */
  60. extern int listen_to_virtual_ips;
  61. extern const char *specific_interface;
  62. #if defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP)
  63. #if defined(CMSG_FIRSTHDR)
  64. #define HAVE_TIMESTAMP
  65. #define USE_TIMESTAMP_CMSG
  66. #ifndef TIMESTAMP_CTLMSGBUF_SIZE
  67. #define TIMESTAMP_CTLMSGBUF_SIZE 1536 /* moderate default */
  68. #endif
  69. #else
  70. /* fill in for old/other timestamp interfaces */
  71. #endif
  72. #endif
  73. #if defined(SYS_WINNT)
  74. #include <transmitbuff.h>
  75. #include <isc/win32os.h>
  76. /*
  77. * Define this macro to control the behavior of connection
  78. * resets on UDP sockets. See Microsoft KnowledgeBase Article Q263823
  79. * for details.
  80. * NOTE: This requires that Windows 2000 systems install Service Pack 2
  81. * or later.
  82. */
  83. #ifndef SIO_UDP_CONNRESET
  84. #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
  85. #endif
  86. /*
  87. * Windows C runtime ioctl() can't deal properly with sockets,
  88. * map to ioctlsocket for this source file.
  89. */
  90. #define ioctl(fd, opt, val) ioctlsocket((fd), (opt), (u_long *)(val))
  91. #endif /* SYS_WINNT */
  92. /*
  93. * We do asynchronous input using the SIGIO facility. A number of
  94. * recvbuf buffers are preallocated for input. In the signal
  95. * handler we poll to see which sockets are ready and read the
  96. * packets from them into the recvbuf's along with a time stamp and
  97. * an indication of the source host and the interface it was received
  98. * through. This allows us to get as accurate receive time stamps
  99. * as possible independent of other processing going on.
  100. *
  101. * We watch the number of recvbufs available to the signal handler
  102. * and allocate more when this number drops below the low water
  103. * mark. If the signal handler should run out of buffers in the
  104. * interim it will drop incoming frames, the idea being that it is
  105. * better to drop a packet than to be inaccurate.
  106. */
  107. /*
  108. * Other statistics of possible interest
  109. */
  110. volatile u_long packets_dropped; /* total number of packets dropped on reception */
  111. volatile u_long packets_ignored; /* packets received on wild card interface */
  112. volatile u_long packets_received; /* total number of packets received */
  113. u_long packets_sent; /* total number of packets sent */
  114. u_long packets_notsent; /* total number of packets which couldn't be sent */
  115. volatile u_long handler_calls; /* number of calls to interrupt handler */
  116. volatile u_long handler_pkts; /* number of pkts received by handler */
  117. u_long io_timereset; /* time counters were reset */
  118. /*
  119. * Interface stuff
  120. */
  121. struct interface *any_interface; /* default ipv4 interface */
  122. struct interface *any6_interface; /* default ipv6 interface */
  123. struct interface *loopback_interface; /* loopback ipv4 interface */
  124. int ninterfaces; /* Total number of interfaces */
  125. volatile int disable_dynamic_updates; /* when set to != 0 dynamic updates won't happen */
  126. #ifdef REFCLOCK
  127. /*
  128. * Refclock stuff. We keep a chain of structures with data concerning
  129. * the guys we are doing I/O for.
  130. */
  131. static struct refclockio *refio;
  132. #endif /* REFCLOCK */
  133. /*
  134. * Define what the possible "soft" errors can be. These are non-fatal returns
  135. * of various network related functions, like recv() and so on.
  136. *
  137. * For some reason, BSDI (and perhaps others) will sometimes return <0
  138. * from recv() but will have errno==0. This is broken, but we have to
  139. * work around it here.
  140. */
  141. #define SOFT_ERROR(e) ((e) == EAGAIN || \
  142. (e) == EWOULDBLOCK || \
  143. (e) == EINTR || \
  144. (e) == 0)
  145. /*
  146. * File descriptor masks etc. for call to select
  147. * Not needed for I/O Completion Ports
  148. */
  149. fd_set activefds;
  150. int maxactivefd;
  151. /*
  152. * bit alternating value to detect verified interfaces during an update cycle
  153. */
  154. static u_char sys_interphase = 0;
  155. static struct interface *new_interface P((struct interface *));
  156. static void add_interface P((struct interface *));
  157. static int update_interfaces P((u_short, interface_receiver_t, void *));
  158. static void remove_interface P((struct interface *));
  159. static struct interface *create_interface P((u_short, struct interface *));
  160. static int move_fd P((SOCKET));
  161. /*
  162. * Multicast functions
  163. */
  164. static isc_boolean_t addr_ismulticast P((struct sockaddr_storage *));
  165. /*
  166. * Not all platforms support multicast
  167. */
  168. #ifdef MCAST
  169. static isc_boolean_t socket_multicast_enable P((struct interface *, int, struct sockaddr_storage *));
  170. static isc_boolean_t socket_multicast_disable P((struct interface *, struct sockaddr_storage *));
  171. #endif
  172. #ifdef DEBUG
  173. static void print_interface P((struct interface *, char *, char *));
  174. #define DPRINT_INTERFACE(_LVL_, _ARGS_) do { if (debug >= (_LVL_)) { print_interface _ARGS_; } } while (0)
  175. #else
  176. #define DPRINT_INTERFACE(_LVL_, _ARGS_) do {} while (0)
  177. #endif
  178. typedef struct vsock vsock_t;
  179. enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE };
  180. struct vsock {
  181. SOCKET fd;
  182. enum desc_type type;
  183. ISC_LINK(vsock_t) link;
  184. };
  185. #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
  186. /*
  187. * async notification processing (e. g. routing sockets)
  188. */
  189. /*
  190. * support for receiving data on fd that is not a refclock or a socket
  191. * like e. g. routing sockets
  192. */
  193. struct asyncio_reader {
  194. SOCKET fd; /* fd to be read */
  195. void *data; /* possibly local data */
  196. void (*receiver)(struct asyncio_reader *); /* input handler */
  197. ISC_LINK(struct asyncio_reader) link; /* the list this is being kept in */
  198. };
  199. ISC_LIST(struct asyncio_reader) asyncio_reader_list;
  200. static void delete_asyncio_reader P((struct asyncio_reader *));
  201. static struct asyncio_reader *new_asyncio_reader P((void));
  202. static void add_asyncio_reader P((struct asyncio_reader *, enum desc_type));
  203. static void remove_asyncio_reader P((struct asyncio_reader *));
  204. #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
  205. static void init_async_notifications P((void));
  206. static int create_sockets P((u_short));
  207. static SOCKET open_socket P((struct sockaddr_storage *, int, int, struct interface *));
  208. static char * fdbits P((int, fd_set *));
  209. static void set_reuseaddr P((int));
  210. static isc_boolean_t socket_broadcast_enable P((struct interface *, SOCKET, struct sockaddr_storage *));
  211. static isc_boolean_t socket_broadcast_disable P((struct interface *, struct sockaddr_storage *));
  212. ISC_LIST(vsock_t) fd_list;
  213. typedef struct remaddr remaddr_t;
  214. struct remaddr {
  215. struct sockaddr_storage addr;
  216. struct interface *interface;
  217. ISC_LINK(remaddr_t) link;
  218. };
  219. ISC_LIST(remaddr_t) remoteaddr_list;
  220. ISC_LIST(struct interface) inter_list;
  221. static struct interface *wildipv4 = NULL;
  222. static struct interface *wildipv6 = NULL;
  223. static void add_fd_to_list P((SOCKET, enum desc_type));
  224. static void close_and_delete_fd_from_list P((SOCKET));
  225. static void add_addr_to_list P((struct sockaddr_storage *, struct interface *));
  226. static void delete_addr_from_list P((struct sockaddr_storage *));
  227. static struct interface *find_addr_in_list P((struct sockaddr_storage *));
  228. static struct interface *find_flagged_addr_in_list P((struct sockaddr_storage *, int));
  229. static void create_wildcards P((u_short));
  230. static isc_boolean_t address_okay P((struct interface *));
  231. static void convert_isc_if P((isc_interface_t *, struct interface *, u_short));
  232. static void delete_interface_from_list P((struct interface *));
  233. static struct interface *getinterface P((struct sockaddr_storage *, int));
  234. static struct interface *findlocalinterface P((struct sockaddr_storage *, int));
  235. static struct interface *findlocalcastinterface P((struct sockaddr_storage *, int));
  236. /*
  237. * Routines to read the ntp packets
  238. */
  239. #if !defined(HAVE_IO_COMPLETION_PORT)
  240. static inline int read_network_packet P((SOCKET, struct interface *, l_fp));
  241. static inline int read_refclock_packet P((SOCKET, struct refclockio *, l_fp));
  242. #endif
  243. #ifdef SYS_WINNT
  244. /*
  245. * Windows 2000 systems incorrectly cause UDP sockets using WASRecvFrom
  246. * to not work correctly, returning a WSACONNRESET error when a WSASendTo
  247. * fails with an "ICMP port unreachable" response and preventing the
  248. * socket from using the WSARecvFrom in subsequent operations.
  249. * The function below fixes this, but requires that Windows 2000
  250. * Service Pack 2 or later be installed on the system. NT 4.0
  251. * systems are not affected by this and work correctly.
  252. * See Microsoft Knowledge Base Article Q263823 for details of this.
  253. */
  254. void
  255. connection_reset_fix(
  256. SOCKET fd,
  257. struct sockaddr_storage *addr
  258. )
  259. {
  260. DWORD dwBytesReturned = 0;
  261. BOOL bNewBehavior = FALSE;
  262. DWORD status;
  263. /*
  264. * disable bad behavior using IOCTL: SIO_UDP_CONNRESET
  265. * NT 4.0 has no problem
  266. */
  267. if (isc_win32os_majorversion() >= 5) {
  268. status = WSAIoctl(fd, SIO_UDP_CONNRESET, &bNewBehavior,
  269. sizeof(bNewBehavior), NULL, 0,
  270. &dwBytesReturned, NULL, NULL);
  271. if (SOCKET_ERROR == status)
  272. netsyslog(LOG_ERR, "connection_reset_fix() "
  273. "failed for address %s: %m",
  274. stoa(addr));
  275. }
  276. }
  277. #endif
  278. /*
  279. * on Unix systems the stdio library typically
  280. * makes use of file descriptors in the lower
  281. * integer range. stdio usually will make use
  282. * of the file descriptor in the range of
  283. * [0..FOPEN_MAX)
  284. * in order to keep this range clean for socket
  285. * file descriptors we attempt to move them above
  286. * FOPEM_MAX. This is not as easy as it sounds as
  287. * FOPEN_MAX changes from implementation to implementation
  288. * and may exceed to current file decriptor limits.
  289. * We are using following strategy:
  290. * - keep a current socket fd boundary initialized with
  291. * max(0, min(getdtablesize() - FD_CHUNK, FOPEN_MAX))
  292. * - attempt to move the descriptor to the boundary or
  293. * above.
  294. * - if that fails and boundary > 0 set boundary
  295. * to min(0, socket_fd_boundary - FD_CHUNK)
  296. * -> retry
  297. * if failure and boundary == 0 return old fd
  298. * - on success close old fd return new fd
  299. *
  300. * effects:
  301. * - fds will be moved above the socket fd boundary
  302. * if at all possible.
  303. * - the socket boundary will be reduced until
  304. * allocation is possible or 0 is reached - at this
  305. * point the algrithm will be disabled
  306. */
  307. static int move_fd(SOCKET fd)
  308. {
  309. #if !defined(SYS_WINNT) && defined(F_DUPFD)
  310. #ifndef FD_CHUNK
  311. #define FD_CHUNK 10
  312. #endif
  313. /*
  314. * number of fds we would like to have for
  315. * stdio FILE* available.
  316. * we can pick a "low" number as our use of
  317. * FILE* is limited to log files and temporarily
  318. * to data and config files. Except for log files
  319. * we don't keep the other FILE* open beyond the
  320. * scope of the function that opened it.
  321. */
  322. #ifndef FD_PREFERRED_SOCKBOUNDARY
  323. #define FD_PREFERRED_SOCKBOUNDARY 48
  324. #endif
  325. #ifndef HAVE_GETDTABLESIZE
  326. /*
  327. * if we have no idea about the max fd value set up things
  328. * so we will start at FOPEN_MAX
  329. */
  330. #define getdtablesize() (FOPEN_MAX+FD_CHUNK)
  331. #endif
  332. #ifndef FOPEN_MAX
  333. #define FOPEN_MAX 20 /* assume that for the lack of anything better */
  334. #endif
  335. static SOCKET socket_boundary = -1;
  336. SOCKET newfd;
  337. /*
  338. * check whether boundary has be set up
  339. * already
  340. */
  341. if (socket_boundary == -1) {
  342. socket_boundary = max(0, min(getdtablesize() - FD_CHUNK,
  343. min(FOPEN_MAX, FD_PREFERRED_SOCKBOUNDARY)));
  344. #ifdef DEBUG
  345. msyslog(LOG_DEBUG, "ntp_io: estimated max descriptors: %d, initial socket boundary: %d",
  346. getdtablesize(), socket_boundary);
  347. #endif
  348. }
  349. /*
  350. * Leave a space for stdio to work in. potentially moving the
  351. * socket_boundary lower until allocation succeeds.
  352. */
  353. do {
  354. if (fd >= 0 && fd < socket_boundary) {
  355. /* inside reserved range: attempt to move fd */
  356. newfd = fcntl(fd, F_DUPFD, socket_boundary);
  357. if (newfd != -1) {
  358. /* success: drop the old one - return the new one */
  359. (void)close(fd);
  360. return (newfd);
  361. }
  362. } else {
  363. /* outside reserved range: no work - return the original one */
  364. return (fd);
  365. }
  366. socket_boundary = max(0, socket_boundary - FD_CHUNK);
  367. #ifdef DEBUG
  368. msyslog(LOG_DEBUG, "ntp_io: selecting new socket boundary: %d",
  369. socket_boundary);
  370. #endif
  371. } while (socket_boundary > 0);
  372. #endif /* !defined(SYS_WINNT) && defined(F_DUPFD) */
  373. return (fd);
  374. }
  375. #ifdef DEBUG_TIMING
  376. /*
  377. * collect timing information for various processing
  378. * paths. currently we only pass then on to the file
  379. * for later processing. this could also do histogram
  380. * based analysis in other to reduce the load (and skew)
  381. * dur to the file output
  382. */
  383. void
  384. collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
  385. {
  386. char buf[2048];
  387. snprintf(buf, sizeof(buf), "%s %d %s %s",
  388. (rb != NULL) ?
  389. ((rb->dstadr) ? stoa(&rb->recv_srcadr) : "-REFCLOCK-") : "-",
  390. count, lfptoa(dts, 9), tag);
  391. record_timing_stats(buf);
  392. }
  393. #endif
  394. /*
  395. * About dynamic interfaces, sockets, reception and more...
  396. *
  397. * the code solves following tasks:
  398. *
  399. * - keep a current list of active interfaces in order
  400. * to bind to to the interface address on NTP_PORT so that
  401. * all wild and specific bindings for NTP_PORT are taken by ntpd
  402. * to avoid other daemons messing with the time or sockets.
  403. * - all interfaces keep a list of peers that are referencing
  404. * the interface in order to quickly re-assign the peers to
  405. * new interface in case an interface is deleted (=> gone from system or
  406. * down)
  407. * - have a preconfigured socket ready with the right local address
  408. * for transmission and reception
  409. * - have an address list for all destination addresses used within ntpd
  410. * to find the "right" preconfigured socket.
  411. * - facilitate updating the internal interface list with respect to
  412. * the current kernel state
  413. *
  414. * special issues:
  415. *
  416. * - mapping of multicast addresses to the interface affected is not always
  417. * one to one - especially on hosts with multiple interfaces
  418. * the code here currently allocates a separate interface entry for those
  419. * multicast addresses
  420. * iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF)
  421. * in case of failure the multicast address is bound to an existing interface.
  422. * - on some systems it is perfectly legal to assign the same address to
  423. * multiple interfaces. Therefore this code does not keep a list of interfaces
  424. * but a list of interfaces that represent a unique address as determined by the kernel
  425. * by the procedure in findlocalinterface. Thus it is perfectly legal to see only
  426. * one representative of a group of real interfaces if they share the same address.
  427. *
  428. * Frank Kardel 20050910
  429. */
  430. /*
  431. * init_io - initialize I/O data structures and call socket creation routine
  432. */
  433. void
  434. init_io(void)
  435. {
  436. #ifdef SYS_WINNT
  437. init_io_completion_port();
  438. if (!Win32InitSockets())
  439. {
  440. netsyslog(LOG_ERR, "No useable winsock.dll: %m");
  441. exit(1);
  442. }
  443. init_transmitbuff();
  444. #endif /* SYS_WINNT */
  445. /*
  446. * Init buffer free list and stat counters
  447. */
  448. init_recvbuff(RECV_INIT);
  449. packets_dropped = packets_received = 0;
  450. packets_ignored = 0;
  451. packets_sent = packets_notsent = 0;
  452. handler_calls = handler_pkts = 0;
  453. io_timereset = 0;
  454. loopback_interface = NULL;
  455. any_interface = NULL;
  456. any6_interface = NULL;
  457. #ifdef REFCLOCK
  458. refio = NULL;
  459. #endif
  460. #if defined(HAVE_SIGNALED_IO)
  461. (void) set_signal();
  462. #endif
  463. ISC_LIST_INIT(fd_list);
  464. #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
  465. ISC_LIST_INIT(asyncio_reader_list);
  466. #endif
  467. ISC_LIST_INIT(remoteaddr_list);
  468. ISC_LIST_INIT(inter_list);
  469. /*
  470. * Create the sockets
  471. */
  472. BLOCKIO();
  473. (void) create_sockets(htons(NTP_PORT));
  474. UNBLOCKIO();
  475. init_async_notifications();
  476. DPRINTF(3, ("init_io: maxactivefd %d\n", maxactivefd));
  477. }
  478. #ifdef DEBUG
  479. /*
  480. * function to dump the contents of the interface structure
  481. * for debugging use only.
  482. */
  483. void
  484. interface_dump(struct interface *itf)
  485. {
  486. u_char* cp;
  487. int i;
  488. /* Limit the size of the sockaddr_storage hex dump */
  489. int maxsize = min(32, sizeof(struct sockaddr_storage));
  490. printf("Dumping interface: %p\n", itf);
  491. printf("fd = %d\n", itf->fd);
  492. printf("bfd = %d\n", itf->bfd);
  493. printf("sin = %s,\n", stoa(&(itf->sin)));
  494. cp = (u_char*) &(itf->sin);
  495. for(i = 0; i < maxsize; i++)
  496. {
  497. printf("%02x", *cp++);
  498. if((i+1)%4 == 0)
  499. printf(" ");
  500. }
  501. printf("\n");
  502. printf("bcast = %s,\n", stoa(&(itf->bcast)));
  503. cp = (u_char*) &(itf->bcast);
  504. for(i = 0; i < maxsize; i++)
  505. {
  506. printf("%02x", *cp++);
  507. if((i+1)%4 == 0)
  508. printf(" ");
  509. }
  510. printf("\n");
  511. printf("mask = %s,\n", stoa(&(itf->mask)));
  512. cp = (u_char*) &(itf->mask);
  513. for(i = 0; i < maxsize; i++)
  514. {
  515. printf("%02x", *cp++);
  516. if((i+1)%4 == 0)
  517. printf(" ");
  518. }
  519. printf("\n");
  520. printf("name = %s\n", itf->name);
  521. printf("flags = 0x%08x\n", itf->flags);
  522. printf("last_ttl = %d\n", itf->last_ttl);
  523. printf("addr_refid = %08x\n", itf->addr_refid);
  524. printf("num_mcast = %d\n", itf->num_mcast);
  525. printf("received = %ld\n", itf->received);
  526. printf("sent = %ld\n", itf->sent);
  527. printf("notsent = %ld\n", itf->notsent);
  528. printf("ifindex = %u\n", itf->ifindex);
  529. printf("scopeid = %u\n", itf->scopeid);
  530. printf("peercnt = %u\n", itf->peercnt);
  531. printf("phase = %u\n", itf->phase);
  532. }
  533. /*
  534. * print_interface - helper to output debug information
  535. */
  536. static void
  537. print_interface(struct interface *iface, char *pfx, char *sfx)
  538. {
  539. printf("%sinterface #%d: fd=%d, bfd=%d, name=%s, flags=0x%x, scope=%d, ifindex=%d",
  540. pfx,
  541. iface->ifnum,
  542. iface->fd,
  543. iface->bfd,
  544. iface->name,
  545. iface->flags,
  546. iface->scopeid,
  547. iface->ifindex);
  548. /* Leave these as three printf calls. */
  549. printf(", sin=%s",
  550. stoa((&iface->sin)));
  551. if (iface->flags & INT_BROADCAST)
  552. printf(", bcast=%s,",
  553. stoa((&iface->bcast)));
  554. if (iface->family == AF_INET)
  555. printf(", mask=%s",
  556. stoa((&iface->mask)));
  557. printf(", %s:%s", iface->ignore_packets == ISC_FALSE ? "Enabled" : "Disabled", sfx);
  558. if (debug > 4) /* in-depth debugging only */
  559. interface_dump(iface);
  560. }
  561. #endif
  562. #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
  563. /*
  564. * create an asyncio_reader structure
  565. */
  566. static struct asyncio_reader *
  567. new_asyncio_reader()
  568. {
  569. struct asyncio_reader *reader;
  570. reader = (struct asyncio_reader *)emalloc(sizeof(struct asyncio_reader));
  571. memset((char *)reader, 0, sizeof(*reader));
  572. ISC_LINK_INIT(reader, link);
  573. reader->fd = INVALID_SOCKET;
  574. return reader;
  575. }
  576. /*
  577. * delete a reader
  578. */
  579. static void
  580. delete_asyncio_reader(struct asyncio_reader *reader)
  581. {
  582. free(reader);
  583. }
  584. /*
  585. * add asynchio_reader
  586. */
  587. static void
  588. add_asyncio_reader(struct asyncio_reader *reader, enum desc_type type)
  589. {
  590. ISC_LIST_APPEND(asyncio_reader_list, reader, link);
  591. add_fd_to_list(reader->fd, type);
  592. }
  593. /*
  594. * remove asynchio_reader
  595. */
  596. static void
  597. remove_asyncio_reader(struct asyncio_reader *reader)
  598. {
  599. ISC_LIST_UNLINK_TYPE(asyncio_reader_list, reader, link, struct asyncio_reader);
  600. if (reader->fd != INVALID_SOCKET)
  601. close_and_delete_fd_from_list(reader->fd);
  602. reader->fd = INVALID_SOCKET;
  603. }
  604. #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
  605. /*
  606. * interface list enumerator - visitor pattern
  607. */
  608. void
  609. interface_enumerate(interface_receiver_t receiver, void *data)
  610. {
  611. interface_info_t ifi;
  612. struct interface *interf;
  613. ifi.action = IFS_EXISTS;
  614. for (interf = ISC_LIST_HEAD(inter_list);
  615. interf != NULL;
  616. interf = ISC_LIST_NEXT(interf, link)) {
  617. ifi.interface = interf;
  618. receiver(data, &ifi);
  619. }
  620. }
  621. /*
  622. * do standard initialization of interface structure
  623. */
  624. static void
  625. init_interface(struct interface *interface)
  626. {
  627. memset((char *)interface, 0, sizeof(struct interface));
  628. ISC_LINK_INIT(interface, link);
  629. ISC_LIST_INIT(interface->peers);
  630. interface->fd = INVALID_SOCKET;
  631. interface->bfd = INVALID_SOCKET;
  632. interface->num_mcast = 0;
  633. interface->received = 0;
  634. interface->sent = 0;
  635. interface->notsent = 0;
  636. interface->peercnt = 0;
  637. interface->phase = sys_interphase;
  638. }
  639. /*
  640. * create new interface structure initialize from
  641. * template structure or via standard initialization
  642. * function
  643. */
  644. static struct interface *
  645. new_interface(struct interface *interface)
  646. {
  647. static u_int sys_ifnum = 0;
  648. struct interface *iface = (struct interface *)emalloc(sizeof(struct interface));
  649. if (interface != NULL)
  650. {
  651. memcpy((char*)iface, (char*)interface, sizeof(*interface));
  652. }
  653. else
  654. {
  655. init_interface(iface);
  656. }
  657. iface->ifnum = sys_ifnum++; /* count every new instance of an interface in the system */
  658. iface->starttime = current_time;
  659. return iface;
  660. }
  661. /*
  662. * return interface storage into free memory pool
  663. */
  664. static void
  665. delete_interface(struct interface *interface)
  666. {
  667. free(interface);
  668. }
  669. /*
  670. * link interface into list of known interfaces
  671. */
  672. static void
  673. add_interface(struct interface *interface)
  674. {
  675. static struct interface *listhead = NULL;
  676. /*
  677. * For ntpd, the first few interfaces (wildcard, localhost)
  678. * will never be removed. This means inter_list.head is
  679. * unchanging once initialized. Take advantage of that to
  680. * watch for changes and catch corruption earlier. This
  681. * helped track down corruption caused by using FD_SET with
  682. * a descriptor numerically larger than FD_SETSIZE.
  683. */
  684. if (NULL == listhead)
  685. listhead = inter_list.head;
  686. if (listhead != inter_list.head) {
  687. msyslog(LOG_ERR, "add_interface inter_list.head corrupted: was %p now %p",
  688. listhead, inter_list.head);
  689. exit(1);
  690. }
  691. /*
  692. * Calculate the address hash
  693. */
  694. interface->addr_refid = addr2refid(&interface->sin);
  695. ISC_LIST_APPEND(inter_list, interface, link);
  696. ninterfaces++;
  697. }
  698. /*
  699. * remove interface from known interface list and clean up
  700. * associated resources
  701. */
  702. static void
  703. remove_interface(struct interface *interface)
  704. {
  705. struct sockaddr_storage resmask;
  706. ISC_LIST_UNLINK_TYPE(inter_list, interface, link, struct interface);
  707. delete_interface_from_list(interface);
  708. if (interface->fd != INVALID_SOCKET)
  709. {
  710. msyslog(LOG_INFO, "Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
  711. interface->ifnum,
  712. interface->name,
  713. stoa((&interface->sin)),
  714. NTP_PORT, /* XXX should extract port from sin structure */
  715. interface->received,
  716. interface->sent,
  717. interface->notsent,
  718. current_time - interface->starttime);
  719. close_and_delete_fd_from_list(interface->fd);
  720. }
  721. if (interface->bfd != INVALID_SOCKET)
  722. {
  723. msyslog(LOG_INFO, "Deleting interface #%d %s, broadcast address %s#%d",
  724. interface->ifnum,
  725. interface->name,
  726. stoa((&interface->bcast)),
  727. (u_short) NTP_PORT); /* XXX extract port from sin structure */
  728. close_and_delete_fd_from_list(interface->bfd);
  729. }
  730. ninterfaces--;
  731. ntp_monclearinterface(interface);
  732. /* remove restrict interface entry */
  733. /*
  734. * Blacklist bound interface address
  735. */
  736. SET_HOSTMASK(&resmask, interface->sin.ss_family);
  737. hack_restrict(RESTRICT_REMOVEIF, &interface->sin, &resmask,
  738. RESM_NTPONLY|RESM_INTERFACE, RES_IGNORE);
  739. }
  740. static void
  741. list_if_listening(struct interface *interface, u_short port)
  742. {
  743. msyslog(LOG_INFO, "Listening on interface #%d %s, %s#%d %s",
  744. interface->ifnum,
  745. interface->name,
  746. stoa((&interface->sin)),
  747. ntohs( (u_short) port),
  748. (interface->ignore_packets == ISC_FALSE) ?
  749. "Enabled": "Disabled");
  750. }
  751. static void
  752. create_wildcards(u_short port) {
  753. isc_boolean_t okipv4 = ISC_TRUE;
  754. /*
  755. * create pseudo-interface with wildcard IPv4 address
  756. */
  757. #ifdef IPV6_V6ONLY
  758. if(isc_net_probeipv4() != ISC_R_SUCCESS)
  759. okipv4 = ISC_FALSE;
  760. #endif
  761. if(okipv4 == ISC_TRUE) {
  762. struct interface *interface = new_interface(NULL);
  763. interface->family = AF_INET;
  764. interface->sin.ss_family = AF_INET;
  765. ((struct sockaddr_in*)&interface->sin)->sin_addr.s_addr = htonl(INADDR_ANY);
  766. ((struct sockaddr_in*)&interface->sin)->sin_port = port;
  767. (void) strncpy(interface->name, "wildcard", sizeof(interface->name));
  768. interface->mask.ss_family = AF_INET;
  769. ((struct sockaddr_in*)&interface->mask)->sin_addr.s_addr = htonl(~(u_int32)0);
  770. interface->flags = INT_BROADCAST | INT_UP | INT_WILDCARD;
  771. interface->ignore_packets = ISC_TRUE;
  772. #if defined(MCAST)
  773. /*
  774. * enable possible multicast reception on the broadcast socket
  775. */
  776. interface->bcast.ss_family = AF_INET;
  777. ((struct sockaddr_in*)&interface->bcast)->sin_port = port;
  778. ((struct sockaddr_in*)&interface->bcast)->sin_addr.s_addr = htonl(INADDR_ANY);
  779. #endif /* MCAST */
  780. interface->fd = open_socket(&interface->sin,
  781. interface->flags, 1, interface);
  782. if (interface->fd != INVALID_SOCKET) {
  783. wildipv4 = interface;
  784. any_interface = interface;
  785. add_addr_to_list(&interface->sin, interface);
  786. add_interface(interface);
  787. list_if_listening(interface, port);
  788. } else {
  789. msyslog(LOG_ERR, "unable to bind to wildcard socket address %s - another process may be running - EXITING",
  790. stoa((&interface->sin)));
  791. exit(1);
  792. }
  793. }
  794. #ifdef INCLUDE_IPV6_SUPPORT
  795. /*
  796. * create pseudo-interface with wildcard IPv6 address
  797. */
  798. if (isc_net_probeipv6() == ISC_R_SUCCESS) {
  799. struct interface *interface = new_interface(NULL);
  800. interface->family = AF_INET6;
  801. interface->sin.ss_family = AF_INET6;
  802. ((struct sockaddr_in6*)&interface->sin)->sin6_addr = in6addr_any;
  803. ((struct sockaddr_in6*)&interface->sin)->sin6_port = port;
  804. # ifdef ISC_PLATFORM_HAVESCOPEID
  805. ((struct sockaddr_in6*)&interface->sin)->sin6_scope_id = 0;
  806. # endif
  807. (void) strncpy(interface->name, "wildcard", sizeof(interface->name));
  808. interface->mask.ss_family = AF_INET6;
  809. memset(&((struct sockaddr_in6*)&interface->mask)->sin6_addr.s6_addr, 0xff, sizeof(struct in6_addr));
  810. interface->flags = INT_UP | INT_WILDCARD;
  811. interface->ignore_packets = ISC_TRUE;
  812. interface->fd = open_socket(&interface->sin,
  813. interface->flags, 1, interface);
  814. if (interface->fd != INVALID_SOCKET) {
  815. wildipv6 = interface;
  816. any6_interface = interface;
  817. add_addr_to_list(&interface->sin, interface);
  818. add_interface(interface);
  819. list_if_listening(interface, port);
  820. } else {
  821. msyslog(LOG_ERR, "unable to bind to wildcard socket address %s - another process may be running - EXITING",
  822. stoa((&interface->sin)));
  823. exit(1);
  824. }
  825. }
  826. #endif
  827. }
  828. static isc_boolean_t
  829. address_okay(struct interface *iface) {
  830. DPRINTF(4, ("address_okay: listen Virtual: %d, IF name: %s\n",
  831. listen_to_virtual_ips, iface->name));
  832. /*
  833. * Always allow the loopback
  834. */
  835. if((iface->flags & INT_LOOPBACK) != 0) {
  836. DPRINTF(4, ("address_okay: loopback - OK\n"));
  837. return (ISC_TRUE);
  838. }
  839. /*
  840. * Check if the interface is specified
  841. */
  842. if (specific_interface != NULL) {
  843. if (strcasecmp(iface->name, specific_interface) == 0) {
  844. DPRINTF(4, ("address_okay: specific interface name matched - OK\n"));
  845. return (ISC_TRUE);
  846. } else {
  847. DPRINTF(4, ("address_okay: specific interface name NOT matched - FAIL\n"));
  848. return (ISC_FALSE);
  849. }
  850. }
  851. else {
  852. if (listen_to_virtual_ips == 0 &&
  853. (strchr(iface->name, (int)':') != NULL)) {
  854. DPRINTF(4, ("address_okay: virtual ip/alias - FAIL\n"));
  855. return (ISC_FALSE);
  856. }
  857. }
  858. DPRINTF(4, ("address_okay: OK\n"));
  859. return (ISC_TRUE);
  860. }
  861. static void
  862. convert_isc_if(isc_interface_t *isc_if, struct interface *itf, u_short port)
  863. {
  864. itf->scopeid = 0;
  865. itf->family = (short) isc_if->af;
  866. strcpy(itf->name, isc_if->name);
  867. if(isc_if->af == AF_INET) {
  868. itf->sin.ss_family = (u_short) isc_if->af;
  869. memcpy(&(((struct sockaddr_in*)&itf->sin)->sin_addr),
  870. &(isc_if->address.type.in),
  871. sizeof(struct in_addr));
  872. ((struct sockaddr_in*)&itf->sin)->sin_port = port;
  873. if((isc_if->flags & INTERFACE_F_BROADCAST) != 0) {
  874. itf->flags |= INT_BROADCAST;
  875. itf->bcast.ss_family = itf->sin.ss_family;
  876. memcpy(&(((struct sockaddr_in*)&itf->bcast)->sin_addr),
  877. &(isc_if->broadcast.type.in),
  878. sizeof(struct in_addr));
  879. ((struct sockaddr_in*)&itf->bcast)->sin_port = port;
  880. }
  881. itf->mask.ss_family = itf->sin.ss_family;
  882. memcpy(&(((struct sockaddr_in*)&itf->mask)->sin_addr),
  883. &(isc_if->netmask.type.in),
  884. sizeof(struct in_addr));
  885. ((struct sockaddr_in*)&itf->mask)->sin_port = port;
  886. }
  887. #ifdef INCLUDE_IPV6_SUPPORT
  888. else if (isc_if->af == AF_INET6) {
  889. itf->sin.ss_family = (u_short) isc_if->af;
  890. memcpy(&(((struct sockaddr_in6 *)&itf->sin)->sin6_addr),
  891. &(isc_if->address.type.in6),
  892. sizeof(((struct sockaddr_in6 *)&itf->sin)->sin6_addr));
  893. ((struct sockaddr_in6 *)&itf->sin)->sin6_port = port;
  894. #ifdef ISC_PLATFORM_HAVESCOPEID
  895. ((struct sockaddr_in6 *)&itf->sin)->sin6_scope_id = isc_netaddr_getzone(&isc_if->address);
  896. itf->scopeid = isc_netaddr_getzone(&isc_if->address);
  897. #endif
  898. itf->mask.ss_family = itf->sin.ss_family;
  899. memcpy(&(((struct sockaddr_in6 *)&itf->mask)->sin6_addr),
  900. &(isc_if->netmask.type.in6),
  901. sizeof(struct in6_addr));
  902. ((struct sockaddr_in6 *)&itf->mask)->sin6_port = port;
  903. /* Copy the interface index */
  904. itf->ifindex = isc_if->ifindex;
  905. }
  906. #endif /* INCLUDE_IPV6_SUPPORT */
  907. /* Process the rest of the flags */
  908. if((isc_if->flags & INTERFACE_F_UP) != 0)
  909. itf->flags |= INT_UP;
  910. if((isc_if->flags & INTERFACE_F_LOOPBACK) != 0)
  911. itf->flags |= INT_LOOPBACK;
  912. if((isc_if->flags & INTERFACE_F_POINTTOPOINT) != 0)
  913. itf->flags |= INT_PPP;
  914. if((isc_if->flags & INTERFACE_F_MULTICAST) != 0)
  915. itf->flags |= INT_MULTICAST;
  916. }
  917. /*
  918. * refresh_interface
  919. *
  920. * some OSes have been observed to keep
  921. * cached routes even when more specific routes
  922. * become available.
  923. * this can be mitigated by re-binding
  924. * the socket.
  925. */
  926. static int
  927. refresh_interface(struct interface * interface)
  928. {
  929. #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
  930. if (interface->fd != INVALID_SOCKET)
  931. {
  932. close_and_delete_fd_from_list(interface->fd);
  933. interface->fd = open_socket(&interface->sin,
  934. interface->flags, 0, interface);
  935. /*
  936. * reset TTL indication so TTL is is set again
  937. * next time around
  938. */
  939. interface->last_ttl = 0;
  940. return interface->fd != INVALID_SOCKET;
  941. }
  942. else
  943. {
  944. return 0; /* invalid sockets are not refreshable */
  945. }
  946. #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
  947. return interface->fd != INVALID_SOCKET;
  948. #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
  949. }
  950. /*
  951. * interface_update - externally callable update function
  952. */
  953. void
  954. interface_update(interface_receiver_t receiver, void *data)
  955. {
  956. if (!disable_dynamic_updates) {
  957. int new_interface_found;
  958. BLOCKIO();
  959. new_interface_found = update_interfaces(htons(NTP_PORT), receiver, data);
  960. UNBLOCKIO();
  961. if (new_interface_found) {
  962. #ifdef DEBUG
  963. msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
  964. #endif
  965. #ifdef SYS_WINNT
  966. /* wake up the resolver thread */
  967. if (ResolverEventHandle != NULL)
  968. SetEvent(ResolverEventHandle);
  969. #else
  970. /* write any single byte to the pipe to wake up the resolver process */
  971. write( resolver_pipe_fd[1], &new_interface_found, 1 );
  972. #endif
  973. }
  974. }
  975. }
  976. /*
  977. * find out if a given interface structure contains
  978. * a wildcard address
  979. */
  980. static int
  981. is_wildcard_addr(struct sockaddr_storage *sas)
  982. {
  983. if (sas->ss_family == AF_INET &&
  984. ((struct sockaddr_in*)sas)->sin_addr.s_addr == htonl(INADDR_ANY))
  985. return 1;
  986. #ifdef INCLUDE_IPV6_SUPPORT
  987. if (sas->ss_family == AF_INET6 &&
  988. memcmp(&((struct sockaddr_in6*)sas)->sin6_addr, &in6addr_any,
  989. sizeof(in6addr_any) == 0))
  990. return 1;
  991. #endif
  992. return 0;
  993. }
  994. #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
  995. /*
  996. * enable/disable re-use of wildcard address socket
  997. */
  998. static void
  999. set_wildcard_reuse(int family, int on)
  1000. {
  1001. int onvalue = 1;
  1002. int offvalue = 0;
  1003. int *onoff;
  1004. SOCKET fd = INVALID_SOCKET;
  1005. onoff = on ? &onvalue : &offvalue;
  1006. switch (family) {
  1007. case AF_INET:
  1008. if (any_interface) {
  1009. fd = any_interface->fd;
  1010. }
  1011. break;
  1012. #ifdef INCLUDE_IPV6_SUPPORT
  1013. case AF_INET6:
  1014. if (any6_interface) {
  1015. fd = any6_interface->fd;
  1016. }
  1017. break;
  1018. #endif /* !INCLUDE_IPV6_SUPPORT */
  1019. }
  1020. if (fd != INVALID_SOCKET) {
  1021. if (setsockopt(fd, SOL_SOCKET,
  1022. SO_REUSEADDR, (char *)onoff,
  1023. sizeof(*onoff))) {
  1024. netsyslog(LOG_ERR, "set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m", *onoff ? "on" : "off");
  1025. }
  1026. DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n", *onoff ? "ON" : "OFF",
  1027. stoa((family == AF_INET) ?
  1028. &any_interface->sin : &any6_interface->sin)));
  1029. }
  1030. }
  1031. #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
  1032. #ifdef INCLUDE_IPV6_SUPPORT
  1033. static isc_boolean_t
  1034. is_anycast(struct sockaddr *sa, char *name)
  1035. {
  1036. #if defined(SIOCGIFAFLAG_IN6) && defined(IN6_IFF_ANYCAST)
  1037. struct in6_ifreq ifr6;
  1038. int fd;
  1039. u_int32_t flags6;
  1040. if (sa->sa_family != AF_INET6)
  1041. return ISC_FALSE;
  1042. if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
  1043. return ISC_FALSE;
  1044. memset(&ifr6, 0, sizeof(ifr6));
  1045. memcpy(&ifr6.ifr_addr, (struct sockaddr_in6 *)sa,
  1046. sizeof(struct sockaddr_in6));
  1047. strlcpy(ifr6.ifr_name, name, IF_NAMESIZE);
  1048. if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
  1049. close(fd);
  1050. return ISC_FALSE;
  1051. }
  1052. close(fd);
  1053. flags6 = ifr6.ifr_ifru.ifru_flags6;
  1054. if ((flags6 & IN6_IFF_ANYCAST) != 0)
  1055. return ISC_TRUE;
  1056. #endif /* !SIOCGIFAFLAG_IN6 || !IN6_IFF_ANYCAST */
  1057. return ISC_FALSE;
  1058. }
  1059. #endif /* !INCLUDE_IPV6_SUPPORT */
  1060. /*
  1061. * update_interface strategy
  1062. *
  1063. * toggle configuration phase
  1064. *
  1065. * Phase 1:
  1066. * forall currently existing interfaces
  1067. * if address is known:
  1068. * drop socket - rebind again
  1069. *
  1070. * if address is NOT known:
  1071. * attempt to create a new interface entry
  1072. *
  1073. * Phase 2:
  1074. * forall currently known non MCAST and WILDCARD interfaces
  1075. * if interface does not match configuration phase (not seen in phase 1):
  1076. * remove interface from known interface list
  1077. * forall peers associated with this interface
  1078. * disconnect peer from this interface
  1079. *
  1080. * Phase 3:
  1081. * attempt to re-assign interfaces to peers
  1082. *
  1083. */
  1084. static int
  1085. update_interfaces(
  1086. u_short port,
  1087. interface_receiver_t receiver,
  1088. void *data
  1089. )
  1090. {
  1091. interface_info_t ifi;
  1092. isc_mem_t *mctx = NULL;
  1093. isc_interfaceiter_t *iter = NULL;
  1094. isc_boolean_t scan_ipv4 = ISC_FALSE;
  1095. isc_boolean_t scan_ipv6 = ISC_FALSE;
  1096. isc_result_t result;
  1097. int new_interface_found = 0;
  1098. DPRINTF(3, ("update_interfaces(%d)\n", ntohs( (u_short) port)));
  1099. #ifdef INCLUDE_IPV6_SUPPORT
  1100. if (isc_net_probeipv6() == ISC_R_SUCCESS)
  1101. scan_ipv6 = ISC_TRUE;
  1102. #if defined(DEBUG)
  1103. else
  1104. if (debug)
  1105. netsyslog(LOG_ERR, "no IPv6 interfaces found");
  1106. #endif
  1107. #endif
  1108. if (isc_net_probeipv6() == ISC_R_SUCCESS)
  1109. scan_ipv6 = ISC_TRUE;
  1110. #if defined(ISC_PLATFORM_HAVEIPV6) && defined(DEBUG)
  1111. else
  1112. if (debug)
  1113. netsyslog(LOG_ERR, "no IPv6 interfaces found");
  1114. #endif
  1115. if (isc_net_probeipv4() == ISC_R_SUCCESS)
  1116. scan_ipv4 = ISC_TRUE;
  1117. #ifdef DEBUG
  1118. else
  1119. if(debug)
  1120. netsyslog(LOG_ERR, "no IPv4 interfaces found");
  1121. #endif
  1122. /*
  1123. * phase one - scan interfaces
  1124. * - create those that are not found
  1125. * - update those that are found
  1126. */
  1127. result = isc_interfaceiter_create(mctx, &iter);
  1128. if (result != ISC_R_SUCCESS)
  1129. return 0;
  1130. sys_interphase ^= 0x1; /* toggle system phase for finding untouched (to be deleted) interfaces */
  1131. for (result = isc_interfaceiter_first(iter);
  1132. result == ISC_R_SUCCESS;
  1133. result = isc_interfaceiter_next(iter))
  1134. {
  1135. isc_interface_t isc_if;
  1136. unsigned int family;
  1137. struct interface interface;
  1138. struct interface *iface;
  1139. result = isc_interfaceiter_current(iter, &isc_if);
  1140. if (result != ISC_R_SUCCESS)
  1141. break;
  1142. /* See if we have a valid family to use */
  1143. family = isc_if.address.family;
  1144. if (family != AF_INET && family != AF_INET6)
  1145. continue;
  1146. if (scan_ipv4 == ISC_FALSE && family == AF_INET)
  1147. continue;
  1148. if (scan_ipv6 == ISC_FALSE && family == AF_INET6)
  1149. continue;
  1150. /*
  1151. * create prototype
  1152. */
  1153. init_interface(&interface);
  1154. convert_isc_if(&isc_if, &interface, port);
  1155. /*
  1156. * Check to see if we are going to use the interface
  1157. * If we don't use it we mark it to drop any packet
  1158. * received but we still must create the socket and
  1159. * bind to it. This prevents other apps binding to it
  1160. * and potentially causing problems with more than one
  1161. * process fiddling with the clock
  1162. */
  1163. if (address_okay(&interface) == ISC_TRUE) {
  1164. interface.ignore_packets = ISC_FALSE;
  1165. }
  1166. else {
  1167. interface.ignore_packets = ISC_TRUE;
  1168. }
  1169. DPRINT_INTERFACE(4, (&interface, "examining ", "\n"));
  1170. if (!(interface.flags & INT_UP)) { /* interfaces must be UP to be usable */
  1171. DPRINTF(4, ("skipping interface %s (%s) - DOWN\n", interface.name, stoa(&interface.sin)));
  1172. continue;
  1173. }
  1174. /*
  1175. * skip any interfaces UP and bound to a wildcard
  1176. * address - some dhcp clients produce that in the
  1177. * wild
  1178. */
  1179. if (is_wildcard_addr(&interface.sin))
  1180. continue;
  1181. #ifdef INCLUDE_IPV6_SUPPORT
  1182. if (is_anycast((struct sockaddr *)&interface.sin, isc_if.name))
  1183. continue;
  1184. #endif /* !INCLUDE_IPV6_SUPPORT */
  1185. /*
  1186. * map to local *address* in order
  1187. * to map all duplicate interfaces to an interface structure
  1188. * with the appropriate socket (our name space is
  1189. * (ip-address) - NOT (interface name, ip-address))
  1190. */
  1191. iface = getinterface(&interface.sin, INT_WILDCARD);
  1192. if (iface && refresh_interface(iface))
  1193. {
  1194. /*
  1195. * found existing and up to date interface - mark present
  1196. */
  1197. iface->phase = sys_interphase;
  1198. DPRINT_INTERFACE(4, (iface, "updating ", " present\n"));
  1199. ifi.action = IFS_EXISTS;
  1200. ifi.interface = iface;
  1201. if (receiver)
  1202. receiver(data, &ifi);
  1203. }
  1204. else
  1205. {
  1206. /*
  1207. * this is new or refreshing failed - add to our interface list
  1208. * if refreshing failed we will delete the interface structure in
  1209. * phase 2 as the interface was not marked current. We can bind to
  1210. * the address as the refresh code already closed the offending socket
  1211. */
  1212. iface = create_interface(port, &interface);
  1213. if (iface)
  1214. {
  1215. ifi.action = IFS_CREATED;
  1216. ifi.interface = iface;
  1217. if (receiver)
  1218. receiver(data, &ifi);
  1219. new_interface_found = 1;
  1220. DPRINT_INTERFACE(3, (iface, "updating ", " new - created\n"));
  1221. }
  1222. else
  1223. {
  1224. DPRINT_INTERFACE(3, (&interface, "updating ", " new - creation FAILED"));
  1225. msyslog(LOG_INFO, "failed to initialize interface for address %s", stoa(&interface.sin));
  1226. continue;
  1227. }
  1228. }
  1229. }
  1230. isc_interfaceiter_destroy(&iter);
  1231. /*
  1232. * phase 2 - delete gone interfaces - reassigning peers to other interfaces
  1233. */
  1234. {
  1235. struct interface *interf = ISC_LIST_HEAD(inter_list);
  1236. while (interf != NULL)
  1237. {
  1238. struct interface *next = ISC_LIST_NEXT(interf, link);
  1239. if (!(interf->flags & (INT_WILDCARD|INT_MCASTIF))) {
  1240. /*
  1241. * if phase does not match sys_phase this interface was not
  1242. * enumerated during interface scan - so it is gone and
  1243. * will be deleted here unless it is solely an MCAST/WILDCARD interface
  1244. */
  1245. if (interf->phase != sys_interphase) {
  1246. struct peer *peer;
  1247. DPRINT_INTERFACE(3, (interf, "updating ", "GONE - deleting\n"));
  1248. remove_interface(interf);
  1249. ifi.action = IFS_DELETED;
  1250. ifi.interface = interf;
  1251. if (receiver)
  1252. receiver(data, &ifi);
  1253. peer = ISC_LIST_HEAD(interf->peers);
  1254. /*
  1255. * disconnect peer from deleted interface
  1256. */
  1257. while (peer != NULL) {
  1258. struct peer *npeer = ISC_LIST_NEXT(peer, ilink);
  1259. /*
  1260. * this one just lost it's interface
  1261. */
  1262. set_peerdstadr(peer, NULL);
  1263. peer = npeer;
  1264. }
  1265. /*
  1266. * update globals in case we lose
  1267. * a loopback interface
  1268. */
  1269. if (interf == loopback_interface)
  1270. loopback_interface = NULL;
  1271. delete_interface(interf);
  1272. }
  1273. }
  1274. interf = next;
  1275. }
  1276. }
  1277. /*
  1278. * phase 3 - re-configure as the world has changed if necessary
  1279. */
  1280. refresh_all_peerinterfaces();
  1281. return new_interface_found;
  1282. }
  1283. /*
  1284. * create_sockets - create a socket for each interface plus a default
  1285. * socket for when we don't know where to send
  1286. */
  1287. static int
  1288. create_sockets(
  1289. u_short port
  1290. )
  1291. {
  1292. #ifndef HAVE_IO_COMPLETION_PORT
  1293. /*
  1294. * I/O Completion Ports don't care about the select and FD_SET
  1295. */
  1296. maxactivefd = 0;
  1297. FD_ZERO(&activefds);
  1298. #endif
  1299. DPRINTF(2, ("create_sockets(%d)\n", ntohs( (u_short) port)));
  1300. create_wildcards(port);
  1301. update_interfaces(port, NULL, NULL);
  1302. /*
  1303. * Now that we have opened all the sockets, turn off the reuse
  1304. * flag for security.
  1305. */
  1306. set_reuseaddr(0);
  1307. DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
  1308. return ninterfaces;
  1309. }
  1310. /*
  1311. * create_interface - create a new interface for a given prototype
  1312. * binding the socket.
  1313. */
  1314. static struct interface *
  1315. create_interface(
  1316. u_short port,
  1317. struct interface *iface
  1318. )
  1319. {
  1320. struct sockaddr_storage resmask;
  1321. struct interface *interface;
  1322. DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&iface->sin), ntohs( (u_short) port)));
  1323. /* build an interface */
  1324. interface = new_interface(iface);
  1325. /*
  1326. * create socket
  1327. */
  1328. interface->fd = open_socket(&interface->sin,
  1329. interface->flags, 0, interface);
  1330. if (interface->fd != INVALID_SOCKET)
  1331. list_if_listening(interface, port);
  1332. if ((interface->flags & INT_BROADCAST) &&
  1333. interface->bfd != INVALID_SOCKET)
  1334. msyslog(LOG_INFO, "Listening on broadcast address %s#%d",
  1335. stoa((&interface->bcast)),
  1336. ntohs( (u_short) port));
  1337. if (interface->fd == INVALID_SOCKET &&
  1338. interface->bfd == INVALID_SOCKET) {
  1339. msyslog(LOG_ERR, "unable to create socket on %s (%d) for %s#%d",
  1340. interface->name,
  1341. interface->ifnum,
  1342. stoa((&interface->sin)),
  1343. ntohs( (u_short) port));
  1344. delete_interface(interface);
  1345. return NULL;
  1346. }
  1347. /*
  1348. * Blacklist bound interface address
  1349. */
  1350. SET_HOSTMASK(&resmask, interface->sin.ss_family);
  1351. hack_restrict(RESTRICT_FLAGS, &interface->sin, &resmask,
  1352. RESM_NTPONLY|RESM_INTERFACE, RES_IGNORE);
  1353. /*
  1354. * set globals with the first found
  1355. * loopback interface of the appropriate class
  1356. */
  1357. if ((loopback_interface == NULL) &&
  1358. (interface->family == AF_INET) &&
  1359. ((interface->flags & INT_LOOPBACK) != 0))
  1360. {
  1361. loopback_interface = interface;
  1362. }
  1363. /*
  1364. * put into our interface list
  1365. */
  1366. add_addr_to_list(&interface->sin, interface);
  1367. add_interface(interface);
  1368. DPRINT_INTERFACE(2, (interface, "created ", "\n"));
  1369. return interface;
  1370. }
  1371. #ifdef SO_EXCLUSIVEADDRUSE
  1372. static void
  1373. set_excladdruse(int fd)
  1374. {
  1375. int one = 1;
  1376. int failed;
  1377. failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
  1378. (char *)&one, sizeof(one));
  1379. if (failed)
  1380. netsyslog(LOG_ERR,
  1381. "setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m", fd);
  1382. }
  1383. #endif /* SO_EXCLUSIVEADDRUSE */
  1384. /*
  1385. * set_reuseaddr() - set/clear REUSEADDR on all sockets
  1386. * NB possible hole - should we be doing this on broadcast
  1387. * fd's also?
  1388. */
  1389. static void
  1390. set_reuseaddr(int flag) {
  1391. struct interface *interf;
  1392. #ifndef SO_EXCLUSIVEADDRUSE
  1393. for (interf = ISC_LIST_HEAD(inter_list);
  1394. interf != NULL;
  1395. interf = ISC_LIST_NEXT(interf, link)) {
  1396. if (interf->flags & INT_WILDCARD)
  1397. continue;
  1398. /*
  1399. * if interf->fd is INVALID_SOCKET, we might have a adapter
  1400. * configured but not present
  1401. */
  1402. DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n", interf->name, stoa(&interf->sin), flag ? "on" : "off"));
  1403. if (interf->fd != INVALID_SOCKET) {
  1404. if (setsockopt(interf->fd, SOL_SOCKET,
  1405. SO_REUSEADDR, (char *)&flag,
  1406. sizeof(flag))) {
  1407. netsyslog(LOG_ERR, "set_reuseaddr: setsockopt(SO_REUSEADDR, %s) failed: %m", flag ? "on" : "off");
  1408. }
  1409. }
  1410. }
  1411. #endif /* ! SO_EXCLUSIVEADDRUSE */
  1412. }
  1413. /*
  1414. * This is just a wrapper around an internal function so we can
  1415. * make other changes as necessary later on
  1416. */
  1417. void
  1418. enable_broadcast(struct interface *iface, struct sockaddr_storage *baddr)
  1419. {
  1420. #ifdef SO_BROADCAST
  1421. socket_broadcast_enable(iface, iface->fd, baddr);
  1422. #endif
  1423. }
  1424. #ifdef OPEN_BCAST_SOCKET
  1425. /*
  1426. * Enable a broadcast address to a given socket
  1427. * The socket is in the inter_list all we need to do is enable
  1428. * broadcasting. It is not this function's job to select the socket
  1429. */
  1430. static isc_boolean_t
  1431. socket_broadcast_enable(struct interface *iface, SOCKET fd, struct sockaddr_storage *maddr)
  1432. {
  1433. #ifdef SO_BROADCAST
  1434. int on = 1;
  1435. if (maddr->ss_family == AF_INET)
  1436. {
  1437. /* if this interface can support broadcast, set SO_BROADCAST */
  1438. if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
  1439. (char *)&on, sizeof(on)))
  1440. {
  1441. netsyslog(LOG_ERR, "setsockopt(SO_BROADCAST) enable failure on address %s: %m",
  1442. stoa(maddr));
  1443. }
  1444. #ifdef DEBUG
  1445. else if (debug > 1) {
  1446. printf("Broadcast enabled on socket %d for address %s\n",
  1447. fd, stoa(maddr));
  1448. }
  1449. #endif
  1450. }
  1451. iface->flags |= INT_BCASTOPEN;
  1452. return ISC_TRUE;
  1453. #else
  1454. return ISC_FALSE;
  1455. #endif /* SO_BROADCAST */
  1456. }
  1457. /*
  1458. * Remove a broadcast address from a given socket
  1459. * The socket is in the inter_list all we need to do is disable
  1460. * broadcasting. It is not this function's job to select the socket
  1461. */
  1462. static isc_boolean_t
  1463. socket_broadcast_disable(struct interface *iface, struct sockaddr_storage *maddr)
  1464. {
  1465. #ifdef SO_BROADCAST
  1466. int off = 0; /* This seems to be OK as an int */
  1467. if (maddr->ss_family == AF_INET)
  1468. {
  1469. if (setsockopt(iface->fd, SOL_SOCKET, SO_BROADCAST,
  1470. (char *)&off, sizeof(off)))
  1471. {
  1472. netsyslog(LOG_ERR, "setsockopt(SO_BROADCAST) disable failure on address %s: %m",
  1473. stoa(maddr));
  1474. }
  1475. }
  1476. iface->flags &= ~INT_BCASTOPEN;
  1477. return ISC_TRUE;
  1478. #else
  1479. return ISC_FALSE;
  1480. #endif /* SO_BROADCAST */
  1481. }
  1482. #endif /* OPEN_BCAST_SOCKET */
  1483. /*
  1484. * Check to see if the address is a multicast address
  1485. */
  1486. static isc_boolean_t
  1487. addr_ismulticast(struct sockaddr_storage *maddr)
  1488. {
  1489. switch (maddr->ss_family)
  1490. {
  1491. case AF_INET :
  1492. if (!IN_CLASSD(ntohl(((struct sockaddr_in*)maddr)->sin_addr.s_addr))) {
  1493. DPRINTF(4, ("multicast address %s not class D\n", stoa(maddr)));
  1494. return (ISC_FALSE);
  1495. }
  1496. else
  1497. {
  1498. return (ISC_TRUE);
  1499. }
  1500. case AF_INET6 :
  1501. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1502. if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)maddr)->sin6_addr)) {
  1503. DPRINTF(4, ("address %s not IPv6 multicast address\n", stoa(maddr)));
  1504. return (ISC_FALSE);
  1505. }
  1506. else
  1507. {
  1508. return (ISC_TRUE);
  1509. }
  1510. /*
  1511. * If we don't have IPV6 support any IPV6 address is not multicast
  1512. */
  1513. #else
  1514. return (ISC_FALSE);
  1515. #endif
  1516. /*
  1517. * Never valid
  1518. */
  1519. default:
  1520. return (ISC_FALSE);
  1521. }
  1522. }
  1523. /*
  1524. * Multicast servers need to set the appropriate Multicast interface
  1525. * socket option in order for it to know which interface to use for
  1526. * send the multicast packet.
  1527. */
  1528. void
  1529. enable_multicast_if(struct interface *iface, struct sockaddr_storage *maddr)
  1530. {
  1531. #ifdef MCAST
  1532. #ifdef IP_MULTICAST_LOOP
  1533. /*u_char*/ TYPEOF_IP_MULTICAST_LOOP off = 0;
  1534. #endif
  1535. #ifdef IPV6_MULTICAST_LOOP
  1536. u_int off6 = 0; /* RFC 3493, 5.2. defines type unsigned int */
  1537. #endif
  1538. switch (maddr->ss_family)
  1539. {
  1540. case AF_INET:
  1541. if (setsockopt(iface->fd, IPPROTO_IP, IP_MULTICAST_IF,
  1542. (char *)&(((struct sockaddr_in*)&iface->sin)->sin_addr.s_addr),
  1543. sizeof(struct in_addr)) == -1) {
  1544. netsyslog(LOG_ERR,
  1545. "setsockopt IP_MULTICAST_IF failure: %m on socket %d, addr %s for multicast address %s",
  1546. iface->fd, stoa(&iface->sin), stoa(maddr));
  1547. return;
  1548. }
  1549. #ifdef IP_MULTICAST_LOOP
  1550. /*
  1551. * Don't send back to itself, but allow it to fail to set it
  1552. */
  1553. if (setsockopt(iface->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
  1554. SETSOCKOPT_ARG_CAST &off, sizeof(off)) == -1) {
  1555. netsyslog(LOG_ERR,
  1556. "setsockopt IP_MULTICAST_LOOP failure: %m on socket %d, addr %s for multicast address %s",
  1557. iface->fd, stoa(&iface->sin), stoa(maddr));
  1558. }
  1559. #endif
  1560. DPRINTF(4, ("Added IPv4 multicast interface on socket %d, addr %s for multicast address %s\n",
  1561. iface->fd, stoa(&iface->sin),
  1562. stoa(maddr)));
  1563. break;
  1564. case AF_INET6:
  1565. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1566. if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1567. (char *) &iface->scopeid, sizeof(iface->scopeid)) == -1) {
  1568. netsyslog(LOG_ERR,
  1569. "setsockopt IPV6_MULTICAST_IF failure: %m on socket %d, addr %s, scope %d for multicast address %s",
  1570. iface->fd, stoa(&iface->sin), iface->scopeid,
  1571. stoa(maddr));
  1572. return;
  1573. }
  1574. #ifdef IPV6_MULTICAST_LOOP
  1575. /*
  1576. * Don't send back to itself, but allow it to fail to set it
  1577. */
  1578. if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1579. (char *) &off6, sizeof(off6)) == -1) {
  1580. netsyslog(LOG_ERR,
  1581. "setsockopt IPV6_MULTICAST_LOOP failure: %m on socket %d, addr %s for multicast address %s",
  1582. iface->fd, stoa(&iface->sin), stoa(maddr));
  1583. }
  1584. #endif
  1585. DPRINTF(4, ("Added IPv6 multicast interface on socket %d, addr %s, scope %d for multicast address %s\n",
  1586. iface->fd, stoa(&iface->sin), iface->scopeid,
  1587. stoa(maddr)));
  1588. break;
  1589. #else
  1590. return;
  1591. #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
  1592. }
  1593. return;
  1594. #endif
  1595. }
  1596. /*
  1597. * Add a multicast address to a given socket
  1598. * The socket is in the inter_list all we need to do is enable
  1599. * multicasting. It is not this function's job to select the socket
  1600. */
  1601. static isc_boolean_t
  1602. socket_multicast_enable(struct interface *iface, int lscope, struct sockaddr_storage *maddr)
  1603. {
  1604. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1605. struct ipv6_mreq mreq6;
  1606. struct in6_addr iaddr6;
  1607. #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
  1608. struct ip_mreq mreq;
  1609. if (find_addr_in_list(maddr)) {
  1610. DPRINTF(4, ("socket_multicast_enable(%s): already enabled\n", stoa(maddr)));
  1611. return ISC_TRUE;
  1612. }
  1613. switch (maddr->ss_family)
  1614. {
  1615. case AF_INET:
  1616. memset((char *)&mreq, 0, sizeof(mreq));
  1617. mreq.imr_multiaddr = (((struct sockaddr_in*)maddr)->sin_addr);
  1618. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  1619. if (setsockopt(iface->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1620. (char *)&mreq, sizeof(mreq)) == -1) {
  1621. netsyslog(LOG_ERR,
  1622. "setsockopt IP_ADD_MEMBERSHIP failure: %m on socket %d, addr %s for %x / %x (%s)",
  1623. iface->fd, stoa(&iface->sin),
  1624. mreq.imr_multiaddr.s_addr,
  1625. mreq.imr_interface.s_addr, stoa(maddr));
  1626. return ISC_FALSE;
  1627. }
  1628. DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n",
  1629. iface->fd, stoa(&iface->sin),
  1630. mreq.imr_multiaddr.s_addr,
  1631. mreq.imr_interface.s_addr, stoa(maddr)));
  1632. break;
  1633. case AF_INET6:
  1634. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1635. /*
  1636. * Enable reception of multicast packets
  1637. * If the address is link-local we can get the interface index
  1638. * from the scope id. Don't do this for other types of multicast
  1639. * addresses. For now let the kernel figure it out.
  1640. */
  1641. memset((char *)&mreq6, 0, sizeof(mreq6));
  1642. iaddr6 = ((struct sockaddr_in6*)maddr)->sin6_addr;
  1643. mreq6.ipv6mr_multiaddr = iaddr6;
  1644. mreq6.ipv6mr_interface = lscope;
  1645. if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1646. (char *)&mreq6, sizeof(mreq6)) == -1) {
  1647. netsyslog(LOG_ERR,
  1648. "setsockopt IPV6_JOIN_GROUP failure: %m on socket %d, addr %s for interface %d(%s)",
  1649. iface->fd, stoa(&iface->sin),
  1650. mreq6.ipv6mr_interface, stoa(maddr));
  1651. return ISC_FALSE;
  1652. }
  1653. DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %d(%s)\n",
  1654. iface->fd, stoa(&iface->sin),
  1655. mreq6.ipv6mr_interface, stoa(maddr)));
  1656. break;
  1657. #else
  1658. return ISC_FALSE;
  1659. #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
  1660. }
  1661. iface->flags |= INT_MCASTOPEN;
  1662. iface->num_mcast++;
  1663. add_addr_to_list(maddr, iface);
  1664. return ISC_TRUE;
  1665. }
  1666. /*
  1667. * Remove a multicast address from a given socket
  1668. * The socket is in the inter_list all we need to do is disable
  1669. * multicasting. It is not this function's job to select the socket
  1670. */
  1671. static isc_boolean_t
  1672. socket_multicast_disable(struct interface *iface, struct sockaddr_storage *maddr)
  1673. {
  1674. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1675. struct ipv6_mreq mreq6;
  1676. struct in6_addr iaddr6;
  1677. #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
  1678. struct ip_mreq mreq;
  1679. memset((char *)&mreq, 0, sizeof(mreq));
  1680. if (find_addr_in_list(maddr) == NULL) {
  1681. DPRINTF(4, ("socket_multicast_disable(%s): not enabled\n", stoa(maddr)));
  1682. return ISC_TRUE;
  1683. }
  1684. switch (maddr->ss_family)
  1685. {
  1686. case AF_INET:
  1687. mreq.imr_multiaddr = (((struct sockaddr_in*)&maddr)->sin_addr);
  1688. mreq.imr_interface.s_addr = ((struct sockaddr_in*)&iface->sin)->sin_addr.s_addr;
  1689. if (setsockopt(iface->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
  1690. (char *)&mreq, sizeof(mreq)) == -1) {
  1691. netsyslog(LOG_ERR,
  1692. "setsockopt IP_DROP_MEMBERSHIP failure: %m on socket %d, addr %s for %x / %x (%s)",
  1693. iface->fd, stoa(&iface->sin),
  1694. mreq.imr_multiaddr.s_addr,
  1695. mreq.imr_interface.s_addr, stoa(maddr));
  1696. return ISC_FALSE;
  1697. }
  1698. break;
  1699. case AF_INET6:
  1700. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1701. /*
  1702. * Disable reception of multicast packets
  1703. * If the address is link-local we can get the interface index
  1704. * from the scope id. Don't do this for other types of multicast
  1705. * addresses. For now let the kernel figure it out.
  1706. */
  1707. iaddr6 = ((struct sockaddr_in6*)&maddr)->sin6_addr;
  1708. mreq6.ipv6mr_multiaddr = iaddr6;
  1709. mreq6.ipv6mr_interface = iface->scopeid;
  1710. if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
  1711. (char *)&mreq6, sizeof(mreq6)) == -1) {
  1712. netsyslog(LOG_ERR,
  1713. "setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d(%s)",
  1714. iface->fd, stoa(&iface->sin),
  1715. mreq6.ipv6mr_interface, stoa(maddr));
  1716. return ISC_FALSE;
  1717. }
  1718. break;
  1719. #else
  1720. return ISC_FALSE;
  1721. #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
  1722. }
  1723. iface->num_mcast--;
  1724. if (iface->num_mcast <= 0) {
  1725. iface->num_mcast = 0;
  1726. iface->flags &= ~INT_MCASTOPEN;
  1727. }
  1728. return ISC_TRUE;
  1729. }
  1730. /*
  1731. * io_setbclient - open the broadcast client sockets
  1732. */
  1733. void
  1734. io_setbclient(void)
  1735. {
  1736. #ifdef OPEN_BCAST_SOCKET
  1737. struct interface *interf;
  1738. int nif = 0;
  1739. isc_boolean_t jstatus;
  1740. SOCKET fd;
  1741. set_reuseaddr(1);
  1742. for (interf = ISC_LIST_HEAD(inter_list);
  1743. interf != NULL;
  1744. interf = ISC_LIST_NEXT(interf, link)) {
  1745. if (interf->flags & INT_WILDCARD)
  1746. continue;
  1747. /* use only allowed addresses */
  1748. if (interf->ignore_packets == ISC_TRUE)
  1749. continue;
  1750. /* Only IPv4 addresses are valid for broadcast */
  1751. if (interf->sin.ss_family != AF_INET)
  1752. continue;
  1753. /* Is this a broadcast address? */
  1754. if (!(interf->flags & INT_BROADCAST))
  1755. continue;
  1756. /* Skip the loopback addresses */
  1757. if (interf->flags & INT_LOOPBACK)
  1758. continue;
  1759. /* Do we already have the broadcast address open? */
  1760. if (interf->flags & INT_BCASTOPEN) {
  1761. /* account for already open interfaces to aviod misleading warning below */
  1762. nif++;
  1763. continue;
  1764. }
  1765. /*
  1766. * Try to open the broadcast address
  1767. */
  1768. interf->family = AF_INET;
  1769. interf->bfd = open_socket(&interf->bcast,
  1770. INT_BROADCAST, 0, interf);
  1771. /*
  1772. * If we succeeded then we use it otherwise
  1773. * enable the underlying address
  1774. */
  1775. if (interf->bfd == INVALID_SOCKET) {
  1776. fd = interf->fd;
  1777. }
  1778. else {
  1779. fd = interf->bfd;
  1780. }
  1781. /* Enable Broadcast on socket */
  1782. jstatus = socket_broadcast_enable(interf, fd, &interf->sin);
  1783. if (jstatus == ISC_TRUE)
  1784. {
  1785. nif++;
  1786. netsyslog(LOG_INFO,"io_setbclient: Opened broadcast client on interface #%d %s, socket: %d",
  1787. interf->ifnum, interf->name, fd);
  1788. interf->addr_refid = addr2refid(&interf->sin);
  1789. }
  1790. }
  1791. set_reuseaddr(0);
  1792. #ifdef DEBUG
  1793. if (debug)
  1794. if (nif > 0)
  1795. printf("io_setbclient: Opened broadcast clients\n");
  1796. #endif
  1797. if (nif == 0)
  1798. netsyslog(LOG_ERR, "Unable to listen for broadcasts, no broadcast interfaces available");
  1799. #else
  1800. netsyslog(LOG_ERR, "io_setbclient: Broadcast Client disabled by build");
  1801. #endif
  1802. }
  1803. /*
  1804. * io_unsetbclient - close the broadcast client sockets
  1805. */
  1806. void
  1807. io_unsetbclient(void)
  1808. {
  1809. struct interface *interf;
  1810. isc_boolean_t lstatus;
  1811. for (interf = ISC_LIST_HEAD(inter_list);
  1812. interf != NULL;
  1813. interf = ISC_LIST_NEXT(interf, link))
  1814. {
  1815. if (interf->flags & INT_WILDCARD)
  1816. continue;
  1817. if (!(interf->flags & INT_BCASTOPEN))
  1818. continue;
  1819. lstatus = socket_broadcast_disable(interf, &interf->sin);
  1820. }
  1821. }
  1822. /*
  1823. * io_multicast_add() - add multicast group address
  1824. */
  1825. void
  1826. io_multicast_add(
  1827. struct sockaddr_storage addr
  1828. )
  1829. {
  1830. #ifdef MCAST
  1831. struct interface *interface;
  1832. #ifndef MULTICAST_NONEWSOCKET
  1833. struct interface *iface;
  1834. #endif
  1835. int lscope = 0;
  1836. /*
  1837. * Check to see if this is a multicast address
  1838. */
  1839. if (addr_ismulticast(&addr) == ISC_FALSE)
  1840. return;
  1841. /* If we already have it we can just return */
  1842. if (find_flagged_addr_in_list(&addr, INT_MCASTOPEN|INT_MCASTIF) != NULL)
  1843. {
  1844. netsyslog(LOG_INFO, "Duplicate request found for multicast address %s",
  1845. stoa(&addr));
  1846. return;
  1847. }
  1848. #ifndef MULTICAST_NONEWSOCKET
  1849. interface = new_interface(NULL);
  1850. /*
  1851. * Open a new socket for the multicast address
  1852. */
  1853. interface->sin.ss_family = addr.ss_family;
  1854. interface->family = addr.ss_family;
  1855. switch(addr.ss_family) {
  1856. case AF_INET:
  1857. memcpy(&(((struct sockaddr_in *)&interface->sin)->sin_addr),
  1858. &(((struct sockaddr_in*)&addr)->sin_addr),
  1859. sizeof(struct in_addr));
  1860. ((struct sockaddr_in*)&interface->sin)->sin_port = htons(NTP_PORT);
  1861. memset(&((struct sockaddr_in*)&interface->mask)->sin_addr.s_addr, 0xff, sizeof(struct in_addr));
  1862. break;
  1863. case AF_INET6:
  1864. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1865. memcpy(&(((struct sockaddr_in6 *)&interface->sin)->sin6_addr),
  1866. &((struct sockaddr_in6*)&addr)->sin6_addr,
  1867. sizeof(struct in6_addr));
  1868. ((struct sockaddr_in6*)&interface->sin)->sin6_port = htons(NTP_PORT);
  1869. #ifdef ISC_PLATFORM_HAVESCOPEID
  1870. ((struct sockaddr_in6*)&interface->sin)->sin6_scope_id = ((struct sockaddr_in6*)&addr)->sin6_scope_id;
  1871. #endif
  1872. memset(&((struct sockaddr_in6*)&interface->mask)->sin6_addr.s6_addr, 0xff, sizeof(struct in6_addr));
  1873. #endif
  1874. iface = findlocalcastinterface(&addr, INT_MULTICAST);
  1875. if (iface) {
  1876. # ifdef ISC_PLATFORM_HAVESCOPEID
  1877. lscope = ((struct sockaddr_in6*)&iface->sin)->sin6_scope_id;
  1878. # endif
  1879. DPRINTF(4, ("Found interface #%d %s, scope: %d for address %s\n", iface->ifnum, iface->name, lscope, stoa(&addr)));
  1880. }
  1881. break;
  1882. }
  1883. set_reuseaddr(1);
  1884. interface->bfd = INVALID_SOCKET;
  1885. interface->fd = open_socket(&interface->sin,
  1886. INT_MULTICAST, 0, interface);
  1887. if (interface->fd != INVALID_SOCKET)
  1888. {
  1889. interface->bfd = INVALID_SOCKET;
  1890. interface->ignore_packets = ISC_FALSE;
  1891. interface->flags |= INT_MCASTIF;
  1892. (void) strncpy(interface->name, "multicast",
  1893. sizeof(interface->name));
  1894. ((struct sockaddr_in*)&interface->mask)->sin_addr.s_addr =
  1895. htonl(~(u_int32)0);
  1896. DPRINT_INTERFACE(2, (interface, "multicast add ", "\n"));
  1897. /* socket_multicast_enable() will add this address to the addresslist */
  1898. add_interface(interface);
  1899. list_if_listening(interface, htons(NTP_PORT));
  1900. }
  1901. else
  1902. {
  1903. delete_interface(interface); /* re-use existing interface */
  1904. interface = NULL;
  1905. if (addr.ss_family == AF_INET)
  1906. interface = wildipv4;
  1907. else if (addr.ss_family == AF_INET6)
  1908. interface = wildipv6;
  1909. if (interface != NULL) {
  1910. /* HACK ! -- stuff in an address */
  1911. interface->bcast = addr;
  1912. netsyslog(LOG_ERR,
  1913. "...multicast address %s using wildcard interface #%d %s",
  1914. stoa(&addr), interface->ifnum, interface->name);
  1915. } else {
  1916. netsyslog(LOG_ERR,
  1917. "No multicast socket available to use for address %s",
  1918. stoa(&addr));
  1919. return;
  1920. }
  1921. }
  1922. #else
  1923. /*
  1924. * For the case where we can't use a separate socket
  1925. */
  1926. interface = findlocalcastinterface(&addr, INT_MULTICAST);
  1927. /*
  1928. * If we don't have a valid socket, just return
  1929. */
  1930. if (!interface)
  1931. {
  1932. netsyslog(LOG_ERR,
  1933. "Cannot add multicast address %s: Cannot find slot",
  1934. stoa(&addr));
  1935. return;
  1936. }
  1937. #endif
  1938. {
  1939. isc_boolean_t jstatus;
  1940. jstatus = socket_multicast_enable(interface, lscope, &addr);
  1941. if (jstatus == ISC_TRUE)
  1942. netsyslog(LOG_INFO, "Added Multicast Listener %s on interface #%d %s\n", stoa(&addr), interface->ifnum, interface->name);
  1943. else
  1944. netsyslog(LOG_ERR, "Failed to add Multicast Listener %s\n", stoa(&addr));
  1945. }
  1946. #else /* MCAST */
  1947. netsyslog(LOG_ERR,
  1948. "Cannot add multicast address %s: no Multicast support",
  1949. stoa(&addr));
  1950. #endif /* MCAST */
  1951. return;
  1952. }
  1953. /*
  1954. * io_multicast_del() - delete multicast group address
  1955. */
  1956. void
  1957. io_multicast_del(
  1958. struct sockaddr_storage addr
  1959. )
  1960. {
  1961. #ifdef MCAST
  1962. struct interface *interface;
  1963. isc_boolean_t lstatus;
  1964. /*
  1965. * Check to see if this is a multicast address
  1966. */
  1967. if (addr_ismulticast(&addr) == ISC_FALSE)
  1968. {
  1969. netsyslog(LOG_ERR,
  1970. "invalid multicast address %s", stoa(&addr));
  1971. return;
  1972. }
  1973. switch (addr.ss_family)
  1974. {
  1975. case AF_INET :
  1976. /*
  1977. * Disable reception of multicast packets
  1978. */
  1979. interface = find_flagged_addr_in_list(&addr, INT_MCASTOPEN);
  1980. while ( interface != NULL) {
  1981. lstatus = socket_multicast_disable(interface, &addr);
  1982. interface = find_flagged_addr_in_list(&addr, INT_MCASTOPEN);
  1983. }
  1984. break;
  1985. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  1986. case AF_INET6 :
  1987. /*
  1988. * Disable reception of multicast packets
  1989. */
  1990. for (interface = ISC_LIST_HEAD(inter_list);
  1991. interface != NULL;
  1992. interface = ISC_LIST_NEXT(interface, link))
  1993. {
  1994. if (interface->flags & INT_WILDCARD)
  1995. continue;
  1996. /* Be sure it's the correct family */
  1997. if (interface->sin.ss_family != AF_INET6)
  1998. continue;
  1999. if (!(interface->flags & INT_MCASTOPEN))
  2000. continue;
  2001. if (!(interface->fd < 0))
  2002. continue;
  2003. if (!SOCKCMP(&addr, &interface->sin))
  2004. continue;
  2005. lstatus = socket_multicast_disable(interface, &addr);
  2006. }
  2007. break;
  2008. #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
  2009. }/* switch */
  2010. delete_addr_from_list(&addr);
  2011. #else /* not MCAST */
  2012. netsyslog(LOG_ERR, "this function requires multicast kernel");
  2013. #endif /* not MCAST */
  2014. }
  2015. /*
  2016. * init_nonblocking_io() - set up descriptor to be non blocking
  2017. */
  2018. static void init_nonblocking_io(SOCKET fd)
  2019. {
  2020. /*
  2021. * set non-blocking,
  2022. */
  2023. #ifdef USE_FIONBIO
  2024. /* in vxWorks we use FIONBIO, but the others are defined for old systems, so
  2025. * all hell breaks loose if we leave them defined
  2026. */
  2027. #undef O_NONBLOCK
  2028. #undef FNDELAY
  2029. #undef O_NDELAY
  2030. #endif
  2031. #if defined(O_NONBLOCK) /* POSIX */
  2032. if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
  2033. {
  2034. netsyslog(LOG_ERR, "fcntl(O_NONBLOCK) fails on fd #%d: %m",
  2035. fd);
  2036. exit(1);
  2037. /*NOTREACHED*/
  2038. }
  2039. #elif defined(FNDELAY)
  2040. if (fcntl(fd, F_SETFL, FNDELAY) < 0)
  2041. {
  2042. netsyslog(LOG_ERR, "fcntl(FNDELAY) fails on fd #%d: %m",
  2043. fd);
  2044. exit(1);
  2045. /*NOTREACHED*/
  2046. }
  2047. #elif defined(O_NDELAY) /* generally the same as FNDELAY */
  2048. if (fcntl(fd, F_SETFL, O_NDELAY) < 0)
  2049. {
  2050. netsyslog(LOG_ERR, "fcntl(O_NDELAY) fails on fd #%d: %m",
  2051. fd);
  2052. exit(1);
  2053. /*NOTREACHED*/
  2054. }
  2055. #elif defined(FIONBIO)
  2056. {
  2057. int on = 1;
  2058. if (ioctl(fd,FIONBIO,&on) < 0)
  2059. {
  2060. netsyslog(LOG_ERR, "ioctl(FIONBIO) fails on fd #%d: %m",
  2061. fd);
  2062. exit(1);
  2063. /*NOTREACHED*/
  2064. }
  2065. }
  2066. #elif defined(FIOSNBIO)
  2067. if (ioctl(fd,FIOSNBIO,&on) < 0)
  2068. {
  2069. netsyslog(LOG_ERR, "ioctl(FIOSNBIO) fails on fd #%d: %m",
  2070. fd);
  2071. exit(1);
  2072. /*NOTREACHED*/
  2073. }
  2074. #else
  2075. # include "Bletch: Need non-blocking I/O!"
  2076. #endif
  2077. }
  2078. /*
  2079. * open_socket - open a socket, returning the file descriptor
  2080. */
  2081. static SOCKET
  2082. open_socket(
  2083. struct sockaddr_storage *addr,
  2084. int flags,
  2085. int turn_off_reuse,
  2086. struct interface *interf
  2087. )
  2088. {
  2089. int errval;
  2090. SOCKET fd;
  2091. /*
  2092. * int is OK for REUSEADR per
  2093. * http://www.kohala.com/start/mcast.api.txt
  2094. */
  2095. int on = 1;
  2096. int off = 0;
  2097. #if defined(IPTOS_LOWDELAY) && defined(IPPROTO_IP) && defined(IP_TOS)
  2098. int tos;
  2099. #endif /* IPTOS_LOWDELAY && IPPROTO_IP && IP_TOS */
  2100. if ((addr->ss_family == AF_INET6) && (isc_net_probeipv6() != ISC_R_SUCCESS))
  2101. return (INVALID_SOCKET);
  2102. /* create a datagram (UDP) socket */
  2103. fd = socket(addr->ss_family, SOCK_DGRAM, 0);
  2104. if (INVALID_SOCKET == fd) {
  2105. #ifndef SYS_WINNT
  2106. errval = errno;
  2107. #else
  2108. errval = WSAGetLastError();
  2109. #endif
  2110. netsyslog(LOG_ERR,
  2111. "socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
  2112. (addr->ss_family == AF_INET6) ? "6" : "",
  2113. stoa(addr));
  2114. if (errval == EPROTONOSUPPORT ||
  2115. errval == EAFNOSUPPORT ||
  2116. errval == EPFNOSUPPORT)
  2117. return (INVALID_SOCKET);
  2118. msyslog(LOG_ERR, "unexpected error code %d (not PROTONOSUPPORT|AFNOSUPPORT|FPNOSUPPORT) - exiting", errval);
  2119. exit(1);
  2120. /*NOTREACHED*/
  2121. }
  2122. #ifdef SYS_WINNT
  2123. connection_reset_fix(fd, addr);
  2124. #endif
  2125. /*
  2126. * Fixup the file descriptor for some systems
  2127. * See bug #530 for details of the issue.
  2128. */
  2129. fd = move_fd(fd);
  2130. /*
  2131. * set SO_REUSEADDR since we will be binding the same port
  2132. * number on each interface according to turn_off_reuse.
  2133. * This is undesirable on Windows versions starting with
  2134. * Windows XP (numeric version 5.1).
  2135. */
  2136. #ifdef SYS_WINNT
  2137. if (isc_win32os_versioncheck(5, 1, 0, 0) < 0) /* before 5.1 */
  2138. #endif
  2139. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
  2140. (char *)(turn_off_reuse
  2141. ? &off
  2142. : &on),
  2143. sizeof(on))) {
  2144. netsyslog(LOG_ERR, "setsockopt SO_REUSEADDR %s"
  2145. " fails for address %s: %m",
  2146. turn_off_reuse
  2147. ? "off"
  2148. : "on",
  2149. stoa(addr));
  2150. closesocket(fd);
  2151. return INVALID_SOCKET;
  2152. }
  2153. #ifdef SO_EXCLUSIVEADDRUSE
  2154. /*
  2155. * setting SO_EXCLUSIVEADDRUSE on the wildcard we open
  2156. * first will cause more specific binds to fail.
  2157. */
  2158. if (!(interf->flags & INT_WILDCARD))
  2159. set_excladdruse(fd);
  2160. #endif
  2161. /*
  2162. * IPv4 specific options go here
  2163. */
  2164. if (addr->ss_family == AF_INET) {
  2165. #if defined(IPTOS_LOWDELAY) && defined(IPPROTO_IP) && defined(IP_TOS)
  2166. /* set IP_TOS to minimize packet delay */
  2167. tos = IPTOS_LOWDELAY;
  2168. if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(tos)) < 0)
  2169. {
  2170. netsyslog(LOG_ERR, "setsockopt IPTOS_LOWDELAY on fails on address %s: %m",
  2171. stoa(addr));
  2172. }
  2173. #endif /* IPTOS_LOWDELAY && IPPROTO_IP && IP_TOS */
  2174. }
  2175. /*
  2176. * IPv6 specific options go here
  2177. */
  2178. if (addr->ss_family == AF_INET6) {
  2179. #if defined(IPV6_V6ONLY)
  2180. if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
  2181. (char*)&on, sizeof(on)))
  2182. {
  2183. netsyslog(LOG_ERR, "setsockopt IPV6_V6ONLY on fails on address %s: %m",
  2184. stoa(addr));
  2185. }
  2186. #endif /* IPV6_V6ONLY */
  2187. #if defined(IPV6_BINDV6ONLY)
  2188. if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
  2189. (char*)&on, sizeof(on)))
  2190. {
  2191. netsyslog(LOG_ERR,
  2192. "setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
  2193. stoa(addr));
  2194. }
  2195. #endif /* IPV6_BINDV6ONLY */
  2196. }
  2197. #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
  2198. /*
  2199. * some OSes don't allow binding to more specific
  2200. * addresses if a wildcard address already bound
  2201. * to the port and SO_REUSEADDR is not set
  2202. */
  2203. if (!is_wildcard_addr(addr)) {
  2204. set_wildcard_reuse(addr->ss_family, 1);
  2205. }
  2206. #endif
  2207. /*
  2208. * bind the local address.
  2209. */
  2210. errval = bind(fd, (struct sockaddr *)addr, SOCKLEN(addr));
  2211. #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
  2212. /*
  2213. * some OSes don't allow binding to more specific
  2214. * addresses if a wildcard address already bound
  2215. * to the port and REUSE_ADDR is not set
  2216. */
  2217. if (!is_wildcard_addr(addr)) {
  2218. set_wildcard_reuse(addr->ss_family, 0);
  2219. }
  2220. #endif
  2221. if (errval < 0) {
  2222. /*
  2223. * Don't log this under all conditions
  2224. */
  2225. if (turn_off_reuse == 0
  2226. #ifdef DEBUG
  2227. || debug > 1
  2228. #endif
  2229. ) {
  2230. if (addr->ss_family == AF_INET)
  2231. netsyslog(LOG_ERR,
  2232. "bind() fd %d, family AF_INET, port %d, addr %s, in_classd=%d flags=0x%x fails: %m",
  2233. fd, (int)ntohs(((struct sockaddr_in*)addr)->sin_port),
  2234. stoa(addr),
  2235. IN_CLASSD(ntohl(((struct sockaddr_in*)addr)->sin_addr.s_addr)),
  2236. flags);
  2237. #ifdef INCLUDE_IPV6_SUPPORT
  2238. else if (addr->ss_family == AF_INET6)
  2239. netsyslog(LOG_ERR,
  2240. "bind() fd %d, family AF_INET6, port %d, scope %d, addr %s, mcast=%d flags=0x%x fails: %m",
  2241. fd, (int)ntohs(((struct sockaddr_in6*)addr)->sin6_port),
  2242. # ifdef ISC_PLATFORM_HAVESCOPEID
  2243. ((struct sockaddr_in6*)addr)->sin6_scope_id
  2244. # else
  2245. -1
  2246. # endif
  2247. , stoa(addr),
  2248. IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)addr)->sin6_addr),
  2249. flags);
  2250. #endif
  2251. }
  2252. closesocket(fd);
  2253. return INVALID_SOCKET;
  2254. }
  2255. #ifdef HAVE_TIMESTAMP
  2256. {
  2257. if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
  2258. (char*)&on, sizeof(on)))
  2259. {
  2260. netsyslog(LOG_DEBUG,
  2261. "setsockopt SO_TIMESTAMP on fails on address %s: %m",
  2262. stoa(addr));
  2263. }
  2264. #ifdef DEBUG
  2265. else
  2266. {
  2267. DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n", fd, stoa(addr)));
  2268. }
  2269. #endif
  2270. }
  2271. #endif
  2272. DPRINTF(4, ("bind() fd %d, family %d, port %d, addr %s, flags=0x%x\n",
  2273. fd,
  2274. addr->ss_family,
  2275. (int)ntohs(((struct sockaddr_in*)addr)->sin_port),
  2276. stoa(addr),
  2277. interf->flags));
  2278. init_nonblocking_io(fd);
  2279. #ifdef HAVE_SIGNALED_IO
  2280. init_socket_sig(fd);
  2281. #endif /* not HAVE_SIGNALED_IO */
  2282. add_fd_to_list(fd, FD_TYPE_SOCKET);
  2283. #if !defined(SYS_WINNT) && !defined(VMS)
  2284. DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
  2285. fcntl(fd, F_GETFL, 0)));
  2286. #endif /* SYS_WINNT || VMS */
  2287. #if defined (HAVE_IO_COMPLETION_PORT)
  2288. /*
  2289. * Add the socket to the completion port
  2290. */
  2291. if (io_completion_port_add_socket(fd, interf))
  2292. {
  2293. msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
  2294. exit(1);
  2295. }
  2296. #endif
  2297. return fd;
  2298. }
  2299. /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
  2300. /*
  2301. * sendpkt - send a packet to the specified destination. Maintain a
  2302. * send error cache so that only the first consecutive error for a
  2303. * destination is logged.
  2304. */
  2305. void
  2306. sendpkt(
  2307. struct sockaddr_storage *dest,
  2308. struct interface *inter,
  2309. int ttl,
  2310. struct pkt *pkt,
  2311. int len
  2312. )
  2313. {
  2314. int cc, slot;
  2315. /*
  2316. * Send error caches. Empty slots have port == 0
  2317. * Set ERRORCACHESIZE to 0 to disable
  2318. */
  2319. struct cache {
  2320. u_short port;
  2321. struct in_addr addr;
  2322. };
  2323. #ifdef INCLUDE_IPV6_SUPPORT
  2324. struct cache6 {
  2325. u_short port;
  2326. struct in6_addr addr;
  2327. };
  2328. #endif /* INCLUDE_IPV6_SUPPORT */
  2329. #ifndef ERRORCACHESIZE
  2330. #define ERRORCACHESIZE 8
  2331. #endif
  2332. #if ERRORCACHESIZE > 0
  2333. static struct cache badaddrs[ERRORCACHESIZE];
  2334. #ifdef INCLUDE_IPV6_SUPPORT
  2335. static struct cache6 badaddrs6[ERRORCACHESIZE];
  2336. #endif /* INCLUDE_IPV6_SUPPORT */
  2337. #else
  2338. #define badaddrs ((struct cache *)0) /* Only used in empty loops! */
  2339. #ifdef INCLUDE_IPV6_SUPPORT
  2340. #define badaddrs6 ((struct cache6 *)0) /* Only used in empty loops! */
  2341. #endif /* INCLUDE_IPV6_SUPPORT */
  2342. #endif
  2343. #ifdef DEBUG
  2344. if (debug > 1)
  2345. {
  2346. if (inter != NULL)
  2347. {
  2348. printf("%ssendpkt(fd=%d dst=%s, src=%s, ttl=%d, len=%d)\n",
  2349. (ttl > 0) ? "\tMCAST\t***** " : "",
  2350. inter->fd, stoa(dest),
  2351. stoa(&inter->sin), ttl, len);
  2352. }
  2353. else
  2354. {
  2355. printf("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
  2356. (ttl > 0) ? "\tMCAST\t***** " : "",
  2357. stoa(dest),
  2358. ttl, len);
  2359. }
  2360. }
  2361. #endif
  2362. if (inter == NULL) /* unbound peer - drop request and wait for better network conditions */
  2363. return;
  2364. #ifdef MCAST
  2365. /*
  2366. * for the moment we use the bcast option to set multicast ttl
  2367. */
  2368. if (ttl > 0 && ttl != inter->last_ttl) {
  2369. /*
  2370. * set the multicast ttl for outgoing packets
  2371. */
  2372. int rtc;
  2373. switch (inter->sin.ss_family) {
  2374. case AF_INET :
  2375. {
  2376. u_char mttl = (u_char) ttl;
  2377. rtc = setsockopt(inter->fd, IPPROTO_IP, IP_MULTICAST_TTL,
  2378. (const void *) &mttl, sizeof(mttl));
  2379. break;
  2380. }
  2381. #ifdef INCLUDE_IPV6_SUPPORT
  2382. case AF_INET6 :
  2383. {
  2384. u_int ittl = (u_char) ttl;
  2385. rtc = setsockopt(inter->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  2386. (const void *) &ittl, sizeof(ittl));
  2387. break;
  2388. }
  2389. #endif /* INCLUDE_IPV6_SUPPORT */
  2390. default: /* just NOP if not supported */
  2391. rtc = 0;
  2392. break;
  2393. }
  2394. if (rtc != 0) {
  2395. netsyslog(LOG_ERR, "setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
  2396. stoa(&inter->sin));
  2397. }
  2398. else
  2399. inter->last_ttl = ttl;
  2400. }
  2401. #endif /* MCAST */
  2402. for (slot = ERRORCACHESIZE; --slot >= 0; )
  2403. if(dest->ss_family == AF_INET) {
  2404. if (badaddrs[slot].port == SRCPORT(dest) &&
  2405. badaddrs[slot].addr.s_addr == ((struct sockaddr_in*)dest)->sin_addr.s_addr)
  2406. break;
  2407. }
  2408. #ifdef INCLUDE_IPV6_SUPPORT
  2409. else if (dest->ss_family == AF_INET6) {
  2410. if (badaddrs6[slot].port == SRCPORT(dest) &&
  2411. !memcmp(&badaddrs6[slot].addr, &((struct sockaddr_in6*)dest)->sin6_addr, sizeof(struct in6_addr)))
  2412. break;
  2413. }
  2414. #endif /* INCLUDE_IPV6_SUPPORT */
  2415. #if defined(HAVE_IO_COMPLETION_PORT)
  2416. cc = io_completion_port_sendto(inter, pkt, len, dest);
  2417. if (cc != ERROR_SUCCESS)
  2418. #else
  2419. #ifdef SIM
  2420. cc = srvr_rply(&ntp_node, dest, inter, pkt);
  2421. #else /* SIM */
  2422. cc = sendto(inter->fd, (char *)pkt, (unsigned int)len, 0, (struct sockaddr *)dest,
  2423. SOCKLEN(dest));
  2424. #endif /* SIM */
  2425. if (cc == -1)
  2426. #endif
  2427. {
  2428. inter->notsent++;
  2429. packets_notsent++;
  2430. #if defined(HAVE_IO_COMPLETION_PORT)
  2431. if (cc != WSAEWOULDBLOCK && cc != WSAENOBUFS && slot < 0)
  2432. #else
  2433. if (errno != EWOULDBLOCK && errno != ENOBUFS && slot < 0)
  2434. #endif
  2435. {
  2436. /*
  2437. * Remember this, if there's an empty slot
  2438. */
  2439. switch (dest->ss_family) {
  2440. case AF_INET :
  2441. for (slot = ERRORCACHESIZE; --slot >= 0; )
  2442. if (badaddrs[slot].port == 0)
  2443. {
  2444. badaddrs[slot].port = SRCPORT(dest);
  2445. badaddrs[slot].addr = ((struct sockaddr_in*)dest)->sin_addr;
  2446. break;
  2447. }
  2448. break;
  2449. #ifdef INCLUDE_IPV6_SUPPORT
  2450. case AF_INET6 :
  2451. for (slot = ERRORCACHESIZE; --slot >= 0; )
  2452. if (badaddrs6[slot].port == 0)
  2453. {
  2454. badaddrs6[slot].port = SRCPORT(dest);
  2455. badaddrs6[slot].addr = ((struct sockaddr_in6*)dest)->sin6_addr;
  2456. break;
  2457. }
  2458. break;
  2459. #endif /* INCLUDE_IPV6_SUPPORT */
  2460. default: /* don't care if not supported */
  2461. break;
  2462. }
  2463. netsyslog(LOG_ERR, "sendto(%s) (fd=%d): %m",
  2464. stoa(dest), inter->fd);
  2465. }
  2466. }
  2467. else
  2468. {
  2469. inter->sent++;
  2470. packets_sent++;
  2471. /*
  2472. * He's not bad any more
  2473. */
  2474. if (slot >= 0)
  2475. {
  2476. netsyslog(LOG_INFO, "Connection re-established to %s", stoa(dest));
  2477. switch (dest->ss_family) {
  2478. case AF_INET :
  2479. badaddrs[slot].port = 0;
  2480. break;
  2481. #ifdef INCLUDE_IPV6_SUPPORT
  2482. case AF_INET6 :
  2483. badaddrs6[slot].port = 0;
  2484. break;
  2485. #endif /* INCLUDE_IPV6_SUPPORT */
  2486. default: /* don't care if not supported */
  2487. break;
  2488. }
  2489. }
  2490. }
  2491. }
  2492. #if !defined(HAVE_IO_COMPLETION_PORT)
  2493. /*
  2494. * fdbits - generate ascii representation of fd_set (FAU debug support)
  2495. * HFDF format - highest fd first.
  2496. */
  2497. static char *
  2498. fdbits(
  2499. int count,
  2500. fd_set *set
  2501. )
  2502. {
  2503. static char buffer[256];
  2504. char * buf = buffer;
  2505. count = (count < 256) ? count : 255;
  2506. while (count >= 0)
  2507. {
  2508. *buf++ = FD_ISSET(count, set) ? '#' : '-';
  2509. count--;
  2510. }
  2511. *buf = '\0';
  2512. return buffer;
  2513. }
  2514. /*
  2515. * Routine to read the refclock packets for a specific interface
  2516. * Return the number of bytes read. That way we know if we should
  2517. * read it again or go on to the next one if no bytes returned
  2518. */
  2519. static inline int
  2520. read_refclock_packet(SOCKET fd, struct refclockio *rp, l_fp ts)
  2521. {
  2522. int i;
  2523. int buflen;
  2524. register struct recvbuf *rb;
  2525. rb = get_free_recv_buffer();
  2526. if (rb == NULL)
  2527. {
  2528. /*
  2529. * No buffer space available - just drop the packet
  2530. */
  2531. char buf[RX_BUFF_SIZE];
  2532. buflen = read(fd, buf, sizeof buf);
  2533. packets_dropped++;
  2534. return (buflen);
  2535. }
  2536. i = (rp->datalen == 0
  2537. || rp->datalen > sizeof(rb->recv_space))
  2538. ? sizeof(rb->recv_space) : rp->datalen;
  2539. buflen = read(fd, (char *)&rb->recv_space, (unsigned)i);
  2540. if (buflen < 0)
  2541. {
  2542. if (errno != EINTR && errno != EAGAIN) {
  2543. netsyslog(LOG_ERR, "clock read fd %d: %m", fd);
  2544. }
  2545. freerecvbuf(rb);
  2546. return (buflen);
  2547. }
  2548. /*
  2549. * Got one. Mark how and when it got here,
  2550. * put it on the full list and do bookkeeping.
  2551. */
  2552. rb->recv_length = buflen;
  2553. rb->recv_srcclock = rp->srcclock;
  2554. rb->dstadr = 0;
  2555. rb->fd = fd;
  2556. rb->recv_time = ts;
  2557. rb->receiver = rp->clock_recv;
  2558. if (rp->io_input)
  2559. {
  2560. /*
  2561. * have direct input routine for refclocks
  2562. */
  2563. if (rp->io_input(rb) == 0)
  2564. {
  2565. /*
  2566. * data was consumed - nothing to pass up
  2567. * into block input machine
  2568. */
  2569. freerecvbuf(rb);
  2570. return (buflen);
  2571. }
  2572. }
  2573. add_full_recv_buffer(rb);
  2574. rp->recvcount++;
  2575. packets_received++;
  2576. return (buflen);
  2577. }
  2578. #ifdef HAVE_TIMESTAMP
  2579. /*
  2580. * extract timestamps from control message buffer
  2581. */
  2582. static l_fp
  2583. fetch_timestamp(struct recvbuf *rb, struct msghdr *msghdr, l_fp ts)
  2584. {
  2585. #ifdef USE_TIMESTAMP_CMSG
  2586. struct cmsghdr *cmsghdr;
  2587. cmsghdr = CMSG_FIRSTHDR(msghdr);
  2588. while (cmsghdr != NULL) {
  2589. switch (cmsghdr->cmsg_type)
  2590. {
  2591. case SCM_TIMESTAMP:
  2592. {
  2593. struct timeval *tvp = (struct timeval *)CMSG_DATA(cmsghdr);
  2594. double dtemp;
  2595. l_fp nts;
  2596. DPRINTF(4, ("fetch_timestamp: system network time stamp: %ld.%06ld\n", tvp->tv_sec, tvp->tv_usec));
  2597. nts.l_i = tvp->tv_sec + JAN_1970;
  2598. dtemp = tvp->tv_usec / 1e6;
  2599. /* fuzz lower bits not covered by precision */
  2600. if (sys_precision != 0)
  2601. dtemp += (ntp_random() / FRAC - .5) / (1 <<
  2602. -sys_precision);
  2603. nts.l_uf = (u_int32)(dtemp*FRAC);
  2604. #ifdef DEBUG_TIMING
  2605. {
  2606. l_fp dts = ts;
  2607. L_SUB(&dts, &nts);
  2608. collect_timing(rb, "input processing delay", 1, &dts);
  2609. DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. prec fuzz)\n", lfptoa(&dts, 9)));
  2610. }
  2611. #endif
  2612. ts = nts; /* network time stamp */
  2613. break;
  2614. }
  2615. default:
  2616. DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n", cmsghdr->cmsg_type));
  2617. break;
  2618. }
  2619. cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
  2620. }
  2621. #endif
  2622. return ts;
  2623. }
  2624. #endif
  2625. /*
  2626. * Routine to read the network NTP packets for a specific interface
  2627. * Return the number of bytes read. That way we know if we should
  2628. * read it again or go on to the next one if no bytes returned
  2629. */
  2630. static inline int
  2631. read_network_packet(SOCKET fd, struct interface *itf, l_fp ts)
  2632. {
  2633. GETSOCKNAME_SOCKLEN_TYPE fromlen;
  2634. int buflen;
  2635. register struct recvbuf *rb;
  2636. #ifdef HAVE_TIMESTAMP
  2637. struct msghdr msghdr;
  2638. struct iovec iovec;
  2639. char control[TIMESTAMP_CTLMSGBUF_SIZE]; /* pick up control messages */
  2640. #endif
  2641. /*
  2642. * Get a buffer and read the frame. If we
  2643. * haven't got a buffer, or this is received
  2644. * on a disallowed socket, just dump the
  2645. * packet.
  2646. */
  2647. rb = get_free_recv_buffer();
  2648. if (rb == NULL || itf->ignore_packets == ISC_TRUE)
  2649. {
  2650. char buf[RX_BUFF_SIZE];
  2651. struct sockaddr_storage from;
  2652. if (rb != NULL)
  2653. freerecvbuf(rb);
  2654. fromlen = sizeof(from);
  2655. buflen = recvfrom(fd, buf, sizeof(buf), 0,
  2656. (struct sockaddr*)&from, &fromlen);
  2657. DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
  2658. (itf->ignore_packets == ISC_TRUE) ? "ignore" : "drop",
  2659. free_recvbuffs(), fd,
  2660. stoa(&from)));
  2661. if (itf->ignore_packets == ISC_TRUE)
  2662. packets_ignored++;
  2663. else
  2664. packets_dropped++;
  2665. return (buflen);
  2666. }
  2667. fromlen = sizeof(struct sockaddr_storage);
  2668. #ifndef HAVE_TIMESTAMP
  2669. rb->recv_length = recvfrom(fd,
  2670. (char *)&rb->recv_space,
  2671. sizeof(rb->recv_space), 0,
  2672. (struct sockaddr *)&rb->recv_srcadr,
  2673. &fromlen);
  2674. #else
  2675. iovec.iov_base = (void *)&rb->recv_space;
  2676. iovec.iov_len = sizeof(rb->recv_space);
  2677. msghdr.msg_name = (void *)&rb->recv_srcadr;
  2678. msghdr.msg_namelen = sizeof(rb->recv_srcadr);
  2679. msghdr.msg_iov = &iovec;
  2680. msghdr.msg_iovlen = 1;
  2681. msghdr.msg_control = (void *)&control;
  2682. msghdr.msg_controllen = sizeof(control);
  2683. msghdr.msg_flags = 0;
  2684. rb->recv_length = recvmsg(fd, &msghdr, 0);
  2685. #endif
  2686. buflen = rb->recv_length;
  2687. if (buflen == 0 || (buflen == -1 &&
  2688. (errno==EWOULDBLOCK
  2689. #ifdef EAGAIN
  2690. || errno==EAGAIN
  2691. #endif
  2692. ))) {
  2693. freerecvbuf(rb);
  2694. return (buflen);
  2695. }
  2696. else if (buflen < 0)
  2697. {
  2698. netsyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
  2699. stoa(&rb->recv_srcadr), fd);
  2700. DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n", fd));
  2701. freerecvbuf(rb);
  2702. return (buflen);
  2703. }
  2704. #ifdef DEBUG
  2705. if (debug > 2) {
  2706. if(rb->recv_srcadr.ss_family == AF_INET)
  2707. printf("read_network_packet: fd=%d length %d from %08lx %s\n",
  2708. fd, buflen,
  2709. (u_long)ntohl(((struct sockaddr_in*)&rb->recv_srcadr)->sin_addr.s_addr) &
  2710. 0x00000000ffffffff,
  2711. stoa(&rb->recv_srcadr));
  2712. else
  2713. printf("read_network_packet: fd=%d length %d from %s\n",
  2714. fd, buflen,
  2715. stoa(&rb->recv_srcadr));
  2716. }
  2717. #endif
  2718. /*
  2719. * Got one. Mark how and when it got here,
  2720. * put it on the full list and do bookkeeping.
  2721. */
  2722. rb->dstadr = itf;
  2723. rb->fd = fd;
  2724. #ifdef HAVE_TIMESTAMP
  2725. ts = fetch_timestamp(rb, &msghdr, ts); /* pick up a network time stamp if possible */
  2726. #endif
  2727. rb->recv_time = ts;
  2728. rb->receiver = receive;
  2729. add_full_recv_buffer(rb);
  2730. itf->received++;
  2731. packets_received++;
  2732. return (buflen);
  2733. }
  2734. /*
  2735. * input_handler - receive packets asynchronously
  2736. */
  2737. void
  2738. input_handler(
  2739. l_fp *cts
  2740. )
  2741. {
  2742. int buflen;
  2743. int n;
  2744. int doing;
  2745. SOCKET fd;
  2746. struct timeval tvzero;
  2747. l_fp ts; /* Timestamp at BOselect() gob */
  2748. #ifdef DEBUG_TIMING
  2749. l_fp ts_e; /* Timestamp at EOselect() gob */
  2750. #endif
  2751. fd_set fds;
  2752. int select_count = 0;
  2753. struct interface *interface;
  2754. #if defined(HAS_ROUTING_SOCKET)
  2755. struct asyncio_reader *asyncio_reader;
  2756. #endif
  2757. handler_calls++;
  2758. /*
  2759. * If we have something to do, freeze a timestamp.
  2760. * See below for the other cases (nothing (left) to do or error)
  2761. */
  2762. ts = *cts;
  2763. /*
  2764. * Do a poll to see who has data
  2765. */
  2766. fds = activefds;
  2767. tvzero.tv_sec = tvzero.tv_usec = 0;
  2768. n = select(maxactivefd+1, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
  2769. /*
  2770. * If there are no packets waiting just return
  2771. */
  2772. if (n < 0)
  2773. {
  2774. int err = errno;
  2775. /*
  2776. * extended FAU debugging output
  2777. */
  2778. if (err != EINTR)
  2779. netsyslog(LOG_ERR,
  2780. "select(%d, %s, 0L, 0L, &0.0) error: %m",
  2781. maxactivefd+1,
  2782. fdbits(maxactivefd, &activefds));
  2783. if (err == EBADF) {
  2784. int j, b;
  2785. fds = activefds;
  2786. for (j = 0; j <= maxactivefd; j++)
  2787. if ((FD_ISSET(j, &fds) && (read(j, &b, 0) == -1)))
  2788. netsyslog(LOG_ERR, "Bad file descriptor %d", j);
  2789. }
  2790. return;
  2791. }
  2792. else if (n == 0)
  2793. return;
  2794. ++handler_pkts;
  2795. #ifdef REFCLOCK
  2796. /*
  2797. * Check out the reference clocks first, if any
  2798. */
  2799. if (refio != NULL)
  2800. {
  2801. register struct refclockio *rp;
  2802. for (rp = refio; rp != NULL; rp = rp->next)
  2803. {
  2804. fd = rp->fd;
  2805. if (FD_ISSET(fd, &fds))
  2806. {
  2807. do {
  2808. ++select_count;
  2809. buflen = read_refclock_packet(fd, rp, ts);
  2810. } while (buflen > 0);
  2811. } /* End if (FD_ISSET(fd, &fds)) */
  2812. } /* End for (rp = refio; rp != 0 && n > 0; rp = rp->next) */
  2813. } /* End if (refio != 0) */
  2814. #endif /* REFCLOCK */
  2815. /*
  2816. * Loop through the interfaces looking for data to read.
  2817. */
  2818. for (interface = ISC_LIST_TAIL(inter_list);
  2819. interface != NULL;
  2820. interface = ISC_LIST_PREV(interface, link))
  2821. {
  2822. for (doing = 0; (doing < 2); doing++)
  2823. {
  2824. if (doing == 0)
  2825. {
  2826. fd = interface->fd;
  2827. }
  2828. else
  2829. {
  2830. if (!(interface->flags & INT_BCASTOPEN))
  2831. break;
  2832. fd = interface->bfd;
  2833. }
  2834. if (fd < 0) continue;
  2835. if (FD_ISSET(fd, &fds))
  2836. {
  2837. do {
  2838. ++select_count;
  2839. buflen = read_network_packet(fd, interface, ts);
  2840. } while (buflen > 0);
  2841. }
  2842. /* Check more interfaces */
  2843. }
  2844. }
  2845. #ifdef HAS_ROUTING_SOCKET
  2846. /*
  2847. * scan list of asyncio readers - currently only used for routing sockets
  2848. */
  2849. asyncio_reader = ISC_LIST_TAIL(asyncio_reader_list);
  2850. while (asyncio_reader != NULL)
  2851. {
  2852. struct asyncio_reader *next = ISC_LIST_PREV(asyncio_reader, link);
  2853. if (FD_ISSET(asyncio_reader->fd, &fds)) {
  2854. ++select_count;
  2855. asyncio_reader->receiver(asyncio_reader);
  2856. }
  2857. asyncio_reader = next;
  2858. }
  2859. #endif /* HAS_ROUTING_SOCKET */
  2860. /*
  2861. * Done everything from that select.
  2862. */
  2863. /*
  2864. * If nothing to do, just return.
  2865. * If an error occurred, complain and return.
  2866. */
  2867. if (select_count == 0) /* We really had nothing to do */
  2868. {
  2869. #ifdef DEBUG
  2870. if (debug)
  2871. netsyslog(LOG_DEBUG, "input_handler: select() returned 0");
  2872. #endif
  2873. return;
  2874. }
  2875. /* We've done our work */
  2876. #ifdef DEBUG_TIMING
  2877. get_systime(&ts_e);
  2878. /*
  2879. * (ts_e - ts) is the amount of time we spent
  2880. * processing this gob of file descriptors. Log
  2881. * it.
  2882. */
  2883. L_SUB(&ts_e, &ts);
  2884. collect_timing(NULL, "input handler", 1, &ts_e);
  2885. if (debug > 3)
  2886. netsyslog(LOG_INFO, "input_handler: Processed a gob of fd's in %s msec", lfptoms(&ts_e, 6));
  2887. #endif
  2888. /* just bail. */
  2889. return;
  2890. }
  2891. #endif
  2892. /*
  2893. * findinterface - find local interface corresponding to address
  2894. */
  2895. struct interface *
  2896. findinterface(
  2897. struct sockaddr_storage *addr
  2898. )
  2899. {
  2900. struct interface *interface;
  2901. interface = findlocalinterface(addr, INT_WILDCARD);
  2902. if (interface == NULL)
  2903. {
  2904. DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
  2905. stoa(addr)));
  2906. return (ANY_INTERFACE_CHOOSE(addr));
  2907. }
  2908. else
  2909. {
  2910. DPRINTF(4, ("Found interface #%d %s for address %s\n",
  2911. interface->ifnum, interface->name, stoa(addr)));
  2912. return (interface);
  2913. }
  2914. }
  2915. /*
  2916. * findlocalinterface - find local interface index corresponding to address
  2917. *
  2918. * This code attempts to find the local sending address for an outgoing
  2919. * address by connecting a new socket to destinationaddress:NTP_PORT
  2920. * and reading the sockname of the resulting connect.
  2921. * the complicated sequence simulates the routing table lookup
  2922. * for to first hop without duplicating any of the routing logic into
  2923. * ntpd. preferably we would have used an API call - but its not there -
  2924. * so this is the best we can do here short of duplicating to entire routing
  2925. * logic in ntpd which would be a silly and really unportable thing to do.
  2926. *
  2927. */
  2928. static struct interface *
  2929. findlocalinterface(
  2930. struct sockaddr_storage *addr,
  2931. int flags
  2932. )
  2933. {
  2934. SOCKET s;
  2935. int rtn;
  2936. struct sockaddr_storage saddr;
  2937. GETSOCKNAME_SOCKLEN_TYPE saddrlen = SOCKLEN(addr);
  2938. struct interface *iface;
  2939. DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
  2940. stoa(addr)));
  2941. memset(&saddr, 0, sizeof(saddr));
  2942. saddr.ss_family = addr->ss_family;
  2943. if(addr->ss_family == AF_INET) {
  2944. memcpy(&((struct sockaddr_in*)&saddr)->sin_addr, &((struct sockaddr_in*)addr)->sin_addr, sizeof(struct in_addr));
  2945. ((struct sockaddr_in*)&saddr)->sin_port = htons(NTP_PORT);
  2946. }
  2947. #ifdef INCLUDE_IPV6_SUPPORT
  2948. else if(addr->ss_family == AF_INET6) {
  2949. memcpy(&((struct sockaddr_in6*)&saddr)->sin6_addr, &((struct sockaddr_in6*)addr)->sin6_addr, sizeof(struct in6_addr));
  2950. ((struct sockaddr_in6*)&saddr)->sin6_port = htons(NTP_PORT);
  2951. # ifdef ISC_PLATFORM_HAVESCOPEID
  2952. ((struct sockaddr_in6*)&saddr)->sin6_scope_id = ((struct sockaddr_in6*)addr)->sin6_scope_id;
  2953. # endif
  2954. }
  2955. #endif
  2956. s = socket(addr->ss_family, SOCK_DGRAM, 0);
  2957. if (s == INVALID_SOCKET)
  2958. return NULL;
  2959. rtn = connect(s, (struct sockaddr *)&saddr, SOCKLEN(&saddr));
  2960. #ifndef SYS_WINNT
  2961. if (rtn < 0)
  2962. #else
  2963. if (rtn == SOCKET_ERROR)
  2964. #endif
  2965. {
  2966. closesocket(s);
  2967. return NULL;
  2968. }
  2969. rtn = getsockname(s, (struct sockaddr *)&saddr, &saddrlen);
  2970. closesocket(s);
  2971. #ifndef SYS_WINNT
  2972. if (rtn < 0)
  2973. #else
  2974. if (rtn == SOCKET_ERROR)
  2975. #endif
  2976. return NULL;
  2977. DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n", stoa(addr), stoa(&saddr)));
  2978. iface = getinterface(&saddr, flags);
  2979. /* Don't both with ignore interfaces */
  2980. if (iface != NULL && iface->ignore_packets == ISC_TRUE)
  2981. {
  2982. return NULL;
  2983. }
  2984. else
  2985. {
  2986. return iface;
  2987. }
  2988. }
  2989. /*
  2990. * fetch an interface structure the matches the
  2991. * address is has the given flags not set
  2992. */
  2993. static struct interface *
  2994. getinterface(struct sockaddr_storage *addr, int flags)
  2995. {
  2996. struct interface *interface = find_addr_in_list(addr);
  2997. if (interface != NULL && interface->flags & flags)
  2998. {
  2999. return NULL;
  3000. }
  3001. else
  3002. {
  3003. return interface;
  3004. }
  3005. }
  3006. /*
  3007. * findlocalcastinterface - find local *cast interface index corresponding to address
  3008. * depending on the flags passed
  3009. */
  3010. static struct interface *
  3011. findlocalcastinterface(
  3012. struct sockaddr_storage *addr, int flags
  3013. )
  3014. {
  3015. struct interface *interface;
  3016. struct interface *nif = NULL;
  3017. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  3018. isc_boolean_t want_linklocal;
  3019. #endif
  3020. /*
  3021. * see how kernel maps the mcast address
  3022. */
  3023. nif = findlocalinterface(addr, 0);
  3024. if (nif) {
  3025. DPRINTF(2, ("findlocalcastinterface: kernel recommends interface #%d %s\n", nif->ifnum, nif->name));
  3026. return nif;
  3027. }
  3028. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  3029. want_linklocal = ISC_FALSE;
  3030. if (addr_ismulticast(addr) && flags == INT_MULTICAST)
  3031. {
  3032. if (IN6_IS_ADDR_MC_LINKLOCAL(&((struct sockaddr_in6*)addr)->sin6_addr))
  3033. {
  3034. want_linklocal = ISC_TRUE;
  3035. }
  3036. else if (IN6_IS_ADDR_MC_SITELOCAL(&((struct sockaddr_in6*)addr)->sin6_addr))
  3037. {
  3038. want_linklocal = ISC_TRUE;
  3039. }
  3040. }
  3041. #endif
  3042. for (interface = ISC_LIST_HEAD(inter_list);
  3043. interface != NULL;
  3044. interface = ISC_LIST_NEXT(interface, link))
  3045. {
  3046. /* use only allowed addresses */
  3047. if (interface->ignore_packets == ISC_TRUE)
  3048. continue;
  3049. /* Skip the loopback and wildcard addresses */
  3050. if (interface->flags & (INT_LOOPBACK|INT_WILDCARD))
  3051. continue;
  3052. /* Skip if different family */
  3053. if(interface->sin.ss_family != addr->ss_family)
  3054. continue;
  3055. /* Is this it one of these based on flags? */
  3056. if (!(interface->flags & flags))
  3057. continue;
  3058. /* for IPv6 multicast check the address for linklocal */
  3059. #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
  3060. if (flags == INT_MULTICAST && interface->sin.ss_family == AF_INET6 &&
  3061. (IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6*)&interface->sin)->sin6_addr))
  3062. && want_linklocal == ISC_TRUE)
  3063. {
  3064. nif = interface;
  3065. break;
  3066. }
  3067. /* If we want a linklocal address and this isn't it, skip */\
  3068. if (want_linklocal == ISC_TRUE)
  3069. continue;
  3070. #endif
  3071. /* Otherwise just look for the flag */
  3072. if((interface->flags & flags))
  3073. {
  3074. nif = interface;
  3075. break;
  3076. }
  3077. }
  3078. #ifdef DEBUG
  3079. if (debug > 2)
  3080. {
  3081. if (nif)
  3082. printf("findlocalcastinterface: found interface #%d %s\n", nif->ifnum, nif->name);
  3083. else
  3084. printf("findlocalcastinterface: no interface found for %s flags 0x%x\n", stoa(addr), flags);
  3085. }
  3086. #endif
  3087. return (nif);
  3088. }
  3089. /*
  3090. * findbcastinter - find broadcast interface corresponding to address
  3091. */
  3092. struct interface *
  3093. findbcastinter(
  3094. struct sockaddr_storage *addr
  3095. )
  3096. {
  3097. #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
  3098. struct interface *interface;
  3099. DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
  3100. stoa(addr)));
  3101. interface = findlocalinterface(addr, INT_LOOPBACK|INT_WILDCARD);
  3102. if (interface != NULL)
  3103. {
  3104. DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n", interface->ifnum, interface->name));
  3105. return interface;
  3106. }
  3107. /* plan B - try to find something reasonable in our lists in case kernel lookup doesn't help */
  3108. for (interface = ISC_LIST_HEAD(inter_list);
  3109. interface != NULL;
  3110. interface = ISC_LIST_NEXT(interface, link))
  3111. {
  3112. if (interface->flags & INT_WILDCARD)
  3113. continue;
  3114. /* Don't bother with ignored interfaces */
  3115. if (interface->ignore_packets == ISC_TRUE)
  3116. continue;
  3117. /*
  3118. * First look if this is the correct family
  3119. */
  3120. if(interface->sin.ss_family != addr->ss_family)
  3121. continue;
  3122. /* Skip the loopback addresses */
  3123. if (interface->flags & INT_LOOPBACK)
  3124. continue;
  3125. /*
  3126. * If we are looking to match a multicast address grab it.
  3127. */
  3128. if (addr_ismulticast(addr) == ISC_TRUE && interface->flags & INT_MULTICAST)
  3129. {
  3130. #ifdef INCLUDE_IPV6_SUPPORT
  3131. if(addr->ss_family == AF_INET6) {
  3132. /* Only use link-local address for link-scope mcast */
  3133. if(IN6_IS_ADDR_MC_LINKLOCAL(&((struct sockaddr_in6*)addr)->sin6_addr) &&
  3134. !IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6*)&interface->sin)->sin6_addr)) {
  3135. continue;
  3136. }
  3137. }
  3138. #endif
  3139. break;
  3140. }
  3141. /*
  3142. * We match only those interfaces marked as
  3143. * broadcastable and either the explicit broadcast
  3144. * address or the network portion of the IP address.
  3145. * Sloppy.
  3146. */
  3147. if(addr->ss_family == AF_INET) {
  3148. if (SOCKCMP(&interface->bcast, addr)) {
  3149. break;
  3150. }
  3151. if ((NSRCADR(&interface->sin) &
  3152. NSRCADR(&interface->mask)) == (NSRCADR(addr) &
  3153. NSRCADR(&interface->mask)))
  3154. break;
  3155. }
  3156. #ifdef INCLUDE_IPV6_SUPPORT
  3157. else if(addr->ss_family == AF_INET6) {
  3158. if (SOCKCMP(&interface->bcast, addr)) {
  3159. break;
  3160. }
  3161. if (SOCKCMP(netof(&interface->sin), netof(addr))) {
  3162. break;
  3163. }
  3164. }
  3165. #endif
  3166. }
  3167. #endif /* SIOCGIFCONF */
  3168. if (interface == NULL) {
  3169. DPRINTF(4, ("No bcast interface found for %s\n", stoa(addr)));
  3170. return ANY_INTERFACE_CHOOSE(addr);
  3171. } else {
  3172. DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n", interface->ifnum, interface->name));
  3173. return interface;
  3174. }
  3175. }
  3176. /*
  3177. * io_clr_stats - clear I/O module statistics
  3178. */
  3179. void
  3180. io_clr_stats(void)
  3181. {
  3182. packets_dropped = 0;
  3183. packets_ignored = 0;
  3184. packets_received = 0;
  3185. packets_sent = 0;
  3186. packets_notsent = 0;
  3187. handler_calls = 0;
  3188. handler_pkts = 0;
  3189. io_timereset = current_time;
  3190. }
  3191. #ifdef REFCLOCK
  3192. /*
  3193. * io_addclock - add a reference clock to the list and arrange that we
  3194. * get SIGIO interrupts from it.
  3195. */
  3196. int
  3197. io_addclock(
  3198. struct refclockio *rio
  3199. )
  3200. {
  3201. BLOCKIO();
  3202. /*
  3203. * Stuff the I/O structure in the list and mark the descriptor
  3204. * in use. There is a harmless (I hope) race condition here.
  3205. */
  3206. rio->next = refio;
  3207. # ifdef HAVE_SIGNALED_IO
  3208. if (init_clock_sig(rio))
  3209. {
  3210. UNBLOCKIO();
  3211. return 0;
  3212. }
  3213. # elif defined(HAVE_IO_COMPLETION_PORT)
  3214. if (io_completion_port_add_clock_io(rio))
  3215. {
  3216. UNBLOCKIO();
  3217. return 0;
  3218. }
  3219. # endif
  3220. /*
  3221. * enqueue
  3222. */
  3223. refio = rio;
  3224. /*
  3225. * register fd
  3226. */
  3227. add_fd_to_list(rio->fd, FD_TYPE_FILE);
  3228. UNBLOCKIO();
  3229. return 1;
  3230. }
  3231. /*
  3232. * io_closeclock - close the clock in the I/O structure given
  3233. */
  3234. void
  3235. io_closeclock(
  3236. struct refclockio *rio
  3237. )
  3238. {
  3239. BLOCKIO();
  3240. /*
  3241. * Remove structure from the list
  3242. */
  3243. if (refio == rio)
  3244. {
  3245. refio = rio->next;
  3246. }
  3247. else
  3248. {
  3249. register struct refclockio *rp;
  3250. for (rp = refio; rp != NULL; rp = rp->next)
  3251. if (rp->next == rio)
  3252. {
  3253. rp->next = rio->next;
  3254. break;
  3255. }
  3256. if (rp == NULL) {
  3257. UNBLOCKIO();
  3258. return;
  3259. }
  3260. }
  3261. /*
  3262. * Close the descriptor.
  3263. */
  3264. close_and_delete_fd_from_list(rio->fd);
  3265. UNBLOCKIO();
  3266. }
  3267. #endif /* REFCLOCK */
  3268. /*
  3269. * On NT a SOCKET is an unsigned int so we cannot possibly keep it in
  3270. * an array. So we use one of the ISC_LIST functions to hold the
  3271. * socket value and use that when we want to enumerate it.
  3272. */
  3273. void
  3274. kill_asyncio(int startfd)
  3275. {
  3276. vsock_t *lsock;
  3277. vsock_t *next;
  3278. BLOCKIO();
  3279. lsock = ISC_LIST_HEAD(fd_list);
  3280. while (lsock != NULL) {
  3281. /*
  3282. * careful here - list is being dismantled while
  3283. * we scan it - setting next here insures that
  3284. * we are able to correctly scan the list
  3285. */
  3286. next = ISC_LIST_NEXT(lsock, link);
  3287. /*
  3288. * will remove socket from list
  3289. */
  3290. close_and_delete_fd_from_list(lsock->fd);
  3291. lsock = next;
  3292. }
  3293. UNBLOCKIO();
  3294. }
  3295. /*
  3296. * Add and delete functions for the list of open sockets
  3297. */
  3298. static void
  3299. add_fd_to_list(SOCKET fd, enum desc_type type) {
  3300. vsock_t *lsock = (vsock_t *)emalloc(sizeof(vsock_t));
  3301. lsock->fd = fd;
  3302. lsock->type = type;
  3303. ISC_LIST_APPEND(fd_list, lsock, link);
  3304. /*
  3305. * I/O Completion Ports don't care about the select and FD_SET
  3306. */
  3307. #ifndef HAVE_IO_COMPLETION_PORT
  3308. if (fd < 0 || fd >= FD_SETSIZE) {
  3309. msyslog(LOG_ERR, "Too many sockets in use, FD_SETSIZE %d exceeded",
  3310. FD_SETSIZE);
  3311. exit(1);
  3312. }
  3313. /*
  3314. * keep activefds in sync
  3315. */
  3316. if (fd > maxactivefd)
  3317. maxactivefd = fd;
  3318. FD_SET( (u_int)fd, &activefds);
  3319. #endif
  3320. }
  3321. static void
  3322. close_and_delete_fd_from_list(SOCKET fd) {
  3323. vsock_t *next;
  3324. vsock_t *lsock = ISC_LIST_HEAD(fd_list);
  3325. while(lsock != NULL) {
  3326. next = ISC_LIST_NEXT(lsock, link);
  3327. if(lsock->fd == fd) {
  3328. ISC_LIST_DEQUEUE_TYPE(fd_list, lsock, link, vsock_t);
  3329. switch (lsock->type) {
  3330. case FD_TYPE_SOCKET:
  3331. #ifdef SYS_WINNT
  3332. closesocket(lsock->fd);
  3333. break;
  3334. #endif
  3335. case FD_TYPE_FILE:
  3336. (void) close(lsock->fd);
  3337. break;
  3338. default:
  3339. msyslog(LOG_ERR, "internal error - illegal descriptor type %d - EXITING", (int)lsock->type);
  3340. exit(1);
  3341. }
  3342. free(lsock);
  3343. /*
  3344. * I/O Completion Ports don't care about select and fd_set
  3345. */
  3346. #ifndef HAVE_IO_COMPLETION_PORT
  3347. /*
  3348. * remove from activefds
  3349. */
  3350. FD_CLR( (u_int) fd, &activefds);
  3351. if (fd == maxactivefd) {
  3352. int i, newmax = 0;
  3353. for (i = 0; i < maxactivefd; i++)
  3354. if (FD_ISSET(i, &activefds))
  3355. newmax = i;
  3356. maxactivefd = newmax;
  3357. }
  3358. #endif
  3359. break;
  3360. }
  3361. lsock = next;
  3362. }
  3363. }
  3364. static void
  3365. add_addr_to_list(struct sockaddr_storage *addr, struct interface *interface){
  3366. #ifdef DEBUG
  3367. if (find_addr_in_list(addr) == NULL) {
  3368. #endif
  3369. /* not there yet - add to list */
  3370. remaddr_t *laddr = (remaddr_t *)emalloc(sizeof(remaddr_t));
  3371. memcpy(&laddr->addr, addr, sizeof(struct sockaddr_storage));
  3372. laddr->interface = interface;
  3373. ISC_LIST_APPEND(remoteaddr_list, laddr, link);
  3374. DPRINTF(4, ("Added addr %s to list of addresses\n",
  3375. stoa(addr)));
  3376. #ifdef DEBUG
  3377. } else {
  3378. DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
  3379. stoa(addr)));
  3380. }
  3381. #endif
  3382. }
  3383. static void
  3384. delete_addr_from_list(struct sockaddr_storage *addr) {
  3385. remaddr_t *next;
  3386. remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
  3387. while(laddr != NULL) {
  3388. next = ISC_LIST_NEXT(laddr, link);
  3389. if(SOCKCMP(&laddr->addr, addr)) {
  3390. ISC_LIST_DEQUEUE_TYPE(remoteaddr_list, laddr, link, remaddr_t);
  3391. DPRINTF(4, ("Deleted addr %s from list of addresses\n",
  3392. stoa(addr)));
  3393. free(laddr);
  3394. break;
  3395. }
  3396. laddr = next;
  3397. }
  3398. }
  3399. static void
  3400. delete_interface_from_list(struct interface *iface) {
  3401. remaddr_t *next;
  3402. remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
  3403. while(laddr != NULL) {
  3404. next = ISC_LIST_NEXT(laddr, link);
  3405. if (laddr->interface == iface) {
  3406. ISC_LIST_DEQUEUE_TYPE(remoteaddr_list, laddr, link, remaddr_t);
  3407. DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
  3408. stoa(&laddr->addr), iface->ifnum, iface->name));
  3409. free(laddr);
  3410. }
  3411. laddr = next;
  3412. }
  3413. }
  3414. static struct interface *
  3415. find_addr_in_list(struct sockaddr_storage *addr) {
  3416. remaddr_t *next;
  3417. remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
  3418. DPRINTF(4, ("Searching for addr %s in list of addresses - ",
  3419. stoa(addr)));
  3420. while(laddr != NULL) {
  3421. next = ISC_LIST_NEXT(laddr, link);
  3422. if(SOCKCMP(&laddr->addr, addr)) {
  3423. DPRINTF(4, ("FOUND\n"));
  3424. return laddr->interface;
  3425. }
  3426. else
  3427. laddr = next;
  3428. }
  3429. DPRINTF(4, ("NOT FOUND\n"));
  3430. return NULL; /* Not found */
  3431. }
  3432. /*
  3433. * Find the given address with the associated flag in the list
  3434. */
  3435. static struct interface *
  3436. find_flagged_addr_in_list(struct sockaddr_storage *addr, int flag) {
  3437. remaddr_t *next;
  3438. remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
  3439. DPRINTF(4, ("Finding addr %s in list of addresses\n",
  3440. stoa(addr)));
  3441. while(laddr != NULL) {
  3442. next = ISC_LIST_NEXT(laddr, link);
  3443. if(SOCKCMP(&laddr->addr, addr) && (laddr->interface->flags & flag)) {
  3444. return laddr->interface;
  3445. break;
  3446. }
  3447. else
  3448. laddr = next;
  3449. }
  3450. return NULL; /* Not found */
  3451. }
  3452. #ifdef HAS_ROUTING_SOCKET
  3453. #include <net/route.h>
  3454. #ifndef UPDATE_GRACE
  3455. #define UPDATE_GRACE 2 /* wait UPDATE_GRACE seconds before scanning */
  3456. #endif
  3457. static void
  3458. process_routing_msgs(struct asyncio_reader *reader)
  3459. {
  3460. char buffer[5120];
  3461. char *p = buffer;
  3462. int cnt;
  3463. if (disable_dynamic_updates) {
  3464. /*
  3465. * discard ourselves if we are not need any more
  3466. * usually happens when running unprivileged
  3467. */
  3468. remove_asyncio_reader(reader);
  3469. delete_asyncio_reader(reader);
  3470. return;
  3471. }
  3472. cnt = read(reader->fd, buffer, sizeof(buffer));
  3473. if (cnt < 0) {
  3474. msyslog(LOG_ERR, "i/o error on routing socket %m - disabling");
  3475. remove_asyncio_reader(reader);
  3476. delete_asyncio_reader(reader);
  3477. return;
  3478. }
  3479. /*
  3480. * process routing message
  3481. */
  3482. while ((p + sizeof(struct rt_msghdr)) <= (buffer + cnt))
  3483. {
  3484. struct rt_msghdr *rtm;
  3485. rtm = (struct rt_msghdr *)p;
  3486. if (rtm->rtm_version != RTM_VERSION) {
  3487. msyslog(LOG_ERR, "version mismatch on routing socket %m - disabling");
  3488. remove_asyncio_reader(reader);
  3489. delete_asyncio_reader(reader);
  3490. return;
  3491. }
  3492. switch (rtm->rtm_type) {
  3493. #ifdef RTM_NEWADDR
  3494. case RTM_NEWADDR:
  3495. #endif
  3496. #ifdef RTM_DELADDR
  3497. case RTM_DELADDR:
  3498. #endif
  3499. #ifdef RTM_ADD
  3500. case RTM_ADD:
  3501. #endif
  3502. #ifdef RTM_DELETE
  3503. case RTM_DELETE:
  3504. #endif
  3505. #ifdef RTM_REDIRECT
  3506. case RTM_REDIRECT:
  3507. #endif
  3508. #ifdef RTM_CHANGE
  3509. case RTM_CHANGE:
  3510. #endif
  3511. #ifdef RTM_LOSING
  3512. case RTM_LOSING:
  3513. #endif
  3514. #ifdef RTM_IFINFO
  3515. case RTM_IFINFO:
  3516. #endif
  3517. #ifdef RTM_IFANNOUNCE
  3518. case RTM_IFANNOUNCE:
  3519. #endif
  3520. /*
  3521. * we are keen on new and deleted addresses and if an interface goes up and down or routing changes
  3522. */
  3523. DPRINTF(3, ("routing message op = %d: scheduling interface update\n", rtm->rtm_type));
  3524. timer_interfacetimeout(current_time + UPDATE_GRACE);
  3525. break;
  3526. default:
  3527. /*
  3528. * the rest doesn't bother us.
  3529. */
  3530. DPRINTF(4, ("routing message op = %d: ignored\n", rtm->rtm_type));
  3531. break;
  3532. }
  3533. p += rtm->rtm_msglen;
  3534. }
  3535. }
  3536. /*
  3537. * set up routing notifications
  3538. */
  3539. static void
  3540. init_async_notifications()
  3541. {
  3542. struct asyncio_reader *reader;
  3543. int fd = socket(PF_ROUTE, SOCK_RAW, 0);
  3544. if (fd >= 0) {
  3545. fd = move_fd(fd);
  3546. init_nonblocking_io(fd);
  3547. #if defined(HAVE_SIGNALED_IO)
  3548. init_socket_sig(fd);
  3549. #endif /* HAVE_SIGNALED_IO */
  3550. reader = new_asyncio_reader();
  3551. reader->fd = fd;
  3552. reader->receiver = process_routing_msgs;
  3553. add_asyncio_reader(reader, FD_TYPE_SOCKET);
  3554. msyslog(LOG_INFO, "Listening on routing socket on fd #%d for interface updates", fd);
  3555. } else {
  3556. msyslog(LOG_ERR, "unable to open routing socket (%m) - using polled interface update");
  3557. }
  3558. }
  3559. #else
  3560. static void
  3561. init_async_notifications()
  3562. {
  3563. }
  3564. #endif