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