1995-11-29 07:40:04 +00:00
|
|
|
/* socket.c
|
|
|
|
|
|
|
|
BSD socket interface code... */
|
|
|
|
|
|
|
|
/*
|
2011-06-27 16:00:32 +00:00
|
|
|
* Copyright (c) 2004-2011 by Internet Systems Consortium, Inc. ("ISC")
|
2005-03-17 20:15:29 +00:00
|
|
|
* Copyright (c) 1995-2003 by Internet Software Consortium
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* Internet Systems Consortium, Inc.
|
|
|
|
* 950 Charter Street
|
|
|
|
* Redwood City, CA 94063
|
|
|
|
* <info@isc.org>
|
2009-07-23 18:52:21 +00:00
|
|
|
* https://www.isc.org/
|
2000-03-17 04:00:32 +00:00
|
|
|
*
|
2005-03-17 20:15:29 +00:00
|
|
|
* This software has been written for Internet Systems Consortium
|
2000-03-17 04:00:32 +00:00
|
|
|
* by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
|
2005-03-17 20:15:29 +00:00
|
|
|
* To learn more about Internet Systems Consortium, see
|
2009-07-23 18:52:21 +00:00
|
|
|
* ``https://www.isc.org/''. To learn more about Vixie Enterprises,
|
2000-03-17 04:00:32 +00:00
|
|
|
* see ``http://www.vix.com''. To learn more about Nominum, Inc., see
|
|
|
|
* ``http://www.nominum.com''.
|
1995-11-29 07:40:04 +00:00
|
|
|
*/
|
|
|
|
|
1997-01-02 12:00:19 +00:00
|
|
|
/* SO_BINDTODEVICE support added by Elliot Poger (poger@leland.stanford.edu).
|
|
|
|
* This sockopt allows a socket to be bound to a particular interface,
|
|
|
|
* thus enabling the use of DHCPD on a multihomed host.
|
|
|
|
* If SO_BINDTODEVICE is defined in your system header files, the use of
|
|
|
|
* this sockopt will be automatically enabled.
|
|
|
|
* I have implemented it under Linux; other systems should be doable also.
|
|
|
|
*/
|
|
|
|
|
1995-11-29 07:40:04 +00:00
|
|
|
#include "dhcpd.h"
|
2007-05-19 18:47:15 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
2007-05-11 15:50:18 +00:00
|
|
|
#include <sys/uio.h>
|
2007-05-16 22:27:35 +00:00
|
|
|
#include <sys/uio.h>
|
1995-11-29 07:40:04 +00:00
|
|
|
|
2011-06-27 16:00:32 +00:00
|
|
|
#if defined(sun) && defined(USE_V4_PKTINFO)
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#endif
|
|
|
|
|
1996-05-22 07:30:19 +00:00
|
|
|
#ifdef USE_SOCKET_FALLBACK
|
1999-10-24 23:23:41 +00:00
|
|
|
# if !defined (USE_SOCKET_SEND)
|
1996-05-22 07:30:19 +00:00
|
|
|
# define if_register_send if_register_fallback
|
|
|
|
# define send_packet send_fallback
|
1997-02-19 10:51:44 +00:00
|
|
|
# define if_reinitialize_send if_reinitialize_fallback
|
1999-10-24 23:23:41 +00:00
|
|
|
# endif
|
1997-02-19 10:51:44 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-29 17:48:57 +00:00
|
|
|
#if defined(DHCPv6)
|
|
|
|
/*
|
|
|
|
* XXX: this is gross. we need to go back and overhaul the API for socket
|
|
|
|
* handling.
|
|
|
|
*/
|
|
|
|
static unsigned int global_v6_socket_references = 0;
|
|
|
|
static int global_v6_socket = -1;
|
|
|
|
|
|
|
|
static void if_register_multicast(struct interface_info *info);
|
|
|
|
#endif
|
|
|
|
|
2011-06-27 16:00:32 +00:00
|
|
|
/*
|
|
|
|
* We can use a single socket for AF_INET (similar to AF_INET6) on all
|
|
|
|
* interfaces configured for DHCP if the system has support for IP_PKTINFO
|
|
|
|
* and IP_RECVPKTINFO (for example Solaris 11).
|
|
|
|
*/
|
|
|
|
#if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
|
|
|
|
static unsigned int global_v4_socket_references = 0;
|
|
|
|
static int global_v4_socket = -1;
|
|
|
|
#endif
|
|
|
|
|
2007-07-13 06:43:43 +00:00
|
|
|
/*
|
|
|
|
* If we can't bind() to a specific interface, then we can only have
|
|
|
|
* a single socket. This variable insures that we don't try to listen
|
|
|
|
* on two sockets.
|
|
|
|
*/
|
|
|
|
#if !defined(SO_BINDTODEVICE) && !defined(USE_FALLBACK)
|
1997-02-19 10:51:44 +00:00
|
|
|
static int once = 0;
|
2007-07-13 06:43:43 +00:00
|
|
|
#endif /* !defined(SO_BINDTODEVICE) && !defined(USE_FALLBACK) */
|
1997-02-19 10:51:44 +00:00
|
|
|
|
|
|
|
/* Reinitializes the specified interface after an address change. This
|
|
|
|
is not required for packet-filter APIs. */
|
|
|
|
|
1999-10-24 23:23:41 +00:00
|
|
|
#if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
|
1997-02-19 10:51:44 +00:00
|
|
|
void if_reinitialize_send (info)
|
|
|
|
struct interface_info *info;
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
#ifndef USE_SOCKET_RECEIVE
|
|
|
|
once = 0;
|
|
|
|
close (info -> wfdesc);
|
|
|
|
#endif
|
|
|
|
if_register_send (info);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_SOCKET_RECEIVE
|
|
|
|
void if_reinitialize_receive (info)
|
|
|
|
struct interface_info *info;
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
once = 0;
|
|
|
|
close (info -> rfdesc);
|
|
|
|
if_register_receive (info);
|
|
|
|
#endif
|
|
|
|
}
|
1996-05-22 07:30:19 +00:00
|
|
|
#endif
|
|
|
|
|
1999-10-24 23:23:41 +00:00
|
|
|
#if defined (USE_SOCKET_SEND) || \
|
|
|
|
defined (USE_SOCKET_RECEIVE) || \
|
|
|
|
defined (USE_SOCKET_FALLBACK)
|
1996-05-13 00:08:05 +00:00
|
|
|
/* Generic interface registration routine... */
|
2007-05-08 23:05:22 +00:00
|
|
|
int
|
2008-08-29 17:48:57 +00:00
|
|
|
if_register_socket(struct interface_info *info, int family,
|
|
|
|
int *do_multicast)
|
|
|
|
{
|
2007-05-08 23:05:22 +00:00
|
|
|
struct sockaddr_storage name;
|
|
|
|
int name_len;
|
1995-11-29 07:40:04 +00:00
|
|
|
int sock;
|
|
|
|
int flag;
|
2007-05-08 23:05:22 +00:00
|
|
|
int domain;
|
2009-10-28 04:12:30 +00:00
|
|
|
#ifdef DHCPv6
|
|
|
|
struct sockaddr_in6 *addr6;
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in *addr;
|
2007-05-08 23:05:22 +00:00
|
|
|
|
|
|
|
/* INSIST((family == AF_INET) || (family == AF_INET6)); */
|
1997-01-02 12:00:19 +00:00
|
|
|
|
2007-05-19 18:47:15 +00:00
|
|
|
#if !defined(SO_BINDTODEVICE) && !defined(USE_FALLBACK)
|
1996-05-13 00:08:05 +00:00
|
|
|
/* Make sure only one interface is registered. */
|
2007-05-08 23:05:22 +00:00
|
|
|
if (once) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("The standard socket API can only support %s",
|
1998-03-15 20:54:20 +00:00
|
|
|
"hosts with a single network interface.");
|
2007-05-08 23:05:22 +00:00
|
|
|
}
|
1997-01-02 12:00:19 +00:00
|
|
|
once = 1;
|
1996-05-22 07:30:19 +00:00
|
|
|
#endif
|
1996-05-13 00:08:05 +00:00
|
|
|
|
2007-05-08 23:05:22 +00:00
|
|
|
/*
|
|
|
|
* Set up the address we're going to bind to, depending on the
|
|
|
|
* address family.
|
|
|
|
*/
|
|
|
|
memset(&name, 0, sizeof(name));
|
2009-10-28 04:12:30 +00:00
|
|
|
switch (family) {
|
2007-05-19 18:47:15 +00:00
|
|
|
#ifdef DHCPv6
|
2009-10-28 04:12:30 +00:00
|
|
|
case AF_INET6:
|
|
|
|
addr6 = (struct sockaddr_in6 *)&name;
|
|
|
|
addr6->sin6_family = AF_INET6;
|
|
|
|
addr6->sin6_port = local_port;
|
2008-08-29 17:48:57 +00:00
|
|
|
/* XXX: What will happen to multicasts if this is nonzero? */
|
2009-10-28 04:12:30 +00:00
|
|
|
memcpy(&addr6->sin6_addr,
|
2007-05-08 23:05:22 +00:00
|
|
|
&local_address6,
|
2009-10-28 04:12:30 +00:00
|
|
|
sizeof(addr6->sin6_addr));
|
2008-06-13 00:55:53 +00:00
|
|
|
#ifdef HAVE_SA_LEN
|
2009-10-28 04:12:30 +00:00
|
|
|
addr6->sin6_len = sizeof(*addr6);
|
2008-06-13 00:55:53 +00:00
|
|
|
#endif
|
2009-10-28 04:12:30 +00:00
|
|
|
name_len = sizeof(*addr6);
|
2007-05-08 23:05:22 +00:00
|
|
|
domain = PF_INET6;
|
2008-06-13 00:55:53 +00:00
|
|
|
if ((info->flags & INTERFACE_STREAMS) == INTERFACE_UPSTREAM) {
|
2008-08-29 17:48:57 +00:00
|
|
|
*do_multicast = 0;
|
2008-06-13 00:55:53 +00:00
|
|
|
}
|
2009-10-28 04:12:30 +00:00
|
|
|
break;
|
2007-05-19 18:47:15 +00:00
|
|
|
#endif /* DHCPv6 */
|
2009-10-28 04:12:30 +00:00
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
default:
|
|
|
|
addr = (struct sockaddr_in *)&name;
|
2007-08-22 13:41:37 +00:00
|
|
|
addr->sin_family = AF_INET;
|
|
|
|
addr->sin_port = local_port;
|
|
|
|
memcpy(&addr->sin_addr,
|
|
|
|
&local_address,
|
|
|
|
sizeof(addr->sin_addr));
|
2008-06-13 00:55:53 +00:00
|
|
|
#ifdef HAVE_SA_LEN
|
|
|
|
addr->sin_len = sizeof(*addr);
|
|
|
|
#endif
|
2007-08-22 13:41:37 +00:00
|
|
|
name_len = sizeof(*addr);
|
|
|
|
domain = PF_INET;
|
2009-10-28 04:12:30 +00:00
|
|
|
break;
|
2007-05-08 23:05:22 +00:00
|
|
|
}
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-22 07:30:19 +00:00
|
|
|
/* Make a socket... */
|
2007-05-08 23:05:22 +00:00
|
|
|
sock = socket(domain, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (sock < 0) {
|
|
|
|
log_fatal("Can't create dhcp socket: %m");
|
|
|
|
}
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-13 00:08:05 +00:00
|
|
|
/* Set the REUSEADDR option so that we don't fail to start if
|
|
|
|
we're being restarted. */
|
1995-11-29 07:40:04 +00:00
|
|
|
flag = 1;
|
2007-05-08 23:05:22 +00:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(char *)&flag, sizeof(flag)) < 0) {
|
|
|
|
log_fatal("Can't set SO_REUSEADDR option on dhcp socket: %m");
|
|
|
|
}
|
1995-11-29 07:40:04 +00:00
|
|
|
|
2000-09-01 23:03:39 +00:00
|
|
|
/* Set the BROADCAST option so that we can broadcast DHCP responses.
|
|
|
|
We shouldn't do this for fallback devices, and we can detect that
|
|
|
|
a device is a fallback because it has no ifp structure. */
|
2007-05-08 23:05:22 +00:00
|
|
|
if (info->ifp &&
|
|
|
|
(setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
|
|
|
|
(char *)&flag, sizeof(flag)) < 0)) {
|
|
|
|
log_fatal("Can't set SO_BROADCAST option on dhcp socket: %m");
|
|
|
|
}
|
1995-11-29 07:40:04 +00:00
|
|
|
|
2008-08-29 17:48:57 +00:00
|
|
|
#if defined(DHCPv6) && defined(SO_REUSEPORT)
|
|
|
|
/*
|
|
|
|
* We only set SO_REUSEPORT on AF_INET6 sockets, so that multiple
|
|
|
|
* daemons can bind to their own sockets and get data for their
|
|
|
|
* respective interfaces. This does not (and should not) affect
|
|
|
|
* DHCPv4 sockets; we can't yet support BSD sockets well, much
|
|
|
|
* less multiple sockets.
|
|
|
|
*/
|
|
|
|
if (local_family == AF_INET6) {
|
|
|
|
flag = 1;
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
|
|
|
|
(char *)&flag, sizeof(flag)) < 0) {
|
|
|
|
log_fatal("Can't set SO_REUSEPORT option on dhcp "
|
|
|
|
"socket: %m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1996-05-13 00:08:05 +00:00
|
|
|
/* Bind the socket to this interface's IP address. */
|
2007-05-08 23:05:22 +00:00
|
|
|
if (bind(sock, (struct sockaddr *)&name, name_len) < 0) {
|
|
|
|
log_error("Can't bind to dhcp address: %m");
|
|
|
|
log_error("Please make sure there is no other dhcp server");
|
|
|
|
log_error("running and that there's no entry for dhcp or");
|
|
|
|
log_error("bootp in /etc/inetd.conf. Also make sure you");
|
|
|
|
log_error("are not running HP JetAdmin software, which");
|
|
|
|
log_fatal("includes a bootp server.");
|
2000-07-20 03:15:00 +00:00
|
|
|
}
|
1995-11-29 07:40:04 +00:00
|
|
|
|
2007-05-19 18:47:15 +00:00
|
|
|
#if defined(SO_BINDTODEVICE)
|
1997-01-02 12:00:19 +00:00
|
|
|
/* Bind this socket to this interface. */
|
2008-08-29 17:48:57 +00:00
|
|
|
if ((local_family != AF_INET6) && (info->ifp != NULL) &&
|
2007-05-08 23:05:22 +00:00
|
|
|
setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
|
|
|
|
(char *)(info -> ifp), sizeof(*(info -> ifp))) < 0) {
|
|
|
|
log_fatal("setsockopt: SO_BINDTODEVICE: %m");
|
1997-01-02 12:00:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-07-25 17:41:18 +00:00
|
|
|
/* IP_BROADCAST_IF instructs the kernel which interface to send
|
|
|
|
* IP packets whose destination address is 255.255.255.255. These
|
|
|
|
* will be treated as subnet broadcasts on the interface identified
|
|
|
|
* by ip address (info -> primary_address). This is only known to
|
|
|
|
* be defined in SCO system headers, and may not be defined in all
|
|
|
|
* releases.
|
|
|
|
*/
|
|
|
|
#if defined(SCO) && defined(IP_BROADCAST_IF)
|
2007-05-08 23:05:22 +00:00
|
|
|
if (info->address_count &&
|
|
|
|
setsockopt(sock, IPPROTO_IP, IP_BROADCAST_IF, &info->addresses[0],
|
|
|
|
sizeof(info->addresses[0])) < 0)
|
|
|
|
log_fatal("Can't set IP_BROADCAST_IF on dhcp socket: %m");
|
2006-07-25 17:41:18 +00:00
|
|
|
#endif
|
|
|
|
|
2011-06-27 16:00:32 +00:00
|
|
|
#if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
|
|
|
|
/*
|
|
|
|
* If we turn on IP_RECVPKTINFO we will be able to receive
|
|
|
|
* the interface index information of the received packet.
|
|
|
|
*/
|
|
|
|
if (family == AF_INET) {
|
|
|
|
int on = 1;
|
|
|
|
if (setsockopt(sock, IPPROTO_IP, IP_RECVPKTINFO,
|
|
|
|
&on, sizeof(on)) != 0) {
|
|
|
|
log_fatal("setsockopt: IPV_RECVPKTINFO: %m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-19 18:47:15 +00:00
|
|
|
#ifdef DHCPv6
|
2007-05-08 23:05:22 +00:00
|
|
|
/*
|
|
|
|
* If we turn on IPV6_PKTINFO, we will be able to receive
|
|
|
|
* additional information, such as the destination IP address.
|
|
|
|
* We need this to spot unicast packets.
|
|
|
|
*/
|
|
|
|
if (family == AF_INET6) {
|
|
|
|
int on = 1;
|
|
|
|
#ifdef IPV6_RECVPKTINFO
|
|
|
|
/* RFC3542 */
|
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
|
|
|
|
&on, sizeof(on)) != 0) {
|
|
|
|
log_fatal("setsockopt: IPV6_RECVPKTINFO: %m");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* RFC2292 */
|
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO,
|
|
|
|
&on, sizeof(on)) != 0) {
|
|
|
|
log_fatal("setsockopt: IPV6_PKTINFO: %m");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-06-13 00:55:53 +00:00
|
|
|
|
|
|
|
if ((family == AF_INET6) &&
|
|
|
|
((info->flags & INTERFACE_UPSTREAM) != 0)) {
|
|
|
|
int hop_limit = 32;
|
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
|
|
|
|
&hop_limit, sizeof(int)) < 0) {
|
|
|
|
log_fatal("setsockopt: IPV6_MULTICAST_HOPS: %m");
|
|
|
|
}
|
|
|
|
}
|
2007-05-19 18:47:15 +00:00
|
|
|
#endif /* DHCPv6 */
|
2007-05-08 23:05:22 +00:00
|
|
|
|
1996-05-13 00:08:05 +00:00
|
|
|
return sock;
|
1995-11-29 07:40:04 +00:00
|
|
|
}
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1999-10-24 23:23:41 +00:00
|
|
|
#if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
|
1997-02-19 10:51:44 +00:00
|
|
|
void if_register_send (info)
|
1996-04-11 06:49:21 +00:00
|
|
|
struct interface_info *info;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
1996-05-13 00:08:05 +00:00
|
|
|
#ifndef USE_SOCKET_RECEIVE
|
2011-06-27 16:00:32 +00:00
|
|
|
info->wfdesc = if_register_socket(info, AF_INET, 0);
|
|
|
|
/* If this is a normal IPv4 address, get the hardware address. */
|
|
|
|
if (strcmp(info->name, "fallback") != 0)
|
|
|
|
get_hw_addr(info->name, &info->hw_address);
|
1999-09-23 01:15:25 +00:00
|
|
|
#if defined (USE_SOCKET_FALLBACK)
|
|
|
|
/* Fallback only registers for send, but may need to receive as
|
|
|
|
well. */
|
2011-06-27 16:00:32 +00:00
|
|
|
info->rfdesc = info->wfdesc;
|
1999-09-23 01:15:25 +00:00
|
|
|
#endif
|
1996-05-13 00:08:05 +00:00
|
|
|
#else
|
2011-06-27 16:00:32 +00:00
|
|
|
info->wfdesc = info->rfdesc;
|
1996-05-13 00:08:05 +00:00
|
|
|
#endif
|
1997-10-20 21:47:15 +00:00
|
|
|
if (!quiet_interface_discovery)
|
1999-02-25 23:30:43 +00:00
|
|
|
log_info ("Sending on Socket/%s%s%s",
|
2011-06-27 16:00:32 +00:00
|
|
|
info->name,
|
|
|
|
(info->shared_network ? "/" : ""),
|
|
|
|
(info->shared_network ?
|
|
|
|
info->shared_network->name : ""));
|
1995-11-29 07:40:04 +00:00
|
|
|
}
|
2000-03-06 19:39:54 +00:00
|
|
|
|
2000-09-12 20:23:54 +00:00
|
|
|
#if defined (USE_SOCKET_SEND)
|
2000-03-06 19:39:54 +00:00
|
|
|
void if_deregister_send (info)
|
|
|
|
struct interface_info *info;
|
|
|
|
{
|
|
|
|
#ifndef USE_SOCKET_RECEIVE
|
|
|
|
close (info -> wfdesc);
|
|
|
|
#endif
|
|
|
|
info -> wfdesc = -1;
|
|
|
|
|
|
|
|
if (!quiet_interface_discovery)
|
|
|
|
log_info ("Disabling output on Socket/%s%s%s",
|
|
|
|
info -> name,
|
|
|
|
(info -> shared_network ? "/" : ""),
|
|
|
|
(info -> shared_network ?
|
|
|
|
info -> shared_network -> name : ""));
|
|
|
|
}
|
2000-09-12 20:23:54 +00:00
|
|
|
#endif /* USE_SOCKET_SEND */
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-13 00:08:05 +00:00
|
|
|
#ifdef USE_SOCKET_RECEIVE
|
1997-02-19 10:51:44 +00:00
|
|
|
void if_register_receive (info)
|
1996-05-13 00:08:05 +00:00
|
|
|
struct interface_info *info;
|
|
|
|
{
|
2011-06-27 16:00:32 +00:00
|
|
|
|
|
|
|
#if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
|
|
|
|
if (global_v4_socket_references == 0) {
|
|
|
|
global_v4_socket = if_register_socket(info, AF_INET, 0);
|
|
|
|
if (global_v4_socket < 0) {
|
|
|
|
/*
|
|
|
|
* if_register_socket() fatally logs if it fails to
|
|
|
|
* create a socket, this is just a sanity check.
|
|
|
|
*/
|
|
|
|
log_fatal("Failed to create AF_INET socket %s:%d",
|
|
|
|
MDL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info->rfdesc = global_v4_socket;
|
|
|
|
global_v4_socket_references++;
|
|
|
|
#else
|
1996-05-13 00:08:05 +00:00
|
|
|
/* If we're using the socket API for sending and receiving,
|
|
|
|
we don't need to register this interface twice. */
|
2011-06-27 16:00:32 +00:00
|
|
|
info->rfdesc = if_register_socket(info, AF_INET, 0);
|
|
|
|
#endif /* IP_PKTINFO... */
|
|
|
|
/* If this is a normal IPv4 address, get the hardware address. */
|
|
|
|
if (strcmp(info->name, "fallback") != 0)
|
|
|
|
get_hw_addr(info->name, &info->hw_address);
|
|
|
|
|
1997-10-20 21:47:15 +00:00
|
|
|
if (!quiet_interface_discovery)
|
1999-02-25 23:30:43 +00:00
|
|
|
log_info ("Listening on Socket/%s%s%s",
|
2011-06-27 16:00:32 +00:00
|
|
|
info->name,
|
|
|
|
(info->shared_network ? "/" : ""),
|
|
|
|
(info->shared_network ?
|
|
|
|
info->shared_network->name : ""));
|
1996-05-13 00:08:05 +00:00
|
|
|
}
|
2000-03-06 19:39:54 +00:00
|
|
|
|
|
|
|
void if_deregister_receive (info)
|
|
|
|
struct interface_info *info;
|
|
|
|
{
|
2011-06-27 16:00:32 +00:00
|
|
|
#if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
|
|
|
|
/* Dereference the global v4 socket. */
|
|
|
|
if ((info->rfdesc == global_v4_socket) &&
|
|
|
|
(info->wfdesc == global_v4_socket) &&
|
|
|
|
(global_v4_socket_references > 0)) {
|
|
|
|
global_v4_socket_references--;
|
|
|
|
info->rfdesc = -1;
|
|
|
|
} else {
|
|
|
|
log_fatal("Impossible condition at %s:%d", MDL);
|
|
|
|
}
|
2000-03-06 19:39:54 +00:00
|
|
|
|
2011-06-27 16:00:32 +00:00
|
|
|
if (global_v4_socket_references == 0) {
|
|
|
|
close(global_v4_socket);
|
|
|
|
global_v4_socket = -1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
close(info->rfdesc);
|
|
|
|
info->rfdesc = -1;
|
|
|
|
#endif /* IP_PKTINFO... */
|
2000-03-06 19:39:54 +00:00
|
|
|
if (!quiet_interface_discovery)
|
|
|
|
log_info ("Disabling input on Socket/%s%s%s",
|
|
|
|
info -> name,
|
|
|
|
(info -> shared_network ? "/" : ""),
|
|
|
|
(info -> shared_network ?
|
|
|
|
info -> shared_network -> name : ""));
|
2000-04-06 23:56:01 +00:00
|
|
|
}
|
1996-05-13 00:08:05 +00:00
|
|
|
#endif /* USE_SOCKET_RECEIVE */
|
|
|
|
|
2007-05-08 23:05:22 +00:00
|
|
|
|
2007-05-19 18:47:15 +00:00
|
|
|
#ifdef DHCPv6
|
2008-08-29 17:48:57 +00:00
|
|
|
/*
|
|
|
|
* This function joins the interface to DHCPv6 multicast groups so we will
|
|
|
|
* receive multicast messages.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
if_register_multicast(struct interface_info *info) {
|
|
|
|
int sock = info->rfdesc;
|
|
|
|
struct ipv6_mreq mreq;
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, All_DHCP_Relay_Agents_and_Servers,
|
|
|
|
&mreq.ipv6mr_multiaddr) <= 0) {
|
|
|
|
log_fatal("inet_pton: unable to convert '%s'",
|
|
|
|
All_DHCP_Relay_Agents_and_Servers);
|
|
|
|
}
|
|
|
|
mreq.ipv6mr_interface = if_nametoindex(info->name);
|
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
|
|
|
|
&mreq, sizeof(mreq)) < 0) {
|
|
|
|
log_fatal("setsockopt: IPV6_JOIN_GROUP: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The relay agent code sets the streams so you know which way
|
|
|
|
* is up and down. But a relay agent shouldn't join to the
|
|
|
|
* Server address, or else you get fun loops. So up or down
|
|
|
|
* doesn't matter, we're just using that config to sense this is
|
|
|
|
* a relay agent.
|
|
|
|
*/
|
|
|
|
if ((info->flags & INTERFACE_STREAMS) == 0) {
|
|
|
|
if (inet_pton(AF_INET6, All_DHCP_Servers,
|
|
|
|
&mreq.ipv6mr_multiaddr) <= 0) {
|
|
|
|
log_fatal("inet_pton: unable to convert '%s'",
|
|
|
|
All_DHCP_Servers);
|
|
|
|
}
|
|
|
|
mreq.ipv6mr_interface = if_nametoindex(info->name);
|
|
|
|
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
|
|
|
|
&mreq, sizeof(mreq)) < 0) {
|
|
|
|
log_fatal("setsockopt: IPV6_JOIN_GROUP: %m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 23:05:22 +00:00
|
|
|
void
|
|
|
|
if_register6(struct interface_info *info, int do_multicast) {
|
2008-08-29 17:48:57 +00:00
|
|
|
/* Bounce do_multicast to a stack variable because we may change it. */
|
|
|
|
int req_multi = do_multicast;
|
|
|
|
|
|
|
|
if (global_v6_socket_references == 0) {
|
|
|
|
global_v6_socket = if_register_socket(info, AF_INET6,
|
|
|
|
&req_multi);
|
|
|
|
if (global_v6_socket < 0) {
|
|
|
|
/*
|
|
|
|
* if_register_socket() fatally logs if it fails to
|
|
|
|
* create a socket, this is just a sanity check.
|
|
|
|
*/
|
|
|
|
log_fatal("Impossible condition at %s:%d", MDL);
|
|
|
|
} else {
|
|
|
|
log_info("Bound to *:%d", ntohs(local_port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info->rfdesc = global_v6_socket;
|
|
|
|
info->wfdesc = global_v6_socket;
|
|
|
|
global_v6_socket_references++;
|
|
|
|
|
|
|
|
if (req_multi)
|
|
|
|
if_register_multicast(info);
|
|
|
|
|
|
|
|
get_hw_addr(info->name, &info->hw_address);
|
|
|
|
|
2007-05-08 23:05:22 +00:00
|
|
|
if (!quiet_interface_discovery) {
|
|
|
|
if (info->shared_network != NULL) {
|
2008-08-29 17:48:57 +00:00
|
|
|
log_info("Listening on Socket/%d/%s/%s",
|
|
|
|
global_v6_socket, info->name,
|
2007-05-08 23:05:22 +00:00
|
|
|
info->shared_network->name);
|
2008-08-29 17:48:57 +00:00
|
|
|
log_info("Sending on Socket/%d/%s/%s",
|
|
|
|
global_v6_socket, info->name,
|
2007-05-08 23:05:22 +00:00
|
|
|
info->shared_network->name);
|
|
|
|
} else {
|
|
|
|
log_info("Listening on Socket/%s", info->name);
|
|
|
|
log_info("Sending on Socket/%s", info->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if_deregister6(struct interface_info *info) {
|
2008-08-29 17:48:57 +00:00
|
|
|
/* Dereference the global v6 socket. */
|
|
|
|
if ((info->rfdesc == global_v6_socket) &&
|
|
|
|
(info->wfdesc == global_v6_socket) &&
|
|
|
|
(global_v6_socket_references > 0)) {
|
|
|
|
global_v6_socket_references--;
|
|
|
|
info->rfdesc = -1;
|
|
|
|
info->wfdesc = -1;
|
|
|
|
} else {
|
|
|
|
log_fatal("Impossible condition at %s:%d", MDL);
|
|
|
|
}
|
2007-05-08 23:05:22 +00:00
|
|
|
|
|
|
|
if (!quiet_interface_discovery) {
|
|
|
|
if (info->shared_network != NULL) {
|
|
|
|
log_info("Disabling input on Socket/%s/%s", info->name,
|
|
|
|
info->shared_network->name);
|
|
|
|
log_info("Disabling output on Socket/%s/%s", info->name,
|
|
|
|
info->shared_network->name);
|
|
|
|
} else {
|
|
|
|
log_info("Disabling input on Socket/%s", info->name);
|
|
|
|
log_info("Disabling output on Socket/%s", info->name);
|
|
|
|
}
|
|
|
|
}
|
2008-08-29 17:48:57 +00:00
|
|
|
|
|
|
|
if (global_v6_socket_references == 0) {
|
|
|
|
close(global_v6_socket);
|
|
|
|
global_v6_socket = -1;
|
|
|
|
|
|
|
|
log_info("Unbound from *:%d", ntohs(local_port));
|
|
|
|
}
|
2007-05-08 23:05:22 +00:00
|
|
|
}
|
2007-05-19 18:47:15 +00:00
|
|
|
#endif /* DHCPv6 */
|
2007-05-08 23:05:22 +00:00
|
|
|
|
1999-10-24 23:23:41 +00:00
|
|
|
#if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
|
1997-06-08 03:18:09 +00:00
|
|
|
ssize_t send_packet (interface, packet, raw, len, from, to, hto)
|
1996-05-13 00:08:05 +00:00
|
|
|
struct interface_info *interface;
|
1996-03-16 17:50:30 +00:00
|
|
|
struct packet *packet;
|
|
|
|
struct dhcp_packet *raw;
|
|
|
|
size_t len;
|
1996-05-22 07:30:19 +00:00
|
|
|
struct in_addr from;
|
1996-04-11 06:49:21 +00:00
|
|
|
struct sockaddr_in *to;
|
1996-05-13 00:08:05 +00:00
|
|
|
struct hardware *hto;
|
1996-03-16 17:50:30 +00:00
|
|
|
{
|
1997-03-29 00:06:07 +00:00
|
|
|
int result;
|
|
|
|
#ifdef IGNORE_HOSTUNREACH
|
|
|
|
int retry = 0;
|
|
|
|
do {
|
2011-06-27 16:00:32 +00:00
|
|
|
#endif
|
|
|
|
#if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
|
|
|
|
struct in_pktinfo pktinfo;
|
|
|
|
|
|
|
|
if (interface->ifp != NULL) {
|
|
|
|
memset(&pktinfo, 0, sizeof (pktinfo));
|
|
|
|
pktinfo.ipi_ifindex = interface->ifp->ifr_index;
|
|
|
|
if (setsockopt(interface->wfdesc, IPPROTO_IP,
|
|
|
|
IP_PKTINFO, (char *)&pktinfo,
|
|
|
|
sizeof(pktinfo)) < 0)
|
|
|
|
log_fatal("setsockopt: IP_PKTINFO: %m");
|
|
|
|
}
|
1997-03-29 00:06:07 +00:00
|
|
|
#endif
|
|
|
|
result = sendto (interface -> wfdesc, (char *)raw, len, 0,
|
|
|
|
(struct sockaddr *)to, sizeof *to);
|
|
|
|
#ifdef IGNORE_HOSTUNREACH
|
1998-03-15 20:54:20 +00:00
|
|
|
} while (to -> sin_addr.s_addr == htonl (INADDR_BROADCAST) &&
|
1997-03-29 00:06:07 +00:00
|
|
|
result < 0 &&
|
|
|
|
(errno == EHOSTUNREACH ||
|
|
|
|
errno == ECONNREFUSED) &&
|
|
|
|
retry++ < 10);
|
|
|
|
#endif
|
1999-02-25 23:30:43 +00:00
|
|
|
if (result < 0) {
|
1999-03-16 06:37:55 +00:00
|
|
|
log_error ("send_packet: %m");
|
1999-02-25 23:30:43 +00:00
|
|
|
if (errno == ENETUNREACH)
|
1999-10-24 23:23:41 +00:00
|
|
|
log_error ("send_packet: please consult README file%s",
|
|
|
|
" regarding broadcast address.");
|
1999-02-25 23:30:43 +00:00
|
|
|
}
|
1997-03-29 01:26:08 +00:00
|
|
|
return result;
|
1996-03-16 17:50:30 +00:00
|
|
|
}
|
2007-05-08 23:05:22 +00:00
|
|
|
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
|
1996-03-16 17:50:30 +00:00
|
|
|
|
2008-01-09 23:02:42 +00:00
|
|
|
#ifdef DHCPv6
|
|
|
|
/*
|
|
|
|
* Solaris 9 is missing the CMSG_LEN and CMSG_SPACE macros, so we will
|
|
|
|
* synthesize them (based on the BIND 9 technique).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CMSG_LEN
|
|
|
|
static size_t CMSG_LEN(size_t len) {
|
|
|
|
size_t hdrlen;
|
|
|
|
/*
|
|
|
|
* Cast NULL so that any pointer arithmetic performed by CMSG_DATA
|
|
|
|
* is correct.
|
|
|
|
*/
|
|
|
|
hdrlen = (size_t)CMSG_DATA(((struct cmsghdr *)NULL));
|
|
|
|
return hdrlen + len;
|
|
|
|
}
|
|
|
|
#endif /* !CMSG_LEN */
|
|
|
|
|
|
|
|
#ifndef CMSG_SPACE
|
|
|
|
static size_t CMSG_SPACE(size_t len) {
|
|
|
|
struct msghdr msg;
|
|
|
|
struct cmsghdr *cmsgp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: The buffer length is an ad-hoc value, but should be enough
|
|
|
|
* in a practical sense.
|
|
|
|
*/
|
|
|
|
union {
|
|
|
|
struct cmsghdr cmsg_sizer;
|
|
|
|
u_int8_t pktinfo_sizer[sizeof(struct cmsghdr) + 1024];
|
|
|
|
} dummybuf;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.msg_control = &dummybuf;
|
|
|
|
msg.msg_controllen = sizeof(dummybuf);
|
|
|
|
|
|
|
|
cmsgp = (struct cmsghdr *)&dummybuf;
|
|
|
|
cmsgp->cmsg_len = CMSG_LEN(len);
|
|
|
|
|
|
|
|
cmsgp = CMSG_NXTHDR(&msg, cmsgp);
|
|
|
|
if (cmsgp != NULL) {
|
|
|
|
return (char *)cmsgp - (char *)msg.msg_control;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* !CMSG_SPACE */
|
|
|
|
|
|
|
|
#endif /* DHCPv6 */
|
|
|
|
|
2011-06-27 16:00:32 +00:00
|
|
|
#if defined(DHCPv6) || \
|
|
|
|
(defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && \
|
|
|
|
defined(USE_V4_PKTINFO))
|
2010-09-09 22:18:02 +00:00
|
|
|
/*
|
|
|
|
* For both send_packet6() and receive_packet6() we need to allocate
|
|
|
|
* space for the cmsg header information. We do this once and reuse
|
2011-06-27 16:00:32 +00:00
|
|
|
* the buffer. We also need the control buf for send_packet() and
|
|
|
|
* receive_packet() when we use a single socket and IP_PKTINFO to
|
|
|
|
* send the packet out the correct interface.
|
2010-09-09 22:18:02 +00:00
|
|
|
*/
|
|
|
|
static void *control_buf = NULL;
|
|
|
|
static size_t control_buf_len = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
allocate_cmsg_cbuf(void) {
|
|
|
|
control_buf_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
|
|
|
|
control_buf = dmalloc(control_buf_len, MDL);
|
|
|
|
return;
|
|
|
|
}
|
2011-06-27 16:00:32 +00:00
|
|
|
#endif /* DHCPv6, IP_PKTINFO ... */
|
2010-09-09 22:18:02 +00:00
|
|
|
|
2011-06-27 16:00:32 +00:00
|
|
|
#ifdef DHCPv6
|
2007-05-08 23:05:22 +00:00
|
|
|
/*
|
|
|
|
* For both send_packet6() and receive_packet6() we need to use the
|
2007-11-30 21:51:43 +00:00
|
|
|
* sendmsg()/recvmsg() functions rather than the simpler send()/recv()
|
2007-05-08 23:05:22 +00:00
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
* In the case of send_packet6(), we need to do this in order to insure
|
|
|
|
* that the reply packet leaves on the same interface that it arrived
|
|
|
|
* on.
|
|
|
|
*
|
|
|
|
* In the case of receive_packet6(), we need to do this in order to
|
|
|
|
* get the IP address the packet was sent to. This is used to identify
|
|
|
|
* whether a packet is multicast or unicast.
|
|
|
|
*
|
|
|
|
* Helpful man pages: recvmsg, readv (talks about the iovec stuff), cmsg.
|
|
|
|
*
|
|
|
|
* Also see the sections in RFC 3542 about IPV6_PKTINFO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Send an IPv6 packet */
|
|
|
|
ssize_t send_packet6(struct interface_info *interface,
|
2007-05-17 18:27:11 +00:00
|
|
|
const unsigned char *raw, size_t len,
|
2007-05-08 23:05:22 +00:00
|
|
|
struct sockaddr_in6 *to) {
|
|
|
|
struct msghdr m;
|
|
|
|
struct iovec v;
|
|
|
|
int result;
|
|
|
|
struct in6_pktinfo *pktinfo;
|
|
|
|
struct cmsghdr *cmsg;
|
2010-09-09 22:18:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If necessary allocate space for the control message header.
|
|
|
|
* The space is common between send and receive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (control_buf == NULL) {
|
|
|
|
allocate_cmsg_cbuf();
|
|
|
|
if (control_buf == NULL) {
|
|
|
|
log_error("send_packet6: unable to allocate cmsg header");
|
|
|
|
return(ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(control_buf, 0, control_buf_len);
|
2007-05-08 23:05:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize our message header structure.
|
|
|
|
*/
|
|
|
|
memset(&m, 0, sizeof(m));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the target address we're sending to.
|
|
|
|
*/
|
|
|
|
m.msg_name = to;
|
|
|
|
m.msg_namelen = sizeof(*to);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the data buffer we're sending. (Using this wacky
|
|
|
|
* "scatter-gather" stuff... we only have a single chunk
|
|
|
|
* of data to send, so we declare a single vector entry.)
|
|
|
|
*/
|
|
|
|
v.iov_base = (char *)raw;
|
|
|
|
v.iov_len = len;
|
|
|
|
m.msg_iov = &v;
|
|
|
|
m.msg_iovlen = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setting the interface is a bit more involved.
|
|
|
|
*
|
|
|
|
* We have to create a "control message", and set that to
|
|
|
|
* define the IPv6 packet information. We could set the
|
|
|
|
* source address if we wanted, but we can safely let the
|
|
|
|
* kernel decide what that should be.
|
|
|
|
*/
|
2010-09-09 22:18:02 +00:00
|
|
|
m.msg_control = control_buf;
|
|
|
|
m.msg_controllen = control_buf_len;
|
2007-05-08 23:05:22 +00:00
|
|
|
cmsg = CMSG_FIRSTHDR(&m);
|
|
|
|
cmsg->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cmsg->cmsg_type = IPV6_PKTINFO;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
|
|
|
|
pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
|
|
|
|
memset(pktinfo, 0, sizeof(*pktinfo));
|
|
|
|
pktinfo->ipi6_ifindex = if_nametoindex(interface->name);
|
|
|
|
m.msg_controllen = cmsg->cmsg_len;
|
|
|
|
|
|
|
|
result = sendmsg(interface->wfdesc, &m, 0);
|
|
|
|
if (result < 0) {
|
|
|
|
log_error("send_packet6: %m");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2007-05-19 18:47:15 +00:00
|
|
|
#endif /* DHCPv6 */
|
2007-05-08 23:05:22 +00:00
|
|
|
|
1996-04-11 06:49:21 +00:00
|
|
|
#ifdef USE_SOCKET_RECEIVE
|
1997-06-08 03:18:09 +00:00
|
|
|
ssize_t receive_packet (interface, buf, len, from, hfrom)
|
1996-05-13 00:08:05 +00:00
|
|
|
struct interface_info *interface;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t len;
|
|
|
|
struct sockaddr_in *from;
|
|
|
|
struct hardware *hfrom;
|
1996-03-16 17:50:30 +00:00
|
|
|
{
|
2011-06-27 16:00:32 +00:00
|
|
|
#if !defined(USE_V4_PKTINFO)
|
2000-02-03 03:43:51 +00:00
|
|
|
SOCKLEN_T flen = sizeof *from;
|
2011-06-27 16:00:32 +00:00
|
|
|
#endif
|
1997-03-29 00:06:07 +00:00
|
|
|
int result;
|
1996-05-13 00:08:05 +00:00
|
|
|
|
2007-05-08 23:05:22 +00:00
|
|
|
/*
|
|
|
|
* The normal Berkeley socket interface doesn't give us any way
|
|
|
|
* to know what hardware interface we received the message on,
|
|
|
|
* but we should at least make sure the structure is emptied.
|
|
|
|
*/
|
|
|
|
memset(hfrom, 0, sizeof(*hfrom));
|
|
|
|
|
1997-03-29 00:06:07 +00:00
|
|
|
#ifdef IGNORE_HOSTUNREACH
|
|
|
|
int retry = 0;
|
|
|
|
do {
|
|
|
|
#endif
|
2011-06-27 16:00:32 +00:00
|
|
|
|
|
|
|
#if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
|
|
|
|
struct msghdr m;
|
|
|
|
struct iovec v;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
struct in_pktinfo *pktinfo;
|
|
|
|
unsigned int ifindex;
|
|
|
|
int found_pktinfo;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If necessary allocate space for the control message header.
|
|
|
|
* The space is common between send and receive.
|
|
|
|
*/
|
|
|
|
if (control_buf == NULL) {
|
|
|
|
allocate_cmsg_cbuf();
|
|
|
|
if (control_buf == NULL) {
|
|
|
|
log_error("receive_packet: unable to allocate cmsg "
|
|
|
|
"header");
|
|
|
|
return(ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(control_buf, 0, control_buf_len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize our message header structure.
|
|
|
|
*/
|
|
|
|
memset(&m, 0, sizeof(m));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Point so we can get the from address.
|
|
|
|
*/
|
|
|
|
m.msg_name = from;
|
|
|
|
m.msg_namelen = sizeof(*from);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the data buffer we're receiving. (Using this wacky
|
|
|
|
* "scatter-gather" stuff... but we that doesn't really make
|
|
|
|
* sense for us, so we use a single vector entry.)
|
|
|
|
*/
|
|
|
|
v.iov_base = buf;
|
|
|
|
v.iov_len = len;
|
|
|
|
m.msg_iov = &v;
|
|
|
|
m.msg_iovlen = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Getting the interface is a bit more involved.
|
|
|
|
*
|
|
|
|
* We set up some space for a "control message". We have
|
|
|
|
* previously asked the kernel to give us packet
|
|
|
|
* information (when we initialized the interface), so we
|
|
|
|
* should get the destination address from that.
|
|
|
|
*/
|
|
|
|
m.msg_control = control_buf;
|
|
|
|
m.msg_controllen = control_buf_len;
|
|
|
|
|
|
|
|
result = recvmsg(interface->rfdesc, &m, 0);
|
|
|
|
|
|
|
|
if (result >= 0) {
|
|
|
|
/*
|
|
|
|
* If we did read successfully, then we need to loop
|
|
|
|
* through the control messages we received and
|
|
|
|
* find the one with our destination address.
|
|
|
|
*
|
|
|
|
* We also keep a flag to see if we found it. If we
|
|
|
|
* didn't, then we consider this to be an error.
|
|
|
|
*/
|
|
|
|
found_pktinfo = 0;
|
|
|
|
cmsg = CMSG_FIRSTHDR(&m);
|
|
|
|
while (cmsg != NULL) {
|
|
|
|
if ((cmsg->cmsg_level == IPPROTO_IP) &&
|
|
|
|
(cmsg->cmsg_type == IP_PKTINFO)) {
|
|
|
|
pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
|
|
|
|
ifindex = pktinfo->ipi_ifindex;
|
|
|
|
/*
|
|
|
|
* We pass the ifindex back to the caller
|
|
|
|
* using the unused hfrom parameter avoiding
|
|
|
|
* interface changes between sockets and
|
|
|
|
* the discover code.
|
|
|
|
*/
|
|
|
|
memcpy(hfrom->hbuf, &ifindex, sizeof(ifindex));
|
|
|
|
found_pktinfo = 1;
|
|
|
|
}
|
|
|
|
cmsg = CMSG_NXTHDR(&m, cmsg);
|
|
|
|
}
|
|
|
|
if (!found_pktinfo) {
|
|
|
|
result = -1;
|
|
|
|
errno = EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
1999-10-24 17:17:51 +00:00
|
|
|
result = recvfrom (interface -> rfdesc, (char *)buf, len, 0,
|
1997-03-29 00:06:07 +00:00
|
|
|
(struct sockaddr *)from, &flen);
|
2011-06-27 16:00:32 +00:00
|
|
|
#endif /* IP_PKTINFO ... */
|
1997-03-29 00:06:07 +00:00
|
|
|
#ifdef IGNORE_HOSTUNREACH
|
|
|
|
} while (result < 0 &&
|
|
|
|
(errno == EHOSTUNREACH ||
|
|
|
|
errno == ECONNREFUSED) &&
|
|
|
|
retry++ < 10);
|
|
|
|
#endif
|
1997-03-29 01:26:08 +00:00
|
|
|
return result;
|
1996-04-11 06:49:21 +00:00
|
|
|
}
|
2007-05-08 23:05:22 +00:00
|
|
|
|
1996-04-11 06:49:21 +00:00
|
|
|
#endif /* USE_SOCKET_RECEIVE */
|
1996-06-12 23:52:38 +00:00
|
|
|
|
2007-05-19 18:47:15 +00:00
|
|
|
#ifdef DHCPv6
|
2007-05-08 23:05:22 +00:00
|
|
|
ssize_t
|
|
|
|
receive_packet6(struct interface_info *interface,
|
|
|
|
unsigned char *buf, size_t len,
|
2008-08-29 17:48:57 +00:00
|
|
|
struct sockaddr_in6 *from, struct in6_addr *to_addr,
|
|
|
|
unsigned int *if_idx)
|
|
|
|
{
|
2007-05-08 23:05:22 +00:00
|
|
|
struct msghdr m;
|
|
|
|
struct iovec v;
|
|
|
|
int result;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
struct in6_pktinfo *pktinfo;
|
2008-08-29 17:48:57 +00:00
|
|
|
int found_pktinfo;
|
2010-09-09 22:18:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If necessary allocate space for the control message header.
|
|
|
|
* The space is common between send and receive.
|
|
|
|
*/
|
|
|
|
if (control_buf == NULL) {
|
|
|
|
allocate_cmsg_cbuf();
|
|
|
|
if (control_buf == NULL) {
|
2010-09-09 22:49:58 +00:00
|
|
|
log_error("receive_packet6: unable to allocate cmsg "
|
|
|
|
"header");
|
2010-09-09 22:18:02 +00:00
|
|
|
return(ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(control_buf, 0, control_buf_len);
|
2007-05-08 23:05:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize our message header structure.
|
|
|
|
*/
|
|
|
|
memset(&m, 0, sizeof(m));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Point so we can get the from address.
|
|
|
|
*/
|
|
|
|
m.msg_name = from;
|
|
|
|
m.msg_namelen = sizeof(*from);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the data buffer we're receiving. (Using this wacky
|
|
|
|
* "scatter-gather" stuff... but we that doesn't really make
|
|
|
|
* sense for us, so we use a single vector entry.)
|
|
|
|
*/
|
|
|
|
v.iov_base = buf;
|
|
|
|
v.iov_len = len;
|
|
|
|
m.msg_iov = &v;
|
|
|
|
m.msg_iovlen = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Getting the interface is a bit more involved.
|
|
|
|
*
|
|
|
|
* We set up some space for a "control message". We have
|
|
|
|
* previously asked the kernel to give us packet
|
|
|
|
* information (when we initialized the interface), so we
|
|
|
|
* should get the destination address from that.
|
|
|
|
*/
|
2010-09-09 22:18:02 +00:00
|
|
|
m.msg_control = control_buf;
|
|
|
|
m.msg_controllen = control_buf_len;
|
2007-05-08 23:05:22 +00:00
|
|
|
|
|
|
|
result = recvmsg(interface->rfdesc, &m, 0);
|
|
|
|
|
|
|
|
if (result >= 0) {
|
|
|
|
/*
|
|
|
|
* If we did read successfully, then we need to loop
|
|
|
|
* through the control messages we received and
|
|
|
|
* find the one with our destination address.
|
|
|
|
*
|
|
|
|
* We also keep a flag to see if we found it. If we
|
|
|
|
* didn't, then we consider this to be an error.
|
|
|
|
*/
|
2008-08-29 17:48:57 +00:00
|
|
|
found_pktinfo = 0;
|
2007-05-08 23:05:22 +00:00
|
|
|
cmsg = CMSG_FIRSTHDR(&m);
|
|
|
|
while (cmsg != NULL) {
|
|
|
|
if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
|
|
|
|
(cmsg->cmsg_type == IPV6_PKTINFO)) {
|
|
|
|
pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
|
|
|
|
*to_addr = pktinfo->ipi6_addr;
|
2008-08-29 17:48:57 +00:00
|
|
|
*if_idx = pktinfo->ipi6_ifindex;
|
|
|
|
found_pktinfo = 1;
|
2007-05-08 23:05:22 +00:00
|
|
|
}
|
|
|
|
cmsg = CMSG_NXTHDR(&m, cmsg);
|
|
|
|
}
|
2008-08-29 17:48:57 +00:00
|
|
|
if (!found_pktinfo) {
|
2007-05-08 23:05:22 +00:00
|
|
|
result = -1;
|
|
|
|
errno = EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2007-05-19 18:47:15 +00:00
|
|
|
#endif /* DHCPv6 */
|
2007-05-08 23:05:22 +00:00
|
|
|
|
1999-05-27 14:18:27 +00:00
|
|
|
#if defined (USE_SOCKET_FALLBACK)
|
1996-06-12 23:52:38 +00:00
|
|
|
/* This just reads in a packet and silently discards it. */
|
|
|
|
|
1999-09-08 01:44:08 +00:00
|
|
|
isc_result_t fallback_discard (object)
|
1999-09-09 23:53:29 +00:00
|
|
|
omapi_object_t *object;
|
1996-06-12 23:52:38 +00:00
|
|
|
{
|
|
|
|
char buf [1540];
|
|
|
|
struct sockaddr_in from;
|
2000-02-03 03:43:51 +00:00
|
|
|
SOCKLEN_T flen = sizeof from;
|
1997-03-06 06:55:53 +00:00
|
|
|
int status;
|
1999-09-08 01:44:08 +00:00
|
|
|
struct interface_info *interface;
|
|
|
|
|
|
|
|
if (object -> type != dhcp_type_interface)
|
2009-10-28 04:12:30 +00:00
|
|
|
return DHCP_R_INVALIDARG;
|
1999-09-08 01:44:08 +00:00
|
|
|
interface = (struct interface_info *)object;
|
1996-06-12 23:52:38 +00:00
|
|
|
|
1997-03-06 06:55:53 +00:00
|
|
|
status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0,
|
|
|
|
(struct sockaddr *)&from, &flen);
|
2002-06-09 22:19:37 +00:00
|
|
|
#if defined (DEBUG)
|
|
|
|
/* Only report fallback discard errors if we're debugging. */
|
1999-09-08 01:44:08 +00:00
|
|
|
if (status < 0) {
|
1999-02-24 17:56:53 +00:00
|
|
|
log_error ("fallback_discard: %m");
|
1999-09-08 01:44:08 +00:00
|
|
|
return ISC_R_UNEXPECTED;
|
|
|
|
}
|
2002-06-09 22:19:37 +00:00
|
|
|
#endif
|
1999-09-08 01:44:08 +00:00
|
|
|
return ISC_R_SUCCESS;
|
1996-06-12 23:52:38 +00:00
|
|
|
}
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif /* USE_SOCKET_FALLBACK */
|
1999-02-14 19:04:05 +00:00
|
|
|
|
1999-10-24 23:23:41 +00:00
|
|
|
#if defined (USE_SOCKET_SEND)
|
1999-03-13 18:56:47 +00:00
|
|
|
int can_unicast_without_arp (ip)
|
|
|
|
struct interface_info *ip;
|
1999-02-14 19:04:05 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-03-13 18:56:47 +00:00
|
|
|
int can_receive_unicast_unconfigured (ip)
|
|
|
|
struct interface_info *ip;
|
1999-03-13 18:53:15 +00:00
|
|
|
{
|
1999-10-24 23:23:41 +00:00
|
|
|
#if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
|
|
|
|
return 1;
|
|
|
|
#else
|
1999-03-13 18:53:15 +00:00
|
|
|
return 0;
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif
|
1999-03-13 18:53:15 +00:00
|
|
|
}
|
|
|
|
|
2000-09-01 23:03:39 +00:00
|
|
|
int supports_multiple_interfaces (ip)
|
|
|
|
struct interface_info *ip;
|
|
|
|
{
|
2011-06-27 16:00:32 +00:00
|
|
|
#if defined(SO_BINDTODEVICE) || \
|
|
|
|
(defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && \
|
|
|
|
defined(USE_V4_PKTINFO))
|
|
|
|
return(1);
|
2000-09-01 23:03:39 +00:00
|
|
|
#else
|
2011-06-27 16:00:32 +00:00
|
|
|
return(0);
|
2000-09-01 23:03:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-02-14 19:04:05 +00:00
|
|
|
/* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
|
|
|
|
do not. */
|
|
|
|
|
|
|
|
void maybe_setup_fallback ()
|
|
|
|
{
|
1999-10-24 23:23:41 +00:00
|
|
|
#if defined (USE_SOCKET_FALLBACK)
|
1999-09-08 01:44:08 +00:00
|
|
|
isc_result_t status;
|
2000-09-11 17:39:14 +00:00
|
|
|
struct interface_info *fbi = (struct interface_info *)0;
|
|
|
|
if (setup_fallback (&fbi, MDL)) {
|
2007-05-08 23:05:22 +00:00
|
|
|
fbi -> wfdesc = if_register_socket (fbi, AF_INET, 0);
|
2000-09-30 01:24:55 +00:00
|
|
|
fbi -> rfdesc = fbi -> wfdesc;
|
|
|
|
log_info ("Sending on Socket/%s%s%s",
|
|
|
|
fbi -> name,
|
|
|
|
(fbi -> shared_network ? "/" : ""),
|
|
|
|
(fbi -> shared_network ?
|
|
|
|
fbi -> shared_network -> name : ""));
|
|
|
|
|
2000-01-02 22:21:23 +00:00
|
|
|
status = omapi_register_io_object ((omapi_object_t *)fbi,
|
1999-09-08 01:44:08 +00:00
|
|
|
if_readsocket, 0,
|
|
|
|
fallback_discard, 0, 0);
|
|
|
|
if (status != ISC_R_SUCCESS)
|
|
|
|
log_fatal ("Can't register I/O handle for %s: %s",
|
|
|
|
fbi -> name, isc_result_totext (status));
|
2000-09-11 17:39:14 +00:00
|
|
|
interface_dereference (&fbi, MDL);
|
1999-02-14 19:04:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2011-06-27 16:00:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if defined(sun) && defined(USE_V4_PKTINFO)
|
|
|
|
/* This code assumes the existence of SIOCGLIFHWADDR */
|
|
|
|
void
|
|
|
|
get_hw_addr(const char *name, struct hardware *hw) {
|
|
|
|
struct sockaddr_dl *dladdrp;
|
|
|
|
int rv, sock, i;
|
|
|
|
struct lifreq lifr;
|
|
|
|
|
|
|
|
memset(&lifr, 0, sizeof (lifr));
|
|
|
|
(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
|
|
|
|
/*
|
|
|
|
* Check if the interface is a virtual or IPMP interface - in those
|
|
|
|
* cases it has no hw address, so generate a random one.
|
|
|
|
*/
|
|
|
|
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
|
|
|
|
ioctl(sock, SIOCGLIFFLAGS, &lifr) < 0) {
|
|
|
|
if (sock != -1)
|
|
|
|
(void) close(sock);
|
|
|
|
|
|
|
|
#ifdef DHCPv6
|
|
|
|
/*
|
|
|
|
* If approrpriate try this with an IPv6 socket
|
|
|
|
*/
|
|
|
|
if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0 &&
|
|
|
|
ioctl(sock, SIOCGLIFFLAGS, &lifr) >= 0) {
|
|
|
|
goto flag_check;
|
|
|
|
}
|
|
|
|
if (sock != -1)
|
|
|
|
(void) close(sock);
|
|
|
|
#endif
|
|
|
|
log_fatal("Couldn't get interface flags for %s: %m", name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
flag_check:
|
|
|
|
if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP)) {
|
|
|
|
hw->hlen = sizeof (hw->hbuf);
|
|
|
|
srandom((long)gethrtime());
|
|
|
|
|
|
|
|
for (i = 0; i < hw->hlen; ++i) {
|
|
|
|
hw->hbuf[i] = random() % 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock != -1)
|
|
|
|
(void) close(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(sock, SIOCGLIFHWADDR, &lifr) < 0)
|
|
|
|
log_fatal("Couldn't get interface hardware address for %s: %m",
|
|
|
|
name);
|
|
|
|
dladdrp = (struct sockaddr_dl *)&lifr.lifr_addr;
|
|
|
|
hw->hlen = dladdrp->sdl_alen;
|
|
|
|
memcpy(hw->hbuf, LLADDR(dladdrp), hw->hlen);
|
|
|
|
|
|
|
|
if (sock != -1)
|
|
|
|
(void) close(sock);
|
|
|
|
}
|
|
|
|
#endif /* defined(sun) */
|
|
|
|
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif /* USE_SOCKET_SEND */
|