245 lines
7 KiB
C
245 lines
7 KiB
C
/*
|
|
Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
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 "packet.h"
|
|
#include "tlv.h"
|
|
#include "tlv_types.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
#include <sys/socket.h>
|
|
#include <sys/uio.h>
|
|
|
|
|
|
static bool send_eth(const eth_addr_t *addr, unsigned ifindex, void *buf, size_t len) {
|
|
static const uint8_t zeros[46] = {0};
|
|
|
|
struct sockaddr_ll sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sll_family = AF_PACKET;
|
|
sa.sll_protocol = htons(FFD_PROTO);
|
|
sa.sll_ifindex = ifindex;
|
|
sa.sll_halen = ETH_ALEN;
|
|
memcpy(sa.sll_addr, addr->d, ETH_ALEN);
|
|
|
|
struct iovec vec[2] = {
|
|
{ .iov_base = buf, .iov_len = len },
|
|
{ .iov_base = (void*)zeros, .iov_len = sizeof(zeros) - len },
|
|
};
|
|
|
|
struct msghdr msg;
|
|
memset(&msg, 0, sizeof(msg));
|
|
msg.msg_name = &sa;
|
|
msg.msg_namelen = sizeof(sa);
|
|
msg.msg_iov = vec;
|
|
msg.msg_iovlen = (len < 46) ? 2 : 1;
|
|
|
|
|
|
while (sendmsg(sockfd, &msg, 0) < 0) {
|
|
if (errno != EINTR)
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void ffd_send_ack(ffd_iface_t *iface, ffd_neigh_t *neigh, uint16_t nonce) {
|
|
ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+FFD_PACKET_MAX);
|
|
|
|
packet->version_magic = htons(FFD_VERSION_MAGIC);
|
|
packet->len = 0;
|
|
|
|
ffd_tlv_ack_t *ack = ffd_tlv_add(packet, FFD_PACKET_MAX, TLV_ACK, sizeof(ffd_tlv_ack_t));
|
|
if (!ack)
|
|
return;
|
|
|
|
ack->nonce = htons(nonce);
|
|
|
|
if (!send_eth(&neigh->addr, iface->ifindex, packet, sizeof(ffd_packet_t)+ntohs(packet->len)))
|
|
fprintf(stderr, "send_eth: %m\n");
|
|
}
|
|
|
|
static void add_ihus(ffd_packet_t *packet, size_t max_len, const ffd_iface_t *iface) {
|
|
const ffd_neigh_t *neigh;
|
|
|
|
for (neigh = iface->neigh_list; neigh; neigh = neigh->next) {
|
|
ffd_tlv_ihu_t *ihu = ffd_tlv_add(packet, FFD_PACKET_MAX, TLV_IHU, sizeof(ffd_tlv_ihu_t)+sizeof(eth_addr_t));
|
|
if (!ihu)
|
|
return;
|
|
|
|
ihu->ae = ADDR_ENC_ETH;
|
|
ihu->reserved = 0;
|
|
ihu->rxcost = htons(ffd_neigh_get_rxcost(neigh));
|
|
ihu->interval = htons(FFD_IHU_INTERVAL);
|
|
memcpy(ihu->address, &neigh->addr, sizeof(eth_addr_t));
|
|
}
|
|
}
|
|
|
|
void ffd_send_hellos(void) {
|
|
ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+FFD_PACKET_MAX);
|
|
|
|
packet->version_magic = htons(FFD_VERSION_MAGIC);
|
|
packet->len = 0;
|
|
|
|
ffd_tlv_hello_t *hello = ffd_tlv_add(packet, FFD_PACKET_MAX, TLV_HELLO, sizeof(ffd_tlv_hello_t));
|
|
if (!hello)
|
|
return;
|
|
|
|
hello->reserved = 0;
|
|
hello->interval = htons(FFD_HELLO_INTERVAL);
|
|
|
|
uint16_t len = packet->len;
|
|
|
|
ffd_iface_t *iface;
|
|
for (iface = iface_list; iface; iface = iface->next) {
|
|
hello->seqno = htons(iface->seqno++);
|
|
|
|
packet->len = len;
|
|
|
|
add_ihus(packet, FFD_PACKET_MAX, iface);
|
|
|
|
if (!send_eth(&ffd_addr, iface->ifindex, packet, sizeof(ffd_packet_t)+ntohs(packet->len)))
|
|
fprintf(stderr, "send_eth: %m\n");
|
|
}
|
|
}
|
|
|
|
static bool add_node_id(ffd_packet_t *packet, size_t max_len, ffd_node_id_t node_id) {
|
|
ffd_tlv_node_id_t *tlv = ffd_tlv_add(packet, FFD_PACKET_MAX, TLV_NODE_ID, sizeof(ffd_tlv_node_id_t));
|
|
if (!tlv)
|
|
return false;
|
|
|
|
tlv->id = node_id;
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool add_update(ffd_packet_t *packet, size_t max_len, ffd_node_id_t *node_id, ffd_announce_t *announce, bool with_data) {
|
|
if (announce->len && !announce->data) {
|
|
/* incomplete announce, handle like non-existant announce */
|
|
return true;
|
|
}
|
|
|
|
uint16_t len = packet->len;
|
|
|
|
if (!node_id || !ffd_are_node_ids_equal(node_id, &announce->node)) {
|
|
if (!add_node_id(packet, max_len, announce->node))
|
|
return false;
|
|
|
|
if (node_id)
|
|
*node_id = announce->node;
|
|
}
|
|
|
|
ffd_tlv_update_t *update = ffd_tlv_add(packet, FFD_PACKET_MAX, TLV_UPDATE, sizeof(ffd_tlv_update_t)+announce->len);
|
|
if (!update) {
|
|
packet->len = len;
|
|
return false;
|
|
}
|
|
|
|
update->flags = 0;
|
|
update->reserved = 0;
|
|
update->interval = htons(FFD_UPDATE_INTERVAL);
|
|
update->seqno = htons(announce->metric.seqno);
|
|
update->metric = htons(announce->metric.metric);
|
|
update->type = htons(announce->type);
|
|
update->key = htons(announce->key);
|
|
|
|
if (announce->len) {
|
|
update->flags |= FFD_UPDATE_WITH_DATA;
|
|
|
|
if (with_data) {
|
|
memcpy(update->data, announce->data, announce->len);
|
|
}
|
|
}
|
|
|
|
if (ffd_is_metric_better(announce->metric, announce->feasibility_distance))
|
|
announce->feasibility_distance = announce->metric;
|
|
|
|
return true;
|
|
}
|
|
|
|
void ffd_send_update(ffd_iface_t *iface, ffd_neigh_t *neigh, ffd_announce_t *announce, bool with_data) {
|
|
ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+FFD_PACKET_MAX);
|
|
|
|
packet->version_magic = htons(FFD_VERSION_MAGIC);
|
|
packet->len = 0;
|
|
|
|
const eth_addr_t *addr = &ffd_addr;
|
|
|
|
if (neigh)
|
|
addr = &neigh->addr;
|
|
|
|
if (announce) {
|
|
add_update(packet, FFD_PACKET_MAX, NULL, announce, with_data);
|
|
}
|
|
else {
|
|
ffd_node_id_t node_id = FFD_NODE_ID_UNSPEC;
|
|
|
|
ffd_announce_t *a;
|
|
for (a = announce_list; a; a = a->next) {
|
|
if (!add_update(packet, FFD_PACKET_MAX, &node_id, a, with_data)) {
|
|
if (!send_eth(addr, iface->ifindex, packet, sizeof(ffd_packet_t)+ntohs(packet->len)))
|
|
fprintf(stderr, "send_eth: %m\n");
|
|
|
|
node_id = FFD_NODE_ID_UNSPEC;
|
|
packet->len = 0;
|
|
}
|
|
}
|
|
|
|
if (packet->len) {
|
|
if (!send_eth(addr, iface->ifindex, packet, sizeof(ffd_packet_t)+ntohs(packet->len)))
|
|
fprintf(stderr, "send_eth: %m\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
void ffd_send_announce_request(ffd_iface_t *iface, ffd_neigh_t *neigh, ffd_node_id_t node, uint16_t type, uint16_t key, bool with_data) {
|
|
ffd_packet_t *packet = alloca(sizeof(ffd_packet_t)+FFD_PACKET_MAX);
|
|
|
|
packet->version_magic = htons(FFD_VERSION_MAGIC);
|
|
packet->len = 0;
|
|
|
|
ffd_tlv_announce_req_t *req = ffd_tlv_add(packet, FFD_PACKET_MAX, TLV_ANNOUNCE_REQ, sizeof(ffd_tlv_announce_req_t));
|
|
if (!req)
|
|
return;
|
|
|
|
req->node = node;
|
|
req->flags = 0;
|
|
req->reserved = 0;
|
|
req->type = htons(type);
|
|
req->key = htons(key);
|
|
|
|
if (with_data)
|
|
req->flags |= FFD_UPDATE_WITH_DATA;
|
|
|
|
if (!send_eth(&ffd_addr, iface->ifindex, packet, sizeof(ffd_packet_t)+ntohs(packet->len)))
|
|
fprintf(stderr, "send_eth: %m\n");
|
|
}
|