blob: 1a9780ddc205b737335f025bafcb3fab21672952 [file] [log] [blame]
The Android Open Source Projectf7c54212009-03-03 19:29:22 -08001/*
2 * dhcpcd - DHCP client daemon
Dmitry Shmidta3a22602012-07-23 16:45:46 -07003 * Copyright (c) 2006-2011 Roy Marples <roy@marples.name>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -08004 * All rights reserved
5
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.
14 *
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.
26 */
27
28#include <sys/types.h>
29#include <sys/ioctl.h>
Dmitry Shmidte86eee12011-01-24 16:27:51 -080030#include <sys/param.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080031#include <sys/socket.h>
32#include <sys/time.h>
33
Dmitry Shmidte86eee12011-01-24 16:27:51 -080034#include <arpa/inet.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080035#include <net/if.h>
36#include <net/if_arp.h>
Dmitry Shmidte86eee12011-01-24 16:27:51 -080037#ifdef AF_LINK
38# include <net/if_dl.h>
39# include <net/if_types.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080040#endif
Dmitry Shmidte86eee12011-01-24 16:27:51 -080041#include <netinet/in_systm.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080042#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
Dmitry Shmidte86eee12011-01-24 16:27:51 -080047#ifdef AF_PACKET
48# include <netpacket/packet.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080049#endif
Dmitry Shmidte86eee12011-01-24 16:27:51 -080050#ifdef SIOCGIFMEDIA
51# include <net/if_media.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080052#endif
53
54#include <ctype.h>
55#include <errno.h>
Dmitry Shmidte86eee12011-01-24 16:27:51 -080056#include <ifaddrs.h>
57#include <fnmatch.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080058#include <stddef.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Dmitry Shmidte86eee12011-01-24 16:27:51 -080062#include <syslog.h>
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080063#include <unistd.h>
64
65#include "config.h"
66#include "common.h"
67#include "dhcp.h"
Dmitry Shmidte86eee12011-01-24 16:27:51 -080068#include "if-options.h"
Dmitry Shmidta3a22602012-07-23 16:45:46 -070069#include "ipv6rs.h"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080070#include "net.h"
71#include "signals.h"
72
Dmitry Shmidte86eee12011-01-24 16:27:51 -080073static char hwaddr_buffer[(HWADDR_LEN * 3) + 1];
74
75int socket_afnet = -1;
76
Dmitry Shmidta3a22602012-07-23 16:45:46 -070077#if defined(__FreeBSD__) && defined(DEBUG_MEMORY)
78/* FreeBSD does not zero the struct, causing valgrind errors */
79unsigned int
80if_nametoindex(const char *ifname)
81{
82 struct ifreq ifr;
83
84 memset(&ifr, 0, sizeof(ifr));
85 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
86 if (ioctl(socket_afnet, SIOCGIFINDEX, &ifr) != -1)
87 return ifr.ifr_index;
88 return 0;
89}
90#endif
91
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080092int
93inet_ntocidr(struct in_addr address)
94{
95 int cidr = 0;
96 uint32_t mask = htonl(address.s_addr);
97
98 while (mask) {
99 cidr++;
100 mask <<= 1;
101 }
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800102 return cidr;
103}
104
105int
106inet_cidrtoaddr(int cidr, struct in_addr *addr)
107{
108 int ocets;
109
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800110 if (cidr < 1 || cidr > 32) {
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800111 errno = EINVAL;
112 return -1;
113 }
114 ocets = (cidr + 7) / 8;
115
116 addr->s_addr = 0;
117 if (ocets > 0) {
118 memset(&addr->s_addr, 255, (size_t)ocets - 1);
119 memset((unsigned char *)&addr->s_addr + (ocets - 1),
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800120 (256 - (1 << (32 - cidr) % 8)), 1);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800121 }
122
123 return 0;
124}
125
126uint32_t
127get_netmask(uint32_t addr)
128{
129 uint32_t dst;
130
131 if (addr == 0)
132 return 0;
133
134 dst = htonl(addr);
135 if (IN_CLASSA(dst))
136 return ntohl(IN_CLASSA_NET);
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800137 if (IN_CLASSB(dst))
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800138 return ntohl(IN_CLASSB_NET);
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800139 if (IN_CLASSC(dst))
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800140 return ntohl(IN_CLASSC_NET);
141
142 return 0;
143}
144
145char *
146hwaddr_ntoa(const unsigned char *hwaddr, size_t hwlen)
147{
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800148 char *p = hwaddr_buffer;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800149 size_t i;
150
151 for (i = 0; i < hwlen && i < HWADDR_LEN; i++) {
152 if (i > 0)
153 *p ++= ':';
154 p += snprintf(p, 3, "%.2x", hwaddr[i]);
155 }
156
157 *p ++= '\0';
158
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800159 return hwaddr_buffer;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800160}
161
162size_t
163hwaddr_aton(unsigned char *buffer, const char *addr)
164{
165 char c[3];
166 const char *p = addr;
167 unsigned char *bp = buffer;
168 size_t len = 0;
169
170 c[2] = '\0';
171 while (*p) {
172 c[0] = *p++;
173 c[1] = *p++;
174 /* Ensure that digits are hex */
175 if (isxdigit((unsigned char)c[0]) == 0 ||
176 isxdigit((unsigned char)c[1]) == 0)
177 {
178 errno = EINVAL;
179 return 0;
180 }
181 /* We should have at least two entries 00:01 */
182 if (len == 0 && *p == '\0') {
183 errno = EINVAL;
184 return 0;
185 }
186 /* Ensure that next data is EOL or a seperator with data */
187 if (!(*p == '\0' || (*p == ':' && *(p + 1) != '\0'))) {
188 errno = EINVAL;
189 return 0;
190 }
191 if (*p)
192 p++;
193 if (bp)
194 *bp++ = (unsigned char)strtol(c, NULL, 16);
195 len++;
196 }
197 return len;
198}
199
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800200struct interface *
201init_interface(const char *ifname)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800202{
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800203 struct ifreq ifr;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800204 struct interface *iface = NULL;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800205
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800206 memset(&ifr, 0, sizeof(ifr));
207 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800208 if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == -1)
209 goto eexit;
210
211 iface = xzalloc(sizeof(*iface));
212 strlcpy(iface->name, ifname, sizeof(iface->name));
213 iface->flags = ifr.ifr_flags;
214 /* We reserve the 100 range for virtual interfaces, if and when
215 * we can work them out. */
216 iface->metric = 200 + if_nametoindex(iface->name);
217 if (getifssid(ifname, iface->ssid) != -1) {
218 iface->wireless = 1;
219 iface->metric += 100;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800220 }
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800221
222 if (ioctl(socket_afnet, SIOCGIFMTU, &ifr) == -1)
223 goto eexit;
224 /* Ensure that the MTU is big enough for DHCP */
225 if (ifr.ifr_mtu < MTU_MIN) {
226 ifr.ifr_mtu = MTU_MIN;
227 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
228 if (ioctl(socket_afnet, SIOCSIFMTU, &ifr) == -1)
229 goto eexit;
230 }
231
232 snprintf(iface->leasefile, sizeof(iface->leasefile),
233 LEASEFILE, ifname);
234 /* 0 is a valid fd, so init to -1 */
235 iface->raw_fd = -1;
236 iface->udp_fd = -1;
237 iface->arp_fd = -1;
238 goto exit;
239
240eexit:
241 free(iface);
242 iface = NULL;
243exit:
244 return iface;
245}
246
247void
248free_interface(struct interface *iface)
249{
250 if (!iface)
251 return;
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700252 ipv6rs_free(iface);
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800253 if (iface->state) {
254 free_options(iface->state->options);
255 free(iface->state->old);
256 free(iface->state->new);
257 free(iface->state->offer);
258 free(iface->state);
259 }
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700260 free(iface->buffer);
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800261 free(iface->clientid);
262 free(iface);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800263}
264
265int
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800266carrier_status(struct interface *iface)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800267{
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800268 int ret;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800269 struct ifreq ifr;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800270#ifdef SIOCGIFMEDIA
271 struct ifmediareq ifmr;
272#endif
273#ifdef __linux__
274 char *p;
275#endif
276
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800277 memset(&ifr, 0, sizeof(ifr));
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800278 strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800279#ifdef __linux__
280 /* We can only test the real interface up */
281 if ((p = strchr(ifr.ifr_name, ':')))
282 *p = '\0';
283#endif
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800284
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800285 if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == -1)
286 return -1;
287 iface->flags = ifr.ifr_flags;
288
289 ret = -1;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800290#ifdef SIOCGIFMEDIA
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800291 memset(&ifmr, 0, sizeof(ifmr));
292 strlcpy(ifmr.ifm_name, iface->name, sizeof(ifmr.ifm_name));
293 if (ioctl(socket_afnet, SIOCGIFMEDIA, &ifmr) != -1 &&
294 ifmr.ifm_status & IFM_AVALID)
295 ret = (ifmr.ifm_status & IFM_ACTIVE) ? 1 : 0;
296#endif
297 if (ret == -1)
298 ret = (ifr.ifr_flags & IFF_RUNNING) ? 1 : 0;
299 return ret;
300}
301
302int
303up_interface(struct interface *iface)
304{
305 struct ifreq ifr;
306 int retval = -1;
307#ifdef __linux__
308 char *p;
309#endif
310
311 memset(&ifr, 0, sizeof(ifr));
312 strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
313#ifdef __linux__
314 /* We can only bring the real interface up */
315 if ((p = strchr(ifr.ifr_name, ':')))
316 *p = '\0';
317#endif
318 if (ioctl(socket_afnet, SIOCGIFFLAGS, &ifr) == 0) {
319 if ((ifr.ifr_flags & IFF_UP))
320 retval = 0;
321 else {
322 ifr.ifr_flags |= IFF_UP;
323 if (ioctl(socket_afnet, SIOCSIFFLAGS, &ifr) == 0)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800324 retval = 0;
325 }
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800326 iface->flags = ifr.ifr_flags;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800327 }
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800328 return retval;
329}
330
331struct interface *
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800332discover_interfaces(int argc, char * const *argv)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800333{
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800334 struct ifaddrs *ifaddrs, *ifa;
335 char *p;
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700336 int i, sdl_type;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800337 struct interface *ifp, *ifs, *ifl;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800338#ifdef __linux__
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800339 char ifn[IF_NAMESIZE];
340#endif
341#ifdef AF_LINK
342 const struct sockaddr_dl *sdl;
343#ifdef IFLR_ACTIVE
344 struct if_laddrreq iflr;
345 int socket_aflink;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800346
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800347 socket_aflink = socket(AF_LINK, SOCK_DGRAM, 0);
348 if (socket_aflink == -1)
349 return NULL;
350 memset(&iflr, 0, sizeof(iflr));
351#endif
352#elif AF_PACKET
353 const struct sockaddr_ll *sll;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800354#endif
355
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800356 if (getifaddrs(&ifaddrs) == -1)
357 return NULL;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800358
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800359 ifs = ifl = NULL;
360 for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
361 if (ifa->ifa_addr != NULL) {
362#ifdef AF_LINK
363 if (ifa->ifa_addr->sa_family != AF_LINK)
364 continue;
365#elif AF_PACKET
366 if (ifa->ifa_addr->sa_family != AF_PACKET)
367 continue;
368#endif
369 }
370
371 /* It's possible for an interface to have >1 AF_LINK.
372 * For our purposes, we use the first one. */
373 for (ifp = ifs; ifp; ifp = ifp->next)
374 if (strcmp(ifp->name, ifa->ifa_name) == 0)
375 break;
376 if (ifp)
377 continue;
378 if (argc > 0) {
379 for (i = 0; i < argc; i++) {
380#ifdef __linux__
381 /* Check the real interface name */
382 strlcpy(ifn, argv[i], sizeof(ifn));
383 p = strchr(ifn, ':');
384 if (p)
385 *p = '\0';
386 if (strcmp(ifn, ifa->ifa_name) == 0)
387 break;
388#else
389 if (strcmp(argv[i], ifa->ifa_name) == 0)
390 break;
391#endif
392 }
393 if (i == argc)
394 continue;
395 p = argv[i];
396 } else {
397 /* -1 means we're discovering against a specific
398 * interface, but we still need the below rules
399 * to apply. */
400 if (argc == -1 && strcmp(argv[0], ifa->ifa_name) != 0)
401 continue;
402 for (i = 0; i < ifdc; i++)
403 if (!fnmatch(ifdv[i], ifa->ifa_name, 0))
404 break;
405 if (i < ifdc)
406 continue;
407 for (i = 0; i < ifac; i++)
408 if (!fnmatch(ifav[i], ifa->ifa_name, 0))
409 break;
410 if (ifac && i == ifac)
411 continue;
412 p = ifa->ifa_name;
413 }
414 if ((ifp = init_interface(p)) == NULL)
415 continue;
416
417 /* Bring the interface up if not already */
418 if (!(ifp->flags & IFF_UP)
419#ifdef SIOCGIFMEDIA
420 && carrier_status(ifp) != -1
421#endif
422 )
423 {
424 if (up_interface(ifp) == 0)
425 options |= DHCPCD_WAITUP;
426 else
427 syslog(LOG_ERR, "%s: up_interface: %m", ifp->name);
428 }
429
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700430 sdl_type = 0;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800431 /* Don't allow loopback unless explicit */
432 if (ifp->flags & IFF_LOOPBACK) {
433 if (argc == 0 && ifac == 0) {
434 free_interface(ifp);
435 continue;
436 }
437 } else if (ifa->ifa_addr != NULL) {
438#ifdef AF_LINK
439 sdl = (const struct sockaddr_dl *)(void *)ifa->ifa_addr;
440
441#ifdef IFLR_ACTIVE
442 /* We need to check for active address */
443 strlcpy(iflr.iflr_name, ifp->name,
444 sizeof(iflr.iflr_name));
445 memcpy(&iflr.addr, ifa->ifa_addr,
446 MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
447 iflr.flags = IFLR_PREFIX;
448 iflr.prefixlen = sdl->sdl_alen * NBBY;
449 if (ioctl(socket_aflink, SIOCGLIFADDR, &iflr) == -1 ||
450 !(iflr.flags & IFLR_ACTIVE))
451 {
452 free_interface(ifp);
453 continue;
454 }
455#endif
456
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700457 sdl_type = sdl->sdl_type;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800458 switch(sdl->sdl_type) {
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700459 case IFT_BRIDGE: /* FALLTHROUGH */
460 case IFT_L2VLAN: /* FALLTHOUGH */
461 case IFT_L3IPVLAN: /* FALLTHROUGH */
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800462 case IFT_ETHER:
463 ifp->family = ARPHRD_ETHER;
464 break;
465 case IFT_IEEE1394:
466 ifp->family = ARPHRD_IEEE1394;
467 break;
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700468#ifdef IFT_INFINIBAND
469 case IFT_INFINIBAND:
470 ifp->family = ARPHRD_INFINIBAND;
471 break;
472#endif
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800473 }
474 ifp->hwlen = sdl->sdl_alen;
475#ifndef CLLADDR
476# define CLLADDR(s) ((const char *)((s)->sdl_data + (s)->sdl_nlen))
477#endif
478 memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
479#elif AF_PACKET
480 sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr;
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700481 ifp->family = sdl_type = sll->sll_hatype;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800482 ifp->hwlen = sll->sll_halen;
483 if (ifp->hwlen != 0)
484 memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
485#endif
486 }
487
488 /* We only work on ethernet by default */
489 if (!(ifp->flags & IFF_POINTOPOINT) &&
490 ifp->family != ARPHRD_ETHER)
491 {
492 if (argc == 0 && ifac == 0) {
493 free_interface(ifp);
494 continue;
495 }
496 switch (ifp->family) {
497 case ARPHRD_IEEE1394: /* FALLTHROUGH */
498 case ARPHRD_INFINIBAND:
499 /* We don't warn for supported families */
500 break;
501 default:
502 syslog(LOG_WARNING,
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700503 "%s: unsupported interface type %.2x"
504 ", falling back to ethernet",
505 ifp->name, sdl_type);
506 ifp->family = ARPHRD_ETHER;
507 break;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800508 }
509 }
510
511 /* Handle any platform init for the interface */
512 if (if_init(ifp) == -1) {
513 syslog(LOG_ERR, "%s: if_init: %m", p);
514 free_interface(ifp);
515 continue;
516 }
517
518 if (ifl)
519 ifl->next = ifp;
520 else
521 ifs = ifp;
522 ifl = ifp;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800523 }
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800524 freeifaddrs(ifaddrs);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800525
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800526#ifdef IFLR_ACTIVE
527 close(socket_aflink);
528#endif
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800529
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800530 return ifs;
531}
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800532
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800533int
534do_address(const char *ifname,
535 struct in_addr *addr, struct in_addr *net, struct in_addr *dst, int act)
536{
537 struct ifaddrs *ifaddrs, *ifa;
538 const struct sockaddr_in *a, *n, *d;
539 int retval;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800540
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800541 if (getifaddrs(&ifaddrs) == -1)
542 return -1;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800543
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800544 retval = 0;
545 for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
546 if (ifa->ifa_addr == NULL ||
547 ifa->ifa_addr->sa_family != AF_INET ||
548 strcmp(ifa->ifa_name, ifname) != 0)
549 continue;
550 a = (const struct sockaddr_in *)(void *)ifa->ifa_addr;
551 n = (const struct sockaddr_in *)(void *)ifa->ifa_netmask;
552 if (ifa->ifa_flags & IFF_POINTOPOINT)
553 d = (const struct sockaddr_in *)(void *)
554 ifa->ifa_dstaddr;
555 else
556 d = NULL;
557 if (act == 1) {
558 addr->s_addr = a->sin_addr.s_addr;
559 net->s_addr = n->sin_addr.s_addr;
560 if (dst) {
Dmitry Shmidt49b71a92011-06-14 16:45:07 -0700561 /* TODO: Fix getifaddrs() */
562 if ((ifa->ifa_flags & IFF_POINTOPOINT) && d)
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800563 dst->s_addr = d->sin_addr.s_addr;
564 else
565 dst->s_addr = INADDR_ANY;
566 }
567 retval = 1;
568 break;
569 }
570 if (addr->s_addr == a->sin_addr.s_addr &&
571 (net == NULL || net->s_addr == n->sin_addr.s_addr))
572 {
573 retval = 1;
574 break;
575 }
576 }
577 freeifaddrs(ifaddrs);
578 return retval;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800579}
580
581int
582do_mtu(const char *ifname, short int mtu)
583{
584 struct ifreq ifr;
585 int r;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800586
587 memset(&ifr, 0, sizeof(ifr));
588 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
589 ifr.ifr_mtu = mtu;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800590 r = ioctl(socket_afnet, mtu ? SIOCSIFMTU : SIOCGIFMTU, &ifr);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800591 if (r == -1)
592 return -1;
593 return ifr.ifr_mtu;
594}
595
596void
597free_routes(struct rt *routes)
598{
599 struct rt *r;
600
601 while (routes) {
602 r = routes->next;
603 free(routes);
604 routes = r;
605 }
606}
607
608int
609open_udp_socket(struct interface *iface)
610{
611 int s;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800612 struct sockaddr_in sin;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800613 int n;
614#ifdef SO_BINDTODEVICE
615 struct ifreq ifr;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800616 char *p;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800617#endif
618
619 if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
620 return -1;
621
622 n = 1;
623 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1)
624 goto eexit;
625#ifdef SO_BINDTODEVICE
626 memset(&ifr, 0, sizeof(ifr));
627 strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800628 /* We can only bind to the real device */
629 p = strchr(ifr.ifr_name, ':');
630 if (p)
631 *p = '\0';
632 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, &ifr,
633 sizeof(ifr)) == -1)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800634 goto eexit;
635#endif
636 /* As we don't use this socket for receiving, set the
637 * receive buffer to 1 */
638 n = 1;
639 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
640 goto eexit;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800641 memset(&sin, 0, sizeof(sin));
642 sin.sin_family = AF_INET;
643 sin.sin_port = htons(DHCP_CLIENT_PORT);
644 sin.sin_addr.s_addr = iface->addr.s_addr;
645 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800646 goto eexit;
647
648 iface->udp_fd = s;
649 set_cloexec(s);
650 return 0;
651
652eexit:
653 close(s);
654 return -1;
655}
656
657ssize_t
658send_packet(const struct interface *iface, struct in_addr to,
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800659 const uint8_t *data, ssize_t len)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800660{
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800661 struct sockaddr_in sin;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800662
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800663 memset(&sin, 0, sizeof(sin));
664 sin.sin_family = AF_INET;
665 sin.sin_addr.s_addr = to.s_addr;
666 sin.sin_port = htons(DHCP_SERVER_PORT);
667 return sendto(iface->udp_fd, data, len, 0,
668 (struct sockaddr *)&sin, sizeof(sin));
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800669}
670
671struct udp_dhcp_packet
672{
673 struct ip ip;
674 struct udphdr udp;
675 struct dhcp_message dhcp;
676};
677const size_t udp_dhcp_len = sizeof(struct udp_dhcp_packet);
678
679static uint16_t
680checksum(const void *data, uint16_t len)
681{
682 const uint8_t *addr = data;
683 uint32_t sum = 0;
684
685 while (len > 1) {
686 sum += addr[0] * 256 + addr[1];
687 addr += 2;
688 len -= 2;
689 }
690
691 if (len == 1)
692 sum += *addr * 256;
693
694 sum = (sum >> 16) + (sum & 0xffff);
695 sum += (sum >> 16);
696
697 sum = htons(sum);
698
699 return ~sum;
700}
701
702ssize_t
703make_udp_packet(uint8_t **packet, const uint8_t *data, size_t length,
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800704 struct in_addr source, struct in_addr dest)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800705{
706 struct udp_dhcp_packet *udpp;
707 struct ip *ip;
708 struct udphdr *udp;
709
710 udpp = xzalloc(sizeof(*udpp));
711 ip = &udpp->ip;
712 udp = &udpp->udp;
713
714 /* OK, this is important :)
715 * We copy the data to our packet and then create a small part of the
716 * ip structure and an invalid ip_len (basically udp length).
717 * We then fill the udp structure and put the checksum
718 * of the whole packet into the udp checksum.
719 * Finally we complete the ip structure and ip checksum.
720 * If we don't do the ordering like so then the udp checksum will be
721 * broken, so find another way of doing it! */
722
723 memcpy(&udpp->dhcp, data, length);
724
725 ip->ip_p = IPPROTO_UDP;
726 ip->ip_src.s_addr = source.s_addr;
727 if (dest.s_addr == 0)
728 ip->ip_dst.s_addr = INADDR_BROADCAST;
729 else
730 ip->ip_dst.s_addr = dest.s_addr;
731
732 udp->uh_sport = htons(DHCP_CLIENT_PORT);
733 udp->uh_dport = htons(DHCP_SERVER_PORT);
734 udp->uh_ulen = htons(sizeof(*udp) + length);
735 ip->ip_len = udp->uh_ulen;
736 udp->uh_sum = checksum(udpp, sizeof(*udpp));
737
738 ip->ip_v = IPVERSION;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800739 ip->ip_hl = sizeof(*ip) >> 2;
740 ip->ip_id = arc4random() & UINT16_MAX;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800741 ip->ip_ttl = IPDEFTTL;
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800742 ip->ip_len = htons(sizeof(*ip) + sizeof(*udp) + length);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800743 ip->ip_sum = checksum(ip, sizeof(*ip));
744
745 *packet = (uint8_t *)udpp;
746 return sizeof(*ip) + sizeof(*udp) + length;
747}
748
749ssize_t
750get_udp_data(const uint8_t **data, const uint8_t *udp)
751{
752 struct udp_dhcp_packet packet;
753
754 memcpy(&packet, udp, sizeof(packet));
755 *data = udp + offsetof(struct udp_dhcp_packet, dhcp);
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800756 return ntohs(packet.ip.ip_len) -
757 sizeof(packet.ip) -
758 sizeof(packet.udp);
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800759}
760
761int
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700762valid_udp_packet(const uint8_t *data, size_t data_len, struct in_addr *from,
763 int noudpcsum)
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800764{
765 struct udp_dhcp_packet packet;
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800766 uint16_t bytes, udpsum;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800767
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800768 if (data_len < sizeof(packet.ip)) {
769 if (from)
770 from->s_addr = INADDR_ANY;
771 errno = EINVAL;
772 return -1;
773 }
774 memcpy(&packet, data, MIN(data_len, sizeof(packet)));
775 if (from)
776 from->s_addr = packet.ip.ip_src.s_addr;
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800777 if (data_len > sizeof(packet)) {
778 errno = EINVAL;
779 return -1;
780 }
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800781 if (checksum(&packet.ip, sizeof(packet.ip)) != 0) {
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800782 errno = EINVAL;
783 return -1;
784 }
785
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800786 bytes = ntohs(packet.ip.ip_len);
787 if (data_len < bytes) {
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800788 errno = EINVAL;
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800789 return -1;
790 }
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700791
792 if (noudpcsum == 0) {
793 udpsum = packet.udp.uh_sum;
794 packet.udp.uh_sum = 0;
795 packet.ip.ip_hl = 0;
796 packet.ip.ip_v = 0;
797 packet.ip.ip_tos = 0;
798 packet.ip.ip_len = packet.udp.uh_ulen;
799 packet.ip.ip_id = 0;
800 packet.ip.ip_off = 0;
801 packet.ip.ip_ttl = 0;
802 packet.ip.ip_sum = 0;
803 if (udpsum && checksum(&packet, bytes) != udpsum) {
804 errno = EINVAL;
805 return -1;
806 }
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800807 }
808
Dmitry Shmidt938bc382010-01-08 10:47:26 -0800809 return 0;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800810}