forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LWIPInterfaceEMAC.cpp
185 lines (159 loc) · 5.68 KB
/
LWIPInterfaceEMAC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lwip/tcpip.h"
#include "lwip/tcp.h"
#include "lwip/ip.h"
#include "netif/etharp.h"
#include "lwip/ethip6.h"
#include "netsocket/nsapi_types.h"
#include "netsocket/EMAC.h"
#include "LWIPStack.h"
#if LWIP_ETHERNET
err_t LWIP::Interface::emac_low_level_output(struct netif *netif, struct pbuf *p)
{
/* Increase reference counter since lwip stores handle to pbuf and frees
it after output */
pbuf_ref(p);
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
bool ret = mbed_if->emac->link_out(p);
return ret ? ERR_OK : ERR_IF;
}
void LWIP::Interface::emac_input(emac_mem_buf_t *buf)
{
struct pbuf *p = static_cast<struct pbuf *>(buf);
/* pass all packets to ethernet_input, which decides what packets it supports */
if (netif.input(p, &netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: IP input error\n"));
pbuf_free(p);
}
}
void LWIP::Interface::emac_state_change(bool up)
{
if (up) {
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, &netif, 1);
} else {
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, &netif, 1);
}
}
#if LWIP_IGMP
#include "lwip/igmp.h"
/**
* IPv4 address filtering setup.
*
* \param[in] netif the lwip network interface structure
* \param[in] group IPv4 group to modify
* \param[in] action
* \return ERR_OK or error code
*/
err_t LWIP::Interface::emac_igmp_mac_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action)
{
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
switch (action) {
case NETIF_ADD_MAC_FILTER: {
uint32_t group23 = ntohl(group->addr) & 0x007FFFFF;
uint8_t addr[6];
addr[0] = LL_IP4_MULTICAST_ADDR_0;
addr[1] = LL_IP4_MULTICAST_ADDR_1;
addr[2] = LL_IP4_MULTICAST_ADDR_2;
addr[3] = group23 >> 16;
addr[4] = group23 >> 8;
addr[5] = group23;
mbed_if->emac->add_multicast_group(addr);
return ERR_OK;
}
case NETIF_DEL_MAC_FILTER:
/* As we don't reference count, silently ignore delete requests */
return ERR_OK;
default:
return ERR_ARG;
}
}
#endif
#if LWIP_IPV6_MLD
#include "lwip/mld6.h"
/**
* IPv6 address filtering setup.
*
* \param[in] netif the lwip network interface structure
* \param[in] group IPv6 group to modify
* \param[in] action
* \return ERR_OK or error code
*/
err_t LWIP::Interface::emac_mld_mac_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action)
{
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
switch (action) {
case NETIF_ADD_MAC_FILTER: {
uint32_t group32 = ntohl(group->addr[3]);
uint8_t addr[6];
addr[0] = LL_IP6_MULTICAST_ADDR_0;
addr[1] = LL_IP6_MULTICAST_ADDR_1;
addr[2] = group32 >> 24;
addr[3] = group32 >> 16;
addr[4] = group32 >> 8;
addr[5] = group32;
mbed_if->emac->add_multicast_group(addr);
return ERR_OK;
}
case NETIF_DEL_MAC_FILTER:
/* As we don't reference count, silently ignore delete requests */
return ERR_OK;
default:
return ERR_ARG;
}
}
#endif
err_t LWIP::Interface::emac_if_init(struct netif *netif)
{
int err = ERR_OK;
LWIP::Interface *mbed_if = static_cast<LWIP::Interface *>(netif->state);
mbed_if->emac->set_memory_manager(*mbed_if->memory_manager);
mbed_if->emac->set_link_input_cb(mbed::callback(mbed_if, &LWIP::Interface::emac_input));
mbed_if->emac->set_link_state_cb(mbed::callback(mbed_if, &LWIP::Interface::emac_state_change));
/* Interface capabilities */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
if (!mbed_if->emac->power_up()) {
err = ERR_IF;
}
netif->mtu = mbed_if->emac->get_mtu_size();
/* We have a default MAC address, so do don't force them to supply one */
netif->hwaddr_len = mbed_if->emac->get_hwaddr_size();
/* They may or may not update hwaddr with their address */
mbed_if->emac->get_hwaddr(netif->hwaddr);
/* Then we write back either what they gave us, or our default */
mbed_if->emac->set_hwaddr(netif->hwaddr);
mbed_if->emac->get_ifname(netif->name, NSAPI_INTERFACE_PREFIX_SIZE);
#if LWIP_IPV4
netif->output = etharp_output;
#if LWIP_IGMP
netif->igmp_mac_filter = &LWIP::Interface::emac_igmp_mac_filter;
netif->flags |= NETIF_FLAG_IGMP;
#endif /* LWIP_IGMP */
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#if LWIP_IPV6_MLD
netif->mld_mac_filter = &LWIP::Interface::emac_mld_mac_filter;
netif->flags |= NETIF_FLAG_MLD6;
#else
// Would need to enable all multicasts here - no API in fsl_enet to do that
#error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system"
#endif
#endif
netif->linkoutput = &LWIP::Interface::emac_low_level_output;
return err;
}
#endif