• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef TLS_COMMON_H
17 #define TLS_COMMON_H
18 
19 #include "wm_osal.h"
20 #include "wm_config.h"
21 #include "wm_regs.h"
22 
23 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
24 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
25 
26 #define time_after(a, b)  ((long)(b) - (long)(a) < 0)
27 #define time_before(a, b)    time_after(b, a)
28 
29 /*
30  *    These are the defined Ethernet Protocol ID's.
31  */
32 
33 #define ETH_P_LOOP    0x0060        /* Ethernet Loopback packet    */
34 #define ETH_P_PUP    0x0200        /* Xerox PUP packet        */
35 #define ETH_P_PUPAT    0x0201        /* Xerox PUP Addr Trans packet    */
36 #define ETH_P_IP    0x0800        /* Internet Protocol packet    */
37 #define ETH_P_X25    0x0805        /* CCITT X.25            */
38 #define ETH_P_ARP    0x0806        /* Address Resolution packet    */
39 #define    ETH_P_BPQ    0x08FF        /* G8BPQ AX.25 Ethernet Packet    [ NOT AN OFFICIALLY REGISTERED ID ] */
40 #define ETH_P_IEEEPUP    0x0a00        /* Xerox IEEE802.3 PUP packet */
41 #define ETH_P_IEEEPUPAT    0x0a01        /* Xerox IEEE802.3 PUP Addr Trans packet */
42 #define ETH_P_DEC       0x6000          /* DEC Assigned proto           */
43 #define ETH_P_DNA_DL    0x6001          /* DEC DNA Dump/Load            */
44 #define ETH_P_DNA_RC    0x6002          /* DEC DNA Remote Console       */
45 #define ETH_P_DNA_RT    0x6003          /* DEC DNA Routing              */
46 #define ETH_P_LAT       0x6004          /* DEC LAT                      */
47 #define ETH_P_DIAG      0x6005          /* DEC Diagnostics              */
48 #define ETH_P_CUST      0x6006          /* DEC Customer use             */
49 #define ETH_P_SCA       0x6007          /* DEC Systems Comms Arch       */
50 #define ETH_P_TEB    0x6558        /* Trans Ether Bridging        */
51 #define ETH_P_RARP      0x8035        /* Reverse Addr Res packet    */
52 #define ETH_P_ATALK    0x809B        /* Appletalk DDP        */
53 #define ETH_P_AARP    0x80F3        /* Appletalk AARP        */
54 #define ETH_P_8021Q    0x8100          /* 802.1Q VLAN Extended Header  */
55 #define ETH_P_IPX    0x8137        /* IPX over DIX            */
56 #define ETH_P_IPV6    0x86DD        /* IPv6 over bluebook        */
57 #define ETH_P_PAUSE    0x8808        /* IEEE Pause frames. See 802.3 31B */
58 #define ETH_P_SLOW    0x8809        /* Slow Protocol. See 802.3ad 43B */
59 #define ETH_P_WCCP    0x883E        /* Web-cache coordination protocol
60                      * defined in draft-wilson-wrec-wccp-v2-00.txt */
61 #define ETH_P_PPP_DISC    0x8863        /* PPPoE discovery messages     */
62 #define ETH_P_PPP_SES    0x8864        /* PPPoE session messages    */
63 #define ETH_P_MPLS_UC    0x8847        /* MPLS Unicast traffic        */
64 #define ETH_P_MPLS_MC    0x8848        /* MPLS Multicast traffic    */
65 #define ETH_P_ATMMPOA    0x884c        /* MultiProtocol Over ATM    */
66 #define ETH_P_ATMFATE    0x8884        /* Frame-based ATM Transport
67                      * over Ethernet
68                      */
69 #define ETH_P_PAE    0x888E        /* Port Access Entity (IEEE 802.1X) */
70 #define ETH_P_WAPI    0x88B4        /* WAPI authentication */
71 #define ETH_P_AOE    0x88A2        /* ATA over Ethernet        */
72 #define ETH_P_TIPC    0x88CA        /* TIPC             */
73 #define ETH_P_FCOE    0x8906        /* Fibre Channel over Ethernet  */
74 #define ETH_P_EDSA    0xDADA        /* Ethertype DSA [ NOT AN OFFICIALLY REGISTERED ID ]  */
75 
76 #define ETH_HLEN    14        /* Total octets in header.     */
77 
78 #ifndef ETH_ALEN
79 #define ETH_ALEN 6
80 #endif
81 #ifndef IFNAMSIZ
82 #define IFNAMSIZ 16
83 #endif
84 #ifndef ETH_P_ALL
85 #define ETH_P_ALL 0x0003
86 #endif
87 #ifndef ETH_P_80211_ENCAP
88 #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
89 #endif
90 #ifndef ETH_P_PAE
91 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
92 #endif /* ETH_P_PAE */
93 #ifndef ETH_P_EAPOL
94 #define ETH_P_EAPOL ETH_P_PAE
95 #endif /* ETH_P_EAPOL */
96 #ifndef ETH_P_RSN_PREAUTH
97 #define ETH_P_RSN_PREAUTH 0x88c7
98 #endif /* ETH_P_RSN_PREAUTH */
99 #ifndef ETH_P_RRB
100 #define ETH_P_RRB 0x890D
101 #endif /* ETH_P_RRB */
102 
103 #define PRINTF_FORMAT(a, b) __attribute__ ((format (printf, (a), (b))))
104 #define STRUCT_PACKED __attribute__ ((packed))
105 
106 #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
107 
108 /**
109  * compare_ether_addr - Compare two Ethernet addresses
110  * @addr1: Pointer to a six-byte array containing the Ethernet address
111  * @addr2: Pointer other six-byte array containing the Ethernet address
112  *
113  * Compare two ethernet addresses, returns 0 if equal
114  */
compare_ether_addr(const u8 * addr1,const u8 * addr2)115 static __inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
116 {
117     return !((addr1[0] == addr2[0]) && (addr1[1] == addr2[1]) && (addr1[2] == addr2[2]) &&   \
118         (addr1[3] == addr2[3]) && (addr1[4] == addr2[4]) && (addr1[5] == addr2[5]));
119 }
120 
is_zero_ether_addr(const u8 * a)121 static __inline int is_zero_ether_addr(const u8 *a)
122 {
123     return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
124 }
125 
is_broadcast_ether_addr(const u8 * a)126 static __inline int is_broadcast_ether_addr(const u8 *a)
127 {
128     return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
129 }
130 
is_multicast_ether_addr(const u8 * addr)131 static __inline int is_multicast_ether_addr(const u8 *addr)
132 {
133     return (0x01 & addr[0]);
134 }
135 
swap_16(unsigned short v)136 static __inline unsigned short swap_16(unsigned short v)
137 {
138     return ((v & 0xff) << 8) | (v >> 8);
139 }
140 
swap_32(unsigned int v)141 static __inline unsigned int swap_32(unsigned int v)
142 {
143     return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
144         ((v & 0xff0000) >> 8) | (v >> 24);
145 }
146 
147 #define le_to_host16(n) (n)
148 #define host_to_le16(n) (n)
149 #define be_to_host16(n) swap_16(n)
150 #define host_to_be16(n) swap_16(n)
151 #define le_to_host32(n) (n)
152 #define le_to_host64(n) (n)
153 #define be_to_host32(n) swap_32(n)
154 #define host_to_be32(n) swap_32(n)
155 
get_unaligned_le16(const u8 * p)156 static __inline u16 get_unaligned_le16(const u8 *p)
157 {
158     return p[0] | (p[1] << 8);
159 }
160 
get_unaligned_le32(const u8 * p)161 static __inline u32 get_unaligned_le32(const u8 *p)
162 {
163     return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
164 }
165 
put_unaligned_le16(u16 val,u8 * p)166 static __inline void put_unaligned_le16(u16 val, u8 *p)
167 {
168     *p++ = val;
169     *p++ = val >> 8;
170 }
171 
put_unaligned_le32(u32 val,u8 * p)172 static __inline void put_unaligned_le32(u32 val, u8 *p)
173 {
174     put_unaligned_le16(val >> 16, p + 2);
175     put_unaligned_le16(val, p);
176 }
177 
get_unaligned_be16(const u8 * p)178 static __inline u16 get_unaligned_be16(const u8 *p)
179 {
180     return be_to_host16(get_unaligned_le16(p));
181 }
182 
get_unaligned_be32(const u8 * p)183 static __inline u32 get_unaligned_be32(const u8 *p)
184 {
185     return be_to_host32(get_unaligned_le32(p));
186 }
187 
put_unaligned_be16(u16 val,u8 * p)188 static __inline void put_unaligned_be16(u16 val, u8 *p)
189 {
190     *p++ = val >> 8;
191     *p++ = val;
192 }
193 
put_unaligned_be32(u32 val,u8 * p)194 static __inline void put_unaligned_be32(u32 val, u8 *p)
195 {
196     put_unaligned_be16(val >> 16, p);
197     put_unaligned_be16(val, p + 2);
198 }
199 
200 #ifdef __CHECKER__
201 #define __force __attribute__((force))
202 #define __bitwise __attribute__((bitwise))
203 #else
204 #define __force
205 #define __bitwise
206 #endif
207 
208 typedef u16 __bitwise be16;
209 typedef u16 __bitwise le16;
210 typedef u32 __bitwise be32;
211 typedef u32 __bitwise le32;
212 typedef u64 __bitwise be64;
213 typedef u64 __bitwise le64;
214 
215 /*
216  *    This is an Ethernet frame header.
217  */
218 struct ethhdr {
219     unsigned char    h_dest[ETH_ALEN];    /* destination eth addr    */
220     unsigned char    h_source[ETH_ALEN];    /* source ether addr    */
221     unsigned short  h_proto;        /* packet type ID field    */
222 } __attribute__((packed));
223 
224 typedef struct {
225     volatile int counter;
226 } atomic_t;
227 
228 struct kref {
229     atomic_t refcount;
230 };
231 
atomic_sub_return(int i,atomic_t * v)232 static __inline int atomic_sub_return(int i, atomic_t *v)
233 {
234     u32  cpu_sr;
235     int val;
236 
237     cpu_sr = tls_os_set_critical();
238     val = v->counter;
239     v->counter = val -= i;
240     tls_os_release_critical(cpu_sr);
241 
242     return val;
243 }
244 
atomic_add_return(int i,atomic_t * v)245 static __inline int atomic_add_return(int i, atomic_t *v)
246 {
247     u32  cpu_sr;
248     int val;
249 
250     cpu_sr = tls_os_set_critical();
251     val = v->counter;
252     v->counter = val += i;
253     tls_os_release_critical(cpu_sr);
254 
255     return val;
256 }
257 
258 #define atomic_set(v, i)                    (((v)->counter) = (i))
259 #define atomic_dec_and_test(v)            (atomic_sub_return(1, v) == 0)
260 #define atomic_inc(v)                    (void) atomic_add_return(1, v)
261 #define atomic_dec(v)                    (void) atomic_sub_return(1, v)
262 
263 #endif /* end of TLS_COMMON_H */
264