blob: f9082ee9d3a83f432016ecdac2821871fd44c6a0 [file] [log] [blame]
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001/* 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>
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020010#include <ifaddrs.h>
11#include <netpacket/packet.h>
12
13int 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);
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020019 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
20 struct sockaddr_in6 *sip6;
21
22 if (!ifa->ifa_addr || (strcmp(ifa->ifa_name, interface) != 0))
23 continue;
24
25 sip6 = (struct sockaddr_in6*)(ifa->ifa_addr);
26
27 if (ifa->ifa_addr->sa_family == AF_PACKET) {
28 struct sockaddr_ll *sll = (struct sockaddr_ll*)(ifa->ifa_addr);
29 memcpy(mac, sll->sll_addr, 6);
Denys Vlasenkob72f1ef2017-07-24 12:06:53 +020030 log2("MAC %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020031 *ifindex = sll->sll_ifindex;
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010032 log2("ifindex %d", *ifindex);
33 retval &= (3 - (1<<0));
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020034 }
35#if 0
36 if (ifa->ifa_addr->sa_family == AF_INET) {
37 *nip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
38 log1("IP %s", inet_ntoa(((struct sockaddr_in *)ifa->ifa_addr)->sin_addr));
39 }
40#endif
41 if (ifa->ifa_addr->sa_family == AF_INET6
42 && IN6_IS_ADDR_LINKLOCAL(&sip6->sin6_addr)
43 ) {
44 *nip6 = sip6->sin6_addr; /* struct copy */
45 log1(
46 "IPv6 %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
47 nip6->s6_addr[0], nip6->s6_addr[1],
48 nip6->s6_addr[2], nip6->s6_addr[3],
49 nip6->s6_addr[4], nip6->s6_addr[5],
50 nip6->s6_addr[6], nip6->s6_addr[7],
51 nip6->s6_addr[8], nip6->s6_addr[9],
52 nip6->s6_addr[10], nip6->s6_addr[11],
53 nip6->s6_addr[12], nip6->s6_addr[13],
54 nip6->s6_addr[14], nip6->s6_addr[15]
55 );
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010056 retval &= (3 - (1<<1));
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020057 }
58 }
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020059 freeifaddrs(ifap);
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010060
61 if (retval & (1<<0)) {
62 /* This iface has no MAC (e.g. ppp), generate a random one */
63 struct ifreq ifr;
64 int fd;
65
66 memset(&ifr, 0, sizeof(ifr));
67 strncpy_IFNAMSIZ(ifr.ifr_name, interface);
68 fd = xsocket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
69 if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
70 *ifindex = ifr.ifr_ifindex;
71 log2("ifindex %d", *ifindex);
72 ((uint32_t*)mac)[0] = rand();
73 ((uint16_t*)mac)[2] = rand();
74 retval &= (3 - (1<<0));
75 }
76 close(fd);
77 }
78
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020079 if (retval == 0)
80 return retval;
81
82 if (retval & (1<<0))
83 bb_error_msg("can't get %s", "MAC");
84 if (retval & (1<<1))
85 bb_error_msg("can't get %s", "link-local IPv6 address");
86 return -1;
87}
Denys Vlasenko9ba75042011-11-07 15:55:39 +010088
89int FAST_FUNC d6_listen_socket(int port, const char *inf)
90{
91 int fd;
92 struct sockaddr_in6 addr;
93
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +020094 log1("opening listen socket on *:%d %s", port, inf);
Denys Vlasenko9ba75042011-11-07 15:55:39 +010095 fd = xsocket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
96
97 setsockopt_reuseaddr(fd);
98 if (setsockopt_broadcast(fd) == -1)
99 bb_perror_msg_and_die("SO_BROADCAST");
100
101 /* NB: bug 1032 says this doesn't work on ethernet aliases (ethN:M) */
102 if (setsockopt_bindtodevice(fd, inf))
103 xfunc_die(); /* warning is already printed */
104
105 memset(&addr, 0, sizeof(addr));
106 addr.sin6_family = AF_INET6;
107 addr.sin6_port = htons(port);
108 /* addr.sin6_addr is all-zeros */
109 xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
110
111 return fd;
112}