2016-03-30 12:28:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
2020-03-11 17:38:03 +01:00
|
|
|
#include <kernel_defines.h>
|
|
|
|
|
2017-08-09 16:30:23 +02:00
|
|
|
#include "net/gnrc/ipv6/nib.h"
|
2016-03-30 12:28:39 +02:00
|
|
|
#include "net/gnrc/ipv6.h"
|
|
|
|
#include "net/gnrc/netapi.h"
|
2017-11-16 18:06:46 +01:00
|
|
|
#include "net/gnrc/netif.h"
|
2017-11-29 13:51:05 +01:00
|
|
|
#include "net/gnrc/rpl.h"
|
2016-03-30 12:28:39 +02:00
|
|
|
#include "net/ipv6/addr.h"
|
2017-02-15 13:07:34 +01:00
|
|
|
#include "net/netdev.h"
|
2016-03-30 12:28:39 +02:00
|
|
|
#include "net/netopt.h"
|
|
|
|
|
|
|
|
#include "net/uhcp.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "fmt.h"
|
|
|
|
|
|
|
|
static kernel_pid_t gnrc_border_interface;
|
|
|
|
static kernel_pid_t gnrc_wireless_interface;
|
|
|
|
|
|
|
|
static void set_interface_roles(void)
|
|
|
|
{
|
2017-11-16 18:06:46 +01:00
|
|
|
gnrc_netif_t *netif = NULL;
|
2016-03-30 12:28:39 +02:00
|
|
|
|
2017-11-16 18:06:46 +01:00
|
|
|
while ((netif = gnrc_netif_iter(netif))) {
|
2017-07-27 15:26:49 +02:00
|
|
|
kernel_pid_t dev = netif->pid;
|
2016-04-11 17:02:06 +02:00
|
|
|
int is_wired = gnrc_netapi_get(dev, NETOPT_IS_WIRED, 0, NULL, 0);
|
|
|
|
if ((!gnrc_border_interface) && (is_wired == 1)) {
|
2017-08-09 16:30:23 +02:00
|
|
|
ipv6_addr_t addr, defroute = IPV6_ADDR_UNSPECIFIED;
|
2016-03-30 12:28:39 +02:00
|
|
|
gnrc_border_interface = dev;
|
|
|
|
|
|
|
|
ipv6_addr_from_str(&addr, "fe80::2");
|
2020-03-30 21:46:33 +02:00
|
|
|
gnrc_netapi_set(dev, NETOPT_IPV6_ADDR, 64 << 8, &addr,
|
|
|
|
sizeof(addr));
|
2016-03-30 12:28:39 +02:00
|
|
|
ipv6_addr_from_str(&addr, "fe80::1");
|
2017-11-17 13:44:18 +01:00
|
|
|
gnrc_ipv6_nib_ft_add(&defroute, IPV6_ADDR_BIT_LEN, &addr, dev, 0);
|
2016-03-30 12:28:39 +02:00
|
|
|
}
|
2016-04-11 17:02:06 +02:00
|
|
|
else if ((!gnrc_wireless_interface) && (is_wired != 1)) {
|
2016-03-30 12:28:39 +02:00
|
|
|
gnrc_wireless_interface = dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gnrc_border_interface && gnrc_wireless_interface) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 21:46:33 +02:00
|
|
|
LOG_INFO("gnrc_uhcpc: Using %u as border interface and %u as wireless "
|
|
|
|
"interface.\n", gnrc_border_interface, gnrc_wireless_interface);
|
2016-03-30 12:28:39 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 21:46:33 +02:00
|
|
|
void uhcp_handle_prefix(uint8_t *prefix, uint8_t prefix_len, uint16_t lifetime,
|
|
|
|
uint8_t *src, uhcp_iface_t iface)
|
2016-03-30 12:28:39 +02:00
|
|
|
{
|
2021-07-22 17:01:21 +02:00
|
|
|
int idx;
|
|
|
|
gnrc_netif_t *wireless;
|
2016-04-04 15:00:11 +02:00
|
|
|
(void)src;
|
|
|
|
|
2016-03-30 12:28:39 +02:00
|
|
|
if (!gnrc_wireless_interface) {
|
2020-03-30 21:46:33 +02:00
|
|
|
LOG_WARNING("gnrc_uhcpc: uhcp_handle_prefix(): received prefix, but "
|
|
|
|
"don't know any wireless interface\n");
|
2016-03-30 12:28:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:00:11 +02:00
|
|
|
if ((kernel_pid_t)iface != gnrc_border_interface) {
|
2020-03-30 21:46:33 +02:00
|
|
|
LOG_WARNING("gnrc_uhcpc: uhcp_handle_prefix(): received prefix from "
|
|
|
|
"unexpected interface\n");
|
2016-03-30 12:28:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:01:21 +02:00
|
|
|
wireless = gnrc_netif_get_by_pid(gnrc_wireless_interface);
|
|
|
|
idx = gnrc_netif_ipv6_add_prefix(wireless, (ipv6_addr_t *)prefix, prefix_len,
|
|
|
|
lifetime, lifetime);
|
|
|
|
if (idx >= 0) {
|
2021-08-18 17:06:47 +02:00
|
|
|
/* start advertising subnet obtained via UHCP */
|
|
|
|
gnrc_ipv6_nib_change_rtr_adv_iface(wireless, true);
|
|
|
|
/* configure this router as RPL root */
|
2021-07-22 17:01:21 +02:00
|
|
|
gnrc_rpl_configure_root(wireless, &wireless->ipv6.addrs[idx]);
|
2017-11-29 13:51:05 +01:00
|
|
|
}
|
2016-03-30 12:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern void uhcp_client(uhcp_iface_t iface);
|
|
|
|
|
2020-03-30 21:46:33 +02:00
|
|
|
static char _uhcp_client_stack[THREAD_STACKSIZE_DEFAULT +
|
|
|
|
THREAD_EXTRA_STACKSIZE_PRINTF];
|
2016-03-30 12:28:39 +02:00
|
|
|
static msg_t _uhcp_msg_queue[4];
|
|
|
|
|
|
|
|
static void* uhcp_client_thread(void *arg)
|
|
|
|
{
|
2016-04-04 15:00:11 +02:00
|
|
|
(void)arg;
|
|
|
|
|
2019-07-18 15:16:43 +02:00
|
|
|
msg_init_queue(_uhcp_msg_queue, ARRAY_SIZE(_uhcp_msg_queue));
|
2016-03-30 12:28:39 +02:00
|
|
|
uhcp_client(gnrc_border_interface);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void auto_init_gnrc_uhcpc(void)
|
|
|
|
{
|
|
|
|
set_interface_roles();
|
|
|
|
|
2016-04-04 15:12:44 +02:00
|
|
|
/* only start client if more than one interface is given */
|
|
|
|
if (! (gnrc_border_interface && gnrc_wireless_interface)) {
|
2016-04-11 17:02:06 +02:00
|
|
|
LOG_WARNING("gnrc_uhcpc: only one interface found, skipping setup.\n");
|
2016-04-04 15:12:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:28:39 +02:00
|
|
|
/* initiate uhcp client */
|
|
|
|
thread_create(_uhcp_client_stack, sizeof(_uhcp_client_stack),
|
|
|
|
THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,
|
|
|
|
uhcp_client_thread, NULL, "uhcp");
|
|
|
|
}
|