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