blob: b9f9892f929e2e95010c94b2754704a66243963a [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);
720 }
721
Neale Ranns8269d3d2018-01-30 09:02:20 -0800722 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
Damjan Marion8b3191e2016-11-09 19:54:20 +0100723 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724
Neale Ranns8269d3d2018-01-30 09:02:20 -0800725 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
726 sw_if_index, is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 return /* no error */ 0;
729}
730
731VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
732
Damjan Marion38173502019-02-13 19:30:09 +0100733VLIB_NODE_FN (ip6_lookup_node) (vlib_main_t * vm,
734 vlib_node_runtime_t * node,
735 vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200736{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100737 return ip6_lookup_inline (vm, node, frame);
Damjan Marionaca64c92016-04-13 09:48:56 +0200738}
739
Dave Barachd7cb1b52016-12-09 09:52:16 -0500740static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100741
Dave Barachd7cb1b52016-12-09 09:52:16 -0500742/* *INDENT-OFF* */
743VLIB_REGISTER_NODE (ip6_lookup_node) =
744{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 .name = "ip6-lookup",
746 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100747 .format_trace = format_ip6_lookup_trace,
Ole Troanf0f85222016-06-14 21:12:32 +0200748 .n_next_nodes = IP6_LOOKUP_N_NEXT,
Damjan Marionb2707892016-04-13 11:21:07 +0200749 .next_nodes = IP6_LOOKUP_NEXT_NODES,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500751/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752
Damjan Marion38173502019-02-13 19:30:09 +0100753VLIB_NODE_FN (ip6_load_balance_node) (vlib_main_t * vm,
754 vlib_node_runtime_t * node,
755 vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200756{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500757 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
Neale Ranns3ce72b22019-05-27 08:21:32 -0400758 u32 n_left, *from;
Damjan Marion067cd622018-07-11 12:47:43 +0200759 u32 thread_index = vm->thread_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500760 ip6_main_t *im = &ip6_main;
Neale Ranns3ce72b22019-05-27 08:21:32 -0400761 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
762 u16 nexts[VLIB_FRAME_SIZE], *next;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100763
764 from = vlib_frame_vector_args (frame);
Neale Ranns3ce72b22019-05-27 08:21:32 -0400765 n_left = frame->n_vectors;
766 next = nexts;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100767
Neale Ranns3ce72b22019-05-27 08:21:32 -0400768 vlib_get_buffers (vm, from, bufs, n_left);
769
770 while (n_left >= 4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100771 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400772 const load_balance_t *lb0, *lb1;
773 const ip6_header_t *ip0, *ip1;
774 u32 lbi0, hc0, lbi1, hc1;
775 const dpo_id_t *dpo0, *dpo1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100776
Neale Ranns3ce72b22019-05-27 08:21:32 -0400777 /* Prefetch next iteration. */
778 {
779 vlib_prefetch_buffer_header (b[2], STORE);
780 vlib_prefetch_buffer_header (b[3], STORE);
Dave Barach75fc8542016-10-11 16:16:02 -0400781
Neale Ranns3ce72b22019-05-27 08:21:32 -0400782 CLIB_PREFETCH (b[2]->data, sizeof (ip0[0]), STORE);
783 CLIB_PREFETCH (b[3]->data, sizeof (ip0[0]), STORE);
784 }
785
786 ip0 = vlib_buffer_get_current (b[0]);
787 ip1 = vlib_buffer_get_current (b[1]);
788 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
789 lbi1 = vnet_buffer (b[1])->ip.adj_index[VLIB_TX];
790
791 lb0 = load_balance_get (lbi0);
792 lb1 = load_balance_get (lbi1);
793
794 /*
795 * this node is for via FIBs we can re-use the hash value from the
796 * to node if present.
797 * We don't want to use the same hash value at each level in the recursion
798 * graph as that would lead to polarisation
799 */
800 hc0 = hc1 = 0;
801
802 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500803 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400804 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500805 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400806 hc0 = vnet_buffer (b[0])->ip.flow_hash =
807 vnet_buffer (b[0])->ip.flow_hash >> 1;
Neale Rannsf12a83f2017-04-18 09:09:40 -0700808 }
809 else
810 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400811 hc0 = vnet_buffer (b[0])->ip.flow_hash =
812 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500813 }
Neale Ranns3ce72b22019-05-27 08:21:32 -0400814 dpo0 = load_balance_get_fwd_bucket
815 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
816 }
817 else
818 {
819 dpo0 = load_balance_get_bucket_i (lb0, 0);
820 }
821 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
822 {
823 if (PREDICT_TRUE (vnet_buffer (b[1])->ip.flow_hash))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500824 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400825 hc1 = vnet_buffer (b[1])->ip.flow_hash =
826 vnet_buffer (b[1])->ip.flow_hash >> 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500827 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700828 else
829 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400830 hc1 = vnet_buffer (b[1])->ip.flow_hash =
831 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
Neale Rannsf12a83f2017-04-18 09:09:40 -0700832 }
Neale Ranns3ce72b22019-05-27 08:21:32 -0400833 dpo1 = load_balance_get_fwd_bucket
834 (lb1, (hc1 & (lb1->lb_n_buckets_minus_1)));
835 }
836 else
837 {
838 dpo1 = load_balance_get_bucket_i (lb1, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500839 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000840
Neale Ranns3ce72b22019-05-27 08:21:32 -0400841 next[0] = dpo0->dpoi_next_node;
842 next[1] = dpo1->dpoi_next_node;
843
844 /* Only process the HBH Option Header if explicitly configured to do so */
845 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500846 {
Neale Ranns3ce72b22019-05-27 08:21:32 -0400847 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
848 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
849 }
850 /* Only process the HBH Option Header if explicitly configured to do so */
851 if (PREDICT_FALSE (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
852 {
853 next[1] = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
854 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[1];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500855 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100856
Neale Ranns3ce72b22019-05-27 08:21:32 -0400857 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
858 vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
859
860 vlib_increment_combined_counter
861 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
862 vlib_increment_combined_counter
863 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, b[1]));
864
865 b += 2;
866 next += 2;
867 n_left -= 2;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100868 }
869
Neale Ranns3ce72b22019-05-27 08:21:32 -0400870 while (n_left > 0)
871 {
872 const load_balance_t *lb0;
873 const ip6_header_t *ip0;
874 const dpo_id_t *dpo0;
875 u32 lbi0, hc0;
876
877 ip0 = vlib_buffer_get_current (b[0]);
878 lbi0 = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
879
880 lb0 = load_balance_get (lbi0);
881
882 hc0 = 0;
883 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
884 {
885 if (PREDICT_TRUE (vnet_buffer (b[0])->ip.flow_hash))
886 {
887 hc0 = vnet_buffer (b[0])->ip.flow_hash =
888 vnet_buffer (b[0])->ip.flow_hash >> 1;
889 }
890 else
891 {
892 hc0 = vnet_buffer (b[0])->ip.flow_hash =
893 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
894 }
895 dpo0 = load_balance_get_fwd_bucket
896 (lb0, (hc0 & (lb0->lb_n_buckets_minus_1)));
897 }
898 else
899 {
900 dpo0 = load_balance_get_bucket_i (lb0, 0);
901 }
902
903 next[0] = dpo0->dpoi_next_node;
904 vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
905
906 /* Only process the HBH Option Header if explicitly configured to do so */
907 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
908 {
909 next[0] = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
910 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next[0];
911 }
912
913 vlib_increment_combined_counter
914 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, b[0]));
915
916 b += 1;
917 next += 1;
918 n_left -= 1;
919 }
920
921 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
922
Neale Rannsa71844f2018-11-08 07:31:36 -0800923 if (node->flags & VLIB_NODE_FLAG_TRACE)
924 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
925
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100926 return frame->n_vectors;
Damjan Marionaca64c92016-04-13 09:48:56 +0200927}
928
Dave Barachd7cb1b52016-12-09 09:52:16 -0500929/* *INDENT-OFF* */
930VLIB_REGISTER_NODE (ip6_load_balance_node) =
931{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100932 .name = "ip6-load-balance",
Damjan Marionaca64c92016-04-13 09:48:56 +0200933 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200934 .sibling_of = "ip6-lookup",
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100935 .format_trace = format_ip6_lookup_trace,
Damjan Marionaca64c92016-04-13 09:48:56 +0200936};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500937/* *INDENT-ON* */
Damjan Marionaca64c92016-04-13 09:48:56 +0200938
Dave Barachd7cb1b52016-12-09 09:52:16 -0500939typedef struct
940{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941 /* Adjacency taken. */
942 u32 adj_index;
943 u32 flow_hash;
John Lo2d343742016-01-19 17:27:17 -0500944 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945
946 /* Packet data, possibly *after* rewrite. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500947 u8 packet_data[128 - 1 * sizeof (u32)];
948}
949ip6_forward_next_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
Damjan Marion38173502019-02-13 19:30:09 +0100951#ifndef CLIB_MARCH_VARIANT
John Lo2b81eb82017-01-30 13:12:10 -0500952u8 *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500953format_ip6_forward_next_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954{
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100955 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
956 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500957 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200958 u32 indent = format_get_indent (s);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100959
Neale Rannsdc617b82020-08-20 08:22:56 +0000960 s = format (s, "%Ufib:%d adj:%d flow:%d",
961 format_white_space, indent,
962 t->fib_index, t->adj_index, t->flow_hash);
963 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500964 format_white_space, indent,
965 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100966 return s;
967}
Damjan Marion38173502019-02-13 19:30:09 +0100968#endif
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100969
Dave Barachd7cb1b52016-12-09 09:52:16 -0500970static u8 *
971format_ip6_lookup_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100972{
973 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
974 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500975 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200976 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977
John Loac8146c2016-09-27 17:44:02 -0400978 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500979 t->fib_index, t->adj_index, t->flow_hash);
980 s = format (s, "\n%U%U",
981 format_white_space, indent,
982 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100983 return s;
984}
Pierre Pfister0febaf12016-06-08 12:23:21 +0100985
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
Dave Barachd7cb1b52016-12-09 09:52:16 -0500987static u8 *
988format_ip6_rewrite_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100989{
990 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
991 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500992 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200993 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100995 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500996 t->fib_index, t->adj_index, format_ip_adjacency,
997 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100998 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500999 format_white_space, indent,
1000 format_ip_adjacency_packet_data,
Neale Ranns0b6a8572019-10-30 17:34:14 +00001001 t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 return s;
1003}
1004
1005/* Common trace function for all ip6-forward next nodes. */
Damjan Marion38173502019-02-13 19:30:09 +01001006#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007void
1008ip6_forward_next_trace (vlib_main_t * vm,
1009 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001010 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001012 u32 *from, n_left;
1013 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014
1015 n_left = frame->n_vectors;
1016 from = vlib_frame_vector_args (frame);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001017
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018 while (n_left >= 4)
1019 {
1020 u32 bi0, bi1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001021 vlib_buffer_t *b0, *b1;
1022 ip6_forward_next_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
1024 /* Prefetch next iteration. */
1025 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1026 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1027
1028 bi0 = from[0];
1029 bi1 = from[1];
1030
1031 b0 = vlib_get_buffer (vm, bi0);
1032 b1 = vlib_get_buffer (vm, bi1);
1033
1034 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1035 {
1036 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1037 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001038 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1039 t0->fib_index =
1040 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1041 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1042 vec_elt (im->fib_index_by_sw_if_index,
1043 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001044
Dave Barach178cf492018-11-13 16:34:13 -05001045 clib_memcpy_fast (t0->packet_data,
1046 vlib_buffer_get_current (b0),
1047 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048 }
1049 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1050 {
1051 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1052 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001053 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1054 t1->fib_index =
1055 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1056 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1057 vec_elt (im->fib_index_by_sw_if_index,
1058 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001059
Dave Barach178cf492018-11-13 16:34:13 -05001060 clib_memcpy_fast (t1->packet_data,
1061 vlib_buffer_get_current (b1),
1062 sizeof (t1->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063 }
1064 from += 2;
1065 n_left -= 2;
1066 }
1067
1068 while (n_left >= 1)
1069 {
1070 u32 bi0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001071 vlib_buffer_t *b0;
1072 ip6_forward_next_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073
1074 bi0 = from[0];
1075
1076 b0 = vlib_get_buffer (vm, bi0);
1077
1078 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1079 {
1080 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1081 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001082 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1083 t0->fib_index =
1084 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1085 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1086 vec_elt (im->fib_index_by_sw_if_index,
1087 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001088
Dave Barach178cf492018-11-13 16:34:13 -05001089 clib_memcpy_fast (t0->packet_data,
1090 vlib_buffer_get_current (b0),
1091 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092 }
1093 from += 1;
1094 n_left -= 1;
1095 }
1096}
1097
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098/* Compute TCP/UDP/ICMP6 checksum in software. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001099u16
1100ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1101 ip6_header_t * ip0, int *bogus_lengthp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102{
Matthew Smith97677a22020-02-05 11:46:40 -06001103 ip_csum_t sum0 = 0;
1104 u16 payload_length, payload_length_host_byte_order;
Srikanth A02833ff2019-10-02 17:48:58 -07001105 u32 i;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001106 u32 headers_size = sizeof (ip0[0]);
Dave Barachc4abafd2019-09-04 12:09:32 -04001107 u8 *data_this_buffer;
Matthew Smith97677a22020-02-05 11:46:40 -06001108 u8 next_hdr = ip0->protocol;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109
Dave Barachd7cb1b52016-12-09 09:52:16 -05001110 ASSERT (bogus_lengthp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111 *bogus_lengthp = 0;
1112
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
Dave Barachc4abafd2019-09-04 12:09:32 -04001114 data_this_buffer = (u8 *) (ip0 + 1);
Matthew Smith97677a22020-02-05 11:46:40 -06001115 payload_length = ip0->payload_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301117 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
1118 * or UDP-Ping packets */
Matthew Smith97677a22020-02-05 11:46:40 -06001119 if (PREDICT_FALSE (next_hdr == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001121 u32 skip_bytes;
1122 ip6_hop_by_hop_ext_t *ext_hdr =
1123 (ip6_hop_by_hop_ext_t *) data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124
1125 /* validate really icmp6 next */
AkshayaNadahalli1b563522017-01-23 22:05:35 +05301126 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1127 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128
Dave Barachd7cb1b52016-12-09 09:52:16 -05001129 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1130 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
Dave Barach75fc8542016-10-11 16:16:02 -04001131
Dave Barachd7cb1b52016-12-09 09:52:16 -05001132 payload_length_host_byte_order -= skip_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133 headers_size += skip_bytes;
Matthew Smith97677a22020-02-05 11:46:40 -06001134
1135 /* pseudo-header adjustments:
1136 * exclude ext header bytes from payload length
1137 * use payload IP proto rather than ext header IP proto
1138 */
1139 payload_length = clib_host_to_net_u16 (payload_length_host_byte_order);
1140 next_hdr = ext_hdr->next_hdr;
1141 }
1142
1143 /* Initialize checksum with ip pseudo-header. */
1144 sum0 = payload_length + clib_host_to_net_u16 (next_hdr);
1145
1146 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1147 {
1148 sum0 = ip_csum_with_carry
1149 (sum0, clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1150 sum0 = ip_csum_with_carry
1151 (sum0, clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001152 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153
John Lo3bc6bc22019-08-03 14:36:39 -04001154 if (p0)
Srikanth A02833ff2019-10-02 17:48:58 -07001155 return ip_calculate_l4_checksum (vm, p0, sum0,
1156 payload_length_host_byte_order,
1157 (u8 *) ip0, headers_size, NULL);
1158 else
1159 return ip_calculate_l4_checksum (vm, 0, sum0,
1160 payload_length_host_byte_order, NULL, 0,
1161 data_this_buffer);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162}
1163
Dave Barachd7cb1b52016-12-09 09:52:16 -05001164u32
1165ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001167 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1168 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 u16 sum16;
1170 int bogus_length;
1171
1172 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1173 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1174 || ip0->protocol == IP_PROTOCOL_ICMP6
1175 || ip0->protocol == IP_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001176 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177
1178 udp0 = (void *) (ip0 + 1);
1179 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1180 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001181 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1182 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183 return p0->flags;
1184 }
1185
1186 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1187
Damjan Marion213b5aa2017-07-13 21:19:27 +02001188 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1189 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001190
1191 return p0->flags;
1192}
Damjan Marion38173502019-02-13 19:30:09 +01001193#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301195/**
1196 * @brief returns number of links on which src is reachable.
1197 */
1198always_inline int
1199ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1200{
1201 const load_balance_t *lb0;
1202 index_t lbi;
Florin Corasf3a3bad2018-03-28 02:18:29 -07001203 u32 fib_index;
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301204
Florin Corasf3a3bad2018-03-28 02:18:29 -07001205 fib_index = vec_elt (im->fib_index_by_sw_if_index,
1206 vnet_buffer (b)->sw_if_index[VLIB_RX]);
1207 fib_index =
1208 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
1209 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301210
Simon Zhange7eba482019-08-25 15:30:45 +08001211 lbi = ip6_fib_table_fwding_lookup (fib_index, &i->src_address);
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301212 lb0 = load_balance_get (lbi);
1213
1214 return (fib_urpf_check_size (lb0->lb_urpf));
1215}
1216
rootc9d1c5b2017-08-15 12:58:31 -04001217always_inline u8
1218ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1219 u32 * udp_offset0)
1220{
1221 u32 proto0;
1222 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1223 if (proto0 != IP_PROTOCOL_UDP)
1224 {
1225 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1226 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1227 }
1228 return proto0;
1229}
1230
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001231/* *INDENT-OFF* */
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001232VNET_FEATURE_ARC_INIT (ip6_local) = {
1233 .arc_name = "ip6-local",
1234 .start_nodes = VNET_FEATURES ("ip6-local", "ip6-receive"),
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001235};
1236/* *INDENT-ON* */
1237
johny17478e42019-10-11 18:28:51 +02001238static_always_inline u8
1239ip6_tcp_udp_icmp_bad_length (vlib_main_t * vm, vlib_buffer_t * p0)
1240{
1241
1242 u16 payload_length_host_byte_order;
1243 u32 n_this_buffer, n_bytes_left;
1244 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1245 u32 headers_size = sizeof (ip0[0]);
1246 u8 *data_this_buffer;
1247
1248
1249 data_this_buffer = (u8 *) (ip0 + 1);
1250
1251 ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t *) data_this_buffer;
1252
1253 /* validate really icmp6 next */
1254
1255 if (!(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
1256 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP))
1257 return 0;
1258
1259
1260 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1261 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1262
johnya633a432019-12-06 13:58:35 +01001263
1264 u32 n_ip_bytes_this_buffer =
1265 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
1266 if (n_this_buffer + headers_size > n_ip_bytes_this_buffer)
johny17478e42019-10-11 18:28:51 +02001267 {
johnya633a432019-12-06 13:58:35 +01001268 n_this_buffer = p0->current_length > headers_size ?
1269 n_ip_bytes_this_buffer - headers_size : 0;
johny17478e42019-10-11 18:28:51 +02001270 }
1271
1272 n_bytes_left -= n_this_buffer;
1273 n_bytes_left -= p0->total_length_not_including_first_buffer;
1274
1275 if (n_bytes_left == 0)
1276 return 0;
1277 else
1278 return 1;
1279}
1280
Benoît Ganne26a10192019-02-14 15:32:45 +01001281always_inline uword
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001282ip6_local_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1283 vlib_frame_t *frame, int head_of_feature_arc,
1284 int is_receive_dpo)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001286 ip6_main_t *im = &ip6_main;
1287 ip_lookup_main_t *lm = &im->lookup_main;
Benoît Ganne26a10192019-02-14 15:32:45 +01001288 u32 *from, n_left_from;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001289 vlib_node_runtime_t *error_node =
1290 vlib_node_get_runtime (vm, ip6_input_node.index);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001291 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
Benoît Ganne26a10192019-02-14 15:32:45 +01001292 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1293 u16 nexts[VLIB_FRAME_SIZE], *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294
1295 from = vlib_frame_vector_args (frame);
1296 n_left_from = frame->n_vectors;
Dave Barach75fc8542016-10-11 16:16:02 -04001297
Ed Warnickecb9cada2015-12-08 15:45:58 -07001298 if (node->flags & VLIB_NODE_FLAG_TRACE)
1299 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1300
Benoît Ganne26a10192019-02-14 15:32:45 +01001301 vlib_get_buffers (vm, from, bufs, n_left_from);
1302 b = bufs;
1303 next = nexts;
1304
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001305 while (n_left_from > 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001306 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001307 /* Prefetch next iteration. */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001308 if (n_left_from >= 6)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001310 vlib_prefetch_buffer_header (b[4], STORE);
1311 vlib_prefetch_buffer_header (b[5], STORE);
1312 vlib_prefetch_buffer_data (b[2], LOAD);
1313 vlib_prefetch_buffer_data (b[3], LOAD);
Benoît Ganne26a10192019-02-14 15:32:45 +01001314 }
Dave Barach75fc8542016-10-11 16:16:02 -04001315
Neale Rannsf6472e02020-12-18 09:42:18 +00001316 ip6_error_t error[2];
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001317 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1318 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
Dave Barach75fc8542016-10-11 16:16:02 -04001319
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001320 ip6_header_t *ip[2];
1321 ip[0] = vlib_buffer_get_current (b[0]);
1322 ip[1] = vlib_buffer_get_current (b[1]);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001323
Benoît Ganne26a10192019-02-14 15:32:45 +01001324 if (head_of_feature_arc)
1325 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001326 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1327 vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001329 u8 type[2];
1330 type[0] = lm->builtin_protocol_by_ip_protocol[ip[0]->protocol];
1331 type[1] = lm->builtin_protocol_by_ip_protocol[ip[1]->protocol];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001333 u32 flags[2];
1334 flags[0] = b[0]->flags;
1335 flags[1] = b[1]->flags;
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001336
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +02001337 vnet_buffer_oflags_t oflags[2];
Mohsin Kazmia7e830e2021-04-23 15:16:50 +02001338 oflags[0] = vnet_buffer (b[0])->oflags;
1339 oflags[1] = vnet_buffer (b[1])->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +01001340
1341 u32 l4_offload[2];
1342 l4_offload[0] = (flags[0] & VNET_BUFFER_F_OFFLOAD) &&
1343 (oflags[0] & (VNET_BUFFER_OFFLOAD_F_TCP_CKSUM |
1344 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM));
1345 l4_offload[1] = (flags[1] & VNET_BUFFER_F_OFFLOAD) &&
1346 (oflags[1] & (VNET_BUFFER_OFFLOAD_F_TCP_CKSUM |
1347 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM));
1348
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001349 u32 good_l4_csum[2];
1350 good_l4_csum[0] =
Mohsin Kazmi68095382021-02-10 11:26:24 +01001351 (flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) | l4_offload[0];
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001352 good_l4_csum[1] =
Mohsin Kazmi68095382021-02-10 11:26:24 +01001353 (flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) | l4_offload[1];
Filip Tehlarb601f222017-01-02 10:22:56 +01001354
Damjan Marion34e823f2019-02-19 08:55:18 +01001355 u32 udp_offset[2] = { };
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001356 u8 is_tcp_udp[2];
1357 is_tcp_udp[0] =
1358 ip6_next_proto_is_tcp_udp (b[0], ip[0], &udp_offset[0]);
1359 is_tcp_udp[1] =
1360 ip6_next_proto_is_tcp_udp (b[1], ip[1], &udp_offset[1]);
1361 i16 len_diff[2] = { 0 };
1362 if (PREDICT_TRUE (is_tcp_udp[0]))
Shwethab78292e2016-09-13 11:51:00 +01001363 {
Benoît Ganne26a10192019-02-14 15:32:45 +01001364 udp_header_t *udp =
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001365 (udp_header_t *) ((u8 *) ip[0] + udp_offset[0]);
1366 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UDP
Benoît Ganne26a10192019-02-14 15:32:45 +01001367 && udp->checksum == 0;
1368 /* optimistically verify UDP length. */
1369 u16 ip_len, udp_len;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001370 ip_len = clib_net_to_host_u16 (ip[0]->payload_length);
Benoît Ganne26a10192019-02-14 15:32:45 +01001371 udp_len = clib_net_to_host_u16 (udp->length);
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001372 len_diff[0] = ip_len - udp_len;
Shwethab78292e2016-09-13 11:51:00 +01001373 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001374 if (PREDICT_TRUE (is_tcp_udp[1]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001376 udp_header_t *udp =
1377 (udp_header_t *) ((u8 *) ip[1] + udp_offset[1]);
1378 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UDP
1379 && udp->checksum == 0;
1380 /* optimistically verify UDP length. */
1381 u16 ip_len, udp_len;
1382 ip_len = clib_net_to_host_u16 (ip[1]->payload_length);
1383 udp_len = clib_net_to_host_u16 (udp->length);
1384 len_diff[1] = ip_len - udp_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001385 }
1386
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001387 good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1388 good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1389
1390 len_diff[0] = type[0] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[0] : 0;
1391 len_diff[1] = type[1] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[1] : 0;
1392
1393 u8 need_csum[2];
1394 need_csum[0] = type[0] != IP_BUILTIN_PROTOCOL_UNKNOWN
1395 && !good_l4_csum[0]
1396 && !(flags[0] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1397 need_csum[1] = type[1] != IP_BUILTIN_PROTOCOL_UNKNOWN
1398 && !good_l4_csum[1]
1399 && !(flags[1] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1400 if (PREDICT_FALSE (need_csum[0]))
1401 {
1402 flags[0] = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1403 good_l4_csum[0] = flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
johny17478e42019-10-11 18:28:51 +02001404 error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1405 }
1406 else
1407 {
1408 if (ip6_tcp_udp_icmp_bad_length (vm, b[0]))
1409 error[0] = IP6_ERROR_BAD_LENGTH;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001410 }
1411 if (PREDICT_FALSE (need_csum[1]))
1412 {
1413 flags[1] = ip6_tcp_udp_icmp_validate_checksum (vm, b[1]);
1414 good_l4_csum[1] = flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
johny17478e42019-10-11 18:28:51 +02001415 error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1416 }
1417 else
1418 {
1419 if (ip6_tcp_udp_icmp_bad_length (vm, b[1]))
1420 error[1] = IP6_ERROR_BAD_LENGTH;
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001421 }
1422
johny17478e42019-10-11 18:28:51 +02001423
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001424 error[0] = len_diff[0] < 0 ? IP6_ERROR_UDP_LENGTH : error[0];
johny17478e42019-10-11 18:28:51 +02001425
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001426 error[1] = len_diff[1] < 0 ? IP6_ERROR_UDP_LENGTH : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001427
Benoît Ganne26a10192019-02-14 15:32:45 +01001428 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1429 IP6_ERROR_UDP_CHECKSUM,
1430 "Wrong IP6 errors constants");
1431 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1432 IP6_ERROR_ICMP_CHECKSUM,
1433 "Wrong IP6 errors constants");
1434
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001435 error[0] =
1436 !good_l4_csum[0] ? IP6_ERROR_UDP_CHECKSUM + type[0] : error[0];
1437 error[1] =
1438 !good_l4_csum[1] ? IP6_ERROR_UDP_CHECKSUM + type[1] : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439
1440 /* Drop packets from unroutable hosts. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001441 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001442 u8 unroutable[2];
1443 unroutable[0] = error[0] == IP6_ERROR_UNKNOWN_PROTOCOL
1444 && type[0] != IP_BUILTIN_PROTOCOL_ICMP
1445 && !ip6_address_is_link_local_unicast (&ip[0]->src_address);
1446 unroutable[1] = error[1] == IP6_ERROR_UNKNOWN_PROTOCOL
1447 && type[1] != IP_BUILTIN_PROTOCOL_ICMP
1448 && !ip6_address_is_link_local_unicast (&ip[1]->src_address);
1449 if (PREDICT_FALSE (unroutable[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001451 error[0] =
1452 !ip6_urpf_loose_check (im, b[0],
1453 ip[0]) ? IP6_ERROR_SRC_LOOKUP_MISS
1454 : error[0];
1455 }
1456 if (PREDICT_FALSE (unroutable[1]))
1457 {
1458 error[1] =
1459 !ip6_urpf_loose_check (im, b[1],
1460 ip[1]) ? IP6_ERROR_SRC_LOOKUP_MISS
1461 : error[1];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462 }
1463
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001464 vnet_buffer (b[0])->ip.fib_index =
1465 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1466 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1467 vnet_buffer (b[0])->ip.fib_index;
1468 vnet_buffer (b[1])->ip.fib_index =
1469 vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ?
1470 vnet_buffer (b[1])->sw_if_index[VLIB_TX] :
1471 vnet_buffer (b[1])->ip.fib_index;
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001472 if (is_receive_dpo)
1473 {
1474 const receive_dpo_t *rd0, *rd1;
1475 rd0 =
1476 receive_dpo_get (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
1477 rd1 =
1478 receive_dpo_get (vnet_buffer (b[1])->ip.adj_index[VLIB_TX]);
1479 vnet_buffer (b[0])->ip.rx_sw_if_index = rd0->rd_sw_if_index;
1480 vnet_buffer (b[1])->ip.rx_sw_if_index = rd1->rd_sw_if_index;
1481 }
1482 else
1483 {
1484 vnet_buffer (b[0])->ip.rx_sw_if_index = ~0;
1485 vnet_buffer (b[1])->ip.rx_sw_if_index = ~0;
1486 }
Benoît Ganne26a10192019-02-14 15:32:45 +01001487 } /* head_of_feature_arc */
Florin Corascea194d2017-10-02 00:18:51 -07001488
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001489 next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol];
1490 next[0] =
1491 error[0] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1492 next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol];
1493 next[1] =
1494 error[1] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[1];
Florin Corascea194d2017-10-02 00:18:51 -07001495
Benoît Gannec15539a2021-01-19 16:40:07 +01001496 b[0]->error = error_node->errors[error[0]];
1497 b[1]->error = error_node->errors[error[1]];
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001498
Benoît Ganne26a10192019-02-14 15:32:45 +01001499 if (head_of_feature_arc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001501 u8 ip6_unknown[2];
1502 ip6_unknown[0] = error[0] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1503 ip6_unknown[1] = error[1] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1504 if (PREDICT_TRUE (ip6_unknown[0]))
Shwethab78292e2016-09-13 11:51:00 +01001505 {
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001506 u32 next32 = next[0];
Benoît Ganne26a10192019-02-14 15:32:45 +01001507 vnet_feature_arc_start (arc_index,
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001508 vnet_buffer (b[0])->sw_if_index
1509 [VLIB_RX], &next32, b[0]);
1510 next[0] = next32;
1511 }
1512 if (PREDICT_TRUE (ip6_unknown[1]))
1513 {
1514 u32 next32 = next[1];
1515 vnet_feature_arc_start (arc_index,
1516 vnet_buffer (b[1])->sw_if_index
1517 [VLIB_RX], &next32, b[1]);
1518 next[1] = next32;
Shwethab78292e2016-09-13 11:51:00 +01001519 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001520 }
Dave Barach75fc8542016-10-11 16:16:02 -04001521
Benoît Ganne26a10192019-02-14 15:32:45 +01001522 /* next */
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001523 b += 2;
1524 next += 2;
1525 n_left_from -= 2;
Benoît Ganne26a10192019-02-14 15:32:45 +01001526 }
Benoît Ganne7dcb80a2019-02-14 15:32:45 +01001527
Benoît Ganne26a10192019-02-14 15:32:45 +01001528 while (n_left_from)
1529 {
1530 u8 error;
1531 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1532
1533 ip6_header_t *ip;
1534 ip = vlib_buffer_get_current (b[0]);
1535
1536 if (head_of_feature_arc)
1537 {
1538 vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1539 u8 type = lm->builtin_protocol_by_ip_protocol[ip->protocol];
1540
1541 u32 flags = b[0]->flags;
Benoît Ganne26a10192019-02-14 15:32:45 +01001542
Mohsin Kazmi36f7a6a2021-05-05 14:26:38 +02001543 vnet_buffer_oflags_t oflags = vnet_buffer (b[0])->oflags;
Mohsin Kazmi68095382021-02-10 11:26:24 +01001544
1545 u32 l4_offload = (flags & VNET_BUFFER_F_OFFLOAD) &&
1546 (oflags & (VNET_BUFFER_OFFLOAD_F_TCP_CKSUM |
1547 VNET_BUFFER_OFFLOAD_F_UDP_CKSUM));
1548
1549 u32 good_l4_csum =
1550 (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) | l4_offload;
Benoît Ganne26a10192019-02-14 15:32:45 +01001551 u32 udp_offset;
1552 i16 len_diff = 0;
1553 u8 is_tcp_udp = ip6_next_proto_is_tcp_udp (b[0], ip, &udp_offset);
1554 if (PREDICT_TRUE (is_tcp_udp))
1555 {
1556 udp_header_t *udp = (udp_header_t *) ((u8 *) ip + udp_offset);
1557 /* Don't verify UDP checksum for packets with explicit zero checksum. */
1558 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UDP
1559 && udp->checksum == 0;
1560 /* optimistically verify UDP length. */
1561 u16 ip_len, udp_len;
1562 ip_len = clib_net_to_host_u16 (ip->payload_length);
1563 udp_len = clib_net_to_host_u16 (udp->length);
1564 len_diff = ip_len - udp_len;
1565 }
1566
1567 good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UNKNOWN;
1568 len_diff = type == IP_BUILTIN_PROTOCOL_UDP ? len_diff : 0;
1569
Mohsin Kazmi68095382021-02-10 11:26:24 +01001570 u8 need_csum = type != IP_BUILTIN_PROTOCOL_UNKNOWN &&
1571 !good_l4_csum &&
1572 !(flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
Benoît Ganne26a10192019-02-14 15:32:45 +01001573 if (PREDICT_FALSE (need_csum))
1574 {
1575 flags = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1576 good_l4_csum = flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
johny17478e42019-10-11 18:28:51 +02001577 error = IP6_ERROR_UNKNOWN_PROTOCOL;
1578 }
1579 else
1580 {
1581 if (ip6_tcp_udp_icmp_bad_length (vm, b[0]))
1582 error = IP6_ERROR_BAD_LENGTH;
Benoît Ganne26a10192019-02-14 15:32:45 +01001583 }
1584
johny17478e42019-10-11 18:28:51 +02001585
1586
Benoît Ganne26a10192019-02-14 15:32:45 +01001587 error = len_diff < 0 ? IP6_ERROR_UDP_LENGTH : error;
Benoît Ganne26a10192019-02-14 15:32:45 +01001588 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1589 IP6_ERROR_UDP_CHECKSUM,
1590 "Wrong IP6 errors constants");
1591 STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1592 IP6_ERROR_ICMP_CHECKSUM,
1593 "Wrong IP6 errors constants");
1594
1595 error = !good_l4_csum ? IP6_ERROR_UDP_CHECKSUM + type : error;
1596
1597 /* Drop packets from unroutable hosts. */
1598 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1599 u8 unroutable = error == IP6_ERROR_UNKNOWN_PROTOCOL
1600 && type != IP_BUILTIN_PROTOCOL_ICMP
1601 && !ip6_address_is_link_local_unicast (&ip->src_address);
1602 if (PREDICT_FALSE (unroutable))
1603 {
1604 error =
1605 !ip6_urpf_loose_check (im, b[0],
1606 ip) ? IP6_ERROR_SRC_LOOKUP_MISS :
1607 error;
1608 }
1609
1610 vnet_buffer (b[0])->ip.fib_index =
1611 vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1612 vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1613 vnet_buffer (b[0])->ip.fib_index;
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001614 if (is_receive_dpo)
1615 {
1616 receive_dpo_t *rd;
1617 rd = receive_dpo_get (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
1618 vnet_buffer (b[0])->ip.rx_sw_if_index = rd->rd_sw_if_index;
1619 }
1620 else
1621 vnet_buffer (b[0])->ip.rx_sw_if_index = ~0;
Benoît Ganne26a10192019-02-14 15:32:45 +01001622 } /* head_of_feature_arc */
1623
1624 next[0] = lm->local_next_by_ip_protocol[ip->protocol];
1625 next[0] =
1626 error != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1627
Benoît Gannec15539a2021-01-19 16:40:07 +01001628 b[0]->error = error_node->errors[error];
Benoît Ganne26a10192019-02-14 15:32:45 +01001629
1630 if (head_of_feature_arc)
1631 {
1632 if (PREDICT_TRUE (error == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1633 {
1634 u32 next32 = next[0];
1635 vnet_feature_arc_start (arc_index,
1636 vnet_buffer (b[0])->sw_if_index
1637 [VLIB_RX], &next32, b[0]);
1638 next[0] = next32;
1639 }
1640 }
1641
1642 /* next */
1643 b += 1;
1644 next += 1;
1645 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646 }
1647
Benoît Ganne26a10192019-02-14 15:32:45 +01001648 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001649 return frame->n_vectors;
1650}
1651
Damjan Marion38173502019-02-13 19:30:09 +01001652VLIB_NODE_FN (ip6_local_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1653 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001654{
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001655 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */,
1656 0 /* ip6_local_inline */);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001657}
1658
Damjan Marion38173502019-02-13 19:30:09 +01001659VLIB_REGISTER_NODE (ip6_local_node) =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001660{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001661 .name = "ip6-local",
1662 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001663 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001664 .n_next_nodes = IP_LOCAL_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001665 .next_nodes =
1666 {
Neale Rannsd91c1db2017-07-31 02:30:50 -07001667 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1668 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001669 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1670 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
Klement Sekera896c8962019-06-24 11:52:49 +00001671 [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-full-reassembly",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001672 },
1673};
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001674
1675VLIB_NODE_FN (ip6_receive_local_node)
1676(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1677{
1678 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */,
1679 1 /* is_receive_dpo */);
1680}
1681
1682VLIB_REGISTER_NODE (ip6_receive_local_node) = {
1683 .name = "ip6-receive",
1684 .vector_size = sizeof (u32),
1685 .format_trace = format_ip6_forward_next_trace,
1686 .sibling_of = "ip6-local"
1687};
Ed Warnickecb9cada2015-12-08 15:45:58 -07001688
Damjan Marion38173502019-02-13 19:30:09 +01001689VLIB_NODE_FN (ip6_local_end_of_arc_node) (vlib_main_t * vm,
1690 vlib_node_runtime_t * node,
1691 vlib_frame_t * frame)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001692{
Nathan Skrzypczakbfa86082021-09-09 18:31:36 +02001693 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */,
1694 0 /* ip6_local_inline */);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001695}
1696
Damjan Marion38173502019-02-13 19:30:09 +01001697VLIB_REGISTER_NODE (ip6_local_end_of_arc_node) = {
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001698 .name = "ip6-local-end-of-arc",
1699 .vector_size = sizeof (u32),
1700
1701 .format_trace = format_ip6_forward_next_trace,
1702 .sibling_of = "ip6-local",
1703};
1704
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001705VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1706 .arc_name = "ip6-local",
1707 .node_name = "ip6-local-end-of-arc",
1708 .runs_before = 0, /* not before any other features */
1709};
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001710
Damjan Marion38173502019-02-13 19:30:09 +01001711#ifdef CLIB_MARCH_VARIANT
1712extern vlib_node_registration_t ip6_local_node;
Damjan Marion38173502019-02-13 19:30:09 +01001713#else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001714void
1715ip6_register_protocol (u32 protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001716{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001717 vlib_main_t *vm = vlib_get_main ();
1718 ip6_main_t *im = &ip6_main;
1719 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001720
1721 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001722 lm->local_next_by_ip_protocol[protocol] =
1723 vlib_node_add_next (vm, ip6_local_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001724}
1725
Neale Rannsb538dd82019-05-21 06:54:54 -07001726void
1727ip6_unregister_protocol (u32 protocol)
1728{
1729 ip6_main_t *im = &ip6_main;
1730 ip_lookup_main_t *lm = &im->lookup_main;
1731
1732 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1733 lm->local_next_by_ip_protocol[protocol] = IP_LOCAL_NEXT_PUNT;
1734}
Damjan Marion38173502019-02-13 19:30:09 +01001735#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07001736
Dave Barachd7cb1b52016-12-09 09:52:16 -05001737typedef enum
1738{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001739 IP6_REWRITE_NEXT_DROP,
Chris Luke816f3e12016-06-14 16:24:47 -04001740 IP6_REWRITE_NEXT_ICMP_ERROR,
Ole Troan313f7e22018-04-10 16:02:51 +02001741 IP6_REWRITE_NEXT_FRAGMENT,
1742 IP6_REWRITE_N_NEXT /* Last */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743} ip6_rewrite_next_t;
1744
Neale Ranns889fe942017-06-01 05:43:19 -04001745/**
1746 * This bits of an IPv6 address to mask to construct a multicast
1747 * MAC address
1748 */
1749#define IP6_MCAST_ADDR_MASK 0xffffffff
1750
Ole Troanda6e11b2018-05-23 11:21:42 +02001751always_inline void
1752ip6_mtu_check (vlib_buffer_t * b, u16 packet_bytes,
Ole Troan313f7e22018-04-10 16:02:51 +02001753 u16 adj_packet_bytes, bool is_locally_generated,
Ole Troaneb284a12019-10-09 13:33:19 +02001754 u32 * next, u8 is_midchain, u32 * error)
Ole Troanda6e11b2018-05-23 11:21:42 +02001755{
1756 if (adj_packet_bytes >= 1280 && packet_bytes > adj_packet_bytes)
1757 {
Ole Troan313f7e22018-04-10 16:02:51 +02001758 if (is_locally_generated)
1759 {
1760 /* IP fragmentation */
Ole Troan282093f2018-09-19 12:38:51 +02001761 ip_frag_set_vnet_buffer (b, adj_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02001762 (is_midchain ?
1763 IP_FRAG_NEXT_IP_REWRITE_MIDCHAIN :
1764 IP_FRAG_NEXT_IP_REWRITE), 0);
Ole Troan313f7e22018-04-10 16:02:51 +02001765 *next = IP6_REWRITE_NEXT_FRAGMENT;
Ole Troan282093f2018-09-19 12:38:51 +02001766 *error = IP6_ERROR_MTU_EXCEEDED;
Ole Troan313f7e22018-04-10 16:02:51 +02001767 }
1768 else
1769 {
1770 *error = IP6_ERROR_MTU_EXCEEDED;
1771 icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1772 adj_packet_bytes);
1773 *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1774 }
Ole Troanda6e11b2018-05-23 11:21:42 +02001775 }
1776}
1777
Ed Warnickecb9cada2015-12-08 15:45:58 -07001778always_inline uword
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001779ip6_rewrite_inline_with_gso (vlib_main_t * vm,
1780 vlib_node_runtime_t * node,
1781 vlib_frame_t * frame,
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01001782 int do_counters, int is_midchain, int is_mcast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001783{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001784 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1785 u32 *from = vlib_frame_vector_args (frame);
1786 u32 n_left_from, n_left_to_next, *to_next, next_index;
1787 vlib_node_runtime_t *error_node =
1788 vlib_node_get_runtime (vm, ip6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001789
1790 n_left_from = frame->n_vectors;
1791 next_index = node->cached_next_index;
Damjan Marion067cd622018-07-11 12:47:43 +02001792 u32 thread_index = vm->thread_index;
Dave Barach75fc8542016-10-11 16:16:02 -04001793
Ed Warnickecb9cada2015-12-08 15:45:58 -07001794 while (n_left_from > 0)
1795 {
1796 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1797
1798 while (n_left_from >= 4 && n_left_to_next >= 2)
1799 {
Neale Ranns960eeea2019-12-02 23:28:50 +00001800 const ip_adjacency_t *adj0, *adj1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001801 vlib_buffer_t *p0, *p1;
1802 ip6_header_t *ip0, *ip1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001803 u32 pi0, rw_len0, next0, error0, adj_index0;
1804 u32 pi1, rw_len1, next1, error1, adj_index1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001805 u32 tx_sw_if_index0, tx_sw_if_index1;
Ole Troan313f7e22018-04-10 16:02:51 +02001806 bool is_locally_originated0, is_locally_originated1;
Dave Barach75fc8542016-10-11 16:16:02 -04001807
Ed Warnickecb9cada2015-12-08 15:45:58 -07001808 /* Prefetch next iteration. */
1809 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001810 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001811
1812 p2 = vlib_get_buffer (vm, from[2]);
1813 p3 = vlib_get_buffer (vm, from[3]);
1814
1815 vlib_prefetch_buffer_header (p2, LOAD);
1816 vlib_prefetch_buffer_header (p3, LOAD);
1817
Damjan Marionaf7fb042021-07-15 11:54:41 +02001818 clib_prefetch_store (p2->pre_data);
1819 clib_prefetch_store (p3->pre_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001820
1821 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1822 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1823 }
1824
1825 pi0 = to_next[0] = from[0];
1826 pi1 = to_next[1] = from[1];
1827
1828 from += 2;
1829 n_left_from -= 2;
1830 to_next += 2;
1831 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04001832
Ed Warnickecb9cada2015-12-08 15:45:58 -07001833 p0 = vlib_get_buffer (vm, pi0);
1834 p1 = vlib_get_buffer (vm, pi1);
1835
Neale Rannsf06aea52016-11-29 06:51:37 -08001836 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1837 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001838
Ed Warnickecb9cada2015-12-08 15:45:58 -07001839 ip0 = vlib_buffer_get_current (p0);
1840 ip1 = vlib_buffer_get_current (p1);
1841
1842 error0 = error1 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001843 next0 = next1 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001844
Ole Troan313f7e22018-04-10 16:02:51 +02001845 is_locally_originated0 =
1846 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1847 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001848 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001849 i32 hop_limit0 = ip0->hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001850
1851 /* Input node should have reject packets with hop limit 0. */
1852 ASSERT (ip0->hop_limit > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001853
1854 hop_limit0 -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001855
1856 ip0->hop_limit = hop_limit0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001857
Dave Barachd7cb1b52016-12-09 09:52:16 -05001858 /*
1859 * If the hop count drops below 1 when forwarding, generate
1860 * an ICMP response.
1861 */
1862 if (PREDICT_FALSE (hop_limit0 <= 0))
1863 {
1864 error0 = IP6_ERROR_TIME_EXPIRED;
1865 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1866 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1867 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1868 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1869 0);
1870 }
Neale Rannsf06aea52016-11-29 06:51:37 -08001871 }
Neale Ranns88cecfa2020-04-08 08:28:06 -04001872
Ole Troan313f7e22018-04-10 16:02:51 +02001873 is_locally_originated1 =
1874 p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1875 if (PREDICT_TRUE (!is_locally_originated1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001876 {
Neale Rannsf06aea52016-11-29 06:51:37 -08001877 i32 hop_limit1 = ip1->hop_limit;
1878
1879 /* Input node should have reject packets with hop limit 0. */
1880 ASSERT (ip1->hop_limit > 0);
1881
1882 hop_limit1 -= 1;
1883
1884 ip1->hop_limit = hop_limit1;
1885
Dave Barachd7cb1b52016-12-09 09:52:16 -05001886 /*
1887 * If the hop count drops below 1 when forwarding, generate
1888 * an ICMP response.
1889 */
1890 if (PREDICT_FALSE (hop_limit1 <= 0))
1891 {
1892 error1 = IP6_ERROR_TIME_EXPIRED;
1893 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1894 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1895 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1896 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1897 0);
1898 }
1899 }
Neale Ranns88cecfa2020-04-08 08:28:06 -04001900
Neale Ranns107e7d42017-04-11 09:55:19 -07001901 adj0 = adj_get (adj_index0);
1902 adj1 = adj_get (adj_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001903
Ed Warnickecb9cada2015-12-08 15:45:58 -07001904 rw_len0 = adj0[0].rewrite_header.data_bytes;
1905 rw_len1 = adj1[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001906 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1907 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001908
Neale Ranns9c6a6132017-02-21 05:33:14 -08001909 if (do_counters)
1910 {
1911 vlib_increment_combined_counter
1912 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001913 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001914 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1915 vlib_increment_combined_counter
1916 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001917 thread_index, adj_index1, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001918 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1919 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920
1921 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001922 u16 ip0_len =
1923 clib_net_to_host_u16 (ip0->payload_length) +
1924 sizeof (ip6_header_t);
1925 u16 ip1_len =
1926 clib_net_to_host_u16 (ip1->payload_length) +
1927 sizeof (ip6_header_t);
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01001928 if (p0->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001929 ip0_len = gso_mtu_sz (p0);
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01001930 if (p1->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001931 ip1_len = gso_mtu_sz (p1);
1932
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001933 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001934 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02001935 is_locally_originated0, &next0, is_midchain,
1936 &error0);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02001937 ip6_mtu_check (p1, ip1_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02001938 adj1[0].rewrite_header.max_l3_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02001939 is_locally_originated1, &next1, is_midchain,
1940 &error1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001941 /* Don't adjust the buffer for hop count issue; icmp-error node
Jim Thompsonf324dec2019-04-08 03:22:21 -05001942 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001943 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1944 {
1945 p0->current_data -= rw_len0;
1946 p0->current_length += rw_len0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001947 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1948 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1949 next0 = adj0[0].rewrite_header.next_index;
Neale Rannsb069a692017-03-15 12:34:25 -04001950 if (PREDICT_FALSE
1951 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
Neale Ranns4ec36c52020-03-31 09:21:29 -04001952 vnet_feature_arc_start_w_cfg_index
1953 (lm->output_feature_arc_index, tx_sw_if_index0, &next0, p0,
1954 adj0->ia_cfg_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001955 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001956 else
1957 {
1958 p0->error = error_node->errors[error0];
1959 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001960 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1961 {
1962 p1->current_data -= rw_len1;
1963 p1->current_length += rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001964
Dave Barachd7cb1b52016-12-09 09:52:16 -05001965 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1966 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1967 next1 = adj1[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001968
Neale Rannsb069a692017-03-15 12:34:25 -04001969 if (PREDICT_FALSE
1970 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
Neale Ranns4ec36c52020-03-31 09:21:29 -04001971 vnet_feature_arc_start_w_cfg_index
1972 (lm->output_feature_arc_index, tx_sw_if_index1, &next1, p1,
1973 adj1->ia_cfg_index);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001974 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04001975 else
1976 {
1977 p1->error = error_node->errors[error1];
1978 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001979
Neale Ranns25edf142019-03-22 08:12:48 +00001980 if (is_midchain)
1981 {
1982 /* before we paint on the next header, update the L4
1983 * checksums if required, since there's no offload on a tunnel */
Dave Barach1bd2c012020-04-12 08:31:39 -04001984 vnet_calc_checksums_inline (vm, p0, 0 /* is_ip4 */ ,
Vladimir Isaev698eb872020-05-21 16:34:17 +03001985 1 /* is_ip6 */ );
Dave Barach1bd2c012020-04-12 08:31:39 -04001986 vnet_calc_checksums_inline (vm, p1, 0 /* is_ip4 */ ,
Vladimir Isaev698eb872020-05-21 16:34:17 +03001987 1 /* is_ip6 */ );
Neale Ranns25edf142019-03-22 08:12:48 +00001988
Neale Ranns4ec36c52020-03-31 09:21:29 -04001989 /* Guess we are only writing on ipv6 header. */
1990 vnet_rewrite_two_headers (adj0[0], adj1[0],
1991 ip0, ip1, sizeof (ip6_header_t));
1992 }
1993 else
1994 /* Guess we are only writing on simple Ethernet header. */
1995 vnet_rewrite_two_headers (adj0[0], adj1[0],
1996 ip0, ip1, sizeof (ethernet_header_t));
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001997
1998 if (is_midchain)
1999 {
Neale Ranns25edf142019-03-22 08:12:48 +00002000 if (adj0->sub_type.midchain.fixup_func)
2001 adj0->sub_type.midchain.fixup_func
2002 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2003 if (adj1->sub_type.midchain.fixup_func)
2004 adj1->sub_type.midchain.fixup_func
2005 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002006 }
2007 if (is_mcast)
2008 {
2009 /*
2010 * copy bytes from the IP address into the MAC rewrite
2011 */
Neale Ranns889fe942017-06-01 05:43:19 -04002012 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2013 adj0->
2014 rewrite_header.dst_mcast_offset,
2015 &ip0->dst_address.as_u32[3],
2016 (u8 *) ip0);
2017 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2018 adj1->
2019 rewrite_header.dst_mcast_offset,
2020 &ip1->dst_address.as_u32[3],
2021 (u8 *) ip1);
Neale Ranns32e1c012016-11-22 17:07:28 +00002022 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002023
Ed Warnickecb9cada2015-12-08 15:45:58 -07002024 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2025 to_next, n_left_to_next,
2026 pi0, pi1, next0, next1);
2027 }
2028
2029 while (n_left_from > 0 && n_left_to_next > 0)
2030 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002031 ip_adjacency_t *adj0;
2032 vlib_buffer_t *p0;
2033 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002034 u32 pi0, rw_len0;
2035 u32 adj_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002036 u32 tx_sw_if_index0;
Ole Troan313f7e22018-04-10 16:02:51 +02002037 bool is_locally_originated0;
Dave Barach75fc8542016-10-11 16:16:02 -04002038
Ed Warnickecb9cada2015-12-08 15:45:58 -07002039 pi0 = to_next[0] = from[0];
2040
2041 p0 = vlib_get_buffer (vm, pi0);
2042
Neale Rannsf06aea52016-11-29 06:51:37 -08002043 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002044
Neale Ranns107e7d42017-04-11 09:55:19 -07002045 adj0 = adj_get (adj_index0);
Dave Barach75fc8542016-10-11 16:16:02 -04002046
Ed Warnickecb9cada2015-12-08 15:45:58 -07002047 ip0 = vlib_buffer_get_current (p0);
2048
2049 error0 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002050 next0 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002051
2052 /* Check hop limit */
Ole Troan313f7e22018-04-10 16:02:51 +02002053 is_locally_originated0 =
2054 p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
2055 if (PREDICT_TRUE (!is_locally_originated0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002056 {
2057 i32 hop_limit0 = ip0->hop_limit;
2058
2059 ASSERT (ip0->hop_limit > 0);
2060
2061 hop_limit0 -= 1;
2062
2063 ip0->hop_limit = hop_limit0;
2064
Dave Barachd7cb1b52016-12-09 09:52:16 -05002065 if (PREDICT_FALSE (hop_limit0 <= 0))
2066 {
2067 /*
2068 * If the hop count drops below 1 when forwarding, generate
2069 * an ICMP response.
2070 */
2071 error0 = IP6_ERROR_TIME_EXPIRED;
2072 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2073 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2074 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2075 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2076 0);
2077 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002078 }
2079
Neale Ranns25edf142019-03-22 08:12:48 +00002080 if (is_midchain)
2081 {
Dave Barach1bd2c012020-04-12 08:31:39 -04002082 vnet_calc_checksums_inline (vm, p0, 0 /* is_ip4 */ ,
Vladimir Isaev698eb872020-05-21 16:34:17 +03002083 1 /* is_ip6 */ );
Neale Ranns25edf142019-03-22 08:12:48 +00002084
Neale Ranns4ec36c52020-03-31 09:21:29 -04002085 /* Guess we are only writing on ip6 header. */
2086 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ip6_header_t));
2087 }
2088 else
2089 /* Guess we are only writing on simple Ethernet header. */
2090 vnet_rewrite_one_header (adj0[0], ip0,
2091 sizeof (ethernet_header_t));
Dave Barach75fc8542016-10-11 16:16:02 -04002092
Ed Warnickecb9cada2015-12-08 15:45:58 -07002093 /* Update packet buffer attributes/set output interface. */
2094 rw_len0 = adj0[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002095 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002096
Neale Ranns9c6a6132017-02-21 05:33:14 -08002097 if (do_counters)
2098 {
2099 vlib_increment_combined_counter
2100 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02002101 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08002102 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2103 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002104
2105 /* Check MTU of outgoing interface. */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002106 u16 ip0_len =
2107 clib_net_to_host_u16 (ip0->payload_length) +
2108 sizeof (ip6_header_t);
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01002109 if (p0->flags & VNET_BUFFER_F_GSO)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002110 ip0_len = gso_mtu_sz (p0);
2111
2112 ip6_mtu_check (p0, ip0_len,
Ole Troanda6e11b2018-05-23 11:21:42 +02002113 adj0[0].rewrite_header.max_l3_packet_bytes,
Ole Troaneb284a12019-10-09 13:33:19 +02002114 is_locally_originated0, &next0, is_midchain,
2115 &error0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002116 /* Don't adjust the buffer for hop count issue; icmp-error node
Ole Troanda6e11b2018-05-23 11:21:42 +02002117 * wants to see the IP header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002118 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2119 {
Chris Luke816f3e12016-06-14 16:24:47 -04002120 p0->current_data -= rw_len0;
2121 p0->current_length += rw_len0;
2122
Dave Barachd7cb1b52016-12-09 09:52:16 -05002123 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
Dave Barach5331c722016-08-17 11:54:30 -04002124
Dave Barachd7cb1b52016-12-09 09:52:16 -05002125 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2126 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04002127
Neale Rannsb069a692017-03-15 12:34:25 -04002128 if (PREDICT_FALSE
2129 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
Neale Ranns4ec36c52020-03-31 09:21:29 -04002130 vnet_feature_arc_start_w_cfg_index
2131 (lm->output_feature_arc_index, tx_sw_if_index0, &next0, p0,
2132 adj0->ia_cfg_index);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002133 }
Kingwel Xiecb36a1d2019-03-20 03:45:47 -04002134 else
2135 {
2136 p0->error = error_node->errors[error0];
2137 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002138
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002139 if (is_midchain)
2140 {
Neale Ranns25edf142019-03-22 08:12:48 +00002141 if (adj0->sub_type.midchain.fixup_func)
2142 adj0->sub_type.midchain.fixup_func
2143 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002144 }
2145 if (is_mcast)
2146 {
Neale Ranns889fe942017-06-01 05:43:19 -04002147 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
2148 adj0->
2149 rewrite_header.dst_mcast_offset,
2150 &ip0->dst_address.as_u32[3],
2151 (u8 *) ip0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002152 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002153
Ed Warnickecb9cada2015-12-08 15:45:58 -07002154 from += 1;
2155 n_left_from -= 1;
2156 to_next += 1;
2157 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04002158
Ed Warnickecb9cada2015-12-08 15:45:58 -07002159 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2160 to_next, n_left_to_next,
2161 pi0, next0);
2162 }
2163
2164 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2165 }
2166
2167 /* Need to do trace after rewrites to pick up new packet data. */
2168 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Rannsf06aea52016-11-29 06:51:37 -08002169 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002170
2171 return frame->n_vectors;
2172}
2173
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002174always_inline uword
2175ip6_rewrite_inline (vlib_main_t * vm,
2176 vlib_node_runtime_t * node,
2177 vlib_frame_t * frame,
2178 int do_counters, int is_midchain, int is_mcast)
2179{
Mohsin Kazmi3f5594d2019-12-03 18:56:26 +01002180 return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2181 is_midchain, is_mcast);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +02002182}
2183
Damjan Marion38173502019-02-13 19:30:09 +01002184VLIB_NODE_FN (ip6_rewrite_node) (vlib_main_t * vm,
2185 vlib_node_runtime_t * node,
2186 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002187{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002188 if (adj_are_counters_enabled ())
2189 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2190 else
2191 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
Neale Ranns32e1c012016-11-22 17:07:28 +00002192}
2193
Damjan Marion38173502019-02-13 19:30:09 +01002194VLIB_NODE_FN (ip6_rewrite_bcast_node) (vlib_main_t * vm,
2195 vlib_node_runtime_t * node,
2196 vlib_frame_t * frame)
Neale Ranns1855b8e2018-07-11 10:31:26 -07002197{
2198 if (adj_are_counters_enabled ())
2199 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2200 else
2201 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2202}
2203
Damjan Marion38173502019-02-13 19:30:09 +01002204VLIB_NODE_FN (ip6_rewrite_mcast_node) (vlib_main_t * vm,
2205 vlib_node_runtime_t * node,
2206 vlib_frame_t * frame)
Neale Ranns32e1c012016-11-22 17:07:28 +00002207{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002208 if (adj_are_counters_enabled ())
2209 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2210 else
2211 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002212}
2213
Damjan Marion38173502019-02-13 19:30:09 +01002214VLIB_NODE_FN (ip6_midchain_node) (vlib_main_t * vm,
2215 vlib_node_runtime_t * node,
2216 vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002217{
Neale Ranns9c6a6132017-02-21 05:33:14 -08002218 if (adj_are_counters_enabled ())
2219 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2220 else
2221 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002222}
2223
Damjan Marion38173502019-02-13 19:30:09 +01002224VLIB_NODE_FN (ip6_mcast_midchain_node) (vlib_main_t * vm,
2225 vlib_node_runtime_t * node,
2226 vlib_frame_t * frame)
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002227{
2228 if (adj_are_counters_enabled ())
2229 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2230 else
Neale Ranns9f171f52017-04-11 08:56:53 -07002231 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002232}
2233
Dave Barachd7cb1b52016-12-09 09:52:16 -05002234/* *INDENT-OFF* */
2235VLIB_REGISTER_NODE (ip6_midchain_node) =
2236{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002237 .name = "ip6-midchain",
2238 .vector_size = sizeof (u32),
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002239 .format_trace = format_ip6_forward_next_trace,
Neale Ranns5e575b12016-10-03 09:40:25 +01002240 .sibling_of = "ip6-rewrite",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002241 };
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002242
Dave Barachd7cb1b52016-12-09 09:52:16 -05002243VLIB_REGISTER_NODE (ip6_rewrite_node) =
2244{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002245 .name = "ip6-rewrite",
2246 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +01002247 .format_trace = format_ip6_rewrite_trace,
Ole Troan313f7e22018-04-10 16:02:51 +02002248 .n_next_nodes = IP6_REWRITE_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002249 .next_nodes =
2250 {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002251 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
Chris Luke816f3e12016-06-14 16:24:47 -04002252 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Ole Troan313f7e22018-04-10 16:02:51 +02002253 [IP6_REWRITE_NEXT_FRAGMENT] = "ip6-frag",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002254 },
2255};
2256
Neale Ranns1855b8e2018-07-11 10:31:26 -07002257VLIB_REGISTER_NODE (ip6_rewrite_bcast_node) = {
Neale Ranns1855b8e2018-07-11 10:31:26 -07002258 .name = "ip6-rewrite-bcast",
2259 .vector_size = sizeof (u32),
2260
2261 .format_trace = format_ip6_rewrite_trace,
2262 .sibling_of = "ip6-rewrite",
2263};
Neale Ranns1855b8e2018-07-11 10:31:26 -07002264
Neale Ranns32e1c012016-11-22 17:07:28 +00002265VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2266{
Neale Ranns32e1c012016-11-22 17:07:28 +00002267 .name = "ip6-rewrite-mcast",
2268 .vector_size = sizeof (u32),
2269 .format_trace = format_ip6_rewrite_trace,
2270 .sibling_of = "ip6-rewrite",
2271};
Neale Ranns32e1c012016-11-22 17:07:28 +00002272
Neale Ranns32e1c012016-11-22 17:07:28 +00002273
Damjan Marion38173502019-02-13 19:30:09 +01002274VLIB_REGISTER_NODE (ip6_mcast_midchain_node) =
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002275{
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002276 .name = "ip6-mcast-midchain",
2277 .vector_size = sizeof (u32),
2278 .format_trace = format_ip6_rewrite_trace,
2279 .sibling_of = "ip6-rewrite",
2280};
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002281
Neale Ranns1855b8e2018-07-11 10:31:26 -07002282/* *INDENT-ON* */
Neale Ranns0f26c5a2017-03-01 15:12:11 -08002283
Ole Troan944f5482016-05-24 11:56:58 +02002284/*
2285 * Hop-by-Hop handling
2286 */
Benoît Ganne47727c02019-02-12 13:35:08 +01002287#ifndef CLIB_MARCH_VARIANT
Ole Troan944f5482016-05-24 11:56:58 +02002288ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
Benoît Ganne47727c02019-02-12 13:35:08 +01002289#endif /* CLIB_MARCH_VARIANT */
Ole Troan944f5482016-05-24 11:56:58 +02002290
2291#define foreach_ip6_hop_by_hop_error \
2292_(PROCESSED, "pkts with ip6 hop-by-hop options") \
2293_(FORMAT, "incorrectly formatted hop-by-hop options") \
2294_(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2295
Neale Ranns32e1c012016-11-22 17:07:28 +00002296/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002297typedef enum
2298{
Ole Troan944f5482016-05-24 11:56:58 +02002299#define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2300 foreach_ip6_hop_by_hop_error
2301#undef _
Neale Ranns32e1c012016-11-22 17:07:28 +00002302 IP6_HOP_BY_HOP_N_ERROR,
Ole Troan944f5482016-05-24 11:56:58 +02002303} ip6_hop_by_hop_error_t;
Neale Ranns32e1c012016-11-22 17:07:28 +00002304/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002305
2306/*
2307 * Primary h-b-h handler trace support
2308 * We work pretty hard on the problem for obvious reasons
2309 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002310typedef struct
2311{
Ole Troan944f5482016-05-24 11:56:58 +02002312 u32 next_index;
2313 u32 trace_len;
2314 u8 option_data[256];
2315} ip6_hop_by_hop_trace_t;
2316
Benoît Ganne47727c02019-02-12 13:35:08 +01002317extern vlib_node_registration_t ip6_hop_by_hop_node;
Ole Troan944f5482016-05-24 11:56:58 +02002318
Dave Barachd7cb1b52016-12-09 09:52:16 -05002319static char *ip6_hop_by_hop_error_strings[] = {
Ole Troan944f5482016-05-24 11:56:58 +02002320#define _(sym,string) string,
2321 foreach_ip6_hop_by_hop_error
2322#undef _
2323};
2324
Damjan Marion38173502019-02-13 19:30:09 +01002325#ifndef CLIB_MARCH_VARIANT
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302326u8 *
2327format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2328{
2329 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2330 int total_len = va_arg (*args, int);
2331 ip6_hop_by_hop_option_t *opt0, *limit0;
2332 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2333 u8 type0;
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302334 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2335 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2336
2337 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2338 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2339
2340 while (opt0 < limit0)
2341 {
2342 type0 = opt0->type;
2343 switch (type0)
2344 {
2345 case 0: /* Pad, just stop */
2346 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2347 break;
2348
2349 default:
2350 if (hm->trace[type0])
2351 {
2352 s = (*hm->trace[type0]) (s, opt0);
2353 }
2354 else
2355 {
Mohsin Kazmi68095382021-02-10 11:26:24 +01002356 s = format (s, "\n unrecognized option %d length %d", type0,
2357 opt0->length);
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302358 }
2359 opt0 =
2360 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2361 sizeof (ip6_hop_by_hop_option_t));
2362 break;
2363 }
2364 }
2365 return s;
2366}
Damjan Marion38173502019-02-13 19:30:09 +01002367#endif
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302368
Ole Troan944f5482016-05-24 11:56:58 +02002369static u8 *
2370format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2371{
2372 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2373 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002374 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
Ole Troan944f5482016-05-24 11:56:58 +02002375 ip6_hop_by_hop_header_t *hbh0;
2376 ip6_hop_by_hop_option_t *opt0, *limit0;
2377 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2378
2379 u8 type0;
2380
Dave Barachd7cb1b52016-12-09 09:52:16 -05002381 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
Ole Troan944f5482016-05-24 11:56:58 +02002382
2383 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002384 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
Ole Troan944f5482016-05-24 11:56:58 +02002385
Dave Barachd7cb1b52016-12-09 09:52:16 -05002386 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2387 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
Ole Troan944f5482016-05-24 11:56:58 +02002388
Dave Barachd7cb1b52016-12-09 09:52:16 -05002389 while (opt0 < limit0)
2390 {
2391 type0 = opt0->type;
2392 switch (type0)
2393 {
2394 case 0: /* Pad, just stop */
2395 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2396 break;
Ole Troan944f5482016-05-24 11:56:58 +02002397
Dave Barachd7cb1b52016-12-09 09:52:16 -05002398 default:
2399 if (hm->trace[type0])
2400 {
2401 s = (*hm->trace[type0]) (s, opt0);
2402 }
2403 else
2404 {
Mohsin Kazmi68095382021-02-10 11:26:24 +01002405 s = format (s, "\n unrecognized option %d length %d", type0,
2406 opt0->length);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002407 }
2408 opt0 =
2409 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2410 sizeof (ip6_hop_by_hop_option_t));
2411 break;
2412 }
Ole Troan944f5482016-05-24 11:56:58 +02002413 }
Ole Troan944f5482016-05-24 11:56:58 +02002414 return s;
2415}
2416
Dave Barachd7cb1b52016-12-09 09:52:16 -05002417always_inline u8
2418ip6_scan_hbh_options (vlib_buffer_t * b0,
2419 ip6_header_t * ip0,
2420 ip6_hop_by_hop_header_t * hbh0,
2421 ip6_hop_by_hop_option_t * opt0,
2422 ip6_hop_by_hop_option_t * limit0, u32 * next0)
Shwethaa91cbe62016-08-08 15:51:04 +01002423{
2424 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2425 u8 type0;
2426 u8 error0 = 0;
2427
2428 while (opt0 < limit0)
2429 {
2430 type0 = opt0->type;
2431 switch (type0)
2432 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002433 case 0: /* Pad1 */
2434 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
Shwethaa91cbe62016-08-08 15:51:04 +01002435 continue;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002436 case 1: /* PadN */
Shwethaa91cbe62016-08-08 15:51:04 +01002437 break;
2438 default:
2439 if (hm->options[type0])
2440 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002441 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2442 {
Shwethaa91cbe62016-08-08 15:51:04 +01002443 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002444 return (error0);
2445 }
Shwethaa91cbe62016-08-08 15:51:04 +01002446 }
2447 else
2448 {
2449 /* Unrecognized mandatory option, check the two high order bits */
2450 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2451 {
2452 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2453 break;
2454 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2455 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2456 *next0 = IP_LOOKUP_NEXT_DROP;
2457 break;
2458 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2459 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2460 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002461 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2462 ICMP6_parameter_problem_unrecognized_option,
2463 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002464 break;
2465 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2466 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002467 if (!ip6_address_is_multicast (&ip0->dst_address))
Shwethaa91cbe62016-08-08 15:51:04 +01002468 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002469 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2470 icmp6_error_set_vnet_buffer (b0,
2471 ICMP6_parameter_problem,
2472 ICMP6_parameter_problem_unrecognized_option,
2473 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002474 }
2475 else
2476 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002477 *next0 = IP_LOOKUP_NEXT_DROP;
Shwethaa91cbe62016-08-08 15:51:04 +01002478 }
2479 break;
2480 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002481 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002482 }
2483 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002484 opt0 =
2485 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2486 sizeof (ip6_hop_by_hop_option_t));
Shwethaa91cbe62016-08-08 15:51:04 +01002487 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002488 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002489}
2490
Ole Troan944f5482016-05-24 11:56:58 +02002491/*
2492 * Process the Hop-by-Hop Options header
2493 */
Damjan Marion38173502019-02-13 19:30:09 +01002494VLIB_NODE_FN (ip6_hop_by_hop_node) (vlib_main_t * vm,
2495 vlib_node_runtime_t * node,
2496 vlib_frame_t * frame)
Ole Troan944f5482016-05-24 11:56:58 +02002497{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002498 vlib_node_runtime_t *error_node =
2499 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
Ole Troan944f5482016-05-24 11:56:58 +02002500 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2501 u32 n_left_from, *from, *to_next;
2502 ip_lookup_next_t next_index;
Ole Troan944f5482016-05-24 11:56:58 +02002503
2504 from = vlib_frame_vector_args (frame);
2505 n_left_from = frame->n_vectors;
2506 next_index = node->cached_next_index;
2507
Dave Barachd7cb1b52016-12-09 09:52:16 -05002508 while (n_left_from > 0)
2509 {
2510 u32 n_left_to_next;
Ole Troan944f5482016-05-24 11:56:58 +02002511
Dave Barachd7cb1b52016-12-09 09:52:16 -05002512 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ole Troan944f5482016-05-24 11:56:58 +02002513
Dave Barachd7cb1b52016-12-09 09:52:16 -05002514 while (n_left_from >= 4 && n_left_to_next >= 2)
Shwethaa91cbe62016-08-08 15:51:04 +01002515 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002516 u32 bi0, bi1;
2517 vlib_buffer_t *b0, *b1;
2518 u32 next0, next1;
2519 ip6_header_t *ip0, *ip1;
2520 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2521 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2522 u8 error0 = 0, error1 = 0;
2523
2524 /* Prefetch next iteration. */
2525 {
2526 vlib_buffer_t *p2, *p3;
2527
2528 p2 = vlib_get_buffer (vm, from[2]);
2529 p3 = vlib_get_buffer (vm, from[3]);
2530
2531 vlib_prefetch_buffer_header (p2, LOAD);
2532 vlib_prefetch_buffer_header (p3, LOAD);
2533
2534 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2535 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Shwethaa91cbe62016-08-08 15:51:04 +01002536 }
2537
Dave Barachd7cb1b52016-12-09 09:52:16 -05002538 /* Speculatively enqueue b0, b1 to the current next frame */
2539 to_next[0] = bi0 = from[0];
2540 to_next[1] = bi1 = from[1];
2541 from += 2;
2542 to_next += 2;
2543 n_left_from -= 2;
2544 n_left_to_next -= 2;
2545
2546 b0 = vlib_get_buffer (vm, bi0);
2547 b1 = vlib_get_buffer (vm, bi1);
2548
2549 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2550 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002551 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002552 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002553 ip_adjacency_t *adj1 = adj_get (adj_index1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002554
2555 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2556 next0 = adj0->lookup_next_index;
2557 next1 = adj1->lookup_next_index;
2558
2559 ip0 = vlib_buffer_get_current (b0);
2560 ip1 = vlib_buffer_get_current (b1);
2561 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2562 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2563 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2564 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2565 limit0 =
2566 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2567 ((hbh0->length + 1) << 3));
2568 limit1 =
2569 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2570 ((hbh1->length + 1) << 3));
2571
2572 /*
2573 * Basic validity checks
2574 */
2575 if ((hbh0->length + 1) << 3 >
2576 clib_net_to_host_u16 (ip0->payload_length))
2577 {
2578 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2579 next0 = IP_LOOKUP_NEXT_DROP;
2580 goto outdual;
2581 }
2582 /* Scan the set of h-b-h options, process ones that we understand */
2583 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2584
2585 if ((hbh1->length + 1) << 3 >
2586 clib_net_to_host_u16 (ip1->payload_length))
2587 {
2588 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2589 next1 = IP_LOOKUP_NEXT_DROP;
2590 goto outdual;
2591 }
2592 /* Scan the set of h-b-h options, process ones that we understand */
2593 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2594
2595 outdual:
2596 /* Has the classifier flagged this buffer for special treatment? */
2597 if (PREDICT_FALSE
2598 ((error0 == 0)
2599 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2600 next0 = hm->next_override;
2601
2602 /* Has the classifier flagged this buffer for special treatment? */
2603 if (PREDICT_FALSE
2604 ((error1 == 0)
2605 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2606 next1 = hm->next_override;
2607
2608 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2609 {
2610 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2611 {
2612 ip6_hop_by_hop_trace_t *t =
2613 vlib_add_trace (vm, node, b0, sizeof (*t));
2614 u32 trace_len = (hbh0->length + 1) << 3;
2615 t->next_index = next0;
2616 /* Capture the h-b-h option verbatim */
2617 trace_len =
2618 trace_len <
2619 ARRAY_LEN (t->option_data) ? trace_len :
2620 ARRAY_LEN (t->option_data);
2621 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002622 clib_memcpy_fast (t->option_data, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002623 }
2624 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2625 {
2626 ip6_hop_by_hop_trace_t *t =
2627 vlib_add_trace (vm, node, b1, sizeof (*t));
2628 u32 trace_len = (hbh1->length + 1) << 3;
2629 t->next_index = next1;
2630 /* Capture the h-b-h option verbatim */
2631 trace_len =
2632 trace_len <
2633 ARRAY_LEN (t->option_data) ? trace_len :
2634 ARRAY_LEN (t->option_data);
2635 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002636 clib_memcpy_fast (t->option_data, hbh1, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002637 }
2638
2639 }
2640
2641 b0->error = error_node->errors[error0];
2642 b1->error = error_node->errors[error1];
2643
2644 /* verify speculative enqueue, maybe switch current next frame */
2645 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2646 n_left_to_next, bi0, bi1, next0,
2647 next1);
Shwethaa91cbe62016-08-08 15:51:04 +01002648 }
2649
Dave Barachd7cb1b52016-12-09 09:52:16 -05002650 while (n_left_from > 0 && n_left_to_next > 0)
2651 {
2652 u32 bi0;
2653 vlib_buffer_t *b0;
2654 u32 next0;
2655 ip6_header_t *ip0;
2656 ip6_hop_by_hop_header_t *hbh0;
2657 ip6_hop_by_hop_option_t *opt0, *limit0;
2658 u8 error0 = 0;
Shwethaa91cbe62016-08-08 15:51:04 +01002659
Dave Barachd7cb1b52016-12-09 09:52:16 -05002660 /* Speculatively enqueue b0 to the current next frame */
2661 bi0 = from[0];
2662 to_next[0] = bi0;
2663 from += 1;
2664 to_next += 1;
2665 n_left_from -= 1;
2666 n_left_to_next -= 1;
2667
2668 b0 = vlib_get_buffer (vm, bi0);
2669 /*
2670 * Default use the next_index from the adjacency.
2671 * A HBH option rarely redirects to a different node
2672 */
2673 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002674 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002675 next0 = adj0->lookup_next_index;
2676
2677 ip0 = vlib_buffer_get_current (b0);
2678 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2679 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2680 limit0 =
2681 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2682 ((hbh0->length + 1) << 3));
2683
2684 /*
2685 * Basic validity checks
2686 */
2687 if ((hbh0->length + 1) << 3 >
2688 clib_net_to_host_u16 (ip0->payload_length))
2689 {
2690 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2691 next0 = IP_LOOKUP_NEXT_DROP;
2692 goto out0;
2693 }
2694
2695 /* Scan the set of h-b-h options, process ones that we understand */
2696 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2697
2698 out0:
2699 /* Has the classifier flagged this buffer for special treatment? */
2700 if (PREDICT_FALSE
2701 ((error0 == 0)
2702 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2703 next0 = hm->next_override;
2704
2705 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2706 {
2707 ip6_hop_by_hop_trace_t *t =
2708 vlib_add_trace (vm, node, b0, sizeof (*t));
2709 u32 trace_len = (hbh0->length + 1) << 3;
2710 t->next_index = next0;
2711 /* Capture the h-b-h option verbatim */
2712 trace_len =
2713 trace_len <
2714 ARRAY_LEN (t->option_data) ? trace_len :
2715 ARRAY_LEN (t->option_data);
2716 t->trace_len = trace_len;
Dave Barach178cf492018-11-13 16:34:13 -05002717 clib_memcpy_fast (t->option_data, hbh0, trace_len);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002718 }
2719
2720 b0->error = error_node->errors[error0];
2721
2722 /* verify speculative enqueue, maybe switch current next frame */
2723 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2724 n_left_to_next, bi0, next0);
2725 }
2726 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Shwethaa91cbe62016-08-08 15:51:04 +01002727 }
Ole Troan944f5482016-05-24 11:56:58 +02002728 return frame->n_vectors;
2729}
2730
Dave Barachd7cb1b52016-12-09 09:52:16 -05002731/* *INDENT-OFF* */
2732VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2733{
Ole Troan944f5482016-05-24 11:56:58 +02002734 .name = "ip6-hop-by-hop",
Ole Troan964f93e2016-06-10 13:22:36 +02002735 .sibling_of = "ip6-lookup",
Ole Troan944f5482016-05-24 11:56:58 +02002736 .vector_size = sizeof (u32),
2737 .format_trace = format_ip6_hop_by_hop_trace,
2738 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002739 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
Ole Troan944f5482016-05-24 11:56:58 +02002740 .error_strings = ip6_hop_by_hop_error_strings,
Ole Troan964f93e2016-06-10 13:22:36 +02002741 .n_next_nodes = 0,
Ole Troan944f5482016-05-24 11:56:58 +02002742};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002743/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002744
Ole Troan944f5482016-05-24 11:56:58 +02002745static clib_error_t *
2746ip6_hop_by_hop_init (vlib_main_t * vm)
2747{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002748 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Dave Barachb7b92992018-10-17 10:38:51 -04002749 clib_memset (hm->options, 0, sizeof (hm->options));
2750 clib_memset (hm->trace, 0, sizeof (hm->trace));
Shwethaa91cbe62016-08-08 15:51:04 +01002751 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
Ole Troan944f5482016-05-24 11:56:58 +02002752 return (0);
2753}
2754
2755VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2756
Damjan Marion38173502019-02-13 19:30:09 +01002757#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05002758void
2759ip6_hbh_set_next_override (uword next)
Shwethaa91cbe62016-08-08 15:51:04 +01002760{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002761 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Shwethaa91cbe62016-08-08 15:51:04 +01002762
2763 hm->next_override = next;
2764}
2765
Ole Troan944f5482016-05-24 11:56:58 +02002766int
2767ip6_hbh_register_option (u8 option,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002768 int options (vlib_buffer_t * b, ip6_header_t * ip,
2769 ip6_hop_by_hop_option_t * opt),
2770 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
Ole Troan944f5482016-05-24 11:56:58 +02002771{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002772 ip6_main_t *im = &ip6_main;
2773 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002774
Neale Ranns756cd942018-04-06 09:18:11 -07002775 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002776
2777 /* Already registered */
2778 if (hm->options[option])
2779 return (-1);
2780
2781 hm->options[option] = options;
2782 hm->trace[option] = trace;
2783
2784 /* Set global variable */
2785 im->hbh_enabled = 1;
2786
2787 return (0);
2788}
2789
2790int
2791ip6_hbh_unregister_option (u8 option)
2792{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002793 ip6_main_t *im = &ip6_main;
2794 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002795
Neale Ranns756cd942018-04-06 09:18:11 -07002796 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002797
2798 /* Not registered */
2799 if (!hm->options[option])
2800 return (-1);
2801
2802 hm->options[option] = NULL;
2803 hm->trace[option] = NULL;
2804
2805 /* Disable global knob if this was the last option configured */
2806 int i;
2807 bool found = false;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002808 for (i = 0; i < 256; i++)
2809 {
2810 if (hm->options[option])
2811 {
2812 found = true;
2813 break;
2814 }
Ole Troan944f5482016-05-24 11:56:58 +02002815 }
Ole Troan944f5482016-05-24 11:56:58 +02002816 if (!found)
2817 im->hbh_enabled = 0;
2818
2819 return (0);
2820}
2821
Ed Warnickecb9cada2015-12-08 15:45:58 -07002822/* Global IP6 main. */
2823ip6_main_t ip6_main;
Damjan Marion38173502019-02-13 19:30:09 +01002824#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07002825
2826static clib_error_t *
2827ip6_lookup_init (vlib_main_t * vm)
2828{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002829 ip6_main_t *im = &ip6_main;
2830 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831 uword i;
2832
Damjan Marion8b3191e2016-11-09 19:54:20 +01002833 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2834 return error;
2835
Ed Warnickecb9cada2015-12-08 15:45:58 -07002836 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2837 {
2838 u32 j, i0, i1;
2839
2840 i0 = i / 32;
2841 i1 = i % 32;
2842
2843 for (j = 0; j < i0; j++)
2844 im->fib_masks[i].as_u32[j] = ~0;
2845
2846 if (i1)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002847 im->fib_masks[i].as_u32[i0] =
2848 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002849 }
2850
2851 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2852
Ed Warnickecb9cada2015-12-08 15:45:58 -07002853 /* Create FIB with index 0 and table id of 0. */
Neale Ranns15002542017-09-10 04:39:11 -07002854 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2855 FIB_SOURCE_DEFAULT_ROUTE);
2856 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2857 MFIB_SOURCE_DEFAULT_ROUTE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002858
2859 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002860 pg_node_t *pn;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002861 pn = pg_get_node (ip6_lookup_node.index);
2862 pn->unformat_edit = unformat_pg_ip6_header;
2863 }
2864
Ole Troan944f5482016-05-24 11:56:58 +02002865 /* Unless explicitly configured, don't process HBH options */
2866 im->hbh_enabled = 0;
2867
Dave Barach203c6322016-06-26 10:29:03 -04002868 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002869}
2870
2871VLIB_INIT_FUNCTION (ip6_lookup_init);
2872
Ed Warnickecb9cada2015-12-08 15:45:58 -07002873static clib_error_t *
2874set_ip6_flow_hash_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002875 unformat_input_t * input,
2876 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002877{
2878 int matched = 0;
2879 u32 table_id = 0;
2880 u32 flow_hash_config = 0;
2881 int rv;
2882
Dave Barachd7cb1b52016-12-09 09:52:16 -05002883 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2884 {
2885 if (unformat (input, "table %d", &table_id))
2886 matched = 1;
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00002887#define _(a, b, v) \
2888 else if (unformat (input, #a)) \
2889 { \
2890 flow_hash_config |= v; \
2891 matched = 1; \
2892 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002893 foreach_flow_hash_bit
Ed Warnickecb9cada2015-12-08 15:45:58 -07002894#undef _
Dave Barachd7cb1b52016-12-09 09:52:16 -05002895 else
2896 break;
2897 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002898
2899 if (matched == 0)
2900 return clib_error_return (0, "unknown input `%U'",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002901 format_unformat_error, input);
Dave Barach75fc8542016-10-11 16:16:02 -04002902
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00002903 rv = ip_flow_hash_set (AF_IP6, table_id, flow_hash_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002904 switch (rv)
2905 {
Neale Ranns227038a2017-04-21 01:07:59 -07002906 case 0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002907 break;
2908
2909 case -1:
2910 return clib_error_return (0, "no such FIB table %d", table_id);
Dave Barach75fc8542016-10-11 16:16:02 -04002911
Ed Warnickecb9cada2015-12-08 15:45:58 -07002912 default:
2913 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2914 break;
2915 }
Dave Barach75fc8542016-10-11 16:16:02 -04002916
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917 return 0;
2918}
2919
Billy McFall0683c9c2016-10-13 08:27:31 -04002920/*?
2921 * Configure the set of IPv6 fields used by the flow hash.
2922 *
2923 * @cliexpar
2924 * @parblock
2925 * Example of how to set the flow hash on a given table:
Billy McFallebb9a6a2016-10-17 11:35:32 -04002926 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2927 *
Billy McFall0683c9c2016-10-13 08:27:31 -04002928 * Example of display the configured flow hash:
2929 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -04002930 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2931 * @::/0
2932 * unicast-ip6-chain
2933 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2934 * [0] [@0]: dpo-drop ip6
2935 * fe80::/10
2936 * unicast-ip6-chain
2937 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2938 * [0] [@2]: dpo-receive
2939 * ff02::1/128
2940 * unicast-ip6-chain
2941 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2942 * [0] [@2]: dpo-receive
2943 * ff02::2/128
2944 * unicast-ip6-chain
2945 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2946 * [0] [@2]: dpo-receive
2947 * ff02::16/128
2948 * unicast-ip6-chain
2949 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2950 * [0] [@2]: dpo-receive
2951 * ff02::1:ff00:0/104
2952 * unicast-ip6-chain
2953 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
2954 * [0] [@2]: dpo-receive
2955 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
2956 * @::/0
2957 * unicast-ip6-chain
2958 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
2959 * [0] [@0]: dpo-drop ip6
2960 * @::a:1:1:0:4/126
2961 * unicast-ip6-chain
2962 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
2963 * [0] [@4]: ipv6-glean: af_packet0
2964 * @::a:1:1:0:7/128
2965 * unicast-ip6-chain
2966 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
2967 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
2968 * fe80::/10
2969 * unicast-ip6-chain
2970 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
2971 * [0] [@2]: dpo-receive
2972 * fe80::fe:3eff:fe3e:9222/128
2973 * unicast-ip6-chain
2974 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
2975 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
2976 * ff02::1/128
2977 * unicast-ip6-chain
2978 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
2979 * [0] [@2]: dpo-receive
2980 * ff02::2/128
2981 * unicast-ip6-chain
2982 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
2983 * [0] [@2]: dpo-receive
2984 * ff02::16/128
2985 * unicast-ip6-chain
2986 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
2987 * [0] [@2]: dpo-receive
2988 * ff02::1:ff00:0/104
2989 * unicast-ip6-chain
2990 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
2991 * [0] [@2]: dpo-receive
Billy McFall0683c9c2016-10-13 08:27:31 -04002992 * @cliexend
2993 * @endparblock
2994?*/
2995/* *INDENT-OFF* */
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00002996VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002997 .path = "set ip6 flow-hash",
Ahmed Abdelsalamf2984bb2020-11-20 18:56:09 +00002998 .short_help = "set ip6 flow-hash table <table-id> [src] [dst] [sport] "
2999 "[dport] [proto] [reverse] [flowlabel]",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003000 .function = set_ip6_flow_hash_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003001};
Billy McFall0683c9c2016-10-13 08:27:31 -04003002/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003003
3004static clib_error_t *
3005show_ip6_local_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003006 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003007{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003008 ip6_main_t *im = &ip6_main;
3009 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003010 int i;
Dave Barach75fc8542016-10-11 16:16:02 -04003011
Ed Warnickecb9cada2015-12-08 15:45:58 -07003012 vlib_cli_output (vm, "Protocols handled by ip6_local");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003013 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003014 {
3015 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02003016 {
3017
3018 u32 node_index = vlib_get_node (vm,
3019 ip6_local_node.index)->
3020 next_nodes[lm->local_next_by_ip_protocol[i]];
3021 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3022 node_index);
3023 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003024 }
3025 return 0;
3026}
3027
3028
3029
Billy McFall0683c9c2016-10-13 08:27:31 -04003030/*?
3031 * Display the set of protocols handled by the local IPv6 stack.
3032 *
3033 * @cliexpar
3034 * Example of how to display local protocol table:
3035 * @cliexstart{show ip6 local}
3036 * Protocols handled by ip6_local
3037 * 17
3038 * 43
3039 * 58
3040 * 115
3041 * @cliexend
3042?*/
3043/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003044VLIB_CLI_COMMAND (show_ip6_local, static) =
3045{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003046 .path = "show ip6 local",
3047 .function = show_ip6_local_command_fn,
Billy McFall0683c9c2016-10-13 08:27:31 -04003048 .short_help = "show ip6 local",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003049};
Billy McFall0683c9c2016-10-13 08:27:31 -04003050/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003051
Damjan Marion38173502019-02-13 19:30:09 +01003052#ifndef CLIB_MARCH_VARIANT
Dave Barachd7cb1b52016-12-09 09:52:16 -05003053int
Mohsin Kazmi68095382021-02-10 11:26:24 +01003054vnet_set_ip6_classify_intfc (vlib_main_t *vm, u32 sw_if_index, u32 table_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003055{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003056 vnet_main_t *vnm = vnet_get_main ();
3057 vnet_interface_main_t *im = &vnm->interface_main;
3058 ip6_main_t *ipm = &ip6_main;
3059 ip_lookup_main_t *lm = &ipm->lookup_main;
3060 vnet_classify_main_t *cm = &vnet_classify_main;
Neale Rannsdf089a82016-10-02 16:39:06 +01003061 ip6_address_t *if_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062
3063 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3064 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3065
3066 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3067 return VNET_API_ERROR_NO_SUCH_ENTRY;
3068
3069 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003070 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003071
Neale Ranns6cfc39c2017-02-14 01:44:25 -08003072 if_addr = ip6_interface_first_address (ipm, sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01003073
3074 if (NULL != if_addr)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003075 {
Neale Rannsdf089a82016-10-02 16:39:06 +01003076 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003077 .fp_len = 128,
3078 .fp_proto = FIB_PROTOCOL_IP6,
3079 .fp_addr.ip6 = *if_addr,
Neale Rannsdf089a82016-10-02 16:39:06 +01003080 };
3081 u32 fib_index;
3082
Dave Barachd7cb1b52016-12-09 09:52:16 -05003083 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3084 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003085 if (table_index != (u32) ~ 0)
3086 {
3087 dpo_id_t dpo = DPO_INVALID;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003088 dpo_set (&dpo,
3089 DPO_CLASSIFY,
3090 DPO_PROTO_IP6,
3091 classify_dpo_create (DPO_PROTO_IP6, table_index));
Dave Barachd7cb1b52016-12-09 09:52:16 -05003092 fib_table_entry_special_dpo_add (fib_index,
3093 &pfx,
3094 FIB_SOURCE_CLASSIFY,
3095 FIB_ENTRY_FLAG_NONE, &dpo);
3096 dpo_reset (&dpo);
3097 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003098 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003099 {
3100 fib_table_entry_special_remove (fib_index,
3101 &pfx, FIB_SOURCE_CLASSIFY);
3102 }
3103 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003104
Ed Warnickecb9cada2015-12-08 15:45:58 -07003105 return 0;
3106}
Damjan Marion38173502019-02-13 19:30:09 +01003107#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -07003108
3109static clib_error_t *
3110set_ip6_classify_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003111 unformat_input_t * input,
3112 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003113{
3114 u32 table_index = ~0;
3115 int table_index_set = 0;
3116 u32 sw_if_index = ~0;
3117 int rv;
Dave Barach75fc8542016-10-11 16:16:02 -04003118
Dave Barachd7cb1b52016-12-09 09:52:16 -05003119 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3120 {
3121 if (unformat (input, "table-index %d", &table_index))
3122 table_index_set = 1;
3123 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3124 vnet_get_main (), &sw_if_index))
3125 ;
3126 else
3127 break;
3128 }
Dave Barach75fc8542016-10-11 16:16:02 -04003129
Ed Warnickecb9cada2015-12-08 15:45:58 -07003130 if (table_index_set == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003131 return clib_error_return (0, "classify table-index must be specified");
Dave Barach75fc8542016-10-11 16:16:02 -04003132
Ed Warnickecb9cada2015-12-08 15:45:58 -07003133 if (sw_if_index == ~0)
3134 return clib_error_return (0, "interface / subif must be specified");
3135
3136 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3137
3138 switch (rv)
3139 {
3140 case 0:
3141 break;
3142
3143 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3144 return clib_error_return (0, "No such interface");
3145
3146 case VNET_API_ERROR_NO_SUCH_ENTRY:
3147 return clib_error_return (0, "No such classifier table");
3148 }
3149 return 0;
3150}
3151
Billy McFall0683c9c2016-10-13 08:27:31 -04003152/*?
3153 * Assign a classification table to an interface. The classification
3154 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3155 * commands. Once the table is create, use this command to filter packets
3156 * on an interface.
3157 *
3158 * @cliexpar
3159 * Example of how to assign a classification table to an interface:
3160 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3161?*/
3162/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003163VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3164{
3165 .path = "set ip6 classify",
3166 .short_help =
3167 "set ip6 classify intfc <interface> table-index <classify-idx>",
3168 .function = set_ip6_classify_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003169};
Billy McFall0683c9c2016-10-13 08:27:31 -04003170/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003171
Dave Barachd7cb1b52016-12-09 09:52:16 -05003172/*
3173 * fd.io coding-style-patch-verification: ON
3174 *
3175 * Local Variables:
3176 * eval: (c-set-style "gnu")
3177 * End:
3178 */