summaryrefslogtreecommitdiff
path: root/common/socket.c
diff options
context:
space:
mode:
authorDavid Hankins <dhankins@isc.org>2007-05-08 23:05:22 +0000
committerDavid Hankins <dhankins@isc.org>2007-05-08 23:05:22 +0000
commit98bd7ca0990e6d88e3345d3bc966ebe8216691a7 (patch)
treec4437ca467e8f733d530170a5c445747b2defd68 /common/socket.c
parent74dc3e0b2786c46956e7517398ae6f7c6dad52d7 (diff)
downloadisc-dhcp-98bd7ca0990e6d88e3345d3bc966ebe8216691a7.tar.gz
DHCPv6 branch merged to HEAD.
Diffstat (limited to 'common/socket.c')
-rw-r--r--common/socket.c361
1 files changed, 322 insertions, 39 deletions
diff --git a/common/socket.c b/common/socket.c
index 1700612f..91ee49de 100644
--- a/common/socket.c
+++ b/common/socket.c
@@ -3,7 +3,7 @@
BSD socket interface code... */
/*
- * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 2004-2006 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1995-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
@@ -42,7 +42,8 @@
#ifndef lint
static char copyright[] =
-"$Id: socket.c,v 1.59 2006/07/25 17:41:18 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
+"$Id: socket.c,v 1.60 2007/05/08 23:05:20 dhankins Exp $ "
+"Copyright (c) 2004-2006 Internet Systems Consortium.\n";
#endif /* not lint */
#include "dhcpd.h"
@@ -90,62 +91,89 @@ void if_reinitialize_receive (info)
defined (USE_SOCKET_RECEIVE) || \
defined (USE_SOCKET_FALLBACK)
/* Generic interface registration routine... */
-int if_register_socket (info)
- struct interface_info *info;
-{
- struct sockaddr_in name;
+int
+if_register_socket(struct interface_info *info, int family, int do_multicast) {
+ struct sockaddr_storage name;
+ int name_len;
int sock;
int flag;
+ int domain;
+
+ /* INSIST((family == AF_INET) || (family == AF_INET6)); */
#if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
/* Make sure only one interface is registered. */
- if (once)
+ if (once) {
log_fatal ("The standard socket API can only support %s",
"hosts with a single network interface.");
+ }
once = 1;
#endif
- memset (&name, 0, sizeof (name));
- /* Set up the address we're going to bind to. */
- name.sin_family = AF_INET;
- name.sin_port = local_port;
- name.sin_addr = local_address;
+ /*
+ * Set up the address we're going to bind to, depending on the
+ * address family.
+ */
+ memset(&name, 0, sizeof(name));
+ if (family == AF_INET) {
+ struct sockaddr_in *addr = (struct sockaddr_in *)&name;
+ addr->sin_family = AF_INET;
+ addr->sin_port = local_port;
+ memcpy(&addr->sin_addr,
+ &local_address,
+ sizeof(addr->sin_addr));
+ name_len = sizeof(*addr);
+ domain = PF_INET;
+ } else {
+ struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&name;
+ addr->sin6_family = AF_INET6;
+ addr->sin6_port = local_port;
+ memcpy(&addr->sin6_addr,
+ &local_address6,
+ sizeof(addr->sin6_addr));
+ name_len = sizeof(*addr);
+ domain = PF_INET6;
+ }
/* Make a socket... */
- if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
- log_fatal ("Can't create dhcp socket: %m");
+ sock = socket(domain, SOCK_DGRAM, IPPROTO_UDP);
+ if (sock < 0) {
+ log_fatal("Can't create dhcp socket: %m");
+ }
/* Set the REUSEADDR option so that we don't fail to start if
we're being restarted. */
flag = 1;
- if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
- (char *)&flag, sizeof flag) < 0)
- log_fatal ("Can't set SO_REUSEADDR option on dhcp socket: %m");
+ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
+ (char *)&flag, sizeof(flag)) < 0) {
+ log_fatal("Can't set SO_REUSEADDR option on dhcp socket: %m");
+ }
/* 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. */
- 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");
+ 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");
+ }
/* Bind the socket to this interface's IP address. */
- if (bind (sock, (struct sockaddr *)&name, sizeof name) < 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.");
+ 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.");
}
#if defined (HAVE_SO_BINDTODEVICE)
/* Bind this socket to this interface. */
- if (info -> ifp &&
- setsockopt (sock, SOL_SOCKET, SO_BINDTODEVICE,
- (char *)(info -> ifp), sizeof *(info -> ifp)) < 0) {
- log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
+ if (info->ifp &&
+ setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
+ (char *)(info -> ifp), sizeof(*(info -> ifp))) < 0) {
+ log_fatal("setsockopt: SO_BINDTODEVICE: %m");
}
#endif
@@ -157,12 +185,63 @@ int if_register_socket (info)
* releases.
*/
#if defined(SCO) && defined(IP_BROADCAST_IF)
- if (setsockopt (sock, IPPROTO_IP, IP_BROADCAST_IF,
- &info -> primary_address,
- sizeof (info -> primary_address)) < 0)
- log_fatal ("Can't set IP_BROADCAST_IF on dhcp socket: %m");
+ 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");
#endif
+ /*
+ * 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
+ }
+
+ if ((family == AF_INET6) && do_multicast) {
+ struct ipv6_mreq mreq;
+
+ /*
+ * Join the DHCPv6 multicast groups so we will receive
+ * multicast messages.
+ */
+ 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");
+ }
+ 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");
+ }
+ }
+
return sock;
}
#endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
@@ -172,7 +251,7 @@ void if_register_send (info)
struct interface_info *info;
{
#ifndef USE_SOCKET_RECEIVE
- info -> wfdesc = if_register_socket (info);
+ info -> wfdesc = if_register_socket (info, AF_INET, 0);
#if defined (USE_SOCKET_FALLBACK)
/* Fallback only registers for send, but may need to receive as
well. */
@@ -214,7 +293,7 @@ void if_register_receive (info)
{
/* If we're using the socket API for sending and receiving,
we don't need to register this interface twice. */
- info -> rfdesc = if_register_socket (info);
+ info -> rfdesc = if_register_socket (info, AF_INET, 0);
if (!quiet_interface_discovery)
log_info ("Listening on Socket/%s%s%s",
info -> name,
@@ -238,6 +317,49 @@ void if_deregister_receive (info)
}
#endif /* USE_SOCKET_RECEIVE */
+
+void
+if_register6(struct interface_info *info, int do_multicast) {
+ info->rfdesc = if_register_socket(info, AF_INET6, do_multicast);
+ info->wfdesc = info->rfdesc;
+ get_hw_addr(info->name, &info->hw_address);
+ if (!quiet_interface_discovery) {
+ if (info->shared_network != NULL) {
+ log_info("Listening on Socket/%s/%s", info->name,
+ info->shared_network->name);
+ log_info("Sending on Socket/%s/%s", info->name,
+ 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) {
+ /*
+ * XXX: it would be nice to check for >= 0, but we need to change
+ * interface_allocate() to set the file descriptors for that.
+ */
+ close(info->rfdesc);
+ info->rfdesc = -1;
+ close(info->wfdesc);
+ info->wfdesc = -1;
+
+ 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);
+ }
+ }
+}
+
#if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
ssize_t send_packet (interface, packet, raw, len, from, to, hto)
struct interface_info *interface;
@@ -270,8 +392,86 @@ ssize_t send_packet (interface, packet, raw, len, from, to, hto)
}
return result;
}
+
#endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
+/*
+ * For both send_packet6() and receive_packet6() we need to use the
+ * sendmsg()/recvmsg() functions rather than the simplier send()/recv()
+ * 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,
+ struct dhcp_packet *raw,
+ size_t len,
+ struct sockaddr_in6 *to) {
+ struct msghdr m;
+ struct iovec v;
+ int result;
+ struct in6_pktinfo *pktinfo;
+ char pbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
+ struct cmsghdr *cmsg;
+
+ /*
+ * 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.
+ */
+ m.msg_control = pbuf;
+ m.msg_controllen = sizeof(pbuf);
+ 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;
+}
+
#ifdef USE_SOCKET_RECEIVE
ssize_t receive_packet (interface, buf, len, from, hfrom)
struct interface_info *interface;
@@ -283,6 +483,13 @@ ssize_t receive_packet (interface, buf, len, from, hfrom)
SOCKLEN_T flen = sizeof *from;
int result;
+ /*
+ * 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));
+
#ifdef IGNORE_HOSTUNREACH
int retry = 0;
do {
@@ -297,8 +504,84 @@ ssize_t receive_packet (interface, buf, len, from, hfrom)
#endif
return result;
}
+
#endif /* USE_SOCKET_RECEIVE */
+ssize_t
+receive_packet6(struct interface_info *interface,
+ unsigned char *buf, size_t len,
+ struct sockaddr_in6 *from, struct in6_addr *to_addr) {
+ struct msghdr m;
+ struct iovec v;
+ char pbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
+ int result;
+ struct cmsghdr *cmsg;
+ struct in6_pktinfo *pktinfo;
+ int found_to_addr;
+
+ /*
+ * 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 = pbuf;
+ m.msg_controllen = sizeof(pbuf);
+
+ 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_to_addr = 0;
+ 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;
+ found_to_addr = 1;
+ }
+ cmsg = CMSG_NXTHDR(&m, cmsg);
+ }
+ if (!found_to_addr) {
+ result = -1;
+ errno = EIO;
+ }
+ }
+
+ return result;
+}
+
#if defined (USE_SOCKET_FALLBACK)
/* This just reads in a packet and silently discards it. */
@@ -364,7 +647,7 @@ void maybe_setup_fallback ()
isc_result_t status;
struct interface_info *fbi = (struct interface_info *)0;
if (setup_fallback (&fbi, MDL)) {
- fbi -> wfdesc = if_register_socket (fbi);
+ fbi -> wfdesc = if_register_socket (fbi, AF_INET, 0);
fbi -> rfdesc = fbi -> wfdesc;
log_info ("Sending on Socket/%s%s%s",
fbi -> name,