Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ip/ip6_neighbor.c: IP6 neighbor handling |
| 3 | * |
| 4 | * Copyright (c) 2010 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/ip-neighbor/ip6_neighbor.h> |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 19 | #include <vnet/ip-neighbor/ip_neighbor.api_enum.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 20 | #include <vnet/util/throttle.h> |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 21 | #include <vnet/fib/fib_sas.h> |
Ed Warnicke | 9e17887 | 2021-08-14 16:19:43 -0500 | [diff] [blame] | 22 | #include <vnet/ip/ip_sas.h> |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 23 | |
| 24 | /** ND throttling */ |
| 25 | static throttle_t nd_throttle; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 26 | |
Steven Luong | 19be328 | 2021-03-23 11:55:33 -0700 | [diff] [blame] | 27 | VLIB_REGISTER_LOG_CLASS (ip6_neighbor_log, static) = { |
| 28 | .class_name = "ip6", |
| 29 | .subclass_name = "neighbor", |
| 30 | }; |
| 31 | |
| 32 | #define log_debug(fmt, ...) \ |
| 33 | vlib_log_debug (ip6_neighbor_log.class, fmt, __VA_ARGS__) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 34 | void |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 35 | ip6_neighbor_probe_dst (u32 sw_if_index, u32 thread_index, |
| 36 | const ip6_address_t *dst) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 37 | { |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 38 | ip6_address_t src; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 39 | |
Ed Warnicke | 9e17887 | 2021-08-14 16:19:43 -0500 | [diff] [blame] | 40 | if (fib_sas6_get (sw_if_index, dst, &src) || |
| 41 | ip6_sas_by_sw_if_index (sw_if_index, dst, &src)) |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 42 | ip6_neighbor_probe (vlib_get_main (), vnet_get_main (), sw_if_index, |
| 43 | thread_index, &src, dst); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 47 | ip6_neighbor_advertise (vlib_main_t *vm, vnet_main_t *vnm, u32 sw_if_index, |
| 48 | u32 thread_index, const ip6_address_t *addr) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 49 | { |
| 50 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 51 | ip6_main_t *i6m = &ip6_main; |
| 52 | u8 *rewrite, rewrite_len; |
| 53 | u8 dst_address[6]; |
| 54 | |
| 55 | if (NULL == addr) |
| 56 | addr = ip6_interface_first_address (i6m, sw_if_index); |
| 57 | |
| 58 | if (addr) |
| 59 | { |
Steven Luong | 19be328 | 2021-03-23 11:55:33 -0700 | [diff] [blame] | 60 | log_debug ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d", |
| 61 | format_ip6_address, addr, sw_if_index); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 62 | |
| 63 | /* Form unsolicited neighbor advertisement packet from NS pkt template */ |
| 64 | int bogus_length; |
| 65 | u32 bi = 0; |
| 66 | icmp6_neighbor_solicitation_header_t *h = |
| 67 | vlib_packet_template_get_packet (vm, |
| 68 | &ip6_neighbor_packet_template, |
| 69 | &bi); |
| 70 | if (!h) |
| 71 | return; |
| 72 | |
| 73 | ip6_set_reserved_multicast_address (&h->ip.dst_address, |
| 74 | IP6_MULTICAST_SCOPE_link_local, |
| 75 | IP6_MULTICAST_GROUP_ID_all_hosts); |
| 76 | h->ip.src_address = addr[0]; |
| 77 | h->neighbor.icmp.type = ICMP6_neighbor_advertisement; |
| 78 | h->neighbor.target_address = addr[0]; |
| 79 | h->neighbor.advertisement_flags = clib_host_to_net_u32 |
| 80 | (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE); |
| 81 | h->link_layer_option.header.type = |
| 82 | ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address; |
| 83 | clib_memcpy (h->link_layer_option.ethernet_address, |
| 84 | hi->hw_address, vec_len (hi->hw_address)); |
| 85 | h->neighbor.icmp.checksum = |
| 86 | ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length); |
| 87 | ASSERT (bogus_length == 0); |
| 88 | |
| 89 | /* Setup MAC header with IP6 Etype and mcast DMAC */ |
| 90 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 91 | ip6_multicast_ethernet_address (dst_address, |
| 92 | IP6_MULTICAST_GROUP_ID_all_hosts); |
| 93 | rewrite = |
| 94 | ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address); |
| 95 | rewrite_len = vec_len (rewrite); |
| 96 | vlib_buffer_advance (b, -rewrite_len); |
| 97 | ethernet_header_t *e = vlib_buffer_get_current (b); |
| 98 | clib_memcpy (e->dst_address, rewrite, rewrite_len); |
| 99 | vec_free (rewrite); |
| 100 | |
| 101 | /* Send unsolicited ND advertisement packet out the specified interface */ |
| 102 | vnet_buffer (b)->sw_if_index[VLIB_RX] = |
| 103 | vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index; |
| 104 | vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index); |
| 105 | u32 *to_next = vlib_frame_vector_args (f); |
| 106 | to_next[0] = bi; |
| 107 | f->n_vectors = 1; |
| 108 | vlib_put_frame_to_node (vm, hi->output_node_index, f); |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 109 | |
| 110 | vlib_increment_simple_counter ( |
| 111 | &ip_neighbor_counters[AF_IP6].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_GRAT], |
| 112 | thread_index, sw_if_index, 1); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | typedef enum |
| 117 | { |
| 118 | IP6_NBR_NEXT_DROP, |
| 119 | IP6_NBR_NEXT_REPLY_TX, |
| 120 | IP6_NBR_N_NEXT, |
| 121 | } ip6_discover_neighbor_next_t; |
| 122 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 123 | static uword |
| 124 | ip6_discover_neighbor_inline (vlib_main_t * vm, |
| 125 | vlib_node_runtime_t * node, |
| 126 | vlib_frame_t * frame, int is_glean) |
| 127 | { |
| 128 | vnet_main_t *vnm = vnet_get_main (); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 129 | u32 *from, *to_next_drop; |
| 130 | uword n_left_from, n_left_to_next_drop; |
| 131 | u64 seed; |
| 132 | u32 thread_index = vm->thread_index; |
| 133 | |
| 134 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 135 | ip6_forward_next_trace (vm, node, frame, VLIB_TX); |
| 136 | |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 137 | seed = throttle_seed (&nd_throttle, thread_index, vlib_time_now (vm)); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 138 | |
| 139 | from = vlib_frame_vector_args (frame); |
| 140 | n_left_from = frame->n_vectors; |
| 141 | |
| 142 | while (n_left_from > 0) |
| 143 | { |
| 144 | vlib_get_next_frame (vm, node, IP6_NBR_NEXT_DROP, |
| 145 | to_next_drop, n_left_to_next_drop); |
| 146 | |
| 147 | while (n_left_from > 0 && n_left_to_next_drop > 0) |
| 148 | { |
| 149 | u32 pi0, adj_index0, sw_if_index0, drop0, r0; |
| 150 | vnet_hw_interface_t *hw_if0; |
| 151 | vlib_buffer_t *p0, *b0; |
| 152 | ip_adjacency_t *adj0; |
| 153 | ip6_address_t src; |
| 154 | ip6_header_t *ip0; |
| 155 | |
| 156 | pi0 = from[0]; |
| 157 | |
| 158 | p0 = vlib_get_buffer (vm, pi0); |
| 159 | |
| 160 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 161 | |
| 162 | ip0 = vlib_buffer_get_current (p0); |
| 163 | |
| 164 | adj0 = adj_get (adj_index0); |
| 165 | |
| 166 | if (!is_glean) |
| 167 | { |
| 168 | ip0->dst_address.as_u64[0] = |
| 169 | adj0->sub_type.nbr.next_hop.ip6.as_u64[0]; |
| 170 | ip0->dst_address.as_u64[1] = |
| 171 | adj0->sub_type.nbr.next_hop.ip6.as_u64[1]; |
| 172 | } |
| 173 | |
| 174 | sw_if_index0 = adj0->rewrite_header.sw_if_index; |
| 175 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 176 | |
| 177 | /* combine the address and interface for a hash */ |
| 178 | r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0; |
| 179 | |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 180 | drop0 = throttle_check (&nd_throttle, thread_index, r0, seed); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 181 | |
| 182 | from += 1; |
| 183 | n_left_from -= 1; |
| 184 | to_next_drop[0] = pi0; |
| 185 | to_next_drop += 1; |
| 186 | n_left_to_next_drop -= 1; |
| 187 | |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 188 | if (drop0) |
| 189 | { |
| 190 | p0->error = node->errors[IP6_NEIGHBOR_ERROR_THROTTLED]; |
| 191 | continue; |
| 192 | } |
| 193 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 194 | hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0); |
| 195 | |
| 196 | /* If the interface is link-down, drop the pkt */ |
| 197 | if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)) |
| 198 | drop0 = 1; |
| 199 | |
| 200 | if (!ip6_link_is_enabled (sw_if_index0)) |
| 201 | drop0 = 1; |
| 202 | |
| 203 | /* |
| 204 | * the adj has been updated to a rewrite but the node the DPO that got |
| 205 | * us here hasn't - yet. no big deal. we'll drop while we wait. |
| 206 | */ |
| 207 | if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index) |
| 208 | drop0 = 1; |
| 209 | |
| 210 | if (drop0) |
| 211 | { |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 212 | p0->error = node->errors[IP6_NEIGHBOR_ERROR_DROP]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 213 | continue; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Choose source address based on destination lookup |
| 218 | * adjacency. |
| 219 | */ |
Ed Warnicke | 312b4cd | 2021-10-22 19:51:22 -0500 | [diff] [blame] | 220 | if (!fib_sas6_get (sw_if_index0, &ip0->dst_address, &src) || |
Ed Warnicke | 9e17887 | 2021-08-14 16:19:43 -0500 | [diff] [blame] | 221 | !ip6_sas_by_sw_if_index (sw_if_index0, &ip0->dst_address, &src)) |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 222 | { |
| 223 | /* There is no address on the interface */ |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 224 | p0->error = node->errors[IP6_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 225 | continue; |
| 226 | } |
| 227 | |
Neale Ranns | fd2417b | 2021-07-16 14:00:16 +0000 | [diff] [blame] | 228 | b0 = ip6_neighbor_probe (vm, vnm, sw_if_index0, thread_index, &src, |
| 229 | &ip0->dst_address); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 230 | |
| 231 | if (PREDICT_TRUE (NULL != b0)) |
| 232 | { |
| 233 | clib_memcpy_fast (b0->opaque2, p0->opaque2, |
| 234 | sizeof (p0->opaque2)); |
| 235 | b0->flags |= p0->flags & VLIB_BUFFER_IS_TRACED; |
| 236 | b0->trace_handle = p0->trace_handle; |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 237 | p0->error = node->errors[IP6_NEIGHBOR_ERROR_REQUEST_SENT]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 238 | } |
| 239 | else |
| 240 | { |
| 241 | /* There is no address on the interface */ |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 242 | p0->error = node->errors[IP6_NEIGHBOR_ERROR_NO_BUFFERS]; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 243 | continue; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | vlib_put_next_frame (vm, node, IP6_NBR_NEXT_DROP, n_left_to_next_drop); |
| 248 | } |
| 249 | |
| 250 | return frame->n_vectors; |
| 251 | } |
| 252 | |
| 253 | static uword |
| 254 | ip6_discover_neighbor (vlib_main_t * vm, |
| 255 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 256 | { |
| 257 | return (ip6_discover_neighbor_inline (vm, node, frame, 0)); |
| 258 | } |
| 259 | |
| 260 | static uword |
| 261 | ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 262 | { |
| 263 | return (ip6_discover_neighbor_inline (vm, node, frame, 1)); |
| 264 | } |
| 265 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 266 | /* *INDENT-OFF* */ |
| 267 | VLIB_REGISTER_NODE (ip6_glean_node) = |
| 268 | { |
| 269 | .function = ip6_glean, |
| 270 | .name = "ip6-glean", |
| 271 | .vector_size = sizeof (u32), |
| 272 | .format_trace = format_ip6_forward_next_trace, |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 273 | .n_errors = IP6_NEIGHBOR_N_ERROR, |
| 274 | .error_counters = ip6_neighbor_error_counters, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 275 | .n_next_nodes = IP6_NBR_N_NEXT, |
| 276 | .next_nodes = |
| 277 | { |
| 278 | [IP6_NBR_NEXT_DROP] = "ip6-drop", |
| 279 | [IP6_NBR_NEXT_REPLY_TX] = "ip6-rewrite-mcast", |
| 280 | }, |
| 281 | }; |
| 282 | VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = |
| 283 | { |
| 284 | .function = ip6_discover_neighbor, |
| 285 | .name = "ip6-discover-neighbor", |
| 286 | .vector_size = sizeof (u32), |
| 287 | .format_trace = format_ip6_forward_next_trace, |
Neale Ranns | 6e4a56e | 2022-08-13 10:58:11 +0000 | [diff] [blame] | 288 | .n_errors = IP6_NEIGHBOR_N_ERROR, |
| 289 | .error_counters = ip6_neighbor_error_counters, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 290 | .n_next_nodes = IP6_NBR_N_NEXT, |
| 291 | .next_nodes = |
| 292 | { |
| 293 | [IP6_NBR_NEXT_DROP] = "ip6-drop", |
| 294 | [IP6_NBR_NEXT_REPLY_TX] = "ip6-rewrite-mcast", |
| 295 | }, |
| 296 | }; |
| 297 | /* *INDENT-ON* */ |
| 298 | |
| 299 | /* Template used to generate IP6 neighbor solicitation packets. */ |
| 300 | vlib_packet_template_t ip6_neighbor_packet_template; |
| 301 | |
| 302 | static clib_error_t * |
| 303 | ip6_neighbor_init (vlib_main_t * vm) |
| 304 | { |
| 305 | icmp6_neighbor_solicitation_header_t p; |
| 306 | |
| 307 | clib_memset (&p, 0, sizeof (p)); |
| 308 | |
| 309 | p.ip.ip_version_traffic_class_and_flow_label = |
| 310 | clib_host_to_net_u32 (0x6 << 28); |
| 311 | p.ip.payload_length = |
| 312 | clib_host_to_net_u16 (sizeof (p) - |
| 313 | STRUCT_OFFSET_OF |
| 314 | (icmp6_neighbor_solicitation_header_t, neighbor)); |
| 315 | p.ip.protocol = IP_PROTOCOL_ICMP6; |
| 316 | p.ip.hop_limit = 255; |
| 317 | ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0); |
| 318 | |
| 319 | p.neighbor.icmp.type = ICMP6_neighbor_solicitation; |
| 320 | |
| 321 | p.link_layer_option.header.type = |
| 322 | ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address; |
| 323 | p.link_layer_option.header.n_data_u64s = |
| 324 | sizeof (p.link_layer_option) / sizeof (u64); |
| 325 | |
| 326 | vlib_packet_template_init (vm, |
| 327 | &ip6_neighbor_packet_template, &p, sizeof (p), |
| 328 | /* alloc chunk size */ 8, |
| 329 | "ip6 neighbor discovery"); |
| 330 | |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | VLIB_INIT_FUNCTION (ip6_neighbor_init); |
| 335 | |
Neale Ranns | e403113 | 2020-10-26 13:00:06 +0000 | [diff] [blame] | 336 | static clib_error_t * |
| 337 | ip6_nd_main_loop_enter (vlib_main_t * vm) |
| 338 | { |
| 339 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 340 | |
| 341 | throttle_init (&nd_throttle, tm->n_vlib_mains, 1e-3); |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | VLIB_MAIN_LOOP_ENTER_FUNCTION (ip6_nd_main_loop_enter); |
| 347 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 348 | /* |
| 349 | * fd.io coding-style-patch-verification: ON |
| 350 | * |
| 351 | * Local Variables: |
| 352 | * eval: (c-set-style "gnu") |
| 353 | * End: |
| 354 | */ |