|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 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 "netif/etharp.h" |
| 22 | +#include "lwip/ethip6.h" |
| 23 | +#include "netsocket/nsapi_types.h" |
| 24 | +#include "netsocket/L3IP.h" |
| 25 | + |
| 26 | +#include "LWIPStack.h" |
| 27 | + |
| 28 | +#if LWIP_L3IP |
| 29 | + |
| 30 | +err_t LWIP::Interface::l3ip_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) |
| 31 | +{ |
| 32 | + /* Increase reference counter since lwip stores handle to pbuf and frees |
| 33 | + it after output */ |
| 34 | + pbuf_ref(p); |
| 35 | + |
| 36 | + LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state); |
| 37 | + bool ret = mbed_if->l3ip->link_out(p); |
| 38 | + return ret ? ERR_OK : ERR_IF; |
| 39 | +} |
| 40 | + |
| 41 | +void LWIP::Interface::l3ip_input(net_stack_mem_buf_t *buf) |
| 42 | +{ |
| 43 | + struct pbuf *p = static_cast<struct pbuf *>(buf); |
| 44 | + |
| 45 | + /* pass all packets to IP stack input */ |
| 46 | + if (netif.input(p, &netif) != ERR_OK) { |
| 47 | + LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n")); |
| 48 | + |
| 49 | + pbuf_free(p); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void LWIP::Interface::l3ip_state_change(bool up) |
| 54 | +{ |
| 55 | + if (up) { |
| 56 | + tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, &netif, 1); |
| 57 | + } else { |
| 58 | + tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, &netif, 1); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#if LWIP_IGMP |
| 63 | + |
| 64 | +#include "lwip/igmp.h" |
| 65 | +/** |
| 66 | + * IPv4 address filtering setup. |
| 67 | + * |
| 68 | + * \param[in] netif the lwip network interface structure |
| 69 | + * \param[in] group IPv4 group to modify |
| 70 | + * \param[in] action |
| 71 | + * \return ERR_OK or error code |
| 72 | + */ |
| 73 | +err_t LWIP::Interface::l3ip_multicast_ipv4_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action) |
| 74 | +{ |
| 75 | + LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state); |
| 76 | + SocketAddress addr(&group, NSAPI_IPv6); |
| 77 | + switch (action) { |
| 78 | + case NETIF_ADD_MAC_FILTER: { |
| 79 | + mbed_if->l3ip->add_ipv4_multicast_group(addr.get_ip_address()); |
| 80 | + return ERR_OK; |
| 81 | + } |
| 82 | + case NETIF_DEL_MAC_FILTER: |
| 83 | + mbed_if->l3ip->remove_ipv4_multicast_group(addr.get_ip_address()); |
| 84 | + return ERR_OK; |
| 85 | + default: |
| 86 | + return ERR_ARG; |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | +#endif |
| 91 | + |
| 92 | +#if LWIP_IPV6_MLD |
| 93 | + |
| 94 | +#include "lwip/mld6.h" |
| 95 | +/** |
| 96 | + * IPv6 address filtering setup. |
| 97 | + * |
| 98 | + * \param[in] netif the lwip network interface structure |
| 99 | + * \param[in] group IPv6 group to modify |
| 100 | + * \param[in] action |
| 101 | + * \return ERR_OK or error code |
| 102 | + */ |
| 103 | +err_t LWIP::Interface::l3ip_multicast_ipv6_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action) |
| 104 | +{ |
| 105 | + LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state); |
| 106 | + SocketAddress addr(&group, NSAPI_IPv6); |
| 107 | + switch (action) { |
| 108 | + case NETIF_ADD_MAC_FILTER: { |
| 109 | + |
| 110 | + mbed_if->l3ip->add_ipv6_multicast_group(addr.get_ip_address()); |
| 111 | + return ERR_OK; |
| 112 | + } |
| 113 | + case NETIF_DEL_MAC_FILTER: |
| 114 | + mbed_if->l3ip->remove_ipv6_multicast_group(addr.get_ip_address()); |
| 115 | + return ERR_OK; |
| 116 | + default: |
| 117 | + return ERR_ARG; |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | +#endif |
| 122 | + |
| 123 | +err_t LWIP::Interface::l3ip_if_init(struct netif *netif) |
| 124 | +{ |
| 125 | + int err = ERR_OK; |
| 126 | + LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state); |
| 127 | + |
| 128 | + mbed_if->l3ip->set_memory_manager(*mbed_if->memory_manager); |
| 129 | + mbed_if->l3ip->set_link_input_cb(mbed::callback(mbed_if, &LWIP::Interface::l3ip_input)); |
| 130 | + mbed_if->l3ip->set_link_state_cb(mbed::callback(mbed_if, &LWIP::Interface::l3ip_state_change)); |
| 131 | + |
| 132 | + /* Interface capabilities */ |
| 133 | + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET; |
| 134 | + |
| 135 | + if (!mbed_if->l3ip->power_up()) { |
| 136 | + err = ERR_IF; |
| 137 | + } |
| 138 | + |
| 139 | + netif->mtu = mbed_if->l3ip->get_mtu_size(); |
| 140 | + mbed_if->l3ip->get_ifname(netif->name, NSAPI_INTERFACE_NAME_SIZE); |
| 141 | + |
| 142 | +#if LWIP_IPV4 |
| 143 | + netif->output = &LWIP::Interface::l3ip_output; |
| 144 | +#if LWIP_IGMP |
| 145 | + netif->igmp_mac_filter = &LWIP::Interface::l3ip_multicast_ipv4_filter; |
| 146 | + netif->flags |= NETIF_FLAG_IGMP; |
| 147 | +#endif /* LWIP_IGMP */ |
| 148 | +#endif /* LWIP_IPV4 */ |
| 149 | +#if LWIP_IPV6 |
| 150 | + //netif->output_ip6 = ethip6_output;//to be done |
| 151 | +#if LWIP_IPV6_MLD |
| 152 | + netif->mld_mac_filter = &LWIP::Interface::l3ip_multicast_ipv6_filter; |
| 153 | + netif->flags |= NETIF_FLAG_MLD6; |
| 154 | +#else |
| 155 | +// Would need to enable all multicasts here - no API in fsl_enet to do that |
| 156 | +#error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system" |
| 157 | +#endif |
| 158 | +#endif |
| 159 | + |
| 160 | + netif->linkoutput = NULL; |
| 161 | + |
| 162 | + return err; |
| 163 | +} |
| 164 | + |
| 165 | +#endif |
| 166 | + |
0 commit comments