1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Link Layer                                                       */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 
20 
21 /**************************************************************************/
22 /*                                                                        */
23 /*  COMPONENT DEFINITION                                   RELEASE        */
24 /*                                                                        */
25 /*    nx_link.h                                           PORTABLE C      */
26 /*                                                           6.4.0        */
27 /*  AUTHOR                                                                */
28 /*                                                                        */
29 /*    Tiejun Zhou, Microsoft Corporation                                  */
30 /*                                                                        */
31 /*  DESCRIPTION                                                           */
32 /*                                                                        */
33 /*    This file defines the NetX link layer component.                    */
34 /*                                                                        */
35 /*    Note: Require driver support to use APIs from this file.            */
36 /*          A quick check in driver is to search for                      */
37 /*          NX_LINK_RAW_PACKET_SEND. APIs are not supported if not found. */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  12-31-2023     Tiejun Zhou               Initial Version 6.4.0        */
44 /*                                                                        */
45 /**************************************************************************/
46 
47 
48 #ifndef _NX_LINK_H_
49 #define _NX_LINK_H_
50 
51 /* Determine if a C++ compiler is being used.  If so, ensure that standard
52    C is used to process the API information.  */
53 #ifdef __cplusplus
54 
55 /* Yes, C++ compiler is present.  Use standard C.  */
56 extern   "C" {
57 
58 #endif
59 
60 #include "nx_api.h"
61 
62 /* Define link layer constants.  */
63 
64 /* All packet types for receive filter.  */
65 #ifndef NX_LINK_PACKET_TYPE_ALL
66 #define NX_LINK_PACKET_TYPE_ALL      0xFFFF
67 #endif /* NX_LINK_PACKET_TYPE_ALL */
68 
69 #ifndef NX_LINK_ETHERNET_HEADER_SIZE
70 #define NX_LINK_ETHERNET_HEADER_SIZE 14
71 #endif /* NX_LINK_ETHERNET_HEADER_SIZE */
72 
73 #ifndef NX_LINK_VLAN_HEADER_SIZE
74 #define NX_LINK_VLAN_HEADER_SIZE     4
75 #endif /* NX_LINK_VLAN_HEADER_SIZE */
76 
77 #define NX_LINK_ETHERNET_TPID        (0x8100)
78 #define NX_LINK_ETHERNET_IP          (0x0800)
79 #define NX_LINK_ETHERNET_ARP         (0x0806)
80 #define NX_LINK_ETHERNET_RARP        (0x8035)
81 #define NX_LINK_ETHERNET_IPV6        (0x86DD)
82 #define NX_LINK_ETHERNET_PTP         (0x88F7)
83 
84 #define NX_LINK_ETHERNET_MVRP        (0x88f5)
85 #define NX_LINK_ETHERNET_MMRP        (0x88f6)
86 #define NX_LINK_ETHERNET_MSRP        (0x22ea)
87 #define NX_LINK_ETHERNET_OPCUA       (0xb62c)
88 
89 #define NX_LINK_VLAN_ID_MASK         (0x0FFF)
90 #define NX_LINK_VLAN_PCP_MASK        (0xE000)
91 #define NX_LINK_VLAN_PCP_SHIFT       (13)
92 
93 struct NX_LINK_TIME_STRUCT;
94 
95 /* Define function pointer for incoming packet received notify.  */
96 typedef UINT nx_link_packet_receive_callback(NX_IP *ip_ptr, UINT interface_index, NX_PACKET *packet_ptr,
97                                              ULONG physical_address_msw, ULONG physical_address_lsw,
98                                              UINT packet_type, UINT header_size, VOID *context,
99                                              struct NX_LINK_TIME_STRUCT *time_ptr);
100 
101 /* Define structures.  */
102 /* Link layer time structure.  */
103 typedef struct NX_LINK_TIME_STRUCT
104 {
105     ULONG second_high;
106     ULONG second_low;
107     ULONG nano_second;
108 } NX_LINK_TIME;
109 
110 /* Receive queue structure for processing raw packets.  */
111 typedef struct NX_LINK_RECEIVE_QUEUE_STRUCT
112 {
113     nx_link_packet_receive_callback     *callback;
114     VOID                                *context;
115     USHORT                               packet_type;
116     USHORT                               reserved;
117     struct NX_LINK_RECEIVE_QUEUE_STRUCT *next_ptr;
118     struct NX_LINK_RECEIVE_QUEUE_STRUCT *previous_ptr;
119 } NX_LINK_RECEIVE_QUEUE;
120 
121 /* Define APIs for application.  */
122 UINT nx_link_vlan_set(NX_IP *ip_ptr, UINT interface_index, UINT vlan_tag);
123 UINT nx_link_vlan_get(NX_IP *ip_ptr, UINT interface_index, USHORT *vlan_tag);
124 UINT nx_link_vlan_clear(NX_IP *ip_ptr, UINT interface_index);
125 UINT nx_link_multicast_join(NX_IP *ip_ptr, UINT interface_index,
126                             ULONG physical_address_msw, ULONG physical_address_lsw);
127 UINT nx_link_multicast_leave(NX_IP *ip_ptr, UINT interface_index,
128                              ULONG physical_address_msw, ULONG physical_address_lsw);
129 UINT nx_link_ethernet_packet_send(NX_IP *ip_ptr, UINT interface_index, NX_PACKET *packet_ptr,
130                                   ULONG physical_address_msw, ULONG physical_address_lsw, UINT packet_type);
131 UINT nx_link_raw_packet_send(NX_IP *ip_ptr, UINT interface_index, NX_PACKET *packet_ptr);
132 UINT nx_link_packet_receive_callback_add(NX_IP *ip_ptr, UINT interface_index, NX_LINK_RECEIVE_QUEUE *queue_ptr,
133                                          UINT packet_type, nx_link_packet_receive_callback *callback_ptr, VOID *context);
134 UINT nx_link_packet_receive_callback_remove(NX_IP *ip_ptr, UINT interface_index, NX_LINK_RECEIVE_QUEUE *queue_ptr);
135 UINT nx_link_ethernet_header_parse(NX_PACKET *packet_ptr, ULONG *destination_msb, ULONG *destination_lsb,
136                                    ULONG *source_msb, ULONG *source_lsb, USHORT *ether_type, USHORT *vlan_tag,
137                                    UCHAR *vlan_tag_valid, UINT *header_size);
138 UINT nx_link_vlan_interface_create(NX_IP *ip_ptr, CHAR *interface_name, ULONG ip_address, ULONG network_mask,
139                                    UINT vlan_tag, UINT parent_interface_index, UINT *interface_index_ptr);
140 
141 
142 /* APIs for network driver.  */
143 UINT nx_link_ethernet_header_add(NX_IP *ip_ptr, UINT interface_index, NX_PACKET *packet_ptr,
144                                  ULONG physical_address_msw, ULONG physical_address_lsw, UINT packet_type);
145 VOID nx_link_packet_transmitted(NX_IP *ip_ptr, UINT interface_index, NX_PACKET *packet_ptr, NX_LINK_TIME *time_ptr);
146 VOID nx_link_ethernet_packet_received(NX_IP *ip_ptr, UINT interface_index, NX_PACKET *packet_ptr,
147                                       NX_LINK_TIME *time_ptr);
148 UINT nx_link_driver_request_preprocess(NX_IP_DRIVER *driver_request, NX_INTERFACE **actual_interface);
149 
150 /* Internal functions.  */
151 void nx_link_vlan_interface_status_change(NX_IP *ip_ptr, UINT interface_index);
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 #endif /* _NX_LINK_H_ */
157 
158