Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LWIPInterfaceL3IP.cpp Source File

LWIPInterfaceL3IP.cpp

00001 /*
00002  * Copyright (c) 2018 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 "netif/etharp.h"
00022 #include "lwip/ethip6.h"
00023 #include "netsocket/nsapi_types.h"
00024 #include "netsocket/L3IP.h"
00025 
00026 #include "LWIPStack.h"
00027 
00028 #if LWIP_L3IP
00029 #if LWIP_IPV4
00030 err_t LWIP::Interface::l3ip4_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
00031 {
00032     /* Increase reference counter since lwip stores handle to pbuf and frees
00033        it after output */
00034     pbuf_ref(p);
00035 
00036     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00037     bool ret = mbed_if->l3ip->link_out(p);
00038     return ret ? ERR_OK : ERR_IF;
00039 }
00040 #endif
00041 #if LWIP_IPV6
00042 err_t LWIP::Interface::l3ip6_output(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
00043 {
00044     /* Increase reference counter since lwip stores handle to pbuf and frees
00045        it after output */
00046     pbuf_ref(p);
00047 
00048     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00049     bool ret = mbed_if->l3ip->link_out(p);
00050     return ret ? ERR_OK : ERR_IF;
00051 }
00052 #endif
00053 void LWIP::Interface::l3ip_input(net_stack_mem_buf_t *buf)
00054 {
00055     struct pbuf *p = static_cast<struct pbuf *>(buf);
00056 
00057     /* pass all packets to IP stack input */
00058     if (netif.input(p, &netif) != ERR_OK) {
00059         LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n"));
00060 
00061         pbuf_free(p);
00062     }
00063 }
00064 
00065 void LWIP::Interface::l3ip_state_change(bool up)
00066 {
00067     if (up) {
00068         tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, &netif, 1);
00069     } else {
00070         tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, &netif, 1);
00071     }
00072 }
00073 
00074 #if LWIP_IGMP
00075 
00076 #include "lwip/igmp.h"
00077 /**
00078  * IPv4 address filtering setup.
00079  *
00080  * \param[in] netif the lwip network interface structure
00081  * \param[in] group IPv4 group to modify
00082  * \param[in] action
00083  * \return ERR_OK or error code
00084  */
00085 err_t LWIP::Interface::l3ip_multicast_ipv4_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action)
00086 {
00087     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00088     SocketAddress addr(&group, NSAPI_IPv6 );
00089     switch (action) {
00090         case NETIF_ADD_MAC_FILTER: {
00091             mbed_if->l3ip->add_ipv4_multicast_group(addr.get_ip_address());
00092             return ERR_OK;
00093         }
00094         case NETIF_DEL_MAC_FILTER:
00095             mbed_if->l3ip->remove_ipv4_multicast_group(addr.get_ip_address());
00096             return ERR_OK;
00097         default:
00098             return ERR_ARG;
00099     }
00100 
00101 }
00102 #endif
00103 
00104 #if LWIP_IPV6_MLD
00105 
00106 #include "lwip/mld6.h"
00107 /**
00108  * IPv6 address filtering setup.
00109  *
00110  * \param[in] netif the lwip network interface structure
00111  * \param[in] group IPv6 group to modify
00112  * \param[in] action
00113  * \return ERR_OK or error code
00114  */
00115 err_t LWIP::Interface::l3ip_multicast_ipv6_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action)
00116 {
00117     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00118     SocketAddress addr(&group, NSAPI_IPv6 );
00119     switch (action) {
00120         case NETIF_ADD_MAC_FILTER: {
00121 
00122             mbed_if->l3ip->add_ipv6_multicast_group(addr.get_ip_address());
00123             return ERR_OK;
00124         }
00125         case NETIF_DEL_MAC_FILTER:
00126             mbed_if->l3ip->remove_ipv6_multicast_group(addr.get_ip_address());
00127             return ERR_OK;
00128         default:
00129             return ERR_ARG;
00130     }
00131 
00132 }
00133 #endif
00134 
00135 err_t LWIP::Interface::l3ip_if_init(struct netif *netif)
00136 {
00137     int err = ERR_OK;
00138     LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
00139 
00140     mbed_if->l3ip->set_memory_manager(*mbed_if->memory_manager);
00141     mbed_if->l3ip->set_link_input_cb(mbed::callback(mbed_if, &LWIP::Interface::l3ip_input));
00142     mbed_if->l3ip->set_link_state_cb(mbed::callback(mbed_if, &LWIP::Interface::l3ip_state_change));
00143 
00144     /* Interface capabilities */
00145     netif->flags = NETIF_FLAG_BROADCAST;
00146 
00147     if (!mbed_if->l3ip->power_up()) {
00148         err = ERR_IF;
00149     }
00150 
00151     netif->mtu = mbed_if->l3ip->get_mtu_size();
00152     mbed_if->l3ip->get_ifname(netif->name, NSAPI_INTERFACE_PREFIX_SIZE);
00153 
00154 #if LWIP_IPV4
00155     netif->output = &LWIP::Interface::l3ip4_output;
00156 #if LWIP_IGMP
00157     netif->igmp_mac_filter = &LWIP::Interface::l3ip_multicast_ipv4_filter;
00158     netif->flags |= NETIF_FLAG_IGMP;
00159 #endif /* LWIP_IGMP */
00160 #endif /* LWIP_IPV4 */
00161 #if LWIP_IPV6
00162     netif->output_ip6 = &LWIP::Interface::l3ip6_output;
00163 #if LWIP_IPV6_MLD
00164     netif->mld_mac_filter = &LWIP::Interface::l3ip_multicast_ipv6_filter;
00165     netif->flags |= NETIF_FLAG_MLD6;
00166 #else
00167 // Would need to enable all multicasts here - no API in fsl_enet to do that
00168 #error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system"
00169 #endif
00170 #endif
00171 
00172     netif->linkoutput = NULL;
00173 
00174     return err;
00175 }
00176 
00177 #endif
00178