1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2011 Denys Vlasenko.
5 * Licensed under GPLv2, see file LICENSE in this source tree.
11 #include <netpacket/packet.h>
13 int FAST_FUNC d6_read_interface(const char *interface, int *ifindex, struct in6_addr *nip6, uint8_t *mac)
16 struct ifaddrs *ifap, *ifa;
20 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
21 struct sockaddr_in6 *sip6;
23 if (!ifa->ifa_addr || (strcmp(ifa->ifa_name, interface) != 0))
26 sip6 = (struct sockaddr_in6*)(ifa->ifa_addr);
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 log2("MAC %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
32 log2("ifindex %d", sll->sll_ifindex);
33 *ifindex = sll->sll_ifindex;
34 retval &= (0xf - (1<<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));
42 if (ifa->ifa_addr->sa_family == AF_INET6
43 && IN6_IS_ADDR_LINKLOCAL(&sip6->sin6_addr)
45 *nip6 = sip6->sin6_addr; /* struct copy */
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]
57 retval &= (0xf - (1<<1));
66 bb_error_msg("can't get %s", "MAC");
68 bb_error_msg("can't get %s", "link-local IPv6 address");
72 int FAST_FUNC d6_listen_socket(int port, const char *inf)
75 struct sockaddr_in6 addr;
77 log1("opening listen socket on *:%d %s", port, inf);
78 fd = xsocket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
80 setsockopt_reuseaddr(fd);
81 if (setsockopt_broadcast(fd) == -1)
82 bb_perror_msg_and_die("SO_BROADCAST");
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 */
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));