1995-11-29 07:40:04 +00:00
|
|
|
/* socket.c
|
|
|
|
|
|
|
|
BSD socket interface code... */
|
|
|
|
|
|
|
|
/*
|
1999-03-16 05:50:46 +00:00
|
|
|
* Copyright (c) 1996-1999 Internet Software Consortium.
|
|
|
|
* Use is subject to license terms which appear in the file named
|
|
|
|
* ISC-LICENSE that should have accompanied this file when you
|
|
|
|
* received it. If a file named ISC-LICENSE did not accompany this
|
|
|
|
* file, or you are not sure the one you have is correct, you may
|
|
|
|
* obtain an applicable copy of the license at:
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
1999-03-16 05:50:46 +00:00
|
|
|
* http://www.isc.org/isc-license-1.0.html.
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
1999-03-16 05:50:46 +00:00
|
|
|
* This file is part of the ISC DHCP distribution. The documentation
|
|
|
|
* associated with this file is listed in the file DOCUMENTATION,
|
|
|
|
* included in the top-level directory of this release.
|
1995-11-29 07:40:04 +00:00
|
|
|
*
|
1999-03-16 05:50:46 +00:00
|
|
|
* Support and other services are available for ISC products - see
|
|
|
|
* http://www.isc.org for more information.
|
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
|
|
|
#ifndef lint
|
|
|
|
static char copyright[] =
|
2000-02-01 03:19:56 +00:00
|
|
|
"$Id: socket.c,v 1.44 2000/02/01 03:19:39 mellon Exp $ Copyright (c) 1995, 1996, 1997, 1998, 1999 The Internet Software Consortium. All rights reserved.\n";
|
1995-11-29 07:40:04 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "dhcpd.h"
|
|
|
|
|
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
|
|
|
|
|
|
|
|
static int once = 0;
|
|
|
|
|
|
|
|
/* 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... */
|
1997-02-19 10:51:44 +00:00
|
|
|
int if_register_socket (info)
|
1996-04-11 06:49:21 +00:00
|
|
|
struct interface_info *info;
|
1995-11-29 07:40:04 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
|
|
|
int sock;
|
|
|
|
int flag;
|
1997-01-02 12:00:19 +00:00
|
|
|
|
1999-02-25 23:30:43 +00:00
|
|
|
#if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
|
1996-05-13 00:08:05 +00:00
|
|
|
/* Make sure only one interface is registered. */
|
|
|
|
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.");
|
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
|
|
|
|
|
|
|
/* Set up the address we're going to bind to. */
|
1995-11-29 07:40:04 +00:00
|
|
|
name.sin_family = AF_INET;
|
1997-02-18 14:32:51 +00:00
|
|
|
name.sin_port = local_port;
|
1996-05-13 00:08:05 +00:00
|
|
|
name.sin_addr.s_addr = INADDR_ANY;
|
1995-11-29 07:40:04 +00:00
|
|
|
memset (name.sin_zero, 0, sizeof (name.sin_zero));
|
|
|
|
|
1996-05-22 07:30:19 +00:00
|
|
|
/* Make a socket... */
|
1995-11-29 07:40:04 +00:00
|
|
|
if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
|
1999-02-24 17:56:53 +00:00
|
|
|
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;
|
|
|
|
if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
|
1996-05-16 23:13:59 +00:00
|
|
|
(char *)&flag, sizeof flag) < 0)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't set SO_REUSEADDR option on dhcp socket: %m");
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-13 00:08:05 +00:00
|
|
|
/* Set the BROADCAST option so that we can broadcast DHCP responses. */
|
1995-11-29 07:40:04 +00:00
|
|
|
if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST,
|
1996-05-16 23:13:59 +00:00
|
|
|
(char *)&flag, sizeof flag) < 0)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't set SO_BROADCAST option on dhcp socket: %m");
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1996-05-13 00:08:05 +00:00
|
|
|
/* Bind the socket to this interface's IP address. */
|
1995-11-29 07:40:04 +00:00
|
|
|
if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0)
|
1999-02-24 17:56:53 +00:00
|
|
|
log_fatal ("Can't bind to dhcp address: %m");
|
1995-11-29 07:40:04 +00:00
|
|
|
|
1999-02-25 23:30:43 +00:00
|
|
|
#if defined (HAVE_SO_BINDTODEVICE)
|
1997-01-02 12:00:19 +00:00
|
|
|
/* Bind this socket to this interface. */
|
1999-02-14 19:04:05 +00:00
|
|
|
if (info -> ifp &&
|
|
|
|
setsockopt (sock, SOL_SOCKET, SO_BINDTODEVICE,
|
1997-02-19 10:51:44 +00:00
|
|
|
(char *)(info -> ifp), sizeof *(info -> ifp)) < 0) {
|
1999-05-06 20:21:39 +00:00
|
|
|
log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
|
1997-01-02 12:00:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
1997-02-19 10:51:44 +00:00
|
|
|
info -> wfdesc = if_register_socket (info);
|
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. */
|
|
|
|
info -> rfdesc = info -> wfdesc;
|
|
|
|
#endif
|
1996-05-13 00:08:05 +00:00
|
|
|
#else
|
|
|
|
info -> wfdesc = info -> rfdesc;
|
|
|
|
#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",
|
1997-10-20 21:47:15 +00:00
|
|
|
info -> name,
|
1999-02-25 23:30:43 +00:00
|
|
|
(info -> shared_network ? "/" : ""),
|
1997-10-20 21:47:15 +00:00
|
|
|
(info -> shared_network ?
|
1999-02-25 23:30:43 +00:00
|
|
|
info -> shared_network -> name : ""));
|
1995-11-29 07:40:04 +00:00
|
|
|
}
|
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;
|
|
|
|
{
|
|
|
|
/* If we're using the socket API for sending and receiving,
|
|
|
|
we don't need to register this interface twice. */
|
1997-02-19 10:51:44 +00:00
|
|
|
info -> rfdesc = if_register_socket (info);
|
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",
|
1997-10-20 21:47:15 +00:00
|
|
|
info -> name,
|
1999-02-25 23:30:43 +00:00
|
|
|
(info -> shared_network ? "/" : ""),
|
1997-10-20 21:47:15 +00:00
|
|
|
(info -> shared_network ?
|
1999-02-25 23:30:43 +00:00
|
|
|
info -> shared_network -> name : ""));
|
1996-05-13 00:08:05 +00:00
|
|
|
}
|
|
|
|
#endif /* USE_SOCKET_RECEIVE */
|
|
|
|
|
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 {
|
|
|
|
#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
|
|
|
}
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
|
1996-03-16 17:50:30 +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
|
|
|
{
|
2000-02-01 03:19:56 +00:00
|
|
|
socklen_t flen = sizeof *from;
|
1997-03-29 00:06:07 +00:00
|
|
|
int result;
|
1996-05-13 00:08:05 +00:00
|
|
|
|
1997-03-29 00:06:07 +00:00
|
|
|
#ifdef IGNORE_HOSTUNREACH
|
|
|
|
int retry = 0;
|
|
|
|
do {
|
|
|
|
#endif
|
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);
|
|
|
|
#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
|
|
|
}
|
|
|
|
#endif /* USE_SOCKET_RECEIVE */
|
1996-06-12 23:52:38 +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-01 03:19:56 +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)
|
|
|
|
return ISC_R_INVALIDARG;
|
|
|
|
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);
|
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;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
1999-02-14 19:04:05 +00:00
|
|
|
struct interface_info *fbi;
|
|
|
|
fbi = setup_fallback ();
|
|
|
|
if (fbi) {
|
|
|
|
fbi -> wfdesc = if_register_socket (fbi);
|
1999-09-08 01:44:08 +00:00
|
|
|
fbi -> refcnt = 1;
|
|
|
|
fbi -> type = dhcp_type_interface;
|
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));
|
1999-02-14 19:04:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
1999-10-24 23:23:41 +00:00
|
|
|
#endif /* USE_SOCKET_SEND */
|