Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LWIPInterfacePPP.cpp Source File

LWIPInterfacePPP.cpp

00001 /*
00002  * Copyright (c) 2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "lwip/tcpip.h"
00019 #include "lwip/tcp.h"
00020 #include "lwip/ip.h"
00021 #include "lwip/ip_addr.h"
00022 #include "lwip/dns.h"
00023 #include "netif/etharp.h"
00024 #include "lwip/ethip6.h"
00025 #include "netsocket/nsapi_types.h"
00026 #include "netsocket/PPP.h"
00027 #include "LWIPStack.h"
00028 #include "lwip_tools.h"
00029 
00030 #if PPP_SUPPORT
00031 
00032 #if PPP_IPV4_SUPPORT && LWIP_IPV4
00033 err_t LWIP::Interface::ppp4_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
00034 {
00035     /* Increase reference counter since lwip stores handle to pbuf and frees
00036        it after output */
00037     pbuf_ref(p);
00038 
00039     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00040     bool ret = mbed_if->ppp->link_out(p, IPV4_STACK);
00041     return ret ? ERR_OK : ERR_IF;
00042 }
00043 #endif
00044 #if PPP_IPV6_SUPPORT && LWIP_IPV6
00045 err_t LWIP::Interface::ppp6_output(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
00046 {
00047     /* Increase reference counter since lwip stores handle to pbuf and frees
00048        it after output */
00049     pbuf_ref(p);
00050 
00051     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00052     bool ret = mbed_if->ppp->link_out(p, IPV6_STACK);
00053     return ret ? ERR_OK : ERR_IF;
00054 }
00055 #endif
00056 void LWIP::Interface::ppp_input(net_stack_mem_buf_t *buf)
00057 {
00058     struct pbuf *p = static_cast<struct pbuf *>(buf);
00059 
00060     /* pass all packets to IP stack input */
00061     if (netif.input(p, &netif) != ERR_OK) {
00062         LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n"));
00063 
00064         pbuf_free(p);
00065     }
00066 }
00067 
00068 void LWIP::Interface::ppp_state_change(bool up)
00069 {
00070     if (up) {
00071 #if PPP_IPV6_SUPPORT && LWIP_IPV6
00072         const nsapi_addr_t *ipv6_addr = LWIP::Interface::ppp->get_ip_address(NSAPI_IPv6 );
00073 
00074         ip_addr_t ip_addr;
00075         if (ipv6_addr && convert_mbed_addr_to_lwip(&ip_addr, ipv6_addr)) {
00076             netif_ip6_addr_set(&netif, 0, ip_2_ip6(&ip_addr));
00077             netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_PREFERRED);
00078         }
00079 #endif
00080 
00081 #if PPP_IPV4_SUPPORT && LWIP_IPV4
00082         const nsapi_addr_t *ipv4_addr = LWIP::Interface::ppp->get_ip_address(NSAPI_IPv4 );
00083         if (ipv4_addr) {
00084             ip_addr_t ip_addr;
00085             ip_addr_t netmask;
00086             ip_addr_t gateway;
00087 
00088             int conv_ip = 0;
00089             if (convert_mbed_addr_to_lwip(&ip_addr, ipv4_addr)) {
00090                 conv_ip++;
00091             }
00092 
00093             const nsapi_addr_t *ipv4_netmask = LWIP::Interface::ppp->get_netmask();
00094             if (ipv4_netmask && convert_mbed_addr_to_lwip(&netmask, ipv4_netmask)) {
00095                 conv_ip++;
00096             }
00097 
00098             const nsapi_addr_t *ipv4_gateway = LWIP::Interface::ppp->get_gateway();
00099             if (ipv4_gateway && convert_mbed_addr_to_lwip(&gateway, ipv4_gateway)) {
00100                 conv_ip++;
00101             }
00102 
00103             if (conv_ip == 3) {
00104                 netif_set_addr(&netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gateway));
00105             }
00106 
00107             unsigned char dns_index = 0;
00108             // If default interface set default DNS addresses, otherwise interface specific
00109             struct netif *dns_netif = &netif;
00110             if (netif_check_default(&netif)) {
00111                 dns_netif = NULL;
00112             }
00113             for (unsigned char index = 0; index < 2; index++) {
00114                 ip_addr_t dns_server;
00115                 const nsapi_addr_t *ipv4_dns_server = LWIP::Interface::ppp->get_dns_server(index);
00116                 if (ipv4_dns_server && convert_mbed_addr_to_lwip(&dns_server, ipv4_dns_server)) {
00117                     dns_setserver(dns_index++, &dns_server, dns_netif);
00118                 }
00119             }
00120         }
00121 #endif
00122         // Read negotiated MTU
00123         uint32_t mtu = LWIP::Interface::ppp->get_mtu_size();
00124         netif.mtu = mtu;
00125 #if PPP_IPV6_SUPPORT && LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES
00126         netif.mtu6 = mtu;
00127 #endif
00128         tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, &netif, 1);
00129     } else {
00130         tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, &netif, 1);
00131     }
00132 }
00133 
00134 err_t LWIP::Interface::ppp_if_init(struct netif *netif)
00135 {
00136     int err = ERR_OK;
00137     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00138 
00139     mbed_if->ppp->set_memory_manager(*mbed_if->memory_manager);
00140     mbed_if->ppp->set_link_input_cb(mbed::callback(mbed_if, &LWIP::Interface::ppp_input));
00141     mbed_if->ppp->set_link_state_cb(mbed::callback(mbed_if, &LWIP::Interface::ppp_state_change));
00142 
00143     /* Interface capabilities */
00144     netif->flags = NETIF_FLAG_BROADCAST;
00145 
00146     if (!mbed_if->ppp->power_up()) {
00147         err = ERR_IF;
00148     }
00149 
00150     netif->mtu = mbed_if->ppp->get_mtu_size();
00151     mbed_if->ppp->get_ifname(netif->name, NSAPI_INTERFACE_PREFIX_SIZE);
00152 
00153 #if PPP_IPV4_SUPPORT && LWIP_IPV4
00154     netif->output = &LWIP::Interface::ppp4_output;
00155 #endif /* PPP_IPV4_SUPPORT */
00156 #if PPP_IPV6_SUPPORT && LWIP_IPV6
00157     netif->output_ip6 = &LWIP::Interface::ppp6_output;
00158 #endif
00159     netif->linkoutput = NULL;
00160 
00161     return err;
00162 }
00163 
00164 #endif
00165 
00166