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 | { |
Benoît Ganne | 89c7b54 | 2023-08-02 11:31:46 +0200 | [diff] [blame] | 52 | const tcp_header_t *tcp; |
| 53 | const udp_header_t *udp = (void *) (ip + 1); |
| 54 | const gtpv1u_header_t *gtpu = (void *) (udp + 1); |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 55 | u64 a, b, c; |
| 56 | u64 t1, t2; |
Takeru Hayasaka | b23c6f4 | 2023-01-17 04:45:58 +0900 | [diff] [blame] | 57 | u32 t3; |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 58 | uword is_tcp_udp = 0; |
| 59 | u8 protocol = ip->protocol; |
Benoît Ganne | 89c7b54 | 2023-08-02 11:31:46 +0200 | [diff] [blame] | 60 | uword is_udp = protocol == IP_PROTOCOL_UDP; |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 61 | |
Benoît Ganne | 89c7b54 | 2023-08-02 11:31:46 +0200 | [diff] [blame] | 62 | if (PREDICT_TRUE ((protocol == IP_PROTOCOL_TCP) || is_udp)) |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 63 | { |
| 64 | is_tcp_udp = 1; |
| 65 | tcp = (void *) (ip + 1); |
| 66 | } |
Benoît Ganne | 89c7b54 | 2023-08-02 11:31:46 +0200 | [diff] [blame] | 67 | else |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 68 | { |
Benoît Ganne | 89c7b54 | 2023-08-02 11:31:46 +0200 | [diff] [blame] | 69 | const void *cur = ip + 1; |
| 70 | if (protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) |
| 71 | { |
| 72 | const ip6_hop_by_hop_header_t *hbh = cur; |
| 73 | protocol = hbh->protocol; |
| 74 | cur += (hbh->length + 1) * 8; |
| 75 | } |
| 76 | if (protocol == IP_PROTOCOL_IPV6_FRAGMENTATION) |
| 77 | { |
| 78 | const ip6_fragment_ext_header_t *frag = cur; |
| 79 | protocol = frag->protocol; |
| 80 | } |
| 81 | else if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_UDP) |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 82 | { |
| 83 | is_tcp_udp = 1; |
Benoît Ganne | 89c7b54 | 2023-08-02 11:31:46 +0200 | [diff] [blame] | 84 | tcp = cur; |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 85 | } |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | t1 = (ip->src_address.as_u64[0] ^ ip->src_address.as_u64[1]); |
| 89 | t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR) ? t1 : 0; |
| 90 | |
| 91 | t2 = (ip->dst_address.as_u64[0] ^ ip->dst_address.as_u64[1]); |
| 92 | t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR) ? t2 : 0; |
| 93 | |
| 94 | a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1; |
| 95 | b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2; |
| 96 | |
| 97 | t1 = is_tcp_udp ? tcp->src : 0; |
| 98 | t2 = is_tcp_udp ? tcp->dst : 0; |
| 99 | |
| 100 | t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0; |
| 101 | t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0; |
| 102 | |
| 103 | if (flow_hash_config & IP_FLOW_HASH_SYMMETRIC) |
| 104 | { |
| 105 | if (b < a) |
| 106 | { |
| 107 | c = a; |
| 108 | a = b; |
| 109 | b = c; |
| 110 | } |
| 111 | if (t2 < t1) |
| 112 | { |
| 113 | t2 += t1; |
| 114 | t1 = t2 - t1; |
| 115 | t2 = t2 - t1; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? protocol : 0; |
Neale Ranns | 3d5f08a | 2021-01-22 16:12:38 +0000 | [diff] [blame] | 120 | c = ((flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? ((t1 << 16) | t2) : |
| 121 | ((t2 << 16) | t1)); |
| 122 | t1 = ((u64) ip_flow_hash_router_id << 32); |
| 123 | t1 |= |
| 124 | ((flow_hash_config & IP_FLOW_HASH_FL) ? ip6_flow_label_network_order (ip) : |
| 125 | 0); |
| 126 | c ^= t1; |
Takeru Hayasaka | b23c6f4 | 2023-01-17 04:45:58 +0900 | [diff] [blame] | 127 | if (PREDICT_TRUE (is_udp) && |
| 128 | PREDICT_FALSE ((flow_hash_config & IP_FLOW_HASH_GTPV1_TEID) && |
| 129 | udp->dst_port == GTPV1_PORT_BE)) |
| 130 | { |
| 131 | t3 = gtpu->teid; |
| 132 | a ^= t3; |
| 133 | } |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 134 | hash_mix64 (a, b, c); |
| 135 | return (u32) c; |
| 136 | } |
| 137 | |
| 138 | /* ip6_locate_header |
| 139 | * |
| 140 | * This function is to search for the header specified by the protocol number |
| 141 | * in find_hdr_type. |
| 142 | * This is used to locate a specific IPv6 extension header |
| 143 | * or to find transport layer header. |
| 144 | * 1. If the find_hdr_type < 0 then it finds and returns the protocol number and |
| 145 | * offset stored in *offset of the transport or ESP header in the chain if |
| 146 | * found. |
| 147 | * 2. If a header with find_hdr_type > 0 protocol number is found then the |
| 148 | * offset is stored in *offset and protocol number of the header is |
| 149 | * returned. |
| 150 | * 3. If find_hdr_type is not found or packet is malformed or |
| 151 | * it is a non-first fragment -1 is returned. |
| 152 | */ |
| 153 | always_inline int |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 154 | ip6_locate_header (vlib_buffer_t *b, ip6_header_t *ip, int find_hdr_type, |
| 155 | u32 *offset) |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 156 | { |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 157 | ip6_ext_hdr_chain_t hdr_chain; |
| 158 | int res = ip6_ext_header_walk (b, ip, find_hdr_type, &hdr_chain); |
| 159 | if (res >= 0) |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 160 | { |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 161 | *offset = hdr_chain.eh[res].offset; |
| 162 | return hdr_chain.eh[res].protocol; |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 163 | } |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 164 | return -1; |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Push IPv6 header to buffer |
| 170 | * |
| 171 | * @param vm - vlib_main |
| 172 | * @param b - buffer to write the header to |
| 173 | * @param src - source IP |
| 174 | * @param dst - destination IP |
| 175 | * @param prot - payload proto |
| 176 | * @param flow_label - flow label |
| 177 | * |
| 178 | * @return - pointer to start of IP header |
| 179 | */ |
| 180 | always_inline void * |
| 181 | vlib_buffer_push_ip6_custom (vlib_main_t * vm, vlib_buffer_t * b, |
| 182 | ip6_address_t * src, ip6_address_t * dst, |
| 183 | int proto, u32 flow_label) |
| 184 | { |
| 185 | ip6_header_t *ip6h; |
| 186 | u16 payload_length; |
| 187 | |
| 188 | /* make some room */ |
| 189 | ip6h = vlib_buffer_push_uninit (b, sizeof (ip6_header_t)); |
| 190 | ASSERT (flow_label < 1 << 20); |
| 191 | ip6h->ip_version_traffic_class_and_flow_label = |
| 192 | clib_host_to_net_u32 ((0x6 << 28) | flow_label); |
| 193 | |
| 194 | /* calculate ip6 payload length */ |
| 195 | payload_length = vlib_buffer_length_in_chain (vm, b); |
| 196 | payload_length -= sizeof (*ip6h); |
| 197 | |
| 198 | ip6h->payload_length = clib_host_to_net_u16 (payload_length); |
| 199 | |
| 200 | ip6h->hop_limit = 0xff; |
| 201 | ip6h->protocol = proto; |
| 202 | clib_memcpy_fast (ip6h->src_address.as_u8, src->as_u8, |
| 203 | sizeof (ip6h->src_address)); |
| 204 | clib_memcpy_fast (ip6h->dst_address.as_u8, dst->as_u8, |
| 205 | sizeof (ip6h->src_address)); |
| 206 | vnet_buffer (b)->l3_hdr_offset = (u8 *) ip6h - b->data; |
| 207 | b->flags |= VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
| 208 | |
| 209 | return ip6h; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Push IPv6 header to buffer |
| 214 | * |
| 215 | * @param vm - vlib_main |
| 216 | * @param b - buffer to write the header to |
| 217 | * @param src - source IP |
| 218 | * @param dst - destination IP |
| 219 | * @param prot - payload proto |
| 220 | * |
| 221 | * @return - pointer to start of IP header |
| 222 | */ |
| 223 | always_inline void * |
| 224 | vlib_buffer_push_ip6 (vlib_main_t * vm, vlib_buffer_t * b, |
| 225 | ip6_address_t * src, ip6_address_t * dst, int proto) |
| 226 | { |
| 227 | return vlib_buffer_push_ip6_custom (vm, b, src, dst, proto, |
| 228 | 0 /* flow label */ ); |
| 229 | |
| 230 | } |
| 231 | |
| 232 | #endif /* included_ip_ip6_h */ |
| 233 | |
| 234 | /* |
| 235 | * fd.io coding-style-patch-verification: ON |
| 236 | * |
| 237 | * Local Variables: |
| 238 | * eval: (c-set-style "gnu") |
| 239 | * End: |
| 240 | */ |