4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 1982, 1986, 1990, 1993
37 * The Regents of the University of California. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)in.h 8.3 (Berkeley) 1/3/94
68 * $FreeBSD: src/sys/netinet/in.h,v 1.82 2003/10/25 09:37:10 ume Exp $
82 #include <rte_memcpy.h>
83 #include <rte_byteorder.h>
93 uint8_t version_ihl; /**< version and header length */
94 uint8_t type_of_service; /**< type of service */
95 uint16_t total_length; /**< length of packet */
96 uint16_t packet_id; /**< packet ID */
97 uint16_t fragment_offset; /**< fragmentation offset */
98 uint8_t time_to_live; /**< time to live */
99 uint8_t next_proto_id; /**< protocol ID */
100 uint16_t hdr_checksum; /**< header checksum */
101 uint32_t src_addr; /**< source address */
102 uint32_t dst_addr; /**< destination address */
103 } __attribute__((__packed__));
105 /** Create IPv4 address */
106 #define IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
107 (((b) & 0xff) << 16) | \
108 (((c) & 0xff) << 8) | \
111 /* Fragment Offset * Flags. */
112 #define IPV4_HDR_DF_SHIFT 14
113 #define IPV4_HDR_MF_SHIFT 13
114 #define IPV4_HDR_FO_SHIFT 3
116 #define IPV4_HDR_DF_FLAG (1 << IPV4_HDR_DF_SHIFT)
117 #define IPV4_HDR_MF_FLAG (1 << IPV4_HDR_MF_SHIFT)
119 #define IPV4_HDR_OFFSET_MASK ((1 << IPV4_HDR_MF_SHIFT) - 1)
121 #define IPV4_HDR_OFFSET_UNITS 8
124 #define IPPROTO_IP 0 /**< dummy for IP */
125 #define IPPROTO_HOPOPTS 0 /**< IP6 hop-by-hop options */
126 #define IPPROTO_ICMP 1 /**< control message protocol */
127 #define IPPROTO_IGMP 2 /**< group mgmt protocol */
128 #define IPPROTO_GGP 3 /**< gateway^2 (deprecated) */
129 #define IPPROTO_IPV4 4 /**< IPv4 encapsulation */
130 #define IPPROTO_TCP 6 /**< tcp */
131 #define IPPROTO_ST 7 /**< Stream protocol II */
132 #define IPPROTO_EGP 8 /**< exterior gateway protocol */
133 #define IPPROTO_PIGP 9 /**< private interior gateway */
134 #define IPPROTO_RCCMON 10 /**< BBN RCC Monitoring */
135 #define IPPROTO_NVPII 11 /**< network voice protocol*/
136 #define IPPROTO_PUP 12 /**< pup */
137 #define IPPROTO_ARGUS 13 /**< Argus */
138 #define IPPROTO_EMCON 14 /**< EMCON */
139 #define IPPROTO_XNET 15 /**< Cross Net Debugger */
140 #define IPPROTO_CHAOS 16 /**< Chaos*/
141 #define IPPROTO_UDP 17 /**< user datagram protocol */
142 #define IPPROTO_MUX 18 /**< Multiplexing */
143 #define IPPROTO_MEAS 19 /**< DCN Measurement Subsystems */
144 #define IPPROTO_HMP 20 /**< Host Monitoring */
145 #define IPPROTO_PRM 21 /**< Packet Radio Measurement */
146 #define IPPROTO_IDP 22 /**< xns idp */
147 #define IPPROTO_TRUNK1 23 /**< Trunk-1 */
148 #define IPPROTO_TRUNK2 24 /**< Trunk-2 */
149 #define IPPROTO_LEAF1 25 /**< Leaf-1 */
150 #define IPPROTO_LEAF2 26 /**< Leaf-2 */
151 #define IPPROTO_RDP 27 /**< Reliable Data */
152 #define IPPROTO_IRTP 28 /**< Reliable Transaction */
153 #define IPPROTO_TP 29 /**< tp-4 w/ class negotiation */
154 #define IPPROTO_BLT 30 /**< Bulk Data Transfer */
155 #define IPPROTO_NSP 31 /**< Network Services */
156 #define IPPROTO_INP 32 /**< Merit Internodal */
157 #define IPPROTO_SEP 33 /**< Sequential Exchange */
158 #define IPPROTO_3PC 34 /**< Third Party Connect */
159 #define IPPROTO_IDPR 35 /**< InterDomain Policy Routing */
160 #define IPPROTO_XTP 36 /**< XTP */
161 #define IPPROTO_DDP 37 /**< Datagram Delivery */
162 #define IPPROTO_CMTP 38 /**< Control Message Transport */
163 #define IPPROTO_TPXX 39 /**< TP++ Transport */
164 #define IPPROTO_IL 40 /**< IL transport protocol */
165 #define IPPROTO_IPV6 41 /**< IP6 header */
166 #define IPPROTO_SDRP 42 /**< Source Demand Routing */
167 #define IPPROTO_ROUTING 43 /**< IP6 routing header */
168 #define IPPROTO_FRAGMENT 44 /**< IP6 fragmentation header */
169 #define IPPROTO_IDRP 45 /**< InterDomain Routing*/
170 #define IPPROTO_RSVP 46 /**< resource reservation */
171 #define IPPROTO_GRE 47 /**< General Routing Encap. */
172 #define IPPROTO_MHRP 48 /**< Mobile Host Routing */
173 #define IPPROTO_BHA 49 /**< BHA */
174 #define IPPROTO_ESP 50 /**< IP6 Encap Sec. Payload */
175 #define IPPROTO_AH 51 /**< IP6 Auth Header */
176 #define IPPROTO_INLSP 52 /**< Integ. Net Layer Security */
177 #define IPPROTO_SWIPE 53 /**< IP with encryption */
178 #define IPPROTO_NHRP 54 /**< Next Hop Resolution */
179 /* 55-57: Unassigned */
180 #define IPPROTO_ICMPV6 58 /**< ICMP6 */
181 #define IPPROTO_NONE 59 /**< IP6 no next header */
182 #define IPPROTO_DSTOPTS 60 /**< IP6 destination option */
183 #define IPPROTO_AHIP 61 /**< any host internal protocol */
184 #define IPPROTO_CFTP 62 /**< CFTP */
185 #define IPPROTO_HELLO 63 /**< "hello" routing protocol */
186 #define IPPROTO_SATEXPAK 64 /**< SATNET/Backroom EXPAK */
187 #define IPPROTO_KRYPTOLAN 65 /**< Kryptolan */
188 #define IPPROTO_RVD 66 /**< Remote Virtual Disk */
189 #define IPPROTO_IPPC 67 /**< Pluribus Packet Core */
190 #define IPPROTO_ADFS 68 /**< Any distributed FS */
191 #define IPPROTO_SATMON 69 /**< Satnet Monitoring */
192 #define IPPROTO_VISA 70 /**< VISA Protocol */
193 #define IPPROTO_IPCV 71 /**< Packet Core Utility */
194 #define IPPROTO_CPNX 72 /**< Comp. Prot. Net. Executive */
195 #define IPPROTO_CPHB 73 /**< Comp. Prot. HeartBeat */
196 #define IPPROTO_WSN 74 /**< Wang Span Network */
197 #define IPPROTO_PVP 75 /**< Packet Video Protocol */
198 #define IPPROTO_BRSATMON 76 /**< BackRoom SATNET Monitoring */
199 #define IPPROTO_ND 77 /**< Sun net disk proto (temp.) */
200 #define IPPROTO_WBMON 78 /**< WIDEBAND Monitoring */
201 #define IPPROTO_WBEXPAK 79 /**< WIDEBAND EXPAK */
202 #define IPPROTO_EON 80 /**< ISO cnlp */
203 #define IPPROTO_VMTP 81 /**< VMTP */
204 #define IPPROTO_SVMTP 82 /**< Secure VMTP */
205 #define IPPROTO_VINES 83 /**< Banyon VINES */
206 #define IPPROTO_TTP 84 /**< TTP */
207 #define IPPROTO_IGP 85 /**< NSFNET-IGP */
208 #define IPPROTO_DGP 86 /**< dissimilar gateway prot. */
209 #define IPPROTO_TCF 87 /**< TCF */
210 #define IPPROTO_IGRP 88 /**< Cisco/GXS IGRP */
211 #define IPPROTO_OSPFIGP 89 /**< OSPFIGP */
212 #define IPPROTO_SRPC 90 /**< Strite RPC protocol */
213 #define IPPROTO_LARP 91 /**< Locus Address Resoloution */
214 #define IPPROTO_MTP 92 /**< Multicast Transport */
215 #define IPPROTO_AX25 93 /**< AX.25 Frames */
216 #define IPPROTO_IPEIP 94 /**< IP encapsulated in IP */
217 #define IPPROTO_MICP 95 /**< Mobile Int.ing control */
218 #define IPPROTO_SCCSP 96 /**< Semaphore Comm. security */
219 #define IPPROTO_ETHERIP 97 /**< Ethernet IP encapsulation */
220 #define IPPROTO_ENCAP 98 /**< encapsulation header */
221 #define IPPROTO_APES 99 /**< any private encr. scheme */
222 #define IPPROTO_GMTP 100 /**< GMTP */
223 #define IPPROTO_IPCOMP 108 /**< payload compression (IPComp) */
224 /* 101-254: Partly Unassigned */
225 #define IPPROTO_PIM 103 /**< Protocol Independent Mcast */
226 #define IPPROTO_PGM 113 /**< PGM */
227 #define IPPROTO_SCTP 132 /**< Stream Control Transport Protocol */
229 /* BSD Private, local use, namespace incursion */
230 #define IPPROTO_DIVERT 254 /**< divert pseudo-protocol */
231 #define IPPROTO_RAW 255 /**< raw IP packet */
232 #define IPPROTO_MAX 256 /**< maximum protocol number */
237 #define IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
238 #define IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
239 #define IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
240 #define IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
241 #define IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
242 #define IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
245 * IPv4 Multicast-related macros
247 #define IPV4_MIN_MCAST IPv4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
248 #define IPV4_MAX_MCAST IPv4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
250 #define IS_IPV4_MCAST(x) \
251 ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
254 * Process the non-complemented checksum of a buffer.
257 * Pointer to the buffer.
259 * Length of the buffer.
261 * The non-complemented checksum.
263 static inline uint16_t
264 rte_raw_cksum(const char *buf, size_t len)
266 const uint16_t *u16 = (const uint16_t *)buf;
269 while (len >= (sizeof(*u16) * 4)) {
274 len -= sizeof(*u16) * 4;
277 while (len >= sizeof(*u16)) {
283 /* if length is in odd bytes */
285 sum += *((const uint8_t *)u16);
287 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
288 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
289 return (uint16_t)sum;
293 * Process the IPv4 checksum of an IPv4 header.
295 * The checksum field must be set to 0 by the caller.
298 * The pointer to the contiguous IPv4 header.
300 * The complemented checksum to set in the IP packet.
302 static inline uint16_t
303 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
306 cksum = rte_raw_cksum((const char *)ipv4_hdr, sizeof(struct ipv4_hdr));
307 return ((cksum == 0xffff) ? cksum : ~cksum);
311 * Process the pseudo-header checksum of an IPv4 header.
313 * The checksum field must be set to 0 by the caller.
316 * The pointer to the contiguous IPv4 header.
318 * The non-complemented checksum to set in the L4 header.
320 static inline uint16_t
321 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr)
323 struct ipv4_psd_header {
324 uint32_t src_addr; /* IP address of source host. */
325 uint32_t dst_addr; /* IP address of destination host. */
326 uint8_t zero; /* zero. */
327 uint8_t proto; /* L4 protocol type. */
328 uint16_t len; /* L4 length. */
331 psd_hdr.src_addr = ipv4_hdr->src_addr;
332 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
334 psd_hdr.proto = ipv4_hdr->next_proto_id;
335 psd_hdr.len = rte_cpu_to_be_16(
336 (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
337 - sizeof(struct ipv4_hdr)));
338 return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
342 * Process the IPv4 UDP or TCP checksum.
344 * The IPv4 header should not contains options. The IP and layer 4
345 * checksum must be set to 0 in the packet by the caller.
348 * The pointer to the contiguous IPv4 header.
350 * The pointer to the beginning of the L4 header.
352 * The complemented checksum to set in the IP packet.
354 static inline uint16_t
355 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
360 l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
361 sizeof(struct ipv4_hdr);
363 cksum = rte_raw_cksum(l4_hdr, l4_len);
364 cksum += rte_ipv4_phdr_cksum(ipv4_hdr);
366 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
367 cksum = (~cksum) & 0xffff;
378 uint32_t vtc_flow; /**< IP version, traffic class & flow label. */
379 uint16_t payload_len; /**< IP packet length - includes sizeof(ip_header). */
380 uint8_t proto; /**< Protocol, next header. */
381 uint8_t hop_limits; /**< Hop limits. */
382 uint8_t src_addr[16]; /**< IP address of source host. */
383 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
384 } __attribute__((__packed__));
387 * Process the pseudo-header checksum of an IPv6 header.
390 * The pointer to the contiguous IPv6 header.
392 * The non-complemented checksum to set in the L4 header.
394 static inline uint16_t
395 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr)
397 struct ipv6_psd_header {
398 uint8_t src_addr[16]; /* IP address of source host. */
399 uint8_t dst_addr[16]; /* IP address of destination host. */
400 uint32_t len; /* L4 length. */
401 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
404 rte_memcpy(&psd_hdr.src_addr, ipv6_hdr->src_addr,
405 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr));
406 psd_hdr.proto = (ipv6_hdr->proto << 24);
407 psd_hdr.len = ipv6_hdr->payload_len;
409 return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
413 * Process the IPv6 UDP or TCP checksum.
415 * The IPv4 header should not contains options. The layer 4 checksum
416 * must be set to 0 in the packet by the caller.
419 * The pointer to the contiguous IPv6 header.
421 * The pointer to the beginning of the L4 header.
423 * The complemented checksum to set in the IP packet.
425 static inline uint16_t
426 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
431 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
433 cksum = rte_raw_cksum(l4_hdr, l4_len);
434 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
436 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
437 cksum = (~cksum) & 0xffff;
448 #endif /* _RTE_IP_H_ */