2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* WinPCap includes. */
\r
33 /* FreeRTOS includes. */
\r
34 #include "FreeRTOS.h"
\r
38 /* lwIP includes. */
\r
39 #include "lwip/opt.h"
\r
40 #include "lwip/def.h"
\r
41 #include "lwip/mem.h"
\r
42 #include "lwip/pbuf.h"
\r
43 #include "lwip/sys.h"
\r
44 #include <lwip/stats.h>
\r
45 #include <lwip/snmp.h>
\r
46 #include "netif/etharp.h"
\r
48 /* Define those to better describe your network interface. */
\r
52 #define netifMAX_MTU 1500
\r
56 struct eth_addr *ethaddr;
\r
57 /* Add whatever per-interface state that is needed here. */
\r
61 * Place received packet in a pbuf and send a message to the tcpip task to let
\r
62 * it know new data has arrived.
\r
64 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
\r
67 * Copy the received data into a pbuf.
\r
69 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
\r
72 * Send data from a pbuf to the hardware.
\r
74 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
77 * Perform any hardware and/or driver initialisation necessary.
\r
79 static void prvLowLevelInit( struct netif *pxNetIf );
\r
82 * Query the computer the simulation is being executed on to find the network
\r
83 * interfaces it has installed.
\r
85 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
88 * Open the network interface. The number of the interface to be opened is set
\r
89 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
91 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
94 * Interrupts cannot truely be simulated using WinPCap. In reality this task
\r
95 * just polls the interface.
\r
97 static void prvInterruptSimulator( void *pvParameters );
\r
100 * Configure the capture filter to allow blocking reads, and to filter out
\r
101 * packets that are not of interest to this demo.
\r
103 static void prvConfigureCaptureBehaviour( void );
\r
105 /*-----------------------------------------------------------*/
\r
107 /* The WinPCap interface being used. */
\r
108 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
110 /* Parameter required for WinPCap API functions. */
\r
111 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
113 /* The network interface that was opened. */
\r
114 static struct netif *pxlwIPNetIf = NULL;
\r
116 /*-----------------------------------------------------------*/
\r
119 * In this function, the hardware should be initialized.
\r
120 * Called from ethernetif_init().
\r
122 * @param pxNetIf the already initialized lwip network interface structure
\r
123 * for this ethernetif.
\r
125 static void prvLowLevelInit( struct netif *pxNetIf )
\r
127 pcap_if_t *pxAllNetworkInterfaces;
\r
129 /* set MAC hardware address length */
\r
130 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
132 /* set MAC hardware address */
\r
133 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
134 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
135 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
136 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
137 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
138 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
140 /* device capabilities */
\r
141 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
\r
142 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
144 /* Query the computer the simulation is being executed on to find the
\r
145 network interfaces it has installed. */
\r
146 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
148 /* Open the network interface. The number of the interface to be opened is
\r
149 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
150 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
151 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
152 the interface could not be opened. */
\r
153 if( pxAllNetworkInterfaces != NULL )
\r
155 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
158 /* Remember which interface was opened as it is used in the interrupt
\r
160 pxlwIPNetIf = pxNetIf;
\r
164 * This function should do the actual transmission of the packet. The packet is
\r
165 * contained in the pbuf that is passed to the function. This pbuf
\r
166 * might be chained.
\r
168 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
169 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
170 * @return ERR_OK if the packet could be sent
\r
171 * an err_t value if the packet couldn't be sent
\r
173 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
174 * strange results. You might consider waiting for space in the DMA queue
\r
175 * to become availale since the stack doesn't retry to send a packet
\r
176 * dropped because of memory failure (except for the TCP timers).
\r
179 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
182 /* This is taken from lwIP example code and therefore does not conform
\r
183 to the FreeRTOS coding standard. */
\r
186 static unsigned char ucBuffer[ 1520 ];
\r
187 unsigned char *pucBuffer = ucBuffer;
\r
188 unsigned char *pucChar;
\r
189 struct eth_hdr *pxHeader;
\r
190 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
191 err_t xReturn = ERR_OK;
\r
195 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
196 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
199 /* Initiate transfer. */
\r
200 if( p->len == p->tot_len )
\r
202 /* No pbuf chain, don't have to copy -> faster. */
\r
203 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
207 /* pbuf chain, copy into contiguous ucBuffer. */
\r
208 if( p->tot_len >= sizeof( ucBuffer ) )
\r
210 LINK_STATS_INC( link.lenerr );
\r
211 LINK_STATS_INC( link.drop );
\r
212 snmp_inc_ifoutdiscards( pxNetIf );
\r
217 pucChar = ucBuffer;
\r
219 for( q = p; q != NULL; q = q->next )
\r
221 /* Send the data from the pbuf to the interface, one pbuf at a
\r
222 time. The size of the data in each pbuf is kept in the ->len
\r
224 /* send data from(q->payload, q->len); */
\r
225 LWIP_DEBUGF( NETIF_DEBUG, ("NETIF: send pucChar %p q->payload %p q->len %i q->next %p\n", pucChar, q->payload, ( int ) q->len, ( void* ) q->next ) );
\r
228 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
229 pucChar += q->len - ETH_PAD_SIZE;
\r
233 memcpy( pucChar, q->payload, q->len );
\r
240 if( xReturn == ERR_OK )
\r
242 /* signal that packet should be sent */
\r
243 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
\r
245 LINK_STATS_INC( link.memerr );
\r
246 LINK_STATS_INC( link.drop );
\r
247 snmp_inc_ifoutdiscards( pxNetIf );
\r
252 LINK_STATS_INC( link.xmit );
\r
253 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
254 pxHeader = ( struct eth_hdr * )p->payload;
\r
256 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
258 /* broadcast or multicast packet*/
\r
259 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
263 /* unicast packet */
\r
264 snmp_inc_ifoutucastpkts( pxNetIf );
\r
273 * Should allocate a pbuf and transfer the bytes of the incoming
\r
274 * packet from the interface into the pbuf.
\r
276 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
277 * @return a pbuf filled with the received packet (including MAC header)
\r
278 * NULL on memory error
\r
280 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
\r
282 struct pbuf *p = NULL, *q;
\r
284 if( lDataLength > 0 )
\r
287 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
290 /* We allocate a pbuf chain of pbufs from the pool. */
\r
291 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
\r
296 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
299 /* We iterate over the pbuf chain until we have read the entire
\r
300 * packet into the pbuf. */
\r
302 for( q = p; q != NULL; q = q->next )
\r
304 /* Read enough bytes to fill this pbuf in the chain. The
\r
305 * available data in the pbuf is given by the q->len
\r
307 * This does not necessarily have to be a memcpy, you can also preallocate
\r
308 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
309 * actually received size. In this case, ensure the usTotalLength member of the
\r
310 * pbuf is the sum of the chained pbuf len members.
\r
312 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
\r
313 lDataLength += q->len;
\r
317 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
320 LINK_STATS_INC( link.recv );
\r
328 * This function should be called when a packet is ready to be read
\r
329 * from the interface. It uses the function prvLowLevelInput() that
\r
330 * should handle the actual reception of bytes from the network
\r
331 * interface. Then the type of the received packet is determined and
\r
332 * the appropriate input function is called.
\r
334 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
336 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
\r
338 /* This is taken from lwIP example code and therefore does not conform
\r
339 to the FreeRTOS coding standard. */
\r
341 struct eth_hdr *pxHeader;
\r
344 /* move received packet into a new pbuf */
\r
345 p = prvLowLevelInput( pucInputData, lInputLength );
\r
347 /* no packet could be read, silently ignore this */
\r
350 /* points to packet payload, which starts with an Ethernet header */
\r
351 pxHeader = p->payload;
\r
353 switch( htons( pxHeader->type ) )
\r
355 /* IP or ARP packet? */
\r
358 /* full packet send to tcpip_thread to process */
\r
359 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
\r
361 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
376 * Should be called at the beginning of the program to set up the
\r
377 * network interface. It calls the function prvLowLevelInit() to do the
\r
378 * actual setup of the hardware.
\r
380 * This function should be passed as a parameter to netif_add().
\r
382 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
383 * @return ERR_OK if the loopif is initialized
\r
384 * ERR_MEM if private data couldn't be allocated
\r
385 * any other err_t on error
\r
387 err_t ethernetif_init( struct netif *pxNetIf )
\r
389 err_t xReturn = ERR_OK;
\r
391 /* This is taken from lwIP example code and therefore does not conform
\r
392 to the FreeRTOS coding standard. */
\r
394 struct xEthernetIf *pxEthernetIf;
\r
396 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
398 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
399 if( pxEthernetIf == NULL )
\r
401 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
406 #if LWIP_NETIF_HOSTNAME
\r
408 /* Initialize interface hostname */
\r
409 pxNetIf->hostname = "lwip";
\r
411 #endif /* LWIP_NETIF_HOSTNAME */
\r
413 pxNetIf->state = pxEthernetIf;
\r
414 pxNetIf->name[ 0 ] = IFNAME0;
\r
415 pxNetIf->name[ 1 ] = IFNAME1;
\r
417 /* We directly use etharp_output() here to save a function call.
\r
418 * You can instead declare your own function an call etharp_output()
\r
419 * from it if you have to do some checks before sending (e.g. if link
\r
420 * is available...) */
\r
421 pxNetIf->output = etharp_output;
\r
422 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
423 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
424 pxNetIf->mtu = netifMAX_MTU;
\r
425 pxNetIf->linkoutput = prvLowLevelOutput;
\r
427 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
429 /* initialize the hardware */
\r
430 prvLowLevelInit( pxNetIf );
\r
432 /* Was an interface opened? */
\r
433 if( pxOpenedInterfaceHandle == NULL )
\r
435 /* Probably an invalid adapter number was defined in
\r
436 FreeRTOSConfig.h. */
\r
438 configASSERT( pxOpenedInterfaceHandle );
\r
444 /*-----------------------------------------------------------*/
\r
446 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
448 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
449 long lInterfaceNumber = 1;
\r
451 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
453 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
454 pxAllNetworkInterfaces = NULL;
\r
457 if( pxAllNetworkInterfaces != NULL )
\r
459 /* Print out the list of network interfaces. The first in the list
\r
460 is interface '1', not interface '0'. */
\r
461 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
463 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
465 if( xInterface->description != NULL )
\r
467 printf( " (%s)\r\n", xInterface->description );
\r
471 printf( " (No description available)\r\n") ;
\r
474 lInterfaceNumber++;
\r
478 if( lInterfaceNumber == 1 )
\r
480 /* The interface number was never incremented, so the above for() loop
\r
481 did not execute meaning no interfaces were found. */
\r
482 printf( " \r\nNo network interfaces were found.\r\n" );
\r
483 pxAllNetworkInterfaces = NULL;
\r
486 printf( "\r\nThe interface that will be opened is set by configNETWORK_INTERFACE_TO_USE which should be defined in FreeRTOSConfig.h\r\n" );
\r
487 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
489 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
491 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
493 if( pxAllNetworkInterfaces != NULL )
\r
495 /* Free the device list, as no devices are going to be opened. */
\r
496 pcap_freealldevs( pxAllNetworkInterfaces );
\r
497 pxAllNetworkInterfaces = NULL;
\r
501 return pxAllNetworkInterfaces;
\r
503 /*-----------------------------------------------------------*/
\r
505 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
507 pcap_if_t *xInterface;
\r
510 /* Walk the list of devices until the selected device is located. */
\r
511 xInterface = pxAllNetworkInterfaces;
\r
512 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
514 xInterface = xInterface->next;
\r
517 /* Open the selected interface. */
\r
518 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
519 netifMAX_MTU, /* The size of the packet to capture. */
\r
520 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
521 IP address is going to be "simulated", and
\r
522 not be the real MAC and IP address. This allows
\r
523 trafic to the simulated IP address to be routed
\r
524 to uIP, and trafic to the real IP address to be
\r
525 routed to the Windows TCP/IP stack. */
\r
526 0L, /* The read time out. This is going to block
\r
527 until data is available. */
\r
528 NULL, /* No authentication is required as this is
\r
529 not a remote capture session. */
\r
533 if ( pxOpenedInterfaceHandle == NULL )
\r
535 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
539 /* Configure the capture filter to allow blocking reads, and to filter
\r
540 out packets that are not of interest to this demo. */
\r
541 prvConfigureCaptureBehaviour();
\r
544 /* The device list is no longer required. */
\r
545 pcap_freealldevs( pxAllNetworkInterfaces );
\r
547 /*-----------------------------------------------------------*/
\r
549 static void prvInterruptSimulator( void *pvParameters )
\r
551 static struct pcap_pkthdr *pxHeader;
\r
552 const unsigned char *pucPacketData;
\r
553 extern QueueHandle_t xEMACEventQueue;
\r
556 /* Just to kill the compiler warning. */
\r
557 ( void ) pvParameters;
\r
561 /* Get the next packet. */
\r
562 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
565 if( pxlwIPNetIf != NULL )
\r
567 prvEthernetInput( pucPacketData, pxHeader->len );
\r
572 /* There is no real way of simulating an interrupt.
\r
573 Make sure other tasks can run. */
\r
578 /*-----------------------------------------------------------*/
\r
580 static void prvConfigureCaptureBehaviour( void )
\r
582 struct bpf_program xFilterCode;
\r
583 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
584 unsigned long ulNetMask;
\r
586 /* Unblock a read as soon as anything is received. */
\r
587 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
589 /* Allow blocking. */
\r
590 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
592 /* Set up a filter so only the packets of interest are passed to the lwIP
\r
593 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
594 confuse this with an error message. */
\r
595 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
597 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
599 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
601 printf( "\r\nThe packet filter string is invalid\r\n" );
\r
605 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
607 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
611 /* Create a task that simulates an interrupt in a real system. This will
\r
612 block waiting for packets, then send a message to the uIP task when data
\r
614 xTaskCreate( prvInterruptSimulator, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r