ReactOS  0.4.15-dev-976-g0f66c66
ethernetif.c
Go to the documentation of this file.
1 
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  * derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38 
39 /*
40  * This file is a skeleton for developing Ethernet network interface
41  * drivers for lwIP. Add code to the low_level functions and do a
42  * search-and-replace for the word "ethernetif" to replace it with
43  * something that better describes your network interface.
44  */
45 
46 #include "lwip/opt.h"
47 
48 #if 0 /* don't build, this is only a skeleton, see previous comment */
49 
50 #include "lwip/def.h"
51 #include "lwip/mem.h"
52 #include "lwip/pbuf.h"
53 #include <lwip/stats.h>
54 #include <lwip/snmp.h>
55 #include "netif/etharp.h"
56 #include "netif/ppp_oe.h"
57 
58 /* Define those to better describe your network interface. */
59 #define IFNAME0 'e'
60 #define IFNAME1 'n'
61 
68 struct ethernetif {
69  struct eth_addr *ethaddr;
70  /* Add whatever per-interface state that is needed here. */
71 };
72 
73 /* Forward declarations. */
74 static void ethernetif_input(struct netif *netif);
75 
83 static void
84 low_level_init(struct netif *netif)
85 {
86  struct ethernetif *ethernetif = netif->state;
87 
88  /* set MAC hardware address length */
89  netif->hwaddr_len = ETHARP_HWADDR_LEN;
90 
91  /* set MAC hardware address */
92  netif->hwaddr[0] = ;
93  ...
94  netif->hwaddr[5] = ;
95 
96  /* maximum transfer unit */
97  netif->mtu = 1500;
98 
99  /* device capabilities */
100  /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
102 
103  /* Do whatever else is needed to initialize interface. */
104 }
105 
122 static err_t
123 low_level_output(struct netif *netif, struct pbuf *p)
124 {
125  struct ethernetif *ethernetif = netif->state;
126  struct pbuf *q;
127 
128  initiate transfer();
129 
130 #if ETH_PAD_SIZE
131  pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
132 #endif
133 
134  for(q = p; q != NULL; q = q->next) {
135  /* Send the data from the pbuf to the interface, one pbuf at a
136  time. The size of the data in each pbuf is kept in the ->len
137  variable. */
138  send data from(q->payload, q->len);
139  }
140 
141  signal that packet should be sent();
142 
143 #if ETH_PAD_SIZE
144  pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
145 #endif
146 
147  LINK_STATS_INC(link.xmit);
148 
149  return ERR_OK;
150 }
151 
160 static struct pbuf *
161 low_level_input(struct netif *netif)
162 {
163  struct ethernetif *ethernetif = netif->state;
164  struct pbuf *p, *q;
165  u16_t len;
166 
167  /* Obtain the size of the packet and put it into the "len"
168  variable. */
169  len = ;
170 
171 #if ETH_PAD_SIZE
172  len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
173 #endif
174 
175  /* We allocate a pbuf chain of pbufs from the pool. */
177 
178  if (p != NULL) {
179 
180 #if ETH_PAD_SIZE
181  pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
182 #endif
183 
184  /* We iterate over the pbuf chain until we have read the entire
185  * packet into the pbuf. */
186  for(q = p; q != NULL; q = q->next) {
187  /* Read enough bytes to fill this pbuf in the chain. The
188  * available data in the pbuf is given by the q->len
189  * variable.
190  * This does not necessarily have to be a memcpy, you can also preallocate
191  * pbufs for a DMA-enabled MAC and after receiving truncate it to the
192  * actually received size. In this case, ensure the tot_len member of the
193  * pbuf is the sum of the chained pbuf len members.
194  */
195  read data into(q->payload, q->len);
196  }
197  acknowledge that packet has been read();
198 
199 #if ETH_PAD_SIZE
200  pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
201 #endif
202 
203  LINK_STATS_INC(link.recv);
204  } else {
205  drop packet();
206  LINK_STATS_INC(link.memerr);
207  LINK_STATS_INC(link.drop);
208  }
209 
210  return p;
211 }
212 
222 static void
223 ethernetif_input(struct netif *netif)
224 {
225  struct ethernetif *ethernetif;
226  struct eth_hdr *ethhdr;
227  struct pbuf *p;
228 
229  ethernetif = netif->state;
230 
231  /* move received packet into a new pbuf */
232  p = low_level_input(netif);
233  /* no packet could be read, silently ignore this */
234  if (p == NULL) return;
235  /* points to packet payload, which starts with an Ethernet header */
236  ethhdr = p->payload;
237 
238  switch (htons(ethhdr->type)) {
239  /* IP or ARP packet? */
240  case ETHTYPE_IP:
241  case ETHTYPE_ARP:
242 #if PPPOE_SUPPORT
243  /* PPPoE packet? */
244  case ETHTYPE_PPPOEDISC:
245  case ETHTYPE_PPPOE:
246 #endif /* PPPOE_SUPPORT */
247  /* full packet send to tcpip_thread to process */
248  if (netif->input(p, netif)!=ERR_OK)
249  { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
250  pbuf_free(p);
251  p = NULL;
252  }
253  break;
254 
255  default:
256  pbuf_free(p);
257  p = NULL;
258  break;
259  }
260 }
261 
274 err_t
275 ethernetif_init(struct netif *netif)
276 {
277  struct ethernetif *ethernetif;
278 
279  LWIP_ASSERT("netif != NULL", (netif != NULL));
280 
281  ethernetif = mem_malloc(sizeof(struct ethernetif));
282  if (ethernetif == NULL) {
283  LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
284  return ERR_MEM;
285  }
286 
287 #if LWIP_NETIF_HOSTNAME
288  /* Initialize interface hostname */
289  netif->hostname = "lwip";
290 #endif /* LWIP_NETIF_HOSTNAME */
291 
292  /*
293  * Initialize the snmp variables and counters inside the struct netif.
294  * The last argument should be replaced with your link speed, in units
295  * of bits per second.
296  */
297  NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
298 
299  netif->state = ethernetif;
300  netif->name[0] = IFNAME0;
301  netif->name[1] = IFNAME1;
302  /* We directly use etharp_output() here to save a function call.
303  * You can instead declare your own function an call etharp_output()
304  * from it if you have to do some checks before sending (e.g. if link
305  * is available...) */
306  netif->output = etharp_output;
307  netif->linkoutput = low_level_output;
308 
309  ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
310 
311  /* initialize the hardware */
312  low_level_init(netif);
313 
314  return ERR_OK;
315 }
316 
317 #endif /* 0 */
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:190
UCHAR packet[_PAGE_SIZE]
Definition: serial.c:53
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:511
#define NETIF_FLAG_LINK_UP
Definition: netif.h:84
void * mem_malloc(mem_size_t size)
Definition: mem.c:494
netif_output_fn output
Definition: netif.h:151
#define NETIF_DEBUG
Definition: opt.h:1905
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:207
int signal
Definition: except.c:82
u8_t hwaddr_len
Definition: netif.h:188
smooth NULL
Definition: ftsmooth.c:416
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
Definition: pbuf.h:54
s8_t err_t
Definition: err.h:47
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define NETIF_FLAG_ETHARP
Definition: netif.h:88
#define ERR_OK
Definition: err.h:52
Definition: dhcpd.h:135
Definition: pbuf.h:79
u8_t flags
Definition: netif.h:192
#define NETIF_FLAG_BROADCAST
Definition: netif.h:72
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
Definition: pbuf.h:61
#define LINK_STATS_INC(x)
Definition: stats.h:227
Definition: netif.h:136
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
Definition: msg.h:34
GLenum GLsizei len
Definition: glext.h:6722
u16_t mtu
Definition: netif.h:186
#define NETIF_INIT_SNMP(netif, type, speed)
Definition: netif.h:248
#define ETH_PAD_SIZE
Definition: opt.h:486
#define ERR_MEM
Definition: fontsub.h:52
void * state
Definition: netif.h:172
netif_input_fn input
Definition: netif.h:147
char name[2]
Definition: netif.h:194
unsigned short u16_t
Definition: cc.h:24
const WCHAR * link
Definition: db.cpp:989
GLfloat GLfloat p
Definition: glext.h:8902
#define htons(x)
Definition: module.h:213
CardRegion * from
Definition: spigame.cpp:19
netif_linkoutput_fn linkoutput
Definition: netif.h:155
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)