Releasing 1.1pre15.
[tinc] / src / ifconfig.c
1 /*
2     ifconfig.c -- Generate platform specific interface configuration commands
3     Copyright (C) 2016-2017 Guus Sliepen <guus@tinc-vpn.org>
4
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.
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     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.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "ifconfig.h"
24 #include "subnet.h"
25
26 static long start;
27
28 #ifndef HAVE_MINGW
29 void ifconfig_header(FILE *out) {
30         fprintf(out, "#!/bin/sh\n");
31         start = ftell(out);
32 }
33
34 void ifconfig_dhcp(FILE *out) {
35         fprintf(out, "dhclient -nw \"$INTERFACE\"\n");
36 }
37
38 void ifconfig_dhcp6(FILE *out) {
39         fprintf(out, "dhclient -6 -nw \"$INTERFACE\"\n");
40 }
41
42 void ifconfig_slaac(FILE *out) {
43 #ifdef HAVE_LINUX
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");
46 #else
47         fprintf(out, "rtsol \"$INTERFACE\" &\n");
48 #endif
49 }
50
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");
54                 return false;
55         } else {
56 #ifdef HAVE_LINUX
57                 fprintf(out, "ip link set \"$INTERFACE\" up\n");
58 #else
59                 fprintf(out, "ifconfig \"$INTERFACE\" up\n");
60 #endif
61                 return true;
62         }
63 }
64 #else
65 void ifconfig_header(FILE *out) {
66         start = ftell(out);
67 }
68
69 void ifconfig_dhcp(FILE *out) {
70         fprintf(out, "netsh interface ipv4 set address \"%%INTERFACE%%\" dhcp\n");
71 }
72
73 void ifconfig_dhcp6(FILE *out) {
74         fprintf(stderr, "DHCPv6 requested, but not supported by tinc on this platform\n");
75 }
76
77 void ifconfig_slaac(FILE *out) {
78         // It's the default?
79 }
80
81 bool ifconfig_footer(FILE *out) {
82         return ftell(out) != start;
83 }
84 #endif
85
86 static subnet_t ipv4, ipv6;
87
88 void ifconfig_address(FILE *out, const char *value) {
89         subnet_t address = {};
90         char address_str[MAXNETSTR];
91         if(!str2net(&address, value) || !net2str(address_str, sizeof address_str, &address)) {
92                 fprintf(stderr, "Could not parse address in Ifconfig statement\n");
93                 return;
94         }
95         switch(address.type) {
96                 case SUBNET_IPV4: ipv4 = address; break;
97                 case SUBNET_IPV6: ipv6 = address; break;
98                 default: return;
99         }
100 #if defined(HAVE_LINUX)
101         switch(address.type) {
102                 case SUBNET_MAC:  fprintf(out, "ip link set \"$INTERFACE\" address %s\n", address_str); break;
103                 case SUBNET_IPV4: fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", address_str); break;
104                 case SUBNET_IPV6: fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", address_str); break;
105                 default: return;
106         }
107 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
108         switch(address.type) {
109                 case SUBNET_MAC:  fprintf(out, "ip link set \"$INTERFACE\" address %s\n", address_str); break;
110                 case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 set address \"$INTERFACE\" static %s\n", address_str); break;
111                 case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 set address \"$INTERFACE\" static %s\n", address_str); break;
112                 default: return;
113         }
114 #else // assume BSD
115         switch(address.type) {
116                 case SUBNET_MAC:  fprintf(out, "ifconfig \"$INTERFACE\" link %s\n", address_str); break;
117                 case SUBNET_IPV4: fprintf(out, "ifconfig \"$INTERFACE\" %s\n", address_str); break;
118                 case SUBNET_IPV6: fprintf(out, "ifconfig \"$INTERFACE\" inet6 %s\n", address_str); break;
119                 default: return;
120         }
121 #endif
122 }
123
124 void ifconfig_route(FILE *out, const char *value) {
125         subnet_t subnet = {}, gateway = {};
126         char subnet_str[MAXNETSTR] = "", gateway_str[MAXNETSTR] = "";
127         char *sep = strchr(value, ' ');
128         if(sep)
129                 *sep++ = 0;
130         if(!str2net(&subnet, value) || !net2str(subnet_str, sizeof subnet_str, &subnet) || subnet.type == SUBNET_MAC) {
131                 fprintf(stderr, "Could not parse subnet in Route statement\n");
132                 return;
133         }
134         if(sep) {
135                 if(!str2net(&gateway, sep) || !net2str(gateway_str, sizeof gateway_str, &gateway) || gateway.type != subnet.type) {
136                         fprintf(stderr, "Could not parse gateway in Route statement\n");
137                         return;
138                 }
139                 char *slash = strchr(gateway_str, '/'); if(slash) *slash = 0;
140         }
141 #if defined(HAVE_LINUX)
142         if(*gateway_str) {
143                 switch(subnet.type) {
144                         case SUBNET_IPV4: fprintf(out, "ip route add %s via %s dev \"$INTERFACE\"\n", subnet_str, gateway_str); break;
145                         case SUBNET_IPV6: fprintf(out, "ip route add %s via %s dev \"$INTERFACE\"\n", subnet_str, gateway_str); break;
146                         default: return;
147                 }
148         } else {
149                 switch(subnet.type) {
150                         case SUBNET_IPV4: fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", subnet_str); break;
151                         case SUBNET_IPV6: fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", subnet_str); break;
152                         default: return;
153                 }
154         }
155 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
156         if(*gateway_str) {
157                 switch(subnet.type) {
158                         case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str); break;
159                         case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str); break;
160                         default: return;
161                 }
162         } else {
163                 switch(subnet.type) {
164                         case SUBNET_IPV4: fprintf(out, "netsh inetface ipv4 add route %s \"%%INTERFACE%%\"\n", subnet_str); break;
165                         case SUBNET_IPV6: fprintf(out, "netsh inetface ipv6 add route %s \"%%INTERFACE%%\"\n", subnet_str); break;
166                         default: return;
167                 }
168         }
169 #else // assume BSD
170         if(!*gateway_str) {
171                 switch(subnet.type) {
172                         case SUBNET_IPV4:
173                                 if(!ipv4.type) {
174                                         fprintf(stderr, "Route requested but no Ifconfig\n");
175                                         return;
176                                 }
177                                 net2str(gateway_str, sizeof gateway_str, &ipv4);
178                                 break;
179                         case SUBNET_IPV6:
180                                 if(!ipv6.type) {
181                                         fprintf(stderr, "Route requested but no Ifconfig\n");
182                                         return;
183                                 }
184                                 net2str(gateway_str, sizeof gateway_str, &ipv6);
185                                 break;
186                         default: return;
187                 }
188                 char *slash = strchr(gateway_str, '/'); if(slash) *slash = 0;
189         }
190
191         switch(subnet.type) {
192                 case SUBNET_IPV4: fprintf(out, "route add %s %s\n", subnet_str, gateway_str); break;
193                 case SUBNET_IPV6: fprintf(out, "route add -inet6 %s %s\n", subnet_str, gateway_str); break;
194                 default: return;
195         }
196 #endif
197 }