3 BSD socket interface code... */
6 * Copyright (c) 1995-2002 Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38 * To learn more about the Internet Software Consortium, see
39 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
40 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
41 * ``http://www.nominum.com''.
44 /* SO_BINDTODEVICE support added by Elliot Poger (poger@leland.stanford.edu).
45 * This sockopt allows a socket to be bound to a particular interface,
46 * thus enabling the use of DHCPD on a multihomed host.
47 * If SO_BINDTODEVICE is defined in your system header files, the use of
48 * this sockopt will be automatically enabled.
49 * I have implemented it under Linux; other systems should be doable also.
53 static char copyright[] =
54 "$Id: socket.c,v 1.55.2.3 2002/11/17 02:26:59 dhankins Exp $ Copyright (c) 1995-2002 The Internet Software Consortium. All rights reserved.\n";
59 #ifdef USE_SOCKET_FALLBACK
60 # if !defined (USE_SOCKET_SEND)
61 # define if_register_send if_register_fallback
62 # define send_packet send_fallback
63 # define if_reinitialize_send if_reinitialize_fallback
69 /* Reinitializes the specified interface after an address change. This
70 is not required for packet-filter APIs. */
72 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
73 void if_reinitialize_send (info)
74 struct interface_info *info;
77 #ifndef USE_SOCKET_RECEIVE
79 close (info -> wfdesc);
81 if_register_send (info);
86 #ifdef USE_SOCKET_RECEIVE
87 void if_reinitialize_receive (info)
88 struct interface_info *info;
92 close (info -> rfdesc);
93 if_register_receive (info);
98 #if defined (USE_SOCKET_SEND) || \
99 defined (USE_SOCKET_RECEIVE) || \
100 defined (USE_SOCKET_FALLBACK)
101 /* Generic interface registration routine... */
102 int if_register_socket (info)
103 struct interface_info *info;
105 struct sockaddr_in name;
109 #if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
110 /* Make sure only one interface is registered. */
112 log_fatal ("The standard socket API can only support %s",
113 "hosts with a single network interface.");
117 memset (&name, 0, sizeof (name));
118 /* Set up the address we're going to bind to. */
119 name.sin_family = AF_INET;
120 name.sin_port = local_port;
121 name.sin_addr = local_address;
123 /* Make a socket... */
124 if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
125 log_fatal ("Can't create dhcp socket: %m");
127 /* Set the REUSEADDR option so that we don't fail to start if
128 we're being restarted. */
130 if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
131 (char *)&flag, sizeof flag) < 0)
132 log_fatal ("Can't set SO_REUSEADDR option on dhcp socket: %m");
134 /* Set the BROADCAST option so that we can broadcast DHCP responses.
135 We shouldn't do this for fallback devices, and we can detect that
136 a device is a fallback because it has no ifp structure. */
138 (setsockopt (sock, SOL_SOCKET, SO_BROADCAST,
139 (char *)&flag, sizeof flag) < 0))
140 log_fatal ("Can't set SO_BROADCAST option on dhcp socket: %m");
142 /* Bind the socket to this interface's IP address. */
143 if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0) {
144 log_error ("Can't bind to dhcp address: %m");
145 log_error ("Please make sure there is no other dhcp server");
146 log_error ("running and that there's no entry for dhcp or");
147 log_error ("bootp in /etc/inetd.conf. Also make sure you");
148 log_error ("are not running HP JetAdmin software, which");
149 log_fatal ("includes a bootp server.");
152 #if defined (HAVE_SO_BINDTODEVICE)
153 /* Bind this socket to this interface. */
155 setsockopt (sock, SOL_SOCKET, SO_BINDTODEVICE,
156 (char *)(info -> ifp), sizeof *(info -> ifp)) < 0) {
157 log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
163 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
165 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
166 void if_register_send (info)
167 struct interface_info *info;
169 #ifndef USE_SOCKET_RECEIVE
170 info -> wfdesc = if_register_socket (info);
171 #if defined (USE_SOCKET_FALLBACK)
172 /* Fallback only registers for send, but may need to receive as
174 info -> rfdesc = info -> wfdesc;
177 info -> wfdesc = info -> rfdesc;
179 if (!quiet_interface_discovery)
180 log_info ("Sending on Socket/%s%s%s",
182 (info -> shared_network ? "/" : ""),
183 (info -> shared_network ?
184 info -> shared_network -> name : ""));
187 #if defined (USE_SOCKET_SEND)
188 void if_deregister_send (info)
189 struct interface_info *info;
191 #ifndef USE_SOCKET_RECEIVE
192 close (info -> wfdesc);
196 if (!quiet_interface_discovery)
197 log_info ("Disabling output on Socket/%s%s%s",
199 (info -> shared_network ? "/" : ""),
200 (info -> shared_network ?
201 info -> shared_network -> name : ""));
203 #endif /* USE_SOCKET_SEND */
204 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
206 #ifdef USE_SOCKET_RECEIVE
207 void if_register_receive (info)
208 struct interface_info *info;
210 /* If we're using the socket API for sending and receiving,
211 we don't need to register this interface twice. */
212 info -> rfdesc = if_register_socket (info);
213 if (!quiet_interface_discovery)
214 log_info ("Listening on Socket/%s%s%s",
216 (info -> shared_network ? "/" : ""),
217 (info -> shared_network ?
218 info -> shared_network -> name : ""));
221 void if_deregister_receive (info)
222 struct interface_info *info;
224 close (info -> rfdesc);
227 if (!quiet_interface_discovery)
228 log_info ("Disabling input on Socket/%s%s%s",
230 (info -> shared_network ? "/" : ""),
231 (info -> shared_network ?
232 info -> shared_network -> name : ""));
234 #endif /* USE_SOCKET_RECEIVE */
236 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
237 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
238 struct interface_info *interface;
239 struct packet *packet;
240 struct dhcp_packet *raw;
243 struct sockaddr_in *to;
244 struct hardware *hto;
247 #ifdef IGNORE_HOSTUNREACH
251 result = sendto (interface -> wfdesc, (char *)raw, len, 0,
252 (struct sockaddr *)to, sizeof *to);
253 #ifdef IGNORE_HOSTUNREACH
254 } while (to -> sin_addr.s_addr == htonl (INADDR_BROADCAST) &&
256 (errno == EHOSTUNREACH ||
257 errno == ECONNREFUSED) &&
261 log_error ("send_packet: %m");
262 if (errno == ENETUNREACH)
263 log_error ("send_packet: please consult README file%s",
264 " regarding broadcast address.");
268 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
270 #ifdef USE_SOCKET_RECEIVE
271 ssize_t receive_packet (interface, buf, len, from, hfrom)
272 struct interface_info *interface;
275 struct sockaddr_in *from;
276 struct hardware *hfrom;
278 SOCKLEN_T flen = sizeof *from;
281 #ifdef IGNORE_HOSTUNREACH
285 result = recvfrom (interface -> rfdesc, (char *)buf, len, 0,
286 (struct sockaddr *)from, &flen);
287 #ifdef IGNORE_HOSTUNREACH
288 } while (result < 0 &&
289 (errno == EHOSTUNREACH ||
290 errno == ECONNREFUSED) &&
295 #endif /* USE_SOCKET_RECEIVE */
297 #if defined (USE_SOCKET_FALLBACK)
298 /* This just reads in a packet and silently discards it. */
300 isc_result_t fallback_discard (object)
301 omapi_object_t *object;
304 struct sockaddr_in from;
305 SOCKLEN_T flen = sizeof from;
307 struct interface_info *interface;
309 if (object -> type != dhcp_type_interface)
310 return ISC_R_INVALIDARG;
311 interface = (struct interface_info *)object;
313 status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0,
314 (struct sockaddr *)&from, &flen);
316 /* Only report fallback discard errors if we're debugging. */
318 log_error ("fallback_discard: %m");
319 return ISC_R_UNEXPECTED;
322 return ISC_R_SUCCESS;
324 #endif /* USE_SOCKET_FALLBACK */
326 #if defined (USE_SOCKET_SEND)
327 int can_unicast_without_arp (ip)
328 struct interface_info *ip;
333 int can_receive_unicast_unconfigured (ip)
334 struct interface_info *ip;
336 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
343 int supports_multiple_interfaces (ip)
344 struct interface_info *ip;
346 #if defined (SO_BINDTODEVICE)
353 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
356 void maybe_setup_fallback ()
358 #if defined (USE_SOCKET_FALLBACK)
360 struct interface_info *fbi = (struct interface_info *)0;
361 if (setup_fallback (&fbi, MDL)) {
362 fbi -> wfdesc = if_register_socket (fbi);
363 fbi -> rfdesc = fbi -> wfdesc;
364 log_info ("Sending on Socket/%s%s%s",
366 (fbi -> shared_network ? "/" : ""),
367 (fbi -> shared_network ?
368 fbi -> shared_network -> name : ""));
370 status = omapi_register_io_object ((omapi_object_t *)fbi,
372 fallback_discard, 0, 0);
373 if (status != ISC_R_SUCCESS)
374 log_fatal ("Can't register I/O handle for %s: %s",
375 fbi -> name, isc_result_totext (status));
376 interface_dereference (&fbi, MDL);
380 #endif /* USE_SOCKET_SEND */