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-2019 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.6
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  * @return Error code
276  **/
277 
278 error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer,
279  size_t offset)
280 {
281  error_t error;
282  bool_t status;
283 
284 #if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
285  //Retrieve the length of the packet
286  size_t length = netBufferGetLength(buffer) - offset;
287 
288  //Debug message
289  TRACE_DEBUG("Sending packet (%" PRIuSIZE " bytes)...\r\n", length);
290  TRACE_DEBUG_NET_BUFFER(" ", buffer, offset, length);
291 #endif
292 
293  //Check whether the interface is enabled for operation
294  if(interface->configured && interface->nicDriver != NULL)
295  {
296  //Loopback interface?
297  if(interface->nicDriver->type == NIC_TYPE_LOOPBACK)
298  {
299  //The loopback interface is always available
300  status = TRUE;
301  }
302  else
303  {
304  //Wait for the transmitter to be ready to send
305  status = osWaitForEvent(&interface->nicTxEvent, NIC_MAX_BLOCKING_TIME);
306  }
307 
308  //Check whether the specified event is in signaled state
309  if(status)
310  {
311  //Disable interrupts
312  interface->nicDriver->disableIrq(interface);
313 
314  //Send Ethernet frame
315  error = interface->nicDriver->sendPacket(interface, buffer, offset);
316 
317  //Re-enable interrupts if necessary
318  if(interface->configured)
319  {
320  interface->nicDriver->enableIrq(interface);
321  }
322  }
323  else
324  {
325  //The transmitter is busy
326  error = ERROR_TRANSMITTER_BUSY;
327  }
328  }
329  else
330  {
331  //Report an error
332  error = ERROR_INVALID_INTERFACE;
333  }
334 
335  //Return status code
336  return error;
337 }
338 
339 
340 /**
341  * @brief Configure MAC address filtering
342  * @param[in] interface Underlying network interface
343  * @return Error code
344  **/
345 
347 {
348  error_t error;
349 
350  //Valid NIC driver?
351  if(interface->nicDriver != NULL)
352  {
353  //Disable interrupts
354  interface->nicDriver->disableIrq(interface);
355 
356  //Update MAC filter table
357  error = interface->nicDriver->updateMacAddrFilter(interface);
358 
359  //Re-enable interrupts if necessary
360  if(interface->configured)
361  {
362  interface->nicDriver->enableIrq(interface);
363  }
364  }
365  else
366  {
367  //Report an error
368  error = ERROR_INVALID_INTERFACE;
369  }
370 
371  //Return status code
372  return error;
373 }
374 
375 
376 /**
377  * @brief Handle a packet received by the network controller
378  * @param[in] interface Underlying network interface
379  * @param[in] packet Incoming packet to process
380  * @param[in] length Total packet length
381  **/
382 
383 void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length)
384 {
385  NicType type;
386 
387  //Check whether the interface is enabled for operation
388  if(interface->configured)
389  {
390  //Re-enable interrupts
391  interface->nicDriver->enableIrq(interface);
392 
393  //Debug message
394  TRACE_DEBUG("Packet received (%" PRIuSIZE " bytes)...\r\n", length);
395  TRACE_DEBUG_ARRAY(" ", packet, length);
396 
397  //Retrieve network interface type
398  type = interface->nicDriver->type;
399 
400 #if (ETH_SUPPORT == ENABLED)
401  //Ethernet interface?
402  if(type == NIC_TYPE_ETHERNET)
403  {
404  //Process incoming Ethernet frame
405  ethProcessFrame(interface, packet, length);
406  }
407  else
408 #endif
409 #if (PPP_SUPPORT == ENABLED)
410  //PPP interface?
411  if(type == NIC_TYPE_PPP)
412  {
413  //Process incoming PPP frame
414  pppProcessFrame(interface, packet, length);
415  }
416  else
417 #endif
418 #if (IPV6_SUPPORT == ENABLED)
419  //6LoWPAN interface?
420  if(type == NIC_TYPE_6LOWPAN)
421  {
422  NetBuffer1 buffer;
423 
424  //The incoming packet fits in a single chunk
425  buffer.chunkCount = 1;
426  buffer.maxChunkCount = 1;
427  buffer.chunk[0].address = packet;
428  buffer.chunk[0].length = (uint16_t) length;
429  buffer.chunk[0].size = 0;
430 
431  //Process incoming IPv6 packet
432  ipv6ProcessPacket(interface, (NetBuffer *) &buffer, 0);
433  }
434  else
435 #endif
436 #if (NET_LOOPBACK_IF_SUPPORT == ENABLED)
437  //Loopback interface?
438  if(type == NIC_TYPE_LOOPBACK)
439  {
440 #if (IPV4_SUPPORT == ENABLED)
441  //IPv4 packet received?
442  if(length >= sizeof(Ipv4Header) && (packet[0] >> 4) == 4)
443  {
444  error_t error;
445  uint_t i;
446  Ipv4Header *header;
447 
448  //Point to the IPv4 header
449  header = (Ipv4Header *) packet;
450 
451  //Loop through network interfaces
452  for(i = 0; i < NET_INTERFACE_COUNT; i++)
453  {
454  //Check destination address
455  error = ipv4CheckDestAddr(&netInterface[i], header->destAddr);
456 
457  //Valid destination address?
458  if(!error)
459  {
460  //Process incoming IPv4 packet
461  ipv4ProcessPacket(&netInterface[i], (Ipv4Header *) packet,
462  length);
463  }
464  }
465  }
466  else
467 #endif
468 #if (IPV6_SUPPORT == ENABLED)
469  //IPv6 packet received?
470  if(length >= sizeof(Ipv6Header) && (packet[0] >> 4) == 6)
471  {
472  error_t error;
473  uint_t i;
474  NetBuffer1 buffer;
475  Ipv6Header *header;
476 
477  //Point to the IPv6 header
478  header = (Ipv6Header *) packet;
479 
480  //Loop through network interfaces
481  for(i = 0; i < NET_INTERFACE_COUNT; i++)
482  {
483  //Check destination address
484  error = ipv6CheckDestAddr(&netInterface[i], &header->destAddr);
485 
486  //Valid destination address?
487  if(!error)
488  {
489  //The incoming packet fits in a single chunk
490  buffer.chunkCount = 1;
491  buffer.maxChunkCount = 1;
492  buffer.chunk[0].address = packet;
493  buffer.chunk[0].length = (uint16_t) length;
494  buffer.chunk[0].size = 0;
495 
496  //Process incoming IPv6 packet
497  ipv6ProcessPacket(&netInterface[i], (NetBuffer *) &buffer, 0);
498  }
499  }
500  }
501  else
502 #endif
503  {
504  //Invalid version number
505  }
506  }
507  else
508 #endif
509  //Unknown interface type?
510  {
511  //Silently discard the received packet
512  }
513 
514  //Disable interrupts
515  interface->nicDriver->disableIrq(interface);
516  }
517 }
518 
519 
520 /**
521  * @brief Process link state change notification
522  * @param[in] interface Underlying network interface
523  **/
524 
526 {
527  uint_t i;
528  NetInterface *physicalInterface;
529  NetInterface *virtualInterface;
530 
531  //Point to the physical interface
532  physicalInterface = nicGetPhysicalInterface(interface);
533 
534  //Re-enable interrupts if necessary
535  if(physicalInterface->configured)
536  {
537  physicalInterface->nicDriver->enableIrq(physicalInterface);
538  }
539 
540  //Loop through network interfaces
541  for(i = 0; i < NET_INTERFACE_COUNT; i++)
542  {
543  //Point to the current interface
544  virtualInterface = &netInterface[i];
545 
546  //Check whether the current virtual interface is attached to the physical
547  //interface
548  if(nicIsParentInterface(virtualInterface, interface) &&
549  nicGetSwitchPort(virtualInterface) == nicGetSwitchPort(interface))
550  {
551  //Set operation mode
552  virtualInterface->linkSpeed = interface->linkSpeed;
553  virtualInterface->duplexMode = interface->duplexMode;
554 
555  //Update link state
556  virtualInterface->linkState = interface->linkState;
557 
558  //Process link state change event
559  netProcessLinkChange(virtualInterface);
560  }
561  }
562 
563  //Disable interrupts
564  physicalInterface->nicDriver->disableIrq(physicalInterface);
565 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
IPv6 (Internet Protocol Version 6)
uint8_t length
Definition: dtls_misc.h:149
int bool_t
Definition: compiler_port.h:49
#define Ipv4Header
Definition: ipv4.h:36
void ipv6ProcessPacket(NetInterface *interface, NetBuffer *ipPacket, size_t ipPacketOffset)
Incoming IPv6 packet processing.
Definition: ipv6.c:887
uint_t chunkCount
Definition: net_mem.h:98
Invalid interface.
Definition: error.h:53
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length)
Handle a packet received by the network controller.
Definition: nic.c:383
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
#define TRUE
Definition: os_port.h:50
#define NIC_MAX_BLOCKING_TIME
Definition: nic.h:46
#define Ipv6Header
Definition: ipv6.h:36
error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet to the network controller.
Definition: nic.c:278
#define NET_INTERFACE_COUNT
Definition: net.h:109
systime_t nicTickCounter
Definition: nic.c:43
void ipv4ProcessPacket(NetInterface *interface, Ipv4Header *packet, size_t length)
Incoming IPv4 packet processing.
Definition: ipv4.c:565
ChunkDesc chunk[1]
Definition: net_mem.h:100
Ethernet.
#define TRACE_DEBUG_NET_BUFFER(p, b, o, n)
Definition: debug.h:108
Loopback interface.
Definition: nic.h:82
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 netInterface[NET_INTERFACE_COUNT]
Definition: net.c:79
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:84
char_t type
error_t
Error codes.
Definition: error.h:42
NicType
NIC types.
Definition: nic.h:76
void * address
Definition: net_mem.h:78
uint_t maxChunkCount
Definition: net_mem.h:99
#define NetInterface
Definition: net.h:36
error_t ipv6CheckDestAddr(NetInterface *interface, const Ipv6Addr *ipAddr)
Destination IPv6 address filtering.
Definition: ipv6_misc.c:777
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
error_t nicUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Definition: nic.c:346
PPP interface.
Definition: nic.h:80
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:107
void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length)
Process an incoming Ethernet frame.
Definition: ethernet.c:85
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:107
void pppProcessFrame(NetInterface *interface, uint8_t *frame, size_t length)
Process an incoming PPP frame.
Definition: ppp.c:889
IPv4 (Internet Protocol Version 4)
#define PRIuSIZE
Definition: compiler_port.h:78
6LoWPAN interface
Definition: nic.h:81
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
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net.c:1214
Debugging facilities.
Ethernet interface.
Definition: nic.h:79