Line data Source code
1 : #ifndef HEADER_fd_src_util_net_fd_ip4_h 2 : #define HEADER_fd_src_util_net_fd_ip4_h 3 : 4 : #include "../bits/fd_bits.h" 5 : 6 : /* FIXME: IP4 CRASH COURSE HERE */ 7 : 8 : #define FD_IP4_HDR_TOS_PREC_INTERNETCONTROL ((uchar)0xc0) /* This packet is should have Internet control type of service */ 9 : 10 : #define FD_IP4_HDR_FRAG_OFF_RF ((ushort)0x8000) /* (in host byte order) Mask for the frag off reserved bit */ 11 0 : #define FD_IP4_HDR_FRAG_OFF_DF ((ushort)0x4000) /* (in host byte order) Mask for the frag off don't frag bit */ 12 : #define FD_IP4_HDR_FRAG_OFF_MF ((ushort)0x2000) /* (in host byte order) Mask for the frag off more frags bit */ 13 : #define FD_IP4_HDR_FRAG_OFF_MASK ((ushort)0x1fff) /* (in host byte order) Mask for the frag off offset bits */ 14 : 15 : #define FD_IP4_HDR_PROTOCOL_IP4 ((uchar) 0) /* The IP4 packet encapsulates an IP4 packet */ 16 0 : #define FD_IP4_HDR_PROTOCOL_ICMP ((uchar) 1) /* The IP4 packet encapsulates an ICMP packet */ 17 : #define FD_IP4_HDR_PROTOCOL_IGMP ((uchar) 2) /* The IP4 packet encapsulates an IGMP packet */ 18 0 : #define FD_IP4_HDR_PROTOCOL_TCP ((uchar) 6) /* The IP4 packet encapsulates an TCP packet */ 19 92664785 : #define FD_IP4_HDR_PROTOCOL_UDP ((uchar)17) /* The IP4 packet encapsulates an UDP packet */ 20 : 21 : #define FD_IP4_OPT_RA ((uchar)148) /* This option is a router alert option */ 22 : #define FD_IP4_OPT_EOL ((uchar)0) /* This is the end of the options list */ 23 : 24 : /* All of the below are in network byte order */ 25 42 : #define IP4_PRIVATE_RANGE1_START_NET FD_IP4_ADDR( 10, 0, 0, 0) 26 36 : #define IP4_PRIVATE_RANGE1_END_NET FD_IP4_ADDR( 10, 255, 255, 255) 27 30 : #define IP4_PRIVATE_RANGE2_START_NET FD_IP4_ADDR(172, 16, 0, 0) 28 18 : #define IP4_PRIVATE_RANGE2_END_NET FD_IP4_ADDR(172, 31, 255, 255) 29 21 : #define IP4_PRIVATE_RANGE3_START_NET FD_IP4_ADDR(192, 168, 0, 0) 30 9 : #define IP4_PRIVATE_RANGE3_END_NET FD_IP4_ADDR(192, 168, 255, 255) 31 : 32 12 : #define IP4_LOOPBACK_START_NET FD_IP4_ADDR(127, 0, 0, 0) 33 3 : #define IP4_LOOPBACK_END_NET FD_IP4_ADDR(127, 255, 255, 255) 34 : 35 : union fd_ip4_hdr { 36 : struct { 37 : uchar verihl; /* 4 msb: IP version (==4), assumes little endian */ 38 : /* 4 lsb: Header length in words (>=5) */ 39 : uchar tos; /* Type of service */ 40 : ushort net_tot_len; /* Frag size in bytes, incl ip hdr, net order */ 41 : ushort net_id; /* Frag id, unique from sender for long enough, net order */ 42 : ushort net_frag_off; /* Frag off (dbl words)+status (top 3 bits), net order */ 43 : uchar ttl; /* Frag time to live */ 44 : uchar protocol; /* Type of payload */ 45 : ushort check; /* Header checksum ("invariant" order) */ 46 : union __attribute__((packed)) { 47 : uchar saddr_c[4]; /* Address of sender, technically net order but all APIs below work with this directly */ 48 : uint saddr; 49 : }; 50 : union __attribute__((packed)) { 51 : uchar daddr_c[4]; /* Address of destination, technically net order but all APIs below work with this directly */ 52 : uint daddr; 53 : }; 54 : /* Up to 40 bytes of options here */ 55 : }; 56 : }; 57 : 58 : typedef union fd_ip4_hdr fd_ip4_hdr_t; 59 : 60 : /* FD_IP4_GET_VERSION obtains the version from the supplied fd_ip4_hdr */ 61 : 62 13858876 : #define FD_IP4_GET_VERSION(ip4) ((uchar)( ( (uint)(ip4).verihl >> 4u ) & 0x0fu )) 63 : 64 : /* FD_IP4_SET_VERSION sets the version in the supplied fd_ip4_hdr */ 65 : 66 : #define FD_IP4_SET_VERSION(ip4,value) (ip4).verihl = ((uchar)( \ 67 : ( (uint)(ip4).verihl & 0x0fu ) | ( ( (uint)(value) & 0x0fu ) << 4u ) )) 68 : 69 : /* FD_IP4_GET_IHL retrieves the IHL field from the supplied fd_ip4_hdr */ 70 : 71 13858876 : #define FD_IP4_GET_IHL(ip4) ((uchar)( (uint)(ip4).verihl & 0x0fu )) 72 : 73 : /* FD_IP4_GET_LEN retrieves and adjusts the IHL field from the supplied fd_ip4_hdr */ 74 : 75 13858876 : #define FD_IP4_GET_LEN(ip4) ( FD_IP4_GET_IHL(ip4) * 4u ) 76 : 77 : /* FD_IP4_SET_IHL sets the IHL field in the supplied fd_ip4_hdr */ 78 : 79 : #define FD_IP4_SET_IHL(ip4,value) ((uchar)( \ 80 : ( (uint)(ip4).verihl & 0xf0u ) | ( (uint)(value) & 0x0fu ) )) 81 : 82 : /* FD_IP4_VERIHL combines the supplied IHL and VERSION into a single verihl fields */ 83 : 84 171472599 : #define FD_IP4_VERIHL(version,ihl) ((uchar)( ( ((uint)(version) & 0x0fu) << 4u ) | \ 85 171472599 : ((uint)(ihl) & 0x0fu) )) 86 : 87 : /* FD_IP4_ADDR constructs an IP4 address from the 4-tuple x.y.z.w. 88 : Assumes x,y,z,w are all integers in [0,255]. */ 89 : 90 600294 : #define FD_IP4_ADDR(x,y,z,w) (((uint)(x)) | (((uint)(y)) << 8) | (((uint)(z)) << 16) | (((uint)(w)) << 24)) 91 : 92 : /* FD_IP4_ADDR_FMT / FD_IP4_ADDR_FMT_ARGS are used to pretty print a 93 : ip4 address by a printf style formatter. a must be safe against 94 : multiple evaluation. Example usage: 95 : 96 : fd_ip4_hdr_t * hdr = ...; 97 : FD_LOG_NOTICE(( "DST MAC: " FD_IP4_ADDR_FMT, FD_IP4_ADDR_FMT_ARGS( hdr->daddr ) */ 98 : 99 : #define FD_IP4_ADDR_FMT "%u.%u.%u.%u" 100 : #define FD_IP4_ADDR_FMT_ARGS(a) ((a) & 255U),(((a)>>8) & 255U),(((a)>>16) & 255U),((a)>>24) 101 : 102 : /* FIXME: CONSIDER AN OVERALL HEADER PRETTY PRINTER? */ 103 : 104 : FD_PROTOTYPES_BEGIN 105 : 106 : /* fd_ip4_addr_is_{mcast,bcast} returns 1 if the ipaddr is {multicast 107 : (in [224-239].y.z.w),global broadcast (255.255.255.255)} and 0 108 : otherwise. fd_ip4_hdr_net_frag_off_is_unfragmented returns 1 if the 109 : net_frag_off field of the ip4 header indicates the encapsulated 110 : packet is not fragmented (i.e. entirely containing the IP4 packet) 111 : and 0 otherwise (i.e. fragmented into multiple IP4 packets). */ 112 : 113 9 : FD_FN_CONST static inline int fd_ip4_addr_is_mcast( uint addr ) { return (((uchar)addr)>>4)==(uchar)0xe; } 114 9 : FD_FN_CONST static inline int fd_ip4_addr_is_bcast( uint addr ) { return addr==~0U; } 115 : 116 : FD_FN_CONST static inline int 117 0 : fd_ip4_hdr_net_frag_off_is_unfragmented( ushort net_frag_off ) { /* net order */ 118 0 : return !(((uint)net_frag_off) & 0xff3fU); /* ff3f is fd_ushort_bswap( NET_IP_HDR_FRAG_OFF_MASK | NET_IP_HDR_FRAG_OFF_MF ) */ 119 0 : } 120 : 121 : /* fd_ip4_hdr_check is used for hdr check field computation and 122 : validation. hdr points to the first byte a memory region containing 123 : an ip4 header and any options that might follow it. If the header 124 : has checksum (check==0), this returns the value to use for check. If 125 : hdr has a checksum (check!=0), this returns 0 if hdr has a valid 126 : checksum (or non-zero if not). This is mostly for use in cases where 127 : the overhead doesn't matter or when the hardware sending/receiving 128 : the packet doesn't do various checksum offload computations. */ 129 : 130 : FD_FN_PURE static inline ushort 131 0 : fd_ip4_hdr_check( void const * vp_hdr ) { 132 0 : uchar * cp = (uchar*)vp_hdr; 133 : 134 0 : uint n = ( (*cp) & 0x0fu ); 135 : 136 : /* optimizes the first 5 by unrolling */ 137 0 : if( n < 5 ) __builtin_unreachable(); 138 : 139 0 : ulong c = 0UL; 140 0 : for( uint i=0U; i<n; i++ ) { 141 0 : uint u; 142 : 143 : /* the compiler elides the copy in practice */ 144 0 : memcpy( &u, cp + i*4, 4 ); 145 0 : c += (ulong)u; 146 0 : } 147 : 148 0 : c = ( c>>32 ) + 149 0 : ((c>>16) & 0xffffUL) + 150 0 : ( c & 0xffffUL); 151 0 : c = ( c>>16 ) + 152 0 : ( c & 0xffffUL); 153 0 : c += ( c>>16 ); 154 : 155 0 : return (ushort)~c; 156 0 : } 157 : 158 : /* fd_ip4_hdr_check_fast is the same as the above but assumes that the 159 : header has no options (i.e. ihl==5) */ 160 : 161 : FD_FN_PURE static inline ushort 162 13856971 : fd_ip4_hdr_check_fast( void const * vp_hdr ) { 163 13856971 : uchar * cp = (uchar*)vp_hdr; 164 : 165 13856971 : uint n = ( (*cp) & 0x0fu ); 166 : 167 : /* branches aren't taken don't use branch table entries */ 168 13856971 : if( FD_UNLIKELY( n != 5 ) ) return fd_ip4_hdr_check(vp_hdr); 169 : 170 : /* the compiler knows n here and completely unrolls the loop */ 171 13856971 : ulong c = 0UL; 172 83141826 : for( uint i=0U; i<n; i++ ) { 173 69284855 : uint u; 174 : 175 : /* the compiler elides the copy in practice */ 176 69284855 : memcpy( &u, cp + i*4, 4 ); 177 69284855 : c += (ulong)u; 178 69284855 : } 179 : 180 13856971 : c = ( c>>32 ) + 181 13856971 : ((c>>16) & 0xffffUL) + 182 13856971 : ( c & 0xffffUL); 183 13856971 : c = ( c>>16 ) + 184 13856971 : ( c & 0xffffUL); 185 13856971 : c += ( c>>16 ); 186 : 187 13856971 : return (ushort)~c; 188 13856971 : } 189 : 190 : /* fd_cstr_to_ip4_addr parses an IPv4 address matching format 191 : %u.%u.%u.%u On success stores address to out and returns 1. On fail 192 : returns 0. The given address is returned in network byte order such 193 : that "1.0.0.0" => 0x00000001. */ 194 : 195 : int 196 : fd_cstr_to_ip4_addr( char const * s, 197 : uint * addr ); 198 : 199 : /* fd_ip4_addr_is_public checks if the given IPv4 address is a public address. 200 : assumed to be in net byte order. */ 201 : 202 : FD_FN_CONST static inline int 203 42 : fd_ip4_addr_is_public( uint addr ) { 204 42 : uint addr_host = fd_uint_bswap( addr ); 205 42 : return !((addr_host >= fd_uint_bswap( IP4_PRIVATE_RANGE1_START_NET ) && addr_host <= fd_uint_bswap( IP4_PRIVATE_RANGE1_END_NET )) || 206 42 : (addr_host >= fd_uint_bswap( IP4_PRIVATE_RANGE2_START_NET ) && addr_host <= fd_uint_bswap( IP4_PRIVATE_RANGE2_END_NET )) || 207 42 : (addr_host >= fd_uint_bswap( IP4_PRIVATE_RANGE3_START_NET ) && addr_host <= fd_uint_bswap( IP4_PRIVATE_RANGE3_END_NET )) || 208 42 : (addr_host >= fd_uint_bswap( IP4_LOOPBACK_START_NET ) && addr_host <= fd_uint_bswap( IP4_LOOPBACK_END_NET ))); 209 42 : } 210 : 211 : /* fd_ip4_hdr_bswap reverses the endianness of all fields in the IPv4 212 : header. */ 213 : 214 : static inline void 215 27717935 : fd_ip4_hdr_bswap( fd_ip4_hdr_t * hdr ) { 216 27717935 : hdr->net_tot_len = (ushort)fd_ushort_bswap( hdr->net_tot_len ); 217 27717935 : hdr->net_id = (ushort)fd_ushort_bswap( hdr->net_id ); 218 27717935 : hdr->net_frag_off = (ushort)fd_ushort_bswap( hdr->net_frag_off ); 219 27717935 : hdr->check = (ushort)fd_ushort_bswap( hdr->check ); 220 27717935 : } 221 : 222 : FD_PROTOTYPES_END 223 : 224 : #endif /* HEADER_fd_src_util_net_fd_ip4_h */