Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * ip/ip6.h: ip6 main include file |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #ifndef included_ip_ip6_inlines_h |
| 41 | #define included_ip_ip6_inlines_h |
| 42 | |
| 43 | #include <vnet/ip/ip6_packet.h> |
| 44 | #include <vnet/ip/ip6_hop_by_hop_packet.h> |
| 45 | |
| 46 | /* Compute flow hash. We'll use it to select which Sponge to use for this |
| 47 | flow. And other things. */ |
| 48 | always_inline u32 |
| 49 | ip6_compute_flow_hash (const ip6_header_t * ip, |
| 50 | flow_hash_config_t flow_hash_config) |
| 51 | { |
| 52 | tcp_header_t *tcp; |
| 53 | u64 a, b, c; |
| 54 | u64 t1, t2; |
| 55 | uword is_tcp_udp = 0; |
| 56 | u8 protocol = ip->protocol; |
| 57 | |
| 58 | if (PREDICT_TRUE |
| 59 | ((ip->protocol == IP_PROTOCOL_TCP) |
| 60 | || (ip->protocol == IP_PROTOCOL_UDP))) |
| 61 | { |
| 62 | is_tcp_udp = 1; |
| 63 | tcp = (void *) (ip + 1); |
| 64 | } |
| 65 | else if (ip->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
| 66 | { |
| 67 | ip6_hop_by_hop_header_t *hbh = (ip6_hop_by_hop_header_t *) (ip + 1); |
| 68 | if ((hbh->protocol == IP_PROTOCOL_TCP) || |
| 69 | (hbh->protocol == IP_PROTOCOL_UDP)) |
| 70 | { |
| 71 | is_tcp_udp = 1; |
| 72 | tcp = (tcp_header_t *) ((u8 *) hbh + ((hbh->length + 1) << 3)); |
| 73 | } |
| 74 | protocol = hbh->protocol; |
| 75 | } |
| 76 | |
| 77 | t1 = (ip->src_address.as_u64[0] ^ ip->src_address.as_u64[1]); |
| 78 | t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR) ? t1 : 0; |
| 79 | |
| 80 | t2 = (ip->dst_address.as_u64[0] ^ ip->dst_address.as_u64[1]); |
| 81 | t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR) ? t2 : 0; |
| 82 | |
| 83 | a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1; |
| 84 | b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2; |
| 85 | |
| 86 | t1 = is_tcp_udp ? tcp->src : 0; |
| 87 | t2 = is_tcp_udp ? tcp->dst : 0; |
| 88 | |
| 89 | t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0; |
| 90 | t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0; |
| 91 | |
| 92 | if (flow_hash_config & IP_FLOW_HASH_SYMMETRIC) |
| 93 | { |
| 94 | if (b < a) |
| 95 | { |
| 96 | c = a; |
| 97 | a = b; |
| 98 | b = c; |
| 99 | } |
| 100 | if (t2 < t1) |
| 101 | { |
| 102 | t2 += t1; |
| 103 | t1 = t2 - t1; |
| 104 | t2 = t2 - t1; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? protocol : 0; |
| 109 | c = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? |
| 110 | ((t1 << 16) | t2) : ((t2 << 16) | t1); |
| 111 | |
| 112 | hash_mix64 (a, b, c); |
| 113 | return (u32) c; |
| 114 | } |
| 115 | |
| 116 | /* ip6_locate_header |
| 117 | * |
| 118 | * This function is to search for the header specified by the protocol number |
| 119 | * in find_hdr_type. |
| 120 | * This is used to locate a specific IPv6 extension header |
| 121 | * or to find transport layer header. |
| 122 | * 1. If the find_hdr_type < 0 then it finds and returns the protocol number and |
| 123 | * offset stored in *offset of the transport or ESP header in the chain if |
| 124 | * found. |
| 125 | * 2. If a header with find_hdr_type > 0 protocol number is found then the |
| 126 | * offset is stored in *offset and protocol number of the header is |
| 127 | * returned. |
| 128 | * 3. If find_hdr_type is not found or packet is malformed or |
| 129 | * it is a non-first fragment -1 is returned. |
| 130 | */ |
| 131 | always_inline int |
| 132 | ip6_locate_header (vlib_buffer_t * p0, |
| 133 | ip6_header_t * ip0, int find_hdr_type, u32 * offset) |
| 134 | { |
| 135 | u8 next_proto = ip0->protocol; |
| 136 | u8 *next_header; |
| 137 | u8 done = 0; |
| 138 | u32 cur_offset; |
| 139 | u8 *temp_nxthdr = 0; |
| 140 | u32 exthdr_len = 0; |
| 141 | |
| 142 | next_header = ip6_next_header (ip0); |
| 143 | cur_offset = sizeof (ip6_header_t); |
| 144 | while (1) |
| 145 | { |
| 146 | done = (next_proto == find_hdr_type); |
| 147 | if (PREDICT_FALSE |
| 148 | (next_header >= |
| 149 | (u8 *) vlib_buffer_get_current (p0) + p0->current_length)) |
| 150 | { |
| 151 | //A malicious packet could set an extension header with a too big size |
| 152 | return (-1); |
| 153 | } |
| 154 | if (done) |
| 155 | break; |
| 156 | if ((!ip6_ext_hdr (next_proto)) || next_proto == IP_PROTOCOL_IP6_NONXT) |
| 157 | { |
| 158 | if (find_hdr_type < 0) |
| 159 | break; |
| 160 | return -1; |
| 161 | } |
| 162 | if (next_proto == IP_PROTOCOL_IPV6_FRAGMENTATION) |
| 163 | { |
| 164 | ip6_frag_hdr_t *frag_hdr = (ip6_frag_hdr_t *) next_header; |
| 165 | u16 frag_off = ip6_frag_hdr_offset (frag_hdr); |
| 166 | /* Non first fragment return -1 */ |
| 167 | if (frag_off) |
| 168 | return (-1); |
| 169 | exthdr_len = sizeof (ip6_frag_hdr_t); |
| 170 | temp_nxthdr = next_header + exthdr_len; |
| 171 | } |
| 172 | else if (next_proto == IP_PROTOCOL_IPSEC_AH) |
| 173 | { |
| 174 | exthdr_len = |
| 175 | ip6_ext_authhdr_len (((ip6_ext_header_t *) next_header)); |
| 176 | temp_nxthdr = next_header + exthdr_len; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | exthdr_len = |
| 181 | ip6_ext_header_len (((ip6_ext_header_t *) next_header)); |
| 182 | temp_nxthdr = next_header + exthdr_len; |
| 183 | } |
| 184 | next_proto = ((ip6_ext_header_t *) next_header)->next_hdr; |
| 185 | next_header = temp_nxthdr; |
| 186 | cur_offset += exthdr_len; |
| 187 | } |
| 188 | |
| 189 | *offset = cur_offset; |
| 190 | return (next_proto); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Push IPv6 header to buffer |
| 196 | * |
| 197 | * @param vm - vlib_main |
| 198 | * @param b - buffer to write the header to |
| 199 | * @param src - source IP |
| 200 | * @param dst - destination IP |
| 201 | * @param prot - payload proto |
| 202 | * @param flow_label - flow label |
| 203 | * |
| 204 | * @return - pointer to start of IP header |
| 205 | */ |
| 206 | always_inline void * |
| 207 | vlib_buffer_push_ip6_custom (vlib_main_t * vm, vlib_buffer_t * b, |
| 208 | ip6_address_t * src, ip6_address_t * dst, |
| 209 | int proto, u32 flow_label) |
| 210 | { |
| 211 | ip6_header_t *ip6h; |
| 212 | u16 payload_length; |
| 213 | |
| 214 | /* make some room */ |
| 215 | ip6h = vlib_buffer_push_uninit (b, sizeof (ip6_header_t)); |
| 216 | ASSERT (flow_label < 1 << 20); |
| 217 | ip6h->ip_version_traffic_class_and_flow_label = |
| 218 | clib_host_to_net_u32 ((0x6 << 28) | flow_label); |
| 219 | |
| 220 | /* calculate ip6 payload length */ |
| 221 | payload_length = vlib_buffer_length_in_chain (vm, b); |
| 222 | payload_length -= sizeof (*ip6h); |
| 223 | |
| 224 | ip6h->payload_length = clib_host_to_net_u16 (payload_length); |
| 225 | |
| 226 | ip6h->hop_limit = 0xff; |
| 227 | ip6h->protocol = proto; |
| 228 | clib_memcpy_fast (ip6h->src_address.as_u8, src->as_u8, |
| 229 | sizeof (ip6h->src_address)); |
| 230 | clib_memcpy_fast (ip6h->dst_address.as_u8, dst->as_u8, |
| 231 | sizeof (ip6h->src_address)); |
| 232 | vnet_buffer (b)->l3_hdr_offset = (u8 *) ip6h - b->data; |
| 233 | b->flags |= VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
| 234 | |
| 235 | return ip6h; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Push IPv6 header to buffer |
| 240 | * |
| 241 | * @param vm - vlib_main |
| 242 | * @param b - buffer to write the header to |
| 243 | * @param src - source IP |
| 244 | * @param dst - destination IP |
| 245 | * @param prot - payload proto |
| 246 | * |
| 247 | * @return - pointer to start of IP header |
| 248 | */ |
| 249 | always_inline void * |
| 250 | vlib_buffer_push_ip6 (vlib_main_t * vm, vlib_buffer_t * b, |
| 251 | ip6_address_t * src, ip6_address_t * dst, int proto) |
| 252 | { |
| 253 | return vlib_buffer_push_ip6_custom (vm, b, src, dst, proto, |
| 254 | 0 /* flow label */ ); |
| 255 | |
| 256 | } |
| 257 | |
| 258 | #endif /* included_ip_ip6_h */ |
| 259 | |
| 260 | /* |
| 261 | * fd.io coding-style-patch-verification: ON |
| 262 | * |
| 263 | * Local Variables: |
| 264 | * eval: (c-set-style "gnu") |
| 265 | * End: |
| 266 | */ |