2 ifconfig.c -- Generate platform specific interface configuration commands
3 Copyright (C) 2016 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) {
91 if(!str2net(&subnet, value) || !net2str(str, sizeof str, &subnet)) {
92 fprintf(stderr, "Could not parse Ifconfig statement\n");
96 case SUBNET_IPV4: ipv4 = subnet; break;
97 case SUBNET_IPV6: ipv6 = subnet; break;
99 #if defined(HAVE_LINUX)
100 switch(subnet.type) {
101 case SUBNET_MAC: fprintf(out, "ip link set \"$INTERFACE\" address %s\n", str); break;
102 case SUBNET_IPV4: fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", str); break;
103 case SUBNET_IPV6: fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", str); break;
105 #elif defined(HAVE_BSD)
106 switch(subnet.type) {
107 case SUBNET_MAC: fprintf(out, "ifconfig \"$INTERFACE\" link %s\n", str); break;
108 case SUBNET_IPV4: fprintf(out, "ifconfig \"$INTERFACE\" %s\n", str); break;
109 case SUBNET_IPV6: fprintf(out, "ifconfig \"$INTERFACE\" inet6 %s\n", str); break;
111 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
112 switch(subnet.type) {
113 case SUBNET_MAC: fprintf(out, "ip link set \"$INTERFACE\" address %s\n", str); break;
114 case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 set address \"$INTERFACE\" static %s\n", str); break;
115 case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 set address \"$INTERFACE\" static %s\n", str); break;
120 void ifconfig_route(FILE *out, const char *value) {
121 subnet_t subnet = {};
123 if(!str2net(&subnet, value) || !net2str(str, sizeof str, &subnet) || subnet.type == SUBNET_MAC) {
124 fprintf(stderr, "Could not parse Ifconfig statement\n");
127 #if defined(HAVE_LINUX)
128 switch(subnet.type) {
129 case SUBNET_IPV4: fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", str); break;
130 case SUBNET_IPV6: fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", str); break;
132 #elif defined(HAVE_BSD)
133 // BSD route command is silly and doesn't accept an interface name as a destination.
134 char gwstr[MAXNETSTR] = "";
135 switch(subnet.type) {
138 fprintf(stderr, "Route requested but no Ifconfig\n");
141 net2str(gwstr, sizeof gwstr, &ipv4);
142 char *p = strchr(gwstr, '/'); if(p) *p = 0;
143 fprintf(out, "route add %s %s\n", str, gwstr);
147 fprintf(stderr, "Route requested but no Ifconfig\n");
150 net2str(gwstr, sizeof gwstr, &ipv6);
151 char *p = strchr(gwstr, '/'); if(p) *p = 0;
152 fprintf(out, "route add -inet6 %s %s\n", str, gwstr);
155 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
156 switch(subnet.type) {
157 case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 add route %s \"$INTERFACE\"\n", str); break;
158 case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 add route %s \"$INTERFACE\"\n", str); break;