Flesh out BUF_CMD_FLUSH support.
[dragonfly.git] / contrib / dhcp-3.0 / common / socket.c
1 /* socket.c
2
3    BSD socket interface code... */
4
5 /*
6  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 1995-2003 by Internet Software Consortium
8  *
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.
12  *
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.
20  *
21  *   Internet Systems Consortium, Inc.
22  *   950 Charter Street
23  *   Redwood City, CA 94063
24  *   <info@isc.org>
25  *   http://www.isc.org/
26  *
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''.
33  */
34
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.
41  */
42
43 #ifndef lint
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";
46 #endif /* not lint */
47
48 #include "dhcpd.h"
49
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
55 # endif
56 #endif
57
58 static int once = 0;
59
60 /* Reinitializes the specified interface after an address change.   This
61    is not required for packet-filter APIs. */
62
63 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
64 void if_reinitialize_send (info)
65         struct interface_info *info;
66 {
67 #if 0
68 #ifndef USE_SOCKET_RECEIVE
69         once = 0;
70         close (info -> wfdesc);
71 #endif
72         if_register_send (info);
73 #endif
74 }
75 #endif
76
77 #ifdef USE_SOCKET_RECEIVE
78 void if_reinitialize_receive (info)
79         struct interface_info *info;
80 {
81 #if 0
82         once = 0;
83         close (info -> rfdesc);
84         if_register_receive (info);
85 #endif
86 }
87 #endif
88
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;
95 {
96         struct sockaddr_in name;
97         int sock;
98         int flag;
99
100 #if !defined (HAVE_SO_BINDTODEVICE) && !defined (USE_FALLBACK)
101         /* Make sure only one interface is registered. */
102         if (once)
103                 log_fatal ("The standard socket API can only support %s",
104                        "hosts with a single network interface.");
105         once = 1;
106 #endif
107
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;
113
114         /* Make a socket... */
115         if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
116                 log_fatal ("Can't create dhcp socket: %m");
117
118         /* Set the REUSEADDR option so that we don't fail to start if
119            we're being restarted. */
120         flag = 1;
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");
124
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. */
128         if (info -> ifp &&
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");
132
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.");
141         }
142
143 #if defined (HAVE_SO_BINDTODEVICE)
144         /* Bind this socket to this interface. */
145         if (info -> ifp &&
146             setsockopt (sock, SOL_SOCKET, SO_BINDTODEVICE,
147                         (char *)(info -> ifp), sizeof *(info -> ifp)) < 0) {
148                 log_fatal ("setsockopt: SO_BINDTODEVICE: %m");
149         }
150 #endif
151
152         return sock;
153 }
154 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
155
156 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
157 void if_register_send (info)
158         struct interface_info *info;
159 {
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
164            well. */
165         info -> rfdesc = info -> wfdesc;
166 #endif
167 #else
168         info -> wfdesc = info -> rfdesc;
169 #endif
170         if (!quiet_interface_discovery)
171                 log_info ("Sending on   Socket/%s%s%s",
172                       info -> name,
173                       (info -> shared_network ? "/" : ""),
174                       (info -> shared_network ?
175                        info -> shared_network -> name : ""));
176 }
177
178 #if defined (USE_SOCKET_SEND)
179 void if_deregister_send (info)
180         struct interface_info *info;
181 {
182 #ifndef USE_SOCKET_RECEIVE
183         close (info -> wfdesc);
184 #endif
185         info -> wfdesc = -1;
186
187         if (!quiet_interface_discovery)
188                 log_info ("Disabling output on Socket/%s%s%s",
189                       info -> name,
190                       (info -> shared_network ? "/" : ""),
191                       (info -> shared_network ?
192                        info -> shared_network -> name : ""));
193 }
194 #endif /* USE_SOCKET_SEND */
195 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
196
197 #ifdef USE_SOCKET_RECEIVE
198 void if_register_receive (info)
199         struct interface_info *info;
200 {
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",
206                       info -> name,
207                       (info -> shared_network ? "/" : ""),
208                       (info -> shared_network ?
209                        info -> shared_network -> name : ""));
210 }
211
212 void if_deregister_receive (info)
213         struct interface_info *info;
214 {
215         close (info -> rfdesc);
216         info -> rfdesc = -1;
217
218         if (!quiet_interface_discovery)
219                 log_info ("Disabling input on Socket/%s%s%s",
220                       info -> name,
221                       (info -> shared_network ? "/" : ""),
222                       (info -> shared_network ?
223                        info -> shared_network -> name : ""));
224 }
225 #endif /* USE_SOCKET_RECEIVE */
226
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;
232         size_t len;
233         struct in_addr from;
234         struct sockaddr_in *to;
235         struct hardware *hto;
236 {
237         int result;
238 #ifdef IGNORE_HOSTUNREACH
239         int retry = 0;
240         do {
241 #endif
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) &&
246                  result < 0 &&
247                  (errno == EHOSTUNREACH ||
248                   errno == ECONNREFUSED) &&
249                  retry++ < 10);
250 #endif
251         if (result < 0) {
252                 log_error ("send_packet: %m");
253                 if (errno == ENETUNREACH)
254                         log_error ("send_packet: please consult README file%s",
255                                    " regarding broadcast address.");
256         }
257         return result;
258 }
259 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
260
261 #ifdef USE_SOCKET_RECEIVE
262 ssize_t receive_packet (interface, buf, len, from, hfrom)
263         struct interface_info *interface;
264         unsigned char *buf;
265         size_t len;
266         struct sockaddr_in *from;
267         struct hardware *hfrom;
268 {
269         SOCKLEN_T flen = sizeof *from;
270         int result;
271
272 #ifdef IGNORE_HOSTUNREACH
273         int retry = 0;
274         do {
275 #endif
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) &&
282                  retry++ < 10);
283 #endif
284         return result;
285 }
286 #endif /* USE_SOCKET_RECEIVE */
287
288 #if defined (USE_SOCKET_FALLBACK)
289 /* This just reads in a packet and silently discards it. */
290
291 isc_result_t fallback_discard (object)
292         omapi_object_t *object;
293 {
294         char buf [1540];
295         struct sockaddr_in from;
296         SOCKLEN_T flen = sizeof from;
297         int status;
298         struct interface_info *interface;
299
300         if (object -> type != dhcp_type_interface)
301                 return ISC_R_INVALIDARG;
302         interface = (struct interface_info *)object;
303
304         status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0,
305                            (struct sockaddr *)&from, &flen);
306 #if defined (DEBUG)
307         /* Only report fallback discard errors if we're debugging. */
308         if (status < 0) {
309                 log_error ("fallback_discard: %m");
310                 return ISC_R_UNEXPECTED;
311         }
312 #endif
313         return ISC_R_SUCCESS;
314 }
315 #endif /* USE_SOCKET_FALLBACK */
316
317 #if defined (USE_SOCKET_SEND)
318 int can_unicast_without_arp (ip)
319         struct interface_info *ip;
320 {
321         return 0;
322 }
323
324 int can_receive_unicast_unconfigured (ip)
325         struct interface_info *ip;
326 {
327 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
328         return 1;
329 #else
330         return 0;
331 #endif
332 }
333
334 int supports_multiple_interfaces (ip)
335         struct interface_info *ip;
336 {
337 #if defined (SO_BINDTODEVICE)
338         return 1;
339 #else
340         return 0;
341 #endif
342 }
343
344 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
345    do not. */
346
347 void maybe_setup_fallback ()
348 {
349 #if defined (USE_SOCKET_FALLBACK)
350         isc_result_t status;
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",
356                       fbi -> name,
357                       (fbi -> shared_network ? "/" : ""),
358                       (fbi -> shared_network ?
359                        fbi -> shared_network -> name : ""));
360         
361                 status = omapi_register_io_object ((omapi_object_t *)fbi,
362                                                    if_readsocket, 0,
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);
368         }
369 #endif
370 }
371 #endif /* USE_SOCKET_SEND */