blob: 252bdf975be695facf1602de442493ad165bc651 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
AkshayaNadahallied4a2fd2016-08-09 13:38:04 +05302 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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_forward.c: IP v6 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/vnet.h>
41#include <vnet/ip/ip.h>
Ole Troan313f7e22018-04-10 16:02:51 +020042#include <vnet/ip/ip_frag.h>
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +020043#include <vnet/ip/ip6_neighbor.h>
Dave Barachd7cb1b52016-12-09 09:52:16 -050044#include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
Ed Warnickecb9cada2015-12-08 15:45:58 -070045#include <vnet/srp/srp.h> /* for srp_hw_interface_class */
46#include <vppinfra/cache.h>
AkshayaNadahalli0f438df2017-02-10 10:54:16 +053047#include <vnet/fib/fib_urpf_list.h> /* for FIB uRPF check */
Neale Ranns0bfe5d82016-08-25 15:29:12 +010048#include <vnet/fib/ip6_fib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000049#include <vnet/mfib/ip6_mfib.h>
Neale Rannsf12a83f2017-04-18 09:09:40 -070050#include <vnet/dpo/load_balance_map.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010051#include <vnet/dpo/classify_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Damjan Marion38173502019-02-13 19:30:09 +010053#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -070054#include <vppinfra/bihash_template.c>
Damjan Marion38173502019-02-13 19:30:09 +010055#endif
Vijayabhaskar Katamreddyacbde662018-01-23 13:39:40 -080056#include <vnet/ip/ip6_forward.h>
Neale Ranns25edf142019-03-22 08:12:48 +000057#include <vnet/interface_output.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
AkshayaNadahallifdd81af2016-12-01 16:33:51 +053059/* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
60#define OI_DECAP 0x80000000
61
Ed Warnickecb9cada2015-12-08 15:45:58 -070062static void
Matthew Smith6c92f5b2019-08-07 11:46:30 -050063ip6_add_interface_prefix_routes (ip6_main_t * im,
64 u32 sw_if_index,
65 u32 fib_index,
66 ip6_address_t * address, u32 address_length)
67{
68 ip_lookup_main_t *lm = &im->lookup_main;
69 ip_interface_prefix_t *if_prefix;
70
71 ip_interface_prefix_key_t key = {
72 .prefix = {
73 .fp_len = address_length,
74 .fp_proto = FIB_PROTOCOL_IP6,
75 .fp_addr.ip6 = {
76 .as_u64 = {
77 address->as_u64[0] &
78 im->fib_masks[address_length].
79 as_u64[0],
80 address->
81 as_u64[1] &
82 im->fib_masks[address_length].
83 as_u64[1],
84 },
85 },
86 },
87 .sw_if_index = sw_if_index,
88 };
89
90 /* If prefix already set on interface, just increment ref count & return */
91 if_prefix = ip_get_interface_prefix (lm, &key);
92 if (if_prefix)
93 {
94 if_prefix->ref_count += 1;
95 return;
96 }
97
98 /* New prefix - allocate a pool entry, initialize it, add to the hash */
99 pool_get (lm->if_prefix_pool, if_prefix);
100 if_prefix->ref_count = 1;
101 clib_memcpy (&if_prefix->key, &key, sizeof (key));
102 mhash_set (&lm->prefix_to_if_prefix_index, &key,
103 if_prefix - lm->if_prefix_pool, 0 /* old value */ );
104
105 /* length < 128 - add glean */
106 if (address_length < 128)
107 {
108 /* set the glean route for the prefix */
109 fib_table_entry_update_one_path (fib_index, &key.prefix,
110 FIB_SOURCE_INTERFACE,
111 (FIB_ENTRY_FLAG_CONNECTED |
112 FIB_ENTRY_FLAG_ATTACHED),
113 DPO_PROTO_IP6,
114 /* No next-hop address */
115 NULL, sw_if_index,
116 /* invalid FIB index */
117 ~0, 1,
118 /* no out-label stack */
119 NULL, FIB_ROUTE_PATH_FLAG_NONE);
120 }
121}
122
123static void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
125 ip6_main_t * im, u32 fib_index,
126 ip_interface_address_t * a)
127{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500128 ip_lookup_main_t *lm = &im->lookup_main;
129 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100130 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500131 .fp_len = a->address_length,
132 .fp_proto = FIB_PROTOCOL_IP6,
133 .fp_addr.ip6 = *address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100134 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500136 /* set special routes for the prefix if needed */
137 ip6_add_interface_prefix_routes (im, sw_if_index, fib_index,
138 address, a->address_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100140 pfx.fp_len = 128;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500142 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100143 u32 classify_table_index =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500144 lm->classify_table_index_by_sw_if_index[sw_if_index];
145 if (classify_table_index != (u32) ~ 0)
146 {
147 dpo_id_t dpo = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100148
Dave Barachd7cb1b52016-12-09 09:52:16 -0500149 dpo_set (&dpo,
150 DPO_CLASSIFY,
151 DPO_PROTO_IP6,
152 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153
Dave Barachd7cb1b52016-12-09 09:52:16 -0500154 fib_table_entry_special_dpo_add (fib_index,
155 &pfx,
156 FIB_SOURCE_CLASSIFY,
157 FIB_ENTRY_FLAG_NONE, &dpo);
158 dpo_reset (&dpo);
159 }
160 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100161
Neale Rannsf12a83f2017-04-18 09:09:40 -0700162 fib_table_entry_update_one_path (fib_index, &pfx,
163 FIB_SOURCE_INTERFACE,
164 (FIB_ENTRY_FLAG_CONNECTED |
165 FIB_ENTRY_FLAG_LOCAL),
Neale Rannsda78f952017-05-24 09:15:43 -0700166 DPO_PROTO_IP6,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700167 &pfx.fp_addr,
168 sw_if_index, ~0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500169 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170}
171
172static void
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500173ip6_del_interface_prefix_routes (ip6_main_t * im,
174 u32 sw_if_index,
175 u32 fib_index,
176 ip6_address_t * address, u32 address_length)
177{
178 ip_lookup_main_t *lm = &im->lookup_main;
179 ip_interface_prefix_t *if_prefix;
180
181 ip_interface_prefix_key_t key = {
182 .prefix = {
183 .fp_len = address_length,
184 .fp_proto = FIB_PROTOCOL_IP6,
185 .fp_addr.ip6 = {
186 .as_u64 = {
187 address->as_u64[0] &
188 im->fib_masks[address_length].
189 as_u64[0],
190 address->
191 as_u64[1] &
192 im->fib_masks[address_length].
193 as_u64[1],
194 },
195 },
196 },
197 .sw_if_index = sw_if_index,
198 };
199
200 if_prefix = ip_get_interface_prefix (lm, &key);
201 if (!if_prefix)
202 {
203 clib_warning ("Prefix not found while deleting %U",
204 format_ip4_address_and_length, address, address_length);
205 return;
206 }
207
208 /* If not deleting last intf addr in prefix, decrement ref count & return */
209 if_prefix->ref_count -= 1;
210 if (if_prefix->ref_count > 0)
211 return;
212
213 /* length <= 30, delete glean route */
214 if (address_length <= 128)
215 {
216 /* remove glean route for prefix */
217 fib_table_entry_delete (fib_index, &key.prefix, FIB_SOURCE_INTERFACE);
218
219 }
220
221 mhash_unset (&lm->prefix_to_if_prefix_index, &key, 0 /* old_value */ );
222 pool_put (lm->if_prefix_pool, if_prefix);
223}
224
225static void
226ip6_del_interface_routes (u32 sw_if_index, ip6_main_t * im,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100227 u32 fib_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228 ip6_address_t * address, u32 address_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500230 fib_prefix_t pfx = {
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500231 .fp_len = 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500232 .fp_proto = FIB_PROTOCOL_IP6,
233 .fp_addr.ip6 = *address,
234 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500236 /* delete special routes for the prefix if needed */
237 ip6_del_interface_prefix_routes (im, sw_if_index, fib_index,
238 address, address_length);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100239
Dave Barachd7cb1b52016-12-09 09:52:16 -0500240 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241}
242
Damjan Marion38173502019-02-13 19:30:09 +0100243#ifndef CLIB_MARCH_VARIANT
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500245ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100246{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500247 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100251 /*
252 * enable/disable only on the 1<->0 transition
253 */
254 if (is_enable)
255 {
256 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500257 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100258 }
259 else
260 {
Neale Ranns75152282017-01-09 01:00:45 -0800261 /* The ref count is 0 when an address is removed from an interface that has
262 * no address - this is not a ciritical error */
263 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
264 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500265 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100266 }
267
Neale Ranns8269d3d2018-01-30 09:02:20 -0800268 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
Neale Ranns630198f2017-05-22 09:20:20 -0400269 !is_enable, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270
Neale Ranns8269d3d2018-01-30 09:02:20 -0800271 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
272 sw_if_index, !is_enable, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100273}
274
Neale Rannsdf089a82016-10-02 16:39:06 +0100275/* get first interface address */
276ip6_address_t *
Neale Ranns6cfc39c2017-02-14 01:44:25 -0800277ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
Neale Rannsdf089a82016-10-02 16:39:06 +0100278{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500279 ip_lookup_main_t *lm = &im->lookup_main;
280 ip_interface_address_t *ia = 0;
281 ip6_address_t *result = 0;
Neale Rannsdf089a82016-10-02 16:39:06 +0100282
Dave Barachd7cb1b52016-12-09 09:52:16 -0500283 /* *INDENT-OFF* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100284 foreach_ip_interface_address (lm, ia, sw_if_index,
285 1 /* honor unnumbered */,
286 ({
287 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
288 result = a;
289 break;
290 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500291 /* *INDENT-ON* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100292 return result;
293}
294
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100295clib_error_t *
296ip6_add_del_interface_address (vlib_main_t * vm,
297 u32 sw_if_index,
298 ip6_address_t * address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 u32 address_length, u32 is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500301 vnet_main_t *vnm = vnet_get_main ();
302 ip6_main_t *im = &ip6_main;
303 ip_lookup_main_t *lm = &im->lookup_main;
304 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305 u32 if_address_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500306 ip6_address_fib_t ip6_af, *addr_fib = 0;
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200307 ip6_address_t ll_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Pavel Kotucek57808982017-08-02 08:20:19 +0200309 /* local0 interface doesn't support IP addressing */
310 if (sw_if_index == 0)
311 {
312 return
313 clib_error_create ("local0 interface doesn't support IP addressing");
314 }
315
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200316 if (ip6_address_is_link_local_unicast (address))
317 {
318 if (address_length != 128)
319 {
320 vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
321 return
322 clib_error_create
323 ("prefix length of link-local address must be 128");
324 }
325 if (!is_del)
326 {
327 return ip6_neighbor_set_link_local_address (vm, sw_if_index,
328 address);
329 }
330 else
331 {
332 ll_addr = ip6_neighbor_get_link_local_address (sw_if_index);
333 if (ip6_address_is_equal (&ll_addr, address))
334 {
335 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_DELETABLE;
336 return clib_error_create ("address not deletable");
337 }
338 else
339 {
340 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
341 return clib_error_create ("address not found");
342 }
343 }
344 }
345
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000347 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
348
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349 ip6_addr_fib_init (&ip6_af, address,
350 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
351 vec_add1 (addr_fib, ip6_af);
352
Neale Ranns744902e2017-08-14 10:35:44 -0700353 /* *INDENT-OFF* */
354 if (!is_del)
355 {
356 /* When adding an address check that it does not conflict
357 with an existing address on any interface in this table. */
358 ip_interface_address_t *ia;
359 vnet_sw_interface_t *sif;
360
361 pool_foreach(sif, vnm->interface_main.sw_interfaces,
362 ({
363 if (im->fib_index_by_sw_if_index[sw_if_index] ==
364 im->fib_index_by_sw_if_index[sif->sw_if_index])
365 {
366 foreach_ip_interface_address
367 (&im->lookup_main, ia, sif->sw_if_index,
368 0 /* honor unnumbered */ ,
369 ({
370 ip6_address_t * x =
371 ip_interface_address_get_address
372 (&im->lookup_main, ia);
373 if (ip6_destination_matches_route
374 (im, address, x, ia->address_length) ||
375 ip6_destination_matches_route (im,
376 x,
377 address,
378 address_length))
379 {
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500380 /* an intf may have >1 addr from the same prefix */
381 if ((sw_if_index == sif->sw_if_index) &&
382 (ia->address_length == address_length) &&
383 !ip6_address_is_equal (x, address))
384 continue;
385
386 /* error if the length or intf was different */
Neale Ranns744902e2017-08-14 10:35:44 -0700387 vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS;
388 return
389 clib_error_create
390 ("failed to add %U which conflicts with %U for interface %U",
391 format_ip6_address_and_length, address,
392 address_length,
393 format_ip6_address_and_length, x,
394 ia->address_length,
395 format_vnet_sw_if_index_name, vnm,
396 sif->sw_if_index);
397 }
398 }));
399 }
400 }));
401 }
402 /* *INDENT-ON* */
403
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 {
405 uword elts_before = pool_elts (lm->if_address_pool);
406
407 error = ip_interface_address_add_del
Dave Barachd7cb1b52016-12-09 09:52:16 -0500408 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 if (error)
410 goto done;
411
412 /* Pool did not grow: add duplicate address. */
413 if (elts_before == pool_elts (lm->if_address_pool))
414 goto done;
415 }
416
Dave Barachd7cb1b52016-12-09 09:52:16 -0500417 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000418
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100419 if (is_del)
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500420 ip6_del_interface_routes (sw_if_index,
421 im, ip6_af.fib_index, address, address_length);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100422 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500423 ip6_add_interface_routes (vnm, sw_if_index,
424 im, ip6_af.fib_index,
425 pool_elt_at_index (lm->if_address_pool,
426 if_address_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
428 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500429 ip6_add_del_interface_address_callback_t *cb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 vec_foreach (cb, im->add_del_interface_address_callbacks)
431 cb->function (im, cb->function_opaque, sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500432 address, address_length, if_address_index, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 }
434
Dave Barachd7cb1b52016-12-09 09:52:16 -0500435done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 vec_free (addr_fib);
437 return error;
438}
439
Damjan Marion38173502019-02-13 19:30:09 +0100440#endif
441
442static clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500443ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500445 ip6_main_t *im = &ip6_main;
446 ip_interface_address_t *ia;
447 ip6_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 u32 is_admin_up, fib_index;
449
450 /* Fill in lookup tables with default table (0). */
451 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
452
Dave Barachd7cb1b52016-12-09 09:52:16 -0500453 vec_validate_init_empty (im->
454 lookup_main.if_address_pool_index_by_sw_if_index,
455 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
457 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
458
459 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
460
Dave Barachd7cb1b52016-12-09 09:52:16 -0500461 /* *INDENT-OFF* */
Dave Barach75fc8542016-10-11 16:16:02 -0400462 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 0 /* honor unnumbered */,
464 ({
465 a = ip_interface_address_get_address (&im->lookup_main, ia);
466 if (is_admin_up)
467 ip6_add_interface_routes (vnm, sw_if_index,
468 im, fib_index,
469 ia);
470 else
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500471 ip6_del_interface_routes (sw_if_index, im, fib_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 a, ia->address_length);
473 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500474 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475
476 return 0;
477}
478
479VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
480
Dave Barachd6534602016-06-14 18:38:02 -0400481/* Built-in ip6 unicast rx feature path definition */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500482/* *INDENT-OFF* */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100483VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
484{
485 .arc_name = "ip6-unicast",
486 .start_nodes = VNET_FEATURES ("ip6-input"),
Dave Baracha25def72018-11-26 11:04:45 -0500487 .last_in_arc = "ip6-lookup",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100488 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
489};
490
Dave Barachd7cb1b52016-12-09 09:52:16 -0500491VNET_FEATURE_INIT (ip6_flow_classify, static) =
492{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100493 .arc_name = "ip6-unicast",
Juraj Sloboda506b2452016-08-07 23:45:24 -0700494 .node_name = "ip6-flow-classify",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100495 .runs_before = VNET_FEATURES ("ip6-inacl"),
Juraj Sloboda506b2452016-08-07 23:45:24 -0700496};
497
Dave Barachd7cb1b52016-12-09 09:52:16 -0500498VNET_FEATURE_INIT (ip6_inacl, static) =
499{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100500 .arc_name = "ip6-unicast",
Dave Barach75fc8542016-10-11 16:16:02 -0400501 .node_name = "ip6-inacl",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100502 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
Dave Barachd6534602016-06-14 18:38:02 -0400503};
504
Dave Barachd7cb1b52016-12-09 09:52:16 -0500505VNET_FEATURE_INIT (ip6_policer_classify, static) =
506{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100507 .arc_name = "ip6-unicast",
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700508 .node_name = "ip6-policer-classify",
Pierre Pfister057b3562018-12-10 17:01:01 +0100509 .runs_before = VNET_FEATURES ("ipsec6-input-feature"),
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700510};
511
Dave Barachd7cb1b52016-12-09 09:52:16 -0500512VNET_FEATURE_INIT (ip6_ipsec, static) =
513{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100514 .arc_name = "ip6-unicast",
Pierre Pfister057b3562018-12-10 17:01:01 +0100515 .node_name = "ipsec6-input-feature",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100516 .runs_before = VNET_FEATURES ("l2tp-decap"),
Dave Barachd6534602016-06-14 18:38:02 -0400517};
518
Dave Barachd7cb1b52016-12-09 09:52:16 -0500519VNET_FEATURE_INIT (ip6_l2tp, static) =
520{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100521 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400522 .node_name = "l2tp-decap",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100523 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
Dave Barachd6534602016-06-14 18:38:02 -0400524};
525
Dave Barachd7cb1b52016-12-09 09:52:16 -0500526VNET_FEATURE_INIT (ip6_vpath, static) =
527{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100528 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400529 .node_name = "vpath-input-ip6",
John Lo2b81eb82017-01-30 13:12:10 -0500530 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
531};
532
533VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
534{
535 .arc_name = "ip6-unicast",
536 .node_name = "ip6-vxlan-bypass",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100537 .runs_before = VNET_FEATURES ("ip6-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400538};
539
Neale Ranns8269d3d2018-01-30 09:02:20 -0800540VNET_FEATURE_INIT (ip6_not_enabled, static) =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500541{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100542 .arc_name = "ip6-unicast",
Neale Ranns8269d3d2018-01-30 09:02:20 -0800543 .node_name = "ip6-not-enabled",
Neale Ranns630198f2017-05-22 09:20:20 -0400544 .runs_before = VNET_FEATURES ("ip6-lookup"),
545};
546
547VNET_FEATURE_INIT (ip6_lookup, static) =
548{
549 .arc_name = "ip6-unicast",
550 .node_name = "ip6-lookup",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100551 .runs_before = 0, /*last feature*/
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100552};
553
Dave Barachd6534602016-06-14 18:38:02 -0400554/* Built-in ip6 multicast rx feature path definition (none now) */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100555VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
556{
557 .arc_name = "ip6-multicast",
558 .start_nodes = VNET_FEATURES ("ip6-input"),
Dave Baracha25def72018-11-26 11:04:45 -0500559 .last_in_arc = "ip6-mfib-forward-lookup",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100560 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
561};
562
563VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
564 .arc_name = "ip6-multicast",
Dave Barachd6534602016-06-14 18:38:02 -0400565 .node_name = "vpath-input-ip6",
Neale Ranns32e1c012016-11-22 17:07:28 +0000566 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400567};
568
Neale Ranns8269d3d2018-01-30 09:02:20 -0800569VNET_FEATURE_INIT (ip6_not_enabled_mc, static) = {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100570 .arc_name = "ip6-multicast",
Neale Ranns8269d3d2018-01-30 09:02:20 -0800571 .node_name = "ip6-not-enabled",
Neale Ranns630198f2017-05-22 09:20:20 -0400572 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
573};
574
575VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
576 .arc_name = "ip6-multicast",
577 .node_name = "ip6-mfib-forward-lookup",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100578 .runs_before = 0, /* last feature */
Neale Ranns5e575b12016-10-03 09:40:25 +0100579};
Dave Barach5331c722016-08-17 11:54:30 -0400580
581/* Built-in ip4 tx feature path definition */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100582VNET_FEATURE_ARC_INIT (ip6_output, static) =
583{
584 .arc_name = "ip6-output",
Neale Rannsf068c3e2018-01-03 04:18:48 -0800585 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain", "ip6-dvr-dpo"),
Dave Baracha25def72018-11-26 11:04:45 -0500586 .last_in_arc = "interface-output",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100587 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
Dave Barach5331c722016-08-17 11:54:30 -0400588};
589
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100590VNET_FEATURE_INIT (ip6_outacl, static) = {
591 .arc_name = "ip6-output",
592 .node_name = "ip6-outacl",
Pierre Pfister057b3562018-12-10 17:01:01 +0100593 .runs_before = VNET_FEATURES ("ipsec6-output-feature"),
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100594};
595
Matus Fabian08a6f012016-11-15 06:08:51 -0800596VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
597 .arc_name = "ip6-output",
Pierre Pfister057b3562018-12-10 17:01:01 +0100598 .node_name = "ipsec6-output-feature",
Matus Fabian08a6f012016-11-15 06:08:51 -0800599 .runs_before = VNET_FEATURES ("interface-output"),
600};
601
Damjan Marion8b3191e2016-11-09 19:54:20 +0100602VNET_FEATURE_INIT (ip6_interface_output, static) = {
603 .arc_name = "ip6-output",
604 .node_name = "interface-output",
605 .runs_before = 0, /* not before any other features */
606};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500607/* *INDENT-ON* */
Dave Barachd6534602016-06-14 18:38:02 -0400608
Damjan Marion38173502019-02-13 19:30:09 +0100609static clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500610ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611{
Florin Corasb5c13fd2017-05-10 12:32:53 -0700612 ip6_main_t *im = &ip6_main;
613
614 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
615 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
616
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200617 if (!is_add)
618 {
619 /* Ensure that IPv6 is disabled */
620 ip6_main_t *im6 = &ip6_main;
621 ip_lookup_main_t *lm6 = &im6->lookup_main;
622 ip_interface_address_t *ia = 0;
623 ip6_address_t *address;
624 vlib_main_t *vm = vlib_get_main ();
625
626 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0 /* is_add */ );
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700627 vnet_sw_interface_update_unnumbered (sw_if_index, ~0, 0);
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200628 /* *INDENT-OFF* */
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700629 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200630 ({
631 address = ip_interface_address_get_address (lm6, ia);
632 ip6_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1);
633 }));
634 /* *INDENT-ON* */
635 ip6_mfib_interface_enable_disable (sw_if_index, 0);
636 }
637
Neale Ranns8269d3d2018-01-30 09:02:20 -0800638 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
Damjan Marion8b3191e2016-11-09 19:54:20 +0100639 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640
Neale Ranns8269d3d2018-01-30 09:02:20 -0800641 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
642 sw_if_index, is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 return /* no error */ 0;
645}
646
647VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
648
Damjan Marion38173502019-02-13 19:30:09 +0100649VLIB_NODE_FN (ip6_lookup_node) (vlib_main_t * vm,
650 vlib_node_runtime_t * node,
651 vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200652{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100653 return ip6_lookup_inline (vm, node, frame);
Damjan Marionaca64c92016-04-13 09:48:56 +0200654}
655
Dave Barachd7cb1b52016-12-09 09:52:16 -0500656static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100657
Dave Barachd7cb1b52016-12-09 09:52:16 -0500658/* *INDENT-OFF* */
659VLIB_REGISTER_NODE (ip6_lookup_node) =
660{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 .name = "ip6-lookup",
662 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100663 .format_trace = format_ip6_lookup_trace,
Ole Troanf0f85222016-06-14 21:12:32 +0200664 .n_next_nodes = IP6_LOOKUP_N_NEXT,
Damjan Marionb2707892016-04-13 11:21:07 +0200665 .next_nodes = IP6_LOOKUP_NEXT_NODES,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500667/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
Damjan Marion38173502019-02-13 19:30:09 +0100669VLIB_NODE_FN (ip6_load_balance_node) (vlib_main_t * vm,
670 vlib_node_runtime_t * node,
671 vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200672{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500673 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
Neale Ranns3ce72b22019-05-27 08:21:32 -0400674 u32 n_left, *from;
Damjan Marion067cd622018-07-11 12:47:43 +0200675 u32 thread_index = vm->thread_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500676 ip6_main_t *im = &ip6_main;
Neale Ranns3ce72b22019-05-27 08:21:32 -0400677 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
678 u16 nexts[VLIB_FRAME_SIZE], *next;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100679
680 from = vlib_frame_vector_args (frame);
Neale Ranns3ce72b22019-05-27 08:21:32 -0400681 n_left = frame->n_vectors;
682 next = nexts;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100683
Neale Ranns3ce72b22019-05-27 08:21:32 -0400684 vlib_get_buffers (vm, from, bufs, n_left);
685
686 while (n_left >= 4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100687 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400688 const load_balance_t *lb0, *lb1;
689 const ip6_header_t *ip0, *ip1;
690 u32 lbi0, hc0, lbi1, hc1;
691 const dpo_id_t *dpo0, *dpo1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100692
Neale Ranns3ce72b22019-05-27 08:21:32 -0400693 /* Prefetch next iteration. */
694 {
695 vlib_prefetch_buffer_header (b[2], STORE);
696 vlib_prefetch_buffer_header (b[3], STORE);
Dave Barach75fc8542016-10-11 16:16:02 -0400697
Neale Ranns3ce72b22019-05-27 08:21:32 -0400698 CLIB_PREFETCH (b[2]->data, sizeof (ip0[0]), STORE);
699 CLIB_PREFETCH (b[3]->data, sizeof (ip0[0]), STORE);
700 }
701
702 ip0 = vlib_buffer_get_current (b[0]);
703 ip1 = vlib_buffer_get_current (b[1]);
704 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
705 lbi1 = vnet_buffer (b[1])->ip.adj_index[VLIB_TX];
706
707 lb0 = load_balance_get (lbi0);
708 lb1 = load_balance_get (lbi1);
709
710 /*
711 * this node is for via FIBs we can re-use the hash value from the
712 * to node if present.
713 * We don't want to use the same hash value at each level in the recursion
714 * graph as that would lead to polarisation
715 */
716 hc0 = hc1 = 0;
717
718 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500719 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400720 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500721 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400722 hc0 = vnet_buffer (b[0])->ip.flow_hash =
723 vnet_buffer (b[0])->ip.flow_hash >> 1;
Neale Rannsf12a83f2017-04-18 09:09:40 -0700724 }
725 else
726 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400727 hc0 = vnet_buffer (b[0])->ip.flow_hash =
728 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500729 }
Neale Ranns3ce72b22019-05-27 08:21:32 -0400730 dpo0 = load_balance_get_fwd_bucket
731 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
732 }
733 else
734 {
735 dpo0 = load_balance_get_bucket_i (lb0, 0);
736 }
737 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
738 {
739 if (PREDICT_TRUE (vnet_buffer (b[1])->ip.flow_hash))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500740 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400741 hc1 = vnet_buffer (b[1])->ip.flow_hash =
742 vnet_buffer (b[1])->ip.flow_hash >> 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500743 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700744 else
745 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400746 hc1 = vnet_buffer (b[1])->ip.flow_hash =
747 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
Neale Rannsf12a83f2017-04-18 09:09:40 -0700748 }
Neale Ranns3ce72b22019-05-27 08:21:32 -0400749 dpo1 = load_balance_get_fwd_bucket
750 (lb1, (hc1 & (lb1->lb_n_buckets_minus_1)));
751 }
752 else
753 {
754 dpo1 = load_balance_get_bucket_i (lb1, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500755 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000756
Neale Ranns3ce72b22019-05-27 08:21:32 -0400757 next[0] = dpo0->dpoi_next_node;
758 next[1] = dpo1->dpoi_next_node;
759
760 /* Only process the HBH Option Header if explicitly configured to do so */
761 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500762 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400763 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
764 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
765 }
766 /* Only process the HBH Option Header if explicitly configured to do so */
767 if (PREDICT_FALSE (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
768 {
769 next[1] = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
770 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[1];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500771 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100772
Neale Ranns3ce72b22019-05-27 08:21:32 -0400773 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
774 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
775
776 vlib_increment_combined_counter
777 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
778 vlib_increment_combined_counter
779 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, b[1]));
780
781 b += 2;
782 next += 2;
783 n_left -= 2;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100784 }
785
Neale Ranns3ce72b22019-05-27 08:21:32 -0400786 while (n_left > 0)
787 {
788 const load_balance_t *lb0;
789 const ip6_header_t *ip0;
790 const dpo_id_t *dpo0;
791 u32 lbi0, hc0;
792
793 ip0 = vlib_buffer_get_current (b[0]);
794 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
795
796 lb0 = load_balance_get (lbi0);
797
798 hc0 = 0;
799 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
800 {
801 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
802 {
803 hc0 = vnet_buffer (b[0])->ip.flow_hash =
804 vnet_buffer (b[0])->ip.flow_hash >> 1;
805 }
806 else
807 {
808 hc0 = vnet_buffer (b[0])->ip.flow_hash =
809 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
810 }
811 dpo0 = load_balance_get_fwd_bucket
812 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
813 }
814 else
815 {
816 dpo0 = load_balance_get_bucket_i (lb0, 0);
817 }
818
819 next[0] = dpo0->dpoi_next_node;
820 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
821
822 /* Only process the HBH Option Header if explicitly configured to do so */
823 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
824 {
825 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
826 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
827 }
828
829 vlib_increment_combined_counter
830 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
831
832 b += 1;
833 next += 1;
834 n_left -= 1;
835 }
836
837 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
838
Neale Rannsa71844f2018-11-08 07:31:36 -0800839 if (node->flags & VLIB_NODE_FLAG_TRACE)
840 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
841
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100842 return frame->n_vectors;
Damjan Marionaca64c92016-04-13 09:48:56 +0200843}
844
Dave Barachd7cb1b52016-12-09 09:52:16 -0500845/* *INDENT-OFF* */
846VLIB_REGISTER_NODE (ip6_load_balance_node) =
847{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100848 .name = "ip6-load-balance",
Damjan Marionaca64c92016-04-13 09:48:56 +0200849 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200850 .sibling_of = "ip6-lookup",
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100851 .format_trace = format_ip6_lookup_trace,
Damjan Marionaca64c92016-04-13 09:48:56 +0200852};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500853/* *INDENT-ON* */
Damjan Marionaca64c92016-04-13 09:48:56 +0200854
Dave Barachd7cb1b52016-12-09 09:52:16 -0500855typedef struct
856{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857 /* Adjacency taken. */
858 u32 adj_index;
859 u32 flow_hash;
John Lo2d343742016-01-19 17:27:17 -0500860 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861
862 /* Packet data, possibly *after* rewrite. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500863 u8 packet_data[128 - 1 * sizeof (u32)];
864}
865ip6_forward_next_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866
Damjan Marion38173502019-02-13 19:30:09 +0100867#ifndef CLIB_MARCH_VARIANT
John Lo2b81eb82017-01-30 13:12:10 -0500868u8 *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500869format_ip6_forward_next_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870{
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100871 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
872 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500873 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200874 u32 indent = format_get_indent (s);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100875
Dave Barachd7cb1b52016-12-09 09:52:16 -0500876 s = format (s, "%U%U",
877 format_white_space, indent,
878 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100879 return s;
880}
Damjan Marion38173502019-02-13 19:30:09 +0100881#endif
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100882
Dave Barachd7cb1b52016-12-09 09:52:16 -0500883static u8 *
884format_ip6_lookup_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100885{
886 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
887 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500888 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200889 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890
John Loac8146c2016-09-27 17:44:02 -0400891 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500892 t->fib_index, t->adj_index, t->flow_hash);
893 s = format (s, "\n%U%U",
894 format_white_space, indent,
895 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100896 return s;
897}
Pierre Pfister0febaf12016-06-08 12:23:21 +0100898
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899
Dave Barachd7cb1b52016-12-09 09:52:16 -0500900static u8 *
901format_ip6_rewrite_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100902{
903 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
904 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500905 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200906 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100908 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500909 t->fib_index, t->adj_index, format_ip_adjacency,
910 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100911 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500912 format_white_space, indent,
913 format_ip_adjacency_packet_data,
Neale Rannsb069a692017-03-15 12:34:25 -0400914 t->adj_index, t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915 return s;
916}
917
918/* Common trace function for all ip6-forward next nodes. */
Damjan Marion38173502019-02-13 19:30:09 +0100919#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920void
921ip6_forward_next_trace (vlib_main_t * vm,
922 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500923 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500925 u32 *from, n_left;
926 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927
928 n_left = frame->n_vectors;
929 from = vlib_frame_vector_args (frame);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100930
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 while (n_left >= 4)
932 {
933 u32 bi0, bi1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500934 vlib_buffer_t *b0, *b1;
935 ip6_forward_next_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936
937 /* Prefetch next iteration. */
938 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
939 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
940
941 bi0 = from[0];
942 bi1 = from[1];
943
944 b0 = vlib_get_buffer (vm, bi0);
945 b1 = vlib_get_buffer (vm, bi1);
946
947 if (b0->flags & VLIB_BUFFER_IS_TRACED)
948 {
949 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
950 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500951 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
952 t0->fib_index =
953 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
954 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
955 vec_elt (im->fib_index_by_sw_if_index,
956 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100957
Dave Barach178cf492018-11-13 16:34:13 -0500958 clib_memcpy_fast (t0->packet_data,
959 vlib_buffer_get_current (b0),
960 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961 }
962 if (b1->flags & VLIB_BUFFER_IS_TRACED)
963 {
964 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
965 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500966 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
967 t1->fib_index =
968 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
969 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
970 vec_elt (im->fib_index_by_sw_if_index,
971 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100972
Dave Barach178cf492018-11-13 16:34:13 -0500973 clib_memcpy_fast (t1->packet_data,
974 vlib_buffer_get_current (b1),
975 sizeof (t1->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976 }
977 from += 2;
978 n_left -= 2;
979 }
980
981 while (n_left >= 1)
982 {
983 u32 bi0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500984 vlib_buffer_t *b0;
985 ip6_forward_next_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
987 bi0 = from[0];
988
989 b0 = vlib_get_buffer (vm, bi0);
990
991 if (b0->flags & VLIB_BUFFER_IS_TRACED)
992 {
993 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
994 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500995 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
996 t0->fib_index =
997 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
998 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
999 vec_elt (im->fib_index_by_sw_if_index,
1000 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001001
Dave Barach178cf492018-11-13 16:34:13 -05001002 clib_memcpy_fast (t0->packet_data,
1003 vlib_buffer_get_current (b0),
1004 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005 }
1006 from += 1;
1007 n_left -= 1;
1008 }
1009}
1010
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011/* Compute TCP/UDP/ICMP6 checksum in software. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001012u16
1013ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1014 ip6_header_t * ip0, int *bogus_lengthp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015{
1016 ip_csum_t sum0;
1017 u16 sum16, payload_length_host_byte_order;
1018 u32 i, n_this_buffer, n_bytes_left;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001019 u32 headers_size = sizeof (ip0[0]);
1020 void *data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021
Dave Barachd7cb1b52016-12-09 09:52:16 -05001022 ASSERT (bogus_lengthp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 *bogus_lengthp = 0;
1024
1025 /* Initialize checksum with ip header. */
1026 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1027 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1028 data_this_buffer = (void *) (ip0 + 1);
Dave Barach75fc8542016-10-11 16:16:02 -04001029
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1031 {
1032 sum0 = ip_csum_with_carry (sum0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001033 clib_mem_unaligned (&ip0->
1034 src_address.as_uword[i],
1035 uword));
1036 sum0 =
1037 ip_csum_with_carry (sum0,
1038 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1039 uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 }
1041
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301042 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1043 * or UDP-Ping packets */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001044 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001046 u32 skip_bytes;
1047 ip6_hop_by_hop_ext_t *ext_hdr =
1048 (ip6_hop_by_hop_ext_t *) data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049
1050 /* validate really icmp6 next */
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301051 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1052 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053
Dave Barachd7cb1b52016-12-09 09:52:16 -05001054 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1055 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
Dave Barach75fc8542016-10-11 16:16:02 -04001056
Dave Barachd7cb1b52016-12-09 09:52:16 -05001057 payload_length_host_byte_order -= skip_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058 headers_size += skip_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001059 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060
1061 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
John Lo3bc6bc22019-08-03 14:36:39 -04001062
1063 if (p0)
1064 {
1065 u32 n_ip_bytes_this_buffer =
1066 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
1067 if (n_this_buffer + headers_size > n_ip_bytes_this_buffer)
1068 {
1069 n_this_buffer = p0->current_length > headers_size ?
1070 n_ip_bytes_this_buffer - headers_size : 0;
1071 }
1072 }
1073
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074 while (1)
1075 {
1076 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1077 n_bytes_left -= n_this_buffer;
1078 if (n_bytes_left == 0)
1079 break;
1080
1081 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001082 {
1083 *bogus_lengthp = 1;
1084 return 0xfefe;
1085 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086 p0 = vlib_get_buffer (vm, p0->next_buffer);
1087 data_this_buffer = vlib_buffer_get_current (p0);
John Lo1cf00072019-04-09 10:23:56 -04001088 n_this_buffer = clib_min (p0->current_length, n_bytes_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090
Dave Barachd7cb1b52016-12-09 09:52:16 -05001091 sum16 = ~ip_csum_fold (sum0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092
1093 return sum16;
1094}
1095
Dave Barachd7cb1b52016-12-09 09:52:16 -05001096u32
1097ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001099 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1100 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101 u16 sum16;
1102 int bogus_length;
1103
1104 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1105 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1106 || ip0->protocol == IP_PROTOCOL_ICMP6
1107 || ip0->protocol == IP_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001108 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109
1110 udp0 = (void *) (ip0 + 1);
1111 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1112 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001113 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1114 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 return p0->flags;
1116 }
1117
1118 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1119
Damjan Marion213b5aa2017-07-13 21:19:27 +02001120 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1121 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122
1123 return p0->flags;
1124}
Damjan Marion38173502019-02-13 19:30:09 +01001125#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301127/**
1128 * @brief returns number of links on which src is reachable.
1129 */
1130always_inline int
1131ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1132{
1133 const load_balance_t *lb0;
1134 index_t lbi;
Florin Corasf3a3bad2018-03-28 02:18:29 -07001135 u32 fib_index;
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301136
Florin Corasf3a3bad2018-03-28 02:18:29 -07001137 fib_index = vec_elt (im->fib_index_by_sw_if_index,
1138 vnet_buffer (b)->sw_if_index[VLIB_RX]);
1139 fib_index =
1140 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
1141 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301142
Simon Zhange7eba482019-08-25 15:30:45 +08001143 lbi = ip6_fib_table_fwding_lookup (fib_index, &i->src_address);
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301144 lb0 = load_balance_get (lbi);
1145
1146 return (fib_urpf_check_size (lb0->lb_urpf));
1147}
1148
rootc9d1c5b2017-08-15 12:58:31 -04001149always_inline u8
1150ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1151 u32 * udp_offset0)
1152{
1153 u32 proto0;
1154 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1155 if (proto0 != IP_PROTOCOL_UDP)
1156 {
1157 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1158 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1159 }
1160 return proto0;
1161}
1162
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001163/* *INDENT-OFF* */
1164VNET_FEATURE_ARC_INIT (ip6_local) =
1165{
1166 .arc_name = "ip6-local",
1167 .start_nodes = VNET_FEATURES ("ip6-local"),
1168};
1169/* *INDENT-ON* */
1170
Benoît Ganne26a10192019-02-14 15:32:45 +01001171always_inline uword
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001172ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1173 vlib_frame_t * frame, int head_of_feature_arc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001174{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001175 ip6_main_t *im = &ip6_main;
1176 ip_lookup_main_t *lm = &im->lookup_main;
Benoît Ganne26a10192019-02-14 15:32:45 +01001177 u32 *from, n_left_from;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001178 vlib_node_runtime_t *error_node =
1179 vlib_node_get_runtime (vm, ip6_input_node.index);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001180 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
Benoît Ganne26a10192019-02-14 15:32:45 +01001181 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1182 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183
1184 from = vlib_frame_vector_args (frame);
1185 n_left_from = frame->n_vectors;
Dave Barach75fc8542016-10-11 16:16:02 -04001186
Ed Warnickecb9cada2015-12-08 15:45:58 -07001187 if (node->flags & VLIB_NODE_FLAG_TRACE)
1188 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1189
Benoît Ganne26a10192019-02-14 15:32:45 +01001190 vlib_get_buffers (vm, from, bufs, n_left_from);
1191 b = bufs;
1192 next = nexts;
1193
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001194 while (n_left_from > 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001196 /* Prefetch next iteration. */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001197 if (n_left_from >= 6)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001199 vlib_prefetch_buffer_header (b[4], STORE);
1200 vlib_prefetch_buffer_header (b[5], STORE);
1201 vlib_prefetch_buffer_data (b[2], LOAD);
1202 vlib_prefetch_buffer_data (b[3], LOAD);
Benoît Ganne26a10192019-02-14 15:32:45 +01001203 }
Dave Barach75fc8542016-10-11 16:16:02 -04001204
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001205 u8 error[2];
1206 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1207 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
Dave Barach75fc8542016-10-11 16:16:02 -04001208
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001209 ip6_header_t *ip[2];
1210 ip[0] = vlib_buffer_get_current (b[0]);
1211 ip[1] = vlib_buffer_get_current (b[1]);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001212
Benoît Ganne26a10192019-02-14 15:32:45 +01001213 if (head_of_feature_arc)
1214 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001215 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1216 vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001218 u8 type[2];
1219 type[0] = lm->builtin_protocol_by_ip_protocol[ip[0]->protocol];
1220 type[1] = lm->builtin_protocol_by_ip_protocol[ip[1]->protocol];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001222 u32 flags[2];
1223 flags[0] = b[0]->flags;
1224 flags[1] = b[1]->flags;
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001225
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001226 u32 good_l4_csum[2];
1227 good_l4_csum[0] =
1228 flags[0] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1229 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1230 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1231 good_l4_csum[1] =
1232 flags[1] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
Benoît Ganne26a10192019-02-14 15:32:45 +01001233 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1234 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
Filip Tehlarb601f222017-01-02 10:22:56 +01001235
Damjan Marion34e823f2019-02-19 08:55:18 +01001236 u32 udp_offset[2] = { };
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001237 u8 is_tcp_udp[2];
1238 is_tcp_udp[0] =
1239 ip6_next_proto_is_tcp_udp (b[0], ip[0], &udp_offset[0]);
1240 is_tcp_udp[1] =
1241 ip6_next_proto_is_tcp_udp (b[1], ip[1], &udp_offset[1]);
1242 i16 len_diff[2] = { 0 };
1243 if (PREDICT_TRUE (is_tcp_udp[0]))
Shwethab78292e2016-09-13 11:51:00 +01001244 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001245 udp_header_t *udp =
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001246 (udp_header_t *) ((u8 *) ip[0] + udp_offset[0]);
1247 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UDP
Benoît Ganne26a10192019-02-14 15:32:45 +01001248 && udp->checksum == 0;
1249 /* optimistically verify UDP length. */
1250 u16 ip_len, udp_len;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001251 ip_len = clib_net_to_host_u16 (ip[0]->payload_length);
Benoît Ganne26a10192019-02-14 15:32:45 +01001252 udp_len = clib_net_to_host_u16 (udp->length);
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001253 len_diff[0] = ip_len - udp_len;
Shwethab78292e2016-09-13 11:51:00 +01001254 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001255 if (PREDICT_TRUE (is_tcp_udp[1]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001257 udp_header_t *udp =
1258 (udp_header_t *) ((u8 *) ip[1] + udp_offset[1]);
1259 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UDP
1260 && udp->checksum == 0;
1261 /* optimistically verify UDP length. */
1262 u16 ip_len, udp_len;
1263 ip_len = clib_net_to_host_u16 (ip[1]->payload_length);
1264 udp_len = clib_net_to_host_u16 (udp->length);
1265 len_diff[1] = ip_len - udp_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266 }
1267
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001268 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1269 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1270
1271 len_diff[0] = type[0] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[0] : 0;
1272 len_diff[1] = type[1] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[1] : 0;
1273
1274 u8 need_csum[2];
1275 need_csum[0] = type[0] != IP_BUILTIN_PROTOCOL_UNKNOWN
1276 && !good_l4_csum[0]
1277 && !(flags[0] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1278 need_csum[1] = type[1] != IP_BUILTIN_PROTOCOL_UNKNOWN
1279 && !good_l4_csum[1]
1280 && !(flags[1] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1281 if (PREDICT_FALSE (need_csum[0]))
1282 {
1283 flags[0] = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1284 good_l4_csum[0] = flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1285 }
1286 if (PREDICT_FALSE (need_csum[1]))
1287 {
1288 flags[1] = ip6_tcp_udp_icmp_validate_checksum (vm, b[1]);
1289 good_l4_csum[1] = flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1290 }
1291
1292 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1293 error[0] = len_diff[0] < 0 ? IP6_ERROR_UDP_LENGTH : error[0];
1294 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1295 error[1] = len_diff[1] < 0 ? IP6_ERROR_UDP_LENGTH : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001296
Benoît Ganne26a10192019-02-14 15:32:45 +01001297 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1298 IP6_ERROR_UDP_CHECKSUM,
1299 "Wrong IP6 errors constants");
1300 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1301 IP6_ERROR_ICMP_CHECKSUM,
1302 "Wrong IP6 errors constants");
1303
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001304 error[0] =
1305 !good_l4_csum[0] ? IP6_ERROR_UDP_CHECKSUM + type[0] : error[0];
1306 error[1] =
1307 !good_l4_csum[1] ? IP6_ERROR_UDP_CHECKSUM + type[1] : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308
1309 /* Drop packets from unroutable hosts. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001310 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001311 u8 unroutable[2];
1312 unroutable[0] = error[0] == IP6_ERROR_UNKNOWN_PROTOCOL
1313 && type[0] != IP_BUILTIN_PROTOCOL_ICMP
1314 && !ip6_address_is_link_local_unicast (&ip[0]->src_address);
1315 unroutable[1] = error[1] == IP6_ERROR_UNKNOWN_PROTOCOL
1316 && type[1] != IP_BUILTIN_PROTOCOL_ICMP
1317 && !ip6_address_is_link_local_unicast (&ip[1]->src_address);
1318 if (PREDICT_FALSE (unroutable[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001320 error[0] =
1321 !ip6_urpf_loose_check (im, b[0],
1322 ip[0]) ? IP6_ERROR_SRC_LOOKUP_MISS
1323 : error[0];
1324 }
1325 if (PREDICT_FALSE (unroutable[1]))
1326 {
1327 error[1] =
1328 !ip6_urpf_loose_check (im, b[1],
1329 ip[1]) ? IP6_ERROR_SRC_LOOKUP_MISS
1330 : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331 }
1332
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001333 vnet_buffer (b[0])->ip.fib_index =
1334 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1335 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1336 vnet_buffer (b[0])->ip.fib_index;
1337 vnet_buffer (b[1])->ip.fib_index =
1338 vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ?
1339 vnet_buffer (b[1])->sw_if_index[VLIB_TX] :
1340 vnet_buffer (b[1])->ip.fib_index;
Benoît Ganne26a10192019-02-14 15:32:45 +01001341 } /* head_of_feature_arc */
Florin Corascea194d2017-10-02 00:18:51 -07001342
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001343 next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol];
1344 next[0] =
1345 error[0] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1346 next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol];
1347 next[1] =
1348 error[1] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[1];
Florin Corascea194d2017-10-02 00:18:51 -07001349
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001350 b[0]->error = error_node->errors[0];
1351 b[1]->error = error_node->errors[1];
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001352
Benoît Ganne26a10192019-02-14 15:32:45 +01001353 if (head_of_feature_arc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001355 u8 ip6_unknown[2];
1356 ip6_unknown[0] = error[0] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1357 ip6_unknown[1] = error[1] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1358 if (PREDICT_TRUE (ip6_unknown[0]))
Shwethab78292e2016-09-13 11:51:00 +01001359 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001360 u32 next32 = next[0];
Benoît Ganne26a10192019-02-14 15:32:45 +01001361 vnet_feature_arc_start (arc_index,
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001362 vnet_buffer (b[0])->sw_if_index
1363 [VLIB_RX], &next32, b[0]);
1364 next[0] = next32;
1365 }
1366 if (PREDICT_TRUE (ip6_unknown[1]))
1367 {
1368 u32 next32 = next[1];
1369 vnet_feature_arc_start (arc_index,
1370 vnet_buffer (b[1])->sw_if_index
1371 [VLIB_RX], &next32, b[1]);
1372 next[1] = next32;
Shwethab78292e2016-09-13 11:51:00 +01001373 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374 }
Dave Barach75fc8542016-10-11 16:16:02 -04001375
Benoît Ganne26a10192019-02-14 15:32:45 +01001376 /* next */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001377 b += 2;
1378 next += 2;
1379 n_left_from -= 2;
Benoît Ganne26a10192019-02-14 15:32:45 +01001380 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001381
Benoît Ganne26a10192019-02-14 15:32:45 +01001382 while (n_left_from)
1383 {
1384 u8 error;
1385 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1386
1387 ip6_header_t *ip;
1388 ip = vlib_buffer_get_current (b[0]);
1389
1390 if (head_of_feature_arc)
1391 {
1392 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1393 u8 type = lm->builtin_protocol_by_ip_protocol[ip->protocol];
1394
1395 u32 flags = b[0]->flags;
1396 u32 good_l4_csum =
1397 flags & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1398 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1399 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1400
1401 u32 udp_offset;
1402 i16 len_diff = 0;
1403 u8 is_tcp_udp = ip6_next_proto_is_tcp_udp (b[0], ip, &udp_offset);
1404 if (PREDICT_TRUE (is_tcp_udp))
1405 {
1406 udp_header_t *udp = (udp_header_t *) ((u8 *) ip + udp_offset);
1407 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1408 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UDP
1409 && udp->checksum == 0;
1410 /* optimistically verify UDP length. */
1411 u16 ip_len, udp_len;
1412 ip_len = clib_net_to_host_u16 (ip->payload_length);
1413 udp_len = clib_net_to_host_u16 (udp->length);
1414 len_diff = ip_len - udp_len;
1415 }
1416
1417 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UNKNOWN;
1418 len_diff = type == IP_BUILTIN_PROTOCOL_UDP ? len_diff : 0;
1419
1420 u8 need_csum = type != IP_BUILTIN_PROTOCOL_UNKNOWN && !good_l4_csum
1421 && !(flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1422 if (PREDICT_FALSE (need_csum))
1423 {
1424 flags = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1425 good_l4_csum = flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1426 }
1427
1428 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1429 error = len_diff < 0 ? IP6_ERROR_UDP_LENGTH : error;
1430
1431 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1432 IP6_ERROR_UDP_CHECKSUM,
1433 "Wrong IP6 errors constants");
1434 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1435 IP6_ERROR_ICMP_CHECKSUM,
1436 "Wrong IP6 errors constants");
1437
1438 error = !good_l4_csum ? IP6_ERROR_UDP_CHECKSUM + type : error;
1439
1440 /* Drop packets from unroutable hosts. */
1441 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1442 u8 unroutable = error == IP6_ERROR_UNKNOWN_PROTOCOL
1443 && type != IP_BUILTIN_PROTOCOL_ICMP
1444 && !ip6_address_is_link_local_unicast (&ip->src_address);
1445 if (PREDICT_FALSE (unroutable))
1446 {
1447 error =
1448 !ip6_urpf_loose_check (im, b[0],
1449 ip) ? IP6_ERROR_SRC_LOOKUP_MISS :
1450 error;
1451 }
1452
1453 vnet_buffer (b[0])->ip.fib_index =
1454 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1455 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1456 vnet_buffer (b[0])->ip.fib_index;
1457 } /* head_of_feature_arc */
1458
1459 next[0] = lm->local_next_by_ip_protocol[ip->protocol];
1460 next[0] =
1461 error != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1462
1463 b[0]->error = error_node->errors[0];
1464
1465 if (head_of_feature_arc)
1466 {
1467 if (PREDICT_TRUE (error == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1468 {
1469 u32 next32 = next[0];
1470 vnet_feature_arc_start (arc_index,
1471 vnet_buffer (b[0])->sw_if_index
1472 [VLIB_RX], &next32, b[0]);
1473 next[0] = next32;
1474 }
1475 }
1476
1477 /* next */
1478 b += 1;
1479 next += 1;
1480 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001481 }
1482
Benoît Ganne26a10192019-02-14 15:32:45 +01001483 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001484 return frame->n_vectors;
1485}
1486
Damjan Marion38173502019-02-13 19:30:09 +01001487VLIB_NODE_FN (ip6_local_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1488 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001489{
1490 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1491}
1492
Dave Barachd7cb1b52016-12-09 09:52:16 -05001493/* *INDENT-OFF* */
Damjan Marion38173502019-02-13 19:30:09 +01001494VLIB_REGISTER_NODE (ip6_local_node) =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001495{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496 .name = "ip6-local",
1497 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001499 .n_next_nodes = IP_LOCAL_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001500 .next_nodes =
1501 {
Neale Rannsd91c1db2017-07-31 02:30:50 -07001502 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1503 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001504 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1505 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
Juraj Sloboda3048b632018-10-02 11:13:53 +02001506 [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-reassembly",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507 },
1508};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001509/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001510
Damjan Marion38173502019-02-13 19:30:09 +01001511VLIB_NODE_FN (ip6_local_end_of_arc_node) (vlib_main_t * vm,
1512 vlib_node_runtime_t * node,
1513 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001514{
1515 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1516}
1517
1518/* *INDENT-OFF* */
Damjan Marion38173502019-02-13 19:30:09 +01001519VLIB_REGISTER_NODE (ip6_local_end_of_arc_node) = {
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001520 .name = "ip6-local-end-of-arc",
1521 .vector_size = sizeof (u32),
1522
1523 .format_trace = format_ip6_forward_next_trace,
1524 .sibling_of = "ip6-local",
1525};
1526
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001527VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1528 .arc_name = "ip6-local",
1529 .node_name = "ip6-local-end-of-arc",
1530 .runs_before = 0, /* not before any other features */
1531};
1532/* *INDENT-ON* */
1533
Damjan Marion38173502019-02-13 19:30:09 +01001534#ifdef CLIB_MARCH_VARIANT
1535extern vlib_node_registration_t ip6_local_node;
1536
1537#else
1538
Dave Barachd7cb1b52016-12-09 09:52:16 -05001539void
1540ip6_register_protocol (u32 protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001541{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001542 vlib_main_t *vm = vlib_get_main ();
1543 ip6_main_t *im = &ip6_main;
1544 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001545
1546 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001547 lm->local_next_by_ip_protocol[protocol] =
1548 vlib_node_add_next (vm, ip6_local_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001549}
1550
Neale Rannsb538dd82019-05-21 06:54:54 -07001551void
1552ip6_unregister_protocol (u32 protocol)
1553{
1554 ip6_main_t *im = &ip6_main;
1555 ip_lookup_main_t *lm = &im->lookup_main;
1556
1557 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1558 lm->local_next_by_ip_protocol[protocol] = IP_LOCAL_NEXT_PUNT;
1559}
1560
Ed Warnickecb9cada2015-12-08 15:45:58 -07001561clib_error_t *
John Lo86376342018-06-11 20:14:49 -04001562ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index,
1563 u8 refresh)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001565 vnet_main_t *vnm = vnet_get_main ();
1566 ip6_main_t *im = &ip6_main;
1567 icmp6_neighbor_solicitation_header_t *h;
1568 ip6_address_t *src;
1569 ip_interface_address_t *ia;
1570 ip_adjacency_t *adj;
1571 vnet_hw_interface_t *hi;
1572 vnet_sw_interface_t *si;
1573 vlib_buffer_t *b;
Neale Ranns7a272742017-05-30 02:08:14 -07001574 adj_index_t ai;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575 u32 bi = 0;
1576 int bogus_length;
1577
1578 si = vnet_get_sw_interface (vnm, sw_if_index);
1579
1580 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1581 {
1582 return clib_error_return (0, "%U: interface %U down",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001583 format_ip6_address, dst,
1584 format_vnet_sw_if_index_name, vnm,
1585 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001586 }
1587
Dave Barachd7cb1b52016-12-09 09:52:16 -05001588 src =
1589 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1590 if (!src)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001591 {
1592 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
Dave Barach75fc8542016-10-11 16:16:02 -04001593 return clib_error_return
Dave Barachd7cb1b52016-12-09 09:52:16 -05001594 (0, "no matching interface address for destination %U (interface %U)",
1595 format_ip6_address, dst,
1596 format_vnet_sw_if_index_name, vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001597 }
1598
Dave Barachd7cb1b52016-12-09 09:52:16 -05001599 h =
1600 vlib_packet_template_get_packet (vm,
1601 &im->discover_neighbor_packet_template,
1602 &bi);
John Lo084606b2018-06-19 15:27:48 -04001603 if (!h)
1604 return clib_error_return (0, "ICMP6 NS packet allocation failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001605
1606 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1607
1608 /* Destination address is a solicited node multicast address. We need to fill in
1609 the low 24 bits with low 24 bits of target's address. */
1610 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1611 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1612 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1613
1614 h->ip.src_address = src[0];
1615 h->neighbor.target_address = dst[0];
1616
Pavel Kotucek57808982017-08-02 08:20:19 +02001617 if (PREDICT_FALSE (!hi->hw_address))
1618 {
1619 return clib_error_return (0, "%U: interface %U do not support ip probe",
1620 format_ip6_address, dst,
1621 format_vnet_sw_if_index_name, vnm,
1622 sw_if_index);
1623 }
1624
Dave Barach178cf492018-11-13 16:34:13 -05001625 clib_memcpy_fast (h->link_layer_option.ethernet_address, hi->hw_address,
1626 vec_len (hi->hw_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627
Dave Barach75fc8542016-10-11 16:16:02 -04001628 h->neighbor.icmp.checksum =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001629 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001630 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001631
1632 b = vlib_get_buffer (vm, bi);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001633 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1634 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001635
1636 /* Add encapsulation string for software interface (e.g. ethernet header). */
Neale Ranns7a272742017-05-30 02:08:14 -07001637 ip46_address_t nh = {
1638 .ip6 = *dst,
1639 };
1640
1641 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1642 VNET_LINK_IP6, &nh, sw_if_index);
1643 adj = adj_get (ai);
1644
Dave Barach59b25652017-09-10 15:04:27 -04001645 /* Peer has been previously resolved, retrieve glean adj instead */
John Lo86376342018-06-11 20:14:49 -04001646 if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE && refresh == 0)
Dave Barach59b25652017-09-10 15:04:27 -04001647 {
1648 adj_unlock (ai);
Ole Troan6ee40512018-02-12 18:14:39 +01001649 ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1650 VNET_LINK_IP6, sw_if_index, &nh);
Dave Barach59b25652017-09-10 15:04:27 -04001651 adj = adj_get (ai);
1652 }
1653
Ed Warnickecb9cada2015-12-08 15:45:58 -07001654 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1655 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1656
1657 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001658 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1659 u32 *to_next = vlib_frame_vector_args (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001660 to_next[0] = bi;
1661 f->n_vectors = 1;
1662 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1663 }
1664
Neale Ranns7a272742017-05-30 02:08:14 -07001665 adj_unlock (ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001666 return /* no error */ 0;
1667}
Damjan Marion38173502019-02-13 19:30:09 +01001668#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001669
Dave Barachd7cb1b52016-12-09 09:52:16 -05001670typedef enum
1671{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001672 IP6_REWRITE_NEXT_DROP,
Chris Luke816f3e12016-06-14 16:24:47 -04001673 IP6_REWRITE_NEXT_ICMP_ERROR,
Ole Troan313f7e22018-04-10 16:02:51 +02001674 IP6_REWRITE_NEXT_FRAGMENT,
1675 IP6_REWRITE_N_NEXT /* Last */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001676} ip6_rewrite_next_t;
1677
Neale Ranns889fe942017-06-01 05:43:19 -04001678/**
1679 * This bits of an IPv6 address to mask to construct a multicast
1680 * MAC address
1681 */
1682#define IP6_MCAST_ADDR_MASK 0xffffffff
1683
Ole Troanda6e11b2018-05-23 11:21:42 +02001684always_inline void
1685ip6_mtu_check (vlib_buffer_t * b, u16 packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001686 u16 adj_packet_bytes, bool is_locally_generated,
1687 u32 * next, u32 * error)
Ole Troanda6e11b2018-05-23 11:21:42 +02001688{
1689 if (adj_packet_bytes >= 1280 && packet_bytes > adj_packet_bytes)
1690 {
Ole Troan313f7e22018-04-10 16:02:51 +02001691 if (is_locally_generated)
1692 {
1693 /* IP fragmentation */
Ole Troan282093f2018-09-19 12:38:51 +02001694 ip_frag_set_vnet_buffer (b, adj_packet_bytes,
Ole Troanb3655e52018-08-16 22:08:49 +02001695 IP6_FRAG_NEXT_IP6_REWRITE, 0);
Ole Troan313f7e22018-04-10 16:02:51 +02001696 *next = IP6_REWRITE_NEXT_FRAGMENT;
Ole Troan282093f2018-09-19 12:38:51 +02001697 *error = IP6_ERROR_MTU_EXCEEDED;
Ole Troan313f7e22018-04-10 16:02:51 +02001698 }
1699 else
1700 {
1701 *error = IP6_ERROR_MTU_EXCEEDED;
1702 icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1703 adj_packet_bytes);
1704 *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1705 }
Ole Troanda6e11b2018-05-23 11:21:42 +02001706 }
1707}
1708
Ed Warnickecb9cada2015-12-08 15:45:58 -07001709always_inline uword
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001710ip6_rewrite_inline_with_gso (vlib_main_t * vm,
1711 vlib_node_runtime_t * node,
1712 vlib_frame_t * frame,
1713 int do_counters, int is_midchain, int is_mcast,
1714 int do_gso)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001715{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001716 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1717 u32 *from = vlib_frame_vector_args (frame);
1718 u32 n_left_from, n_left_to_next, *to_next, next_index;
1719 vlib_node_runtime_t *error_node =
1720 vlib_node_get_runtime (vm, ip6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001721
1722 n_left_from = frame->n_vectors;
1723 next_index = node->cached_next_index;
Damjan Marion067cd622018-07-11 12:47:43 +02001724 u32 thread_index = vm->thread_index;
Dave Barach75fc8542016-10-11 16:16:02 -04001725
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726 while (n_left_from > 0)
1727 {
1728 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1729
1730 while (n_left_from >= 4 && n_left_to_next >= 2)
1731 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001732 ip_adjacency_t *adj0, *adj1;
1733 vlib_buffer_t *p0, *p1;
1734 ip6_header_t *ip0, *ip1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001735 u32 pi0, rw_len0, next0, error0, adj_index0;
1736 u32 pi1, rw_len1, next1, error1, adj_index1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001737 u32 tx_sw_if_index0, tx_sw_if_index1;
Ole Troan313f7e22018-04-10 16:02:51 +02001738 bool is_locally_originated0, is_locally_originated1;
Dave Barach75fc8542016-10-11 16:16:02 -04001739
Ed Warnickecb9cada2015-12-08 15:45:58 -07001740 /* Prefetch next iteration. */
1741 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001742 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743
1744 p2 = vlib_get_buffer (vm, from[2]);
1745 p3 = vlib_get_buffer (vm, from[3]);
1746
1747 vlib_prefetch_buffer_header (p2, LOAD);
1748 vlib_prefetch_buffer_header (p3, LOAD);
1749
1750 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1751 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1752
1753 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1754 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1755 }
1756
1757 pi0 = to_next[0] = from[0];
1758 pi1 = to_next[1] = from[1];
1759
1760 from += 2;
1761 n_left_from -= 2;
1762 to_next += 2;
1763 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04001764
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765 p0 = vlib_get_buffer (vm, pi0);
1766 p1 = vlib_get_buffer (vm, pi1);
1767
Neale Rannsf06aea52016-11-29 06:51:37 -08001768 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1769 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001770
Ed Warnickecb9cada2015-12-08 15:45:58 -07001771 ip0 = vlib_buffer_get_current (p0);
1772 ip1 = vlib_buffer_get_current (p1);
1773
1774 error0 = error1 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001775 next0 = next1 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001776
Ole Troan313f7e22018-04-10 16:02:51 +02001777 is_locally_originated0 =
1778 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1779 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001780 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001781 i32 hop_limit0 = ip0->hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001782
1783 /* Input node should have reject packets with hop limit 0. */
1784 ASSERT (ip0->hop_limit > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001785
1786 hop_limit0 -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001787
1788 ip0->hop_limit = hop_limit0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001789
Dave Barachd7cb1b52016-12-09 09:52:16 -05001790 /*
1791 * If the hop count drops below 1 when forwarding, generate
1792 * an ICMP response.
1793 */
1794 if (PREDICT_FALSE (hop_limit0 <= 0))
1795 {
1796 error0 = IP6_ERROR_TIME_EXPIRED;
1797 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1798 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1799 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1800 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1801 0);
1802 }
Neale Rannsf06aea52016-11-29 06:51:37 -08001803 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001804 else
1805 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001806 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001807 }
Ole Troan313f7e22018-04-10 16:02:51 +02001808 is_locally_originated1 =
1809 p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1810 if (PREDICT_TRUE (!is_locally_originated1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001811 {
Neale Rannsf06aea52016-11-29 06:51:37 -08001812 i32 hop_limit1 = ip1->hop_limit;
1813
1814 /* Input node should have reject packets with hop limit 0. */
1815 ASSERT (ip1->hop_limit > 0);
1816
1817 hop_limit1 -= 1;
1818
1819 ip1->hop_limit = hop_limit1;
1820
Dave Barachd7cb1b52016-12-09 09:52:16 -05001821 /*
1822 * If the hop count drops below 1 when forwarding, generate
1823 * an ICMP response.
1824 */
1825 if (PREDICT_FALSE (hop_limit1 <= 0))
1826 {
1827 error1 = IP6_ERROR_TIME_EXPIRED;
1828 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1829 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1830 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1831 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1832 0);
1833 }
1834 }
1835 else
1836 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001837 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001838 }
Neale Ranns107e7d42017-04-11 09:55:19 -07001839 adj0 = adj_get (adj_index0);
1840 adj1 = adj_get (adj_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001841
Ed Warnickecb9cada2015-12-08 15:45:58 -07001842 rw_len0 = adj0[0].rewrite_header.data_bytes;
1843 rw_len1 = adj1[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001844 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1845 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001846
Neale Ranns9c6a6132017-02-21 05:33:14 -08001847 if (do_counters)
1848 {
1849 vlib_increment_combined_counter
1850 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001851 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001852 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1853 vlib_increment_combined_counter
1854 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001855 thread_index, adj_index1, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001856 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1857 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001858
1859 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001860 u16 ip0_len =
1861 clib_net_to_host_u16 (ip0->payload_length) +
1862 sizeof (ip6_header_t);
1863 u16 ip1_len =
1864 clib_net_to_host_u16 (ip1->payload_length) +
1865 sizeof (ip6_header_t);
1866 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
1867 ip0_len = gso_mtu_sz (p0);
1868 if (do_gso && (p1->flags & VNET_BUFFER_F_GSO))
1869 ip1_len = gso_mtu_sz (p1);
1870
1871
1872
1873 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001874 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001875 is_locally_originated0, &next0, &error0);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001876 ip6_mtu_check (p1, ip1_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001877 adj1[0].rewrite_header.max_l3_packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001878 is_locally_originated1, &next1, &error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001879
Dave Barachd7cb1b52016-12-09 09:52:16 -05001880 /* Don't adjust the buffer for hop count issue; icmp-error node
Jim Thompsonf324dec2019-04-08 03:22:21 -05001881 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001882 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1883 {
1884 p0->current_data -= rw_len0;
1885 p0->current_length += rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886
Dave Barachd7cb1b52016-12-09 09:52:16 -05001887 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1888 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1889 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001890
Neale Rannsb069a692017-03-15 12:34:25 -04001891 if (PREDICT_FALSE
1892 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1893 vnet_feature_arc_start (lm->output_feature_arc_index,
1894 tx_sw_if_index0, &next0, p0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001895 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001896 else
1897 {
1898 p0->error = error_node->errors[error0];
1899 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001900 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1901 {
1902 p1->current_data -= rw_len1;
1903 p1->current_length += rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001904
Dave Barachd7cb1b52016-12-09 09:52:16 -05001905 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1906 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1907 next1 = adj1[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001908
Neale Rannsb069a692017-03-15 12:34:25 -04001909 if (PREDICT_FALSE
1910 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1911 vnet_feature_arc_start (lm->output_feature_arc_index,
1912 tx_sw_if_index1, &next1, p1);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001913 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001914 else
1915 {
1916 p1->error = error_node->errors[error1];
1917 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001918
Neale Ranns25edf142019-03-22 08:12:48 +00001919 if (is_midchain)
1920 {
1921 /* before we paint on the next header, update the L4
1922 * checksums if required, since there's no offload on a tunnel */
1923 calc_checksums (vm, p0);
1924 calc_checksums (vm, p1);
1925 }
1926
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001927 /* Guess we are only writing on simple Ethernet header. */
1928 vnet_rewrite_two_headers (adj0[0], adj1[0],
1929 ip0, ip1, sizeof (ethernet_header_t));
1930
1931 if (is_midchain)
1932 {
Neale Ranns25edf142019-03-22 08:12:48 +00001933 if (adj0->sub_type.midchain.fixup_func)
1934 adj0->sub_type.midchain.fixup_func
1935 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1936 if (adj1->sub_type.midchain.fixup_func)
1937 adj1->sub_type.midchain.fixup_func
1938 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001939 }
1940 if (is_mcast)
1941 {
1942 /*
1943 * copy bytes from the IP address into the MAC rewrite
1944 */
Neale Ranns889fe942017-06-01 05:43:19 -04001945 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1946 adj0->
1947 rewrite_header.dst_mcast_offset,
1948 &ip0->dst_address.as_u32[3],
1949 (u8 *) ip0);
1950 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1951 adj1->
1952 rewrite_header.dst_mcast_offset,
1953 &ip1->dst_address.as_u32[3],
1954 (u8 *) ip1);
Neale Ranns32e1c012016-11-22 17:07:28 +00001955 }
Neale Ranns5e575b12016-10-03 09:40:25 +01001956
Ed Warnickecb9cada2015-12-08 15:45:58 -07001957 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1958 to_next, n_left_to_next,
1959 pi0, pi1, next0, next1);
1960 }
1961
1962 while (n_left_from > 0 && n_left_to_next > 0)
1963 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001964 ip_adjacency_t *adj0;
1965 vlib_buffer_t *p0;
1966 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001967 u32 pi0, rw_len0;
1968 u32 adj_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001969 u32 tx_sw_if_index0;
Ole Troan313f7e22018-04-10 16:02:51 +02001970 bool is_locally_originated0;
Dave Barach75fc8542016-10-11 16:16:02 -04001971
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972 pi0 = to_next[0] = from[0];
1973
1974 p0 = vlib_get_buffer (vm, pi0);
1975
Neale Rannsf06aea52016-11-29 06:51:37 -08001976 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001977
Neale Ranns107e7d42017-04-11 09:55:19 -07001978 adj0 = adj_get (adj_index0);
Dave Barach75fc8542016-10-11 16:16:02 -04001979
Ed Warnickecb9cada2015-12-08 15:45:58 -07001980 ip0 = vlib_buffer_get_current (p0);
1981
1982 error0 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001983 next0 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001984
1985 /* Check hop limit */
Ole Troan313f7e22018-04-10 16:02:51 +02001986 is_locally_originated0 =
1987 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1988 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001989 {
1990 i32 hop_limit0 = ip0->hop_limit;
1991
1992 ASSERT (ip0->hop_limit > 0);
1993
1994 hop_limit0 -= 1;
1995
1996 ip0->hop_limit = hop_limit0;
1997
Dave Barachd7cb1b52016-12-09 09:52:16 -05001998 if (PREDICT_FALSE (hop_limit0 <= 0))
1999 {
2000 /*
2001 * If the hop count drops below 1 when forwarding, generate
2002 * an ICMP response.
2003 */
2004 error0 = IP6_ERROR_TIME_EXPIRED;
2005 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2006 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2007 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2008 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2009 0);
2010 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002012 else
2013 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02002014 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002015 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002016
Neale Ranns25edf142019-03-22 08:12:48 +00002017 if (is_midchain)
2018 {
2019 calc_checksums (vm, p0);
2020 }
2021
Ed Warnickecb9cada2015-12-08 15:45:58 -07002022 /* Guess we are only writing on simple Ethernet header. */
2023 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
Dave Barach75fc8542016-10-11 16:16:02 -04002024
Ed Warnickecb9cada2015-12-08 15:45:58 -07002025 /* Update packet buffer attributes/set output interface. */
2026 rw_len0 = adj0[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002027 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002028
Neale Ranns9c6a6132017-02-21 05:33:14 -08002029 if (do_counters)
2030 {
2031 vlib_increment_combined_counter
2032 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02002033 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08002034 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2035 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002036
2037 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002038 u16 ip0_len =
2039 clib_net_to_host_u16 (ip0->payload_length) +
2040 sizeof (ip6_header_t);
2041 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
2042 ip0_len = gso_mtu_sz (p0);
2043
2044 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02002045 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02002046 is_locally_originated0, &next0, &error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002047
Dave Barachd7cb1b52016-12-09 09:52:16 -05002048 /* Don't adjust the buffer for hop count issue; icmp-error node
Ole Troanda6e11b2018-05-23 11:21:42 +02002049 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002050 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2051 {
Chris Luke816f3e12016-06-14 16:24:47 -04002052 p0->current_data -= rw_len0;
2053 p0->current_length += rw_len0;
2054
Dave Barachd7cb1b52016-12-09 09:52:16 -05002055 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
Dave Barach5331c722016-08-17 11:54:30 -04002056
Dave Barachd7cb1b52016-12-09 09:52:16 -05002057 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2058 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04002059
Neale Rannsb069a692017-03-15 12:34:25 -04002060 if (PREDICT_FALSE
2061 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2062 vnet_feature_arc_start (lm->output_feature_arc_index,
2063 tx_sw_if_index0, &next0, p0);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002064 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04002065 else
2066 {
2067 p0->error = error_node->errors[error0];
2068 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002069
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002070 if (is_midchain)
2071 {
Neale Ranns25edf142019-03-22 08:12:48 +00002072 if (adj0->sub_type.midchain.fixup_func)
2073 adj0->sub_type.midchain.fixup_func
2074 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002075 }
2076 if (is_mcast)
2077 {
Neale Ranns889fe942017-06-01 05:43:19 -04002078 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2079 adj0->
2080 rewrite_header.dst_mcast_offset,
2081 &ip0->dst_address.as_u32[3],
2082 (u8 *) ip0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002083 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002084
Ed Warnickecb9cada2015-12-08 15:45:58 -07002085 from += 1;
2086 n_left_from -= 1;
2087 to_next += 1;
2088 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04002089
Ed Warnickecb9cada2015-12-08 15:45:58 -07002090 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2091 to_next, n_left_to_next,
2092 pi0, next0);
2093 }
2094
2095 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2096 }
2097
2098 /* Need to do trace after rewrites to pick up new packet data. */
2099 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Rannsf06aea52016-11-29 06:51:37 -08002100 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002101
2102 return frame->n_vectors;
2103}
2104
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002105always_inline uword
2106ip6_rewrite_inline (vlib_main_t * vm,
2107 vlib_node_runtime_t * node,
2108 vlib_frame_t * frame,
2109 int do_counters, int is_midchain, int is_mcast)
2110{
2111 vnet_main_t *vnm = vnet_get_main ();
2112 if (PREDICT_FALSE (vnm->interface_main.gso_interface_count > 0))
2113 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2114 is_midchain, is_mcast,
2115 1 /* do_gso */ );
2116 else
2117 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2118 is_midchain, is_mcast,
2119 0 /* no do_gso */ );
2120}
2121
Damjan Marion38173502019-02-13 19:30:09 +01002122VLIB_NODE_FN (ip6_rewrite_node) (vlib_main_t * vm,
2123 vlib_node_runtime_t * node,
2124 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002125{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002126 if (adj_are_counters_enabled ())
2127 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2128 else
2129 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002130}
2131
Damjan Marion38173502019-02-13 19:30:09 +01002132VLIB_NODE_FN (ip6_rewrite_bcast_node) (vlib_main_t * vm,
2133 vlib_node_runtime_t * node,
2134 vlib_frame_t * frame)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002135{
2136 if (adj_are_counters_enabled ())
2137 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2138 else
2139 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2140}
2141
Damjan Marion38173502019-02-13 19:30:09 +01002142VLIB_NODE_FN (ip6_rewrite_mcast_node) (vlib_main_t * vm,
2143 vlib_node_runtime_t * node,
2144 vlib_frame_t * frame)
Neale Ranns32e1c012016-11-22 17:07:28 +00002145{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002146 if (adj_are_counters_enabled ())
2147 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2148 else
2149 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002150}
2151
Damjan Marion38173502019-02-13 19:30:09 +01002152VLIB_NODE_FN (ip6_midchain_node) (vlib_main_t * vm,
2153 vlib_node_runtime_t * node,
2154 vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002155{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002156 if (adj_are_counters_enabled ())
2157 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2158 else
2159 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002160}
2161
Damjan Marion38173502019-02-13 19:30:09 +01002162VLIB_NODE_FN (ip6_mcast_midchain_node) (vlib_main_t * vm,
2163 vlib_node_runtime_t * node,
2164 vlib_frame_t * frame)
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002165{
2166 if (adj_are_counters_enabled ())
2167 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2168 else
Neale Ranns9f171f52017-04-11 08:56:53 -07002169 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002170}
2171
Dave Barachd7cb1b52016-12-09 09:52:16 -05002172/* *INDENT-OFF* */
2173VLIB_REGISTER_NODE (ip6_midchain_node) =
2174{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002175 .name = "ip6-midchain",
2176 .vector_size = sizeof (u32),
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002177 .format_trace = format_ip6_forward_next_trace,
Neale Ranns5e575b12016-10-03 09:40:25 +01002178 .sibling_of = "ip6-rewrite",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002179 };
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002180
Dave Barachd7cb1b52016-12-09 09:52:16 -05002181VLIB_REGISTER_NODE (ip6_rewrite_node) =
2182{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002183 .name = "ip6-rewrite",
2184 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +01002185 .format_trace = format_ip6_rewrite_trace,
Ole Troan313f7e22018-04-10 16:02:51 +02002186 .n_next_nodes = IP6_REWRITE_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002187 .next_nodes =
2188 {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002189 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
Chris Luke816f3e12016-06-14 16:24:47 -04002190 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Ole Troan313f7e22018-04-10 16:02:51 +02002191 [IP6_REWRITE_NEXT_FRAGMENT] = "ip6-frag",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002192 },
2193};
2194
Neale Ranns1855b8e2018-07-11 10:31:26 -07002195VLIB_REGISTER_NODE (ip6_rewrite_bcast_node) = {
Neale Ranns1855b8e2018-07-11 10:31:26 -07002196 .name = "ip6-rewrite-bcast",
2197 .vector_size = sizeof (u32),
2198
2199 .format_trace = format_ip6_rewrite_trace,
2200 .sibling_of = "ip6-rewrite",
2201};
Neale Ranns1855b8e2018-07-11 10:31:26 -07002202
Neale Ranns32e1c012016-11-22 17:07:28 +00002203VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2204{
Neale Ranns32e1c012016-11-22 17:07:28 +00002205 .name = "ip6-rewrite-mcast",
2206 .vector_size = sizeof (u32),
2207 .format_trace = format_ip6_rewrite_trace,
2208 .sibling_of = "ip6-rewrite",
2209};
Neale Ranns32e1c012016-11-22 17:07:28 +00002210
Neale Ranns32e1c012016-11-22 17:07:28 +00002211
Damjan Marion38173502019-02-13 19:30:09 +01002212VLIB_REGISTER_NODE (ip6_mcast_midchain_node) =
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002213{
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002214 .name = "ip6-mcast-midchain",
2215 .vector_size = sizeof (u32),
2216 .format_trace = format_ip6_rewrite_trace,
2217 .sibling_of = "ip6-rewrite",
2218};
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002219
Neale Ranns1855b8e2018-07-11 10:31:26 -07002220/* *INDENT-ON* */
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002221
Ole Troan944f5482016-05-24 11:56:58 +02002222/*
2223 * Hop-by-Hop handling
2224 */
Benoît Ganne47727c02019-02-12 13:35:08 +01002225#ifndef CLIB_MARCH_VARIANT
Ole Troan944f5482016-05-24 11:56:58 +02002226ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
Benoît Ganne47727c02019-02-12 13:35:08 +01002227#endif /* CLIB_MARCH_VARIANT */
Ole Troan944f5482016-05-24 11:56:58 +02002228
2229#define foreach_ip6_hop_by_hop_error \
2230_(PROCESSED, "pkts with ip6 hop-by-hop options") \
2231_(FORMAT, "incorrectly formatted hop-by-hop options") \
2232_(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2233
Neale Ranns32e1c012016-11-22 17:07:28 +00002234/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002235typedef enum
2236{
Ole Troan944f5482016-05-24 11:56:58 +02002237#define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2238 foreach_ip6_hop_by_hop_error
2239#undef _
Neale Ranns32e1c012016-11-22 17:07:28 +00002240 IP6_HOP_BY_HOP_N_ERROR,
Ole Troan944f5482016-05-24 11:56:58 +02002241} ip6_hop_by_hop_error_t;
Neale Ranns32e1c012016-11-22 17:07:28 +00002242/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002243
2244/*
2245 * Primary h-b-h handler trace support
2246 * We work pretty hard on the problem for obvious reasons
2247 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002248typedef struct
2249{
Ole Troan944f5482016-05-24 11:56:58 +02002250 u32 next_index;
2251 u32 trace_len;
2252 u8 option_data[256];
2253} ip6_hop_by_hop_trace_t;
2254
Benoît Ganne47727c02019-02-12 13:35:08 +01002255extern vlib_node_registration_t ip6_hop_by_hop_node;
Ole Troan944f5482016-05-24 11:56:58 +02002256
Dave Barachd7cb1b52016-12-09 09:52:16 -05002257static char *ip6_hop_by_hop_error_strings[] = {
Ole Troan944f5482016-05-24 11:56:58 +02002258#define _(sym,string) string,
2259 foreach_ip6_hop_by_hop_error
2260#undef _
2261};
2262
Damjan Marion38173502019-02-13 19:30:09 +01002263#ifndef CLIB_MARCH_VARIANT
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302264u8 *
2265format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2266{
2267 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2268 int total_len = va_arg (*args, int);
2269 ip6_hop_by_hop_option_t *opt0, *limit0;
2270 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2271 u8 type0;
2272
2273 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2274 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2275
2276 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2277 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2278
2279 while (opt0 < limit0)
2280 {
2281 type0 = opt0->type;
2282 switch (type0)
2283 {
2284 case 0: /* Pad, just stop */
2285 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2286 break;
2287
2288 default:
2289 if (hm->trace[type0])
2290 {
2291 s = (*hm->trace[type0]) (s, opt0);
2292 }
2293 else
2294 {
2295 s =
2296 format (s, "\n unrecognized option %d length %d", type0,
2297 opt0->length);
2298 }
2299 opt0 =
2300 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2301 sizeof (ip6_hop_by_hop_option_t));
2302 break;
2303 }
2304 }
2305 return s;
2306}
Damjan Marion38173502019-02-13 19:30:09 +01002307#endif
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302308
Ole Troan944f5482016-05-24 11:56:58 +02002309static u8 *
2310format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2311{
2312 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2313 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002314 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
Ole Troan944f5482016-05-24 11:56:58 +02002315 ip6_hop_by_hop_header_t *hbh0;
2316 ip6_hop_by_hop_option_t *opt0, *limit0;
2317 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2318
2319 u8 type0;
2320
Dave Barachd7cb1b52016-12-09 09:52:16 -05002321 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
Ole Troan944f5482016-05-24 11:56:58 +02002322
2323 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002324 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
Ole Troan944f5482016-05-24 11:56:58 +02002325
Dave Barachd7cb1b52016-12-09 09:52:16 -05002326 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2327 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
Ole Troan944f5482016-05-24 11:56:58 +02002328
Dave Barachd7cb1b52016-12-09 09:52:16 -05002329 while (opt0 < limit0)
2330 {
2331 type0 = opt0->type;
2332 switch (type0)
2333 {
2334 case 0: /* Pad, just stop */
2335 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2336 break;
Ole Troan944f5482016-05-24 11:56:58 +02002337
Dave Barachd7cb1b52016-12-09 09:52:16 -05002338 default:
2339 if (hm->trace[type0])
2340 {
2341 s = (*hm->trace[type0]) (s, opt0);
2342 }
2343 else
2344 {
2345 s =
2346 format (s, "\n unrecognized option %d length %d", type0,
2347 opt0->length);
2348 }
2349 opt0 =
2350 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2351 sizeof (ip6_hop_by_hop_option_t));
2352 break;
2353 }
Ole Troan944f5482016-05-24 11:56:58 +02002354 }
Ole Troan944f5482016-05-24 11:56:58 +02002355 return s;
2356}
2357
Dave Barachd7cb1b52016-12-09 09:52:16 -05002358always_inline u8
2359ip6_scan_hbh_options (vlib_buffer_t * b0,
2360 ip6_header_t * ip0,
2361 ip6_hop_by_hop_header_t * hbh0,
2362 ip6_hop_by_hop_option_t * opt0,
2363 ip6_hop_by_hop_option_t * limit0, u32 * next0)
Shwethaa91cbe62016-08-08 15:51:04 +01002364{
2365 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2366 u8 type0;
2367 u8 error0 = 0;
2368
2369 while (opt0 < limit0)
2370 {
2371 type0 = opt0->type;
2372 switch (type0)
2373 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002374 case 0: /* Pad1 */
2375 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
Shwethaa91cbe62016-08-08 15:51:04 +01002376 continue;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002377 case 1: /* PadN */
Shwethaa91cbe62016-08-08 15:51:04 +01002378 break;
2379 default:
2380 if (hm->options[type0])
2381 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002382 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2383 {
Shwethaa91cbe62016-08-08 15:51:04 +01002384 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002385 return (error0);
2386 }
Shwethaa91cbe62016-08-08 15:51:04 +01002387 }
2388 else
2389 {
2390 /* Unrecognized mandatory option, check the two high order bits */
2391 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2392 {
2393 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2394 break;
2395 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2396 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2397 *next0 = IP_LOOKUP_NEXT_DROP;
2398 break;
2399 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2400 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2401 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002402 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2403 ICMP6_parameter_problem_unrecognized_option,
2404 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002405 break;
2406 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2407 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002408 if (!ip6_address_is_multicast (&ip0->dst_address))
Shwethaa91cbe62016-08-08 15:51:04 +01002409 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002410 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2411 icmp6_error_set_vnet_buffer (b0,
2412 ICMP6_parameter_problem,
2413 ICMP6_parameter_problem_unrecognized_option,
2414 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002415 }
2416 else
2417 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002418 *next0 = IP_LOOKUP_NEXT_DROP;
Shwethaa91cbe62016-08-08 15:51:04 +01002419 }
2420 break;
2421 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002422 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002423 }
2424 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002425 opt0 =
2426 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2427 sizeof (ip6_hop_by_hop_option_t));
Shwethaa91cbe62016-08-08 15:51:04 +01002428 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002429 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002430}
2431
Ole Troan944f5482016-05-24 11:56:58 +02002432/*
2433 * Process the Hop-by-Hop Options header
2434 */
Damjan Marion38173502019-02-13 19:30:09 +01002435VLIB_NODE_FN (ip6_hop_by_hop_node) (vlib_main_t * vm,
2436 vlib_node_runtime_t * node,
2437 vlib_frame_t * frame)
Ole Troan944f5482016-05-24 11:56:58 +02002438{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002439 vlib_node_runtime_t *error_node =
2440 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
Ole Troan944f5482016-05-24 11:56:58 +02002441 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2442 u32 n_left_from, *from, *to_next;
2443 ip_lookup_next_t next_index;
Ole Troan944f5482016-05-24 11:56:58 +02002444
2445 from = vlib_frame_vector_args (frame);
2446 n_left_from = frame->n_vectors;
2447 next_index = node->cached_next_index;
2448
Dave Barachd7cb1b52016-12-09 09:52:16 -05002449 while (n_left_from > 0)
2450 {
2451 u32 n_left_to_next;
Ole Troan944f5482016-05-24 11:56:58 +02002452
Dave Barachd7cb1b52016-12-09 09:52:16 -05002453 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ole Troan944f5482016-05-24 11:56:58 +02002454
Dave Barachd7cb1b52016-12-09 09:52:16 -05002455 while (n_left_from >= 4 && n_left_to_next >= 2)
Shwethaa91cbe62016-08-08 15:51:04 +01002456 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002457 u32 bi0, bi1;
2458 vlib_buffer_t *b0, *b1;
2459 u32 next0, next1;
2460 ip6_header_t *ip0, *ip1;
2461 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2462 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2463 u8 error0 = 0, error1 = 0;
2464
2465 /* Prefetch next iteration. */
2466 {
2467 vlib_buffer_t *p2, *p3;
2468
2469 p2 = vlib_get_buffer (vm, from[2]);
2470 p3 = vlib_get_buffer (vm, from[3]);
2471
2472 vlib_prefetch_buffer_header (p2, LOAD);
2473 vlib_prefetch_buffer_header (p3, LOAD);
2474
2475 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2476 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Shwethaa91cbe62016-08-08 15:51:04 +01002477 }
2478
Dave Barachd7cb1b52016-12-09 09:52:16 -05002479 /* Speculatively enqueue b0, b1 to the current next frame */
2480 to_next[0] = bi0 = from[0];
2481 to_next[1] = bi1 = from[1];
2482 from += 2;
2483 to_next += 2;
2484 n_left_from -= 2;
2485 n_left_to_next -= 2;
2486
2487 b0 = vlib_get_buffer (vm, bi0);
2488 b1 = vlib_get_buffer (vm, bi1);
2489
2490 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2491 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002492 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002493 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002494 ip_adjacency_t *adj1 = adj_get (adj_index1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002495
2496 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2497 next0 = adj0->lookup_next_index;
2498 next1 = adj1->lookup_next_index;
2499
2500 ip0 = vlib_buffer_get_current (b0);
2501 ip1 = vlib_buffer_get_current (b1);
2502 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2503 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2504 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2505 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2506 limit0 =
2507 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2508 ((hbh0->length + 1) << 3));
2509 limit1 =
2510 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2511 ((hbh1->length + 1) << 3));
2512
2513 /*
2514 * Basic validity checks
2515 */
2516 if ((hbh0->length + 1) << 3 >
2517 clib_net_to_host_u16 (ip0->payload_length))
2518 {
2519 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2520 next0 = IP_LOOKUP_NEXT_DROP;
2521 goto outdual;
2522 }
2523 /* Scan the set of h-b-h options, process ones that we understand */
2524 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2525
2526 if ((hbh1->length + 1) << 3 >
2527 clib_net_to_host_u16 (ip1->payload_length))
2528 {
2529 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2530 next1 = IP_LOOKUP_NEXT_DROP;
2531 goto outdual;
2532 }
2533 /* Scan the set of h-b-h options, process ones that we understand */
2534 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2535
2536 outdual:
2537 /* Has the classifier flagged this buffer for special treatment? */
2538 if (PREDICT_FALSE
2539 ((error0 == 0)
2540 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2541 next0 = hm->next_override;
2542
2543 /* Has the classifier flagged this buffer for special treatment? */
2544 if (PREDICT_FALSE
2545 ((error1 == 0)
2546 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2547 next1 = hm->next_override;
2548
2549 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2550 {
2551 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2552 {
2553 ip6_hop_by_hop_trace_t *t =
2554 vlib_add_trace (vm, node, b0, sizeof (*t));
2555 u32 trace_len = (hbh0->length + 1) << 3;
2556 t->next_index = next0;
2557 /* Capture the h-b-h option verbatim */
2558 trace_len =
2559 trace_len <
2560 ARRAY_LEN (t->option_data) ? trace_len :
2561 ARRAY_LEN (t->option_data);
2562 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002563 clib_memcpy_fast (t->option_data, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002564 }
2565 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2566 {
2567 ip6_hop_by_hop_trace_t *t =
2568 vlib_add_trace (vm, node, b1, sizeof (*t));
2569 u32 trace_len = (hbh1->length + 1) << 3;
2570 t->next_index = next1;
2571 /* Capture the h-b-h option verbatim */
2572 trace_len =
2573 trace_len <
2574 ARRAY_LEN (t->option_data) ? trace_len :
2575 ARRAY_LEN (t->option_data);
2576 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002577 clib_memcpy_fast (t->option_data, hbh1, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002578 }
2579
2580 }
2581
2582 b0->error = error_node->errors[error0];
2583 b1->error = error_node->errors[error1];
2584
2585 /* verify speculative enqueue, maybe switch current next frame */
2586 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2587 n_left_to_next, bi0, bi1, next0,
2588 next1);
Shwethaa91cbe62016-08-08 15:51:04 +01002589 }
2590
Dave Barachd7cb1b52016-12-09 09:52:16 -05002591 while (n_left_from > 0 && n_left_to_next > 0)
2592 {
2593 u32 bi0;
2594 vlib_buffer_t *b0;
2595 u32 next0;
2596 ip6_header_t *ip0;
2597 ip6_hop_by_hop_header_t *hbh0;
2598 ip6_hop_by_hop_option_t *opt0, *limit0;
2599 u8 error0 = 0;
Shwethaa91cbe62016-08-08 15:51:04 +01002600
Dave Barachd7cb1b52016-12-09 09:52:16 -05002601 /* Speculatively enqueue b0 to the current next frame */
2602 bi0 = from[0];
2603 to_next[0] = bi0;
2604 from += 1;
2605 to_next += 1;
2606 n_left_from -= 1;
2607 n_left_to_next -= 1;
2608
2609 b0 = vlib_get_buffer (vm, bi0);
2610 /*
2611 * Default use the next_index from the adjacency.
2612 * A HBH option rarely redirects to a different node
2613 */
2614 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002615 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002616 next0 = adj0->lookup_next_index;
2617
2618 ip0 = vlib_buffer_get_current (b0);
2619 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2620 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2621 limit0 =
2622 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2623 ((hbh0->length + 1) << 3));
2624
2625 /*
2626 * Basic validity checks
2627 */
2628 if ((hbh0->length + 1) << 3 >
2629 clib_net_to_host_u16 (ip0->payload_length))
2630 {
2631 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2632 next0 = IP_LOOKUP_NEXT_DROP;
2633 goto out0;
2634 }
2635
2636 /* Scan the set of h-b-h options, process ones that we understand */
2637 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2638
2639 out0:
2640 /* Has the classifier flagged this buffer for special treatment? */
2641 if (PREDICT_FALSE
2642 ((error0 == 0)
2643 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2644 next0 = hm->next_override;
2645
2646 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2647 {
2648 ip6_hop_by_hop_trace_t *t =
2649 vlib_add_trace (vm, node, b0, sizeof (*t));
2650 u32 trace_len = (hbh0->length + 1) << 3;
2651 t->next_index = next0;
2652 /* Capture the h-b-h option verbatim */
2653 trace_len =
2654 trace_len <
2655 ARRAY_LEN (t->option_data) ? trace_len :
2656 ARRAY_LEN (t->option_data);
2657 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002658 clib_memcpy_fast (t->option_data, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002659 }
2660
2661 b0->error = error_node->errors[error0];
2662
2663 /* verify speculative enqueue, maybe switch current next frame */
2664 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2665 n_left_to_next, bi0, next0);
2666 }
2667 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Shwethaa91cbe62016-08-08 15:51:04 +01002668 }
Ole Troan944f5482016-05-24 11:56:58 +02002669 return frame->n_vectors;
2670}
2671
Dave Barachd7cb1b52016-12-09 09:52:16 -05002672/* *INDENT-OFF* */
2673VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2674{
Ole Troan944f5482016-05-24 11:56:58 +02002675 .name = "ip6-hop-by-hop",
Ole Troan964f93e2016-06-10 13:22:36 +02002676 .sibling_of = "ip6-lookup",
Ole Troan944f5482016-05-24 11:56:58 +02002677 .vector_size = sizeof (u32),
2678 .format_trace = format_ip6_hop_by_hop_trace,
2679 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002680 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
Ole Troan944f5482016-05-24 11:56:58 +02002681 .error_strings = ip6_hop_by_hop_error_strings,
Ole Troan964f93e2016-06-10 13:22:36 +02002682 .n_next_nodes = 0,
Ole Troan944f5482016-05-24 11:56:58 +02002683};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002684/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002685
Ole Troan944f5482016-05-24 11:56:58 +02002686static clib_error_t *
2687ip6_hop_by_hop_init (vlib_main_t * vm)
2688{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002689 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Dave Barachb7b92992018-10-17 10:38:51 -04002690 clib_memset (hm->options, 0, sizeof (hm->options));
2691 clib_memset (hm->trace, 0, sizeof (hm->trace));
Shwethaa91cbe62016-08-08 15:51:04 +01002692 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
Ole Troan944f5482016-05-24 11:56:58 +02002693 return (0);
2694}
2695
2696VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2697
Damjan Marion38173502019-02-13 19:30:09 +01002698#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05002699void
2700ip6_hbh_set_next_override (uword next)
Shwethaa91cbe62016-08-08 15:51:04 +01002701{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002702 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Shwethaa91cbe62016-08-08 15:51:04 +01002703
2704 hm->next_override = next;
2705}
2706
Ole Troan944f5482016-05-24 11:56:58 +02002707int
2708ip6_hbh_register_option (u8 option,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002709 int options (vlib_buffer_t * b, ip6_header_t * ip,
2710 ip6_hop_by_hop_option_t * opt),
2711 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
Ole Troan944f5482016-05-24 11:56:58 +02002712{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002713 ip6_main_t *im = &ip6_main;
2714 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002715
Neale Ranns756cd942018-04-06 09:18:11 -07002716 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002717
2718 /* Already registered */
2719 if (hm->options[option])
2720 return (-1);
2721
2722 hm->options[option] = options;
2723 hm->trace[option] = trace;
2724
2725 /* Set global variable */
2726 im->hbh_enabled = 1;
2727
2728 return (0);
2729}
2730
2731int
2732ip6_hbh_unregister_option (u8 option)
2733{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002734 ip6_main_t *im = &ip6_main;
2735 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002736
Neale Ranns756cd942018-04-06 09:18:11 -07002737 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002738
2739 /* Not registered */
2740 if (!hm->options[option])
2741 return (-1);
2742
2743 hm->options[option] = NULL;
2744 hm->trace[option] = NULL;
2745
2746 /* Disable global knob if this was the last option configured */
2747 int i;
2748 bool found = false;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002749 for (i = 0; i < 256; i++)
2750 {
2751 if (hm->options[option])
2752 {
2753 found = true;
2754 break;
2755 }
Ole Troan944f5482016-05-24 11:56:58 +02002756 }
Ole Troan944f5482016-05-24 11:56:58 +02002757 if (!found)
2758 im->hbh_enabled = 0;
2759
2760 return (0);
2761}
2762
Ed Warnickecb9cada2015-12-08 15:45:58 -07002763/* Global IP6 main. */
2764ip6_main_t ip6_main;
Damjan Marion38173502019-02-13 19:30:09 +01002765#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07002766
2767static clib_error_t *
2768ip6_lookup_init (vlib_main_t * vm)
2769{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002770 ip6_main_t *im = &ip6_main;
2771 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002772 uword i;
2773
Damjan Marion8b3191e2016-11-09 19:54:20 +01002774 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2775 return error;
2776
Ed Warnickecb9cada2015-12-08 15:45:58 -07002777 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2778 {
2779 u32 j, i0, i1;
2780
2781 i0 = i / 32;
2782 i1 = i % 32;
2783
2784 for (j = 0; j < i0; j++)
2785 im->fib_masks[i].as_u32[j] = ~0;
2786
2787 if (i1)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002788 im->fib_masks[i].as_u32[i0] =
2789 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002790 }
2791
2792 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2793
2794 if (im->lookup_table_nbuckets == 0)
2795 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2796
Dave Barachd7cb1b52016-12-09 09:52:16 -05002797 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798
2799 if (im->lookup_table_size == 0)
2800 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
Dave Barach75fc8542016-10-11 16:16:02 -04002801
Neale Rannsae809832018-11-23 09:00:27 -08002802 clib_bihash_init_24_8 (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
Dave Barachd7cb1b52016-12-09 09:52:16 -05002803 "ip6 FIB fwding table",
2804 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Rannsae809832018-11-23 09:00:27 -08002805 clib_bihash_init_24_8 (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002806 "ip6 FIB non-fwding table",
2807 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Rannsae809832018-11-23 09:00:27 -08002808 clib_bihash_init_40_8 (&im->ip6_mtable.ip6_mhash,
2809 "ip6 mFIB table",
2810 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002811
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812 /* Create FIB with index 0 and table id of 0. */
Neale Ranns15002542017-09-10 04:39:11 -07002813 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2814 FIB_SOURCE_DEFAULT_ROUTE);
2815 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2816 MFIB_SOURCE_DEFAULT_ROUTE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002817
2818 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002819 pg_node_t *pn;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002820 pn = pg_get_node (ip6_lookup_node.index);
2821 pn->unformat_edit = unformat_pg_ip6_header;
2822 }
2823
Ole Troan944f5482016-05-24 11:56:58 +02002824 /* Unless explicitly configured, don't process HBH options */
2825 im->hbh_enabled = 0;
2826
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827 {
2828 icmp6_neighbor_solicitation_header_t p;
2829
Dave Barachb7b92992018-10-17 10:38:51 -04002830 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831
Dave Barachd7cb1b52016-12-09 09:52:16 -05002832 p.ip.ip_version_traffic_class_and_flow_label =
2833 clib_host_to_net_u32 (0x6 << 28);
2834 p.ip.payload_length =
2835 clib_host_to_net_u16 (sizeof (p) -
2836 STRUCT_OFFSET_OF
2837 (icmp6_neighbor_solicitation_header_t, neighbor));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838 p.ip.protocol = IP_PROTOCOL_ICMP6;
2839 p.ip.hop_limit = 255;
2840 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2841
2842 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2843
Dave Barachd7cb1b52016-12-09 09:52:16 -05002844 p.link_layer_option.header.type =
2845 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2846 p.link_layer_option.header.n_data_u64s =
2847 sizeof (p.link_layer_option) / sizeof (u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002848
2849 vlib_packet_template_init (vm,
2850 &im->discover_neighbor_packet_template,
2851 &p, sizeof (p),
2852 /* alloc chunk size */ 8,
2853 "ip6 neighbor discovery");
2854 }
2855
Dave Barach203c6322016-06-26 10:29:03 -04002856 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002857}
2858
2859VLIB_INIT_FUNCTION (ip6_lookup_init);
2860
Dave Barach75fc8542016-10-11 16:16:02 -04002861static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07002862test_ip6_link_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002863 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002864{
2865 u8 mac[6];
2866 ip6_address_t _a, *a = &_a;
2867
2868 if (unformat (input, "%U", unformat_ethernet_address, mac))
2869 {
2870 ip6_link_local_address_from_ethernet_mac_address (a, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002871 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2873 vlib_cli_output (vm, "Original MAC address: %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002874 format_ethernet_address, mac);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002875 }
Dave Barach75fc8542016-10-11 16:16:02 -04002876
Ed Warnickecb9cada2015-12-08 15:45:58 -07002877 return 0;
2878}
2879
Billy McFall0683c9c2016-10-13 08:27:31 -04002880/*?
2881 * This command converts the given MAC Address into an IPv6 link-local
2882 * address.
2883 *
2884 * @cliexpar
2885 * Example of how to create an IPv6 link-local address:
2886 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2887 * Link local address: fe80::14d9:e0ff:fe91:7986
2888 * Original MAC address: 16:d9:e0:91:79:86
2889 * @cliexend
2890?*/
2891/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002892VLIB_CLI_COMMAND (test_link_command, static) =
2893{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002894 .path = "test ip6 link",
Dave Barach75fc8542016-10-11 16:16:02 -04002895 .function = test_ip6_link_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002896 .short_help = "test ip6 link <mac-address>",
2897};
Billy McFall0683c9c2016-10-13 08:27:31 -04002898/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002899
Damjan Marion38173502019-02-13 19:30:09 +01002900#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05002901int
2902vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002903{
Neale Ranns107e7d42017-04-11 09:55:19 -07002904 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002905
Neale Ranns107e7d42017-04-11 09:55:19 -07002906 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002907
Neale Ranns107e7d42017-04-11 09:55:19 -07002908 if (~0 == fib_index)
2909 return VNET_API_ERROR_NO_SUCH_FIB;
2910
Neale Ranns227038a2017-04-21 01:07:59 -07002911 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2912 flow_hash_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002913
Neale Ranns227038a2017-04-21 01:07:59 -07002914 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002915}
Damjan Marion38173502019-02-13 19:30:09 +01002916#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917
2918static clib_error_t *
2919set_ip6_flow_hash_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002920 unformat_input_t * input,
2921 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002922{
2923 int matched = 0;
2924 u32 table_id = 0;
2925 u32 flow_hash_config = 0;
2926 int rv;
2927
Dave Barachd7cb1b52016-12-09 09:52:16 -05002928 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2929 {
2930 if (unformat (input, "table %d", &table_id))
2931 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002932#define _(a,v) \
2933 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
Dave Barachd7cb1b52016-12-09 09:52:16 -05002934 foreach_flow_hash_bit
Ed Warnickecb9cada2015-12-08 15:45:58 -07002935#undef _
Dave Barachd7cb1b52016-12-09 09:52:16 -05002936 else
2937 break;
2938 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002939
2940 if (matched == 0)
2941 return clib_error_return (0, "unknown input `%U'",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002942 format_unformat_error, input);
Dave Barach75fc8542016-10-11 16:16:02 -04002943
Ed Warnickecb9cada2015-12-08 15:45:58 -07002944 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2945 switch (rv)
2946 {
Neale Ranns227038a2017-04-21 01:07:59 -07002947 case 0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002948 break;
2949
2950 case -1:
2951 return clib_error_return (0, "no such FIB table %d", table_id);
Dave Barach75fc8542016-10-11 16:16:02 -04002952
Ed Warnickecb9cada2015-12-08 15:45:58 -07002953 default:
2954 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2955 break;
2956 }
Dave Barach75fc8542016-10-11 16:16:02 -04002957
Ed Warnickecb9cada2015-12-08 15:45:58 -07002958 return 0;
2959}
2960
Billy McFall0683c9c2016-10-13 08:27:31 -04002961/*?
2962 * Configure the set of IPv6 fields used by the flow hash.
2963 *
2964 * @cliexpar
2965 * @parblock
2966 * Example of how to set the flow hash on a given table:
Billy McFallebb9a6a2016-10-17 11:35:32 -04002967 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2968 *
Billy McFall0683c9c2016-10-13 08:27:31 -04002969 * Example of display the configured flow hash:
2970 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -04002971 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2972 * @::/0
2973 * unicast-ip6-chain
2974 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2975 * [0] [@0]: dpo-drop ip6
2976 * fe80::/10
2977 * unicast-ip6-chain
2978 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2979 * [0] [@2]: dpo-receive
2980 * ff02::1/128
2981 * unicast-ip6-chain
2982 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2983 * [0] [@2]: dpo-receive
2984 * ff02::2/128
2985 * unicast-ip6-chain
2986 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2987 * [0] [@2]: dpo-receive
2988 * ff02::16/128
2989 * unicast-ip6-chain
2990 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2991 * [0] [@2]: dpo-receive
2992 * ff02::1:ff00:0/104
2993 * unicast-ip6-chain
2994 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
2995 * [0] [@2]: dpo-receive
2996 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
2997 * @::/0
2998 * unicast-ip6-chain
2999 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3000 * [0] [@0]: dpo-drop ip6
3001 * @::a:1:1:0:4/126
3002 * unicast-ip6-chain
3003 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3004 * [0] [@4]: ipv6-glean: af_packet0
3005 * @::a:1:1:0:7/128
3006 * unicast-ip6-chain
3007 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3008 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3009 * fe80::/10
3010 * unicast-ip6-chain
3011 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3012 * [0] [@2]: dpo-receive
3013 * fe80::fe:3eff:fe3e:9222/128
3014 * unicast-ip6-chain
3015 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3016 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3017 * ff02::1/128
3018 * unicast-ip6-chain
3019 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3020 * [0] [@2]: dpo-receive
3021 * ff02::2/128
3022 * unicast-ip6-chain
3023 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3024 * [0] [@2]: dpo-receive
3025 * ff02::16/128
3026 * unicast-ip6-chain
3027 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3028 * [0] [@2]: dpo-receive
3029 * ff02::1:ff00:0/104
3030 * unicast-ip6-chain
3031 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3032 * [0] [@2]: dpo-receive
Billy McFall0683c9c2016-10-13 08:27:31 -04003033 * @cliexend
3034 * @endparblock
3035?*/
3036/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003037VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3038{
3039 .path = "set ip6 flow-hash",
3040 .short_help =
3041 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3042 .function = set_ip6_flow_hash_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003043};
Billy McFall0683c9c2016-10-13 08:27:31 -04003044/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003045
3046static clib_error_t *
3047show_ip6_local_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003048 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003049{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003050 ip6_main_t *im = &ip6_main;
3051 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003052 int i;
Dave Barach75fc8542016-10-11 16:16:02 -04003053
Ed Warnickecb9cada2015-12-08 15:45:58 -07003054 vlib_cli_output (vm, "Protocols handled by ip6_local");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003055 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003056 {
3057 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02003058 {
3059
3060 u32 node_index = vlib_get_node (vm,
3061 ip6_local_node.index)->
3062 next_nodes[lm->local_next_by_ip_protocol[i]];
3063 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3064 node_index);
3065 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003066 }
3067 return 0;
3068}
3069
3070
3071
Billy McFall0683c9c2016-10-13 08:27:31 -04003072/*?
3073 * Display the set of protocols handled by the local IPv6 stack.
3074 *
3075 * @cliexpar
3076 * Example of how to display local protocol table:
3077 * @cliexstart{show ip6 local}
3078 * Protocols handled by ip6_local
3079 * 17
3080 * 43
3081 * 58
3082 * 115
3083 * @cliexend
3084?*/
3085/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003086VLIB_CLI_COMMAND (show_ip6_local, static) =
3087{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003088 .path = "show ip6 local",
3089 .function = show_ip6_local_command_fn,
Billy McFall0683c9c2016-10-13 08:27:31 -04003090 .short_help = "show ip6 local",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003091};
Billy McFall0683c9c2016-10-13 08:27:31 -04003092/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003093
Damjan Marion38173502019-02-13 19:30:09 +01003094#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05003095int
3096vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3097 u32 table_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003098{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003099 vnet_main_t *vnm = vnet_get_main ();
3100 vnet_interface_main_t *im = &vnm->interface_main;
3101 ip6_main_t *ipm = &ip6_main;
3102 ip_lookup_main_t *lm = &ipm->lookup_main;
3103 vnet_classify_main_t *cm = &vnet_classify_main;
Neale Rannsdf089a82016-10-02 16:39:06 +01003104 ip6_address_t *if_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003105
3106 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3107 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3108
3109 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3110 return VNET_API_ERROR_NO_SUCH_ENTRY;
3111
3112 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003113 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003114
Neale Ranns6cfc39c2017-02-14 01:44:25 -08003115 if_addr = ip6_interface_first_address (ipm, sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01003116
3117 if (NULL != if_addr)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003118 {
Neale Rannsdf089a82016-10-02 16:39:06 +01003119 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003120 .fp_len = 128,
3121 .fp_proto = FIB_PROTOCOL_IP6,
3122 .fp_addr.ip6 = *if_addr,
Neale Rannsdf089a82016-10-02 16:39:06 +01003123 };
3124 u32 fib_index;
3125
Dave Barachd7cb1b52016-12-09 09:52:16 -05003126 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3127 sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01003128
3129
Dave Barachd7cb1b52016-12-09 09:52:16 -05003130 if (table_index != (u32) ~ 0)
3131 {
3132 dpo_id_t dpo = DPO_INVALID;
Neale Rannsdf089a82016-10-02 16:39:06 +01003133
Dave Barachd7cb1b52016-12-09 09:52:16 -05003134 dpo_set (&dpo,
3135 DPO_CLASSIFY,
3136 DPO_PROTO_IP6,
3137 classify_dpo_create (DPO_PROTO_IP6, table_index));
Neale Rannsdf089a82016-10-02 16:39:06 +01003138
Dave Barachd7cb1b52016-12-09 09:52:16 -05003139 fib_table_entry_special_dpo_add (fib_index,
3140 &pfx,
3141 FIB_SOURCE_CLASSIFY,
3142 FIB_ENTRY_FLAG_NONE, &dpo);
3143 dpo_reset (&dpo);
3144 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003145 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003146 {
3147 fib_table_entry_special_remove (fib_index,
3148 &pfx, FIB_SOURCE_CLASSIFY);
3149 }
3150 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003151
Ed Warnickecb9cada2015-12-08 15:45:58 -07003152 return 0;
3153}
Damjan Marion38173502019-02-13 19:30:09 +01003154#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07003155
3156static clib_error_t *
3157set_ip6_classify_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003158 unformat_input_t * input,
3159 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003160{
3161 u32 table_index = ~0;
3162 int table_index_set = 0;
3163 u32 sw_if_index = ~0;
3164 int rv;
Dave Barach75fc8542016-10-11 16:16:02 -04003165
Dave Barachd7cb1b52016-12-09 09:52:16 -05003166 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3167 {
3168 if (unformat (input, "table-index %d", &table_index))
3169 table_index_set = 1;
3170 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3171 vnet_get_main (), &sw_if_index))
3172 ;
3173 else
3174 break;
3175 }
Dave Barach75fc8542016-10-11 16:16:02 -04003176
Ed Warnickecb9cada2015-12-08 15:45:58 -07003177 if (table_index_set == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003178 return clib_error_return (0, "classify table-index must be specified");
Dave Barach75fc8542016-10-11 16:16:02 -04003179
Ed Warnickecb9cada2015-12-08 15:45:58 -07003180 if (sw_if_index == ~0)
3181 return clib_error_return (0, "interface / subif must be specified");
3182
3183 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3184
3185 switch (rv)
3186 {
3187 case 0:
3188 break;
3189
3190 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3191 return clib_error_return (0, "No such interface");
3192
3193 case VNET_API_ERROR_NO_SUCH_ENTRY:
3194 return clib_error_return (0, "No such classifier table");
3195 }
3196 return 0;
3197}
3198
Billy McFall0683c9c2016-10-13 08:27:31 -04003199/*?
3200 * Assign a classification table to an interface. The classification
3201 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3202 * commands. Once the table is create, use this command to filter packets
3203 * on an interface.
3204 *
3205 * @cliexpar
3206 * Example of how to assign a classification table to an interface:
3207 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3208?*/
3209/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003210VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3211{
3212 .path = "set ip6 classify",
3213 .short_help =
3214 "set ip6 classify intfc <interface> table-index <classify-idx>",
3215 .function = set_ip6_classify_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003216};
Billy McFall0683c9c2016-10-13 08:27:31 -04003217/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003218
3219static clib_error_t *
3220ip6_config (vlib_main_t * vm, unformat_input_t * input)
3221{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003222 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003223 uword heapsize = 0;
3224 u32 tmp;
3225 u32 nbuckets = 0;
3226
Dave Barachd7cb1b52016-12-09 09:52:16 -05003227 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3228 {
3229 if (unformat (input, "hash-buckets %d", &tmp))
3230 nbuckets = tmp;
Neale Ranns1ec36522017-11-29 05:20:37 -08003231 else if (unformat (input, "heap-size %U",
3232 unformat_memory_size, &heapsize))
3233 ;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003234 else
3235 return clib_error_return (0, "unknown input '%U'",
3236 format_unformat_error, input);
3237 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003238
3239 im->lookup_table_nbuckets = nbuckets;
3240 im->lookup_table_size = heapsize;
3241
3242 return 0;
3243}
3244
3245VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003246
3247/*
3248 * fd.io coding-style-patch-verification: ON
3249 *
3250 * Local Variables:
3251 * eval: (c-set-style "gnu")
3252 * End:
3253 */