4 * Created on: 05.01.2010
10 #include "common/avl.h"
15 #include "olsr_cookie.h"
16 #include "scheduler.h"
17 #include "kernel_routes.h"
18 #include "kernel_tunnel.h"
20 #include "duplicate_set.h"
22 #include "gateway_default_handler.h"
28 /** the gateway tree */
29 struct avl_tree gateway_tree;
32 static struct olsr_cookie_info *gw_mem_cookie = NULL;
34 /** the gateway netmask for the HNA */
35 static uint8_t smart_gateway_netmask[sizeof(union olsr_ip_addr)];
37 /** the gateway handler/plugin */
38 static struct olsr_gw_handler *gw_handler;
40 /** the current IPv4 gateway */
41 static struct gateway_entry *current_ipv4_gw;
43 /** the tunnel of the current IPv4 gateway */
44 static struct olsr_iptunnel_entry *v4gw_tunnel;
46 /** the current IPv6 gateway */
47 static struct gateway_entry *current_ipv6_gw;
49 /** the tunnel of the current IPv6 gateway */
50 static struct olsr_iptunnel_entry *v6gw_tunnel;
56 #define OLSR_IP_ADDR_2_HNA_PTR(mask, prefixlen) (((uint8_t *)mask) + ((prefixlen+7)/8))
59 * Convert an encoded 1 byte transport value (5 bits mantissa, 3 bits exponent)
60 * to an uplink/downlink speed value
62 * @param value the encoded 1 byte transport value
63 * @return the uplink/downlink speed value (in kbit/s)
65 static uint32_t deserialize_gw_speed(uint8_t value) {
70 /* 0 and 1 alias onto 0 during serialisation. We take 0 here to mean 0 and
71 * not 1 (since a bandwidth of 1 is no bandwidth at all really) */
75 speed = (value >> 3) + 1;
85 * Convert an uplink/downlink speed value into an encoded 1 byte transport
86 * value (5 bits mantissa, 3 bits exponent)
88 * @param speed the uplink/downlink speed value (in kbit/s)
89 * @return value the encoded 1 byte transport value
91 static uint8_t serialize_gw_speed(uint32_t speed) {
98 if (speed > 320000000) {
102 while ((speed > 32 || (speed % 10) == 0) && exp < 7) {
106 return ((speed - 1) << 3) | exp;
114 * Callback for tunnel interface monitoring which will set the route into the tunnel
115 * when the interface comes up again.
117 * @param if_index the interface index
118 * @param ifh the interface
119 * @param flag interface change flags
121 static void smartgw_tunnel_monitor(int if_index __attribute__ ((unused)),
122 struct interface *ifh __attribute__ ((unused)), enum olsr_ifchg_flag flag __attribute__ ((unused))) {
127 * Triggers an instant gateway selection based on the current data
129 * @param ipv4 trigger a ipv4 gateway lookup
130 * @param ipv6 trigger a ipv6 gateway lookup
131 * @return 0 if successful, -1 otherwise
133 static int olsr_trigger_inetgw_selection(bool ipv4, bool ipv6) {
135 gw_handler->choose(ipv4, ipv6);
136 return ((ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL)) ? -1 : 0;
140 * Timer callback to remove and cleanup a gateway entry
144 static void cleanup_gateway_handler(void *ptr) {
145 struct gateway_entry *gw = ptr;
147 if (gw->ipv4 || gw->ipv6) {
148 /* do not clean it up when it is in use */
152 /* remove gateway entry */
153 avl_delete(&gateway_tree, &gw->node);
154 olsr_cookie_free(gw_mem_cookie, gw);
162 * Initialize gateway system
164 int olsr_init_gateways(void) {
165 gw_mem_cookie = olsr_alloc_cookie("Gateway cookie", OLSR_COOKIE_TYPE_MEMORY);
166 olsr_cookie_set_memory_size(gw_mem_cookie, sizeof(struct gateway_entry));
168 avl_init(&gateway_tree, avl_comp_default);
170 current_ipv4_gw = NULL;
173 current_ipv6_gw = NULL;
178 refresh_smartgw_netmask();
180 if (olsr_os_init_iptunnel(olsr_cnf->ip_version == AF_INET ? TUNNEL_ENDPOINT_IF : TUNNEL_ENDPOINT_IF6)) {
184 olsr_add_ifchange_handler(smartgw_tunnel_monitor);
186 /* initialize default gateway handler */
187 gw_handler = &gw_def_handler;
194 * Cleanup gateway tunnel system
196 void olsr_cleanup_gateways(void) {
197 if (current_ipv4_gw) {
198 olsr_os_del_ipip_tunnel(v4gw_tunnel);
200 if (current_ipv6_gw) {
201 olsr_os_del_ipip_tunnel(v6gw_tunnel);
204 olsr_remove_ifchange_handler(smartgw_tunnel_monitor);
207 gw_handler->cleanup();
210 olsr_os_cleanup_iptunnel(olsr_cnf->ip_version == AF_INET ? TUNNEL_ENDPOINT_IF : TUNNEL_ENDPOINT_IF6);
214 * Triggers the first lookup of a gateway.
216 void olsr_trigger_inetgw_startup(void) {
218 gw_handler->startup();
222 * Print debug information about gateway entries
224 void olsr_print_gateway_entries(void) {
226 struct ipaddr_str buf;
227 struct gateway_entry *gw;
228 const int addrsize = olsr_cnf->ip_version == AF_INET ? 15 : 39;
230 OLSR_PRINTF(0, "\n--- %s ---------------------------------------------------- GATEWAYS\n\n", olsr_wallclock_string());
231 OLSR_PRINTF(0, "%-*s %-6s %-9s %-9s %s\n",
232 addrsize, "IP address", "Type", "Uplink", "Downlink", olsr_cnf->ip_version == AF_INET ? "" : "External Prefix");
234 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw) {
235 OLSR_PRINTF(0, "%-*s %s%c%s%c%c %-9u %-9u %s\n",
237 olsr_ip_to_string(&buf, &gw->originator),
238 gw->ipv4nat ? "" : " ",
239 gw->ipv4 ? '4' : ' ',
240 gw->ipv4nat ? "(N)" : "",
241 (gw->ipv4 && gw->ipv6) ? ',' : ' ',
242 gw->ipv6 ? '6' : ' ',
245 gw->external_prefix.prefix_len == 0 ? "" : olsr_ip_prefix_to_string(&gw->external_prefix));
246 } OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
255 * Apply the smart gateway modifications to an outgoing HNA
257 * @param mask pointer to netmask of the HNA
258 * @param prefixlen of the HNA
260 void olsr_modifiy_inetgw_netmask(union olsr_ip_addr *mask, int prefixlen) {
261 uint8_t *ptr = OLSR_IP_ADDR_2_HNA_PTR(mask, prefixlen);
263 memcpy(ptr, &smart_gateway_netmask, sizeof(smart_gateway_netmask) - prefixlen / 8);
264 if (olsr_cnf->has_ipv4_gateway) {
265 ptr[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV4;
267 if (olsr_cnf->smart_gw_uplink_nat) {
268 ptr[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV4_NAT;
271 if (olsr_cnf->has_ipv6_gateway) {
272 ptr[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV6;
274 if (!olsr_cnf->has_ipv6_gateway || prefixlen != ipv6_internet_route.prefix_len) {
275 ptr[GW_HNA_FLAGS] &= ~GW_HNA_FLAG_IPV6PREFIX;
280 * SgwDynSpeed Plugin Interface
284 * Setup the gateway netmask
286 void refresh_smartgw_netmask(void) {
288 memset(&smart_gateway_netmask, 0, sizeof(smart_gateway_netmask));
290 if (olsr_cnf->smart_gw_active) {
291 ip = (uint8_t *) &smart_gateway_netmask;
293 if (olsr_cnf->smart_gw_uplink > 0 && olsr_cnf->smart_gw_downlink > 0) {
294 /* the link is bi-directional with a non-zero bandwidth */
295 ip[GW_HNA_FLAGS] |= GW_HNA_FLAG_LINKSPEED;
296 ip[GW_HNA_DOWNLINK] = serialize_gw_speed(olsr_cnf->smart_gw_downlink);
297 ip[GW_HNA_UPLINK] = serialize_gw_speed(olsr_cnf->smart_gw_uplink);
299 if (olsr_cnf->ip_version == AF_INET6 && olsr_cnf->smart_gw_prefix.prefix_len > 0) {
300 ip[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV6PREFIX;
301 ip[GW_HNA_V6PREFIXLEN] = olsr_cnf->smart_gw_prefix.prefix_len;
302 memcpy(&ip[GW_HNA_V6PREFIX], &olsr_cnf->smart_gw_prefix.prefix, 8);
308 * TC/SPF/HNA Interface
312 * Checks if a HNA prefix/netmask combination is a smart gateway
316 * @return true if is a valid smart gateway HNA, false otherwise
318 bool olsr_is_smart_gateway(struct olsr_ip_prefix *prefix, union olsr_ip_addr *mask) {
321 if (!is_prefix_inetgw(prefix)) {
325 ptr = OLSR_IP_ADDR_2_HNA_PTR(mask, prefix->prefix_len);
326 return ptr[GW_HNA_PAD] == 0 && ptr[GW_HNA_FLAGS] != 0;
330 * Update a gateway_entry based on a HNA
332 * @param originator ip of the source of the HNA
333 * @param mask netmask of the HNA
334 * @param prefixlen of the HNA
335 * @param seqno the sequence number of the HNA
337 void olsr_update_gateway_entry(union olsr_ip_addr *originator, union olsr_ip_addr *mask, int prefixlen, uint16_t seqno) {
338 struct gateway_entry *gw = node2gateway(avl_find(&gateway_tree, originator));
339 uint8_t *ptr = OLSR_IP_ADDR_2_HNA_PTR(mask, prefixlen);
342 gw = olsr_cookie_malloc(gw_mem_cookie);
343 gw->originator = *originator;
344 gw->node.key = &gw->originator;
346 avl_insert(&gateway_tree, &gw->node, AVL_DUP_NO);
347 } else if (olsr_seqno_diff(seqno, gw->seqno) <= 0) {
348 /* ignore older HNAs */
352 /* keep new HNA seqno */
355 if ((ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_LINKSPEED) != 0) {
356 gw->uplink = deserialize_gw_speed(ptr[GW_HNA_UPLINK]);
357 gw->downlink = deserialize_gw_speed(ptr[GW_HNA_DOWNLINK]);
363 gw->ipv4 = (ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV4) != 0;
364 gw->ipv4nat = (ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV4_NAT) != 0;
366 if (olsr_cnf->ip_version == AF_INET6) {
367 gw->ipv6 = (ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV6) != 0;
369 /* do not reset prefixlength for ::ffff:0:0 HNAs */
370 if (prefixlen == ipv6_internet_route.prefix_len) {
371 memset(&gw->external_prefix, 0, sizeof(gw->external_prefix));
373 if ((ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV6PREFIX) != 0
374 && memcmp(mask->v6.s6_addr, &ipv6_internet_route.prefix, olsr_cnf->ipsize) == 0) {
375 /* this is the right prefix (2000::/3), so we can copy the prefix */
376 gw->external_prefix.prefix_len = ptr[GW_HNA_V6PREFIXLEN];
377 memcpy(&gw->external_prefix.prefix, &ptr[GW_HNA_V6PREFIX], 8);
382 /* stop cleanup timer if necessary */
383 if (gw->cleanup_timer) {
384 olsr_stop_timer(gw->cleanup_timer);
385 gw->cleanup_timer = NULL;
388 /* call update handler */
390 gw_handler->update(gw);
394 * Delete a gateway based on the originator IP and the prefixlength of a HNA.
395 * Should only be called if prefix is a smart_gw prefix or if node is removed
401 void olsr_delete_gateway_entry(union olsr_ip_addr *originator, uint8_t prefixlen) {
402 struct gateway_entry *gw = node2gateway(avl_find(&gateway_tree, originator));
405 if (gw && (gw->cleanup_timer == NULL || gw->ipv4 || gw->ipv6)) {
406 /* found a gw and it wasn't deleted yet */
408 if (olsr_cnf->ip_version == AF_INET && prefixlen == 0) {
412 } else if (olsr_cnf->ip_version == AF_INET6 && prefixlen == ipv6_internet_route.prefix_len) {
415 } else if (olsr_cnf->ip_version == AF_INET6 && prefixlen == ipv6_mappedv4_route.prefix_len) {
421 if (prefixlen == FORCE_DELETE_GW_ENTRY || !(gw->ipv4 || gw->ipv6)) {
422 /* prevent this gateway from being chosen as the new gateway */
427 /* handle gateway loss */
429 gw_handler->delete(gw);
431 /* cleanup gateway if necessary */
432 if (current_ipv4_gw == gw) {
433 olsr_os_inetgw_tunnel_route(v4gw_tunnel->if_index, true, false);
434 olsr_os_del_ipip_tunnel(v4gw_tunnel);
436 current_ipv4_gw = NULL;
439 if (current_ipv6_gw == gw) {
440 olsr_os_inetgw_tunnel_route(v6gw_tunnel->if_index, false, false);
441 olsr_os_del_ipip_tunnel(v6gw_tunnel);
443 current_ipv6_gw = NULL;
447 /* remove gateway entry on a delayed schedule */
448 olsr_set_timer(&gw->cleanup_timer, GW_CLEANUP_INTERVAL, 0, false, cleanup_gateway_handler, gw, NULL);
451 gw_handler->update(gw);
457 * Triggers a check if the one of the gateways have been lost or has an
460 void olsr_trigger_gatewayloss_check(void) {
464 if (current_ipv4_gw) {
465 struct tc_entry *tc = olsr_lookup_tc_entry(¤t_ipv4_gw->originator);
466 ipv4 = (tc == NULL || tc->path_cost == ROUTE_COST_BROKEN);
468 if (current_ipv6_gw) {
469 struct tc_entry *tc = olsr_lookup_tc_entry(¤t_ipv6_gw->originator);
470 ipv6 = (tc == NULL || tc->path_cost == ROUTE_COST_BROKEN);
474 olsr_trigger_inetgw_selection(ipv4, ipv6);
479 * Gateway Plugin Functions
483 * Sets a new internet gateway.
485 * @param originator ip address of the node with the new gateway
486 * @param ipv4 set ipv4 gateway
487 * @param ipv6 set ipv6 gateway
488 * @return true if an error happened, false otherwise
490 bool olsr_set_inet_gateway(union olsr_ip_addr *originator, bool ipv4, bool ipv6) {
491 struct gateway_entry *entry;
492 struct gateway_entry *oldV4 = current_ipv4_gw;
493 struct gateway_entry *oldV6 = current_ipv6_gw;
494 struct olsr_iptunnel_entry *tunnelV4 = v4gw_tunnel;
495 struct olsr_iptunnel_entry *tunnelV6 = v6gw_tunnel;
497 ipv4 = ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit);
498 ipv6 = ipv6 && (olsr_cnf->ip_version == AF_INET6);
501 current_ipv4_gw = NULL;
504 current_ipv6_gw = NULL;
507 entry = node2gateway(avl_find(&gateway_tree, originator));
509 if (ipv4 && entry != current_ipv4_gw && entry->ipv4 && (!entry->ipv4nat || olsr_cnf->smart_gw_allow_nat)) {
510 /* valid ipv4 gateway */
511 current_ipv4_gw = entry;
513 if (ipv6 && entry != current_ipv6_gw && entry->ipv6) {
514 /* valid ipv6 gateway */
515 current_ipv6_gw = entry;
520 if (oldV4 != current_ipv4_gw) {
521 if ((v4gw_tunnel = olsr_os_add_ipip_tunnel(¤t_ipv4_gw->originator, true)) != NULL) {
522 olsr_os_inetgw_tunnel_route(v4gw_tunnel->if_index, true, true);
524 /* adding the tunnel failed, we try again in the next cycle */
525 current_ipv4_gw = NULL;
528 olsr_os_del_ipip_tunnel(tunnelV4);
532 if (oldV6 != current_ipv6_gw) {
533 if ((v6gw_tunnel = olsr_os_add_ipip_tunnel(¤t_ipv6_gw->originator, false)) != NULL) {
534 olsr_os_inetgw_tunnel_route(v6gw_tunnel->if_index, false, true);
536 /* adding the tunnel failed, we try again in the next cycle */
537 current_ipv6_gw = NULL;
540 olsr_os_del_ipip_tunnel(tunnelV6);
543 return (ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL);
547 * @return a pointer to the gateway_entry of the current ipv4 internet gw or
550 struct gateway_entry *olsr_get_ipv4_inet_gateway(void) {
551 return current_ipv4_gw;
555 * @return a pointer to the gateway_entry of the current ipv4 internet gw or
558 struct gateway_entry *olsr_get_ipv6_inet_gateway(void) {
559 return current_ipv6_gw;