Skip to content

Commit dd346ec

Browse files
author
Mika Leppänen
committed
Either ipv4 or ipv4v6 PPP and IP stacks are enabled based on 3GPP context
1 parent 457f940 commit dd346ec

File tree

10 files changed

+127
-65
lines changed

10 files changed

+127
-65
lines changed

features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ nsapi_error_t EthernetInterface::connect()
4949
return mbed_lwip_bringup_2(_dhcp, false,
5050
_ip_address[0] ? _ip_address : 0,
5151
_netmask[0] ? _netmask : 0,
52-
_gateway[0] ? _gateway : 0);
52+
_gateway[0] ? _gateway : 0,
53+
DEFAULT_STACK);
5354
}
5455

5556
nsapi_error_t EthernetInterface::disconnect()

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,10 @@ nsapi_error_t mbed_lwip_init(emac_interface_t *emac)
511511
// Backwards compatibility with people using DEVICE_EMAC
512512
nsapi_error_t mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
513513
{
514-
return mbed_lwip_bringup_2(dhcp, false, ip, netmask, gw);
514+
return mbed_lwip_bringup_2(dhcp, false, ip, netmask, gw, DEFAULT_STACK);
515515
}
516516

517-
nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const char *netmask, const char *gw)
517+
nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const char *netmask, const char *gw, const nsapi_ip_stack_t stack)
518518
{
519519
// Check if we've already connected
520520
if (lwip_connected) {
@@ -533,7 +533,7 @@ nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const cha
533533
}
534534
} else {
535535
if (ppp) {
536-
ret = ppp_lwip_if_init(&lwip_netif);
536+
ret = ppp_lwip_if_init(&lwip_netif, stack);
537537
} else {
538538
ret = mbed_lwip_emac_init(NULL);
539539
}
@@ -553,43 +553,49 @@ nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const cha
553553
netif_set_status_callback(&lwip_netif, mbed_lwip_netif_status_irq);
554554

555555
#if LWIP_IPV6
556-
if (lwip_netif.hwaddr_len == ETH_HWADDR_LEN) {
557-
netif_create_ip6_linklocal_address(&lwip_netif, 1/*from MAC*/);
558-
}
556+
if (stack != IPV4_STACK) {
557+
if (lwip_netif.hwaddr_len == ETH_HWADDR_LEN) {
558+
netif_create_ip6_linklocal_address(&lwip_netif, 1/*from MAC*/);
559+
}
559560

560561
#if LWIP_IPV6_MLD
561-
/*
562-
* For hardware/netifs that implement MAC filtering.
563-
* All-nodes link-local is handled by default, so we must let the hardware know
564-
* to allow multicast packets in.
565-
* Should set mld_mac_filter previously. */
566-
if (lwip_netif.mld_mac_filter != NULL) {
567-
ip6_addr_t ip6_allnodes_ll;
568-
ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
569-
lwip_netif.mld_mac_filter(&lwip_netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
570-
}
562+
/*
563+
* For hardware/netifs that implement MAC filtering.
564+
* All-nodes link-local is handled by default, so we must let the hardware know
565+
* to allow multicast packets in.
566+
* Should set mld_mac_filter previously. */
567+
if (lwip_netif.mld_mac_filter != NULL) {
568+
ip6_addr_t ip6_allnodes_ll;
569+
ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
570+
lwip_netif.mld_mac_filter(&lwip_netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
571+
}
571572
#endif /* LWIP_IPV6_MLD */
572573

573574
#if LWIP_IPV6_AUTOCONFIG
574-
/* IPv6 address autoconfiguration not enabled by default */
575-
lwip_netif.ip6_autoconfig_enabled = 1;
575+
/* IPv6 address autoconfiguration not enabled by default */
576+
lwip_netif.ip6_autoconfig_enabled = 1;
577+
} else {
578+
// Disable router solidifications
579+
lwip_netif.rs_count = 0;
580+
}
576581
#endif /* LWIP_IPV6_AUTOCONFIG */
577582
#endif // LWIP_IPV6
578583

579-
580584
#if LWIP_IPV4
581-
if (!dhcp && !ppp) {
582-
ip4_addr_t ip_addr;
583-
ip4_addr_t netmask_addr;
584-
ip4_addr_t gw_addr;
585-
586-
if (!inet_aton(ip, &ip_addr) ||
587-
!inet_aton(netmask, &netmask_addr) ||
588-
!inet_aton(gw, &gw_addr)) {
589-
return NSAPI_ERROR_PARAMETER;
590-
}
585+
if (stack != IPV6_STACK) {
586+
if (!dhcp && !ppp) {
587+
ip4_addr_t ip_addr;
588+
ip4_addr_t netmask_addr;
589+
ip4_addr_t gw_addr;
590+
591+
if (!inet_aton(ip, &ip_addr) ||
592+
!inet_aton(netmask, &netmask_addr) ||
593+
!inet_aton(gw, &gw_addr)) {
594+
return NSAPI_ERROR_PARAMETER;
595+
}
591596

592-
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
597+
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
598+
}
593599
}
594600
#endif
595601

@@ -614,13 +620,15 @@ nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const cha
614620
}
615621

616622
#if LWIP_DHCP
617-
// Connect to the network
618-
lwip_dhcp = dhcp;
619-
620-
if (lwip_dhcp) {
621-
err_t err = dhcp_start(&lwip_netif);
622-
if (err) {
623-
return NSAPI_ERROR_DHCP_FAILURE;
623+
if (stack != IPV6_STACK) {
624+
// Connect to the network
625+
lwip_dhcp = dhcp;
626+
627+
if (lwip_dhcp) {
628+
err_t err = dhcp_start(&lwip_netif);
629+
if (err) {
630+
return NSAPI_ERROR_DHCP_FAILURE;
631+
}
624632
}
625633
}
626634
#endif
@@ -636,17 +644,21 @@ nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const cha
636644
}
637645

638646
#if PREF_ADDR_TIMEOUT
639-
// If address is not for preferred stack waits a while to see
640-
// if preferred stack address is acquired
641-
if (!mbed_lwip_get_ip_addr(false, &lwip_netif)) {
642-
sys_arch_sem_wait(&lwip_netif_has_pref_addr, PREF_ADDR_TIMEOUT * 1000);
647+
if (stack != IPV4_STACK && stack != IPV6_STACK) {
648+
// If address is not for preferred stack waits a while to see
649+
// if preferred stack address is acquired
650+
if (!mbed_lwip_get_ip_addr(false, &lwip_netif)) {
651+
sys_arch_sem_wait(&lwip_netif_has_pref_addr, PREF_ADDR_TIMEOUT * 1000);
652+
}
643653
}
644654
#endif
645655
#if BOTH_ADDR_TIMEOUT
646-
// If addresses for both stacks are not available waits a while to
647-
// see if address for both stacks are acquired
648-
if (!(mbed_lwip_get_ipv4_addr(&lwip_netif) && mbed_lwip_get_ipv6_addr(&lwip_netif))) {
649-
sys_arch_sem_wait(&lwip_netif_has_both_addr, BOTH_ADDR_TIMEOUT * 1000);
656+
if (stack != IPV4_STACK && stack != IPV6_STACK) {
657+
// If addresses for both stacks are not available waits a while to
658+
// see if address for both stacks are acquired
659+
if (!(mbed_lwip_get_ipv4_addr(&lwip_netif) && mbed_lwip_get_ipv6_addr(&lwip_netif))) {
660+
sys_arch_sem_wait(&lwip_netif_has_both_addr, BOTH_ADDR_TIMEOUT * 1000);
661+
}
650662
}
651663
#endif
652664

features/FEATURE_LWIP/lwip-interface/lwip_stack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extern "C" {
2929
nsapi_error_t mbed_lwip_init(emac_interface_t *emac);
3030
nsapi_error_t mbed_lwip_emac_init(emac_interface_t *emac);
3131
nsapi_error_t mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw);
32-
nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const char *netmask, const char *gw);
32+
nsapi_error_t mbed_lwip_bringup_2(bool dhcp, bool ppp, const char *ip, const char *netmask, const char *gw, const nsapi_ip_stack_t stack);
3333
nsapi_error_t mbed_lwip_bringdown(void);
3434
nsapi_error_t mbed_lwip_bringdown_2(bool ppp);
3535

features/FEATURE_LWIP/lwip-interface/mbed_lib.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
"value": false,
3535
"macro_name": "NSAPI_PPP_AVAILABLE"
3636
},
37+
"ppp-ipv4-enabled": {
38+
"help": "Enable support for ipv4 PPP interface",
39+
"value": true,
40+
"macro_name": "NSAPI_PPP_IPV4_AVAILABLE"
41+
},
42+
"ppp-ipv6-enabled": {
43+
"help": "Enable support for ipv6 PPP interface",
44+
"value": false,
45+
"macro_name": "NSAPI_PPP_IPV6_AVAILABLE"
46+
},
3747
"use-mbed-trace": {
3848
"help": "Use mbed trace for debug, rather than printf",
3949
"value": false

features/FEATURE_LWIP/lwip-interface/ppp_lwip.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ extern "C" err_t ppp_lwip_disconnect()
313313
return ret;
314314
}
315315

316-
extern "C" nsapi_error_t ppp_lwip_if_init(struct netif *netif)
316+
extern "C" nsapi_error_t ppp_lwip_if_init(struct netif *netif, const nsapi_ip_stack_t stack)
317317
{
318318
if (!prepare_event_queue()) {
319319
return NSAPI_ERROR_NO_MEMORY;
@@ -330,9 +330,17 @@ extern "C" nsapi_error_t ppp_lwip_if_init(struct netif *netif)
330330
}
331331

332332
#if LWIP_IPV4
333-
ppp_set_usepeerdns(my_ppp_pcb, true);
333+
if (stack != IPV6_STACK) {
334+
ppp_set_usepeerdns(my_ppp_pcb, true);
335+
}
334336
#endif
335337

338+
if (stack == IPV4_STACK) {
339+
my_ppp_pcb->ipv6cp_disabled = true;
340+
} else if (stack == IPV6_STACK) {
341+
my_ppp_pcb->ipcp_disabled = true;
342+
}
343+
336344
return NSAPI_ERROR_OK;
337345
}
338346

@@ -341,7 +349,7 @@ nsapi_error_t nsapi_ppp_error_code()
341349
return connect_error_code;
342350
}
343351

344-
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, Callback<void(nsapi_error_t)> cb, const char *uname, const char *password)
352+
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, Callback<void(nsapi_error_t)> cb, const char *uname, const char *password, const nsapi_ip_stack_t stack)
345353
{
346354
if (my_stream) {
347355
return NSAPI_ERROR_PARAMETER;
@@ -354,7 +362,7 @@ nsapi_error_t nsapi_ppp_connect(FileHandle *stream, Callback<void(nsapi_error_t)
354362

355363
// mustn't start calling input until after connect -
356364
// attach deferred until ppp_lwip_connect, called from mbed_lwip_bringup
357-
nsapi_error_t retcode = mbed_lwip_bringup_2(false, true, NULL, NULL, NULL);
365+
nsapi_error_t retcode = mbed_lwip_bringup_2(false, true, NULL, NULL, NULL, stack);
358366

359367
if (retcode != NSAPI_ERROR_OK && connect_error_code != NSAPI_ERROR_OK) {
360368
return connect_error_code;
@@ -388,7 +396,7 @@ const char *nsapi_ppp_get_ip_addr(FileHandle *stream)
388396
}
389397
const char *nsapi_ppp_get_netmask(FileHandle *stream)
390398
{
391-
#if LWIP_IPV6
399+
#if !LWIP_IPV4
392400
return NULL;
393401
#endif
394402

@@ -403,7 +411,7 @@ const char *nsapi_ppp_get_netmask(FileHandle *stream)
403411
}
404412
const char *nsapi_ppp_get_gw_addr(FileHandle *stream)
405413
{
406-
#if LWIP_IPV6
414+
#if !LWIP_IPV4
407415
return NULL;
408416
#endif
409417

features/FEATURE_LWIP/lwip-interface/ppp_lwip.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern "C" {
3030
*
3131
* @return 0 for success and negative error codes for failure
3232
*/
33-
nsapi_error_t ppp_lwip_if_init(struct netif *netif);
33+
nsapi_error_t ppp_lwip_if_init(struct netif *netif, const nsapi_ip_stack_t stack);
3434

3535
/** Connects to a PPP pipe
3636
*
@@ -53,9 +53,9 @@ err_t ppp_lwip_disconnect(void);
5353
/**
5454
* Stubs in case LWIP PPP is not enabled
5555
*/
56-
#define ppp_lwip_if_init(netif) NSAPI_ERROR_UNSUPPORTED
57-
#define ppp_lwip_connect() ERR_IF
58-
#define ppp_lwip_disconnect() ERR_IF
56+
#define ppp_lwip_if_init(netif, stack) NSAPI_ERROR_UNSUPPORTED
57+
#define ppp_lwip_connect() ERR_IF
58+
#define ppp_lwip_disconnect() ERR_IF
5959
#endif //LWIP_PPP_API
6060
#ifdef __cplusplus
6161
}

features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ PPPCellularInterface::PPPCellularInterface(FileHandle *fh, bool debug)
262262
_pwd = NULL;
263263
_fh = fh;
264264
_debug_trace_on = debug;
265+
_stack = DEFAULT_STACK;
265266
dev_info.reg_status_csd = CSD_NOT_REGISTERED_NOT_SEARCHING;
266267
dev_info.reg_status_psd = PSD_NOT_REGISTERED_NOT_SEARCHING;
267268
dev_info.ppp_connection_up = false;
@@ -451,21 +452,38 @@ nsapi_error_t PPPCellularInterface::setup_context_and_credentials()
451452
return NSAPI_ERROR_PARAMETER;
452453
}
453454

454-
bool try_ipv6 = false;
455+
#if NSAPI_PPP_IPV4_AVAILABLE && NSAPI_PPP_IPV6_AVAILABLE
456+
const char ipv4v6_pdp_type[] = {"IPV4V6"};
457+
const char ipv4_pdp_type[] = {"IP"};
458+
const char *pdp_type = ipv4v6_pdp_type;
459+
_stack = IPV4V6_STACK;
460+
#elif NSAPI_PPP_IPV6_AVAILABLE
461+
const char pdp_type[] = {"IPV6"};
462+
#elif NSAPI_PPP_IPV4_AVAILABLE
463+
const char pdp_type[] = {"IP"};
464+
#endif
455465
const char *auth = _uname && _pwd ? "CHAP:" : "";
456466

457-
retry_without_ipv6:
467+
#if NSAPI_PPP_IPV4_AVAILABLE && NSAPI_PPP_IPV6_AVAILABLE
468+
retry_without_dual_stack:
469+
#endif
458470
success = _at->send("AT"
459471
"+FCLASS=0;" // set to connection (ATD) to data mode
460472
"+CGDCONT="CTX",\"%s\",\"%s%s\"",
461-
try_ipv6 ? "IPV4V6" : "IP", auth, _apn
473+
pdp_type, auth, _apn
462474
)
463475
&& _at->recv("OK");
464476

465-
if (!success && try_ipv6) {
466-
try_ipv6 = false;
467-
goto retry_without_ipv6;
477+
#if NSAPI_PPP_IPV4_AVAILABLE && NSAPI_PPP_IPV6_AVAILABLE
478+
if (_stack == IPV4V6_STACK) {
479+
if (!success) {
480+
// fallback to ipv4
481+
pdp_type = ipv4_pdp_type;
482+
_stack = IPV4_STACK;
483+
goto retry_without_dual_stack;
484+
}
468485
}
486+
#endif
469487

470488
if (!success) {
471489
_at->recv("OK");
@@ -662,7 +680,7 @@ nsapi_error_t PPPCellularInterface::connect()
662680
/* Initialize PPP
663681
* mbed_ppp_init() is a blocking call, it will block until
664682
* connected, or timeout after 30 seconds*/
665-
retcode = nsapi_ppp_connect(_fh, _connection_status_cb, _uname, _pwd);
683+
retcode = nsapi_ppp_connect(_fh, _connection_status_cb, _uname, _pwd, _stack);
666684
if (retcode == NSAPI_ERROR_OK) {
667685
dev_info.ppp_connection_up = true;
668686
}

features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ class PPPCellularInterface : public CellularBase {
252252
const char *_uname;
253253
const char *_pwd;
254254
bool _debug_trace_on;
255+
nsapi_ip_stack_t _stack;
255256
Callback<void(nsapi_error_t)> _connection_status_cb;
256257
void base_initialization();
257258
void setup_at_parser();

features/netsocket/nsapi_ppp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ NetworkStack *nsapi_ppp_get_stack();
3636
* @param status_cb Optional, user provided callback for connection status
3737
* @param uname Optional, username for the connection
3838
* @param pwd Optional, password for the connection
39+
* @param stack Optional, stack for the connection
3940
*
4041
* @return 0 on success, negative error code on failure
4142
*/
42-
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, Callback<void(nsapi_error_t)> status_cb=0, const char *uname=0, const char *pwd=0);
43+
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, Callback<void(nsapi_error_t)> status_cb=0, const char *uname=0, const char *pwd=0, const nsapi_ip_stack_t stack=DEFAULT_STACK);
4344

4445
/** Close a PPP connection
4546
*

features/netsocket/nsapi_types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,17 @@ typedef enum nsapi_socket_option {
216216
NSAPI_RCVBUF, /*!< Sets recv buffer size */
217217
} nsapi_socket_option_t;
218218

219+
/** Supported IP protocol versions of IP stack
220+
*
221+
* @enum nsapi_ip_stack
222+
*/
223+
typedef enum nsapi_ip_stack {
224+
DEFAULT_STACK = 0,
225+
IPV4_STACK,
226+
IPV6_STACK,
227+
IPV4V6_STACK
228+
} nsapi_ip_stack_t;
229+
219230
/* Backwards compatibility - previously didn't distinguish stack and socket options */
220231
typedef nsapi_socket_level_t nsapi_level_t;
221232
typedef nsapi_socket_option_t nsapi_option_t;

0 commit comments

Comments
 (0)