nic.c
Go to the documentation of this file.
1 /**
2  * @file nic.c
3  * @brief Network interface controller abstraction layer
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2020 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 1.9.8
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/nic.h"
37 #include "core/ethernet.h"
38 #include "ipv4/ipv4.h"
39 #include "ipv6/ipv6.h"
40 #include "debug.h"
41 
42 //Tick counter to handle periodic operations
44 
45 
46 /**
47  * @brief Retrieve logical interface
48  * @param[in] interface Pointer to the network interface
49  * @return Pointer to the physical interface
50  **/
51 
53 {
54 #if (ETH_VLAN_SUPPORT == ENABLED)
55  uint_t i;
56 
57  //A virtual interface can inherit from multiple parent interfaces
58  for(i = 0; i < NET_INTERFACE_COUNT; i++)
59  {
60  //Check whether a valid MAC address has been assigned to the interface
61  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
62  break;
63 
64  //Last interface in the list?
65  if(interface->parent == NULL)
66  break;
67 
68  //Point to the interface on top of which the virtual interface runs
69  interface = interface->parent;
70  }
71 #endif
72 
73  //Return a pointer to the logical interface
74  return interface;
75 }
76 
77 
78 /**
79  * @brief Retrieve physical interface
80  * @param[in] interface Pointer to the network interface
81  * @return Pointer to the physical interface
82  **/
83 
85 {
86 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_VLAN_SUPPORT == ENABLED || \
87  ETH_PORT_TAGGING_SUPPORT == ENABLED)
88  uint_t i;
89 
90  //A virtual interface can inherit from multiple parent interfaces
91  for(i = 0; i < NET_INTERFACE_COUNT; i++)
92  {
93  //Physical interface?
94  if(interface->nicDriver != NULL || interface->parent == NULL)
95  break;
96 
97  //Point to the interface on top of which the virtual interface runs
98  interface = interface->parent;
99  }
100 #endif
101 
102  //Return a pointer to the physical interface
103  return interface;
104 }
105 
106 
107 /**
108  * @brief Retrieve switch port identifier
109  * @param[in] interface Pointer to the network interface
110  * @return Switch port identifier
111  **/
112 
113 uint8_t nicGetSwitchPort(NetInterface *interface)
114 {
115 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
116  uint_t i;
117 
118  //A virtual interface can inherit from multiple parent interfaces
119  for(i = 0; i < NET_INTERFACE_COUNT; i++)
120  {
121  //Valid switch port identifier?
122  if(interface->port != 0 || interface->parent == NULL)
123  break;
124 
125  //Point to the interface on top of which the virtual interface runs
126  interface = interface->parent;
127  }
128 
129  //Return switch port identifier
130  return interface->port;
131 #else
132  //Ethernet port multiplication (VLAN or tail tagging) is not supported
133  return 0;
134 #endif
135 }
136 
137 
138 /**
139  * @brief Retrieve VLAN identifier
140  * @param[in] interface Pointer to the network interface
141  * @return VLAN identifier
142  **/
143 
144 uint16_t nicGetVlanId(NetInterface *interface)
145 {
146 #if (ETH_VLAN_SUPPORT == ENABLED)
147  uint_t i;
148 
149  //A virtual interface can inherit from multiple parent interfaces
150  for(i = 0; i < NET_INTERFACE_COUNT; i++)
151  {
152  //Valid VLAN identifier?
153  if(interface->vlanId != 0 || interface->parent == NULL)
154  break;
155 
156  //Point to the interface on top of which the virtual interface runs
157  interface = interface->parent;
158  }
159 
160  //Return VLAN identifier
161  return interface->vlanId;
162 #else
163  //VLAN is not supported
164  return 0;
165 #endif
166 }
167 
168 
169 /**
170  * @brief Retrieve VMAN identifier
171  * @param[in] interface Pointer to the network interface
172  * @return VMAN identifier
173  **/
174 
175 uint16_t nicGetVmanId(NetInterface *interface)
176 {
177 #if (ETH_VMAN_SUPPORT == ENABLED)
178  uint_t i;
179 
180  //A virtual interface can inherit from multiple parent interfaces
181  for(i = 0; i < NET_INTERFACE_COUNT; i++)
182  {
183  //Valid VMAN identifier?
184  if(interface->vmanId != 0 || interface->parent == NULL)
185  break;
186 
187  //Point to the interface on top of which the virtual interface runs
188  interface = interface->parent;
189  }
190 
191  //Return VMAN identifier
192  return interface->vmanId;
193 #else
194  //VMAN is not supported
195  return 0;
196 #endif
197 }
198 
199 
200 /**
201  * @brief Test parent/child relationship between 2 interfaces
202  * @param[in] interface Pointer to the child interface
203  * @param[in] parent Pointer to the parent interface
204  * @return TRUE is an existing parent/child relationship is found, else FALSE
205  **/
206 
208 {
209 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_VLAN_SUPPORT == ENABLED || \
210  ETH_PORT_TAGGING_SUPPORT == ENABLED)
211  uint_t i;
212  bool_t flag;
213 
214  //Iterate through the parent interfaces
215  for(flag = FALSE, i = 0; i < NET_INTERFACE_COUNT; i++)
216  {
217  //Any parent/child relationship?
218  if(interface == parent)
219  {
220  flag = TRUE;
221  break;
222  }
223 
224  //Last interface in the list?
225  if(interface->parent == NULL)
226  break;
227 
228  //Point to the interface on top of which the virtual interface runs
229  interface = interface->parent;
230  }
231 
232  //Return TRUE is an existing parent/child relationship is found
233  return flag;
234 #else
235  //Virtual interfaces are not supported
236  return (interface == parent) ? TRUE : FALSE;
237 #endif
238 }
239 
240 
241 /**
242  * @brief Network controller timer handler
243  *
244  * This routine is periodically called by the TCP/IP stack to
245  * handle periodic operations such as polling the link state
246  *
247  * @param[in] interface Underlying network interface
248  **/
249 
250 void nicTick(NetInterface *interface)
251 {
252  //Valid NIC driver?
253  if(interface->nicDriver != NULL)
254  {
255  //Disable interrupts
256  interface->nicDriver->disableIrq(interface);
257 
258  //Handle periodic operations
259  interface->nicDriver->tick(interface);
260 
261  //Re-enable interrupts if necessary
262  if(interface->configured)
263  {
264  interface->nicDriver->enableIrq(interface);
265  }
266  }
267 }
268 
269 
270 /**
271  * @brief Send a packet to the network controller
272  * @param[in] interface Underlying network interface
273  * @param[in] buffer Multi-part buffer containing the data to send
274  * @param[in] offset Offset to the first data byte
275  * @param[in] ancillary Additional options passed to the stack along with
276  * the packet
277  * @return Error code
278  **/
279 
280 error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer,
281  size_t offset, NetTxAncillary *ancillary)
282 {
283  error_t error;
284  bool_t status;
285 
286 #if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
287  //Retrieve the length of the packet
288  size_t length = netBufferGetLength(buffer) - offset;
289 
290  //Debug message
291  TRACE_DEBUG("Sending packet (%" PRIuSIZE " bytes)...\r\n", length);
292  TRACE_DEBUG_NET_BUFFER(" ", buffer, offset, length);
293 #endif
294 
295  //Check whether the interface is enabled for operation
296  if(interface->configured && interface->nicDriver != NULL)
297  {
298  //Loopback interface?
299  if(interface->nicDriver->type == NIC_TYPE_LOOPBACK)
300  {
301  //The loopback interface is always available
302  status = TRUE;
303  }
304  else
305  {
306  //Wait for the transmitter to be ready to send
307  status = osWaitForEvent(&interface->nicTxEvent, NIC_MAX_BLOCKING_TIME);
308  }
309 
310  //Check whether the specified event is in signaled state
311  if(status)
312  {
313  //Disable interrupts
314  interface->nicDriver->disableIrq(interface);
315 
316  //Send the packet
317  error = interface->nicDriver->sendPacket(interface, buffer, offset,
318  ancillary);
319 
320  //Re-enable interrupts if necessary
321  if(interface->configured)
322  {
323  interface->nicDriver->enableIrq(interface);
324  }
325  }
326  else
327  {
328  //If the transmitter is busy, then drop the packet
329  error = NO_ERROR;
330  }
331  }
332  else
333  {
334  //Report an error
335  error = ERROR_INVALID_INTERFACE;
336  }
337 
338  //Return status code
339  return error;
340 }
341 
342 
343 /**
344  * @brief Configure MAC address filtering
345  * @param[in] interface Underlying network interface
346  * @return Error code
347  **/
348 
350 {
351  error_t error;
352 
353  //Valid NIC driver?
354  if(interface->nicDriver != NULL)
355  {
356  //Disable interrupts
357  interface->nicDriver->disableIrq(interface);
358 
359  //Update MAC filter table
360  error = interface->nicDriver->updateMacAddrFilter(interface);
361 
362  //Re-enable interrupts if necessary
363  if(interface->configured)
364  {
365  interface->nicDriver->enableIrq(interface);
366  }
367  }
368  else
369  {
370  //Report an error
371  error = ERROR_INVALID_INTERFACE;
372  }
373 
374  //Return status code
375  return error;
376 }
377 
378 
379 /**
380  * @brief Handle a packet received by the network controller
381  * @param[in] interface Underlying network interface
382  * @param[in] packet Incoming packet to process
383  * @param[in] length Total packet length
384  * @param[in] ancillary Additional options passed to the stack along with
385  * the packet
386  **/
387 
388 void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length,
389  NetRxAncillary *ancillary)
390 {
391  NicType type;
392 
393  //Check whether the interface is enabled for operation
394  if(interface->configured)
395  {
396  //Re-enable interrupts
397  interface->nicDriver->enableIrq(interface);
398 
399  //Debug message
400  TRACE_DEBUG("Packet received (%" PRIuSIZE " bytes)...\r\n", length);
401  TRACE_DEBUG_ARRAY(" ", packet, length);
402 
403  //Retrieve network interface type
404  type = interface->nicDriver->type;
405 
406 #if (ETH_SUPPORT == ENABLED)
407  //Ethernet interface?
408  if(type == NIC_TYPE_ETHERNET)
409  {
410  //Process incoming Ethernet frame
411  ethProcessFrame(interface, packet, length, ancillary);
412  }
413  else
414 #endif
415 #if (PPP_SUPPORT == ENABLED)
416  //PPP interface?
417  if(type == NIC_TYPE_PPP)
418  {
419  //Process incoming PPP frame
420  pppProcessFrame(interface, packet, length, ancillary);
421  }
422  else
423 #endif
424 #if (IPV6_SUPPORT == ENABLED)
425  //6LoWPAN interface?
426  if(type == NIC_TYPE_6LOWPAN)
427  {
428  NetBuffer1 buffer;
429 
430  //The incoming packet fits in a single chunk
431  buffer.chunkCount = 1;
432  buffer.maxChunkCount = 1;
433  buffer.chunk[0].address = packet;
434  buffer.chunk[0].length = (uint16_t) length;
435  buffer.chunk[0].size = 0;
436 
437  //Process incoming IPv6 packet
438  ipv6ProcessPacket(interface, (NetBuffer *) &buffer, 0, ancillary);
439  }
440  else
441 #endif
442 #if (NET_LOOPBACK_IF_SUPPORT == ENABLED)
443  //Loopback interface?
444  if(type == NIC_TYPE_LOOPBACK)
445  {
446 #if (IPV4_SUPPORT == ENABLED)
447  //IPv4 packet received?
448  if(length >= sizeof(Ipv4Header) && (packet[0] >> 4) == 4)
449  {
450  error_t error;
451  uint_t i;
452  Ipv4Header *header;
453 
454  //Point to the IPv4 header
455  header = (Ipv4Header *) packet;
456 
457  //Loop through network interfaces
458  for(i = 0; i < NET_INTERFACE_COUNT; i++)
459  {
460  //Check destination address
461  error = ipv4CheckDestAddr(&netInterface[i], header->destAddr);
462 
463  //Valid destination address?
464  if(!error)
465  {
466  //Process incoming IPv4 packet
467  ipv4ProcessPacket(&netInterface[i], (Ipv4Header *) packet,
468  length, ancillary);
469  }
470  }
471  }
472  else
473 #endif
474 #if (IPV6_SUPPORT == ENABLED)
475  //IPv6 packet received?
476  if(length >= sizeof(Ipv6Header) && (packet[0] >> 4) == 6)
477  {
478  error_t error;
479  uint_t i;
480  NetBuffer1 buffer;
481  Ipv6Header *header;
482 
483  //Point to the IPv6 header
484  header = (Ipv6Header *) packet;
485 
486  //Loop through network interfaces
487  for(i = 0; i < NET_INTERFACE_COUNT; i++)
488  {
489  //Check destination address
490  error = ipv6CheckDestAddr(&netInterface[i], &header->destAddr);
491 
492  //Valid destination address?
493  if(!error)
494  {
495  //The incoming packet fits in a single chunk
496  buffer.chunkCount = 1;
497  buffer.maxChunkCount = 1;
498  buffer.chunk[0].address = packet;
499  buffer.chunk[0].length = (uint16_t) length;
500  buffer.chunk[0].size = 0;
501 
502  //Process incoming IPv6 packet
503  ipv6ProcessPacket(&netInterface[i], (NetBuffer *) &buffer, 0,
504  ancillary);
505  }
506  }
507  }
508  else
509 #endif
510  {
511  //Invalid version number
512  }
513  }
514  else
515 #endif
516  //Unknown interface type?
517  {
518  //Silently discard the received packet
519  }
520 
521  //Disable interrupts
522  interface->nicDriver->disableIrq(interface);
523  }
524 }
525 
526 
527 /**
528  * @brief Process link state change notification
529  * @param[in] interface Underlying network interface
530  **/
531 
533 {
534  uint_t i;
535  NetInterface *physicalInterface;
536  NetInterface *virtualInterface;
537 
538  //Point to the physical interface
539  physicalInterface = nicGetPhysicalInterface(interface);
540 
541  //Re-enable interrupts if necessary
542  if(physicalInterface->configured)
543  {
544  physicalInterface->nicDriver->enableIrq(physicalInterface);
545  }
546 
547  //Loop through network interfaces
548  for(i = 0; i < NET_INTERFACE_COUNT; i++)
549  {
550  //Point to the current interface
551  virtualInterface = &netInterface[i];
552 
553  //Check whether the current virtual interface is attached to the physical
554  //interface
555  if(nicIsParentInterface(virtualInterface, interface) &&
556  nicGetSwitchPort(virtualInterface) == nicGetSwitchPort(interface))
557  {
558  //Set operation mode
559  virtualInterface->linkSpeed = interface->linkSpeed;
560  virtualInterface->duplexMode = interface->duplexMode;
561 
562  //Update link state
563  virtualInterface->linkState = interface->linkState;
564 
565  //Process link state change event
566  netProcessLinkChange(virtualInterface);
567  }
568  }
569 
570  //Disable interrupts
571  physicalInterface->nicDriver->disableIrq(physicalInterface);
572 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:532
uint8_t length
Definition: coap_common.h:190
IPv6 (Internet Protocol Version 6)
int bool_t
Definition: compiler_port.h:49
#define Ipv4Header
Definition: ipv4.h:36
uint_t chunkCount
Definition: net_mem.h:98
Invalid interface.
Definition: error.h:53
error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet to the network controller.
Definition: nic.c:280
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
#define TRUE
Definition: os_port.h:50
void ipv4ProcessPacket(NetInterface *interface, Ipv4Header *packet, size_t length, NetRxAncillary *ancillary)
Incoming IPv4 packet processing.
Definition: ipv4.c:567
#define NIC_MAX_BLOCKING_TIME
Definition: nic.h:46
#define Ipv6Header
Definition: ipv6.h:36
#define NET_INTERFACE_COUNT
Definition: net.h:110
systime_t nicTickCounter
Definition: nic.c:43
void ipv6ProcessPacket(NetInterface *interface, NetBuffer *ipPacket, size_t ipPacketOffset, NetRxAncillary *ancillary)
Incoming IPv6 packet processing.
Definition: ipv6.c:889
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:388
ChunkDesc chunk[1]
Definition: net_mem.h:100
Ethernet.
#define TRACE_DEBUG_NET_BUFFER(p, b, o, n)
Definition: debug.h:109
Loopback interface.
Definition: nic.h:85
error_t ipv4CheckDestAddr(NetInterface *interface, Ipv4Addr ipAddr)
Destination IPv4 address filtering.
Definition: ipv4_misc.c:79
uint16_t length
Definition: net_mem.h:79
#define FALSE
Definition: os_port.h:46
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:84
void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length, NetRxAncillary *ancillary)
Process an incoming Ethernet frame.
Definition: ethernet.c:87
char_t type
error_t
Error codes.
Definition: error.h:42
#define netInterface
Definition: net_legacy.h:273
NicType
NIC types.
Definition: nic.h:79
void * address
Definition: net_mem.h:78
uint_t maxChunkCount
Definition: net_mem.h:99
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
error_t ipv6CheckDestAddr(NetInterface *interface, const Ipv6Addr *ipAddr)
Destination IPv6 address filtering.
Definition: ipv6_misc.c:777
#define NetTxAncillary
Definition: net_misc.h:36
void pppProcessFrame(NetInterface *interface, uint8_t *frame, size_t length, NetRxAncillary *ancillary)
Process an incoming PPP frame.
Definition: ppp.c:891
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net_misc.c:191
error_t nicUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Definition: nic.c:349
PPP interface.
Definition: nic.h:83
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:108
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
Network interface controller abstraction layer.
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:121
IPv4 (Internet Protocol Version 4)
#define PRIuSIZE
Definition: compiler_port.h:78
6LoWPAN interface
Definition: nic.h:84
unsigned int uint_t
Definition: compiler_port.h:45
void nicTick(NetInterface *interface)
Network controller timer handler.
Definition: nic.c:250
TCP/IP stack core.
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:52
uint16_t nicGetVmanId(NetInterface *interface)
Retrieve VMAN identifier.
Definition: nic.c:175
uint16_t size
Definition: net_mem.h:80
uint16_t nicGetVlanId(NetInterface *interface)
Retrieve VLAN identifier.
Definition: nic.c:144
uint32_t systime_t
Definition: compiler_port.h:46
uint8_t nicGetSwitchPort(NetInterface *interface)
Retrieve switch port identifier.
Definition: nic.c:113
bool_t nicIsParentInterface(NetInterface *interface, NetInterface *parent)
Test parent/child relationship between 2 interfaces.
Definition: nic.c:207
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:56
Success.
Definition: error.h:44
Debugging facilities.
Ethernet interface.
Definition: nic.h:82