464xlat: import from routing, add myself as maintainer
[oweals/openwrt.git] / package / network / ipv6 / 464xlat / src / 464xlatcfg.c
1 /* 464xlatcfg.c
2  *
3  * Copyright (c) 2015 Steven Barth <cyrus@openwrt.org>
4  * Copyright (c) 2017 Hans Dedecker <dedeckeh@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <netinet/icmp6.h>
17 #include <netinet/in.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <net/if.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <netdb.h>
26
27 static void sighandler(__attribute__((unused)) int signal)
28 {
29 }
30
31 int main(int argc, const char *argv[])
32 {
33         char buf[INET6_ADDRSTRLEN], prefix[INET6_ADDRSTRLEN + 4];
34         int pid;
35
36         if (argc <= 1) {
37                 fprintf(stderr, "Usage: %s <name> [ifname] [ipv6prefix] [ipv4addr] [ipv6addr]\n", argv[0]);
38                 return 1;
39         }
40
41         snprintf(buf, sizeof(buf), "/var/run/%s.pid", argv[1]);
42         FILE *fp = fopen(buf, "r");
43         if (fp) {
44                 if (fscanf(fp, "%d", &pid) == 1)
45                         kill(pid, SIGTERM);
46
47                 unlink(buf);
48                 fclose(fp);
49         }
50
51         if (!argv[2])
52                 return 0;
53
54         if (!argv[3] || !argv[4] || !(fp = fopen(buf, "wx")))
55                 return 1;
56
57         signal(SIGTERM, SIG_DFL);
58         setvbuf(fp, NULL, _IOLBF, 0);
59         fprintf(fp, "%d\n", getpid());
60
61         prefix[sizeof(prefix) - 1] = 0;
62         strncpy(prefix, argv[3], sizeof(prefix) - 1);
63
64         if (!prefix[0]) {
65                 struct addrinfo hints = { .ai_family = AF_INET6 }, *res;
66                 if (getaddrinfo("ipv4only.arpa", NULL, &hints, &res) || !res) {
67                         sleep(3);
68                         if (getaddrinfo("ipv4only.arpa", NULL, &hints, &res) || !res)
69                                 return 2;
70                 }
71
72                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)res->ai_addr;
73                 inet_ntop(AF_INET6, &sin6->sin6_addr, prefix, sizeof(prefix) - 4);
74                 strcat(prefix, "/96");
75                 freeaddrinfo(res);
76         }
77
78         int i = 0;
79         int sock;
80         struct sockaddr_in6 saddr;
81
82         do {
83                 socklen_t saddrlen = sizeof(saddr);
84                 struct icmp6_filter filt;
85
86                 sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
87                 ICMP6_FILTER_SETBLOCKALL(&filt);
88                 setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
89                 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, argv[2], strlen(argv[2]));
90                 memset(&saddr, 0, sizeof(saddr));
91                 saddr.sin6_family = AF_INET6;
92                 saddr.sin6_addr.s6_addr32[0] = htonl(0x2001);
93                 saddr.sin6_addr.s6_addr32[1] = htonl(0xdb8);
94                 if (connect(sock, (struct sockaddr*)&saddr, sizeof(saddr)) ||
95                                 getsockname(sock, (struct sockaddr*)&saddr, &saddrlen))
96                         return 3;
97
98                 if (!IN6_IS_ADDR_LINKLOCAL(&saddr.sin6_addr) || argv[5])
99                         break;
100
101                 close(sock);
102                 sleep(3);
103                 i++;
104         } while (i < 3);
105
106         struct ipv6_mreq mreq = {saddr.sin6_addr, if_nametoindex(argv[2])};
107         if (!argv[5]) {
108                 if (IN6_IS_ADDR_LINKLOCAL(&mreq.ipv6mr_multiaddr))
109                         return 5;
110
111                 srandom(mreq.ipv6mr_multiaddr.s6_addr32[0] ^ mreq.ipv6mr_multiaddr.s6_addr32[1] ^
112                                 mreq.ipv6mr_multiaddr.s6_addr32[2] ^ mreq.ipv6mr_multiaddr.s6_addr32[3]);
113                 mreq.ipv6mr_multiaddr.s6_addr32[2] = random();
114                 mreq.ipv6mr_multiaddr.s6_addr32[3] = random();
115         } else if (inet_pton(AF_INET6, argv[5], &mreq.ipv6mr_multiaddr) != 1) {
116                 return 1;
117         }
118
119         if (setsockopt(sock, SOL_IPV6, IPV6_JOIN_ANYCAST, &mreq, sizeof(mreq)))
120                 return 3;
121
122         inet_ntop(AF_INET6, &mreq.ipv6mr_multiaddr, buf, sizeof(buf));
123         fputs(buf, stdout);
124         fputc('\n', stdout);
125         fflush(stdout);
126
127         FILE *nat46 = fopen("/proc/net/nat46/control", "w");
128         if (!nat46 || fprintf(nat46, "add %s\nconfig %s local.style NONE local.v4 %s/32 local.v6 %s/128 "
129                         "remote.style RFC6052 remote.v6 %s\n", argv[1], argv[1], argv[4], buf, prefix) < 0 ||
130                         fclose(nat46))
131                 return 4;
132
133         if (!(pid = fork())) {
134                 fclose(fp);
135                 fclose(stdin);
136                 fclose(stdout);
137                 fclose(stderr);
138                 chdir("/");
139                 setsid();
140                 signal(SIGTERM, sighandler);
141                 pause();
142
143                 nat46 = fopen("/proc/net/nat46/control", "w");
144                 if (nat46) {
145                         fprintf(nat46, "del %s\n", argv[1]);
146                         fclose(nat46);
147                 }
148         } else {
149                 rewind(fp);
150                 fprintf(fp, "%d\n", pid);
151         }
152
153         return 0;
154 }