/* Copyright (c) 2013, Matthias Schiffer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "babel.h" #include "neigh.h" #include "packet.h" #include "tlv_types.h" #include typedef struct handle_tlv_arg { gmrf_iface_state_t *iface; const gmrf_addr_t *source; gp_babel_node_id_t node_id; } handle_tlv_arg_t; static inline gp_babel_neigh_t* get_tlv_neigh(gmrf_context_t *ctx, handle_tlv_arg_t *arg) { gp_babel_neigh_t *neigh = gp_babel_neigh_get(arg->iface, arg->source); neigh->last_packet = gmrf_now(ctx->gmrf); return neigh; } static void handle_tlv_ack_req(gmrf_context_t *ctx, const gp_babel_tlv_ack_req_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ack_req_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement request TLV."); return; } gp_babel_send_ack(ctx, get_tlv_neigh(ctx, arg), ntohs(tlv->nonce)); } static void handle_tlv_ack(gmrf_context_t *ctx, const gp_babel_tlv_ack_t *tlv UNUSED, size_t len, handle_tlv_arg_t *arg UNUSED) { if (len < sizeof(gp_babel_tlv_ack_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short acknowledement TLV."); return; } //gp_babel_ack_handle(ntohs(tlv->nonce)); } static void handle_tlv_hello(gmrf_context_t *ctx, const gp_babel_tlv_hello_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_hello_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short hello TLV."); return; } gmrf_logf(ctx->gmrf, LOG_DEBUG, "received hello from %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x[%s] with seqno %04x.", arg->source->d[0], arg->source->d[1], arg->source->d[2], arg->source->d[3], arg->source->d[4], arg->source->d[5], arg->source->d[6], arg->source->d[7], gmrf_iface_get_name(ctx->gmrf, arg->iface->gmrf_iface), ntohs(tlv->seqno)); gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg); uint16_t seqno = ntohs(tlv->seqno); if (neigh->last_hello != gmrf_time_unspec) { int timediff = gp_babel_since(ctx, neigh->last_hello); uint16_t seqexp = neigh->last_seqno + (timediff - neigh->hello_interval/2)/neigh->hello_interval; /* cast to int16_t to ensure correct handling of seqno wrapping */ if (abs((int16_t)(seqno - seqexp)) > 16) { gmrf_logf(ctx->gmrf, LOG_INFO, "neighbour was reset."); neigh->hello_log = 0; neigh->txcost = GP_BABEL_INFINITY; } else { int16_t seqdiff = seqno - neigh->last_seqno; if (seqdiff <= 0) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "seqno already seen, ignoring."); return; } neigh->hello_log <<= seqdiff; } } else { gmrf_logf(ctx->gmrf, LOG_DEBUG, "received hello from new neighbour."); } neigh->hello_log |= 1; neigh->hello_interval = ntohs(tlv->interval); neigh->last_seqno = seqno; neigh->last_hello = gmrf_now(ctx->gmrf); if (neigh->hello_log == 1) /* new or reset neighbour */ gp_babel_neigh_reset(ctx, arg->iface, neigh); gmrf_logf(ctx->gmrf, LOG_DEBUG, "accepted hello, log %04x, rxcost is %u, cost is %u now.", neigh->hello_log, gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_cost(ctx, neigh)); gmrf_debug_neigh(ctx->gmrf, arg->iface->gmrf_iface, &neigh->addr, gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_txcost(ctx, neigh)); } static void handle_tlv_ihu(gmrf_context_t *ctx, const gp_babel_tlv_ihu_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_ihu_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV."); return; } if (tlv->ae == ADDR_ENC_GMRF) { if (len < sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short IHU TLV."); return; } gmrf_addr_t iface_addr = gmrf_iface_get_addr(ctx->gmrf, arg->iface->gmrf_iface); if (memcmp(tlv->address, &iface_addr, sizeof(gmrf_addr_t)) != 0) return; } else if (tlv->ae != ADDR_ENC_UNSPEC) { return; } gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg); neigh->ihu_interval = ntohs(tlv->interval); neigh->last_ihu = gmrf_now(ctx->gmrf); neigh->txcost = ntohs(tlv->rxcost); gmrf_logf(ctx->gmrf, LOG_DEBUG, "accepted IHU, txcost is %u, cost is %u now.", gp_babel_neigh_get_txcost(ctx, neigh), gp_babel_neigh_get_cost(ctx, neigh)); gmrf_debug_neigh(ctx->gmrf, arg->iface->gmrf_iface, &neigh->addr, gp_babel_neigh_get_rxcost(ctx, neigh), gp_babel_neigh_get_txcost(ctx, neigh)); } static void handle_tlv_node_id(gmrf_context_t *ctx, const gp_babel_tlv_node_id_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_node_id_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short node id TLV."); return; } arg->node_id = tlv->id; } static void handle_tlv_update(gmrf_context_t *ctx, const gp_babel_tlv_update_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_update_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short update TLV."); return; } if (gp_babel_node_id_equal(&tlv->node, &ctx->self)) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "update source is myself."); return; } gp_babel_metric_seqno_t ms = { ntohs(tlv->metric), ntohs(tlv->seqno) }; gp_babel_route_t *route; if (ms.metric == GP_BABEL_INFINITY) { route = gp_babel_route_find(ctx, &tlv->node); if (!route) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "retract received for unknown node %04x%04x.", ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4))); return; } } else { route = gp_babel_route_get(ctx, &tlv->node); } bool feasible = gp_babel_is_feasible(route, ms); gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg); gp_babel_nexthop_t *nexthop = gp_babel_route_nexthop_find(route, neigh); gmrf_logf(ctx->gmrf, LOG_DEBUG, "update received from %04x%04x, metric %u, seqno %04x.", ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4)), ms.metric, ms.seqno); gmrf_logf(ctx->gmrf, LOG_DEBUG, "the update is %sfeasible and there is %s nexthop.", feasible ? "" : "not ", nexthop ? "a" : "no"); if (!nexthop) { if (feasible && tlv->metric != GP_BABEL_INFINITY /* no need to ntohs */) nexthop = gp_babel_route_nexthop_new(route, neigh); } else { if (!feasible && nexthop == route->selected) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno"); gp_babel_send_seqno_request_for(ctx, neigh, route); nexthop = NULL; } } if (!feasible && nexthop && nexthop != route->selected) { if (ms.metric + gp_babel_neigh_get_cost(ctx, neigh) < route->metric.metric) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "requesting new seqno"); gp_babel_send_seqno_request_for(ctx, neigh, route); } } if (!nexthop) return; gmrf_logf(ctx->gmrf, LOG_DEBUG, "the update was accepted."); gp_babel_route_update_nexthop(ctx, route, nexthop, ms, ntohs(tlv->interval)); } static void handle_tlv_route_req(gmrf_context_t *ctx, const gp_babel_tlv_route_req_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_route_req_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short route request TLV."); return; } gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg); if (gp_babel_node_id_is_unspec(&tlv->node)) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "received wildcard route request, dumping table."); gp_babel_send_update(ctx, arg->iface, neigh); return; } gp_babel_route_t *route = gp_babel_route_find(ctx, &tlv->node); if (!route) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "received route request for unknown route, retracting."); gp_babel_send_retract(ctx, arg->iface, neigh, &tlv->node); return; } gmrf_logf(ctx->gmrf, LOG_DEBUG, "received route request, responding."); gp_babel_send_update_for_route(ctx, arg->iface, neigh, route); } static void handle_tlv_seqno_req(gmrf_context_t *ctx, const gp_babel_tlv_seqno_req_t *tlv, size_t len, handle_tlv_arg_t *arg) { if (len < sizeof(gp_babel_tlv_seqno_req_t)) { gmrf_logf(ctx->gmrf, LOG_WARNING, "received short seqno request TLV."); return; } gp_babel_route_t *route = gp_babel_route_find(ctx, &tlv->node); if (!route || !route->selected) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "received seqno request for unknown/unreachable route."); return; } gmrf_logf(ctx->gmrf, LOG_DEBUG, "seqno request received for %04x%04x, seqno %04x.", ntohl(*(uint32_t*)tlv->node.id), ntohl(*(uint32_t*)(tlv->node.id+4)), ntohs(tlv->seqno)); gp_babel_neigh_t *neigh = get_tlv_neigh(ctx, arg); if (!gp_babel_less(route->metric.seqno, ntohs(tlv->seqno))) { /* local seqno is high enough */ gmrf_logf(ctx->gmrf, LOG_DEBUG, "sending update."); gp_babel_send_update_for_route(ctx, arg->iface, neigh, route); return; } if (!route->selected->neigh) { /* route is local, increment seqno */ gmrf_logf(ctx->gmrf, LOG_DEBUG, "incrementing seqno."); route->selected->metric_seqno.seqno++; gp_babel_send_update_for_route(ctx, arg->iface, neigh, route); return; } if (tlv->hop_count < 2) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "hopcount too low, discarding."); return; } if (route->selected->neigh == neigh) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "not forwarding backwards."); return; } /* forward request */ gmrf_logf(ctx->gmrf, LOG_DEBUG, "forwarding request."); gp_babel_send_seqno_request(ctx, route->selected->neigh, route, ntohs(tlv->seqno), tlv->hop_count-1); } static void handle_tlv(gmrf_context_t *ctx, gp_babel_tlv_type_t type, const void *data, size_t len, void *arg) { handle_tlv_arg_t *tlv_arg = arg; switch (type) { case TLV_ACK_REQ: handle_tlv_ack_req(ctx, data, len, tlv_arg); return; case TLV_ACK: handle_tlv_ack(ctx, data, len, tlv_arg); return; case TLV_HELLO: handle_tlv_hello(ctx, data, len, tlv_arg); return; case TLV_IHU: handle_tlv_ihu(ctx, data, len, tlv_arg); return; case TLV_NODE_ID: handle_tlv_node_id(ctx, data, len, tlv_arg); return; case TLV_UPDATE: handle_tlv_update(ctx, data, len, tlv_arg); return; case TLV_ROUTE_REQ: handle_tlv_route_req(ctx, data, len, tlv_arg); return; case TLV_SEQNO_REQ: handle_tlv_seqno_req(ctx, data, len, tlv_arg); return; default: gmrf_logf(ctx->gmrf, LOG_DEBUG, "received unknown TLV %u on %s.", type, gmrf_iface_get_name(ctx->gmrf, tlv_arg->iface->gmrf_iface)); return; } } void gp_babel_handle_packet(gmrf_context_t *ctx, gmrf_iface_state_t *iface, const gmrf_addr_t *source, const gp_babel_packet_t *packet) { handle_tlv_arg_t arg = { .iface = iface, .source = source }; if (!gp_babel_tlv_parse(ctx, packet, handle_tlv, &arg)) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "received invalid packet."); } }