1e41f4b71Sopenharmony_ci# New IP Kernel Protocol Stack 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Basic Concepts 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciOn basis of the traditional IP, New IP employs lightweight packet headers and variable-length, multi-semantic addresses and integrates Layer 2 and Layer 3 protocols to simplify protocols, reduce redundant bytes, and improve the energy efficiency ratio (EER), net throughput, and communication efficiency. New IP strives to implement end-to-end interconnection between heterogeneous networks to support ultimate experience of Super Device via efficient communication between devices. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThe Wi-Fi protocol packets cause low transmission efficiency due to high overheads in Layer 3 packet headers and addressing. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci``` 13e41f4b71Sopenharmony_ciAn IPv4 address has a fixed length of 4 bytes, and an IPv6 address has a fixed length of 16 bytes. 14e41f4b71Sopenharmony_ciThe network layer header ranges from 20 to 60 bytes for an IPv4 packet and is 40 bytes for an IPv6 packet. 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciNew IP supports variable-length multi-semantic addresses (min. 1 byte) and customized header encapsulation (min. 5 bytes). Simplified packet headers reduce overheads and improve transmission efficiency. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciNew IP provides 25.9% fewer packet header overheads than IPv4 and 44.9% less than IPv6. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciNew IP provides at least 1% higher payload transmission efficiency than IPv4 and 2.33% higher than IPv6. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| Scenario | Header Overhead (Bytes) | Payload Transmission Efficiency<br>(Wi-Fi MTU = 1500 Bytes, BT MTU = 255 Bytes)| 24e41f4b71Sopenharmony_ci| --------------- | ------------ | ------------------------------------------- | 25e41f4b71Sopenharmony_ci| IPv4 for Wi-Fi | 30 + 8 + 20 = 58 | (1500 - 58)/1500 = 96.13% | 26e41f4b71Sopenharmony_ci| IPv6 for Wi-Fi | 30 + 8 + 40 = 78 | (1500 - 78)/1500 = 94.8% | 27e41f4b71Sopenharmony_ci| New IP for Wi-Fi | 30 + 8 + 5 = 43 | (1500 - 43)/1500 = 97.13% | 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## Variable-Length Header Format 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciThe following figure shows a New IP Wi-Fi packet header. "EtherType = 0xEADD" in the LLC header identifies the New IP packet. A bitmap is a binary sequence. The value of each binary bit indicates whether a field is carried in the New IP header. That is, the New IP header can be customized based on service requirements. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci- **Dispatch** indicates the encapsulation type. The value **0b0** indicates the New IP encapsulation child class, which is 1 bit long (**0b** indicates that the following values are binary). 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- **Bitmap** is of variable length. By default, it is seven bits following the **Dispatch** valid bit. The length of **Bitmap** can be extended contiguously. If the last bit of **Bitmap** is **0**, it indicates the end of **Bitmap**. If the last bit is **1**, it means one more byte until the last bit **0**. 38e41f4b71Sopenharmony_ci- **Value** indicates the field value. The length is an integer multiple of 1 byte. The value type and length are determined by the semantic table of the header field. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciThe **Bitmap** field is defined as follows: 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| Bitmap Identifier | Bitops | Length of the Field Carried (Byte) | Setting | Remarks | 43e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ---------------- | -------------- | --------------------------------------- | 44e41f4b71Sopenharmony_ci| Bitmap first byte | - | - | - | The eight bits are from the most significant bit to the least significant bit. | 45e41f4b71Sopenharmony_ci| Dispatch | 0 | - | Set to **0**. | **0**: indicates a New IP packet; **1**: indicates a non-New-IP packet. | 46e41f4b71Sopenharmony_ci| Whether the packet header carries the TTL | 1 | 1 | Set to **1**. | Indicates the number of remaining hops. | 47e41f4b71Sopenharmony_ci| Whether the packet header carries the total length | 2 | 2 | Set to **0** for UDP and **1** for TCP.| Total length of the New IP packet (including the header) | 48e41f4b71Sopenharmony_ci| Whether the packet header carries the Next Header | 3 | 1 | Set to **1**. | Protocol type. | 49e41f4b71Sopenharmony_ci| Reserve | 4 | Reserved | Set to **0**. | Reserved. | 50e41f4b71Sopenharmony_ci| Whether the packet header carries the destination address | 5 | Variable length (1 to 8 bytes)| Set to **1**. | Destination address. | 51e41f4b71Sopenharmony_ci| Whether the packet header carries the source address| 6 | Variable length (1 to 8 bytes)| Determined by the protocol.| Source address. | 52e41f4b71Sopenharmony_ci| Flag bit, indicating whether there is the second byte | 7 | - | - | **0**: indicates the end of the bitmap. **1**: indicates another 8-bit bitmap.| 53e41f4b71Sopenharmony_ci| Bitmap second byte | - | - | - | The eight bits are from the most significant bit to the least significant bit. | 54e41f4b71Sopenharmony_ci| Whether the packet header carries the header Length | 0 | 1 | - | New IP header length. | 55e41f4b71Sopenharmony_ci| Reserve | 1 | Reserved | Set to **0**. | - | 56e41f4b71Sopenharmony_ci| Reserve | 2 | Reserved | Set to **0**. | - | 57e41f4b71Sopenharmony_ci| Reserve | 3 | Reserved | Set to **0**. | - | 58e41f4b71Sopenharmony_ci| Reserve | 4 | Reserved | Set to **0**. | - | 59e41f4b71Sopenharmony_ci| Reserve | 5 | Reserved | Set to **0**. | - | 60e41f4b71Sopenharmony_ci| Reserve | 6 | Reserved | Set to **0**. | - | 61e41f4b71Sopenharmony_ci| Flag bit, indicating whether there is the third byte | 7 | - | - | **0**: indicates the end of the bitmap. **1**: indicates another 8-bit bitmap.| 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciThe New IP header is parsed as follows: 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciOnly the bitmap fields defined in New IP are parsed. All the bitmap fields with unknown semantics are skipped. The start position of the packet is located for parsing based on the header length. If the packet header contains bitmap fields with unknown semantics and does not contain the header length, the packet will be discarded. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci## Variable-Length Address Format 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ciDifferent from IPv4 and IPv6, which use fixed-length addresses, New IP supports variable-length addresses and parse of the address length. The packet header may not carry the address length field. The encoding format of New IP addresses is as follows: 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci| First Byte | Semantics | Valid Range of Address | 72e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 73e41f4b71Sopenharmony_ci| 0x00 | Address is 0 | [1 byte] 0 to 220 (0x00 to 0xDC) | 74e41f4b71Sopenharmony_ci| 0x01 | Address is 1 | - | 75e41f4b71Sopenharmony_ci| 0x02 | Address is 2 | - | 76e41f4b71Sopenharmony_ci| ... | ... | - | 77e41f4b71Sopenharmony_ci| 0xDC | Address is 220 | - | 78e41f4b71Sopenharmony_ci| 0xDD | An 16-bit address, which is 0 + 256 * (0xDD - 0xDD) + the last byte value | [2 bytes] 221 to 255 (0x**DD**DD to 0x**DD**FF) | 79e41f4b71Sopenharmony_ci| 0xDE | An 16-bit address, which is 0 + 256 * (0xDE - 0xDD) + the last byte value | [2 bytes] 256 to 511 (0x**DE**00 to 0x**DE**FF) | 80e41f4b71Sopenharmony_ci| 0xDF | An 16-bit address, which is 0 + 256 * (0xDF - 0xDD) + the last byte value | [2 bytes] 512 to 767 (0x**DF**00 to 0x**DF**FF) | 81e41f4b71Sopenharmony_ci| ... | ... | - | 82e41f4b71Sopenharmony_ci| 0xF0 | An 16-bit address, which is 0 + 256 * (0xF0 - 0xDD) + the last byte value | [2 bytes] 4864 to 5119 (0x**F0**00 to 0x**F0**FF) | 83e41f4b71Sopenharmony_ci| 0xF1 | An 16-bit address is followed | [3 bytes] 5120 to 65535 (0x**F1** 1400 to 0x**F1** FFFF) | 84e41f4b71Sopenharmony_ci| 0xF2 | An 32-bit address is followed | [5 bytes] 65536 to 4,294,967,295 (0x**F2** 0001 0000 to 0x**F2** FFFF FFFF)| 85e41f4b71Sopenharmony_ci| 0xF3 | An 48-bit address is followed | [7 bytes] 4,294,967,296 to 281,474,976,710,655 (0x**F3** 0001 0000 0000 to 0x**F3** FFFF FFFF FFFF)| 86e41f4b71Sopenharmony_ci| 0xFE | An 56-bit address is followed | [8 bytes] 0 to 72,057,594,037,927,935 (0x**FE**00 0000 0000 0000 to 0x**FE**FF FFFF FFFF FFFF)| 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci## New IP Configuration 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci### Enabling New IP 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciOnly the Linux 5.10 kernel of the RK3568 development board supports the New IP kernel protocol stack. To enable New IP, search for "NEWIP" in the kernel module configuration file of the RK3568 development board and set related parameters as follows: 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci``` 97e41f4b71Sopenharmony_ci# kernel/linux/config/linux-5.10/arch/arm64/configs/rk3568_standard_defconfig 98e41f4b71Sopenharmony_ciCONFIG_NEWIP=y // Enable the New IP kernel protocol stack. 99e41f4b71Sopenharmony_ciCONFIG_NEWIP_HOOKS=y // Enable New IP stub functions to be dynamically registered non-disruptively. This feature must be enabled when New IP is enabled. 100e41f4b71Sopenharmony_ciHCK_VENDOR_HOOKS=y // Enable the basic kernel instrumentation framework. New IP depends on this framework. It is enabled by default on the RK3568 development board. 101e41f4b71Sopenharmony_ci``` 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ciRun the following command to check whether the New IP protocol stack is successfully enabled: 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci``` 106e41f4b71Sopenharmony_cifind out/ -name *nip*.o 107e41f4b71Sopenharmony_ci... 108e41f4b71Sopenharmony_ciout/kernel/OBJ/linux-5.10/net/newip/nip_addrconf_core.o 109e41f4b71Sopenharmony_ciout/kernel/OBJ/linux-5.10/net/newip/nip_hdr_decap.o 110e41f4b71Sopenharmony_ciout/kernel/OBJ/linux-5.10/net/newip/nip_addr.o 111e41f4b71Sopenharmony_ciout/kernel/OBJ/linux-5.10/net/newip/nip_checksum.o 112e41f4b71Sopenharmony_ciout/kernel/OBJ/linux-5.10/net/newip/tcp_nip_output.o 113e41f4b71Sopenharmony_ci... 114e41f4b71Sopenharmony_ci``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci> **NOTE** 117e41f4b71Sopenharmony_ci> 118e41f4b71Sopenharmony_ci> All native kernel code must be non-disruptively modified into stub functions. For example, when New IP is added to the common process of IPv4/IPv6 stacks, you need to break into New IP stubs instead of calling New IP functions directly. After New IP is enabled, register the New IP functions with the function pointers during module initialization. Then, the New IP functions can be called in the common IPv4/IPv6 process through the function pointers. 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci```c 121e41f4b71Sopenharmony_ci/* Register the New IP ehash function with the kernel. */ 122e41f4b71Sopenharmony_ci/* Call the newip hook function in sk_ehashfn function (net\ipv4\inet_hashtables.c): 123e41f4b71Sopenharmony_ci */ 124e41f4b71Sopenharmony_civoid nip_ninet_ehashfn(const struct sock *sk, u32 *ret) 125e41f4b71Sopenharmony_ci{ 126e41f4b71Sopenharmony_ci *ret = ninet_ehashfn(sock_net(sk), &sk->SK_NIP_RCV_SADDR, 127e41f4b71Sopenharmony_ci sk->sk_num, &sk->SK_NIP_DADDR, sk->sk_dport); 128e41f4b71Sopenharmony_ci} 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_civoid nip_ninet_ehashfn_lhck_register(void) 131e41f4b71Sopenharmony_ci{ 132e41f4b71Sopenharmony_ci REGISTER_HCK_LITE_HOOK(nip_ninet_ehashfn_lhck, nip_ninet_ehashfn); 133e41f4b71Sopenharmony_ci} 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci/* Add the New IP stack processing to the general entry function of IPv4/IPv6 stacks. */ 136e41f4b71Sopenharmony_cistatic u32 sk_ehashfn(const struct sock *sk) 137e41f4b71Sopenharmony_ci{ 138e41f4b71Sopenharmony_ci /* IPv6 */ 139e41f4b71Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 140e41f4b71Sopenharmony_ci if (sk->sk_family == AF_INET6 && 141e41f4b71Sopenharmony_ci !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 142e41f4b71Sopenharmony_ci return inet6_ehashfn(sock_net(sk), 143e41f4b71Sopenharmony_ci &sk->sk_v6_rcv_saddr, sk->sk_num, 144e41f4b71Sopenharmony_ci &sk->sk_v6_daddr, sk->sk_dport); 145e41f4b71Sopenharmony_ci#endif 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci if (sk->sk_family == AF_NINET) { 148e41f4b71Sopenharmony_ci u32 ret = 0; 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci /* Register the New IP ehash function. */ 151e41f4b71Sopenharmony_ci CALL_HCK_LITE_HOOK(nip_ninet_ehashfn_lhck, sk, &ret); 152e41f4b71Sopenharmony_ci return ret; 153e41f4b71Sopenharmony_ci } 154e41f4b71Sopenharmony_ci /* IPv4 */ 155e41f4b71Sopenharmony_ci return inet_ehashfn(sock_net(sk), 156e41f4b71Sopenharmony_ci sk->sk_rcv_saddr, sk->sk_num, 157e41f4b71Sopenharmony_ci sk->sk_daddr, sk->sk_dport); 158e41f4b71Sopenharmony_ci} 159e41f4b71Sopenharmony_ci``` 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci### Disabling New IP 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ciTo disable New IP, search for "NEWIP" in the kernel module configuration file of the RK3568 development board and delete or comment out "CONFIG_NEWIP=y" and "CONFIG_NEWIP_HOOKS=y". 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci``` 166e41f4b71Sopenharmony_ci# kernel/linux/config/linux-5.10/arch/arm64/configs/rk3568_standard_defconfig 167e41f4b71Sopenharmony_ci# CONFIG_NEWIP is not set 168e41f4b71Sopenharmony_ci# CONFIG_NEWIP_HOOKS is not set 169e41f4b71Sopenharmony_ci``` 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci## New IP APIs 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ciThe user-mode application calls **socket()** to create a New IP socket and uses the New IP frame header encapsulation to send and receive packets. The following table lists the socket APIs for New IP. 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci| API | Input | Output | Return Value | Description | 176e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---------------------------------------------- | ---------------- | ------------------------------------------------------------ | 177e41f4b71Sopenharmony_ci| socket | int **domain**, int type, int **protocol** | NA | Socket handle **sockfd**.| Creates a New IP socket. <br>**domain** must be **AF_NINET**, which indicates a New IP socket.<br>**protocol** can be **IPPROTO_TCP** or **IPPROTO_UDP**.<br>This API returns the handle of the **socket** instance created. | 178e41f4b71Sopenharmony_ci| bind | int sockfd, const **struct sockaddr_nin** *myaddr, socklen_t addrlen | NA | Error code, which is an integer. | Binds the **socket** instance to the specified IP address and port.<br>**myaddr->sin_family** must be **AF_NINET**. | 179e41f4b71Sopenharmony_ci| listen | int socket, int backlog | NA | Error code, which is an integer. | Listens for the New IP address and port from the server. | 180e41f4b71Sopenharmony_ci| connect | int sockfd, const **struct sockaddr_nin** *addr, aocklen_t addrlen | NA | Error code, which is an integer. | Sets up a connection between the client and the server. | 181e41f4b71Sopenharmony_ci| accept | int sockfd, **struct sockaddr_nin** *address, socklen_t *address_len | NA | **sockfd**. | Accepts the connection request from the client. | 182e41f4b71Sopenharmony_ci| send | int sockfd, const void *msg, int len, unsigned int flags, const **struct sockaddr_nin** *dst_addr, int addrlen | NA | Error code, which is an integer. | Sends New IP packets via the socket using TCP. | 183e41f4b71Sopenharmony_ci| recv | int sockfd, size_t len, int flags, **struct sockaddr_nin** *src_addr, | void **buf, int* *fromlen | Error code, which is an integer. | Receives New IP packets via the socket using TCP. | 184e41f4b71Sopenharmony_ci| close | int sockfd | NA | Error code, which is an integer. | Closes the socket to release resources. | 185e41f4b71Sopenharmony_ci| ioctl | int sockfd, unsigned long cmd, ... | NA | Error code, which is an integer. | Queries or modifies information about the New IP protocol stack. | 186e41f4b71Sopenharmony_ci| sendto | int sockfd, const void *msg, int len, unsigned int flags, const **struct sockaddr** *dst_addr, int addrlen | NA | Error code, which is an integer. | Sends New IP packets via the socket using UDP. | 187e41f4b71Sopenharmony_ci| recvfrom | int sockfd, size_t len, int flags, | void *buf, struct sockaddr *from, int *fromlen | Error code, which is an integer. | Receives New IP packets via the socket using UDP. | 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciThe structure of the New IP short address is as follows: 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci```c 192e41f4b71Sopenharmony_cienum nip_8bit_addr_index { 193e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_0 = 0, 194e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_1 = 1, 195e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_2 = 2, 196e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_3 = 3, 197e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_4 = 4, 198e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_5 = 5, 199e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_6 = 6, 200e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_7 = 7, 201e41f4b71Sopenharmony_ci NIP_8BIT_ADDR_INDEX_MAX, 202e41f4b71Sopenharmony_ci}; 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_cienum nip_16bit_addr_index { 205e41f4b71Sopenharmony_ci NIP_16BIT_ADDR_INDEX_0 = 0, 206e41f4b71Sopenharmony_ci NIP_16BIT_ADDR_INDEX_1 = 1, 207e41f4b71Sopenharmony_ci NIP_16BIT_ADDR_INDEX_2 = 2, 208e41f4b71Sopenharmony_ci NIP_16BIT_ADDR_INDEX_3 = 3, 209e41f4b71Sopenharmony_ci NIP_16BIT_ADDR_INDEX_MAX, 210e41f4b71Sopenharmony_ci}; 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_cienum nip_32bit_addr_index { 213e41f4b71Sopenharmony_ci NIP_32BIT_ADDR_INDEX_0 = 0, 214e41f4b71Sopenharmony_ci NIP_32BIT_ADDR_INDEX_1 = 1, 215e41f4b71Sopenharmony_ci NIP_32BIT_ADDR_INDEX_MAX, 216e41f4b71Sopenharmony_ci}; 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci#define nip_addr_field8 v.u.field8 219e41f4b71Sopenharmony_ci#define nip_addr_field16 v.u.field16 220e41f4b71Sopenharmony_ci#define nip_addr_field32 v.u.field32 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci#pragma pack(1) 223e41f4b71Sopenharmony_cistruct nip_addr_field { 224e41f4b71Sopenharmony_ci union { 225e41f4b71Sopenharmony_ci unsigned char field8[NIP_8BIT_ADDR_INDEX_MAX]; 226e41f4b71Sopenharmony_ci unsigned short field16[NIP_16BIT_ADDR_INDEX_MAX]; /* Big-endian */ 227e41f4b71Sopenharmony_ci unsigned int field32[NIP_32BIT_ADDR_INDEX_MAX]; /* Big-endian */ 228e41f4b71Sopenharmony_ci } u; 229e41f4b71Sopenharmony_ci}; 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_cistruct nip_addr { 232e41f4b71Sopenharmony_ci unsigned char bitlen; /* The address length is in bit (not byte). */ 233e41f4b71Sopenharmony_ci struct nip_addr_field v; 234e41f4b71Sopenharmony_ci}; 235e41f4b71Sopenharmony_ci#pragma pack() 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci/* The following structure must be larger than V4. System calls use V4. 238e41f4b71Sopenharmony_ci * If the definition is smaller than V4, the read process will have memory overruns 239e41f4b71Sopenharmony_ci * v4: include\linux\socket.h --> sockaddr (16Byte) 240e41f4b71Sopenharmony_ci */ 241e41f4b71Sopenharmony_ci#define POD_SOCKADDR_SIZE 3 242e41f4b71Sopenharmony_cistruct sockaddr_nin { 243e41f4b71Sopenharmony_ci unsigned short sin_family; /* [2 bytes] AF_NINET */ 244e41f4b71Sopenharmony_ci unsigned short sin_port; /* [2 bytes] Transport layer port, big-endian */ 245e41f4b71Sopenharmony_ci struct nip_addr sin_addr; /* [9 bytes] New IP address */ 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci unsigned char sin_zero[POD_SOCKADDR_SIZE]; /* [3 bytes] Byte alignment */ 248e41f4b71Sopenharmony_ci}; 249e41f4b71Sopenharmony_ci``` 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci## New IP Development 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ciOnly the OpenHarmony Linux-5.10 kernel supports New IP kernel protocol stack. You must manually configure IP address and route data for New IP in user mode, and connect the two devices through the router Wi-Fi. If you want to automatically switch to the New IP kernel protocol stack after configuring the New IP address and route, see the description in the blue box in the following figure. 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ciFor details about the address and route configuration, see [examples](https://gitee.com/openharmony/kernel_linux_common_modules/tree/master/newip/examples). Modify the CC definition in Makefile based on the CPU you use, compile the CC definition into a binary file, and push the file to the development board. Refer to the figure above to configure the address and route data for New IP. 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci| File | Description | 260e41f4b71Sopenharmony_ci| ------------------ | -------------------------------------------------------- | 261e41f4b71Sopenharmony_ci| nip_addr.c | Sample code for configuring variable-length New IP addresses (any valid New IP address can be configured).| 262e41f4b71Sopenharmony_ci| nip_route.c | Sample code for configuring New IP route information (any valid New IP address can be configured). | 263e41f4b71Sopenharmony_ci| check_nip_enable.c | Code for obtaining the New IP capabilities of the local host. | 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ciCheck the New IP address and route information on device 1. 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci```sh 268e41f4b71Sopenharmony_ci# cat /proc/net/nip_addr 269e41f4b71Sopenharmony_ci01 wlan0 270e41f4b71Sopenharmony_ci# cat /proc/net/nip_route 271e41f4b71Sopenharmony_ci02 ff09 1 wlan0 # Route to device 2. 272e41f4b71Sopenharmony_ci01 01 2149580801 wlan0 # Route for sending packets to itself and receiving the packets. 273e41f4b71Sopenharmony_ci``` 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ciCheck the New IP address and route information on device 2. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci```sh 278e41f4b71Sopenharmony_ci# cat /proc/net/nip_addr 279e41f4b71Sopenharmony_ci02 wlan0 280e41f4b71Sopenharmony_ci# cat /proc/net/nip_route 281e41f4b71Sopenharmony_ci01 ff09 1 wlan0 # Route to device 1. 282e41f4b71Sopenharmony_ci02 02 2149580801 wlan0 # Route for sending packets to itself and receiving the packets. 283e41f4b71Sopenharmony_ci``` 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci## Sample Code for Receiving and Sending New IP Packets 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ciThe following table lists the related sample code. For details about how to use the user-mode APIs of the New IP stack, see [examples](https://gitee.com/openharmony/kernel_linux_common_modules/tree/master/newip/examples). Fixed addresses and routes are configured in the demo code. You do not need to manually specify the addresses and routes when executing the binary program. 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci| File | Description | 290e41f4b71Sopenharmony_ci| --------------------- | ------------------------------ | 291e41f4b71Sopenharmony_ci| nip_addr_cfg_demo.c | Sample code for configuring variable-length IP addresses. | 292e41f4b71Sopenharmony_ci| nip_route_cfg_demo.c | Sample code for configuring NEW IP routes. | 293e41f4b71Sopenharmony_ci| nip_udp_server_demo.c | Sample code for the server to send and receive New IP packets using UDP.| 294e41f4b71Sopenharmony_ci| nip_udp_client_demo.c | Sample code for the client to send and receive New IP packets using UDP.| 295e41f4b71Sopenharmony_ci| nip_tcp_server_demo.c | Sample code for the server to send and receive New IP packets using TCP.| 296e41f4b71Sopenharmony_ci| nip_tcp_client_demo.c | Sample code for the client to send and receive New IP packets using TCP.| 297e41f4b71Sopenharmony_ci| nip_lib.c | API demo code, for example, obtaining the interface index. | 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci**Basic Procedure** 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci1. Copy the demo code to the Linux compiler, and run **make clean** and **make all** to compile the demo code. 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci2. Upload the generated binary files to device 1 and device 2. 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci3. Run the **ifconfig wlan0 up** command to start the network adapter. 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci4. Run the **./nip_addr_cfg_demo server** command on shell of device 1 to configure a variable-length address **0xDE00** (2 bytes) for the server. Run the **./nip_addr_cfg_demo client** command on shell of device 2 to configure a variable-length address **0x50** (1 byte) for the client. Run **cat /proc/net/nip_addr** to view the configuration result. 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci5. Run the **./nip_route_cfg_demo server** command on shell of device 1 to configure the server route data. Run the **./nip_route_cfg_demo client** command on shell of device 2 to configure the client route data. Then, run the **cat /proc/net/nip_route** command to check the kernel route configuration. 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ciNow, you can send and receive packets over UDP/TCP. By default, the addresses and routes configured are used for sending and receiving packets. 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci**Sending and Receiving Packets over UDP** 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ciRun the **./nip_udp_server_demo** command on the server and then the **./nip_udp_client_demo** command on the client. The client sends 10 New IP packets. After receiving the packets, the server sends them to the client. 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci``` 322e41f4b71Sopenharmony_ciThe following information is displayed in the shell window on the server: 323e41f4b71Sopenharmony_ciReceived -- 1661826989 498038 NIP_UDP # 0 -- from 0x50:57605 324e41f4b71Sopenharmony_ciSending -- 1661826989 498038 NIP_UDP # 0 -- to 0x50:57605 325e41f4b71Sopenharmony_ciReceived -- 1661826990 14641 NIP_UDP # 1 -- from 0x50:57605 326e41f4b71Sopenharmony_ciSending -- 1661826990 14641 NIP_UDP # 1 -- to 0x50:57605 327e41f4b71Sopenharmony_ciReceived -- 1661826990 518388 NIP_UDP # 2 -- from 0x50:57605 328e41f4b71Sopenharmony_ciSending -- 1661826990 518388 NIP_UDP # 2 -- to 0x50:57605 329e41f4b71Sopenharmony_ci... 330e41f4b71Sopenharmony_ciReceived -- 1661827011 590576 NIP_UDP # 9 -- from 0x50:37758 331e41f4b71Sopenharmony_ciSending -- 1661827011 590576 NIP_UDP # 9 -- to 0x50:37758 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ciThe following information is displayed in the shell window on the client: 334e41f4b71Sopenharmony_ciReceived --1661827007 55221 NIP_UDP # 0 sock 3 success: 1/ 1/no= 0 335e41f4b71Sopenharmony_ciReceived --1661827007 557926 NIP_UDP # 1 sock 3 success: 2/ 2/no= 1 336e41f4b71Sopenharmony_ciReceived --1661827008 62653 NIP_UDP # 2 sock 3 success: 3/ 3/no= 2 337e41f4b71Sopenharmony_ci... 338e41f4b71Sopenharmony_ciReceived --1661827011 590576 NIP_UDP # 9 sock 3 success: 10/ 10/no= 9 339e41f4b71Sopenharmony_ci``` 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci**Sending and Receiving Packets over TCP** 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ciRun the **./nip_tcp_server_demo** command on the server and then the **./nip_tcp_client_demo** command on the client. The client sends 10 New IP packets. After receiving the packets, the server sends them to the client. 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci``` 348e41f4b71Sopenharmony_ciThe following information is displayed in the shell window on the server: 349e41f4b71Sopenharmony_ciReceived -- 1661760202 560605 NIP_TCP # 0 --:1024 350e41f4b71Sopenharmony_ciSending -- 1661760202 560605 NIP_TCP # 0 --:1024 351e41f4b71Sopenharmony_ciReceived -- 1661760203 69254 NIP_TCP # 1 --:1024 352e41f4b71Sopenharmony_ciSending -- 1661760203 69254 NIP_TCP # 1 --:1024 353e41f4b71Sopenharmony_ciReceived -- 1661760203 571604 NIP_TCP # 2 --:1024 354e41f4b71Sopenharmony_ciSending -- 1661760203 571604 NIP_TCP # 2 --:1024 355e41f4b71Sopenharmony_ci... 356e41f4b71Sopenharmony_ciReceived -- 1661760207 86544 NIP_TCP # 9 --:1024 357e41f4b71Sopenharmony_ciSending -- 1661760207 86544 NIP_TCP # 9 --:1024 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ciThe following information is displayed in the shell window on the client: 360e41f4b71Sopenharmony_ciReceived --1661760202 560605 NIP_TCP # 0 sock 3 success: 1/ 1/no= 0 361e41f4b71Sopenharmony_ciReceived --1661760203 69254 NIP_TCP # 1 sock 3 success: 2/ 2/no= 1 362e41f4b71Sopenharmony_ci... 363e41f4b71Sopenharmony_ciReceived --1661760207 86544 NIP_TCP # 9 sock 3 success: 10/ 10/no= 9 364e41f4b71Sopenharmony_ci``` 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci## SELinux Policy 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ciThe SELinux policy must be added for the user-mode process to use New IP sockets. Otherwise, the operation will be intercepted. 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci```sh 371e41f4b71Sopenharmony_ci# base\security\selinux\sepolicy\ohos_policy\xxx\xxx.te 372e41f4b71Sopenharmony_ci# socket operation 373e41f4b71Sopenharmony_ci# avc: denied { create } for pid=540 comm="thread_xxx" scontext=u:r:thread_xxx:s0 tcontext=u:r:thread_xxx:s0 tclass=socket permissive=0 374e41f4b71Sopenharmony_ciallow thread_xxx thread_xxx:socket { create bind connect listen accept read write shutdown setopt getopt }; 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci# ioctl operation 377e41f4b71Sopenharmony_ci# The operation code is defined in linux-xxx\include\uapi\linux\sockios.h. 378e41f4b71Sopenharmony_ci# 0x8933 : name -> if_index mapping 379e41f4b71Sopenharmony_ci# 0x8916 : set PA address 380e41f4b71Sopenharmony_ci# 0x890B : add routing table entry 381e41f4b71Sopenharmony_ciallowxperm thread_xxx thread_xxx:socket ioctl { 0x8933 0x8916 0x890B }; 382e41f4b71Sopenharmony_ci``` 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ci## WireShark Packet Parsing Template 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ciThe default packet parsing rules of Wireshark cannot parse New IP packets. You can add a New IP packet parsing template to Wireshark to parse New IP packets. For details about the template, see [New IP packet parsing template](https://gitee.com/openharmony/kernel_linux_common_modules/blob/master/newip/tools/wireshark_cfg_for_newip.lua). 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ciThe procedure is as follows: 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci1. Choose **Help** > **About Wireshark** > **Folders**, and open the **init.lua** file in the **Global Configuration** directory. 391e41f4b71Sopenharmony_ci2. Add **dofile (DATA_DIR.."newip.lua")** to the end of the file. *DATA_DIR* is the path of the **newip.lua** file. 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ciExample: 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci``` 398e41f4b71Sopenharmony_ciPath of the New IP packet parsing template: 399e41f4b71Sopenharmony_ciD:\tools\WireShark\wireshark_cfg_for_newip.lua 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ciPath of the WireShark configuration file: 402e41f4b71Sopenharmony_ciC:\Program Files\Wireshark\init.lua 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ciAdd the following to the end of the init.lua file (Windows 11, for example): 405e41f4b71Sopenharmony_cidofile("D:\\tools\\WireShark\\wireshark_cfg_for_newip.lua") 406e41f4b71Sopenharmony_ci``` 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci### Packet Parsing Example 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci#### ND Request 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ciThe following figure shows the format of a New IP Neighbor Discovery (ND) request packet. The header contains a 1-byte bitmap (**0x76**), which is followed by the TTL, total length of the packet, upper-layer protocol type, destination address, and source address. The New IP ND request packet contains the packet type, operation code, checksum, and request address. 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci> **NOTE** 415e41f4b71Sopenharmony_ci> 416e41f4b71Sopenharmony_ci> New IP supports variable-length addresses (1 to 8 bytes). The 1-byte address in Bitmap 1 indicates that the address carried in the current packet header is of 1 byte. 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci#### ND Response 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ciThe following figure shows the format of a New IP ND response packet. The New IP header contains two bitmaps (**0x77** and **0x00**). Bitmap1 is followed by the TTL, total packet length, upper-layer protocol type, destination address, and source address. Bitmap2 is used for byte alignment and does not carry any data. (For the rk3568 development board, the data transmitted in the link layer must be of an even number of bytes.) A New IP ND response packet contains the packet type, operation code, checksum, neighbor MAC address length, and neighbor MAC address. 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci> **NOTE** 427e41f4b71Sopenharmony_ci> 428e41f4b71Sopenharmony_ci> New IP supports variable-length addresses (1 to 8 bytes). The 1-byte address in Bitmap 1 indicates that the address carried in the current packet header is of 1 byte. 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci#### TCP Handshake 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ciThe figure below shows the format of a TCP three-way handshake SYN packet. The New IP packet header contains two bitmaps (**0x77** and **0x00**). Bitmap1 is followed by the TTL, total packet length, upper-layer protocol type, destination address, and source address. Bitmap2 is used for byte alignment and does not carry any data. (For the rk3568 development board, the data transmitted in the link layer must be of an even number of bytes.) 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci> **NOTE** 439e41f4b71Sopenharmony_ci> 440e41f4b71Sopenharmony_ci> New IP supports variable-length addresses (1 to 8 bytes). The 1-byte address in Bitmap 1 indicates that the address carried in the current packet header is of 1 byte. 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci#### TCP Data Packet 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ciThe figure below shows the TCP data format. The New IP header contains two bitmaps (**0x77** and **0x00**). Bitmap 1 is followed by the TTL, total packet length, upper-layer protocol type, destination address, and source address. Bitmap2 is used for byte alignment and does not carry any data. (For the rk3568 development board, the data transmitted in the link layer must be of an even number of bytes.) 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci> **NOTE** 451e41f4b71Sopenharmony_ci> 452e41f4b71Sopenharmony_ci> New IP supports variable-length addresses (1 to 8 bytes). The 1-byte address in Bitmap 1 indicates that the address carried in the current packet header is of 1 byte. 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci 457