blob: 84ea5a068aaf65b9b1bdffe1bee798221372e943 [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>
Neale Rannscbe25aa2019-09-30 10:53:31 +000043#include <vnet/ip/ip6_link.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>
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +020051#include <vnet/dpo/receive_dpo.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052#include <vnet/dpo/classify_dpo.h>
Neale Rannsba4a5bf2020-01-09 06:43:14 +000053#include <vnet/classify/vnet_classify.h>
Neale Ranns68d48d92021-06-03 14:59:47 +000054#include <vnet/pg/pg.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Damjan Marion38173502019-02-13 19:30:09 +010056#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#include <vppinfra/bihash_template.c>
Damjan Marion38173502019-02-13 19:30:09 +010058#endif
Vijayabhaskar Katamreddyacbde662018-01-23 13:39:40 -080059#include <vnet/ip/ip6_forward.h>
Neale Ranns25edf142019-03-22 08:12:48 +000060#include <vnet/interface_output.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
AkshayaNadahallifdd81af2016-12-01 16:33:51 +053062/* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
63#define OI_DECAP 0x80000000
64
Ed Warnickecb9cada2015-12-08 15:45:58 -070065static void
Matthew Smith6c92f5b2019-08-07 11:46:30 -050066ip6_add_interface_prefix_routes (ip6_main_t * im,
67 u32 sw_if_index,
68 u32 fib_index,
69 ip6_address_t * address, u32 address_length)
70{
71 ip_lookup_main_t *lm = &im->lookup_main;
72 ip_interface_prefix_t *if_prefix;
73
Neale Ranns1ff3c152019-10-07 22:40:54 -070074 /* *INDENT-OFF* */
Matthew Smith6c92f5b2019-08-07 11:46:30 -050075 ip_interface_prefix_key_t key = {
76 .prefix = {
Neale Ranns1ff3c152019-10-07 22:40:54 -070077 .fp_len = address_length,
78 .fp_proto = FIB_PROTOCOL_IP6,
79 .fp_addr.ip6 = {
80 .as_u64 = {
81 address->as_u64[0] & im->fib_masks[address_length].as_u64[0],
82 address->as_u64[1] & im->fib_masks[address_length].as_u64[1],
83 },
84 },
85 },
Matthew Smith6c92f5b2019-08-07 11:46:30 -050086 .sw_if_index = sw_if_index,
87 };
Neale Ranns1ff3c152019-10-07 22:40:54 -070088 /* *INDENT-ON* */
Matthew Smith6c92f5b2019-08-07 11:46:30 -050089
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
Neale Ranns1ff3c152019-10-07 22:40:54 -0700181 /* *INDENT-OFF* */
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500182 ip_interface_prefix_key_t key = {
183 .prefix = {
Neale Ranns1ff3c152019-10-07 22:40:54 -0700184 .fp_len = address_length,
185 .fp_proto = FIB_PROTOCOL_IP6,
186 .fp_addr.ip6 = {
187 .as_u64 = {
188 address->as_u64[0] & im->fib_masks[address_length].as_u64[0],
189 address->as_u64[1] & im->fib_masks[address_length].as_u64[1],
190 },
191 },
192 },
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500193 .sw_if_index = sw_if_index,
194 };
Neale Ranns1ff3c152019-10-07 22:40:54 -0700195 /* *INDENT-ON* */
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500196
197 if_prefix = ip_get_interface_prefix (lm, &key);
198 if (!if_prefix)
199 {
200 clib_warning ("Prefix not found while deleting %U",
201 format_ip4_address_and_length, address, address_length);
202 return;
203 }
204
205 /* If not deleting last intf addr in prefix, decrement ref count & return */
206 if_prefix->ref_count -= 1;
207 if (if_prefix->ref_count > 0)
208 return;
209
Neale Ranns1ff3c152019-10-07 22:40:54 -0700210 /* length <= 128, delete glean route */
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500211 if (address_length <= 128)
212 {
213 /* remove glean route for prefix */
214 fib_table_entry_delete (fib_index, &key.prefix, FIB_SOURCE_INTERFACE);
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500215 }
216
217 mhash_unset (&lm->prefix_to_if_prefix_index, &key, 0 /* old_value */ );
218 pool_put (lm->if_prefix_pool, if_prefix);
219}
220
221static void
222ip6_del_interface_routes (u32 sw_if_index, ip6_main_t * im,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100223 u32 fib_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500224 ip6_address_t * address, u32 address_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500226 fib_prefix_t pfx = {
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500227 .fp_len = 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228 .fp_proto = FIB_PROTOCOL_IP6,
229 .fp_addr.ip6 = *address,
230 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500232 /* delete special routes for the prefix if needed */
233 ip6_del_interface_prefix_routes (im, sw_if_index, fib_index,
234 address, address_length);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100235
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237}
238
Damjan Marion38173502019-02-13 19:30:09 +0100239#ifndef CLIB_MARCH_VARIANT
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500241ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100242{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500243 ip6_main_t *im = &ip6_main;
John Lo4a302ee2020-05-12 22:34:39 -0400244 vnet_main_t *vnm = vnet_get_main ();
245 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100247 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249 /*
250 * enable/disable only on the 1<->0 transition
251 */
252 if (is_enable)
253 {
254 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500255 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100256 }
257 else
258 {
Neale Ranns75152282017-01-09 01:00:45 -0800259 /* The ref count is 0 when an address is removed from an interface that has
260 * no address - this is not a ciritical error */
261 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
262 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500263 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100264 }
265
Neale Ranns8269d3d2018-01-30 09:02:20 -0800266 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
Neale Ranns630198f2017-05-22 09:20:20 -0400267 !is_enable, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100268
Neale Ranns8269d3d2018-01-30 09:02:20 -0800269 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
270 sw_if_index, !is_enable, 0, 0);
John Lo4a302ee2020-05-12 22:34:39 -0400271
272 if (is_enable)
273 hi->l3_if_count++;
274 else if (hi->l3_if_count)
275 hi->l3_if_count--;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100276}
277
Neale Rannsdf089a82016-10-02 16:39:06 +0100278/* get first interface address */
279ip6_address_t *
Neale Ranns6cfc39c2017-02-14 01:44:25 -0800280ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
Neale Rannsdf089a82016-10-02 16:39:06 +0100281{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500282 ip_lookup_main_t *lm = &im->lookup_main;
283 ip_interface_address_t *ia = 0;
284 ip6_address_t *result = 0;
Neale Rannsdf089a82016-10-02 16:39:06 +0100285
Dave Barachd7cb1b52016-12-09 09:52:16 -0500286 /* *INDENT-OFF* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100287 foreach_ip_interface_address (lm, ia, sw_if_index,
288 1 /* honor unnumbered */,
289 ({
290 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
291 result = a;
292 break;
293 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500294 /* *INDENT-ON* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100295 return result;
296}
297
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100298clib_error_t *
299ip6_add_del_interface_address (vlib_main_t * vm,
300 u32 sw_if_index,
301 ip6_address_t * address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500302 u32 address_length, u32 is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304 vnet_main_t *vnm = vnet_get_main ();
305 ip6_main_t *im = &ip6_main;
306 ip_lookup_main_t *lm = &im->lookup_main;
Neale Ranns59f71132020-04-08 12:19:38 +0000307 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 u32 if_address_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500309 ip6_address_fib_t ip6_af, *addr_fib = 0;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000310 const ip6_address_t *ll_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
Pim van Pelt76b19ce2021-08-10 23:44:44 +0200312 error = vnet_sw_interface_supports_addressing (vnm, sw_if_index);
313 if (error)
Matthew Smith1b6c7932021-09-24 15:27:36 -0500314 {
315 vnm->api_errno = VNET_API_ERROR_UNSUPPORTED;
316 return error;
317 }
Pavel Kotucek57808982017-08-02 08:20:19 +0200318
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200319 if (ip6_address_is_link_local_unicast (address))
320 {
321 if (address_length != 128)
322 {
323 vnm->api_errno = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
324 return
325 clib_error_create
326 ("prefix length of link-local address must be 128");
327 }
328 if (!is_del)
329 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000330 int rv;
331
Neale Rannsec40a7d2020-04-23 07:36:12 +0000332 rv = ip6_link_set_local_address (sw_if_index, address);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000333
334 if (rv)
335 {
336 vnm->api_errno = rv;
337 return clib_error_create ("address not assignable");
338 }
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200339 }
340 else
341 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000342 ll_addr = ip6_get_link_local_address (sw_if_index);
343 if (ip6_address_is_equal (ll_addr, address))
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200344 {
345 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_DELETABLE;
346 return clib_error_create ("address not deletable");
347 }
348 else
349 {
350 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
351 return clib_error_create ("address not found");
352 }
353 }
Neale Rannsec40a7d2020-04-23 07:36:12 +0000354
355 return (NULL);
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200356 }
357
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 ip6_addr_fib_init (&ip6_af, address,
359 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
360 vec_add1 (addr_fib, ip6_af);
361
Neale Ranns744902e2017-08-14 10:35:44 -0700362 /* *INDENT-OFF* */
363 if (!is_del)
364 {
365 /* When adding an address check that it does not conflict
366 with an existing address on any interface in this table. */
367 ip_interface_address_t *ia;
368 vnet_sw_interface_t *sif;
369
Damjan Marionb2c31b62020-12-13 21:47:40 +0100370 pool_foreach (sif, vnm->interface_main.sw_interfaces)
371 {
Neale Ranns744902e2017-08-14 10:35:44 -0700372 if (im->fib_index_by_sw_if_index[sw_if_index] ==
373 im->fib_index_by_sw_if_index[sif->sw_if_index])
374 {
375 foreach_ip_interface_address
376 (&im->lookup_main, ia, sif->sw_if_index,
377 0 /* honor unnumbered */ ,
378 ({
379 ip6_address_t * x =
380 ip_interface_address_get_address
381 (&im->lookup_main, ia);
Neale Ranns59f71132020-04-08 12:19:38 +0000382
Neale Ranns744902e2017-08-14 10:35:44 -0700383 if (ip6_destination_matches_route
384 (im, address, x, ia->address_length) ||
385 ip6_destination_matches_route (im,
386 x,
387 address,
388 address_length))
389 {
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500390 /* an intf may have >1 addr from the same prefix */
391 if ((sw_if_index == sif->sw_if_index) &&
392 (ia->address_length == address_length) &&
393 !ip6_address_is_equal (x, address))
394 continue;
395
Neale Ranns59f71132020-04-08 12:19:38 +0000396 if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
397 /* if the address we're comparing against is stale
398 * then the CP has not added this one back yet, maybe
399 * it never will, so we have to assume it won't and
400 * ignore it. if it does add it back, then it will fail
401 * because this one is now present */
402 continue;
403
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500404 /* error if the length or intf was different */
Neale Ranns744902e2017-08-14 10:35:44 -0700405 vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS;
Neale Ranns59f71132020-04-08 12:19:38 +0000406 error = clib_error_create
Neale Ranns744902e2017-08-14 10:35:44 -0700407 ("failed to add %U which conflicts with %U for interface %U",
408 format_ip6_address_and_length, address,
409 address_length,
410 format_ip6_address_and_length, x,
411 ia->address_length,
412 format_vnet_sw_if_index_name, vnm,
413 sif->sw_if_index);
Neale Ranns59f71132020-04-08 12:19:38 +0000414 goto done;
Neale Ranns744902e2017-08-14 10:35:44 -0700415 }
416 }));
417 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100418 }
Neale Ranns744902e2017-08-14 10:35:44 -0700419 }
420 /* *INDENT-ON* */
421
Neale Ranns59f71132020-04-08 12:19:38 +0000422 if_address_index = ip_interface_address_find (lm, addr_fib, address_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
Neale Ranns59f71132020-04-08 12:19:38 +0000424 if (is_del)
425 {
426 if (~0 == if_address_index)
427 {
428 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
429 error = clib_error_create ("%U not found for interface %U",
430 lm->format_address_and_length,
431 addr_fib, address_length,
432 format_vnet_sw_if_index_name, vnm,
433 sw_if_index);
434 goto done;
435 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
yedgdbd366b2020-05-14 10:51:53 +0800437 error = ip_interface_address_del (lm, vnm, if_address_index, addr_fib,
438 address_length, sw_if_index);
439 if (error)
440 goto done;
Neale Ranns59f71132020-04-08 12:19:38 +0000441 }
442 else
443 {
444 if (~0 != if_address_index)
445 {
446 ip_interface_address_t *ia;
447
448 ia = pool_elt_at_index (lm->if_address_pool, if_address_index);
449
450 if (ia->flags & IP_INTERFACE_ADDRESS_FLAG_STALE)
451 {
452 if (ia->sw_if_index == sw_if_index)
453 {
454 /* re-adding an address during the replace action.
455 * consdier this the update. clear the flag and
456 * we're done */
457 ia->flags &= ~IP_INTERFACE_ADDRESS_FLAG_STALE;
458 goto done;
459 }
460 else
461 {
462 /* The prefix is moving from one interface to another.
463 * delete the stale and add the new */
464 ip6_add_del_interface_address (vm,
465 ia->sw_if_index,
466 address, address_length, 1);
467 ia = NULL;
468 error = ip_interface_address_add (lm, sw_if_index,
469 addr_fib, address_length,
470 &if_address_index);
471 }
472 }
473 else
474 {
475 vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS;
476 error = clib_error_create
477 ("Prefix %U already found on interface %U",
478 lm->format_address_and_length, addr_fib, address_length,
479 format_vnet_sw_if_index_name, vnm, ia->sw_if_index);
480 }
481 }
482 else
483 error = ip_interface_address_add (lm, sw_if_index,
484 addr_fib, address_length,
485 &if_address_index);
486 }
487
488 if (error)
489 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490
Dave Barachd7cb1b52016-12-09 09:52:16 -0500491 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000492 if (!is_del)
Neale Rannsec40a7d2020-04-23 07:36:12 +0000493 ip6_link_enable (sw_if_index, NULL);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000494
Neale Ranns1ff3c152019-10-07 22:40:54 -0700495 /* intf addr routes are added/deleted on admin up/down */
496 if (vnet_sw_interface_is_admin_up (vnm, sw_if_index))
497 {
498 if (is_del)
499 ip6_del_interface_routes (sw_if_index,
500 im, ip6_af.fib_index, address,
501 address_length);
502 else
503 ip6_add_interface_routes (vnm, sw_if_index,
504 im, ip6_af.fib_index,
505 pool_elt_at_index (lm->if_address_pool,
506 if_address_index));
507 }
Neale Ranns59f71132020-04-08 12:19:38 +0000508
509 ip6_add_del_interface_address_callback_t *cb;
510 vec_foreach (cb, im->add_del_interface_address_callbacks)
511 cb->function (im, cb->function_opaque, sw_if_index,
512 address, address_length, if_address_index, is_del);
513
Neale Rannscbe25aa2019-09-30 10:53:31 +0000514 if (is_del)
515 ip6_link_disable (sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
Dave Barachd7cb1b52016-12-09 09:52:16 -0500517done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 vec_free (addr_fib);
519 return error;
520}
521
Damjan Marion38173502019-02-13 19:30:09 +0100522#endif
523
524static clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500525ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500527 ip6_main_t *im = &ip6_main;
528 ip_interface_address_t *ia;
529 ip6_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 u32 is_admin_up, fib_index;
531
Dave Barachd7cb1b52016-12-09 09:52:16 -0500532 vec_validate_init_empty (im->
533 lookup_main.if_address_pool_index_by_sw_if_index,
534 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
536 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
537
538 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
539
Dave Barachd7cb1b52016-12-09 09:52:16 -0500540 /* *INDENT-OFF* */
Dave Barach75fc8542016-10-11 16:16:02 -0400541 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 0 /* honor unnumbered */,
543 ({
544 a = ip_interface_address_get_address (&im->lookup_main, ia);
545 if (is_admin_up)
546 ip6_add_interface_routes (vnm, sw_if_index,
547 im, fib_index,
548 ia);
549 else
Matthew Smith6c92f5b2019-08-07 11:46:30 -0500550 ip6_del_interface_routes (sw_if_index, im, fib_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551 a, ia->address_length);
552 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500553 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
555 return 0;
556}
557
558VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
559
Dave Barachd6534602016-06-14 18:38:02 -0400560/* Built-in ip6 unicast rx feature path definition */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500561/* *INDENT-OFF* */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100562VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
563{
564 .arc_name = "ip6-unicast",
565 .start_nodes = VNET_FEATURES ("ip6-input"),
Dave Baracha25def72018-11-26 11:04:45 -0500566 .last_in_arc = "ip6-lookup",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100567 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
568};
569
Dave Barachd7cb1b52016-12-09 09:52:16 -0500570VNET_FEATURE_INIT (ip6_flow_classify, static) =
571{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100572 .arc_name = "ip6-unicast",
Juraj Sloboda506b2452016-08-07 23:45:24 -0700573 .node_name = "ip6-flow-classify",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100574 .runs_before = VNET_FEATURES ("ip6-inacl"),
Juraj Sloboda506b2452016-08-07 23:45:24 -0700575};
576
Dave Barachd7cb1b52016-12-09 09:52:16 -0500577VNET_FEATURE_INIT (ip6_inacl, static) =
578{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100579 .arc_name = "ip6-unicast",
Dave Barach75fc8542016-10-11 16:16:02 -0400580 .node_name = "ip6-inacl",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100581 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
Dave Barachd6534602016-06-14 18:38:02 -0400582};
583
Dave Barachd7cb1b52016-12-09 09:52:16 -0500584VNET_FEATURE_INIT (ip6_policer_classify, static) =
585{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100586 .arc_name = "ip6-unicast",
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700587 .node_name = "ip6-policer-classify",
Pierre Pfister057b3562018-12-10 17:01:01 +0100588 .runs_before = VNET_FEATURES ("ipsec6-input-feature"),
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700589};
590
Dave Barachd7cb1b52016-12-09 09:52:16 -0500591VNET_FEATURE_INIT (ip6_ipsec, static) =
592{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100593 .arc_name = "ip6-unicast",
Pierre Pfister057b3562018-12-10 17:01:01 +0100594 .node_name = "ipsec6-input-feature",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100595 .runs_before = VNET_FEATURES ("l2tp-decap"),
Dave Barachd6534602016-06-14 18:38:02 -0400596};
597
Dave Barachd7cb1b52016-12-09 09:52:16 -0500598VNET_FEATURE_INIT (ip6_l2tp, static) =
599{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100600 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400601 .node_name = "l2tp-decap",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100602 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
Dave Barachd6534602016-06-14 18:38:02 -0400603};
604
Dave Barachd7cb1b52016-12-09 09:52:16 -0500605VNET_FEATURE_INIT (ip6_vpath, static) =
606{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100607 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400608 .node_name = "vpath-input-ip6",
John Lo2b81eb82017-01-30 13:12:10 -0500609 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
610};
611
612VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
613{
614 .arc_name = "ip6-unicast",
615 .node_name = "ip6-vxlan-bypass",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100616 .runs_before = VNET_FEATURES ("ip6-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400617};
618
Neale Ranns8269d3d2018-01-30 09:02:20 -0800619VNET_FEATURE_INIT (ip6_not_enabled, static) =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500620{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100621 .arc_name = "ip6-unicast",
Neale Ranns8269d3d2018-01-30 09:02:20 -0800622 .node_name = "ip6-not-enabled",
Neale Ranns630198f2017-05-22 09:20:20 -0400623 .runs_before = VNET_FEATURES ("ip6-lookup"),
624};
625
626VNET_FEATURE_INIT (ip6_lookup, static) =
627{
628 .arc_name = "ip6-unicast",
629 .node_name = "ip6-lookup",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100630 .runs_before = 0, /*last feature*/
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100631};
632
Dave Barachd6534602016-06-14 18:38:02 -0400633/* Built-in ip6 multicast rx feature path definition (none now) */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100634VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
635{
636 .arc_name = "ip6-multicast",
637 .start_nodes = VNET_FEATURES ("ip6-input"),
Dave Baracha25def72018-11-26 11:04:45 -0500638 .last_in_arc = "ip6-mfib-forward-lookup",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100639 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
640};
641
642VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
643 .arc_name = "ip6-multicast",
Dave Barachd6534602016-06-14 18:38:02 -0400644 .node_name = "vpath-input-ip6",
Neale Ranns32e1c012016-11-22 17:07:28 +0000645 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400646};
647
Neale Ranns8269d3d2018-01-30 09:02:20 -0800648VNET_FEATURE_INIT (ip6_not_enabled_mc, static) = {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100649 .arc_name = "ip6-multicast",
Neale Ranns8269d3d2018-01-30 09:02:20 -0800650 .node_name = "ip6-not-enabled",
Neale Ranns630198f2017-05-22 09:20:20 -0400651 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
652};
653
654VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
655 .arc_name = "ip6-multicast",
656 .node_name = "ip6-mfib-forward-lookup",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100657 .runs_before = 0, /* last feature */
Neale Ranns5e575b12016-10-03 09:40:25 +0100658};
Dave Barach5331c722016-08-17 11:54:30 -0400659
660/* Built-in ip4 tx feature path definition */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100661VNET_FEATURE_ARC_INIT (ip6_output, static) =
662{
663 .arc_name = "ip6-output",
Neale Rannsf068c3e2018-01-03 04:18:48 -0800664 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain", "ip6-dvr-dpo"),
Dave Baracha25def72018-11-26 11:04:45 -0500665 .last_in_arc = "interface-output",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100666 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
Dave Barach5331c722016-08-17 11:54:30 -0400667};
668
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100669VNET_FEATURE_INIT (ip6_outacl, static) = {
670 .arc_name = "ip6-output",
671 .node_name = "ip6-outacl",
Pierre Pfister057b3562018-12-10 17:01:01 +0100672 .runs_before = VNET_FEATURES ("ipsec6-output-feature"),
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100673};
674
Matus Fabian08a6f012016-11-15 06:08:51 -0800675VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
676 .arc_name = "ip6-output",
Pierre Pfister057b3562018-12-10 17:01:01 +0100677 .node_name = "ipsec6-output-feature",
Matus Fabian08a6f012016-11-15 06:08:51 -0800678 .runs_before = VNET_FEATURES ("interface-output"),
679};
680
Damjan Marion8b3191e2016-11-09 19:54:20 +0100681VNET_FEATURE_INIT (ip6_interface_output, static) = {
682 .arc_name = "ip6-output",
683 .node_name = "interface-output",
684 .runs_before = 0, /* not before any other features */
685};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500686/* *INDENT-ON* */
Dave Barachd6534602016-06-14 18:38:02 -0400687
Damjan Marion38173502019-02-13 19:30:09 +0100688static clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500689ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690{
Florin Corasb5c13fd2017-05-10 12:32:53 -0700691 ip6_main_t *im = &ip6_main;
692
Nathan Skrzypczak7854b462021-08-16 16:13:40 +0200693 vec_validate_init_empty (im->fib_index_by_sw_if_index, sw_if_index, ~0);
694 vec_validate_init_empty (im->mfib_index_by_sw_if_index, sw_if_index, ~0);
Florin Corasb5c13fd2017-05-10 12:32:53 -0700695
Nathan Skrzypczak7854b462021-08-16 16:13:40 +0200696 if (is_add)
697 {
698 /* Fill in lookup tables with default table (0). */
699 im->fib_index_by_sw_if_index[sw_if_index] = 0;
700 im->mfib_index_by_sw_if_index[sw_if_index] = 0;
701 }
702 else
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200703 {
704 /* Ensure that IPv6 is disabled */
705 ip6_main_t *im6 = &ip6_main;
706 ip_lookup_main_t *lm6 = &im6->lookup_main;
707 ip_interface_address_t *ia = 0;
708 ip6_address_t *address;
709 vlib_main_t *vm = vlib_get_main ();
710
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700711 vnet_sw_interface_update_unnumbered (sw_if_index, ~0, 0);
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200712 /* *INDENT-OFF* */
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700713 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200714 ({
715 address = ip_interface_address_get_address (lm6, ia);
716 ip6_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1);
717 }));
718 /* *INDENT-ON* */
719 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Nathan Skrzypczaka424dd12021-08-20 15:53:43 +0200720
721 if (0 != im6->fib_index_by_sw_if_index[sw_if_index])
722 fib_table_bind (FIB_PROTOCOL_IP6, sw_if_index, 0);
723 if (0 != im6->mfib_index_by_sw_if_index[sw_if_index])
724 mfib_table_bind (FIB_PROTOCOL_IP6, sw_if_index, 0);
725
726 /* Erase the lookup tables just in case */
727 im6->fib_index_by_sw_if_index[sw_if_index] = ~0;
728 im6->mfib_index_by_sw_if_index[sw_if_index] = ~0;
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200729 }
730
Neale Ranns8269d3d2018-01-30 09:02:20 -0800731 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
Damjan Marion8b3191e2016-11-09 19:54:20 +0100732 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733
Neale Ranns8269d3d2018-01-30 09:02:20 -0800734 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
735 sw_if_index, is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737 return /* no error */ 0;
738}
739
740VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
741
Damjan Marion38173502019-02-13 19:30:09 +0100742VLIB_NODE_FN (ip6_lookup_node) (vlib_main_t * vm,
743 vlib_node_runtime_t * node,
744 vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200745{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100746 return ip6_lookup_inline (vm, node, frame);
Damjan Marionaca64c92016-04-13 09:48:56 +0200747}
748
Dave Barachd7cb1b52016-12-09 09:52:16 -0500749static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100750
Dave Barachd7cb1b52016-12-09 09:52:16 -0500751/* *INDENT-OFF* */
752VLIB_REGISTER_NODE (ip6_lookup_node) =
753{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 .name = "ip6-lookup",
755 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100756 .format_trace = format_ip6_lookup_trace,
Ole Troanf0f85222016-06-14 21:12:32 +0200757 .n_next_nodes = IP6_LOOKUP_N_NEXT,
Damjan Marionb2707892016-04-13 11:21:07 +0200758 .next_nodes = IP6_LOOKUP_NEXT_NODES,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500760/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Damjan Marion38173502019-02-13 19:30:09 +0100762VLIB_NODE_FN (ip6_load_balance_node) (vlib_main_t * vm,
763 vlib_node_runtime_t * node,
764 vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200765{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500766 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
Neale Ranns3ce72b22019-05-27 08:21:32 -0400767 u32 n_left, *from;
Damjan Marion067cd622018-07-11 12:47:43 +0200768 u32 thread_index = vm->thread_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500769 ip6_main_t *im = &ip6_main;
Neale Ranns3ce72b22019-05-27 08:21:32 -0400770 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
771 u16 nexts[VLIB_FRAME_SIZE], *next;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100772
773 from = vlib_frame_vector_args (frame);
Neale Ranns3ce72b22019-05-27 08:21:32 -0400774 n_left = frame->n_vectors;
775 next = nexts;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100776
Neale Ranns3ce72b22019-05-27 08:21:32 -0400777 vlib_get_buffers (vm, from, bufs, n_left);
778
779 while (n_left >= 4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100780 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400781 const load_balance_t *lb0, *lb1;
782 const ip6_header_t *ip0, *ip1;
783 u32 lbi0, hc0, lbi1, hc1;
784 const dpo_id_t *dpo0, *dpo1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100785
Neale Ranns3ce72b22019-05-27 08:21:32 -0400786 /* Prefetch next iteration. */
787 {
788 vlib_prefetch_buffer_header (b[2], STORE);
789 vlib_prefetch_buffer_header (b[3], STORE);
Dave Barach75fc8542016-10-11 16:16:02 -0400790
Neale Ranns3ce72b22019-05-27 08:21:32 -0400791 CLIB_PREFETCH (b[2]->data, sizeof (ip0[0]), STORE);
792 CLIB_PREFETCH (b[3]->data, sizeof (ip0[0]), STORE);
793 }
794
795 ip0 = vlib_buffer_get_current (b[0]);
796 ip1 = vlib_buffer_get_current (b[1]);
797 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
798 lbi1 = vnet_buffer (b[1])->ip.adj_index[VLIB_TX];
799
800 lb0 = load_balance_get (lbi0);
801 lb1 = load_balance_get (lbi1);
802
803 /*
804 * this node is for via FIBs we can re-use the hash value from the
805 * to node if present.
806 * We don't want to use the same hash value at each level in the recursion
807 * graph as that would lead to polarisation
808 */
809 hc0 = hc1 = 0;
810
811 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500812 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400813 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500814 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400815 hc0 = vnet_buffer (b[0])->ip.flow_hash =
816 vnet_buffer (b[0])->ip.flow_hash >> 1;
Neale Rannsf12a83f2017-04-18 09:09:40 -0700817 }
818 else
819 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400820 hc0 = vnet_buffer (b[0])->ip.flow_hash =
821 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500822 }
Neale Ranns3ce72b22019-05-27 08:21:32 -0400823 dpo0 = load_balance_get_fwd_bucket
824 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
825 }
826 else
827 {
828 dpo0 = load_balance_get_bucket_i (lb0, 0);
829 }
830 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
831 {
832 if (PREDICT_TRUE (vnet_buffer (b[1])->ip.flow_hash))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500833 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400834 hc1 = vnet_buffer (b[1])->ip.flow_hash =
835 vnet_buffer (b[1])->ip.flow_hash >> 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500836 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700837 else
838 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400839 hc1 = vnet_buffer (b[1])->ip.flow_hash =
840 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
Neale Rannsf12a83f2017-04-18 09:09:40 -0700841 }
Neale Ranns3ce72b22019-05-27 08:21:32 -0400842 dpo1 = load_balance_get_fwd_bucket
843 (lb1, (hc1 & (lb1->lb_n_buckets_minus_1)));
844 }
845 else
846 {
847 dpo1 = load_balance_get_bucket_i (lb1, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500848 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000849
Neale Ranns3ce72b22019-05-27 08:21:32 -0400850 next[0] = dpo0->dpoi_next_node;
851 next[1] = dpo1->dpoi_next_node;
852
853 /* Only process the HBH Option Header if explicitly configured to do so */
854 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500855 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400856 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
857 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
858 }
859 /* Only process the HBH Option Header if explicitly configured to do so */
860 if (PREDICT_FALSE (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
861 {
862 next[1] = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
863 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[1];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500864 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100865
Neale Ranns3ce72b22019-05-27 08:21:32 -0400866 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
867 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
868
869 vlib_increment_combined_counter
870 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
871 vlib_increment_combined_counter
872 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, b[1]));
873
874 b += 2;
875 next += 2;
876 n_left -= 2;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100877 }
878
Neale Ranns3ce72b22019-05-27 08:21:32 -0400879 while (n_left > 0)
880 {
881 const load_balance_t *lb0;
882 const ip6_header_t *ip0;
883 const dpo_id_t *dpo0;
884 u32 lbi0, hc0;
885
886 ip0 = vlib_buffer_get_current (b[0]);
887 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
888
889 lb0 = load_balance_get (lbi0);
890
891 hc0 = 0;
892 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
893 {
894 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
895 {
896 hc0 = vnet_buffer (b[0])->ip.flow_hash =
897 vnet_buffer (b[0])->ip.flow_hash >> 1;
898 }
899 else
900 {
901 hc0 = vnet_buffer (b[0])->ip.flow_hash =
902 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
903 }
904 dpo0 = load_balance_get_fwd_bucket
905 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
906 }
907 else
908 {
909 dpo0 = load_balance_get_bucket_i (lb0, 0);
910 }
911
912 next[0] = dpo0->dpoi_next_node;
913 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
914
915 /* Only process the HBH Option Header if explicitly configured to do so */
916 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
917 {
918 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
919 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
920 }
921
922 vlib_increment_combined_counter
923 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
924
925 b += 1;
926 next += 1;
927 n_left -= 1;
928 }
929
930 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
931
Neale Rannsa71844f2018-11-08 07:31:36 -0800932 if (node->flags & VLIB_NODE_FLAG_TRACE)
933 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
934
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100935 return frame->n_vectors;
Damjan Marionaca64c92016-04-13 09:48:56 +0200936}
937
Dave Barachd7cb1b52016-12-09 09:52:16 -0500938/* *INDENT-OFF* */
939VLIB_REGISTER_NODE (ip6_load_balance_node) =
940{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100941 .name = "ip6-load-balance",
Damjan Marionaca64c92016-04-13 09:48:56 +0200942 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200943 .sibling_of = "ip6-lookup",
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100944 .format_trace = format_ip6_lookup_trace,
Damjan Marionaca64c92016-04-13 09:48:56 +0200945};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500946/* *INDENT-ON* */
Damjan Marionaca64c92016-04-13 09:48:56 +0200947
Dave Barachd7cb1b52016-12-09 09:52:16 -0500948typedef struct
949{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 /* Adjacency taken. */
951 u32 adj_index;
952 u32 flow_hash;
John Lo2d343742016-01-19 17:27:17 -0500953 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
955 /* Packet data, possibly *after* rewrite. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500956 u8 packet_data[128 - 1 * sizeof (u32)];
957}
958ip6_forward_next_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959
Damjan Marion38173502019-02-13 19:30:09 +0100960#ifndef CLIB_MARCH_VARIANT
John Lo2b81eb82017-01-30 13:12:10 -0500961u8 *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500962format_ip6_forward_next_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963{
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100964 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
965 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500966 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200967 u32 indent = format_get_indent (s);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100968
Neale Rannsdc617b82020-08-20 08:22:56 +0000969 s = format (s, "%Ufib:%d adj:%d flow:%d",
970 format_white_space, indent,
971 t->fib_index, t->adj_index, t->flow_hash);
972 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500973 format_white_space, indent,
974 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100975 return s;
976}
Damjan Marion38173502019-02-13 19:30:09 +0100977#endif
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100978
Dave Barachd7cb1b52016-12-09 09:52:16 -0500979static u8 *
980format_ip6_lookup_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100981{
982 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
983 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500984 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200985 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
John Loac8146c2016-09-27 17:44:02 -0400987 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500988 t->fib_index, t->adj_index, t->flow_hash);
989 s = format (s, "\n%U%U",
990 format_white_space, indent,
991 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100992 return s;
993}
Pierre Pfister0febaf12016-06-08 12:23:21 +0100994
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995
Dave Barachd7cb1b52016-12-09 09:52:16 -0500996static u8 *
997format_ip6_rewrite_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100998{
999 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1000 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001001 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +02001002 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003
Pierre Pfistera38c3df2016-06-13 10:28:09 +01001004 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001005 t->fib_index, t->adj_index, format_ip_adjacency,
1006 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
Pierre Pfistera38c3df2016-06-13 10:28:09 +01001007 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001008 format_white_space, indent,
1009 format_ip_adjacency_packet_data,
Neale Ranns0b6a8572019-10-30 17:34:14 +00001010 t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 return s;
1012}
1013
1014/* Common trace function for all ip6-forward next nodes. */
Damjan Marion38173502019-02-13 19:30:09 +01001015#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016void
1017ip6_forward_next_trace (vlib_main_t * vm,
1018 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001019 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001021 u32 *from, n_left;
1022 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
1024 n_left = frame->n_vectors;
1025 from = vlib_frame_vector_args (frame);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001026
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027 while (n_left >= 4)
1028 {
1029 u32 bi0, bi1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001030 vlib_buffer_t *b0, *b1;
1031 ip6_forward_next_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032
1033 /* Prefetch next iteration. */
1034 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1035 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1036
1037 bi0 = from[0];
1038 bi1 = from[1];
1039
1040 b0 = vlib_get_buffer (vm, bi0);
1041 b1 = vlib_get_buffer (vm, bi1);
1042
1043 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1044 {
1045 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1046 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001047 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1048 t0->fib_index =
1049 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1050 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1051 vec_elt (im->fib_index_by_sw_if_index,
1052 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001053
Dave Barach178cf492018-11-13 16:34:13 -05001054 clib_memcpy_fast (t0->packet_data,
1055 vlib_buffer_get_current (b0),
1056 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057 }
1058 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1059 {
1060 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1061 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001062 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1063 t1->fib_index =
1064 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1065 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1066 vec_elt (im->fib_index_by_sw_if_index,
1067 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001068
Dave Barach178cf492018-11-13 16:34:13 -05001069 clib_memcpy_fast (t1->packet_data,
1070 vlib_buffer_get_current (b1),
1071 sizeof (t1->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072 }
1073 from += 2;
1074 n_left -= 2;
1075 }
1076
1077 while (n_left >= 1)
1078 {
1079 u32 bi0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001080 vlib_buffer_t *b0;
1081 ip6_forward_next_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082
1083 bi0 = from[0];
1084
1085 b0 = vlib_get_buffer (vm, bi0);
1086
1087 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1088 {
1089 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1090 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001091 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1092 t0->fib_index =
1093 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1094 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1095 vec_elt (im->fib_index_by_sw_if_index,
1096 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001097
Dave Barach178cf492018-11-13 16:34:13 -05001098 clib_memcpy_fast (t0->packet_data,
1099 vlib_buffer_get_current (b0),
1100 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101 }
1102 from += 1;
1103 n_left -= 1;
1104 }
1105}
1106
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107/* Compute TCP/UDP/ICMP6 checksum in software. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001108u16
1109ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1110 ip6_header_t * ip0, int *bogus_lengthp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111{
Matthew Smith97677a22020-02-05 11:46:40 -06001112 ip_csum_t sum0 = 0;
1113 u16 payload_length, payload_length_host_byte_order;
Srikanth A02833ff2019-10-02 17:48:58 -07001114 u32 i;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001115 u32 headers_size = sizeof (ip0[0]);
Dave Barachc4abafd2019-09-04 12:09:32 -04001116 u8 *data_this_buffer;
Matthew Smith97677a22020-02-05 11:46:40 -06001117 u8 next_hdr = ip0->protocol;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118
Dave Barachd7cb1b52016-12-09 09:52:16 -05001119 ASSERT (bogus_lengthp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 *bogus_lengthp = 0;
1121
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
Dave Barachc4abafd2019-09-04 12:09:32 -04001123 data_this_buffer = (u8 *) (ip0 + 1);
Matthew Smith97677a22020-02-05 11:46:40 -06001124 payload_length = ip0->payload_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301126 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1127 * or UDP-Ping packets */
Matthew Smith97677a22020-02-05 11:46:40 -06001128 if (PREDICT_FALSE (next_hdr == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001130 u32 skip_bytes;
1131 ip6_hop_by_hop_ext_t *ext_hdr =
1132 (ip6_hop_by_hop_ext_t *) data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133
1134 /* validate really icmp6 next */
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301135 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1136 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137
Dave Barachd7cb1b52016-12-09 09:52:16 -05001138 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1139 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
Dave Barach75fc8542016-10-11 16:16:02 -04001140
Dave Barachd7cb1b52016-12-09 09:52:16 -05001141 payload_length_host_byte_order -= skip_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 headers_size += skip_bytes;
Matthew Smith97677a22020-02-05 11:46:40 -06001143
1144 /* pseudo-header adjustments:
1145 * exclude ext header bytes from payload length
1146 * use payload IP proto rather than ext header IP proto
1147 */
1148 payload_length = clib_host_to_net_u16 (payload_length_host_byte_order);
1149 next_hdr = ext_hdr->next_hdr;
1150 }
1151
1152 /* Initialize checksum with ip pseudo-header. */
1153 sum0 = payload_length + clib_host_to_net_u16 (next_hdr);
1154
1155 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1156 {
1157 sum0 = ip_csum_with_carry
1158 (sum0, clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1159 sum0 = ip_csum_with_carry
1160 (sum0, clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001161 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162
John Lo3bc6bc22019-08-03 14:36:39 -04001163 if (p0)
Srikanth A02833ff2019-10-02 17:48:58 -07001164 return ip_calculate_l4_checksum (vm, p0, sum0,
1165 payload_length_host_byte_order,
1166 (u8 *) ip0, headers_size, NULL);
1167 else
1168 return ip_calculate_l4_checksum (vm, 0, sum0,
1169 payload_length_host_byte_order, NULL, 0,
1170 data_this_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171}
1172
Dave Barachd7cb1b52016-12-09 09:52:16 -05001173u32
1174ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001176 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1177 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 u16 sum16;
1179 int bogus_length;
1180
1181 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1182 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1183 || ip0->protocol == IP_PROTOCOL_ICMP6
1184 || ip0->protocol == IP_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001185 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
1187 udp0 = (void *) (ip0 + 1);
1188 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1189 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001190 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1191 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 return p0->flags;
1193 }
1194
1195 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1196
Damjan Marion213b5aa2017-07-13 21:19:27 +02001197 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1198 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
1200 return p0->flags;
1201}
Damjan Marion38173502019-02-13 19:30:09 +01001202#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001203
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301204/**
1205 * @brief returns number of links on which src is reachable.
1206 */
1207always_inline int
1208ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1209{
1210 const load_balance_t *lb0;
1211 index_t lbi;
Florin Corasf3a3bad2018-03-28 02:18:29 -07001212 u32 fib_index;
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301213
Florin Corasf3a3bad2018-03-28 02:18:29 -07001214 fib_index = vec_elt (im->fib_index_by_sw_if_index,
1215 vnet_buffer (b)->sw_if_index[VLIB_RX]);
1216 fib_index =
1217 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
1218 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301219
Simon Zhange7eba482019-08-25 15:30:45 +08001220 lbi = ip6_fib_table_fwding_lookup (fib_index, &i->src_address);
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301221 lb0 = load_balance_get (lbi);
1222
1223 return (fib_urpf_check_size (lb0->lb_urpf));
1224}
1225
rootc9d1c5b2017-08-15 12:58:31 -04001226always_inline u8
1227ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1228 u32 * udp_offset0)
1229{
Ole Troan03092c12021-11-23 15:55:39 +01001230 int nh = ip6_locate_header (p0, ip0, -1, udp_offset0);
1231 if (nh > 0)
1232 if (nh == IP_PROTOCOL_UDP || nh == IP_PROTOCOL_TCP)
1233 return nh;
1234 return 0;
rootc9d1c5b2017-08-15 12:58:31 -04001235}
1236
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001237/* *INDENT-OFF* */
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001238VNET_FEATURE_ARC_INIT (ip6_local) = {
1239 .arc_name = "ip6-local",
1240 .start_nodes = VNET_FEATURES ("ip6-local", "ip6-receive"),
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001241};
1242/* *INDENT-ON* */
1243
johny17478e42019-10-11 18:28:51 +02001244static_always_inline u8
1245ip6_tcp_udp_icmp_bad_length (vlib_main_t * vm, vlib_buffer_t * p0)
1246{
1247
1248 u16 payload_length_host_byte_order;
1249 u32 n_this_buffer, n_bytes_left;
1250 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1251 u32 headers_size = sizeof (ip0[0]);
1252 u8 *data_this_buffer;
1253
1254
1255 data_this_buffer = (u8 *) (ip0 + 1);
1256
1257 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *) data_this_buffer;
1258
1259 /* validate really icmp6 next */
1260
1261 if (!(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1262 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP))
1263 return 0;
1264
1265
1266 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1267 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1268
johnya633a432019-12-06 13:58:35 +01001269
1270 u32 n_ip_bytes_this_buffer =
1271 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
1272 if (n_this_buffer + headers_size > n_ip_bytes_this_buffer)
johny17478e42019-10-11 18:28:51 +02001273 {
johnya633a432019-12-06 13:58:35 +01001274 n_this_buffer = p0->current_length > headers_size ?
1275 n_ip_bytes_this_buffer - headers_size : 0;
johny17478e42019-10-11 18:28:51 +02001276 }
1277
1278 n_bytes_left -= n_this_buffer;
Klement Sekeracf1e8c12022-01-28 13:16:46 +00001279 n_bytes_left -= vlib_buffer_length_in_chain (vm, p0) - p0->current_length;
johny17478e42019-10-11 18:28:51 +02001280
1281 if (n_bytes_left == 0)
1282 return 0;
1283 else
1284 return 1;
1285}
1286
Benoît Ganne26a10192019-02-14 15:32:45 +01001287always_inline uword
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001288ip6_local_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1289 vlib_frame_t *frame, int head_of_feature_arc,
1290 int is_receive_dpo)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001291{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001292 ip6_main_t *im = &ip6_main;
1293 ip_lookup_main_t *lm = &im->lookup_main;
Benoît Ganne26a10192019-02-14 15:32:45 +01001294 u32 *from, n_left_from;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001295 vlib_node_runtime_t *error_node =
1296 vlib_node_get_runtime (vm, ip6_input_node.index);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001297 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
Benoît Ganne26a10192019-02-14 15:32:45 +01001298 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1299 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001300
1301 from = vlib_frame_vector_args (frame);
1302 n_left_from = frame->n_vectors;
Dave Barach75fc8542016-10-11 16:16:02 -04001303
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304 if (node->flags & VLIB_NODE_FLAG_TRACE)
1305 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1306
Benoît Ganne26a10192019-02-14 15:32:45 +01001307 vlib_get_buffers (vm, from, bufs, n_left_from);
1308 b = bufs;
1309 next = nexts;
1310
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001311 while (n_left_from > 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001313 /* Prefetch next iteration. */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001314 if (n_left_from >= 6)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001316 vlib_prefetch_buffer_header (b[4], STORE);
1317 vlib_prefetch_buffer_header (b[5], STORE);
1318 vlib_prefetch_buffer_data (b[2], LOAD);
1319 vlib_prefetch_buffer_data (b[3], LOAD);
Benoît Ganne26a10192019-02-14 15:32:45 +01001320 }
Dave Barach75fc8542016-10-11 16:16:02 -04001321
Neale Rannse22a7042022-08-09 03:03:29 +00001322 vl_counter_ip6_enum_t error[2];
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001323 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1324 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
Dave Barach75fc8542016-10-11 16:16:02 -04001325
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001326 ip6_header_t *ip[2];
1327 ip[0] = vlib_buffer_get_current (b[0]);
1328 ip[1] = vlib_buffer_get_current (b[1]);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001329
Benoît Ganne26a10192019-02-14 15:32:45 +01001330 if (head_of_feature_arc)
1331 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001332 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1333 vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001334
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001335 u8 type[2];
1336 type[0] = lm->builtin_protocol_by_ip_protocol[ip[0]->protocol];
1337 type[1] = lm->builtin_protocol_by_ip_protocol[ip[1]->protocol];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001339 u32 flags[2];
1340 flags[0] = b[0]->flags;
1341 flags[1] = b[1]->flags;
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001342
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +02001343 vnet_buffer_oflags_t oflags[2];
Mohsin Kazmia7e830e2021-04-23 15:16:50 +02001344 oflags[0] = vnet_buffer (b[0])->oflags;
1345 oflags[1] = vnet_buffer (b[1])->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +01001346
1347 u32 l4_offload[2];
1348 l4_offload[0] = (flags[0] & VNET_BUFFER_F_OFFLOAD) &&
1349 (oflags[0] & (VNET_BUFFER_OFFLOAD_F_TCP_CKSUM |
1350 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM));
1351 l4_offload[1] = (flags[1] & VNET_BUFFER_F_OFFLOAD) &&
1352 (oflags[1] & (VNET_BUFFER_OFFLOAD_F_TCP_CKSUM |
1353 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM));
1354
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001355 u32 good_l4_csum[2];
1356 good_l4_csum[0] =
Mohsin Kazmi68095382021-02-10 11:26:24 +01001357 (flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) | l4_offload[0];
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001358 good_l4_csum[1] =
Mohsin Kazmi68095382021-02-10 11:26:24 +01001359 (flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) | l4_offload[1];
Filip Tehlarb601f222017-01-02 10:22:56 +01001360
Damjan Marion34e823f2019-02-19 08:55:18 +01001361 u32 udp_offset[2] = { };
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001362 u8 is_tcp_udp[2];
1363 is_tcp_udp[0] =
1364 ip6_next_proto_is_tcp_udp (b[0], ip[0], &udp_offset[0]);
1365 is_tcp_udp[1] =
1366 ip6_next_proto_is_tcp_udp (b[1], ip[1], &udp_offset[1]);
1367 i16 len_diff[2] = { 0 };
1368 if (PREDICT_TRUE (is_tcp_udp[0]))
Shwethab78292e2016-09-13 11:51:00 +01001369 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001370 udp_header_t *udp =
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001371 (udp_header_t *) ((u8 *) ip[0] + udp_offset[0]);
1372 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UDP
Benoît Ganne26a10192019-02-14 15:32:45 +01001373 && udp->checksum == 0;
1374 /* optimistically verify UDP length. */
1375 u16 ip_len, udp_len;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001376 ip_len = clib_net_to_host_u16 (ip[0]->payload_length);
Benoît Ganne26a10192019-02-14 15:32:45 +01001377 udp_len = clib_net_to_host_u16 (udp->length);
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001378 len_diff[0] = ip_len - udp_len;
Shwethab78292e2016-09-13 11:51:00 +01001379 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001380 if (PREDICT_TRUE (is_tcp_udp[1]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001382 udp_header_t *udp =
1383 (udp_header_t *) ((u8 *) ip[1] + udp_offset[1]);
1384 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UDP
1385 && udp->checksum == 0;
1386 /* optimistically verify UDP length. */
1387 u16 ip_len, udp_len;
1388 ip_len = clib_net_to_host_u16 (ip[1]->payload_length);
1389 udp_len = clib_net_to_host_u16 (udp->length);
1390 len_diff[1] = ip_len - udp_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001391 }
1392
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001393 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1394 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1395
1396 len_diff[0] = type[0] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[0] : 0;
1397 len_diff[1] = type[1] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[1] : 0;
1398
1399 u8 need_csum[2];
1400 need_csum[0] = type[0] != IP_BUILTIN_PROTOCOL_UNKNOWN
1401 && !good_l4_csum[0]
1402 && !(flags[0] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1403 need_csum[1] = type[1] != IP_BUILTIN_PROTOCOL_UNKNOWN
1404 && !good_l4_csum[1]
1405 && !(flags[1] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1406 if (PREDICT_FALSE (need_csum[0]))
1407 {
1408 flags[0] = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1409 good_l4_csum[0] = flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
johny17478e42019-10-11 18:28:51 +02001410 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1411 }
1412 else
1413 {
1414 if (ip6_tcp_udp_icmp_bad_length (vm, b[0]))
1415 error[0] = IP6_ERROR_BAD_LENGTH;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001416 }
1417 if (PREDICT_FALSE (need_csum[1]))
1418 {
1419 flags[1] = ip6_tcp_udp_icmp_validate_checksum (vm, b[1]);
1420 good_l4_csum[1] = flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
johny17478e42019-10-11 18:28:51 +02001421 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1422 }
1423 else
1424 {
1425 if (ip6_tcp_udp_icmp_bad_length (vm, b[1]))
1426 error[1] = IP6_ERROR_BAD_LENGTH;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001427 }
1428
johny17478e42019-10-11 18:28:51 +02001429
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001430 error[0] = len_diff[0] < 0 ? IP6_ERROR_UDP_LENGTH : error[0];
johny17478e42019-10-11 18:28:51 +02001431
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001432 error[1] = len_diff[1] < 0 ? IP6_ERROR_UDP_LENGTH : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001433
Benoît Ganne26a10192019-02-14 15:32:45 +01001434 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1435 IP6_ERROR_UDP_CHECKSUM,
1436 "Wrong IP6 errors constants");
1437 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1438 IP6_ERROR_ICMP_CHECKSUM,
1439 "Wrong IP6 errors constants");
1440
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001441 error[0] =
1442 !good_l4_csum[0] ? IP6_ERROR_UDP_CHECKSUM + type[0] : error[0];
1443 error[1] =
1444 !good_l4_csum[1] ? IP6_ERROR_UDP_CHECKSUM + type[1] : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001445
1446 /* Drop packets from unroutable hosts. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001447 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001448 u8 unroutable[2];
1449 unroutable[0] = error[0] == IP6_ERROR_UNKNOWN_PROTOCOL
1450 && type[0] != IP_BUILTIN_PROTOCOL_ICMP
1451 && !ip6_address_is_link_local_unicast (&ip[0]->src_address);
1452 unroutable[1] = error[1] == IP6_ERROR_UNKNOWN_PROTOCOL
1453 && type[1] != IP_BUILTIN_PROTOCOL_ICMP
1454 && !ip6_address_is_link_local_unicast (&ip[1]->src_address);
1455 if (PREDICT_FALSE (unroutable[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001456 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001457 error[0] =
1458 !ip6_urpf_loose_check (im, b[0],
1459 ip[0]) ? IP6_ERROR_SRC_LOOKUP_MISS
1460 : error[0];
1461 }
1462 if (PREDICT_FALSE (unroutable[1]))
1463 {
1464 error[1] =
1465 !ip6_urpf_loose_check (im, b[1],
1466 ip[1]) ? IP6_ERROR_SRC_LOOKUP_MISS
1467 : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468 }
1469
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001470 vnet_buffer (b[0])->ip.fib_index =
1471 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1472 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1473 vnet_buffer (b[0])->ip.fib_index;
1474 vnet_buffer (b[1])->ip.fib_index =
1475 vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ?
1476 vnet_buffer (b[1])->sw_if_index[VLIB_TX] :
1477 vnet_buffer (b[1])->ip.fib_index;
Alexander Chernavin65e770d2022-04-11 13:02:11 +00001478
1479 vnet_buffer (b[0])->ip.rx_sw_if_index =
1480 vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1481 vnet_buffer (b[1])->ip.rx_sw_if_index =
1482 vnet_buffer (b[1])->sw_if_index[VLIB_RX];
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001483 if (is_receive_dpo)
1484 {
1485 const receive_dpo_t *rd0, *rd1;
1486 rd0 =
1487 receive_dpo_get (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
1488 rd1 =
1489 receive_dpo_get (vnet_buffer (b[1])->ip.adj_index[VLIB_TX]);
Alexander Chernavin65e770d2022-04-11 13:02:11 +00001490 if (rd0->rd_sw_if_index != ~0)
1491 vnet_buffer (b[0])->ip.rx_sw_if_index = rd0->rd_sw_if_index;
1492 if (rd1->rd_sw_if_index != ~0)
1493 vnet_buffer (b[1])->ip.rx_sw_if_index = rd1->rd_sw_if_index;
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001494 }
Benoît Ganne26a10192019-02-14 15:32:45 +01001495 } /* head_of_feature_arc */
Florin Corascea194d2017-10-02 00:18:51 -07001496
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001497 next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol];
1498 next[0] =
1499 error[0] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1500 next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol];
1501 next[1] =
1502 error[1] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[1];
Florin Corascea194d2017-10-02 00:18:51 -07001503
Benoît Gannec15539a2021-01-19 16:40:07 +01001504 b[0]->error = error_node->errors[error[0]];
1505 b[1]->error = error_node->errors[error[1]];
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001506
Benoît Ganne26a10192019-02-14 15:32:45 +01001507 if (head_of_feature_arc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001509 u8 ip6_unknown[2];
1510 ip6_unknown[0] = error[0] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1511 ip6_unknown[1] = error[1] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1512 if (PREDICT_TRUE (ip6_unknown[0]))
Shwethab78292e2016-09-13 11:51:00 +01001513 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001514 u32 next32 = next[0];
Benoît Ganne26a10192019-02-14 15:32:45 +01001515 vnet_feature_arc_start (arc_index,
Florin Corasf8408802021-11-09 18:29:03 -08001516 vnet_buffer (b[0])->ip.rx_sw_if_index,
1517 &next32, b[0]);
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001518 next[0] = next32;
1519 }
1520 if (PREDICT_TRUE (ip6_unknown[1]))
1521 {
1522 u32 next32 = next[1];
1523 vnet_feature_arc_start (arc_index,
Florin Corasf8408802021-11-09 18:29:03 -08001524 vnet_buffer (b[1])->ip.rx_sw_if_index,
1525 &next32, b[1]);
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001526 next[1] = next32;
Shwethab78292e2016-09-13 11:51:00 +01001527 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001528 }
Dave Barach75fc8542016-10-11 16:16:02 -04001529
Benoît Ganne26a10192019-02-14 15:32:45 +01001530 /* next */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001531 b += 2;
1532 next += 2;
1533 n_left_from -= 2;
Benoît Ganne26a10192019-02-14 15:32:45 +01001534 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001535
Benoît Ganne26a10192019-02-14 15:32:45 +01001536 while (n_left_from)
1537 {
1538 u8 error;
1539 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1540
1541 ip6_header_t *ip;
1542 ip = vlib_buffer_get_current (b[0]);
1543
1544 if (head_of_feature_arc)
1545 {
1546 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1547 u8 type = lm->builtin_protocol_by_ip_protocol[ip->protocol];
1548
1549 u32 flags = b[0]->flags;
Benoît Ganne26a10192019-02-14 15:32:45 +01001550
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +02001551 vnet_buffer_oflags_t oflags = vnet_buffer (b[0])->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +01001552
1553 u32 l4_offload = (flags & VNET_BUFFER_F_OFFLOAD) &&
1554 (oflags & (VNET_BUFFER_OFFLOAD_F_TCP_CKSUM |
1555 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM));
1556
1557 u32 good_l4_csum =
1558 (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) | l4_offload;
Benoît Ganne26a10192019-02-14 15:32:45 +01001559 u32 udp_offset;
1560 i16 len_diff = 0;
1561 u8 is_tcp_udp = ip6_next_proto_is_tcp_udp (b[0], ip, &udp_offset);
1562 if (PREDICT_TRUE (is_tcp_udp))
1563 {
1564 udp_header_t *udp = (udp_header_t *) ((u8 *) ip + udp_offset);
1565 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1566 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UDP
1567 && udp->checksum == 0;
1568 /* optimistically verify UDP length. */
1569 u16 ip_len, udp_len;
1570 ip_len = clib_net_to_host_u16 (ip->payload_length);
1571 udp_len = clib_net_to_host_u16 (udp->length);
1572 len_diff = ip_len - udp_len;
1573 }
1574
1575 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UNKNOWN;
1576 len_diff = type == IP_BUILTIN_PROTOCOL_UDP ? len_diff : 0;
1577
Mohsin Kazmi68095382021-02-10 11:26:24 +01001578 u8 need_csum = type != IP_BUILTIN_PROTOCOL_UNKNOWN &&
1579 !good_l4_csum &&
1580 !(flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
Benoît Ganne26a10192019-02-14 15:32:45 +01001581 if (PREDICT_FALSE (need_csum))
1582 {
1583 flags = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1584 good_l4_csum = flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
johny17478e42019-10-11 18:28:51 +02001585 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1586 }
1587 else
1588 {
1589 if (ip6_tcp_udp_icmp_bad_length (vm, b[0]))
1590 error = IP6_ERROR_BAD_LENGTH;
Benoît Ganne26a10192019-02-14 15:32:45 +01001591 }
1592
johny17478e42019-10-11 18:28:51 +02001593
1594
Benoît Ganne26a10192019-02-14 15:32:45 +01001595 error = len_diff < 0 ? IP6_ERROR_UDP_LENGTH : error;
Benoît Ganne26a10192019-02-14 15:32:45 +01001596 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1597 IP6_ERROR_UDP_CHECKSUM,
1598 "Wrong IP6 errors constants");
1599 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1600 IP6_ERROR_ICMP_CHECKSUM,
1601 "Wrong IP6 errors constants");
1602
1603 error = !good_l4_csum ? IP6_ERROR_UDP_CHECKSUM + type : error;
1604
1605 /* Drop packets from unroutable hosts. */
1606 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1607 u8 unroutable = error == IP6_ERROR_UNKNOWN_PROTOCOL
1608 && type != IP_BUILTIN_PROTOCOL_ICMP
1609 && !ip6_address_is_link_local_unicast (&ip->src_address);
1610 if (PREDICT_FALSE (unroutable))
1611 {
1612 error =
1613 !ip6_urpf_loose_check (im, b[0],
1614 ip) ? IP6_ERROR_SRC_LOOKUP_MISS :
1615 error;
1616 }
1617
1618 vnet_buffer (b[0])->ip.fib_index =
1619 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1620 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1621 vnet_buffer (b[0])->ip.fib_index;
Alexander Chernavin65e770d2022-04-11 13:02:11 +00001622
1623 vnet_buffer (b[0])->ip.rx_sw_if_index =
1624 vnet_buffer (b[0])->sw_if_index[VLIB_RX];
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001625 if (is_receive_dpo)
1626 {
1627 receive_dpo_t *rd;
1628 rd = receive_dpo_get (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
Alexander Chernavin65e770d2022-04-11 13:02:11 +00001629 if (rd->rd_sw_if_index != ~0)
1630 vnet_buffer (b[0])->ip.rx_sw_if_index = rd->rd_sw_if_index;
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001631 }
Benoît Ganne26a10192019-02-14 15:32:45 +01001632 } /* head_of_feature_arc */
1633
1634 next[0] = lm->local_next_by_ip_protocol[ip->protocol];
1635 next[0] =
1636 error != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1637
Benoît Gannec15539a2021-01-19 16:40:07 +01001638 b[0]->error = error_node->errors[error];
Benoît Ganne26a10192019-02-14 15:32:45 +01001639
1640 if (head_of_feature_arc)
1641 {
1642 if (PREDICT_TRUE (error == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1643 {
1644 u32 next32 = next[0];
1645 vnet_feature_arc_start (arc_index,
Florin Corasf8408802021-11-09 18:29:03 -08001646 vnet_buffer (b[0])->ip.rx_sw_if_index,
1647 &next32, b[0]);
Benoît Ganne26a10192019-02-14 15:32:45 +01001648 next[0] = next32;
1649 }
1650 }
1651
1652 /* next */
1653 b += 1;
1654 next += 1;
1655 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001656 }
1657
Benoît Ganne26a10192019-02-14 15:32:45 +01001658 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001659 return frame->n_vectors;
1660}
1661
Damjan Marion38173502019-02-13 19:30:09 +01001662VLIB_NODE_FN (ip6_local_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1663 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001664{
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001665 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */,
1666 0 /* ip6_local_inline */);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001667}
1668
Damjan Marion38173502019-02-13 19:30:09 +01001669VLIB_REGISTER_NODE (ip6_local_node) =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001670{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001671 .name = "ip6-local",
1672 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001673 .format_trace = format_ip6_forward_next_trace,
Neale Rannse22a7042022-08-09 03:03:29 +00001674 .n_errors = IP6_N_ERROR,
1675 .error_counters = ip6_error_counters,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001676 .n_next_nodes = IP_LOCAL_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001677 .next_nodes =
1678 {
Neale Rannsd91c1db2017-07-31 02:30:50 -07001679 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1680 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001681 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1682 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
Klement Sekera01c1fa42021-12-14 18:25:11 +00001683 [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-local-full-reassembly",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001684 },
1685};
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001686
1687VLIB_NODE_FN (ip6_receive_local_node)
1688(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1689{
1690 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */,
1691 1 /* is_receive_dpo */);
1692}
1693
1694VLIB_REGISTER_NODE (ip6_receive_local_node) = {
1695 .name = "ip6-receive",
1696 .vector_size = sizeof (u32),
1697 .format_trace = format_ip6_forward_next_trace,
1698 .sibling_of = "ip6-local"
1699};
Ed Warnickecb9cada2015-12-08 15:45:58 -07001700
Damjan Marion38173502019-02-13 19:30:09 +01001701VLIB_NODE_FN (ip6_local_end_of_arc_node) (vlib_main_t * vm,
1702 vlib_node_runtime_t * node,
1703 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001704{
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001705 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */,
1706 0 /* ip6_local_inline */);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001707}
1708
Damjan Marion38173502019-02-13 19:30:09 +01001709VLIB_REGISTER_NODE (ip6_local_end_of_arc_node) = {
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001710 .name = "ip6-local-end-of-arc",
1711 .vector_size = sizeof (u32),
1712
1713 .format_trace = format_ip6_forward_next_trace,
1714 .sibling_of = "ip6-local",
1715};
1716
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001717VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1718 .arc_name = "ip6-local",
1719 .node_name = "ip6-local-end-of-arc",
1720 .runs_before = 0, /* not before any other features */
1721};
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001722
Damjan Marion38173502019-02-13 19:30:09 +01001723#ifdef CLIB_MARCH_VARIANT
1724extern vlib_node_registration_t ip6_local_node;
Damjan Marion38173502019-02-13 19:30:09 +01001725#else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001726void
1727ip6_register_protocol (u32 protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001728{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001729 vlib_main_t *vm = vlib_get_main ();
1730 ip6_main_t *im = &ip6_main;
1731 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001732
1733 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001734 lm->local_next_by_ip_protocol[protocol] =
1735 vlib_node_add_next (vm, ip6_local_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001736}
1737
Neale Rannsb538dd82019-05-21 06:54:54 -07001738void
1739ip6_unregister_protocol (u32 protocol)
1740{
1741 ip6_main_t *im = &ip6_main;
1742 ip_lookup_main_t *lm = &im->lookup_main;
1743
1744 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1745 lm->local_next_by_ip_protocol[protocol] = IP_LOCAL_NEXT_PUNT;
1746}
Damjan Marion38173502019-02-13 19:30:09 +01001747#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001748
Dave Barachd7cb1b52016-12-09 09:52:16 -05001749typedef enum
1750{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001751 IP6_REWRITE_NEXT_DROP,
Chris Luke816f3e12016-06-14 16:24:47 -04001752 IP6_REWRITE_NEXT_ICMP_ERROR,
Ole Troan313f7e22018-04-10 16:02:51 +02001753 IP6_REWRITE_NEXT_FRAGMENT,
1754 IP6_REWRITE_N_NEXT /* Last */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001755} ip6_rewrite_next_t;
1756
Neale Ranns889fe942017-06-01 05:43:19 -04001757/**
1758 * This bits of an IPv6 address to mask to construct a multicast
1759 * MAC address
1760 */
1761#define IP6_MCAST_ADDR_MASK 0xffffffff
1762
Ole Troanda6e11b2018-05-23 11:21:42 +02001763always_inline void
1764ip6_mtu_check (vlib_buffer_t * b, u16 packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001765 u16 adj_packet_bytes, bool is_locally_generated,
Ole Troaneb284a12019-10-09 13:33:19 +02001766 u32 * next, u8 is_midchain, u32 * error)
Ole Troanda6e11b2018-05-23 11:21:42 +02001767{
1768 if (adj_packet_bytes >= 1280 && packet_bytes > adj_packet_bytes)
1769 {
Ole Troan313f7e22018-04-10 16:02:51 +02001770 if (is_locally_generated)
1771 {
1772 /* IP fragmentation */
Ole Troan282093f2018-09-19 12:38:51 +02001773 ip_frag_set_vnet_buffer (b, adj_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02001774 (is_midchain ?
1775 IP_FRAG_NEXT_IP_REWRITE_MIDCHAIN :
1776 IP_FRAG_NEXT_IP_REWRITE), 0);
Ole Troan313f7e22018-04-10 16:02:51 +02001777 *next = IP6_REWRITE_NEXT_FRAGMENT;
Ole Troan282093f2018-09-19 12:38:51 +02001778 *error = IP6_ERROR_MTU_EXCEEDED;
Ole Troan313f7e22018-04-10 16:02:51 +02001779 }
1780 else
1781 {
1782 *error = IP6_ERROR_MTU_EXCEEDED;
1783 icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1784 adj_packet_bytes);
1785 *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1786 }
Ole Troanda6e11b2018-05-23 11:21:42 +02001787 }
1788}
1789
Ed Warnickecb9cada2015-12-08 15:45:58 -07001790always_inline uword
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001791ip6_rewrite_inline_with_gso (vlib_main_t * vm,
1792 vlib_node_runtime_t * node,
1793 vlib_frame_t * frame,
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01001794 int do_counters, int is_midchain, int is_mcast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001795{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001796 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1797 u32 *from = vlib_frame_vector_args (frame);
1798 u32 n_left_from, n_left_to_next, *to_next, next_index;
1799 vlib_node_runtime_t *error_node =
1800 vlib_node_get_runtime (vm, ip6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001801
1802 n_left_from = frame->n_vectors;
1803 next_index = node->cached_next_index;
Damjan Marion067cd622018-07-11 12:47:43 +02001804 u32 thread_index = vm->thread_index;
Dave Barach75fc8542016-10-11 16:16:02 -04001805
Ed Warnickecb9cada2015-12-08 15:45:58 -07001806 while (n_left_from > 0)
1807 {
1808 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1809
1810 while (n_left_from >= 4 && n_left_to_next >= 2)
1811 {
Neale Ranns960eeea2019-12-02 23:28:50 +00001812 const ip_adjacency_t *adj0, *adj1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001813 vlib_buffer_t *p0, *p1;
1814 ip6_header_t *ip0, *ip1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001815 u32 pi0, rw_len0, next0, error0, adj_index0;
1816 u32 pi1, rw_len1, next1, error1, adj_index1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001817 u32 tx_sw_if_index0, tx_sw_if_index1;
Ole Troan313f7e22018-04-10 16:02:51 +02001818 bool is_locally_originated0, is_locally_originated1;
Dave Barach75fc8542016-10-11 16:16:02 -04001819
Ed Warnickecb9cada2015-12-08 15:45:58 -07001820 /* Prefetch next iteration. */
1821 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001822 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001823
1824 p2 = vlib_get_buffer (vm, from[2]);
1825 p3 = vlib_get_buffer (vm, from[3]);
1826
1827 vlib_prefetch_buffer_header (p2, LOAD);
1828 vlib_prefetch_buffer_header (p3, LOAD);
1829
Damjan Marionaf7fb042021-07-15 11:54:41 +02001830 clib_prefetch_store (p2->pre_data);
1831 clib_prefetch_store (p3->pre_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001832
1833 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1834 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1835 }
1836
1837 pi0 = to_next[0] = from[0];
1838 pi1 = to_next[1] = from[1];
1839
1840 from += 2;
1841 n_left_from -= 2;
1842 to_next += 2;
1843 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04001844
Ed Warnickecb9cada2015-12-08 15:45:58 -07001845 p0 = vlib_get_buffer (vm, pi0);
1846 p1 = vlib_get_buffer (vm, pi1);
1847
Neale Rannsf06aea52016-11-29 06:51:37 -08001848 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1849 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001850
Ed Warnickecb9cada2015-12-08 15:45:58 -07001851 ip0 = vlib_buffer_get_current (p0);
1852 ip1 = vlib_buffer_get_current (p1);
1853
1854 error0 = error1 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001855 next0 = next1 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001856
Ole Troan313f7e22018-04-10 16:02:51 +02001857 is_locally_originated0 =
1858 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1859 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001860 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001861 i32 hop_limit0 = ip0->hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001862
1863 /* Input node should have reject packets with hop limit 0. */
1864 ASSERT (ip0->hop_limit > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001865
1866 hop_limit0 -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001867
1868 ip0->hop_limit = hop_limit0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001869
Dave Barachd7cb1b52016-12-09 09:52:16 -05001870 /*
1871 * If the hop count drops below 1 when forwarding, generate
1872 * an ICMP response.
1873 */
1874 if (PREDICT_FALSE (hop_limit0 <= 0))
1875 {
1876 error0 = IP6_ERROR_TIME_EXPIRED;
1877 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1878 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1879 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1880 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1881 0);
1882 }
Neale Rannsf06aea52016-11-29 06:51:37 -08001883 }
Neale Ranns88cecfa2020-04-08 08:28:06 -04001884
Ole Troan313f7e22018-04-10 16:02:51 +02001885 is_locally_originated1 =
1886 p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1887 if (PREDICT_TRUE (!is_locally_originated1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001888 {
Neale Rannsf06aea52016-11-29 06:51:37 -08001889 i32 hop_limit1 = ip1->hop_limit;
1890
1891 /* Input node should have reject packets with hop limit 0. */
1892 ASSERT (ip1->hop_limit > 0);
1893
1894 hop_limit1 -= 1;
1895
1896 ip1->hop_limit = hop_limit1;
1897
Dave Barachd7cb1b52016-12-09 09:52:16 -05001898 /*
1899 * If the hop count drops below 1 when forwarding, generate
1900 * an ICMP response.
1901 */
1902 if (PREDICT_FALSE (hop_limit1 <= 0))
1903 {
1904 error1 = IP6_ERROR_TIME_EXPIRED;
1905 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1906 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1907 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1908 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1909 0);
1910 }
1911 }
Neale Ranns88cecfa2020-04-08 08:28:06 -04001912
Neale Ranns107e7d42017-04-11 09:55:19 -07001913 adj0 = adj_get (adj_index0);
1914 adj1 = adj_get (adj_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001915
Ed Warnickecb9cada2015-12-08 15:45:58 -07001916 rw_len0 = adj0[0].rewrite_header.data_bytes;
1917 rw_len1 = adj1[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001918 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1919 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920
Neale Ranns9c6a6132017-02-21 05:33:14 -08001921 if (do_counters)
1922 {
1923 vlib_increment_combined_counter
1924 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001925 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001926 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1927 vlib_increment_combined_counter
1928 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001929 thread_index, adj_index1, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001930 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1931 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001932
1933 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001934 u16 ip0_len =
1935 clib_net_to_host_u16 (ip0->payload_length) +
1936 sizeof (ip6_header_t);
1937 u16 ip1_len =
1938 clib_net_to_host_u16 (ip1->payload_length) +
1939 sizeof (ip6_header_t);
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01001940 if (p0->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001941 ip0_len = gso_mtu_sz (p0);
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01001942 if (p1->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001943 ip1_len = gso_mtu_sz (p1);
1944
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001945 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001946 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02001947 is_locally_originated0, &next0, is_midchain,
1948 &error0);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001949 ip6_mtu_check (p1, ip1_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001950 adj1[0].rewrite_header.max_l3_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02001951 is_locally_originated1, &next1, is_midchain,
1952 &error1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001953 /* Don't adjust the buffer for hop count issue; icmp-error node
Jim Thompsonf324dec2019-04-08 03:22:21 -05001954 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001955 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1956 {
1957 p0->current_data -= rw_len0;
1958 p0->current_length += rw_len0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001959 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1960 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1961 next0 = adj0[0].rewrite_header.next_index;
Neale Rannsb069a692017-03-15 12:34:25 -04001962 if (PREDICT_FALSE
1963 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
Neale Ranns4ec36c52020-03-31 09:21:29 -04001964 vnet_feature_arc_start_w_cfg_index
1965 (lm->output_feature_arc_index, tx_sw_if_index0, &next0, p0,
1966 adj0->ia_cfg_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001967 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001968 else
1969 {
1970 p0->error = error_node->errors[error0];
1971 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001972 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1973 {
1974 p1->current_data -= rw_len1;
1975 p1->current_length += rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001976
Dave Barachd7cb1b52016-12-09 09:52:16 -05001977 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1978 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1979 next1 = adj1[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001980
Neale Rannsb069a692017-03-15 12:34:25 -04001981 if (PREDICT_FALSE
1982 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
Neale Ranns4ec36c52020-03-31 09:21:29 -04001983 vnet_feature_arc_start_w_cfg_index
1984 (lm->output_feature_arc_index, tx_sw_if_index1, &next1, p1,
1985 adj1->ia_cfg_index);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001986 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001987 else
1988 {
1989 p1->error = error_node->errors[error1];
1990 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001991
Neale Ranns25edf142019-03-22 08:12:48 +00001992 if (is_midchain)
1993 {
1994 /* before we paint on the next header, update the L4
1995 * checksums if required, since there's no offload on a tunnel */
Dave Barach1bd2c012020-04-12 08:31:39 -04001996 vnet_calc_checksums_inline (vm, p0, 0 /* is_ip4 */ ,
Vladimir Isaev698eb872020-05-21 16:34:17 +03001997 1 /* is_ip6 */ );
Dave Barach1bd2c012020-04-12 08:31:39 -04001998 vnet_calc_checksums_inline (vm, p1, 0 /* is_ip4 */ ,
Vladimir Isaev698eb872020-05-21 16:34:17 +03001999 1 /* is_ip6 */ );
Neale Ranns25edf142019-03-22 08:12:48 +00002000
Neale Ranns4ec36c52020-03-31 09:21:29 -04002001 /* Guess we are only writing on ipv6 header. */
2002 vnet_rewrite_two_headers (adj0[0], adj1[0],
2003 ip0, ip1, sizeof (ip6_header_t));
2004 }
2005 else
2006 /* Guess we are only writing on simple Ethernet header. */
2007 vnet_rewrite_two_headers (adj0[0], adj1[0],
2008 ip0, ip1, sizeof (ethernet_header_t));
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002009
2010 if (is_midchain)
2011 {
Neale Ranns25edf142019-03-22 08:12:48 +00002012 if (adj0->sub_type.midchain.fixup_func)
2013 adj0->sub_type.midchain.fixup_func
2014 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2015 if (adj1->sub_type.midchain.fixup_func)
2016 adj1->sub_type.midchain.fixup_func
2017 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002018 }
2019 if (is_mcast)
2020 {
2021 /*
2022 * copy bytes from the IP address into the MAC rewrite
2023 */
Neale Ranns889fe942017-06-01 05:43:19 -04002024 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2025 adj0->
2026 rewrite_header.dst_mcast_offset,
2027 &ip0->dst_address.as_u32[3],
2028 (u8 *) ip0);
2029 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2030 adj1->
2031 rewrite_header.dst_mcast_offset,
2032 &ip1->dst_address.as_u32[3],
2033 (u8 *) ip1);
Neale Ranns32e1c012016-11-22 17:07:28 +00002034 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002035
Ed Warnickecb9cada2015-12-08 15:45:58 -07002036 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2037 to_next, n_left_to_next,
2038 pi0, pi1, next0, next1);
2039 }
2040
2041 while (n_left_from > 0 && n_left_to_next > 0)
2042 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002043 ip_adjacency_t *adj0;
2044 vlib_buffer_t *p0;
2045 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002046 u32 pi0, rw_len0;
2047 u32 adj_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002048 u32 tx_sw_if_index0;
Ole Troan313f7e22018-04-10 16:02:51 +02002049 bool is_locally_originated0;
Dave Barach75fc8542016-10-11 16:16:02 -04002050
Ed Warnickecb9cada2015-12-08 15:45:58 -07002051 pi0 = to_next[0] = from[0];
2052
2053 p0 = vlib_get_buffer (vm, pi0);
2054
Neale Rannsf06aea52016-11-29 06:51:37 -08002055 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002056
Neale Ranns107e7d42017-04-11 09:55:19 -07002057 adj0 = adj_get (adj_index0);
Dave Barach75fc8542016-10-11 16:16:02 -04002058
Ed Warnickecb9cada2015-12-08 15:45:58 -07002059 ip0 = vlib_buffer_get_current (p0);
2060
2061 error0 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002062 next0 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002063
2064 /* Check hop limit */
Ole Troan313f7e22018-04-10 16:02:51 +02002065 is_locally_originated0 =
2066 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
2067 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002068 {
2069 i32 hop_limit0 = ip0->hop_limit;
2070
2071 ASSERT (ip0->hop_limit > 0);
2072
2073 hop_limit0 -= 1;
2074
2075 ip0->hop_limit = hop_limit0;
2076
Dave Barachd7cb1b52016-12-09 09:52:16 -05002077 if (PREDICT_FALSE (hop_limit0 <= 0))
2078 {
2079 /*
2080 * If the hop count drops below 1 when forwarding, generate
2081 * an ICMP response.
2082 */
2083 error0 = IP6_ERROR_TIME_EXPIRED;
2084 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2085 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2086 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2087 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2088 0);
2089 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002090 }
2091
Neale Ranns25edf142019-03-22 08:12:48 +00002092 if (is_midchain)
2093 {
Dave Barach1bd2c012020-04-12 08:31:39 -04002094 vnet_calc_checksums_inline (vm, p0, 0 /* is_ip4 */ ,
Vladimir Isaev698eb872020-05-21 16:34:17 +03002095 1 /* is_ip6 */ );
Neale Ranns25edf142019-03-22 08:12:48 +00002096
Neale Ranns4ec36c52020-03-31 09:21:29 -04002097 /* Guess we are only writing on ip6 header. */
2098 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ip6_header_t));
2099 }
2100 else
2101 /* Guess we are only writing on simple Ethernet header. */
2102 vnet_rewrite_one_header (adj0[0], ip0,
2103 sizeof (ethernet_header_t));
Dave Barach75fc8542016-10-11 16:16:02 -04002104
Ed Warnickecb9cada2015-12-08 15:45:58 -07002105 /* Update packet buffer attributes/set output interface. */
2106 rw_len0 = adj0[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002107 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002108
Neale Ranns9c6a6132017-02-21 05:33:14 -08002109 if (do_counters)
2110 {
2111 vlib_increment_combined_counter
2112 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02002113 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08002114 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2115 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002116
2117 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002118 u16 ip0_len =
2119 clib_net_to_host_u16 (ip0->payload_length) +
2120 sizeof (ip6_header_t);
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01002121 if (p0->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002122 ip0_len = gso_mtu_sz (p0);
2123
2124 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02002125 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02002126 is_locally_originated0, &next0, is_midchain,
2127 &error0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002128 /* Don't adjust the buffer for hop count issue; icmp-error node
Ole Troanda6e11b2018-05-23 11:21:42 +02002129 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002130 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2131 {
Chris Luke816f3e12016-06-14 16:24:47 -04002132 p0->current_data -= rw_len0;
2133 p0->current_length += rw_len0;
2134
Dave Barachd7cb1b52016-12-09 09:52:16 -05002135 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
Dave Barach5331c722016-08-17 11:54:30 -04002136
Dave Barachd7cb1b52016-12-09 09:52:16 -05002137 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2138 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04002139
Neale Rannsb069a692017-03-15 12:34:25 -04002140 if (PREDICT_FALSE
2141 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
Neale Ranns4ec36c52020-03-31 09:21:29 -04002142 vnet_feature_arc_start_w_cfg_index
2143 (lm->output_feature_arc_index, tx_sw_if_index0, &next0, p0,
2144 adj0->ia_cfg_index);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002145 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04002146 else
2147 {
2148 p0->error = error_node->errors[error0];
2149 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002150
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002151 if (is_midchain)
2152 {
Neale Ranns25edf142019-03-22 08:12:48 +00002153 if (adj0->sub_type.midchain.fixup_func)
2154 adj0->sub_type.midchain.fixup_func
2155 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002156 }
2157 if (is_mcast)
2158 {
Neale Ranns889fe942017-06-01 05:43:19 -04002159 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2160 adj0->
2161 rewrite_header.dst_mcast_offset,
2162 &ip0->dst_address.as_u32[3],
2163 (u8 *) ip0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002164 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002165
Ed Warnickecb9cada2015-12-08 15:45:58 -07002166 from += 1;
2167 n_left_from -= 1;
2168 to_next += 1;
2169 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04002170
Ed Warnickecb9cada2015-12-08 15:45:58 -07002171 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2172 to_next, n_left_to_next,
2173 pi0, next0);
2174 }
2175
2176 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2177 }
2178
2179 /* Need to do trace after rewrites to pick up new packet data. */
2180 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Rannsf06aea52016-11-29 06:51:37 -08002181 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002182
2183 return frame->n_vectors;
2184}
2185
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002186always_inline uword
2187ip6_rewrite_inline (vlib_main_t * vm,
2188 vlib_node_runtime_t * node,
2189 vlib_frame_t * frame,
2190 int do_counters, int is_midchain, int is_mcast)
2191{
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01002192 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2193 is_midchain, is_mcast);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002194}
2195
Damjan Marion38173502019-02-13 19:30:09 +01002196VLIB_NODE_FN (ip6_rewrite_node) (vlib_main_t * vm,
2197 vlib_node_runtime_t * node,
2198 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002199{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002200 if (adj_are_counters_enabled ())
2201 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2202 else
2203 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002204}
2205
Damjan Marion38173502019-02-13 19:30:09 +01002206VLIB_NODE_FN (ip6_rewrite_bcast_node) (vlib_main_t * vm,
2207 vlib_node_runtime_t * node,
2208 vlib_frame_t * frame)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002209{
2210 if (adj_are_counters_enabled ())
2211 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2212 else
2213 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2214}
2215
Damjan Marion38173502019-02-13 19:30:09 +01002216VLIB_NODE_FN (ip6_rewrite_mcast_node) (vlib_main_t * vm,
2217 vlib_node_runtime_t * node,
2218 vlib_frame_t * frame)
Neale Ranns32e1c012016-11-22 17:07:28 +00002219{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002220 if (adj_are_counters_enabled ())
2221 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2222 else
2223 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002224}
2225
Damjan Marion38173502019-02-13 19:30:09 +01002226VLIB_NODE_FN (ip6_midchain_node) (vlib_main_t * vm,
2227 vlib_node_runtime_t * node,
2228 vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002229{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002230 if (adj_are_counters_enabled ())
2231 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2232 else
2233 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002234}
2235
Damjan Marion38173502019-02-13 19:30:09 +01002236VLIB_NODE_FN (ip6_mcast_midchain_node) (vlib_main_t * vm,
2237 vlib_node_runtime_t * node,
2238 vlib_frame_t * frame)
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002239{
2240 if (adj_are_counters_enabled ())
2241 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2242 else
Neale Ranns9f171f52017-04-11 08:56:53 -07002243 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002244}
2245
Dave Barachd7cb1b52016-12-09 09:52:16 -05002246/* *INDENT-OFF* */
Neale Rannse22a7042022-08-09 03:03:29 +00002247VLIB_REGISTER_NODE (ip6_midchain_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002248 .name = "ip6-midchain",
2249 .vector_size = sizeof (u32),
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002250 .format_trace = format_ip6_forward_next_trace,
Neale Ranns5e575b12016-10-03 09:40:25 +01002251 .sibling_of = "ip6-rewrite",
Neale Rannse22a7042022-08-09 03:03:29 +00002252};
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002253
Dave Barachd7cb1b52016-12-09 09:52:16 -05002254VLIB_REGISTER_NODE (ip6_rewrite_node) =
2255{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002256 .name = "ip6-rewrite",
2257 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +01002258 .format_trace = format_ip6_rewrite_trace,
Neale Rannse22a7042022-08-09 03:03:29 +00002259 .n_errors = IP6_N_ERROR,
2260 .error_counters = ip6_error_counters,
Ole Troan313f7e22018-04-10 16:02:51 +02002261 .n_next_nodes = IP6_REWRITE_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002262 .next_nodes =
2263 {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002264 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
Chris Luke816f3e12016-06-14 16:24:47 -04002265 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Ole Troan313f7e22018-04-10 16:02:51 +02002266 [IP6_REWRITE_NEXT_FRAGMENT] = "ip6-frag",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002267 },
2268};
2269
Neale Ranns1855b8e2018-07-11 10:31:26 -07002270VLIB_REGISTER_NODE (ip6_rewrite_bcast_node) = {
Neale Ranns1855b8e2018-07-11 10:31:26 -07002271 .name = "ip6-rewrite-bcast",
2272 .vector_size = sizeof (u32),
2273
2274 .format_trace = format_ip6_rewrite_trace,
2275 .sibling_of = "ip6-rewrite",
2276};
Neale Ranns1855b8e2018-07-11 10:31:26 -07002277
Neale Ranns32e1c012016-11-22 17:07:28 +00002278VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2279{
Neale Ranns32e1c012016-11-22 17:07:28 +00002280 .name = "ip6-rewrite-mcast",
2281 .vector_size = sizeof (u32),
2282 .format_trace = format_ip6_rewrite_trace,
2283 .sibling_of = "ip6-rewrite",
2284};
Neale Ranns32e1c012016-11-22 17:07:28 +00002285
Neale Ranns32e1c012016-11-22 17:07:28 +00002286
Damjan Marion38173502019-02-13 19:30:09 +01002287VLIB_REGISTER_NODE (ip6_mcast_midchain_node) =
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002288{
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002289 .name = "ip6-mcast-midchain",
2290 .vector_size = sizeof (u32),
2291 .format_trace = format_ip6_rewrite_trace,
2292 .sibling_of = "ip6-rewrite",
2293};
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002294
Neale Ranns1855b8e2018-07-11 10:31:26 -07002295/* *INDENT-ON* */
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002296
Ole Troan944f5482016-05-24 11:56:58 +02002297/*
2298 * Hop-by-Hop handling
2299 */
Benoît Ganne47727c02019-02-12 13:35:08 +01002300#ifndef CLIB_MARCH_VARIANT
Ole Troan944f5482016-05-24 11:56:58 +02002301ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
Benoît Ganne47727c02019-02-12 13:35:08 +01002302#endif /* CLIB_MARCH_VARIANT */
Ole Troan944f5482016-05-24 11:56:58 +02002303
2304#define foreach_ip6_hop_by_hop_error \
2305_(PROCESSED, "pkts with ip6 hop-by-hop options") \
2306_(FORMAT, "incorrectly formatted hop-by-hop options") \
2307_(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2308
Neale Ranns32e1c012016-11-22 17:07:28 +00002309/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002310typedef enum
2311{
Ole Troan944f5482016-05-24 11:56:58 +02002312#define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2313 foreach_ip6_hop_by_hop_error
2314#undef _
Neale Ranns32e1c012016-11-22 17:07:28 +00002315 IP6_HOP_BY_HOP_N_ERROR,
Ole Troan944f5482016-05-24 11:56:58 +02002316} ip6_hop_by_hop_error_t;
Neale Ranns32e1c012016-11-22 17:07:28 +00002317/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002318
2319/*
2320 * Primary h-b-h handler trace support
2321 * We work pretty hard on the problem for obvious reasons
2322 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002323typedef struct
2324{
Ole Troan944f5482016-05-24 11:56:58 +02002325 u32 next_index;
2326 u32 trace_len;
2327 u8 option_data[256];
2328} ip6_hop_by_hop_trace_t;
2329
Benoît Ganne47727c02019-02-12 13:35:08 +01002330extern vlib_node_registration_t ip6_hop_by_hop_node;
Ole Troan944f5482016-05-24 11:56:58 +02002331
Dave Barachd7cb1b52016-12-09 09:52:16 -05002332static char *ip6_hop_by_hop_error_strings[] = {
Ole Troan944f5482016-05-24 11:56:58 +02002333#define _(sym,string) string,
2334 foreach_ip6_hop_by_hop_error
2335#undef _
2336};
2337
Damjan Marion38173502019-02-13 19:30:09 +01002338#ifndef CLIB_MARCH_VARIANT
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302339u8 *
2340format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2341{
2342 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2343 int total_len = va_arg (*args, int);
2344 ip6_hop_by_hop_option_t *opt0, *limit0;
2345 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2346 u8 type0;
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302347 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2348 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2349
2350 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2351 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2352
2353 while (opt0 < limit0)
2354 {
2355 type0 = opt0->type;
2356 switch (type0)
2357 {
2358 case 0: /* Pad, just stop */
2359 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2360 break;
2361
2362 default:
2363 if (hm->trace[type0])
2364 {
2365 s = (*hm->trace[type0]) (s, opt0);
2366 }
2367 else
2368 {
Mohsin Kazmi68095382021-02-10 11:26:24 +01002369 s = format (s, "\n unrecognized option %d length %d", type0,
2370 opt0->length);
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302371 }
2372 opt0 =
2373 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2374 sizeof (ip6_hop_by_hop_option_t));
2375 break;
2376 }
2377 }
2378 return s;
2379}
Damjan Marion38173502019-02-13 19:30:09 +01002380#endif
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302381
Ole Troan944f5482016-05-24 11:56:58 +02002382static u8 *
2383format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2384{
2385 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2386 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002387 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
Ole Troan944f5482016-05-24 11:56:58 +02002388 ip6_hop_by_hop_header_t *hbh0;
2389 ip6_hop_by_hop_option_t *opt0, *limit0;
2390 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2391
2392 u8 type0;
2393
Dave Barachd7cb1b52016-12-09 09:52:16 -05002394 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
Ole Troan944f5482016-05-24 11:56:58 +02002395
2396 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002397 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
Ole Troan944f5482016-05-24 11:56:58 +02002398
Dave Barachd7cb1b52016-12-09 09:52:16 -05002399 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2400 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
Ole Troan944f5482016-05-24 11:56:58 +02002401
Dave Barachd7cb1b52016-12-09 09:52:16 -05002402 while (opt0 < limit0)
2403 {
2404 type0 = opt0->type;
2405 switch (type0)
2406 {
2407 case 0: /* Pad, just stop */
2408 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2409 break;
Ole Troan944f5482016-05-24 11:56:58 +02002410
Dave Barachd7cb1b52016-12-09 09:52:16 -05002411 default:
2412 if (hm->trace[type0])
2413 {
2414 s = (*hm->trace[type0]) (s, opt0);
2415 }
2416 else
2417 {
Mohsin Kazmi68095382021-02-10 11:26:24 +01002418 s = format (s, "\n unrecognized option %d length %d", type0,
2419 opt0->length);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002420 }
2421 opt0 =
2422 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2423 sizeof (ip6_hop_by_hop_option_t));
2424 break;
2425 }
Ole Troan944f5482016-05-24 11:56:58 +02002426 }
Ole Troan944f5482016-05-24 11:56:58 +02002427 return s;
2428}
2429
Dave Barachd7cb1b52016-12-09 09:52:16 -05002430always_inline u8
2431ip6_scan_hbh_options (vlib_buffer_t * b0,
2432 ip6_header_t * ip0,
2433 ip6_hop_by_hop_header_t * hbh0,
2434 ip6_hop_by_hop_option_t * opt0,
2435 ip6_hop_by_hop_option_t * limit0, u32 * next0)
Shwethaa91cbe62016-08-08 15:51:04 +01002436{
2437 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2438 u8 type0;
2439 u8 error0 = 0;
2440
2441 while (opt0 < limit0)
2442 {
2443 type0 = opt0->type;
2444 switch (type0)
2445 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002446 case 0: /* Pad1 */
2447 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
Shwethaa91cbe62016-08-08 15:51:04 +01002448 continue;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002449 case 1: /* PadN */
Shwethaa91cbe62016-08-08 15:51:04 +01002450 break;
2451 default:
2452 if (hm->options[type0])
2453 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002454 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2455 {
Shwethaa91cbe62016-08-08 15:51:04 +01002456 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002457 return (error0);
2458 }
Shwethaa91cbe62016-08-08 15:51:04 +01002459 }
2460 else
2461 {
2462 /* Unrecognized mandatory option, check the two high order bits */
2463 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2464 {
2465 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2466 break;
2467 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2468 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2469 *next0 = IP_LOOKUP_NEXT_DROP;
2470 break;
2471 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2472 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2473 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002474 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2475 ICMP6_parameter_problem_unrecognized_option,
2476 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002477 break;
2478 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2479 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002480 if (!ip6_address_is_multicast (&ip0->dst_address))
Shwethaa91cbe62016-08-08 15:51:04 +01002481 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002482 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2483 icmp6_error_set_vnet_buffer (b0,
2484 ICMP6_parameter_problem,
2485 ICMP6_parameter_problem_unrecognized_option,
2486 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002487 }
2488 else
2489 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002490 *next0 = IP_LOOKUP_NEXT_DROP;
Shwethaa91cbe62016-08-08 15:51:04 +01002491 }
2492 break;
2493 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002494 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002495 }
2496 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002497 opt0 =
2498 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2499 sizeof (ip6_hop_by_hop_option_t));
Shwethaa91cbe62016-08-08 15:51:04 +01002500 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002501 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002502}
2503
Ole Troan944f5482016-05-24 11:56:58 +02002504/*
2505 * Process the Hop-by-Hop Options header
2506 */
Damjan Marion38173502019-02-13 19:30:09 +01002507VLIB_NODE_FN (ip6_hop_by_hop_node) (vlib_main_t * vm,
2508 vlib_node_runtime_t * node,
2509 vlib_frame_t * frame)
Ole Troan944f5482016-05-24 11:56:58 +02002510{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002511 vlib_node_runtime_t *error_node =
2512 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
Ole Troan944f5482016-05-24 11:56:58 +02002513 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2514 u32 n_left_from, *from, *to_next;
2515 ip_lookup_next_t next_index;
Ole Troan944f5482016-05-24 11:56:58 +02002516
2517 from = vlib_frame_vector_args (frame);
2518 n_left_from = frame->n_vectors;
2519 next_index = node->cached_next_index;
2520
Dave Barachd7cb1b52016-12-09 09:52:16 -05002521 while (n_left_from > 0)
2522 {
2523 u32 n_left_to_next;
Ole Troan944f5482016-05-24 11:56:58 +02002524
Dave Barachd7cb1b52016-12-09 09:52:16 -05002525 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ole Troan944f5482016-05-24 11:56:58 +02002526
Dave Barachd7cb1b52016-12-09 09:52:16 -05002527 while (n_left_from >= 4 && n_left_to_next >= 2)
Shwethaa91cbe62016-08-08 15:51:04 +01002528 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002529 u32 bi0, bi1;
2530 vlib_buffer_t *b0, *b1;
2531 u32 next0, next1;
2532 ip6_header_t *ip0, *ip1;
2533 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2534 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2535 u8 error0 = 0, error1 = 0;
2536
2537 /* Prefetch next iteration. */
2538 {
2539 vlib_buffer_t *p2, *p3;
2540
2541 p2 = vlib_get_buffer (vm, from[2]);
2542 p3 = vlib_get_buffer (vm, from[3]);
2543
2544 vlib_prefetch_buffer_header (p2, LOAD);
2545 vlib_prefetch_buffer_header (p3, LOAD);
2546
2547 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2548 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Shwethaa91cbe62016-08-08 15:51:04 +01002549 }
2550
Dave Barachd7cb1b52016-12-09 09:52:16 -05002551 /* Speculatively enqueue b0, b1 to the current next frame */
2552 to_next[0] = bi0 = from[0];
2553 to_next[1] = bi1 = from[1];
2554 from += 2;
2555 to_next += 2;
2556 n_left_from -= 2;
2557 n_left_to_next -= 2;
2558
2559 b0 = vlib_get_buffer (vm, bi0);
2560 b1 = vlib_get_buffer (vm, bi1);
2561
2562 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2563 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002564 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002565 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002566 ip_adjacency_t *adj1 = adj_get (adj_index1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002567
2568 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2569 next0 = adj0->lookup_next_index;
2570 next1 = adj1->lookup_next_index;
2571
2572 ip0 = vlib_buffer_get_current (b0);
2573 ip1 = vlib_buffer_get_current (b1);
2574 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2575 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2576 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2577 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2578 limit0 =
2579 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2580 ((hbh0->length + 1) << 3));
2581 limit1 =
2582 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2583 ((hbh1->length + 1) << 3));
2584
2585 /*
2586 * Basic validity checks
2587 */
2588 if ((hbh0->length + 1) << 3 >
2589 clib_net_to_host_u16 (ip0->payload_length))
2590 {
2591 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2592 next0 = IP_LOOKUP_NEXT_DROP;
2593 goto outdual;
2594 }
2595 /* Scan the set of h-b-h options, process ones that we understand */
2596 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2597
2598 if ((hbh1->length + 1) << 3 >
2599 clib_net_to_host_u16 (ip1->payload_length))
2600 {
2601 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2602 next1 = IP_LOOKUP_NEXT_DROP;
2603 goto outdual;
2604 }
2605 /* Scan the set of h-b-h options, process ones that we understand */
2606 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2607
2608 outdual:
2609 /* Has the classifier flagged this buffer for special treatment? */
2610 if (PREDICT_FALSE
2611 ((error0 == 0)
2612 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2613 next0 = hm->next_override;
2614
2615 /* Has the classifier flagged this buffer for special treatment? */
2616 if (PREDICT_FALSE
2617 ((error1 == 0)
2618 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2619 next1 = hm->next_override;
2620
2621 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2622 {
2623 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2624 {
2625 ip6_hop_by_hop_trace_t *t =
2626 vlib_add_trace (vm, node, b0, sizeof (*t));
2627 u32 trace_len = (hbh0->length + 1) << 3;
2628 t->next_index = next0;
2629 /* Capture the h-b-h option verbatim */
2630 trace_len =
2631 trace_len <
2632 ARRAY_LEN (t->option_data) ? trace_len :
2633 ARRAY_LEN (t->option_data);
2634 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002635 clib_memcpy_fast (t->option_data, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002636 }
2637 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2638 {
2639 ip6_hop_by_hop_trace_t *t =
2640 vlib_add_trace (vm, node, b1, sizeof (*t));
2641 u32 trace_len = (hbh1->length + 1) << 3;
2642 t->next_index = next1;
2643 /* Capture the h-b-h option verbatim */
2644 trace_len =
2645 trace_len <
2646 ARRAY_LEN (t->option_data) ? trace_len :
2647 ARRAY_LEN (t->option_data);
2648 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002649 clib_memcpy_fast (t->option_data, hbh1, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002650 }
2651
2652 }
2653
2654 b0->error = error_node->errors[error0];
2655 b1->error = error_node->errors[error1];
2656
2657 /* verify speculative enqueue, maybe switch current next frame */
2658 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2659 n_left_to_next, bi0, bi1, next0,
2660 next1);
Shwethaa91cbe62016-08-08 15:51:04 +01002661 }
2662
Dave Barachd7cb1b52016-12-09 09:52:16 -05002663 while (n_left_from > 0 && n_left_to_next > 0)
2664 {
2665 u32 bi0;
2666 vlib_buffer_t *b0;
2667 u32 next0;
2668 ip6_header_t *ip0;
2669 ip6_hop_by_hop_header_t *hbh0;
2670 ip6_hop_by_hop_option_t *opt0, *limit0;
2671 u8 error0 = 0;
Shwethaa91cbe62016-08-08 15:51:04 +01002672
Dave Barachd7cb1b52016-12-09 09:52:16 -05002673 /* Speculatively enqueue b0 to the current next frame */
2674 bi0 = from[0];
2675 to_next[0] = bi0;
2676 from += 1;
2677 to_next += 1;
2678 n_left_from -= 1;
2679 n_left_to_next -= 1;
2680
2681 b0 = vlib_get_buffer (vm, bi0);
2682 /*
2683 * Default use the next_index from the adjacency.
2684 * A HBH option rarely redirects to a different node
2685 */
2686 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002687 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002688 next0 = adj0->lookup_next_index;
2689
2690 ip0 = vlib_buffer_get_current (b0);
2691 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2692 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2693 limit0 =
2694 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2695 ((hbh0->length + 1) << 3));
2696
2697 /*
2698 * Basic validity checks
2699 */
2700 if ((hbh0->length + 1) << 3 >
2701 clib_net_to_host_u16 (ip0->payload_length))
2702 {
2703 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2704 next0 = IP_LOOKUP_NEXT_DROP;
2705 goto out0;
2706 }
2707
2708 /* Scan the set of h-b-h options, process ones that we understand */
2709 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2710
2711 out0:
2712 /* Has the classifier flagged this buffer for special treatment? */
2713 if (PREDICT_FALSE
2714 ((error0 == 0)
2715 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2716 next0 = hm->next_override;
2717
2718 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2719 {
2720 ip6_hop_by_hop_trace_t *t =
2721 vlib_add_trace (vm, node, b0, sizeof (*t));
2722 u32 trace_len = (hbh0->length + 1) << 3;
2723 t->next_index = next0;
2724 /* Capture the h-b-h option verbatim */
2725 trace_len =
2726 trace_len <
2727 ARRAY_LEN (t->option_data) ? trace_len :
2728 ARRAY_LEN (t->option_data);
2729 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002730 clib_memcpy_fast (t->option_data, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002731 }
2732
2733 b0->error = error_node->errors[error0];
2734
2735 /* verify speculative enqueue, maybe switch current next frame */
2736 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2737 n_left_to_next, bi0, next0);
2738 }
2739 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Shwethaa91cbe62016-08-08 15:51:04 +01002740 }
Ole Troan944f5482016-05-24 11:56:58 +02002741 return frame->n_vectors;
2742}
2743
Dave Barachd7cb1b52016-12-09 09:52:16 -05002744/* *INDENT-OFF* */
2745VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2746{
Ole Troan944f5482016-05-24 11:56:58 +02002747 .name = "ip6-hop-by-hop",
Ole Troan964f93e2016-06-10 13:22:36 +02002748 .sibling_of = "ip6-lookup",
Ole Troan944f5482016-05-24 11:56:58 +02002749 .vector_size = sizeof (u32),
2750 .format_trace = format_ip6_hop_by_hop_trace,
2751 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002752 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
Ole Troan944f5482016-05-24 11:56:58 +02002753 .error_strings = ip6_hop_by_hop_error_strings,
Ole Troan964f93e2016-06-10 13:22:36 +02002754 .n_next_nodes = 0,
Ole Troan944f5482016-05-24 11:56:58 +02002755};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002756/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002757
Ole Troan944f5482016-05-24 11:56:58 +02002758static clib_error_t *
2759ip6_hop_by_hop_init (vlib_main_t * vm)
2760{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002761 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Dave Barachb7b92992018-10-17 10:38:51 -04002762 clib_memset (hm->options, 0, sizeof (hm->options));
2763 clib_memset (hm->trace, 0, sizeof (hm->trace));
Shwethaa91cbe62016-08-08 15:51:04 +01002764 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
Ole Troan944f5482016-05-24 11:56:58 +02002765 return (0);
2766}
2767
2768VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2769
Damjan Marion38173502019-02-13 19:30:09 +01002770#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05002771void
2772ip6_hbh_set_next_override (uword next)
Shwethaa91cbe62016-08-08 15:51:04 +01002773{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002774 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Shwethaa91cbe62016-08-08 15:51:04 +01002775
2776 hm->next_override = next;
2777}
2778
Ole Troan944f5482016-05-24 11:56:58 +02002779int
2780ip6_hbh_register_option (u8 option,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002781 int options (vlib_buffer_t * b, ip6_header_t * ip,
2782 ip6_hop_by_hop_option_t * opt),
2783 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
Ole Troan944f5482016-05-24 11:56:58 +02002784{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002785 ip6_main_t *im = &ip6_main;
2786 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002787
Neale Ranns756cd942018-04-06 09:18:11 -07002788 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002789
2790 /* Already registered */
2791 if (hm->options[option])
2792 return (-1);
2793
2794 hm->options[option] = options;
2795 hm->trace[option] = trace;
2796
2797 /* Set global variable */
2798 im->hbh_enabled = 1;
2799
2800 return (0);
2801}
2802
2803int
2804ip6_hbh_unregister_option (u8 option)
2805{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002806 ip6_main_t *im = &ip6_main;
2807 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002808
Neale Ranns756cd942018-04-06 09:18:11 -07002809 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002810
2811 /* Not registered */
2812 if (!hm->options[option])
2813 return (-1);
2814
2815 hm->options[option] = NULL;
2816 hm->trace[option] = NULL;
2817
2818 /* Disable global knob if this was the last option configured */
2819 int i;
2820 bool found = false;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002821 for (i = 0; i < 256; i++)
2822 {
2823 if (hm->options[option])
2824 {
2825 found = true;
2826 break;
2827 }
Ole Troan944f5482016-05-24 11:56:58 +02002828 }
Ole Troan944f5482016-05-24 11:56:58 +02002829 if (!found)
2830 im->hbh_enabled = 0;
2831
2832 return (0);
2833}
2834
Ed Warnickecb9cada2015-12-08 15:45:58 -07002835/* Global IP6 main. */
2836ip6_main_t ip6_main;
Damjan Marion38173502019-02-13 19:30:09 +01002837#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838
2839static clib_error_t *
2840ip6_lookup_init (vlib_main_t * vm)
2841{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002842 ip6_main_t *im = &ip6_main;
2843 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002844 uword i;
2845
Damjan Marion8b3191e2016-11-09 19:54:20 +01002846 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2847 return error;
2848
Ed Warnickecb9cada2015-12-08 15:45:58 -07002849 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2850 {
2851 u32 j, i0, i1;
2852
2853 i0 = i / 32;
2854 i1 = i % 32;
2855
2856 for (j = 0; j < i0; j++)
2857 im->fib_masks[i].as_u32[j] = ~0;
2858
2859 if (i1)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002860 im->fib_masks[i].as_u32[i0] =
2861 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002862 }
2863
2864 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2865
Ed Warnickecb9cada2015-12-08 15:45:58 -07002866 /* Create FIB with index 0 and table id of 0. */
Neale Ranns15002542017-09-10 04:39:11 -07002867 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2868 FIB_SOURCE_DEFAULT_ROUTE);
2869 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2870 MFIB_SOURCE_DEFAULT_ROUTE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002871
2872 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002873 pg_node_t *pn;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874 pn = pg_get_node (ip6_lookup_node.index);
2875 pn->unformat_edit = unformat_pg_ip6_header;
2876 }
2877
Ole Troan944f5482016-05-24 11:56:58 +02002878 /* Unless explicitly configured, don't process HBH options */
2879 im->hbh_enabled = 0;
2880
Dave Barach203c6322016-06-26 10:29:03 -04002881 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002882}
2883
2884VLIB_INIT_FUNCTION (ip6_lookup_init);
2885
Ed Warnickecb9cada2015-12-08 15:45:58 -07002886static clib_error_t *
2887set_ip6_flow_hash_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002888 unformat_input_t * input,
2889 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002890{
2891 int matched = 0;
2892 u32 table_id = 0;
2893 u32 flow_hash_config = 0;
2894 int rv;
2895
Dave Barachd7cb1b52016-12-09 09:52:16 -05002896 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2897 {
2898 if (unformat (input, "table %d", &table_id))
2899 matched = 1;
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00002900#define _(a, b, v) \
2901 else if (unformat (input, #a)) \
2902 { \
2903 flow_hash_config |= v; \
2904 matched = 1; \
2905 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002906 foreach_flow_hash_bit
Ed Warnickecb9cada2015-12-08 15:45:58 -07002907#undef _
Dave Barachd7cb1b52016-12-09 09:52:16 -05002908 else
2909 break;
2910 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002911
2912 if (matched == 0)
2913 return clib_error_return (0, "unknown input `%U'",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002914 format_unformat_error, input);
Dave Barach75fc8542016-10-11 16:16:02 -04002915
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00002916 rv = ip_flow_hash_set (AF_IP6, table_id, flow_hash_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917 switch (rv)
2918 {
Neale Ranns227038a2017-04-21 01:07:59 -07002919 case 0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002920 break;
2921
2922 case -1:
2923 return clib_error_return (0, "no such FIB table %d", table_id);
Dave Barach75fc8542016-10-11 16:16:02 -04002924
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925 default:
2926 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2927 break;
2928 }
Dave Barach75fc8542016-10-11 16:16:02 -04002929
Ed Warnickecb9cada2015-12-08 15:45:58 -07002930 return 0;
2931}
2932
Billy McFall0683c9c2016-10-13 08:27:31 -04002933/*?
2934 * Configure the set of IPv6 fields used by the flow hash.
2935 *
2936 * @cliexpar
2937 * @parblock
2938 * Example of how to set the flow hash on a given table:
Billy McFallebb9a6a2016-10-17 11:35:32 -04002939 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2940 *
Billy McFall0683c9c2016-10-13 08:27:31 -04002941 * Example of display the configured flow hash:
2942 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -04002943 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2944 * @::/0
2945 * unicast-ip6-chain
2946 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2947 * [0] [@0]: dpo-drop ip6
2948 * fe80::/10
2949 * unicast-ip6-chain
2950 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2951 * [0] [@2]: dpo-receive
2952 * ff02::1/128
2953 * unicast-ip6-chain
2954 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2955 * [0] [@2]: dpo-receive
2956 * ff02::2/128
2957 * unicast-ip6-chain
2958 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2959 * [0] [@2]: dpo-receive
2960 * ff02::16/128
2961 * unicast-ip6-chain
2962 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2963 * [0] [@2]: dpo-receive
2964 * ff02::1:ff00:0/104
2965 * unicast-ip6-chain
2966 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
2967 * [0] [@2]: dpo-receive
2968 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
2969 * @::/0
2970 * unicast-ip6-chain
2971 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
2972 * [0] [@0]: dpo-drop ip6
2973 * @::a:1:1:0:4/126
2974 * unicast-ip6-chain
2975 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
2976 * [0] [@4]: ipv6-glean: af_packet0
2977 * @::a:1:1:0:7/128
2978 * unicast-ip6-chain
2979 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
2980 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
2981 * fe80::/10
2982 * unicast-ip6-chain
2983 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
2984 * [0] [@2]: dpo-receive
2985 * fe80::fe:3eff:fe3e:9222/128
2986 * unicast-ip6-chain
2987 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
2988 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
2989 * ff02::1/128
2990 * unicast-ip6-chain
2991 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
2992 * [0] [@2]: dpo-receive
2993 * ff02::2/128
2994 * unicast-ip6-chain
2995 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
2996 * [0] [@2]: dpo-receive
2997 * ff02::16/128
2998 * unicast-ip6-chain
2999 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3000 * [0] [@2]: dpo-receive
3001 * ff02::1:ff00:0/104
3002 * unicast-ip6-chain
3003 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3004 * [0] [@2]: dpo-receive
Billy McFall0683c9c2016-10-13 08:27:31 -04003005 * @cliexend
3006 * @endparblock
3007?*/
3008/* *INDENT-OFF* */
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00003009VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003010 .path = "set ip6 flow-hash",
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00003011 .short_help = "set ip6 flow-hash table <table-id> [src] [dst] [sport] "
3012 "[dport] [proto] [reverse] [flowlabel]",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003013 .function = set_ip6_flow_hash_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003014};
Billy McFall0683c9c2016-10-13 08:27:31 -04003015/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003016
3017static clib_error_t *
3018show_ip6_local_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003019 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003020{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003021 ip6_main_t *im = &ip6_main;
3022 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003023 int i;
Dave Barach75fc8542016-10-11 16:16:02 -04003024
Ed Warnickecb9cada2015-12-08 15:45:58 -07003025 vlib_cli_output (vm, "Protocols handled by ip6_local");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003026 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003027 {
3028 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02003029 {
3030
3031 u32 node_index = vlib_get_node (vm,
3032 ip6_local_node.index)->
3033 next_nodes[lm->local_next_by_ip_protocol[i]];
3034 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3035 node_index);
3036 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003037 }
3038 return 0;
3039}
3040
3041
3042
Billy McFall0683c9c2016-10-13 08:27:31 -04003043/*?
3044 * Display the set of protocols handled by the local IPv6 stack.
3045 *
3046 * @cliexpar
3047 * Example of how to display local protocol table:
3048 * @cliexstart{show ip6 local}
3049 * Protocols handled by ip6_local
3050 * 17
3051 * 43
3052 * 58
3053 * 115
3054 * @cliexend
3055?*/
3056/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003057VLIB_CLI_COMMAND (show_ip6_local, static) =
3058{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003059 .path = "show ip6 local",
3060 .function = show_ip6_local_command_fn,
Billy McFall0683c9c2016-10-13 08:27:31 -04003061 .short_help = "show ip6 local",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062};
Billy McFall0683c9c2016-10-13 08:27:31 -04003063/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003064
Damjan Marion38173502019-02-13 19:30:09 +01003065#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05003066int
Mohsin Kazmi68095382021-02-10 11:26:24 +01003067vnet_set_ip6_classify_intfc (vlib_main_t *vm, u32 sw_if_index, u32 table_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003068{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003069 vnet_main_t *vnm = vnet_get_main ();
3070 vnet_interface_main_t *im = &vnm->interface_main;
3071 ip6_main_t *ipm = &ip6_main;
3072 ip_lookup_main_t *lm = &ipm->lookup_main;
3073 vnet_classify_main_t *cm = &vnet_classify_main;
Neale Rannsdf089a82016-10-02 16:39:06 +01003074 ip6_address_t *if_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003075
3076 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3077 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3078
3079 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3080 return VNET_API_ERROR_NO_SUCH_ENTRY;
3081
3082 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003083 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003084
Neale Ranns6cfc39c2017-02-14 01:44:25 -08003085 if_addr = ip6_interface_first_address (ipm, sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01003086
3087 if (NULL != if_addr)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003088 {
Neale Rannsdf089a82016-10-02 16:39:06 +01003089 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003090 .fp_len = 128,
3091 .fp_proto = FIB_PROTOCOL_IP6,
3092 .fp_addr.ip6 = *if_addr,
Neale Rannsdf089a82016-10-02 16:39:06 +01003093 };
3094 u32 fib_index;
3095
Dave Barachd7cb1b52016-12-09 09:52:16 -05003096 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3097 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003098 if (table_index != (u32) ~ 0)
3099 {
3100 dpo_id_t dpo = DPO_INVALID;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003101 dpo_set (&dpo,
3102 DPO_CLASSIFY,
3103 DPO_PROTO_IP6,
3104 classify_dpo_create (DPO_PROTO_IP6, table_index));
Dave Barachd7cb1b52016-12-09 09:52:16 -05003105 fib_table_entry_special_dpo_add (fib_index,
3106 &pfx,
3107 FIB_SOURCE_CLASSIFY,
3108 FIB_ENTRY_FLAG_NONE, &dpo);
3109 dpo_reset (&dpo);
3110 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003111 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003112 {
3113 fib_table_entry_special_remove (fib_index,
3114 &pfx, FIB_SOURCE_CLASSIFY);
3115 }
3116 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003117
Ed Warnickecb9cada2015-12-08 15:45:58 -07003118 return 0;
3119}
Damjan Marion38173502019-02-13 19:30:09 +01003120#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07003121
3122static clib_error_t *
3123set_ip6_classify_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003124 unformat_input_t * input,
3125 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003126{
3127 u32 table_index = ~0;
3128 int table_index_set = 0;
3129 u32 sw_if_index = ~0;
3130 int rv;
Dave Barach75fc8542016-10-11 16:16:02 -04003131
Dave Barachd7cb1b52016-12-09 09:52:16 -05003132 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3133 {
3134 if (unformat (input, "table-index %d", &table_index))
3135 table_index_set = 1;
3136 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3137 vnet_get_main (), &sw_if_index))
3138 ;
3139 else
3140 break;
3141 }
Dave Barach75fc8542016-10-11 16:16:02 -04003142
Ed Warnickecb9cada2015-12-08 15:45:58 -07003143 if (table_index_set == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003144 return clib_error_return (0, "classify table-index must be specified");
Dave Barach75fc8542016-10-11 16:16:02 -04003145
Ed Warnickecb9cada2015-12-08 15:45:58 -07003146 if (sw_if_index == ~0)
3147 return clib_error_return (0, "interface / subif must be specified");
3148
3149 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3150
3151 switch (rv)
3152 {
3153 case 0:
3154 break;
3155
3156 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3157 return clib_error_return (0, "No such interface");
3158
3159 case VNET_API_ERROR_NO_SUCH_ENTRY:
3160 return clib_error_return (0, "No such classifier table");
3161 }
3162 return 0;
3163}
3164
Billy McFall0683c9c2016-10-13 08:27:31 -04003165/*?
3166 * Assign a classification table to an interface. The classification
3167 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3168 * commands. Once the table is create, use this command to filter packets
3169 * on an interface.
3170 *
3171 * @cliexpar
3172 * Example of how to assign a classification table to an interface:
3173 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3174?*/
3175/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003176VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3177{
3178 .path = "set ip6 classify",
3179 .short_help =
3180 "set ip6 classify intfc <interface> table-index <classify-idx>",
3181 .function = set_ip6_classify_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003182};
Billy McFall0683c9c2016-10-13 08:27:31 -04003183/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003184
Dave Barachd7cb1b52016-12-09 09:52:16 -05003185/*
3186 * fd.io coding-style-patch-verification: ON
3187 *
3188 * Local Variables:
3189 * eval: (c-set-style "gnu")
3190 * End:
3191 */