4 * Created on: 05.01.2010
8 #include "common/avl.h"
13 #include "olsr_cookie.h"
14 #include "scheduler.h"
15 #include "kernel_routes.h"
16 #include "kernel_tunnel.h"
18 #include "duplicate_set.h"
20 #include "gateway_default_handler.h"
26 #ifdef LINUX_NETLINK_ROUTING
27 struct avl_tree gateway_tree;
29 static struct olsr_cookie_info *gw_mem_cookie = NULL;
30 static uint8_t smart_gateway_netmask[sizeof(union olsr_ip_addr)];
31 static struct gateway_entry *current_ipv4_gw, *current_ipv6_gw;
32 static struct olsr_gw_handler *gw_handler;
34 static struct olsr_iptunnel_entry *v4gw_tunnel, *v6gw_tunnel;
35 static bool v4gw_choosen_external, v6gw_choosen_external;
38 * Reconstructs an uplink/downlink speed value from the encoded
39 * 1 byte transport value (3 bit mantissa, 5 bit exponent)
44 deserialize_gw_speed(uint8_t value) {
47 speed = (value >> 3)+1;
56 * Convert an uplink/downlink speed into an exponential encoded
57 * transport value (1 byte, 3 bit mantissa and 5 bit exponent)
58 * @param value uplink/downlink speed in kbit/s
59 * @return encoded byte value
62 serialize_gw_speed(uint32_t speed) {
65 if (speed == 0 || speed > 320000000) {
69 while (speed > 32 || (speed % 10) == 0) {
73 return ((speed-1) << 3) | exp;
77 * Callback for tunnel interface monitoring which will set the route into the tunnel
78 * when the interface comes up again.
83 static void smartgw_tunnel_monitor (int if_index __attribute__ ((unused)),
84 struct interface *ifh __attribute__ ((unused)),
85 enum olsr_ifchg_flag flag __attribute__ ((unused))) {
90 * Initialize gateway system
93 olsr_init_gateways(void) {
94 gw_mem_cookie = olsr_alloc_cookie("Gateway cookie", OLSR_COOKIE_TYPE_MEMORY);
95 olsr_cookie_set_memory_size(gw_mem_cookie, sizeof(struct gateway_entry));
97 avl_init(&gateway_tree, avl_comp_default);
98 current_ipv4_gw = NULL;
99 current_ipv6_gw = NULL;
104 refresh_smartgw_netmask();
106 if (olsr_os_init_iptunnel()) {
110 olsr_add_ifchange_handler(smartgw_tunnel_monitor);
113 * initialize default gateway handler,
114 * can be overwritten with olsr_set_inetgw_handler
116 olsr_gw_default_init();
120 void refresh_smartgw_netmask(void) {
122 memset(&smart_gateway_netmask, 0, sizeof(smart_gateway_netmask));
124 if (olsr_cnf->smart_gw_active) {
125 union olsr_ip_addr gw_net;
126 memset(&gw_net, 0, sizeof(gw_net));
128 ip = (uint8_t *) &smart_gateway_netmask;
130 if (olsr_cnf->smart_gw_uplink > 0 || olsr_cnf->smart_gw_downlink > 0) {
131 ip[GW_HNA_FLAGS] |= GW_HNA_FLAG_LINKSPEED;
132 ip[GW_HNA_DOWNLINK] = serialize_gw_speed(olsr_cnf->smart_gw_downlink);
133 ip[GW_HNA_UPLINK] = serialize_gw_speed(olsr_cnf->smart_gw_uplink);
135 if (olsr_cnf->ip_version == AF_INET6 && olsr_cnf->smart_gw_prefix.prefix_len > 0) {
136 ip[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV6PREFIX;
137 ip[GW_HNA_V6PREFIXLEN] = olsr_cnf->smart_gw_prefix.prefix_len;
138 memcpy(&ip[GW_HNA_V6PREFIX], &olsr_cnf->smart_gw_prefix.prefix, 8);
144 * Cleanup gateway tunnel system
146 void olsr_cleanup_gateways(void) {
147 if (current_ipv4_gw) {
148 olsr_os_del_ipip_tunnel(v4gw_tunnel);
150 if (current_ipv6_gw) {
151 olsr_os_del_ipip_tunnel(v6gw_tunnel);
154 olsr_remove_ifchange_handler(smartgw_tunnel_monitor);
155 olsr_os_cleanup_iptunnel();
159 * Triggers the first lookup of an gateway. This lookup might
160 * take some time to wait for routing/gateway information.
163 olsr_trigger_inetgw_startup(void) {
164 gw_handler->handle_startup();
168 * Triggers an instant gateway selection based on the current data
169 * @param ipv4 trigger a ipv4 gateway lookup
170 * @param ipv6 trigger a ipv6 gateway lookup
171 * @return 0 if successful, -1 otherwise
174 olsr_trigger_inetgw_selection(bool ipv4, bool ipv6) {
175 gw_handler->select_gateway(ipv4, ipv6);
176 return ((ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL)) ? -1 : 0;
180 * Triggers a check if the one of the gateways have been lost
181 * through ETX = infinity
183 void olsr_trigger_gatewayloss_check(void) {
185 bool ipv4 = false, ipv6 = false;
186 if (current_ipv4_gw) {
187 tc = olsr_lookup_tc_entry(¤t_ipv4_gw->originator);
188 if (tc == NULL || tc->path_cost == ROUTE_COST_BROKEN) {
192 if (current_ipv6_gw) {
193 tc = olsr_lookup_tc_entry(¤t_ipv6_gw->originator);
194 if (tc == NULL || tc->path_cost == ROUTE_COST_BROKEN) {
199 olsr_trigger_inetgw_selection(ipv4, ipv6);
203 * Set a new gateway handler. Do only call this once during startup from
204 * a plugin to overwrite the default handler.
205 * @param h pointer to gateway handler struct
208 olsr_set_inetgw_handler(struct olsr_gw_handler *h) {
213 * Sets a new internet gateway.
214 * An external set command might trigger an internal one if
215 * address is not a legal gateway.
217 * @param originator ip address of the node with the new gateway
218 * @param ipv4 set ipv4 gateway
219 * @param ipv6 set ipv6 gateway
220 * @param external true if change was triggered directly by an user,
221 * false if triggered by automatic lookup.
222 * @return true if an error happened, false otherwise
225 olsr_set_inet_gateway(union olsr_ip_addr *originator, bool ipv4, bool ipv6, bool external) {
226 struct gateway_entry *entry, *oldV4, *oldV6;
227 struct olsr_iptunnel_entry *tunnelV4, *tunnelV6;
229 oldV4 = current_ipv4_gw;
230 oldV6 = current_ipv6_gw;
231 tunnelV4 = v4gw_tunnel;
232 tunnelV6 = v6gw_tunnel;
234 ipv4 = ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit);
235 ipv6 = ipv6 && (olsr_cnf->ip_version == AF_INET6);
238 current_ipv4_gw = NULL;
241 current_ipv6_gw = NULL;
244 entry = olsr_find_gateway_entry(originator);
246 if (ipv4 && entry != current_ipv4_gw && entry->ipv4
247 && (!entry->ipv4nat || olsr_cnf->smart_gw_allow_nat)) {
248 /* valid ipv4 gateway */
249 current_ipv4_gw = entry;
251 if (ipv6 && entry != current_ipv6_gw && entry->ipv6) {
252 /* valid ipv6 gateway */
253 current_ipv6_gw = entry;
258 if (oldV4 != current_ipv4_gw) {
259 if ((v4gw_tunnel = olsr_os_add_ipip_tunnel(¤t_ipv4_gw->originator, true)) != NULL) {
260 olsr_os_inetgw_tunnel_route(v4gw_tunnel->if_index, true, true);
261 v4gw_choosen_external = external;
264 // TODO: what to do now ? Choose another one ? Fire up a timer ?
265 current_ipv4_gw = NULL;
268 olsr_os_del_ipip_tunnel(tunnelV4);
272 if (oldV6 != current_ipv6_gw) {
273 if ((v6gw_tunnel = olsr_os_add_ipip_tunnel(¤t_ipv6_gw->originator, false)) != NULL) {
274 olsr_os_inetgw_tunnel_route(v6gw_tunnel->if_index, false, true);
275 v6gw_choosen_external = external;
278 // TODO: what to do now ? Choose another one ? Fire up a timer ?
279 current_ipv6_gw = NULL;
282 olsr_os_del_ipip_tunnel(tunnelV6);
285 return (ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL);
289 * returns the gateway_entry of the current ipv4 internet gw.
290 * @return pointer to gateway_entry or NULL if not set
292 struct gateway_entry *olsr_get_ipv4_inet_gateway(bool *ext) {
294 *ext = v4gw_choosen_external;
296 return current_ipv4_gw;
300 * returns the gateway_entry of the current ipv6 internet gw.
301 * @return pointer to gateway_entry or NULL if not set
303 struct gateway_entry *olsr_get_ipv6_inet_gateway(bool *ext) {
305 *ext = v6gw_choosen_external;
307 return current_ipv6_gw;
312 * @return gateway_entry for corresponding router
314 struct gateway_entry *
315 olsr_find_gateway_entry(union olsr_ip_addr *originator) {
316 struct avl_node *node = avl_find(&gateway_tree, originator);
318 return node == NULL ? NULL : node2gateway(node);
322 * update a gateway_entry based on data received from a HNA
323 * @param originator ip of the source of the HNA
324 * @param mask netmask of the HNA
325 * @param prefixlen of the HNA
328 olsr_update_gateway_entry(union olsr_ip_addr *originator, union olsr_ip_addr *mask, int prefixlen, uint16_t seqno) {
329 struct gateway_entry *gw;
332 ptr = ((uint8_t *)mask) + ((prefixlen+7)/8);
334 gw = olsr_find_gateway_entry(originator);
336 struct ipaddr_str buf;
337 gw = olsr_cookie_malloc(gw_mem_cookie);
339 gw->originator = *originator;
340 gw->node.key = &gw->originator;
342 avl_insert(&gateway_tree, &gw->node, AVL_DUP_NO);
344 else if (olsr_seqno_diff(seqno, gw->seqno) <= 0) {
345 /* ignore older HNAs */
349 /* keep new HNA seqno */
352 if ((ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_LINKSPEED) != 0) {
353 gw->uplink = deserialize_gw_speed(ptr[GW_HNA_UPLINK]);
354 gw->downlink = deserialize_gw_speed(ptr[GW_HNA_DOWNLINK]);
361 gw->ipv4 = (ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV4) != 0;
362 gw->ipv4nat = (ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV4_NAT) != 0;
364 if (olsr_cnf->ip_version == AF_INET6) {
365 gw->ipv6 = (ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV6) != 0;
367 /* do not reset prefixlength for ::ffff:0:0 HNAs */
368 if (prefixlen == ipv6_internet_route.prefix_len) {
369 memset(&gw->external_prefix, 0, sizeof(gw->external_prefix));
371 if ((ptr[GW_HNA_FLAGS] & GW_HNA_FLAG_IPV6PREFIX) != 0
372 && memcmp(mask->v6.s6_addr, &ipv6_internet_route.prefix, olsr_cnf->ipsize) == 0) {
373 /* this is the right prefix (2000::/3), so we can copy the prefix */
374 gw->external_prefix.prefix_len = ptr[GW_HNA_V6PREFIXLEN];
375 memcpy(&gw->external_prefix.prefix, &ptr[GW_HNA_V6PREFIX], 8);
380 /* stop cleanup timer if necessary */
381 if (gw->cleanup_timer) {
382 olsr_stop_timer(gw->cleanup_timer);
383 gw->cleanup_timer = NULL;
386 /* call update handler */
387 gw_handler->handle_update_gw(gw);
390 static void cleanup_gateway_handler(void *ptr) {
391 struct gateway_entry *gw = ptr;
393 if (gw->ipv4 || gw->ipv6) {
397 /* remove gateway entry */
398 avl_delete(&gateway_tree, &gw->node);
399 olsr_cookie_free(gw_mem_cookie, gw);
403 * Delete a gateway based on the originator IP and the prefixlength of a HNA.
404 * Should only be called if prefix is a smart_gw prefix or if node is removed
410 olsr_delete_gateway_entry(union olsr_ip_addr *originator, uint8_t prefixlen) {
411 struct gateway_entry *gw;
413 gw = olsr_find_gateway_entry(originator);
414 if (gw && (gw->cleanup_timer == NULL || gw->ipv4 || gw->ipv6)) {
415 if (olsr_cnf->ip_version == AF_INET && prefixlen == 0) {
420 else if (olsr_cnf->ip_version == AF_INET6 && prefixlen == ipv6_internet_route.prefix_len) {
424 else if (olsr_cnf->ip_version == AF_INET6 && prefixlen == ipv6_mappedv4_route.prefix_len) {
430 if (prefixlen == FORCE_DELETE_GW_ENTRY || !(gw->ipv4 || gw->ipv6)) {
431 /* prevent this gateway from being choosen as the new gateway */
436 /* handle gateway loss */
437 gw_handler->handle_delete_gw(gw);
439 /* cleanup gateway if necessary */
440 if (current_ipv4_gw == gw) {
441 olsr_os_inetgw_tunnel_route(v4gw_tunnel->if_index, true, false);
442 olsr_os_del_ipip_tunnel(v4gw_tunnel);
444 current_ipv4_gw = NULL;
447 if (current_ipv6_gw == gw) {
448 olsr_os_inetgw_tunnel_route(v6gw_tunnel->if_index, false, false);
449 olsr_os_del_ipip_tunnel(v6gw_tunnel);
451 current_ipv6_gw = NULL;
455 /* remove gateway entry */
456 olsr_set_timer(&gw->cleanup_timer, GW_CLEANUP_INTERVAL, 0, false, cleanup_gateway_handler, gw, NULL);
459 gw_handler->handle_update_gw(gw);
465 * Checks if a prefix/netmask combination is a smart gateway
468 * @return true if is a valid smart gateway HNA, false otherwise
471 olsr_is_smart_gateway(struct olsr_ip_prefix *prefix, union olsr_ip_addr *mask) {
474 if (!is_prefix_inetgw(prefix)) {
478 ptr = ((uint8_t *)mask) + ((prefix->prefix_len+7)/8);
479 return ptr[GW_HNA_PAD] == 0 && ptr[GW_HNA_FLAGS] != 0;
483 * Apply the smart gateway modifications to an outgoing HNA
484 * @param mask pointer to netmask of the HNA
485 * @param prefixlen of the HNA
488 olsr_modifiy_inetgw_netmask(union olsr_ip_addr *mask, int prefixlen) {
489 uint8_t *ptr = ((uint8_t *)mask) + ((prefixlen+7)/8);
491 memcpy(ptr, &smart_gateway_netmask, sizeof(smart_gateway_netmask) - prefixlen/8);
492 if (olsr_cnf->has_ipv4_gateway) {
493 ptr[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV4;
495 if (olsr_cnf->smart_gw_uplink_nat) {
496 ptr[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV4_NAT;
499 if (olsr_cnf->has_ipv6_gateway) {
500 ptr[GW_HNA_FLAGS] |= GW_HNA_FLAG_IPV6;
502 if (!olsr_cnf->has_ipv6_gateway || prefixlen != ipv6_internet_route.prefix_len){
503 ptr[GW_HNA_FLAGS] &= ~GW_HNA_FLAG_IPV6PREFIX;
508 * Print debug information about gateway entries
511 olsr_print_gateway_entries(void) {
513 struct ipaddr_str buf;
514 struct gateway_entry *gw;
515 const int addrsize = olsr_cnf->ip_version == AF_INET ? 15 : 39;
517 OLSR_PRINTF(0, "\n--- %s ---------------------------------------------------- GATEWAYS\n\n",
518 olsr_wallclock_string());
519 OLSR_PRINTF(0, "%-*s %-6s %-9s %-9s %s\n", addrsize, "IP address", "Type", "Uplink", "Downlink",
520 olsr_cnf->ip_version == AF_INET ? "" : "External Prefix");
522 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw) {
523 OLSR_PRINTF(0, "%-*s %s%c%s%c%c %-9u %-9u %s\n", addrsize, olsr_ip_to_string(&buf, &gw->originator),
524 gw->ipv4nat ? "" : " ",
525 gw->ipv4 ? '4' : ' ',
526 gw->ipv4nat ? "(N)" : "",
527 (gw->ipv4 && gw->ipv6) ? ',' : ' ',
528 gw->ipv6 ? '6' : ' ',
529 gw->uplink, gw->downlink,
530 gw->external_prefix.prefix_len == 0 ? "" : olsr_ip_prefix_to_string(&gw->external_prefix));
531 } OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)