blob: acf108367a688c60704011ca60b0330d6afd2a8d [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
Denys Vlasenko60bf77f2019-04-14 17:01:10 +020013int FAST_FUNC d6_read_interface(
14 const char *interface,
15 int *ifindex,
16 struct in6_addr *nip6,
17 uint8_t *mac)
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020018{
19 int retval = 3;
20 struct ifaddrs *ifap, *ifa;
21
22 getifaddrs(&ifap);
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020023 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
24 struct sockaddr_in6 *sip6;
25
26 if (!ifa->ifa_addr || (strcmp(ifa->ifa_name, interface) != 0))
27 continue;
28
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020029 if (ifa->ifa_addr->sa_family == AF_PACKET) {
Denys Vlasenko60bf77f2019-04-14 17:01:10 +020030 struct sockaddr_ll *sll = (void*)(ifa->ifa_addr);
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020031 memcpy(mac, sll->sll_addr, 6);
Denys Vlasenko60bf77f2019-04-14 17:01:10 +020032 log2("MAC %02x:%02x:%02x:%02x:%02x:%02x",
33 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
34 );
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020035 *ifindex = sll->sll_ifindex;
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010036 log2("ifindex %d", *ifindex);
37 retval &= (3 - (1<<0));
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020038 }
39#if 0
40 if (ifa->ifa_addr->sa_family == AF_INET) {
41 *nip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
42 log1("IP %s", inet_ntoa(((struct sockaddr_in *)ifa->ifa_addr)->sin_addr));
43 }
44#endif
Denys Vlasenko0d75e8b2019-04-13 19:43:15 +020045/* RFC 3315
46 * 16. Client Source Address and Interface Selection
47 *
48 * "When a client sends a DHCP message to the
49 * All_DHCP_Relay_Agents_and_Servers address, ... ... The client
50 * MUST use a link-local address assigned to the interface for which it
51 * is requesting configuration information as the source address in the
52 * header of the IP datagram."
53 */
Denys Vlasenko60bf77f2019-04-14 17:01:10 +020054 sip6 = (void*)(ifa->ifa_addr);
55
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020056 if (ifa->ifa_addr->sa_family == AF_INET6
57 && IN6_IS_ADDR_LINKLOCAL(&sip6->sin6_addr)
58 ) {
59 *nip6 = sip6->sin6_addr; /* struct copy */
60 log1(
61 "IPv6 %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
62 nip6->s6_addr[0], nip6->s6_addr[1],
63 nip6->s6_addr[2], nip6->s6_addr[3],
64 nip6->s6_addr[4], nip6->s6_addr[5],
65 nip6->s6_addr[6], nip6->s6_addr[7],
66 nip6->s6_addr[8], nip6->s6_addr[9],
67 nip6->s6_addr[10], nip6->s6_addr[11],
68 nip6->s6_addr[12], nip6->s6_addr[13],
69 nip6->s6_addr[14], nip6->s6_addr[15]
70 );
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010071 retval &= (3 - (1<<1));
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020072 }
73 }
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020074 freeifaddrs(ifap);
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010075
76 if (retval & (1<<0)) {
77 /* This iface has no MAC (e.g. ppp), generate a random one */
78 struct ifreq ifr;
79 int fd;
80
Denys Vlasenko46ba2462018-03-27 23:54:54 +020081 /*memset(&ifr, 0, sizeof(ifr)); - SIOCGIFINDEX does not need to clear all */
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010082 strncpy_IFNAMSIZ(ifr.ifr_name, interface);
83 fd = xsocket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
84 if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
85 *ifindex = ifr.ifr_ifindex;
86 log2("ifindex %d", *ifindex);
Denys Vlasenko30b290f2018-01-23 16:46:31 +010087 if (((uint32_t*)mac)[0] == 0) {
88 /* invent a fictitious MAC (once) */
89 ((uint32_t*)mac)[0] = rand();
90 ((uint16_t*)mac)[2] = rand();
91 mac[0] &= 0xfc; /* make sure it's not bcast */
92 }
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +010093 retval &= (3 - (1<<0));
94 }
95 close(fd);
96 }
97
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +020098 if (retval & (1<<0))
99 bb_error_msg("can't get %s", "MAC");
100 if (retval & (1<<1))
101 bb_error_msg("can't get %s", "link-local IPv6 address");
Denys Vlasenko60bf77f2019-04-14 17:01:10 +0200102 return retval;
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +0200103}
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100104
105int FAST_FUNC d6_listen_socket(int port, const char *inf)
106{
107 int fd;
108 struct sockaddr_in6 addr;
Denys Vlasenko02ca5652022-12-15 23:57:27 +0100109 char *colon;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100110
Denys Vlasenko84b89b42022-08-02 18:04:51 +0200111 log2("opening listen socket on *:%d %s", port, inf);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100112 fd = xsocket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
113
114 setsockopt_reuseaddr(fd);
115 if (setsockopt_broadcast(fd) == -1)
James Byrne69374872019-07-02 11:35:03 +0200116 bb_simple_perror_msg_and_die("SO_BROADCAST");
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100117
Denys Vlasenko02ca5652022-12-15 23:57:27 +0100118 /* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */
119 colon = strrchr(inf, ':');
120 if (colon)
121 *colon = '\0';
122
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100123 if (setsockopt_bindtodevice(fd, inf))
124 xfunc_die(); /* warning is already printed */
125
Denys Vlasenko02ca5652022-12-15 23:57:27 +0100126 if (colon)
127 *colon = ':';
128
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100129 memset(&addr, 0, sizeof(addr));
130 addr.sin6_family = AF_INET6;
131 addr.sin6_port = htons(port);
132 /* addr.sin6_addr is all-zeros */
133 xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
134
135 return fd;
136}