/* Copyright (c) 2012, 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 "ffd.h" #include "neigh.h" #include "netif.h" #include "packet.h" #include "tlv.h" #include "tlv_types.h" #include #include #include #include #include #include #include #include const eth_addr_t ffd_addr = {{0x03, 0x00, 0x00, 0x00, 0x0f, 0xfd}}; static char *mesh = "bat0"; int sockfd; struct timespec now; ffd_node_id_t self; ffd_iface_t *iface_list = NULL; ffd_announce_t *announce_list = NULL; static inline bool use_netif(const char *ifname) { char *if_mesh = netif_get_mesh(ifname); bool ret = (if_mesh && !strcmp(if_mesh, mesh)); free(if_mesh); return ret; } static void update_time(void) { clock_gettime(CLOCK_MONOTONIC, &now); } static bool check_config(void) { if (!netif_is_mesh(mesh)) { fprintf(stderr, "error: configured interface is no mesh\n"); return false; } return true; } static bool init_self(void) { eth_addr_t primary_addr = netif_mesh_get_primary_addr(mesh); if (is_eth_addr_unspec(&primary_addr)) return false; self.id[0] = primary_addr.d[0]^0x02; self.id[1] = primary_addr.d[1]; self.id[2] = primary_addr.d[2]; self.id[3] = 0xff; self.id[4] = 0xfe; self.id[5] = primary_addr.d[3]; self.id[6] = primary_addr.d[4]; self.id[7] = primary_addr.d[5]; ffd_announce_t *announce = announce_list = calloc(1, sizeof(ffd_announce_t)); announce->node = self; announce->type = 1; announce->key = 1337; announce->interval = 6000; ffd_nexthop_t *nexthop = announce->nexthop_list = malloc(sizeof(ffd_nexthop_t)); nexthop->addr = ETH_ADDR_UNSPEC; nexthop->metric_seqno.metric = 0; nexthop->metric_seqno.seqno = 0; return true; } static bool init_socket(void) { sockfd = socket(AF_PACKET, SOCK_DGRAM, htons(FFD_PROTO)); if (sockfd < 0) { fprintf(stderr, "error: socket: %m\n"); return false; } return true; } static inline ffd_iface_t* get_iface(unsigned ifindex) { ffd_iface_t *iface; for (iface = iface_list; iface; iface = iface->next) { if (iface->ifindex == ifindex) return iface; } return NULL; } static void update_netif(const char *ifname, unsigned ifindex, void *arg) { if (!use_netif(ifname)) return; /* join multicast group */ struct packet_mreq mr; memset(&mr, 0, sizeof(mr)); mr.mr_ifindex = ifindex; mr.mr_type = PACKET_MR_MULTICAST; mr.mr_alen = ETH_ALEN; memcpy(mr.mr_address, ffd_addr.d, ETH_ALEN); if (setsockopt(sockfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) && errno != EADDRINUSE) fprintf(stderr, "warning: setsockopt: %m\n"); ffd_iface_t *iface = get_iface(ifindex); if (!iface) { /* new iface */ iface = malloc(sizeof(ffd_iface_t)); iface->next = iface_list; iface_list = iface; iface->seqno = 0; iface->neigh_list = NULL; } iface->ifindex = ifindex; strncpy(iface->name, ifname, IF_NAMESIZE); iface->type = netif_get_type(ifname); iface->addr = netif_get_eth_addr(ifname); } static void update_netifs(void) { ffd_iface_t *iface, **cur; for (iface = iface_list; iface; iface = iface->next) iface->type = IF_UNSPEC; netif_foreach(update_netif, NULL); cur = &iface_list; while (*cur) { iface = *cur; if (iface->type == IF_UNSPEC) { *cur = iface->next; ffd_neigh_free_list(iface->neigh_list); free(iface); } else { cur = &iface->next; } } } static void handle_tlv_hello(const ffd_tlv_hello_t *tlv_hello, size_t len, const eth_addr_t *addr, ffd_iface_t *iface) { if (len < sizeof(ffd_tlv_hello_t)) { fprintf(stderr, "warn: received short hello TLV.\n"); return; } fprintf(stderr, "debug: received hello with seqno %u.\n", ntohs(tlv_hello->seqno)); ffd_neigh_t *neigh = ffd_neigh_get(iface, addr); uint16_t seqno = ntohs(tlv_hello->seqno); if (neigh->last_hello.tv_sec) { int timediff = timespec_diff(&now, &neigh->last_hello)/10; 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) { fprintf(stderr, "info: neighbour was reset.\n"); neigh->hello_log = 0; } else { int16_t seqdiff = seqno - neigh->last_seqno; if (seqdiff <= 0) { fprintf(stderr, "debug: seqno already seen, ignoring.\n"); return; } neigh->hello_log <<= seqdiff; } } else { fprintf(stderr, "info: received hello from new neighbour.\n"); } neigh->hello_log |= 1; neigh->hello_interval = ntohs(tlv_hello->interval); neigh->last_seqno = seqno; neigh->last_hello = now; if (neigh->hello_log == 1) /* new or reset neighbour */ ffd_neigh_reset(iface, neigh); fprintf(stderr, "debug: accepted hello, log %04x, rxcost is %u, cost is %u now.\n", neigh->hello_log, ffd_neigh_get_rxcost(neigh), ffd_neigh_get_cost(neigh)); } static void handle_tlv_ihu(const ffd_tlv_ihu_t *tlv_ihu, size_t len, const eth_addr_t *addr, ffd_iface_t *iface) { if (len < sizeof(ffd_tlv_ihu_t)) { fprintf(stderr, "warn: received short IHU TLV.\n"); return; } if (tlv_ihu->ae == ADDR_ENC_ETH) { if (len < sizeof(ffd_tlv_ihu_t)+sizeof(eth_addr_t)) { fprintf(stderr, "warn: received short IHU TLV.\n"); return; } if (memcmp(tlv_ihu->address, &iface->addr, sizeof(eth_addr_t)) != 0) return; } else if (tlv_ihu->ae != ADDR_ENC_UNSPEC) { return; } ffd_neigh_t *neigh = ffd_neigh_get(iface, addr); neigh->last_ihu = now; neigh->txcost = ntohs(tlv_ihu->rxcost); fprintf(stderr, "debug: accepted IHU, txcost is %u, cost is %u now.\n", neigh->txcost, ffd_neigh_get_cost(neigh)); } static void handle_tlv_announce_req(const ffd_tlv_announce_req_t *tlv_req, size_t len, const eth_addr_t *addr, ffd_iface_t *iface) { if (len < sizeof(ffd_tlv_announce_req_t)) { fprintf(stderr, "warn: received short announce request TLV.\n"); return; } ffd_announce_t *announce = NULL; if (!ffd_is_node_id_unspec(&tlv_req->node)) { announce = NULL; } ffd_send_update(iface, ffd_neigh_get(iface, addr), announce); } static void handle_tlv(ffd_tlv_type_t type, const void *data, size_t len, void *arg) { const struct sockaddr_ll *from = arg; const eth_addr_t *addr = (const eth_addr_t*)from->sll_addr; ffd_iface_t *iface = get_iface(from->sll_ifindex); if (!iface) return; switch (type) { case TLV_HELLO: handle_tlv_hello(data, len, addr, iface); return; case TLV_IHU: handle_tlv_ihu(data, len, addr, iface); return; case TLV_ANNOUNCE_REQ: handle_tlv_announce_req(data, len, addr, iface); return; default: fprintf(stderr, "debug: received unknown TLV %u on %s.\n", type, iface->name); return; } } static void receive_packet(void) { ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+FFD_PACKET_MAX); struct sockaddr_ll from; socklen_t fromlen = sizeof(from); ssize_t readlen = recvfrom(sockfd, packet, sizeof(ffd_packet_t)+FFD_PACKET_MAX, 0, (struct sockaddr*)&from, &fromlen); if (readlen < 0) { fprintf(stderr, "error: recvfrom: %m\n"); return; } if (from.sll_halen != ETH_ALEN) { fprintf(stderr, "error: receive_packet: strange address length\n"); return; } if (readlen < sizeof(ffd_packet_t) || readlen < sizeof(ffd_packet_t)+ntohs(packet->len)) { fprintf(stderr, "debug: received short packet.\n"); return; } if (!ffd_tlv_parse(packet, handle_tlv, &from)) { fprintf(stderr, "debug: received invalid packet.\n"); } } void add_interval(struct timespec *time, int interval) { int cs = time->tv_nsec/1e7 + interval; time->tv_sec += cs/100; time->tv_nsec = time->tv_nsec%(long)1e7 + (cs%100)*1e7; } int main() { if (!check_config()) return 1; if (!init_self()) return 1; if (!init_socket()) return 1; update_time(); struct timespec next_hello = now; while (true) { update_netifs(); int timeout = timespec_diff(&next_hello, &now); if (timeout <= 0) { ffd_send_hellos(); add_interval(&next_hello, FFD_HELLO_INTERVAL); continue; } struct pollfd fds[1]; fds[0].fd = sockfd; fds[0].events = POLLIN; poll(fds, 1, timeout); update_time(); if (fds[0].revents & POLLIN) receive_packet(); } return 0; }