1 /* 2 * Copyright (C) 1999-2011, Broadcom Corporation 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * 16 * Fundamental constants relating to IP Protocol 17 * 18 * $Id: bcmip.h,v 9.19 2009-11-10 20:08:33 Exp $ 19 */ 20 21 22 #ifndef _bcmip_h_ 23 #define _bcmip_h_ 24 25 #ifndef _TYPEDEFS_H_ 26 #include <typedefs.h> 27 #endif 28 29 30 #include <packed_section_start.h> 31 32 33 34 #define IP_VER_OFFSET 0x0 35 #define IP_VER_MASK 0xf0 36 #define IP_VER_SHIFT 4 37 #define IP_VER_4 4 38 #define IP_VER_6 6 39 40 #define IP_VER(ip_body) \ 41 ((((uint8 *)(ip_body))[IP_VER_OFFSET] & IP_VER_MASK) >> IP_VER_SHIFT) 42 43 #define IP_PROT_ICMP 0x1 44 #define IP_PROT_TCP 0x6 45 #define IP_PROT_UDP 0x11 46 47 48 #define IPV4_VER_HL_OFFSET 0 49 #define IPV4_TOS_OFFSET 1 50 #define IPV4_PKTLEN_OFFSET 2 51 #define IPV4_PKTFLAG_OFFSET 6 52 #define IPV4_PROT_OFFSET 9 53 #define IPV4_CHKSUM_OFFSET 10 54 #define IPV4_SRC_IP_OFFSET 12 55 #define IPV4_DEST_IP_OFFSET 16 56 #define IPV4_OPTIONS_OFFSET 20 57 58 59 #define IPV4_VER_MASK 0xf0 60 #define IPV4_VER_SHIFT 4 61 62 #define IPV4_HLEN_MASK 0x0f 63 #define IPV4_HLEN(ipv4_body) (4 * (((uint8 *)(ipv4_body))[IPV4_VER_HL_OFFSET] & IPV4_HLEN_MASK)) 64 65 #define IPV4_ADDR_LEN 4 66 67 #define IPV4_ADDR_NULL(a) ((((uint8 *)(a))[0] | ((uint8 *)(a))[1] | \ 68 ((uint8 *)(a))[2] | ((uint8 *)(a))[3]) == 0) 69 70 #define IPV4_ADDR_BCAST(a) ((((uint8 *)(a))[0] & ((uint8 *)(a))[1] & \ 71 ((uint8 *)(a))[2] & ((uint8 *)(a))[3]) == 0xff) 72 73 #define IPV4_TOS_DSCP_MASK 0xfc 74 #define IPV4_TOS_DSCP_SHIFT 2 75 76 #define IPV4_TOS(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_TOS_OFFSET]) 77 78 #define IPV4_TOS_PREC_MASK 0xe0 79 #define IPV4_TOS_PREC_SHIFT 5 80 81 #define IPV4_TOS_LOWDELAY 0x10 82 #define IPV4_TOS_THROUGHPUT 0x8 83 #define IPV4_TOS_RELIABILITY 0x4 84 85 #define IPV4_PROT(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_PROT_OFFSET]) 86 87 #define IPV4_FRAG_RESV 0x8000 88 #define IPV4_FRAG_DONT 0x4000 89 #define IPV4_FRAG_MORE 0x2000 90 #define IPV4_FRAG_OFFSET_MASK 0x1fff 91 92 #define IPV4_ADDR_STR_LEN 16 93 94 95 BWL_PRE_PACKED_STRUCT struct ipv4_addr { 96 uint8 addr[IPV4_ADDR_LEN]; 97 } BWL_POST_PACKED_STRUCT; 98 99 BWL_PRE_PACKED_STRUCT struct ipv4_hdr { 100 uint8 version_ihl; 101 uint8 tos; 102 uint16 tot_len; 103 uint16 id; 104 uint16 frag; 105 uint8 ttl; 106 uint8 prot; 107 uint16 hdr_chksum; 108 uint8 src_ip[IPV4_ADDR_LEN]; 109 uint8 dst_ip[IPV4_ADDR_LEN]; 110 } BWL_POST_PACKED_STRUCT; 111 112 113 #define IPV6_PAYLOAD_LEN_OFFSET 4 114 #define IPV6_NEXT_HDR_OFFSET 6 115 #define IPV6_HOP_LIMIT_OFFSET 7 116 #define IPV6_SRC_IP_OFFSET 8 117 #define IPV6_DEST_IP_OFFSET 24 118 119 120 #define IPV6_TRAFFIC_CLASS(ipv6_body) \ 121 (((((uint8 *)(ipv6_body))[0] & 0x0f) << 4) | \ 122 ((((uint8 *)(ipv6_body))[1] & 0xf0) >> 4)) 123 124 #define IPV6_FLOW_LABEL(ipv6_body) \ 125 (((((uint8 *)(ipv6_body))[1] & 0x0f) << 16) | \ 126 (((uint8 *)(ipv6_body))[2] << 8) | \ 127 (((uint8 *)(ipv6_body))[3])) 128 129 #define IPV6_PAYLOAD_LEN(ipv6_body) \ 130 ((((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 0] << 8) | \ 131 ((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 1]) 132 133 #define IPV6_NEXT_HDR(ipv6_body) \ 134 (((uint8 *)(ipv6_body))[IPV6_NEXT_HDR_OFFSET]) 135 136 #define IPV6_PROT(ipv6_body) IPV6_NEXT_HDR(ipv6_body) 137 138 #define IPV6_ADDR_LEN 16 139 140 141 #define IP_TOS46(ip_body) \ 142 (IP_VER(ip_body) == IP_VER_4 ? IPV4_TOS(ip_body) : \ 143 IP_VER(ip_body) == IP_VER_6 ? IPV6_TRAFFIC_CLASS(ip_body) : 0) 144 145 146 #include <packed_section_end.h> 147 148 #endif 149