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-2024 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.4.4
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 
742  //Select the relevant network interface
743  if(message->interface != NULL)
744  {
745  interface = message->interface;
746  }
747  else if(socket->interface != NULL)
748  {
749  interface = socket->interface;
750  }
751  else
752  {
753  interface = netGetDefaultInterface();
754  }
755 
756  //Forward the frame to the physical interface
757  interface = nicGetPhysicalInterface(interface);
758 
759  //Ethernet interface?
760  if(interface->nicDriver != NULL &&
761  interface->nicDriver->type == NIC_TYPE_ETHERNET)
762  {
763  //Allocate a buffer to hold the raw Ethernet packet
764  buffer = ethAllocBuffer(0, &offset);
765  //Failed to allocate buffer?
766  if(buffer == NULL)
767  return ERROR_OUT_OF_MEMORY;
768 
769  //Copy the raw data
770  error = netBufferAppend(buffer, message->data, message->length);
771 
772  //Check status code
773  if(!error)
774  {
775  NetTxAncillary ancillary;
776 
777  //Additional options can be passed to the stack along with the packet
778  ancillary = NET_DEFAULT_TX_ANCILLARY;
779 
780  //Set source MAC address
781  ancillary.srcMacAddr = message->srcMacAddr;
782 
783 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
784  //Set switch port identifier
785  ancillary.port = message->switchPort;
786 #endif
787 
788 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
789  //Unique identifier for hardware time stamping
790  ancillary.timestampId = message->timestampId;
791 #endif
792  //Debug message
793  TRACE_DEBUG("Sending raw Ethernet frame (%" PRIuSIZE " bytes)...\r\n", length);
794 
795  //Send raw Ethernet packet
796  error = ethSendFrame(interface, &message->destMacAddr,
797  message->ethType, buffer, offset, &ancillary);
798  }
799 
800  //Free previously allocated memory block
801  netBufferFree(buffer);
802  }
803  else
804 #endif
805  //Unknown interface type?
806  {
807  //Report an error
808  error = ERROR_INVALID_INTERFACE;
809  }
810 
811  //Return status code
812  return error;
813 }
814 
815 
816 /**
817  * @brief Receive an IP packet from a raw socket
818  * @param[in] socket Handle referencing the socket
819  * @param[out] message Received IP packet and ancillary data
820  * @param[in] flags Set of flags that influences the behavior of this function
821  * @return Error code
822  **/
823 
825  uint_t flags)
826 {
827  error_t error;
828  SocketQueueItem *queueItem;
829 
830  //The SOCKET_FLAG_DONT_WAIT enables non-blocking operation
831  if((flags & SOCKET_FLAG_DONT_WAIT) == 0)
832  {
833  //Check whether the receive queue is empty
834  if(socket->receiveQueue == NULL)
835  {
836  //Set the events the application is interested in
837  socket->eventMask = SOCKET_EVENT_RX_READY;
838 
839  //Reset the event object
840  osResetEvent(&socket->event);
841 
842  //Release exclusive access
844  //Wait until an event is triggered
845  osWaitForEvent(&socket->event, socket->timeout);
846  //Get exclusive access
848  }
849  }
850 
851  //Any packet received?
852  if(socket->receiveQueue != NULL)
853  {
854  //Point to the first item in the receive queue
855  queueItem = socket->receiveQueue;
856 
857  //Copy data to user buffer
858  message->length = netBufferRead(message->data, queueItem->buffer,
859  queueItem->offset, message->size);
860 
861  //Network interface where the packet was received
862  message->interface = queueItem->interface;
863  //Save the source IP address
864  message->srcIpAddr = queueItem->srcIpAddr;
865  //Save the source port number
866  message->srcPort = queueItem->srcPort;
867  //Save the destination IP address
868  message->destIpAddr = queueItem->destIpAddr;
869 
870  //Save TTL value
871  message->ttl = queueItem->ancillary.ttl;
872  //Save ToS field
873  message->tos = queueItem->ancillary.tos;
874 
875 #if (ETH_SUPPORT == ENABLED)
876  //Save source and destination MAC addresses
877  message->srcMacAddr = queueItem->ancillary.srcMacAddr;
878  message->destMacAddr = queueItem->ancillary.destMacAddr;
879 #endif
880 
881 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
882  //Save switch port identifier
883  message->switchPort = queueItem->ancillary.port;
884 #endif
885 
886 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
887  //Save captured time stamp
888  message->timestamp = queueItem->ancillary.timestamp;
889 #endif
890 
891  //If the SOCKET_FLAG_PEEK flag is set, the data is copied into the
892  //buffer but is not removed from the input queue
893  if((flags & SOCKET_FLAG_PEEK) == 0)
894  {
895  //Remove the item from the receive queue
896  socket->receiveQueue = queueItem->next;
897 
898  //Deallocate memory buffer
899  netBufferFree(queueItem->buffer);
900  }
901 
902  //Update the state of events
904 
905  //Successful read operation
906  error = NO_ERROR;
907  }
908  else
909  {
910  //Total number of data that have been received
911  message->length = 0;
912 
913  //Report a timeout error
914  error = ERROR_TIMEOUT;
915  }
916 
917  //Return status code
918  return error;
919 }
920 
921 
922 /**
923  * @brief Receive an Ethernet packet from a raw socket
924  * @param[in] socket Handle referencing the socket
925  * @param[out] message Received Ethernet packet and ancillary data
926  * @param[in] flags Set of flags that influences the behavior of this function
927  * @return Error code
928  **/
929 
931  uint_t flags)
932 {
933  error_t error;
934  SocketQueueItem *queueItem;
935 
936  //The SOCKET_FLAG_DONT_WAIT enables non-blocking operation
937  if((flags & SOCKET_FLAG_DONT_WAIT) == 0)
938  {
939  //Check whether the receive queue is empty
940  if(socket->receiveQueue == NULL)
941  {
942  //Set the events the application is interested in
943  socket->eventMask = SOCKET_EVENT_RX_READY;
944 
945  //Reset the event object
946  osResetEvent(&socket->event);
947 
948  //Release exclusive access
950  //Wait until an event is triggered
951  osWaitForEvent(&socket->event, socket->timeout);
952  //Get exclusive access
954  }
955  }
956 
957  //Any packet received?
958  if(socket->receiveQueue != NULL)
959  {
960  //Point to the first item in the receive queue
961  queueItem = socket->receiveQueue;
962 
963  //Copy data to user buffer
964  message->length = netBufferRead(message->data, queueItem->buffer,
965  queueItem->offset, message->size);
966 
967  //Network interface where the packet was received
968  message->interface = queueItem->interface;
969 
970 #if (ETH_SUPPORT == ENABLED)
971  //Save source and destination MAC addresses
972  message->srcMacAddr = queueItem->ancillary.srcMacAddr;
973  message->destMacAddr = queueItem->ancillary.destMacAddr;
974 
975  //Save the value of the EtherType field
976  message->ethType = queueItem->ancillary.ethType;
977 #endif
978 
979 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
980  //Save switch port identifier
981  message->switchPort = queueItem->ancillary.port;
982 #endif
983 
984 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
985  //Save captured time stamp
986  message->timestamp = queueItem->ancillary.timestamp;
987 #endif
988 
989  //If the SOCKET_FLAG_PEEK flag is set, the data is copied into the
990  //buffer but is not removed from the input queue
991  if((flags & SOCKET_FLAG_PEEK) == 0)
992  {
993  //Remove the item from the receive queue
994  socket->receiveQueue = queueItem->next;
995 
996  //Deallocate memory buffer
997  netBufferFree(queueItem->buffer);
998  }
999 
1000  //Update the state of events
1002 
1003  //Successful read operation
1004  error = NO_ERROR;
1005  }
1006  else
1007  {
1008  //Total number of data that have been received
1009  message->length = 0;
1010 
1011  //Report a timeout error
1012  error = ERROR_TIMEOUT;
1013  }
1014 
1015  //Return status code
1016  return error;
1017 }
1018 
1019 
1020 /**
1021  * @brief Update event state for raw sockets
1022  * @param[in] socket Handle referencing the socket
1023  **/
1024 
1026 {
1027  //Clear event flags
1028  socket->eventFlags = 0;
1029 
1030  //The socket is marked as readable if a datagram is pending in the queue
1031  if(socket->receiveQueue)
1032  socket->eventFlags |= SOCKET_EVENT_RX_READY;
1033 
1034  //Check whether the socket is bound to a particular network interface
1035  if(socket->interface != NULL)
1036  {
1037  //Handle link up and link down events
1038  if(socket->interface->linkState)
1039  {
1040  socket->eventFlags |= SOCKET_EVENT_LINK_UP;
1041  }
1042  else
1043  {
1044  socket->eventFlags |= SOCKET_EVENT_LINK_DOWN;
1045  }
1046  }
1047 
1048  //Mask unused events
1049  socket->eventFlags &= socket->eventMask;
1050 
1051  //Any event to signal?
1052  if(socket->eventFlags)
1053  {
1054  //Unblock I/O operations currently in waiting state
1055  osSetEvent(&socket->event);
1056 
1057  //Set user event to signaled state if necessary
1058  if(socket->userEvent != NULL)
1059  {
1060  osSetEvent(socket->userEvent);
1061  }
1062  }
1063 }
1064 
1065 #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:711
Ipv6PseudoHeader ipv6Data
Definition: ip.h:118
Ipv4Addr destAddr
Definition: ipv4.h:329
struct _SocketQueueItem SocketQueueItem
Receive queue item.
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:71
#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:222
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:297
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.
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:870
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:467
@ 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:368
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:1025
@ 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:351
#define TRACE_DEBUG(...)
Definition: debug.h:107
@ 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:930
#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:220
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:308
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:50
@ 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:824
#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