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