randomconfig fixes
[oweals/busybox.git] / networking / udhcp / d6_socket.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright (C) 2011 Denys Vlasenko.
4  *
5  * Licensed under GPLv2, see file LICENSE in this source tree.
6  */
7 #include "common.h"
8 #include "d6_common.h"
9 #include <net/if.h>
10 #include <ifaddrs.h>
11 #include <netpacket/packet.h>
12
13 int FAST_FUNC d6_read_interface(const char *interface, int *ifindex, struct in6_addr *nip6, uint8_t *mac)
14 {
15         int retval = 3;
16         struct ifaddrs *ifap, *ifa;
17
18         getifaddrs(&ifap);
19
20         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
21                 struct sockaddr_in6 *sip6;
22
23                 if (!ifa->ifa_addr || (strcmp(ifa->ifa_name, interface) != 0))
24                         continue;
25
26                 sip6 = (struct sockaddr_in6*)(ifa->ifa_addr);
27
28                 if (ifa->ifa_addr->sa_family == AF_PACKET) {
29                         struct sockaddr_ll *sll = (struct sockaddr_ll*)(ifa->ifa_addr);
30                         memcpy(mac, sll->sll_addr, 6);
31                         log1("MAC %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
32                         log1("adapter index %d", sll->sll_ifindex);
33                         *ifindex = sll->sll_ifindex;
34                         retval &= (0xf - (1<<0));
35                 }
36 #if 0
37                 if (ifa->ifa_addr->sa_family == AF_INET) {
38                         *nip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
39                         log1("IP %s", inet_ntoa(((struct sockaddr_in *)ifa->ifa_addr)->sin_addr));
40                 }
41 #endif
42                 if (ifa->ifa_addr->sa_family == AF_INET6
43                  && IN6_IS_ADDR_LINKLOCAL(&sip6->sin6_addr)
44                 ) {
45                         *nip6 = sip6->sin6_addr; /* struct copy */
46                         log1(
47                                 "IPv6 %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
48                                 nip6->s6_addr[0], nip6->s6_addr[1],
49                                 nip6->s6_addr[2], nip6->s6_addr[3],
50                                 nip6->s6_addr[4], nip6->s6_addr[5],
51                                 nip6->s6_addr[6], nip6->s6_addr[7],
52                                 nip6->s6_addr[8], nip6->s6_addr[9],
53                                 nip6->s6_addr[10], nip6->s6_addr[11],
54                                 nip6->s6_addr[12], nip6->s6_addr[13],
55                                 nip6->s6_addr[14], nip6->s6_addr[15]
56                         );
57                         retval &= (0xf - (1<<1));
58                 }
59         }
60
61         freeifaddrs(ifap);
62         if (retval == 0)
63                 return retval;
64
65         if (retval & (1<<0))
66                 bb_error_msg("can't get %s", "MAC");
67         if (retval & (1<<1))
68                 bb_error_msg("can't get %s", "link-local IPv6 address");
69         return -1;
70 }
71
72 int FAST_FUNC d6_listen_socket(int port, const char *inf)
73 {
74         int fd;
75         struct sockaddr_in6 addr;
76
77         log1("opening listen socket on *:%d %s", port, inf);
78         fd = xsocket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
79
80         setsockopt_reuseaddr(fd);
81         if (setsockopt_broadcast(fd) == -1)
82                 bb_perror_msg_and_die("SO_BROADCAST");
83
84         /* NB: bug 1032 says this doesn't work on ethernet aliases (ethN:M) */
85         if (setsockopt_bindtodevice(fd, inf))
86                 xfunc_die(); /* warning is already printed */
87
88         memset(&addr, 0, sizeof(addr));
89         addr.sin6_family = AF_INET6;
90         addr.sin6_port = htons(port);
91         /* addr.sin6_addr is all-zeros */
92         xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
93
94         return fd;
95 }