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