raw_socket.c
Go to the documentation of this file.
1 /**
2  * @file raw_socket.c
3  * @brief TCP/IP raw sockets
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @section Description
28  *
29  * A raw socket is a type of socket that allows access to the
30  * underlying transport provider
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.6.8
34  **/
35 
36 //Switch to the appropriate trace level
37 #define TRACE_LEVEL RAW_SOCKET_TRACE_LEVEL
38 
39 //Dependencies
40 #include "core/net.h"
41 #include "core/socket.h"
42 #include "core/socket_misc.h"
43 #include "core/raw_socket.h"
44 #include "core/ethernet_misc.h"
45 #include "ipv4/ipv4.h"
46 #include "ipv4/ipv4_misc.h"
47 #include "ipv6/ipv6.h"
48 #include "ipv6/ipv6_misc.h"
49 #include "debug.h"
50 
51 //Check TCP/IP stack configuration
52 #if (RAW_SOCKET_SUPPORT == ENABLED)
53 
54 
55 /**
56  * @brief Process incoming IP packet
57  * @param[in] interface Underlying network interface
58  * @param[in] pseudoHeader IPv4 or IPv6 pseudo header
59  * @param[in] buffer Multi-part buffer containing the IP packet
60  * @param[in] offset Offset to the first byte of the IP packet
61  * @param[in] ancillary Additional options passed to the stack along with
62  * the packet
63  * @return Error code
64  **/
65 
67  const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset,
68  const NetRxAncillary *ancillary)
69 {
70  uint_t i;
71  size_t length;
72  NetContext *context;
73  Socket *socket;
74  SocketQueueItem *queueItem;
75  NetBuffer *p;
76 
77  //Point to the TCP/IP stack context
78  context = interface->netContext;
79 
80  //Retrieve the length of the raw IP packet
81  length = netBufferGetLength(buffer) - offset;
82 
83  //Loop through opened sockets
84  for(i = 0; i < SOCKET_MAX_COUNT; i++)
85  {
86  //Point to the current socket
87  socket = &context->socketTable[i];
88 
89  //Raw socket found?
90  if(socket->type != SOCKET_TYPE_RAW_IP)
91  {
92  continue;
93  }
94 
95  //Check whether the socket is bound to a particular interface
96  if(socket->interface != NULL && socket->interface != interface)
97  {
98  continue;
99  }
100 
101 #if (IPV4_SUPPORT == ENABLED)
102  //IPv4 packet received?
103  if(pseudoHeader->length == sizeof(Ipv4PseudoHeader))
104  {
105  //Check whether the socket is restricted to IPv6 communications only
106  if((socket->options & SOCKET_OPTION_IPV6_ONLY) != 0)
107  {
108  continue;
109  }
110 
111  //Check protocol field
112  if(socket->protocol != pseudoHeader->ipv4Data.protocol)
113  {
114  continue;
115  }
116 
117  //Check whether the destination address is a unicast, broadcast or
118  //multicast address
119  if(ipv4IsBroadcastAddr(interface, pseudoHeader->ipv4Data.destAddr))
120  {
121  //Check whether broadcast datagrams are accepted or not
122  if((socket->options & SOCKET_OPTION_BROADCAST) == 0)
123  {
124  continue;
125  }
126  }
127  else if(ipv4IsMulticastAddr(pseudoHeader->ipv4Data.destAddr))
128  {
129  IpAddr srcAddr;
131 
132  //Get source IPv4 address
133  srcAddr.length = sizeof(Ipv4Addr);
134  srcAddr.ipv4Addr = pseudoHeader->ipv4Data.srcAddr;
135 
136  //Get destination IPv4 address
137  destAddr.length = sizeof(Ipv4Addr);
138  destAddr.ipv4Addr = pseudoHeader->ipv4Data.destAddr;
139 
140  //Multicast address filtering
142  {
143  continue;
144  }
145  }
146  else
147  {
148  //Destination IP address filtering
149  if(socket->localIpAddr.length != 0)
150  {
151  //An IPv4 address is expected
152  if(socket->localIpAddr.length != sizeof(Ipv4Addr))
153  {
154  continue;
155  }
156 
157  //Filter out non-matching addresses
158  if(socket->localIpAddr.ipv4Addr != IPV4_UNSPECIFIED_ADDR &&
159  socket->localIpAddr.ipv4Addr != pseudoHeader->ipv4Data.destAddr)
160  {
161  continue;
162  }
163  }
164  }
165 
166  //Source IP address filtering
167  if(socket->remoteIpAddr.length != 0)
168  {
169  //An IPv4 address is expected
170  if(socket->remoteIpAddr.length != sizeof(Ipv4Addr))
171  {
172  continue;
173  }
174 
175  //Filter out non-matching addresses
176  if(socket->remoteIpAddr.ipv4Addr != IPV4_UNSPECIFIED_ADDR &&
177  socket->remoteIpAddr.ipv4Addr != pseudoHeader->ipv4Data.srcAddr)
178  {
179  continue;
180  }
181  }
182  }
183  else
184 #endif
185 #if (IPV6_SUPPORT == ENABLED)
186  //IPv6 packet received?
187  if(pseudoHeader->length == sizeof(Ipv6PseudoHeader))
188  {
189  //Check protocol field
190  if(socket->protocol != pseudoHeader->ipv6Data.nextHeader)
191  {
192  continue;
193  }
194 
195  //Check whether the destination address is a unicast or multicast
196  //address
197  if(ipv6IsMulticastAddr(&pseudoHeader->ipv6Data.destAddr))
198  {
199  IpAddr srcAddr;
201 
202  //Get source IPv6 address
203  srcAddr.length = sizeof(Ipv6Addr);
204  srcAddr.ipv6Addr = pseudoHeader->ipv6Data.srcAddr;
205 
206  //Get destination IPv6 address
207  destAddr.length = sizeof(Ipv6Addr);
208  destAddr.ipv6Addr = pseudoHeader->ipv6Data.destAddr;
209 
210  //Multicast address filtering
212  {
213  continue;
214  }
215  }
216  else
217  {
218  //Destination IP address filtering
219  if(socket->localIpAddr.length != 0)
220  {
221  //An IPv6 address is expected
222  if(socket->localIpAddr.length != sizeof(Ipv6Addr))
223  {
224  continue;
225  }
226 
227  //Filter out non-matching addresses
228  if(!ipv6CompAddr(&socket->localIpAddr.ipv6Addr,
230  !ipv6CompAddr(&socket->localIpAddr.ipv6Addr,
231  &pseudoHeader->ipv6Data.destAddr))
232  {
233  continue;
234  }
235  }
236  }
237 
238  //Source IP address filtering
239  if(socket->remoteIpAddr.length != 0)
240  {
241  //An IPv6 address is expected
242  if(socket->remoteIpAddr.length != sizeof(Ipv6Addr))
243  {
244  continue;
245  }
246 
247  //Filter out non-matching addresses
248  if(!ipv6CompAddr(&socket->remoteIpAddr.ipv6Addr,
250  !ipv6CompAddr(&socket->remoteIpAddr.ipv6Addr,
251  &pseudoHeader->ipv6Data.srcAddr))
252  {
253  continue;
254  }
255  }
256  }
257  else
258 #endif
259  //Invalid packet received?
260  {
261  //This should never occur...
262  continue;
263  }
264 
265  //The current socket meets all the criteria
266  break;
267  }
268 
269  //Drop incoming packet if no matching socket was found
270  if(i >= SOCKET_MAX_COUNT)
271  {
273  }
274 
275  //Empty receive queue?
276  if(socket->receiveQueue == NULL)
277  {
278  //Allocate a memory buffer to hold the data and the associated descriptor
279  p = netBufferAlloc(sizeof(SocketQueueItem) + length);
280 
281  //Successful memory allocation?
282  if(p != NULL)
283  {
284  //Point to the newly created item
285  queueItem = netBufferAt(p, 0, 0);
286  queueItem->buffer = p;
287 
288  //Add the newly created item to the queue
289  socket->receiveQueue = queueItem;
290  }
291  else
292  {
293  //Memory allocation failed
294  queueItem = NULL;
295  }
296  }
297  else
298  {
299  //Point to the very first item
300  queueItem = socket->receiveQueue;
301 
302  //Reach the last item in the receive queue
303  for(i = 1; queueItem->next; i++)
304  {
305  queueItem = queueItem->next;
306  }
307 
308  //Check whether the receive queue is full
309  if(i >= RAW_SOCKET_RX_QUEUE_SIZE)
310  {
311  //Number of inbound packets which were chosen to be discarded even
312  //though no errors had been detected
313  NET_IF_STATS_INC_COUNTER32(inDiscards, 1);
314 
315  //Report an error
317  }
318 
319  //Allocate a memory buffer to hold the data and the associated descriptor
320  p = netBufferAlloc(sizeof(SocketQueueItem) + length);
321 
322  //Successful memory allocation?
323  if(p != NULL)
324  {
325  //Add the newly created item to the queue
326  queueItem->next = netBufferAt(p, 0, 0);
327 
328  //Point to the newly created item
329  queueItem = queueItem->next;
330  queueItem->buffer = p;
331  }
332  else
333  {
334  //Memory allocation failed
335  queueItem = NULL;
336  }
337  }
338 
339  //Not enough resources to properly handle the packet?
340  if(queueItem == NULL)
341  {
342  //Number of inbound packets which were chosen to be discarded even
343  //though no errors had been detected
344  NET_IF_STATS_INC_COUNTER32(inDiscards, 1);
345 
346  //Report an error
347  return ERROR_OUT_OF_MEMORY;
348  }
349 
350  //Initialize next field
351  queueItem->next = NULL;
352  //Network interface where the packet was received
353  queueItem->interface = interface;
354  //Port number is unused
355  queueItem->srcPort = 0;
356 
357 #if (IPV4_SUPPORT == ENABLED)
358  //IPv4 remote address?
359  if(pseudoHeader->length == sizeof(Ipv4PseudoHeader))
360  {
361  //Save the source IPv4 address
362  queueItem->srcIpAddr.length = sizeof(Ipv4Addr);
363  queueItem->srcIpAddr.ipv4Addr = pseudoHeader->ipv4Data.srcAddr;
364 
365  //Save the destination IPv4 address
366  queueItem->destIpAddr.length = sizeof(Ipv4Addr);
367  queueItem->destIpAddr.ipv4Addr = pseudoHeader->ipv4Data.destAddr;
368  }
369 #endif
370 #if (IPV6_SUPPORT == ENABLED)
371  //IPv6 remote address?
372  if(pseudoHeader->length == sizeof(Ipv6PseudoHeader))
373  {
374  //Save the source IPv6 address
375  queueItem->srcIpAddr.length = sizeof(Ipv6Addr);
376  queueItem->srcIpAddr.ipv6Addr = pseudoHeader->ipv6Data.srcAddr;
377 
378  //Save the destination IPv6 address
379  queueItem->destIpAddr.length = sizeof(Ipv6Addr);
380  queueItem->destIpAddr.ipv6Addr = pseudoHeader->ipv6Data.destAddr;
381  }
382 #endif
383 
384  //Offset to the raw IP packet
385  queueItem->offset = sizeof(SocketQueueItem);
386  //Copy the raw data
387  netBufferCopy(queueItem->buffer, queueItem->offset, buffer, offset, length);
388 
389  //Additional options can be passed to the stack along with the packet
390  queueItem->ancillary = *ancillary;
391 
392  //Notify user that data is available
394 
395  //Successful processing
396  return NO_ERROR;
397 }
398 
399 
400 /**
401  * @brief Process incoming Ethernet packet
402  * @param[in] interface Underlying network interface
403  * @param[in] data Pointer to the payload data
404  * @param[in] length Length of the payload data, in bytes
405  * @param[in] ancillary Additional options passed to the stack along with
406  * the packet
407  **/
408 
409 void rawSocketProcessEthPacket(NetInterface *interface, const uint8_t *data,
410  size_t length, const NetRxAncillary *ancillary)
411 {
412 #if (ETH_SUPPORT == ENABLED)
413  uint_t i;
414  uint_t j;
415  NetContext *context;
416  Socket *socket;
417  SocketQueueItem *queueItem;
418  NetBuffer *p;
419 
420  //Point to the TCP/IP stack context
421  context = interface->netContext;
422 
423  //Loop through opened sockets
424  for(i = 0; i < SOCKET_MAX_COUNT; i++)
425  {
426  //Point to the current socket
427  socket = &context->socketTable[i];
428 
429  //Raw socket found?
430  if(socket->type != SOCKET_TYPE_RAW_ETH)
431  {
432  continue;
433  }
434 
435  //Check whether the socket is bound to a particular interface
436  if(socket->interface != NULL && socket->interface != interface)
437  {
438  continue;
439  }
440 
441  //Check protocol field
442  if(socket->protocol == SOCKET_ETH_PROTO_ALL)
443  {
444  //Accept all EtherType values
445  }
446  else if(socket->protocol == SOCKET_ETH_PROTO_LLC)
447  {
448  //Only accept LLC frames
449  if(ancillary->ethType > ETH_MTU)
450  {
451  continue;
452  }
453  }
454  else
455  {
456  //Only accept frames with the correct EtherType value
457  if(ancillary->ethType != socket->protocol)
458  {
459  continue;
460  }
461  }
462 
463  //Empty receive queue?
464  if(socket->receiveQueue == NULL)
465  {
466  //Allocate a memory buffer to hold the data and the associated
467  //descriptor
468  p = netBufferAlloc(sizeof(SocketQueueItem) + length);
469 
470  //Successful memory allocation?
471  if(p != NULL)
472  {
473  //Point to the newly created item
474  queueItem = netBufferAt(p, 0, 0);
475  queueItem->buffer = p;
476 
477  //Add the newly created item to the queue
478  socket->receiveQueue = queueItem;
479  }
480  else
481  {
482  //Memory allocation failed
483  queueItem = NULL;
484  }
485  }
486  else
487  {
488  //Point to the very first item
489  queueItem = socket->receiveQueue;
490 
491  //Reach the last item in the receive queue
492  for(j = 1; queueItem->next; j++)
493  {
494  queueItem = queueItem->next;
495  }
496 
497  //Check whether the receive queue is full
498  if(j >= RAW_SOCKET_RX_QUEUE_SIZE)
499  {
500  //Number of inbound packets which were chosen to be discarded even
501  //though no errors had been detected
502  NET_IF_STATS_INC_COUNTER32(inDiscards, 1);
503 
504  //Skip this socket and deliver the frame to the other sockets
505  continue;
506  }
507 
508  //Allocate a memory buffer to hold the data and the associated
509  //descriptor
510  p = netBufferAlloc(sizeof(SocketQueueItem) + length);
511 
512  //Successful memory allocation?
513  if(p != NULL)
514  {
515  //Add the newly created item to the queue
516  queueItem->next = netBufferAt(p, 0, 0);
517 
518  //Point to the newly created item
519  queueItem = queueItem->next;
520  queueItem->buffer = p;
521  }
522  else
523  {
524  //Memory allocation failed
525  queueItem = NULL;
526  }
527  }
528 
529  //Not enough resources to properly handle the packet?
530  if(queueItem == NULL)
531  {
532  //Number of inbound packets which were chosen to be discarded even
533  //though no errors had been detected
534  NET_IF_STATS_INC_COUNTER32(inDiscards, 1);
535 
536  //Skip this socket and deliver the frame to the other sockets
537  continue;
538  }
539 
540  //Initialize next field
541  queueItem->next = NULL;
542  //Network interface where the packet was received
543  queueItem->interface = interface;
544 
545  //Other fields are meaningless
546  queueItem->srcPort = 0;
547  queueItem->srcIpAddr = IP_ADDR_ANY;
548  queueItem->destIpAddr = IP_ADDR_ANY;
549 
550  //Offset to the raw datagram
551  queueItem->offset = sizeof(SocketQueueItem);
552 
553  //Copy the payload
554  netBufferWrite(queueItem->buffer, queueItem->offset, data, length);
555 
556  //Additional options can be passed to the stack along with the packet
557  queueItem->ancillary = *ancillary;
558 
559  //Notify user that data is available
561  }
562 #endif
563 }
564 
565 
566 /**
567  * @brief Send a raw IP packet
568  * @param[in] socket Handle referencing the socket
569  * @param[in] message Pointer to the structure describing the raw packet
570  * @param[in] flags Set of flags that influences the behavior of this function
571  * @return Error code
572  **/
573 
575  uint_t flags)
576 {
577  error_t error;
578  size_t offset;
579  NetBuffer *buffer;
580  NetInterface *interface;
581  IpPseudoHeader pseudoHeader;
582  NetTxAncillary ancillary;
583 
584  //Select the relevant network interface
585  if(message->interface != NULL)
586  {
587  interface = message->interface;
588  }
589  else
590  {
591  interface = socket->interface;
592  }
593 
594  //Allocate a buffer to hold the raw IP datagram
595  buffer = ipAllocBuffer(0, &offset);
596  //Failed to allocate memory?
597  if(buffer == NULL)
598  {
599  return ERROR_OUT_OF_MEMORY;
600  }
601 
602  //Start of exception handling block
603  do
604  {
605  //Copy the raw data
606  error = netBufferAppend(buffer, message->data, message->length);
607  //Any error to report?
608  if(error)
609  {
610  break;
611  }
612 
613 #if (IPV4_SUPPORT == ENABLED)
614  //Destination address is an IPv4 address?
615  if(message->destIpAddr.length == sizeof(Ipv4Addr))
616  {
618 
619  //Select the source IPv4 address and the relevant network interface
620  //to use when sending data to the specified destination host
621  error = ipv4SelectSourceAddr(socket->netContext, &interface,
622  message->destIpAddr.ipv4Addr, &srcIpAddr);
623  //Any error to report?
624  if(error)
625  {
626  break;
627  }
628 
629  //Format IPv4 pseudo header
630  pseudoHeader.length = sizeof(Ipv4PseudoHeader);
631  pseudoHeader.ipv4Data.srcAddr = srcIpAddr;
632  pseudoHeader.ipv4Data.destAddr = message->destIpAddr.ipv4Addr;
633  pseudoHeader.ipv4Data.reserved = 0;
634  pseudoHeader.ipv4Data.protocol = socket->protocol;
635  pseudoHeader.ipv4Data.length = htons(message->length);
636  }
637  else
638 #endif
639 #if (IPV6_SUPPORT == ENABLED)
640  //Destination address is an IPv6 address?
641  if(message->destIpAddr.length == sizeof(Ipv6Addr))
642  {
643  //Select the source IPv6 address and the relevant network interface
644  //to use when sending data to the specified destination host
645  error = ipv6SelectSourceAddr(socket->netContext, &interface,
646  &message->destIpAddr.ipv6Addr, &pseudoHeader.ipv6Data.srcAddr);
647  //Any error to report?
648  if(error)
649  {
650  break;
651  }
652 
653  //Format IPv6 pseudo header
654  pseudoHeader.length = sizeof(Ipv6PseudoHeader);
655  pseudoHeader.ipv6Data.destAddr = message->destIpAddr.ipv6Addr;
656  pseudoHeader.ipv6Data.length = htonl(message->length);
657  pseudoHeader.ipv6Data.reserved[0] = 0;
658  pseudoHeader.ipv6Data.reserved[1] = 0;
659  pseudoHeader.ipv6Data.reserved[2] = 0;
660  pseudoHeader.ipv6Data.nextHeader = socket->protocol;
661  }
662  else
663 #endif
664  //Invalid destination address?
665  {
666  //An internal error has occurred
667  error = ERROR_FAILURE;
668  //Exit immediately
669  break;
670  }
671 
672  //Additional options can be passed to the stack along with the packet
673  ancillary = NET_DEFAULT_TX_ANCILLARY;
674 
675  //Set the TTL value to be used
676  if(message->ttl != 0)
677  {
678  ancillary.ttl = message->ttl;
679  }
680  else if(ipIsMulticastAddr(&message->destIpAddr))
681  {
682  ancillary.ttl = socket->multicastTtl;
683  }
684  else
685  {
686  ancillary.ttl = socket->ttl;
687  }
688 
689  //This flag can be used to send IP packets without fragmentation
690  if(message->destIpAddr.length == sizeof(Ipv4Addr) &&
691  (socket->options & SOCKET_OPTION_IPV4_DONT_FRAG) != 0)
692  {
693  ancillary.dontFrag = TRUE;
694  }
695  else if(message->destIpAddr.length == sizeof(Ipv6Addr) &&
696  (socket->options & SOCKET_OPTION_IPV6_DONT_FRAG) != 0)
697  {
698  ancillary.dontFrag = TRUE;
699  }
700  else
701  {
702  ancillary.dontFrag = message->dontFrag;
703  }
704 
705  //This flag tells the stack that the destination is on a locally attached
706  //network and not to perform a lookup of the routing table
707  if((flags & SOCKET_FLAG_DONT_ROUTE) != 0)
708  {
709  ancillary.dontRoute = TRUE;
710  }
711 
712  //Set ToS field
713  if(message->tos != 0)
714  {
715  ancillary.tos = message->tos;
716  }
717  else
718  {
719  ancillary.tos = socket->tos;
720  }
721 
722 #if (ETH_SUPPORT == ENABLED)
723  //Set source and destination MAC addresses
724  ancillary.srcMacAddr = message->srcMacAddr;
725  ancillary.destMacAddr = message->destMacAddr;
726 #endif
727 
728 #if (ETH_VLAN_SUPPORT == ENABLED)
729  //Set VLAN PCP and DEI fields
730  ancillary.vlanPcp = socket->vlanPcp;
731  ancillary.vlanDei = socket->vlanDei;
732 #endif
733 
734 #if (ETH_VMAN_SUPPORT == ENABLED)
735  //Set VMAN PCP and DEI fields
736  ancillary.vmanPcp = socket->vmanPcp;
737  ancillary.vmanDei = socket->vmanDei;
738 #endif
739 
740 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
741  //Set switch port identifier
742  ancillary.port = message->switchPort;
743 #endif
744 
745 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
746  //Unique identifier for hardware time stamping
747  ancillary.timestampId = message->timestampId;
748 #endif
749 
750  //Send raw IP datagram
751  error = ipSendDatagram(interface, &pseudoHeader, buffer, offset,
752  &ancillary);
753  //Failed to send data?
754  if(error)
755  {
756  break;
757  }
758 
759  //End of exception handling block
760  } while(0);
761 
762  //Free previously allocated memory block
763  netBufferFree(buffer);
764 
765  //Return status code
766  return error;
767 }
768 
769 
770 /**
771  * @brief Send a raw Ethernet packet
772  * @param[in] socket Handle referencing the socket
773  * @param[in] message Pointer to the structure describing the raw packet
774  * @param[in] flags Set of flags that influences the behavior of this function
775  * @return Error code
776  **/
777 
779  uint_t flags)
780 {
781  error_t error;
782 
783 #if (ETH_SUPPORT == ENABLED)
784  size_t offset;
785  NetBuffer *buffer;
786  NetInterface *interface;
787  NetInterface *physicalInterface;
788 
789  //Select the relevant network interface
790  if(message->interface != NULL)
791  {
792  interface = message->interface;
793  }
794  else if(socket->interface != NULL)
795  {
796  interface = socket->interface;
797  }
798  else
799  {
800  interface = netGetDefaultInterface(socket->netContext);
801  }
802 
803  //Point to the physical interface
804  physicalInterface = nicGetPhysicalInterface(interface);
805 
806  //Ethernet interface?
807  if(physicalInterface->nicDriver != NULL &&
808  physicalInterface->nicDriver->type == NIC_TYPE_ETHERNET)
809  {
810  //Allocate a buffer to hold the raw Ethernet packet
811  buffer = ethAllocBuffer(0, &offset);
812  //Failed to allocate buffer?
813  if(buffer == NULL)
814  {
815  return ERROR_OUT_OF_MEMORY;
816  }
817 
818  //Copy the raw data
819  error = netBufferAppend(buffer, message->data, message->length);
820 
821  //Check status code
822  if(!error)
823  {
824  NetTxAncillary ancillary;
825 
826  //Additional options can be passed to the stack along with the packet
827  ancillary = NET_DEFAULT_TX_ANCILLARY;
828 
829  //Set source MAC address
830  ancillary.srcMacAddr = message->srcMacAddr;
831 
832 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
833  //Set switch port identifier
834  ancillary.port = message->switchPort;
835 #endif
836 
837 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
838  //Unique identifier for hardware time stamping
839  ancillary.timestampId = message->timestampId;
840 #endif
841  //Debug message
842  TRACE_DEBUG("Sending raw Ethernet frame (%" PRIuSIZE " bytes)...\r\n",
843  message->length);
844 
845  //Send raw Ethernet packet
846  error = ethSendFrame(interface, &message->destMacAddr,
847  message->ethType, buffer, offset, &ancillary);
848  }
849 
850  //Free previously allocated memory block
851  netBufferFree(buffer);
852  }
853  else
854 #endif
855  //Unknown interface type?
856  {
857  //Report an error
858  error = ERROR_INVALID_INTERFACE;
859  }
860 
861  //Return status code
862  return error;
863 }
864 
865 
866 /**
867  * @brief Receive an IP packet from a raw socket
868  * @param[in] socket Handle referencing the socket
869  * @param[out] message Received IP packet and ancillary data
870  * @param[in] flags Set of flags that influences the behavior of this function
871  * @return Error code
872  **/
873 
875  uint_t flags)
876 {
877  error_t error;
878  SocketQueueItem *queueItem;
879 
880  //The SOCKET_FLAG_DONT_WAIT enables non-blocking operation
881  if((flags & SOCKET_FLAG_DONT_WAIT) == 0)
882  {
883  //Check whether the receive queue is empty
884  if(socket->receiveQueue == NULL)
885  {
886  //Set the events the application is interested in
887  socket->eventMask = SOCKET_EVENT_RX_READY;
888 
889  //Reset the event object
890  osResetEvent(&socket->event);
891 
892  //Release exclusive access
893  netUnlock(socket->netContext);
894  //Wait until an event is triggered
895  osWaitForEvent(&socket->event, socket->timeout);
896  //Get exclusive access
897  netLock(socket->netContext);
898  }
899  }
900 
901  //Any packet received?
902  if(socket->receiveQueue != NULL)
903  {
904  //Point to the first item in the receive queue
905  queueItem = socket->receiveQueue;
906 
907  //Copy data to user buffer
908  message->length = netBufferRead(message->data, queueItem->buffer,
909  queueItem->offset, message->size);
910 
911  //Network interface where the packet was received
912  message->interface = queueItem->interface;
913 
914  //Save the source IP address and port
915  message->srcIpAddr = queueItem->srcIpAddr;
916  message->srcPort = queueItem->srcPort;
917 
918  //Save the destination IP address and port
919  message->destIpAddr = queueItem->destIpAddr;
920  message->destPort = socket->localPort;
921 
922  //Save TTL value
923  message->ttl = queueItem->ancillary.ttl;
924  //Save ToS field
925  message->tos = queueItem->ancillary.tos;
926 
927 #if (ETH_SUPPORT == ENABLED)
928  //Save source and destination MAC addresses
929  message->srcMacAddr = queueItem->ancillary.srcMacAddr;
930  message->destMacAddr = queueItem->ancillary.destMacAddr;
931 #endif
932 
933 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
934  //Save switch port identifier
935  message->switchPort = queueItem->ancillary.port;
936 #endif
937 
938 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
939  //Save captured time stamp
940  message->timestamp = queueItem->ancillary.timestamp;
941 #endif
942 
943  //If the SOCKET_FLAG_PEEK flag is set, the data is copied into the
944  //buffer but is not removed from the input queue
945  if((flags & SOCKET_FLAG_PEEK) == 0)
946  {
947  //Remove the item from the receive queue
948  socket->receiveQueue = queueItem->next;
949 
950  //Deallocate memory buffer
951  netBufferFree(queueItem->buffer);
952  }
953 
954  //Update the state of events
956 
957  //Successful read operation
958  error = NO_ERROR;
959  }
960  else
961  {
962  //Total number of data that have been received
963  message->length = 0;
964 
965  //Report a timeout error
966  error = ERROR_TIMEOUT;
967  }
968 
969  //Return status code
970  return error;
971 }
972 
973 
974 /**
975  * @brief Receive an Ethernet packet from a raw socket
976  * @param[in] socket Handle referencing the socket
977  * @param[out] message Received Ethernet packet and ancillary data
978  * @param[in] flags Set of flags that influences the behavior of this function
979  * @return Error code
980  **/
981 
983  uint_t flags)
984 {
985  error_t error;
986  SocketQueueItem *queueItem;
987 
988  //The SOCKET_FLAG_DONT_WAIT enables non-blocking operation
989  if((flags & SOCKET_FLAG_DONT_WAIT) == 0)
990  {
991  //Check whether the receive queue is empty
992  if(socket->receiveQueue == NULL)
993  {
994  //Set the events the application is interested in
995  socket->eventMask = SOCKET_EVENT_RX_READY;
996 
997  //Reset the event object
998  osResetEvent(&socket->event);
999 
1000  //Release exclusive access
1001  netUnlock(socket->netContext);
1002  //Wait until an event is triggered
1003  osWaitForEvent(&socket->event, socket->timeout);
1004  //Get exclusive access
1005  netLock(socket->netContext);
1006  }
1007  }
1008 
1009  //Any packet received?
1010  if(socket->receiveQueue != NULL)
1011  {
1012  //Point to the first item in the receive queue
1013  queueItem = socket->receiveQueue;
1014 
1015  //Copy data to user buffer
1016  message->length = netBufferRead(message->data, queueItem->buffer,
1017  queueItem->offset, message->size);
1018 
1019  //Network interface where the packet was received
1020  message->interface = queueItem->interface;
1021 
1022 #if (ETH_SUPPORT == ENABLED)
1023  //Save source and destination MAC addresses
1024  message->srcMacAddr = queueItem->ancillary.srcMacAddr;
1025  message->destMacAddr = queueItem->ancillary.destMacAddr;
1026 
1027  //Save the value of the EtherType field
1028  message->ethType = queueItem->ancillary.ethType;
1029 #endif
1030 
1031 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
1032  //Save switch port identifier
1033  message->switchPort = queueItem->ancillary.port;
1034 #endif
1035 
1036 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
1037  //Save captured time stamp
1038  message->timestamp = queueItem->ancillary.timestamp;
1039 #endif
1040 
1041  //If the SOCKET_FLAG_PEEK flag is set, the data is copied into the
1042  //buffer but is not removed from the input queue
1043  if((flags & SOCKET_FLAG_PEEK) == 0)
1044  {
1045  //Remove the item from the receive queue
1046  socket->receiveQueue = queueItem->next;
1047 
1048  //Deallocate memory buffer
1049  netBufferFree(queueItem->buffer);
1050  }
1051 
1052  //Update the state of events
1054 
1055  //Successful read operation
1056  error = NO_ERROR;
1057  }
1058  else
1059  {
1060  //Total number of data that have been received
1061  message->length = 0;
1062 
1063  //Report a timeout error
1064  error = ERROR_TIMEOUT;
1065  }
1066 
1067  //Return status code
1068  return error;
1069 }
1070 
1071 
1072 /**
1073  * @brief Update event state for raw sockets
1074  * @param[in] socket Handle referencing the socket
1075  **/
1076 
1078 {
1079  //Clear event flags
1080  socket->eventFlags = 0;
1081 
1082  //The socket is marked as readable if a datagram is pending in the queue
1083  if(socket->receiveQueue)
1084  {
1085  socket->eventFlags |= SOCKET_EVENT_RX_READY;
1086  }
1087 
1088  //Check whether the socket is bound to a particular network interface
1089  if(socket->interface != NULL)
1090  {
1091  //Handle link up and link down events
1092  if(socket->interface->linkState)
1093  {
1094  socket->eventFlags |= SOCKET_EVENT_LINK_UP;
1095  }
1096  else
1097  {
1098  socket->eventFlags |= SOCKET_EVENT_LINK_DOWN;
1099  }
1100  }
1101 
1102  //Mask unused events
1103  socket->eventFlags &= socket->eventMask;
1104 
1105  //Any event to signal?
1106  if(socket->eventFlags)
1107  {
1108  //Unblock I/O operations currently in waiting state
1109  osSetEvent(&socket->event);
1110 
1111  //Set user event to signaled state if necessary
1112  if(socket->userEvent != NULL)
1113  {
1114  osSetEvent(socket->userEvent);
1115  }
1116  }
1117 }
1118 
1119 #endif
#define ipv4IsMulticastAddr(ipAddr)
Definition: ipv4.h:191
#define htons(value)
Definition: cpu_endian.h:413
IPv6 (Internet Protocol Version 6)
error_t rawSocketSendEthPacket(Socket *socket, const SocketMsg *message, uint_t flags)
Send a raw Ethernet packet.
Definition: raw_socket.c:778
@ SOCKET_ETH_PROTO_LLC
Definition: socket.h:120
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:335
#define NetContext
Definition: net.h:36
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:717
Ipv4Addr destAddr
Definition: ipv4.h:360
struct _SocketQueueItem SocketQueueItem
Receive queue item.
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:70
error_t rawSocketProcessIpPacket(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Process incoming IP packet.
Definition: raw_socket.c:66
IP network address.
Definition: ip.h:97
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:722
@ ERROR_INVALID_INTERFACE
Invalid interface.
Definition: error.h:53
uint8_t p
Definition: ndp.h:300
@ SOCKET_FLAG_DONT_ROUTE
Definition: socket.h:137
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
bool_t ipv4IsBroadcastAddr(NetInterface *interface, Ipv4Addr ipAddr)
Check whether an IPv4 address is a broadcast address.
Definition: ipv4_misc.c:482
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
Message and ancillary data.
Definition: socket.h:241
error_t ipSendDatagram(NetInterface *interface, const IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send an IP datagram.
Definition: ip.c:68
uint16_t srcPort
Definition: socket.h:292
error_t ethSendFrame(NetInterface *interface, const MacAddr *destAddr, uint16_t type, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send an Ethernet frame.
Definition: ethernet.c:422
Ipv6Addr
Definition: ipv6.h:282
@ SOCKET_OPTION_IPV6_ONLY
Definition: socket.h:200
IpAddr srcIpAddr
Definition: socket.h:291
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:136
Ipv4Addr srcIpAddr
Definition: ipcp.h:79
struct _SocketQueueItem * next
Definition: socket.h:289
@ SOCKET_OPTION_IPV6_DONT_FRAG
Definition: socket.h:201
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:328
error_t rawSocketSendIpPacket(Socket *socket, const SocketMsg *message, uint_t flags)
Send a raw IP packet.
Definition: raw_socket.c:574
@ SOCKET_FLAG_PEEK
Definition: socket.h:136
const IpAddr IP_ADDR_ANY
Definition: ip.c:53
#define ipv6IsMulticastAddr(ipAddr)
Definition: ipv6.h:148
NetRxAncillary ancillary
Definition: socket.h:296
Helper functions for IPv4.
#define htonl(value)
Definition: cpu_endian.h:414
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:91
error_t
Error codes.
Definition: error.h:43
@ ERROR_PROTOCOL_UNREACHABLE
Definition: error.h:84
bool_t ipIsMulticastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a multicast address.
Definition: ip.c:251
#define Ipv6PseudoHeader
Definition: ipv6.h:44
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:65
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ SOCKET_EVENT_LINK_DOWN
Definition: socket.h:182
error_t ipv4SelectSourceAddr(NetContext *context, NetInterface **interface, Ipv4Addr destAddr, Ipv4Addr *srcAddr)
IPv4 source address selection.
Definition: ipv4_misc.c:173
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
void netBufferFree(NetBuffer *buffer)
Dispose a multi-part buffer.
Definition: net_mem.c:296
@ SOCKET_OPTION_IPV4_DONT_FRAG
Definition: socket.h:195
Helper functions for IPv6.
@ SOCKET_OPTION_BROADCAST
Definition: socket.h:193
void rawSocketProcessEthPacket(NetInterface *interface, const uint8_t *data, size_t length, const NetRxAncillary *ancillary)
Process incoming Ethernet packet.
Definition: raw_socket.c:409
#define NetTxAncillary
Definition: net_misc.h:36
NetInterface * netGetDefaultInterface(NetContext *context)
Get default network interface.
Definition: net.c:559
@ SOCKET_TYPE_RAW_IP
Definition: socket.h:94
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:65
IpAddr destIpAddr
Definition: socket.h:293
#define RAW_SOCKET_RX_QUEUE_SIZE
Definition: raw_socket.h:48
error_t netBufferCopy(NetBuffer *dest, size_t destOffset, const NetBuffer *src, size_t srcOffset, size_t length)
Copy data between multi-part buffers.
Definition: net_mem.c:544
#define Ipv4PseudoHeader
Definition: ipv4.h:40
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:311
size_t length
Definition: ip.h:98
void rawSocketUpdateEvents(Socket *socket)
Update event state for raw sockets.
Definition: raw_socket.c:1077
@ SOCKET_EVENT_LINK_UP
Definition: socket.h:181
TCP/IP raw sockets.
NetBuffer * netBufferAlloc(size_t length)
Allocate a multi-part buffer.
Definition: net_mem.c:255
Receive queue item.
Definition: socket.h:288
uint8_t flags
Definition: tcp.h:358
#define TRACE_DEBUG(...)
Definition: debug.h:119
@ ERROR_TIMEOUT
Definition: error.h:95
Ipv4Addr ipv4Addr
Definition: ip.h:102
Helper functions for sockets.
#define ETH_MTU
Definition: ethernet.h:116
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
#define IpPseudoHeader
Definition: ip.h:36
error_t netBufferAppend(NetBuffer *dest, const void *src, size_t length)
Append data a multi-part buffer.
Definition: net_mem.c:634
error_t rawSocketReceiveEthPacket(Socket *socket, SocketMsg *message, uint_t flags)
Receive an Ethernet packet from a raw socket.
Definition: raw_socket.c:982
#define Socket
Definition: socket.h:36
size_t netBufferWrite(NetBuffer *dest, size_t destOffset, const void *src, size_t length)
Write data to a multi-part buffer.
Definition: net_mem.c:669
MacAddr srcAddr
Definition: ethernet.h:222
Socket API.
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:95
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:323
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ ERROR_RECEIVE_QUEUE_FULL
Definition: error.h:94
NetInterface * interface
Definition: socket.h:290
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:434
bool_t socketMulticastFilter(Socket *socket, const IpAddr *destAddr, const IpAddr *srcAddr)
Filter out incoming multicast traffic.
Definition: socket_misc.c:313
IPv4 (Internet Protocol Version 4)
Ipv6Addr ipv6Addr
Definition: ip.h:105
NetBuffer * ethAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an Ethernet frame.
Definition: ethernet.c:816
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:57
@ SOCKET_FLAG_DONT_WAIT
Definition: socket.h:139
TCP/IP stack core.
error_t rawSocketReceiveIpPacket(Socket *socket, SocketMsg *message, uint_t flags)
Receive an IP packet from a raw socket.
Definition: raw_socket.c:874
#define SOCKET_MAX_COUNT
Definition: socket.h:46
#define NET_IF_STATS_INC_COUNTER32(name, value)
Definition: net.h:203
@ SOCKET_ETH_PROTO_ALL
Definition: socket.h:119
Helper functions for Ethernet.
NetBuffer * buffer
Definition: socket.h:294
error_t ipv6SelectSourceAddr(NetContext *context, NetInterface **interface, const Ipv6Addr *destAddr, Ipv6Addr *srcAddr)
IPv6 source address selection.
Definition: ipv6_misc.c:905
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
size_t offset
Definition: socket.h:295
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:129
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83