2 ifconfig.c -- Generate platform specific interface configuration commands
3 Copyright (C) 2016-2017 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 void ifconfig_header(FILE *out) {
30 fprintf(out, "#!/bin/sh\n");
34 void ifconfig_dhcp(FILE *out) {
35 fprintf(out, "dhclient -nw \"$INTERFACE\"\n");
38 void ifconfig_dhcp6(FILE *out) {
39 fprintf(out, "dhclient -6 -nw \"$INTERFACE\"\n");
42 void ifconfig_slaac(FILE *out) {
44 fprintf(out, "echo 1 >\"/proc/sys/net/ipv6/conf/$INTERFACE/accept_ra\"\n");
45 fprintf(out, "echo 1 >\"/proc/sys/net/ipv6/conf/$INTERFACE/autoconf\"\n");
47 fprintf(out, "rtsol \"$INTERFACE\" &\n");
51 bool ifconfig_footer(FILE *out) {
52 if(ftell(out) == start) {
53 fprintf(out, "echo 'Unconfigured tinc-up script, please edit '$0'!'\n\n#ifconfig $INTERFACE <your vpn IP address> netmask <netmask of whole VPN>\n");
57 fprintf(out, "ip link set \"$INTERFACE\" up\n");
59 fprintf(out, "ifconfig \"$INTERFACE\" up\n");
65 void ifconfig_header(FILE *out) {
69 void ifconfig_dhcp(FILE *out) {
70 fprintf(out, "netsh interface ipv4 set address \"%%INTERFACE%%\" dhcp\n");
73 void ifconfig_dhcp6(FILE *out) {
74 fprintf(stderr, "DHCPv6 requested, but not supported by tinc on this platform\n");
77 void ifconfig_slaac(FILE *out) {
81 bool ifconfig_footer(FILE *out) {
82 return ftell(out) != start;
86 static subnet_t ipv4, ipv6;
88 void ifconfig_address(FILE *out, const char *value) {
89 subnet_t address = {NULL};
90 char address_str[MAXNETSTR];
92 if(!str2net(&address, value) || !net2str(address_str, sizeof(address_str), &address)) {
93 fprintf(stderr, "Could not parse address in Ifconfig statement\n");
97 switch(address.type) {
110 #if defined(HAVE_LINUX)
112 switch(address.type) {
114 fprintf(out, "ip link set \"$INTERFACE\" address %s\n", address_str);
118 fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", address_str);
122 fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", address_str);
129 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
131 switch(address.type) {
133 fprintf(out, "ip link set \"$INTERFACE\" address %s\n", address_str);
137 fprintf(out, "netsh inetface ipv4 set address \"$INTERFACE\" static %s\n", address_str);
141 fprintf(out, "netsh inetface ipv6 set address \"$INTERFACE\" static %s\n", address_str);
150 switch(address.type) {
152 fprintf(out, "ifconfig \"$INTERFACE\" link %s\n", address_str);
156 fprintf(out, "ifconfig \"$INTERFACE\" %s\n", address_str);
160 fprintf(out, "ifconfig \"$INTERFACE\" inet6 %s\n", address_str);
170 void ifconfig_route(FILE *out, const char *value) {
171 subnet_t subnet = {NULL}, gateway = {NULL};
172 char subnet_str[MAXNETSTR] = "", gateway_str[MAXNETSTR] = "";
173 char *sep = strchr(value, ' ');
179 if(!str2net(&subnet, value) || !net2str(subnet_str, sizeof(subnet_str), &subnet) || subnet.type == SUBNET_MAC) {
180 fprintf(stderr, "Could not parse subnet in Route statement\n");
185 if(!str2net(&gateway, sep) || !net2str(gateway_str, sizeof(gateway_str), &gateway) || gateway.type != subnet.type) {
186 fprintf(stderr, "Could not parse gateway in Route statement\n");
190 char *slash = strchr(gateway_str, '/');
197 #if defined(HAVE_LINUX)
200 switch(subnet.type) {
202 fprintf(out, "ip route add %s via %s dev \"$INTERFACE\"\n", subnet_str, gateway_str);
206 fprintf(out, "ip route add %s via %s dev \"$INTERFACE\"\n", subnet_str, gateway_str);
213 switch(subnet.type) {
215 fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", subnet_str);
219 fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", subnet_str);
227 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
230 switch(subnet.type) {
232 fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str);
236 fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str);
243 switch(subnet.type) {
245 fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\"\n", subnet_str);
249 fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\"\n", subnet_str);
260 switch(subnet.type) {
263 fprintf(stderr, "Route requested but no Ifconfig\n");
267 net2str(gateway_str, sizeof(gateway_str), &ipv4);
272 fprintf(stderr, "Route requested but no Ifconfig\n");
276 net2str(gateway_str, sizeof(gateway_str), &ipv6);
283 char *slash = strchr(gateway_str, '/');
290 switch(subnet.type) {
292 fprintf(out, "route add %s %s\n", subnet_str, gateway_str);
296 fprintf(out, "route add -inet6 %s %s\n", subnet_str, gateway_str);