Skip to content

Commit 399b517

Browse files
authored
Merge pull request #10974 from mikaleppanen/nano_ppp
Created PPP interface and PPP service classes to netsocket
2 parents 476ce09 + 4e60d2f commit 399b517

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+5063
-2131
lines changed

.astyleignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
^features/nanostack/sal-stack-nanostack
1717
^features/nanostack/targets
1818
^features/netsocket/emac-drivers
19+
^features/netsocket/ppp/include
20+
^features/netsocket/ppp/polarssl
21+
^features/netsocket/ppp/source
1922
^features/storage/filesystem/fat/ChaN
2023
^features/storage/filesystem/littlefs/littlefs/
2124
^features/unsupported/

features/lwipstack/LWIPInterface.cpp

Lines changed: 73 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include "lwip/dns.h"
3737
#include "lwip/udp.h"
3838

39-
#include "ppp_lwip.h"
40-
4139
#include "LWIPStack.h"
4240

4341
LWIP::Interface *LWIP::Interface::list;
@@ -352,7 +350,7 @@ char *LWIP::Interface::get_gateway(char *buf, nsapi_size_t buflen)
352350
LWIP::Interface::Interface() :
353351
hw(NULL), has_addr_state(0),
354352
connected(NSAPI_STATUS_DISCONNECTED),
355-
dhcp_started(false), dhcp_has_to_be_set(false), blocking(true), ppp(false)
353+
dhcp_started(false), dhcp_has_to_be_set(false), blocking(true), ppp_enabled(false)
356354
{
357355
memset(&netif, 0, sizeof netif);
358356

@@ -395,7 +393,7 @@ nsapi_error_t LWIP::add_ethernet_interface(EMAC &emac, bool default_if, OnboardN
395393
}
396394
interface->emac = &emac;
397395
interface->memory_manager = &memory_manager;
398-
interface->ppp = false;
396+
interface->ppp_enabled = false;
399397

400398
#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
401399
netif->interface.hwaddr[0] = MBED_MAC_ADDR_0;
@@ -452,7 +450,7 @@ nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetwo
452450
}
453451
interface->l3ip = &l3ip;
454452
interface->memory_manager = &memory_manager;
455-
interface->ppp = false;
453+
interface->ppp_enabled = false;
456454

457455

458456

@@ -462,7 +460,7 @@ nsapi_error_t LWIP::add_l3ip_interface(L3IP &l3ip, bool default_if, OnboardNetwo
462460
#if LWIP_IPV4
463461
0, 0, 0,
464462
#endif
465-
interface, &LWIP::Interface::l3ip_if_init, tcpip_input)) {
463+
interface, &LWIP::Interface::l3ip_if_init, ip_input)) {
466464
return NSAPI_ERROR_DEVICE_ERROR;
467465
}
468466

@@ -521,21 +519,27 @@ nsapi_error_t LWIP::remove_l3ip_interface(OnboardNetworkStack::Interface **inter
521519

522520
#endif //LWIP_L3IP
523521
}
524-
/* Internal API to preserve existing PPP functionality - revise to better match mbed_ipstak_add_ethernet_interface later */
525-
nsapi_error_t LWIP::_add_ppp_interface(void *hw, bool default_if, nsapi_ip_stack_t stack, LWIP::Interface **interface_out)
522+
523+
524+
nsapi_error_t LWIP::add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out)
526525
{
527-
#if LWIP_PPP_API
526+
#if PPP_SUPPORT
528527
Interface *interface = new (std::nothrow) Interface();
529528
if (!interface) {
530529
return NSAPI_ERROR_NO_MEMORY;
531530
}
532-
interface->hw = hw;
533-
interface->ppp = true;
531+
interface->ppp = &ppp;
532+
interface->memory_manager = &memory_manager;
533+
interface->ppp_enabled = true;
534534

535-
nsapi_error_t ret = ppp_lwip_if_init(hw, &interface->netif, stack);
536-
if (ret != NSAPI_ERROR_OK) {
537-
free(interface);
538-
return ret;
535+
// interface->netif.hwaddr_len = 0; should we set?
536+
537+
if (!netif_add(&interface->netif,
538+
#if LWIP_IPV4
539+
0, 0, 0,
540+
#endif
541+
interface, &LWIP::Interface::ppp_if_init, tcpip_input)) {
542+
return NSAPI_ERROR_DEVICE_ERROR;
539543
}
540544

541545
if (default_if) {
@@ -548,11 +552,62 @@ nsapi_error_t LWIP::_add_ppp_interface(void *hw, bool default_if, nsapi_ip_stack
548552

549553
*interface_out = interface;
550554

555+
//lwip_add_random_seed(seed); to do?
556+
557+
return NSAPI_ERROR_OK;
558+
559+
#else
560+
return NSAPI_ERROR_UNSUPPORTED;
561+
562+
#endif //PPP_SUPPORT
563+
}
564+
565+
nsapi_error_t LWIP::remove_ppp_interface(OnboardNetworkStack::Interface **interface_out)
566+
{
567+
#if PPP_SUPPORT
568+
if ((interface_out != NULL) && (*interface_out != NULL)) {
569+
570+
Interface *lwip = static_cast<Interface *>(*interface_out);
571+
Interface *node = lwip->list;
572+
573+
if (lwip->list != NULL) {
574+
if (lwip->list == lwip) {
575+
// Power down PPP service
576+
lwip->ppp->power_down();
577+
if (netif_is_link_up(&lwip->netif)) {
578+
// Wait PPP service to report link down
579+
osSemaphoreAcquire(lwip->unlinked, osWaitForever);
580+
}
581+
netif_remove(&node->netif);
582+
lwip->list = lwip->list->next;
583+
delete node;
584+
} else {
585+
while (node->next != NULL && node->next != lwip) {
586+
node = node->next;
587+
}
588+
if (node->next != NULL && node->next == lwip) {
589+
Interface *remove = node->next;
590+
// Power down PPP service
591+
remove->ppp->power_down();
592+
if (netif_is_link_up(&lwip->netif)) {
593+
// Wait PPP service to report link down
594+
osSemaphoreAcquire(lwip->unlinked, osWaitForever);
595+
}
596+
netif_remove(&remove->netif);
597+
node->next = node->next->next;
598+
delete remove;
599+
}
600+
}
601+
}
602+
}
603+
551604
return NSAPI_ERROR_OK;
552605
#else
553606
return NSAPI_ERROR_UNSUPPORTED;
554-
#endif //LWIP_PPP_API
607+
608+
#endif //PPP_SUPPORT
555609
}
610+
556611
void LWIP::set_default_interface(OnboardNetworkStack::Interface *interface)
557612
{
558613
if (interface) {
@@ -609,7 +664,7 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
609664

610665
#if LWIP_IPV4
611666
if (stack != IPV6_STACK) {
612-
if (!dhcp && !ppp) {
667+
if (!dhcp && !ppp_enabled) {
613668
ip4_addr_t ip_addr;
614669
ip4_addr_t netmask_addr;
615670
ip4_addr_t gw_addr;
@@ -629,23 +684,10 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
629684
client_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_CONNECTING);
630685
}
631686

632-
if (ppp) {
633-
err_t err = ppp_lwip_connect(hw);
634-
if (err) {
635-
connected = NSAPI_STATUS_DISCONNECTED;
636-
if (client_callback) {
637-
client_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED);
638-
}
639-
return err_remap(err);
640-
}
641-
}
642687

643688
if (!netif_is_link_up(&netif)) {
644689
if (blocking) {
645690
if (osSemaphoreAcquire(linked, LINK_TIMEOUT * 1000) != osOK) {
646-
if (ppp) {
647-
(void) ppp_lwip_disconnect(hw);
648-
}
649691
return NSAPI_ERROR_NO_CONNECTION;
650692
}
651693
}
@@ -665,9 +707,6 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
665707
// If doesn't have address
666708
if (!LWIP::get_ip_addr(true, &netif)) {
667709
if (osSemaphoreAcquire(has_any_addr, DHCP_TIMEOUT * 1000) != osOK) {
668-
if (ppp) {
669-
(void) ppp_lwip_disconnect(hw);
670-
}
671710
return NSAPI_ERROR_DHCP_FAILURE;
672711
}
673712
}
@@ -713,21 +752,7 @@ nsapi_error_t LWIP::Interface::bringdown()
713752
}
714753
#endif
715754

716-
if (ppp) {
717-
/* this is a blocking call, returns when PPP is properly closed */
718-
err_t err = ppp_lwip_disconnect(hw);
719-
if (err) {
720-
return err_remap(err);
721-
}
722-
MBED_ASSERT(!netif_is_link_up(&netif));
723-
/*if (netif_is_link_up(&netif)) {
724-
if (sys_arch_sem_wait(&unlinked, 15000) == SYS_ARCH_TIMEOUT) {
725-
return NSAPI_ERROR_DEVICE_ERROR;
726-
}
727-
}*/
728-
} else {
729-
netif_set_down(&netif);
730-
}
755+
netif_set_down(&netif);
731756

732757
#if LWIP_IPV6
733758
mbed_lwip_clear_ipv6_addresses(&netif);
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "lwip/tcpip.h"
19+
#include "lwip/tcp.h"
20+
#include "lwip/ip.h"
21+
#include "lwip/ip_addr.h"
22+
#include "lwip/dns.h"
23+
#include "netif/etharp.h"
24+
#include "lwip/ethip6.h"
25+
#include "netsocket/nsapi_types.h"
26+
#include "netsocket/PPP.h"
27+
#include "LWIPStack.h"
28+
#include "lwip_tools.h"
29+
30+
#if PPP_SUPPORT
31+
32+
#if PPP_IPV4_SUPPORT && LWIP_IPV4
33+
err_t LWIP::Interface::ppp4_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
34+
{
35+
/* Increase reference counter since lwip stores handle to pbuf and frees
36+
it after output */
37+
pbuf_ref(p);
38+
39+
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
40+
bool ret = mbed_if->ppp->link_out(p, IPV4_STACK);
41+
return ret ? ERR_OK : ERR_IF;
42+
}
43+
#endif
44+
#if PPP_IPV6_SUPPORT && LWIP_IPV6
45+
err_t LWIP::Interface::ppp6_output(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
46+
{
47+
/* Increase reference counter since lwip stores handle to pbuf and frees
48+
it after output */
49+
pbuf_ref(p);
50+
51+
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
52+
bool ret = mbed_if->ppp->link_out(p, IPV6_STACK);
53+
return ret ? ERR_OK : ERR_IF;
54+
}
55+
#endif
56+
void LWIP::Interface::ppp_input(net_stack_mem_buf_t *buf)
57+
{
58+
struct pbuf *p = static_cast<struct pbuf *>(buf);
59+
60+
/* pass all packets to IP stack input */
61+
if (netif.input(p, &netif) != ERR_OK) {
62+
LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n"));
63+
64+
pbuf_free(p);
65+
}
66+
}
67+
68+
void LWIP::Interface::ppp_state_change(bool up)
69+
{
70+
if (up) {
71+
#if PPP_IPV6_SUPPORT && LWIP_IPV6
72+
const nsapi_addr_t *ipv6_addr = LWIP::Interface::ppp->get_ip_address(NSAPI_IPv6);
73+
74+
ip_addr_t ip_addr;
75+
if (ipv6_addr && convert_mbed_addr_to_lwip(&ip_addr, ipv6_addr)) {
76+
netif_ip6_addr_set(&netif, 0, ip_2_ip6(&ip_addr));
77+
netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_PREFERRED);
78+
}
79+
#endif
80+
81+
#if PPP_IPV4_SUPPORT && LWIP_IPV4
82+
const nsapi_addr_t *ipv4_addr = LWIP::Interface::ppp->get_ip_address(NSAPI_IPv4);
83+
if (ipv4_addr) {
84+
ip_addr_t ip_addr;
85+
ip_addr_t netmask;
86+
ip_addr_t gateway;
87+
88+
int conv_ip = 0;
89+
if (convert_mbed_addr_to_lwip(&ip_addr, ipv4_addr)) {
90+
conv_ip++;
91+
}
92+
93+
const nsapi_addr_t *ipv4_netmask = LWIP::Interface::ppp->get_netmask();
94+
if (ipv4_netmask && convert_mbed_addr_to_lwip(&netmask, ipv4_netmask)) {
95+
conv_ip++;
96+
}
97+
98+
const nsapi_addr_t *ipv4_gateway = LWIP::Interface::ppp->get_gateway();
99+
if (ipv4_gateway && convert_mbed_addr_to_lwip(&gateway, ipv4_gateway)) {
100+
conv_ip++;
101+
}
102+
103+
if (conv_ip == 3) {
104+
netif_set_addr(&netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gateway));
105+
}
106+
107+
unsigned char dns_index = 0;
108+
// If default interface set default DNS addresses, otherwise interface specific
109+
struct netif *dns_netif = &netif;
110+
if (netif_check_default(&netif)) {
111+
dns_netif = NULL;
112+
}
113+
for (unsigned char index = 0; index < 2; index++) {
114+
ip_addr_t dns_server;
115+
const nsapi_addr_t *ipv4_dns_server = LWIP::Interface::ppp->get_dns_server(index);
116+
if (ipv4_dns_server && convert_mbed_addr_to_lwip(&dns_server, ipv4_dns_server)) {
117+
dns_setserver(dns_index++, &dns_server, dns_netif);
118+
}
119+
}
120+
}
121+
#endif
122+
// Read negotiated MTU
123+
uint32_t mtu = LWIP::Interface::ppp->get_mtu_size();
124+
netif.mtu = mtu;
125+
#if PPP_IPV6_SUPPORT && LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES
126+
netif.mtu6 = mtu;
127+
#endif
128+
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, &netif, 1);
129+
} else {
130+
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, &netif, 1);
131+
}
132+
}
133+
134+
err_t LWIP::Interface::ppp_if_init(struct netif *netif)
135+
{
136+
int err = ERR_OK;
137+
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
138+
139+
mbed_if->ppp->set_memory_manager(*mbed_if->memory_manager);
140+
mbed_if->ppp->set_link_input_cb(mbed::callback(mbed_if, &LWIP::Interface::ppp_input));
141+
mbed_if->ppp->set_link_state_cb(mbed::callback(mbed_if, &LWIP::Interface::ppp_state_change));
142+
143+
/* Interface capabilities */
144+
netif->flags = NETIF_FLAG_BROADCAST;
145+
146+
if (!mbed_if->ppp->power_up()) {
147+
err = ERR_IF;
148+
}
149+
150+
netif->mtu = mbed_if->ppp->get_mtu_size();
151+
mbed_if->ppp->get_ifname(netif->name, NSAPI_INTERFACE_PREFIX_SIZE);
152+
153+
#if PPP_IPV4_SUPPORT && LWIP_IPV4
154+
netif->output = &LWIP::Interface::ppp4_output;
155+
#endif /* PPP_IPV4_SUPPORT */
156+
#if PPP_IPV6_SUPPORT && LWIP_IPV6
157+
netif->output_ip6 = &LWIP::Interface::ppp6_output;
158+
#endif
159+
netif->linkoutput = NULL;
160+
161+
return err;
162+
}
163+
164+
#endif
165+
166+

0 commit comments

Comments
 (0)