1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* ntp_io.c - input/output routines for ntpd. The socket-opening code
|
|
|
|
* was shamelessly stolen from ntpd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_FNMATCH_H
|
|
|
|
# include <fnmatch.h>
|
|
|
|
# if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE)
|
|
|
|
# define FNM_CASEFOLD FNM_IGNORECASE
|
|
|
|
# endif
|
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKIO_H /* UXPV: SIOC* #defines (Frank Vance <fvance@waii.com>) */
|
|
|
|
# include <sys/sockio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
# include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
|
2001-08-29 14:35:15 +00:00
|
|
|
#include "ntp_machine.h"
|
|
|
|
#include "ntpd.h"
|
|
|
|
#include "ntp_io.h"
|
|
|
|
#include "iosignal.h"
|
2013-12-04 21:33:17 +00:00
|
|
|
#include "ntp_lists.h"
|
2001-08-29 14:35:15 +00:00
|
|
|
#include "ntp_refclock.h"
|
|
|
|
#include "ntp_stdlib.h"
|
2014-12-20 22:52:39 +00:00
|
|
|
#include "ntp_worker.h"
|
2008-08-18 14:26:05 +00:00
|
|
|
#include "ntp_request.h"
|
2013-12-04 21:33:17 +00:00
|
|
|
#include "ntp_assert.h"
|
2014-12-20 22:52:39 +00:00
|
|
|
#include "timevalops.h"
|
|
|
|
#include "timespecops.h"
|
2013-12-04 21:33:17 +00:00
|
|
|
#include "ntpd-opts.h"
|
2016-01-08 08:06:14 +00:00
|
|
|
#include "safecast.h"
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
/* Don't include ISC's version of IPv6 variables and structures */
|
|
|
|
#define ISC_IPV6_H 1
|
2013-12-04 21:33:17 +00:00
|
|
|
#include <isc/mem.h>
|
2004-07-20 15:01:56 +00:00
|
|
|
#include <isc/interfaceiter.h>
|
2013-12-04 21:33:17 +00:00
|
|
|
#include <isc/netaddr.h>
|
2004-07-20 15:01:56 +00:00
|
|
|
#include <isc/result.h>
|
2013-12-04 21:33:17 +00:00
|
|
|
#include <isc/sockaddr.h>
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
#ifdef SIM
|
|
|
|
#include "ntpsim.h"
|
|
|
|
#endif
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef HAS_ROUTING_SOCKET
|
|
|
|
# include <net/route.h>
|
|
|
|
# ifdef HAVE_RTNETLINK
|
|
|
|
# include <linux/rtnetlink.h>
|
|
|
|
# endif
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* setsockopt does not always have the same arg declaration
|
|
|
|
* across all platforms. If it's not defined we make it empty
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SETSOCKOPT_ARG_CAST
|
|
|
|
#define SETSOCKOPT_ARG_CAST
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
extern int listen_to_virtual_ips;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2015-07-01 03:12:13 +00:00
|
|
|
#ifndef IPTOS_DSCP_EF
|
|
|
|
#define IPTOS_DSCP_EF 0xb8
|
|
|
|
#endif
|
|
|
|
int qos = IPTOS_DSCP_EF; /* QoS RFC3246 */
|
|
|
|
|
|
|
|
#ifdef LEAP_SMEAR
|
|
|
|
/* TODO burnicki: This should be moved to ntp_timer.c, but if we do so
|
|
|
|
* we get a linker error. Since we're running out of time before the leap
|
|
|
|
* second occurs, we let it here where it just works.
|
|
|
|
*/
|
|
|
|
int leap_smear_intv;
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* NIC rule entry
|
|
|
|
*/
|
|
|
|
typedef struct nic_rule_tag nic_rule;
|
|
|
|
|
|
|
|
struct nic_rule_tag {
|
|
|
|
nic_rule * next;
|
|
|
|
nic_rule_action action;
|
|
|
|
nic_rule_match match_type;
|
|
|
|
char * if_name;
|
|
|
|
sockaddr_u addr;
|
|
|
|
int prefixlen;
|
|
|
|
};
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* NIC rule listhead. Entries are added at the head so that the first
|
|
|
|
* match in the list is the last matching rule specified.
|
|
|
|
*/
|
|
|
|
nic_rule *nic_rule_list;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#if defined(SO_BINTIME) && defined(SCM_BINTIME) && defined(CMSG_FIRSTHDR)
|
|
|
|
# define HAVE_PACKET_TIMESTAMP
|
|
|
|
# define HAVE_BINTIME
|
|
|
|
# ifdef BINTIME_CTLMSGBUF_SIZE
|
|
|
|
# define CMSG_BUFSIZE BINTIME_CTLMSGBUF_SIZE
|
|
|
|
# else
|
|
|
|
# define CMSG_BUFSIZE 1536 /* moderate default */
|
|
|
|
# endif
|
|
|
|
#elif defined(SO_TIMESTAMPNS) && defined(SCM_TIMESTAMPNS) && defined(CMSG_FIRSTHDR)
|
|
|
|
# define HAVE_PACKET_TIMESTAMP
|
|
|
|
# define HAVE_TIMESTAMPNS
|
|
|
|
# ifdef TIMESTAMPNS_CTLMSGBUF_SIZE
|
|
|
|
# define CMSG_BUFSIZE TIMESTAMPNS_CTLMSGBUF_SIZE
|
|
|
|
# else
|
|
|
|
# define CMSG_BUFSIZE 1536 /* moderate default */
|
|
|
|
# endif
|
|
|
|
#elif defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP) && defined(CMSG_FIRSTHDR)
|
|
|
|
# define HAVE_PACKET_TIMESTAMP
|
|
|
|
# define HAVE_TIMESTAMP
|
|
|
|
# ifdef TIMESTAMP_CTLMSGBUF_SIZE
|
|
|
|
# define CMSG_BUFSIZE TIMESTAMP_CTLMSGBUF_SIZE
|
|
|
|
# else
|
|
|
|
# define CMSG_BUFSIZE 1536 /* moderate default */
|
|
|
|
# endif
|
2008-08-18 14:26:05 +00:00
|
|
|
#else
|
|
|
|
/* fill in for old/other timestamp interfaces */
|
|
|
|
#endif
|
2002-10-29 19:58:12 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
#if defined(SYS_WINNT)
|
2014-12-20 22:52:39 +00:00
|
|
|
#include "win32_io.h"
|
2004-07-20 15:01:56 +00:00
|
|
|
#include <isc/win32os.h>
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We do asynchronous input using the SIGIO facility. A number of
|
|
|
|
* recvbuf buffers are preallocated for input. In the signal
|
|
|
|
* handler we poll to see which sockets are ready and read the
|
|
|
|
* packets from them into the recvbuf's along with a time stamp and
|
|
|
|
* an indication of the source host and the interface it was received
|
|
|
|
* through. This allows us to get as accurate receive time stamps
|
|
|
|
* as possible independent of other processing going on.
|
|
|
|
*
|
|
|
|
* We watch the number of recvbufs available to the signal handler
|
|
|
|
* and allocate more when this number drops below the low water
|
|
|
|
* mark. If the signal handler should run out of buffers in the
|
|
|
|
* interim it will drop incoming frames, the idea being that it is
|
|
|
|
* better to drop a packet than to be inaccurate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Other statistics of possible interest
|
|
|
|
*/
|
|
|
|
volatile u_long packets_dropped; /* total number of packets dropped on reception */
|
|
|
|
volatile u_long packets_ignored; /* packets received on wild card interface */
|
|
|
|
volatile u_long packets_received; /* total number of packets received */
|
2013-12-04 21:33:17 +00:00
|
|
|
u_long packets_sent; /* total number of packets sent */
|
|
|
|
u_long packets_notsent; /* total number of packets which couldn't be sent */
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
volatile u_long handler_calls; /* number of calls to interrupt handler */
|
|
|
|
volatile u_long handler_pkts; /* number of pkts received by handler */
|
|
|
|
u_long io_timereset; /* time counters were reset */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface stuff
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt * any_interface; /* wildcard ipv4 interface */
|
|
|
|
endpt * any6_interface; /* wildcard ipv6 interface */
|
|
|
|
endpt * loopback_interface; /* loopback ipv4 interface */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2020-03-04 13:59:29 +00:00
|
|
|
static isc_boolean_t broadcast_client_enabled; /* is broadcast client enabled */
|
2014-12-20 22:52:39 +00:00
|
|
|
u_int sys_ifnum; /* next .ifnum to assign */
|
2004-07-20 15:01:56 +00:00
|
|
|
int ninterfaces; /* Total number of interfaces */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
int disable_dynamic_updates; /* scan interfaces once only */
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
#ifdef REFCLOCK
|
|
|
|
/*
|
|
|
|
* Refclock stuff. We keep a chain of structures with data concerning
|
|
|
|
* the guys we are doing I/O for.
|
|
|
|
*/
|
|
|
|
static struct refclockio *refio;
|
|
|
|
#endif /* REFCLOCK */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File descriptor masks etc. for call to select
|
2014-12-20 22:52:39 +00:00
|
|
|
* Not needed for I/O Completion Ports or anything outside this file
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
static fd_set activefds;
|
|
|
|
static int maxactivefd;
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* bit alternating value to detect verified interfaces during an update cycle
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
static u_short sys_interphase = 0;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
static endpt * new_interface(endpt *);
|
|
|
|
static void add_interface(endpt *);
|
|
|
|
static int update_interfaces(u_short, interface_receiver_t,
|
|
|
|
void *);
|
|
|
|
static void remove_interface(endpt *);
|
|
|
|
static endpt * create_interface(u_short, endpt *);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
static int is_wildcard_addr (const sockaddr_u *);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Multicast functions
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
static isc_boolean_t addr_ismulticast (sockaddr_u *);
|
|
|
|
static isc_boolean_t is_anycast (sockaddr_u *,
|
|
|
|
const char *);
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Not all platforms support multicast
|
|
|
|
*/
|
|
|
|
#ifdef MCAST
|
2013-12-04 21:33:17 +00:00
|
|
|
static isc_boolean_t socket_multicast_enable (endpt *, sockaddr_u *);
|
|
|
|
static isc_boolean_t socket_multicast_disable(endpt *, sockaddr_u *);
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-12-04 21:33:17 +00:00
|
|
|
static void interface_dump (const endpt *);
|
|
|
|
static void sockaddr_dump (const sockaddr_u *);
|
2015-02-05 20:53:33 +00:00
|
|
|
static void print_interface (const endpt *, const char *, const char *);
|
2013-12-04 21:33:17 +00:00
|
|
|
#define DPRINT_INTERFACE(level, args) do { if (debug >= (level)) { print_interface args; } } while (0)
|
2008-08-18 14:26:05 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
#define DPRINT_INTERFACE(level, args) do {} while (0)
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct vsock vsock_t;
|
2008-08-18 14:26:05 +00:00
|
|
|
enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE };
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
struct vsock {
|
2013-12-04 21:33:17 +00:00
|
|
|
vsock_t * link;
|
|
|
|
SOCKET fd;
|
|
|
|
enum desc_type type;
|
2004-07-20 15:01:56 +00:00
|
|
|
};
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
vsock_t *fd_list;
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
|
|
|
|
/*
|
|
|
|
* async notification processing (e. g. routing sockets)
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* support for receiving data on fd that is not a refclock or a socket
|
|
|
|
* like e. g. routing sockets
|
|
|
|
*/
|
|
|
|
struct asyncio_reader {
|
2013-12-04 21:33:17 +00:00
|
|
|
struct asyncio_reader *link; /* the list this is being kept in */
|
|
|
|
SOCKET fd; /* fd to be read */
|
|
|
|
void *data; /* possibly local data */
|
2008-08-18 14:26:05 +00:00
|
|
|
void (*receiver)(struct asyncio_reader *); /* input handler */
|
|
|
|
};
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
struct asyncio_reader *asyncio_reader_list;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
static void delete_asyncio_reader (struct asyncio_reader *);
|
|
|
|
static struct asyncio_reader *new_asyncio_reader (void);
|
|
|
|
static void add_asyncio_reader (struct asyncio_reader *, enum desc_type);
|
|
|
|
static void remove_asyncio_reader (struct asyncio_reader *);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
#endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
static void init_async_notifications (void);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
static int addr_eqprefix (const sockaddr_u *, const sockaddr_u *,
|
|
|
|
int);
|
|
|
|
static int addr_samesubnet (const sockaddr_u *, const sockaddr_u *,
|
|
|
|
const sockaddr_u *, const sockaddr_u *);
|
|
|
|
static int create_sockets (u_short);
|
|
|
|
static SOCKET open_socket (sockaddr_u *, int, int, endpt *);
|
|
|
|
static void set_reuseaddr (int);
|
|
|
|
static isc_boolean_t socket_broadcast_enable (struct interface *, SOCKET, sockaddr_u *);
|
2016-01-21 08:50:56 +00:00
|
|
|
|
|
|
|
#if !defined(HAVE_IO_COMPLETION_PORT) && !defined(HAVE_SIGNALED_IO)
|
|
|
|
static char * fdbits (int, const fd_set *);
|
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
|
2013-12-04 21:33:17 +00:00
|
|
|
static isc_boolean_t socket_broadcast_disable (struct interface *, sockaddr_u *);
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
typedef struct remaddr remaddr_t;
|
|
|
|
|
|
|
|
struct remaddr {
|
2013-12-04 21:33:17 +00:00
|
|
|
remaddr_t * link;
|
|
|
|
sockaddr_u addr;
|
|
|
|
endpt * ep;
|
2004-07-20 15:01:56 +00:00
|
|
|
};
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
remaddr_t * remoteaddr_list;
|
|
|
|
endpt * ep_list; /* complete endpt list */
|
|
|
|
endpt * mc4_list; /* IPv4 mcast-capable unicast endpts */
|
|
|
|
endpt * mc6_list; /* IPv6 mcast-capable unicast endpts */
|
|
|
|
|
|
|
|
static endpt * wildipv4;
|
|
|
|
static endpt * wildipv6;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef SYS_WINNT
|
|
|
|
int accept_wildcard_if_for_winnt;
|
|
|
|
#else
|
|
|
|
const int accept_wildcard_if_for_winnt = FALSE;
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
static void add_fd_to_list (SOCKET, enum desc_type);
|
|
|
|
static endpt * find_addr_in_list (sockaddr_u *);
|
|
|
|
static endpt * find_flagged_addr_in_list(sockaddr_u *, u_int32);
|
|
|
|
static void delete_addr_from_list (sockaddr_u *);
|
|
|
|
static void delete_interface_from_list(endpt *);
|
|
|
|
static void close_and_delete_fd_from_list(SOCKET);
|
|
|
|
static void add_addr_to_list (sockaddr_u *, endpt *);
|
|
|
|
static void create_wildcards (u_short);
|
|
|
|
static endpt * findlocalinterface (sockaddr_u *, int, int);
|
|
|
|
static endpt * findclosestinterface (sockaddr_u *, int);
|
|
|
|
#ifdef DEBUG
|
|
|
|
static const char * action_text (nic_rule_action);
|
|
|
|
#endif
|
|
|
|
static nic_rule_action interface_action(char *, sockaddr_u *, u_int32);
|
|
|
|
static void convert_isc_if (isc_interface_t *,
|
|
|
|
endpt *, u_short);
|
|
|
|
static void calc_addr_distance(sockaddr_u *,
|
|
|
|
const sockaddr_u *,
|
|
|
|
const sockaddr_u *);
|
|
|
|
static int cmp_addr_distance(const sockaddr_u *,
|
|
|
|
const sockaddr_u *);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines to read the ntp packets
|
|
|
|
*/
|
|
|
|
#if !defined(HAVE_IO_COMPLETION_PORT)
|
2014-12-20 22:52:39 +00:00
|
|
|
static inline int read_network_packet (SOCKET, struct interface *, l_fp);
|
|
|
|
static void ntpd_addremove_io_fd (int, int, int);
|
2016-01-21 08:50:56 +00:00
|
|
|
static void input_handler_scan (const l_fp*, const fd_set*);
|
|
|
|
static int/*BOOL*/ sanitize_fdset (int errc);
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef REFCLOCK
|
|
|
|
static inline int read_refclock_packet (SOCKET, struct refclockio *, l_fp);
|
|
|
|
#endif
|
2016-01-21 08:50:56 +00:00
|
|
|
#ifdef HAVE_SIGNALED_IO
|
|
|
|
static void input_handler (l_fp*);
|
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifndef HAVE_IO_COMPLETION_PORT
|
|
|
|
void
|
|
|
|
maintain_activefds(
|
|
|
|
int fd,
|
|
|
|
int closing
|
2013-12-04 21:33:17 +00:00
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
int i;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (fd < 0 || fd >= FD_SETSIZE) {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"Too many sockets in use, FD_SETSIZE %d exceeded by fd %d",
|
|
|
|
FD_SETSIZE, fd);
|
|
|
|
exit(1);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (!closing) {
|
|
|
|
FD_SET(fd, &activefds);
|
|
|
|
maxactivefd = max(fd, maxactivefd);
|
|
|
|
} else {
|
|
|
|
FD_CLR(fd, &activefds);
|
|
|
|
if (maxactivefd && fd == maxactivefd) {
|
|
|
|
for (i = maxactivefd - 1; i >= 0; i--)
|
|
|
|
if (FD_ISSET(i, &activefds)) {
|
|
|
|
maxactivefd = i;
|
|
|
|
break;
|
|
|
|
}
|
2015-10-21 19:16:13 +00:00
|
|
|
INSIST(fd != maxactivefd);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* !HAVE_IO_COMPLETION_PORT */
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_TIMING
|
|
|
|
/*
|
|
|
|
* collect timing information for various processing
|
2014-12-20 22:52:39 +00:00
|
|
|
* paths. currently we only pass them on to the file
|
2008-08-18 14:26:05 +00:00
|
|
|
* for later processing. this could also do histogram
|
|
|
|
* based analysis in other to reduce the load (and skew)
|
|
|
|
* dur to the file output
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
char buf[256];
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s %d %s %s",
|
2013-12-04 21:33:17 +00:00
|
|
|
(rb != NULL)
|
|
|
|
? ((rb->dstadr != NULL)
|
|
|
|
? stoa(&rb->recv_srcadr)
|
|
|
|
: "-REFCLOCK-")
|
|
|
|
: "-",
|
2008-08-18 14:26:05 +00:00
|
|
|
count, lfptoa(dts, 9), tag);
|
|
|
|
record_timing_stats(buf);
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* About dynamic interfaces, sockets, reception and more...
|
|
|
|
*
|
|
|
|
* the code solves following tasks:
|
|
|
|
*
|
|
|
|
* - keep a current list of active interfaces in order
|
|
|
|
* to bind to to the interface address on NTP_PORT so that
|
|
|
|
* all wild and specific bindings for NTP_PORT are taken by ntpd
|
|
|
|
* to avoid other daemons messing with the time or sockets.
|
2014-12-20 22:52:39 +00:00
|
|
|
* - all interfaces keep a list of peers that are referencing
|
2008-08-18 14:26:05 +00:00
|
|
|
* the interface in order to quickly re-assign the peers to
|
|
|
|
* new interface in case an interface is deleted (=> gone from system or
|
|
|
|
* down)
|
|
|
|
* - have a preconfigured socket ready with the right local address
|
|
|
|
* for transmission and reception
|
|
|
|
* - have an address list for all destination addresses used within ntpd
|
|
|
|
* to find the "right" preconfigured socket.
|
|
|
|
* - facilitate updating the internal interface list with respect to
|
|
|
|
* the current kernel state
|
|
|
|
*
|
|
|
|
* special issues:
|
|
|
|
*
|
|
|
|
* - mapping of multicast addresses to the interface affected is not always
|
|
|
|
* one to one - especially on hosts with multiple interfaces
|
|
|
|
* the code here currently allocates a separate interface entry for those
|
|
|
|
* multicast addresses
|
|
|
|
* iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF)
|
|
|
|
* in case of failure the multicast address is bound to an existing interface.
|
|
|
|
* - on some systems it is perfectly legal to assign the same address to
|
|
|
|
* multiple interfaces. Therefore this code does not keep a list of interfaces
|
|
|
|
* but a list of interfaces that represent a unique address as determined by the kernel
|
|
|
|
* by the procedure in findlocalinterface. Thus it is perfectly legal to see only
|
|
|
|
* one representative of a group of real interfaces if they share the same address.
|
2014-12-20 22:52:39 +00:00
|
|
|
*
|
2008-08-18 14:26:05 +00:00
|
|
|
* Frank Kardel 20050910
|
|
|
|
*/
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* init_io - initialize I/O module.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_io(void)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
/* Init buffer free list and stat counters */
|
1999-12-09 13:01:21 +00:00
|
|
|
init_recvbuff(RECV_INIT);
|
2014-12-20 22:52:39 +00:00
|
|
|
/* update interface every 5 minutes as default */
|
|
|
|
interface_interval = 300;
|
|
|
|
|
|
|
|
#ifdef WORK_PIPE
|
|
|
|
addremove_io_fd = &ntpd_addremove_io_fd;
|
|
|
|
#endif
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2016-01-21 08:50:56 +00:00
|
|
|
#if defined(SYS_WINNT)
|
2013-12-04 21:33:17 +00:00
|
|
|
init_io_completion_port();
|
2016-01-21 08:50:56 +00:00
|
|
|
#elif defined(HAVE_SIGNALED_IO)
|
2014-12-20 22:52:39 +00:00
|
|
|
(void) set_signal(input_handler);
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
static void
|
|
|
|
ntpd_addremove_io_fd(
|
|
|
|
int fd,
|
|
|
|
int is_pipe,
|
|
|
|
int remove_it
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UNUSED_ARG(is_pipe);
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGNALED_IO
|
2016-01-21 08:50:56 +00:00
|
|
|
if (!remove_it)
|
|
|
|
init_socket_sig(fd);
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* not HAVE_SIGNALED_IO */
|
|
|
|
|
|
|
|
maintain_activefds(fd, remove_it);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* io_open_sockets - call socket creation routine
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
io_open_sockets(void)
|
|
|
|
{
|
|
|
|
static int already_opened;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (already_opened || HAVE_OPT( SAVECONFIGQUIT ))
|
|
|
|
return;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
already_opened = 1;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* Create the sockets
|
|
|
|
*/
|
|
|
|
BLOCKIO();
|
2013-12-04 21:33:17 +00:00
|
|
|
create_sockets(NTP_PORT);
|
1999-12-09 13:01:21 +00:00
|
|
|
UNBLOCKIO();
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
init_async_notifications();
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(3, ("io_open_sockets: maxactivefd %d\n", maxactivefd));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
/*
|
|
|
|
* function to dump the contents of the interface structure
|
|
|
|
* for debugging use only.
|
2017-03-23 05:19:00 +00:00
|
|
|
* We face a dilemma here -- sockets are FDs under POSIX and
|
|
|
|
* actually HANDLES under Windows. So we use '%lld' as format
|
|
|
|
* and cast the value to 'long long'; this should not hurt
|
|
|
|
* with UNIX-like systems and does not truncate values on Win64.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-12-04 21:33:17 +00:00
|
|
|
interface_dump(const endpt *itf)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
printf("Dumping interface: %p\n", itf);
|
2017-03-23 05:19:00 +00:00
|
|
|
printf("fd = %lld\n", (long long)itf->fd);
|
|
|
|
printf("bfd = %lld\n", (long long)itf->bfd);
|
2013-12-04 21:33:17 +00:00
|
|
|
printf("sin = %s,\n", stoa(&itf->sin));
|
|
|
|
sockaddr_dump(&itf->sin);
|
|
|
|
printf("bcast = %s,\n", stoa(&itf->bcast));
|
|
|
|
sockaddr_dump(&itf->bcast);
|
|
|
|
printf("mask = %s,\n", stoa(&itf->mask));
|
|
|
|
sockaddr_dump(&itf->mask);
|
2008-08-18 14:26:05 +00:00
|
|
|
printf("name = %s\n", itf->name);
|
|
|
|
printf("flags = 0x%08x\n", itf->flags);
|
|
|
|
printf("last_ttl = %d\n", itf->last_ttl);
|
|
|
|
printf("addr_refid = %08x\n", itf->addr_refid);
|
|
|
|
printf("num_mcast = %d\n", itf->num_mcast);
|
|
|
|
printf("received = %ld\n", itf->received);
|
|
|
|
printf("sent = %ld\n", itf->sent);
|
|
|
|
printf("notsent = %ld\n", itf->notsent);
|
|
|
|
printf("ifindex = %u\n", itf->ifindex);
|
|
|
|
printf("peercnt = %u\n", itf->peercnt);
|
|
|
|
printf("phase = %u\n", itf->phase);
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* sockaddr_dump - hex dump the start of a sockaddr_u
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sockaddr_dump(const sockaddr_u *psau)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
/* Limit the size of the sockaddr_in6 hex dump */
|
2013-12-04 21:33:17 +00:00
|
|
|
const int maxsize = min(32, sizeof(psau->sa6));
|
|
|
|
const u_char * cp;
|
|
|
|
int i;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/* XXX: Should we limit maxsize based on psau->saX.sin_family? */
|
|
|
|
cp = (const void *)&psau->sa6;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
for(i = 0; i < maxsize; i++) {
|
|
|
|
printf("%02x", *cp++);
|
|
|
|
if (!((i + 1) % 4))
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* print_interface - helper to output debug information
|
|
|
|
*/
|
|
|
|
static void
|
2015-02-05 20:53:33 +00:00
|
|
|
print_interface(const endpt *iface, const char *pfx, const char *sfx)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2017-03-23 05:19:00 +00:00
|
|
|
printf("%sinterface #%d: fd=%lld, bfd=%lld, name=%s, flags=0x%x, ifindex=%u, sin=%s",
|
2008-08-18 14:26:05 +00:00
|
|
|
pfx,
|
|
|
|
iface->ifnum,
|
2017-03-23 05:19:00 +00:00
|
|
|
(long long)iface->fd,
|
|
|
|
(long long)iface->bfd,
|
2008-08-18 14:26:05 +00:00
|
|
|
iface->name,
|
|
|
|
iface->flags,
|
2013-12-04 21:33:17 +00:00
|
|
|
iface->ifindex,
|
|
|
|
stoa(&iface->sin));
|
|
|
|
if (AF_INET == iface->family) {
|
|
|
|
if (iface->flags & INT_BROADCAST)
|
|
|
|
printf(", bcast=%s", stoa(&iface->bcast));
|
|
|
|
printf(", mask=%s", stoa(&iface->mask));
|
|
|
|
}
|
|
|
|
printf(", %s:%s",
|
2014-12-20 22:52:39 +00:00
|
|
|
(iface->ignore_packets)
|
2013-12-04 21:33:17 +00:00
|
|
|
? "Disabled"
|
|
|
|
: "Enabled",
|
|
|
|
sfx);
|
2008-08-18 14:26:05 +00:00
|
|
|
if (debug > 4) /* in-depth debugging only */
|
|
|
|
interface_dump(iface);
|
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
|
|
|
|
/*
|
|
|
|
* create an asyncio_reader structure
|
|
|
|
*/
|
|
|
|
static struct asyncio_reader *
|
2013-12-04 21:33:17 +00:00
|
|
|
new_asyncio_reader(void)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
struct asyncio_reader *reader;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
reader = emalloc_zero(sizeof(*reader));
|
2008-08-18 14:26:05 +00:00
|
|
|
reader->fd = INVALID_SOCKET;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
return reader;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* delete a reader
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
delete_asyncio_reader(
|
|
|
|
struct asyncio_reader *reader
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
free(reader);
|
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* add asynchio_reader
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
add_asyncio_reader(
|
2014-12-20 22:52:39 +00:00
|
|
|
struct asyncio_reader * reader,
|
2013-12-04 21:33:17 +00:00
|
|
|
enum desc_type type)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
LINK_SLIST(asyncio_reader_list, reader, link);
|
2008-08-18 14:26:05 +00:00
|
|
|
add_fd_to_list(reader->fd, type);
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* remove asynchio_reader
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
remove_asyncio_reader(
|
|
|
|
struct asyncio_reader *reader
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
struct asyncio_reader *unlinked;
|
|
|
|
|
|
|
|
UNLINK_SLIST(unlinked, asyncio_reader_list, reader, link,
|
|
|
|
struct asyncio_reader);
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
if (reader->fd != INVALID_SOCKET)
|
|
|
|
close_and_delete_fd_from_list(reader->fd);
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
reader->fd = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
#endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/* compare two sockaddr prefixes */
|
|
|
|
static int
|
|
|
|
addr_eqprefix(
|
|
|
|
const sockaddr_u * a,
|
|
|
|
const sockaddr_u * b,
|
|
|
|
int prefixlen
|
|
|
|
)
|
|
|
|
{
|
|
|
|
isc_netaddr_t isc_a;
|
|
|
|
isc_netaddr_t isc_b;
|
|
|
|
isc_sockaddr_t isc_sa;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(isc_sa);
|
|
|
|
memcpy(&isc_sa.type, a, min(sizeof(isc_sa.type), sizeof(*a)));
|
2013-12-04 21:33:17 +00:00
|
|
|
isc_netaddr_fromsockaddr(&isc_a, &isc_sa);
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(isc_sa);
|
|
|
|
memcpy(&isc_sa.type, b, min(sizeof(isc_sa.type), sizeof(*b)));
|
2013-12-04 21:33:17 +00:00
|
|
|
isc_netaddr_fromsockaddr(&isc_b, &isc_sa);
|
|
|
|
|
|
|
|
return (int)isc_netaddr_eqprefix(&isc_a, &isc_b,
|
|
|
|
(u_int)prefixlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
addr_samesubnet(
|
|
|
|
const sockaddr_u * a,
|
|
|
|
const sockaddr_u * a_mask,
|
|
|
|
const sockaddr_u * b,
|
|
|
|
const sockaddr_u * b_mask
|
|
|
|
)
|
|
|
|
{
|
|
|
|
const u_int32 * pa;
|
|
|
|
const u_int32 * pa_limit;
|
|
|
|
const u_int32 * pb;
|
|
|
|
const u_int32 * pm;
|
|
|
|
size_t loops;
|
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(AF(a) == AF(a_mask));
|
|
|
|
REQUIRE(AF(b) == AF(b_mask));
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* With address and mask families verified to match, comparing
|
|
|
|
* the masks also validates the address's families match.
|
|
|
|
*/
|
|
|
|
if (!SOCK_EQ(a_mask, b_mask))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (IS_IPV6(a)) {
|
|
|
|
loops = sizeof(NSRCADR6(a)) / sizeof(*pa);
|
|
|
|
pa = (const void *)&NSRCADR6(a);
|
|
|
|
pb = (const void *)&NSRCADR6(b);
|
|
|
|
pm = (const void *)&NSRCADR6(a_mask);
|
|
|
|
} else {
|
|
|
|
loops = sizeof(NSRCADR(a)) / sizeof(*pa);
|
|
|
|
pa = (const void *)&NSRCADR(a);
|
|
|
|
pb = (const void *)&NSRCADR(b);
|
|
|
|
pm = (const void *)&NSRCADR(a_mask);
|
|
|
|
}
|
|
|
|
for (pa_limit = pa + loops; pa < pa_limit; pa++, pb++, pm++)
|
|
|
|
if ((*pa & *pm) != (*pb & *pm))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* interface list enumerator - visitor pattern
|
|
|
|
*/
|
|
|
|
void
|
2013-12-04 21:33:17 +00:00
|
|
|
interface_enumerate(
|
2014-12-20 22:52:39 +00:00
|
|
|
interface_receiver_t receiver,
|
2013-12-04 21:33:17 +00:00
|
|
|
void * data
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
interface_info_t ifi;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
ifi.action = IFS_EXISTS;
|
2013-12-04 21:33:17 +00:00
|
|
|
for (ifi.ep = ep_list; ifi.ep != NULL; ifi.ep = ifi.ep->elink)
|
|
|
|
(*receiver)(data, &ifi);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* do standard initialization of interface structure
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
init_interface(
|
|
|
|
endpt *ep
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(*ep);
|
2013-12-04 21:33:17 +00:00
|
|
|
ep->fd = INVALID_SOCKET;
|
|
|
|
ep->bfd = INVALID_SOCKET;
|
|
|
|
ep->phase = sys_interphase;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* create new interface structure initialize from
|
|
|
|
* template structure or via standard initialization
|
|
|
|
* function
|
2004-07-20 15:01:56 +00:00
|
|
|
*/
|
2008-08-18 14:26:05 +00:00
|
|
|
static struct interface *
|
2013-12-04 21:33:17 +00:00
|
|
|
new_interface(
|
|
|
|
struct interface *interface
|
|
|
|
)
|
2004-07-20 15:01:56 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
struct interface * iface;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
iface = emalloc(sizeof(*iface));
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL == interface)
|
2008-08-18 14:26:05 +00:00
|
|
|
init_interface(iface);
|
2013-12-04 21:33:17 +00:00
|
|
|
else /* use the template */
|
|
|
|
memcpy(iface, interface, sizeof(*iface));
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* count every new instance of an interface in the system */
|
|
|
|
iface->ifnum = sys_ifnum++;
|
2008-08-18 14:26:05 +00:00
|
|
|
iface->starttime = current_time;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2016-04-27 05:37:54 +00:00
|
|
|
# ifdef HAVE_IO_COMPLETION_PORT
|
|
|
|
if (!io_completion_port_add_interface(iface)) {
|
|
|
|
msyslog(LOG_EMERG, "cannot register interface with IO engine -- will exit now");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
# endif
|
2008-08-18 14:26:05 +00:00
|
|
|
return iface;
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* return interface storage into free memory pool
|
|
|
|
*/
|
2016-04-27 05:37:54 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
delete_interface(
|
|
|
|
endpt *ep
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2016-04-27 05:37:54 +00:00
|
|
|
# ifdef HAVE_IO_COMPLETION_PORT
|
|
|
|
io_completion_port_remove_interface(ep);
|
|
|
|
# endif
|
2013-12-04 21:33:17 +00:00
|
|
|
free(ep);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* link interface into list of known interfaces
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
add_interface(
|
|
|
|
endpt * ep
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt ** pmclisthead;
|
|
|
|
endpt * scan;
|
|
|
|
endpt * scan_next;
|
|
|
|
endpt * unlinked;
|
|
|
|
sockaddr_u * addr;
|
|
|
|
int ep_local;
|
|
|
|
int scan_local;
|
|
|
|
int same_subnet;
|
|
|
|
int ep_univ_iid; /* iface ID from MAC address */
|
|
|
|
int scan_univ_iid; /* see RFC 4291 */
|
|
|
|
int ep_privacy; /* random local iface ID */
|
|
|
|
int scan_privacy; /* see RFC 4941 */
|
|
|
|
int rc;
|
2009-12-12 22:29:30 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/* Calculate the refid */
|
2013-12-04 21:33:17 +00:00
|
|
|
ep->addr_refid = addr2refid(&ep->sin);
|
2014-12-20 22:52:39 +00:00
|
|
|
/* link at tail so ntpdc -c ifstats index increases each row */
|
|
|
|
LINK_TAIL_SLIST(ep_list, ep, elink, endpt);
|
2013-12-04 21:33:17 +00:00
|
|
|
ninterfaces++;
|
|
|
|
#ifdef MCAST
|
|
|
|
/* the rest is for enabled multicast-capable addresses only */
|
|
|
|
if (ep->ignore_packets || !(INT_MULTICAST & ep->flags) ||
|
|
|
|
INT_LOOPBACK & ep->flags)
|
|
|
|
return;
|
|
|
|
# ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
|
|
|
|
if (AF_INET6 == ep->family)
|
|
|
|
return;
|
|
|
|
# endif
|
|
|
|
pmclisthead = (AF_INET == ep->family)
|
|
|
|
? &mc4_list
|
|
|
|
: &mc6_list;
|
|
|
|
|
|
|
|
if (AF_INET6 == ep->family) {
|
|
|
|
ep_local =
|
|
|
|
IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&ep->sin)) ||
|
|
|
|
IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(&ep->sin));
|
|
|
|
ep_univ_iid = IS_IID_UNIV(&ep->sin);
|
|
|
|
ep_privacy = !!(INT_PRIVACY & ep->flags);
|
|
|
|
} else {
|
|
|
|
ep_local = FALSE;
|
|
|
|
ep_univ_iid = FALSE;
|
|
|
|
ep_privacy = FALSE;
|
2009-12-12 22:29:30 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("add_interface mcast-capable %s%s%s%s\n",
|
|
|
|
stoa(&ep->sin),
|
|
|
|
(ep_local) ? " link/scope-local" : "",
|
|
|
|
(ep_univ_iid) ? " univ-IID" : "",
|
|
|
|
(ep_privacy) ? " privacy" : ""));
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* If we have multiple local addresses on the same network
|
|
|
|
* interface, and some are link- or site-local, do not multicast
|
|
|
|
* out from the link-/site-local addresses by default, to avoid
|
|
|
|
* duplicate manycastclient associations between v6 peers using
|
|
|
|
* link-local and global addresses. link-local can still be
|
|
|
|
* chosen using "nic ignore myv6globalprefix::/64".
|
|
|
|
* Similarly, if we have multiple global addresses from the same
|
|
|
|
* prefix on the same network interface, multicast from one,
|
|
|
|
* preferring EUI-64, then static, then least RFC 4941 privacy
|
|
|
|
* addresses.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
for (scan = *pmclisthead; scan != NULL; scan = scan_next) {
|
|
|
|
scan_next = scan->mclink;
|
|
|
|
if (ep->family != scan->family)
|
|
|
|
continue;
|
|
|
|
if (strcmp(ep->name, scan->name))
|
|
|
|
continue;
|
|
|
|
same_subnet = addr_samesubnet(&ep->sin, &ep->mask,
|
|
|
|
&scan->sin, &scan->mask);
|
|
|
|
if (AF_INET6 == ep->family) {
|
|
|
|
addr = &scan->sin;
|
|
|
|
scan_local =
|
|
|
|
IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(addr)) ||
|
|
|
|
IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(addr));
|
|
|
|
scan_univ_iid = IS_IID_UNIV(addr);
|
|
|
|
scan_privacy = !!(INT_PRIVACY & scan->flags);
|
|
|
|
} else {
|
|
|
|
scan_local = FALSE;
|
|
|
|
scan_univ_iid = FALSE;
|
|
|
|
scan_privacy = FALSE;
|
|
|
|
}
|
|
|
|
DPRINTF(4, ("add_interface mcast-capable scan %s%s%s%s\n",
|
|
|
|
stoa(&scan->sin),
|
|
|
|
(scan_local) ? " link/scope-local" : "",
|
|
|
|
(scan_univ_iid) ? " univ-IID" : "",
|
|
|
|
(scan_privacy) ? " privacy" : ""));
|
2014-12-20 22:52:39 +00:00
|
|
|
if ((ep_local && !scan_local) || (same_subnet &&
|
2013-12-04 21:33:17 +00:00
|
|
|
((ep_privacy && !scan_privacy) ||
|
|
|
|
(!ep_univ_iid && scan_univ_iid)))) {
|
|
|
|
DPRINTF(4, ("did not add %s to %s of IPv6 multicast-capable list which already has %s\n",
|
|
|
|
stoa(&ep->sin),
|
|
|
|
(ep_local)
|
|
|
|
? "tail"
|
|
|
|
: "head",
|
|
|
|
stoa(&scan->sin)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((scan_local && !ep_local) || (same_subnet &&
|
|
|
|
((scan_privacy && !ep_privacy) ||
|
|
|
|
(!scan_univ_iid && ep_univ_iid)))) {
|
|
|
|
UNLINK_SLIST(unlinked, *pmclisthead,
|
|
|
|
scan, mclink, endpt);
|
|
|
|
DPRINTF(4, ("%s %s from IPv6 multicast-capable list to add %s\n",
|
|
|
|
(unlinked != scan)
|
|
|
|
? "Failed to remove"
|
|
|
|
: "removed",
|
|
|
|
stoa(&scan->sin), stoa(&ep->sin)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Add link/site local at the tail of the multicast-
|
|
|
|
* capable unicast interfaces list, so that ntpd will
|
|
|
|
* send from global addresses before link-/site-local
|
|
|
|
* ones.
|
|
|
|
*/
|
|
|
|
if (ep_local)
|
|
|
|
LINK_TAIL_SLIST(*pmclisthead, ep, mclink, endpt);
|
|
|
|
else
|
|
|
|
LINK_SLIST(*pmclisthead, ep, mclink);
|
|
|
|
DPRINTF(4, ("added %s to %s of IPv%s multicast-capable unicast local address list\n",
|
|
|
|
stoa(&ep->sin),
|
|
|
|
(ep_local)
|
|
|
|
? "tail"
|
|
|
|
: "head",
|
|
|
|
(AF_INET == ep->family)
|
|
|
|
? "4"
|
|
|
|
: "6"));
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (INVALID_SOCKET == ep->fd)
|
|
|
|
return;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* select the local address from which to send to multicast.
|
|
|
|
*/
|
|
|
|
switch (AF(&ep->sin)) {
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
case AF_INET :
|
|
|
|
rc = setsockopt(ep->fd, IPPROTO_IP,
|
|
|
|
IP_MULTICAST_IF,
|
|
|
|
(void *)&NSRCADR(&ep->sin),
|
|
|
|
sizeof(NSRCADR(&ep->sin)));
|
|
|
|
if (rc)
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IP_MULTICAST_IF %s fails: %m",
|
|
|
|
stoa(&ep->sin));
|
|
|
|
break;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
# ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
|
|
|
|
case AF_INET6 :
|
|
|
|
rc = setsockopt(ep->fd, IPPROTO_IPV6,
|
|
|
|
IPV6_MULTICAST_IF,
|
|
|
|
(void *)&ep->ifindex,
|
|
|
|
sizeof(ep->ifindex));
|
2014-12-20 22:52:39 +00:00
|
|
|
/* do not complain if bound addr scope is ifindex */
|
|
|
|
if (rc && ep->ifindex != SCOPE(&ep->sin))
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IPV6_MULTICAST_IF %u for %s fails: %m",
|
|
|
|
ep->ifindex, stoa(&ep->sin));
|
|
|
|
break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
#endif /* MCAST */
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* remove interface from known interface list and clean up
|
|
|
|
* associated resources
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
remove_interface(
|
|
|
|
endpt * ep
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt * unlinked;
|
|
|
|
endpt ** pmclisthead;
|
|
|
|
sockaddr_u resmask;
|
|
|
|
|
|
|
|
UNLINK_SLIST(unlinked, ep_list, ep, elink, endpt);
|
|
|
|
if (!ep->ignore_packets && INT_MULTICAST & ep->flags) {
|
|
|
|
pmclisthead = (AF_INET == ep->family)
|
|
|
|
? &mc4_list
|
|
|
|
: &mc6_list;
|
|
|
|
UNLINK_SLIST(unlinked, *pmclisthead, ep, mclink, endpt);
|
|
|
|
DPRINTF(4, ("%s %s IPv%s multicast-capable unicast local address list\n",
|
|
|
|
stoa(&ep->sin),
|
|
|
|
(unlinked != NULL)
|
|
|
|
? "removed from"
|
|
|
|
: "not found on",
|
|
|
|
(AF_INET == ep->family)
|
|
|
|
? "4"
|
|
|
|
: "6"));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
delete_interface_from_list(ep);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (ep->fd != INVALID_SOCKET) {
|
|
|
|
msyslog(LOG_INFO,
|
|
|
|
"Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
|
|
|
|
ep->ifnum,
|
|
|
|
ep->name,
|
|
|
|
stoa(&ep->sin),
|
|
|
|
SRCPORT(&ep->sin),
|
|
|
|
ep->received,
|
|
|
|
ep->sent,
|
|
|
|
ep->notsent,
|
|
|
|
current_time - ep->starttime);
|
2016-04-27 05:37:54 +00:00
|
|
|
# ifdef HAVE_IO_COMPLETION_PORT
|
|
|
|
io_completion_port_remove_socket(ep->fd, ep);
|
|
|
|
# endif
|
2013-12-04 21:33:17 +00:00
|
|
|
close_and_delete_fd_from_list(ep->fd);
|
2014-12-20 22:52:39 +00:00
|
|
|
ep->fd = INVALID_SOCKET;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ep->bfd != INVALID_SOCKET) {
|
|
|
|
msyslog(LOG_INFO,
|
2014-12-20 22:52:39 +00:00
|
|
|
"stop listening for broadcasts to %s on interface #%d %s",
|
|
|
|
stoa(&ep->bcast), ep->ifnum, ep->name);
|
2016-04-27 05:37:54 +00:00
|
|
|
# ifdef HAVE_IO_COMPLETION_PORT
|
|
|
|
io_completion_port_remove_socket(ep->bfd, ep);
|
|
|
|
# endif
|
2013-12-04 21:33:17 +00:00
|
|
|
close_and_delete_fd_from_list(ep->bfd);
|
2014-12-20 22:52:39 +00:00
|
|
|
ep->bfd = INVALID_SOCKET;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2016-04-27 05:37:54 +00:00
|
|
|
# ifdef HAVE_IO_COMPLETION_PORT
|
|
|
|
io_completion_port_remove_interface(ep);
|
|
|
|
# endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
ninterfaces--;
|
2014-12-20 22:52:39 +00:00
|
|
|
mon_clearinterface(ep);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/* remove restrict interface entry */
|
2013-12-04 21:33:17 +00:00
|
|
|
SET_HOSTMASK(&resmask, AF(&ep->sin));
|
|
|
|
hack_restrict(RESTRICT_REMOVEIF, &ep->sin, &resmask,
|
2018-02-28 06:23:12 +00:00
|
|
|
-3, RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
log_listen_address(
|
|
|
|
endpt * ep
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_INFO, "%s on %d %s %s",
|
|
|
|
(ep->ignore_packets)
|
2013-12-04 21:33:17 +00:00
|
|
|
? "Listen and drop"
|
|
|
|
: "Listen normally",
|
|
|
|
ep->ifnum,
|
|
|
|
ep->name,
|
2014-12-20 22:52:39 +00:00
|
|
|
sptoa(&ep->sin));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
create_wildcards(
|
|
|
|
u_short port
|
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
int v4wild;
|
|
|
|
#ifdef INCLUDE_IPV6_SUPPORT
|
|
|
|
int v6wild;
|
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u wildaddr;
|
|
|
|
nic_rule_action action;
|
|
|
|
struct interface * wildif;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* silence "potentially uninitialized" warnings from VC9
|
|
|
|
* failing to follow the logic. Ideally action could remain
|
|
|
|
* uninitialized, and the memset be the first statement under
|
|
|
|
* the first if (v4wild).
|
|
|
|
*/
|
|
|
|
action = ACTION_LISTEN;
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(wildaddr);
|
|
|
|
|
|
|
|
#ifdef INCLUDE_IPV6_SUPPORT
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* create pseudo-interface with wildcard IPv6 address
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
v6wild = ipv6_works;
|
|
|
|
if (v6wild) {
|
|
|
|
/* set wildaddr to the v6 wildcard address :: */
|
|
|
|
ZERO(wildaddr);
|
|
|
|
AF(&wildaddr) = AF_INET6;
|
|
|
|
SET_ADDR6N(&wildaddr, in6addr_any);
|
2013-12-04 21:33:17 +00:00
|
|
|
SET_PORT(&wildaddr, port);
|
2014-12-20 22:52:39 +00:00
|
|
|
SET_SCOPE(&wildaddr, 0);
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/* check for interface/nic rules affecting the wildcard */
|
|
|
|
action = interface_action(NULL, &wildaddr, 0);
|
2014-12-20 22:52:39 +00:00
|
|
|
v6wild = (ACTION_IGNORE != action);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
if (v6wild) {
|
2013-12-04 21:33:17 +00:00
|
|
|
wildif = new_interface(NULL);
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
strlcpy(wildif->name, "v6wildcard", sizeof(wildif->name));
|
2013-12-04 21:33:17 +00:00
|
|
|
memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
|
2014-12-20 22:52:39 +00:00
|
|
|
wildif->family = AF_INET6;
|
|
|
|
AF(&wildif->mask) = AF_INET6;
|
2013-12-04 21:33:17 +00:00
|
|
|
SET_ONESMASK(&wildif->mask);
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
wildif->flags = INT_UP | INT_WILDCARD;
|
2013-12-04 21:33:17 +00:00
|
|
|
wildif->ignore_packets = (ACTION_DROP == action);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (wildif->fd != INVALID_SOCKET) {
|
2014-12-20 22:52:39 +00:00
|
|
|
wildipv6 = wildif;
|
|
|
|
any6_interface = wildif;
|
2013-12-04 21:33:17 +00:00
|
|
|
add_addr_to_list(&wildif->sin, wildif);
|
|
|
|
add_interface(wildif);
|
|
|
|
log_listen_address(wildif);
|
2008-08-18 14:26:05 +00:00
|
|
|
} else {
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
2013-12-04 21:33:17 +00:00
|
|
|
"unable to bind to wildcard address %s - another process may be running - EXITING",
|
|
|
|
stoa(&wildif->sin));
|
2008-08-18 14:26:05 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* create pseudo-interface with wildcard IPv4 address
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
v4wild = ipv4_works;
|
|
|
|
if (v4wild) {
|
|
|
|
/* set wildaddr to the v4 wildcard address 0.0.0.0 */
|
|
|
|
AF(&wildaddr) = AF_INET;
|
|
|
|
SET_ADDR4N(&wildaddr, INADDR_ANY);
|
2013-12-04 21:33:17 +00:00
|
|
|
SET_PORT(&wildaddr, port);
|
|
|
|
|
|
|
|
/* check for interface/nic rules affecting the wildcard */
|
|
|
|
action = interface_action(NULL, &wildaddr, 0);
|
2014-12-20 22:52:39 +00:00
|
|
|
v4wild = (ACTION_IGNORE != action);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
if (v4wild) {
|
2013-12-04 21:33:17 +00:00
|
|
|
wildif = new_interface(NULL);
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
strlcpy(wildif->name, "v4wildcard", sizeof(wildif->name));
|
2013-12-04 21:33:17 +00:00
|
|
|
memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
|
2014-12-20 22:52:39 +00:00
|
|
|
wildif->family = AF_INET;
|
|
|
|
AF(&wildif->mask) = AF_INET;
|
2013-12-04 21:33:17 +00:00
|
|
|
SET_ONESMASK(&wildif->mask);
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
wildif->flags = INT_BROADCAST | INT_UP | INT_WILDCARD;
|
2013-12-04 21:33:17 +00:00
|
|
|
wildif->ignore_packets = (ACTION_DROP == action);
|
2014-12-20 22:52:39 +00:00
|
|
|
#if defined(MCAST)
|
|
|
|
/*
|
|
|
|
* enable multicast reception on the broadcast socket
|
|
|
|
*/
|
|
|
|
AF(&wildif->bcast) = AF_INET;
|
|
|
|
SET_ADDR4N(&wildif->bcast, INADDR_ANY);
|
|
|
|
SET_PORT(&wildif->bcast, port);
|
|
|
|
#endif /* MCAST */
|
2013-12-04 21:33:17 +00:00
|
|
|
wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
|
|
|
|
|
|
|
|
if (wildif->fd != INVALID_SOCKET) {
|
2014-12-20 22:52:39 +00:00
|
|
|
wildipv4 = wildif;
|
|
|
|
any_interface = wildif;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
add_addr_to_list(&wildif->sin, wildif);
|
|
|
|
add_interface(wildif);
|
|
|
|
log_listen_address(wildif);
|
2008-08-18 14:26:05 +00:00
|
|
|
} else {
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"unable to bind to wildcard address %s - another process may be running - EXITING",
|
|
|
|
stoa(&wildif->sin));
|
2008-08-18 14:26:05 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* add_nic_rule() -- insert a rule entry at the head of nic_rule_list.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
add_nic_rule(
|
|
|
|
nic_rule_match match_type,
|
|
|
|
const char * if_name, /* interface name or numeric address */
|
|
|
|
int prefixlen,
|
|
|
|
nic_rule_action action
|
|
|
|
)
|
|
|
|
{
|
|
|
|
nic_rule * rule;
|
|
|
|
isc_boolean_t is_ip;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
rule = emalloc_zero(sizeof(*rule));
|
2013-12-04 21:33:17 +00:00
|
|
|
rule->match_type = match_type;
|
|
|
|
rule->prefixlen = prefixlen;
|
|
|
|
rule->action = action;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (MATCH_IFNAME == match_type) {
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(NULL != if_name);
|
2013-12-04 21:33:17 +00:00
|
|
|
rule->if_name = estrdup(if_name);
|
|
|
|
} else if (MATCH_IFADDR == match_type) {
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(NULL != if_name);
|
2013-12-04 21:33:17 +00:00
|
|
|
/* set rule->addr */
|
2014-12-20 22:52:39 +00:00
|
|
|
is_ip = is_ip_address(if_name, AF_UNSPEC, &rule->addr);
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(is_ip);
|
2013-12-04 21:33:17 +00:00
|
|
|
} else
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(NULL == if_name);
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
LINK_SLIST(nic_rule_list, rule, next);
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
static const char *
|
|
|
|
action_text(
|
|
|
|
nic_rule_action action
|
|
|
|
)
|
|
|
|
{
|
|
|
|
const char *t;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
t = "ERROR"; /* quiet uninit warning */
|
|
|
|
DPRINTF(1, ("fatal: unknown nic_rule_action %d\n",
|
|
|
|
action));
|
2015-10-21 19:16:13 +00:00
|
|
|
ENSURE(0);
|
2013-12-04 21:33:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_LISTEN:
|
|
|
|
t = "listen";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_IGNORE:
|
|
|
|
t = "ignore";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_DROP:
|
|
|
|
t = "drop";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
|
|
|
|
static nic_rule_action
|
|
|
|
interface_action(
|
|
|
|
char * if_name,
|
|
|
|
sockaddr_u * if_addr,
|
|
|
|
u_int32 if_flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
nic_rule * rule;
|
|
|
|
int isloopback;
|
|
|
|
int iswildcard;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("interface_action: interface %s ",
|
|
|
|
(if_name != NULL) ? if_name : "wildcard"));
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
iswildcard = is_wildcard_addr(if_addr);
|
2014-12-20 22:52:39 +00:00
|
|
|
isloopback = !!(INT_LOOPBACK & if_flags);
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find any matching NIC rule from --interface / -I or ntp.conf
|
|
|
|
* interface/nic rules.
|
|
|
|
*/
|
|
|
|
for (rule = nic_rule_list; rule != NULL; rule = rule->next) {
|
|
|
|
|
|
|
|
switch (rule->match_type) {
|
|
|
|
|
|
|
|
case MATCH_ALL:
|
|
|
|
/* loopback and wildcard excluded from "all" */
|
|
|
|
if (isloopback || iswildcard)
|
|
|
|
break;
|
|
|
|
DPRINTF(4, ("nic all %s\n",
|
|
|
|
action_text(rule->action)));
|
|
|
|
return rule->action;
|
|
|
|
|
|
|
|
case MATCH_IPV4:
|
|
|
|
if (IS_IPV4(if_addr)) {
|
|
|
|
DPRINTF(4, ("nic ipv4 %s\n",
|
|
|
|
action_text(rule->action)));
|
|
|
|
return rule->action;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATCH_IPV6:
|
|
|
|
if (IS_IPV6(if_addr)) {
|
|
|
|
DPRINTF(4, ("nic ipv6 %s\n",
|
|
|
|
action_text(rule->action)));
|
|
|
|
return rule->action;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATCH_WILDCARD:
|
|
|
|
if (iswildcard) {
|
|
|
|
DPRINTF(4, ("nic wildcard %s\n",
|
|
|
|
action_text(rule->action)));
|
|
|
|
return rule->action;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATCH_IFADDR:
|
|
|
|
if (rule->prefixlen != -1) {
|
|
|
|
if (addr_eqprefix(if_addr, &rule->addr,
|
|
|
|
rule->prefixlen)) {
|
|
|
|
|
|
|
|
DPRINTF(4, ("subnet address match - %s\n",
|
|
|
|
action_text(rule->action)));
|
|
|
|
return rule->action;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (SOCK_EQ(if_addr, &rule->addr)) {
|
|
|
|
|
|
|
|
DPRINTF(4, ("address match - %s\n",
|
|
|
|
action_text(rule->action)));
|
|
|
|
return rule->action;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATCH_IFNAME:
|
|
|
|
if (if_name != NULL
|
2014-12-20 22:52:39 +00:00
|
|
|
#if defined(HAVE_FNMATCH) && defined(FNM_CASEFOLD)
|
|
|
|
&& !fnmatch(rule->if_name, if_name, FNM_CASEFOLD)
|
|
|
|
#else
|
|
|
|
&& !strcasecmp(if_name, rule->if_name)
|
|
|
|
#endif
|
|
|
|
) {
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
DPRINTF(4, ("interface name match - %s\n",
|
|
|
|
action_text(rule->action)));
|
|
|
|
return rule->action;
|
|
|
|
}
|
|
|
|
break;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Unless explicitly disabled such as with "nic ignore ::1"
|
|
|
|
* listen on loopback addresses. Since ntpq and ntpdc query
|
|
|
|
* "localhost" by default, which typically resolves to ::1 and
|
|
|
|
* 127.0.0.1, it's useful to default to listening on both.
|
|
|
|
*/
|
|
|
|
if (isloopback) {
|
|
|
|
DPRINTF(4, ("default loopback listen\n"));
|
|
|
|
return ACTION_LISTEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Treat wildcard addresses specially. If there is no explicit
|
|
|
|
* "nic ... wildcard" or "nic ... 0.0.0.0" or "nic ... ::" rule
|
|
|
|
* default to drop.
|
|
|
|
*/
|
|
|
|
if (iswildcard) {
|
|
|
|
DPRINTF(4, ("default wildcard drop\n"));
|
|
|
|
return ACTION_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for "virtual IP" (colon in the interface name) after
|
|
|
|
* the rules so that "ntpd --interface eth0:1 -novirtualips"
|
|
|
|
* does indeed listen on eth0:1's addresses.
|
|
|
|
*/
|
|
|
|
if (!listen_to_virtual_ips && if_name != NULL
|
|
|
|
&& (strchr(if_name, ':') != NULL)) {
|
|
|
|
|
|
|
|
DPRINTF(4, ("virtual ip - ignore\n"));
|
|
|
|
return ACTION_IGNORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are no --interface/-I command-line options and no
|
|
|
|
* interface/nic rules in ntp.conf, the default action is to
|
|
|
|
* listen. In the presence of rules from either, the default
|
|
|
|
* is to ignore. This implements ntpd's traditional listen-
|
|
|
|
* every default with no interface listen configuration, and
|
|
|
|
* ensures a single -I eth0 or "nic listen eth0" means do not
|
|
|
|
* listen on any other addresses.
|
|
|
|
*/
|
|
|
|
if (NULL == nic_rule_list) {
|
|
|
|
DPRINTF(4, ("default listen\n"));
|
|
|
|
return ACTION_LISTEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(4, ("implicit ignore\n"));
|
|
|
|
return ACTION_IGNORE;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
convert_isc_if(
|
|
|
|
isc_interface_t *isc_if,
|
|
|
|
endpt *itf,
|
|
|
|
u_short port
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
const u_char v6loop[16] = {0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 1};
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
strlcpy(itf->name, isc_if->name, sizeof(itf->name));
|
2013-12-04 21:33:17 +00:00
|
|
|
itf->ifindex = isc_if->ifindex;
|
|
|
|
itf->family = (u_short)isc_if->af;
|
|
|
|
AF(&itf->sin) = itf->family;
|
|
|
|
AF(&itf->mask) = itf->family;
|
|
|
|
AF(&itf->bcast) = itf->family;
|
|
|
|
SET_PORT(&itf->sin, port);
|
|
|
|
SET_PORT(&itf->mask, port);
|
|
|
|
SET_PORT(&itf->bcast, port);
|
|
|
|
|
|
|
|
if (IS_IPV4(&itf->sin)) {
|
|
|
|
NSRCADR(&itf->sin) = isc_if->address.type.in.s_addr;
|
|
|
|
NSRCADR(&itf->mask) = isc_if->netmask.type.in.s_addr;
|
|
|
|
|
|
|
|
if (isc_if->flags & INTERFACE_F_BROADCAST) {
|
2008-08-18 14:26:05 +00:00
|
|
|
itf->flags |= INT_BROADCAST;
|
2014-12-20 22:52:39 +00:00
|
|
|
NSRCADR(&itf->bcast) =
|
2013-12-04 21:33:17 +00:00
|
|
|
isc_if->broadcast.type.in.s_addr;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef INCLUDE_IPV6_SUPPORT
|
2013-12-04 21:33:17 +00:00
|
|
|
else if (IS_IPV6(&itf->sin)) {
|
|
|
|
SET_ADDR6N(&itf->sin, isc_if->address.type.in6);
|
|
|
|
SET_ADDR6N(&itf->mask, isc_if->netmask.type.in6);
|
|
|
|
|
|
|
|
SET_SCOPE(&itf->sin, isc_if->address.zone);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
#endif /* INCLUDE_IPV6_SUPPORT */
|
|
|
|
|
|
|
|
|
|
|
|
/* Process the rest of the flags */
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
itf->flags |=
|
|
|
|
((INTERFACE_F_UP & isc_if->flags)
|
|
|
|
? INT_UP : 0)
|
2014-12-20 22:52:39 +00:00
|
|
|
| ((INTERFACE_F_LOOPBACK & isc_if->flags)
|
2013-12-04 21:33:17 +00:00
|
|
|
? INT_LOOPBACK : 0)
|
2014-12-20 22:52:39 +00:00
|
|
|
| ((INTERFACE_F_POINTTOPOINT & isc_if->flags)
|
2013-12-04 21:33:17 +00:00
|
|
|
? INT_PPP : 0)
|
2014-12-20 22:52:39 +00:00
|
|
|
| ((INTERFACE_F_MULTICAST & isc_if->flags)
|
2013-12-04 21:33:17 +00:00
|
|
|
? INT_MULTICAST : 0)
|
|
|
|
| ((INTERFACE_F_PRIVACY & isc_if->flags)
|
|
|
|
? INT_PRIVACY : 0)
|
|
|
|
;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Clear the loopback flag if the address is not localhost.
|
|
|
|
* http://bugs.ntp.org/1683
|
|
|
|
*/
|
|
|
|
if (INT_LOOPBACK & itf->flags) {
|
|
|
|
if (AF_INET == itf->family) {
|
|
|
|
if (127 != (SRCADR(&itf->sin) >> 24))
|
|
|
|
itf->flags &= ~INT_LOOPBACK;
|
|
|
|
} else {
|
|
|
|
if (memcmp(v6loop, NSRCADR6(&itf->sin),
|
|
|
|
sizeof(NSRCADR6(&itf->sin))))
|
|
|
|
itf->flags &= ~INT_LOOPBACK;
|
|
|
|
}
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* refresh_interface
|
|
|
|
*
|
|
|
|
* some OSes have been observed to keep
|
|
|
|
* cached routes even when more specific routes
|
|
|
|
* become available.
|
|
|
|
* this can be mitigated by re-binding
|
|
|
|
* the socket.
|
|
|
|
*/
|
|
|
|
static int
|
2013-12-04 21:33:17 +00:00
|
|
|
refresh_interface(
|
|
|
|
struct interface * interface
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
#ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
|
2013-12-04 21:33:17 +00:00
|
|
|
if (interface->fd != INVALID_SOCKET) {
|
2014-12-20 22:52:39 +00:00
|
|
|
int bcast = (interface->flags & INT_BCASTXMIT) != 0;
|
|
|
|
/* as we forcibly close() the socket remove the
|
|
|
|
broadcast permission indication */
|
|
|
|
if (bcast)
|
|
|
|
socket_broadcast_disable(interface, &interface->sin);
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
close_and_delete_fd_from_list(interface->fd);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
/* create new socket picking up a new first hop binding
|
|
|
|
at connect() time */
|
2008-08-18 14:26:05 +00:00
|
|
|
interface->fd = open_socket(&interface->sin,
|
2014-12-20 22:52:39 +00:00
|
|
|
bcast, 0, interface);
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* reset TTL indication so TTL is is set again
|
2008-08-18 14:26:05 +00:00
|
|
|
* next time around
|
|
|
|
*/
|
|
|
|
interface->last_ttl = 0;
|
2013-12-04 21:33:17 +00:00
|
|
|
return (interface->fd != INVALID_SOCKET);
|
|
|
|
} else
|
2008-08-18 14:26:05 +00:00
|
|
|
return 0; /* invalid sockets are not refreshable */
|
|
|
|
#else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
|
2013-12-04 21:33:17 +00:00
|
|
|
return (interface->fd != INVALID_SOCKET);
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* interface_update - externally callable update function
|
|
|
|
*/
|
|
|
|
void
|
2013-12-04 21:33:17 +00:00
|
|
|
interface_update(
|
|
|
|
interface_receiver_t receiver,
|
|
|
|
void * data)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
int new_interface_found;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (disable_dynamic_updates)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BLOCKIO();
|
|
|
|
new_interface_found = update_interfaces(NTP_PORT, receiver, data);
|
|
|
|
UNBLOCKIO();
|
|
|
|
|
|
|
|
if (!new_interface_found)
|
|
|
|
return;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
interrupt_worker_sleep();
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* sau_from_netaddr() - convert network address on-wire formats.
|
|
|
|
* Convert from libisc's isc_netaddr_t to NTP's sockaddr_u
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
void
|
|
|
|
sau_from_netaddr(
|
|
|
|
sockaddr_u *psau,
|
|
|
|
const isc_netaddr_t *pna
|
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO_SOCK(psau);
|
2013-12-04 21:33:17 +00:00
|
|
|
AF(psau) = (u_short)pna->family;
|
|
|
|
switch (pna->family) {
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
memcpy(&psau->sa4.sin_addr, &pna->type.in,
|
|
|
|
sizeof(psau->sa4.sin_addr));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(&psau->sa6.sin6_addr, &pna->type.in6,
|
|
|
|
sizeof(psau->sa6.sin6_addr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static int
|
2013-12-04 21:33:17 +00:00
|
|
|
is_wildcard_addr(
|
|
|
|
const sockaddr_u *psau
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV4(psau) && !NSRCADR(psau))
|
2008-08-18 14:26:05 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
#ifdef INCLUDE_IPV6_SUPPORT
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV6(psau) && S_ADDR6_EQ(psau, &in6addr_any))
|
2008-08-18 14:26:05 +00:00
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
|
|
|
|
/*
|
|
|
|
* enable/disable re-use of wildcard address socket
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
set_wildcard_reuse(
|
|
|
|
u_short family,
|
|
|
|
int on
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
struct interface *any;
|
2008-08-18 14:26:05 +00:00
|
|
|
SOCKET fd = INVALID_SOCKET;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
any = ANY_INTERFACE_BYFAM(family);
|
|
|
|
if (any != NULL)
|
|
|
|
fd = any->fd;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (fd != INVALID_SOCKET) {
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on, sizeof(on)))
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m",
|
|
|
|
on ? "on" : "off");
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n",
|
2013-12-04 21:33:17 +00:00
|
|
|
on ? "on" : "off",
|
|
|
|
stoa(&any->sin)));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
#endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2018-08-20 06:07:33 +00:00
|
|
|
static isc_boolean_t
|
|
|
|
check_flags(
|
|
|
|
sockaddr_u *psau,
|
|
|
|
const char *name,
|
|
|
|
u_int32 flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
#if defined(SIOCGIFAFLAG_IN)
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (psau->sa.sa_family != AF_INET)
|
|
|
|
return ISC_FALSE;
|
|
|
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
|
|
return ISC_FALSE;
|
|
|
|
ZERO(ifr);
|
|
|
|
memcpy(&ifr.ifr_addr, &psau->sa, sizeof(ifr.ifr_addr));
|
|
|
|
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
|
|
|
if (ioctl(fd, SIOCGIFAFLAG_IN, &ifr) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return ISC_FALSE;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if ((ifr.ifr_addrflags & flags) != 0)
|
|
|
|
return ISC_TRUE;
|
|
|
|
#endif /* SIOCGIFAFLAG_IN */
|
|
|
|
return ISC_FALSE;
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
static isc_boolean_t
|
2015-02-05 20:53:33 +00:00
|
|
|
check_flags6(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *psau,
|
2015-02-05 20:53:33 +00:00
|
|
|
const char *name,
|
|
|
|
u_int32 flags6
|
2013-12-04 21:33:17 +00:00
|
|
|
)
|
|
|
|
{
|
2015-02-05 20:53:33 +00:00
|
|
|
#if defined(INCLUDE_IPV6_SUPPORT) && defined(SIOCGIFAFLAG_IN6)
|
2013-12-04 21:33:17 +00:00
|
|
|
struct in6_ifreq ifr6;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (psau->sa.sa_family != AF_INET6)
|
|
|
|
return ISC_FALSE;
|
|
|
|
if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
|
|
|
|
return ISC_FALSE;
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(ifr6);
|
2013-12-04 21:33:17 +00:00
|
|
|
memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr));
|
2014-12-20 22:52:39 +00:00
|
|
|
strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
|
2013-12-04 21:33:17 +00:00
|
|
|
if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return ISC_FALSE;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
close(fd);
|
2015-02-05 20:53:33 +00:00
|
|
|
if ((ifr6.ifr_ifru.ifru_flags6 & flags6) != 0)
|
2013-12-04 21:33:17 +00:00
|
|
|
return ISC_TRUE;
|
2015-02-05 20:53:33 +00:00
|
|
|
#endif /* INCLUDE_IPV6_SUPPORT && SIOCGIFAFLAG_IN6 */
|
2013-12-04 21:33:17 +00:00
|
|
|
return ISC_FALSE;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2015-02-05 20:53:33 +00:00
|
|
|
static isc_boolean_t
|
|
|
|
is_anycast(
|
|
|
|
sockaddr_u *psau,
|
|
|
|
const char *name
|
|
|
|
)
|
|
|
|
{
|
|
|
|
#ifdef IN6_IFF_ANYCAST
|
|
|
|
return check_flags6(psau, name, IN6_IFF_ANYCAST);
|
|
|
|
#else
|
|
|
|
return ISC_FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_boolean_t
|
|
|
|
is_valid(
|
|
|
|
sockaddr_u *psau,
|
|
|
|
const char *name
|
|
|
|
)
|
|
|
|
{
|
2018-08-20 06:07:33 +00:00
|
|
|
u_int32 flags;
|
2015-02-05 20:53:33 +00:00
|
|
|
|
2018-08-20 06:07:33 +00:00
|
|
|
flags = 0;
|
|
|
|
switch (psau->sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
#ifdef IN_IFF_DETACHED
|
|
|
|
flags |= IN_IFF_DETACHED;
|
|
|
|
#endif
|
|
|
|
#ifdef IN_IFF_TENTATIVE
|
|
|
|
flags |= IN_IFF_TENTATIVE;
|
|
|
|
#endif
|
|
|
|
return check_flags(psau, name, flags) ? ISC_FALSE : ISC_TRUE;
|
|
|
|
case AF_INET6:
|
2015-02-05 20:53:33 +00:00
|
|
|
#ifdef IN6_IFF_DEPARTED
|
2018-08-20 06:07:33 +00:00
|
|
|
flags |= IN6_IFF_DEPARTED;
|
2015-02-05 20:53:33 +00:00
|
|
|
#endif
|
|
|
|
#ifdef IN6_IFF_DETACHED
|
2018-08-20 06:07:33 +00:00
|
|
|
flags |= IN6_IFF_DETACHED;
|
2015-02-05 20:53:33 +00:00
|
|
|
#endif
|
|
|
|
#ifdef IN6_IFF_TENTATIVE
|
2018-08-20 06:07:33 +00:00
|
|
|
flags |= IN6_IFF_TENTATIVE;
|
2015-02-05 20:53:33 +00:00
|
|
|
#endif
|
2018-08-20 06:07:33 +00:00
|
|
|
return check_flags6(psau, name, flags) ? ISC_FALSE : ISC_TRUE;
|
|
|
|
default:
|
|
|
|
return ISC_FALSE;
|
|
|
|
}
|
2015-02-05 20:53:33 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* update_interface strategy
|
|
|
|
*
|
|
|
|
* toggle configuration phase
|
|
|
|
*
|
|
|
|
* Phase 1:
|
|
|
|
* forall currently existing interfaces
|
|
|
|
* if address is known:
|
2013-12-04 21:33:17 +00:00
|
|
|
* drop socket - rebind again
|
2008-08-18 14:26:05 +00:00
|
|
|
*
|
|
|
|
* if address is NOT known:
|
2013-12-04 21:33:17 +00:00
|
|
|
* attempt to create a new interface entry
|
2008-08-18 14:26:05 +00:00
|
|
|
*
|
|
|
|
* Phase 2:
|
|
|
|
* forall currently known non MCAST and WILDCARD interfaces
|
|
|
|
* if interface does not match configuration phase (not seen in phase 1):
|
2013-12-04 21:33:17 +00:00
|
|
|
* remove interface from known interface list
|
|
|
|
* forall peers associated with this interface
|
|
|
|
* disconnect peer from this interface
|
2008-08-18 14:26:05 +00:00
|
|
|
*
|
|
|
|
* Phase 3:
|
|
|
|
* attempt to re-assign interfaces to peers
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
update_interfaces(
|
2013-12-04 21:33:17 +00:00
|
|
|
u_short port,
|
|
|
|
interface_receiver_t receiver,
|
|
|
|
void * data
|
2008-08-18 14:26:05 +00:00
|
|
|
)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
isc_mem_t * mctx = (void *)-1;
|
|
|
|
interface_info_t ifi;
|
|
|
|
isc_interfaceiter_t * iter;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_interface_t isc_if;
|
|
|
|
int new_interface_found;
|
|
|
|
unsigned int family;
|
|
|
|
endpt enumep;
|
|
|
|
endpt * ep;
|
|
|
|
endpt * next_ep;
|
|
|
|
|
|
|
|
DPRINTF(3, ("update_interfaces(%d)\n", port));
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* phase one - scan interfaces
|
|
|
|
* - create those that are not found
|
|
|
|
* - update those that are found
|
|
|
|
*/
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
new_interface_found = FALSE;
|
|
|
|
iter = NULL;
|
2004-07-20 15:01:56 +00:00
|
|
|
result = isc_interfaceiter_create(mctx, &iter);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2008-08-18 14:26:05 +00:00
|
|
|
return 0;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* Toggle system interface scan phase to find untouched
|
|
|
|
* interfaces to be deleted.
|
|
|
|
*/
|
|
|
|
sys_interphase ^= 0x1;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
for (result = isc_interfaceiter_first(iter);
|
2013-12-04 21:33:17 +00:00
|
|
|
ISC_R_SUCCESS == result;
|
|
|
|
result = isc_interfaceiter_next(iter)) {
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
result = isc_interfaceiter_current(iter, &isc_if);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
break;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/* See if we have a valid family to use */
|
|
|
|
family = isc_if.address.family;
|
2013-12-04 21:33:17 +00:00
|
|
|
if (AF_INET != family && AF_INET6 != family)
|
1999-12-09 13:01:21 +00:00
|
|
|
continue;
|
2013-12-04 21:33:17 +00:00
|
|
|
if (AF_INET == family && !ipv4_works)
|
1999-12-09 13:01:21 +00:00
|
|
|
continue;
|
2013-12-04 21:33:17 +00:00
|
|
|
if (AF_INET6 == family && !ipv6_works)
|
1999-12-09 13:01:21 +00:00
|
|
|
continue;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* create prototype */
|
|
|
|
init_interface(&enumep);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
convert_isc_if(&isc_if, &enumep, port);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINT_INTERFACE(4, (&enumep, "examining ", "\n"));
|
|
|
|
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* Check if and how we are going to use the interface.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
switch (interface_action(enumep.name, &enumep.sin,
|
|
|
|
enumep.flags)) {
|
|
|
|
|
|
|
|
case ACTION_IGNORE:
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("ignoring interface %s (%s) - by nic rules\n",
|
|
|
|
enumep.name, stoa(&enumep.sin)));
|
2013-12-04 21:33:17 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case ACTION_LISTEN:
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("listen interface %s (%s) - by nic rules\n",
|
|
|
|
enumep.name, stoa(&enumep.sin)));
|
2013-12-04 21:33:17 +00:00
|
|
|
enumep.ignore_packets = ISC_FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_DROP:
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("drop on interface %s (%s) - by nic rules\n",
|
|
|
|
enumep.name, stoa(&enumep.sin)));
|
2013-12-04 21:33:17 +00:00
|
|
|
enumep.ignore_packets = ISC_TRUE;
|
|
|
|
break;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* interfaces must be UP to be usable */
|
|
|
|
if (!(enumep.flags & INT_UP)) {
|
|
|
|
DPRINTF(4, ("skipping interface %s (%s) - DOWN\n",
|
|
|
|
enumep.name, stoa(&enumep.sin)));
|
2008-08-18 14:26:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip any interfaces UP and bound to a wildcard
|
|
|
|
* address - some dhcp clients produce that in the
|
|
|
|
* wild
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (is_wildcard_addr(&enumep.sin))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (is_anycast(&enumep.sin, isc_if.name))
|
2008-08-18 14:26:05 +00:00
|
|
|
continue;
|
|
|
|
|
2015-02-05 20:53:33 +00:00
|
|
|
/*
|
|
|
|
* skip any address that is an invalid state to be used
|
|
|
|
*/
|
|
|
|
if (!is_valid(&enumep.sin, isc_if.name))
|
|
|
|
continue;
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* map to local *address* in order to map all duplicate
|
|
|
|
* interfaces to an endpt structure with the appropriate
|
|
|
|
* socket. Our name space is (ip-address), NOT
|
|
|
|
* (interface name, ip-address).
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
ep = getinterface(&enumep.sin, INT_WILDCARD);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (ep != NULL && refresh_interface(ep)) {
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* found existing and up to date interface -
|
|
|
|
* mark present.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (ep->phase != sys_interphase) {
|
|
|
|
/*
|
|
|
|
* On a new round we reset the name so
|
|
|
|
* the interface name shows up again if
|
|
|
|
* this address is no longer shared.
|
|
|
|
* We reset ignore_packets from the
|
|
|
|
* new prototype to respect any runtime
|
|
|
|
* changes to the nic rules.
|
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
strlcpy(ep->name, enumep.name,
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(ep->name));
|
2014-12-20 22:52:39 +00:00
|
|
|
ep->ignore_packets =
|
2013-12-04 21:33:17 +00:00
|
|
|
enumep.ignore_packets;
|
|
|
|
} else {
|
|
|
|
/* name collision - rename interface */
|
2014-12-20 22:52:39 +00:00
|
|
|
strlcpy(ep->name, "*multiple*",
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(ep->name));
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINT_INTERFACE(4, (ep, "updating ",
|
2013-12-04 21:33:17 +00:00
|
|
|
" present\n"));
|
|
|
|
|
|
|
|
if (ep->ignore_packets !=
|
|
|
|
enumep.ignore_packets) {
|
|
|
|
/*
|
|
|
|
* We have conflicting configurations
|
|
|
|
* for the interface address. This is
|
|
|
|
* caused by using -I <interfacename>
|
2014-12-20 22:52:39 +00:00
|
|
|
* for an interface that shares its
|
2013-12-04 21:33:17 +00:00
|
|
|
* address with other interfaces. We
|
|
|
|
* can not disambiguate incoming
|
|
|
|
* packets delivered to this socket
|
|
|
|
* without extra syscalls/features.
|
|
|
|
* These are not (commonly) available.
|
|
|
|
* Note this is a more unusual
|
|
|
|
* configuration where several
|
|
|
|
* interfaces share an address but
|
|
|
|
* filtering via interface name is
|
|
|
|
* attempted. We resolve the
|
|
|
|
* configuration conflict by disabling
|
|
|
|
* the processing of received packets.
|
|
|
|
* This leads to no service on the
|
|
|
|
* interface address where the conflict
|
|
|
|
* occurs.
|
|
|
|
*/
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"WARNING: conflicting enable configuration for interfaces %s and %s for address %s - unsupported configuration - address DISABLED",
|
|
|
|
enumep.name, ep->name,
|
|
|
|
stoa(&enumep.sin));
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
ep->ignore_packets = ISC_TRUE;
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ep->phase = sys_interphase;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
ifi.action = IFS_EXISTS;
|
2013-12-04 21:33:17 +00:00
|
|
|
ifi.ep = ep;
|
|
|
|
if (receiver != NULL)
|
|
|
|
(*receiver)(data, &ifi);
|
|
|
|
} else {
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* This is new or refreshing failed - add to
|
|
|
|
* our interface list. If refreshing failed we
|
|
|
|
* will delete the interface structure in phase
|
|
|
|
* 2 as the interface was not marked current.
|
|
|
|
* We can bind to the address as the refresh
|
|
|
|
* code already closed the offending socket
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
ep = create_interface(port, &enumep);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (ep != NULL) {
|
2008-08-18 14:26:05 +00:00
|
|
|
ifi.action = IFS_CREATED;
|
2013-12-04 21:33:17 +00:00
|
|
|
ifi.ep = ep;
|
|
|
|
if (receiver != NULL)
|
|
|
|
(*receiver)(data, &ifi);
|
|
|
|
|
|
|
|
new_interface_found = TRUE;
|
|
|
|
DPRINT_INTERFACE(3,
|
|
|
|
(ep, "updating ",
|
|
|
|
" new - created\n"));
|
|
|
|
} else {
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINT_INTERFACE(3,
|
2013-12-04 21:33:17 +00:00
|
|
|
(&enumep, "updating ",
|
|
|
|
" new - creation FAILED"));
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_INFO,
|
2014-12-20 22:52:39 +00:00
|
|
|
"failed to init interface for address %s",
|
2013-12-04 21:33:17 +00:00
|
|
|
stoa(&enumep.sin));
|
2008-08-18 14:26:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
isc_interfaceiter_destroy(&iter);
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* phase 2 - delete gone interfaces - reassigning peers to
|
|
|
|
* other interfaces
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
for (ep = ep_list; ep != NULL; ep = next_ep) {
|
|
|
|
next_ep = ep->elink;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* if phase does not match sys_phase this interface was
|
|
|
|
* not enumerated during the last interface scan - so it
|
|
|
|
* is gone and will be deleted here unless it did not
|
|
|
|
* originate from interface enumeration (INT_WILDCARD,
|
|
|
|
* INT_MCASTIF).
|
|
|
|
*/
|
|
|
|
if (((INT_WILDCARD | INT_MCASTIF) & ep->flags) ||
|
|
|
|
ep->phase == sys_interphase)
|
|
|
|
continue;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINT_INTERFACE(3, (ep, "updating ",
|
|
|
|
"GONE - deleting\n"));
|
|
|
|
remove_interface(ep);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
ifi.action = IFS_DELETED;
|
|
|
|
ifi.ep = ep;
|
|
|
|
if (receiver != NULL)
|
|
|
|
(*receiver)(data, &ifi);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* disconnect peers from deleted endpt. */
|
|
|
|
while (ep->peers != NULL)
|
|
|
|
set_peerdstadr(ep->peers, NULL);
|
|
|
|
|
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* update globals in case we lose
|
2013-12-04 21:33:17 +00:00
|
|
|
* a loopback interface
|
|
|
|
*/
|
|
|
|
if (ep == loopback_interface)
|
|
|
|
loopback_interface = NULL;
|
|
|
|
|
|
|
|
delete_interface(ep);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* phase 3 - re-configure as the world has possibly changed
|
|
|
|
*
|
|
|
|
* never ever make this conditional again - it is needed to track
|
|
|
|
* routing updates. see bug #2506
|
2004-07-20 15:01:56 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
refresh_all_peerinterfaces();
|
|
|
|
|
2020-03-04 13:59:29 +00:00
|
|
|
if (broadcast_client_enabled || sys_bclient)
|
2015-02-05 20:53:33 +00:00
|
|
|
io_setbclient();
|
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
#ifdef MCAST
|
2015-07-01 03:12:13 +00:00
|
|
|
/*
|
|
|
|
* Check multicast interfaces and try to join multicast groups if
|
|
|
|
* not joined yet.
|
|
|
|
*/
|
|
|
|
for (ep = ep_list; ep != NULL; ep = ep->elink) {
|
|
|
|
remaddr_t *entry;
|
|
|
|
|
|
|
|
if (!(INT_MCASTIF & ep->flags) || (INT_MCASTOPEN & ep->flags))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Find remote address that was linked to this interface */
|
|
|
|
for (entry = remoteaddr_list;
|
|
|
|
entry != NULL;
|
|
|
|
entry = entry->link) {
|
|
|
|
if (entry->ep == ep) {
|
|
|
|
if (socket_multicast_enable(ep, &entry->addr)) {
|
|
|
|
msyslog(LOG_INFO,
|
|
|
|
"Joined %s socket to multicast group %s",
|
|
|
|
stoa(&ep->sin),
|
|
|
|
stoa(&entry->addr));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 19:16:13 +00:00
|
|
|
#endif /* MCAST */
|
2015-07-01 03:12:13 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
return new_interface_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_sockets - create a socket for each interface plus a default
|
|
|
|
* socket for when we don't know where to send
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
create_sockets(
|
|
|
|
u_short port
|
|
|
|
)
|
|
|
|
{
|
2004-07-20 15:01:56 +00:00
|
|
|
#ifndef HAVE_IO_COMPLETION_PORT
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* I/O Completion Ports don't care about the select and FD_SET
|
|
|
|
*/
|
1999-12-09 13:01:21 +00:00
|
|
|
maxactivefd = 0;
|
|
|
|
FD_ZERO(&activefds);
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(2, ("create_sockets(%d)\n", port));
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
create_wildcards(port);
|
|
|
|
|
|
|
|
update_interfaces(port, NULL, NULL);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Now that we have opened all the sockets, turn off the reuse
|
|
|
|
* flag for security.
|
|
|
|
*/
|
|
|
|
set_reuseaddr(0);
|
|
|
|
|
|
|
|
DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
|
|
|
|
|
|
|
|
return ninterfaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_interface - create a new interface for a given prototype
|
|
|
|
* binding the socket.
|
|
|
|
*/
|
|
|
|
static struct interface *
|
|
|
|
create_interface(
|
2013-12-04 21:33:17 +00:00
|
|
|
u_short port,
|
|
|
|
struct interface * protot
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u resmask;
|
|
|
|
endpt * iface;
|
|
|
|
#if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
|
|
|
|
remaddr_t * entry;
|
|
|
|
remaddr_t * next_entry;
|
|
|
|
#endif
|
|
|
|
DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&protot->sin),
|
|
|
|
port));
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/* build an interface */
|
2013-12-04 21:33:17 +00:00
|
|
|
iface = new_interface(protot);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* create socket
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
iface->fd = open_socket(&iface->sin, 0, 0, iface);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (iface->fd != INVALID_SOCKET)
|
|
|
|
log_listen_address(iface);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if ((INT_BROADCAST & iface->flags)
|
|
|
|
&& iface->bfd != INVALID_SOCKET)
|
|
|
|
msyslog(LOG_INFO, "Listening on broadcast address %s#%d",
|
|
|
|
stoa((&iface->bcast)), port);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (INVALID_SOCKET == iface->fd
|
|
|
|
&& INVALID_SOCKET == iface->bfd) {
|
2008-08-18 14:26:05 +00:00
|
|
|
msyslog(LOG_ERR, "unable to create socket on %s (%d) for %s#%d",
|
2013-12-04 21:33:17 +00:00
|
|
|
iface->name,
|
|
|
|
iface->ifnum,
|
|
|
|
stoa((&iface->sin)),
|
|
|
|
port);
|
|
|
|
delete_interface(iface);
|
2008-08-18 14:26:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Blacklist our own addresses, no use talking to ourself
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
SET_HOSTMASK(&resmask, AF(&iface->sin));
|
|
|
|
hack_restrict(RESTRICT_FLAGS, &iface->sin, &resmask,
|
2018-02-28 06:23:12 +00:00
|
|
|
-4, RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* set globals with the first found
|
|
|
|
* loopback interface of the appropriate class
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL == loopback_interface && AF_INET == iface->family
|
|
|
|
&& (INT_LOOPBACK & iface->flags))
|
|
|
|
loopback_interface = iface;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* put into our interface list
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
add_addr_to_list(&iface->sin, iface);
|
|
|
|
add_interface(iface);
|
|
|
|
|
|
|
|
#if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
|
|
|
|
/*
|
|
|
|
* Join any previously-configured compatible multicast groups.
|
|
|
|
*/
|
|
|
|
if (INT_MULTICAST & iface->flags &&
|
|
|
|
!((INT_LOOPBACK | INT_WILDCARD) & iface->flags) &&
|
|
|
|
!iface->ignore_packets) {
|
|
|
|
for (entry = remoteaddr_list;
|
|
|
|
entry != NULL;
|
|
|
|
entry = next_entry) {
|
|
|
|
next_entry = entry->link;
|
|
|
|
if (AF(&iface->sin) != AF(&entry->addr) ||
|
|
|
|
!IS_MCAST(&entry->addr))
|
|
|
|
continue;
|
|
|
|
if (socket_multicast_enable(iface,
|
|
|
|
&entry->addr))
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_INFO,
|
2013-12-04 21:33:17 +00:00
|
|
|
"Joined %s socket to multicast group %s",
|
|
|
|
stoa(&iface->sin),
|
|
|
|
stoa(&entry->addr));
|
|
|
|
else
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"Failed to join %s socket to multicast group %s",
|
|
|
|
stoa(&iface->sin),
|
|
|
|
stoa(&entry->addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MCAST && MCAST_NONEWSOCKET */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINT_INTERFACE(2, (iface, "created ", "\n"));
|
|
|
|
return iface;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2009-12-12 22:29:30 +00:00
|
|
|
|
|
|
|
#ifdef SO_EXCLUSIVEADDRUSE
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
set_excladdruse(
|
|
|
|
SOCKET fd
|
|
|
|
)
|
2009-12-12 22:29:30 +00:00
|
|
|
{
|
|
|
|
int one = 1;
|
|
|
|
int failed;
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef SYS_WINNT
|
|
|
|
DWORD err;
|
|
|
|
#endif
|
2009-12-12 22:29:30 +00:00
|
|
|
|
|
|
|
failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&one, sizeof(one));
|
2009-12-12 22:29:30 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!failed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef SYS_WINNT
|
|
|
|
/*
|
|
|
|
* Prior to Windows XP setting SO_EXCLUSIVEADDRUSE can fail with
|
|
|
|
* error WSAINVAL depending on service pack level and whether
|
2014-12-20 22:52:39 +00:00
|
|
|
* the user account is in the Administrators group. Do not
|
2013-12-04 21:33:17 +00:00
|
|
|
* complain if it fails that way on versions prior to XP (5.1).
|
|
|
|
*/
|
|
|
|
err = GetLastError();
|
|
|
|
|
|
|
|
if (isc_win32os_versioncheck(5, 1, 0, 0) < 0 /* < 5.1/XP */
|
|
|
|
&& WSAEINVAL == err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SetLastError(err);
|
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
2013-12-04 21:33:17 +00:00
|
|
|
"setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m",
|
|
|
|
(int)fd);
|
2009-12-12 22:29:30 +00:00
|
|
|
}
|
|
|
|
#endif /* SO_EXCLUSIVEADDRUSE */
|
|
|
|
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* set_reuseaddr() - set/clear REUSEADDR on all sockets
|
|
|
|
* NB possible hole - should we be doing this on broadcast
|
|
|
|
* fd's also?
|
|
|
|
*/
|
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
set_reuseaddr(
|
|
|
|
int flag
|
|
|
|
)
|
|
|
|
{
|
2009-12-12 22:29:30 +00:00
|
|
|
#ifndef SO_EXCLUSIVEADDRUSE
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt *ep;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
for (ep = ep_list; ep != NULL; ep = ep->elink) {
|
|
|
|
if (ep->flags & INT_WILDCARD)
|
2009-12-12 22:29:30 +00:00
|
|
|
continue;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* if ep->fd is INVALID_SOCKET, we might have a adapter
|
2008-08-18 14:26:05 +00:00
|
|
|
* configured but not present
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n",
|
|
|
|
ep->name, stoa(&ep->sin),
|
|
|
|
flag ? "on" : "off"));
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (ep->fd != INVALID_SOCKET) {
|
|
|
|
if (setsockopt(ep->fd, SOL_SOCKET, SO_REUSEADDR,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&flag, sizeof(flag))) {
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR, "set_reuseaddr: setsockopt(%s, SO_REUSEADDR, %s) failed: %m",
|
|
|
|
stoa(&ep->sin), flag ? "on" : "off");
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-12 22:29:30 +00:00
|
|
|
#endif /* ! SO_EXCLUSIVEADDRUSE */
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is just a wrapper around an internal function so we can
|
|
|
|
* make other changes as necessary later on
|
|
|
|
*/
|
|
|
|
void
|
2013-12-04 21:33:17 +00:00
|
|
|
enable_broadcast(
|
2014-12-20 22:52:39 +00:00
|
|
|
struct interface * iface,
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u * baddr
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef OPEN_BCAST_SOCKET
|
2008-08-18 14:26:05 +00:00
|
|
|
socket_broadcast_enable(iface, iface->fd, baddr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef OPEN_BCAST_SOCKET
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Enable a broadcast address to a given socket
|
2014-12-20 22:52:39 +00:00
|
|
|
* The socket is in the ep_list all we need to do is enable
|
2008-08-18 14:26:05 +00:00
|
|
|
* broadcasting. It is not this function's job to select the socket
|
|
|
|
*/
|
|
|
|
static isc_boolean_t
|
2013-12-04 21:33:17 +00:00
|
|
|
socket_broadcast_enable(
|
2014-12-20 22:52:39 +00:00
|
|
|
struct interface * iface,
|
|
|
|
SOCKET fd,
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u * baddr
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
#ifdef SO_BROADCAST
|
|
|
|
int on = 1;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV4(baddr)) {
|
2008-08-18 14:26:05 +00:00
|
|
|
/* if this interface can support broadcast, set SO_BROADCAST */
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on, sizeof(on)))
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt(SO_BROADCAST) enable failure on address %s: %m",
|
|
|
|
stoa(baddr));
|
|
|
|
else
|
|
|
|
DPRINTF(2, ("Broadcast enabled on socket %d for address %s\n",
|
|
|
|
fd, stoa(baddr)));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
iface->flags |= INT_BCASTXMIT;
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_TRUE;
|
|
|
|
#else
|
|
|
|
return ISC_FALSE;
|
|
|
|
#endif /* SO_BROADCAST */
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Remove a broadcast address from a given socket
|
2014-12-20 22:52:39 +00:00
|
|
|
* The socket is in the ep_list all we need to do is disable
|
2008-08-18 14:26:05 +00:00
|
|
|
* broadcasting. It is not this function's job to select the socket
|
|
|
|
*/
|
|
|
|
static isc_boolean_t
|
2013-12-04 21:33:17 +00:00
|
|
|
socket_broadcast_disable(
|
2014-12-20 22:52:39 +00:00
|
|
|
struct interface * iface,
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u * baddr
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
#ifdef SO_BROADCAST
|
|
|
|
int off = 0; /* This seems to be OK as an int */
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV4(baddr) && setsockopt(iface->fd, SOL_SOCKET,
|
2018-02-28 06:23:12 +00:00
|
|
|
SO_BROADCAST, (void *)&off, sizeof(off)))
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt(SO_BROADCAST) disable failure on address %s: %m",
|
|
|
|
stoa(baddr));
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
iface->flags &= ~INT_BCASTXMIT;
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_TRUE;
|
|
|
|
#else
|
|
|
|
return ISC_FALSE;
|
|
|
|
#endif /* SO_BROADCAST */
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* OS_MISSES_SPECIFIC_ROUTE_UPDATES */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
#endif /* OPEN_BCAST_SOCKET */
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* return the broadcast client flag value
|
|
|
|
*/
|
2020-03-04 13:59:29 +00:00
|
|
|
/*isc_boolean_t
|
2013-12-04 21:33:17 +00:00
|
|
|
get_broadcastclient_flag(void)
|
|
|
|
{
|
|
|
|
return (broadcast_client_enabled);
|
|
|
|
}
|
2020-03-04 13:59:29 +00:00
|
|
|
*/
|
2016-01-21 08:50:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Check to see if the address is a multicast address
|
|
|
|
*/
|
|
|
|
static isc_boolean_t
|
2013-12-04 21:33:17 +00:00
|
|
|
addr_ismulticast(
|
|
|
|
sockaddr_u *maddr
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
isc_boolean_t result;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* If we don't have IPV6 support any IPV6 addr is not multicast
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV6(maddr))
|
|
|
|
result = ISC_FALSE;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
result = IS_MCAST(maddr);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
DPRINTF(4, ("address %s is not multicast\n",
|
|
|
|
stoa(maddr)));
|
|
|
|
|
|
|
|
return result;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multicast servers need to set the appropriate Multicast interface
|
|
|
|
* socket option in order for it to know which interface to use for
|
|
|
|
* send the multicast packet.
|
|
|
|
*/
|
|
|
|
void
|
2013-12-04 21:33:17 +00:00
|
|
|
enable_multicast_if(
|
|
|
|
struct interface * iface,
|
|
|
|
sockaddr_u * maddr
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
#ifdef MCAST
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef IP_MULTICAST_LOOP
|
2013-12-04 21:33:17 +00:00
|
|
|
TYPEOF_IP_MULTICAST_LOOP off = 0;
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif
|
2015-02-05 20:53:33 +00:00
|
|
|
#if defined(INCLUDE_IPV6_MULTICAST_SUPPORT) && defined(IPV6_MULTICAST_LOOP)
|
2014-12-20 22:52:39 +00:00
|
|
|
u_int off6 = 0;
|
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(AF(maddr) == AF(&iface->sin));
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
switch (AF(&iface->sin)) {
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
#ifdef IP_MULTICAST_LOOP
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* Don't send back to itself, but allow failure to set
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (setsockopt(iface->fd, IPPROTO_IP,
|
|
|
|
IP_MULTICAST_LOOP,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&off,
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(off))) {
|
|
|
|
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IP_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
|
2014-12-20 22:52:39 +00:00
|
|
|
iface->fd, stoa(&iface->sin),
|
2013-12-04 21:33:17 +00:00
|
|
|
stoa(maddr));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
#ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
|
|
|
|
#ifdef IPV6_MULTICAST_LOOP
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* Don't send back to itself, but allow failure to set
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (setsockopt(iface->fd, IPPROTO_IPV6,
|
|
|
|
IPV6_MULTICAST_LOOP,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *) &off6, sizeof(off6))) {
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
msyslog(LOG_ERR,
|
2014-12-20 22:52:39 +00:00
|
|
|
"setsockopt IPV6_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
|
2013-12-04 21:33:17 +00:00
|
|
|
iface->fd, stoa(&iface->sin),
|
|
|
|
stoa(maddr));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a multicast address to a given socket
|
2014-12-20 22:52:39 +00:00
|
|
|
* The socket is in the ep_list all we need to do is enable
|
2008-08-18 14:26:05 +00:00
|
|
|
* multicasting. It is not this function's job to select the socket
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
#if defined(MCAST)
|
2008-08-18 14:26:05 +00:00
|
|
|
static isc_boolean_t
|
2013-12-04 21:33:17 +00:00
|
|
|
socket_multicast_enable(
|
|
|
|
endpt * iface,
|
|
|
|
sockaddr_u * maddr
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
struct ip_mreq mreq;
|
2015-10-21 19:16:13 +00:00
|
|
|
# ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
|
2013-12-04 21:33:17 +00:00
|
|
|
struct ipv6_mreq mreq6;
|
2015-10-21 19:16:13 +00:00
|
|
|
# endif
|
2013-12-04 21:33:17 +00:00
|
|
|
switch (AF(maddr)) {
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
case AF_INET:
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(mreq);
|
2013-12-04 21:33:17 +00:00
|
|
|
mreq.imr_multiaddr = SOCK_ADDR4(maddr);
|
2008-08-18 14:26:05 +00:00
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
2013-12-04 21:33:17 +00:00
|
|
|
if (setsockopt(iface->fd,
|
|
|
|
IPPROTO_IP,
|
|
|
|
IP_ADD_MEMBERSHIP,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&mreq,
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(mreq))) {
|
2015-07-01 03:12:13 +00:00
|
|
|
DPRINTF(2, (
|
2013-12-04 21:33:17 +00:00
|
|
|
"setsockopt IP_ADD_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
|
|
|
|
iface->fd, stoa(&iface->sin),
|
|
|
|
mreq.imr_multiaddr.s_addr,
|
|
|
|
mreq.imr_interface.s_addr,
|
2015-07-01 03:12:13 +00:00
|
|
|
stoa(maddr)));
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_FALSE;
|
|
|
|
}
|
|
|
|
DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n",
|
|
|
|
iface->fd, stoa(&iface->sin),
|
|
|
|
mreq.imr_multiaddr.s_addr,
|
|
|
|
mreq.imr_interface.s_addr, stoa(maddr)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
2015-10-21 19:16:13 +00:00
|
|
|
# ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* Enable reception of multicast packets.
|
|
|
|
* If the address is link-local we can get the
|
|
|
|
* interface index from the scope id. Don't do this
|
|
|
|
* for other types of multicast addresses. For now let
|
|
|
|
* the kernel figure it out.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(mreq6);
|
2013-12-04 21:33:17 +00:00
|
|
|
mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
|
|
|
|
mreq6.ipv6mr_interface = iface->ifindex;
|
|
|
|
|
|
|
|
if (setsockopt(iface->fd, IPPROTO_IPV6,
|
2018-02-28 06:23:12 +00:00
|
|
|
IPV6_JOIN_GROUP, (void *)&mreq6,
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(mreq6))) {
|
2015-07-01 03:12:13 +00:00
|
|
|
DPRINTF(2, (
|
2013-12-04 21:33:17 +00:00
|
|
|
"setsockopt IPV6_JOIN_GROUP failed: %m on socket %d, addr %s for interface %u (%s)",
|
|
|
|
iface->fd, stoa(&iface->sin),
|
2015-07-01 03:12:13 +00:00
|
|
|
mreq6.ipv6mr_interface, stoa(maddr)));
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_FALSE;
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %u (%s)\n",
|
2008-08-18 14:26:05 +00:00
|
|
|
iface->fd, stoa(&iface->sin),
|
|
|
|
mreq6.ipv6mr_interface, stoa(maddr)));
|
2015-10-21 19:16:13 +00:00
|
|
|
# else
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_FALSE;
|
2015-10-21 19:16:13 +00:00
|
|
|
# endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
iface->flags |= INT_MCASTOPEN;
|
|
|
|
iface->num_mcast++;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_TRUE;
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif /* MCAST */
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Remove a multicast address from a given socket
|
2014-12-20 22:52:39 +00:00
|
|
|
* The socket is in the ep_list all we need to do is disable
|
2008-08-18 14:26:05 +00:00
|
|
|
* multicasting. It is not this function's job to select the socket
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef MCAST
|
2008-08-18 14:26:05 +00:00
|
|
|
static isc_boolean_t
|
2013-12-04 21:33:17 +00:00
|
|
|
socket_multicast_disable(
|
|
|
|
struct interface * iface,
|
|
|
|
sockaddr_u * maddr
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2015-10-21 19:16:13 +00:00
|
|
|
# ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
|
2008-08-18 14:26:05 +00:00
|
|
|
struct ipv6_mreq mreq6;
|
2015-10-21 19:16:13 +00:00
|
|
|
# endif
|
2008-08-18 14:26:05 +00:00
|
|
|
struct ip_mreq mreq;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(mreq);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
if (find_addr_in_list(maddr) == NULL) {
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("socket_multicast_disable(%s): not found\n",
|
2013-12-04 21:33:17 +00:00
|
|
|
stoa(maddr)));
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_TRUE;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
switch (AF(maddr)) {
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
case AF_INET:
|
2013-12-04 21:33:17 +00:00
|
|
|
mreq.imr_multiaddr = SOCK_ADDR4(maddr);
|
|
|
|
mreq.imr_interface = SOCK_ADDR4(&iface->sin);
|
|
|
|
if (setsockopt(iface->fd, IPPROTO_IP,
|
2018-02-28 06:23:12 +00:00
|
|
|
IP_DROP_MEMBERSHIP, (void *)&mreq,
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(mreq))) {
|
|
|
|
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IP_DROP_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
|
|
|
|
iface->fd, stoa(&iface->sin),
|
|
|
|
SRCADR(maddr), SRCADR(&iface->sin),
|
|
|
|
stoa(maddr));
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2015-10-21 19:16:13 +00:00
|
|
|
# ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Disable reception of multicast packets
|
2013-12-04 21:33:17 +00:00
|
|
|
* If the address is link-local we can get the
|
|
|
|
* interface index from the scope id. Don't do this
|
|
|
|
* for other types of multicast addresses. For now let
|
|
|
|
* the kernel figure it out.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
|
|
|
|
mreq6.ipv6mr_interface = iface->ifindex;
|
|
|
|
|
|
|
|
if (setsockopt(iface->fd, IPPROTO_IPV6,
|
2018-02-28 06:23:12 +00:00
|
|
|
IPV6_LEAVE_GROUP, (void *)&mreq6,
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(mreq6))) {
|
|
|
|
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d (%s)",
|
|
|
|
iface->fd, stoa(&iface->sin),
|
|
|
|
iface->ifindex, stoa(maddr));
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_FALSE;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
break;
|
2015-10-21 19:16:13 +00:00
|
|
|
# else
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_FALSE;
|
2015-10-21 19:16:13 +00:00
|
|
|
# endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
iface->num_mcast--;
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!iface->num_mcast)
|
2008-08-18 14:26:05 +00:00
|
|
|
iface->flags &= ~INT_MCASTOPEN;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
return ISC_TRUE;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif /* MCAST */
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* io_setbclient - open the broadcast client sockets
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
io_setbclient(void)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef OPEN_BCAST_SOCKET
|
2020-03-04 13:59:29 +00:00
|
|
|
endpt * ep;
|
|
|
|
unsigned int nif, ni4, ni6;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2020-03-04 13:59:29 +00:00
|
|
|
nif = ni4 = ni6 = 0;
|
2004-07-20 15:01:56 +00:00
|
|
|
set_reuseaddr(1);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2020-03-04 13:59:29 +00:00
|
|
|
for (ep = ep_list; ep != NULL; ep = ep->elink) {
|
|
|
|
/* count IPv6 vs IPv4 interfaces. Needed later to decide
|
|
|
|
* if we should log an error or not.
|
|
|
|
*/
|
|
|
|
switch (ep->family) {
|
|
|
|
case AF_INET : ++ni4; break;
|
|
|
|
case AF_INET6: ++ni6; break;
|
|
|
|
default : break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ep->flags & (INT_WILDCARD | INT_LOOPBACK))
|
2013-12-04 21:33:17 +00:00
|
|
|
continue;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/* use only allowed addresses */
|
2020-03-04 13:59:29 +00:00
|
|
|
if (ep->ignore_packets)
|
2004-07-20 15:01:56 +00:00
|
|
|
continue;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* Need a broadcast-capable interface */
|
2020-03-04 13:59:29 +00:00
|
|
|
if (!(ep->flags & INT_BROADCAST))
|
2008-08-18 14:26:05 +00:00
|
|
|
continue;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* Only IPv4 addresses are valid for broadcast */
|
2020-03-04 13:59:29 +00:00
|
|
|
REQUIRE(IS_IPV4(&ep->bcast));
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/* Do we already have the broadcast address open? */
|
2020-03-04 13:59:29 +00:00
|
|
|
if (ep->flags & INT_BCASTOPEN) {
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* account for already open interfaces to avoid
|
2014-12-20 22:52:39 +00:00
|
|
|
* misleading warning below
|
2013-12-04 21:33:17 +00:00
|
|
|
*/
|
2008-08-18 14:26:05 +00:00
|
|
|
nif++;
|
2001-08-29 14:35:15 +00:00
|
|
|
continue;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Try to open the broadcast address
|
|
|
|
*/
|
2020-03-04 13:59:29 +00:00
|
|
|
ep->family = AF_INET;
|
|
|
|
ep->bfd = open_socket(&ep->bcast, 1, 0, ep);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* If we succeeded then we use it otherwise enable
|
|
|
|
* broadcast on the interface address
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2020-03-04 13:59:29 +00:00
|
|
|
if (ep->bfd != INVALID_SOCKET) {
|
2008-08-18 14:26:05 +00:00
|
|
|
nif++;
|
2020-03-04 13:59:29 +00:00
|
|
|
ep->flags |= INT_BCASTOPEN;
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_INFO,
|
2014-12-20 22:52:39 +00:00
|
|
|
"Listen for broadcasts to %s on interface #%d %s",
|
2020-03-04 13:59:29 +00:00
|
|
|
stoa(&ep->bcast), ep->ifnum, ep->name);
|
2016-04-27 05:37:54 +00:00
|
|
|
} else switch (errno) {
|
|
|
|
/* Silently ignore EADDRINUSE as we probably
|
|
|
|
* opened the socket already for an address in
|
|
|
|
* the same network */
|
|
|
|
case EADDRINUSE:
|
|
|
|
/* Some systems cannot bind a socket to a broadcast
|
|
|
|
* address, as that is not a valid host address. */
|
|
|
|
case EADDRNOTAVAIL:
|
|
|
|
# ifdef SYS_WINNT /*TODO: use for other systems, too? */
|
|
|
|
/* avoid recurrence here -- if we already have a
|
|
|
|
* regular socket, it's quite useless to try this
|
|
|
|
* again.
|
|
|
|
*/
|
2020-03-04 13:59:29 +00:00
|
|
|
if (ep->fd != INVALID_SOCKET) {
|
|
|
|
ep->flags |= INT_BCASTOPEN;
|
2016-04-27 05:37:54 +00:00
|
|
|
nif++;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
msyslog(LOG_INFO,
|
|
|
|
"failed to listen for broadcasts to %s on interface #%d %s",
|
2020-03-04 13:59:29 +00:00
|
|
|
stoa(&ep->bcast), ep->ifnum, ep->name);
|
2016-04-27 05:37:54 +00:00
|
|
|
break;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
set_reuseaddr(0);
|
2017-03-23 05:19:00 +00:00
|
|
|
if (nif != 0) {
|
2014-12-20 22:52:39 +00:00
|
|
|
broadcast_client_enabled = ISC_TRUE;
|
|
|
|
DPRINTF(1, ("io_setbclient: listening to %d broadcast addresses\n", nif));
|
2017-03-23 05:19:00 +00:00
|
|
|
} else {
|
2014-12-20 22:52:39 +00:00
|
|
|
broadcast_client_enabled = ISC_FALSE;
|
2020-03-04 13:59:29 +00:00
|
|
|
/* This is expected when having only IPv6 interfaces
|
|
|
|
* and no IPv4 interfaces at all. We suppress the error
|
|
|
|
* log in that case... everything else should work!
|
|
|
|
*/
|
|
|
|
if (ni4 && !ni6) {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"Unable to listen for broadcasts, no broadcast interfaces available");
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"io_setbclient: Broadcast Client disabled by build");
|
|
|
|
#endif /* OPEN_BCAST_SOCKET */
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* io_unsetbclient - close the broadcast client sockets
|
2004-07-20 15:01:56 +00:00
|
|
|
*/
|
2008-08-18 14:26:05 +00:00
|
|
|
void
|
|
|
|
io_unsetbclient(void)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt *ep;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
for (ep = ep_list; ep != NULL; ep = ep->elink) {
|
|
|
|
if (INT_WILDCARD & ep->flags)
|
|
|
|
continue;
|
|
|
|
if (!(INT_BCASTOPEN & ep->flags))
|
|
|
|
continue;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
if (ep->bfd != INVALID_SOCKET) {
|
|
|
|
/* destroy broadcast listening socket */
|
|
|
|
msyslog(LOG_INFO,
|
|
|
|
"stop listening for broadcasts to %s on interface #%d %s",
|
|
|
|
stoa(&ep->bcast), ep->ifnum, ep->name);
|
2016-04-27 05:37:54 +00:00
|
|
|
# ifdef HAVE_IO_COMPLETION_PORT
|
|
|
|
io_completion_port_remove_socket(ep->bfd, ep);
|
|
|
|
# endif
|
2014-12-20 22:52:39 +00:00
|
|
|
close_and_delete_fd_from_list(ep->bfd);
|
|
|
|
ep->bfd = INVALID_SOCKET;
|
|
|
|
}
|
2016-04-27 05:37:54 +00:00
|
|
|
ep->flags &= ~INT_BCASTOPEN;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
broadcast_client_enabled = ISC_FALSE;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* io_multicast_add() - add multicast group address
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
io_multicast_add(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *addr
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
#ifdef MCAST
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt * ep;
|
|
|
|
endpt * one_ep;
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Check to see if this is a multicast address
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!addr_ismulticast(addr))
|
2008-08-18 14:26:05 +00:00
|
|
|
return;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/* If we already have it we can just return */
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL != find_flagged_addr_in_list(addr, INT_MCASTOPEN)) {
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_INFO,
|
2013-12-04 21:33:17 +00:00
|
|
|
"Duplicate request found for multicast address %s",
|
|
|
|
stoa(addr));
|
2008-08-18 14:26:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
# ifndef MULTICAST_NONEWSOCKET
|
2013-12-04 21:33:17 +00:00
|
|
|
ep = new_interface(NULL);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Open a new socket for the multicast address
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
ep->sin = *addr;
|
|
|
|
SET_PORT(&ep->sin, NTP_PORT);
|
|
|
|
ep->family = AF(&ep->sin);
|
|
|
|
AF(&ep->mask) = ep->family;
|
|
|
|
SET_ONESMASK(&ep->mask);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
set_reuseaddr(1);
|
2013-12-04 21:33:17 +00:00
|
|
|
ep->bfd = INVALID_SOCKET;
|
|
|
|
ep->fd = open_socket(&ep->sin, 0, 0, ep);
|
|
|
|
if (ep->fd != INVALID_SOCKET) {
|
|
|
|
ep->ignore_packets = ISC_FALSE;
|
|
|
|
ep->flags |= INT_MCASTIF;
|
2018-02-28 06:23:12 +00:00
|
|
|
ep->ifindex = SCOPE(addr);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
strlcpy(ep->name, "multicast", sizeof(ep->name));
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINT_INTERFACE(2, (ep, "multicast add ", "\n"));
|
|
|
|
add_interface(ep);
|
|
|
|
log_listen_address(ep);
|
|
|
|
} else {
|
|
|
|
/* bind failed, re-use wildcard interface */
|
|
|
|
delete_interface(ep);
|
|
|
|
|
|
|
|
if (IS_IPV4(addr))
|
|
|
|
ep = wildipv4;
|
|
|
|
else if (IS_IPV6(addr))
|
|
|
|
ep = wildipv6;
|
|
|
|
else
|
|
|
|
ep = NULL;
|
|
|
|
|
|
|
|
if (ep != NULL) {
|
2008-08-18 14:26:05 +00:00
|
|
|
/* HACK ! -- stuff in an address */
|
2013-12-04 21:33:17 +00:00
|
|
|
/* because we don't bind addr? DH */
|
|
|
|
ep->bcast = *addr;
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"multicast address %s using wildcard interface #%d %s",
|
|
|
|
stoa(addr), ep->ifnum, ep->name);
|
2004-07-20 15:01:56 +00:00
|
|
|
} else {
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"No multicast socket available to use for address %s",
|
|
|
|
stoa(addr));
|
2008-08-18 14:26:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
{ /* in place of the { following for in #else clause */
|
|
|
|
one_ep = ep;
|
2015-10-21 19:16:13 +00:00
|
|
|
# else /* MULTICAST_NONEWSOCKET follows */
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* For the case where we can't use a separate socket (Windows)
|
|
|
|
* join each applicable endpoint socket to the group address.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV4(addr))
|
|
|
|
one_ep = wildipv4;
|
|
|
|
else
|
|
|
|
one_ep = wildipv6;
|
|
|
|
for (ep = ep_list; ep != NULL; ep = ep->elink) {
|
|
|
|
if (ep->ignore_packets || AF(&ep->sin) != AF(addr) ||
|
|
|
|
!(INT_MULTICAST & ep->flags) ||
|
|
|
|
(INT_LOOPBACK | INT_WILDCARD) & ep->flags)
|
|
|
|
continue;
|
|
|
|
one_ep = ep;
|
2015-10-21 19:16:13 +00:00
|
|
|
# endif /* MULTICAST_NONEWSOCKET */
|
2013-12-04 21:33:17 +00:00
|
|
|
if (socket_multicast_enable(ep, addr))
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_INFO,
|
2013-12-04 21:33:17 +00:00
|
|
|
"Joined %s socket to multicast group %s",
|
|
|
|
stoa(&ep->sin),
|
|
|
|
stoa(addr));
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
add_addr_to_list(addr, one_ep);
|
|
|
|
#else /* !MCAST follows*/
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"Can not add multicast address %s: no multicast support",
|
|
|
|
stoa(addr));
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
return;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* io_multicast_del() - delete multicast group address
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
io_multicast_del(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u * addr
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
#ifdef MCAST
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt *iface;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Check to see if this is a multicast address
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!addr_ismulticast(addr)) {
|
|
|
|
msyslog(LOG_ERR, "invalid multicast address %s",
|
|
|
|
stoa(addr));
|
2008-08-18 14:26:05 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Disable reception of multicast packets
|
|
|
|
*/
|
|
|
|
while ((iface = find_flagged_addr_in_list(addr, INT_MCASTOPEN))
|
|
|
|
!= NULL)
|
|
|
|
socket_multicast_disable(iface, addr);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
delete_addr_from_list(addr);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#else /* not MCAST */
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"Can not delete multicast address %s: no multicast support",
|
|
|
|
stoa(addr));
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif /* not MCAST */
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* open_socket - open a socket, returning the file descriptor
|
|
|
|
*/
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
static SOCKET
|
1999-12-09 13:01:21 +00:00
|
|
|
open_socket(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u * addr,
|
|
|
|
int bcast,
|
|
|
|
int turn_off_reuse,
|
|
|
|
endpt * interf
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
SOCKET fd;
|
|
|
|
int errval;
|
2009-12-12 22:29:30 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* int is OK for REUSEADR per
|
2009-12-12 22:29:30 +00:00
|
|
|
* http://www.kohala.com/start/mcast.api.txt
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
int on = 1;
|
|
|
|
int off = 0;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV6(addr) && !ipv6_works)
|
|
|
|
return INVALID_SOCKET;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/* create a datagram (UDP) socket */
|
2013-12-04 21:33:17 +00:00
|
|
|
fd = socket(AF(addr), SOCK_DGRAM, 0);
|
2009-12-12 22:29:30 +00:00
|
|
|
if (INVALID_SOCKET == fd) {
|
2013-12-04 21:33:17 +00:00
|
|
|
errval = socket_errno();
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
2013-12-04 21:33:17 +00:00
|
|
|
"socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
|
|
|
|
IS_IPV6(addr) ? "6" : "", stoa(addr));
|
2009-12-12 22:29:30 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (errval == EPROTONOSUPPORT ||
|
2009-12-12 22:29:30 +00:00
|
|
|
errval == EAFNOSUPPORT ||
|
2008-08-18 14:26:05 +00:00
|
|
|
errval == EPFNOSUPPORT)
|
2004-07-20 15:01:56 +00:00
|
|
|
return (INVALID_SOCKET);
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
errno = errval;
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - exiting",
|
|
|
|
errno);
|
1999-12-09 13:01:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-12-12 22:29:30 +00:00
|
|
|
#ifdef SYS_WINNT
|
|
|
|
connection_reset_fix(fd, addr);
|
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Fixup the file descriptor for some systems
|
|
|
|
* See bug #530 for details of the issue.
|
|
|
|
*/
|
|
|
|
fd = move_fd(fd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set SO_REUSEADDR since we will be binding the same port
|
2009-12-12 22:29:30 +00:00
|
|
|
* number on each interface according to turn_off_reuse.
|
|
|
|
* This is undesirable on Windows versions starting with
|
|
|
|
* Windows XP (numeric version 5.1).
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2009-12-12 22:29:30 +00:00
|
|
|
#ifdef SYS_WINNT
|
|
|
|
if (isc_win32os_versioncheck(5, 1, 0, 0) < 0) /* before 5.1 */
|
|
|
|
#endif
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)((turn_off_reuse)
|
2014-12-20 22:52:39 +00:00
|
|
|
? &off
|
|
|
|
: &on),
|
2009-12-12 22:29:30 +00:00
|
|
|
sizeof(on))) {
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt SO_REUSEADDR %s fails for address %s: %m",
|
|
|
|
(turn_off_reuse)
|
2014-12-20 22:52:39 +00:00
|
|
|
? "off"
|
|
|
|
: "on",
|
2013-12-04 21:33:17 +00:00
|
|
|
stoa(addr));
|
2009-12-12 22:29:30 +00:00
|
|
|
closesocket(fd);
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
#ifdef SO_EXCLUSIVEADDRUSE
|
|
|
|
/*
|
|
|
|
* setting SO_EXCLUSIVEADDRUSE on the wildcard we open
|
|
|
|
* first will cause more specific binds to fail.
|
|
|
|
*/
|
|
|
|
if (!(interf->flags & INT_WILDCARD))
|
|
|
|
set_excladdruse(fd);
|
|
|
|
#endif
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* IPv4 specific options go here
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV4(addr)) {
|
2014-12-20 22:52:39 +00:00
|
|
|
#if defined(IPPROTO_IP) && defined(IP_TOS)
|
2018-02-28 06:23:12 +00:00
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void *)&qos,
|
2013-12-04 21:33:17 +00:00
|
|
|
sizeof(qos)))
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IP_TOS (%02x) fails on address %s: %m",
|
|
|
|
qos, stoa(addr));
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* IPPROTO_IP && IP_TOS */
|
2013-12-04 21:33:17 +00:00
|
|
|
if (bcast)
|
|
|
|
socket_broadcast_enable(interf, fd, addr);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* IPv6 specific options go here
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV6(addr)) {
|
2014-12-20 22:52:39 +00:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
|
2018-02-28 06:23:12 +00:00
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, (void *)&qos,
|
2014-12-20 22:52:39 +00:00
|
|
|
sizeof(qos)))
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IPV6_TCLASS (%02x) fails on address %s: %m",
|
|
|
|
qos, stoa(addr));
|
|
|
|
#endif /* IPPROTO_IPV6 && IPV6_TCLASS */
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
if (isc_net_probe_ipv6only() == ISC_R_SUCCESS
|
|
|
|
&& setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on, sizeof(on)))
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IPV6_V6ONLY on fails on address %s: %m",
|
2004-07-20 15:01:56 +00:00
|
|
|
stoa(addr));
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif
|
|
|
|
#ifdef IPV6_BINDV6ONLY
|
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on, sizeof(on)))
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
|
|
|
|
stoa(addr));
|
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2000-01-28 14:55:50 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* some OSes don't allow binding to more specific
|
|
|
|
* addresses if a wildcard address already bound
|
|
|
|
* to the port and SO_REUSEADDR is not set
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!is_wildcard_addr(addr))
|
|
|
|
set_wildcard_reuse(AF(addr), 1);
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* bind the local address.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
errval = bind(fd, &addr->sa, SOCKLEN(addr));
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!is_wildcard_addr(addr))
|
|
|
|
set_wildcard_reuse(AF(addr), 0);
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
if (errval < 0) {
|
|
|
|
/*
|
|
|
|
* Don't log this under all conditions
|
|
|
|
*/
|
|
|
|
if (turn_off_reuse == 0
|
|
|
|
#ifdef DEBUG
|
|
|
|
|| debug > 1
|
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
) {
|
|
|
|
msyslog(LOG_ERR,
|
2015-02-05 20:53:33 +00:00
|
|
|
"bind(%d) AF_INET%s %s#%d%s flags 0x%x failed: %m",
|
2013-12-04 21:33:17 +00:00
|
|
|
fd, IS_IPV6(addr) ? "6" : "",
|
2015-02-05 20:53:33 +00:00
|
|
|
stoa(addr), SRCPORT(addr),
|
2013-12-04 21:33:17 +00:00
|
|
|
IS_MCAST(addr) ? " (multicast)" : "",
|
|
|
|
interf->flags);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closesocket(fd);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
return INVALID_SOCKET;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_TIMESTAMP
|
1999-12-09 13:01:21 +00:00
|
|
|
{
|
2008-08-18 14:26:05 +00:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on, sizeof(on)))
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_DEBUG,
|
|
|
|
"setsockopt SO_TIMESTAMP on fails on address %s: %m",
|
|
|
|
stoa(addr));
|
2008-08-18 14:26:05 +00:00
|
|
|
else
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
|
|
|
|
fd, stoa(addr)));
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TIMESTAMPNS
|
|
|
|
{
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on, sizeof(on)))
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_DEBUG,
|
|
|
|
"setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
|
|
|
|
stoa(addr));
|
|
|
|
else
|
|
|
|
DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
|
|
|
|
fd, stoa(addr)));
|
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_BINTIME
|
|
|
|
{
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_BINTIME,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on, sizeof(on)))
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_DEBUG,
|
|
|
|
"setsockopt SO_BINTIME on fails on address %s: %m",
|
|
|
|
stoa(addr));
|
|
|
|
else
|
|
|
|
DPRINTF(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
|
|
|
|
fd, stoa(addr)));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("bind(%d) AF_INET%s, addr %s%%%d#%d, flags 0x%x\n",
|
|
|
|
fd, IS_IPV6(addr) ? "6" : "", stoa(addr),
|
|
|
|
SCOPE(addr), SRCPORT(addr), interf->flags));
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
make_socket_nonblocking(fd);
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
#ifdef HAVE_SIGNALED_IO
|
|
|
|
init_socket_sig(fd);
|
|
|
|
#endif /* not HAVE_SIGNALED_IO */
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
add_fd_to_list(fd, FD_TYPE_SOCKET);
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
#if !defined(SYS_WINNT) && !defined(VMS)
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
|
|
|
|
fcntl(fd, F_GETFL, 0)));
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif /* SYS_WINNT || VMS */
|
|
|
|
|
2016-04-27 05:37:54 +00:00
|
|
|
#if defined(HAVE_IO_COMPLETION_PORT)
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* Add the socket to the completion port
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2016-04-27 05:37:54 +00:00
|
|
|
if (!io_completion_port_add_socket(fd, interf, bcast)) {
|
2008-08-18 14:26:05 +00:00
|
|
|
msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
|
|
|
|
exit(1);
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
return fd;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
|
|
|
|
/*
|
|
|
|
* sendpkt - send a packet to the specified destination. Maintain a
|
|
|
|
* send error cache so that only the first consecutive error for a
|
|
|
|
* destination is logged.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sendpkt(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u * dest,
|
|
|
|
struct interface * ep,
|
|
|
|
int ttl,
|
|
|
|
struct pkt * pkt,
|
|
|
|
int len
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt * src;
|
|
|
|
int ismcast;
|
|
|
|
int cc;
|
|
|
|
int rc;
|
|
|
|
u_char cttl;
|
2018-08-20 06:07:33 +00:00
|
|
|
l_fp fp_zero = { { 0 }, 0 };
|
2020-03-04 13:59:29 +00:00
|
|
|
l_fp org, rec, xmt;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
ismcast = IS_MCAST(dest);
|
|
|
|
if (!ismcast)
|
|
|
|
src = ep;
|
|
|
|
else
|
|
|
|
src = (IS_IPV4(dest))
|
|
|
|
? mc4_list
|
|
|
|
: mc6_list;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL == src) {
|
2001-08-29 14:35:15 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* unbound peer - drop request and wait for better
|
2014-12-20 22:52:39 +00:00
|
|
|
* network conditions
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
|
|
|
|
ismcast ? "\tMCAST\t***** " : "",
|
|
|
|
stoa(dest), ttl, len));
|
|
|
|
return;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
do {
|
|
|
|
DPRINTF(2, ("%ssendpkt(%d, dst=%s, src=%s, ttl=%d, len=%d)\n",
|
|
|
|
ismcast ? "\tMCAST\t***** " : "", src->fd,
|
|
|
|
stoa(dest), stoa(&src->sin), ttl, len));
|
|
|
|
#ifdef MCAST
|
|
|
|
/*
|
|
|
|
* for the moment we use the bcast option to set multicast ttl
|
|
|
|
*/
|
|
|
|
if (ismcast && ttl > 0 && ttl != src->last_ttl) {
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* set the multicast ttl for outgoing packets
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
switch (AF(&src->sin)) {
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
case AF_INET :
|
2013-12-04 21:33:17 +00:00
|
|
|
cttl = (u_char)ttl;
|
|
|
|
rc = setsockopt(src->fd, IPPROTO_IP,
|
|
|
|
IP_MULTICAST_TTL,
|
|
|
|
(void *)&cttl,
|
|
|
|
sizeof(cttl));
|
2004-07-20 15:01:56 +00:00
|
|
|
break;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
# ifdef INCLUDE_IPV6_SUPPORT
|
2004-07-20 15:01:56 +00:00
|
|
|
case AF_INET6 :
|
2013-12-04 21:33:17 +00:00
|
|
|
rc = setsockopt(src->fd, IPPROTO_IPV6,
|
|
|
|
IPV6_MULTICAST_HOPS,
|
|
|
|
(void *)&ttl,
|
|
|
|
sizeof(ttl));
|
2009-12-12 22:29:30 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
# endif /* INCLUDE_IPV6_SUPPORT */
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
default:
|
|
|
|
rc = 0;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!rc)
|
|
|
|
src->last_ttl = ttl;
|
|
|
|
else
|
2014-12-20 22:52:39 +00:00
|
|
|
msyslog(LOG_ERR,
|
2013-12-04 21:33:17 +00:00
|
|
|
"setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
|
|
|
|
stoa(&src->sin));
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif /* MCAST */
|
|
|
|
|
|
|
|
#ifdef SIM
|
|
|
|
cc = simulate_server(dest, src, pkt);
|
2016-04-27 05:37:54 +00:00
|
|
|
#elif defined(HAVE_IO_COMPLETION_PORT)
|
|
|
|
cc = io_completion_port_sendto(src, src->fd, pkt,
|
|
|
|
(size_t)len, (sockaddr_u *)&dest->sa);
|
2013-12-04 21:33:17 +00:00
|
|
|
#else
|
|
|
|
cc = sendto(src->fd, (char *)pkt, (u_int)len, 0,
|
|
|
|
&dest->sa, SOCKLEN(dest));
|
|
|
|
#endif
|
|
|
|
if (cc == -1) {
|
|
|
|
src->notsent++;
|
|
|
|
packets_notsent++;
|
|
|
|
} else {
|
|
|
|
src->sent++;
|
|
|
|
packets_sent++;
|
|
|
|
}
|
|
|
|
if (ismcast)
|
|
|
|
src = src->mclink;
|
|
|
|
} while (ismcast && src != NULL);
|
2018-02-28 06:23:12 +00:00
|
|
|
|
|
|
|
/* HMS: pkt->rootdisp is usually random here */
|
2020-03-04 13:59:29 +00:00
|
|
|
NTOHL_FP(&pkt->org, &org);
|
|
|
|
NTOHL_FP(&pkt->rec, &rec);
|
|
|
|
NTOHL_FP(&pkt->xmt, &xmt);
|
2018-02-28 06:23:12 +00:00
|
|
|
record_raw_stats(src ? &src->sin : NULL, dest,
|
2020-03-04 13:59:29 +00:00
|
|
|
&org, &rec, &xmt, &fp_zero,
|
2018-02-28 06:23:12 +00:00
|
|
|
PKT_LEAP(pkt->li_vn_mode),
|
2020-03-04 13:59:29 +00:00
|
|
|
PKT_VERSION(pkt->li_vn_mode),
|
|
|
|
PKT_MODE(pkt->li_vn_mode),
|
2018-02-28 06:23:12 +00:00
|
|
|
pkt->stratum,
|
|
|
|
pkt->ppoll, pkt->precision,
|
|
|
|
pkt->rootdelay, pkt->rootdisp, pkt->refid,
|
|
|
|
len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
|
|
|
|
|
|
|
|
return;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2016-04-27 05:37:54 +00:00
|
|
|
#if !defined(HAVE_IO_COMPLETION_PORT)
|
|
|
|
#if !defined(HAVE_SIGNALED_IO)
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* fdbits - generate ascii representation of fd_set (FAU debug support)
|
|
|
|
* HFDF format - highest fd first.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
fdbits(
|
2016-01-21 08:50:56 +00:00
|
|
|
int count,
|
|
|
|
const fd_set* set
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
char * buf = buffer;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
count = min(count, 255);
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
while (count >= 0) {
|
1999-12-09 13:01:21 +00:00
|
|
|
*buf++ = FD_ISSET(count, set) ? '#' : '-';
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2016-01-21 08:50:56 +00:00
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
#ifdef REFCLOCK
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* Routine to read the refclock packets for a specific interface
|
|
|
|
* Return the number of bytes read. That way we know if we should
|
|
|
|
* read it again or go on to the next one if no bytes returned
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2008-08-18 14:26:05 +00:00
|
|
|
static inline int
|
2014-12-20 22:52:39 +00:00
|
|
|
read_refclock_packet(
|
|
|
|
SOCKET fd,
|
|
|
|
struct refclockio * rp,
|
|
|
|
l_fp ts
|
|
|
|
)
|
1999-12-09 13:01:21 +00:00
|
|
|
{
|
2015-10-21 19:16:13 +00:00
|
|
|
u_int read_count;
|
2014-12-20 22:52:39 +00:00
|
|
|
int buflen;
|
|
|
|
int saved_errno;
|
|
|
|
int consumed;
|
|
|
|
struct recvbuf * rb;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2020-06-24 00:20:45 +00:00
|
|
|
rb = get_free_recv_buffer(TRUE);
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL == rb) {
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2020-06-24 00:20:45 +00:00
|
|
|
* No buffer space available - just drop the 'packet'.
|
|
|
|
* Since this is a non-blocking character stream we read
|
|
|
|
* all data that we can.
|
|
|
|
*
|
|
|
|
* ...hmmmm... what about "tcflush(fd,TCIFLUSH)" here?!?
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2020-06-24 00:20:45 +00:00
|
|
|
char buf[128];
|
|
|
|
do
|
|
|
|
buflen = read(fd, buf, sizeof(buf));
|
|
|
|
while (buflen > 0);
|
2008-08-18 14:26:05 +00:00
|
|
|
packets_dropped++;
|
|
|
|
return (buflen);
|
|
|
|
}
|
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
/* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead
|
|
|
|
* to buffer overrun and memory corruption
|
|
|
|
*/
|
2016-01-21 08:50:56 +00:00
|
|
|
if (rp->datalen <= 0 || (size_t)rp->datalen > sizeof(rb->recv_space))
|
2015-10-21 19:16:13 +00:00
|
|
|
read_count = sizeof(rb->recv_space);
|
|
|
|
else
|
|
|
|
read_count = (u_int)rp->datalen;
|
2014-12-20 22:52:39 +00:00
|
|
|
do {
|
2015-10-21 19:16:13 +00:00
|
|
|
buflen = read(fd, (char *)&rb->recv_space, read_count);
|
2014-12-20 22:52:39 +00:00
|
|
|
} while (buflen < 0 && EINTR == errno);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (buflen <= 0) {
|
|
|
|
saved_errno = errno;
|
2008-08-18 14:26:05 +00:00
|
|
|
freerecvbuf(rb);
|
2014-12-20 22:52:39 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
return buflen;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Got one. Mark how and when it got here,
|
|
|
|
* put it on the full list and do bookkeeping.
|
|
|
|
*/
|
|
|
|
rb->recv_length = buflen;
|
2014-12-20 22:52:39 +00:00
|
|
|
rb->recv_peer = rp->srcclock;
|
2008-08-18 14:26:05 +00:00
|
|
|
rb->dstadr = 0;
|
|
|
|
rb->fd = fd;
|
|
|
|
rb->recv_time = ts;
|
|
|
|
rb->receiver = rp->clock_recv;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
consumed = indicate_refclock_packet(rp, rb);
|
|
|
|
if (!consumed) {
|
|
|
|
rp->recvcount++;
|
|
|
|
packets_received++;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
return buflen;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* REFCLOCK */
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_PACKET_TIMESTAMP
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* extract timestamps from control message buffer
|
|
|
|
*/
|
|
|
|
static l_fp
|
2013-12-04 21:33:17 +00:00
|
|
|
fetch_timestamp(
|
|
|
|
struct recvbuf * rb,
|
2014-12-20 22:52:39 +00:00
|
|
|
struct msghdr * msghdr,
|
2013-12-04 21:33:17 +00:00
|
|
|
l_fp ts
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
struct cmsghdr * cmsghdr;
|
|
|
|
unsigned long ticks;
|
|
|
|
double fuzz;
|
|
|
|
l_fp lfpfuzz;
|
|
|
|
l_fp nts;
|
|
|
|
#ifdef DEBUG_TIMING
|
|
|
|
l_fp dts;
|
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
cmsghdr = CMSG_FIRSTHDR(msghdr);
|
|
|
|
while (cmsghdr != NULL) {
|
|
|
|
switch (cmsghdr->cmsg_type)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_BINTIME
|
|
|
|
case SCM_BINTIME:
|
|
|
|
#endif /* HAVE_BINTIME */
|
|
|
|
#ifdef HAVE_TIMESTAMPNS
|
|
|
|
case SCM_TIMESTAMPNS:
|
|
|
|
#endif /* HAVE_TIMESTAMPNS */
|
|
|
|
#ifdef HAVE_TIMESTAMP
|
2008-08-18 14:26:05 +00:00
|
|
|
case SCM_TIMESTAMP:
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* HAVE_TIMESTAMP */
|
|
|
|
#if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP)
|
|
|
|
switch (cmsghdr->cmsg_type)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_BINTIME
|
|
|
|
case SCM_BINTIME:
|
2016-06-03 05:55:24 +00:00
|
|
|
{
|
|
|
|
struct bintime pbt;
|
|
|
|
memcpy(&pbt, CMSG_DATA(cmsghdr), sizeof(pbt));
|
|
|
|
/*
|
|
|
|
* bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
|
|
|
|
*/
|
|
|
|
nts.l_i = pbt.sec + JAN_1970;
|
|
|
|
nts.l_uf = (u_int32)(pbt.frac >> 32);
|
|
|
|
if (sys_tick > measured_tick &&
|
|
|
|
sys_tick > 1e-9) {
|
|
|
|
ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC));
|
|
|
|
nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC));
|
|
|
|
}
|
|
|
|
DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
|
|
|
|
pbt.sec, (unsigned long)((nts.l_uf / FRAC) * 1e9)));
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_BINTIME */
|
|
|
|
#ifdef HAVE_TIMESTAMPNS
|
|
|
|
case SCM_TIMESTAMPNS:
|
2016-06-03 05:55:24 +00:00
|
|
|
{
|
|
|
|
struct timespec pts;
|
|
|
|
memcpy(&pts, CMSG_DATA(cmsghdr), sizeof(pts));
|
|
|
|
if (sys_tick > measured_tick &&
|
|
|
|
sys_tick > 1e-9) {
|
|
|
|
ticks = (unsigned long)((pts.tv_nsec * 1e-9) /
|
|
|
|
sys_tick);
|
|
|
|
pts.tv_nsec = (long)(ticks * 1e9 *
|
|
|
|
sys_tick);
|
|
|
|
}
|
|
|
|
DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
|
|
|
|
pts.tv_sec, pts.tv_nsec));
|
|
|
|
nts = tspec_stamp_to_lfp(pts);
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_TIMESTAMPNS */
|
|
|
|
#ifdef HAVE_TIMESTAMP
|
|
|
|
case SCM_TIMESTAMP:
|
2016-06-03 05:55:24 +00:00
|
|
|
{
|
|
|
|
struct timeval ptv;
|
|
|
|
memcpy(&ptv, CMSG_DATA(cmsghdr), sizeof(ptv));
|
|
|
|
if (sys_tick > measured_tick &&
|
|
|
|
sys_tick > 1e-6) {
|
|
|
|
ticks = (unsigned long)((ptv.tv_usec * 1e-6) /
|
|
|
|
sys_tick);
|
|
|
|
ptv.tv_usec = (long)(ticks * 1e6 *
|
|
|
|
sys_tick);
|
|
|
|
}
|
|
|
|
DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
|
|
|
|
(intmax_t)ptv.tv_sec, (long)ptv.tv_usec));
|
|
|
|
nts = tval_stamp_to_lfp(ptv);
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_TIMESTAMP */
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
|
|
|
|
DTOLFP(fuzz, &lfpfuzz);
|
|
|
|
L_ADD(&nts, &lfpfuzz);
|
|
|
|
#ifdef DEBUG_TIMING
|
|
|
|
dts = ts;
|
|
|
|
L_SUB(&dts, &nts);
|
|
|
|
collect_timing(rb, "input processing delay", 1,
|
|
|
|
&dts);
|
|
|
|
DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
|
|
|
|
lfptoa(&dts, 9)));
|
|
|
|
#endif /* DEBUG_TIMING */
|
2008-08-18 14:26:05 +00:00
|
|
|
ts = nts; /* network time stamp */
|
|
|
|
break;
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
default:
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n",
|
|
|
|
cmsghdr->cmsg_type));
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
|
|
|
|
}
|
|
|
|
return ts;
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* HAVE_PACKET_TIMESTAMP */
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Routine to read the network NTP packets for a specific interface
|
|
|
|
* Return the number of bytes read. That way we know if we should
|
|
|
|
* read it again or go on to the next one if no bytes returned
|
|
|
|
*/
|
|
|
|
static inline int
|
2013-12-04 21:33:17 +00:00
|
|
|
read_network_packet(
|
|
|
|
SOCKET fd,
|
|
|
|
struct interface * itf,
|
|
|
|
l_fp ts
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
|
|
|
GETSOCKNAME_SOCKLEN_TYPE fromlen;
|
|
|
|
int buflen;
|
|
|
|
register struct recvbuf *rb;
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_PACKET_TIMESTAMP
|
2008-08-18 14:26:05 +00:00
|
|
|
struct msghdr msghdr;
|
|
|
|
struct iovec iovec;
|
2014-12-20 22:52:39 +00:00
|
|
|
char control[CMSG_BUFSIZE];
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2020-06-24 00:20:45 +00:00
|
|
|
* Get a buffer and read the frame. If we haven't got a buffer,
|
|
|
|
* or this is received on a disallowed socket, just dump the
|
2008-08-18 14:26:05 +00:00
|
|
|
* packet.
|
|
|
|
*/
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2020-06-24 00:20:45 +00:00
|
|
|
rb = itf->ignore_packets ? NULL : get_free_recv_buffer(FALSE);
|
|
|
|
if (NULL == rb) {
|
|
|
|
/* A partial read on a UDP socket truncates the data and
|
|
|
|
* removes the message from the queue. So there's no
|
|
|
|
* need to have a full buffer here on the stack.
|
|
|
|
*/
|
|
|
|
char buf[16];
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u from;
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
if (rb != NULL)
|
|
|
|
freerecvbuf(rb);
|
|
|
|
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
buflen = recvfrom(fd, buf, sizeof(buf), 0,
|
2013-12-04 21:33:17 +00:00
|
|
|
&from.sa, &fromlen);
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
|
2013-12-04 21:33:17 +00:00
|
|
|
(itf->ignore_packets)
|
|
|
|
? "ignore"
|
|
|
|
: "drop",
|
|
|
|
free_recvbuffs(), fd, stoa(&from)));
|
|
|
|
if (itf->ignore_packets)
|
2008-08-18 14:26:05 +00:00
|
|
|
packets_ignored++;
|
1999-12-09 13:01:21 +00:00
|
|
|
else
|
2008-08-18 14:26:05 +00:00
|
|
|
packets_dropped++;
|
|
|
|
return (buflen);
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
fromlen = sizeof(rb->recv_srcadr);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifndef HAVE_PACKET_TIMESTAMP
|
2013-12-04 21:33:17 +00:00
|
|
|
rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
|
|
|
|
sizeof(rb->recv_space), 0,
|
|
|
|
&rb->recv_srcadr.sa, &fromlen);
|
2008-08-18 14:26:05 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
iovec.iov_base = &rb->recv_space;
|
2008-08-18 14:26:05 +00:00
|
|
|
iovec.iov_len = sizeof(rb->recv_space);
|
2013-12-04 21:33:17 +00:00
|
|
|
msghdr.msg_name = &rb->recv_srcadr;
|
|
|
|
msghdr.msg_namelen = fromlen;
|
2008-08-18 14:26:05 +00:00
|
|
|
msghdr.msg_iov = &iovec;
|
|
|
|
msghdr.msg_iovlen = 1;
|
|
|
|
msghdr.msg_control = (void *)&control;
|
|
|
|
msghdr.msg_controllen = sizeof(control);
|
|
|
|
msghdr.msg_flags = 0;
|
|
|
|
rb->recv_length = recvmsg(fd, &msghdr, 0);
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
buflen = rb->recv_length;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (buflen == 0 || (buflen == -1 &&
|
2013-12-04 21:33:17 +00:00
|
|
|
(EWOULDBLOCK == errno
|
1999-12-09 13:01:21 +00:00
|
|
|
#ifdef EAGAIN
|
2013-12-04 21:33:17 +00:00
|
|
|
|| EAGAIN == errno
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
))) {
|
1999-12-09 13:01:21 +00:00
|
|
|
freerecvbuf(rb);
|
2008-08-18 14:26:05 +00:00
|
|
|
return (buflen);
|
2013-12-04 21:33:17 +00:00
|
|
|
} else if (buflen < 0) {
|
|
|
|
msyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
|
|
|
|
stoa(&rb->recv_srcadr), fd);
|
|
|
|
DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n",
|
|
|
|
fd));
|
1999-12-09 13:01:21 +00:00
|
|
|
freerecvbuf(rb);
|
2008-08-18 14:26:05 +00:00
|
|
|
return (buflen);
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(3, ("read_network_packet: fd=%d length %d from %s\n",
|
|
|
|
fd, buflen, stoa(&rb->recv_srcadr)));
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2016-04-27 05:37:54 +00:00
|
|
|
#ifdef ENABLE_BUG3020_FIX
|
|
|
|
if (ISREFCLOCKADR(&rb->recv_srcadr)) {
|
|
|
|
msyslog(LOG_ERR, "recvfrom(%s) fd=%d: refclock srcadr on a network interface!",
|
|
|
|
stoa(&rb->recv_srcadr), fd);
|
|
|
|
DPRINTF(1, ("read_network_packet: fd=%d dropped (refclock srcadr))\n",
|
|
|
|
fd));
|
|
|
|
packets_dropped++;
|
|
|
|
freerecvbuf(rb);
|
|
|
|
return (buflen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
** Bug 2672: Some OSes (MacOSX and Linux) don't block spoofed ::1
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (AF_INET6 == itf->family) {
|
2015-02-05 20:53:33 +00:00
|
|
|
DPRINTF(2, ("Got an IPv6 packet, from <%s> (%d) to <%s> (%d)\n",
|
2014-12-20 22:52:39 +00:00
|
|
|
stoa(&rb->recv_srcadr),
|
2015-02-05 20:53:33 +00:00
|
|
|
IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr)),
|
2014-12-20 22:52:39 +00:00
|
|
|
stoa(&itf->sin),
|
2015-02-05 20:53:33 +00:00
|
|
|
!IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
|
2014-12-20 22:52:39 +00:00
|
|
|
));
|
|
|
|
|
2015-02-05 20:53:33 +00:00
|
|
|
if ( IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr))
|
|
|
|
&& !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
|
|
|
|
) {
|
|
|
|
packets_dropped++;
|
|
|
|
DPRINTF(2, ("DROPPING that packet\n"));
|
|
|
|
freerecvbuf(rb);
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
DPRINTF(2, ("processing that packet\n"));
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* Got one. Mark how and when it got here,
|
|
|
|
* put it on the full list and do bookkeeping.
|
|
|
|
*/
|
2008-08-18 14:26:05 +00:00
|
|
|
rb->dstadr = itf;
|
1999-12-09 13:01:21 +00:00
|
|
|
rb->fd = fd;
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_PACKET_TIMESTAMP
|
2013-12-04 21:33:17 +00:00
|
|
|
/* pick up a network time stamp if possible */
|
|
|
|
ts = fetch_timestamp(rb, &msghdr, ts);
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
1999-12-09 13:01:21 +00:00
|
|
|
rb->recv_time = ts;
|
|
|
|
rb->receiver = receive;
|
|
|
|
|
|
|
|
add_full_recv_buffer(rb);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
itf->received++;
|
1999-12-09 13:01:21 +00:00
|
|
|
packets_received++;
|
2008-08-18 14:26:05 +00:00
|
|
|
return (buflen);
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
* attempt to handle io (select()/signaled IO)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
io_handler(void)
|
|
|
|
{
|
|
|
|
# ifndef HAVE_SIGNALED_IO
|
|
|
|
fd_set rdfdes;
|
|
|
|
int nfound;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use select() on all on all input fd's for unlimited
|
|
|
|
* time. select() will terminate on SIGALARM or on the
|
|
|
|
* reception of input. Using select() means we can't do
|
|
|
|
* robust signal handling and we get a potential race
|
|
|
|
* between checking for alarms and doing the select().
|
|
|
|
* Mostly harmless, I think.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* On VMS, I suspect that select() can't be interrupted
|
|
|
|
* by a "signal" either, so I take the easy way out and
|
|
|
|
* have select() time out after one second.
|
|
|
|
* System clock updates really aren't time-critical,
|
|
|
|
* and - lacking a hardware reference clock - I have
|
|
|
|
* yet to learn about anything else that is.
|
|
|
|
*/
|
2016-01-21 08:50:56 +00:00
|
|
|
++handler_calls;
|
2014-12-20 22:52:39 +00:00
|
|
|
rdfdes = activefds;
|
|
|
|
# if !defined(VMS) && !defined(SYS_VXWORKS)
|
|
|
|
nfound = select(maxactivefd + 1, &rdfdes, NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
# else /* VMS, VxWorks */
|
|
|
|
/* make select() wake up after one second */
|
|
|
|
{
|
|
|
|
struct timeval t1;
|
2016-01-21 08:50:56 +00:00
|
|
|
t1.tv_sec = 1;
|
2014-12-20 22:52:39 +00:00
|
|
|
t1.tv_usec = 0;
|
|
|
|
nfound = select(maxactivefd + 1,
|
|
|
|
&rdfdes, NULL, NULL,
|
|
|
|
&t1);
|
|
|
|
}
|
|
|
|
# endif /* VMS, VxWorks */
|
2016-01-21 08:50:56 +00:00
|
|
|
if (nfound < 0 && sanitize_fdset(errno)) {
|
|
|
|
struct timeval t1;
|
|
|
|
t1.tv_sec = 0;
|
|
|
|
t1.tv_usec = 0;
|
|
|
|
rdfdes = activefds;
|
|
|
|
nfound = select(maxactivefd + 1,
|
|
|
|
&rdfdes, NULL, NULL,
|
|
|
|
&t1);
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (nfound > 0) {
|
|
|
|
l_fp ts;
|
|
|
|
|
|
|
|
get_systime(&ts);
|
|
|
|
|
2016-01-21 08:50:56 +00:00
|
|
|
input_handler_scan(&ts, &rdfdes);
|
2014-12-20 22:52:39 +00:00
|
|
|
} else if (nfound == -1 && errno != EINTR) {
|
|
|
|
msyslog(LOG_ERR, "select() error: %m");
|
|
|
|
}
|
|
|
|
# ifdef DEBUG
|
|
|
|
else if (debug > 4) {
|
|
|
|
msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
|
|
|
|
} else {
|
2015-07-01 03:12:13 +00:00
|
|
|
DPRINTF(3, ("select() returned %d: %m\n", nfound));
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
# endif /* DEBUG */
|
|
|
|
# else /* HAVE_SIGNALED_IO */
|
|
|
|
wait_for_signal();
|
|
|
|
# endif /* HAVE_SIGNALED_IO */
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2016-01-21 08:50:56 +00:00
|
|
|
#ifdef HAVE_SIGNALED_IO
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* input_handler - receive packets asynchronously
|
2016-01-21 08:50:56 +00:00
|
|
|
*
|
|
|
|
* ALWAYS IN SIGNAL HANDLER CONTEXT -- only async-safe functions allowed!
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2016-01-21 08:50:56 +00:00
|
|
|
static RETSIGTYPE
|
2008-08-18 14:26:05 +00:00
|
|
|
input_handler(
|
2014-12-20 22:52:39 +00:00
|
|
|
l_fp * cts
|
2008-08-18 14:26:05 +00:00
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
int n;
|
|
|
|
struct timeval tvzero;
|
|
|
|
fd_set fds;
|
2016-01-21 08:50:56 +00:00
|
|
|
|
|
|
|
++handler_calls;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
|
|
|
* Do a poll to see who has data
|
|
|
|
*/
|
|
|
|
|
|
|
|
fds = activefds;
|
|
|
|
tvzero.tv_sec = tvzero.tv_usec = 0;
|
|
|
|
|
2016-01-08 08:06:14 +00:00
|
|
|
n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
|
2016-01-21 08:50:56 +00:00
|
|
|
if (n < 0 && sanitize_fdset(errno)) {
|
|
|
|
fds = activefds;
|
|
|
|
tvzero.tv_sec = tvzero.tv_usec = 0;
|
|
|
|
n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
|
|
|
|
}
|
|
|
|
if (n > 0)
|
|
|
|
input_handler_scan(cts, &fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_SIGNALED_IO */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to sanitize the global FD set
|
|
|
|
*
|
|
|
|
* SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
|
|
|
|
*/
|
|
|
|
static int/*BOOL*/
|
|
|
|
sanitize_fdset(
|
|
|
|
int errc
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int j, b, maxscan;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2016-01-21 08:50:56 +00:00
|
|
|
# ifndef HAVE_SIGNALED_IO
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2016-01-21 08:50:56 +00:00
|
|
|
* extended FAU debugging output
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2016-01-21 08:50:56 +00:00
|
|
|
if (errc != EINTR) {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"select(%d, %s, 0L, 0L, &0.0) error: %m",
|
|
|
|
maxactivefd + 1,
|
|
|
|
fdbits(maxactivefd, &activefds));
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
if (errc != EBADF)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* if we have oviously bad FDs, try to sanitize the FD set. */
|
|
|
|
for (j = 0, maxscan = 0; j <= maxactivefd; j++) {
|
|
|
|
if (FD_ISSET(j, &activefds)) {
|
|
|
|
if (-1 != read(j, &b, 0)) {
|
|
|
|
maxscan = j;
|
|
|
|
continue;
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
2016-01-21 08:50:56 +00:00
|
|
|
# ifndef HAVE_SIGNALED_IO
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"Removing bad file descriptor %d from select set",
|
|
|
|
j);
|
|
|
|
# endif
|
|
|
|
FD_CLR(j, &activefds);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 08:50:56 +00:00
|
|
|
if (maxactivefd != maxscan)
|
|
|
|
maxactivefd = maxscan;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* scan the known FDs (clocks, servers, ...) for presence in a 'fd_set'.
|
|
|
|
*
|
|
|
|
* SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
input_handler_scan(
|
|
|
|
const l_fp * cts,
|
|
|
|
const fd_set * pfds
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int buflen;
|
|
|
|
u_int idx;
|
|
|
|
int doing;
|
|
|
|
SOCKET fd;
|
|
|
|
blocking_child *c;
|
|
|
|
l_fp ts; /* Timestamp at BOselect() gob */
|
|
|
|
|
|
|
|
#if defined(DEBUG_TIMING)
|
|
|
|
l_fp ts_e; /* Timestamp at EOselect() gob */
|
|
|
|
#endif
|
|
|
|
endpt * ep;
|
|
|
|
#ifdef REFCLOCK
|
|
|
|
struct refclockio *rp;
|
|
|
|
int saved_errno;
|
|
|
|
const char * clk;
|
|
|
|
#endif
|
|
|
|
#ifdef HAS_ROUTING_SOCKET
|
|
|
|
struct asyncio_reader * asyncio_reader;
|
|
|
|
struct asyncio_reader * next_asyncio_reader;
|
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
++handler_pkts;
|
2016-01-21 08:50:56 +00:00
|
|
|
ts = *cts;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
#ifdef REFCLOCK
|
|
|
|
/*
|
|
|
|
* Check out the reference clocks first, if any
|
|
|
|
*/
|
2016-01-21 08:50:56 +00:00
|
|
|
|
|
|
|
for (rp = refio; rp != NULL; rp = rp->next) {
|
|
|
|
fd = rp->fd;
|
|
|
|
|
|
|
|
if (!FD_ISSET(fd, pfds))
|
|
|
|
continue;
|
|
|
|
buflen = read_refclock_packet(fd, rp, ts);
|
|
|
|
/*
|
|
|
|
* The first read must succeed after select() indicates
|
|
|
|
* readability, or we've reached a permanent EOF.
|
|
|
|
* http://bugs.ntp.org/1732 reported ntpd munching CPU
|
|
|
|
* after a USB GPS was unplugged because select was
|
|
|
|
* indicating EOF but ntpd didn't remove the descriptor
|
|
|
|
* from the activefds set.
|
|
|
|
*/
|
|
|
|
if (buflen < 0 && EAGAIN != errno) {
|
|
|
|
saved_errno = errno;
|
|
|
|
clk = refnumtoa(&rp->srcclock->srcadr);
|
|
|
|
errno = saved_errno;
|
|
|
|
msyslog(LOG_ERR, "%s read: %m", clk);
|
|
|
|
maintain_activefds(fd, TRUE);
|
|
|
|
} else if (0 == buflen) {
|
|
|
|
clk = refnumtoa(&rp->srcclock->srcadr);
|
|
|
|
msyslog(LOG_ERR, "%s read EOF", clk);
|
|
|
|
maintain_activefds(fd, TRUE);
|
|
|
|
} else {
|
|
|
|
/* drain any remaining refclock input */
|
|
|
|
do {
|
|
|
|
buflen = read_refclock_packet(fd, rp, ts);
|
|
|
|
} while (buflen > 0);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif /* REFCLOCK */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through the interfaces looking for data to read.
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
for (ep = ep_list; ep != NULL; ep = ep->elink) {
|
|
|
|
for (doing = 0; doing < 2; doing++) {
|
|
|
|
if (!doing) {
|
|
|
|
fd = ep->fd;
|
|
|
|
} else {
|
|
|
|
if (!(ep->flags & INT_BCASTOPEN))
|
|
|
|
break;
|
|
|
|
fd = ep->bfd;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
if (fd < 0)
|
|
|
|
continue;
|
2016-01-21 08:50:56 +00:00
|
|
|
if (FD_ISSET(fd, pfds))
|
2008-08-18 14:26:05 +00:00
|
|
|
do {
|
2013-12-04 21:33:17 +00:00
|
|
|
buflen = read_network_packet(
|
|
|
|
fd, ep, ts);
|
2008-08-18 14:26:05 +00:00
|
|
|
} while (buflen > 0);
|
2013-12-04 21:33:17 +00:00
|
|
|
/* Check more interfaces */
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_ROUTING_SOCKET
|
|
|
|
/*
|
|
|
|
* scan list of asyncio readers - currently only used for routing sockets
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
asyncio_reader = asyncio_reader_list;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
while (asyncio_reader != NULL) {
|
2014-12-20 22:52:39 +00:00
|
|
|
/* callback may unlink and free asyncio_reader */
|
|
|
|
next_asyncio_reader = asyncio_reader->link;
|
2016-01-21 08:50:56 +00:00
|
|
|
if (FD_ISSET(asyncio_reader->fd, pfds))
|
2014-12-20 22:52:39 +00:00
|
|
|
(*asyncio_reader->receiver)(asyncio_reader);
|
|
|
|
asyncio_reader = next_asyncio_reader;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
#endif /* HAS_ROUTING_SOCKET */
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* Check for a response from a blocking child
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
for (idx = 0; idx < blocking_children_alloc; idx++) {
|
|
|
|
c = blocking_children[idx];
|
|
|
|
if (NULL == c || -1 == c->resp_read_pipe)
|
|
|
|
continue;
|
2016-01-21 08:50:56 +00:00
|
|
|
if (FD_ISSET(c->resp_read_pipe, pfds)) {
|
|
|
|
++c->resp_ready_seen;
|
|
|
|
++blocking_child_ready_seen;
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* We've done our work */
|
2016-01-21 08:50:56 +00:00
|
|
|
#if defined(DEBUG_TIMING)
|
2008-08-18 14:26:05 +00:00
|
|
|
get_systime(&ts_e);
|
|
|
|
/*
|
|
|
|
* (ts_e - ts) is the amount of time we spent
|
|
|
|
* processing this gob of file descriptors. Log
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
L_SUB(&ts_e, &ts);
|
|
|
|
collect_timing(NULL, "input handler", 1, &ts_e);
|
|
|
|
if (debug > 3)
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_DEBUG,
|
|
|
|
"input_handler: Processed a gob of fd's in %s msec",
|
|
|
|
lfptoms(&ts_e, 6));
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* DEBUG_TIMING */
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2016-04-27 05:37:54 +00:00
|
|
|
#endif /* !HAVE_IO_COMPLETION_PORT */
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* find an interface suitable for the src address
|
|
|
|
*/
|
|
|
|
endpt *
|
|
|
|
select_peerinterface(
|
|
|
|
struct peer * peer,
|
|
|
|
sockaddr_u * srcadr,
|
|
|
|
endpt * dstadr
|
|
|
|
)
|
|
|
|
{
|
|
|
|
endpt *ep;
|
|
|
|
#ifndef SIM
|
|
|
|
endpt *wild;
|
|
|
|
|
|
|
|
wild = ANY_INTERFACE_CHOOSE(srcadr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the peer structure and dance the interface jig.
|
|
|
|
* Reference clocks step the loopback waltz, the others
|
|
|
|
* squaredance around the interface list looking for a buddy. If
|
|
|
|
* the dance peters out, there is always the wildcard interface.
|
|
|
|
* This might happen in some systems and would preclude proper
|
|
|
|
* operation with public key cryptography.
|
|
|
|
*/
|
|
|
|
if (ISREFCLOCKADR(srcadr)) {
|
|
|
|
ep = loopback_interface;
|
|
|
|
} else if (peer->cast_flags &
|
|
|
|
(MDF_BCLNT | MDF_ACAST | MDF_MCAST | MDF_BCAST)) {
|
|
|
|
ep = findbcastinter(srcadr);
|
|
|
|
if (ep != NULL)
|
|
|
|
DPRINTF(4, ("Found *-cast interface %s for address %s\n",
|
|
|
|
stoa(&ep->sin), stoa(srcadr)));
|
|
|
|
else
|
|
|
|
DPRINTF(4, ("No *-cast local address found for address %s\n",
|
|
|
|
stoa(srcadr)));
|
|
|
|
} else {
|
|
|
|
ep = dstadr;
|
|
|
|
if (NULL == ep)
|
|
|
|
ep = wild;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If it is a multicast address, findbcastinter() may not find
|
|
|
|
* it. For unicast, we get to find the interface when dstadr is
|
|
|
|
* given to us as the wildcard (ANY_INTERFACE_CHOOSE). Either
|
|
|
|
* way, try a little harder.
|
|
|
|
*/
|
|
|
|
if (wild == ep)
|
|
|
|
ep = findinterface(srcadr);
|
|
|
|
/*
|
|
|
|
* we do not bind to the wildcard interfaces for output
|
|
|
|
* as our (network) source address would be undefined and
|
|
|
|
* crypto will not work without knowing the own transmit address
|
|
|
|
*/
|
|
|
|
if (ep != NULL && INT_WILDCARD & ep->flags)
|
|
|
|
if (!accept_wildcard_if_for_winnt)
|
|
|
|
ep = NULL;
|
|
|
|
#else /* SIM follows */
|
|
|
|
ep = loopback_interface;
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
return ep;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* findinterface - find local interface corresponding to address
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt *
|
1999-12-09 13:01:21 +00:00
|
|
|
findinterface(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *addr
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt *iface;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
iface = findlocalinterface(addr, INT_WILDCARD, 0);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL == iface) {
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
|
|
|
|
stoa(addr)));
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
iface = ANY_INTERFACE_CHOOSE(addr);
|
|
|
|
} else
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("Found interface #%d %s for address %s\n",
|
2013-12-04 21:33:17 +00:00
|
|
|
iface->ifnum, iface->name, stoa(addr)));
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
return iface;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* findlocalinterface - find local interface corresponding to addr,
|
|
|
|
* which does not have any of flags set. If bast is nonzero, addr is
|
|
|
|
* a broadcast address.
|
2008-08-18 14:26:05 +00:00
|
|
|
*
|
|
|
|
* This code attempts to find the local sending address for an outgoing
|
|
|
|
* address by connecting a new socket to destinationaddress:NTP_PORT
|
|
|
|
* and reading the sockname of the resulting connect.
|
|
|
|
* the complicated sequence simulates the routing table lookup
|
|
|
|
* for to first hop without duplicating any of the routing logic into
|
|
|
|
* ntpd. preferably we would have used an API call - but its not there -
|
|
|
|
* so this is the best we can do here short of duplicating to entire routing
|
|
|
|
* logic in ntpd which would be a silly and really unportable thing to do.
|
|
|
|
*
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
static endpt *
|
2008-08-18 14:26:05 +00:00
|
|
|
findlocalinterface(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u * addr,
|
|
|
|
int flags,
|
|
|
|
int bcast
|
2008-08-18 14:26:05 +00:00
|
|
|
)
|
1999-12-09 13:01:21 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
GETSOCKNAME_SOCKLEN_TYPE sockaddrlen;
|
|
|
|
endpt * iface;
|
|
|
|
sockaddr_u saddr;
|
|
|
|
SOCKET s;
|
|
|
|
int rtn;
|
|
|
|
int on;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
|
2009-12-12 22:29:30 +00:00
|
|
|
stoa(addr)));
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2018-02-28 06:23:12 +00:00
|
|
|
/* [Bug 3437] The dummy POOL peer comes in with an AF of
|
|
|
|
* zero. This is bound to fail, but on the way to nowhere it
|
|
|
|
* triggers a security incident on SELinux.
|
|
|
|
*
|
|
|
|
* Checking the condition and failing early is probably a good
|
|
|
|
* advice, and even saves us some syscalls in that case.
|
|
|
|
* Thanks to Miroslav Lichvar for finding this.
|
|
|
|
*/
|
|
|
|
if (AF_UNSPEC == AF(addr))
|
|
|
|
return NULL;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
s = socket(AF(addr), SOCK_DGRAM, 0);
|
|
|
|
if (INVALID_SOCKET == s)
|
2008-08-18 14:26:05 +00:00
|
|
|
return NULL;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* If we are looking for broadcast interface we need to set this
|
|
|
|
* socket to allow broadcast
|
|
|
|
*/
|
|
|
|
if (bcast) {
|
|
|
|
on = 1;
|
2014-12-20 22:52:39 +00:00
|
|
|
if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET,
|
|
|
|
SO_BROADCAST,
|
2018-02-28 06:23:12 +00:00
|
|
|
(void *)&on,
|
2014-12-20 22:52:39 +00:00
|
|
|
sizeof(on))) {
|
|
|
|
closesocket(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rtn = connect(s, &addr->sa, SOCKLEN(addr));
|
|
|
|
if (SOCKET_ERROR == rtn) {
|
2004-07-20 15:01:56 +00:00
|
|
|
closesocket(s);
|
2008-08-18 14:26:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddrlen = sizeof(saddr);
|
|
|
|
rtn = getsockname(s, &saddr.sa, &sockaddrlen);
|
2008-08-18 14:26:05 +00:00
|
|
|
closesocket(s);
|
2013-12-04 21:33:17 +00:00
|
|
|
if (SOCKET_ERROR == rtn)
|
2008-08-18 14:26:05 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n",
|
|
|
|
stoa(addr), stoa(&saddr)));
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
iface = getinterface(&saddr, flags);
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* if we didn't find an exact match on saddr, find the closest
|
|
|
|
* available local address. This handles the case of the
|
|
|
|
* address suggested by the kernel being excluded by nic rules
|
|
|
|
* or the user's -I and -L options to ntpd.
|
|
|
|
* See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683
|
|
|
|
* for more background.
|
|
|
|
*/
|
|
|
|
if (NULL == iface || iface->ignore_packets)
|
2014-12-20 22:52:39 +00:00
|
|
|
iface = findclosestinterface(&saddr,
|
2013-12-04 21:33:17 +00:00
|
|
|
flags | INT_LOOPBACK);
|
|
|
|
|
|
|
|
/* Don't use an interface which will ignore replies */
|
|
|
|
if (iface != NULL && iface->ignore_packets)
|
|
|
|
iface = NULL;
|
|
|
|
|
|
|
|
return iface;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* findclosestinterface
|
|
|
|
*
|
|
|
|
* If there are -I/--interface or -L/novirtualips command-line options,
|
|
|
|
* or "nic" or "interface" rules in ntp.conf, findlocalinterface() may
|
|
|
|
* find the kernel's preferred local address for a given peer address is
|
|
|
|
* administratively unavailable to ntpd, and punt to this routine's more
|
|
|
|
* expensive search.
|
|
|
|
*
|
|
|
|
* Find the numerically closest local address to the one connect()
|
|
|
|
* suggested. This matches an address on the same subnet first, as
|
|
|
|
* needed by Bug 1184, and provides a consistent choice if there are
|
|
|
|
* multiple feasible local addresses, regardless of the order ntpd
|
|
|
|
* enumerated them.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
endpt *
|
2013-12-04 21:33:17 +00:00
|
|
|
findclosestinterface(
|
|
|
|
sockaddr_u * addr,
|
|
|
|
int flags
|
|
|
|
)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt * ep;
|
|
|
|
endpt * winner;
|
|
|
|
sockaddr_u addr_dist;
|
|
|
|
sockaddr_u min_dist;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO_SOCK(&min_dist);
|
2013-12-04 21:33:17 +00:00
|
|
|
winner = NULL;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
for (ep = ep_list; ep != NULL; ep = ep->elink) {
|
|
|
|
if (ep->ignore_packets ||
|
2014-12-20 22:52:39 +00:00
|
|
|
AF(addr) != ep->family ||
|
2013-12-04 21:33:17 +00:00
|
|
|
flags & ep->flags)
|
|
|
|
continue;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
calc_addr_distance(&addr_dist, addr, &ep->sin);
|
|
|
|
if (NULL == winner ||
|
|
|
|
-1 == cmp_addr_distance(&addr_dist, &min_dist)) {
|
|
|
|
min_dist = addr_dist;
|
|
|
|
winner = ep;
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL == winner)
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("findclosestinterface(%s) failed\n",
|
2013-12-04 21:33:17 +00:00
|
|
|
stoa(addr)));
|
2008-08-18 14:26:05 +00:00
|
|
|
else
|
2014-12-20 22:52:39 +00:00
|
|
|
DPRINTF(4, ("findclosestinterface(%s) -> %s\n",
|
2013-12-04 21:33:17 +00:00
|
|
|
stoa(addr), stoa(&winner->sin)));
|
|
|
|
|
|
|
|
return winner;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* calc_addr_distance - calculate the distance between two addresses,
|
|
|
|
* the absolute value of the difference between
|
|
|
|
* the addresses numerically, stored as an address.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
static void
|
|
|
|
calc_addr_distance(
|
|
|
|
sockaddr_u * dist,
|
|
|
|
const sockaddr_u * a1,
|
|
|
|
const sockaddr_u * a2
|
2008-08-18 14:26:05 +00:00
|
|
|
)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
u_int32 a1val;
|
|
|
|
u_int32 a2val;
|
|
|
|
u_int32 v4dist;
|
|
|
|
int found_greater;
|
|
|
|
int a1_greater;
|
|
|
|
int i;
|
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(AF(a1) == AF(a2));
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO_SOCK(dist);
|
2013-12-04 21:33:17 +00:00
|
|
|
AF(dist) = AF(a1);
|
|
|
|
|
|
|
|
/* v4 can be done a bit simpler */
|
|
|
|
if (IS_IPV4(a1)) {
|
|
|
|
a1val = SRCADR(a1);
|
|
|
|
a2val = SRCADR(a2);
|
|
|
|
v4dist = (a1val > a2val)
|
|
|
|
? a1val - a2val
|
|
|
|
: a2val - a1val;
|
|
|
|
SET_ADDR4(dist, v4dist);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
return;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
found_greater = FALSE;
|
|
|
|
a1_greater = FALSE; /* suppress pot. uninit. warning */
|
2015-02-05 20:53:33 +00:00
|
|
|
for (i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!found_greater &&
|
|
|
|
NSRCADR6(a1)[i] != NSRCADR6(a2)[i]) {
|
|
|
|
found_greater = TRUE;
|
|
|
|
a1_greater = (NSRCADR6(a1)[i] > NSRCADR6(a2)[i]);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!found_greater) {
|
|
|
|
NSRCADR6(dist)[i] = 0;
|
|
|
|
} else {
|
|
|
|
if (a1_greater)
|
|
|
|
NSRCADR6(dist)[i] = NSRCADR6(a1)[i] -
|
|
|
|
NSRCADR6(a2)[i];
|
|
|
|
else
|
|
|
|
NSRCADR6(dist)[i] = NSRCADR6(a2)[i] -
|
|
|
|
NSRCADR6(a1)[i];
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* cmp_addr_distance - compare two address distances, returning -1, 0,
|
|
|
|
* 1 to indicate their relationship.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cmp_addr_distance(
|
|
|
|
const sockaddr_u * d1,
|
|
|
|
const sockaddr_u * d2
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int i;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2015-10-21 19:16:13 +00:00
|
|
|
REQUIRE(AF(d1) == AF(d2));
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV4(d1)) {
|
|
|
|
if (SRCADR(d1) < SRCADR(d2))
|
|
|
|
return -1;
|
|
|
|
else if (SRCADR(d1) == SRCADR(d2))
|
|
|
|
return 0;
|
2008-08-18 14:26:05 +00:00
|
|
|
else
|
2013-12-04 21:33:17 +00:00
|
|
|
return 1;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2015-02-05 20:53:33 +00:00
|
|
|
for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i])
|
|
|
|
return -1;
|
|
|
|
else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i])
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fetch an interface structure the matches the
|
|
|
|
* address and has the given flags NOT set
|
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
endpt *
|
2013-12-04 21:33:17 +00:00
|
|
|
getinterface(
|
2014-12-20 22:52:39 +00:00
|
|
|
sockaddr_u * addr,
|
2013-12-04 21:33:17 +00:00
|
|
|
u_int32 flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
endpt *iface;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
iface = find_addr_in_list(addr);
|
|
|
|
|
|
|
|
if (iface != NULL && (iface->flags & flags))
|
|
|
|
iface = NULL;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
return iface;
|
2001-08-29 14:35:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2001-08-29 14:35:15 +00:00
|
|
|
/*
|
|
|
|
* findbcastinter - find broadcast interface corresponding to address
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
endpt *
|
2001-08-29 14:35:15 +00:00
|
|
|
findbcastinter(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *addr
|
2001-08-29 14:35:15 +00:00
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
endpt * iface;
|
|
|
|
|
|
|
|
iface = NULL;
|
2002-10-29 19:58:12 +00:00
|
|
|
#if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
|
|
|
|
stoa(addr)));
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
iface = findlocalinterface(addr, INT_LOOPBACK | INT_WILDCARD,
|
2014-12-20 22:52:39 +00:00
|
|
|
1);
|
2013-12-04 21:33:17 +00:00
|
|
|
if (iface != NULL) {
|
|
|
|
DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n",
|
|
|
|
iface->ifnum, iface->name));
|
|
|
|
return iface;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* plan B - try to find something reasonable in our lists in
|
|
|
|
* case kernel lookup doesn't help
|
|
|
|
*/
|
|
|
|
for (iface = ep_list; iface != NULL; iface = iface->elink) {
|
|
|
|
if (iface->flags & INT_WILDCARD)
|
|
|
|
continue;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/* Don't bother with ignored interfaces */
|
2013-12-04 21:33:17 +00:00
|
|
|
if (iface->ignore_packets)
|
2008-08-18 14:26:05 +00:00
|
|
|
continue;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* First look if this is the correct family
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if(AF(&iface->sin) != AF(addr))
|
|
|
|
continue;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
/* Skip the loopback addresses */
|
2013-12-04 21:33:17 +00:00
|
|
|
if (iface->flags & INT_LOOPBACK)
|
2008-08-18 14:26:05 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* If we are looking to match a multicast address and
|
|
|
|
* this interface is one...
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
if (addr_ismulticast(addr)
|
2013-12-04 21:33:17 +00:00
|
|
|
&& (iface->flags & INT_MULTICAST)) {
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef INCLUDE_IPV6_SUPPORT
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* ...it is the winner unless we're looking for
|
|
|
|
* an interface to use for link-local multicast
|
|
|
|
* and its address is not link-local.
|
|
|
|
*/
|
|
|
|
if (IS_IPV6(addr)
|
|
|
|
&& IN6_IS_ADDR_MC_LINKLOCAL(PSOCK_ADDR6(addr))
|
|
|
|
&& !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin)))
|
|
|
|
continue;
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-08-29 14:35:15 +00:00
|
|
|
/*
|
|
|
|
* We match only those interfaces marked as
|
|
|
|
* broadcastable and either the explicit broadcast
|
|
|
|
* address or the network portion of the IP address.
|
|
|
|
* Sloppy.
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if (IS_IPV4(addr)) {
|
|
|
|
if (SOCK_EQ(&iface->bcast, addr))
|
2008-08-18 14:26:05 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask))
|
2013-12-04 21:33:17 +00:00
|
|
|
== (NSRCADR(addr) & NSRCADR(&iface->mask)))
|
2008-08-18 14:26:05 +00:00
|
|
|
break;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef INCLUDE_IPV6_SUPPORT
|
2013-12-04 21:33:17 +00:00
|
|
|
else if (IS_IPV6(addr)) {
|
|
|
|
if (SOCK_EQ(&iface->bcast, addr))
|
2008-08-18 14:26:05 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
if (SOCK_EQ(netof(&iface->sin), netof(addr)))
|
2008-08-18 14:26:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-29 14:35:15 +00:00
|
|
|
}
|
|
|
|
#endif /* SIOCGIFCONF */
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL == iface) {
|
|
|
|
DPRINTF(4, ("No bcast interface found for %s\n",
|
|
|
|
stoa(addr)));
|
|
|
|
iface = ANY_INTERFACE_CHOOSE(addr);
|
2014-12-20 22:52:39 +00:00
|
|
|
} else {
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n",
|
|
|
|
iface->ifnum, iface->name));
|
2014-12-20 22:52:39 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
return iface;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* io_clr_stats - clear I/O module statistics
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
io_clr_stats(void)
|
|
|
|
{
|
|
|
|
packets_dropped = 0;
|
|
|
|
packets_ignored = 0;
|
|
|
|
packets_received = 0;
|
|
|
|
packets_sent = 0;
|
|
|
|
packets_notsent = 0;
|
|
|
|
|
|
|
|
handler_calls = 0;
|
|
|
|
handler_pkts = 0;
|
|
|
|
io_timereset = current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef REFCLOCK
|
|
|
|
/*
|
|
|
|
* io_addclock - add a reference clock to the list and arrange that we
|
|
|
|
* get SIGIO interrupts from it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
io_addclock(
|
|
|
|
struct refclockio *rio
|
|
|
|
)
|
|
|
|
{
|
|
|
|
BLOCKIO();
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* Stuff the I/O structure in the list and mark the descriptor
|
2013-12-04 21:33:17 +00:00
|
|
|
* in use. There is a harmless (I hope) race condition here.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
rio->active = TRUE;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
# ifdef HAVE_SIGNALED_IO
|
2013-12-04 21:33:17 +00:00
|
|
|
if (init_clock_sig(rio)) {
|
1999-12-09 13:01:21 +00:00
|
|
|
UNBLOCKIO();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
# elif defined(HAVE_IO_COMPLETION_PORT)
|
2016-04-27 05:37:54 +00:00
|
|
|
if (!io_completion_port_add_clock_io(rio)) {
|
1999-12-09 13:01:21 +00:00
|
|
|
UNBLOCKIO();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* enqueue
|
2004-07-20 15:01:56 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
LINK_SLIST(refio, rio, next);
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
2008-08-18 14:26:05 +00:00
|
|
|
* register fd
|
|
|
|
*/
|
|
|
|
add_fd_to_list(rio->fd, FD_TYPE_FILE);
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
UNBLOCKIO();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* io_closeclock - close the clock in the I/O structure given
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
io_closeclock(
|
|
|
|
struct refclockio *rio
|
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
struct refclockio *unlinked;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
BLOCKIO();
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* Remove structure from the list
|
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
rio->active = FALSE;
|
|
|
|
UNLINK_SLIST(unlinked, refio, rio, next, struct refclockio);
|
|
|
|
if (NULL != unlinked) {
|
2016-04-27 05:37:54 +00:00
|
|
|
/* Close the descriptor. The order of operations is
|
|
|
|
* important here in case of async / overlapped IO:
|
|
|
|
* only after we have removed the clock from the
|
|
|
|
* IO completion port we can be sure no further
|
|
|
|
* input is queued. So...
|
|
|
|
* - we first disable feeding to the queu by removing
|
|
|
|
* the clock from the IO engine
|
|
|
|
* - close the file (which brings down any IO on it)
|
|
|
|
* - clear the buffer from results for this fd
|
2014-12-20 22:52:39 +00:00
|
|
|
*/
|
2016-04-27 05:37:54 +00:00
|
|
|
# ifdef HAVE_IO_COMPLETION_PORT
|
|
|
|
io_completion_port_remove_clock_io(rio);
|
|
|
|
# endif
|
2014-12-20 22:52:39 +00:00
|
|
|
close_and_delete_fd_from_list(rio->fd);
|
2016-04-27 05:37:54 +00:00
|
|
|
purge_recv_buffers_for_fd(rio->fd);
|
|
|
|
rio->fd = -1;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
UNBLOCKIO();
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
#endif /* REFCLOCK */
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
|
|
|
* On NT a SOCKET is an unsigned int so we cannot possibly keep it in
|
|
|
|
* an array. So we use one of the ISC_LIST functions to hold the
|
|
|
|
* socket value and use that when we want to enumerate it.
|
2013-12-04 21:33:17 +00:00
|
|
|
*
|
|
|
|
* This routine is called by the forked intres child process to close
|
|
|
|
* all open sockets. On Windows there's no need as intres runs in
|
|
|
|
* the same process as a thread.
|
2004-07-20 15:01:56 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifndef SYS_WINNT
|
2004-07-20 15:01:56 +00:00
|
|
|
void
|
2014-12-20 22:52:39 +00:00
|
|
|
kill_asyncio(
|
|
|
|
int startfd
|
|
|
|
)
|
2004-07-20 15:01:56 +00:00
|
|
|
{
|
|
|
|
BLOCKIO();
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* In the child process we do not maintain activefds and
|
|
|
|
* maxactivefd. Zeroing maxactivefd disables code which
|
|
|
|
* maintains it in close_and_delete_fd_from_list().
|
|
|
|
*/
|
|
|
|
maxactivefd = 0;
|
|
|
|
|
|
|
|
while (fd_list != NULL)
|
|
|
|
close_and_delete_fd_from_list(fd_list->fd);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
UNBLOCKIO();
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif /* !SYS_WINNT */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
|
|
|
* Add and delete functions for the list of open sockets
|
|
|
|
*/
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
add_fd_to_list(
|
|
|
|
SOCKET fd,
|
|
|
|
enum desc_type type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
vsock_t *lsock = emalloc(sizeof(*lsock));
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
lsock->fd = fd;
|
2008-08-18 14:26:05 +00:00
|
|
|
lsock->type = type;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
LINK_SLIST(fd_list, lsock, link);
|
2014-12-20 22:52:39 +00:00
|
|
|
maintain_activefds(fd, 0);
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
close_and_delete_fd_from_list(
|
|
|
|
SOCKET fd
|
2014-12-20 22:52:39 +00:00
|
|
|
)
|
2013-12-04 21:33:17 +00:00
|
|
|
{
|
|
|
|
vsock_t *lsock;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
UNLINK_EXPR_SLIST(lsock, fd_list, fd ==
|
2013-12-04 21:33:17 +00:00
|
|
|
UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (NULL == lsock)
|
|
|
|
return;
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
switch (lsock->type) {
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
case FD_TYPE_SOCKET:
|
|
|
|
closesocket(lsock->fd);
|
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
case FD_TYPE_FILE:
|
2016-01-08 08:06:14 +00:00
|
|
|
closeserial((int)lsock->fd);
|
2014-12-20 22:52:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"internal error - illegal descriptor type %d - EXITING",
|
|
|
|
(int)lsock->type);
|
|
|
|
exit(1);
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
|
|
|
|
free(lsock);
|
|
|
|
/*
|
|
|
|
* remove from activefds
|
|
|
|
*/
|
|
|
|
maintain_activefds(fd, 1);
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
add_addr_to_list(
|
|
|
|
sockaddr_u * addr,
|
|
|
|
endpt * ep
|
|
|
|
)
|
|
|
|
{
|
|
|
|
remaddr_t *laddr;
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
#ifdef DEBUG
|
2008-08-18 14:26:05 +00:00
|
|
|
if (find_addr_in_list(addr) == NULL) {
|
|
|
|
#endif
|
|
|
|
/* not there yet - add to list */
|
2013-12-04 21:33:17 +00:00
|
|
|
laddr = emalloc(sizeof(*laddr));
|
|
|
|
laddr->addr = *addr;
|
|
|
|
laddr->ep = ep;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
LINK_SLIST(remoteaddr_list, laddr, link);
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("Added addr %s to list of addresses\n",
|
|
|
|
stoa(addr)));
|
|
|
|
#ifdef DEBUG
|
2013-12-04 21:33:17 +00:00
|
|
|
} else
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
|
|
|
|
stoa(addr)));
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
delete_addr_from_list(
|
|
|
|
sockaddr_u *addr
|
2014-12-20 22:52:39 +00:00
|
|
|
)
|
2013-12-04 21:33:17 +00:00
|
|
|
{
|
|
|
|
remaddr_t *unlinked;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr,
|
|
|
|
&(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t);
|
|
|
|
|
|
|
|
if (unlinked != NULL) {
|
|
|
|
DPRINTF(4, ("Deleted addr %s from list of addresses\n",
|
|
|
|
stoa(addr)));
|
|
|
|
free(unlinked);
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
delete_interface_from_list(
|
|
|
|
endpt *iface
|
|
|
|
)
|
|
|
|
{
|
|
|
|
remaddr_t *unlinked;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
for (;;) {
|
2013-12-04 21:33:17 +00:00
|
|
|
UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface ==
|
|
|
|
UNLINK_EXPR_SLIST_CURRENT()->ep, link,
|
|
|
|
remaddr_t);
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
if (unlinked == NULL)
|
|
|
|
break;
|
|
|
|
DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
|
|
|
|
stoa(&unlinked->addr), iface->ifnum,
|
|
|
|
iface->name));
|
|
|
|
free(unlinked);
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
static struct interface *
|
|
|
|
find_addr_in_list(
|
|
|
|
sockaddr_u *addr
|
2014-12-20 22:52:39 +00:00
|
|
|
)
|
2013-12-04 21:33:17 +00:00
|
|
|
{
|
|
|
|
remaddr_t *entry;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("Searching for addr %s in list of addresses - ",
|
|
|
|
stoa(addr)));
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
for (entry = remoteaddr_list;
|
|
|
|
entry != NULL;
|
|
|
|
entry = entry->link)
|
|
|
|
if (SOCK_EQ(&entry->addr, addr)) {
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("FOUND\n"));
|
2013-12-04 21:33:17 +00:00
|
|
|
return entry->ep;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
DPRINTF(4, ("NOT FOUND\n"));
|
2013-12-04 21:33:17 +00:00
|
|
|
return NULL;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* Find the given address with the all given flags set in the list
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
static endpt *
|
|
|
|
find_flagged_addr_in_list(
|
|
|
|
sockaddr_u * addr,
|
|
|
|
u_int32 flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
remaddr_t *entry;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("Finding addr %s with flags %d in list: ",
|
|
|
|
stoa(addr), flags));
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
for (entry = remoteaddr_list;
|
|
|
|
entry != NULL;
|
|
|
|
entry = entry->link)
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
if (SOCK_EQ(&entry->addr, addr)
|
|
|
|
&& (entry->ep->flags & flags) == flags) {
|
|
|
|
|
|
|
|
DPRINTF(4, ("FOUND\n"));
|
|
|
|
return entry->ep;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
DPRINTF(4, ("NOT FOUND\n"));
|
|
|
|
return NULL;
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
const char *
|
|
|
|
localaddrtoa(
|
|
|
|
endpt *la
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return (NULL == la)
|
|
|
|
? "<null>"
|
|
|
|
: stoa(&la->sin);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAS_ROUTING_SOCKET
|
|
|
|
# ifndef UPDATE_GRACE
|
|
|
|
# define UPDATE_GRACE 2 /* wait UPDATE_GRACE seconds before scanning */
|
|
|
|
# endif
|
2008-08-18 14:26:05 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
process_routing_msgs(struct asyncio_reader *reader)
|
|
|
|
{
|
|
|
|
char buffer[5120];
|
2013-12-04 21:33:17 +00:00
|
|
|
int cnt, msg_type;
|
|
|
|
#ifdef HAVE_RTNETLINK
|
|
|
|
struct nlmsghdr *nh;
|
|
|
|
#else
|
2014-12-20 22:52:39 +00:00
|
|
|
struct rt_msghdr rtm;
|
2013-12-04 21:33:17 +00:00
|
|
|
char *p;
|
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
if (disable_dynamic_updates) {
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* discard ourselves if we are not needed any more
|
2008-08-18 14:26:05 +00:00
|
|
|
* usually happens when running unprivileged
|
|
|
|
*/
|
|
|
|
remove_asyncio_reader(reader);
|
|
|
|
delete_asyncio_reader(reader);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt = read(reader->fd, buffer, sizeof(buffer));
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
if (cnt < 0) {
|
2015-10-21 19:16:13 +00:00
|
|
|
if (errno == ENOBUFS) {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"routing socket reports: %m");
|
|
|
|
} else {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"routing socket reports: %m - disabling");
|
|
|
|
remove_asyncio_reader(reader);
|
|
|
|
delete_asyncio_reader(reader);
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process routing message
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef HAVE_RTNETLINK
|
2016-01-08 08:06:14 +00:00
|
|
|
for (nh = UA_PTR(struct nlmsghdr, buffer);
|
2013-12-04 21:33:17 +00:00
|
|
|
NLMSG_OK(nh, cnt);
|
2020-06-24 00:20:45 +00:00
|
|
|
nh = NLMSG_NEXT(nh, cnt))
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
msg_type = nh->nlmsg_type;
|
|
|
|
#else
|
|
|
|
for (p = buffer;
|
|
|
|
(p + sizeof(struct rt_msghdr)) <= (buffer + cnt);
|
2020-06-24 00:20:45 +00:00
|
|
|
p += rtm.rtm_msglen)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
memcpy(&rtm, p, sizeof(rtm));
|
|
|
|
if (rtm.rtm_version != RTM_VERSION) {
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"version mismatch (got %d - expected %d) on routing socket - disabling",
|
2014-12-20 22:52:39 +00:00
|
|
|
rtm.rtm_version, RTM_VERSION);
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
remove_asyncio_reader(reader);
|
|
|
|
delete_asyncio_reader(reader);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
msg_type = rtm.rtm_type;
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif
|
|
|
|
switch (msg_type) {
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef RTM_NEWADDR
|
|
|
|
case RTM_NEWADDR:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_DELADDR
|
|
|
|
case RTM_DELADDR:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_ADD
|
|
|
|
case RTM_ADD:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_DELETE
|
|
|
|
case RTM_DELETE:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_REDIRECT
|
|
|
|
case RTM_REDIRECT:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_CHANGE
|
|
|
|
case RTM_CHANGE:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_LOSING
|
|
|
|
case RTM_LOSING:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_IFINFO
|
|
|
|
case RTM_IFINFO:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_IFANNOUNCE
|
|
|
|
case RTM_IFANNOUNCE:
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RTM_NEWLINK
|
|
|
|
case RTM_NEWLINK:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_DELLINK
|
|
|
|
case RTM_DELLINK:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_NEWROUTE
|
|
|
|
case RTM_NEWROUTE:
|
|
|
|
#endif
|
|
|
|
#ifdef RTM_DELROUTE
|
|
|
|
case RTM_DELROUTE:
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* we are keen on new and deleted addresses and
|
|
|
|
* if an interface goes up and down or routing
|
|
|
|
* changes
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(3, ("routing message op = %d: scheduling interface update\n",
|
|
|
|
msg_type));
|
2008-08-18 14:26:05 +00:00
|
|
|
timer_interfacetimeout(current_time + UPDATE_GRACE);
|
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef HAVE_RTNETLINK
|
|
|
|
case NLMSG_DONE:
|
|
|
|
/* end of multipart message */
|
|
|
|
return;
|
|
|
|
#endif
|
2008-08-18 14:26:05 +00:00
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* the rest doesn't bother us.
|
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
DPRINTF(4, ("routing message op = %d: ignored\n",
|
|
|
|
msg_type));
|
2008-08-18 14:26:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up routing notifications
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_async_notifications()
|
|
|
|
{
|
|
|
|
struct asyncio_reader *reader;
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef HAVE_RTNETLINK
|
|
|
|
int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
struct sockaddr_nl sa;
|
|
|
|
#else
|
2008-08-18 14:26:05 +00:00
|
|
|
int fd = socket(PF_ROUTE, SOCK_RAW, 0);
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif
|
|
|
|
if (fd < 0) {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"unable to open routing socket (%m) - using polled interface update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = move_fd(fd);
|
|
|
|
#ifdef HAVE_RTNETLINK
|
2014-12-20 22:52:39 +00:00
|
|
|
ZERO(sa);
|
2013-12-04 21:33:17 +00:00
|
|
|
sa.nl_family = PF_NETLINK;
|
|
|
|
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR
|
|
|
|
| RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE
|
|
|
|
| RTMGRP_IPV4_MROUTE | RTMGRP_IPV6_ROUTE
|
|
|
|
| RTMGRP_IPV6_MROUTE;
|
|
|
|
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
|
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"bind failed on routing socket (%m) - using polled interface update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
make_socket_nonblocking(fd);
|
2008-08-18 14:26:05 +00:00
|
|
|
#if defined(HAVE_SIGNALED_IO)
|
2013-12-04 21:33:17 +00:00
|
|
|
init_socket_sig(fd);
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif /* HAVE_SIGNALED_IO */
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
reader = new_asyncio_reader();
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
reader->fd = fd;
|
|
|
|
reader->receiver = process_routing_msgs;
|
2014-12-20 22:52:39 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
add_asyncio_reader(reader, FD_TYPE_SOCKET);
|
|
|
|
msyslog(LOG_INFO,
|
|
|
|
"Listening on routing socket on fd #%d for interface updates",
|
|
|
|
fd);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
/* HAS_ROUTING_SOCKET not defined */
|
2008-08-18 14:26:05 +00:00
|
|
|
static void
|
2013-12-04 21:33:17 +00:00
|
|
|
init_async_notifications(void)
|
2008-08-18 14:26:05 +00:00
|
|
|
{
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
|