Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / dhcpcd / dist / net.c
blob626733b8bbd880b2c613dde676718e16f7f17a90
1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2009 Roy Marples <roy@marples.name>
4 * All rights reserved
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <sys/time.h>
34 #include <arpa/inet.h>
35 #include <net/if.h>
36 #include <net/if_arp.h>
37 #ifdef AF_LINK
38 # include <net/if_dl.h>
39 # include <net/if_types.h>
40 #endif
41 #include <netinet/in_systm.h>
42 #include <netinet/in.h>
43 #include <netinet/ip.h>
44 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
45 #include <netinet/udp.h>
46 #undef __FAVOR_BSD
47 #ifdef AF_PACKET
48 # include <netpacket/packet.h>
49 #endif
50 #ifdef SIOCGIFMEDIA
51 # include <net/if_media.h>
52 #endif
54 #include <ctype.h>
55 #include <errno.h>
56 #include <ifaddrs.h>
57 #include <fnmatch.h>
58 #include <stddef.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <syslog.h>
63 #include <unistd.h>
65 #include "config.h"
66 #include "common.h"
67 #include "dhcp.h"
68 #include "if-options.h"
69 #include "net.h"
70 #include "signals.h"
72 static char hwaddr_buffer[(HWADDR_LEN * 3) + 1];
74 int socket_afnet = -1;
76 int
77 inet_ntocidr(struct in_addr address)
79 int cidr = 0;
80 uint32_t mask = htonl(address.s_addr);
82 while (mask) {
83 cidr++;
84 mask <<= 1;
86 return cidr;
89 int
90 inet_cidrtoaddr(int cidr, struct in_addr *addr)
92 int ocets;
94 if (cidr < 1 || cidr > 32) {
95 errno = EINVAL;
96 return -1;
98 ocets = (cidr + 7) / 8;
100 addr->s_addr = 0;
101 if (ocets > 0) {
102 memset(&addr->s_addr, 255, (size_t)ocets - 1);
103 memset((unsigned char *)&addr->s_addr + (ocets - 1),
104 (256 - (1 << (32 - cidr) % 8)), 1);
107 return 0;
110 uint32_t
111 get_netmask(uint32_t addr)
113 uint32_t dst;
115 if (addr == 0)
116 return 0;
118 dst = htonl(addr);
119 if (IN_CLASSA(dst))
120 return ntohl(IN_CLASSA_NET);
121 if (IN_CLASSB(dst))
122 return ntohl(IN_CLASSB_NET);
123 if (IN_CLASSC(dst))
124 return ntohl(IN_CLASSC_NET);
126 return 0;
129 char *
130 hwaddr_ntoa(const unsigned char *hwaddr, size_t hwlen)
132 char *p = hwaddr_buffer;
133 size_t i;
135 for (i = 0; i < hwlen && i < HWADDR_LEN; i++) {
136 if (i > 0)
137 *p ++= ':';
138 p += snprintf(p, 3, "%.2x", hwaddr[i]);
141 *p ++= '\0';
143 return hwaddr_buffer;
146 size_t
147 hwaddr_aton(unsigned char *buffer, const char *addr)
149 char c[3];
150 const char *p = addr;
151 unsigned char *bp = buffer;
152 size_t len = 0;
154 c[2] = '\0';
155 while (*p) {
156 c[0] = *p++;
157 c[1] = *p++;
158 /* Ensure that digits are hex */
159 if (isxdigit((unsigned char)c[0]) == 0 ||
160 isxdigit((unsigned char)c[1]) == 0)
162 errno = EINVAL;
163 return 0;
165 /* We should have at least two entries 00:01 */
166 if (len == 0 && *p == '\0') {
167 errno = EINVAL;
168 return 0;
170 /* Ensure that next data is EOL or a seperator with data */
171 if (!(*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) {
172 errno = EINVAL;
173 return 0;
175 if (*p)
176 p++;
177 if (bp)
178 *bp++ = (unsigned char)strtol(c, NULL, 16);
179 len++;
181 return len;
184 struct interface *
185 init_interface(const char *ifname)
187 struct ifreq ifr;
188 struct interface *iface = NULL;
190 memset(&ifr, 0, sizeof(ifr));
191 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
192 if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == -1)
193 goto eexit;
195 iface = xzalloc(sizeof(*iface));
196 strlcpy(iface->name, ifname, sizeof(iface->name));
197 iface->flags = ifr.ifr_flags;
198 /* We reserve the 100 range for virtual interfaces, if and when
199 * we can work them out. */
200 iface->metric = 200 + if_nametoindex(iface->name);
201 if (getifssid(ifname, iface->ssid) != -1) {
202 iface->wireless = 1;
203 iface->metric += 100;
206 if (ioctl(socket_afnet, SIOCGIFMTU, &ifr) == -1)
207 goto eexit;
208 /* Ensure that the MTU is big enough for DHCP */
209 if (ifr.ifr_mtu < MTU_MIN) {
210 ifr.ifr_mtu = MTU_MIN;
211 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
212 if (ioctl(socket_afnet, SIOCSIFMTU, &ifr) == -1)
213 goto eexit;
216 snprintf(iface->leasefile, sizeof(iface->leasefile),
217 LEASEFILE, ifname);
218 /* 0 is a valid fd, so init to -1 */
219 iface->raw_fd = -1;
220 iface->udp_fd = -1;
221 iface->arp_fd = -1;
222 goto exit;
224 eexit:
225 free(iface);
226 iface = NULL;
227 exit:
228 return iface;
231 void
232 free_interface(struct interface *iface)
234 if (!iface)
235 return;
236 if (iface->state) {
237 free_options(iface->state->options);
238 free(iface->state->old);
239 free(iface->state->new);
240 free(iface->state->offer);
241 free(iface->state);
243 free(iface->clientid);
244 free(iface);
248 carrier_status(struct interface *iface)
250 int ret;
251 struct ifreq ifr;
252 #ifdef SIOCGIFMEDIA
253 struct ifmediareq ifmr;
254 #endif
255 #ifdef __linux__
256 char *p;
257 #endif
259 memset(&ifr, 0, sizeof(ifr));
260 strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
261 #ifdef __linux__
262 /* We can only test the real interface up */
263 if ((p = strchr(ifr.ifr_name, ':')))
264 *p = '\0';
265 #endif
267 if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == -1)
268 return -1;
269 iface->flags = ifr.ifr_flags;
271 ret = -1;
272 #ifdef SIOCGIFMEDIA
273 memset(&ifmr, 0, sizeof(ifmr));
274 strlcpy(ifmr.ifm_name, iface->name, sizeof(ifmr.ifm_name));
275 if (ioctl(socket_afnet, SIOCGIFMEDIA, &ifmr) != -1 &&
276 ifmr.ifm_status & IFM_AVALID)
277 ret = (ifmr.ifm_status & IFM_ACTIVE) ? 1 : 0;
278 #endif
279 if (ret == -1)
280 ret = (ifr.ifr_flags & IFF_RUNNING) ? 1 : 0;
281 return ret;
285 up_interface(struct interface *iface)
287 struct ifreq ifr;
288 int retval = -1;
289 #ifdef __linux__
290 char *p;
291 #endif
293 memset(&ifr, 0, sizeof(ifr));
294 strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
295 #ifdef __linux__
296 /* We can only bring the real interface up */
297 if ((p = strchr(ifr.ifr_name, ':')))
298 *p = '\0';
299 #endif
300 if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == 0) {
301 if ((ifr.ifr_flags & IFF_UP))
302 retval = 0;
303 else {
304 ifr.ifr_flags |= IFF_UP;
305 if (ioctl(socket_afnet, SIOCSIFFLAGS, &ifr) == 0)
306 retval = 0;
308 iface->flags = ifr.ifr_flags;
310 return retval;
313 struct interface *
314 discover_interfaces(int argc, char * const *argv)
316 struct ifaddrs *ifaddrs, *ifa;
317 char *p;
318 int i;
319 struct interface *ifp, *ifs, *ifl;
320 #ifdef __linux__
321 char ifn[IF_NAMESIZE];
322 #endif
323 #ifdef AF_LINK
324 const struct sockaddr_dl *sdl;
325 #elif AF_PACKET
326 const struct sockaddr_ll *sll;
327 #endif
329 if (getifaddrs(&ifaddrs) == -1)
330 return NULL;
332 ifs = ifl = NULL;
333 for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
334 if (ifa->ifa_addr != NULL) {
335 #ifdef AF_LINK
336 if (ifa->ifa_addr->sa_family != AF_LINK)
337 continue;
338 #elif AF_PACKET
339 if (ifa->ifa_addr->sa_family != AF_PACKET)
340 continue;
341 #endif
344 /* It's possible for an interface to have >1 AF_LINK.
345 * For our purposes, we use the first one. */
346 for (ifp = ifs; ifp; ifp = ifp->next)
347 if (strcmp(ifp->name, ifa->ifa_name) == 0)
348 break;
349 if (ifp)
350 continue;
351 if (argc > 0) {
352 for (i = 0; i < argc; i++) {
353 #ifdef __linux__
354 /* Check the real interface name */
355 strlcpy(ifn, argv[i], sizeof(ifn));
356 p = strchr(ifn, ':');
357 if (p)
358 *p = '\0';
359 if (strcmp(ifn, ifa->ifa_name) == 0)
360 break;
361 #else
362 if (strcmp(argv[i], ifa->ifa_name) == 0)
363 break;
364 #endif
366 if (i == argc)
367 continue;
368 p = argv[i];
369 } else {
370 /* -1 means we're discovering against a specific
371 * interface, but we still need the below rules
372 * to apply. */
373 if (argc == -1 && strcmp(argv[0], ifa->ifa_name) != 0)
374 continue;
375 for (i = 0; i < ifdc; i++)
376 if (!fnmatch(ifdv[i], ifa->ifa_name, 0))
377 break;
378 if (i < ifdc)
379 continue;
380 for (i = 0; i < ifac; i++)
381 if (!fnmatch(ifav[i], ifa->ifa_name, 0))
382 break;
383 if (ifac && i == ifac)
384 continue;
385 p = ifa->ifa_name;
387 if ((ifp = init_interface(p)) == NULL)
388 continue;
390 /* Bring the interface up if not already */
391 if (!(ifp->flags & IFF_UP)
392 #ifdef SIOCGIFMEDIA
393 && carrier_status(ifp) != -1
394 #endif
397 if (up_interface(ifp) == 0)
398 options |= DHCPCD_WAITUP;
399 else
400 syslog(LOG_ERR, "%s: up_interface: %m", ifp->name);
403 /* Don't allow loopback unless explicit */
404 if (ifp->flags & IFF_LOOPBACK) {
405 if (argc == 0 && ifac == 0) {
406 free_interface(ifp);
407 continue;
409 } else if (ifa->ifa_addr != NULL) {
410 #ifdef AF_LINK
411 sdl = (const struct sockaddr_dl *)(void *)ifa->ifa_addr;
412 switch(sdl->sdl_type) {
413 case IFT_ETHER:
414 ifp->family = ARPHRD_ETHER;
415 break;
416 case IFT_IEEE1394:
417 ifp->family = ARPHRD_IEEE1394;
418 break;
420 ifp->hwlen = sdl->sdl_alen;
421 #ifndef CLLADDR
422 # define CLLADDR(s) ((const char *)((s)->sdl_data + (s)->sdl_nlen))
423 #endif
424 memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
425 #elif AF_PACKET
426 sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr;
427 ifp->family = sll->sll_hatype;
428 ifp->hwlen = sll->sll_halen;
429 if (ifp->hwlen != 0)
430 memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
431 #endif
434 /* We only work on ethernet by default */
435 if (!(ifp->flags & IFF_POINTOPOINT) &&
436 ifp->family != ARPHRD_ETHER)
438 if (argc == 0 && ifac == 0) {
439 free_interface(ifp);
440 continue;
442 if (ifp->family != ARPHRD_IEEE1394)
443 syslog(LOG_WARNING,
444 "%s: unknown hardware family", p);
446 if (ifl)
447 ifl->next = ifp;
448 else
449 ifs = ifp;
450 ifl = ifp;
452 freeifaddrs(ifaddrs);
453 return ifs;
457 do_address(const char *ifname,
458 struct in_addr *addr, struct in_addr *net, struct in_addr *dst, int act)
460 struct ifaddrs *ifaddrs, *ifa;
461 const struct sockaddr_in *a, *n, *d;
462 int retval;
464 if (getifaddrs(&ifaddrs) == -1)
465 return -1;
467 retval = 0;
468 for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
469 if (ifa->ifa_addr == NULL ||
470 ifa->ifa_addr->sa_family != AF_INET ||
471 strcmp(ifa->ifa_name, ifname) != 0)
472 continue;
473 a = (const struct sockaddr_in *)(void *)ifa->ifa_addr;
474 n = (const struct sockaddr_in *)(void *)ifa->ifa_netmask;
475 if (ifa->ifa_flags & IFF_POINTOPOINT)
476 d = (const struct sockaddr_in *)(void *)
477 ifa->ifa_dstaddr;
478 else
479 d = NULL;
480 if (act == 1) {
481 addr->s_addr = a->sin_addr.s_addr;
482 net->s_addr = n->sin_addr.s_addr;
483 if (dst && ifa->ifa_flags & IFF_POINTOPOINT)
484 dst->s_addr = d->sin_addr.s_addr;
485 retval = 1;
486 break;
488 if (addr->s_addr == a->sin_addr.s_addr &&
489 (net == NULL || net->s_addr == n->sin_addr.s_addr))
491 retval = 1;
492 break;
495 freeifaddrs(ifaddrs);
496 return retval;
500 do_mtu(const char *ifname, short int mtu)
502 struct ifreq ifr;
503 int r;
505 memset(&ifr, 0, sizeof(ifr));
506 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
507 ifr.ifr_mtu = mtu;
508 r = ioctl(socket_afnet, mtu ? SIOCSIFMTU : SIOCGIFMTU, &ifr);
509 if (r == -1)
510 return -1;
511 return ifr.ifr_mtu;
514 void
515 free_routes(struct rt *routes)
517 struct rt *r;
519 while (routes) {
520 r = routes->next;
521 free(routes);
522 routes = r;
527 open_udp_socket(struct interface *iface)
529 int s;
530 struct sockaddr_in sin;
531 int n;
532 #ifdef SO_BINDTODEVICE
533 struct ifreq ifr;
534 char *p;
535 #endif
537 if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
538 return -1;
540 n = 1;
541 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1)
542 goto eexit;
543 #ifdef SO_BINDTODEVICE
544 memset(&ifr, 0, sizeof(ifr));
545 strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
546 /* We can only bind to the real device */
547 p = strchr(ifr.ifr_name, ':');
548 if (p)
549 *p = '\0';
550 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, &ifr,
551 sizeof(ifr)) == -1)
552 goto eexit;
553 #endif
554 /* As we don't use this socket for receiving, set the
555 * receive buffer to 1 */
556 n = 1;
557 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
558 goto eexit;
559 memset(&sin, 0, sizeof(sin));
560 sin.sin_family = AF_INET;
561 sin.sin_port = htons(DHCP_CLIENT_PORT);
562 sin.sin_addr.s_addr = iface->addr.s_addr;
563 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
564 goto eexit;
566 iface->udp_fd = s;
567 set_cloexec(s);
568 return 0;
570 eexit:
571 close(s);
572 return -1;
575 ssize_t
576 send_packet(const struct interface *iface, struct in_addr to,
577 const uint8_t *data, ssize_t len)
579 struct sockaddr_in sin;
581 memset(&sin, 0, sizeof(sin));
582 sin.sin_family = AF_INET;
583 sin.sin_addr.s_addr = to.s_addr;
584 sin.sin_port = htons(DHCP_SERVER_PORT);
585 return sendto(iface->udp_fd, data, len, 0,
586 (struct sockaddr *)&sin, sizeof(sin));
589 struct udp_dhcp_packet
591 struct ip ip;
592 struct udphdr udp;
593 struct dhcp_message dhcp;
595 const size_t udp_dhcp_len = sizeof(struct udp_dhcp_packet);
597 static uint16_t
598 checksum(const void *data, uint16_t len)
600 const uint8_t *addr = data;
601 uint32_t sum = 0;
603 while (len > 1) {
604 sum += addr[0] * 256 + addr[1];
605 addr += 2;
606 len -= 2;
609 if (len == 1)
610 sum += *addr * 256;
612 sum = (sum >> 16) + (sum & 0xffff);
613 sum += (sum >> 16);
615 sum = htons(sum);
617 return ~sum;
620 ssize_t
621 make_udp_packet(uint8_t **packet, const uint8_t *data, size_t length,
622 struct in_addr source, struct in_addr dest)
624 struct udp_dhcp_packet *udpp;
625 struct ip *ip;
626 struct udphdr *udp;
628 udpp = xzalloc(sizeof(*udpp));
629 ip = &udpp->ip;
630 udp = &udpp->udp;
632 /* OK, this is important :)
633 * We copy the data to our packet and then create a small part of the
634 * ip structure and an invalid ip_len (basically udp length).
635 * We then fill the udp structure and put the checksum
636 * of the whole packet into the udp checksum.
637 * Finally we complete the ip structure and ip checksum.
638 * If we don't do the ordering like so then the udp checksum will be
639 * broken, so find another way of doing it! */
641 memcpy(&udpp->dhcp, data, length);
643 ip->ip_p = IPPROTO_UDP;
644 ip->ip_src.s_addr = source.s_addr;
645 if (dest.s_addr == 0)
646 ip->ip_dst.s_addr = INADDR_BROADCAST;
647 else
648 ip->ip_dst.s_addr = dest.s_addr;
650 udp->uh_sport = htons(DHCP_CLIENT_PORT);
651 udp->uh_dport = htons(DHCP_SERVER_PORT);
652 udp->uh_ulen = htons(sizeof(*udp) + length);
653 ip->ip_len = udp->uh_ulen;
654 udp->uh_sum = checksum(udpp, sizeof(*udpp));
656 ip->ip_v = IPVERSION;
657 ip->ip_hl = 5;
658 ip->ip_id = 0;
659 ip->ip_tos = IPTOS_LOWDELAY;
660 ip->ip_len = htons (sizeof(*ip) + sizeof(*udp) + length);
661 ip->ip_id = 0;
662 ip->ip_off = htons(IP_DF); /* Don't fragment */
663 ip->ip_ttl = IPDEFTTL;
665 ip->ip_sum = checksum(ip, sizeof(*ip));
667 *packet = (uint8_t *)udpp;
668 return sizeof(*ip) + sizeof(*udp) + length;
671 ssize_t
672 get_udp_data(const uint8_t **data, const uint8_t *udp)
674 struct udp_dhcp_packet packet;
676 memcpy(&packet, udp, sizeof(packet));
677 *data = udp + offsetof(struct udp_dhcp_packet, dhcp);
678 return ntohs(packet.ip.ip_len) -
679 sizeof(packet.ip) -
680 sizeof(packet.udp);
684 valid_udp_packet(const uint8_t *data, size_t data_len, struct in_addr *from)
686 struct udp_dhcp_packet packet;
687 uint16_t bytes, udpsum;
689 if (data_len < sizeof(packet.ip)) {
690 if (from)
691 from->s_addr = INADDR_ANY;
692 errno = EINVAL;
693 return -1;
695 memcpy(&packet, data, MIN(data_len, sizeof(packet)));
696 if (from)
697 from->s_addr = packet.ip.ip_src.s_addr;
698 if (data_len > sizeof(packet)) {
699 errno = EINVAL;
700 return -1;
702 if (checksum(&packet.ip, sizeof(packet.ip)) != 0) {
703 errno = EINVAL;
704 return -1;
707 bytes = ntohs(packet.ip.ip_len);
708 if (data_len < bytes) {
709 errno = EINVAL;
710 return -1;
712 udpsum = packet.udp.uh_sum;
713 packet.udp.uh_sum = 0;
714 packet.ip.ip_hl = 0;
715 packet.ip.ip_v = 0;
716 packet.ip.ip_tos = 0;
717 packet.ip.ip_len = packet.udp.uh_ulen;
718 packet.ip.ip_id = 0;
719 packet.ip.ip_off = 0;
720 packet.ip.ip_ttl = 0;
721 packet.ip.ip_sum = 0;
722 if (udpsum && checksum(&packet, bytes) != udpsum) {
723 errno = EINVAL;
724 return -1;
727 return 0;