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