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$ 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 /* ***CHECK HERE*** */
70 #ifdef SOCKET_IS_NOT_A_FILE
71 CloseSocket (info
-> wfdesc
);
73 close (info
-> wfdesc
);
76 if_register_send (info
);
81 #ifdef USE_SOCKET_RECEIVE
82 void if_reinitialize_receive (info
)
83 struct interface_info
*info
;
87 #ifdef SOCKET_IS_NOT_A_FILE
88 CloseSocket (info
-> rfdesc
);
90 close (info
-> rfdesc
);
92 if_register_receive (info
);
97 #if defined (USE_SOCKET_SEND) || \
98 defined (USE_SOCKET_RECEIVE) || \
99 defined (USE_SOCKET_FALLBACK)
100 /* Generic interface registration routine... */
101 int if_register_socket (info
)
102 struct interface_info
*info
;
104 struct sockaddr_in name
;
108 #if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
109 /* Make sure only one interface is registered. */
111 log_fatal ("The standard socket API can only support %s",
112 "hosts with a single network interface.");
116 memset (&name
, 0, sizeof (name
));
117 /* Set up the address we're going to bind to. */
118 name
.sin_family
= AF_INET
;
119 name
.sin_port
= local_port
;
120 name
.sin_addr
= local_address
;
122 /* Make a socket... */
123 if ((sock
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
124 log_fatal ("Can't create dhcp socket: %m");
126 /* Set the REUSEADDR option so that we don't fail to start if
127 we're being restarted. */
129 if (setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
,
130 (char *)&flag
, sizeof flag
) < 0)
131 log_fatal ("Can't set SO_REUSEADDR option on dhcp socket: %m");
133 /* Set the BROADCAST option so that we can broadcast DHCP responses.
134 We shouldn't do this for fallback devices, and we can detect that
135 a device is a fallback because it has no ifp structure. */
137 (setsockopt (sock
, SOL_SOCKET
, SO_BROADCAST
,
138 (char *)&flag
, sizeof flag
) < 0))
139 log_fatal ("Can't set SO_BROADCAST option on dhcp socket: %m");
141 /* Bind the socket to this interface's IP address. */
142 if (bind (sock
, (struct sockaddr
*)&name
, sizeof name
) < 0) {
143 log_error ("Can't bind to dhcp address: %m");
144 log_error ("Please make sure there is no other dhcp server");
145 log_error ("running and that there's no entry for dhcp or");
146 log_error ("bootp in /etc/inetd.conf. Also make sure you");
147 log_error ("are not running HP JetAdmin software, which");
148 log_fatal ("includes a bootp server.");
151 #if defined (HAVE_SO_BINDTODEVICE)
152 /* Bind this socket to this interface. */
154 setsockopt (sock
, SOL_SOCKET
, SO_BINDTODEVICE
,
155 (char *)(info
-> ifp
), sizeof *(info
-> ifp
)) < 0) {
156 log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
162 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
164 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
165 void if_register_send (info
)
166 struct interface_info
*info
;
168 #ifndef USE_SOCKET_RECEIVE
169 info
-> wfdesc
= if_register_socket (info
);
170 #if defined (USE_SOCKET_FALLBACK)
171 /* Fallback only registers for send, but may need to receive as
173 info
-> rfdesc
= info
-> wfdesc
;
176 info
-> wfdesc
= info
-> rfdesc
;
178 if (!quiet_interface_discovery
)
179 log_info ("Sending on Socket/%s%s%s",
181 (info
-> shared_network
? "/" : ""),
182 (info
-> shared_network
?
183 info
-> shared_network
-> name
: ""));
186 #if defined (USE_SOCKET_SEND)
187 void if_deregister_send (info
)
188 struct interface_info
*info
;
190 #ifndef USE_SOCKET_RECEIVE
191 #ifdef SOCKET_IS_NOT_A_FILE /* ***CHECK HERE*** */
192 CloseSocket(info
-> wfdesc
);
194 close (info
-> wfdesc
);
199 if (!quiet_interface_discovery
)
200 log_info ("Disabling output on Socket/%s%s%s",
202 (info
-> shared_network
? "/" : ""),
203 (info
-> shared_network
?
204 info
-> shared_network
-> name
: ""));
206 #endif /* USE_SOCKET_SEND */
207 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
209 #ifdef USE_SOCKET_RECEIVE
210 void if_register_receive (info
)
211 struct interface_info
*info
;
213 /* If we're using the socket API for sending and receiving,
214 we don't need to register this interface twice. */
215 info
-> rfdesc
= if_register_socket (info
);
216 if (!quiet_interface_discovery
)
217 log_info ("Listening on Socket/%s%s%s",
219 (info
-> shared_network
? "/" : ""),
220 (info
-> shared_network
?
221 info
-> shared_network
-> name
: ""));
224 void if_deregister_receive (info
)
225 struct interface_info
*info
;
227 #ifdef SOCKET_IS_NOT_A_FILE
228 CloseSocket (info
-> rfdesc
);
230 close (info
-> rfdesc
);
234 if (!quiet_interface_discovery
)
235 log_info ("Disabling input on Socket/%s%s%s",
237 (info
-> shared_network
? "/" : ""),
238 (info
-> shared_network
?
239 info
-> shared_network
-> name
: ""));
241 #endif /* USE_SOCKET_RECEIVE */
243 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
244 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
245 struct interface_info
*interface
;
246 struct packet
*packet
;
247 struct dhcp_packet
*raw
;
250 struct sockaddr_in
*to
;
251 struct hardware
*hto
;
254 #ifdef IGNORE_HOSTUNREACH
258 result
= sendto (interface
-> wfdesc
, (char *)raw
, len
, 0,
259 (struct sockaddr
*)to
, sizeof *to
);
260 #ifdef IGNORE_HOSTUNREACH
261 } while (to
-> sin_addr
.s_addr
== htonl (INADDR_BROADCAST
) &&
263 (errno
== EHOSTUNREACH
||
264 errno
== ECONNREFUSED
) &&
268 log_error ("send_packet: %m");
269 if (errno
== ENETUNREACH
)
270 log_error ("send_packet: please consult README file%s",
271 " regarding broadcast address.");
275 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
277 #ifdef USE_SOCKET_RECEIVE
278 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
279 struct interface_info
*interface
;
282 struct sockaddr_in
*from
;
283 struct hardware
*hfrom
;
285 SOCKLEN_T flen
= sizeof *from
;
288 #ifdef IGNORE_HOSTUNREACH
292 result
= recvfrom (interface
-> rfdesc
, (char *)buf
, len
, 0,
293 (struct sockaddr
*)from
, &flen
);
294 #ifdef IGNORE_HOSTUNREACH
295 } while (result
< 0 &&
296 (errno
== EHOSTUNREACH
||
297 errno
== ECONNREFUSED
) &&
302 #endif /* USE_SOCKET_RECEIVE */
304 #if defined (USE_SOCKET_FALLBACK)
305 /* This just reads in a packet and silently discards it. */
307 isc_result_t
fallback_discard (object
)
308 omapi_object_t
*object
;
311 struct sockaddr_in from
;
312 SOCKLEN_T flen
= sizeof from
;
314 struct interface_info
*interface
;
316 if (object
-> type
!= dhcp_type_interface
)
317 return ISC_R_INVALIDARG
;
318 interface
= (struct interface_info
*)object
;
320 status
= recvfrom (interface
-> wfdesc
, buf
, sizeof buf
, 0,
321 (struct sockaddr
*)&from
, &flen
);
323 /* Only report fallback discard errors if we're debugging. */
325 log_error ("fallback_discard: %m");
326 return ISC_R_UNEXPECTED
;
329 return ISC_R_SUCCESS
;
331 #endif /* USE_SOCKET_FALLBACK */
333 #if defined (USE_SOCKET_SEND)
334 int can_unicast_without_arp (ip
)
335 struct interface_info
*ip
;
340 int can_receive_unicast_unconfigured (ip
)
341 struct interface_info
*ip
;
343 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
350 int supports_multiple_interfaces (ip
)
351 struct interface_info
*ip
;
353 #if defined (SO_BINDTODEVICE)
360 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
363 void maybe_setup_fallback ()
365 #if defined (USE_SOCKET_FALLBACK)
367 struct interface_info
*fbi
= (struct interface_info
*)0;
368 if (setup_fallback (&fbi
, MDL
)) {
369 fbi
-> wfdesc
= if_register_socket (fbi
);
370 fbi
-> rfdesc
= fbi
-> wfdesc
;
371 log_info ("Sending on Socket/%s%s%s",
373 (fbi
-> shared_network
? "/" : ""),
374 (fbi
-> shared_network
?
375 fbi
-> shared_network
-> name
: ""));
377 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
379 fallback_discard
, 0, 0);
380 if (status
!= ISC_R_SUCCESS
)
381 log_fatal ("Can't register I/O handle for %s: %s",
382 fbi
-> name
, isc_result_totext (status
));
383 interface_dereference (&fbi
, MDL
);
387 #endif /* USE_SOCKET_SEND */