blob: c45b65fd22d3416c741c5b8cd0d983a54e9a94be [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>
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +020042#include <vnet/ip/ip6_neighbor.h>
Dave Barachd7cb1b52016-12-09 09:52:16 -050043#include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#include <vnet/srp/srp.h> /* for srp_hw_interface_class */
45#include <vppinfra/cache.h>
AkshayaNadahalli0f438df2017-02-10 10:54:16 +053046#include <vnet/fib/fib_urpf_list.h> /* for FIB uRPF check */
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047#include <vnet/fib/ip6_fib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000048#include <vnet/mfib/ip6_mfib.h>
Neale Rannsf12a83f2017-04-18 09:09:40 -070049#include <vnet/dpo/load_balance_map.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010050#include <vnet/dpo/classify_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
52#include <vppinfra/bihash_template.c>
Vijayabhaskar Katamreddyacbde662018-01-23 13:39:40 -080053#include <vnet/ip/ip6_forward.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
AkshayaNadahallifdd81af2016-12-01 16:33:51 +053055/* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
56#define OI_DECAP 0x80000000
57
Ed Warnickecb9cada2015-12-08 15:45:58 -070058static void
59ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
60 ip6_main_t * im, u32 fib_index,
61 ip_interface_address_t * a)
62{
Dave Barachd7cb1b52016-12-09 09:52:16 -050063 ip_lookup_main_t *lm = &im->lookup_main;
64 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -050066 .fp_len = a->address_length,
67 .fp_proto = FIB_PROTOCOL_IP6,
68 .fp_addr.ip6 = *address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010069 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 if (a->address_length < 128)
Dave Barachd7cb1b52016-12-09 09:52:16 -050072 {
Neale Ranns7a272742017-05-30 02:08:14 -070073 fib_table_entry_update_one_path (fib_index,
74 &pfx,
75 FIB_SOURCE_INTERFACE,
76 (FIB_ENTRY_FLAG_CONNECTED |
77 FIB_ENTRY_FLAG_ATTACHED),
Neale Rannsda78f952017-05-24 09:15:43 -070078 DPO_PROTO_IP6,
Neale Ranns7a272742017-05-30 02:08:14 -070079 /* No next-hop address */
80 NULL, sw_if_index,
81 /* invalid FIB index */
82 ~0, 1,
83 /* no label stack */
84 NULL, FIB_ROUTE_PATH_FLAG_NONE);
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
Neale Ranns0bfe5d82016-08-25 15:29:12 +010087 pfx.fp_len = 128;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
Dave Barachd7cb1b52016-12-09 09:52:16 -050089 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010090 u32 classify_table_index =
Dave Barachd7cb1b52016-12-09 09:52:16 -050091 lm->classify_table_index_by_sw_if_index[sw_if_index];
92 if (classify_table_index != (u32) ~ 0)
93 {
94 dpo_id_t dpo = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010095
Dave Barachd7cb1b52016-12-09 09:52:16 -050096 dpo_set (&dpo,
97 DPO_CLASSIFY,
98 DPO_PROTO_IP6,
99 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100100
Dave Barachd7cb1b52016-12-09 09:52:16 -0500101 fib_table_entry_special_dpo_add (fib_index,
102 &pfx,
103 FIB_SOURCE_CLASSIFY,
104 FIB_ENTRY_FLAG_NONE, &dpo);
105 dpo_reset (&dpo);
106 }
107 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108
Neale Rannsf12a83f2017-04-18 09:09:40 -0700109 fib_table_entry_update_one_path (fib_index, &pfx,
110 FIB_SOURCE_INTERFACE,
111 (FIB_ENTRY_FLAG_CONNECTED |
112 FIB_ENTRY_FLAG_LOCAL),
Neale Rannsda78f952017-05-24 09:15:43 -0700113 DPO_PROTO_IP6,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700114 &pfx.fp_addr,
115 sw_if_index, ~0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500116 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117}
118
119static void
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100120ip6_del_interface_routes (ip6_main_t * im,
121 u32 fib_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122 ip6_address_t * address, u32 address_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500124 fib_prefix_t pfx = {
125 .fp_len = address_length,
126 .fp_proto = FIB_PROTOCOL_IP6,
127 .fp_addr.ip6 = *address,
128 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Dave Barachd7cb1b52016-12-09 09:52:16 -0500130 if (pfx.fp_len < 128)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500132 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100133
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 }
135
Dave Barachd7cb1b52016-12-09 09:52:16 -0500136 pfx.fp_len = 128;
137 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138}
139
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100140void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500141ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100142{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500143 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147 /*
148 * enable/disable only on the 1<->0 transition
149 */
150 if (is_enable)
151 {
152 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500153 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100154 }
155 else
156 {
Neale Ranns75152282017-01-09 01:00:45 -0800157 /* The ref count is 0 when an address is removed from an interface that has
158 * no address - this is not a ciritical error */
159 if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
160 0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500161 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100162 }
163
Neale Ranns8269d3d2018-01-30 09:02:20 -0800164 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
Neale Ranns630198f2017-05-22 09:20:20 -0400165 !is_enable, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100166
Neale Ranns8269d3d2018-01-30 09:02:20 -0800167 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
168 sw_if_index, !is_enable, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100169}
170
Neale Rannsdf089a82016-10-02 16:39:06 +0100171/* get first interface address */
172ip6_address_t *
Neale Ranns6cfc39c2017-02-14 01:44:25 -0800173ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index)
Neale Rannsdf089a82016-10-02 16:39:06 +0100174{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500175 ip_lookup_main_t *lm = &im->lookup_main;
176 ip_interface_address_t *ia = 0;
177 ip6_address_t *result = 0;
Neale Rannsdf089a82016-10-02 16:39:06 +0100178
Dave Barachd7cb1b52016-12-09 09:52:16 -0500179 /* *INDENT-OFF* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100180 foreach_ip_interface_address (lm, ia, sw_if_index,
181 1 /* honor unnumbered */,
182 ({
183 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
184 result = a;
185 break;
186 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500187 /* *INDENT-ON* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100188 return result;
189}
190
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191clib_error_t *
192ip6_add_del_interface_address (vlib_main_t * vm,
193 u32 sw_if_index,
194 ip6_address_t * address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500195 u32 address_length, u32 is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500197 vnet_main_t *vnm = vnet_get_main ();
198 ip6_main_t *im = &ip6_main;
199 ip_lookup_main_t *lm = &im->lookup_main;
200 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 u32 if_address_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500202 ip6_address_fib_t ip6_af, *addr_fib = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
Pavel Kotucek57808982017-08-02 08:20:19 +0200204 /* local0 interface doesn't support IP addressing */
205 if (sw_if_index == 0)
206 {
207 return
208 clib_error_create ("local0 interface doesn't support IP addressing");
209 }
210
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000212 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
213
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 ip6_addr_fib_init (&ip6_af, address,
215 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
216 vec_add1 (addr_fib, ip6_af);
217
Neale Ranns744902e2017-08-14 10:35:44 -0700218 /* *INDENT-OFF* */
219 if (!is_del)
220 {
221 /* When adding an address check that it does not conflict
222 with an existing address on any interface in this table. */
223 ip_interface_address_t *ia;
224 vnet_sw_interface_t *sif;
225
226 pool_foreach(sif, vnm->interface_main.sw_interfaces,
227 ({
228 if (im->fib_index_by_sw_if_index[sw_if_index] ==
229 im->fib_index_by_sw_if_index[sif->sw_if_index])
230 {
231 foreach_ip_interface_address
232 (&im->lookup_main, ia, sif->sw_if_index,
233 0 /* honor unnumbered */ ,
234 ({
235 ip6_address_t * x =
236 ip_interface_address_get_address
237 (&im->lookup_main, ia);
238 if (ip6_destination_matches_route
239 (im, address, x, ia->address_length) ||
240 ip6_destination_matches_route (im,
241 x,
242 address,
243 address_length))
244 {
245 vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS;
246 return
247 clib_error_create
248 ("failed to add %U which conflicts with %U for interface %U",
249 format_ip6_address_and_length, address,
250 address_length,
251 format_ip6_address_and_length, x,
252 ia->address_length,
253 format_vnet_sw_if_index_name, vnm,
254 sif->sw_if_index);
255 }
256 }));
257 }
258 }));
259 }
260 /* *INDENT-ON* */
261
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 {
263 uword elts_before = pool_elts (lm->if_address_pool);
264
265 error = ip_interface_address_add_del
Dave Barachd7cb1b52016-12-09 09:52:16 -0500266 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 if (error)
268 goto done;
269
270 /* Pool did not grow: add duplicate address. */
271 if (elts_before == pool_elts (lm->if_address_pool))
272 goto done;
273 }
274
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000276
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100277 if (is_del)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500278 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100279 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500280 ip6_add_interface_routes (vnm, sw_if_index,
281 im, ip6_af.fib_index,
282 pool_elt_at_index (lm->if_address_pool,
283 if_address_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
285 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500286 ip6_add_del_interface_address_callback_t *cb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 vec_foreach (cb, im->add_del_interface_address_callbacks)
288 cb->function (im, cb->function_opaque, sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500289 address, address_length, if_address_index, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 }
291
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 vec_free (addr_fib);
294 return error;
295}
296
297clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500298ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500300 ip6_main_t *im = &ip6_main;
301 ip_interface_address_t *ia;
302 ip6_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 u32 is_admin_up, fib_index;
304
305 /* Fill in lookup tables with default table (0). */
306 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
307
Dave Barachd7cb1b52016-12-09 09:52:16 -0500308 vec_validate_init_empty (im->
309 lookup_main.if_address_pool_index_by_sw_if_index,
310 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
313
314 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
315
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316 /* *INDENT-OFF* */
Dave Barach75fc8542016-10-11 16:16:02 -0400317 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 0 /* honor unnumbered */,
319 ({
320 a = ip_interface_address_get_address (&im->lookup_main, ia);
321 if (is_admin_up)
322 ip6_add_interface_routes (vnm, sw_if_index,
323 im, fib_index,
324 ia);
325 else
326 ip6_del_interface_routes (im, fib_index,
327 a, ia->address_length);
328 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500329 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
331 return 0;
332}
333
334VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
335
Dave Barachd6534602016-06-14 18:38:02 -0400336/* Built-in ip6 unicast rx feature path definition */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500337/* *INDENT-OFF* */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100338VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
339{
340 .arc_name = "ip6-unicast",
341 .start_nodes = VNET_FEATURES ("ip6-input"),
342 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
343};
344
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345VNET_FEATURE_INIT (ip6_flow_classify, static) =
346{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100347 .arc_name = "ip6-unicast",
Juraj Sloboda506b2452016-08-07 23:45:24 -0700348 .node_name = "ip6-flow-classify",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100349 .runs_before = VNET_FEATURES ("ip6-inacl"),
Juraj Sloboda506b2452016-08-07 23:45:24 -0700350};
351
Dave Barachd7cb1b52016-12-09 09:52:16 -0500352VNET_FEATURE_INIT (ip6_inacl, static) =
353{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100354 .arc_name = "ip6-unicast",
Dave Barach75fc8542016-10-11 16:16:02 -0400355 .node_name = "ip6-inacl",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100356 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
Dave Barachd6534602016-06-14 18:38:02 -0400357};
358
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359VNET_FEATURE_INIT (ip6_policer_classify, static) =
360{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100361 .arc_name = "ip6-unicast",
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700362 .node_name = "ip6-policer-classify",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100363 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700364};
365
Dave Barachd7cb1b52016-12-09 09:52:16 -0500366VNET_FEATURE_INIT (ip6_ipsec, static) =
367{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100368 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400369 .node_name = "ipsec-input-ip6",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100370 .runs_before = VNET_FEATURES ("l2tp-decap"),
Dave Barachd6534602016-06-14 18:38:02 -0400371};
372
Dave Barachd7cb1b52016-12-09 09:52:16 -0500373VNET_FEATURE_INIT (ip6_l2tp, static) =
374{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100375 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400376 .node_name = "l2tp-decap",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100377 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
Dave Barachd6534602016-06-14 18:38:02 -0400378};
379
Dave Barachd7cb1b52016-12-09 09:52:16 -0500380VNET_FEATURE_INIT (ip6_vpath, static) =
381{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100382 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400383 .node_name = "vpath-input-ip6",
John Lo2b81eb82017-01-30 13:12:10 -0500384 .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
385};
386
387VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
388{
389 .arc_name = "ip6-unicast",
390 .node_name = "ip6-vxlan-bypass",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100391 .runs_before = VNET_FEATURES ("ip6-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400392};
393
Neale Ranns8269d3d2018-01-30 09:02:20 -0800394VNET_FEATURE_INIT (ip6_not_enabled, static) =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500395{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100396 .arc_name = "ip6-unicast",
Neale Ranns8269d3d2018-01-30 09:02:20 -0800397 .node_name = "ip6-not-enabled",
Neale Ranns630198f2017-05-22 09:20:20 -0400398 .runs_before = VNET_FEATURES ("ip6-lookup"),
399};
400
401VNET_FEATURE_INIT (ip6_lookup, static) =
402{
403 .arc_name = "ip6-unicast",
404 .node_name = "ip6-lookup",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100405 .runs_before = 0, /*last feature*/
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100406};
407
Dave Barachd6534602016-06-14 18:38:02 -0400408/* Built-in ip6 multicast rx feature path definition (none now) */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100409VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
410{
411 .arc_name = "ip6-multicast",
412 .start_nodes = VNET_FEATURES ("ip6-input"),
413 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
414};
415
416VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
417 .arc_name = "ip6-multicast",
Dave Barachd6534602016-06-14 18:38:02 -0400418 .node_name = "vpath-input-ip6",
Neale Ranns32e1c012016-11-22 17:07:28 +0000419 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400420};
421
Neale Ranns8269d3d2018-01-30 09:02:20 -0800422VNET_FEATURE_INIT (ip6_not_enabled_mc, static) = {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100423 .arc_name = "ip6-multicast",
Neale Ranns8269d3d2018-01-30 09:02:20 -0800424 .node_name = "ip6-not-enabled",
Neale Ranns630198f2017-05-22 09:20:20 -0400425 .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
426};
427
428VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
429 .arc_name = "ip6-multicast",
430 .node_name = "ip6-mfib-forward-lookup",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100431 .runs_before = 0, /* last feature */
Neale Ranns5e575b12016-10-03 09:40:25 +0100432};
Dave Barach5331c722016-08-17 11:54:30 -0400433
434/* Built-in ip4 tx feature path definition */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100435VNET_FEATURE_ARC_INIT (ip6_output, static) =
436{
437 .arc_name = "ip6-output",
Neale Rannsf068c3e2018-01-03 04:18:48 -0800438 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain", "ip6-dvr-dpo"),
Damjan Marion8b3191e2016-11-09 19:54:20 +0100439 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
Dave Barach5331c722016-08-17 11:54:30 -0400440};
441
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100442VNET_FEATURE_INIT (ip6_outacl, static) = {
443 .arc_name = "ip6-output",
444 .node_name = "ip6-outacl",
445 .runs_before = VNET_FEATURES ("ipsec-output-ip6"),
446};
447
Matus Fabian08a6f012016-11-15 06:08:51 -0800448VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
449 .arc_name = "ip6-output",
450 .node_name = "ipsec-output-ip6",
451 .runs_before = VNET_FEATURES ("interface-output"),
452};
453
Damjan Marion8b3191e2016-11-09 19:54:20 +0100454VNET_FEATURE_INIT (ip6_interface_output, static) = {
455 .arc_name = "ip6-output",
456 .node_name = "interface-output",
457 .runs_before = 0, /* not before any other features */
458};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500459/* *INDENT-ON* */
Dave Barachd6534602016-06-14 18:38:02 -0400460
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500462ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463{
Florin Corasb5c13fd2017-05-10 12:32:53 -0700464 ip6_main_t *im = &ip6_main;
465
466 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
467 vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
468
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200469 if (!is_add)
470 {
471 /* Ensure that IPv6 is disabled */
472 ip6_main_t *im6 = &ip6_main;
473 ip_lookup_main_t *lm6 = &im6->lookup_main;
474 ip_interface_address_t *ia = 0;
475 ip6_address_t *address;
476 vlib_main_t *vm = vlib_get_main ();
477
478 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, 0 /* is_add */ );
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700479 vnet_sw_interface_update_unnumbered (sw_if_index, ~0, 0);
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200480 /* *INDENT-OFF* */
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700481 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +0200482 ({
483 address = ip_interface_address_get_address (lm6, ia);
484 ip6_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1);
485 }));
486 /* *INDENT-ON* */
487 ip6_mfib_interface_enable_disable (sw_if_index, 0);
488 }
489
Neale Ranns8269d3d2018-01-30 09:02:20 -0800490 vnet_feature_enable_disable ("ip6-unicast", "ip6-not-enabled", sw_if_index,
Damjan Marion8b3191e2016-11-09 19:54:20 +0100491 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492
Neale Ranns8269d3d2018-01-30 09:02:20 -0800493 vnet_feature_enable_disable ("ip6-multicast", "ip6-not-enabled",
494 sw_if_index, is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 return /* no error */ 0;
497}
498
499VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
500
Damjan Marionaca64c92016-04-13 09:48:56 +0200501static uword
502ip6_lookup (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500503 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200504{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100505 return ip6_lookup_inline (vm, node, frame);
Damjan Marionaca64c92016-04-13 09:48:56 +0200506}
507
Dave Barachd7cb1b52016-12-09 09:52:16 -0500508static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100509
Dave Barachd7cb1b52016-12-09 09:52:16 -0500510/* *INDENT-OFF* */
511VLIB_REGISTER_NODE (ip6_lookup_node) =
512{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 .function = ip6_lookup,
514 .name = "ip6-lookup",
515 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100516 .format_trace = format_ip6_lookup_trace,
Ole Troanf0f85222016-06-14 21:12:32 +0200517 .n_next_nodes = IP6_LOOKUP_N_NEXT,
Damjan Marionb2707892016-04-13 11:21:07 +0200518 .next_nodes = IP6_LOOKUP_NEXT_NODES,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500520/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
Dave Barachd7cb1b52016-12-09 09:52:16 -0500522VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
Damjan Marion1c80e832016-05-11 23:07:18 +0200523
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100524always_inline uword
525ip6_load_balance (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500526 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200527{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500528 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
529 u32 n_left_from, n_left_to_next, *from, *to_next;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100530 ip_lookup_next_t next;
Damjan Marion586afd72017-04-05 19:18:20 +0200531 u32 thread_index = vlib_get_thread_index ();
Dave Barachd7cb1b52016-12-09 09:52:16 -0500532 ip6_main_t *im = &ip6_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100533
534 from = vlib_frame_vector_args (frame);
535 n_left_from = frame->n_vectors;
536 next = node->cached_next_index;
537
538 if (node->flags & VLIB_NODE_FLAG_TRACE)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500539 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100540
541 while (n_left_from > 0)
542 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500543 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100544
Dave Barach75fc8542016-10-11 16:16:02 -0400545
Neale Ranns2be95c12016-11-19 13:50:04 +0000546 while (n_left_from >= 4 && n_left_to_next >= 2)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500547 {
548 ip_lookup_next_t next0, next1;
549 const load_balance_t *lb0, *lb1;
550 vlib_buffer_t *p0, *p1;
551 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
552 const ip6_header_t *ip0, *ip1;
553 const dpo_id_t *dpo0, *dpo1;
Neale Ranns2be95c12016-11-19 13:50:04 +0000554
Dave Barachd7cb1b52016-12-09 09:52:16 -0500555 /* Prefetch next iteration. */
556 {
557 vlib_buffer_t *p2, *p3;
Neale Ranns2be95c12016-11-19 13:50:04 +0000558
Dave Barachd7cb1b52016-12-09 09:52:16 -0500559 p2 = vlib_get_buffer (vm, from[2]);
560 p3 = vlib_get_buffer (vm, from[3]);
Neale Ranns2be95c12016-11-19 13:50:04 +0000561
Dave Barachd7cb1b52016-12-09 09:52:16 -0500562 vlib_prefetch_buffer_header (p2, STORE);
563 vlib_prefetch_buffer_header (p3, STORE);
Neale Ranns2be95c12016-11-19 13:50:04 +0000564
Dave Barachd7cb1b52016-12-09 09:52:16 -0500565 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
566 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
567 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000568
Dave Barachd7cb1b52016-12-09 09:52:16 -0500569 pi0 = to_next[0] = from[0];
570 pi1 = to_next[1] = from[1];
Neale Ranns2be95c12016-11-19 13:50:04 +0000571
Dave Barachd7cb1b52016-12-09 09:52:16 -0500572 from += 2;
573 n_left_from -= 2;
574 to_next += 2;
575 n_left_to_next -= 2;
Neale Ranns2be95c12016-11-19 13:50:04 +0000576
Dave Barachd7cb1b52016-12-09 09:52:16 -0500577 p0 = vlib_get_buffer (vm, pi0);
578 p1 = vlib_get_buffer (vm, pi1);
Neale Ranns2be95c12016-11-19 13:50:04 +0000579
Dave Barachd7cb1b52016-12-09 09:52:16 -0500580 ip0 = vlib_buffer_get_current (p0);
581 ip1 = vlib_buffer_get_current (p1);
582 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
583 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Neale Ranns2be95c12016-11-19 13:50:04 +0000584
Dave Barachd7cb1b52016-12-09 09:52:16 -0500585 lb0 = load_balance_get (lbi0);
586 lb1 = load_balance_get (lbi1);
Neale Ranns2be95c12016-11-19 13:50:04 +0000587
Dave Barachd7cb1b52016-12-09 09:52:16 -0500588 /*
589 * this node is for via FIBs we can re-use the hash value from the
590 * to node if present.
591 * We don't want to use the same hash value at each level in the recursion
592 * graph as that would lead to polarisation
593 */
AkshayaNadahalli153b8712017-03-06 18:22:29 +0000594 hc0 = hc1 = 0;
Neale Ranns2be95c12016-11-19 13:50:04 +0000595
Dave Barachd7cb1b52016-12-09 09:52:16 -0500596 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
597 {
598 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
599 {
600 hc0 = vnet_buffer (p0)->ip.flow_hash =
601 vnet_buffer (p0)->ip.flow_hash >> 1;
602 }
603 else
604 {
605 hc0 = vnet_buffer (p0)->ip.flow_hash =
AkshayaNadahalli153b8712017-03-06 18:22:29 +0000606 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500607 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700608 dpo0 =
609 load_balance_get_fwd_bucket (lb0,
610 (hc0 &
611 lb0->lb_n_buckets_minus_1));
612 }
613 else
614 {
615 dpo0 = load_balance_get_bucket_i (lb0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500616 }
617 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
618 {
619 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
620 {
621 hc1 = vnet_buffer (p1)->ip.flow_hash =
622 vnet_buffer (p1)->ip.flow_hash >> 1;
623 }
624 else
625 {
626 hc1 = vnet_buffer (p1)->ip.flow_hash =
AkshayaNadahalli153b8712017-03-06 18:22:29 +0000627 ip6_compute_flow_hash (ip1, lb1->lb_hash_config);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500628 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700629 dpo1 =
630 load_balance_get_fwd_bucket (lb1,
631 (hc1 &
632 lb1->lb_n_buckets_minus_1));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500633 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700634 else
635 {
636 dpo1 = load_balance_get_bucket_i (lb1, 0);
637 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000638
Dave Barachd7cb1b52016-12-09 09:52:16 -0500639 next0 = dpo0->dpoi_next_node;
640 next1 = dpo1->dpoi_next_node;
Neale Ranns2be95c12016-11-19 13:50:04 +0000641
Dave Barachd7cb1b52016-12-09 09:52:16 -0500642 /* Only process the HBH Option Header if explicitly configured to do so */
643 if (PREDICT_FALSE
644 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
645 {
646 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
647 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
648 }
649 /* Only process the HBH Option Header if explicitly configured to do so */
650 if (PREDICT_FALSE
651 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
652 {
653 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
654 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
655 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000656
Dave Barachd7cb1b52016-12-09 09:52:16 -0500657 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
658 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
Neale Ranns2be95c12016-11-19 13:50:04 +0000659
Dave Barachd7cb1b52016-12-09 09:52:16 -0500660 vlib_increment_combined_counter
Damjan Marion586afd72017-04-05 19:18:20 +0200661 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500662 vlib_increment_combined_counter
Damjan Marion586afd72017-04-05 19:18:20 +0200663 (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
Neale Ranns2be95c12016-11-19 13:50:04 +0000664
Dave Barachd7cb1b52016-12-09 09:52:16 -0500665 vlib_validate_buffer_enqueue_x2 (vm, node, next,
666 to_next, n_left_to_next,
667 pi0, pi1, next0, next1);
668 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000669
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100670 while (n_left_from > 0 && n_left_to_next > 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500671 {
672 ip_lookup_next_t next0;
673 const load_balance_t *lb0;
674 vlib_buffer_t *p0;
675 u32 pi0, lbi0, hc0;
676 const ip6_header_t *ip0;
677 const dpo_id_t *dpo0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100678
Dave Barachd7cb1b52016-12-09 09:52:16 -0500679 pi0 = from[0];
680 to_next[0] = pi0;
681 from += 1;
682 to_next += 1;
683 n_left_to_next -= 1;
684 n_left_from -= 1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100685
Dave Barachd7cb1b52016-12-09 09:52:16 -0500686 p0 = vlib_get_buffer (vm, pi0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100687
Dave Barachd7cb1b52016-12-09 09:52:16 -0500688 ip0 = vlib_buffer_get_current (p0);
689 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100690
Dave Barachd7cb1b52016-12-09 09:52:16 -0500691 lb0 = load_balance_get (lbi0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100692
AkshayaNadahalli153b8712017-03-06 18:22:29 +0000693 hc0 = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500694 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
695 {
696 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
697 {
698 hc0 = vnet_buffer (p0)->ip.flow_hash =
699 vnet_buffer (p0)->ip.flow_hash >> 1;
700 }
701 else
702 {
703 hc0 = vnet_buffer (p0)->ip.flow_hash =
AkshayaNadahalli153b8712017-03-06 18:22:29 +0000704 ip6_compute_flow_hash (ip0, lb0->lb_hash_config);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500705 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700706 dpo0 =
707 load_balance_get_fwd_bucket (lb0,
708 (hc0 &
709 lb0->lb_n_buckets_minus_1));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500710 }
Neale Rannsf12a83f2017-04-18 09:09:40 -0700711 else
712 {
713 dpo0 = load_balance_get_bucket_i (lb0, 0);
714 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100715
Dave Barachd7cb1b52016-12-09 09:52:16 -0500716 next0 = dpo0->dpoi_next_node;
717 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
Neale Ranns2be95c12016-11-19 13:50:04 +0000718
Dave Barachd7cb1b52016-12-09 09:52:16 -0500719 /* Only process the HBH Option Header if explicitly configured to do so */
720 if (PREDICT_FALSE
721 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
722 {
723 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
724 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
725 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000726
Dave Barachd7cb1b52016-12-09 09:52:16 -0500727 vlib_increment_combined_counter
Damjan Marion586afd72017-04-05 19:18:20 +0200728 (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100729
Dave Barachd7cb1b52016-12-09 09:52:16 -0500730 vlib_validate_buffer_enqueue_x1 (vm, node, next,
731 to_next, n_left_to_next,
732 pi0, next0);
733 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100734
735 vlib_put_next_frame (vm, node, next, n_left_to_next);
736 }
737
738 return frame->n_vectors;
Damjan Marionaca64c92016-04-13 09:48:56 +0200739}
740
Dave Barachd7cb1b52016-12-09 09:52:16 -0500741/* *INDENT-OFF* */
742VLIB_REGISTER_NODE (ip6_load_balance_node) =
743{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100744 .function = ip6_load_balance,
745 .name = "ip6-load-balance",
Damjan Marionaca64c92016-04-13 09:48:56 +0200746 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200747 .sibling_of = "ip6-lookup",
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100748 .format_trace = format_ip6_lookup_trace,
Damjan Marionaca64c92016-04-13 09:48:56 +0200749};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500750/* *INDENT-ON* */
Damjan Marionaca64c92016-04-13 09:48:56 +0200751
Dave Barachd7cb1b52016-12-09 09:52:16 -0500752VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
Damjan Marion1c80e832016-05-11 23:07:18 +0200753
Dave Barachd7cb1b52016-12-09 09:52:16 -0500754typedef struct
755{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 /* Adjacency taken. */
757 u32 adj_index;
758 u32 flow_hash;
John Lo2d343742016-01-19 17:27:17 -0500759 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
761 /* Packet data, possibly *after* rewrite. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500762 u8 packet_data[128 - 1 * sizeof (u32)];
763}
764ip6_forward_next_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
John Lo2b81eb82017-01-30 13:12:10 -0500766u8 *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500767format_ip6_forward_next_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768{
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100769 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
770 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500771 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200772 u32 indent = format_get_indent (s);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100773
Dave Barachd7cb1b52016-12-09 09:52:16 -0500774 s = format (s, "%U%U",
775 format_white_space, indent,
776 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100777 return s;
778}
779
Dave Barachd7cb1b52016-12-09 09:52:16 -0500780static u8 *
781format_ip6_lookup_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100782{
783 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
784 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500785 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200786 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
John Loac8146c2016-09-27 17:44:02 -0400788 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500789 t->fib_index, t->adj_index, t->flow_hash);
790 s = format (s, "\n%U%U",
791 format_white_space, indent,
792 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100793 return s;
794}
Pierre Pfister0febaf12016-06-08 12:23:21 +0100795
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796
Dave Barachd7cb1b52016-12-09 09:52:16 -0500797static u8 *
798format_ip6_rewrite_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100799{
800 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
801 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500802 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200803 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100805 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500806 t->fib_index, t->adj_index, format_ip_adjacency,
807 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100808 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500809 format_white_space, indent,
810 format_ip_adjacency_packet_data,
Neale Rannsb069a692017-03-15 12:34:25 -0400811 t->adj_index, t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812 return s;
813}
814
815/* Common trace function for all ip6-forward next nodes. */
816void
817ip6_forward_next_trace (vlib_main_t * vm,
818 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500819 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500821 u32 *from, n_left;
822 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
824 n_left = frame->n_vectors;
825 from = vlib_frame_vector_args (frame);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100826
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827 while (n_left >= 4)
828 {
829 u32 bi0, bi1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500830 vlib_buffer_t *b0, *b1;
831 ip6_forward_next_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832
833 /* Prefetch next iteration. */
834 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
835 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
836
837 bi0 = from[0];
838 bi1 = from[1];
839
840 b0 = vlib_get_buffer (vm, bi0);
841 b1 = vlib_get_buffer (vm, bi1);
842
843 if (b0->flags & VLIB_BUFFER_IS_TRACED)
844 {
845 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
846 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500847 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
848 t0->fib_index =
849 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
850 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
851 vec_elt (im->fib_index_by_sw_if_index,
852 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100853
Damjan Marionf1213b82016-03-13 02:22:06 +0100854 clib_memcpy (t0->packet_data,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500855 vlib_buffer_get_current (b0),
856 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857 }
858 if (b1->flags & VLIB_BUFFER_IS_TRACED)
859 {
860 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
861 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500862 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
863 t1->fib_index =
864 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
865 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
866 vec_elt (im->fib_index_by_sw_if_index,
867 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100868
Damjan Marionf1213b82016-03-13 02:22:06 +0100869 clib_memcpy (t1->packet_data,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500870 vlib_buffer_get_current (b1),
871 sizeof (t1->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872 }
873 from += 2;
874 n_left -= 2;
875 }
876
877 while (n_left >= 1)
878 {
879 u32 bi0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500880 vlib_buffer_t *b0;
881 ip6_forward_next_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
883 bi0 = from[0];
884
885 b0 = vlib_get_buffer (vm, bi0);
886
887 if (b0->flags & VLIB_BUFFER_IS_TRACED)
888 {
889 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
890 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500891 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
892 t0->fib_index =
893 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
894 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
895 vec_elt (im->fib_index_by_sw_if_index,
896 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100897
Damjan Marionf1213b82016-03-13 02:22:06 +0100898 clib_memcpy (t0->packet_data,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500899 vlib_buffer_get_current (b0),
900 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901 }
902 from += 1;
903 n_left -= 1;
904 }
905}
906
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907/* Compute TCP/UDP/ICMP6 checksum in software. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500908u16
909ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
910 ip6_header_t * ip0, int *bogus_lengthp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911{
912 ip_csum_t sum0;
913 u16 sum16, payload_length_host_byte_order;
914 u32 i, n_this_buffer, n_bytes_left;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500915 u32 headers_size = sizeof (ip0[0]);
916 void *data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917
Dave Barachd7cb1b52016-12-09 09:52:16 -0500918 ASSERT (bogus_lengthp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 *bogus_lengthp = 0;
920
921 /* Initialize checksum with ip header. */
922 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
923 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
924 data_this_buffer = (void *) (ip0 + 1);
Dave Barach75fc8542016-10-11 16:16:02 -0400925
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
927 {
928 sum0 = ip_csum_with_carry (sum0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500929 clib_mem_unaligned (&ip0->
930 src_address.as_uword[i],
931 uword));
932 sum0 =
933 ip_csum_with_carry (sum0,
934 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
935 uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936 }
937
AkshayaNadahalli1b563522017-01-23 22:05:35 +0530938 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
939 * or UDP-Ping packets */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500940 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500942 u32 skip_bytes;
943 ip6_hop_by_hop_ext_t *ext_hdr =
944 (ip6_hop_by_hop_ext_t *) data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945
946 /* validate really icmp6 next */
AkshayaNadahalli1b563522017-01-23 22:05:35 +0530947 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_ICMP6)
948 || (ext_hdr->next_hdr == IP_PROTOCOL_UDP));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949
Dave Barachd7cb1b52016-12-09 09:52:16 -0500950 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
951 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
Dave Barach75fc8542016-10-11 16:16:02 -0400952
Dave Barachd7cb1b52016-12-09 09:52:16 -0500953 payload_length_host_byte_order -= skip_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954 headers_size += skip_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500955 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956
957 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500958 if (p0 && n_this_buffer + headers_size > p0->current_length)
959 n_this_buffer =
960 p0->current_length >
961 headers_size ? p0->current_length - headers_size : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962 while (1)
963 {
964 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
965 n_bytes_left -= n_this_buffer;
966 if (n_bytes_left == 0)
967 break;
968
969 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500970 {
971 *bogus_lengthp = 1;
972 return 0xfefe;
973 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974 p0 = vlib_get_buffer (vm, p0->next_buffer);
975 data_this_buffer = vlib_buffer_get_current (p0);
976 n_this_buffer = p0->current_length;
977 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978
Dave Barachd7cb1b52016-12-09 09:52:16 -0500979 sum16 = ~ip_csum_fold (sum0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980
981 return sum16;
982}
983
Dave Barachd7cb1b52016-12-09 09:52:16 -0500984u32
985ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500987 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
988 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989 u16 sum16;
990 int bogus_length;
991
992 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
993 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
994 || ip0->protocol == IP_PROTOCOL_ICMP6
995 || ip0->protocol == IP_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -0500996 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997
998 udp0 = (void *) (ip0 + 1);
999 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1000 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001001 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1002 | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 return p0->flags;
1004 }
1005
1006 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1007
Damjan Marion213b5aa2017-07-13 21:19:27 +02001008 p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED
1009 | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010
1011 return p0->flags;
1012}
1013
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301014/**
1015 * @brief returns number of links on which src is reachable.
1016 */
1017always_inline int
1018ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1019{
1020 const load_balance_t *lb0;
1021 index_t lbi;
Florin Corasf3a3bad2018-03-28 02:18:29 -07001022 u32 fib_index;
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301023
Florin Corasf3a3bad2018-03-28 02:18:29 -07001024 fib_index = vec_elt (im->fib_index_by_sw_if_index,
1025 vnet_buffer (b)->sw_if_index[VLIB_RX]);
1026 fib_index =
1027 (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
1028 fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301029
Florin Corasf3a3bad2018-03-28 02:18:29 -07001030 lbi = ip6_fib_table_fwding_lookup (im, fib_index, &i->src_address);
AkshayaNadahalli0f438df2017-02-10 10:54:16 +05301031 lb0 = load_balance_get (lbi);
1032
1033 return (fib_urpf_check_size (lb0->lb_urpf));
1034}
1035
rootc9d1c5b2017-08-15 12:58:31 -04001036always_inline u8
1037ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
1038 u32 * udp_offset0)
1039{
1040 u32 proto0;
1041 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
1042 if (proto0 != IP_PROTOCOL_UDP)
1043 {
1044 proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1045 proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1046 }
1047 return proto0;
1048}
1049
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001050/* *INDENT-OFF* */
1051VNET_FEATURE_ARC_INIT (ip6_local) =
1052{
1053 .arc_name = "ip6-local",
1054 .start_nodes = VNET_FEATURES ("ip6-local"),
1055};
1056/* *INDENT-ON* */
1057
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058static uword
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001059ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1060 vlib_frame_t * frame, int head_of_feature_arc)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001062 ip6_main_t *im = &ip6_main;
1063 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064 ip_local_next_t next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001065 u32 *from, *to_next, n_left_from, n_left_to_next;
1066 vlib_node_runtime_t *error_node =
1067 vlib_node_get_runtime (vm, ip6_input_node.index);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001068 u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069
1070 from = vlib_frame_vector_args (frame);
1071 n_left_from = frame->n_vectors;
1072 next_index = node->cached_next_index;
Dave Barach75fc8542016-10-11 16:16:02 -04001073
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074 if (node->flags & VLIB_NODE_FLAG_TRACE)
1075 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1076
1077 while (n_left_from > 0)
1078 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001079 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080
1081 while (n_left_from >= 4 && n_left_to_next >= 2)
1082 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001083 vlib_buffer_t *p0, *p1;
1084 ip6_header_t *ip0, *ip1;
1085 udp_header_t *udp0, *udp1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086 u32 pi0, ip_len0, udp_len0, flags0, next0;
1087 u32 pi1, ip_len1, udp_len1, flags1, next1;
1088 i32 len_diff0, len_diff1;
rootc9d1c5b2017-08-15 12:58:31 -04001089 u8 error0, type0, good_l4_csum0, is_tcp_udp0;
1090 u8 error1, type1, good_l4_csum1, is_tcp_udp1;
Shwethab78292e2016-09-13 11:51:00 +01001091 u32 udp_offset0, udp_offset1;
Dave Barach75fc8542016-10-11 16:16:02 -04001092
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093 pi0 = to_next[0] = from[0];
1094 pi1 = to_next[1] = from[1];
1095 from += 2;
1096 n_left_from -= 2;
1097 to_next += 2;
1098 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04001099
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001100 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1101
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 p0 = vlib_get_buffer (vm, pi0);
1103 p1 = vlib_get_buffer (vm, pi1);
1104
1105 ip0 = vlib_buffer_get_current (p0);
1106 ip1 = vlib_buffer_get_current (p1);
1107
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001108 if (head_of_feature_arc == 0)
1109 goto skip_checks;
1110
Damjan Marion072401e2017-07-13 18:53:27 +02001111 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1112 vnet_buffer (p1)->l3_hdr_offset = p1->current_data;
Filip Tehlarb601f222017-01-02 10:22:56 +01001113
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1115 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1116
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117 flags0 = p0->flags;
1118 flags1 = p1->flags;
1119
rootc9d1c5b2017-08-15 12:58:31 -04001120 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1121 is_tcp_udp1 = ip6_next_proto_is_tcp_udp (p1, ip1, &udp_offset1);
1122
Jakub Grajciar96be8e82017-10-30 14:56:17 +01001123 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
Jakub Grajciar2eeeb4b2017-11-07 14:39:10 +01001124 || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1125 || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
Jakub Grajciar96be8e82017-10-30 14:56:17 +01001126 != 0;
1127 good_l4_csum1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
Jakub Grajciar2eeeb4b2017-11-07 14:39:10 +01001128 || (flags1 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1129 || flags1 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
Jakub Grajciar96be8e82017-10-30 14:56:17 +01001130 != 0;
Shwethab78292e2016-09-13 11:51:00 +01001131 len_diff0 = 0;
1132 len_diff1 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133
rootc9d1c5b2017-08-15 12:58:31 -04001134 if (PREDICT_TRUE (is_tcp_udp0))
Shwethab78292e2016-09-13 11:51:00 +01001135 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001136 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
Shwethab78292e2016-09-13 11:51:00 +01001137 /* Don't verify UDP checksum for packets with explicit zero checksum. */
rootc9d1c5b2017-08-15 12:58:31 -04001138 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001139 && udp0->checksum == 0;
Shwethab78292e2016-09-13 11:51:00 +01001140 /* Verify UDP length. */
rootc9d1c5b2017-08-15 12:58:31 -04001141 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1142 {
1143 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1144 udp_len0 = clib_net_to_host_u16 (udp0->length);
1145 len_diff0 = ip_len0 - udp_len0;
1146 }
Shwethab78292e2016-09-13 11:51:00 +01001147 }
rootc9d1c5b2017-08-15 12:58:31 -04001148 if (PREDICT_TRUE (is_tcp_udp1))
Shwethab78292e2016-09-13 11:51:00 +01001149 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001150 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
Shwethab78292e2016-09-13 11:51:00 +01001151 /* Don't verify UDP checksum for packets with explicit zero checksum. */
rootc9d1c5b2017-08-15 12:58:31 -04001152 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001153 && udp1->checksum == 0;
Shwethab78292e2016-09-13 11:51:00 +01001154 /* Verify UDP length. */
rootc9d1c5b2017-08-15 12:58:31 -04001155 if (is_tcp_udp1 == IP_PROTOCOL_UDP)
1156 {
1157 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1158 udp_len1 = clib_net_to_host_u16 (udp1->length);
1159 len_diff1 = ip_len1 - udp_len1;
1160 }
Shwethab78292e2016-09-13 11:51:00 +01001161 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162
rootc9d1c5b2017-08-15 12:58:31 -04001163 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1164 good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1167 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1168
1169 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
rootc9d1c5b2017-08-15 12:58:31 -04001170 && !good_l4_csum0
Damjan Marion213b5aa2017-07-13 21:19:27 +02001171 && !(flags0 &
1172 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173 {
1174 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
rootc9d1c5b2017-08-15 12:58:31 -04001175 good_l4_csum0 =
Damjan Marion213b5aa2017-07-13 21:19:27 +02001176 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177 }
1178 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
rootc9d1c5b2017-08-15 12:58:31 -04001179 && !good_l4_csum1
Damjan Marion213b5aa2017-07-13 21:19:27 +02001180 && !(flags1 &
1181 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001182 {
1183 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
rootc9d1c5b2017-08-15 12:58:31 -04001184 good_l4_csum1 =
Damjan Marion213b5aa2017-07-13 21:19:27 +02001185 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186 }
1187
1188 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1190 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1191
Dave Barachd7cb1b52016-12-09 09:52:16 -05001192 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1193 IP6_ERROR_UDP_CHECKSUM);
1194 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1195 IP6_ERROR_ICMP_CHECKSUM);
rootc9d1c5b2017-08-15 12:58:31 -04001196 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1197 error1 = (!good_l4_csum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198
1199 /* Drop packets from unroutable hosts. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001200 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001201 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1202 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
Dave Barachd7cb1b52016-12-09 09:52:16 -05001203 !ip6_address_is_link_local_unicast (&ip0->src_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204 {
AkshayaNadahalli8ea6d712017-02-07 23:59:54 +05301205 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001206 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001208 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1209 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
Dave Barachd7cb1b52016-12-09 09:52:16 -05001210 !ip6_address_is_link_local_unicast (&ip1->src_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 {
AkshayaNadahalli8ea6d712017-02-07 23:59:54 +05301212 error1 = (!ip6_urpf_loose_check (im, p1, ip1)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001213 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214 }
1215
Florin Corascea194d2017-10-02 00:18:51 -07001216 /* TODO maybe move to lookup? */
1217 vnet_buffer (p0)->ip.fib_index =
1218 vec_elt (im->fib_index_by_sw_if_index,
1219 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1220 vnet_buffer (p0)->ip.fib_index =
1221 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1222 (u32) ~ 0) ? vnet_buffer (p0)->ip.
1223 fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1224
1225 vnet_buffer (p1)->ip.fib_index =
1226 vec_elt (im->fib_index_by_sw_if_index,
1227 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
1228 vnet_buffer (p1)->ip.fib_index =
1229 (vnet_buffer (p1)->sw_if_index[VLIB_TX] ==
1230 (u32) ~ 0) ? vnet_buffer (p1)->ip.
1231 fib_index : vnet_buffer (p1)->sw_if_index[VLIB_TX];
1232
1233
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001234 skip_checks:
1235
1236 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1237 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1238
Dave Barachd7cb1b52016-12-09 09:52:16 -05001239 next0 =
1240 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1241 next1 =
1242 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243
1244 p0->error = error_node->errors[error0];
1245 p1->error = error_node->errors[error1];
1246
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001247 if (head_of_feature_arc)
1248 {
1249 if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1250 vnet_feature_arc_start (arc_index,
1251 vnet_buffer (p0)->sw_if_index
1252 [VLIB_RX], &next0, p0);
1253 if (PREDICT_TRUE (error1 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1254 vnet_feature_arc_start (arc_index,
1255 vnet_buffer (p1)->sw_if_index
1256 [VLIB_RX], &next1, p1);
1257 }
1258
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1260 to_next, n_left_to_next,
1261 pi0, pi1, next0, next1);
1262 }
1263
1264 while (n_left_from > 0 && n_left_to_next > 0)
1265 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001266 vlib_buffer_t *p0;
1267 ip6_header_t *ip0;
1268 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269 u32 pi0, ip_len0, udp_len0, flags0, next0;
1270 i32 len_diff0;
rootc9d1c5b2017-08-15 12:58:31 -04001271 u8 error0, type0, good_l4_csum0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001272 u32 udp_offset0;
rootc9d1c5b2017-08-15 12:58:31 -04001273 u8 is_tcp_udp0;
Dave Barach75fc8542016-10-11 16:16:02 -04001274
Ed Warnickecb9cada2015-12-08 15:45:58 -07001275 pi0 = to_next[0] = from[0];
1276 from += 1;
1277 n_left_from -= 1;
1278 to_next += 1;
1279 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04001280
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001281 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1282
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283 p0 = vlib_get_buffer (vm, pi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001284 ip0 = vlib_buffer_get_current (p0);
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001285
1286 if (head_of_feature_arc == 0)
1287 goto skip_check;
1288
Damjan Marion072401e2017-07-13 18:53:27 +02001289 vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
Filip Tehlarb601f222017-01-02 10:22:56 +01001290
Ed Warnickecb9cada2015-12-08 15:45:58 -07001291 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001292 flags0 = p0->flags;
rootc9d1c5b2017-08-15 12:58:31 -04001293 is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
Jakub Grajciar96be8e82017-10-30 14:56:17 +01001294 good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
Jakub Grajciar2eeeb4b2017-11-07 14:39:10 +01001295 || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1296 || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
Jakub Grajciar96be8e82017-10-30 14:56:17 +01001297 != 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001298
Shwethab78292e2016-09-13 11:51:00 +01001299 len_diff0 = 0;
rootc9d1c5b2017-08-15 12:58:31 -04001300 if (PREDICT_TRUE (is_tcp_udp0))
Shwethab78292e2016-09-13 11:51:00 +01001301 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001302 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
rootc9d1c5b2017-08-15 12:58:31 -04001303 /* Don't verify UDP checksum for packets with explicit zero
1304 * checksum. */
1305 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001306 && udp0->checksum == 0;
Shwethab78292e2016-09-13 11:51:00 +01001307 /* Verify UDP length. */
rootc9d1c5b2017-08-15 12:58:31 -04001308 if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1309 {
1310 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1311 udp_len0 = clib_net_to_host_u16 (udp0->length);
1312 len_diff0 = ip_len0 - udp_len0;
1313 }
Shwethab78292e2016-09-13 11:51:00 +01001314 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315
rootc9d1c5b2017-08-15 12:58:31 -04001316 good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001317 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1318
1319 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
rootc9d1c5b2017-08-15 12:58:31 -04001320 && !good_l4_csum0
Damjan Marion213b5aa2017-07-13 21:19:27 +02001321 && !(flags0 &
1322 VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323 {
1324 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
rootc9d1c5b2017-08-15 12:58:31 -04001325 good_l4_csum0 =
Damjan Marion213b5aa2017-07-13 21:19:27 +02001326 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327 }
1328
1329 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001330 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1331
Dave Barachd7cb1b52016-12-09 09:52:16 -05001332 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1333 IP6_ERROR_UDP_CHECKSUM);
1334 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1335 IP6_ERROR_ICMP_CHECKSUM);
rootc9d1c5b2017-08-15 12:58:31 -04001336 error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001337
rootc9d1c5b2017-08-15 12:58:31 -04001338 /* If this is a neighbor solicitation (ICMP), skip src RPF check */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001339 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1340 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
Dave Barachd7cb1b52016-12-09 09:52:16 -05001341 !ip6_address_is_link_local_unicast (&ip0->src_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001342 {
AkshayaNadahalli8ea6d712017-02-07 23:59:54 +05301343 error0 = (!ip6_urpf_loose_check (im, p0, ip0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001344 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345 }
1346
Florin Corascea194d2017-10-02 00:18:51 -07001347 vnet_buffer (p0)->ip.fib_index =
1348 vec_elt (im->fib_index_by_sw_if_index,
1349 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1350 vnet_buffer (p0)->ip.fib_index =
1351 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1352 (u32) ~ 0) ? vnet_buffer (p0)->ip.
1353 fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1354
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001355 skip_check:
1356
1357 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001358 next0 =
1359 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001360
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361 p0->error = error_node->errors[error0];
1362
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001363 if (head_of_feature_arc)
1364 {
1365 if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1366 vnet_feature_arc_start (arc_index,
1367 vnet_buffer (p0)->sw_if_index
1368 [VLIB_RX], &next0, p0);
1369 }
1370
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1372 to_next, n_left_to_next,
1373 pi0, next0);
1374 }
Dave Barach75fc8542016-10-11 16:16:02 -04001375
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1377 }
1378
1379 return frame->n_vectors;
1380}
1381
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001382static uword
1383ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1384{
1385 return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1386}
1387
Dave Barachd7cb1b52016-12-09 09:52:16 -05001388/* *INDENT-OFF* */
1389VLIB_REGISTER_NODE (ip6_local_node, static) =
1390{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001391 .function = ip6_local,
1392 .name = "ip6-local",
1393 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001394 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001395 .n_next_nodes = IP_LOCAL_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001396 .next_nodes =
1397 {
Neale Rannsd91c1db2017-07-31 02:30:50 -07001398 [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1399 [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1401 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1402 },
1403};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001404/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001405
Dave Barachd7cb1b52016-12-09 09:52:16 -05001406VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
Damjan Marion1c80e832016-05-11 23:07:18 +02001407
Pierre Pfister1bfd3722017-09-18 11:40:32 +02001408
1409static uword
1410ip6_local_end_of_arc (vlib_main_t * vm,
1411 vlib_node_runtime_t * node, vlib_frame_t * frame)
1412{
1413 return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1414}
1415
1416/* *INDENT-OFF* */
1417VLIB_REGISTER_NODE (ip6_local_end_of_arc_node,static) = {
1418 .function = ip6_local_end_of_arc,
1419 .name = "ip6-local-end-of-arc",
1420 .vector_size = sizeof (u32),
1421
1422 .format_trace = format_ip6_forward_next_trace,
1423 .sibling_of = "ip6-local",
1424};
1425
1426VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_end_of_arc_node, ip6_local_end_of_arc)
1427
1428VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1429 .arc_name = "ip6-local",
1430 .node_name = "ip6-local-end-of-arc",
1431 .runs_before = 0, /* not before any other features */
1432};
1433/* *INDENT-ON* */
1434
Dave Barachd7cb1b52016-12-09 09:52:16 -05001435void
1436ip6_register_protocol (u32 protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001437{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001438 vlib_main_t *vm = vlib_get_main ();
1439 ip6_main_t *im = &ip6_main;
1440 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001441
1442 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001443 lm->local_next_by_ip_protocol[protocol] =
1444 vlib_node_add_next (vm, ip6_local_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001445}
1446
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1449{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001450 vnet_main_t *vnm = vnet_get_main ();
1451 ip6_main_t *im = &ip6_main;
1452 icmp6_neighbor_solicitation_header_t *h;
1453 ip6_address_t *src;
1454 ip_interface_address_t *ia;
1455 ip_adjacency_t *adj;
1456 vnet_hw_interface_t *hi;
1457 vnet_sw_interface_t *si;
1458 vlib_buffer_t *b;
Neale Ranns7a272742017-05-30 02:08:14 -07001459 adj_index_t ai;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001460 u32 bi = 0;
1461 int bogus_length;
1462
1463 si = vnet_get_sw_interface (vnm, sw_if_index);
1464
1465 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1466 {
1467 return clib_error_return (0, "%U: interface %U down",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001468 format_ip6_address, dst,
1469 format_vnet_sw_if_index_name, vnm,
1470 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001471 }
1472
Dave Barachd7cb1b52016-12-09 09:52:16 -05001473 src =
1474 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1475 if (!src)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476 {
1477 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
Dave Barach75fc8542016-10-11 16:16:02 -04001478 return clib_error_return
Dave Barachd7cb1b52016-12-09 09:52:16 -05001479 (0, "no matching interface address for destination %U (interface %U)",
1480 format_ip6_address, dst,
1481 format_vnet_sw_if_index_name, vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001482 }
1483
Dave Barachd7cb1b52016-12-09 09:52:16 -05001484 h =
1485 vlib_packet_template_get_packet (vm,
1486 &im->discover_neighbor_packet_template,
1487 &bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488
1489 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1490
1491 /* Destination address is a solicited node multicast address. We need to fill in
1492 the low 24 bits with low 24 bits of target's address. */
1493 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1494 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1495 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1496
1497 h->ip.src_address = src[0];
1498 h->neighbor.target_address = dst[0];
1499
Pavel Kotucek57808982017-08-02 08:20:19 +02001500 if (PREDICT_FALSE (!hi->hw_address))
1501 {
1502 return clib_error_return (0, "%U: interface %U do not support ip probe",
1503 format_ip6_address, dst,
1504 format_vnet_sw_if_index_name, vnm,
1505 sw_if_index);
1506 }
1507
Dave Barachd7cb1b52016-12-09 09:52:16 -05001508 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1509 vec_len (hi->hw_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001510
Dave Barach75fc8542016-10-11 16:16:02 -04001511 h->neighbor.icmp.checksum =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001512 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001513 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001514
1515 b = vlib_get_buffer (vm, bi);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001516 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1517 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001518
1519 /* Add encapsulation string for software interface (e.g. ethernet header). */
Neale Ranns7a272742017-05-30 02:08:14 -07001520 ip46_address_t nh = {
1521 .ip6 = *dst,
1522 };
1523
1524 ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1525 VNET_LINK_IP6, &nh, sw_if_index);
1526 adj = adj_get (ai);
1527
Dave Barach59b25652017-09-10 15:04:27 -04001528 /* Peer has been previously resolved, retrieve glean adj instead */
1529 if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1530 {
1531 adj_unlock (ai);
Ole Troan6ee40512018-02-12 18:14:39 +01001532 ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1533 VNET_LINK_IP6, sw_if_index, &nh);
Dave Barach59b25652017-09-10 15:04:27 -04001534 adj = adj_get (ai);
1535 }
1536
Ed Warnickecb9cada2015-12-08 15:45:58 -07001537 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1538 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1539
1540 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001541 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1542 u32 *to_next = vlib_frame_vector_args (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543 to_next[0] = bi;
1544 f->n_vectors = 1;
1545 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1546 }
1547
Neale Ranns7a272742017-05-30 02:08:14 -07001548 adj_unlock (ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001549 return /* no error */ 0;
1550}
1551
Dave Barachd7cb1b52016-12-09 09:52:16 -05001552typedef enum
1553{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001554 IP6_REWRITE_NEXT_DROP,
Chris Luke816f3e12016-06-14 16:24:47 -04001555 IP6_REWRITE_NEXT_ICMP_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001556} ip6_rewrite_next_t;
1557
Neale Ranns889fe942017-06-01 05:43:19 -04001558/**
1559 * This bits of an IPv6 address to mask to construct a multicast
1560 * MAC address
1561 */
1562#define IP6_MCAST_ADDR_MASK 0xffffffff
1563
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564always_inline uword
1565ip6_rewrite_inline (vlib_main_t * vm,
1566 vlib_node_runtime_t * node,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001567 vlib_frame_t * frame,
1568 int do_counters, int is_midchain, int is_mcast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001569{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001570 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1571 u32 *from = vlib_frame_vector_args (frame);
1572 u32 n_left_from, n_left_to_next, *to_next, next_index;
1573 vlib_node_runtime_t *error_node =
1574 vlib_node_get_runtime (vm, ip6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575
1576 n_left_from = frame->n_vectors;
1577 next_index = node->cached_next_index;
Damjan Marion586afd72017-04-05 19:18:20 +02001578 u32 thread_index = vlib_get_thread_index ();
Dave Barach75fc8542016-10-11 16:16:02 -04001579
Ed Warnickecb9cada2015-12-08 15:45:58 -07001580 while (n_left_from > 0)
1581 {
1582 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1583
1584 while (n_left_from >= 4 && n_left_to_next >= 2)
1585 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001586 ip_adjacency_t *adj0, *adj1;
1587 vlib_buffer_t *p0, *p1;
1588 ip6_header_t *ip0, *ip1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001589 u32 pi0, rw_len0, next0, error0, adj_index0;
1590 u32 pi1, rw_len1, next1, error1, adj_index1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001591 u32 tx_sw_if_index0, tx_sw_if_index1;
Dave Barach75fc8542016-10-11 16:16:02 -04001592
Ed Warnickecb9cada2015-12-08 15:45:58 -07001593 /* Prefetch next iteration. */
1594 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001595 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001596
1597 p2 = vlib_get_buffer (vm, from[2]);
1598 p3 = vlib_get_buffer (vm, from[3]);
1599
1600 vlib_prefetch_buffer_header (p2, LOAD);
1601 vlib_prefetch_buffer_header (p3, LOAD);
1602
1603 CLIB_PREFETCH (p2->pre_data, 32, STORE);
1604 CLIB_PREFETCH (p3->pre_data, 32, STORE);
1605
1606 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1607 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1608 }
1609
1610 pi0 = to_next[0] = from[0];
1611 pi1 = to_next[1] = from[1];
1612
1613 from += 2;
1614 n_left_from -= 2;
1615 to_next += 2;
1616 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04001617
Ed Warnickecb9cada2015-12-08 15:45:58 -07001618 p0 = vlib_get_buffer (vm, pi0);
1619 p1 = vlib_get_buffer (vm, pi1);
1620
Neale Rannsf06aea52016-11-29 06:51:37 -08001621 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1622 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001623
Ed Warnickecb9cada2015-12-08 15:45:58 -07001624 ip0 = vlib_buffer_get_current (p0);
1625 ip1 = vlib_buffer_get_current (p1);
1626
1627 error0 = error1 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001628 next0 = next1 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001629
Damjan Marion213b5aa2017-07-13 21:19:27 +02001630 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001631 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001632 i32 hop_limit0 = ip0->hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001633
1634 /* Input node should have reject packets with hop limit 0. */
1635 ASSERT (ip0->hop_limit > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001636
1637 hop_limit0 -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001638
1639 ip0->hop_limit = hop_limit0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001640
Dave Barachd7cb1b52016-12-09 09:52:16 -05001641 /*
1642 * If the hop count drops below 1 when forwarding, generate
1643 * an ICMP response.
1644 */
1645 if (PREDICT_FALSE (hop_limit0 <= 0))
1646 {
1647 error0 = IP6_ERROR_TIME_EXPIRED;
1648 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1649 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1650 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1651 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1652 0);
1653 }
Neale Rannsf06aea52016-11-29 06:51:37 -08001654 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001655 else
1656 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001657 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001658 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001659 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001660 {
Neale Rannsf06aea52016-11-29 06:51:37 -08001661 i32 hop_limit1 = ip1->hop_limit;
1662
1663 /* Input node should have reject packets with hop limit 0. */
1664 ASSERT (ip1->hop_limit > 0);
1665
1666 hop_limit1 -= 1;
1667
1668 ip1->hop_limit = hop_limit1;
1669
Dave Barachd7cb1b52016-12-09 09:52:16 -05001670 /*
1671 * If the hop count drops below 1 when forwarding, generate
1672 * an ICMP response.
1673 */
1674 if (PREDICT_FALSE (hop_limit1 <= 0))
1675 {
1676 error1 = IP6_ERROR_TIME_EXPIRED;
1677 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1678 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1679 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1680 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1681 0);
1682 }
1683 }
1684 else
1685 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001686 p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001687 }
Neale Ranns107e7d42017-04-11 09:55:19 -07001688 adj0 = adj_get (adj_index0);
1689 adj1 = adj_get (adj_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691 rw_len0 = adj0[0].rewrite_header.data_bytes;
1692 rw_len1 = adj1[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001693 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1694 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001695
Neale Ranns9c6a6132017-02-21 05:33:14 -08001696 if (do_counters)
1697 {
1698 vlib_increment_combined_counter
1699 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001700 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001701 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1702 vlib_increment_combined_counter
1703 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001704 thread_index, adj_index1, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001705 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1706 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001707
1708 /* Check MTU of outgoing interface. */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001709 error0 =
1710 (vlib_buffer_length_in_chain (vm, p0) >
1711 adj0[0].
1712 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1713 error0);
1714 error1 =
1715 (vlib_buffer_length_in_chain (vm, p1) >
1716 adj1[0].
1717 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1718 error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001719
Dave Barachd7cb1b52016-12-09 09:52:16 -05001720 /* Don't adjust the buffer for hop count issue; icmp-error node
1721 * wants to see the IP headerr */
1722 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1723 {
1724 p0->current_data -= rw_len0;
1725 p0->current_length += rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726
Dave Barachd7cb1b52016-12-09 09:52:16 -05001727 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1728 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1729 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001730
Neale Rannsb069a692017-03-15 12:34:25 -04001731 if (PREDICT_FALSE
1732 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1733 vnet_feature_arc_start (lm->output_feature_arc_index,
1734 tx_sw_if_index0, &next0, p0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001735 }
1736 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1737 {
1738 p1->current_data -= rw_len1;
1739 p1->current_length += rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001740
Dave Barachd7cb1b52016-12-09 09:52:16 -05001741 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1742 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1743 next1 = adj1[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001744
Neale Rannsb069a692017-03-15 12:34:25 -04001745 if (PREDICT_FALSE
1746 (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1747 vnet_feature_arc_start (lm->output_feature_arc_index,
1748 tx_sw_if_index1, &next1, p1);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001749 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001750
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001751 /* Guess we are only writing on simple Ethernet header. */
1752 vnet_rewrite_two_headers (adj0[0], adj1[0],
1753 ip0, ip1, sizeof (ethernet_header_t));
1754
1755 if (is_midchain)
1756 {
1757 adj0->sub_type.midchain.fixup_func
1758 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1759 adj1->sub_type.midchain.fixup_func
1760 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
1761 }
1762 if (is_mcast)
1763 {
1764 /*
1765 * copy bytes from the IP address into the MAC rewrite
1766 */
Neale Ranns889fe942017-06-01 05:43:19 -04001767 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1768 adj0->
1769 rewrite_header.dst_mcast_offset,
1770 &ip0->dst_address.as_u32[3],
1771 (u8 *) ip0);
1772 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1773 adj1->
1774 rewrite_header.dst_mcast_offset,
1775 &ip1->dst_address.as_u32[3],
1776 (u8 *) ip1);
Neale Ranns32e1c012016-11-22 17:07:28 +00001777 }
Neale Ranns5e575b12016-10-03 09:40:25 +01001778
Ed Warnickecb9cada2015-12-08 15:45:58 -07001779 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1780 to_next, n_left_to_next,
1781 pi0, pi1, next0, next1);
1782 }
1783
1784 while (n_left_from > 0 && n_left_to_next > 0)
1785 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001786 ip_adjacency_t *adj0;
1787 vlib_buffer_t *p0;
1788 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001789 u32 pi0, rw_len0;
1790 u32 adj_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001791 u32 tx_sw_if_index0;
Dave Barach75fc8542016-10-11 16:16:02 -04001792
Ed Warnickecb9cada2015-12-08 15:45:58 -07001793 pi0 = to_next[0] = from[0];
1794
1795 p0 = vlib_get_buffer (vm, pi0);
1796
Neale Rannsf06aea52016-11-29 06:51:37 -08001797 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001798
Neale Ranns107e7d42017-04-11 09:55:19 -07001799 adj0 = adj_get (adj_index0);
Dave Barach75fc8542016-10-11 16:16:02 -04001800
Ed Warnickecb9cada2015-12-08 15:45:58 -07001801 ip0 = vlib_buffer_get_current (p0);
1802
1803 error0 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001804 next0 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001805
1806 /* Check hop limit */
Damjan Marion213b5aa2017-07-13 21:19:27 +02001807 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001808 {
1809 i32 hop_limit0 = ip0->hop_limit;
1810
1811 ASSERT (ip0->hop_limit > 0);
1812
1813 hop_limit0 -= 1;
1814
1815 ip0->hop_limit = hop_limit0;
1816
Dave Barachd7cb1b52016-12-09 09:52:16 -05001817 if (PREDICT_FALSE (hop_limit0 <= 0))
1818 {
1819 /*
1820 * If the hop count drops below 1 when forwarding, generate
1821 * an ICMP response.
1822 */
1823 error0 = IP6_ERROR_TIME_EXPIRED;
1824 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1825 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1826 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1827 ICMP6_time_exceeded_ttl_exceeded_in_transit,
1828 0);
1829 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001830 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001831 else
1832 {
Damjan Marion213b5aa2017-07-13 21:19:27 +02001833 p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001834 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001835
Ed Warnickecb9cada2015-12-08 15:45:58 -07001836 /* Guess we are only writing on simple Ethernet header. */
1837 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
Dave Barach75fc8542016-10-11 16:16:02 -04001838
Ed Warnickecb9cada2015-12-08 15:45:58 -07001839 /* Update packet buffer attributes/set output interface. */
1840 rw_len0 = adj0[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001841 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001842
Neale Ranns9c6a6132017-02-21 05:33:14 -08001843 if (do_counters)
1844 {
1845 vlib_increment_combined_counter
1846 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +02001847 thread_index, adj_index0, 1,
Neale Ranns9c6a6132017-02-21 05:33:14 -08001848 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1849 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001850
1851 /* Check MTU of outgoing interface. */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001852 error0 =
1853 (vlib_buffer_length_in_chain (vm, p0) >
1854 adj0[0].
1855 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1856 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001857
Dave Barachd7cb1b52016-12-09 09:52:16 -05001858 /* Don't adjust the buffer for hop count issue; icmp-error node
1859 * wants to see the IP headerr */
1860 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1861 {
Chris Luke816f3e12016-06-14 16:24:47 -04001862 p0->current_data -= rw_len0;
1863 p0->current_length += rw_len0;
1864
Dave Barachd7cb1b52016-12-09 09:52:16 -05001865 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
Dave Barach5331c722016-08-17 11:54:30 -04001866
Dave Barachd7cb1b52016-12-09 09:52:16 -05001867 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1868 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04001869
Neale Rannsb069a692017-03-15 12:34:25 -04001870 if (PREDICT_FALSE
1871 (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1872 vnet_feature_arc_start (lm->output_feature_arc_index,
1873 tx_sw_if_index0, &next0, p0);
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001874 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001875
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001876 if (is_midchain)
1877 {
1878 adj0->sub_type.midchain.fixup_func
1879 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1880 }
1881 if (is_mcast)
1882 {
Neale Ranns889fe942017-06-01 05:43:19 -04001883 vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1884 adj0->
1885 rewrite_header.dst_mcast_offset,
1886 &ip0->dst_address.as_u32[3],
1887 (u8 *) ip0);
Neale Ranns32e1c012016-11-22 17:07:28 +00001888 }
Neale Ranns5e575b12016-10-03 09:40:25 +01001889
Ed Warnickecb9cada2015-12-08 15:45:58 -07001890 p0->error = error_node->errors[error0];
1891
1892 from += 1;
1893 n_left_from -= 1;
1894 to_next += 1;
1895 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04001896
Ed Warnickecb9cada2015-12-08 15:45:58 -07001897 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1898 to_next, n_left_to_next,
1899 pi0, next0);
1900 }
1901
1902 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1903 }
1904
1905 /* Need to do trace after rewrites to pick up new packet data. */
1906 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Rannsf06aea52016-11-29 06:51:37 -08001907 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001908
1909 return frame->n_vectors;
1910}
1911
1912static uword
Neale Rannsf06aea52016-11-29 06:51:37 -08001913ip6_rewrite (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001914 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001915{
Neale Ranns9c6a6132017-02-21 05:33:14 -08001916 if (adj_are_counters_enabled ())
1917 return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
1918 else
1919 return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
Neale Ranns32e1c012016-11-22 17:07:28 +00001920}
1921
1922static uword
1923ip6_rewrite_mcast (vlib_main_t * vm,
1924 vlib_node_runtime_t * node, vlib_frame_t * frame)
1925{
Neale Ranns9c6a6132017-02-21 05:33:14 -08001926 if (adj_are_counters_enabled ())
1927 return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
1928 else
1929 return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001930}
1931
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001932static uword
1933ip6_midchain (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001934 vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001935{
Neale Ranns9c6a6132017-02-21 05:33:14 -08001936 if (adj_are_counters_enabled ())
1937 return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
1938 else
1939 return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001940}
1941
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001942static uword
1943ip6_mcast_midchain (vlib_main_t * vm,
1944 vlib_node_runtime_t * node, vlib_frame_t * frame)
1945{
1946 if (adj_are_counters_enabled ())
1947 return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
1948 else
Neale Ranns9f171f52017-04-11 08:56:53 -07001949 return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001950}
1951
Dave Barachd7cb1b52016-12-09 09:52:16 -05001952/* *INDENT-OFF* */
1953VLIB_REGISTER_NODE (ip6_midchain_node) =
1954{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001955 .function = ip6_midchain,
1956 .name = "ip6-midchain",
1957 .vector_size = sizeof (u32),
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001958 .format_trace = format_ip6_forward_next_trace,
Neale Ranns5e575b12016-10-03 09:40:25 +01001959 .sibling_of = "ip6-rewrite",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001960 };
1961/* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001962
Dave Barachd7cb1b52016-12-09 09:52:16 -05001963VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001964
Dave Barachd7cb1b52016-12-09 09:52:16 -05001965/* *INDENT-OFF* */
1966VLIB_REGISTER_NODE (ip6_rewrite_node) =
1967{
Neale Rannsf06aea52016-11-29 06:51:37 -08001968 .function = ip6_rewrite,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001969 .name = "ip6-rewrite",
1970 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +01001971 .format_trace = format_ip6_rewrite_trace,
Chris Luke816f3e12016-06-14 16:24:47 -04001972 .n_next_nodes = 2,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001973 .next_nodes =
1974 {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08001975 [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
Chris Luke816f3e12016-06-14 16:24:47 -04001976 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001977 },
1978};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001979/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001980
Neale Rannsf06aea52016-11-29 06:51:37 -08001981VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
Damjan Marion1c80e832016-05-11 23:07:18 +02001982
Neale Ranns32e1c012016-11-22 17:07:28 +00001983/* *INDENT-OFF* */
1984VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
1985{
1986 .function = ip6_rewrite_mcast,
1987 .name = "ip6-rewrite-mcast",
1988 .vector_size = sizeof (u32),
1989 .format_trace = format_ip6_rewrite_trace,
1990 .sibling_of = "ip6-rewrite",
1991};
1992/* *INDENT-ON* */
1993
1994VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
1995
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001996/* *INDENT-OFF* */
1997VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
1998{
1999 .function = ip6_mcast_midchain,
2000 .name = "ip6-mcast-midchain",
2001 .vector_size = sizeof (u32),
2002 .format_trace = format_ip6_rewrite_trace,
2003 .sibling_of = "ip6-rewrite",
2004};
2005/* *INDENT-ON* */
2006
2007VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
2008
Ole Troan944f5482016-05-24 11:56:58 +02002009/*
2010 * Hop-by-Hop handling
2011 */
Ole Troan944f5482016-05-24 11:56:58 +02002012ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2013
2014#define foreach_ip6_hop_by_hop_error \
2015_(PROCESSED, "pkts with ip6 hop-by-hop options") \
2016_(FORMAT, "incorrectly formatted hop-by-hop options") \
2017_(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2018
Neale Ranns32e1c012016-11-22 17:07:28 +00002019/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002020typedef enum
2021{
Ole Troan944f5482016-05-24 11:56:58 +02002022#define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2023 foreach_ip6_hop_by_hop_error
2024#undef _
Neale Ranns32e1c012016-11-22 17:07:28 +00002025 IP6_HOP_BY_HOP_N_ERROR,
Ole Troan944f5482016-05-24 11:56:58 +02002026} ip6_hop_by_hop_error_t;
Neale Ranns32e1c012016-11-22 17:07:28 +00002027/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002028
2029/*
2030 * Primary h-b-h handler trace support
2031 * We work pretty hard on the problem for obvious reasons
2032 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002033typedef struct
2034{
Ole Troan944f5482016-05-24 11:56:58 +02002035 u32 next_index;
2036 u32 trace_len;
2037 u8 option_data[256];
2038} ip6_hop_by_hop_trace_t;
2039
2040vlib_node_registration_t ip6_hop_by_hop_node;
2041
Dave Barachd7cb1b52016-12-09 09:52:16 -05002042static char *ip6_hop_by_hop_error_strings[] = {
Ole Troan944f5482016-05-24 11:56:58 +02002043#define _(sym,string) string,
2044 foreach_ip6_hop_by_hop_error
2045#undef _
2046};
2047
Shwetha Bhandari78372a92017-01-18 12:43:54 +05302048u8 *
2049format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2050{
2051 ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2052 int total_len = va_arg (*args, int);
2053 ip6_hop_by_hop_option_t *opt0, *limit0;
2054 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2055 u8 type0;
2056
2057 s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2058 hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2059
2060 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2061 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2062
2063 while (opt0 < limit0)
2064 {
2065 type0 = opt0->type;
2066 switch (type0)
2067 {
2068 case 0: /* Pad, just stop */
2069 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2070 break;
2071
2072 default:
2073 if (hm->trace[type0])
2074 {
2075 s = (*hm->trace[type0]) (s, opt0);
2076 }
2077 else
2078 {
2079 s =
2080 format (s, "\n unrecognized option %d length %d", type0,
2081 opt0->length);
2082 }
2083 opt0 =
2084 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2085 sizeof (ip6_hop_by_hop_option_t));
2086 break;
2087 }
2088 }
2089 return s;
2090}
2091
Ole Troan944f5482016-05-24 11:56:58 +02002092static u8 *
2093format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2094{
2095 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2096 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002097 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
Ole Troan944f5482016-05-24 11:56:58 +02002098 ip6_hop_by_hop_header_t *hbh0;
2099 ip6_hop_by_hop_option_t *opt0, *limit0;
2100 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2101
2102 u8 type0;
2103
Dave Barachd7cb1b52016-12-09 09:52:16 -05002104 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
Ole Troan944f5482016-05-24 11:56:58 +02002105
2106 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002107 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
Ole Troan944f5482016-05-24 11:56:58 +02002108
Dave Barachd7cb1b52016-12-09 09:52:16 -05002109 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2110 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
Ole Troan944f5482016-05-24 11:56:58 +02002111
Dave Barachd7cb1b52016-12-09 09:52:16 -05002112 while (opt0 < limit0)
2113 {
2114 type0 = opt0->type;
2115 switch (type0)
2116 {
2117 case 0: /* Pad, just stop */
2118 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2119 break;
Ole Troan944f5482016-05-24 11:56:58 +02002120
Dave Barachd7cb1b52016-12-09 09:52:16 -05002121 default:
2122 if (hm->trace[type0])
2123 {
2124 s = (*hm->trace[type0]) (s, opt0);
2125 }
2126 else
2127 {
2128 s =
2129 format (s, "\n unrecognized option %d length %d", type0,
2130 opt0->length);
2131 }
2132 opt0 =
2133 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2134 sizeof (ip6_hop_by_hop_option_t));
2135 break;
2136 }
Ole Troan944f5482016-05-24 11:56:58 +02002137 }
Ole Troan944f5482016-05-24 11:56:58 +02002138 return s;
2139}
2140
Dave Barachd7cb1b52016-12-09 09:52:16 -05002141always_inline u8
2142ip6_scan_hbh_options (vlib_buffer_t * b0,
2143 ip6_header_t * ip0,
2144 ip6_hop_by_hop_header_t * hbh0,
2145 ip6_hop_by_hop_option_t * opt0,
2146 ip6_hop_by_hop_option_t * limit0, u32 * next0)
Shwethaa91cbe62016-08-08 15:51:04 +01002147{
2148 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2149 u8 type0;
2150 u8 error0 = 0;
2151
2152 while (opt0 < limit0)
2153 {
2154 type0 = opt0->type;
2155 switch (type0)
2156 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002157 case 0: /* Pad1 */
2158 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
Shwethaa91cbe62016-08-08 15:51:04 +01002159 continue;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002160 case 1: /* PadN */
Shwethaa91cbe62016-08-08 15:51:04 +01002161 break;
2162 default:
2163 if (hm->options[type0])
2164 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002165 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2166 {
Shwethaa91cbe62016-08-08 15:51:04 +01002167 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002168 return (error0);
2169 }
Shwethaa91cbe62016-08-08 15:51:04 +01002170 }
2171 else
2172 {
2173 /* Unrecognized mandatory option, check the two high order bits */
2174 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2175 {
2176 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2177 break;
2178 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2179 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2180 *next0 = IP_LOOKUP_NEXT_DROP;
2181 break;
2182 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2183 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2184 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002185 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2186 ICMP6_parameter_problem_unrecognized_option,
2187 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002188 break;
2189 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2190 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002191 if (!ip6_address_is_multicast (&ip0->dst_address))
Shwethaa91cbe62016-08-08 15:51:04 +01002192 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002193 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2194 icmp6_error_set_vnet_buffer (b0,
2195 ICMP6_parameter_problem,
2196 ICMP6_parameter_problem_unrecognized_option,
2197 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002198 }
2199 else
2200 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002201 *next0 = IP_LOOKUP_NEXT_DROP;
Shwethaa91cbe62016-08-08 15:51:04 +01002202 }
2203 break;
2204 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002205 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002206 }
2207 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002208 opt0 =
2209 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2210 sizeof (ip6_hop_by_hop_option_t));
Shwethaa91cbe62016-08-08 15:51:04 +01002211 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002212 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002213}
2214
Ole Troan944f5482016-05-24 11:56:58 +02002215/*
2216 * Process the Hop-by-Hop Options header
2217 */
2218static uword
2219ip6_hop_by_hop (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002220 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ole Troan944f5482016-05-24 11:56:58 +02002221{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002222 vlib_node_runtime_t *error_node =
2223 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
Ole Troan944f5482016-05-24 11:56:58 +02002224 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2225 u32 n_left_from, *from, *to_next;
2226 ip_lookup_next_t next_index;
Ole Troan944f5482016-05-24 11:56:58 +02002227
2228 from = vlib_frame_vector_args (frame);
2229 n_left_from = frame->n_vectors;
2230 next_index = node->cached_next_index;
2231
Dave Barachd7cb1b52016-12-09 09:52:16 -05002232 while (n_left_from > 0)
2233 {
2234 u32 n_left_to_next;
Ole Troan944f5482016-05-24 11:56:58 +02002235
Dave Barachd7cb1b52016-12-09 09:52:16 -05002236 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ole Troan944f5482016-05-24 11:56:58 +02002237
Dave Barachd7cb1b52016-12-09 09:52:16 -05002238 while (n_left_from >= 4 && n_left_to_next >= 2)
Shwethaa91cbe62016-08-08 15:51:04 +01002239 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002240 u32 bi0, bi1;
2241 vlib_buffer_t *b0, *b1;
2242 u32 next0, next1;
2243 ip6_header_t *ip0, *ip1;
2244 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2245 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2246 u8 error0 = 0, error1 = 0;
2247
2248 /* Prefetch next iteration. */
2249 {
2250 vlib_buffer_t *p2, *p3;
2251
2252 p2 = vlib_get_buffer (vm, from[2]);
2253 p3 = vlib_get_buffer (vm, from[3]);
2254
2255 vlib_prefetch_buffer_header (p2, LOAD);
2256 vlib_prefetch_buffer_header (p3, LOAD);
2257
2258 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2259 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Shwethaa91cbe62016-08-08 15:51:04 +01002260 }
2261
Dave Barachd7cb1b52016-12-09 09:52:16 -05002262 /* Speculatively enqueue b0, b1 to the current next frame */
2263 to_next[0] = bi0 = from[0];
2264 to_next[1] = bi1 = from[1];
2265 from += 2;
2266 to_next += 2;
2267 n_left_from -= 2;
2268 n_left_to_next -= 2;
2269
2270 b0 = vlib_get_buffer (vm, bi0);
2271 b1 = vlib_get_buffer (vm, bi1);
2272
2273 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2274 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002275 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002276 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002277 ip_adjacency_t *adj1 = adj_get (adj_index1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002278
2279 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2280 next0 = adj0->lookup_next_index;
2281 next1 = adj1->lookup_next_index;
2282
2283 ip0 = vlib_buffer_get_current (b0);
2284 ip1 = vlib_buffer_get_current (b1);
2285 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2286 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2287 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2288 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2289 limit0 =
2290 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2291 ((hbh0->length + 1) << 3));
2292 limit1 =
2293 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2294 ((hbh1->length + 1) << 3));
2295
2296 /*
2297 * Basic validity checks
2298 */
2299 if ((hbh0->length + 1) << 3 >
2300 clib_net_to_host_u16 (ip0->payload_length))
2301 {
2302 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2303 next0 = IP_LOOKUP_NEXT_DROP;
2304 goto outdual;
2305 }
2306 /* Scan the set of h-b-h options, process ones that we understand */
2307 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2308
2309 if ((hbh1->length + 1) << 3 >
2310 clib_net_to_host_u16 (ip1->payload_length))
2311 {
2312 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2313 next1 = IP_LOOKUP_NEXT_DROP;
2314 goto outdual;
2315 }
2316 /* Scan the set of h-b-h options, process ones that we understand */
2317 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2318
2319 outdual:
2320 /* Has the classifier flagged this buffer for special treatment? */
2321 if (PREDICT_FALSE
2322 ((error0 == 0)
2323 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2324 next0 = hm->next_override;
2325
2326 /* Has the classifier flagged this buffer for special treatment? */
2327 if (PREDICT_FALSE
2328 ((error1 == 0)
2329 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2330 next1 = hm->next_override;
2331
2332 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2333 {
2334 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2335 {
2336 ip6_hop_by_hop_trace_t *t =
2337 vlib_add_trace (vm, node, b0, sizeof (*t));
2338 u32 trace_len = (hbh0->length + 1) << 3;
2339 t->next_index = next0;
2340 /* Capture the h-b-h option verbatim */
2341 trace_len =
2342 trace_len <
2343 ARRAY_LEN (t->option_data) ? trace_len :
2344 ARRAY_LEN (t->option_data);
2345 t->trace_len = trace_len;
2346 clib_memcpy (t->option_data, hbh0, trace_len);
2347 }
2348 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2349 {
2350 ip6_hop_by_hop_trace_t *t =
2351 vlib_add_trace (vm, node, b1, sizeof (*t));
2352 u32 trace_len = (hbh1->length + 1) << 3;
2353 t->next_index = next1;
2354 /* Capture the h-b-h option verbatim */
2355 trace_len =
2356 trace_len <
2357 ARRAY_LEN (t->option_data) ? trace_len :
2358 ARRAY_LEN (t->option_data);
2359 t->trace_len = trace_len;
2360 clib_memcpy (t->option_data, hbh1, trace_len);
2361 }
2362
2363 }
2364
2365 b0->error = error_node->errors[error0];
2366 b1->error = error_node->errors[error1];
2367
2368 /* verify speculative enqueue, maybe switch current next frame */
2369 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2370 n_left_to_next, bi0, bi1, next0,
2371 next1);
Shwethaa91cbe62016-08-08 15:51:04 +01002372 }
2373
Dave Barachd7cb1b52016-12-09 09:52:16 -05002374 while (n_left_from > 0 && n_left_to_next > 0)
2375 {
2376 u32 bi0;
2377 vlib_buffer_t *b0;
2378 u32 next0;
2379 ip6_header_t *ip0;
2380 ip6_hop_by_hop_header_t *hbh0;
2381 ip6_hop_by_hop_option_t *opt0, *limit0;
2382 u8 error0 = 0;
Shwethaa91cbe62016-08-08 15:51:04 +01002383
Dave Barachd7cb1b52016-12-09 09:52:16 -05002384 /* Speculatively enqueue b0 to the current next frame */
2385 bi0 = from[0];
2386 to_next[0] = bi0;
2387 from += 1;
2388 to_next += 1;
2389 n_left_from -= 1;
2390 n_left_to_next -= 1;
2391
2392 b0 = vlib_get_buffer (vm, bi0);
2393 /*
2394 * Default use the next_index from the adjacency.
2395 * A HBH option rarely redirects to a different node
2396 */
2397 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
Neale Ranns107e7d42017-04-11 09:55:19 -07002398 ip_adjacency_t *adj0 = adj_get (adj_index0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002399 next0 = adj0->lookup_next_index;
2400
2401 ip0 = vlib_buffer_get_current (b0);
2402 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2403 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2404 limit0 =
2405 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2406 ((hbh0->length + 1) << 3));
2407
2408 /*
2409 * Basic validity checks
2410 */
2411 if ((hbh0->length + 1) << 3 >
2412 clib_net_to_host_u16 (ip0->payload_length))
2413 {
2414 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2415 next0 = IP_LOOKUP_NEXT_DROP;
2416 goto out0;
2417 }
2418
2419 /* Scan the set of h-b-h options, process ones that we understand */
2420 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2421
2422 out0:
2423 /* Has the classifier flagged this buffer for special treatment? */
2424 if (PREDICT_FALSE
2425 ((error0 == 0)
2426 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2427 next0 = hm->next_override;
2428
2429 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2430 {
2431 ip6_hop_by_hop_trace_t *t =
2432 vlib_add_trace (vm, node, b0, sizeof (*t));
2433 u32 trace_len = (hbh0->length + 1) << 3;
2434 t->next_index = next0;
2435 /* Capture the h-b-h option verbatim */
2436 trace_len =
2437 trace_len <
2438 ARRAY_LEN (t->option_data) ? trace_len :
2439 ARRAY_LEN (t->option_data);
2440 t->trace_len = trace_len;
2441 clib_memcpy (t->option_data, hbh0, trace_len);
2442 }
2443
2444 b0->error = error_node->errors[error0];
2445
2446 /* verify speculative enqueue, maybe switch current next frame */
2447 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2448 n_left_to_next, bi0, next0);
2449 }
2450 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Shwethaa91cbe62016-08-08 15:51:04 +01002451 }
Ole Troan944f5482016-05-24 11:56:58 +02002452 return frame->n_vectors;
2453}
2454
Dave Barachd7cb1b52016-12-09 09:52:16 -05002455/* *INDENT-OFF* */
2456VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2457{
Ole Troan944f5482016-05-24 11:56:58 +02002458 .function = ip6_hop_by_hop,
2459 .name = "ip6-hop-by-hop",
Ole Troan964f93e2016-06-10 13:22:36 +02002460 .sibling_of = "ip6-lookup",
Ole Troan944f5482016-05-24 11:56:58 +02002461 .vector_size = sizeof (u32),
2462 .format_trace = format_ip6_hop_by_hop_trace,
2463 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002464 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
Ole Troan944f5482016-05-24 11:56:58 +02002465 .error_strings = ip6_hop_by_hop_error_strings,
Ole Troan964f93e2016-06-10 13:22:36 +02002466 .n_next_nodes = 0,
Ole Troan944f5482016-05-24 11:56:58 +02002467};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002468/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002469
Dave Barach5331c722016-08-17 11:54:30 -04002470VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
Ole Troan944f5482016-05-24 11:56:58 +02002471
2472static clib_error_t *
2473ip6_hop_by_hop_init (vlib_main_t * vm)
2474{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002475 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2476 memset (hm->options, 0, sizeof (hm->options));
2477 memset (hm->trace, 0, sizeof (hm->trace));
Shwethaa91cbe62016-08-08 15:51:04 +01002478 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
Ole Troan944f5482016-05-24 11:56:58 +02002479 return (0);
2480}
2481
2482VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2483
Dave Barachd7cb1b52016-12-09 09:52:16 -05002484void
2485ip6_hbh_set_next_override (uword next)
Shwethaa91cbe62016-08-08 15:51:04 +01002486{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002487 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Shwethaa91cbe62016-08-08 15:51:04 +01002488
2489 hm->next_override = next;
2490}
2491
Ole Troan944f5482016-05-24 11:56:58 +02002492int
2493ip6_hbh_register_option (u8 option,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002494 int options (vlib_buffer_t * b, ip6_header_t * ip,
2495 ip6_hop_by_hop_option_t * opt),
2496 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
Ole Troan944f5482016-05-24 11:56:58 +02002497{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002498 ip6_main_t *im = &ip6_main;
2499 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002500
Neale Ranns756cd942018-04-06 09:18:11 -07002501 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002502
2503 /* Already registered */
2504 if (hm->options[option])
2505 return (-1);
2506
2507 hm->options[option] = options;
2508 hm->trace[option] = trace;
2509
2510 /* Set global variable */
2511 im->hbh_enabled = 1;
2512
2513 return (0);
2514}
2515
2516int
2517ip6_hbh_unregister_option (u8 option)
2518{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002519 ip6_main_t *im = &ip6_main;
2520 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002521
Neale Ranns756cd942018-04-06 09:18:11 -07002522 ASSERT ((u32) option < ARRAY_LEN (hm->options));
Ole Troan944f5482016-05-24 11:56:58 +02002523
2524 /* Not registered */
2525 if (!hm->options[option])
2526 return (-1);
2527
2528 hm->options[option] = NULL;
2529 hm->trace[option] = NULL;
2530
2531 /* Disable global knob if this was the last option configured */
2532 int i;
2533 bool found = false;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002534 for (i = 0; i < 256; i++)
2535 {
2536 if (hm->options[option])
2537 {
2538 found = true;
2539 break;
2540 }
Ole Troan944f5482016-05-24 11:56:58 +02002541 }
Ole Troan944f5482016-05-24 11:56:58 +02002542 if (!found)
2543 im->hbh_enabled = 0;
2544
2545 return (0);
2546}
2547
Ed Warnickecb9cada2015-12-08 15:45:58 -07002548/* Global IP6 main. */
2549ip6_main_t ip6_main;
2550
2551static clib_error_t *
2552ip6_lookup_init (vlib_main_t * vm)
2553{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002554 ip6_main_t *im = &ip6_main;
2555 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002556 uword i;
2557
Damjan Marion8b3191e2016-11-09 19:54:20 +01002558 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2559 return error;
2560
Ed Warnickecb9cada2015-12-08 15:45:58 -07002561 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2562 {
2563 u32 j, i0, i1;
2564
2565 i0 = i / 32;
2566 i1 = i % 32;
2567
2568 for (j = 0; j < i0; j++)
2569 im->fib_masks[i].as_u32[j] = ~0;
2570
2571 if (i1)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002572 im->fib_masks[i].as_u32[i0] =
2573 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002574 }
2575
2576 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2577
2578 if (im->lookup_table_nbuckets == 0)
2579 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2580
Dave Barachd7cb1b52016-12-09 09:52:16 -05002581 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002582
2583 if (im->lookup_table_size == 0)
2584 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
Dave Barach75fc8542016-10-11 16:16:02 -04002585
Dave Barachd7cb1b52016-12-09 09:52:16 -05002586 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2587 "ip6 FIB fwding table",
2588 im->lookup_table_nbuckets, im->lookup_table_size);
2589 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2590 "ip6 FIB non-fwding table",
2591 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002592
Ed Warnickecb9cada2015-12-08 15:45:58 -07002593 /* Create FIB with index 0 and table id of 0. */
Neale Ranns15002542017-09-10 04:39:11 -07002594 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2595 FIB_SOURCE_DEFAULT_ROUTE);
2596 mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2597 MFIB_SOURCE_DEFAULT_ROUTE);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002598
2599 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002600 pg_node_t *pn;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002601 pn = pg_get_node (ip6_lookup_node.index);
2602 pn->unformat_edit = unformat_pg_ip6_header;
2603 }
2604
Ole Troan944f5482016-05-24 11:56:58 +02002605 /* Unless explicitly configured, don't process HBH options */
2606 im->hbh_enabled = 0;
2607
Ed Warnickecb9cada2015-12-08 15:45:58 -07002608 {
2609 icmp6_neighbor_solicitation_header_t p;
2610
2611 memset (&p, 0, sizeof (p));
2612
Dave Barachd7cb1b52016-12-09 09:52:16 -05002613 p.ip.ip_version_traffic_class_and_flow_label =
2614 clib_host_to_net_u32 (0x6 << 28);
2615 p.ip.payload_length =
2616 clib_host_to_net_u16 (sizeof (p) -
2617 STRUCT_OFFSET_OF
2618 (icmp6_neighbor_solicitation_header_t, neighbor));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002619 p.ip.protocol = IP_PROTOCOL_ICMP6;
2620 p.ip.hop_limit = 255;
2621 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2622
2623 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2624
Dave Barachd7cb1b52016-12-09 09:52:16 -05002625 p.link_layer_option.header.type =
2626 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2627 p.link_layer_option.header.n_data_u64s =
2628 sizeof (p.link_layer_option) / sizeof (u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002629
2630 vlib_packet_template_init (vm,
2631 &im->discover_neighbor_packet_template,
2632 &p, sizeof (p),
2633 /* alloc chunk size */ 8,
2634 "ip6 neighbor discovery");
2635 }
2636
Dave Barach203c6322016-06-26 10:29:03 -04002637 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002638}
2639
2640VLIB_INIT_FUNCTION (ip6_lookup_init);
2641
Dave Barach75fc8542016-10-11 16:16:02 -04002642void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002643ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
2644 u8 * mac)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002645{
2646 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2647 /* Invert the "u" bit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002648 ip->as_u8[8] = mac[0] ^ (1 << 1);
2649 ip->as_u8[9] = mac[1];
2650 ip->as_u8[10] = mac[2];
2651 ip->as_u8[11] = 0xFF;
2652 ip->as_u8[12] = 0xFE;
2653 ip->as_u8[13] = mac[3];
2654 ip->as_u8[14] = mac[4];
2655 ip->as_u8[15] = mac[5];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002656}
2657
Dave Barach75fc8542016-10-11 16:16:02 -04002658void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002659ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
2660 ip6_address_t * ip)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002661{
2662 /* Invert the previously inverted "u" bit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002663 mac[0] = ip->as_u8[8] ^ (1 << 1);
2664 mac[1] = ip->as_u8[9];
2665 mac[2] = ip->as_u8[10];
2666 mac[3] = ip->as_u8[13];
2667 mac[4] = ip->as_u8[14];
2668 mac[5] = ip->as_u8[15];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002669}
2670
Dave Barach75fc8542016-10-11 16:16:02 -04002671static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07002672test_ip6_link_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002673 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002674{
2675 u8 mac[6];
2676 ip6_address_t _a, *a = &_a;
2677
2678 if (unformat (input, "%U", unformat_ethernet_address, mac))
2679 {
2680 ip6_link_local_address_from_ethernet_mac_address (a, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002681 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002682 ip6_ethernet_mac_address_from_link_local_address (mac, a);
2683 vlib_cli_output (vm, "Original MAC address: %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002684 format_ethernet_address, mac);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002685 }
Dave Barach75fc8542016-10-11 16:16:02 -04002686
Ed Warnickecb9cada2015-12-08 15:45:58 -07002687 return 0;
2688}
2689
Billy McFall0683c9c2016-10-13 08:27:31 -04002690/*?
2691 * This command converts the given MAC Address into an IPv6 link-local
2692 * address.
2693 *
2694 * @cliexpar
2695 * Example of how to create an IPv6 link-local address:
2696 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2697 * Link local address: fe80::14d9:e0ff:fe91:7986
2698 * Original MAC address: 16:d9:e0:91:79:86
2699 * @cliexend
2700?*/
2701/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002702VLIB_CLI_COMMAND (test_link_command, static) =
2703{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002704 .path = "test ip6 link",
Dave Barach75fc8542016-10-11 16:16:02 -04002705 .function = test_ip6_link_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002706 .short_help = "test ip6 link <mac-address>",
2707};
Billy McFall0683c9c2016-10-13 08:27:31 -04002708/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002709
Dave Barachd7cb1b52016-12-09 09:52:16 -05002710int
2711vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002712{
Neale Ranns107e7d42017-04-11 09:55:19 -07002713 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002714
Neale Ranns107e7d42017-04-11 09:55:19 -07002715 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002716
Neale Ranns107e7d42017-04-11 09:55:19 -07002717 if (~0 == fib_index)
2718 return VNET_API_ERROR_NO_SUCH_FIB;
2719
Neale Ranns227038a2017-04-21 01:07:59 -07002720 fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2721 flow_hash_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002722
Neale Ranns227038a2017-04-21 01:07:59 -07002723 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002724}
2725
2726static clib_error_t *
2727set_ip6_flow_hash_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002728 unformat_input_t * input,
2729 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002730{
2731 int matched = 0;
2732 u32 table_id = 0;
2733 u32 flow_hash_config = 0;
2734 int rv;
2735
Dave Barachd7cb1b52016-12-09 09:52:16 -05002736 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2737 {
2738 if (unformat (input, "table %d", &table_id))
2739 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002740#define _(a,v) \
2741 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
Dave Barachd7cb1b52016-12-09 09:52:16 -05002742 foreach_flow_hash_bit
Ed Warnickecb9cada2015-12-08 15:45:58 -07002743#undef _
Dave Barachd7cb1b52016-12-09 09:52:16 -05002744 else
2745 break;
2746 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002747
2748 if (matched == 0)
2749 return clib_error_return (0, "unknown input `%U'",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002750 format_unformat_error, input);
Dave Barach75fc8542016-10-11 16:16:02 -04002751
Ed Warnickecb9cada2015-12-08 15:45:58 -07002752 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2753 switch (rv)
2754 {
Neale Ranns227038a2017-04-21 01:07:59 -07002755 case 0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002756 break;
2757
2758 case -1:
2759 return clib_error_return (0, "no such FIB table %d", table_id);
Dave Barach75fc8542016-10-11 16:16:02 -04002760
Ed Warnickecb9cada2015-12-08 15:45:58 -07002761 default:
2762 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2763 break;
2764 }
Dave Barach75fc8542016-10-11 16:16:02 -04002765
Ed Warnickecb9cada2015-12-08 15:45:58 -07002766 return 0;
2767}
2768
Billy McFall0683c9c2016-10-13 08:27:31 -04002769/*?
2770 * Configure the set of IPv6 fields used by the flow hash.
2771 *
2772 * @cliexpar
2773 * @parblock
2774 * Example of how to set the flow hash on a given table:
Billy McFallebb9a6a2016-10-17 11:35:32 -04002775 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2776 *
Billy McFall0683c9c2016-10-13 08:27:31 -04002777 * Example of display the configured flow hash:
2778 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -04002779 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2780 * @::/0
2781 * unicast-ip6-chain
2782 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2783 * [0] [@0]: dpo-drop ip6
2784 * fe80::/10
2785 * unicast-ip6-chain
2786 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2787 * [0] [@2]: dpo-receive
2788 * ff02::1/128
2789 * unicast-ip6-chain
2790 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2791 * [0] [@2]: dpo-receive
2792 * ff02::2/128
2793 * unicast-ip6-chain
2794 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2795 * [0] [@2]: dpo-receive
2796 * ff02::16/128
2797 * unicast-ip6-chain
2798 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2799 * [0] [@2]: dpo-receive
2800 * ff02::1:ff00:0/104
2801 * unicast-ip6-chain
2802 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
2803 * [0] [@2]: dpo-receive
2804 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
2805 * @::/0
2806 * unicast-ip6-chain
2807 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
2808 * [0] [@0]: dpo-drop ip6
2809 * @::a:1:1:0:4/126
2810 * unicast-ip6-chain
2811 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
2812 * [0] [@4]: ipv6-glean: af_packet0
2813 * @::a:1:1:0:7/128
2814 * unicast-ip6-chain
2815 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
2816 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
2817 * fe80::/10
2818 * unicast-ip6-chain
2819 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
2820 * [0] [@2]: dpo-receive
2821 * fe80::fe:3eff:fe3e:9222/128
2822 * unicast-ip6-chain
2823 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
2824 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
2825 * ff02::1/128
2826 * unicast-ip6-chain
2827 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
2828 * [0] [@2]: dpo-receive
2829 * ff02::2/128
2830 * unicast-ip6-chain
2831 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
2832 * [0] [@2]: dpo-receive
2833 * ff02::16/128
2834 * unicast-ip6-chain
2835 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
2836 * [0] [@2]: dpo-receive
2837 * ff02::1:ff00:0/104
2838 * unicast-ip6-chain
2839 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
2840 * [0] [@2]: dpo-receive
Billy McFall0683c9c2016-10-13 08:27:31 -04002841 * @cliexend
2842 * @endparblock
2843?*/
2844/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002845VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
2846{
2847 .path = "set ip6 flow-hash",
2848 .short_help =
2849 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
2850 .function = set_ip6_flow_hash_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002851};
Billy McFall0683c9c2016-10-13 08:27:31 -04002852/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002853
2854static clib_error_t *
2855show_ip6_local_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002856 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002857{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002858 ip6_main_t *im = &ip6_main;
2859 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002860 int i;
Dave Barach75fc8542016-10-11 16:16:02 -04002861
Ed Warnickecb9cada2015-12-08 15:45:58 -07002862 vlib_cli_output (vm, "Protocols handled by ip6_local");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002863 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002864 {
2865 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
Pierre Pfister1bfd3722017-09-18 11:40:32 +02002866 {
2867
2868 u32 node_index = vlib_get_node (vm,
2869 ip6_local_node.index)->
2870 next_nodes[lm->local_next_by_ip_protocol[i]];
2871 vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
2872 node_index);
2873 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874 }
2875 return 0;
2876}
2877
2878
2879
Billy McFall0683c9c2016-10-13 08:27:31 -04002880/*?
2881 * Display the set of protocols handled by the local IPv6 stack.
2882 *
2883 * @cliexpar
2884 * Example of how to display local protocol table:
2885 * @cliexstart{show ip6 local}
2886 * Protocols handled by ip6_local
2887 * 17
2888 * 43
2889 * 58
2890 * 115
2891 * @cliexend
2892?*/
2893/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002894VLIB_CLI_COMMAND (show_ip6_local, static) =
2895{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002896 .path = "show ip6 local",
2897 .function = show_ip6_local_command_fn,
Billy McFall0683c9c2016-10-13 08:27:31 -04002898 .short_help = "show ip6 local",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002899};
Billy McFall0683c9c2016-10-13 08:27:31 -04002900/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002901
Dave Barachd7cb1b52016-12-09 09:52:16 -05002902int
2903vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2904 u32 table_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002905{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002906 vnet_main_t *vnm = vnet_get_main ();
2907 vnet_interface_main_t *im = &vnm->interface_main;
2908 ip6_main_t *ipm = &ip6_main;
2909 ip_lookup_main_t *lm = &ipm->lookup_main;
2910 vnet_classify_main_t *cm = &vnet_classify_main;
Neale Rannsdf089a82016-10-02 16:39:06 +01002911 ip6_address_t *if_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002912
2913 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2914 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2915
2916 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2917 return VNET_API_ERROR_NO_SUCH_ENTRY;
2918
2919 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002920 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002921
Neale Ranns6cfc39c2017-02-14 01:44:25 -08002922 if_addr = ip6_interface_first_address (ipm, sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01002923
2924 if (NULL != if_addr)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002925 {
Neale Rannsdf089a82016-10-02 16:39:06 +01002926 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002927 .fp_len = 128,
2928 .fp_proto = FIB_PROTOCOL_IP6,
2929 .fp_addr.ip6 = *if_addr,
Neale Rannsdf089a82016-10-02 16:39:06 +01002930 };
2931 u32 fib_index;
2932
Dave Barachd7cb1b52016-12-09 09:52:16 -05002933 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2934 sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01002935
2936
Dave Barachd7cb1b52016-12-09 09:52:16 -05002937 if (table_index != (u32) ~ 0)
2938 {
2939 dpo_id_t dpo = DPO_INVALID;
Neale Rannsdf089a82016-10-02 16:39:06 +01002940
Dave Barachd7cb1b52016-12-09 09:52:16 -05002941 dpo_set (&dpo,
2942 DPO_CLASSIFY,
2943 DPO_PROTO_IP6,
2944 classify_dpo_create (DPO_PROTO_IP6, table_index));
Neale Rannsdf089a82016-10-02 16:39:06 +01002945
Dave Barachd7cb1b52016-12-09 09:52:16 -05002946 fib_table_entry_special_dpo_add (fib_index,
2947 &pfx,
2948 FIB_SOURCE_CLASSIFY,
2949 FIB_ENTRY_FLAG_NONE, &dpo);
2950 dpo_reset (&dpo);
2951 }
Neale Rannsdf089a82016-10-02 16:39:06 +01002952 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05002953 {
2954 fib_table_entry_special_remove (fib_index,
2955 &pfx, FIB_SOURCE_CLASSIFY);
2956 }
2957 }
Neale Rannsdf089a82016-10-02 16:39:06 +01002958
Ed Warnickecb9cada2015-12-08 15:45:58 -07002959 return 0;
2960}
2961
2962static clib_error_t *
2963set_ip6_classify_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002964 unformat_input_t * input,
2965 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002966{
2967 u32 table_index = ~0;
2968 int table_index_set = 0;
2969 u32 sw_if_index = ~0;
2970 int rv;
Dave Barach75fc8542016-10-11 16:16:02 -04002971
Dave Barachd7cb1b52016-12-09 09:52:16 -05002972 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2973 {
2974 if (unformat (input, "table-index %d", &table_index))
2975 table_index_set = 1;
2976 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2977 vnet_get_main (), &sw_if_index))
2978 ;
2979 else
2980 break;
2981 }
Dave Barach75fc8542016-10-11 16:16:02 -04002982
Ed Warnickecb9cada2015-12-08 15:45:58 -07002983 if (table_index_set == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002984 return clib_error_return (0, "classify table-index must be specified");
Dave Barach75fc8542016-10-11 16:16:02 -04002985
Ed Warnickecb9cada2015-12-08 15:45:58 -07002986 if (sw_if_index == ~0)
2987 return clib_error_return (0, "interface / subif must be specified");
2988
2989 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2990
2991 switch (rv)
2992 {
2993 case 0:
2994 break;
2995
2996 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2997 return clib_error_return (0, "No such interface");
2998
2999 case VNET_API_ERROR_NO_SUCH_ENTRY:
3000 return clib_error_return (0, "No such classifier table");
3001 }
3002 return 0;
3003}
3004
Billy McFall0683c9c2016-10-13 08:27:31 -04003005/*?
3006 * Assign a classification table to an interface. The classification
3007 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3008 * commands. Once the table is create, use this command to filter packets
3009 * on an interface.
3010 *
3011 * @cliexpar
3012 * Example of how to assign a classification table to an interface:
3013 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3014?*/
3015/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003016VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3017{
3018 .path = "set ip6 classify",
3019 .short_help =
3020 "set ip6 classify intfc <interface> table-index <classify-idx>",
3021 .function = set_ip6_classify_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003022};
Billy McFall0683c9c2016-10-13 08:27:31 -04003023/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003024
3025static clib_error_t *
3026ip6_config (vlib_main_t * vm, unformat_input_t * input)
3027{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003028 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003029 uword heapsize = 0;
3030 u32 tmp;
3031 u32 nbuckets = 0;
3032
Dave Barachd7cb1b52016-12-09 09:52:16 -05003033 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3034 {
3035 if (unformat (input, "hash-buckets %d", &tmp))
3036 nbuckets = tmp;
Neale Ranns1ec36522017-11-29 05:20:37 -08003037 else if (unformat (input, "heap-size %U",
3038 unformat_memory_size, &heapsize))
3039 ;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003040 else
3041 return clib_error_return (0, "unknown input '%U'",
3042 format_unformat_error, input);
3043 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003044
3045 im->lookup_table_nbuckets = nbuckets;
3046 im->lookup_table_size = heapsize;
3047
3048 return 0;
3049}
3050
3051VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003052
3053/*
3054 * fd.io coding-style-patch-verification: ON
3055 *
3056 * Local Variables:
3057 * eval: (c-set-style "gnu")
3058 * End:
3059 */