Make setthetime() static per the prototype.
[dragonfly.git] / contrib / isc-dhcp / common / socket.c
1 /* socket.c
2
3    BSD socket interface code... */
4
5 /*
6  * Copyright (c) 1995-2002 Internet Software Consortium.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
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.
21  *
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
34  * SUCH DAMAGE.
35  *
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''.
42  */
43
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.
50  */
51
52 #ifndef lint
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";
55 #endif /* not lint */
56
57 #include "dhcpd.h"
58
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
64 # endif
65 #endif
66
67 static int once = 0;
68
69 /* Reinitializes the specified interface after an address change.   This
70    is not required for packet-filter APIs. */
71
72 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
73 void if_reinitialize_send (info)
74         struct interface_info *info;
75 {
76 #if 0
77 #ifndef USE_SOCKET_RECEIVE
78         once = 0;
79         close (info -> wfdesc);
80 #endif
81         if_register_send (info);
82 #endif
83 }
84 #endif
85
86 #ifdef USE_SOCKET_RECEIVE
87 void if_reinitialize_receive (info)
88         struct interface_info *info;
89 {
90 #if 0
91         once = 0;
92         close (info -> rfdesc);
93         if_register_receive (info);
94 #endif
95 }
96 #endif
97
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;
104 {
105         struct sockaddr_in name;
106         int sock;
107         int flag;
108
109 #if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
110         /* Make sure only one interface is registered. */
111         if (once)
112                 log_fatal ("The standard socket API can only support %s",
113                        "hosts with a single network interface.");
114         once = 1;
115 #endif
116
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;
122
123         /* Make a socket... */
124         if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
125                 log_fatal ("Can't create dhcp socket: %m");
126
127         /* Set the REUSEADDR option so that we don't fail to start if
128            we're being restarted. */
129         flag = 1;
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");
133
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. */
137         if (info -> ifp &&
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");
141
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.");
150         }
151
152 #if defined (HAVE_SO_BINDTODEVICE)
153         /* Bind this socket to this interface. */
154         if (info -> ifp &&
155             setsockopt (sock, SOL_SOCKET, SO_BINDTODEVICE,
156                         (char *)(info -> ifp), sizeof *(info -> ifp)) < 0) {
157                 log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
158         }
159 #endif
160
161         return sock;
162 }
163 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
164
165 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
166 void if_register_send (info)
167         struct interface_info *info;
168 {
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
173            well. */
174         info -> rfdesc = info -> wfdesc;
175 #endif
176 #else
177         info -> wfdesc = info -> rfdesc;
178 #endif
179         if (!quiet_interface_discovery)
180                 log_info ("Sending on   Socket/%s%s%s",
181                       info -> name,
182                       (info -> shared_network ? "/" : ""),
183                       (info -> shared_network ?
184                        info -> shared_network -> name : ""));
185 }
186
187 #if defined (USE_SOCKET_SEND)
188 void if_deregister_send (info)
189         struct interface_info *info;
190 {
191 #ifndef USE_SOCKET_RECEIVE
192         close (info -> wfdesc);
193 #endif
194         info -> wfdesc = -1;
195
196         if (!quiet_interface_discovery)
197                 log_info ("Disabling output on Socket/%s%s%s",
198                       info -> name,
199                       (info -> shared_network ? "/" : ""),
200                       (info -> shared_network ?
201                        info -> shared_network -> name : ""));
202 }
203 #endif /* USE_SOCKET_SEND */
204 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
205
206 #ifdef USE_SOCKET_RECEIVE
207 void if_register_receive (info)
208         struct interface_info *info;
209 {
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",
215                       info -> name,
216                       (info -> shared_network ? "/" : ""),
217                       (info -> shared_network ?
218                        info -> shared_network -> name : ""));
219 }
220
221 void if_deregister_receive (info)
222         struct interface_info *info;
223 {
224         close (info -> rfdesc);
225         info -> rfdesc = -1;
226
227         if (!quiet_interface_discovery)
228                 log_info ("Disabling input on Socket/%s%s%s",
229                       info -> name,
230                       (info -> shared_network ? "/" : ""),
231                       (info -> shared_network ?
232                        info -> shared_network -> name : ""));
233 }
234 #endif /* USE_SOCKET_RECEIVE */
235
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;
241         size_t len;
242         struct in_addr from;
243         struct sockaddr_in *to;
244         struct hardware *hto;
245 {
246         int result;
247 #ifdef IGNORE_HOSTUNREACH
248         int retry = 0;
249         do {
250 #endif
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) &&
255                  result < 0 &&
256                  (errno == EHOSTUNREACH ||
257                   errno == ECONNREFUSED) &&
258                  retry++ < 10);
259 #endif
260         if (result < 0) {
261                 log_error ("send_packet: %m");
262                 if (errno == ENETUNREACH)
263                         log_error ("send_packet: please consult README file%s",
264                                    " regarding broadcast address.");
265         }
266         return result;
267 }
268 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
269
270 #ifdef USE_SOCKET_RECEIVE
271 ssize_t receive_packet (interface, buf, len, from, hfrom)
272         struct interface_info *interface;
273         unsigned char *buf;
274         size_t len;
275         struct sockaddr_in *from;
276         struct hardware *hfrom;
277 {
278         SOCKLEN_T flen = sizeof *from;
279         int result;
280
281 #ifdef IGNORE_HOSTUNREACH
282         int retry = 0;
283         do {
284 #endif
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) &&
291                  retry++ < 10);
292 #endif
293         return result;
294 }
295 #endif /* USE_SOCKET_RECEIVE */
296
297 #if defined (USE_SOCKET_FALLBACK)
298 /* This just reads in a packet and silently discards it. */
299
300 isc_result_t fallback_discard (object)
301         omapi_object_t *object;
302 {
303         char buf [1540];
304         struct sockaddr_in from;
305         SOCKLEN_T flen = sizeof from;
306         int status;
307         struct interface_info *interface;
308
309         if (object -> type != dhcp_type_interface)
310                 return ISC_R_INVALIDARG;
311         interface = (struct interface_info *)object;
312
313         status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0,
314                            (struct sockaddr *)&from, &flen);
315 #if defined (DEBUG)
316         /* Only report fallback discard errors if we're debugging. */
317         if (status < 0) {
318                 log_error ("fallback_discard: %m");
319                 return ISC_R_UNEXPECTED;
320         }
321 #endif
322         return ISC_R_SUCCESS;
323 }
324 #endif /* USE_SOCKET_FALLBACK */
325
326 #if defined (USE_SOCKET_SEND)
327 int can_unicast_without_arp (ip)
328         struct interface_info *ip;
329 {
330         return 0;
331 }
332
333 int can_receive_unicast_unconfigured (ip)
334         struct interface_info *ip;
335 {
336 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
337         return 1;
338 #else
339         return 0;
340 #endif
341 }
342
343 int supports_multiple_interfaces (ip)
344         struct interface_info *ip;
345 {
346 #if defined (SO_BINDTODEVICE)
347         return 1;
348 #else
349         return 0;
350 #endif
351 }
352
353 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
354    do not. */
355
356 void maybe_setup_fallback ()
357 {
358 #if defined (USE_SOCKET_FALLBACK)
359         isc_result_t status;
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",
365                       fbi -> name,
366                       (fbi -> shared_network ? "/" : ""),
367                       (fbi -> shared_network ?
368                        fbi -> shared_network -> name : ""));
369         
370                 status = omapi_register_io_object ((omapi_object_t *)fbi,
371                                                    if_readsocket, 0,
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);
377         }
378 #endif
379 }
380 #endif /* USE_SOCKET_SEND */