00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <arpa/inet.h>
00044 #include <netinet/in.h>
00045 #include <sys/ioctl.h>
00046 #include <net/if.h>
00047 #include <linux/ip.h>
00048 #include <linux/if_tunnel.h>
00049 #include <linux/version.h>
00050 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
00051 #include <linux/ip6_tunnel.h>
00052 #endif
00053
00054
00055 #include <sys/socket.h>
00056 #include <sys/ioctl.h>
00057 #include <sys/types.h>
00058 #include <net/if.h>
00059
00060 #include <assert.h>
00061 #include <errno.h>
00062 #include <stdio.h>
00063
00064 #include "common/avl.h"
00065 #include "avl_olsr_comp.h"
00066 #include "defs.h"
00067 #include "olsr_types.h"
00068 #include "olsr_memcookie.h"
00069 #include "olsr_logging.h"
00070 #include "ipcalc.h"
00071 #include "os_net.h"
00072 #include "os_kernel_tunnel.h"
00073 #include "os_kernel_routes.h"
00074
00075 static const char DEV_IPV4_TUNNEL[IFNAMSIZ] = TUNNEL_ENDPOINT_IF;
00076 static const char DEV_IPV6_TUNNEL[IFNAMSIZ] = TUNNEL_ENDPOINT_IF6;
00077
00078 static bool store_iptunnel_state;
00079 static struct olsr_memcookie_info *tunnel_cookie;
00080 static struct avl_tree tunnel_tree;
00081
00082 int os_iptunnel_init(void) {
00083 const char *dev = olsr_cnf->ip_version == AF_INET ? DEV_IPV4_TUNNEL : DEV_IPV6_TUNNEL;
00084
00085 tunnel_cookie = olsr_memcookie_add("iptunnel", sizeof(struct olsr_iptunnel_entry));
00086 avl_init(&tunnel_tree, avl_comp_default, false, NULL);
00087
00088 store_iptunnel_state = os_is_interface_up(dev);
00089 if (store_iptunnel_state) {
00090 return 0;
00091 }
00092 if (os_interface_set_state(dev, true)) {
00093 return -1;
00094 }
00095
00096 return olsr_os_ifip(if_nametoindex(dev), &olsr_cnf->router_id, true);
00097 }
00098
00099 void os_iptunnel_cleanup(void) {
00100 while (tunnel_tree.count > 0) {
00101 struct olsr_iptunnel_entry *t;
00102
00103
00104 t = avl_first_element(&tunnel_tree, t, node);
00105 t->usage = 1;
00106
00107 os_iptunnel_del_ipip(t);
00108 }
00109 if (!store_iptunnel_state) {
00110 os_interface_set_state(olsr_cnf->ip_version == AF_INET ? DEV_IPV4_TUNNEL : DEV_IPV6_TUNNEL, false);
00111 }
00112
00113 olsr_memcookie_remove(tunnel_cookie);
00114 }
00115
00123 static int os_ip4_tunnel(const char *name, in_addr_t *target)
00124 {
00125 struct ifreq ifr;
00126 int err;
00127 struct ip_tunnel_parm p;
00128
00129
00130 assert (olsr_cnf->ip_version == AF_INET);
00131 memset(&p, 0, sizeof(p));
00132 p.iph.version = 4;
00133 p.iph.ihl = 5;
00134 p.iph.ttl = 64;
00135 p.iph.protocol = IPPROTO_IPIP;
00136 if (target) {
00137 p.iph.daddr = *target;
00138 }
00139 strncpy(p.name, name, IFNAMSIZ);
00140
00141 memset(&ifr, 0, sizeof(ifr));
00142 strncpy(ifr.ifr_name, target != NULL ? DEV_IPV4_TUNNEL : name, IFNAMSIZ);
00143 ifr.ifr_ifru.ifru_data = (void *) &p;
00144
00145 if ((err = ioctl(olsr_cnf->ioctl_s, target != NULL ? SIOCADDTUNNEL : SIOCDELTUNNEL, &ifr))) {
00146 #if !defined(REMOVE_LOG_WARN)
00147 char buffer[INET6_ADDRSTRLEN];
00148 #endif
00149 OLSR_WARN(LOG_TUNNEL, "Cannot %s a tunnel %s to %s: %s (%d)\n",
00150 target != NULL ? "add" : "remove", name,
00151 target != NULL ? inet_ntop(olsr_cnf->ip_version, target, buffer, sizeof(buffer)) : "-",
00152 strerror(errno), errno);
00153 return 0;
00154 }
00155 return target != NULL ? if_nametoindex(name) : 1;
00156 }
00157
00165 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
00166 static int os_ip6_tunnel(const char *name, struct in6_addr *target)
00167 {
00168 struct ifreq ifr;
00169 int err;
00170 struct ip6_tnl_parm p;
00171
00172
00173 assert (olsr_cnf->ip_version == AF_INET6);
00174 memset(&p, 0, sizeof(p));
00175 p.proto = 0;
00176 if (target) {
00177 p.raddr = *target;
00178 }
00179 strncpy(p.name, name, IFNAMSIZ);
00180
00181 memset(&ifr, 0, sizeof(ifr));
00182 strncpy(ifr.ifr_name, target != NULL ? DEV_IPV6_TUNNEL : name, IFNAMSIZ);
00183 ifr.ifr_ifru.ifru_data = (void *) &p;
00184
00185 if ((err = ioctl(olsr_cnf->ioctl_s, target != NULL ? SIOCADDTUNNEL : SIOCDELTUNNEL, &ifr))) {
00186 #if !defined(REMOVE_LOG_WARN)
00187 char buffer[INET6_ADDRSTRLEN];
00188 #endif
00189 OLSR_WARN(LOG_TUNNEL, "Cannot %s a tunnel %s to %s: %s (%d)\n",
00190 target != NULL ? "add" : "remove", name,
00191 target != NULL ? inet_ntop(olsr_cnf->ip_version, target, buffer, sizeof(buffer)) : "-",
00192 strerror(errno), errno);
00193 return 0;
00194 }
00195 return target != NULL ? if_nametoindex(name) : 1;
00196 }
00197 #endif
00198
00204 static void generate_iptunnel_name(union olsr_ip_addr *target, char *name) {
00205 static char PREFIX[] = "tnl_";
00206 static uint32_t counter = 0;
00207
00208 snprintf(name, IFNAMSIZ, "%s%08x", PREFIX,
00209 olsr_cnf->ip_version == AF_INET ? target->v4.s_addr : ++counter);
00210 }
00211
00218 struct olsr_iptunnel_entry *os_iptunnel_add_ipip(union olsr_ip_addr *target, bool transportV4 __attribute__ ((unused))) {
00219 struct olsr_iptunnel_entry *t;
00220
00221 assert(olsr_cnf->ip_version == AF_INET6 || transportV4);
00222
00223 t = (struct olsr_iptunnel_entry *)avl_find(&tunnel_tree, target);
00224 if (t == NULL) {
00225 char name[IFNAMSIZ];
00226 int if_idx;
00227
00228 memset(name, 0, sizeof(name));
00229 generate_iptunnel_name(target, name);
00230
00231 if (olsr_cnf->ip_version == AF_INET) {
00232 if_idx = os_ip4_tunnel(name, &target->v4.s_addr);
00233 }
00234 else {
00235 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
00236 if_idx = os_ip6_tunnel(name, &target->v6);
00237 #else
00238 if_idx = 0;
00239 #endif
00240 }
00241
00242 if (if_idx == 0) {
00243
00244 OLSR_WARN(LOG_TUNNEL, "Cannot create tunnel %s\n", name);
00245 return NULL;
00246 }
00247
00248 if (os_interface_set_state(name, true)) {
00249 if (olsr_cnf->ip_version == AF_INET) {
00250 os_ip4_tunnel(name, NULL);
00251 }
00252 else {
00253 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
00254 os_ip6_tunnel(name, NULL);
00255 #endif
00256 }
00257 return NULL;
00258 }
00259
00260
00261 olsr_os_ifip(if_idx, &olsr_cnf->router_id, true);
00262
00263 t = olsr_memcookie_malloc(tunnel_cookie);
00264 memcpy(&t->target, target, sizeof(*target));
00265 t->node.key = &t->target;
00266
00267 strncpy(t->if_name, name, IFNAMSIZ);
00268 t->if_index = if_idx;
00269
00270 avl_insert(&tunnel_tree, &t->node);
00271 }
00272
00273 t->usage++;
00274 return t;
00275 }
00276
00282 static void internal_olsr_os_del_ipip_tunnel(struct olsr_iptunnel_entry *t, bool cleanup) {
00283 if (!cleanup) {
00284 if (t->usage == 0) {
00285 return;
00286 }
00287 t->usage--;
00288
00289 if (t->usage > 0) {
00290 return;
00291 }
00292 }
00293
00294 os_interface_set_state(t->if_name, false);
00295 if (olsr_cnf->ip_version == AF_INET) {
00296 os_ip4_tunnel(t->if_name, NULL);
00297 }
00298 else {
00299 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
00300 os_ip6_tunnel(t->if_name, NULL);
00301 #endif
00302 }
00303
00304 avl_delete(&tunnel_tree, &t->node);
00305 if (!cleanup) {
00306 olsr_memcookie_free(tunnel_cookie, t);
00307 }
00308 }
00309
00310 void os_iptunnel_del_ipip(struct olsr_iptunnel_entry *t) {
00311 internal_olsr_os_del_ipip_tunnel(t, false);
00312 }