Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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/ip4_forward.c: IP v4 forwarding |
| 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 | #include <vnet/ip-neighbor/ip4_neighbor.h> |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 41 | #include <vnet/ip-neighbor/ip_neighbor.api_enum.h> |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 42 | #include <vnet/ethernet/ethernet.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 43 | #include <vnet/util/throttle.h> |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 44 | #include <vnet/fib/fib_sas.h> |
Ed Warnicke | 9e17887 | 2021-08-14 16:19:43 -0500 | [diff] [blame] | 45 | #include <vnet/ip/ip_sas.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 46 | |
| 47 | /** ARP throttling */ |
| 48 | static throttle_t arp_throttle; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 49 | |
Steven Luong | 19be328 | 2021-03-23 11:55:33 -0700 | [diff] [blame] | 50 | VLIB_REGISTER_LOG_CLASS (ip4_neighbor_log, static) = { |
| 51 | .class_name = "ip4", |
| 52 | .subclass_name = "neighbor", |
| 53 | }; |
| 54 | |
| 55 | #define log_debug(fmt, ...) \ |
| 56 | vlib_log_debug (ip4_neighbor_log.class, fmt, __VA_ARGS__) |
| 57 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 58 | void |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 59 | ip4_neighbor_probe_dst (u32 sw_if_index, u32 thread_index, |
| 60 | const ip4_address_t *dst) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 61 | { |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 62 | ip4_address_t src; |
| 63 | adj_index_t ai; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 64 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 65 | /* any glean will do, it's just for the rewrite */ |
| 66 | ai = adj_glean_get (FIB_PROTOCOL_IP4, sw_if_index, NULL); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 67 | |
Ed Warnicke | 9e17887 | 2021-08-14 16:19:43 -0500 | [diff] [blame] | 68 | if (ADJ_INDEX_INVALID != ai && |
| 69 | (fib_sas4_get (sw_if_index, dst, &src) || |
| 70 | ip4_sas_by_sw_if_index (sw_if_index, dst, &src))) |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 71 | ip4_neighbor_probe (vlib_get_main (), |
| 72 | vnet_get_main (), adj_get (ai), &src, dst); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 76 | ip4_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index, |
| 77 | u32 thread_index, const ip4_address_t *addr) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 78 | { |
| 79 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 80 | ip4_main_t *i4m = &ip4_main; |
| 81 | u8 *rewrite, rewrite_len; |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 82 | ip4_address_t tmp; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 83 | |
| 84 | if (NULL == addr) |
| 85 | { |
Ed Warnicke | 9e17887 | 2021-08-14 16:19:43 -0500 | [diff] [blame] | 86 | if (fib_sas4_get (sw_if_index, NULL, &tmp) || |
| 87 | ip4_sas_by_sw_if_index (sw_if_index, NULL, &tmp)) |
Steven Luong | 18991be | 2021-07-15 08:57:02 -0700 | [diff] [blame] | 88 | addr = &tmp; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (addr) |
| 92 | { |
Steven Luong | 19be328 | 2021-03-23 11:55:33 -0700 | [diff] [blame] | 93 | log_debug ("Sending GARP for IP4 address %U on sw_if_idex %d", |
| 94 | format_ip4_address, addr, sw_if_index); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 95 | |
| 96 | /* Form GARP packet for output - Gratuitous ARP is an ARP request packet |
| 97 | where the interface IP/MAC pair is used for both source and request |
| 98 | MAC/IP pairs in the request */ |
| 99 | u32 bi = 0; |
| 100 | ethernet_arp_header_t *h = vlib_packet_template_get_packet |
| 101 | (vm, &i4m->ip4_arp_request_packet_template, &bi); |
| 102 | |
| 103 | if (!h) |
| 104 | return; |
| 105 | |
| 106 | mac_address_from_bytes (&h->ip4_over_ethernet[0].mac, hi->hw_address); |
| 107 | mac_address_from_bytes (&h->ip4_over_ethernet[1].mac, hi->hw_address); |
| 108 | h->ip4_over_ethernet[0].ip4 = addr[0]; |
| 109 | h->ip4_over_ethernet[1].ip4 = addr[0]; |
| 110 | |
| 111 | /* Setup MAC header with ARP Etype and broadcast DMAC */ |
| 112 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 113 | rewrite = |
| 114 | ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_ARP, |
| 115 | VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST); |
| 116 | rewrite_len = vec_len (rewrite); |
| 117 | vlib_buffer_advance (b, -rewrite_len); |
| 118 | ethernet_header_t *e = vlib_buffer_get_current (b); |
| 119 | clib_memcpy_fast (e->dst_address, rewrite, rewrite_len); |
| 120 | vec_free (rewrite); |
| 121 | |
| 122 | /* Send GARP packet out the specified interface */ |
| 123 | vnet_buffer (b)->sw_if_index[VLIB_RX] = |
| 124 | vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index; |
| 125 | vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index); |
| 126 | u32 *to_next = vlib_frame_vector_args (f); |
| 127 | to_next[0] = bi; |
| 128 | f->n_vectors = 1; |
| 129 | vlib_put_frame_to_node (vm, hi->output_node_index, f); |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 130 | |
| 131 | vlib_increment_simple_counter ( |
| 132 | &ip_neighbor_counters[AF_IP4].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_GRAT], |
| 133 | thread_index, sw_if_index, 1); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | always_inline uword |
| 138 | ip4_arp_inline (vlib_main_t * vm, |
| 139 | vlib_node_runtime_t * node, |
| 140 | vlib_frame_t * frame, int is_glean) |
| 141 | { |
| 142 | vnet_main_t *vnm = vnet_get_main (); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 143 | u32 *from, *to_next_drop; |
| 144 | uword n_left_from, n_left_to_next_drop, next_index; |
| 145 | u32 thread_index = vm->thread_index; |
| 146 | u64 seed; |
| 147 | |
| 148 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 149 | ip4_forward_next_trace (vm, node, frame, VLIB_TX); |
| 150 | |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 151 | seed = throttle_seed (&arp_throttle, thread_index, vlib_time_now (vm)); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 152 | |
| 153 | from = vlib_frame_vector_args (frame); |
| 154 | n_left_from = frame->n_vectors; |
| 155 | next_index = node->cached_next_index; |
| 156 | if (next_index == IP4_ARP_NEXT_DROP) |
| 157 | next_index = IP4_ARP_N_NEXT; /* point to first interface */ |
| 158 | |
| 159 | while (n_left_from > 0) |
| 160 | { |
| 161 | vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP, |
| 162 | to_next_drop, n_left_to_next_drop); |
| 163 | |
| 164 | while (n_left_from > 0 && n_left_to_next_drop > 0) |
| 165 | { |
| 166 | u32 pi0, adj_index0, sw_if_index0; |
| 167 | ip4_address_t resolve0, src0; |
| 168 | vlib_buffer_t *p0, *b0; |
| 169 | ip_adjacency_t *adj0; |
| 170 | u64 r0; |
| 171 | |
| 172 | pi0 = from[0]; |
| 173 | p0 = vlib_get_buffer (vm, pi0); |
| 174 | |
| 175 | from += 1; |
| 176 | n_left_from -= 1; |
| 177 | to_next_drop[0] = pi0; |
| 178 | to_next_drop += 1; |
| 179 | n_left_to_next_drop -= 1; |
| 180 | |
| 181 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 182 | adj0 = adj_get (adj_index0); |
| 183 | sw_if_index0 = adj0->rewrite_header.sw_if_index; |
| 184 | |
| 185 | if (is_glean) |
| 186 | { |
| 187 | /* resolve the packet's destination */ |
| 188 | ip4_header_t *ip0 = vlib_buffer_get_current (p0); |
| 189 | resolve0 = ip0->dst_address; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 190 | } |
| 191 | else |
Neale Ranns | 3952879 | 2023-03-08 04:53:37 +0000 | [diff] [blame] | 192 | /* resolve the incomplete adj */ |
| 193 | resolve0 = adj0->sub_type.nbr.next_hop.ip4; |
| 194 | |
| 195 | if (is_glean && adj0->sub_type.glean.rx_pfx.fp_len) |
| 196 | /* the glean is for a connected, local prefix */ |
| 197 | src0 = adj0->sub_type.glean.rx_pfx.fp_addr.ip4; |
| 198 | else |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 199 | { |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 200 | /* Src IP address in ARP header. */ |
Ed Warnicke | 9e17887 | 2021-08-14 16:19:43 -0500 | [diff] [blame] | 201 | if (!fib_sas4_get (sw_if_index0, &resolve0, &src0) && |
| 202 | !ip4_sas_by_sw_if_index (sw_if_index0, &resolve0, &src0)) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 203 | { |
| 204 | /* No source address available */ |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 205 | p0->error = |
| 206 | node->errors[IP4_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 207 | continue; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* combine the address and interface for the hash key */ |
| 212 | r0 = (u64) resolve0.data_u32 << 32; |
| 213 | r0 |= sw_if_index0; |
| 214 | |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 215 | if (throttle_check (&arp_throttle, thread_index, r0, seed)) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 216 | { |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 217 | p0->error = node->errors[IP4_NEIGHBOR_ERROR_THROTTLED]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 218 | continue; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * the adj has been updated to a rewrite but the node the DPO that got |
| 223 | * us here hasn't - yet. no big deal. we'll drop while we wait. |
| 224 | */ |
| 225 | if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index) |
| 226 | { |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 227 | p0->error = node->errors[IP4_NEIGHBOR_ERROR_RESOLVED]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 228 | continue; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Can happen if the control-plane is programming tables |
| 233 | * with traffic flowing; at least that's today's lame excuse. |
| 234 | */ |
| 235 | if ((is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_GLEAN) |
| 236 | || (!is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP)) |
| 237 | { |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 238 | p0->error = node->errors[IP4_NEIGHBOR_ERROR_NON_ARP_ADJ]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 239 | continue; |
| 240 | } |
| 241 | |
| 242 | /* Send ARP request. */ |
| 243 | b0 = ip4_neighbor_probe (vm, vnm, adj0, &src0, &resolve0); |
| 244 | |
| 245 | if (PREDICT_TRUE (NULL != b0)) |
| 246 | { |
| 247 | /* copy the persistent fields from the original */ |
| 248 | clib_memcpy_fast (b0->opaque2, p0->opaque2, |
| 249 | sizeof (p0->opaque2)); |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 250 | p0->error = node->errors[IP4_NEIGHBOR_ERROR_REQUEST_SENT]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | { |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 254 | p0->error = node->errors[IP4_NEIGHBOR_ERROR_NO_BUFFERS]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 255 | continue; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop); |
| 260 | } |
| 261 | |
| 262 | return frame->n_vectors; |
| 263 | } |
| 264 | |
| 265 | VLIB_NODE_FN (ip4_arp_node) (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 266 | vlib_frame_t * frame) |
| 267 | { |
| 268 | return (ip4_arp_inline (vm, node, frame, 0)); |
| 269 | } |
| 270 | |
| 271 | VLIB_NODE_FN (ip4_glean_node) (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 272 | vlib_frame_t * frame) |
| 273 | { |
| 274 | return (ip4_arp_inline (vm, node, frame, 1)); |
| 275 | } |
| 276 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 277 | VLIB_REGISTER_NODE (ip4_arp_node) = |
| 278 | { |
| 279 | .name = "ip4-arp", |
| 280 | .vector_size = sizeof (u32), |
| 281 | .format_trace = format_ip4_forward_next_trace, |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 282 | .n_errors = IP4_NEIGHBOR_N_ERROR, |
| 283 | .error_counters = ip4_neighbor_error_counters, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 284 | .n_next_nodes = IP4_ARP_N_NEXT, |
| 285 | .next_nodes = { |
| 286 | [IP4_ARP_NEXT_DROP] = "ip4-drop", |
| 287 | }, |
| 288 | }; |
| 289 | |
| 290 | VLIB_REGISTER_NODE (ip4_glean_node) = |
| 291 | { |
| 292 | .name = "ip4-glean", |
| 293 | .vector_size = sizeof (u32), |
| 294 | .format_trace = format_ip4_forward_next_trace, |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 295 | .n_errors = IP4_NEIGHBOR_N_ERROR, |
| 296 | .error_counters = ip4_neighbor_error_counters, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 297 | .n_next_nodes = IP4_ARP_N_NEXT, |
| 298 | .next_nodes = { |
| 299 | [IP4_ARP_NEXT_DROP] = "ip4-drop", |
| 300 | }, |
| 301 | }; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 302 | |
| 303 | #define foreach_notrace_ip4_arp_error \ |
| 304 | _(THROTTLED) \ |
| 305 | _(RESOLVED) \ |
| 306 | _(NO_BUFFERS) \ |
| 307 | _(REQUEST_SENT) \ |
| 308 | _(NON_ARP_ADJ) \ |
| 309 | _(NO_SOURCE_ADDRESS) |
| 310 | |
| 311 | static clib_error_t * |
| 312 | arp_notrace_init (vlib_main_t * vm) |
| 313 | { |
| 314 | vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, ip4_arp_node.index); |
| 315 | |
| 316 | /* don't trace ARP request packets */ |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 317 | #define _(a) \ |
| 318 | vnet_pcap_drop_trace_filter_add_del (rt->errors[IP4_NEIGHBOR_ERROR_##a], \ |
| 319 | 1 /* is_add */); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 320 | foreach_notrace_ip4_arp_error; |
| 321 | #undef _ |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | VLIB_INIT_FUNCTION (arp_notrace_init); |
| 326 | |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 327 | static clib_error_t * |
| 328 | ip4_neighbor_main_loop_enter (vlib_main_t * vm) |
| 329 | { |
| 330 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 331 | u32 n_vlib_mains = tm->n_vlib_mains; |
| 332 | |
Maxime Peim | f6ba562 | 2023-02-06 10:14:20 +0000 | [diff] [blame] | 333 | throttle_init (&arp_throttle, n_vlib_mains, THROTTLE_BITS, 1e-3); |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 334 | |
| 335 | return (NULL); |
| 336 | } |
| 337 | |
| 338 | VLIB_MAIN_LOOP_ENTER_FUNCTION (ip4_neighbor_main_loop_enter); |
| 339 | |
| 340 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 341 | /* |
| 342 | * fd.io coding-style-patch-verification: ON |
| 343 | * |
| 344 | * Local Variables: |
| 345 | * eval: (c-set-style "gnu") |
| 346 | * End: |
| 347 | */ |