• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (c) 2022 Huawei Device Co., Ltd.
4  *
5  * Description: Constructs and functions are provided
6  * to handle encapsulation and parsing of the NewIP
7  * network layer protocol header.
8  *
9  * Author: Yang Yanjun <yangyanjun@huawei.com>
10  *
11  * Data: 2022-07-18
12  */
13 #ifndef _NEWIP_HDR_H
14 #define _NEWIP_HDR_H
15 
16 #include "nip_addr.h"
17 
18 /* Ethernet head 14B, +2B byte alignment, +66 to avoid
19  * HMAC driver SKB space expansion caused by Coredum problem
20  */
21 /* This parameter is used only to apply for the length of the packet buffer,
22  * but not to determine the actual packet header length
23  */
24 #define NIP_ETH_HDR_BASE_LEN 14
25 #define NIP_ETH_HDR_LEN (NIP_ETH_HDR_BASE_LEN + 2 + 66)
26 
27 /* bitmap1 + bitmap2 + TTL + total len + nexthd + daddr + saddr
28  * 1B        1B        1B    2B          1B       9B      9B    = 24B
29  * V4  TCP 1448
30  * NIP TCP 1430 + 30 = 1460
31  */
32 /* This interface is only used to define the buffer length.
33  * To calculate the packet header length, use the "get_nip_hdr_len" func
34  */
35 #define NIP_HDR_MAX 24
36 #define NIP_UDP_HDR_LEN 8
37 #define NIP_MIN_MTU (NIP_HDR_MAX + NIP_UDP_HDR_LEN)
38 #define NIP_BYTE_ALIGNMENT 2
39 
40 #define NIP_BITMAP_HAVE_MORE_BIT     0x01
41 
42 /* Bitmap 1st Byte: bit0 - bit7 */
43 #define NIP_BITMAP_INVALID_SET       0x80                      /* Bit 0 is set */
44 #define NIP_BITMAP_INCLUDE_TTL       0x40                      /* Bit 1 is set */
45 #define NIP_BITMAP_INCLUDE_TOTAL_LEN 0x20                      /* Bit 2 is set */
46 #define NIP_BITMAP_INCLUDE_NEXT_HDR  0x10                      /* Bit 3 is set */
47 #define NIP_BITMAP_INCLUDE_RES1      0x08                      /* Bit 4 is set */
48 #define NIP_BITMAP_INCLUDE_DADDR     0x04                      /* Bit 5 is set */
49 #define NIP_BITMAP_INCLUDE_SADDR     0x02                      /* Bit 6 is set */
50 #define NIP_BITMAP_HAVE_BYTE_2       NIP_BITMAP_HAVE_MORE_BIT  /* Bit 7 is set */
51 
52 /* Bitmap 2nd Byte: bit0 - bit7 */
53 #define NIP_BITMAP_INCLUDE_HDR_LEN   0x80                      /* Bit 0 is set */
54 #define NIP_BITMAP_INCLUDE_RES2      0x40                      /* Bit 1 is set */
55 #define NIP_BITMAP_INCLUDE_RES3      0x20                      /* Bit 2 is set */
56 #define NIP_BITMAP_INCLUDE_RES4      0x10                      /* Bit 3 is set */
57 #define NIP_BITMAP_INCLUDE_RES5      0x08                      /* Bit 4 is set */
58 #define NIP_BITMAP_INCLUDE_RES6      0x04                      /* Bit 5 is set */
59 #define NIP_BITMAP_INCLUDE_RES7      0x02                      /* Bit 6 is set */
60 #define NIP_BITMAP_HAVE_BYTE_3       NIP_BITMAP_HAVE_MORE_BIT  /* Bit 7 is set */
61 
62 /* Bitmap 1st Byte:
63  * | valid | ttl | total_len | next_hdr | res1 | daddr | saddr | have byte2 |
64  * |   0   |  1  |     0     |     1    |  0   |   1   |   1   |     0      |
65  */
66 #define NIP_UDP_BITMAP_1          0x56
67 #define NIP_UDP_BITMAP_1_INC_2    0x57
68 
69 /* Bitmap 1st Byte:
70  * | valid | ttl | total_len | next_hdr | res1 | daddr | saddr | have byte2 |
71  * |   0   |  1  |     1     |     1    |  0   |   1   |   1   |     0      |
72  */
73 #define NIP_NORMAL_BITMAP_1        0x76
74 #define NIP_NORMAL_BITMAP_1_INC_2  0x77
75 
76 /* Bitmap 2nd Byte:
77  * | hdr_len | res2 | res2 | res2 | res2 | res2 | res2 | have byte3 |
78  * |  0 or 1 |  0   |  0   |  0   |  0   |  0   |  0   |      0     |
79  */
80 #define NIP_NODATA_BITMAP_2        0x00
81 #define NIP_NORMAL_BITMAP_2        0x80
82 
83 /* invalid Bitmap 2nd Byte:
84  * | hdr_len | res2 | res2 | res2 | res2 | res2 | res2 | have byte3 |
85  * |  0 or 1 |  1   |  1   |  1   |  1   |  1   |  1   |      1     |
86  */
87 #define NIP_INVALID_BITMAP_2       0x7F
88 
89 #define NIP_DEFAULT_TTL 128
90 #define NIP_ARP_DEFAULT_TTL 64
91 #define IPPROTO_NIP_ICMP 0xB1
92 
93 enum NIP_HDR_TYPE {
94 	NIP_HDR_UDP = 0,
95 	NIP_HDR_COMM = 1,
96 
97 	NIP_HDR_TYPE_MAX,
98 };
99 
100 enum NIP_HDR_DECAP_ERR {
101 	NIP_HDR_BITMAP_INVALID = 1,
102 	NIP_HDR_BITMAP_NUM_OUT_RANGE = 2,
103 	NIP_HDR_NO_TTL = 3,
104 	NIP_HDR_NO_NEXT_HDR = 4,
105 	NIP_HDR_NO_DADDR = 5,
106 	NIP_HDR_DECAP_DADDR_ERR = 6,
107 	NIP_HDR_DADDR_INVALID = 7,
108 	NIP_HDR_DECAP_SADDR_ERR = 8,
109 	NIP_HDR_SADDR_INVALID = 9,
110 	NIP_HDR_RCV_BUF_READ_OUT_RANGE = 10,
111 	NIP_HDR_UNKNOWN_AND_NO_HDR_LEN = 11,
112 	NIP_HDR_LEN_INVALID = 12,
113 	NIP_HDR_LEN_OUT_RANGE = 13,
114 
115 	NIP_HDR_DECAP_ERRCODE_MAX,
116 };
117 
118 /* The newIP header contains variable-length fields.
119  * The header structure is defined only for function parameter transmission.
120  * The fields are parsed in the original packet and saved
121  */
122 struct nip_hdr_decap {
123 	struct nip_addr saddr; /* Source address, network order.(big end) */
124 	struct nip_addr daddr; /* Destination address, network order.(big end) */
125 
126 	unsigned char ttl;          /* Hop count limit */
127 	unsigned char nexthdr;      /* Upper-layer Protocol Type: IPPROTO_UDP */
128 	unsigned char hdr_len;      /* Indicates the length of the packet header */
129 	unsigned char hdr_real_len; /* Indicates the actual length of the packet header */
130 
131 	unsigned short total_len;   /* Packet length (Header + packet), network order.(big end) */
132 	unsigned short no_hdr_len : 1;  /* The header does not contain a header length field */
133 	unsigned short include_unknown_bit : 1; /* There is no other bitmap field */
134 	unsigned short include_saddr : 1;
135 	unsigned short include_daddr : 1;
136 	unsigned short include_ttl : 1;
137 	unsigned short include_nexthdr : 1;
138 	unsigned short include_hdr_len : 1;
139 	unsigned short include_total_len : 1;
140 	unsigned short res : 8;
141 
142 	unsigned int rcv_buf_len;
143 };
144 
145 /* The newIP packet header function is an incoming or outgoing parameter,
146  * which is not the content encapsulated in the packet
147  */
148 #define BITMAP_MAX 8
149 #define RES_NUM 2
150 struct nip_hdr_encap {
151 	struct nip_addr daddr; /* Destination address, network order.(big end) */
152 	struct nip_addr saddr; /* Source address, network order.(big end) */
153 
154 	unsigned char ttl;     /* Hop count limit */
155 	unsigned char nexthdr; /* Upper-layer Protocol Type: IPPROTO_UDP */
156 	unsigned short total_len; /* Packet header length + packet data length */
157 
158 	void *usr_data;             /* User data pointer */
159 	unsigned int usr_data_len;  /* Length of data sent by the user */
160 	unsigned int trans_hdr_len; /* Transport layer header length */
161 
162 	unsigned short sport;
163 	unsigned short dport;
164 
165 	/* The following are the output parameters */
166 	unsigned char bitmap[BITMAP_MAX]; /* Bitmap currently supports a maximum of 8 bytes */
167 	unsigned int bitmap_num;          /* Number of valid elements in the bitmap array */
168 
169 	unsigned char *hdr_buf;        /* Cache the newIP header */
170 	unsigned int hdr_buf_pos;      /* Buf Buffer writable address offset */
171 	unsigned short *frag_id_pos;   /* Fragment Offset in the original packet */
172 	unsigned char *hdr_len_pos;    /* Indicates the actual length of the packet header */
173 	unsigned short *total_len_pos; /* Total length position of the packet */
174 
175 	/* Whether the bitmap of the packet header carries a flag */
176 	unsigned char encap_ttl : 1;
177 	unsigned char encap_hdr_len : 1;
178 	unsigned char encap_daddr : 1;
179 	unsigned char encap_saddr : 1;
180 	unsigned char encap_total_len : 1;
181 	unsigned char encap_res : 3;
182 };
183 
184 /* Packet segment information */
185 struct nip_pkt_seg_info {
186 	unsigned int mid_pkt_num;      /* Number of intermediate segments */
187 	unsigned int last_pkt_num;     /* Number of last segments */
188 
189 	unsigned int mid_usr_pkt_len;  /* Middle segment data length (8B aligned) */
190 	unsigned int last_usr_pkt_len; /* Length of the last data segment */
191 
192 	unsigned char *usr_data;       /* Holds a pointer to the user's raw data */
193 	unsigned int usr_data_len;     /* Length of user data read this time */
194 };
195 
196 void nip_calc_pkt_frag_num(unsigned int mtu,
197 			   unsigned int nip_hdr_len,
198 			   unsigned int usr_data_len,
199 			   struct nip_pkt_seg_info *seg_info);
200 
201 void nip_hdr_udp_encap(struct nip_hdr_encap *head);
202 
203 /* need update total len after this func, call nip_update_total_len */
204 void nip_hdr_comm_encap(struct nip_hdr_encap *head);
205 
206 /* input must be network order. */
207 void nip_update_total_len(struct nip_hdr_encap *head, unsigned short total_len);
208 
209 /* Note: a function call requires its own byte order conversion.(niph->total_len) */
210 int nip_hdr_parse(unsigned char *rcv_buf, unsigned int buf_len, struct nip_hdr_decap *niph);
211 
212 /* The length of the packet header is obtained according to the packet type,
213  * source ADDRESS, and destination address.
214  * If the packet does not carry the source address or destination address, fill in the blank
215  */
216 int get_nip_hdr_len(enum NIP_HDR_TYPE hdr_type,
217 		    const struct nip_addr *saddr,
218 		    const struct nip_addr *daddr);
219 
220 struct udp_hdr {
221 	unsigned short	sport;
222 	unsigned short	dport;
223 	unsigned short	len;
224 	unsigned short	checksum;
225 };
226 
227 /* input must be network order. */
nip_build_udp_hdr(unsigned short sport,unsigned short dport,unsigned short len,unsigned char * buf,unsigned short checksum)228 static inline void nip_build_udp_hdr(unsigned short sport, unsigned short dport,
229 				     unsigned short len, unsigned char *buf,
230 				     unsigned short checksum)
231 {
232 	struct udp_hdr *uh;
233 
234 	uh = (struct udp_hdr *)buf;
235 	uh->sport = sport;
236 	uh->dport = dport;
237 	uh->len = len;
238 	uh->checksum = checksum;
239 }
240 
241 #endif /* _NEWIP_HDR_H */
242 
243