2013-06-22 05:11:53 +02:00
|
|
|
/**
|
|
|
|
* 6lowpan border router implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 INRIA.
|
|
|
|
*
|
|
|
|
* This file subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @ingroup sixlowpan
|
|
|
|
* @{
|
|
|
|
* @file sixlowborder.c
|
|
|
|
* @brief constraint node implementation for a 6lowpan border router
|
|
|
|
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
|
|
|
|
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2011-06-22 21:09:06 +02:00
|
|
|
#include <stdint.h>
|
2011-07-05 04:24:13 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2011-06-24 01:48:41 +02:00
|
|
|
#include <mutex.h>
|
2011-07-12 15:00:21 +02:00
|
|
|
#include <thread.h>
|
|
|
|
#include <msg.h>
|
2011-07-05 04:24:13 +02:00
|
|
|
|
|
|
|
#include <posix_io.h>
|
|
|
|
#include <board_uart0.h>
|
2011-07-25 17:00:18 +02:00
|
|
|
|
|
|
|
#include "bordermultiplex.h"
|
2011-07-25 16:33:24 +02:00
|
|
|
#include "flowcontrol.h"
|
2013-08-05 16:10:54 +02:00
|
|
|
#include "border.h"
|
|
|
|
#include "ip.h"
|
|
|
|
#include "icmp.h"
|
2011-06-22 21:09:06 +02:00
|
|
|
#include "serialnumber.h"
|
2013-08-05 16:10:54 +02:00
|
|
|
#include "error.h"
|
|
|
|
#include "sys/net/ieee802154/ieee802154_frame.h"
|
2011-10-13 04:31:07 +02:00
|
|
|
#include "sys/net/net_help/net_help.h"
|
2011-06-22 21:09:06 +02:00
|
|
|
|
2011-07-12 15:00:21 +02:00
|
|
|
#define READER_STACK_SIZE 512
|
|
|
|
|
2011-07-27 02:12:30 +02:00
|
|
|
ipv6_addr_t abr_addr;
|
|
|
|
|
2011-07-23 22:17:52 +02:00
|
|
|
char serial_reader_stack[READER_STACK_SIZE];
|
|
|
|
uint16_t serial_reader_pid;
|
2011-07-12 15:00:21 +02:00
|
|
|
|
2011-07-26 01:59:34 +02:00
|
|
|
uint8_t serial_out_buf[BORDER_BUFFER_SIZE];
|
|
|
|
uint8_t serial_in_buf[BORDER_BUFFER_SIZE];
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
uint8_t *get_serial_out_buffer(int offset)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (offset > BUFFER_SIZE) {
|
2011-07-26 01:59:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-26 01:59:34 +02:00
|
|
|
return &(serial_out_buf[offset]);
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
uint8_t *get_serial_in_buffer(int offset)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (offset > BUFFER_SIZE) {
|
2011-07-26 01:59:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-26 01:59:34 +02:00
|
|
|
return &(serial_in_buf[offset]);
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
uint16_t border_get_serial_reader()
|
|
|
|
{
|
2011-07-25 16:14:15 +02:00
|
|
|
return serial_reader_pid;
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
void serial_reader_f(void)
|
|
|
|
{
|
2011-07-12 15:00:21 +02:00
|
|
|
int main_pid = 0;
|
2011-07-11 18:26:12 +02:00
|
|
|
int bytes;
|
2011-07-12 15:00:21 +02:00
|
|
|
msg_t m;
|
2011-07-23 22:17:52 +02:00
|
|
|
border_packet_t *uart_buf;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-12 15:00:21 +02:00
|
|
|
posix_open(uart0_handler_pid, 0);
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-12 15:00:21 +02:00
|
|
|
msg_receive(&m);
|
|
|
|
main_pid = m.sender_pid;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
2011-07-12 15:00:21 +02:00
|
|
|
posix_open(uart0_handler_pid, 0);
|
2011-07-25 16:14:15 +02:00
|
|
|
bytes = readpacket(get_serial_in_buffer(0), BORDER_BUFFER_SIZE);
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (bytes < 0) {
|
2013-06-22 05:11:53 +02:00
|
|
|
switch(bytes) {
|
2013-06-24 22:37:35 +02:00
|
|
|
case (-SIXLOWERROR_ARRAYFULL): {
|
2011-07-11 18:26:12 +02:00
|
|
|
printf("ERROR: Array was full\n");
|
2011-07-05 04:24:13 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
default: {
|
2011-07-05 04:24:13 +02:00
|
|
|
printf("ERROR: unknown\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-05 04:24:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
uart_buf = (border_packet_t *)get_serial_in_buffer(0);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (uart_buf->empty == 0) {
|
|
|
|
if (uart_buf->type == BORDER_PACKET_CONF_TYPE) {
|
2013-06-22 05:11:53 +02:00
|
|
|
border_conf_header_t *conf_packet = (border_conf_header_t *)uart_buf;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (conf_packet->conftype == BORDER_CONF_SYN) {
|
2011-07-12 15:00:21 +02:00
|
|
|
m.content.ptr = (char *)conf_packet;
|
|
|
|
msg_send(&m, main_pid, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-23 22:17:52 +02:00
|
|
|
flowcontrol_deliver_from_uart(uart_buf, bytes);
|
2011-07-11 18:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
uint8_t border_initialize(transceiver_type_t trans, ipv6_addr_t *border_router_addr)
|
|
|
|
{
|
2011-07-25 17:02:42 +02:00
|
|
|
ipv6_addr_t addr;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-25 17:02:42 +02:00
|
|
|
serial_reader_pid = thread_create(
|
2013-06-22 05:11:53 +02:00
|
|
|
serial_reader_stack, READER_STACK_SIZE,
|
|
|
|
PRIORITY_MAIN - 1, CREATE_STACKTEST,
|
|
|
|
serial_reader_f, "serial_reader");
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (border_router_addr == NULL) {
|
2011-07-25 17:02:42 +02:00
|
|
|
border_router_addr = &addr;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-25 17:02:42 +02:00
|
|
|
addr = flowcontrol_init();
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
/* only allow addresses generated accoding to
|
|
|
|
* RFC 4944 (Section 6) & RFC 2464 (Section 4) from short address
|
2011-07-25 17:02:42 +02:00
|
|
|
* -- for now
|
|
|
|
*/
|
2013-06-24 22:37:35 +02:00
|
|
|
if (border_router_addr->uint16[4] != HTONS(IEEE_802154_PAN_ID ^ 0x0200) ||
|
2013-06-22 05:11:53 +02:00
|
|
|
border_router_addr->uint16[5] != HTONS(0x00FF) ||
|
|
|
|
border_router_addr->uint16[6] != HTONS(0xFE00)
|
|
|
|
) {
|
2011-07-25 17:02:42 +02:00
|
|
|
return SIXLOWERROR_ADDRESS;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
/* radio-address is 8-bit so this must be tested extra */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (border_router_addr->uint8[14] != 0) {
|
2011-07-25 17:02:42 +02:00
|
|
|
return SIXLOWERROR_ADDRESS;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
memcpy(&(abr_addr.uint8[0]), &(border_router_addr->uint8[0]), 16);
|
|
|
|
|
|
|
|
sixlowpan_init(trans, border_router_addr->uint8[15], 1);
|
|
|
|
|
2011-07-25 17:02:42 +02:00
|
|
|
ipv6_init_iface_as_router();
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-25 17:02:42 +02:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-06-24 14:11:30 +02:00
|
|
|
void border_send_ipv6_over_lowpan(ipv6_hdr_t *packet, uint8_t aro_flag, uint8_t sixco_flag)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
|
|
|
uint16_t offset = IPV6_HDR_LEN + HTONS(packet->length);
|
|
|
|
|
2011-07-09 19:06:28 +02:00
|
|
|
packet->flowlabel = HTONS(packet->flowlabel);
|
|
|
|
packet->length = HTONS(packet->length);
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2011-07-09 19:06:28 +02:00
|
|
|
memset(buffer, 0, BUFFER_SIZE);
|
2013-06-22 05:11:53 +02:00
|
|
|
memcpy(buffer + LL_HDR_LEN, packet, offset);
|
|
|
|
|
|
|
|
lowpan_init((ieee_802154_long_t *)&(packet->destaddr.uint16[4]), (uint8_t *)packet);
|
2011-07-09 19:06:28 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
void border_process_lowpan(void)
|
|
|
|
{
|
2011-07-10 22:49:41 +02:00
|
|
|
msg_t m;
|
2013-06-24 14:11:30 +02:00
|
|
|
ipv6_hdr_t *ipv6_buf;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
2011-07-09 19:19:31 +02:00
|
|
|
msg_receive(&m);
|
2013-06-24 14:11:30 +02:00
|
|
|
ipv6_buf = (ipv6_hdr_t *)m.content.ptr;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (ipv6_buf->nextheader == PROTO_NUM_ICMPV6) {
|
2011-07-26 13:41:40 +02:00
|
|
|
struct icmpv6_hdr_t *icmp_buf = (struct icmpv6_hdr_t *)(((uint8_t *)ipv6_buf) + IPV6_HDR_LEN);
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (icmp_buf->type == ICMP_REDIRECT) {
|
2011-07-26 13:41:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (icmpv6_demultiplex(icmp_buf) == 0) {
|
2011-07-26 13:41:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
/* Here, other ICMPv6 message types for ND may follow. */
|
2011-07-26 13:41:40 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
|
|
|
/* TODO: Bei ICMPv6-Paketen entsprechende LoWPAN-Optionen verarbeiten und entfernen */
|
2011-07-23 22:17:52 +02:00
|
|
|
multiplex_send_ipv6_over_uart(ipv6_buf);
|
2011-07-09 19:19:31 +02:00
|
|
|
}
|
|
|
|
}
|