ISC DHCP  4.3.0
A reference DHCPv4 and DHCPv6 implementation
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  ((info->flags & INTERFACE_UPSTREAM) != 0)) {
305  int hop_limit = 32;
306  if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
307  &hop_limit, sizeof(int)) < 0) {
308  log_fatal("setsockopt: IPV6_MULTICAST_HOPS: %m");
309  }
310  }
311 #endif /* DHCPv6 */
312 
313  return sock;
314 }
315 #endif /* USE_SOCKET_SEND || USE_SOCKET_RECEIVE || USE_SOCKET_FALLBACK */
316 
317 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
318 void if_register_send (info)
319  struct interface_info *info;
320 {
321 #ifndef USE_SOCKET_RECEIVE
322  info->wfdesc = if_register_socket(info, AF_INET, 0, NULL);
323  /* If this is a normal IPv4 address, get the hardware address. */
324  if (strcmp(info->name, "fallback") != 0)
325  get_hw_addr(info);
326 #if defined (USE_SOCKET_FALLBACK)
327  /* Fallback only registers for send, but may need to receive as
328  well. */
329  info->rfdesc = info->wfdesc;
330 #endif
331 #else
332  info->wfdesc = info->rfdesc;
333 #endif
335  log_info ("Sending on Socket/%s%s%s",
336  info->name,
337  (info->shared_network ? "/" : ""),
338  (info->shared_network ?
339  info->shared_network->name : ""));
340 }
341 
342 #if defined (USE_SOCKET_SEND)
343 void if_deregister_send (info)
344  struct interface_info *info;
345 {
346 #ifndef USE_SOCKET_RECEIVE
347  close (info -> wfdesc);
348 #endif
349  info -> wfdesc = -1;
350 
352  log_info ("Disabling output on Socket/%s%s%s",
353  info -> name,
354  (info -> shared_network ? "/" : ""),
355  (info -> shared_network ?
356  info -> shared_network -> name : ""));
357 }
358 #endif /* USE_SOCKET_SEND */
359 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
360 
361 #ifdef USE_SOCKET_RECEIVE
362 void if_register_receive (info)
363  struct interface_info *info;
364 {
365 
366 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
367  if (global_v4_socket_references == 0) {
368  global_v4_socket = if_register_socket(info, AF_INET, 0, NULL);
369  if (global_v4_socket < 0) {
370  /*
371  * if_register_socket() fatally logs if it fails to
372  * create a socket, this is just a sanity check.
373  */
374  log_fatal("Failed to create AF_INET socket %s:%d",
375  MDL);
376  }
377  }
378 
379  info->rfdesc = global_v4_socket;
380  global_v4_socket_references++;
381 #else
382  /* If we're using the socket API for sending and receiving,
383  we don't need to register this interface twice. */
384  info->rfdesc = if_register_socket(info, AF_INET, 0, NULL);
385 #endif /* IP_PKTINFO... */
386  /* If this is a normal IPv4 address, get the hardware address. */
387  if (strcmp(info->name, "fallback") != 0)
388  get_hw_addr(info);
389 
391  log_info ("Listening on Socket/%s%s%s",
392  info->name,
393  (info->shared_network ? "/" : ""),
394  (info->shared_network ?
395  info->shared_network->name : ""));
396 }
397 
398 void if_deregister_receive (info)
399  struct interface_info *info;
400 {
401 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
402  /* Dereference the global v4 socket. */
403  if ((info->rfdesc == global_v4_socket) &&
404  (info->wfdesc == global_v4_socket) &&
405  (global_v4_socket_references > 0)) {
406  global_v4_socket_references--;
407  info->rfdesc = -1;
408  } else {
409  log_fatal("Impossible condition at %s:%d", MDL);
410  }
411 
412  if (global_v4_socket_references == 0) {
413  close(global_v4_socket);
414  global_v4_socket = -1;
415  }
416 #else
417  close(info->rfdesc);
418  info->rfdesc = -1;
419 #endif /* IP_PKTINFO... */
421  log_info ("Disabling input on Socket/%s%s%s",
422  info -> name,
423  (info -> shared_network ? "/" : ""),
424  (info -> shared_network ?
425  info -> shared_network -> name : ""));
426 }
427 #endif /* USE_SOCKET_RECEIVE */
428 
429 
430 #ifdef DHCPv6
431 /*
432  * This function joins the interface to DHCPv6 multicast groups so we will
433  * receive multicast messages.
434  */
435 static void
436 if_register_multicast(struct interface_info *info) {
437  int sock = info->rfdesc;
438  struct ipv6_mreq mreq;
439 
440  if (inet_pton(AF_INET6, All_DHCP_Relay_Agents_and_Servers,
441  &mreq.ipv6mr_multiaddr) <= 0) {
442  log_fatal("inet_pton: unable to convert '%s'",
444  }
445  mreq.ipv6mr_interface = if_nametoindex(info->name);
446  if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
447  &mreq, sizeof(mreq)) < 0) {
448  log_fatal("setsockopt: IPV6_JOIN_GROUP: %m");
449  }
450 
451  /*
452  * The relay agent code sets the streams so you know which way
453  * is up and down. But a relay agent shouldn't join to the
454  * Server address, or else you get fun loops. So up or down
455  * doesn't matter, we're just using that config to sense this is
456  * a relay agent.
457  */
458  if ((info->flags & INTERFACE_STREAMS) == 0) {
459  if (inet_pton(AF_INET6, All_DHCP_Servers,
460  &mreq.ipv6mr_multiaddr) <= 0) {
461  log_fatal("inet_pton: unable to convert '%s'",
463  }
464  mreq.ipv6mr_interface = if_nametoindex(info->name);
465  if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
466  &mreq, sizeof(mreq)) < 0) {
467  log_fatal("setsockopt: IPV6_JOIN_GROUP: %m");
468  }
469  }
470 }
471 
472 void
473 if_register6(struct interface_info *info, int do_multicast) {
474  /* Bounce do_multicast to a stack variable because we may change it. */
475  int req_multi = do_multicast;
476 
477  if (no_global_v6_socket) {
478  log_fatal("Impossible condition at %s:%d", MDL);
479  }
480 
481  if (global_v6_socket_references == 0) {
482  global_v6_socket = if_register_socket(info, AF_INET6,
483  &req_multi, NULL);
484  if (global_v6_socket < 0) {
485  /*
486  * if_register_socket() fatally logs if it fails to
487  * create a socket, this is just a sanity check.
488  */
489  log_fatal("Impossible condition at %s:%d", MDL);
490  } else {
491  log_info("Bound to *:%d", ntohs(local_port));
492  }
493  }
494 
495  info->rfdesc = global_v6_socket;
496  info->wfdesc = global_v6_socket;
497  global_v6_socket_references++;
498 
499  if (req_multi)
500  if_register_multicast(info);
501 
502  get_hw_addr(info);
503 
505  if (info->shared_network != NULL) {
506  log_info("Listening on Socket/%d/%s/%s",
507  global_v6_socket, info->name,
508  info->shared_network->name);
509  log_info("Sending on Socket/%d/%s/%s",
510  global_v6_socket, info->name,
511  info->shared_network->name);
512  } else {
513  log_info("Listening on Socket/%s", info->name);
514  log_info("Sending on Socket/%s", info->name);
515  }
516  }
517 }
518 
519 /*
520  * Register an IPv6 socket bound to the link-local address of
521  * the argument interface (used by clients on a multiple interface box,
522  * vs. a server or a relay using the global IPv6 socket and running
523  * *only* in a single instance).
524  */
525 void
527  int sock;
528  int count;
529  struct in6_addr *addr6 = NULL;
530  int req_multi = 0;
531 
532  if (global_v6_socket >= 0) {
533  log_fatal("Impossible condition at %s:%d", MDL);
534  }
535 
536  no_global_v6_socket = 1;
537 
538  /* get the (?) link-local address */
539  for (count = 0; count < info->v6address_count; count++) {
540  addr6 = &info->v6addresses[count];
541  if (IN6_IS_ADDR_LINKLOCAL(addr6))
542  break;
543  }
544 
545  if (!addr6) {
546  log_fatal("no link-local IPv6 address for %s", info->name);
547  }
548 
549  sock = if_register_socket(info, AF_INET6, &req_multi, addr6);
550 
551  if (sock < 0) {
552  log_fatal("if_register_socket for %s fails", info->name);
553  }
554 
555  info->rfdesc = sock;
556  info->wfdesc = sock;
557 
558  get_hw_addr(info);
559 
561  if (info->shared_network != NULL) {
562  log_info("Listening on Socket/%d/%s/%s",
563  global_v6_socket, info->name,
564  info->shared_network->name);
565  log_info("Sending on Socket/%d/%s/%s",
566  global_v6_socket, info->name,
567  info->shared_network->name);
568  } else {
569  log_info("Listening on Socket/%s", info->name);
570  log_info("Sending on Socket/%s", info->name);
571  }
572  }
573 }
574 
575 void
576 if_deregister6(struct interface_info *info) {
577  /* client case */
578  if (no_global_v6_socket) {
579  close(info->rfdesc);
580  info->rfdesc = -1;
581  info->wfdesc = -1;
582  } else if ((info->rfdesc == global_v6_socket) &&
583  (info->wfdesc == global_v6_socket) &&
584  (global_v6_socket_references > 0)) {
585  /* Dereference the global v6 socket. */
586  global_v6_socket_references--;
587  info->rfdesc = -1;
588  info->wfdesc = -1;
589  } else {
590  log_fatal("Impossible condition at %s:%d", MDL);
591  }
592 
594  if (info->shared_network != NULL) {
595  log_info("Disabling input on Socket/%s/%s", info->name,
596  info->shared_network->name);
597  log_info("Disabling output on Socket/%s/%s", info->name,
598  info->shared_network->name);
599  } else {
600  log_info("Disabling input on Socket/%s", info->name);
601  log_info("Disabling output on Socket/%s", info->name);
602  }
603  }
604 
605  if (!no_global_v6_socket &&
606  (global_v6_socket_references == 0)) {
607  close(global_v6_socket);
608  global_v6_socket = -1;
609 
610  log_info("Unbound from *:%d", ntohs(local_port));
611  }
612 }
613 #endif /* DHCPv6 */
614 
615 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_FALLBACK)
616 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
617  struct interface_info *interface;
618  struct packet *packet;
619  struct dhcp_packet *raw;
620  size_t len;
621  struct in_addr from;
622  struct sockaddr_in *to;
623  struct hardware *hto;
624 {
625  int result;
626 #ifdef IGNORE_HOSTUNREACH
627  int retry = 0;
628  do {
629 #endif
630 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
631  struct in_pktinfo pktinfo;
632 
633  if (interface->ifp != NULL) {
634  memset(&pktinfo, 0, sizeof (pktinfo));
635  pktinfo.ipi_ifindex = interface->ifp->ifr_index;
636  if (setsockopt(interface->wfdesc, IPPROTO_IP,
637  IP_PKTINFO, (char *)&pktinfo,
638  sizeof(pktinfo)) < 0)
639  log_fatal("setsockopt: IP_PKTINFO: %m");
640  }
641 #endif
642  result = sendto (interface -> wfdesc, (char *)raw, len, 0,
643  (struct sockaddr *)to, sizeof *to);
644 #ifdef IGNORE_HOSTUNREACH
645  } while (to -> sin_addr.s_addr == htonl (INADDR_BROADCAST) &&
646  result < 0 &&
647  (errno == EHOSTUNREACH ||
648  errno == ECONNREFUSED) &&
649  retry++ < 10);
650 #endif
651  if (result < 0) {
652  log_error ("send_packet: %m");
653  if (errno == ENETUNREACH)
654  log_error ("send_packet: please consult README file%s",
655  " regarding broadcast address.");
656  }
657  return result;
658 }
659 
660 #endif /* USE_SOCKET_SEND || USE_SOCKET_FALLBACK */
661 
662 #ifdef DHCPv6
663 /*
664  * Solaris 9 is missing the CMSG_LEN and CMSG_SPACE macros, so we will
665  * synthesize them (based on the BIND 9 technique).
666  */
667 
668 #ifndef CMSG_LEN
669 static size_t CMSG_LEN(size_t len) {
670  size_t hdrlen;
671  /*
672  * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
673  * is correct.
674  */
675  hdrlen = (size_t)CMSG_DATA(((struct cmsghdr *)NULL));
676  return hdrlen + len;
677 }
678 #endif /* !CMSG_LEN */
679 
680 #ifndef CMSG_SPACE
681 static size_t CMSG_SPACE(size_t len) {
682  struct msghdr msg;
683  struct cmsghdr *cmsgp;
684 
685  /*
686  * XXX: The buffer length is an ad-hoc value, but should be enough
687  * in a practical sense.
688  */
689  union {
690  struct cmsghdr cmsg_sizer;
691  u_int8_t pktinfo_sizer[sizeof(struct cmsghdr) + 1024];
692  } dummybuf;
693 
694  memset(&msg, 0, sizeof(msg));
695  msg.msg_control = &dummybuf;
696  msg.msg_controllen = sizeof(dummybuf);
697 
698  cmsgp = (struct cmsghdr *)&dummybuf;
699  cmsgp->cmsg_len = CMSG_LEN(len);
700 
701  cmsgp = CMSG_NXTHDR(&msg, cmsgp);
702  if (cmsgp != NULL) {
703  return (char *)cmsgp - (char *)msg.msg_control;
704  } else {
705  return 0;
706  }
707 }
708 #endif /* !CMSG_SPACE */
709 
710 #endif /* DHCPv6 */
711 
712 #if defined(DHCPv6) || \
713  (defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && \
714  defined(USE_V4_PKTINFO))
715 /*
716  * For both send_packet6() and receive_packet6() we need to allocate
717  * space for the cmsg header information. We do this once and reuse
718  * the buffer. We also need the control buf for send_packet() and
719  * receive_packet() when we use a single socket and IP_PKTINFO to
720  * send the packet out the correct interface.
721  */
722 static void *control_buf = NULL;
723 static size_t control_buf_len = 0;
724 
725 static void
726 allocate_cmsg_cbuf(void) {
727  control_buf_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
728  control_buf = dmalloc(control_buf_len, MDL);
729  return;
730 }
731 #endif /* DHCPv6, IP_PKTINFO ... */
732 
733 #ifdef DHCPv6
734 /*
735  * For both send_packet6() and receive_packet6() we need to use the
736  * sendmsg()/recvmsg() functions rather than the simpler send()/recv()
737  * functions.
738  *
739  * In the case of send_packet6(), we need to do this in order to insure
740  * that the reply packet leaves on the same interface that it arrived
741  * on.
742  *
743  * In the case of receive_packet6(), we need to do this in order to
744  * get the IP address the packet was sent to. This is used to identify
745  * whether a packet is multicast or unicast.
746  *
747  * Helpful man pages: recvmsg, readv (talks about the iovec stuff), cmsg.
748  *
749  * Also see the sections in RFC 3542 about IPV6_PKTINFO.
750  */
751 
752 /* Send an IPv6 packet */
753 ssize_t send_packet6(struct interface_info *interface,
754  const unsigned char *raw, size_t len,
755  struct sockaddr_in6 *to) {
756  struct msghdr m;
757  struct iovec v;
758  struct sockaddr_in6 dst;
759  int result;
760  struct in6_pktinfo *pktinfo;
761  struct cmsghdr *cmsg;
762  unsigned int ifindex;
763 
764  /*
765  * If necessary allocate space for the control message header.
766  * The space is common between send and receive.
767  */
768 
769  if (control_buf == NULL) {
770  allocate_cmsg_cbuf();
771  if (control_buf == NULL) {
772  log_error("send_packet6: unable to allocate cmsg header");
773  return(ENOMEM);
774  }
775  }
776  memset(control_buf, 0, control_buf_len);
777 
778  /*
779  * Initialize our message header structure.
780  */
781  memset(&m, 0, sizeof(m));
782 
783  /*
784  * Set the target address we're sending to.
785  * Enforce the scope ID for bogus BSDs.
786  */
787  memcpy(&dst, to, sizeof(dst));
788  m.msg_name = &dst;
789  m.msg_namelen = sizeof(dst);
790  ifindex = if_nametoindex(interface->name);
791  if (no_global_v6_socket)
792  dst.sin6_scope_id = ifindex;
793 
794  /*
795  * Set the data buffer we're sending. (Using this wacky
796  * "scatter-gather" stuff... we only have a single chunk
797  * of data to send, so we declare a single vector entry.)
798  */
799  v.iov_base = (char *)raw;
800  v.iov_len = len;
801  m.msg_iov = &v;
802  m.msg_iovlen = 1;
803 
804  /*
805  * Setting the interface is a bit more involved.
806  *
807  * We have to create a "control message", and set that to
808  * define the IPv6 packet information. We could set the
809  * source address if we wanted, but we can safely let the
810  * kernel decide what that should be.
811  */
812  m.msg_control = control_buf;
813  m.msg_controllen = control_buf_len;
814  cmsg = CMSG_FIRSTHDR(&m);
815  INSIST(cmsg != NULL);
816  cmsg->cmsg_level = IPPROTO_IPV6;
817  cmsg->cmsg_type = IPV6_PKTINFO;
818  cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
819  pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
820  memset(pktinfo, 0, sizeof(*pktinfo));
821  pktinfo->ipi6_ifindex = ifindex;
822  m.msg_controllen = cmsg->cmsg_len;
823 
824  result = sendmsg(interface->wfdesc, &m, 0);
825  if (result < 0) {
826  log_error("send_packet6: %m");
827  }
828  return result;
829 }
830 #endif /* DHCPv6 */
831 
832 #ifdef USE_SOCKET_RECEIVE
833 ssize_t receive_packet (interface, buf, len, from, hfrom)
834  struct interface_info *interface;
835  unsigned char *buf;
836  size_t len;
837  struct sockaddr_in *from;
838  struct hardware *hfrom;
839 {
840 #if !(defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO))
841  SOCKLEN_T flen = sizeof *from;
842 #endif
843  int result;
844 
845  /*
846  * The normal Berkeley socket interface doesn't give us any way
847  * to know what hardware interface we received the message on,
848  * but we should at least make sure the structure is emptied.
849  */
850  memset(hfrom, 0, sizeof(*hfrom));
851 
852 #ifdef IGNORE_HOSTUNREACH
853  int retry = 0;
854  do {
855 #endif
856 
857 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
858  struct msghdr m;
859  struct iovec v;
860  struct cmsghdr *cmsg;
861  struct in_pktinfo *pktinfo;
862  unsigned int ifindex;
863 
864  /*
865  * If necessary allocate space for the control message header.
866  * The space is common between send and receive.
867  */
868  if (control_buf == NULL) {
869  allocate_cmsg_cbuf();
870  if (control_buf == NULL) {
871  log_error("receive_packet: unable to allocate cmsg "
872  "header");
873  return(ENOMEM);
874  }
875  }
876  memset(control_buf, 0, control_buf_len);
877 
878  /*
879  * Initialize our message header structure.
880  */
881  memset(&m, 0, sizeof(m));
882 
883  /*
884  * Point so we can get the from address.
885  */
886  m.msg_name = from;
887  m.msg_namelen = sizeof(*from);
888 
889  /*
890  * Set the data buffer we're receiving. (Using this wacky
891  * "scatter-gather" stuff... but we that doesn't really make
892  * sense for us, so we use a single vector entry.)
893  */
894  v.iov_base = buf;
895  v.iov_len = len;
896  m.msg_iov = &v;
897  m.msg_iovlen = 1;
898 
899  /*
900  * Getting the interface is a bit more involved.
901  *
902  * We set up some space for a "control message". We have
903  * previously asked the kernel to give us packet
904  * information (when we initialized the interface), so we
905  * should get the interface index from that.
906  */
907  m.msg_control = control_buf;
908  m.msg_controllen = control_buf_len;
909 
910  result = recvmsg(interface->rfdesc, &m, 0);
911 
912  if (result >= 0) {
913  /*
914  * If we did read successfully, then we need to loop
915  * through the control messages we received and
916  * find the one with our inteface index.
917  */
918  cmsg = CMSG_FIRSTHDR(&m);
919  while (cmsg != NULL) {
920  if ((cmsg->cmsg_level == IPPROTO_IP) &&
921  (cmsg->cmsg_type == IP_PKTINFO)) {
922  pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
923  ifindex = pktinfo->ipi_ifindex;
924  /*
925  * We pass the ifindex back to the caller
926  * using the unused hfrom parameter avoiding
927  * interface changes between sockets and
928  * the discover code.
929  */
930  memcpy(hfrom->hbuf, &ifindex, sizeof(ifindex));
931  return (result);
932  }
933  cmsg = CMSG_NXTHDR(&m, cmsg);
934  }
935 
936  /*
937  * We didn't find the necessary control message
938  * flag it as an error
939  */
940  result = -1;
941  errno = EIO;
942  }
943 #else
944  result = recvfrom(interface -> rfdesc, (char *)buf, len, 0,
945  (struct sockaddr *)from, &flen);
946 #endif /* IP_PKTINFO ... */
947 #ifdef IGNORE_HOSTUNREACH
948  } while (result < 0 &&
949  (errno == EHOSTUNREACH ||
950  errno == ECONNREFUSED) &&
951  retry++ < 10);
952 #endif
953  return (result);
954 }
955 
956 #endif /* USE_SOCKET_RECEIVE */
957 
958 #ifdef DHCPv6
959 ssize_t
960 receive_packet6(struct interface_info *interface,
961  unsigned char *buf, size_t len,
962  struct sockaddr_in6 *from, struct in6_addr *to_addr,
963  unsigned int *if_idx)
964 {
965  struct msghdr m;
966  struct iovec v;
967  int result;
968  struct cmsghdr *cmsg;
969  struct in6_pktinfo *pktinfo;
970 
971  /*
972  * If necessary allocate space for the control message header.
973  * The space is common between send and receive.
974  */
975  if (control_buf == NULL) {
976  allocate_cmsg_cbuf();
977  if (control_buf == NULL) {
978  log_error("receive_packet6: unable to allocate cmsg "
979  "header");
980  return(ENOMEM);
981  }
982  }
983  memset(control_buf, 0, control_buf_len);
984 
985  /*
986  * Initialize our message header structure.
987  */
988  memset(&m, 0, sizeof(m));
989 
990  /*
991  * Point so we can get the from address.
992  */
993  m.msg_name = from;
994  m.msg_namelen = sizeof(*from);
995 
996  /*
997  * Set the data buffer we're receiving. (Using this wacky
998  * "scatter-gather" stuff... but we that doesn't really make
999  * sense for us, so we use a single vector entry.)
1000  */
1001  v.iov_base = buf;
1002  v.iov_len = len;
1003  m.msg_iov = &v;
1004  m.msg_iovlen = 1;
1005 
1006  /*
1007  * Getting the interface is a bit more involved.
1008  *
1009  * We set up some space for a "control message". We have
1010  * previously asked the kernel to give us packet
1011  * information (when we initialized the interface), so we
1012  * should get the destination address from that.
1013  */
1014  m.msg_control = control_buf;
1015  m.msg_controllen = control_buf_len;
1016 
1017  result = recvmsg(interface->rfdesc, &m, 0);
1018 
1019  if (result >= 0) {
1020  /*
1021  * If we did read successfully, then we need to loop
1022  * through the control messages we received and
1023  * find the one with our destination address.
1024  */
1025  cmsg = CMSG_FIRSTHDR(&m);
1026  while (cmsg != NULL) {
1027  if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
1028  (cmsg->cmsg_type == IPV6_PKTINFO)) {
1029  pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
1030  *to_addr = pktinfo->ipi6_addr;
1031  *if_idx = pktinfo->ipi6_ifindex;
1032 
1033  return (result);
1034  }
1035  cmsg = CMSG_NXTHDR(&m, cmsg);
1036  }
1037 
1038  /*
1039  * We didn't find the necessary control message
1040  * flag is as an error
1041  */
1042  result = -1;
1043  errno = EIO;
1044  }
1045 
1046  return (result);
1047 }
1048 #endif /* DHCPv6 */
1049 
1050 #if defined (USE_SOCKET_FALLBACK)
1051 /* This just reads in a packet and silently discards it. */
1052 
1053 isc_result_t fallback_discard (object)
1054  omapi_object_t *object;
1055 {
1056  char buf [1540];
1057  struct sockaddr_in from;
1058  SOCKLEN_T flen = sizeof from;
1059  int status;
1060  struct interface_info *interface;
1061 
1062  if (object -> type != dhcp_type_interface)
1063  return DHCP_R_INVALIDARG;
1064  interface = (struct interface_info *)object;
1065 
1066  status = recvfrom (interface -> wfdesc, buf, sizeof buf, 0,
1067  (struct sockaddr *)&from, &flen);
1068 #if defined (DEBUG)
1069  /* Only report fallback discard errors if we're debugging. */
1070  if (status < 0) {
1071  log_error ("fallback_discard: %m");
1072  return ISC_R_UNEXPECTED;
1073  }
1074 #else
1075  /* ignore the fact that status value is never used */
1076  IGNORE_UNUSED(status);
1077 #endif
1078  return ISC_R_SUCCESS;
1079 }
1080 #endif /* USE_SOCKET_FALLBACK */
1081 
1082 #if defined (USE_SOCKET_SEND)
1084  struct interface_info *ip;
1085 {
1086  return 0;
1087 }
1088 
1090  struct interface_info *ip;
1091 {
1092 #if defined (SOCKET_CAN_RECEIVE_UNICAST_UNCONFIGURED)
1093  return 1;
1094 #else
1095  return 0;
1096 #endif
1097 }
1098 
1100  struct interface_info *ip;
1101 {
1102 #if defined(SO_BINDTODEVICE) || \
1103  (defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && \
1104  defined(USE_V4_PKTINFO))
1105  return(1);
1106 #else
1107  return(0);
1108 #endif
1109 }
1110 
1111 /* If we have SO_BINDTODEVICE, set up a fallback interface; otherwise,
1112  do not. */
1113 
1114 void maybe_setup_fallback ()
1115 {
1116 #if defined (USE_SOCKET_FALLBACK)
1117  isc_result_t status;
1118  struct interface_info *fbi = (struct interface_info *)0;
1119  if (setup_fallback (&fbi, MDL)) {
1120  fbi -> wfdesc = if_register_socket (fbi, AF_INET, 0, NULL);
1121  fbi -> rfdesc = fbi -> wfdesc;
1122  log_info ("Sending on Socket/%s%s%s",
1123  fbi -> name,
1124  (fbi -> shared_network ? "/" : ""),
1125  (fbi -> shared_network ?
1126  fbi -> shared_network -> name : ""));
1127 
1128  status = omapi_register_io_object ((omapi_object_t *)fbi,
1129  if_readsocket, 0,
1130  fallback_discard, 0, 0);
1131  if (status != ISC_R_SUCCESS)
1132  log_fatal ("Can't register I/O handle for %s: %s",
1133  fbi -> name, isc_result_totext (status));
1134  interface_dereference (&fbi, MDL);
1135  }
1136 #endif
1137 }
1138 
1139 
1140 #if defined(sun) && defined(USE_V4_PKTINFO)
1141 /* This code assumes the existence of SIOCGLIFHWADDR */
1142 void
1143 get_hw_addr(const char *name, struct hardware *hw) {
1144  struct sockaddr_dl *dladdrp;
1145  int sock, i;
1146  struct lifreq lifr;
1147 
1148  memset(&lifr, 0, sizeof (lifr));
1149  (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1150  /*
1151  * Check if the interface is a virtual or IPMP interface - in those
1152  * cases it has no hw address, so generate a random one.
1153  */
1154  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
1155  ioctl(sock, SIOCGLIFFLAGS, &lifr) < 0) {
1156  if (sock != -1)
1157  (void) close(sock);
1158 
1159 #ifdef DHCPv6
1160  /*
1161  * If approrpriate try this with an IPv6 socket
1162  */
1163  if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0 &&
1164  ioctl(sock, SIOCGLIFFLAGS, &lifr) >= 0) {
1165  goto flag_check;
1166  }
1167  if (sock != -1)
1168  (void) close(sock);
1169 #endif
1170  log_fatal("Couldn't get interface flags for %s: %m", name);
1171 
1172  }
1173 
1174  flag_check:
1175  if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP)) {
1176  hw->hlen = sizeof (hw->hbuf);
1177  srandom((long)gethrtime());
1178 
1179  hw->hbuf[0] = HTYPE_IPMP;
1180  for (i = 1; i < hw->hlen; ++i) {
1181  hw->hbuf[i] = random() % 256;
1182  }
1183 
1184  if (sock != -1)
1185  (void) close(sock);
1186  return;
1187  }
1188 
1189  if (ioctl(sock, SIOCGLIFHWADDR, &lifr) < 0)
1190  log_fatal("Couldn't get interface hardware address for %s: %m",
1191  name);
1192  dladdrp = (struct sockaddr_dl *)&lifr.lifr_addr;
1193  hw->hlen = dladdrp->sdl_alen+1;
1194  switch (dladdrp->sdl_type) {
1195  case DL_CSMACD: /* IEEE 802.3 */
1196  case DL_ETHER:
1197  hw->hbuf[0] = HTYPE_ETHER;
1198  break;
1199  case DL_TPR:
1200  hw->hbuf[0] = HTYPE_IEEE802;
1201  break;
1202  case DL_FDDI:
1203  hw->hbuf[0] = HTYPE_FDDI;
1204  break;
1205  case DL_IB:
1206  hw->hbuf[0] = HTYPE_INFINIBAND;
1207  break;
1208  default:
1209  log_fatal("%s: unsupported DLPI MAC type %lu", name,
1210  (unsigned long)dladdrp->sdl_type);
1211  }
1212 
1213  memcpy(hw->hbuf+1, LLADDR(dladdrp), hw->hlen-1);
1214 
1215  if (sock != -1)
1216  (void) close(sock);
1217 }
1218 #endif /* defined(sun) */
1219 
1220 #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:186
struct shared_network * shared_network
Definition: dhcpd.h:1243
u_int8_t hlen
Definition: dhcpd.h:440
int if_readsocket(omapi_object_t *h)
Definition: discover.c:961
char name[IFNAMSIZ]
Definition: dhcpd.h:1267
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:1247
int setup_fallback(struct interface_info **fp, const char *file, int line)
Definition: discover.c:972
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:87
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:68
int int log_info(const char *,...) __attribute__((__format__(__printf__
u_int32_t flags
Definition: dhcpd.h:1281
int v6address_count
Definition: dhcpd.h:1254
void if_register6(struct interface_info *info, int do_multicast)
int local_family
Definition: discover.c:52
int quiet_interface_discovery
Definition: discover.c:42
#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:1250
#define INTERFACE_UPSTREAM
Definition: dhcpd.h:1286
struct in_addr local_address
Definition: discover.c:53
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:933
#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:1287
struct ifreq * ifp
Definition: dhcpd.h:1277
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:1252