blob: 067db77c412441048c1945892d7aa879f767ab9b [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]);
Dave Barachc4abafd2019-09-04 12:09:32 -04001020 u8 *data_this_buffer;
1021 u8 length_odd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022
Dave Barachd7cb1b52016-12-09 09:52:16 -05001023 ASSERT (bogus_lengthp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024 *bogus_lengthp = 0;
1025
1026 /* Initialize checksum with ip header. */
1027 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1028 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
Dave Barachc4abafd2019-09-04 12:09:32 -04001029 data_this_buffer = (u8 *) (ip0 + 1);
Dave Barach75fc8542016-10-11 16:16:02 -04001030
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1032 {
1033 sum0 = ip_csum_with_carry (sum0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001034 clib_mem_unaligned (&ip0->
1035 src_address.as_uword[i],
1036 uword));
1037 sum0 =
1038 ip_csum_with_carry (sum0,
1039 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1040 uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041 }
1042
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301043 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1044 * or UDP-Ping packets */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001045 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001047 u32 skip_bytes;
1048 ip6_hop_by_hop_ext_t *ext_hdr =
1049 (ip6_hop_by_hop_ext_t *) data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
1051 /* validate really icmp6 next */
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301052 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1053 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
Dave Barachd7cb1b52016-12-09 09:52:16 -05001055 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1056 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
Dave Barach75fc8542016-10-11 16:16:02 -04001057
Dave Barachd7cb1b52016-12-09 09:52:16 -05001058 payload_length_host_byte_order -= skip_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059 headers_size += skip_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001060 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061
1062 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
John Lo3bc6bc22019-08-03 14:36:39 -04001063
1064 if (p0)
1065 {
1066 u32 n_ip_bytes_this_buffer =
1067 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
1068 if (n_this_buffer + headers_size > n_ip_bytes_this_buffer)
1069 {
1070 n_this_buffer = p0->current_length > headers_size ?
1071 n_ip_bytes_this_buffer - headers_size : 0;
1072 }
1073 }
1074
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075 while (1)
1076 {
1077 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1078 n_bytes_left -= n_this_buffer;
1079 if (n_bytes_left == 0)
1080 break;
1081
Dave Barachc4abafd2019-09-04 12:09:32 -04001082 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001084 {
1085 *bogus_lengthp = 1;
1086 return 0xfefe;
1087 }
Dave Barachc4abafd2019-09-04 12:09:32 -04001088
1089 length_odd = (n_this_buffer & 1);
1090
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091 p0 = vlib_get_buffer (vm, p0->next_buffer);
1092 data_this_buffer = vlib_buffer_get_current (p0);
John Lo1cf00072019-04-09 10:23:56 -04001093 n_this_buffer = clib_min (p0->current_length, n_bytes_left);
Dave Barachc4abafd2019-09-04 12:09:32 -04001094
1095 if (PREDICT_FALSE (length_odd))
1096 {
1097 /* Prepend a 0 or the resulting checksum will be incorrect. */
1098 data_this_buffer--;
1099 n_this_buffer++;
1100 n_bytes_left++;
1101 data_this_buffer[0] = 0;
1102 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
Dave Barachd7cb1b52016-12-09 09:52:16 -05001105 sum16 = ~ip_csum_fold (sum0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106
1107 return sum16;
1108}
1109
Dave Barachd7cb1b52016-12-09 09:52:16 -05001110u32
1111ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001113 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1114 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 u16 sum16;
1116 int bogus_length;
1117
1118 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1119 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1120 || ip0->protocol == IP_PROTOCOL_ICMP6
1121 || ip0->protocol == IP_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001122 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123
1124 udp0 = (void *) (ip0 + 1);
1125 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1126 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001127 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1128 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129 return p0->flags;
1130 }
1131
1132 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1133
Damjan Marion213b5aa2017-07-13 21:19:27 +02001134 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1135 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136
1137 return p0->flags;
1138}
Damjan Marion38173502019-02-13 19:30:09 +01001139#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301141/**
1142 * @brief returns number of links on which src is reachable.
1143 */
1144always_inline int
1145ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1146{
1147 const load_balance_t *lb0;
1148 index_t lbi;
Florin Corasf3a3bad2018-03-28 02:18:29 -07001149 u32 fib_index;
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301150
Florin Corasf3a3bad2018-03-28 02:18:29 -07001151 fib_index = vec_elt (im->fib_index_by_sw_if_index,
1152 vnet_buffer (b)->sw_if_index[VLIB_RX]);
1153 fib_index =
1154 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
1155 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301156
Simon Zhange7eba482019-08-25 15:30:45 +08001157 lbi = ip6_fib_table_fwding_lookup (fib_index, &i->src_address);
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301158 lb0 = load_balance_get (lbi);
1159
1160 return (fib_urpf_check_size (lb0->lb_urpf));
1161}
1162
rootc9d1c5b2017-08-15 12:58:31 -04001163always_inline u8
1164ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1165 u32 * udp_offset0)
1166{
1167 u32 proto0;
1168 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1169 if (proto0 != IP_PROTOCOL_UDP)
1170 {
1171 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1172 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1173 }
1174 return proto0;
1175}
1176
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001177/* *INDENT-OFF* */
1178VNET_FEATURE_ARC_INIT (ip6_local) =
1179{
1180 .arc_name = "ip6-local",
1181 .start_nodes = VNET_FEATURES ("ip6-local"),
1182};
1183/* *INDENT-ON* */
1184
Benoît Ganne26a10192019-02-14 15:32:45 +01001185always_inline uword
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001186ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1187 vlib_frame_t * frame, int head_of_feature_arc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001189 ip6_main_t *im = &ip6_main;
1190 ip_lookup_main_t *lm = &im->lookup_main;
Benoît Ganne26a10192019-02-14 15:32:45 +01001191 u32 *from, n_left_from;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001192 vlib_node_runtime_t *error_node =
1193 vlib_node_get_runtime (vm, ip6_input_node.index);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001194 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
Benoît Ganne26a10192019-02-14 15:32:45 +01001195 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1196 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197
1198 from = vlib_frame_vector_args (frame);
1199 n_left_from = frame->n_vectors;
Dave Barach75fc8542016-10-11 16:16:02 -04001200
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201 if (node->flags & VLIB_NODE_FLAG_TRACE)
1202 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1203
Benoît Ganne26a10192019-02-14 15:32:45 +01001204 vlib_get_buffers (vm, from, bufs, n_left_from);
1205 b = bufs;
1206 next = nexts;
1207
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001208 while (n_left_from > 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001210 /* Prefetch next iteration. */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001211 if (n_left_from >= 6)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001213 vlib_prefetch_buffer_header (b[4], STORE);
1214 vlib_prefetch_buffer_header (b[5], STORE);
1215 vlib_prefetch_buffer_data (b[2], LOAD);
1216 vlib_prefetch_buffer_data (b[3], LOAD);
Benoît Ganne26a10192019-02-14 15:32:45 +01001217 }
Dave Barach75fc8542016-10-11 16:16:02 -04001218
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001219 u8 error[2];
1220 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1221 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
Dave Barach75fc8542016-10-11 16:16:02 -04001222
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001223 ip6_header_t *ip[2];
1224 ip[0] = vlib_buffer_get_current (b[0]);
1225 ip[1] = vlib_buffer_get_current (b[1]);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001226
Benoît Ganne26a10192019-02-14 15:32:45 +01001227 if (head_of_feature_arc)
1228 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001229 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1230 vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001232 u8 type[2];
1233 type[0] = lm->builtin_protocol_by_ip_protocol[ip[0]->protocol];
1234 type[1] = lm->builtin_protocol_by_ip_protocol[ip[1]->protocol];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001236 u32 flags[2];
1237 flags[0] = b[0]->flags;
1238 flags[1] = b[1]->flags;
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001239
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001240 u32 good_l4_csum[2];
1241 good_l4_csum[0] =
1242 flags[0] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1243 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1244 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1245 good_l4_csum[1] =
1246 flags[1] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
Benoît Ganne26a10192019-02-14 15:32:45 +01001247 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1248 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
Filip Tehlarb601f222017-01-02 10:22:56 +01001249
Damjan Marion34e823f2019-02-19 08:55:18 +01001250 u32 udp_offset[2] = { };
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001251 u8 is_tcp_udp[2];
1252 is_tcp_udp[0] =
1253 ip6_next_proto_is_tcp_udp (b[0], ip[0], &udp_offset[0]);
1254 is_tcp_udp[1] =
1255 ip6_next_proto_is_tcp_udp (b[1], ip[1], &udp_offset[1]);
1256 i16 len_diff[2] = { 0 };
1257 if (PREDICT_TRUE (is_tcp_udp[0]))
Shwethab78292e2016-09-13 11:51:00 +01001258 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001259 udp_header_t *udp =
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001260 (udp_header_t *) ((u8 *) ip[0] + udp_offset[0]);
1261 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UDP
Benoît Ganne26a10192019-02-14 15:32:45 +01001262 && udp->checksum == 0;
1263 /* optimistically verify UDP length. */
1264 u16 ip_len, udp_len;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001265 ip_len = clib_net_to_host_u16 (ip[0]->payload_length);
Benoît Ganne26a10192019-02-14 15:32:45 +01001266 udp_len = clib_net_to_host_u16 (udp->length);
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001267 len_diff[0] = ip_len - udp_len;
Shwethab78292e2016-09-13 11:51:00 +01001268 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001269 if (PREDICT_TRUE (is_tcp_udp[1]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001270 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001271 udp_header_t *udp =
1272 (udp_header_t *) ((u8 *) ip[1] + udp_offset[1]);
1273 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UDP
1274 && udp->checksum == 0;
1275 /* optimistically verify UDP length. */
1276 u16 ip_len, udp_len;
1277 ip_len = clib_net_to_host_u16 (ip[1]->payload_length);
1278 udp_len = clib_net_to_host_u16 (udp->length);
1279 len_diff[1] = ip_len - udp_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001280 }
1281
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001282 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1283 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1284
1285 len_diff[0] = type[0] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[0] : 0;
1286 len_diff[1] = type[1] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[1] : 0;
1287
1288 u8 need_csum[2];
1289 need_csum[0] = type[0] != IP_BUILTIN_PROTOCOL_UNKNOWN
1290 && !good_l4_csum[0]
1291 && !(flags[0] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1292 need_csum[1] = type[1] != IP_BUILTIN_PROTOCOL_UNKNOWN
1293 && !good_l4_csum[1]
1294 && !(flags[1] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1295 if (PREDICT_FALSE (need_csum[0]))
1296 {
1297 flags[0] = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1298 good_l4_csum[0] = flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1299 }
1300 if (PREDICT_FALSE (need_csum[1]))
1301 {
1302 flags[1] = ip6_tcp_udp_icmp_validate_checksum (vm, b[1]);
1303 good_l4_csum[1] = flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1304 }
1305
1306 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1307 error[0] = len_diff[0] < 0 ? IP6_ERROR_UDP_LENGTH : error[0];
1308 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1309 error[1] = len_diff[1] < 0 ? IP6_ERROR_UDP_LENGTH : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001310
Benoît Ganne26a10192019-02-14 15:32:45 +01001311 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1312 IP6_ERROR_UDP_CHECKSUM,
1313 "Wrong IP6 errors constants");
1314 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1315 IP6_ERROR_ICMP_CHECKSUM,
1316 "Wrong IP6 errors constants");
1317
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001318 error[0] =
1319 !good_l4_csum[0] ? IP6_ERROR_UDP_CHECKSUM + type[0] : error[0];
1320 error[1] =
1321 !good_l4_csum[1] ? IP6_ERROR_UDP_CHECKSUM + type[1] : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001322
1323 /* Drop packets from unroutable hosts. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001324 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001325 u8 unroutable[2];
1326 unroutable[0] = error[0] == IP6_ERROR_UNKNOWN_PROTOCOL
1327 && type[0] != IP_BUILTIN_PROTOCOL_ICMP
1328 && !ip6_address_is_link_local_unicast (&ip[0]->src_address);
1329 unroutable[1] = error[1] == IP6_ERROR_UNKNOWN_PROTOCOL
1330 && type[1] != IP_BUILTIN_PROTOCOL_ICMP
1331 && !ip6_address_is_link_local_unicast (&ip[1]->src_address);
1332 if (PREDICT_FALSE (unroutable[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001334 error[0] =
1335 !ip6_urpf_loose_check (im, b[0],
1336 ip[0]) ? IP6_ERROR_SRC_LOOKUP_MISS
1337 : error[0];
1338 }
1339 if (PREDICT_FALSE (unroutable[1]))
1340 {
1341 error[1] =
1342 !ip6_urpf_loose_check (im, b[1],
1343 ip[1]) ? IP6_ERROR_SRC_LOOKUP_MISS
1344 : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345 }
1346
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001347 vnet_buffer (b[0])->ip.fib_index =
1348 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1349 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1350 vnet_buffer (b[0])->ip.fib_index;
1351 vnet_buffer (b[1])->ip.fib_index =
1352 vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ?
1353 vnet_buffer (b[1])->sw_if_index[VLIB_TX] :
1354 vnet_buffer (b[1])->ip.fib_index;
Benoît Ganne26a10192019-02-14 15:32:45 +01001355 } /* head_of_feature_arc */
Florin Corascea194d2017-10-02 00:18:51 -07001356
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001357 next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol];
1358 next[0] =
1359 error[0] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1360 next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol];
1361 next[1] =
1362 error[1] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[1];
Florin Corascea194d2017-10-02 00:18:51 -07001363
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001364 b[0]->error = error_node->errors[0];
1365 b[1]->error = error_node->errors[1];
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001366
Benoît Ganne26a10192019-02-14 15:32:45 +01001367 if (head_of_feature_arc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001369 u8 ip6_unknown[2];
1370 ip6_unknown[0] = error[0] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1371 ip6_unknown[1] = error[1] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1372 if (PREDICT_TRUE (ip6_unknown[0]))
Shwethab78292e2016-09-13 11:51:00 +01001373 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001374 u32 next32 = next[0];
Benoît Ganne26a10192019-02-14 15:32:45 +01001375 vnet_feature_arc_start (arc_index,
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001376 vnet_buffer (b[0])->sw_if_index
1377 [VLIB_RX], &next32, b[0]);
1378 next[0] = next32;
1379 }
1380 if (PREDICT_TRUE (ip6_unknown[1]))
1381 {
1382 u32 next32 = next[1];
1383 vnet_feature_arc_start (arc_index,
1384 vnet_buffer (b[1])->sw_if_index
1385 [VLIB_RX], &next32, b[1]);
1386 next[1] = next32;
Shwethab78292e2016-09-13 11:51:00 +01001387 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388 }
Dave Barach75fc8542016-10-11 16:16:02 -04001389
Benoît Ganne26a10192019-02-14 15:32:45 +01001390 /* next */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001391 b += 2;
1392 next += 2;
1393 n_left_from -= 2;
Benoît Ganne26a10192019-02-14 15:32:45 +01001394 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001395
Benoît Ganne26a10192019-02-14 15:32:45 +01001396 while (n_left_from)
1397 {
1398 u8 error;
1399 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1400
1401 ip6_header_t *ip;
1402 ip = vlib_buffer_get_current (b[0]);
1403
1404 if (head_of_feature_arc)
1405 {
1406 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1407 u8 type = lm->builtin_protocol_by_ip_protocol[ip->protocol];
1408
1409 u32 flags = b[0]->flags;
1410 u32 good_l4_csum =
1411 flags & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1412 VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1413 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1414
1415 u32 udp_offset;
1416 i16 len_diff = 0;
1417 u8 is_tcp_udp = ip6_next_proto_is_tcp_udp (b[0], ip, &udp_offset);
1418 if (PREDICT_TRUE (is_tcp_udp))
1419 {
1420 udp_header_t *udp = (udp_header_t *) ((u8 *) ip + udp_offset);
1421 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1422 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UDP
1423 && udp->checksum == 0;
1424 /* optimistically verify UDP length. */
1425 u16 ip_len, udp_len;
1426 ip_len = clib_net_to_host_u16 (ip->payload_length);
1427 udp_len = clib_net_to_host_u16 (udp->length);
1428 len_diff = ip_len - udp_len;
1429 }
1430
1431 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UNKNOWN;
1432 len_diff = type == IP_BUILTIN_PROTOCOL_UDP ? len_diff : 0;
1433
1434 u8 need_csum = type != IP_BUILTIN_PROTOCOL_UNKNOWN && !good_l4_csum
1435 && !(flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1436 if (PREDICT_FALSE (need_csum))
1437 {
1438 flags = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1439 good_l4_csum = flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1440 }
1441
1442 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1443 error = len_diff < 0 ? IP6_ERROR_UDP_LENGTH : error;
1444
1445 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1446 IP6_ERROR_UDP_CHECKSUM,
1447 "Wrong IP6 errors constants");
1448 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1449 IP6_ERROR_ICMP_CHECKSUM,
1450 "Wrong IP6 errors constants");
1451
1452 error = !good_l4_csum ? IP6_ERROR_UDP_CHECKSUM + type : error;
1453
1454 /* Drop packets from unroutable hosts. */
1455 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1456 u8 unroutable = error == IP6_ERROR_UNKNOWN_PROTOCOL
1457 && type != IP_BUILTIN_PROTOCOL_ICMP
1458 && !ip6_address_is_link_local_unicast (&ip->src_address);
1459 if (PREDICT_FALSE (unroutable))
1460 {
1461 error =
1462 !ip6_urpf_loose_check (im, b[0],
1463 ip) ? IP6_ERROR_SRC_LOOKUP_MISS :
1464 error;
1465 }
1466
1467 vnet_buffer (b[0])->ip.fib_index =
1468 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1469 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1470 vnet_buffer (b[0])->ip.fib_index;
1471 } /* head_of_feature_arc */
1472
1473 next[0] = lm->local_next_by_ip_protocol[ip->protocol];
1474 next[0] =
1475 error != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1476
1477 b[0]->error = error_node->errors[0];
1478
1479 if (head_of_feature_arc)
1480 {
1481 if (PREDICT_TRUE (error == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1482 {
1483 u32 next32 = next[0];
1484 vnet_feature_arc_start (arc_index,
1485 vnet_buffer (b[0])->sw_if_index
1486 [VLIB_RX], &next32, b[0]);
1487 next[0] = next32;
1488 }
1489 }
1490
1491 /* next */
1492 b += 1;
1493 next += 1;
1494 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495 }
1496
Benoît Ganne26a10192019-02-14 15:32:45 +01001497 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498 return frame->n_vectors;
1499}
1500
Damjan Marion38173502019-02-13 19:30:09 +01001501VLIB_NODE_FN (ip6_local_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1502 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001503{
1504 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1505}
1506
Dave Barachd7cb1b52016-12-09 09:52:16 -05001507/* *INDENT-OFF* */
Damjan Marion38173502019-02-13 19:30:09 +01001508VLIB_REGISTER_NODE (ip6_local_node) =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001509{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001510 .name = "ip6-local",
1511 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001512 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513 .n_next_nodes = IP_LOCAL_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001514 .next_nodes =
1515 {
Neale Rannsd91c1db2017-07-31 02:30:50 -07001516 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1517 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001518 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1519 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
Juraj Sloboda3048b632018-10-02 11:13:53 +02001520 [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-reassembly",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001521 },
1522};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001523/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524
Damjan Marion38173502019-02-13 19:30:09 +01001525VLIB_NODE_FN (ip6_local_end_of_arc_node) (vlib_main_t * vm,
1526 vlib_node_runtime_t * node,
1527 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001528{
1529 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1530}
1531
1532/* *INDENT-OFF* */
Damjan Marion38173502019-02-13 19:30:09 +01001533VLIB_REGISTER_NODE (ip6_local_end_of_arc_node) = {
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001534 .name = "ip6-local-end-of-arc",
1535 .vector_size = sizeof (u32),
1536
1537 .format_trace = format_ip6_forward_next_trace,
1538 .sibling_of = "ip6-local",
1539};
1540
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001541VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1542 .arc_name = "ip6-local",
1543 .node_name = "ip6-local-end-of-arc",
1544 .runs_before = 0, /* not before any other features */
1545};
1546/* *INDENT-ON* */
1547
Damjan Marion38173502019-02-13 19:30:09 +01001548#ifdef CLIB_MARCH_VARIANT
1549extern vlib_node_registration_t ip6_local_node;
1550
1551#else
1552
Dave Barachd7cb1b52016-12-09 09:52:16 -05001553void
1554ip6_register_protocol (u32 protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001556 vlib_main_t *vm = vlib_get_main ();
1557 ip6_main_t *im = &ip6_main;
1558 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001559
1560 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001561 lm->local_next_by_ip_protocol[protocol] =
1562 vlib_node_add_next (vm, ip6_local_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563}
1564
Neale Rannsb538dd82019-05-21 06:54:54 -07001565void
1566ip6_unregister_protocol (u32 protocol)
1567{
1568 ip6_main_t *im = &ip6_main;
1569 ip_lookup_main_t *lm = &im->lookup_main;
1570
1571 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1572 lm->local_next_by_ip_protocol[protocol] = IP_LOCAL_NEXT_PUNT;
1573}
1574
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575clib_error_t *
John Lo86376342018-06-11 20:14:49 -04001576ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index,
1577 u8 refresh)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001578{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001579 vnet_main_t *vnm = vnet_get_main ();
1580 ip6_main_t *im = &ip6_main;
1581 icmp6_neighbor_solicitation_header_t *h;
1582 ip6_address_t *src;
1583 ip_interface_address_t *ia;
1584 ip_adjacency_t *adj;
1585 vnet_hw_interface_t *hi;
1586 vnet_sw_interface_t *si;
1587 vlib_buffer_t *b;
Neale Ranns7a272742017-05-30 02:08:14 -07001588 adj_index_t ai;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001589 u32 bi = 0;
1590 int bogus_length;
1591
1592 si = vnet_get_sw_interface (vnm, sw_if_index);
1593
1594 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1595 {
1596 return clib_error_return (0, "%U: interface %U down",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001597 format_ip6_address, dst,
1598 format_vnet_sw_if_index_name, vnm,
1599 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001600 }
1601
Dave Barachd7cb1b52016-12-09 09:52:16 -05001602 src =
1603 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1604 if (!src)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001605 {
1606 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
Dave Barach75fc8542016-10-11 16:16:02 -04001607 return clib_error_return
Dave Barachd7cb1b52016-12-09 09:52:16 -05001608 (0, "no matching interface address for destination %U (interface %U)",
1609 format_ip6_address, dst,
1610 format_vnet_sw_if_index_name, vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001611 }
1612
Dave Barachd7cb1b52016-12-09 09:52:16 -05001613 h =
1614 vlib_packet_template_get_packet (vm,
1615 &im->discover_neighbor_packet_template,
1616 &bi);
John Lo084606b2018-06-19 15:27:48 -04001617 if (!h)
1618 return clib_error_return (0, "ICMP6 NS packet allocation failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619
1620 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1621
1622 /* Destination address is a solicited node multicast address. We need to fill in
1623 the low 24 bits with low 24 bits of target's address. */
1624 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1625 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1626 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1627
1628 h->ip.src_address = src[0];
1629 h->neighbor.target_address = dst[0];
1630
Pavel Kotucek57808982017-08-02 08:20:19 +02001631 if (PREDICT_FALSE (!hi->hw_address))
1632 {
1633 return clib_error_return (0, "%U: interface %U do not support ip probe",
1634 format_ip6_address, dst,
1635 format_vnet_sw_if_index_name, vnm,
1636 sw_if_index);
1637 }
1638
Dave Barach178cf492018-11-13 16:34:13 -05001639 clib_memcpy_fast (h->link_layer_option.ethernet_address, hi->hw_address,
1640 vec_len (hi->hw_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001641
Dave Barach75fc8542016-10-11 16:16:02 -04001642 h->neighbor.icmp.checksum =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001643 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001644 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001645
1646 b = vlib_get_buffer (vm, bi);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001647 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1648 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001649
1650 /* Add encapsulation string for software interface (e.g. ethernet header). */
Neale Ranns7a272742017-05-30 02:08:14 -07001651 ip46_address_t nh = {
1652 .ip6 = *dst,
1653 };
1654
1655 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1656 VNET_LINK_IP6, &nh, sw_if_index);
1657 adj = adj_get (ai);
1658
Dave Barach59b25652017-09-10 15:04:27 -04001659 /* Peer has been previously resolved, retrieve glean adj instead */
John Lo86376342018-06-11 20:14:49 -04001660 if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE && refresh == 0)
Dave Barach59b25652017-09-10 15:04:27 -04001661 {
1662 adj_unlock (ai);
Ole Troan6ee40512018-02-12 18:14:39 +01001663 ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1664 VNET_LINK_IP6, sw_if_index, &nh);
Dave Barach59b25652017-09-10 15:04:27 -04001665 adj = adj_get (ai);
1666 }
1667
Ed Warnickecb9cada2015-12-08 15:45:58 -07001668 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1669 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1670
1671 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001672 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1673 u32 *to_next = vlib_frame_vector_args (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001674 to_next[0] = bi;
1675 f->n_vectors = 1;
1676 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1677 }
1678
Neale Ranns7a272742017-05-30 02:08:14 -07001679 adj_unlock (ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001680 return /* no error */ 0;
1681}
Damjan Marion38173502019-02-13 19:30:09 +01001682#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683
Dave Barachd7cb1b52016-12-09 09:52:16 -05001684typedef enum
1685{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001686 IP6_REWRITE_NEXT_DROP,
Chris Luke816f3e12016-06-14 16:24:47 -04001687 IP6_REWRITE_NEXT_ICMP_ERROR,
Ole Troan313f7e22018-04-10 16:02:51 +02001688 IP6_REWRITE_NEXT_FRAGMENT,
1689 IP6_REWRITE_N_NEXT /* Last */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690} ip6_rewrite_next_t;
1691
Neale Ranns889fe942017-06-01 05:43:19 -04001692/**
1693 * This bits of an IPv6 address to mask to construct a multicast
1694 * MAC address
1695 */
1696#define IP6_MCAST_ADDR_MASK 0xffffffff
1697
Ole Troanda6e11b2018-05-23 11:21:42 +02001698always_inline void
1699ip6_mtu_check (vlib_buffer_t * b, u16 packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001700 u16 adj_packet_bytes, bool is_locally_generated,
1701 u32 * next, u32 * error)
Ole Troanda6e11b2018-05-23 11:21:42 +02001702{
1703 if (adj_packet_bytes >= 1280 && packet_bytes > adj_packet_bytes)
1704 {
Ole Troan313f7e22018-04-10 16:02:51 +02001705 if (is_locally_generated)
1706 {
1707 /* IP fragmentation */
Ole Troan282093f2018-09-19 12:38:51 +02001708 ip_frag_set_vnet_buffer (b, adj_packet_bytes,
Ole Troanb3655e52018-08-16 22:08:49 +02001709 IP6_FRAG_NEXT_IP6_REWRITE, 0);
Ole Troan313f7e22018-04-10 16:02:51 +02001710 *next = IP6_REWRITE_NEXT_FRAGMENT;
Ole Troan282093f2018-09-19 12:38:51 +02001711 *error = IP6_ERROR_MTU_EXCEEDED;
Ole Troan313f7e22018-04-10 16:02:51 +02001712 }
1713 else
1714 {
1715 *error = IP6_ERROR_MTU_EXCEEDED;
1716 icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1717 adj_packet_bytes);
1718 *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1719 }
Ole Troanda6e11b2018-05-23 11:21:42 +02001720 }
1721}
1722
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723always_inline uword
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001724ip6_rewrite_inline_with_gso (vlib_main_t * vm,
1725 vlib_node_runtime_t * node,
1726 vlib_frame_t * frame,
1727 int do_counters, int is_midchain, int is_mcast,
1728 int do_gso)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001729{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001730 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1731 u32 *from = vlib_frame_vector_args (frame);
1732 u32 n_left_from, n_left_to_next, *to_next, next_index;
1733 vlib_node_runtime_t *error_node =
1734 vlib_node_get_runtime (vm, ip6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001735
1736 n_left_from = frame->n_vectors;
1737 next_index = node->cached_next_index;
Damjan Marion067cd622018-07-11 12:47:43 +02001738 u32 thread_index = vm->thread_index;
Dave Barach75fc8542016-10-11 16:16:02 -04001739
Ed Warnickecb9cada2015-12-08 15:45:58 -07001740 while (n_left_from > 0)
1741 {
1742 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1743
1744 while (n_left_from >= 4 && n_left_to_next >= 2)
1745 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001746 ip_adjacency_t *adj0, *adj1;
1747 vlib_buffer_t *p0, *p1;
1748 ip6_header_t *ip0, *ip1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001749 u32 pi0, rw_len0, next0, error0, adj_index0;
1750 u32 pi1, rw_len1, next1, error1, adj_index1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001751 u32 tx_sw_if_index0, tx_sw_if_index1;
Ole Troan313f7e22018-04-10 16:02:51 +02001752 bool is_locally_originated0, is_locally_originated1;
Dave Barach75fc8542016-10-11 16:16:02 -04001753
Ed Warnickecb9cada2015-12-08 15:45:58 -07001754 /* Prefetch next iteration. */
1755 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001756 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757
1758 p2 = vlib_get_buffer (vm, from[2]);
1759 p3 = vlib_get_buffer (vm, from[3]);
1760
1761 vlib_prefetch_buffer_header (p2, LOAD);
1762 vlib_prefetch_buffer_header (p3, LOAD);
1763
1764 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1765 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1766
1767 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1768 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1769 }
1770
1771 pi0 = to_next[0] = from[0];
1772 pi1 = to_next[1] = from[1];
1773
1774 from += 2;
1775 n_left_from -= 2;
1776 to_next += 2;
1777 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04001778
Ed Warnickecb9cada2015-12-08 15:45:58 -07001779 p0 = vlib_get_buffer (vm, pi0);
1780 p1 = vlib_get_buffer (vm, pi1);
1781
Neale Rannsf06aea52016-11-29 06:51:37 -08001782 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1783 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001784
Ed Warnickecb9cada2015-12-08 15:45:58 -07001785 ip0 = vlib_buffer_get_current (p0);
1786 ip1 = vlib_buffer_get_current (p1);
1787
1788 error0 = error1 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001789 next0 = next1 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001790
Ole Troan313f7e22018-04-10 16:02:51 +02001791 is_locally_originated0 =
1792 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1793 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001794 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001795 i32 hop_limit0 = ip0->hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001796
1797 /* Input node should have reject packets with hop limit 0. */
1798 ASSERT (ip0->hop_limit > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001799
1800 hop_limit0 -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001801
1802 ip0->hop_limit = hop_limit0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001803
Dave Barachd7cb1b52016-12-09 09:52:16 -05001804 /*
1805 * If the hop count drops below 1 when forwarding, generate
1806 * an ICMP response.
1807 */
1808 if (PREDICT_FALSE (hop_limit0 <= 0))
1809 {
1810 error0 = IP6_ERROR_TIME_EXPIRED;
1811 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1812 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1813 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1814 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1815 0);
1816 }
Neale Rannsf06aea52016-11-29 06:51:37 -08001817 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001818 else
1819 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001820 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001821 }
Ole Troan313f7e22018-04-10 16:02:51 +02001822 is_locally_originated1 =
1823 p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1824 if (PREDICT_TRUE (!is_locally_originated1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001825 {
Neale Rannsf06aea52016-11-29 06:51:37 -08001826 i32 hop_limit1 = ip1->hop_limit;
1827
1828 /* Input node should have reject packets with hop limit 0. */
1829 ASSERT (ip1->hop_limit > 0);
1830
1831 hop_limit1 -= 1;
1832
1833 ip1->hop_limit = hop_limit1;
1834
Dave Barachd7cb1b52016-12-09 09:52:16 -05001835 /*
1836 * If the hop count drops below 1 when forwarding, generate
1837 * an ICMP response.
1838 */
1839 if (PREDICT_FALSE (hop_limit1 <= 0))
1840 {
1841 error1 = IP6_ERROR_TIME_EXPIRED;
1842 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1843 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1844 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1845 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1846 0);
1847 }
1848 }
1849 else
1850 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001851 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001852 }
Neale Ranns107e7d42017-04-11 09:55:19 -07001853 adj0 = adj_get (adj_index0);
1854 adj1 = adj_get (adj_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001855
Ed Warnickecb9cada2015-12-08 15:45:58 -07001856 rw_len0 = adj0[0].rewrite_header.data_bytes;
1857 rw_len1 = adj1[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001858 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1859 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001860
Neale Ranns9c6a6132017-02-21 05:33:14 -08001861 if (do_counters)
1862 {
1863 vlib_increment_combined_counter
1864 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001865 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001866 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1867 vlib_increment_combined_counter
1868 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001869 thread_index, adj_index1, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001870 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1871 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001872
1873 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001874 u16 ip0_len =
1875 clib_net_to_host_u16 (ip0->payload_length) +
1876 sizeof (ip6_header_t);
1877 u16 ip1_len =
1878 clib_net_to_host_u16 (ip1->payload_length) +
1879 sizeof (ip6_header_t);
1880 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
1881 ip0_len = gso_mtu_sz (p0);
1882 if (do_gso && (p1->flags & VNET_BUFFER_F_GSO))
1883 ip1_len = gso_mtu_sz (p1);
1884
1885
1886
1887 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001888 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001889 is_locally_originated0, &next0, &error0);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001890 ip6_mtu_check (p1, ip1_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001891 adj1[0].rewrite_header.max_l3_packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001892 is_locally_originated1, &next1, &error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001893
Dave Barachd7cb1b52016-12-09 09:52:16 -05001894 /* Don't adjust the buffer for hop count issue; icmp-error node
Jim Thompsonf324dec2019-04-08 03:22:21 -05001895 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001896 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1897 {
1898 p0->current_data -= rw_len0;
1899 p0->current_length += rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001900
Dave Barachd7cb1b52016-12-09 09:52:16 -05001901 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1902 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1903 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001904
Neale Rannsb069a692017-03-15 12:34:25 -04001905 if (PREDICT_FALSE
1906 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1907 vnet_feature_arc_start (lm->output_feature_arc_index,
1908 tx_sw_if_index0, &next0, p0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001909 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001910 else
1911 {
1912 p0->error = error_node->errors[error0];
1913 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001914 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1915 {
1916 p1->current_data -= rw_len1;
1917 p1->current_length += rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001918
Dave Barachd7cb1b52016-12-09 09:52:16 -05001919 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1920 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1921 next1 = adj1[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001922
Neale Rannsb069a692017-03-15 12:34:25 -04001923 if (PREDICT_FALSE
1924 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1925 vnet_feature_arc_start (lm->output_feature_arc_index,
1926 tx_sw_if_index1, &next1, p1);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001927 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001928 else
1929 {
1930 p1->error = error_node->errors[error1];
1931 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001932
Neale Ranns25edf142019-03-22 08:12:48 +00001933 if (is_midchain)
1934 {
1935 /* before we paint on the next header, update the L4
1936 * checksums if required, since there's no offload on a tunnel */
1937 calc_checksums (vm, p0);
1938 calc_checksums (vm, p1);
1939 }
1940
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001941 /* Guess we are only writing on simple Ethernet header. */
1942 vnet_rewrite_two_headers (adj0[0], adj1[0],
1943 ip0, ip1, sizeof (ethernet_header_t));
1944
1945 if (is_midchain)
1946 {
Neale Ranns25edf142019-03-22 08:12:48 +00001947 if (adj0->sub_type.midchain.fixup_func)
1948 adj0->sub_type.midchain.fixup_func
1949 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1950 if (adj1->sub_type.midchain.fixup_func)
1951 adj1->sub_type.midchain.fixup_func
1952 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001953 }
1954 if (is_mcast)
1955 {
1956 /*
1957 * copy bytes from the IP address into the MAC rewrite
1958 */
Neale Ranns889fe942017-06-01 05:43:19 -04001959 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1960 adj0->
1961 rewrite_header.dst_mcast_offset,
1962 &ip0->dst_address.as_u32[3],
1963 (u8 *) ip0);
1964 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1965 adj1->
1966 rewrite_header.dst_mcast_offset,
1967 &ip1->dst_address.as_u32[3],
1968 (u8 *) ip1);
Neale Ranns32e1c012016-11-22 17:07:28 +00001969 }
Neale Ranns5e575b12016-10-03 09:40:25 +01001970
Ed Warnickecb9cada2015-12-08 15:45:58 -07001971 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1972 to_next, n_left_to_next,
1973 pi0, pi1, next0, next1);
1974 }
1975
1976 while (n_left_from > 0 && n_left_to_next > 0)
1977 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001978 ip_adjacency_t *adj0;
1979 vlib_buffer_t *p0;
1980 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001981 u32 pi0, rw_len0;
1982 u32 adj_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001983 u32 tx_sw_if_index0;
Ole Troan313f7e22018-04-10 16:02:51 +02001984 bool is_locally_originated0;
Dave Barach75fc8542016-10-11 16:16:02 -04001985
Ed Warnickecb9cada2015-12-08 15:45:58 -07001986 pi0 = to_next[0] = from[0];
1987
1988 p0 = vlib_get_buffer (vm, pi0);
1989
Neale Rannsf06aea52016-11-29 06:51:37 -08001990 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001991
Neale Ranns107e7d42017-04-11 09:55:19 -07001992 adj0 = adj_get (adj_index0);
Dave Barach75fc8542016-10-11 16:16:02 -04001993
Ed Warnickecb9cada2015-12-08 15:45:58 -07001994 ip0 = vlib_buffer_get_current (p0);
1995
1996 error0 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001997 next0 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001998
1999 /* Check hop limit */
Ole Troan313f7e22018-04-10 16:02:51 +02002000 is_locally_originated0 =
2001 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
2002 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002003 {
2004 i32 hop_limit0 = ip0->hop_limit;
2005
2006 ASSERT (ip0->hop_limit > 0);
2007
2008 hop_limit0 -= 1;
2009
2010 ip0->hop_limit = hop_limit0;
2011
Dave Barachd7cb1b52016-12-09 09:52:16 -05002012 if (PREDICT_FALSE (hop_limit0 <= 0))
2013 {
2014 /*
2015 * If the hop count drops below 1 when forwarding, generate
2016 * an ICMP response.
2017 */
2018 error0 = IP6_ERROR_TIME_EXPIRED;
2019 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2020 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2021 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2022 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2023 0);
2024 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002025 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002026 else
2027 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02002028 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002029 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002030
Neale Ranns25edf142019-03-22 08:12:48 +00002031 if (is_midchain)
2032 {
2033 calc_checksums (vm, p0);
2034 }
2035
Ed Warnickecb9cada2015-12-08 15:45:58 -07002036 /* Guess we are only writing on simple Ethernet header. */
2037 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
Dave Barach75fc8542016-10-11 16:16:02 -04002038
Ed Warnickecb9cada2015-12-08 15:45:58 -07002039 /* Update packet buffer attributes/set output interface. */
2040 rw_len0 = adj0[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002041 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002042
Neale Ranns9c6a6132017-02-21 05:33:14 -08002043 if (do_counters)
2044 {
2045 vlib_increment_combined_counter
2046 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02002047 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08002048 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2049 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002050
2051 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002052 u16 ip0_len =
2053 clib_net_to_host_u16 (ip0->payload_length) +
2054 sizeof (ip6_header_t);
2055 if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
2056 ip0_len = gso_mtu_sz (p0);
2057
2058 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02002059 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02002060 is_locally_originated0, &next0, &error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002061
Dave Barachd7cb1b52016-12-09 09:52:16 -05002062 /* Don't adjust the buffer for hop count issue; icmp-error node
Ole Troanda6e11b2018-05-23 11:21:42 +02002063 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002064 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2065 {
Chris Luke816f3e12016-06-14 16:24:47 -04002066 p0->current_data -= rw_len0;
2067 p0->current_length += rw_len0;
2068
Dave Barachd7cb1b52016-12-09 09:52:16 -05002069 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
Dave Barach5331c722016-08-17 11:54:30 -04002070
Dave Barachd7cb1b52016-12-09 09:52:16 -05002071 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2072 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04002073
Neale Rannsb069a692017-03-15 12:34:25 -04002074 if (PREDICT_FALSE
2075 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2076 vnet_feature_arc_start (lm->output_feature_arc_index,
2077 tx_sw_if_index0, &next0, p0);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002078 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04002079 else
2080 {
2081 p0->error = error_node->errors[error0];
2082 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002083
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002084 if (is_midchain)
2085 {
Neale Ranns25edf142019-03-22 08:12:48 +00002086 if (adj0->sub_type.midchain.fixup_func)
2087 adj0->sub_type.midchain.fixup_func
2088 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002089 }
2090 if (is_mcast)
2091 {
Neale Ranns889fe942017-06-01 05:43:19 -04002092 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2093 adj0->
2094 rewrite_header.dst_mcast_offset,
2095 &ip0->dst_address.as_u32[3],
2096 (u8 *) ip0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002097 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002098
Ed Warnickecb9cada2015-12-08 15:45:58 -07002099 from += 1;
2100 n_left_from -= 1;
2101 to_next += 1;
2102 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04002103
Ed Warnickecb9cada2015-12-08 15:45:58 -07002104 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2105 to_next, n_left_to_next,
2106 pi0, next0);
2107 }
2108
2109 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2110 }
2111
2112 /* Need to do trace after rewrites to pick up new packet data. */
2113 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Rannsf06aea52016-11-29 06:51:37 -08002114 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002115
2116 return frame->n_vectors;
2117}
2118
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002119always_inline uword
2120ip6_rewrite_inline (vlib_main_t * vm,
2121 vlib_node_runtime_t * node,
2122 vlib_frame_t * frame,
2123 int do_counters, int is_midchain, int is_mcast)
2124{
2125 vnet_main_t *vnm = vnet_get_main ();
2126 if (PREDICT_FALSE (vnm->interface_main.gso_interface_count > 0))
2127 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2128 is_midchain, is_mcast,
2129 1 /* do_gso */ );
2130 else
2131 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2132 is_midchain, is_mcast,
2133 0 /* no do_gso */ );
2134}
2135
Damjan Marion38173502019-02-13 19:30:09 +01002136VLIB_NODE_FN (ip6_rewrite_node) (vlib_main_t * vm,
2137 vlib_node_runtime_t * node,
2138 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002139{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002140 if (adj_are_counters_enabled ())
2141 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2142 else
2143 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002144}
2145
Damjan Marion38173502019-02-13 19:30:09 +01002146VLIB_NODE_FN (ip6_rewrite_bcast_node) (vlib_main_t * vm,
2147 vlib_node_runtime_t * node,
2148 vlib_frame_t * frame)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002149{
2150 if (adj_are_counters_enabled ())
2151 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2152 else
2153 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2154}
2155
Damjan Marion38173502019-02-13 19:30:09 +01002156VLIB_NODE_FN (ip6_rewrite_mcast_node) (vlib_main_t * vm,
2157 vlib_node_runtime_t * node,
2158 vlib_frame_t * frame)
Neale Ranns32e1c012016-11-22 17:07:28 +00002159{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002160 if (adj_are_counters_enabled ())
2161 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2162 else
2163 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002164}
2165
Damjan Marion38173502019-02-13 19:30:09 +01002166VLIB_NODE_FN (ip6_midchain_node) (vlib_main_t * vm,
2167 vlib_node_runtime_t * node,
2168 vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002169{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002170 if (adj_are_counters_enabled ())
2171 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2172 else
2173 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002174}
2175
Damjan Marion38173502019-02-13 19:30:09 +01002176VLIB_NODE_FN (ip6_mcast_midchain_node) (vlib_main_t * vm,
2177 vlib_node_runtime_t * node,
2178 vlib_frame_t * frame)
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002179{
2180 if (adj_are_counters_enabled ())
2181 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2182 else
Neale Ranns9f171f52017-04-11 08:56:53 -07002183 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002184}
2185
Dave Barachd7cb1b52016-12-09 09:52:16 -05002186/* *INDENT-OFF* */
2187VLIB_REGISTER_NODE (ip6_midchain_node) =
2188{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002189 .name = "ip6-midchain",
2190 .vector_size = sizeof (u32),
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002191 .format_trace = format_ip6_forward_next_trace,
Neale Ranns5e575b12016-10-03 09:40:25 +01002192 .sibling_of = "ip6-rewrite",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002193 };
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002194
Dave Barachd7cb1b52016-12-09 09:52:16 -05002195VLIB_REGISTER_NODE (ip6_rewrite_node) =
2196{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002197 .name = "ip6-rewrite",
2198 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +01002199 .format_trace = format_ip6_rewrite_trace,
Ole Troan313f7e22018-04-10 16:02:51 +02002200 .n_next_nodes = IP6_REWRITE_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002201 .next_nodes =
2202 {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002203 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
Chris Luke816f3e12016-06-14 16:24:47 -04002204 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Ole Troan313f7e22018-04-10 16:02:51 +02002205 [IP6_REWRITE_NEXT_FRAGMENT] = "ip6-frag",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002206 },
2207};
2208
Neale Ranns1855b8e2018-07-11 10:31:26 -07002209VLIB_REGISTER_NODE (ip6_rewrite_bcast_node) = {
Neale Ranns1855b8e2018-07-11 10:31:26 -07002210 .name = "ip6-rewrite-bcast",
2211 .vector_size = sizeof (u32),
2212
2213 .format_trace = format_ip6_rewrite_trace,
2214 .sibling_of = "ip6-rewrite",
2215};
Neale Ranns1855b8e2018-07-11 10:31:26 -07002216
Neale Ranns32e1c012016-11-22 17:07:28 +00002217VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2218{
Neale Ranns32e1c012016-11-22 17:07:28 +00002219 .name = "ip6-rewrite-mcast",
2220 .vector_size = sizeof (u32),
2221 .format_trace = format_ip6_rewrite_trace,
2222 .sibling_of = "ip6-rewrite",
2223};
Neale Ranns32e1c012016-11-22 17:07:28 +00002224
Neale Ranns32e1c012016-11-22 17:07:28 +00002225
Damjan Marion38173502019-02-13 19:30:09 +01002226VLIB_REGISTER_NODE (ip6_mcast_midchain_node) =
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002227{
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002228 .name = "ip6-mcast-midchain",
2229 .vector_size = sizeof (u32),
2230 .format_trace = format_ip6_rewrite_trace,
2231 .sibling_of = "ip6-rewrite",
2232};
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002233
Neale Ranns1855b8e2018-07-11 10:31:26 -07002234/* *INDENT-ON* */
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002235
Ole Troan944f5482016-05-24 11:56:58 +02002236/*
2237 * Hop-by-Hop handling
2238 */
Benoît Ganne47727c02019-02-12 13:35:08 +01002239#ifndef CLIB_MARCH_VARIANT
Ole Troan944f5482016-05-24 11:56:58 +02002240ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
Benoît Ganne47727c02019-02-12 13:35:08 +01002241#endif /* CLIB_MARCH_VARIANT */
Ole Troan944f5482016-05-24 11:56:58 +02002242
2243#define foreach_ip6_hop_by_hop_error \
2244_(PROCESSED, "pkts with ip6 hop-by-hop options") \
2245_(FORMAT, "incorrectly formatted hop-by-hop options") \
2246_(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2247
Neale Ranns32e1c012016-11-22 17:07:28 +00002248/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002249typedef enum
2250{
Ole Troan944f5482016-05-24 11:56:58 +02002251#define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2252 foreach_ip6_hop_by_hop_error
2253#undef _
Neale Ranns32e1c012016-11-22 17:07:28 +00002254 IP6_HOP_BY_HOP_N_ERROR,
Ole Troan944f5482016-05-24 11:56:58 +02002255} ip6_hop_by_hop_error_t;
Neale Ranns32e1c012016-11-22 17:07:28 +00002256/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002257
2258/*
2259 * Primary h-b-h handler trace support
2260 * We work pretty hard on the problem for obvious reasons
2261 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002262typedef struct
2263{
Ole Troan944f5482016-05-24 11:56:58 +02002264 u32 next_index;
2265 u32 trace_len;
2266 u8 option_data[256];
2267} ip6_hop_by_hop_trace_t;
2268
Benoît Ganne47727c02019-02-12 13:35:08 +01002269extern vlib_node_registration_t ip6_hop_by_hop_node;
Ole Troan944f5482016-05-24 11:56:58 +02002270
Dave Barachd7cb1b52016-12-09 09:52:16 -05002271static char *ip6_hop_by_hop_error_strings[] = {
Ole Troan944f5482016-05-24 11:56:58 +02002272#define _(sym,string) string,
2273 foreach_ip6_hop_by_hop_error
2274#undef _
2275};
2276
Damjan Marion38173502019-02-13 19:30:09 +01002277#ifndef CLIB_MARCH_VARIANT
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302278u8 *
2279format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2280{
2281 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2282 int total_len = va_arg (*args, int);
2283 ip6_hop_by_hop_option_t *opt0, *limit0;
2284 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2285 u8 type0;
2286
2287 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2288 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2289
2290 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2291 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2292
2293 while (opt0 < limit0)
2294 {
2295 type0 = opt0->type;
2296 switch (type0)
2297 {
2298 case 0: /* Pad, just stop */
2299 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2300 break;
2301
2302 default:
2303 if (hm->trace[type0])
2304 {
2305 s = (*hm->trace[type0]) (s, opt0);
2306 }
2307 else
2308 {
2309 s =
2310 format (s, "\n unrecognized option %d length %d", type0,
2311 opt0->length);
2312 }
2313 opt0 =
2314 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2315 sizeof (ip6_hop_by_hop_option_t));
2316 break;
2317 }
2318 }
2319 return s;
2320}
Damjan Marion38173502019-02-13 19:30:09 +01002321#endif
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302322
Ole Troan944f5482016-05-24 11:56:58 +02002323static u8 *
2324format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2325{
2326 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2327 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002328 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
Ole Troan944f5482016-05-24 11:56:58 +02002329 ip6_hop_by_hop_header_t *hbh0;
2330 ip6_hop_by_hop_option_t *opt0, *limit0;
2331 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2332
2333 u8 type0;
2334
Dave Barachd7cb1b52016-12-09 09:52:16 -05002335 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
Ole Troan944f5482016-05-24 11:56:58 +02002336
2337 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002338 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
Ole Troan944f5482016-05-24 11:56:58 +02002339
Dave Barachd7cb1b52016-12-09 09:52:16 -05002340 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2341 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
Ole Troan944f5482016-05-24 11:56:58 +02002342
Dave Barachd7cb1b52016-12-09 09:52:16 -05002343 while (opt0 < limit0)
2344 {
2345 type0 = opt0->type;
2346 switch (type0)
2347 {
2348 case 0: /* Pad, just stop */
2349 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2350 break;
Ole Troan944f5482016-05-24 11:56:58 +02002351
Dave Barachd7cb1b52016-12-09 09:52:16 -05002352 default:
2353 if (hm->trace[type0])
2354 {
2355 s = (*hm->trace[type0]) (s, opt0);
2356 }
2357 else
2358 {
2359 s =
2360 format (s, "\n unrecognized option %d length %d", type0,
2361 opt0->length);
2362 }
2363 opt0 =
2364 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2365 sizeof (ip6_hop_by_hop_option_t));
2366 break;
2367 }
Ole Troan944f5482016-05-24 11:56:58 +02002368 }
Ole Troan944f5482016-05-24 11:56:58 +02002369 return s;
2370}
2371
Dave Barachd7cb1b52016-12-09 09:52:16 -05002372always_inline u8
2373ip6_scan_hbh_options (vlib_buffer_t * b0,
2374 ip6_header_t * ip0,
2375 ip6_hop_by_hop_header_t * hbh0,
2376 ip6_hop_by_hop_option_t * opt0,
2377 ip6_hop_by_hop_option_t * limit0, u32 * next0)
Shwethaa91cbe62016-08-08 15:51:04 +01002378{
2379 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2380 u8 type0;
2381 u8 error0 = 0;
2382
2383 while (opt0 < limit0)
2384 {
2385 type0 = opt0->type;
2386 switch (type0)
2387 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002388 case 0: /* Pad1 */
2389 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
Shwethaa91cbe62016-08-08 15:51:04 +01002390 continue;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002391 case 1: /* PadN */
Shwethaa91cbe62016-08-08 15:51:04 +01002392 break;
2393 default:
2394 if (hm->options[type0])
2395 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002396 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2397 {
Shwethaa91cbe62016-08-08 15:51:04 +01002398 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002399 return (error0);
2400 }
Shwethaa91cbe62016-08-08 15:51:04 +01002401 }
2402 else
2403 {
2404 /* Unrecognized mandatory option, check the two high order bits */
2405 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2406 {
2407 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2408 break;
2409 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2410 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2411 *next0 = IP_LOOKUP_NEXT_DROP;
2412 break;
2413 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2414 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2415 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002416 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2417 ICMP6_parameter_problem_unrecognized_option,
2418 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002419 break;
2420 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2421 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002422 if (!ip6_address_is_multicast (&ip0->dst_address))
Shwethaa91cbe62016-08-08 15:51:04 +01002423 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002424 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2425 icmp6_error_set_vnet_buffer (b0,
2426 ICMP6_parameter_problem,
2427 ICMP6_parameter_problem_unrecognized_option,
2428 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002429 }
2430 else
2431 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002432 *next0 = IP_LOOKUP_NEXT_DROP;
Shwethaa91cbe62016-08-08 15:51:04 +01002433 }
2434 break;
2435 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002436 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002437 }
2438 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002439 opt0 =
2440 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2441 sizeof (ip6_hop_by_hop_option_t));
Shwethaa91cbe62016-08-08 15:51:04 +01002442 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002443 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002444}
2445
Ole Troan944f5482016-05-24 11:56:58 +02002446/*
2447 * Process the Hop-by-Hop Options header
2448 */
Damjan Marion38173502019-02-13 19:30:09 +01002449VLIB_NODE_FN (ip6_hop_by_hop_node) (vlib_main_t * vm,
2450 vlib_node_runtime_t * node,
2451 vlib_frame_t * frame)
Ole Troan944f5482016-05-24 11:56:58 +02002452{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002453 vlib_node_runtime_t *error_node =
2454 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
Ole Troan944f5482016-05-24 11:56:58 +02002455 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2456 u32 n_left_from, *from, *to_next;
2457 ip_lookup_next_t next_index;
Ole Troan944f5482016-05-24 11:56:58 +02002458
2459 from = vlib_frame_vector_args (frame);
2460 n_left_from = frame->n_vectors;
2461 next_index = node->cached_next_index;
2462
Dave Barachd7cb1b52016-12-09 09:52:16 -05002463 while (n_left_from > 0)
2464 {
2465 u32 n_left_to_next;
Ole Troan944f5482016-05-24 11:56:58 +02002466
Dave Barachd7cb1b52016-12-09 09:52:16 -05002467 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ole Troan944f5482016-05-24 11:56:58 +02002468
Dave Barachd7cb1b52016-12-09 09:52:16 -05002469 while (n_left_from >= 4 && n_left_to_next >= 2)
Shwethaa91cbe62016-08-08 15:51:04 +01002470 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002471 u32 bi0, bi1;
2472 vlib_buffer_t *b0, *b1;
2473 u32 next0, next1;
2474 ip6_header_t *ip0, *ip1;
2475 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2476 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2477 u8 error0 = 0, error1 = 0;
2478
2479 /* Prefetch next iteration. */
2480 {
2481 vlib_buffer_t *p2, *p3;
2482
2483 p2 = vlib_get_buffer (vm, from[2]);
2484 p3 = vlib_get_buffer (vm, from[3]);
2485
2486 vlib_prefetch_buffer_header (p2, LOAD);
2487 vlib_prefetch_buffer_header (p3, LOAD);
2488
2489 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2490 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Shwethaa91cbe62016-08-08 15:51:04 +01002491 }
2492
Dave Barachd7cb1b52016-12-09 09:52:16 -05002493 /* Speculatively enqueue b0, b1 to the current next frame */
2494 to_next[0] = bi0 = from[0];
2495 to_next[1] = bi1 = from[1];
2496 from += 2;
2497 to_next += 2;
2498 n_left_from -= 2;
2499 n_left_to_next -= 2;
2500
2501 b0 = vlib_get_buffer (vm, bi0);
2502 b1 = vlib_get_buffer (vm, bi1);
2503
2504 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2505 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002506 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002507 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002508 ip_adjacency_t *adj1 = adj_get (adj_index1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002509
2510 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2511 next0 = adj0->lookup_next_index;
2512 next1 = adj1->lookup_next_index;
2513
2514 ip0 = vlib_buffer_get_current (b0);
2515 ip1 = vlib_buffer_get_current (b1);
2516 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2517 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2518 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2519 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2520 limit0 =
2521 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2522 ((hbh0->length + 1) << 3));
2523 limit1 =
2524 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2525 ((hbh1->length + 1) << 3));
2526
2527 /*
2528 * Basic validity checks
2529 */
2530 if ((hbh0->length + 1) << 3 >
2531 clib_net_to_host_u16 (ip0->payload_length))
2532 {
2533 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2534 next0 = IP_LOOKUP_NEXT_DROP;
2535 goto outdual;
2536 }
2537 /* Scan the set of h-b-h options, process ones that we understand */
2538 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2539
2540 if ((hbh1->length + 1) << 3 >
2541 clib_net_to_host_u16 (ip1->payload_length))
2542 {
2543 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2544 next1 = IP_LOOKUP_NEXT_DROP;
2545 goto outdual;
2546 }
2547 /* Scan the set of h-b-h options, process ones that we understand */
2548 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2549
2550 outdual:
2551 /* Has the classifier flagged this buffer for special treatment? */
2552 if (PREDICT_FALSE
2553 ((error0 == 0)
2554 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2555 next0 = hm->next_override;
2556
2557 /* Has the classifier flagged this buffer for special treatment? */
2558 if (PREDICT_FALSE
2559 ((error1 == 0)
2560 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2561 next1 = hm->next_override;
2562
2563 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2564 {
2565 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2566 {
2567 ip6_hop_by_hop_trace_t *t =
2568 vlib_add_trace (vm, node, b0, sizeof (*t));
2569 u32 trace_len = (hbh0->length + 1) << 3;
2570 t->next_index = next0;
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, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002578 }
2579 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2580 {
2581 ip6_hop_by_hop_trace_t *t =
2582 vlib_add_trace (vm, node, b1, sizeof (*t));
2583 u32 trace_len = (hbh1->length + 1) << 3;
2584 t->next_index = next1;
2585 /* Capture the h-b-h option verbatim */
2586 trace_len =
2587 trace_len <
2588 ARRAY_LEN (t->option_data) ? trace_len :
2589 ARRAY_LEN (t->option_data);
2590 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002591 clib_memcpy_fast (t->option_data, hbh1, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002592 }
2593
2594 }
2595
2596 b0->error = error_node->errors[error0];
2597 b1->error = error_node->errors[error1];
2598
2599 /* verify speculative enqueue, maybe switch current next frame */
2600 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2601 n_left_to_next, bi0, bi1, next0,
2602 next1);
Shwethaa91cbe62016-08-08 15:51:04 +01002603 }
2604
Dave Barachd7cb1b52016-12-09 09:52:16 -05002605 while (n_left_from > 0 && n_left_to_next > 0)
2606 {
2607 u32 bi0;
2608 vlib_buffer_t *b0;
2609 u32 next0;
2610 ip6_header_t *ip0;
2611 ip6_hop_by_hop_header_t *hbh0;
2612 ip6_hop_by_hop_option_t *opt0, *limit0;
2613 u8 error0 = 0;
Shwethaa91cbe62016-08-08 15:51:04 +01002614
Dave Barachd7cb1b52016-12-09 09:52:16 -05002615 /* Speculatively enqueue b0 to the current next frame */
2616 bi0 = from[0];
2617 to_next[0] = bi0;
2618 from += 1;
2619 to_next += 1;
2620 n_left_from -= 1;
2621 n_left_to_next -= 1;
2622
2623 b0 = vlib_get_buffer (vm, bi0);
2624 /*
2625 * Default use the next_index from the adjacency.
2626 * A HBH option rarely redirects to a different node
2627 */
2628 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002629 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002630 next0 = adj0->lookup_next_index;
2631
2632 ip0 = vlib_buffer_get_current (b0);
2633 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2634 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2635 limit0 =
2636 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2637 ((hbh0->length + 1) << 3));
2638
2639 /*
2640 * Basic validity checks
2641 */
2642 if ((hbh0->length + 1) << 3 >
2643 clib_net_to_host_u16 (ip0->payload_length))
2644 {
2645 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2646 next0 = IP_LOOKUP_NEXT_DROP;
2647 goto out0;
2648 }
2649
2650 /* Scan the set of h-b-h options, process ones that we understand */
2651 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2652
2653 out0:
2654 /* Has the classifier flagged this buffer for special treatment? */
2655 if (PREDICT_FALSE
2656 ((error0 == 0)
2657 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2658 next0 = hm->next_override;
2659
2660 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2661 {
2662 ip6_hop_by_hop_trace_t *t =
2663 vlib_add_trace (vm, node, b0, sizeof (*t));
2664 u32 trace_len = (hbh0->length + 1) << 3;
2665 t->next_index = next0;
2666 /* Capture the h-b-h option verbatim */
2667 trace_len =
2668 trace_len <
2669 ARRAY_LEN (t->option_data) ? trace_len :
2670 ARRAY_LEN (t->option_data);
2671 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002672 clib_memcpy_fast (t->option_data, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002673 }
2674
2675 b0->error = error_node->errors[error0];
2676
2677 /* verify speculative enqueue, maybe switch current next frame */
2678 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2679 n_left_to_next, bi0, next0);
2680 }
2681 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Shwethaa91cbe62016-08-08 15:51:04 +01002682 }
Ole Troan944f5482016-05-24 11:56:58 +02002683 return frame->n_vectors;
2684}
2685
Dave Barachd7cb1b52016-12-09 09:52:16 -05002686/* *INDENT-OFF* */
2687VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2688{
Ole Troan944f5482016-05-24 11:56:58 +02002689 .name = "ip6-hop-by-hop",
Ole Troan964f93e2016-06-10 13:22:36 +02002690 .sibling_of = "ip6-lookup",
Ole Troan944f5482016-05-24 11:56:58 +02002691 .vector_size = sizeof (u32),
2692 .format_trace = format_ip6_hop_by_hop_trace,
2693 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002694 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
Ole Troan944f5482016-05-24 11:56:58 +02002695 .error_strings = ip6_hop_by_hop_error_strings,
Ole Troan964f93e2016-06-10 13:22:36 +02002696 .n_next_nodes = 0,
Ole Troan944f5482016-05-24 11:56:58 +02002697};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002698/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002699
Ole Troan944f5482016-05-24 11:56:58 +02002700static clib_error_t *
2701ip6_hop_by_hop_init (vlib_main_t * vm)
2702{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002703 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Dave Barachb7b92992018-10-17 10:38:51 -04002704 clib_memset (hm->options, 0, sizeof (hm->options));
2705 clib_memset (hm->trace, 0, sizeof (hm->trace));
Shwethaa91cbe62016-08-08 15:51:04 +01002706 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
Ole Troan944f5482016-05-24 11:56:58 +02002707 return (0);
2708}
2709
2710VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2711
Damjan Marion38173502019-02-13 19:30:09 +01002712#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05002713void
2714ip6_hbh_set_next_override (uword next)
Shwethaa91cbe62016-08-08 15:51:04 +01002715{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002716 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Shwethaa91cbe62016-08-08 15:51:04 +01002717
2718 hm->next_override = next;
2719}
2720
Ole Troan944f5482016-05-24 11:56:58 +02002721int
2722ip6_hbh_register_option (u8 option,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002723 int options (vlib_buffer_t * b, ip6_header_t * ip,
2724 ip6_hop_by_hop_option_t * opt),
2725 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
Ole Troan944f5482016-05-24 11:56:58 +02002726{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002727 ip6_main_t *im = &ip6_main;
2728 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002729
Neale Ranns756cd942018-04-06 09:18:11 -07002730 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002731
2732 /* Already registered */
2733 if (hm->options[option])
2734 return (-1);
2735
2736 hm->options[option] = options;
2737 hm->trace[option] = trace;
2738
2739 /* Set global variable */
2740 im->hbh_enabled = 1;
2741
2742 return (0);
2743}
2744
2745int
2746ip6_hbh_unregister_option (u8 option)
2747{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002748 ip6_main_t *im = &ip6_main;
2749 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002750
Neale Ranns756cd942018-04-06 09:18:11 -07002751 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002752
2753 /* Not registered */
2754 if (!hm->options[option])
2755 return (-1);
2756
2757 hm->options[option] = NULL;
2758 hm->trace[option] = NULL;
2759
2760 /* Disable global knob if this was the last option configured */
2761 int i;
2762 bool found = false;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002763 for (i = 0; i < 256; i++)
2764 {
2765 if (hm->options[option])
2766 {
2767 found = true;
2768 break;
2769 }
Ole Troan944f5482016-05-24 11:56:58 +02002770 }
Ole Troan944f5482016-05-24 11:56:58 +02002771 if (!found)
2772 im->hbh_enabled = 0;
2773
2774 return (0);
2775}
2776
Ed Warnickecb9cada2015-12-08 15:45:58 -07002777/* Global IP6 main. */
2778ip6_main_t ip6_main;
Damjan Marion38173502019-02-13 19:30:09 +01002779#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07002780
2781static clib_error_t *
2782ip6_lookup_init (vlib_main_t * vm)
2783{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002784 ip6_main_t *im = &ip6_main;
2785 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002786 uword i;
2787
Damjan Marion8b3191e2016-11-09 19:54:20 +01002788 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2789 return error;
2790
Ed Warnickecb9cada2015-12-08 15:45:58 -07002791 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2792 {
2793 u32 j, i0, i1;
2794
2795 i0 = i / 32;
2796 i1 = i % 32;
2797
2798 for (j = 0; j < i0; j++)
2799 im->fib_masks[i].as_u32[j] = ~0;
2800
2801 if (i1)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002802 im->fib_masks[i].as_u32[i0] =
2803 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002804 }
2805
2806 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2807
2808 if (im->lookup_table_nbuckets == 0)
2809 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2810
Dave Barachd7cb1b52016-12-09 09:52:16 -05002811 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812
2813 if (im->lookup_table_size == 0)
2814 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
Dave Barach75fc8542016-10-11 16:16:02 -04002815
Neale Rannsae809832018-11-23 09:00:27 -08002816 clib_bihash_init_24_8 (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
Dave Barachd7cb1b52016-12-09 09:52:16 -05002817 "ip6 FIB fwding table",
2818 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Rannsae809832018-11-23 09:00:27 -08002819 clib_bihash_init_24_8 (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002820 "ip6 FIB non-fwding table",
2821 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Rannsae809832018-11-23 09:00:27 -08002822 clib_bihash_init_40_8 (&im->ip6_mtable.ip6_mhash,
2823 "ip6 mFIB table",
2824 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002825
Ed Warnickecb9cada2015-12-08 15:45:58 -07002826 /* Create FIB with index 0 and table id of 0. */
Neale Ranns15002542017-09-10 04:39:11 -07002827 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2828 FIB_SOURCE_DEFAULT_ROUTE);
2829 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2830 MFIB_SOURCE_DEFAULT_ROUTE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831
2832 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002833 pg_node_t *pn;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002834 pn = pg_get_node (ip6_lookup_node.index);
2835 pn->unformat_edit = unformat_pg_ip6_header;
2836 }
2837
Ole Troan944f5482016-05-24 11:56:58 +02002838 /* Unless explicitly configured, don't process HBH options */
2839 im->hbh_enabled = 0;
2840
Ed Warnickecb9cada2015-12-08 15:45:58 -07002841 {
2842 icmp6_neighbor_solicitation_header_t p;
2843
Dave Barachb7b92992018-10-17 10:38:51 -04002844 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002845
Dave Barachd7cb1b52016-12-09 09:52:16 -05002846 p.ip.ip_version_traffic_class_and_flow_label =
2847 clib_host_to_net_u32 (0x6 << 28);
2848 p.ip.payload_length =
2849 clib_host_to_net_u16 (sizeof (p) -
2850 STRUCT_OFFSET_OF
2851 (icmp6_neighbor_solicitation_header_t, neighbor));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002852 p.ip.protocol = IP_PROTOCOL_ICMP6;
2853 p.ip.hop_limit = 255;
2854 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2855
2856 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2857
Dave Barachd7cb1b52016-12-09 09:52:16 -05002858 p.link_layer_option.header.type =
2859 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2860 p.link_layer_option.header.n_data_u64s =
2861 sizeof (p.link_layer_option) / sizeof (u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002862
2863 vlib_packet_template_init (vm,
2864 &im->discover_neighbor_packet_template,
2865 &p, sizeof (p),
2866 /* alloc chunk size */ 8,
2867 "ip6 neighbor discovery");
2868 }
2869
Dave Barach203c6322016-06-26 10:29:03 -04002870 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002871}
2872
2873VLIB_INIT_FUNCTION (ip6_lookup_init);
2874
Dave Barach75fc8542016-10-11 16:16:02 -04002875static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07002876test_ip6_link_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002877 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002878{
2879 u8 mac[6];
2880 ip6_address_t _a, *a = &_a;
2881
2882 if (unformat (input, "%U", unformat_ethernet_address, mac))
2883 {
2884 ip6_link_local_address_from_ethernet_mac_address (a, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002885 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002886 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2887 vlib_cli_output (vm, "Original MAC address: %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002888 format_ethernet_address, mac);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002889 }
Dave Barach75fc8542016-10-11 16:16:02 -04002890
Ed Warnickecb9cada2015-12-08 15:45:58 -07002891 return 0;
2892}
2893
Billy McFall0683c9c2016-10-13 08:27:31 -04002894/*?
2895 * This command converts the given MAC Address into an IPv6 link-local
2896 * address.
2897 *
2898 * @cliexpar
2899 * Example of how to create an IPv6 link-local address:
2900 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2901 * Link local address: fe80::14d9:e0ff:fe91:7986
2902 * Original MAC address: 16:d9:e0:91:79:86
2903 * @cliexend
2904?*/
2905/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002906VLIB_CLI_COMMAND (test_link_command, static) =
2907{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002908 .path = "test ip6 link",
Dave Barach75fc8542016-10-11 16:16:02 -04002909 .function = test_ip6_link_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002910 .short_help = "test ip6 link <mac-address>",
2911};
Billy McFall0683c9c2016-10-13 08:27:31 -04002912/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002913
Damjan Marion38173502019-02-13 19:30:09 +01002914#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05002915int
2916vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917{
Neale Ranns107e7d42017-04-11 09:55:19 -07002918 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002919
Neale Ranns107e7d42017-04-11 09:55:19 -07002920 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002921
Neale Ranns107e7d42017-04-11 09:55:19 -07002922 if (~0 == fib_index)
2923 return VNET_API_ERROR_NO_SUCH_FIB;
2924
Neale Ranns227038a2017-04-21 01:07:59 -07002925 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2926 flow_hash_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002927
Neale Ranns227038a2017-04-21 01:07:59 -07002928 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002929}
Damjan Marion38173502019-02-13 19:30:09 +01002930#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07002931
2932static clib_error_t *
2933set_ip6_flow_hash_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002934 unformat_input_t * input,
2935 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002936{
2937 int matched = 0;
2938 u32 table_id = 0;
2939 u32 flow_hash_config = 0;
2940 int rv;
2941
Dave Barachd7cb1b52016-12-09 09:52:16 -05002942 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2943 {
2944 if (unformat (input, "table %d", &table_id))
2945 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002946#define _(a,v) \
2947 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
Dave Barachd7cb1b52016-12-09 09:52:16 -05002948 foreach_flow_hash_bit
Ed Warnickecb9cada2015-12-08 15:45:58 -07002949#undef _
Dave Barachd7cb1b52016-12-09 09:52:16 -05002950 else
2951 break;
2952 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002953
2954 if (matched == 0)
2955 return clib_error_return (0, "unknown input `%U'",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002956 format_unformat_error, input);
Dave Barach75fc8542016-10-11 16:16:02 -04002957
Ed Warnickecb9cada2015-12-08 15:45:58 -07002958 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2959 switch (rv)
2960 {
Neale Ranns227038a2017-04-21 01:07:59 -07002961 case 0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002962 break;
2963
2964 case -1:
2965 return clib_error_return (0, "no such FIB table %d", table_id);
Dave Barach75fc8542016-10-11 16:16:02 -04002966
Ed Warnickecb9cada2015-12-08 15:45:58 -07002967 default:
2968 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2969 break;
2970 }
Dave Barach75fc8542016-10-11 16:16:02 -04002971
Ed Warnickecb9cada2015-12-08 15:45:58 -07002972 return 0;
2973}
2974
Billy McFall0683c9c2016-10-13 08:27:31 -04002975/*?
2976 * Configure the set of IPv6 fields used by the flow hash.
2977 *
2978 * @cliexpar
2979 * @parblock
2980 * Example of how to set the flow hash on a given table:
Billy McFallebb9a6a2016-10-17 11:35:32 -04002981 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2982 *
Billy McFall0683c9c2016-10-13 08:27:31 -04002983 * Example of display the configured flow hash:
2984 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -04002985 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2986 * @::/0
2987 * unicast-ip6-chain
2988 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2989 * [0] [@0]: dpo-drop ip6
2990 * fe80::/10
2991 * unicast-ip6-chain
2992 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2993 * [0] [@2]: dpo-receive
2994 * ff02::1/128
2995 * unicast-ip6-chain
2996 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2997 * [0] [@2]: dpo-receive
2998 * ff02::2/128
2999 * unicast-ip6-chain
3000 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3001 * [0] [@2]: dpo-receive
3002 * ff02::16/128
3003 * unicast-ip6-chain
3004 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3005 * [0] [@2]: dpo-receive
3006 * ff02::1:ff00:0/104
3007 * unicast-ip6-chain
3008 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3009 * [0] [@2]: dpo-receive
3010 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3011 * @::/0
3012 * unicast-ip6-chain
3013 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3014 * [0] [@0]: dpo-drop ip6
3015 * @::a:1:1:0:4/126
3016 * unicast-ip6-chain
3017 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3018 * [0] [@4]: ipv6-glean: af_packet0
3019 * @::a:1:1:0:7/128
3020 * unicast-ip6-chain
3021 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3022 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3023 * fe80::/10
3024 * unicast-ip6-chain
3025 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3026 * [0] [@2]: dpo-receive
3027 * fe80::fe:3eff:fe3e:9222/128
3028 * unicast-ip6-chain
3029 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3030 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3031 * ff02::1/128
3032 * unicast-ip6-chain
3033 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3034 * [0] [@2]: dpo-receive
3035 * ff02::2/128
3036 * unicast-ip6-chain
3037 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3038 * [0] [@2]: dpo-receive
3039 * ff02::16/128
3040 * unicast-ip6-chain
3041 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3042 * [0] [@2]: dpo-receive
3043 * ff02::1:ff00:0/104
3044 * unicast-ip6-chain
3045 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3046 * [0] [@2]: dpo-receive
Billy McFall0683c9c2016-10-13 08:27:31 -04003047 * @cliexend
3048 * @endparblock
3049?*/
3050/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003051VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3052{
3053 .path = "set ip6 flow-hash",
3054 .short_help =
3055 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3056 .function = set_ip6_flow_hash_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003057};
Billy McFall0683c9c2016-10-13 08:27:31 -04003058/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003059
3060static clib_error_t *
3061show_ip6_local_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003062 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003063{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003064 ip6_main_t *im = &ip6_main;
3065 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003066 int i;
Dave Barach75fc8542016-10-11 16:16:02 -04003067
Ed Warnickecb9cada2015-12-08 15:45:58 -07003068 vlib_cli_output (vm, "Protocols handled by ip6_local");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003069 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003070 {
3071 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02003072 {
3073
3074 u32 node_index = vlib_get_node (vm,
3075 ip6_local_node.index)->
3076 next_nodes[lm->local_next_by_ip_protocol[i]];
3077 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3078 node_index);
3079 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003080 }
3081 return 0;
3082}
3083
3084
3085
Billy McFall0683c9c2016-10-13 08:27:31 -04003086/*?
3087 * Display the set of protocols handled by the local IPv6 stack.
3088 *
3089 * @cliexpar
3090 * Example of how to display local protocol table:
3091 * @cliexstart{show ip6 local}
3092 * Protocols handled by ip6_local
3093 * 17
3094 * 43
3095 * 58
3096 * 115
3097 * @cliexend
3098?*/
3099/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003100VLIB_CLI_COMMAND (show_ip6_local, static) =
3101{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003102 .path = "show ip6 local",
3103 .function = show_ip6_local_command_fn,
Billy McFall0683c9c2016-10-13 08:27:31 -04003104 .short_help = "show ip6 local",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003105};
Billy McFall0683c9c2016-10-13 08:27:31 -04003106/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003107
Damjan Marion38173502019-02-13 19:30:09 +01003108#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05003109int
3110vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3111 u32 table_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003112{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003113 vnet_main_t *vnm = vnet_get_main ();
3114 vnet_interface_main_t *im = &vnm->interface_main;
3115 ip6_main_t *ipm = &ip6_main;
3116 ip_lookup_main_t *lm = &ipm->lookup_main;
3117 vnet_classify_main_t *cm = &vnet_classify_main;
Neale Rannsdf089a82016-10-02 16:39:06 +01003118 ip6_address_t *if_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003119
3120 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3121 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3122
3123 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3124 return VNET_API_ERROR_NO_SUCH_ENTRY;
3125
3126 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003127 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003128
Neale Ranns6cfc39c2017-02-14 01:44:25 -08003129 if_addr = ip6_interface_first_address (ipm, sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01003130
3131 if (NULL != if_addr)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003132 {
Neale Rannsdf089a82016-10-02 16:39:06 +01003133 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003134 .fp_len = 128,
3135 .fp_proto = FIB_PROTOCOL_IP6,
3136 .fp_addr.ip6 = *if_addr,
Neale Rannsdf089a82016-10-02 16:39:06 +01003137 };
3138 u32 fib_index;
3139
Dave Barachd7cb1b52016-12-09 09:52:16 -05003140 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3141 sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01003142
3143
Dave Barachd7cb1b52016-12-09 09:52:16 -05003144 if (table_index != (u32) ~ 0)
3145 {
3146 dpo_id_t dpo = DPO_INVALID;
Neale Rannsdf089a82016-10-02 16:39:06 +01003147
Dave Barachd7cb1b52016-12-09 09:52:16 -05003148 dpo_set (&dpo,
3149 DPO_CLASSIFY,
3150 DPO_PROTO_IP6,
3151 classify_dpo_create (DPO_PROTO_IP6, table_index));
Neale Rannsdf089a82016-10-02 16:39:06 +01003152
Dave Barachd7cb1b52016-12-09 09:52:16 -05003153 fib_table_entry_special_dpo_add (fib_index,
3154 &pfx,
3155 FIB_SOURCE_CLASSIFY,
3156 FIB_ENTRY_FLAG_NONE, &dpo);
3157 dpo_reset (&dpo);
3158 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003159 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003160 {
3161 fib_table_entry_special_remove (fib_index,
3162 &pfx, FIB_SOURCE_CLASSIFY);
3163 }
3164 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003165
Ed Warnickecb9cada2015-12-08 15:45:58 -07003166 return 0;
3167}
Damjan Marion38173502019-02-13 19:30:09 +01003168#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07003169
3170static clib_error_t *
3171set_ip6_classify_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003172 unformat_input_t * input,
3173 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003174{
3175 u32 table_index = ~0;
3176 int table_index_set = 0;
3177 u32 sw_if_index = ~0;
3178 int rv;
Dave Barach75fc8542016-10-11 16:16:02 -04003179
Dave Barachd7cb1b52016-12-09 09:52:16 -05003180 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3181 {
3182 if (unformat (input, "table-index %d", &table_index))
3183 table_index_set = 1;
3184 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3185 vnet_get_main (), &sw_if_index))
3186 ;
3187 else
3188 break;
3189 }
Dave Barach75fc8542016-10-11 16:16:02 -04003190
Ed Warnickecb9cada2015-12-08 15:45:58 -07003191 if (table_index_set == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003192 return clib_error_return (0, "classify table-index must be specified");
Dave Barach75fc8542016-10-11 16:16:02 -04003193
Ed Warnickecb9cada2015-12-08 15:45:58 -07003194 if (sw_if_index == ~0)
3195 return clib_error_return (0, "interface / subif must be specified");
3196
3197 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3198
3199 switch (rv)
3200 {
3201 case 0:
3202 break;
3203
3204 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3205 return clib_error_return (0, "No such interface");
3206
3207 case VNET_API_ERROR_NO_SUCH_ENTRY:
3208 return clib_error_return (0, "No such classifier table");
3209 }
3210 return 0;
3211}
3212
Billy McFall0683c9c2016-10-13 08:27:31 -04003213/*?
3214 * Assign a classification table to an interface. The classification
3215 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3216 * commands. Once the table is create, use this command to filter packets
3217 * on an interface.
3218 *
3219 * @cliexpar
3220 * Example of how to assign a classification table to an interface:
3221 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3222?*/
3223/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003224VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3225{
3226 .path = "set ip6 classify",
3227 .short_help =
3228 "set ip6 classify intfc <interface> table-index <classify-idx>",
3229 .function = set_ip6_classify_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003230};
Billy McFall0683c9c2016-10-13 08:27:31 -04003231/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003232
3233static clib_error_t *
3234ip6_config (vlib_main_t * vm, unformat_input_t * input)
3235{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003236 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003237 uword heapsize = 0;
3238 u32 tmp;
3239 u32 nbuckets = 0;
3240
Dave Barachd7cb1b52016-12-09 09:52:16 -05003241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3242 {
3243 if (unformat (input, "hash-buckets %d", &tmp))
3244 nbuckets = tmp;
Neale Ranns1ec36522017-11-29 05:20:37 -08003245 else if (unformat (input, "heap-size %U",
3246 unformat_memory_size, &heapsize))
3247 ;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003248 else
3249 return clib_error_return (0, "unknown input '%U'",
3250 format_unformat_error, input);
3251 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003252
3253 im->lookup_table_nbuckets = nbuckets;
3254 im->lookup_table_size = heapsize;
3255
3256 return 0;
3257}
3258
3259VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003260
3261/*
3262 * fd.io coding-style-patch-verification: ON
3263 *
3264 * Local Variables:
3265 * eval: (c-set-style "gnu")
3266 * End:
3267 */