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