aboutsummaryrefslogtreecommitdiff
path: root/contrib/isc-dhcp/common/lpf.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/isc-dhcp/common/lpf.c')
-rw-r--r--contrib/isc-dhcp/common/lpf.c203
1 files changed, 136 insertions, 67 deletions
diff --git a/contrib/isc-dhcp/common/lpf.c b/contrib/isc-dhcp/common/lpf.c
index 14dd21af7adb..da03c31115b1 100644
--- a/contrib/isc-dhcp/common/lpf.c
+++ b/contrib/isc-dhcp/common/lpf.c
@@ -4,8 +4,8 @@
Support Services in Vancouver, B.C. */
/*
- * Copyright (c) 1995, 1996, 1998, 1999
- * The Internet Software Consortium. All rights reserved.
+ * Copyright (c) 1996-2000 Internet Software Consortium.
+ * All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -33,17 +33,11 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
- * This software has been written for the Internet Software Consortium
- * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
- * Enterprises. To learn more about the Internet Software Consortium,
- * see ``http://www.vix.com/isc''. To learn more about Vixie
- * Enterprises, see ``http://www.vix.com''.
*/
#ifndef lint
static char copyright[] =
-"$Id: lpf.c,v 1.1.2.10 1999/10/25 15:39:02 mellon Exp $ Copyright (c) 1995, 1996, 1998, 1999 The Internet Software Consortium. All rights reserved.\n";
+"$Id: lpf.c,v 1.29 2001/04/24 00:36:00 mellon Exp $ Copyright (c) 1996-2000 The Internet Software Consortium. All rights reserved.\n";
#endif /* not lint */
#include "dhcpd.h"
@@ -59,9 +53,6 @@ static char copyright[] =
#include "includes/netinet/udp.h"
#include "includes/netinet/if_ether.h"
-static void lpf_gen_filter_setup PROTO ((struct interface_info *));
-static void lpf_tr_filter_setup PROTO ((struct interface_info *));
-
/* Reinitializes the specified interface after an address change. This
is not required for packet-filter APIs. */
@@ -92,16 +83,19 @@ int if_register_lpf (info)
struct sockaddr sa;
/* Make an LPF socket. */
- if ((sock = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) {
+ if ((sock = socket(PF_PACKET, SOCK_PACKET,
+ htons((short)ETH_P_ALL))) < 0) {
if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
errno == EAFNOSUPPORT || errno == EINVAL) {
- warn ("socket: %m");
- error ("Make sure to set %s %s!",
- "CONFIG_PACKET=y and CONFIG_FILTER=y",
- "in your kernel configuration");
+ log_error ("socket: %m - make sure");
+ log_error ("CONFIG_PACKET (Packet socket) %s",
+ "and CONFIG_FILTER");
+ log_error ("(Socket Filtering) are enabled %s",
+ "in your kernel");
+ log_fatal ("configuration!");
}
- error("Open a socket for LPF: %m");
+ log_fatal ("Open a socket for LPF: %m");
}
/* Bind to the interface name */
@@ -112,12 +106,14 @@ int if_register_lpf (info)
if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
errno == EAFNOSUPPORT || errno == EINVAL) {
- warn ("bind: %m");
- error ("Set %s %s!",
- "CONFIG_PACKET=y and CONFIG_FILTER=y",
- "in your kernel configuration");
+ log_error ("socket: %m - make sure");
+ log_error ("CONFIG_PACKET (Packet socket) %s",
+ "and CONFIG_FILTER");
+ log_error ("(Socket Filtering) are enabled %s",
+ "in your kernel");
+ log_fatal ("configuration!");
}
- error("Bind socket to interface: %m");
+ log_fatal ("Bind socket to interface: %m");
}
return sock;
@@ -131,16 +127,38 @@ void if_register_send (info)
/* If we're using the lpf API for sending and receiving,
we don't need to register this interface twice. */
#ifndef USE_LPF_RECEIVE
- info -> wfdesc = if_register_lpf (info, interface);
+ info -> wfdesc = if_register_lpf (info);
#else
info -> wfdesc = info -> rfdesc;
#endif
if (!quiet_interface_discovery)
- note ("Sending on LPF/%s/%s%s%s",
+ log_info ("Sending on LPF/%s/%s%s%s",
+ info -> name,
+ print_hw_addr (info -> hw_address.hbuf [0],
+ info -> hw_address.hlen - 1,
+ &info -> hw_address.hbuf [1]),
+ (info -> shared_network ? "/" : ""),
+ (info -> shared_network ?
+ info -> shared_network -> name : ""));
+}
+
+void if_deregister_send (info)
+ struct interface_info *info;
+{
+ /* don't need to close twice if we are using lpf for sending and
+ receiving */
+#ifndef USE_LPF_RECEIVE
+ /* for LPF this is simple, packet filters are removed when sockets
+ are closed */
+ close (info -> wfdesc);
+#endif
+ info -> wfdesc = -1;
+ if (!quiet_interface_discovery)
+ log_info ("Disabling output on LPF/%s/%s%s%s",
info -> name,
- print_hw_addr (info -> hw_address.htype,
- info -> hw_address.hlen,
- info -> hw_address.haddr),
+ print_hw_addr (info -> hw_address.hbuf [0],
+ info -> hw_address.hlen - 1,
+ &info -> hw_address.hbuf [1]),
(info -> shared_network ? "/" : ""),
(info -> shared_network ?
info -> shared_network -> name : ""));
@@ -152,11 +170,14 @@ void if_register_send (info)
in bpf includes... */
extern struct sock_filter dhcp_bpf_filter [];
extern int dhcp_bpf_filter_len;
+
+#if defined (HAVE_TR_SUPPORT)
extern struct sock_filter dhcp_bpf_tr_filter [];
extern int dhcp_bpf_tr_filter_len;
+static void lpf_tr_filter_setup (struct interface_info *);
+#endif
static void lpf_gen_filter_setup (struct interface_info *);
-static void lpf_tr_filter_setup (struct interface_info *);
void if_register_receive (info)
struct interface_info *info;
@@ -164,20 +185,40 @@ void if_register_receive (info)
/* Open a LPF device and hang it on this interface... */
info -> rfdesc = if_register_lpf (info);
- if (info -> hw_address.htype == HTYPE_IEEE802)
+#if defined (HAVE_TR_SUPPORT)
+ if (info -> hw_address.hbuf [0] == HTYPE_IEEE802)
lpf_tr_filter_setup (info);
else
+#endif
lpf_gen_filter_setup (info);
if (!quiet_interface_discovery)
- note ("Listening on LPF/%s/%s%s%s",
- info -> name,
- print_hw_addr (info -> hw_address.htype,
- info -> hw_address.hlen,
- info -> hw_address.haddr),
- (info -> shared_network ? "/" : ""),
- (info -> shared_network ?
- info -> shared_network -> name : ""));
+ log_info ("Listening on LPF/%s/%s%s%s",
+ info -> name,
+ print_hw_addr (info -> hw_address.hbuf [0],
+ info -> hw_address.hlen - 1,
+ &info -> hw_address.hbuf [1]),
+ (info -> shared_network ? "/" : ""),
+ (info -> shared_network ?
+ info -> shared_network -> name : ""));
+}
+
+void if_deregister_receive (info)
+ struct interface_info *info;
+{
+ /* for LPF this is simple, packet filters are removed when sockets
+ are closed */
+ close (info -> rfdesc);
+ info -> rfdesc = -1;
+ if (!quiet_interface_discovery)
+ log_info ("Disabling input on LPF/%s/%s%s%s",
+ info -> name,
+ print_hw_addr (info -> hw_address.hbuf [0],
+ info -> hw_address.hlen - 1,
+ &info -> hw_address.hbuf [1]),
+ (info -> shared_network ? "/" : ""),
+ (info -> shared_network ?
+ info -> shared_network -> name : ""));
}
static void lpf_gen_filter_setup (info)
@@ -193,20 +234,25 @@ static void lpf_gen_filter_setup (info)
/* Patch the server port into the LPF program...
XXX changes to filter program may require changes
to the insn number(s) used below! XXX */
- dhcp_bpf_filter [8].k = ntohs (local_port);
+ dhcp_bpf_filter [8].k = ntohs ((short)local_port);
if (setsockopt (info -> rfdesc, SOL_SOCKET, SO_ATTACH_FILTER, &p,
sizeof p) < 0) {
if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
- errno == EAFNOSUPPORT)
- error ("socket: %m - make sure %s %s!",
- "CONFIG_PACKET and CONFIG_FILTER are defined",
- "in your kernel configuration");
- error ("Can't install packet filter program: %m");
+ errno == EAFNOSUPPORT) {
+ log_error ("socket: %m - make sure");
+ log_error ("CONFIG_PACKET (Packet socket) %s",
+ "and CONFIG_FILTER");
+ log_error ("(Socket Filtering) are enabled %s",
+ "in your kernel");
+ log_fatal ("configuration!");
+ }
+ log_fatal ("Can't install packet filter program: %m");
}
}
+#if defined (HAVE_TR_SUPPORT)
static void lpf_tr_filter_setup (info)
struct interface_info *info;
{
@@ -228,13 +274,18 @@ static void lpf_tr_filter_setup (info)
sizeof p) < 0) {
if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
- errno == EAFNOSUPPORT)
- error ("socket: %m - make sure %s %s!",
- "CONFIG_PACKET and CONFIG_FILTER are defined",
- "in your kernel configuration");
- error ("Can't install packet filter program: %m");
+ errno == EAFNOSUPPORT) {
+ log_error ("socket: %m - make sure");
+ log_error ("CONFIG_PACKET (Packet socket) %s",
+ "and CONFIG_FILTER");
+ log_error ("(Socket Filtering) are enabled %s",
+ "in your kernel");
+ log_fatal ("configuration!");
+ }
+ log_fatal ("Can't install packet filter program: %m");
}
}
+#endif /* HAVE_TR_SUPPORT */
#endif /* USE_LPF_RECEIVE */
#ifdef USE_LPF_SEND
@@ -247,21 +298,27 @@ ssize_t send_packet (interface, packet, raw, len, from, to, hto)
struct sockaddr_in *to;
struct hardware *hto;
{
- int bufp = 0;
- unsigned char buf [1500];
+ unsigned hbufp = 0, ibufp = 0;
+ double hh [16];
+ double ih [1536 / sizeof (double)];
+ unsigned char *buf = (unsigned char *)ih;
struct sockaddr sa;
int result;
+ int fudge;
if (!strcmp (interface -> name, "fallback"))
return send_fallback (interface, packet, raw,
len, from, to, hto);
/* Assemble the headers... */
- assemble_hw_header (interface, buf, &bufp, hto);
- assemble_udp_ip_header (interface, buf, &bufp, from.s_addr,
+ assemble_hw_header (interface, (unsigned char *)hh, &hbufp, hto);
+ fudge = hbufp % 4; /* IP header must be word-aligned. */
+ memcpy (buf + fudge, (unsigned char *)hh, hbufp);
+ ibufp = hbufp + fudge;
+ assemble_udp_ip_header (interface, buf, &ibufp, from.s_addr,
to -> sin_addr.s_addr, to -> sin_port,
(unsigned char *)raw, len);
- memcpy (buf + bufp, raw, len);
+ memcpy (buf + ibufp, raw, len);
/* For some reason, SOCK_PACKET sockets can't be connected,
so we have to do a sentdo every time. */
@@ -270,10 +327,10 @@ ssize_t send_packet (interface, packet, raw, len, from, to, hto)
strncpy (sa.sa_data,
(const char *)interface -> ifp, sizeof sa.sa_data);
- result = sendto (interface -> wfdesc, buf, bufp + len, 0,
- &sa, sizeof sa);
+ result = sendto (interface -> wfdesc,
+ buf + fudge, ibufp + len - fudge, 0, &sa, sizeof sa);
if (result < 0)
- warn ("send_packet: %m");
+ log_error ("send_packet: %m");
return result;
}
#endif /* USE_LPF_SEND */
@@ -289,8 +346,8 @@ ssize_t receive_packet (interface, buf, len, from, hfrom)
int nread;
int length = 0;
int offset = 0;
- unsigned char ibuf [1500];
- int bufix = 0;
+ unsigned char ibuf [1536];
+ unsigned bufix = 0;
length = read (interface -> rfdesc, ibuf, sizeof ibuf);
if (length <= 0)
@@ -311,8 +368,8 @@ ssize_t receive_packet (interface, buf, len, from, hfrom)
length -= offset;
/* Decode the IP and UDP headers... */
- offset = decode_udp_ip_header (interface, ibuf, bufix,
- from, (unsigned char *)0, length);
+ offset = decode_udp_ip_header (interface, ibuf, bufix, from,
+ (unsigned char *)0, (unsigned)length);
/* If the IP or UDP checksum was bad, skip the packet... */
if (offset < 0)
@@ -326,7 +383,8 @@ ssize_t receive_packet (interface, buf, len, from, hfrom)
return length;
}
-int can_unicast_without_arp ()
+int can_unicast_without_arp (ip)
+ struct interface_info *ip;
{
return 1;
}
@@ -337,14 +395,25 @@ int can_receive_unicast_unconfigured (ip)
return 1;
}
+int supports_multiple_interfaces (ip)
+ struct interface_info *ip;
+{
+ return 1;
+}
+
void maybe_setup_fallback ()
{
- struct interface_info *fbi;
- fbi = setup_fallback ();
- if (fbi) {
+ isc_result_t status;
+ struct interface_info *fbi = (struct interface_info *)0;
+ if (setup_fallback (&fbi, MDL)) {
if_register_fallback (fbi);
- add_protocol ("fallback", fallback_interface -> wfdesc,
- fallback_discard, fallback_interface);
+ status = omapi_register_io_object ((omapi_object_t *)fbi,
+ 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));
+ interface_dereference (&fbi, MDL);
}
}
#endif