ISC DHCP  4.3.1
A reference DHCPv4 and DHCPv6 implementation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
socket.c
Go to the documentation of this file.
1 /* socket.c
2 
3  BSD socket interface code... */
4 
5 /*
6  * Copyright (c) 2004-2014 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  * https://www.isc.org/
26  *
27  */
28 
29 /* SO_BINDTODEVICE support added by Elliot Poger (poger@leland.stanford.edu).
30  * This sockopt allows a socket to be bound to a particular interface,
31  * thus enabling the use of DHCPD on a multihomed host.
32  * If SO_BINDTODEVICE is defined in your system header files, the use of
33  * this sockopt will be automatically enabled.
34  * I have implemented it under Linux; other systems should be doable also.
35  */
36 
37 #include "dhcpd.h"
38 #include <errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/uio.h>
41 #include <sys/uio.h>
42 
43 #if defined(sun) && defined(USE_V4_PKTINFO)
44 #include <sys/sysmacros.h>
45 #include <net/if.h>
46 #include <sys/sockio.h>
47 #include <net/if_dl.h>
48 #include <sys/dlpi.h>
49 #endif
50 
51 #ifdef USE_SOCKET_FALLBACK
52 # if !defined (USE_SOCKET_SEND)
53 # define if_register_send if_register_fallback
54 # define send_packet send_fallback
55 # define if_reinitialize_send if_reinitialize_fallback
56 # endif
57 #endif
58 
59 #if defined(DHCPv6)
60 /*
61  * XXX: this is gross. we need to go back and overhaul the API for socket
62  * handling.
63  */
64 static int no_global_v6_socket = 0;
65 static unsigned int global_v6_socket_references = 0;
66 static int global_v6_socket = -1;
67 
68 static void if_register_multicast(struct interface_info *info);
69 #endif
70 
71 /*
72  * We can use a single socket for AF_INET (similar to AF_INET6) on all
73  * interfaces configured for DHCP if the system has support for IP_PKTINFO
74  * and IP_RECVPKTINFO (for example Solaris 11).
75  */
76 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
77 static unsigned int global_v4_socket_references = 0;
78 static int global_v4_socket = -1;
79 #endif
80 
81 /*
82  * If we can't bind() to a specific interface, then we can only have
83  * a single socket. This variable insures that we don't try to listen
84  * on two sockets.
85  */
86 #if !defined(SO_BINDTODEVICE) && !defined(USE_FALLBACK)
87 static int once = 0;
88 #endif /* !defined(SO_BINDTODEVICE) && !defined(USE_FALLBACK) */
89 
90 /* Reinitializes the specified interface after an address change. This
91  is not required for packet-filter APIs. */
92 
93 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
94 void if_reinitialize_send (info)
95  struct interface_info *info;
96 {
97 #if 0
98 #ifndef USE_SOCKET_RECEIVE
99  once = 0;
100  close (info -> wfdesc);
101 #endif
102  if_register_send (info);
103 #endif
104 }
105 #endif
106 
107 #ifdef USE_SOCKET_RECEIVE
108 void if_reinitialize_receive (info)
109  struct interface_info *info;
110 {
111 #if 0
112  once = 0;
113  close (info -> rfdesc);
114  if_register_receive (info);
115 #endif
116 }
117 #endif
118 
119 #if defined (USE_SOCKET_SEND) || \
120  defined (USE_SOCKET_RECEIVE) || \
121  defined (USE_SOCKET_FALLBACK)
122 /* Generic interface registration routine... */
123 int
124 if_register_socket(struct interface_info *info, int family,
125  int *do_multicast, struct in6_addr *linklocal6)
126 {
127  struct sockaddr_storage name;
128  int name_len;
129  int sock;
130  int flag;
131  int domain;
132 #ifdef DHCPv6
133  struct sockaddr_in6 *addr6;
134 #endif
135  struct sockaddr_in *addr;
136 
137  /* INSIST((family == AF_INET) || (family == AF_INET6)); */
138 
139 #if !defined(SO_BINDTODEVICE) && !defined(USE_FALLBACK)
140  /* Make sure only one interface is registered. */
141  if (once) {
142  log_fatal ("The standard socket API can only support %s",
143  "hosts with a single network interface.");
144  }
145  once = 1;
146 #endif
147 
148  /*
149  * Set up the address we're going to bind to, depending on the
150  * address family.
151  */
152  memset(&name, 0, sizeof(name));
153  switch (family) {
154 #ifdef DHCPv6
155  case AF_INET6:
156  addr6 = (struct sockaddr_in6 *)&name;
157  addr6->sin6_family = AF_INET6;
158  addr6->sin6_port = local_port;
159  if (linklocal6) {
160  memcpy(&addr6->sin6_addr,
161  linklocal6,
162  sizeof(addr6->sin6_addr));
163  addr6->sin6_scope_id = if_nametoindex(info->name);
164  }
165 #ifdef HAVE_SA_LEN
166  addr6->sin6_len = sizeof(*addr6);
167 #endif
168  name_len = sizeof(*addr6);
169  domain = PF_INET6;
170  if ((info->flags & INTERFACE_STREAMS) == INTERFACE_UPSTREAM) {
171  *do_multicast = 0;
172  }
173  break;
174 #endif /* DHCPv6 */
175 
176  case AF_INET:
177  default:
178  addr = (struct sockaddr_in *)&name;
179  addr->sin_family = AF_INET;
180  addr->sin_port = local_port;
181  memcpy(&addr->sin_addr,
182  &local_address,
183  sizeof(addr->sin_addr));
184 #ifdef HAVE_SA_LEN
185  addr->sin_len = sizeof(*addr);
186 #endif
187  name_len = sizeof(*addr);
188  domain = PF_INET;
189  break;
190  }
191 
192  /* Make a socket... */
193  sock = socket(domain, SOCK_DGRAM, IPPROTO_UDP);
194  if (sock < 0) {
195  log_fatal("Can't create dhcp socket: %m");
196  }
197 
198  /* Set the REUSEADDR option so that we don't fail to start if
199  we're being restarted. */
200  flag = 1;
201  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
202  (char *)&flag, sizeof(flag)) < 0) {
203  log_fatal("Can't set SO_REUSEADDR option on dhcp socket: %m");
204  }
205 
206  /* Set the BROADCAST option so that we can broadcast DHCP responses.
207  We shouldn't do this for fallback devices, and we can detect that
208  a device is a fallback because it has no ifp structure. */
209  if (info->ifp &&
210  (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
211  (char *)&flag, sizeof(flag)) < 0)) {
212  log_fatal("Can't set SO_BROADCAST option on dhcp socket: %m");
213  }
214 
215 #if defined(DHCPv6) && defined(SO_REUSEPORT)
216  /*
217  * We only set SO_REUSEPORT on AF_INET6 sockets, so that multiple
218  * daemons can bind to their own sockets and get data for their
219  * respective interfaces. This does not (and should not) affect
220  * DHCPv4 sockets; we can't yet support BSD sockets well, much
221  * less multiple sockets. Make sense only with multicast.
222  */
223  if ((local_family == AF_INET6) && *do_multicast) {
224  flag = 1;
225  if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
226  (char *)&flag, sizeof(flag)) < 0) {
227  log_fatal("Can't set SO_REUSEPORT option on dhcp "
228  "socket: %m");
229  }
230  }
231 #endif
232 
233  /* Bind the socket to this interface's IP address. */
234  if (bind(sock, (struct sockaddr *)&name, name_len) < 0) {
235  log_error("Can't bind to dhcp address: %m");
236  log_error("Please make sure there is no other dhcp server");
237  log_error("running and that there's no entry for dhcp or");
238  log_error("bootp in /etc/inetd.conf. Also make sure you");
239  log_error("are not running HP JetAdmin software, which");
240  log_fatal("includes a bootp server.");
241  }
242 
243 #if defined(SO_BINDTODEVICE)
244  /* Bind this socket to this interface. */
245  if ((local_family != AF_INET6) && (info->ifp != NULL) &&
246  setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
247  (char *)(info -> ifp), sizeof(*(info -> ifp))) < 0) {
248  log_fatal("setsockopt: SO_BINDTODEVICE: %m");
249  }
250 #endif
251 
252  /* IP_BROADCAST_IF instructs the kernel which interface to send
253  * IP packets whose destination address is 255.255.255.255. These
254  * will be treated as subnet broadcasts on the interface identified
255  * by ip address (info -> primary_address). This is only known to
256  * be defined in SCO system headers, and may not be defined in all
257  * releases.
258  */
259 #if defined(SCO) && defined(IP_BROADCAST_IF)
260  if (info->address_count &&
261  setsockopt(sock, IPPROTO_IP, IP_BROADCAST_IF, &info->addresses[0],
262  sizeof(info->addresses[0])) < 0)
263  log_fatal("Can't set IP_BROADCAST_IF on dhcp socket: %m");
264 #endif
265 
266 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
267  /*
268  * If we turn on IP_RECVPKTINFO we will be able to receive
269  * the interface index information of the received packet.
270  */
271  if (family == AF_INET) {
272  int on = 1;
273  if (setsockopt(sock, IPPROTO_IP, IP_RECVPKTINFO,
274  &on, sizeof(on)) != 0) {
275  log_fatal("setsockopt: IPV_RECVPKTINFO: %m");
276  }
277  }
278 #endif
279 
280 #ifdef DHCPv6
281  /*
282  * If we turn on IPV6_PKTINFO, we will be able to receive
283  * additional information, such as the destination IP address.
284  * We need this to spot unicast packets.
285  */
286  if (family == AF_INET6) {
287  int on = 1;
288 #ifdef IPV6_RECVPKTINFO
289  /* RFC3542 */
290  if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
291  &on, sizeof(on)) != 0) {
292  log_fatal("setsockopt: IPV6_RECVPKTINFO: %m");
293  }
294 #else
295  /* RFC2292 */
296  if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO,
297  &on, sizeof(on)) != 0) {
298  log_fatal("setsockopt: IPV6_PKTINFO: %m");
299  }
300 #endif
301  }
302 
303  if ((family == AF_INET6) &&
304  /*
305  * rfc3315 says that: "If relay agent relays messages to
306  * All_DHCP_Servers multicast address or other multicast addresses,
307  * it sets the Hop Limit field to 32."
308  *
309  * Because we use the same socket for all (upper/lower)
310  * interfaces, the hop limit is the same for both/all.
311  * There should be INTERFACE_UPSTREAM in the below condition, but
312  * problem is when the interface which registers the socket is
313  * lower interface, in that case the hop limit was not set
314  * (i.e. set to 1) for both.
315  * Because rfc doesn't say anything about hop limit for lower,
316  * it'd be lesser evil to have 32 in both cases.
317  */
318  ((info->flags & INTERFACE_STREAMS) != 0)) {
319  int hop_limit = 32;
320  if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
321  &hop_limit, sizeof(int)) < 0) {
322  log_fatal("setsockopt: IPV6_MULTICAST_HOPS: %m");
323  }
324  }
325 #endif /* DHCPv6 */
326 
327  return sock;
328 }
329 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
330 
331 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
332 void if_register_send (info)
333  struct interface_info *info;
334 {
335 #ifndef USE_SOCKET_RECEIVE
336  info->wfdesc = if_register_socket(info, AF_INET, 0, NULL);
337  /* If this is a normal IPv4 address, get the hardware address. */
338  if (strcmp(info->name, "fallback") != 0)
339  get_hw_addr(info);
340 #if defined (USE_SOCKET_FALLBACK)
341  /* Fallback only registers for send, but may need to receive as
342  well. */
343  info->rfdesc = info->wfdesc;
344 #endif
345 #else
346  info->wfdesc = info->rfdesc;
347 #endif
349  log_info ("Sending on Socket/%s%s%s",
350  info->name,
351  (info->shared_network ? "/" : ""),
352  (info->shared_network ?
353  info->shared_network->name : ""));
354 }
355 
356 #if defined (USE_SOCKET_SEND)
357 void if_deregister_send (info)
358  struct interface_info *info;
359 {
360 #ifndef USE_SOCKET_RECEIVE
361  close (info -> wfdesc);
362 #endif
363  info -> wfdesc = -1;
364 
366  log_info ("Disabling output on Socket/%s%s%s",
367  info -> name,
368  (info -> shared_network ? "/" : ""),
369  (info -> shared_network ?
370  info -> shared_network -> name : ""));
371 }
372 #endif /* USE_SOCKET_SEND */
373 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
374 
375 #ifdef USE_SOCKET_RECEIVE
376 void if_register_receive (info)
377  struct interface_info *info;
378 {
379 
380 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
381  if (global_v4_socket_references == 0) {
382  global_v4_socket = if_register_socket(info, AF_INET, 0, NULL);
383  if (global_v4_socket < 0) {
384  /*
385  * if_register_socket() fatally logs if it fails to
386  * create a socket, this is just a sanity check.
387  */
388  log_fatal("Failed to create AF_INET socket %s:%d",
389  MDL);
390  }
391  }
392 
393  info->rfdesc = global_v4_socket;
394  global_v4_socket_references++;
395 #else
396  /* If we're using the socket API for sending and receiving,
397  we don't need to register this interface twice. */
398  info->rfdesc = if_register_socket(info, AF_INET, 0, NULL);
399 #endif /* IP_PKTINFO... */
400  /* If this is a normal IPv4 address, get the hardware address. */
401  if (strcmp(info->name, "fallback") != 0)
402  get_hw_addr(info);
403 
405  log_info ("Listening on Socket/%s%s%s",
406  info->name,
407  (info->shared_network ? "/" : ""),
408  (info->shared_network ?
409  info->shared_network->name : ""));
410 }
411 
412 void if_deregister_receive (info)
413  struct interface_info *info;
414 {
415 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
416  /* Dereference the global v4 socket. */
417  if ((info->rfdesc == global_v4_socket) &&
418  (info->wfdesc == global_v4_socket) &&
419  (global_v4_socket_references > 0)) {
420  global_v4_socket_references--;
421  info->rfdesc = -1;
422  } else {
423  log_fatal("Impossible condition at %s:%d", MDL);
424  }
425 
426  if (global_v4_socket_references == 0) {
427  close(global_v4_socket);
428  global_v4_socket = -1;
429  }
430 #else
431  close(info->rfdesc);
432  info->rfdesc = -1;
433 #endif /* IP_PKTINFO... */
435  log_info ("Disabling input on Socket/%s%s%s",
436  info -> name,
437  (info -> shared_network ? "/" : ""),
438  (info -> shared_network ?
439  info -> shared_network -> name : ""));
440 }
441 #endif /* USE_SOCKET_RECEIVE */
442 
443 
444 #ifdef DHCPv6
445 /*
446  * This function joins the interface to DHCPv6 multicast groups so we will
447  * receive multicast messages.
448  */
449 static void
450 if_register_multicast(struct interface_info *info) {
451  int sock = info->rfdesc;
452  struct ipv6_mreq mreq;
453 
454  if (inet_pton(AF_INET6, All_DHCP_Relay_Agents_and_Servers,
455  &mreq.ipv6mr_multiaddr) <= 0) {
456  log_fatal("inet_pton: unable to convert '%s'",
458  }
459  mreq.ipv6mr_interface = if_nametoindex(info->name);
460  if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
461  &mreq, sizeof(mreq)) < 0) {
462  log_fatal("setsockopt: IPV6_JOIN_GROUP: %m");
463  }
464 
465  /*
466  * The relay agent code sets the streams so you know which way
467  * is up and down. But a relay agent shouldn't join to the
468  * Server address, or else you get fun loops. So up or down
469  * doesn't matter, we're just using that config to sense this is
470  * a relay agent.
471  */
472  if ((info->flags & INTERFACE_STREAMS) == 0) {
473  if (inet_pton(AF_INET6, All_DHCP_Servers,
474  &mreq.ipv6mr_multiaddr) <= 0) {
475  log_fatal("inet_pton: unable to convert '%s'",
477  }
478  mreq.ipv6mr_interface = if_nametoindex(info->name);
479  if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
480  &mreq, sizeof(mreq)) < 0) {
481  log_fatal("setsockopt: IPV6_JOIN_GROUP: %m");
482  }
483  }
484 }
485 
486 void
487 if_register6(struct interface_info *info, int do_multicast) {
488  /* Bounce do_multicast to a stack variable because we may change it. */
489  int req_multi = do_multicast;
490 
491  if (no_global_v6_socket) {
492  log_fatal("Impossible condition at %s:%d", MDL);
493  }
494 
495  if (global_v6_socket_references == 0) {
496  global_v6_socket = if_register_socket(info, AF_INET6,
497  &req_multi, NULL);
498  if (global_v6_socket < 0) {
499  /*
500  * if_register_socket() fatally logs if it fails to
501  * create a socket, this is just a sanity check.
502  */
503  log_fatal("Impossible condition at %s:%d", MDL);
504  } else {
505  log_info("Bound to *:%d", ntohs(local_port));
506  }
507  }
508 
509  info->rfdesc = global_v6_socket;
510  info->wfdesc = global_v6_socket;
511  global_v6_socket_references++;
512 
513  if (req_multi)
514  if_register_multicast(info);
515 
516  get_hw_addr(info);
517 
519  if (info->shared_network != NULL) {
520  log_info("Listening on Socket/%d/%s/%s",
521  global_v6_socket, info->name,
522  info->shared_network->name);
523  log_info("Sending on Socket/%d/%s/%s",
524  global_v6_socket, info->name,
525  info->shared_network->name);
526  } else {
527  log_info("Listening on Socket/%s", info->name);
528  log_info("Sending on Socket/%s", info->name);
529  }
530  }
531 }
532 
533 /*
534  * Register an IPv6 socket bound to the link-local address of
535  * the argument interface (used by clients on a multiple interface box,
536  * vs. a server or a relay using the global IPv6 socket and running
537  * *only* in a single instance).
538  */
539 void
541  int sock;
542  int count;
543  struct in6_addr *addr6 = NULL;
544  int req_multi = 0;
545 
546  if (global_v6_socket >= 0) {
547  log_fatal("Impossible condition at %s:%d", MDL);
548  }
549 
550  no_global_v6_socket = 1;
551 
552  /* get the (?) link-local address */
553  for (count = 0; count < info->v6address_count; count++) {
554  addr6 = &info->v6addresses[count];
555  if (IN6_IS_ADDR_LINKLOCAL(addr6))
556  break;
557  }
558 
559  if (!addr6) {
560  log_fatal("no link-local IPv6 address for %s", info->name);
561  }
562 
563  sock = if_register_socket(info, AF_INET6, &req_multi, addr6);
564 
565  if (sock < 0) {
566  log_fatal("if_register_socket for %s fails", info->name);
567  }
568 
569  info->rfdesc = sock;
570  info->wfdesc = sock;
571 
572  get_hw_addr(info);
573 
575  if (info->shared_network != NULL) {
576  log_info("Listening on Socket/%d/%s/%s",
577  global_v6_socket, info->name,
578  info->shared_network->name);
579  log_info("Sending on Socket/%d/%s/%s",
580  global_v6_socket, info->name,
581  info->shared_network->name);
582  } else {
583  log_info("Listening on Socket/%s", info->name);
584  log_info("Sending on Socket/%s", info->name);
585  }
586  }
587 }
588 
589 void
590 if_deregister6(struct interface_info *info) {
591  /* client case */
592  if (no_global_v6_socket) {
593  close(info->rfdesc);
594  info->rfdesc = -1;
595  info->wfdesc = -1;
596  } else if ((info->rfdesc == global_v6_socket) &&
597  (info->wfdesc == global_v6_socket) &&
598  (global_v6_socket_references > 0)) {
599  /* Dereference the global v6 socket. */
600  global_v6_socket_references--;
601  info->rfdesc = -1;
602  info->wfdesc = -1;
603  } else {
604  log_fatal("Impossible condition at %s:%d", MDL);
605  }
606 
608  if (info->shared_network != NULL) {
609  log_info("Disabling input on Socket/%s/%s", info->name,
610  info->shared_network->name);
611  log_info("Disabling output on Socket/%s/%s", info->name,
612  info->shared_network->name);
613  } else {
614  log_info("Disabling input on Socket/%s", info->name);
615  log_info("Disabling output on Socket/%s", info->name);
616  }
617  }
618 
619  if (!no_global_v6_socket &&
620  (global_v6_socket_references == 0)) {
621  close(global_v6_socket);
622  global_v6_socket = -1;
623 
624  log_info("Unbound from *:%d", ntohs(local_port));
625  }
626 }
627 #endif /* DHCPv6 */
628 
629 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
630 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
631  struct interface_info *interface;
632  struct packet *packet;
633  struct dhcp_packet *raw;
634  size_t len;
635  struct in_addr from;
636  struct sockaddr_in *to;
637  struct hardware *hto;
638 {
639  int result;
640 #ifdef IGNORE_HOSTUNREACH
641  int retry = 0;
642  do {
643 #endif
644 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
645  struct in_pktinfo pktinfo;
646 
647  if (interface->ifp != NULL) {
648  memset(&pktinfo, 0, sizeof (pktinfo));
649  pktinfo.ipi_ifindex = interface->ifp->ifr_index;
650  if (setsockopt(interface->wfdesc, IPPROTO_IP,
651  IP_PKTINFO, (char *)&pktinfo,
652  sizeof(pktinfo)) < 0)
653  log_fatal("setsockopt: IP_PKTINFO: %m");
654  }
655 #endif
656  result = sendto (interface -> wfdesc, (char *)raw, len, 0,
657  (struct sockaddr *)to, sizeof *to);
658 #ifdef IGNORE_HOSTUNREACH
659  } while (to -> sin_addr.s_addr == htonl (INADDR_BROADCAST) &&
660  result < 0 &&
661  (errno == EHOSTUNREACH ||
662  errno == ECONNREFUSED) &&
663  retry++ < 10);
664 #endif
665  if (result < 0) {
666  log_error ("send_packet: %m");
667  if (errno == ENETUNREACH)
668  log_error ("send_packet: please consult README file%s",
669  " regarding broadcast address.");
670  }
671  return result;
672 }
673 
674 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
675 
676 #ifdef DHCPv6
677 /*
678  * Solaris 9 is missing the CMSG_LEN and CMSG_SPACE macros, so we will
679  * synthesize them (based on the BIND 9 technique).
680  */
681 
682 #ifndef CMSG_LEN
683 static size_t CMSG_LEN(size_t len) {
684  size_t hdrlen;
685  /*
686  * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
687  * is correct.
688  */
689  hdrlen = (size_t)CMSG_DATA(((struct cmsghdr *)NULL));
690  return hdrlen + len;
691 }
692 #endif /* !CMSG_LEN */
693 
694 #ifndef CMSG_SPACE
695 static size_t CMSG_SPACE(size_t len) {
696  struct msghdr msg;
697  struct cmsghdr *cmsgp;
698 
699  /*
700  * XXX: The buffer length is an ad-hoc value, but should be enough
701  * in a practical sense.
702  */
703  union {
704  struct cmsghdr cmsg_sizer;
705  u_int8_t pktinfo_sizer[sizeof(struct cmsghdr) + 1024];
706  } dummybuf;
707 
708  memset(&msg, 0, sizeof(msg));
709  msg.msg_control = &dummybuf;
710  msg.msg_controllen = sizeof(dummybuf);
711 
712  cmsgp = (struct cmsghdr *)&dummybuf;
713  cmsgp->cmsg_len = CMSG_LEN(len);
714 
715  cmsgp = CMSG_NXTHDR(&msg, cmsgp);
716  if (cmsgp != NULL) {
717  return (char *)cmsgp - (char *)msg.msg_control;
718  } else {
719  return 0;
720  }
721 }
722 #endif /* !CMSG_SPACE */
723 
724 #endif /* DHCPv6 */
725 
726 #if defined(DHCPv6) || \
727  (defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && \
728  defined(USE_V4_PKTINFO))
729 /*
730  * For both send_packet6() and receive_packet6() we need to allocate
731  * space for the cmsg header information. We do this once and reuse
732  * the buffer. We also need the control buf for send_packet() and
733  * receive_packet() when we use a single socket and IP_PKTINFO to
734  * send the packet out the correct interface.
735  */
736 static void *control_buf = NULL;
737 static size_t control_buf_len = 0;
738 
739 static void
740 allocate_cmsg_cbuf(void) {
741  control_buf_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
742  control_buf = dmalloc(control_buf_len, MDL);
743  return;
744 }
745 #endif /* DHCPv6, IP_PKTINFO ... */
746 
747 #ifdef DHCPv6
748 /*
749  * For both send_packet6() and receive_packet6() we need to use the
750  * sendmsg()/recvmsg() functions rather than the simpler send()/recv()
751  * functions.
752  *
753  * In the case of send_packet6(), we need to do this in order to insure
754  * that the reply packet leaves on the same interface that it arrived
755  * on.
756  *
757  * In the case of receive_packet6(), we need to do this in order to
758  * get the IP address the packet was sent to. This is used to identify
759  * whether a packet is multicast or unicast.
760  *
761  * Helpful man pages: recvmsg, readv (talks about the iovec stuff), cmsg.
762  *
763  * Also see the sections in RFC 3542 about IPV6_PKTINFO.
764  */
765 
766 /* Send an IPv6 packet */
767 ssize_t send_packet6(struct interface_info *interface,
768  const unsigned char *raw, size_t len,
769  struct sockaddr_in6 *to) {
770  struct msghdr m;
771  struct iovec v;
772  struct sockaddr_in6 dst;
773  int result;
774  struct in6_pktinfo *pktinfo;
775  struct cmsghdr *cmsg;
776  unsigned int ifindex;
777 
778  /*
779  * If necessary allocate space for the control message header.
780  * The space is common between send and receive.
781  */
782 
783  if (control_buf == NULL) {
784  allocate_cmsg_cbuf();
785  if (control_buf == NULL) {
786  log_error("send_packet6: unable to allocate cmsg header");
787  return(ENOMEM);
788  }
789  }
790  memset(control_buf, 0, control_buf_len);
791 
792  /*
793  * Initialize our message header structure.
794  */
795  memset(&m, 0, sizeof(m));
796 
797  /*
798  * Set the target address we're sending to.
799  * Enforce the scope ID for bogus BSDs.
800  */
801  memcpy(&dst, to, sizeof(dst));
802  m.msg_name = &dst;
803  m.msg_namelen = sizeof(dst);
804  ifindex = if_nametoindex(interface->name);
805  if (no_global_v6_socket)
806  dst.sin6_scope_id = ifindex;
807 
808  /*
809  * Set the data buffer we're sending. (Using this wacky
810  * "scatter-gather" stuff... we only have a single chunk
811  * of data to send, so we declare a single vector entry.)
812  */
813  v.iov_base = (char *)raw;
814  v.iov_len = len;
815  m.msg_iov = &v;
816  m.msg_iovlen = 1;
817 
818  /*
819  * Setting the interface is a bit more involved.
820  *
821  * We have to create a "control message", and set that to
822  * define the IPv6 packet information. We could set the
823  * source address if we wanted, but we can safely let the
824  * kernel decide what that should be.
825  */
826  m.msg_control = control_buf;
827  m.msg_controllen = control_buf_len;
828  cmsg = CMSG_FIRSTHDR(&m);
829  INSIST(cmsg != NULL);
830  cmsg->cmsg_level = IPPROTO_IPV6;
831  cmsg->cmsg_type = IPV6_PKTINFO;
832  cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
833  pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
834  memset(pktinfo, 0, sizeof(*pktinfo));
835  pktinfo->ipi6_ifindex = ifindex;
836 
837  result = sendmsg(interface->wfdesc, &m, 0);
838  if (result < 0) {
839  log_error("send_packet6: %m");
840  }
841  return result;
842 }
843 #endif /* DHCPv6 */
844 
845 #ifdef USE_SOCKET_RECEIVE
846 ssize_t receive_packet (interface, buf, len, from, hfrom)
847  struct interface_info *interface;
848  unsigned char *buf;
849  size_t len;
850  struct sockaddr_in *from;
851  struct hardware *hfrom;
852 {
853 #if !(defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO))
854  SOCKLEN_T flen = sizeof *from;
855 #endif
856  int result;
857 
858  /*
859  * The normal Berkeley socket interface doesn't give us any way
860  * to know what hardware interface we received the message on,
861  * but we should at least make sure the structure is emptied.
862  */
863  memset(hfrom, 0, sizeof(*hfrom));
864 
865 #ifdef IGNORE_HOSTUNREACH
866  int retry = 0;
867  do {
868 #endif
869 
870 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
871  struct msghdr m;
872  struct iovec v;
873  struct cmsghdr *cmsg;
874  struct in_pktinfo *pktinfo;
875  unsigned int ifindex;
876 
877  /*
878  * If necessary allocate space for the control message header.
879  * The space is common between send and receive.
880  */
881  if (control_buf == NULL) {
882  allocate_cmsg_cbuf();
883  if (control_buf == NULL) {
884  log_error("receive_packet: unable to allocate cmsg "
885  "header");
886  return(ENOMEM);
887  }
888  }
889  memset(control_buf, 0, control_buf_len);
890 
891  /*
892  * Initialize our message header structure.
893  */
894  memset(&m, 0, sizeof(m));
895 
896  /*
897  * Point so we can get the from address.
898  */
899  m.msg_name = from;
900  m.msg_namelen = sizeof(*from);
901 
902  /*
903  * Set the data buffer we're receiving. (Using this wacky
904  * "scatter-gather" stuff... but we that doesn't really make
905  * sense for us, so we use a single vector entry.)
906  */
907  v.iov_base = buf;
908  v.iov_len = len;
909  m.msg_iov = &v;
910  m.msg_iovlen = 1;
911 
912  /*
913  * Getting the interface is a bit more involved.
914  *
915  * We set up some space for a "control message". We have
916  * previously asked the kernel to give us packet
917  * information (when we initialized the interface), so we
918  * should get the interface index from that.
919  */
920  m.msg_control = control_buf;
921  m.msg_controllen = control_buf_len;
922 
923  result = recvmsg(interface->rfdesc, &m, 0);
924 
925  if (result >= 0) {
926  /*
927  * If we did read successfully, then we need to loop
928  * through the control messages we received and
929  * find the one with our inteface index.
930  */
931  cmsg = CMSG_FIRSTHDR(&m);
932  while (cmsg != NULL) {
933  if ((cmsg->cmsg_level == IPPROTO_IP) &&
934  (cmsg->cmsg_type == IP_PKTINFO)) {
935  pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
936  ifindex = pktinfo->ipi_ifindex;
937  /*
938  * We pass the ifindex back to the caller
939  * using the unused hfrom parameter avoiding
940  * interface changes between sockets and
941  * the discover code.
942  */
943  memcpy(hfrom->hbuf, &ifindex, sizeof(ifindex));
944  return (result);
945  }
946  cmsg = CMSG_NXTHDR(&m, cmsg);
947  }
948 
949  /*
950  * We didn't find the necessary control message
951  * flag it as an error
952  */
953  result = -1;
954  errno = EIO;
955  }
956 #else
957  result = recvfrom(interface -> rfdesc, (char *)buf, len, 0,
958  (struct sockaddr *)from, &flen);
959 #endif /* IP_PKTINFO ... */
960 #ifdef IGNORE_HOSTUNREACH
961  } while (result < 0 &&
962  (errno == EHOSTUNREACH ||
963  errno == ECONNREFUSED) &&
964  retry++ < 10);
965 #endif
966  return (result);
967 }
968 
969 #endif /* USE_SOCKET_RECEIVE */
970 
971 #ifdef DHCPv6
972 ssize_t
973 receive_packet6(struct interface_info *interface,
974  unsigned char *buf, size_t len,
975  struct sockaddr_in6 *from, struct in6_addr *to_addr,
976  unsigned int *if_idx)
977 {
978  struct msghdr m;
979  struct iovec v;
980  int result;
981  struct cmsghdr *cmsg;
982  struct in6_pktinfo *pktinfo;
983 
984  /*
985  * If necessary allocate space for the control message header.
986  * The space is common between send and receive.
987  */
988  if (control_buf == NULL) {
989  allocate_cmsg_cbuf();
990  if (control_buf == NULL) {
991  log_error("receive_packet6: unable to allocate cmsg "
992  "header");
993  return(ENOMEM);
994  }
995  }
996  memset(control_buf, 0, control_buf_len);
997 
998  /*
999  * Initialize our message header structure.
1000  */
1001  memset(&m, 0, sizeof(m));
1002 
1003  /*
1004  * Point so we can get the from address.
1005  */
1006  m.msg_name = from;
1007  m.msg_namelen = sizeof(*from);
1008 
1009  /*
1010  * Set the data buffer we're receiving. (Using this wacky
1011  * "scatter-gather" stuff... but we that doesn't really make
1012  * sense for us, so we use a single vector entry.)
1013  */
1014  v.iov_base = buf;
1015  v.iov_len = len;
1016  m.msg_iov = &v;
1017  m.msg_iovlen = 1;
1018 
1019  /*
1020  * Getting the interface is a bit more involved.
1021  *
1022  * We set up some space for a "control message". We have
1023  * previously asked the kernel to give us packet
1024  * information (when we initialized the interface), so we
1025  * should get the destination address from that.
1026  */
1027  m.msg_control = control_buf;
1028  m.msg_controllen = control_buf_len;
1029 
1030  result = recvmsg(interface->rfdesc, &m, 0);
1031 
1032  if (result >= 0) {
1033  /*
1034  * If we did read successfully, then we need to loop
1035  * through the control messages we received and
1036  * find the one with our destination address.
1037  */
1038  cmsg = CMSG_FIRSTHDR(&m);
1039  while (cmsg != NULL) {
1040  if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
1041  (cmsg->cmsg_type == IPV6_PKTINFO)) {
1042  pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
1043  *to_addr = pktinfo->ipi6_addr;
1044  *if_idx = pktinfo->ipi6_ifindex;
1045 
1046  return (result);
1047  }
1048  cmsg = CMSG_NXTHDR(&m, cmsg);
1049  }
1050 
1051  /*
1052  * We didn't find the necessary control message
1053  * flag is as an error
1054  */
1055  result = -1;
1056  errno = EIO;
1057  }
1058 
1059  return (result);
1060 }
1061 #endif /* DHCPv6 */
1062 
1063 #if defined (USE_SOCKET_FALLBACK)
1064 /* This just reads in a packet and silently discards it. */
1065 
1066 isc_result_t fallback_discard (object)
1067  omapi_object_t *object;
1068 {
1069  char buf [1540];
1070  struct sockaddr_in from;
1071  SOCKLEN_T flen = sizeof from;
1072  int status;
1073  struct interface_info *interface;
1074 
1075  if (object -> type != dhcp_type_interface)
1076  return DHCP_R_INVALIDARG;
1077  interface = (struct interface_info *)object;
1078 
1079  status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0,
1080  (struct sockaddr *)&from, &flen);
1081 #if defined (DEBUG)
1082  /* Only report fallback discard errors if we're debugging. */
1083  if (status < 0) {
1084  log_error ("fallback_discard: %m");
1085  return ISC_R_UNEXPECTED;
1086  }
1087 #else
1088  /* ignore the fact that status value is never used */
1089  IGNORE_UNUSED(status);
1090 #endif
1091  return ISC_R_SUCCESS;
1092 }
1093 #endif /* USE_SOCKET_FALLBACK */
1094 
1095 #if defined (USE_SOCKET_SEND)
1097  struct interface_info *ip;
1098 {
1099  return 0;
1100 }
1101 
1103  struct interface_info *ip;
1104 {
1105 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
1106  return 1;
1107 #else
1108  return 0;
1109 #endif
1110 }
1111 
1113  struct interface_info *ip;
1114 {
1115 #if defined(SO_BINDTODEVICE) || \
1116  (defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && \
1117  defined(USE_V4_PKTINFO))
1118  return(1);
1119 #else
1120  return(0);
1121 #endif
1122 }
1123 
1124 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
1125  do not. */
1126 
1127 void maybe_setup_fallback ()
1128 {
1129 #if defined (USE_SOCKET_FALLBACK)
1130  isc_result_t status;
1131  struct interface_info *fbi = (struct interface_info *)0;
1132  if (setup_fallback (&fbi, MDL)) {
1133  fbi -> wfdesc = if_register_socket (fbi, AF_INET, 0, NULL);
1134  fbi -> rfdesc = fbi -> wfdesc;
1135  log_info ("Sending on Socket/%s%s%s",
1136  fbi -> name,
1137  (fbi -> shared_network ? "/" : ""),
1138  (fbi -> shared_network ?
1139  fbi -> shared_network -> name : ""));
1140 
1141  status = omapi_register_io_object ((omapi_object_t *)fbi,
1142  if_readsocket, 0,
1143  fallback_discard, 0, 0);
1144  if (status != ISC_R_SUCCESS)
1145  log_fatal ("Can't register I/O handle for %s: %s",
1146  fbi -> name, isc_result_totext (status));
1147  interface_dereference (&fbi, MDL);
1148  }
1149 #endif
1150 }
1151 
1152 
1153 #if defined(sun) && defined(USE_V4_PKTINFO)
1154 /* This code assumes the existence of SIOCGLIFHWADDR */
1155 void
1156 get_hw_addr(const char *name, struct hardware *hw) {
1157  struct sockaddr_dl *dladdrp;
1158  int sock, i;
1159  struct lifreq lifr;
1160 
1161  memset(&lifr, 0, sizeof (lifr));
1162  (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1163  /*
1164  * Check if the interface is a virtual or IPMP interface - in those
1165  * cases it has no hw address, so generate a random one.
1166  */
1167  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
1168  ioctl(sock, SIOCGLIFFLAGS, &lifr) < 0) {
1169  if (sock != -1)
1170  (void) close(sock);
1171 
1172 #ifdef DHCPv6
1173  /*
1174  * If approrpriate try this with an IPv6 socket
1175  */
1176  if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0 &&
1177  ioctl(sock, SIOCGLIFFLAGS, &lifr) >= 0) {
1178  goto flag_check;
1179  }
1180  if (sock != -1)
1181  (void) close(sock);
1182 #endif
1183  log_fatal("Couldn't get interface flags for %s: %m", name);
1184 
1185  }
1186 
1187  flag_check:
1188  if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP)) {
1189  hw->hlen = sizeof (hw->hbuf);
1190  srandom((long)gethrtime());
1191 
1192  hw->hbuf[0] = HTYPE_IPMP;
1193  for (i = 1; i < hw->hlen; ++i) {
1194  hw->hbuf[i] = random() % 256;
1195  }
1196 
1197  if (sock != -1)
1198  (void) close(sock);
1199  return;
1200  }
1201 
1202  if (ioctl(sock, SIOCGLIFHWADDR, &lifr) < 0)
1203  log_fatal("Couldn't get interface hardware address for %s: %m",
1204  name);
1205  dladdrp = (struct sockaddr_dl *)&lifr.lifr_addr;
1206  hw->hlen = dladdrp->sdl_alen+1;
1207  switch (dladdrp->sdl_type) {
1208  case DL_CSMACD: /* IEEE 802.3 */
1209  case DL_ETHER:
1210  hw->hbuf[0] = HTYPE_ETHER;
1211  break;
1212  case DL_TPR:
1213  hw->hbuf[0] = HTYPE_IEEE802;
1214  break;
1215  case DL_FDDI:
1216  hw->hbuf[0] = HTYPE_FDDI;
1217  break;
1218  case DL_IB:
1219  hw->hbuf[0] = HTYPE_INFINIBAND;
1220  break;
1221  default:
1222  log_fatal("%s: unsupported DLPI MAC type %lu", name,
1223  (unsigned long)dladdrp->sdl_type);
1224  }
1225 
1226  memcpy(hw->hbuf+1, LLADDR(dladdrp), hw->hlen-1);
1227 
1228  if (sock != -1)
1229  (void) close(sock);
1230 }
1231 #endif /* defined(sun) */
1232 
1233 #endif /* USE_SOCKET_SEND */
void if_register_send(struct interface_info *)
#define IGNORE_UNUSED(x)
Definition: cdefs.h:68
isc_result_t omapi_register_io_object(omapi_object_t *, int(*)(omapi_object_t *), int(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *))
Definition: dispatch.c:199
#define SIOCGLIFFLAGS
Definition: discover.c:189
struct shared_network * shared_network
Definition: dhcpd.h:1248
u_int8_t hlen
Definition: dhcpd.h:440
int if_readsocket(omapi_object_t *h)
Definition: discover.c:964
char name[IFNAMSIZ]
Definition: dhcpd.h:1272
void if_reinitialize_send(struct interface_info *)
#define All_DHCP_Relay_Agents_and_Servers
Definition: dhcp6.h:140
void * dmalloc(unsigned, const char *, int)
Definition: alloc.c:56
#define MDL
Definition: omapip.h:568
#define DHCP_R_INVALIDARG
Definition: result.h:48
int can_receive_unicast_unconfigured(struct interface_info *)
struct in_addr * addresses
Definition: dhcpd.h:1252
int setup_fallback(struct interface_info **fp, const char *file, int line)
Definition: discover.c:975
int log_error(const char *,...) __attribute__((__format__(__printf__
void if_deregister_receive(struct interface_info *)
void get_hw_addr(struct interface_info *info)
void maybe_setup_fallback(void)
void if_deregister_send(struct interface_info *)
void log_fatal(const char *,...) __attribute__((__format__(__printf__
#define HTYPE_ETHER
Definition: dhcp.h:76
void if_deregister6(struct interface_info *info)
void if_register_linklocal6(struct interface_info *info)
#define HTYPE_INFINIBAND
Definition: dhcp.h:79
ssize_t send_packet6(struct interface_info *, const unsigned char *, size_t, struct sockaddr_in6 *)
u_int16_t local_port
Definition: dhclient.c:88
Definition: dhcpd.h:369
Definition: ip.h:47
ssize_t send_packet(struct interface_info *, struct packet *, struct dhcp_packet *, size_t, struct in_addr, struct sockaddr_in *, struct hardware *)
omapi_object_type_t * dhcp_type_interface
Definition: discover.c:71
int int log_info(const char *,...) __attribute__((__format__(__printf__
u_int32_t flags
Definition: dhcpd.h:1286
int v6address_count
Definition: dhcpd.h:1259
void if_register6(struct interface_info *info, int do_multicast)
int local_family
Definition: discover.c:55
int quiet_interface_discovery
Definition: discover.c:45
#define HTYPE_FDDI
Definition: dhcp.h:78
#define HTYPE_IPMP
Definition: dhcp.h:80
#define All_DHCP_Servers
Definition: dhcp6.h:141
int supports_multiple_interfaces(struct interface_info *)
u_int8_t hbuf[HARDWARE_ADDR_LEN+1]
Definition: dhcpd.h:441
int address_count
Definition: dhcpd.h:1255
#define INTERFACE_UPSTREAM
Definition: dhcpd.h:1291
struct in_addr local_address
Definition: discover.c:56
ssize_t receive_packet(struct interface_info *, unsigned char *, size_t, struct sockaddr_in *, struct hardware *)
#define HTYPE_IEEE802
Definition: dhcp.h:77
char * name
Definition: dhcpd.h:937
#define SOCKLEN_T
Definition: osdep.h:281
void if_reinitialize_receive(struct interface_info *)
int can_unicast_without_arp(struct interface_info *)
void if_register_receive(struct interface_info *)
isc_result_t fallback_discard(omapi_object_t *)
#define INTERFACE_STREAMS
Definition: dhcpd.h:1292
struct ifreq * ifp
Definition: dhcpd.h:1282
int if_register_socket(struct interface_info *, int, int *, struct in6_addr *)
ssize_t receive_packet6(struct interface_info *interface, unsigned char *buf, size_t len, struct sockaddr_in6 *from, struct in6_addr *to_addr, unsigned int *if_index)
struct in6_addr * v6addresses
Definition: dhcpd.h:1257