2 * Copyright 2007, Broadcom Corporation
5 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
10 * Fundamental constants relating to IP Protocol
12 * $Id: bcmip.h,v 1.1.1.4 2007/05/31 08:00:41 michael Exp $
18 /* enable structure packing */
20 #define PACKED __attribute__((packed))
27 /* IPV4 and IPV6 common */
28 #define IP_VER_OFFSET 0x0 /* offset to version field */
29 #define IP_VER_MASK 0xf0 /* version mask */
30 #define IP_VER_SHIFT 4 /* version shift */
31 #define IP_VER_4 4 /* version number for IPV4 */
32 #define IP_VER_6 6 /* version number for IPV6 */
34 #define IP_VER(ip_body) \
35 ((((uint8 *)(ip_body))[IP_VER_OFFSET] & IP_VER_MASK) >> IP_VER_SHIFT)
37 #define IP_PROT_ICMP 0x1 /* ICMP protocol */
38 #define IP_PROT_TCP 0x6 /* TCP protocol */
39 #define IP_PROT_UDP 0x11 /* UDP protocol type */
41 /* IPV4 field offsets */
42 #define IPV4_VER_HL_OFFSET 0 /* version and ihl byte offset */
43 #define IPV4_TOS_OFFSET 1 /* type of service offset */
44 #define IPV4_PKTLEN_OFFSET 2 /* packet length offset */
45 #define IPV4_PKTFLAG_OFFSET 6 /* more-frag,dont-frag flag offset */
46 #define IPV4_PROT_OFFSET 9 /* protocol type offset */
47 #define IPV4_CHKSUM_OFFSET 10 /* IP header checksum offset */
48 #define IPV4_SRC_IP_OFFSET 12 /* src IP addr offset */
49 #define IPV4_DEST_IP_OFFSET 16 /* dest IP addr offset */
50 #define IPV4_OPTIONS_OFFSET 20 /* IP options offset */
52 /* IPV4 field decodes */
53 #define IPV4_VER_MASK 0xf0 /* IPV4 version mask */
54 #define IPV4_VER_SHIFT 4 /* IPV4 version shift */
56 #define IPV4_HLEN_MASK 0x0f /* IPV4 header length mask */
57 #define IPV4_HLEN(ipv4_body) (4 * (((uint8 *)(ipv4_body))[IPV4_VER_HL_OFFSET] & IPV4_HLEN_MASK))
59 #define IPV4_ADDR_LEN 4 /* IPV4 address length */
61 #define IPV4_ADDR_NULL(a) ((((uint8 *)(a))[0] | ((uint8 *)(a))[1] | \
62 ((uint8 *)(a))[2] | ((uint8 *)(a))[3]) == 0)
64 #define IPV4_ADDR_BCAST(a) ((((uint8 *)(a))[0] & ((uint8 *)(a))[1] & \
65 ((uint8 *)(a))[2] & ((uint8 *)(a))[3]) == 0xff)
67 #define IPV4_TOS_DSCP_MASK 0xfc /* DiffServ codepoint mask */
68 #define IPV4_TOS_DSCP_SHIFT 2 /* DiffServ codepoint shift */
70 #define IPV4_TOS(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_TOS_OFFSET])
72 #define IPV4_TOS_PREC_MASK 0xe0 /* Historical precedence mask */
73 #define IPV4_TOS_PREC_SHIFT 5 /* Historical precedence shift */
75 #define IPV4_TOS_LOWDELAY 0x10 /* Lowest delay requested */
76 #define IPV4_TOS_THROUGHPUT 0x8 /* Best throughput requested */
77 #define IPV4_TOS_RELIABILITY 0x4 /* Most reliable delivery requested */
79 #define IPV4_PROT(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_PROT_OFFSET])
81 #define IPV4_FRAG_RESV 0x8000 /* Reserved */
82 #define IPV4_FRAG_DONT 0x4000 /* Don't fragment */
83 #define IPV4_FRAG_MORE 0x2000 /* More fragments */
84 #define IPV4_FRAG_OFFSET_MASK 0x1fff /* Fragment offset */
86 #define IPV4_ADDR_STR_LEN 16 /* Max IP address length in string format */
88 /* IPv4, no options only. */
89 #define IPV4_NO_OPTIONS_HDR_LEN 20
90 #define IPV4_NO_OPTIONS_PAYLOAD(ip_hdr) (&(((uint8 *)(ip_hdr))[IPV4_NO_OPTIONS_HDR_LEN]))
92 #define IPV4_PAYLOAD_LEN(ip_body) \
93 (((int)(((uint8 *)(ip_body))[IPV4_PKTLEN_OFFSET + 0]) << 8) | \
94 ((uint8 *)(ip_body))[IPV4_PKTLEN_OFFSET + 1])
96 /* IPV4 packet formats */
98 uint8 addr
[IPV4_ADDR_LEN
];
102 uint8 version_ihl
; /* Version and Internet Header Length */
103 uint8 tos
; /* Type Of Service */
104 uint16 tot_len
; /* Number of bytes in packet (max 65535) */
106 uint16 frag
; /* 3 flag bits and fragment offset */
107 uint8 ttl
; /* Time To Live */
108 uint8 prot
; /* Protocol */
109 uint16 hdr_chksum
; /* IP header checksum */
110 uint8 src_ip
[IPV4_ADDR_LEN
]; /* Source IP Address */
111 uint8 dst_ip
[IPV4_ADDR_LEN
]; /* Destination IP Address */
114 /* IPV6 field offsets */
115 #define IPV6_PAYLOAD_LEN_OFFSET 4 /* payload length offset */
116 #define IPV6_NEXT_HDR_OFFSET 6 /* next header/protocol offset */
117 #define IPV6_HOP_LIMIT_OFFSET 7 /* hop limit offset */
118 #define IPV6_SRC_IP_OFFSET 8 /* src IP addr offset */
119 #define IPV6_DEST_IP_OFFSET 24 /* dst IP addr offset */
121 /* IPV6 field decodes */
122 #define IPV6_TRAFFIC_CLASS(ipv6_body) \
123 (((((uint8 *)(ipv6_body))[0] & 0x0f) << 4) | \
124 ((((uint8 *)(ipv6_body))[1] & 0xf0) >> 4))
126 #define IPV6_FLOW_LABEL(ipv6_body) \
127 (((((uint8 *)(ipv6_body))[1] & 0x0f) << 16) | \
128 (((uint8 *)(ipv6_body))[2] << 8) | \
129 (((uint8 *)(ipv6_body))[3]))
131 #define IPV6_PAYLOAD_LEN(ipv6_body) \
132 ((((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 0] << 8) | \
133 ((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 1])
135 #define IPV6_NEXT_HDR(ipv6_body) \
136 (((uint8 *)(ipv6_body))[IPV6_NEXT_HDR_OFFSET])
138 #define IPV6_PROT(ipv6_body) IPV6_NEXT_HDR(ipv6_body)
140 #define IPV6_ADDR_LEN 16 /* IPV6 address length */
142 /* IPV4 TOS or IPV6 Traffic Classifier or 0 */
143 #define IP_TOS(ip_body) \
144 (IP_VER(ip_body) == IP_VER_4 ? IPV4_TOS(ip_body) : \
145 IP_VER(ip_body) == IP_VER_6 ? IPV6_TRAFFIC_CLASS(ip_body) : 0)
148 #if !defined(__GNUC__)
152 #endif /* _bcmip_h_ */
This page took 0.052576 seconds and 5 git commands to generate.