Contiki-NG
Loading...
Searching...
No Matches
border-router-native.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32/**
33 * \file
34 * border-router
35 * \author
36 * Niclas Finne <nfi@sics.se>
37 * Joakim Eriksson <joakime@sics.se>
38 * Nicolas Tsiftes <nvt@sics.se>
39 */
40
41#include "contiki.h"
42#include "contiki-net.h"
43
44#include "net/routing/routing.h"
45#include "rpl-border-router.h"
46#include "cmd.h"
47#include "border-router.h"
48#include "border-router-cmds.h"
49#include "border-router-cbor.h"
50#include "tun6-net.h"
51#include "dev/radio.h"
52#include "net/mac/mac.h"
54
55#if BUILD_WITH_NAT64
56#include "nat64-platform.h"
57#endif /* BUILD_WITH_NAT64 */
58
59/*---------------------------------------------------------------------------*/
60/* Log configuration */
61#include "sys/log.h"
62#define LOG_MODULE "BR"
63#define LOG_LEVEL LOG_LEVEL_INFO
64
65#include <stdlib.h>
66#include <stdbool.h>
67
68extern long slip_sent;
69extern long slip_received;
70
71static bool is_mac_set;
72
73extern int contiki_argc;
74extern char **contiki_argv;
75
76CMD_HANDLERS(border_router_cmd_handler);
77
78PROCESS(border_router_process, "Border router process");
79
80/*---------------------------------------------------------------------------*/
81static void
82request_mac(void)
83{
84#if BORDER_ROUTER_SERIAL_RADIO
85 /* serialradio: ask the radio to report its EUI-64 so we can adopt it as our
86 own link-layer address (see border_router_set_mac()). */
88#else
89 write_to_slip((uint8_t *)"?M", 2);
90#endif
91}
92/*---------------------------------------------------------------------------*/
93void
94border_router_set_mac(const uint8_t *data)
95{
96 if(is_mac_set) {
97 /* only set MAC address once */
98 return;
99 }
100
101 memcpy(uip_lladdr.addr, data, sizeof(uip_lladdr.addr));
102 linkaddr_set_node_addr((linkaddr_t *)uip_lladdr.addr);
103
104 /* is this ok - should instead remove all addresses and
105 add them back again - a bit messy... ?*/
106 PROCESS_CONTEXT_BEGIN(&tcpip_process);
107 uip_ds6_init();
108 NETSTACK_ROUTING.init();
109 PROCESS_CONTEXT_END(&tcpip_process);
110
111 is_mac_set = true;
112}
113/*---------------------------------------------------------------------------*/
114void
115border_router_print_stat()
116{
117 printf("bytes received over SLIP: %ld\n", slip_received);
118 printf("bytes sent over SLIP: %ld\n", slip_sent);
119}
120/*---------------------------------------------------------------------------*/
121PROCESS_THREAD(border_router_process, ev, data)
122{
123 static struct etimer et;
124
126
127 is_mac_set = false;
128 prefix_set = 0;
129
131
132 process_start(&border_router_cmd_process, NULL);
133
134 LOG_INFO("RPL-Border router started\n");
135
136 slip_config_handle_arguments(contiki_argc, contiki_argv);
137
138 /* tun init is also responsible for setting up the SLIP connection */
139 tun_init();
140
141#if BUILD_WITH_NAT64
142 if(nat64_is_enabled()) {
143 if(!nat64_platform_init()) {
144 LOG_ERR("Failed to initialize NAT64\n");
145 }
146 }
147#endif /* BUILD_WITH_NAT64 */
148
149 while(!is_mac_set) {
151 request_mac();
153 }
154
155#if BORDER_ROUTER_SERIAL_RADIO
156 /* The radio's EUI-64 is now adopted as our link-layer address. Configure
157 the serial radio for border-router operation: matching PAN ID and
158 channel, then enable address-filtered, auto-ACKing router mode so that
159 unicast traffic to this node is received and acknowledged in hardware. */
160 br_cbor_send_set_param(0, RADIO_PARAM_PAN_ID, IEEE802154_PANID);
163#endif /* BORDER_ROUTER_SERIAL_RADIO */
164
165 const char *config_ipaddr = tun6_net_get_prefix();
166 if(config_ipaddr != NULL) {
167 uip_ipaddr_t prefix;
168
169 if(uiplib_ipaddrconv(config_ipaddr, &prefix)) {
170 LOG_INFO("Setting prefix ");
171 LOG_INFO_6ADDR(&prefix);
172 LOG_INFO_("\n");
173 set_prefix_64(&prefix);
174 } else {
175 LOG_ERR("Parse error: %s\n", config_ipaddr);
176 exit(EXIT_FAILURE);
177 }
178 }
179
180 print_local_addresses();
181
182 while(1) {
183 etimer_set(&et, CLOCK_SECOND * 2);
185 /* do anything here??? */
186 }
187
188 PROCESS_END();
189}
190/*---------------------------------------------------------------------------*/
CBOR-over-SLIP protocol for talking to the serialradio firmware (examples/serialradio) from the nativ...
void br_cbor_send_router_mode(uint8_t msg_id, bool enable)
Enable or disable border-router radio mode (ROUTER_MODE).
void br_cbor_send_get_addr64(uint8_t msg_id)
Request the radio's EUI-64 link-layer address (GET_ADDR64).
void br_cbor_send_set_param(uint8_t msg_id, uint16_t param, int32_t value)
Set a radio parameter on the serial radio (SET_PARAM).
Sets up some commands for the border router.
Border router header file.
Simple command handler.
802.15.4 frame creation and parsing functions
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:105
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition etimer.c:177
static void linkaddr_set_node_addr(linkaddr_t *addr)
Set the address of the current node.
Definition linkaddr.h:151
bool nat64_platform_init(void)
Initialize the platform layer.
Definition nat64-sock.c:690
bool nat64_is_enabled(void)
Check whether the NAT64 gateway has been enabled at runtime.
Definition nat64-sock.c:724
#define PROCESS(name, strname)
Declare a process.
Definition process.h:309
#define PROCESS_PAUSE()
Yield the process for a short while.
Definition process.h:223
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:122
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition process.h:159
#define PROCESS_END()
Define the end of a process.
Definition process.h:133
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition process.h:429
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:275
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition process.h:443
@ RADIO_PARAM_CHANNEL
Channel used for radio communication.
Definition radio.h:134
@ RADIO_PARAM_PAN_ID
The personal area network identifier (PAN ID), which is used by the h/w frame filtering functionality...
Definition radio.h:150
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition uiplib.h:71
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
void uip_ds6_init(void)
Initialize data structures.
Definition uip-ds6.c:115
Header file for the logging system.
MAC driver header file.
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
Definition mac.h:52
NAT64 platform interface — socket-based.
Header file for the radio API.
Routing driver header file.
A timer.
Definition etimer.h:79