2 * FreeRTOS Kernel V10.0.1
\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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* WinPCap includes. */
\r
32 /* FreeRTOS includes. */
\r
33 #include "FreeRTOS.h"
\r
37 /* lwIP includes. */
\r
38 #include "lwip/opt.h"
\r
39 #include "lwip/def.h"
\r
40 #include "lwip/mem.h"
\r
41 #include "lwip/pbuf.h"
\r
42 #include "lwip/sys.h"
\r
43 #include <lwip/stats.h>
\r
44 #include <lwip/snmp.h>
\r
45 #include "netif/etharp.h"
\r
47 /* Define those to better describe your network interface. */
\r
51 #define netifMAX_MTU 1500
\r
55 struct eth_addr *ethaddr;
\r
56 /* Add whatever per-interface state that is needed here. */
\r
60 * Place received packet in a pbuf and send a message to the tcpip task to let
\r
61 * it know new data has arrived.
\r
63 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );
\r
66 * Copy the received data into a pbuf.
\r
68 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );
\r
71 * Send data from a pbuf to the hardware.
\r
73 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );
\r
76 * Perform any hardware and/or driver initialisation necessary.
\r
78 static void prvLowLevelInit( struct netif *pxNetIf );
\r
81 * Query the computer the simulation is being executed on to find the network
\r
82 * interfaces it has installed.
\r
84 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
\r
87 * Open the network interface. The number of the interface to be opened is set
\r
88 * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
90 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
\r
93 * Interrupts cannot truely be simulated using WinPCap. In reality this task
\r
94 * just polls the interface.
\r
96 static void prvInterruptSimulator( void *pvParameters );
\r
99 * Configure the capture filter to allow blocking reads, and to filter out
\r
100 * packets that are not of interest to this demo.
\r
102 static void prvConfigureCaptureBehaviour( void );
\r
104 /*-----------------------------------------------------------*/
\r
106 /* The WinPCap interface being used. */
\r
107 static pcap_t *pxOpenedInterfaceHandle = NULL;
\r
109 /* Parameter required for WinPCap API functions. */
\r
110 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
\r
112 /* The network interface that was opened. */
\r
113 static struct netif *pxlwIPNetIf = NULL;
\r
115 /*-----------------------------------------------------------*/
\r
118 * In this function, the hardware should be initialized.
\r
119 * Called from ethernetif_init().
\r
121 * @param pxNetIf the already initialized lwip network interface structure
\r
122 * for this ethernetif.
\r
124 static void prvLowLevelInit( struct netif *pxNetIf )
\r
126 pcap_if_t *pxAllNetworkInterfaces;
\r
128 /* set MAC hardware address length */
\r
129 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
131 /* set MAC hardware address */
\r
132 pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;
\r
133 pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;
\r
134 pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;
\r
135 pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;
\r
136 pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;
\r
137 pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;
\r
139 /* device capabilities */
\r
140 /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */
\r
141 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
\r
143 /* Query the computer the simulation is being executed on to find the
\r
144 network interfaces it has installed. */
\r
145 pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
\r
147 /* Open the network interface. The number of the interface to be opened is
\r
148 set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
\r
149 Calling this function will set the pxOpenedInterfaceHandle variable. If,
\r
150 after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
\r
151 the interface could not be opened. */
\r
152 if( pxAllNetworkInterfaces != NULL )
\r
154 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
\r
157 /* Remember which interface was opened as it is used in the interrupt
\r
159 pxlwIPNetIf = pxNetIf;
\r
163 * This function should do the actual transmission of the packet. The packet is
\r
164 * contained in the pbuf that is passed to the function. This pbuf
\r
165 * might be chained.
\r
167 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
168 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
\r
169 * @return ERR_OK if the packet could be sent
\r
170 * an err_t value if the packet couldn't be sent
\r
172 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
\r
173 * strange results. You might consider waiting for space in the DMA queue
\r
174 * to become availale since the stack doesn't retry to send a packet
\r
175 * dropped because of memory failure (except for the TCP timers).
\r
178 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )
\r
181 /* This is taken from lwIP example code and therefore does not conform
\r
182 to the FreeRTOS coding standard. */
\r
185 static unsigned char ucBuffer[ 1520 ];
\r
186 unsigned char *pucBuffer = ucBuffer;
\r
187 unsigned char *pucChar;
\r
188 struct eth_hdr *pxHeader;
\r
189 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;
\r
190 err_t xReturn = ERR_OK;
\r
194 #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
\r
195 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
\r
198 /* Initiate transfer. */
\r
199 if( p->len == p->tot_len )
\r
201 /* No pbuf chain, don't have to copy -> faster. */
\r
202 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];
\r
206 /* pbuf chain, copy into contiguous ucBuffer. */
\r
207 if( p->tot_len >= sizeof( ucBuffer ) )
\r
209 LINK_STATS_INC( link.lenerr );
\r
210 LINK_STATS_INC( link.drop );
\r
211 snmp_inc_ifoutdiscards( pxNetIf );
\r
216 pucChar = ucBuffer;
\r
218 for( q = p; q != NULL; q = q->next )
\r
220 /* Send the data from the pbuf to the interface, one pbuf at a
\r
221 time. The size of the data in each pbuf is kept in the ->len
\r
223 /* send data from(q->payload, q->len); */
\r
224 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
227 memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );
\r
228 pucChar += q->len - ETH_PAD_SIZE;
\r
232 memcpy( pucChar, q->payload, q->len );
\r
239 if( xReturn == ERR_OK )
\r
241 /* signal that packet should be sent */
\r
242 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 )
\r
244 LINK_STATS_INC( link.memerr );
\r
245 LINK_STATS_INC( link.drop );
\r
246 snmp_inc_ifoutdiscards( pxNetIf );
\r
251 LINK_STATS_INC( link.xmit );
\r
252 snmp_add_ifoutoctets( pxNetIf, usTotalLength );
\r
253 pxHeader = ( struct eth_hdr * )p->payload;
\r
255 if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 )
\r
257 /* broadcast or multicast packet*/
\r
258 snmp_inc_ifoutnucastpkts( pxNetIf );
\r
262 /* unicast packet */
\r
263 snmp_inc_ifoutucastpkts( pxNetIf );
\r
272 * Should allocate a pbuf and transfer the bytes of the incoming
\r
273 * packet from the interface into the pbuf.
\r
275 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
276 * @return a pbuf filled with the received packet (including MAC header)
\r
277 * NULL on memory error
\r
279 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )
\r
281 struct pbuf *p = NULL, *q;
\r
283 if( lDataLength > 0 )
\r
286 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
\r
289 /* We allocate a pbuf chain of pbufs from the pool. */
\r
290 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );
\r
295 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */
\r
298 /* We iterate over the pbuf chain until we have read the entire
\r
299 * packet into the pbuf. */
\r
301 for( q = p; q != NULL; q = q->next )
\r
303 /* Read enough bytes to fill this pbuf in the chain. The
\r
304 * available data in the pbuf is given by the q->len
\r
306 * This does not necessarily have to be a memcpy, you can also preallocate
\r
307 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
\r
308 * actually received size. In this case, ensure the usTotalLength member of the
\r
309 * pbuf is the sum of the chained pbuf len members.
\r
311 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );
\r
312 lDataLength += q->len;
\r
316 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */
\r
319 LINK_STATS_INC( link.recv );
\r
327 * This function should be called when a packet is ready to be read
\r
328 * from the interface. It uses the function prvLowLevelInput() that
\r
329 * should handle the actual reception of bytes from the network
\r
330 * interface. Then the type of the received packet is determined and
\r
331 * the appropriate input function is called.
\r
333 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
335 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )
\r
337 /* This is taken from lwIP example code and therefore does not conform
\r
338 to the FreeRTOS coding standard. */
\r
340 struct eth_hdr *pxHeader;
\r
343 /* move received packet into a new pbuf */
\r
344 p = prvLowLevelInput( pucInputData, lInputLength );
\r
346 /* no packet could be read, silently ignore this */
\r
349 /* points to packet payload, which starts with an Ethernet header */
\r
350 pxHeader = p->payload;
\r
352 switch( htons( pxHeader->type ) )
\r
354 /* IP or ARP packet? */
\r
357 /* full packet send to tcpip_thread to process */
\r
358 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )
\r
360 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );
\r
375 * Should be called at the beginning of the program to set up the
\r
376 * network interface. It calls the function prvLowLevelInit() to do the
\r
377 * actual setup of the hardware.
\r
379 * This function should be passed as a parameter to netif_add().
\r
381 * @param pxNetIf the lwip network interface structure for this ethernetif
\r
382 * @return ERR_OK if the loopif is initialized
\r
383 * ERR_MEM if private data couldn't be allocated
\r
384 * any other err_t on error
\r
386 err_t ethernetif_init( struct netif *pxNetIf )
\r
388 err_t xReturn = ERR_OK;
\r
390 /* This is taken from lwIP example code and therefore does not conform
\r
391 to the FreeRTOS coding standard. */
\r
393 struct xEthernetIf *pxEthernetIf;
\r
395 LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );
\r
397 pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );
\r
398 if( pxEthernetIf == NULL )
\r
400 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );
\r
405 #if LWIP_NETIF_HOSTNAME
\r
407 /* Initialize interface hostname */
\r
408 pxNetIf->hostname = "lwip";
\r
410 #endif /* LWIP_NETIF_HOSTNAME */
\r
412 pxNetIf->state = pxEthernetIf;
\r
413 pxNetIf->name[ 0 ] = IFNAME0;
\r
414 pxNetIf->name[ 1 ] = IFNAME1;
\r
416 /* We directly use etharp_output() here to save a function call.
\r
417 * You can instead declare your own function an call etharp_output()
\r
418 * from it if you have to do some checks before sending (e.g. if link
\r
419 * is available...) */
\r
420 pxNetIf->output = etharp_output;
\r
421 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
\r
422 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;
\r
423 pxNetIf->mtu = netifMAX_MTU;
\r
424 pxNetIf->linkoutput = prvLowLevelOutput;
\r
426 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );
\r
428 /* initialize the hardware */
\r
429 prvLowLevelInit( pxNetIf );
\r
431 /* Was an interface opened? */
\r
432 if( pxOpenedInterfaceHandle == NULL )
\r
434 /* Probably an invalid adapter number was defined in
\r
435 FreeRTOSConfig.h. */
\r
437 configASSERT( pxOpenedInterfaceHandle );
\r
443 /*-----------------------------------------------------------*/
\r
445 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
\r
447 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
\r
448 long lInterfaceNumber = 1;
\r
450 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
\r
452 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );
\r
453 pxAllNetworkInterfaces = NULL;
\r
456 if( pxAllNetworkInterfaces != NULL )
\r
458 /* Print out the list of network interfaces. The first in the list
\r
459 is interface '1', not interface '0'. */
\r
460 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
\r
462 printf( "%d. %s", lInterfaceNumber, xInterface->name );
\r
464 if( xInterface->description != NULL )
\r
466 printf( " (%s)\r\n", xInterface->description );
\r
470 printf( " (No description available)\r\n") ;
\r
473 lInterfaceNumber++;
\r
477 if( lInterfaceNumber == 1 )
\r
479 /* The interface number was never incremented, so the above for() loop
\r
480 did not execute meaning no interfaces were found. */
\r
481 printf( " \r\nNo network interfaces were found.\r\n" );
\r
482 pxAllNetworkInterfaces = NULL;
\r
485 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
486 printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );
\r
488 if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )
\r
490 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );
\r
492 if( pxAllNetworkInterfaces != NULL )
\r
494 /* Free the device list, as no devices are going to be opened. */
\r
495 pcap_freealldevs( pxAllNetworkInterfaces );
\r
496 pxAllNetworkInterfaces = NULL;
\r
500 return pxAllNetworkInterfaces;
\r
502 /*-----------------------------------------------------------*/
\r
504 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
\r
506 pcap_if_t *xInterface;
\r
509 /* Walk the list of devices until the selected device is located. */
\r
510 xInterface = pxAllNetworkInterfaces;
\r
511 for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )
\r
513 xInterface = xInterface->next;
\r
516 /* Open the selected interface. */
\r
517 pxOpenedInterfaceHandle = pcap_open( xInterface->name, /* The name of the selected interface. */
\r
518 netifMAX_MTU, /* The size of the packet to capture. */
\r
519 PCAP_OPENFLAG_PROMISCUOUS, /* Open in promiscious mode as the MAC and
\r
520 IP address is going to be "simulated", and
\r
521 not be the real MAC and IP address. This allows
\r
522 trafic to the simulated IP address to be routed
\r
523 to uIP, and trafic to the real IP address to be
\r
524 routed to the Windows TCP/IP stack. */
\r
525 0L, /* The read time out. This is going to block
\r
526 until data is available. */
\r
527 NULL, /* No authentication is required as this is
\r
528 not a remote capture session. */
\r
532 if ( pxOpenedInterfaceHandle == NULL )
\r
534 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );
\r
538 /* Configure the capture filter to allow blocking reads, and to filter
\r
539 out packets that are not of interest to this demo. */
\r
540 prvConfigureCaptureBehaviour();
\r
543 /* The device list is no longer required. */
\r
544 pcap_freealldevs( pxAllNetworkInterfaces );
\r
546 /*-----------------------------------------------------------*/
\r
548 static void prvInterruptSimulator( void *pvParameters )
\r
550 static struct pcap_pkthdr *pxHeader;
\r
551 const unsigned char *pucPacketData;
\r
552 extern QueueHandle_t xEMACEventQueue;
\r
555 /* Just to kill the compiler warning. */
\r
556 ( void ) pvParameters;
\r
560 /* Get the next packet. */
\r
561 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );
\r
564 if( pxlwIPNetIf != NULL )
\r
566 prvEthernetInput( pucPacketData, pxHeader->len );
\r
571 /* There is no real way of simulating an interrupt.
\r
572 Make sure other tasks can run. */
\r
577 /*-----------------------------------------------------------*/
\r
579 static void prvConfigureCaptureBehaviour( void )
\r
581 struct bpf_program xFilterCode;
\r
582 const long lMinBytesToCopy = 10L, lBlocking = 1L;
\r
583 unsigned long ulNetMask;
\r
585 /* Unblock a read as soon as anything is received. */
\r
586 pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );
\r
588 /* Allow blocking. */
\r
589 pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );
\r
591 /* Set up a filter so only the packets of interest are passed to the lwIP
\r
592 stack. cErrorBuffer is used for convenience to create the string. Don't
\r
593 confuse this with an error message. */
\r
594 sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
596 ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
\r
598 if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
\r
600 printf( "\r\nThe packet filter string is invalid\r\n" );
\r
604 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
\r
606 printf( "\r\nAn error occurred setting the packet filter.\r\n" );
\r
610 /* Create a task that simulates an interrupt in a real system. This will
\r
611 block waiting for packets, then send a message to the uIP task when data
\r
613 xTaskCreate( prvInterruptSimulator, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
\r