net.c
Go to the documentation of this file.
1 /**
2  * @file net.c
3  * @brief TCP/IP stack core
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/socket.h"
37 #include "core/raw_socket.h"
38 #include "core/tcp_timer.h"
39 #include "core/tcp_misc.h"
40 #include "core/ethernet.h"
41 #include "ipv4/arp.h"
42 #include "ipv4/ipv4.h"
43 #include "ipv4/ipv4_routing.h"
44 #include "ipv4/auto_ip_misc.h"
45 #include "igmp/igmp_host.h"
46 #include "igmp/igmp_router.h"
47 #include "igmp/igmp_snooping.h"
48 #include "dhcp/dhcp_client_misc.h"
49 #include "dhcp/dhcp_server_misc.h"
50 #include "nat/nat_misc.h"
51 #include "ipv6/ipv6.h"
52 #include "ipv6/ipv6_routing.h"
53 #include "ipv6/ndp.h"
54 #include "ipv6/ndp_router_adv.h"
55 #include "mld/mld_node.h"
57 #include "dns/dns_cache.h"
58 #include "dns/dns_client.h"
59 #include "mdns/mdns_client.h"
60 #include "mdns/mdns_responder.h"
61 #include "mdns/mdns_common.h"
63 #include "netbios/nbns_client.h"
64 #include "netbios/nbns_responder.h"
65 #include "netbios/nbns_common.h"
66 #include "llmnr/llmnr_responder.h"
67 #include "str.h"
68 #include "debug.h"
69 
70 #if (defined(WEB_SOCKET_SUPPORT) && WEB_SOCKET_SUPPORT == ENABLED)
71  #include "web_socket/web_socket.h"
72 #endif
73 
74 //Global variable
75 static NetContext *netDefaultContext;
76 
77 
78 /**
79  * @brief Initialize settings with default values
80  * @param[out] settings Structure that contains TCP/IP stack settings
81  **/
82 
84 {
85  //Default task parameters
86  settings->task = OS_TASK_DEFAULT_PARAMS;
88  settings->task.priority = NET_TASK_PRIORITY;
89 
90  //Network interfaces
91  settings->interfaces = NULL;
92  settings->numInterfaces = 0;
93 }
94 
95 
96 /**
97  * @brief Initialize TCP/IP stack
98  * @param[in] context Pointer to the TCP/IP stack context
99  * @param[in] settings TCP/IP stack specific settings
100  * @return Error code
101  **/
102 
103 error_t netInit(NetContext *context, const NetSettings *settings)
104 {
105  error_t error;
106  uint_t i;
107  NetInterface *interface;
108 
109  //Ensure the parameters are valid
110  if(context == NULL || settings == NULL)
112 
113  //Check settings
114  if(settings->interfaces == NULL || settings->numInterfaces < 1)
115  {
117  }
118 
119  //Set default TCP/IP stack context
120  netDefaultContext = context;
121 
122  //Clear TCP/IP stack context
123  osMemset(context, 0, sizeof(NetContext));
124 
125  //Initialize task parameters
126  context->taskParams = settings->task;
127  context->taskId = OS_INVALID_TASK_ID;
128 
129  //Save user settings
130  context->interfaces = settings->interfaces;
131  context->numInterfaces = settings->numInterfaces;
132 
133  //The TCP/IP process is currently suspended
134  context->running = FALSE;
135  //Get current time
136  context->timestamp = osGetSystemTime();
137 
138  //Create a mutex to prevent simultaneous access to the TCP/IP stack
139  if(!osCreateMutex(&context->mutex))
140  {
141  //Failed to create mutex
142  return ERROR_OUT_OF_RESOURCES;
143  }
144 
145  //Create a event object to receive notifications from device drivers
146  if(!osCreateEvent(&context->event))
147  {
148  //Failed to create mutex
149  return ERROR_OUT_OF_RESOURCES;
150  }
151 
152  //Memory pool initialization
153  error = memPoolInit();
154  //Any error to report?
155  if(error)
156  return error;
157 
158  //Loop through network interfaces
159  for(i = 0; i < context->numInterfaces; i++)
160  {
161  //Point to the current network interface
162  interface = &context->interfaces[i];
163 
164  //Initialize the structure representing the network interface
165  osMemset(interface, 0, sizeof(NetInterface));
166 
167  //Attach TCP/IP stack context
168  interface->netContext = context;
169 
170  //Zero-based index
171  interface->index = i;
172  //Unique number identifying the interface
173  interface->id = i;
174 
175  //Default interface name
176  osSprintf(interface->name, "eth%u", i);
177 
178 #if (ETH_SUPPORT == ENABLED)
179  //Default PHY address
180  interface->phyAddr = UINT8_MAX;
181 #endif
182 #if (TCP_SUPPORT == ENABLED)
183  //Default TCP initial retransmission timeout
184  interface->initialRto = TCP_INITIAL_RTO;
185 #endif
186  }
187 
188  //Socket related initialization
189  error = socketInit(context);
190  //Any error to report?
191  if(error)
192  return error;
193 
194 #if (defined(WEB_SOCKET_SUPPORT) && WEB_SOCKET_SUPPORT == ENABLED)
195  //WebSocket related initialization
196  webSocketInit();
197 #endif
198 
199 #if (IPV4_SUPPORT == ENABLED && IPV4_ROUTING_SUPPORT == ENABLED)
200  //Initialize IPv4 routing table
201  error = ipv4InitRouting(context);
202  //Any error to report?
203  if(error)
204  return error;
205 #endif
206 
207 #if (IPV6_SUPPORT == ENABLED && IPV6_ROUTING_SUPPORT == ENABLED)
208  //Initialize IPv6 routing table
209  error = ipv6InitRouting(context);
210  //Any error to report?
211  if(error)
212  return error;
213 #endif
214 
215 #if (UDP_SUPPORT == ENABLED)
216  //UDP related initialization
217  error = udpInit(context);
218  //Any error to report?
219  if(error)
220  return error;
221 #endif
222 
223 #if (TCP_SUPPORT == ENABLED)
224  //TCP related initialization
225  error = tcpInit(context);
226  //Any error to report?
227  if(error)
228  return error;
229 #endif
230 
231 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
232  NBNS_CLIENT_SUPPORT == ENABLED)
233  //DNS cache initialization
234  error = dnsInit();
235  //Any error to report?
236  if(error)
237  return error;
238 #endif
239 
240  //Initialize tick counters
241  context->nicTickCounter = 0;
242 
243 #if (PPP_SUPPORT == ENABLED)
244  context->pppTickCounter = 0;
245 #endif
246 #if (IPV4_SUPPORT == ENABLED && ETH_SUPPORT == ENABLED)
247  context->arpTickCounter = 0;
248 #endif
249 #if (IPV4_SUPPORT == ENABLED && IPV4_FRAG_SUPPORT == ENABLED)
250  context->ipv4FragTickCounter = 0;
251 #endif
252 #if (IPV4_SUPPORT == ENABLED && (IGMP_HOST_SUPPORT == ENABLED || \
253  IGMP_ROUTER_SUPPORT == ENABLED || IGMP_SNOOPING_SUPPORT == ENABLED))
254  context->igmpTickCounter = 0;
255 #endif
256 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
257  context->autoIpTickCounter = 0;
258 #endif
259 #if (IPV4_SUPPORT == ENABLED && DHCP_CLIENT_SUPPORT == ENABLED)
260  context->dhcpClientTickCounter = 0;
261 #endif
262 #if (IPV4_SUPPORT == ENABLED && DHCP_SERVER_SUPPORT == ENABLED)
263  context->dhcpServerTickCounter = 0;
264 #endif
265 #if (IPV4_SUPPORT == ENABLED && NAT_SUPPORT == ENABLED)
266  context->natTickCounter = 0;
267 #endif
268 #if (IPV6_SUPPORT == ENABLED && IPV6_FRAG_SUPPORT == ENABLED)
269  context->ipv6FragTickCounter = 0;
270 #endif
271 #if (IPV6_SUPPORT == ENABLED && MLD_NODE_SUPPORT == ENABLED)
272  context->mldTickCounter = 0;
273 #endif
274 #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED)
275  context->ndpTickCounter = 0;
276 #endif
277 #if (IPV6_SUPPORT == ENABLED && NDP_ROUTER_ADV_SUPPORT == ENABLED)
278  context->ndpRouterAdvTickCounter = 0;
279 #endif
280 #if (IPV6_SUPPORT == ENABLED && DHCPV6_CLIENT_SUPPORT == ENABLED)
281  context->dhcpv6ClientTickCounter = 0;
282 #endif
283 #if (TCP_SUPPORT == ENABLED)
284  context->tcpTickCounter = 0;
285 #endif
286 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
287  NBNS_CLIENT_SUPPORT == ENABLED)
288  context->dnsTickCounter = 0;
289 #endif
290 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
291  context->mdnsResponderTickCounter = 0;
292 #endif
293 #if (DNS_SD_RESPONDER_SUPPORT == ENABLED)
294  context->dnsSdResponderTickCounter = 0;
295 #endif
296 
297  //Successful initialization
298  return NO_ERROR;
299 }
300 
301 
302 /**
303  * @brief Get exclusive access to the core of the TCP/IP stack
304  * @param[in] context Pointer to the TCP/IP stack context
305  **/
306 
307 void netLock(NetContext *context)
308 {
309  //Get exclusive access
310  osAcquireMutex(&context->mutex);
311 }
312 
313 
314 /**
315  * @brief Release exclusive access to the core of the TCP/IP stack
316  * @param[in] context Pointer to the TCP/IP stack context
317  **/
318 
319 void netUnlock(NetContext *context)
320 {
321  //Release exclusive access
322  osReleaseMutex(&context->mutex);
323 }
324 
325 
326 /**
327  * @brief Start TCP/IP stack
328  * @param[in] context Pointer to the TCP/IP stack context
329  * @return Error code
330  **/
331 
333 {
334  error_t error;
335 
336  //Initialize status code
337  error = NO_ERROR;
338 
339  //Make sure the TCP/IP stack context is valid
340  if(context != NULL)
341  {
342  //Make sure the TCP/IP stack service is not already running
343  if(!context->running)
344  {
345  //Start the TCP/IP stack
346  context->stop = FALSE;
347 #if (NET_RTOS_SUPPORT == DISABLED)
348  context->running = TRUE;
349 #endif
350  //Create a task
351  context->taskId = osCreateTask("TCP/IP", (OsTaskCode) netTask, context,
352  &context->taskParams);
353 
354  //Failed to create task?
355  if(context->taskId == OS_INVALID_TASK_ID)
356  {
357  //Report an error
358  error = ERROR_OUT_OF_RESOURCES;
359  }
360 
361  //Any error to report?
362  if(error)
363  {
364  //Clean up side effects
365  context->running = FALSE;
366  }
367  }
368  else
369  {
370  //The TCP/IP stack is already running
371  error = ERROR_ALREADY_RUNNING;
372  }
373  }
374  else
375  {
376  //Report an error
377  error = ERROR_INVALID_PARAMETER;
378  }
379 
380  //Return status code
381  return error;
382 }
383 
384 
385 /**
386  * @brief Seed the pseudo-random number generator
387  * @param[in] context Pointer to the TCP/IP stack context
388  * @param[in] seed Pointer to the random seed
389  * @param[in] length Length of the random seed, in bytes
390  * @return Error code
391  **/
392 
393 error_t netSeedRand(NetContext *context, const uint8_t *seed, size_t length)
394 {
395  size_t i;
396  size_t j;
397 
398  //Check parameters
399  if(context == NULL || seed == NULL || length == 0)
401 
402  //Get exclusive access
403  if(context->running)
404  {
405  netLock(context);
406  }
407 
408  //Save random seed
409  for(i = 0, j = 0; i < NET_RAND_SEED_SIZE; i++)
410  {
411  //Copy current byte
412  context->randSeed[i] = seed[j];
413 
414  //Increment index and wrap around if necessary
415  if(++j >= length)
416  {
417  j = 0;
418  }
419  }
420 
421  //Initialize pseudo-random generator
422  netInitRand(context);
423 
424  //Release exclusive access
425  if(context->running)
426  {
427  netUnlock(context);
428  }
429 
430  //Successful processing
431  return NO_ERROR;
432 }
433 
434 
435 /**
436  * @brief Generate a random 32-bit value
437  * @param[in] context Pointer to the TCP/IP stack context
438  * @return Random value
439  **/
440 
441 uint32_t netGetRand(NetContext *context)
442 {
443  uint32_t value;
444 
445  //Get exclusive access
446  if(context->running)
447  {
448  netLock(context);
449  }
450 
451  //Generate a random 32-bit value
452  value = netGenerateRand(context);
453 
454  //Release exclusive access
455  if(context->running)
456  {
457  netUnlock(context);
458  }
459 
460  //Return the random value
461  return value;
462 }
463 
464 
465 /**
466  * @brief Generate a random value in the specified range
467  * @param[in] context Pointer to the TCP/IP stack context
468  * @param[in] min Lower bound
469  * @param[in] max Upper bound
470  * @return Random value in the specified range
471  **/
472 
473 uint32_t netGetRandRange(NetContext *context, uint32_t min, uint32_t max)
474 {
475  uint32_t value;
476 
477  //Get exclusive access
478  if(context->running)
479  {
480  netLock(context);
481  }
482 
483  //Generate a random value in the specified range
484  value = netGenerateRandRange(context, min, max);
485 
486  //Release exclusive access
487  if(context->running)
488  {
489  netUnlock(context);
490  }
491 
492  //Return the random value
493  return value;
494 }
495 
496 
497 /**
498  * @brief Get a string of random data
499  * @param[in] context Pointer to the TCP/IP stack context
500  * @param[out] data Buffer where to store random data
501  * @param[in] length Number of random bytes to generate
502  **/
503 
504 void netGetRandData(NetContext *context, uint8_t *data, size_t length)
505 {
506  //Get exclusive access
507  if(context->running)
508  {
509  netLock(context);
510  }
511 
512  //Generate a random value in the specified range
513  netGenerateRandData(context, data, length);
514 
515  //Release exclusive access
516  if(context->running)
517  {
518  netUnlock(context);
519  }
520 }
521 
522 
523 /**
524  * @brief Get default TCP/IP stack context
525  **/
526 
528 {
529  //Return a pointer to the default TCP/IP stack context
530  return netDefaultContext;
531 }
532 
533 
534 /**
535  * @brief Get default network interface
536  * @param[in] context Pointer to the TCP/IP stack context
537  * @return Pointer to the default network interface to be used
538  **/
539 
541 {
542  NetInterface *interface;
543 
544  //Select the default network interface
545  if(context != NULL)
546  {
547  interface = &context->interfaces[0];
548  }
549  else if(netDefaultContext != NULL)
550  {
551  interface = &netDefaultContext->interfaces[0];
552  }
553  else
554  {
555  interface = NULL;
556  }
557 
558  //Return a pointer to the network interface
559  return interface;
560 }
561 
562 
563 /**
564  * @brief Set MAC address
565  * @param[in] interface Pointer to the desired network interface
566  * @param[in] macAddr MAC address
567  * @return Error code
568  **/
569 
570 error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
571 {
572 #if (ETH_SUPPORT == ENABLED)
573  //Check parameters
574  if(interface == NULL || macAddr == NULL)
576 
577  //Get exclusive access
578  netLock(interface->netContext);
579 
580  //Set MAC address
581  interface->macAddr = *macAddr;
582 
583  //Generate the 64-bit interface identifier
584  macAddrToEui64(macAddr, &interface->eui64);
585 
586  //Release exclusive access
587  netUnlock(interface->netContext);
588 
589  //Successful processing
590  return NO_ERROR;
591 #else
592  //Not implemented
593  return ERROR_NOT_IMPLEMENTED;
594 #endif
595 }
596 
597 
598 /**
599  * @brief Retrieve MAC address
600  * @param[in] interface Pointer to the desired network interface
601  * @param[out] macAddr MAC address
602  * @return Error code
603  **/
604 
606 {
607 #if (ETH_SUPPORT == ENABLED)
608  NetInterface *logicalInterface;
609 
610  //Check parameters
611  if(interface == NULL || macAddr == NULL)
613 
614  //Get exclusive access
615  netLock(interface->netContext);
616 
617  //Point to the logical interface
618  logicalInterface = nicGetLogicalInterface(interface);
619 
620  //Get MAC address
621  *macAddr = logicalInterface->macAddr;
622 
623  //Release exclusive access
624  netUnlock(interface->netContext);
625 
626  //Successful processing
627  return NO_ERROR;
628 #else
629  //Not implemented
630  return ERROR_NOT_IMPLEMENTED;
631 #endif
632 }
633 
634 
635 /**
636  * @brief Set EUI-64 interface identifier
637  * @param[in] interface Pointer to the desired network interface
638  * @param[in] eui64 Interface identifier
639  * @return Error code
640  **/
641 
642 error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
643 {
644  //Check parameters
645  if(interface == NULL || eui64 == NULL)
647 
648  //Get exclusive access
649  netLock(interface->netContext);
650  //Set interface identifier
651  interface->eui64 = *eui64;
652  //Release exclusive access
653  netUnlock(interface->netContext);
654 
655  //Successful processing
656  return NO_ERROR;
657 }
658 
659 
660 /**
661  * @brief Retrieve EUI-64 interface identifier
662  * @param[in] interface Pointer to the desired network interface
663  * @param[out] eui64 Interface identifier
664  * @return Error code
665  **/
666 
668 {
669  NetInterface *logicalInterface;
670 
671  //Check parameters
672  if(interface == NULL || eui64 == NULL)
674 
675  //Get exclusive access
676  netLock(interface->netContext);
677 
678  //Point to the logical interface
679  logicalInterface = nicGetLogicalInterface(interface);
680 
681  //Get interface identifier
682  *eui64 = logicalInterface->eui64;
683 
684  //Release exclusive access
685  netUnlock(interface->netContext);
686 
687  //Successful processing
688  return NO_ERROR;
689 }
690 
691 
692 /**
693  * @brief Set interface identifier
694  * @param[in] interface Pointer to the desired network interface
695  * @param[in] id Unique number identifying the interface
696  * @return Error code
697  **/
698 
699 error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
700 {
701  //Check parameters
702  if(interface == NULL)
704 
705  //Get exclusive access
706  netLock(interface->netContext);
707  //Set interface identifier
708  interface->id = id;
709  //Release exclusive access
710  netUnlock(interface->netContext);
711 
712  //Successful processing
713  return NO_ERROR;
714 }
715 
716 
717 /**
718  * @brief Set interface name
719  * @param[in] interface Pointer to the desired network interface
720  * @param[in] name NULL-terminated string that contains the interface name
721  * @return Error code
722  **/
723 
725 {
726  //Check parameters
727  if(interface == NULL || name == NULL)
729 
730  //Make sure the length of the interface name is acceptable
732  return ERROR_INVALID_LENGTH;
733 
734  //Get exclusive access
735  netLock(interface->netContext);
736  //Set interface name
737  osStrcpy(interface->name, name);
738  //Release exclusive access
739  netUnlock(interface->netContext);
740 
741  //Successful processing
742  return NO_ERROR;
743 }
744 
745 
746 /**
747  * @brief Set host name
748  * @param[in] interface Pointer to the desired network interface
749  * @param[in] name NULL-terminated string that contains the host name
750  * @return Error code
751  **/
752 
754 {
755  //Check parameters
756  if(interface == NULL || name == NULL)
758 
759  //Make sure the length of the host name is acceptable
761  return ERROR_INVALID_LENGTH;
762 
763  //Get exclusive access
764  netLock(interface->netContext);
765  //Set host name
766  osStrcpy(interface->hostname, name);
767  //Release exclusive access
768  netUnlock(interface->netContext);
769 
770  //Successful processing
771  return NO_ERROR;
772 }
773 
774 
775 /**
776  * @brief Specify VLAN identifier (802.1Q)
777  * @param[in] interface Pointer to the desired network interface
778  * @param[in] vlanId VLAN identifier
779  * @return Error code
780  **/
781 
782 error_t netSetVlanId(NetInterface *interface, uint16_t vlanId)
783 {
784 #if (ETH_VLAN_SUPPORT == ENABLED)
785  //Make sure the network interface is valid
786  if(interface == NULL)
788 
789  //The VID value FFF is reserved
790  if((vlanId & VLAN_VID_MASK) == VLAN_VID_MASK)
792 
793  //Get exclusive access
794  netLock(interface->netContext);
795  //Set VLAN identifier
796  interface->vlanId = vlanId;
797  //Release exclusive access
798  netUnlock(interface->netContext);
799 
800  //Successful processing
801  return NO_ERROR;
802 #else
803  //Not implemented
804  return ERROR_NOT_IMPLEMENTED;
805 #endif
806 }
807 
808 
809 /**
810  * @brief Specify VMAN identifier (802.1ad)
811  * @param[in] interface Pointer to the desired network interface
812  * @param[in] vmanId VMAN identifier
813  * @return Error code
814  **/
815 
816 error_t netSetVmanId(NetInterface *interface, uint16_t vmanId)
817 {
818 #if (ETH_VMAN_SUPPORT == ENABLED)
819  //Make sure the network interface is valid
820  if(interface == NULL)
822 
823  //The VID value FFF is reserved
824  if((vmanId & VLAN_VID_MASK) == VLAN_VID_MASK)
826 
827  //Get exclusive access
828  netLock(interface->netContext);
829  //Set VMAN identifier
830  interface->vmanId = vmanId;
831  //Release exclusive access
832  netUnlock(interface->netContext);
833 
834  //Successful processing
835  return NO_ERROR;
836 #else
837  //Not implemented
838  return ERROR_NOT_IMPLEMENTED;
839 #endif
840 }
841 
842 
843 /**
844  * @brief Attach a virtual interface to a given physical interface
845  * @param[in] interface Pointer to the virtual interface
846  * @param[in] physicalInterface physical interface on top of which the virtual
847  * interface will run
848  * @return Error code
849  **/
850 
852  NetInterface *physicalInterface)
853 {
854 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_VLAN_SUPPORT == ENABLED || \
855  ETH_PORT_TAGGING_SUPPORT == ENABLED)
856  //Make sure the network interface is valid
857  if(interface == NULL)
859 
860  //Get exclusive access
861  netLock(interface->netContext);
862  //Bind the virtual interface to the physical interface
863  interface->parent = physicalInterface;
864  //Release exclusive access
865  netUnlock(interface->netContext);
866 
867  //Successful processing
868  return NO_ERROR;
869 #else
870  //Not implemented
871  return ERROR_NOT_IMPLEMENTED;
872 #endif
873 }
874 
875 
876 /**
877  * @brief Set Ethernet MAC driver
878  * @param[in] interface Pointer to the desired network interface
879  * @param[in] driver Ethernet MAC driver
880  * @return Error code
881  **/
882 
883 error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
884 {
885  //Check parameters
886  if(interface == NULL || driver == NULL)
888 
889  //Get exclusive access
890  netLock(interface->netContext);
891  //Set Ethernet MAC driver
892  interface->nicDriver = driver;
893  //Release exclusive access
894  netUnlock(interface->netContext);
895 
896  //Successful processing
897  return NO_ERROR;
898 }
899 
900 
901 /**
902  * @brief Set Ethernet PHY driver
903  * @param[in] interface Pointer to the desired network interface
904  * @param[in] driver Ethernet PHY driver (can be NULL for MAC + PHY controller)
905  * @return Error code
906  **/
907 
908 error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
909 {
910 #if (ETH_SUPPORT == ENABLED)
911  //Check parameters
912  if(interface == NULL || driver == NULL)
914 
915  //Get exclusive access
916  netLock(interface->netContext);
917  //Set Ethernet PHY driver
918  interface->phyDriver = driver;
919  //Release exclusive access
920  netUnlock(interface->netContext);
921 
922  //Successful processing
923  return NO_ERROR;
924 #else
925  //Not implemented
926  return ERROR_NOT_IMPLEMENTED;
927 #endif
928 }
929 
930 
931 /**
932  * @brief Specify Ethernet PHY address
933  * @param[in] interface Pointer to the desired network interface
934  * @param[in] phyAddr PHY address
935  * @return Error code
936  **/
937 
938 error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
939 {
940 #if (ETH_SUPPORT == ENABLED)
941  //Make sure the network interface is valid
942  if(interface == NULL)
944 
945  //Make sure the PHY address is valid
946  if(phyAddr >= 32)
947  return ERROR_OUT_OF_RANGE;
948 
949  //Get exclusive access
950  netLock(interface->netContext);
951  //Set PHY address
952  interface->phyAddr = phyAddr;
953  //Release exclusive access
954  netUnlock(interface->netContext);
955 
956  //Successful processing
957  return NO_ERROR;
958 #else
959  //Not implemented
960  return ERROR_NOT_IMPLEMENTED;
961 #endif
962 }
963 
964 
965 /**
966  * @brief Set Ethernet switch driver
967  * @param[in] interface Pointer to the desired network interface
968  * @param[in] driver Ethernet switch driver
969  * @return Error code
970  **/
971 
973 {
974 #if (ETH_SUPPORT == ENABLED)
975  //Check parameters
976  if(interface == NULL || driver == NULL)
978 
979  //Get exclusive access
980  netLock(interface->netContext);
981  //Set Ethernet switch driver
982  interface->switchDriver = driver;
983  //Release exclusive access
984  netUnlock(interface->netContext);
985 
986  //Successful processing
987  return NO_ERROR;
988 #else
989  //Not implemented
990  return ERROR_NOT_IMPLEMENTED;
991 #endif
992 }
993 
994 
995 /**
996  * @brief Specify switch port
997  * @param[in] interface Pointer to the desired network interface
998  * @param[in] port Switch port identifier
999  * @return Error code
1000  **/
1001 
1003 {
1004 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
1005  //Make sure the network interface is valid
1006  if(interface == NULL)
1007  return ERROR_INVALID_PARAMETER;
1008 
1009  //Get exclusive access
1010  netLock(interface->netContext);
1011  //Set switch port identifier
1012  interface->port = port;
1013  //Release exclusive access
1014  netUnlock(interface->netContext);
1015 
1016  //Successful processing
1017  return NO_ERROR;
1018 #else
1019  //Not implemented
1020  return ERROR_NOT_IMPLEMENTED;
1021 #endif
1022 }
1023 
1024 
1025 /**
1026  * @brief Set SMI driver
1027  * @param[in] interface Pointer to the desired network interface
1028  * @param[in] driver Underlying SMI driver
1029  * @return Error code
1030  **/
1031 
1032 error_t netSetSmiDriver(NetInterface *interface, const SmiDriver *driver)
1033 {
1034 #if (ETH_SUPPORT == ENABLED)
1035  //Check parameters
1036  if(interface == NULL || driver == NULL)
1037  return ERROR_INVALID_PARAMETER;
1038 
1039  //Get exclusive access
1040  netLock(interface->netContext);
1041  //Set SMI driver
1042  interface->smiDriver = driver;
1043  //Release exclusive access
1044  netUnlock(interface->netContext);
1045 
1046  //Successful processing
1047  return NO_ERROR;
1048 #else
1049  //Not implemented
1050  return ERROR_NOT_IMPLEMENTED;
1051 #endif
1052 }
1053 
1054 
1055 /**
1056  * @brief Set SPI driver
1057  * @param[in] interface Pointer to the desired network interface
1058  * @param[in] driver Underlying SPI driver
1059  * @return Error code
1060  **/
1061 
1062 error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
1063 {
1064  //Check parameters
1065  if(interface == NULL || driver == NULL)
1066  return ERROR_INVALID_PARAMETER;
1067 
1068  //Get exclusive access
1069  netLock(interface->netContext);
1070  //Set SPI driver
1071  interface->spiDriver = driver;
1072  //Release exclusive access
1073  netUnlock(interface->netContext);
1074 
1075  //Successful processing
1076  return NO_ERROR;
1077 }
1078 
1079 
1080 /**
1081  * @brief Set UART driver
1082  * @param[in] interface Pointer to the desired network interface
1083  * @param[in] driver Underlying UART driver
1084  * @return Error code
1085  **/
1086 
1088 {
1089  //Check parameters
1090  if(interface == NULL || driver == NULL)
1091  return ERROR_INVALID_PARAMETER;
1092 
1093  //Get exclusive access
1094  netLock(interface->netContext);
1095  //Set UART driver
1096  interface->uartDriver = driver;
1097  //Release exclusive access
1098  netUnlock(interface->netContext);
1099 
1100  //Successful processing
1101  return NO_ERROR;
1102 }
1103 
1104 
1105 /**
1106  * @brief Set external interrupt line driver
1107  * @param[in] interface Pointer to the desired network interface
1108  * @param[in] driver Underlying SPI driver
1109  * @return Error code
1110  **/
1111 
1113 {
1114  //Check parameters
1115  if(interface == NULL || driver == NULL)
1116  return ERROR_INVALID_PARAMETER;
1117 
1118  //Get exclusive access
1119  netLock(interface->netContext);
1120  //Set external interrupt line driver
1121  interface->extIntDriver = driver;
1122  //Release exclusive access
1123  netUnlock(interface->netContext);
1124 
1125  //Successful processing
1126  return NO_ERROR;
1127 }
1128 
1129 
1130 /**
1131  * @brief Set administrative link state
1132  * @param[in] interface Pointer to the desired network interface
1133  * @param[in] linkState Administrative link state (up or down)
1134  * @return Error code
1135  **/
1136 
1138 {
1139  //Make sure the network interface is valid
1140  if(interface == NULL)
1141  return ERROR_INVALID_PARAMETER;
1142 
1143  //Get exclusive access
1144  netLock(interface->netContext);
1145 
1146  //Any change detected?
1147  if(linkState != interface->linkState)
1148  {
1149  //Update link state
1150  interface->linkState = linkState;
1151  //Process link state change event
1152  netProcessLinkChange(interface);
1153  }
1154 
1155  //Release exclusive access
1156  netUnlock(interface->netContext);
1157 
1158  //Successful processing
1159  return NO_ERROR;
1160 }
1161 
1162 
1163 /**
1164  * @brief Get link state
1165  * @param[in] interface Pointer to the desired network interface
1166  * @return Link state
1167  **/
1168 
1170 {
1171  bool_t linkState;
1172 
1173  //Make sure the network interface is valid
1174  if(interface != NULL)
1175  {
1176  //Get exclusive access
1177  netLock(interface->netContext);
1178  //Retrieve link state
1179  linkState = interface->linkState;
1180  //Release exclusive access
1181  netUnlock(interface->netContext);
1182  }
1183  else
1184  {
1185  //Unknown link state
1186  linkState = FALSE;
1187  }
1188 
1189  //Return link state
1190  return linkState;
1191 }
1192 
1193 
1194 /**
1195  * @brief Get link speed
1196  * @param[in] interface Pointer to the desired network interface
1197  * @return Link speed
1198  **/
1199 
1201 {
1202  uint_t linkSpeed;
1203 
1204  //Make sure the network interface is valid
1205  if(interface != NULL)
1206  {
1207  //Get exclusive access
1208  netLock(interface->netContext);
1209  //Retrieve link speed
1210  linkSpeed = interface->linkSpeed;
1211  //Release exclusive access
1212  netUnlock(interface->netContext);
1213  }
1214  else
1215  {
1216  //Unknown link speed
1217  linkSpeed = NIC_LINK_SPEED_UNKNOWN;
1218  }
1219 
1220  //Return link speed
1221  return linkSpeed;
1222 }
1223 
1224 
1225 /**
1226  * @brief Get duplex mode
1227  * @param[in] interface Pointer to the desired network interface
1228  * @return Duplex mode
1229  **/
1230 
1232 {
1233  NicDuplexMode duplexMode;
1234 
1235  //Make sure the network interface is valid
1236  if(interface != NULL)
1237  {
1238  //Get exclusive access
1239  netLock(interface->netContext);
1240  //Retrieve duplex mode
1241  duplexMode = interface->duplexMode;
1242  //Release exclusive access
1243  netUnlock(interface->netContext);
1244  }
1245  else
1246  {
1247  //Unknown duplex mode
1248  duplexMode = NIC_UNKNOWN_DUPLEX_MODE;
1249  }
1250 
1251  //Return duplex mode
1252  return duplexMode;
1253 }
1254 
1255 
1256 /**
1257  * @brief Enable promiscuous mode
1258  * @param[in] interface Pointer to the desired network interface
1259  * @param[in] enable Enable or disable promiscuous mode
1260  * @return Error code
1261  **/
1262 
1264 {
1265  //Make sure the network interface is valid
1266  if(interface == NULL)
1267  return ERROR_INVALID_PARAMETER;
1268 
1269 #if (ETH_SUPPORT == ENABLED)
1270  //Get exclusive access
1271  netLock(interface->netContext);
1272  //Enable or disable promiscuous mode
1273  interface->promiscuous = enable;
1274  //Release exclusive access
1275  netUnlock(interface->netContext);
1276 #endif
1277 
1278  //Successful processing
1279  return NO_ERROR;
1280 }
1281 
1282 
1283 /**
1284  * @brief Configure network interface
1285  * @param[in] interface Network interface to configure
1286  * @return Error code
1287  **/
1288 
1290 {
1291  error_t error;
1292 
1293  //Make sure the network interface is valid
1294  if(interface == NULL)
1295  return ERROR_INVALID_PARAMETER;
1296 
1297  //Get exclusive access
1298  netLock(interface->netContext);
1299 
1300  //Disable hardware interrupts
1301  if(interface->nicDriver != NULL)
1302  {
1303  interface->nicDriver->disableIrq(interface);
1304  }
1305 
1306  //Start of exception handling block
1307  do
1308  {
1309  //Receive notifications when the transmitter is ready to send
1310  if(!osCreateEvent(&interface->nicTxEvent))
1311  {
1312  //Failed to create event object
1313  error = ERROR_OUT_OF_RESOURCES;
1314  //Stop immediately
1315  break;
1316  }
1317 
1318  //Valid NIC driver?
1319  if(interface->nicDriver != NULL)
1320  {
1321  //Network controller initialization
1322  error = interface->nicDriver->init(interface);
1323  //Any error to report?
1324  if(error)
1325  break;
1326  }
1327  else
1328  {
1329 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_PORT_TAGGING_SUPPORT == ENABLED)
1330  NetInterface *physicalInterface;
1331 
1332  //Point to the physical interface
1333  physicalInterface = nicGetPhysicalInterface(interface);
1334 
1335  //Check whether the network interface is a virtual interface
1336  if(physicalInterface != interface)
1337  {
1338  //Valid MAC address assigned to the virtual interface?
1339  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1340  {
1341  //Configure the physical interface to accept the MAC address of
1342  //the virtual interface
1343  error = ethAcceptMacAddr(physicalInterface, &interface->macAddr);
1344  //Any error to report?
1345  if(error)
1346  break;
1347  }
1348  }
1349 #endif
1350  }
1351 
1352 #if (ETH_SUPPORT == ENABLED)
1353  //Ethernet related initialization
1354  error = ethInit(interface);
1355  //Any error to report?
1356  if(error)
1357  break;
1358 #endif
1359 
1360 #if (IPV4_SUPPORT == ENABLED)
1361  //IPv4 initialization
1362  error = ipv4Init(interface);
1363  //Any error to report?
1364  if(error)
1365  break;
1366 
1367 #if (ETH_SUPPORT == ENABLED)
1368  //ARP cache initialization
1369  error = arpInit(interface);
1370  //Any error to report?
1371  if(error)
1372  break;
1373 #endif
1374 
1375 #if (IPV4_SUPPORT == ENABLED && (IGMP_HOST_SUPPORT == ENABLED || \
1376  IGMP_ROUTER_SUPPORT == ENABLED || IGMP_SNOOPING_SUPPORT == ENABLED))
1377  //IGMP related initialization
1378  error = igmpInit(interface);
1379  //Any error to report?
1380  if(error)
1381  break;
1382 #endif
1383 
1384 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
1385  //NetBIOS Name Service related initialization
1386  error = nbnsInit(interface);
1387  //Any error to report?
1388  if(error)
1389  break;
1390 #endif
1391 #endif
1392 
1393 #if (IPV6_SUPPORT == ENABLED)
1394  //IPv6 initialization
1395  error = ipv6Init(interface);
1396  //Any error to report?
1397  if(error)
1398  break;
1399 
1400 #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED)
1401  //NDP related initialization
1402  error = ndpInit(interface);
1403  //Any error to report?
1404  if(error)
1405  break;
1406 #endif
1407 
1408 #if (IPV6_SUPPORT == ENABLED && MLD_NODE_SUPPORT == ENABLED)
1409  //MLD related initialization
1410  error = mldInit(interface);
1411  //Any error to report?
1412  if(error)
1413  break;
1414 #endif
1415 #endif
1416 
1417 #if (MDNS_CLIENT_SUPPORT == ENABLED || MDNS_RESPONDER_SUPPORT == ENABLED)
1418  //mDNS related initialization
1419  error = mdnsInit(interface);
1420  //Any error to report?
1421  if(error)
1422  break;
1423 #endif
1424 
1425 #if (LLMNR_RESPONDER_SUPPORT == ENABLED)
1426  //LLMNR responder initialization
1427  error = llmnrResponderInit(interface);
1428  //Any error to report?
1429  if(error)
1430  break;
1431 #endif
1432 
1433  //End of exception handling block
1434  } while(0);
1435 
1436  //Check status code
1437  if(!error)
1438  {
1439  //Initialize pseudo-random generator
1440  netInitRand(interface->netContext);
1441 
1442  //The network interface is now fully configured
1443  interface->configured = TRUE;
1444 
1445  //Check whether the TCP/IP process is running
1446  if(interface->netContext->running)
1447  {
1448  //Interrupts can be safely enabled
1449  if(interface->nicDriver != NULL)
1450  {
1451  interface->nicDriver->enableIrq(interface);
1452  }
1453  }
1454  }
1455  else
1456  {
1457  //Clean up side effects before returning
1458  osDeleteEvent(&interface->nicTxEvent);
1459  }
1460 
1461  //Release exclusive access
1462  netUnlock(interface->netContext);
1463 
1464  //Return status code
1465  return error;
1466 }
1467 
1468 
1469 /**
1470  * @brief Start network interface
1471  * @param[in] interface Network interface to start
1472  * @return Error code
1473  **/
1474 
1476 {
1477  error_t error;
1478 
1479  //Make sure the network interface is valid
1480  if(interface == NULL)
1481  return ERROR_INVALID_PARAMETER;
1482 
1483  //Initialize status code
1484  error = NO_ERROR;
1485 
1486  //Get exclusive access
1487  netLock(interface->netContext);
1488 
1489 #if (ETH_SUPPORT == ENABLED)
1490  //Check whether the interface is enabled for operation
1491  if(!interface->configured)
1492  {
1493  NetInterface *physicalInterface;
1494 
1495  //Point to the physical interface
1496  physicalInterface = nicGetPhysicalInterface(interface);
1497 
1498  //Virtual interface?
1499  if(interface != physicalInterface)
1500  {
1501  //Valid MAC address assigned to the virtual interface?
1502  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1503  {
1504  //Configure the physical interface to accept the MAC address of
1505  //the virtual interface
1506  error = ethAcceptMacAddr(physicalInterface, &interface->macAddr);
1507  }
1508  }
1509  else
1510  {
1511 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
1512  //Valid switch driver?
1513  if(interface->switchDriver != NULL &&
1514  interface->switchDriver->init != NULL)
1515  {
1516  //Reconfigure switch operation
1517  error = interface->switchDriver->init(interface);
1518  }
1519 #endif
1520  //Check status code
1521  if(!error)
1522  {
1523  //Update the MAC filter
1524  error = nicUpdateMacAddrFilter(interface);
1525  }
1526  }
1527  }
1528 #endif
1529 
1530  //Enable network interface
1531  interface->configured = TRUE;
1532 
1533  //Check whether the TCP/IP process is running
1534  if(interface->netContext->running)
1535  {
1536  //Interrupts can be safely enabled
1537  if(interface->nicDriver != NULL)
1538  {
1539  interface->nicDriver->enableIrq(interface);
1540  }
1541  }
1542 
1543  //Release exclusive access
1544  netUnlock(interface->netContext);
1545 
1546  //Return status code
1547  return error;
1548 }
1549 
1550 
1551 /**
1552  * @brief Stop network interface
1553  * @param[in] interface Network interface to stop
1554  * @return Error code
1555  **/
1556 
1558 {
1559  NetInterface *physicalInterface;
1560 
1561  //Make sure the network interface is valid
1562  if(interface == NULL)
1563  return ERROR_INVALID_PARAMETER;
1564 
1565  //Get exclusive access
1566  netLock(interface->netContext);
1567 
1568  //Point to the physical interface
1569  physicalInterface = nicGetPhysicalInterface(interface);
1570 
1571  //Check whether the interface is enabled for operation
1572  if(interface->configured)
1573  {
1574  //Update link state
1575  interface->linkState = FALSE;
1576  //Process link state change event
1577  netProcessLinkChange(interface);
1578 
1579  //Disable hardware interrupts
1580  if(interface->nicDriver != NULL)
1581  interface->nicDriver->disableIrq(interface);
1582 
1583  //Disable network interface
1584  interface->configured = FALSE;
1585 
1586  //Virtual interface?
1587  if(interface != physicalInterface)
1588  {
1589 #if (ETH_SUPPORT == ENABLED)
1590  //Valid MAC address assigned to the virtual interface?
1591  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1592  {
1593  //Drop the corresponding address from the MAC filter table of
1594  //the physical interface
1595  ethDropMacAddr(physicalInterface, &interface->macAddr);
1596  }
1597 #endif
1598  }
1599  }
1600 
1601  //Release exclusive access
1602  netUnlock(interface->netContext);
1603 
1604  //Successful operation
1605  return NO_ERROR;
1606 }
1607 
1608 
1609 /**
1610  * @brief TCP/IP events handling
1611  * @param[in] context Pointer to the TCP/IP stack context
1612  **/
1613 
1614 void netTask(NetContext *context)
1615 {
1616  uint_t i;
1617  bool_t status;
1618  systime_t time;
1619  systime_t timeout;
1620  NetInterface *interface;
1621 
1622 #if (NET_RTOS_SUPPORT == ENABLED)
1623  //Task prologue
1624  osEnterTask();
1625 
1626  //Get exclusive access
1627  netLock(context);
1628 
1629  //The TCP/IP process is now running
1630  context->running = TRUE;
1631 
1632  //Loop through network interfaces
1633  for(i = 0; i < context->numInterfaces; i++)
1634  {
1635  //Point to the current network interface
1636  interface = &context->interfaces[i];
1637 
1638  //Check whether the interface is fully configured
1639  if(interface->configured)
1640  {
1641  //Interrupts can be safely enabled
1642  if(interface->nicDriver != NULL)
1643  {
1644  interface->nicDriver->enableIrq(interface);
1645  }
1646  }
1647  }
1648 
1649  //Release exclusive access
1650  netUnlock(context);
1651 
1652  //Main loop
1653  while(1)
1654  {
1655 #endif
1656  //Get current time
1657  time = osGetSystemTime();
1658 
1659  //Compute the maximum blocking time when waiting for an event
1660  if(timeCompare(time, context->timestamp) < 0)
1661  {
1662  timeout = context->timestamp - time;
1663  }
1664  else
1665  {
1666  timeout = 0;
1667  }
1668 
1669  //Receive notifications when a frame has been received, or the link state
1670  //of any network interfaces has changed
1671  status = osWaitForEvent(&context->event, timeout);
1672 
1673  //Check whether the specified event is in signaled state
1674  if(status)
1675  {
1676  //Get exclusive access
1677  netLock(context);
1678 
1679  //Process events
1680  for(i = 0; i < context->numInterfaces; i++)
1681  {
1682  //Point to the current network interface
1683  interface = &context->interfaces[i];
1684 
1685  //Check whether a NIC event is pending
1686  if(interface->nicEvent)
1687  {
1688  //Acknowledge the event by clearing the flag
1689  interface->nicEvent = FALSE;
1690 
1691  //Valid NIC driver?
1692  if(interface->nicDriver != NULL)
1693  {
1694  //Disable hardware interrupts
1695  interface->nicDriver->disableIrq(interface);
1696  //Handle NIC events
1697  interface->nicDriver->eventHandler(interface);
1698  //Re-enable hardware interrupts
1699  interface->nicDriver->enableIrq(interface);
1700  }
1701  }
1702 
1703 #if (ETH_SUPPORT == ENABLED)
1704  //Check whether a PHY event is pending
1705  if(interface->phyEvent)
1706  {
1707  //Acknowledge the event by clearing the flag
1708  interface->phyEvent = FALSE;
1709 
1710  //Valid NIC driver?
1711  if(interface->nicDriver != NULL)
1712  {
1713  //Disable hardware interrupts
1714  interface->nicDriver->disableIrq(interface);
1715 
1716  //Valid Ethernet PHY or switch driver?
1717  if(interface->phyDriver != NULL)
1718  {
1719  //Handle events
1720  interface->phyDriver->eventHandler(interface);
1721  }
1722  else if(interface->switchDriver != NULL)
1723  {
1724  //Handle events
1725  interface->switchDriver->eventHandler(interface);
1726  }
1727  else
1728  {
1729  //The interface is not properly configured
1730  }
1731 
1732  //Re-enable hardware interrupts
1733  interface->nicDriver->enableIrq(interface);
1734  }
1735  }
1736 #endif
1737  }
1738 
1739  //Release exclusive access
1740  netUnlock(context);
1741  }
1742 
1743  //Stop request?
1744  if(context->stop)
1745  {
1746  //Stop TCP/IP stack
1747  context->running = FALSE;
1748  //Task epilogue
1749  osExitTask();
1750  //Kill ourselves
1752  }
1753 
1754  //Get current time
1755  time = osGetSystemTime();
1756 
1757  //Check current time
1758  if(timeCompare(time, context->timestamp) >= 0)
1759  {
1760  //Get exclusive access
1761  netLock(context);
1762  //Handle periodic operations
1763  netTick(context);
1764  //Release exclusive access
1765  netUnlock(context);
1766 
1767  //Next event
1768  context->timestamp = time + NET_TICK_INTERVAL;
1769  }
1770 #if (NET_RTOS_SUPPORT == ENABLED)
1771  }
1772 #endif
1773 }
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:601
IPv6 (Internet Protocol Version 6)
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
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
String manipulation helper functions.
int bool_t
Definition: compiler_port.h:63
@ ERROR_OUT_OF_RANGE
Definition: error.h:138
void netGetRandData(NetContext *context, uint8_t *data, size_t length)
Get a string of random data.
Definition: net.c:504
@ NIC_LINK_SPEED_UNKNOWN
Definition: nic.h:110
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
Set SPI driver.
Definition: net.c:1062
error_t netInit(NetContext *context, const NetSettings *settings)
Initialize TCP/IP stack.
Definition: net.c:103
error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
Set Ethernet MAC driver.
Definition: net.c:883
#define osExitTask()
Eui64
Definition: ethernet.h:212
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:953
error_t netSetUartDriver(NetInterface *interface, const UartDriver *driver)
Set UART driver.
Definition: net.c:1087
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
uint32_t netGetRandRange(NetContext *context, uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net.c:473
WebSocket API (client and server)
error_t socketInit(NetContext *context)
Socket related initialization.
Definition: socket.c:86
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint32_t netGenerateRand(NetContext *context)
Generate a random 32-bit value.
Definition: net_misc.c:956
UART driver.
Definition: nic.h:385
uint8_t data[]
Definition: ethernet.h:224
#define OS_INVALID_TASK_ID
External interrupt line driver.
Definition: nic.h:398
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
SMI driver.
Definition: nic.h:357
IPv6 routing.
IGMP snooping switch.
char_t name[]
error_t arpInit(NetInterface *interface)
ARP cache initialization.
Definition: arp.c:58
error_t netSetSwitchDriver(NetInterface *interface, const SwitchDriver *driver)
Set Ethernet switch driver.
Definition: net.c:972
error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
Set MAC address.
Definition: net.c:570
#define NET_TICK_INTERVAL
Definition: net.h:190
SPI driver.
Definition: nic.h:369
#define osStrlen(s)
Definition: os_port.h:168
error_t netSetParentInterface(NetInterface *interface, NetInterface *physicalInterface)
Attach a virtual interface to a given physical interface.
Definition: net.c:851
error_t ipv6Init(NetInterface *interface)
IPv6 related initialization.
Definition: ipv6.c:95
Helper functions for DHCPv6 client.
#define VLAN_VID_MASK
Definition: ethernet.h:124
#define NET_TASK_STACK_SIZE
Definition: net.h:178
#define OS_SELF_TASK_ID
#define timeCompare(t1, t2)
Definition: os_port.h:40
Helper functions for DHCP client.
uint_t netGetLinkSpeed(NetInterface *interface)
Get link speed.
Definition: net.c:1200
Helper functions for DHCP server.
Ethernet.
error_t webSocketInit(void)
WebSocket related initialization.
Definition: web_socket.c:60
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:673
Router advertisement service.
void netInitRand(NetContext *context)
Initialize random number generator.
Definition: net_misc.c:899
Definitions common to mDNS client and mDNS responder.
IGMP router.
void netTask(NetContext *context)
TCP/IP events handling.
Definition: net.c:1614
void osDeleteTask(OsTaskId taskId)
Delete a task.
#define FALSE
Definition: os_port.h:46
Helper functions for TCP.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:87
uint32_t netGetRand(NetContext *context)
Generate a random 32-bit value.
Definition: net.c:441
error_t
Error codes.
Definition: error.h:43
#define osSprintf(dest,...)
Definition: os_port.h:234
void(* OsTaskCode)(void *arg)
Task routine.
Definitions common to NBNS client and NBNS responder.
DNS-SD responder (DNS-Based Service Discovery)
error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
Specify Ethernet PHY address.
Definition: net.c:938
error_t mdnsInit(NetInterface *interface)
mDNS related initialization
Definition: mdns_common.c:69
NBNS client (NetBIOS Name Service)
error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
Set EUI-64 interface identifier.
Definition: net.c:642
error_t netConfigInterface(NetInterface *interface)
Configure network interface.
Definition: net.c:1289
error_t ipv4InitRouting(NetContext *context)
void osDeleteEvent(OsEvent *event)
Delete an event object.
#define NetInterface
Definition: net.h:40
error_t memPoolInit(void)
Memory pool initialization.
Definition: net_mem.c:70
void netGetDefaultSettings(NetSettings *settings)
Initialize settings with default values.
Definition: net.c:83
void netTick(NetContext *context)
Manage TCP/IP timers.
Definition: net_misc.c:418
@ ERROR_INVALID_LENGTH
Definition: error.h:111
error_t netSetSmiDriver(NetInterface *interface, const SmiDriver *driver)
Set SMI driver.
Definition: net.c:1032
error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Retrieve MAC address.
Definition: net.c:605
uint32_t netGenerateRandRange(NetContext *context, uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:983
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:527
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
Set Ethernet PHY driver.
Definition: net.c:908
mDNS client (Multicast DNS)
NetInterface * netGetDefaultInterface(NetContext *context)
Get default network interface.
Definition: net.c:540
error_t netSetLinkState(NetInterface *interface, bool_t linkState)
Set administrative link state.
Definition: net.c:1137
IGMP host.
uint8_t length
Definition: tcp.h:375
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net_misc.c:202
#define osEnterTask()
error_t igmpInit(NetInterface *interface)
IGMP initialization.
Definition: igmp_common.c:70
Helper functions for NAT.
MacAddr
Definition: ethernet.h:197
NDP (Neighbor Discovery Protocol)
error_t netSeedRand(NetContext *context, const uint8_t *seed, size_t length)
Seed the pseudo-random number generator.
Definition: net.c:393
error_t nicUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Definition: nic.c:379
error_t netEnablePromiscuousMode(NetInterface *interface, bool_t enable)
Enable promiscuous mode.
Definition: net.c:1263
DNS client (Domain Name System)
TCP/IP raw sockets.
uint32_t systime_t
System time.
uint16_t port
Definition: dns_common.h:270
MLD node (Multicast Listener Discovery for IPv6)
char char_t
Definition: compiler_port.h:55
DNS cache management.
uint32_t time
error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
Set interface identifier.
Definition: net.c:699
error_t ndpInit(NetInterface *interface)
Neighbor cache initialization.
Definition: ndp.c:64
uint_t numInterfaces
Number of network interfaces.
Definition: net.h:369
error_t ipv6InitRouting(NetContext *context)
Initialize IPv6 routing table.
Definition: ipv6_routing.c:56
Ethernet switch driver.
Definition: nic.h:325
#define NET_MAX_IF_NAME_LEN
Definition: net.h:157
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
error_t udpInit(NetContext *context)
UDP related initialization.
Definition: udp.c:61
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
error_t netSetVmanId(NetInterface *interface, uint16_t vmanId)
Specify VMAN identifier (802.1ad)
Definition: net.c:816
NicDuplexMode
Duplex mode.
Definition: nic.h:122
error_t tcpInit(NetContext *context)
TCP related initialization.
Definition: tcp.c:55
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void netGenerateRandData(NetContext *context, uint8_t *data, size_t length)
Get a string of random data.
Definition: net_misc.c:1011
uint8_t value[]
Definition: tcp.h:376
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
error_t netSetVlanId(NetInterface *interface, uint16_t vlanId)
Specify VLAN identifier (802.1Q)
Definition: net.c:782
IPv4 routing.
error_t netStopInterface(NetInterface *interface)
Stop network interface.
Definition: net.c:1557
error_t netSetHostname(NetInterface *interface, const char_t *name)
Set host name.
Definition: net.c:753
Socket API.
@ NIC_UNKNOWN_DUPLEX_MODE
Definition: nic.h:123
error_t ethInit(NetInterface *interface)
Ethernet related initialization.
Definition: ethernet.c:62
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:53
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
Helper functions for Auto-IP.
#define NET_RAND_SEED_SIZE
Definition: net.h:171
LLMNR responder (Link-Local Multicast Name Resolution)
error_t netGetEui64(NetInterface *interface, Eui64 *eui64)
Retrieve EUI-64 interface identifier.
Definition: net.c:667
NicDuplexMode netGetDuplexMode(NetInterface *interface)
Get duplex mode.
Definition: net.c:1231
IPv4 (Internet Protocol Version 4)
OsTaskParameters task
Task parameters.
Definition: net.h:367
error_t netSetSwitchPort(NetInterface *interface, uint8_t port)
Specify switch port.
Definition: net.c:1002
error_t mldInit(NetInterface *interface)
MLD initialization.
Definition: mld_common.c:69
#define TCP_INITIAL_RTO
Definition: tcp.h:117
TCP timer management.
TCP/IP stack settings.
Definition: net.h:366
unsigned int uint_t
Definition: compiler_port.h:57
error_t netStartInterface(NetInterface *interface)
Start network interface.
Definition: net.c:1475
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:51
error_t netSetExtIntDriver(NetInterface *interface, const ExtIntDriver *driver)
Set external interrupt line driver.
Definition: net.c:1112
NIC driver.
Definition: nic.h:286
error_t dnsInit(void)
DNS cache initialization.
Definition: dns_cache.c:57
bool_t netGetLinkState(NetInterface *interface)
Get link state.
Definition: net.c:1169
#define osStrcpy(s1, s2)
Definition: os_port.h:210
error_t llmnrResponderInit(NetInterface *interface)
LLMNR responder initialization.
ARP (Address Resolution Protocol)
#define NET_TASK_PRIORITY
Definition: net.h:185
error_t ipv4Init(NetInterface *interface)
IPv4 related initialization.
Definition: ipv4.c:79
NetInterface * interfaces
Network interfaces.
Definition: net.h:368
@ ERROR_ALREADY_RUNNING
Definition: error.h:294
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define NET_MAX_HOSTNAME_LEN
Definition: net.h:164
NBNS responder (NetBIOS Name Service)
error_t netStart(NetContext *context)
Start TCP/IP stack.
Definition: net.c:332
mDNS responder (Multicast DNS)
systime_t osGetSystemTime(void)
Retrieve system time.
error_t netSetInterfaceName(NetInterface *interface, const char_t *name)
Set interface name.
Definition: net.c:724