3 BSD socket interface code... */
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1995-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29 * To learn more about Internet Systems Consortium, see
30 * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
31 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32 * ``http://www.nominum.com''.
35 /* SO_BINDTODEVICE support added by Elliot Poger (poger@leland.stanford.edu).
36 * This sockopt allows a socket to be bound to a particular interface,
37 * thus enabling the use of DHCPD on a multihomed host.
38 * If SO_BINDTODEVICE is defined in your system header files, the use of
39 * this sockopt will be automatically enabled.
40 * I have implemented it under Linux; other systems should be doable also.
44 static char copyright
[] =
45 "$Id: socket.c,v 1.55.2.4 2004/06/10 17:59:21 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
50 #ifdef USE_SOCKET_FALLBACK
51 # if !defined (USE_SOCKET_SEND)
52 # define if_register_send if_register_fallback
53 # define send_packet send_fallback
54 # define if_reinitialize_send if_reinitialize_fallback
60 /* Reinitializes the specified interface after an address change. This
61 is not required for packet-filter APIs. */
63 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
64 void if_reinitialize_send (info
)
65 struct interface_info
*info
;
68 #ifndef USE_SOCKET_RECEIVE
70 close (info
-> wfdesc
);
72 if_register_send (info
);
77 #ifdef USE_SOCKET_RECEIVE
78 void if_reinitialize_receive (info
)
79 struct interface_info
*info
;
83 close (info
-> rfdesc
);
84 if_register_receive (info
);
89 #if defined (USE_SOCKET_SEND) || \
90 defined (USE_SOCKET_RECEIVE) || \
91 defined (USE_SOCKET_FALLBACK)
92 /* Generic interface registration routine... */
93 int if_register_socket (info
)
94 struct interface_info
*info
;
96 struct sockaddr_in name
;
100 #if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
101 /* Make sure only one interface is registered. */
103 log_fatal ("The standard socket API can only support %s",
104 "hosts with a single network interface.");
108 memset (&name
, 0, sizeof (name
));
109 /* Set up the address we're going to bind to. */
110 name
.sin_family
= AF_INET
;
111 name
.sin_port
= local_port
;
112 name
.sin_addr
= local_address
;
114 /* Make a socket... */
115 if ((sock
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
116 log_fatal ("Can't create dhcp socket: %m");
118 /* Set the REUSEADDR option so that we don't fail to start if
119 we're being restarted. */
121 if (setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
,
122 (char *)&flag
, sizeof flag
) < 0)
123 log_fatal ("Can't set SO_REUSEADDR option on dhcp socket: %m");
125 /* Set the BROADCAST option so that we can broadcast DHCP responses.
126 We shouldn't do this for fallback devices, and we can detect that
127 a device is a fallback because it has no ifp structure. */
129 (setsockopt (sock
, SOL_SOCKET
, SO_BROADCAST
,
130 (char *)&flag
, sizeof flag
) < 0))
131 log_fatal ("Can't set SO_BROADCAST option on dhcp socket: %m");
133 /* Bind the socket to this interface's IP address. */
134 if (bind (sock
, (struct sockaddr
*)&name
, sizeof name
) < 0) {
135 log_error ("Can't bind to dhcp address: %m");
136 log_error ("Please make sure there is no other dhcp server");
137 log_error ("running and that there's no entry for dhcp or");
138 log_error ("bootp in /etc/inetd.conf. Also make sure you");
139 log_error ("are not running HP JetAdmin software, which");
140 log_fatal ("includes a bootp server.");
143 #if defined (HAVE_SO_BINDTODEVICE)
144 /* Bind this socket to this interface. */
146 setsockopt (sock
, SOL_SOCKET
, SO_BINDTODEVICE
,
147 (char *)(info
-> ifp
), sizeof *(info
-> ifp
)) < 0) {
148 log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
154 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
156 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
157 void if_register_send (info
)
158 struct interface_info
*info
;
160 #ifndef USE_SOCKET_RECEIVE
161 info
-> wfdesc
= if_register_socket (info
);
162 #if defined (USE_SOCKET_FALLBACK)
163 /* Fallback only registers for send, but may need to receive as
165 info
-> rfdesc
= info
-> wfdesc
;
168 info
-> wfdesc
= info
-> rfdesc
;
170 if (!quiet_interface_discovery
)
171 log_info ("Sending on Socket/%s%s%s",
173 (info
-> shared_network
? "/" : ""),
174 (info
-> shared_network
?
175 info
-> shared_network
-> name
: ""));
178 #if defined (USE_SOCKET_SEND)
179 void if_deregister_send (info
)
180 struct interface_info
*info
;
182 #ifndef USE_SOCKET_RECEIVE
183 close (info
-> wfdesc
);
187 if (!quiet_interface_discovery
)
188 log_info ("Disabling output on Socket/%s%s%s",
190 (info
-> shared_network
? "/" : ""),
191 (info
-> shared_network
?
192 info
-> shared_network
-> name
: ""));
194 #endif /* USE_SOCKET_SEND */
195 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
197 #ifdef USE_SOCKET_RECEIVE
198 void if_register_receive (info
)
199 struct interface_info
*info
;
201 /* If we're using the socket API for sending and receiving,
202 we don't need to register this interface twice. */
203 info
-> rfdesc
= if_register_socket (info
);
204 if (!quiet_interface_discovery
)
205 log_info ("Listening on Socket/%s%s%s",
207 (info
-> shared_network
? "/" : ""),
208 (info
-> shared_network
?
209 info
-> shared_network
-> name
: ""));
212 void if_deregister_receive (info
)
213 struct interface_info
*info
;
215 close (info
-> rfdesc
);
218 if (!quiet_interface_discovery
)
219 log_info ("Disabling input on Socket/%s%s%s",
221 (info
-> shared_network
? "/" : ""),
222 (info
-> shared_network
?
223 info
-> shared_network
-> name
: ""));
225 #endif /* USE_SOCKET_RECEIVE */
227 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
228 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
229 struct interface_info
*interface
;
230 struct packet
*packet
;
231 struct dhcp_packet
*raw
;
234 struct sockaddr_in
*to
;
235 struct hardware
*hto
;
238 #ifdef IGNORE_HOSTUNREACH
242 result
= sendto (interface
-> wfdesc
, (char *)raw
, len
, 0,
243 (struct sockaddr
*)to
, sizeof *to
);
244 #ifdef IGNORE_HOSTUNREACH
245 } while (to
-> sin_addr
.s_addr
== htonl (INADDR_BROADCAST
) &&
247 (errno
== EHOSTUNREACH
||
248 errno
== ECONNREFUSED
) &&
252 log_error ("send_packet: %m");
253 if (errno
== ENETUNREACH
)
254 log_error ("send_packet: please consult README file%s",
255 " regarding broadcast address.");
259 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
261 #ifdef USE_SOCKET_RECEIVE
262 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
263 struct interface_info
*interface
;
266 struct sockaddr_in
*from
;
267 struct hardware
*hfrom
;
269 SOCKLEN_T flen
= sizeof *from
;
272 #ifdef IGNORE_HOSTUNREACH
276 result
= recvfrom (interface
-> rfdesc
, (char *)buf
, len
, 0,
277 (struct sockaddr
*)from
, &flen
);
278 #ifdef IGNORE_HOSTUNREACH
279 } while (result
< 0 &&
280 (errno
== EHOSTUNREACH
||
281 errno
== ECONNREFUSED
) &&
286 #endif /* USE_SOCKET_RECEIVE */
288 #if defined (USE_SOCKET_FALLBACK)
289 /* This just reads in a packet and silently discards it. */
291 isc_result_t
fallback_discard (object
)
292 omapi_object_t
*object
;
295 struct sockaddr_in from
;
296 SOCKLEN_T flen
= sizeof from
;
298 struct interface_info
*interface
;
300 if (object
-> type
!= dhcp_type_interface
)
301 return ISC_R_INVALIDARG
;
302 interface
= (struct interface_info
*)object
;
304 status
= recvfrom (interface
-> wfdesc
, buf
, sizeof buf
, 0,
305 (struct sockaddr
*)&from
, &flen
);
307 /* Only report fallback discard errors if we're debugging. */
309 log_error ("fallback_discard: %m");
310 return ISC_R_UNEXPECTED
;
313 return ISC_R_SUCCESS
;
315 #endif /* USE_SOCKET_FALLBACK */
317 #if defined (USE_SOCKET_SEND)
318 int can_unicast_without_arp (ip
)
319 struct interface_info
*ip
;
324 int can_receive_unicast_unconfigured (ip
)
325 struct interface_info
*ip
;
327 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
334 int supports_multiple_interfaces (ip
)
335 struct interface_info
*ip
;
337 #if defined (SO_BINDTODEVICE)
344 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
347 void maybe_setup_fallback ()
349 #if defined (USE_SOCKET_FALLBACK)
351 struct interface_info
*fbi
= (struct interface_info
*)0;
352 if (setup_fallback (&fbi
, MDL
)) {
353 fbi
-> wfdesc
= if_register_socket (fbi
);
354 fbi
-> rfdesc
= fbi
-> wfdesc
;
355 log_info ("Sending on Socket/%s%s%s",
357 (fbi
-> shared_network
? "/" : ""),
358 (fbi
-> shared_network
?
359 fbi
-> shared_network
-> name
: ""));
361 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
363 fallback_discard
, 0, 0);
364 if (status
!= ISC_R_SUCCESS
)
365 log_fatal ("Can't register I/O handle for %s: %s",
366 fbi
-> name
, isc_result_totext (status
));
367 interface_dereference (&fbi
, MDL
);
371 #endif /* USE_SOCKET_SEND */