blob: b5c795523caa800bd551f2f5517fad38b6920f86 [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>
Dave Barachd7cb1b52016-12-09 09:52:16 -050042#include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */
Ed Warnickecb9cada2015-12-08 15:45:58 -070043#include <vnet/srp/srp.h> /* for srp_hw_interface_class */
44#include <vppinfra/cache.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010045#include <vnet/fib/fib_table.h>
46#include <vnet/fib/ip6_fib.h>
47#include <vnet/dpo/load_balance.h>
48#include <vnet/dpo/classify_dpo.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
50#include <vppinfra/bihash_template.c>
51
Billy McFall0683c9c2016-10-13 08:27:31 -040052/**
53 * @file
54 * @brief IPv6 Forwarding.
55 *
56 * This file contains the source code for IPv6 forwarding.
57 */
58
Pierre Pfister0febaf12016-06-08 12:23:21 +010059void
60ip6_forward_next_trace (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -050061 vlib_node_runtime_t * node,
62 vlib_frame_t * frame,
63 vlib_rx_or_tx_t which_adj_index);
Pierre Pfister0febaf12016-06-08 12:23:21 +010064
Damjan Marionaca64c92016-04-13 09:48:56 +020065always_inline uword
66ip6_lookup_inline (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -050067 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barachd7cb1b52016-12-09 09:52:16 -050069 ip6_main_t *im = &ip6_main;
70 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_to_counters;
71 u32 n_left_from, n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 ip_lookup_next_t next;
Dave Barachd7cb1b52016-12-09 09:52:16 -050073 u32 cpu_index = os_get_cpu_number ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75 from = vlib_frame_vector_args (frame);
76 n_left_from = frame->n_vectors;
77 next = node->cached_next_index;
78
79 while (n_left_from > 0)
80 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050081 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 while (n_left_from >= 4 && n_left_to_next >= 2)
84 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 vlib_buffer_t *p0, *p1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010086 u32 pi0, pi1, lbi0, lbi1, wrong_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 ip_lookup_next_t next0, next1;
Dave Barachd7cb1b52016-12-09 09:52:16 -050088 ip6_header_t *ip0, *ip1;
89 ip6_address_t *dst_addr0, *dst_addr1;
90 u32 fib_index0, fib_index1;
91 u32 flow_hash_config0, flow_hash_config1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092 const dpo_id_t *dpo0, *dpo1;
93 const load_balance_t *lb0, *lb1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
95 /* Prefetch next iteration. */
96 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050097 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99 p2 = vlib_get_buffer (vm, from[2]);
100 p3 = vlib_get_buffer (vm, from[3]);
101
102 vlib_prefetch_buffer_header (p2, LOAD);
103 vlib_prefetch_buffer_header (p3, LOAD);
104 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
105 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
106 }
107
108 pi0 = to_next[0] = from[0];
109 pi1 = to_next[1] = from[1];
110
111 p0 = vlib_get_buffer (vm, pi0);
112 p1 = vlib_get_buffer (vm, pi1);
113
114 ip0 = vlib_buffer_get_current (p0);
115 ip1 = vlib_buffer_get_current (p1);
116
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100117 dst_addr0 = &ip0->dst_address;
118 dst_addr1 = &ip1->dst_address;
Damjan Marionaca64c92016-04-13 09:48:56 +0200119
Dave Barachd7cb1b52016-12-09 09:52:16 -0500120 fib_index0 =
121 vec_elt (im->fib_index_by_sw_if_index,
122 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
123 fib_index1 =
124 vec_elt (im->fib_index_by_sw_if_index,
125 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126
Dave Barachd7cb1b52016-12-09 09:52:16 -0500127 fib_index0 = (vnet_buffer (p0)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
128 fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
129 fib_index1 = (vnet_buffer (p1)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
130 fib_index1 : vnet_buffer (p1)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100132 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
133 lbi1 = ip6_fib_table_fwding_lookup (im, fib_index1, dst_addr1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100135 lb0 = load_balance_get (lbi0);
136 lb1 = load_balance_get (lbi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
Dave Barachd7cb1b52016-12-09 09:52:16 -0500138 vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
Dave Barachd7cb1b52016-12-09 09:52:16 -0500140 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
141 {
142 flow_hash_config0 = lb0->lb_hash_config;
143 vnet_buffer (p0)->ip.flow_hash =
144 ip6_compute_flow_hash (ip0, flow_hash_config0);
145 }
146 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
147 {
148 flow_hash_config1 = lb1->lb_hash_config;
149 vnet_buffer (p1)->ip.flow_hash =
150 ip6_compute_flow_hash (ip1, flow_hash_config1);
151 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153 ASSERT (lb0->lb_n_buckets > 0);
154 ASSERT (lb1->lb_n_buckets > 0);
155 ASSERT (is_pow2 (lb0->lb_n_buckets));
156 ASSERT (is_pow2 (lb1->lb_n_buckets));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500157 dpo0 = load_balance_get_bucket_i (lb0,
158 (vnet_buffer (p0)->ip.flow_hash &
159 lb0->lb_n_buckets_minus_1));
160 dpo1 = load_balance_get_bucket_i (lb1,
161 (vnet_buffer (p1)->ip.flow_hash &
162 lb1->lb_n_buckets_minus_1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164 next0 = dpo0->dpoi_next_node;
165 next1 = dpo1->dpoi_next_node;
166
167 /* Only process the HBH Option Header if explicitly configured to do so */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500168 if (PREDICT_FALSE
169 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Shwetha57fc8542016-09-27 08:04:05 +0100170 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500171 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
Shwetha57fc8542016-09-27 08:04:05 +0100172 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
173 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500174 if (PREDICT_FALSE
175 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Shwetha57fc8542016-09-27 08:04:05 +0100176 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500177 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
Shwetha57fc8542016-09-27 08:04:05 +0100178 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
179 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100180 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
181 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach75fc8542016-10-11 16:16:02 -0400183 vlib_increment_combined_counter
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
Dave Barach75fc8542016-10-11 16:16:02 -0400185 vlib_increment_combined_counter
Dave Barachd7cb1b52016-12-09 09:52:16 -0500186 (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 from += 2;
189 to_next += 2;
190 n_left_to_next -= 2;
191 n_left_from -= 2;
192
Dave Barachd7cb1b52016-12-09 09:52:16 -0500193 wrong_next = (next0 != next) + 2 * (next1 != next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 if (PREDICT_FALSE (wrong_next != 0))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500195 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 switch (wrong_next)
197 {
198 case 1:
199 /* A B A */
200 to_next[-2] = pi1;
201 to_next -= 1;
202 n_left_to_next += 1;
203 vlib_set_next_frame_buffer (vm, node, next0, pi0);
204 break;
205
206 case 2:
207 /* A A B */
208 to_next -= 1;
209 n_left_to_next += 1;
210 vlib_set_next_frame_buffer (vm, node, next1, pi1);
211 break;
212
213 case 3:
214 /* A B C */
215 to_next -= 2;
216 n_left_to_next += 2;
217 vlib_set_next_frame_buffer (vm, node, next0, pi0);
218 vlib_set_next_frame_buffer (vm, node, next1, pi1);
219 if (next0 == next1)
220 {
221 /* A B B */
222 vlib_put_next_frame (vm, node, next, n_left_to_next);
223 next = next1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500224 vlib_get_next_frame (vm, node, next, to_next,
225 n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 }
227 }
228 }
229 }
Dave Barach75fc8542016-10-11 16:16:02 -0400230
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 while (n_left_from > 0 && n_left_to_next > 0)
232 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 vlib_buffer_t *p0;
234 ip6_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100235 u32 pi0, lbi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 ip_lookup_next_t next0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500237 load_balance_t *lb0;
238 ip6_address_t *dst_addr0;
239 u32 fib_index0, flow_hash_config0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240 const dpo_id_t *dpo0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
242 pi0 = from[0];
243 to_next[0] = pi0;
244
245 p0 = vlib_get_buffer (vm, pi0);
246
247 ip0 = vlib_buffer_get_current (p0);
248
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249 dst_addr0 = &ip0->dst_address;
Damjan Marionaca64c92016-04-13 09:48:56 +0200250
Dave Barachd7cb1b52016-12-09 09:52:16 -0500251 fib_index0 =
252 vec_elt (im->fib_index_by_sw_if_index,
253 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
254 fib_index0 =
255 (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
256 (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Dave Barachd7cb1b52016-12-09 09:52:16 -0500258 flow_hash_config0 = ip6_fib_get (fib_index0)->flow_hash_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100260 lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100262 lb0 = load_balance_get (lbi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
Dave Barachd7cb1b52016-12-09 09:52:16 -0500264 vnet_buffer (p0)->ip.flow_hash = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Dave Barachd7cb1b52016-12-09 09:52:16 -0500266 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
267 {
268 flow_hash_config0 = lb0->lb_hash_config;
269 vnet_buffer (p0)->ip.flow_hash =
270 ip6_compute_flow_hash (ip0, flow_hash_config0);
271 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100273 ASSERT (lb0->lb_n_buckets > 0);
274 ASSERT (is_pow2 (lb0->lb_n_buckets));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 dpo0 = load_balance_get_bucket_i (lb0,
276 (vnet_buffer (p0)->ip.flow_hash &
277 lb0->lb_n_buckets_minus_1));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100278 next0 = dpo0->dpoi_next_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Shwetha57fc8542016-09-27 08:04:05 +0100280 /* Only process the HBH Option Header if explicitly configured to do so */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500281 if (PREDICT_FALSE
282 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Shwetha57fc8542016-09-27 08:04:05 +0100283 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500284 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
Shwetha57fc8542016-09-27 08:04:05 +0100285 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
286 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100287 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barach75fc8542016-10-11 16:16:02 -0400289 vlib_increment_combined_counter
Dave Barachd7cb1b52016-12-09 09:52:16 -0500290 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
292 from += 1;
293 to_next += 1;
294 n_left_to_next -= 1;
295 n_left_from -= 1;
296
297 if (PREDICT_FALSE (next0 != next))
298 {
299 n_left_to_next += 1;
300 vlib_put_next_frame (vm, node, next, n_left_to_next);
301 next = next0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500302 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 to_next[0] = pi0;
304 to_next += 1;
305 n_left_to_next -= 1;
306 }
307 }
308
309 vlib_put_next_frame (vm, node, next, n_left_to_next);
310 }
311
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100312 if (node->flags & VLIB_NODE_FLAG_TRACE)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500313 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100314
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 return frame->n_vectors;
316}
317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318static void
319ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
320 ip6_main_t * im, u32 fib_index,
321 ip_interface_address_t * a)
322{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 ip_lookup_main_t *lm = &im->lookup_main;
324 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100325 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500326 .fp_len = a->address_length,
327 .fp_proto = FIB_PROTOCOL_IP6,
328 .fp_addr.ip6 = *address,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100329 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
331 a->neighbor_probe_adj_index = ~0;
332 if (a->address_length < 128)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500333 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100334 fib_node_index_t fei;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335
Dave Barachd7cb1b52016-12-09 09:52:16 -0500336 fei = fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_ATTACHED), FIB_PROTOCOL_IP6, NULL, /* No next-hop address */
337 sw_if_index, ~0, // invalid FIB index
338 1, NULL, // no label stack
339 FIB_ROUTE_PATH_FLAG_NONE);
340 a->neighbor_probe_adj_index = fib_entry_get_adj (fei);
341 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100343 pfx.fp_len = 128;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344 if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100346 u32 classify_table_index =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500347 lm->classify_table_index_by_sw_if_index[sw_if_index];
348 if (classify_table_index != (u32) ~ 0)
349 {
350 dpo_id_t dpo = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100351
Dave Barachd7cb1b52016-12-09 09:52:16 -0500352 dpo_set (&dpo,
353 DPO_CLASSIFY,
354 DPO_PROTO_IP6,
355 classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100356
Dave Barachd7cb1b52016-12-09 09:52:16 -0500357 fib_table_entry_special_dpo_add (fib_index,
358 &pfx,
359 FIB_SOURCE_CLASSIFY,
360 FIB_ENTRY_FLAG_NONE, &dpo);
361 dpo_reset (&dpo);
362 }
363 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100364
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365 fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL), FIB_PROTOCOL_IP6, &pfx.fp_addr, sw_if_index, ~0, // invalid FIB index
366 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367}
368
369static void
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100370ip6_del_interface_routes (ip6_main_t * im,
371 u32 fib_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500372 ip6_address_t * address, u32 address_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500374 fib_prefix_t pfx = {
375 .fp_len = address_length,
376 .fp_proto = FIB_PROTOCOL_IP6,
377 .fp_addr.ip6 = *address,
378 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379
Dave Barachd7cb1b52016-12-09 09:52:16 -0500380 if (pfx.fp_len < 128)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500382 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100383
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 }
385
Dave Barachd7cb1b52016-12-09 09:52:16 -0500386 pfx.fp_len = 128;
387 fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388}
389
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100390void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500391ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100392{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500393 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100395 vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100397 /*
398 * enable/disable only on the 1<->0 transition
399 */
400 if (is_enable)
401 {
402 if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100404 }
405 else
406 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500407 ASSERT (im->ip_enabled_by_sw_if_index[sw_if_index] > 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100408 if (0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
Dave Barachd7cb1b52016-12-09 09:52:16 -0500409 return;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100410 }
411
Damjan Marion8b3191e2016-11-09 19:54:20 +0100412 vnet_feature_enable_disable ("ip6-unicast", "ip6-lookup", sw_if_index,
413 is_enable, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100414
Damjan Marion8b3191e2016-11-09 19:54:20 +0100415 vnet_feature_enable_disable ("ip6-multicast", "ip6-lookup", sw_if_index,
416 is_enable, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100417
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100418}
419
Neale Rannsdf089a82016-10-02 16:39:06 +0100420/* get first interface address */
421ip6_address_t *
422ip6_interface_first_address (ip6_main_t * im,
423 u32 sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500424 ip_interface_address_t ** result_ia)
Neale Rannsdf089a82016-10-02 16:39:06 +0100425{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500426 ip_lookup_main_t *lm = &im->lookup_main;
427 ip_interface_address_t *ia = 0;
428 ip6_address_t *result = 0;
Neale Rannsdf089a82016-10-02 16:39:06 +0100429
Dave Barachd7cb1b52016-12-09 09:52:16 -0500430 /* *INDENT-OFF* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100431 foreach_ip_interface_address (lm, ia, sw_if_index,
432 1 /* honor unnumbered */,
433 ({
434 ip6_address_t * a = ip_interface_address_get_address (lm, ia);
435 result = a;
436 break;
437 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500438 /* *INDENT-ON* */
Neale Rannsdf089a82016-10-02 16:39:06 +0100439 if (result_ia)
440 *result_ia = result ? ia : 0;
441 return result;
442}
443
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100444clib_error_t *
445ip6_add_del_interface_address (vlib_main_t * vm,
446 u32 sw_if_index,
447 ip6_address_t * address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500448 u32 address_length, u32 is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500450 vnet_main_t *vnm = vnet_get_main ();
451 ip6_main_t *im = &ip6_main;
452 ip_lookup_main_t *lm = &im->lookup_main;
453 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 u32 if_address_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500455 ip6_address_fib_t ip6_af, *addr_fib = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
457 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
458 ip6_addr_fib_init (&ip6_af, address,
459 vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
460 vec_add1 (addr_fib, ip6_af);
461
462 {
463 uword elts_before = pool_elts (lm->if_address_pool);
464
465 error = ip_interface_address_add_del
Dave Barachd7cb1b52016-12-09 09:52:16 -0500466 (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 if (error)
468 goto done;
469
470 /* Pool did not grow: add duplicate address. */
471 if (elts_before == pool_elts (lm->if_address_pool))
472 goto done;
473 }
474
Dave Barachd7cb1b52016-12-09 09:52:16 -0500475 ip6_sw_interface_enable_disable (sw_if_index, !is_del);
Neale Ranns177bbdc2016-11-15 09:46:51 +0000476
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100477 if (is_del)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500478 ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100479 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500480 ip6_add_interface_routes (vnm, sw_if_index,
481 im, ip6_af.fib_index,
482 pool_elt_at_index (lm->if_address_pool,
483 if_address_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
485 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500486 ip6_add_del_interface_address_callback_t *cb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 vec_foreach (cb, im->add_del_interface_address_callbacks)
488 cb->function (im, cb->function_opaque, sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500489 address, address_length, if_address_index, is_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 }
491
Dave Barachd7cb1b52016-12-09 09:52:16 -0500492done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 vec_free (addr_fib);
494 return error;
495}
496
497clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500498ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500500 ip6_main_t *im = &ip6_main;
501 ip_interface_address_t *ia;
502 ip6_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 u32 is_admin_up, fib_index;
504
505 /* Fill in lookup tables with default table (0). */
506 vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
507
Dave Barachd7cb1b52016-12-09 09:52:16 -0500508 vec_validate_init_empty (im->
509 lookup_main.if_address_pool_index_by_sw_if_index,
510 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
512 is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
513
514 fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
515
Dave Barachd7cb1b52016-12-09 09:52:16 -0500516 /* *INDENT-OFF* */
Dave Barach75fc8542016-10-11 16:16:02 -0400517 foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 0 /* honor unnumbered */,
519 ({
520 a = ip_interface_address_get_address (&im->lookup_main, ia);
521 if (is_admin_up)
522 ip6_add_interface_routes (vnm, sw_if_index,
523 im, fib_index,
524 ia);
525 else
526 ip6_del_interface_routes (im, fib_index,
527 a, ia->address_length);
528 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500529 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
531 return 0;
532}
533
534VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
535
Dave Barachd6534602016-06-14 18:38:02 -0400536/* Built-in ip6 unicast rx feature path definition */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500537/* *INDENT-OFF* */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100538VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
539{
540 .arc_name = "ip6-unicast",
541 .start_nodes = VNET_FEATURES ("ip6-input"),
542 .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
543};
544
Dave Barachd7cb1b52016-12-09 09:52:16 -0500545VNET_FEATURE_INIT (ip6_flow_classify, static) =
546{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100547 .arc_name = "ip6-unicast",
Juraj Sloboda506b2452016-08-07 23:45:24 -0700548 .node_name = "ip6-flow-classify",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100549 .runs_before = VNET_FEATURES ("ip6-inacl"),
Juraj Sloboda506b2452016-08-07 23:45:24 -0700550};
551
Dave Barachd7cb1b52016-12-09 09:52:16 -0500552VNET_FEATURE_INIT (ip6_inacl, static) =
553{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100554 .arc_name = "ip6-unicast",
Dave Barach75fc8542016-10-11 16:16:02 -0400555 .node_name = "ip6-inacl",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100556 .runs_before = VNET_FEATURES ("ip6-policer-classify"),
Dave Barachd6534602016-06-14 18:38:02 -0400557};
558
Dave Barachd7cb1b52016-12-09 09:52:16 -0500559VNET_FEATURE_INIT (ip6_policer_classify, static) =
560{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100561 .arc_name = "ip6-unicast",
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700562 .node_name = "ip6-policer-classify",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100563 .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700564};
565
Dave Barachd7cb1b52016-12-09 09:52:16 -0500566VNET_FEATURE_INIT (ip6_ipsec, static) =
567{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100568 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400569 .node_name = "ipsec-input-ip6",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100570 .runs_before = VNET_FEATURES ("l2tp-decap"),
Dave Barachd6534602016-06-14 18:38:02 -0400571};
572
Dave Barachd7cb1b52016-12-09 09:52:16 -0500573VNET_FEATURE_INIT (ip6_l2tp, static) =
574{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100575 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400576 .node_name = "l2tp-decap",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100577 .runs_before = VNET_FEATURES ("vpath-input-ip6"),
Dave Barachd6534602016-06-14 18:38:02 -0400578};
579
Dave Barachd7cb1b52016-12-09 09:52:16 -0500580VNET_FEATURE_INIT (ip6_vpath, static) =
581{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100582 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400583 .node_name = "vpath-input-ip6",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100584 .runs_before = VNET_FEATURES ("ip6-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400585};
586
Dave Barachd7cb1b52016-12-09 09:52:16 -0500587VNET_FEATURE_INIT (ip6_lookup, static) =
588{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100589 .arc_name = "ip6-unicast",
Dave Barachd6534602016-06-14 18:38:02 -0400590 .node_name = "ip6-lookup",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100591 .runs_before = VNET_FEATURES ("ip6-drop"),
Dave Barachd6534602016-06-14 18:38:02 -0400592};
593
Dave Barachd7cb1b52016-12-09 09:52:16 -0500594VNET_FEATURE_INIT (ip6_drop, static) =
595{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100596 .arc_name = "ip6-unicast",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100597 .node_name = "ip6-drop",
598 .runs_before = 0, /*last feature*/
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100599};
600
Dave Barachd6534602016-06-14 18:38:02 -0400601/* Built-in ip6 multicast rx feature path definition (none now) */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100602VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
603{
604 .arc_name = "ip6-multicast",
605 .start_nodes = VNET_FEATURES ("ip6-input"),
606 .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
607};
608
609VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
610 .arc_name = "ip6-multicast",
Dave Barachd6534602016-06-14 18:38:02 -0400611 .node_name = "vpath-input-ip6",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100612 .runs_before = VNET_FEATURES ("ip6-lookup"),
Dave Barachd6534602016-06-14 18:38:02 -0400613};
614
Damjan Marion8b3191e2016-11-09 19:54:20 +0100615VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
616 .arc_name = "ip6-multicast",
Dave Barachd6534602016-06-14 18:38:02 -0400617 .node_name = "ip6-lookup",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100618 .runs_before = VNET_FEATURES ("ip6-drop"),
Dave Barachd6534602016-06-14 18:38:02 -0400619};
620
Damjan Marion8b3191e2016-11-09 19:54:20 +0100621VNET_FEATURE_INIT (ip6_drop_mc, static) = {
622 .arc_name = "ip6-multicast",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100623 .node_name = "ip6-drop",
624 .runs_before = 0, /* last feature */
Neale Ranns5e575b12016-10-03 09:40:25 +0100625};
Dave Barach5331c722016-08-17 11:54:30 -0400626
627/* Built-in ip4 tx feature path definition */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100628VNET_FEATURE_ARC_INIT (ip6_output, static) =
629{
630 .arc_name = "ip6-output",
631 .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain"),
632 .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
Dave Barach5331c722016-08-17 11:54:30 -0400633};
634
Matus Fabian08a6f012016-11-15 06:08:51 -0800635VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
636 .arc_name = "ip6-output",
637 .node_name = "ipsec-output-ip6",
638 .runs_before = VNET_FEATURES ("interface-output"),
639};
640
Damjan Marion8b3191e2016-11-09 19:54:20 +0100641VNET_FEATURE_INIT (ip6_interface_output, static) = {
642 .arc_name = "ip6-output",
643 .node_name = "interface-output",
644 .runs_before = 0, /* not before any other features */
645};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500646/* *INDENT-ON* */
Dave Barachd6534602016-06-14 18:38:02 -0400647
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500649ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650{
Damjan Marion8b3191e2016-11-09 19:54:20 +0100651 vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
652 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
Damjan Marion8b3191e2016-11-09 19:54:20 +0100654 vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
655 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Damjan Marion8b3191e2016-11-09 19:54:20 +0100657 vnet_feature_enable_disable ("ip6-output", "interface-output", sw_if_index,
658 is_add, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660 return /* no error */ 0;
661}
662
663VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
664
Damjan Marionaca64c92016-04-13 09:48:56 +0200665static uword
666ip6_lookup (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500667 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200668{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100669 return ip6_lookup_inline (vm, node, frame);
Damjan Marionaca64c92016-04-13 09:48:56 +0200670}
671
Dave Barachd7cb1b52016-12-09 09:52:16 -0500672static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100673
Dave Barachd7cb1b52016-12-09 09:52:16 -0500674/* *INDENT-OFF* */
675VLIB_REGISTER_NODE (ip6_lookup_node) =
676{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677 .function = ip6_lookup,
678 .name = "ip6-lookup",
679 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100680 .format_trace = format_ip6_lookup_trace,
Ole Troanf0f85222016-06-14 21:12:32 +0200681 .n_next_nodes = IP6_LOOKUP_N_NEXT,
Damjan Marionb2707892016-04-13 11:21:07 +0200682 .next_nodes = IP6_LOOKUP_NEXT_NODES,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500684/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685
Dave Barachd7cb1b52016-12-09 09:52:16 -0500686VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
Damjan Marion1c80e832016-05-11 23:07:18 +0200687
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100688always_inline uword
689ip6_load_balance (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500690 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marionaca64c92016-04-13 09:48:56 +0200691{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500692 vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
693 u32 n_left_from, n_left_to_next, *from, *to_next;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100694 ip_lookup_next_t next;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500695 u32 cpu_index = os_get_cpu_number ();
696 ip6_main_t *im = &ip6_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100697
698 from = vlib_frame_vector_args (frame);
699 n_left_from = frame->n_vectors;
700 next = node->cached_next_index;
701
702 if (node->flags & VLIB_NODE_FLAG_TRACE)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500703 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100704
705 while (n_left_from > 0)
706 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500707 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100708
Dave Barach75fc8542016-10-11 16:16:02 -0400709
Neale Ranns2be95c12016-11-19 13:50:04 +0000710 while (n_left_from >= 4 && n_left_to_next >= 2)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500711 {
712 ip_lookup_next_t next0, next1;
713 const load_balance_t *lb0, *lb1;
714 vlib_buffer_t *p0, *p1;
715 u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
716 const ip6_header_t *ip0, *ip1;
717 const dpo_id_t *dpo0, *dpo1;
Neale Ranns2be95c12016-11-19 13:50:04 +0000718
Dave Barachd7cb1b52016-12-09 09:52:16 -0500719 /* Prefetch next iteration. */
720 {
721 vlib_buffer_t *p2, *p3;
Neale Ranns2be95c12016-11-19 13:50:04 +0000722
Dave Barachd7cb1b52016-12-09 09:52:16 -0500723 p2 = vlib_get_buffer (vm, from[2]);
724 p3 = vlib_get_buffer (vm, from[3]);
Neale Ranns2be95c12016-11-19 13:50:04 +0000725
Dave Barachd7cb1b52016-12-09 09:52:16 -0500726 vlib_prefetch_buffer_header (p2, STORE);
727 vlib_prefetch_buffer_header (p3, STORE);
Neale Ranns2be95c12016-11-19 13:50:04 +0000728
Dave Barachd7cb1b52016-12-09 09:52:16 -0500729 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
730 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
731 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000732
Dave Barachd7cb1b52016-12-09 09:52:16 -0500733 pi0 = to_next[0] = from[0];
734 pi1 = to_next[1] = from[1];
Neale Ranns2be95c12016-11-19 13:50:04 +0000735
Dave Barachd7cb1b52016-12-09 09:52:16 -0500736 from += 2;
737 n_left_from -= 2;
738 to_next += 2;
739 n_left_to_next -= 2;
Neale Ranns2be95c12016-11-19 13:50:04 +0000740
Dave Barachd7cb1b52016-12-09 09:52:16 -0500741 p0 = vlib_get_buffer (vm, pi0);
742 p1 = vlib_get_buffer (vm, pi1);
Neale Ranns2be95c12016-11-19 13:50:04 +0000743
Dave Barachd7cb1b52016-12-09 09:52:16 -0500744 ip0 = vlib_buffer_get_current (p0);
745 ip1 = vlib_buffer_get_current (p1);
746 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
747 lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Neale Ranns2be95c12016-11-19 13:50:04 +0000748
Dave Barachd7cb1b52016-12-09 09:52:16 -0500749 lb0 = load_balance_get (lbi0);
750 lb1 = load_balance_get (lbi1);
Neale Ranns2be95c12016-11-19 13:50:04 +0000751
Dave Barachd7cb1b52016-12-09 09:52:16 -0500752 /*
753 * this node is for via FIBs we can re-use the hash value from the
754 * to node if present.
755 * We don't want to use the same hash value at each level in the recursion
756 * graph as that would lead to polarisation
757 */
758 hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
759 hc1 = vnet_buffer (p1)->ip.flow_hash = 0;
Neale Ranns2be95c12016-11-19 13:50:04 +0000760
Dave Barachd7cb1b52016-12-09 09:52:16 -0500761 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
762 {
763 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
764 {
765 hc0 = vnet_buffer (p0)->ip.flow_hash =
766 vnet_buffer (p0)->ip.flow_hash >> 1;
767 }
768 else
769 {
770 hc0 = vnet_buffer (p0)->ip.flow_hash =
771 ip6_compute_flow_hash (ip0, hc0);
772 }
773 }
774 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
775 {
776 if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
777 {
778 hc1 = vnet_buffer (p1)->ip.flow_hash =
779 vnet_buffer (p1)->ip.flow_hash >> 1;
780 }
781 else
782 {
783 hc1 = vnet_buffer (p1)->ip.flow_hash =
784 ip6_compute_flow_hash (ip1, hc1);
785 }
786 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000787
Dave Barachd7cb1b52016-12-09 09:52:16 -0500788 dpo0 =
789 load_balance_get_bucket_i (lb0,
790 hc0 & (lb0->lb_n_buckets_minus_1));
791 dpo1 =
792 load_balance_get_bucket_i (lb1,
793 hc1 & (lb1->lb_n_buckets_minus_1));
Neale Ranns2be95c12016-11-19 13:50:04 +0000794
Dave Barachd7cb1b52016-12-09 09:52:16 -0500795 next0 = dpo0->dpoi_next_node;
796 next1 = dpo1->dpoi_next_node;
Neale Ranns2be95c12016-11-19 13:50:04 +0000797
Dave Barachd7cb1b52016-12-09 09:52:16 -0500798 /* Only process the HBH Option Header if explicitly configured to do so */
799 if (PREDICT_FALSE
800 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
801 {
802 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
803 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
804 }
805 /* Only process the HBH Option Header if explicitly configured to do so */
806 if (PREDICT_FALSE
807 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
808 {
809 next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
810 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
811 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000812
Dave Barachd7cb1b52016-12-09 09:52:16 -0500813 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
814 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
Neale Ranns2be95c12016-11-19 13:50:04 +0000815
Dave Barachd7cb1b52016-12-09 09:52:16 -0500816 vlib_increment_combined_counter
817 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
818 vlib_increment_combined_counter
819 (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
Neale Ranns2be95c12016-11-19 13:50:04 +0000820
Dave Barachd7cb1b52016-12-09 09:52:16 -0500821 vlib_validate_buffer_enqueue_x2 (vm, node, next,
822 to_next, n_left_to_next,
823 pi0, pi1, next0, next1);
824 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000825
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100826 while (n_left_from > 0 && n_left_to_next > 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500827 {
828 ip_lookup_next_t next0;
829 const load_balance_t *lb0;
830 vlib_buffer_t *p0;
831 u32 pi0, lbi0, hc0;
832 const ip6_header_t *ip0;
833 const dpo_id_t *dpo0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100834
Dave Barachd7cb1b52016-12-09 09:52:16 -0500835 pi0 = from[0];
836 to_next[0] = pi0;
837 from += 1;
838 to_next += 1;
839 n_left_to_next -= 1;
840 n_left_from -= 1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100841
Dave Barachd7cb1b52016-12-09 09:52:16 -0500842 p0 = vlib_get_buffer (vm, pi0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100843
Dave Barachd7cb1b52016-12-09 09:52:16 -0500844 ip0 = vlib_buffer_get_current (p0);
845 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100846
Dave Barachd7cb1b52016-12-09 09:52:16 -0500847 lb0 = load_balance_get (lbi0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100848
Dave Barachd7cb1b52016-12-09 09:52:16 -0500849 hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
850 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
851 {
852 if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
853 {
854 hc0 = vnet_buffer (p0)->ip.flow_hash =
855 vnet_buffer (p0)->ip.flow_hash >> 1;
856 }
857 else
858 {
859 hc0 = vnet_buffer (p0)->ip.flow_hash =
860 ip6_compute_flow_hash (ip0, hc0);
861 }
862 }
863 dpo0 =
864 load_balance_get_bucket_i (lb0,
865 hc0 & (lb0->lb_n_buckets_minus_1));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100866
Dave Barachd7cb1b52016-12-09 09:52:16 -0500867 next0 = dpo0->dpoi_next_node;
868 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
Neale Ranns2be95c12016-11-19 13:50:04 +0000869
Dave Barachd7cb1b52016-12-09 09:52:16 -0500870 /* Only process the HBH Option Header if explicitly configured to do so */
871 if (PREDICT_FALSE
872 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
873 {
874 next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
875 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
876 }
Neale Ranns2be95c12016-11-19 13:50:04 +0000877
Dave Barachd7cb1b52016-12-09 09:52:16 -0500878 vlib_increment_combined_counter
879 (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100880
Dave Barachd7cb1b52016-12-09 09:52:16 -0500881 vlib_validate_buffer_enqueue_x1 (vm, node, next,
882 to_next, n_left_to_next,
883 pi0, next0);
884 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100885
886 vlib_put_next_frame (vm, node, next, n_left_to_next);
887 }
888
889 return frame->n_vectors;
Damjan Marionaca64c92016-04-13 09:48:56 +0200890}
891
Dave Barachd7cb1b52016-12-09 09:52:16 -0500892/* *INDENT-OFF* */
893VLIB_REGISTER_NODE (ip6_load_balance_node) =
894{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100895 .function = ip6_load_balance,
896 .name = "ip6-load-balance",
Damjan Marionaca64c92016-04-13 09:48:56 +0200897 .vector_size = sizeof (u32),
Ole Troanf0f85222016-06-14 21:12:32 +0200898 .sibling_of = "ip6-lookup",
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100899 .format_trace = format_ip6_lookup_trace,
Damjan Marionaca64c92016-04-13 09:48:56 +0200900};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500901/* *INDENT-ON* */
Damjan Marionaca64c92016-04-13 09:48:56 +0200902
Dave Barachd7cb1b52016-12-09 09:52:16 -0500903VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
Damjan Marion1c80e832016-05-11 23:07:18 +0200904
Dave Barachd7cb1b52016-12-09 09:52:16 -0500905typedef struct
906{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 /* Adjacency taken. */
908 u32 adj_index;
909 u32 flow_hash;
John Lo2d343742016-01-19 17:27:17 -0500910 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911
912 /* Packet data, possibly *after* rewrite. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500913 u8 packet_data[128 - 1 * sizeof (u32)];
914}
915ip6_forward_next_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916
Dave Barachd7cb1b52016-12-09 09:52:16 -0500917static u8 *
918format_ip6_forward_next_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919{
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100920 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
921 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500922 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100923 uword indent = format_get_indent (s);
924
Dave Barachd7cb1b52016-12-09 09:52:16 -0500925 s = format (s, "%U%U",
926 format_white_space, indent,
927 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100928 return s;
929}
930
Dave Barachd7cb1b52016-12-09 09:52:16 -0500931static u8 *
932format_ip6_lookup_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100933{
934 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
935 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500936 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937 uword indent = format_get_indent (s);
938
John Loac8146c2016-09-27 17:44:02 -0400939 s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500940 t->fib_index, t->adj_index, t->flow_hash);
941 s = format (s, "\n%U%U",
942 format_white_space, indent,
943 format_ip6_header, t->packet_data, sizeof (t->packet_data));
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100944 return s;
945}
Pierre Pfister0febaf12016-06-08 12:23:21 +0100946
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947
Dave Barachd7cb1b52016-12-09 09:52:16 -0500948static u8 *
949format_ip6_rewrite_trace (u8 * s, va_list * args)
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100950{
951 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
952 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500953 ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
954 vnet_main_t *vnm = vnet_get_main ();
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100955 uword indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100957 s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500958 t->fib_index, t->adj_index, format_ip_adjacency,
959 t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
Pierre Pfistera38c3df2016-06-13 10:28:09 +0100960 s = format (s, "\n%U%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500961 format_white_space, indent,
962 format_ip_adjacency_packet_data,
963 vnm, t->adj_index, t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964 return s;
965}
966
967/* Common trace function for all ip6-forward next nodes. */
968void
969ip6_forward_next_trace (vlib_main_t * vm,
970 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500971 vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500973 u32 *from, n_left;
974 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
976 n_left = frame->n_vectors;
977 from = vlib_frame_vector_args (frame);
Pierre Pfister0febaf12016-06-08 12:23:21 +0100978
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979 while (n_left >= 4)
980 {
981 u32 bi0, bi1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500982 vlib_buffer_t *b0, *b1;
983 ip6_forward_next_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984
985 /* Prefetch next iteration. */
986 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
987 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
988
989 bi0 = from[0];
990 bi1 = from[1];
991
992 b0 = vlib_get_buffer (vm, bi0);
993 b1 = vlib_get_buffer (vm, bi1);
994
995 if (b0->flags & VLIB_BUFFER_IS_TRACED)
996 {
997 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
998 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500999 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1000 t0->fib_index =
1001 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1002 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1003 vec_elt (im->fib_index_by_sw_if_index,
1004 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001005
Damjan Marionf1213b82016-03-13 02:22:06 +01001006 clib_memcpy (t0->packet_data,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001007 vlib_buffer_get_current (b0),
1008 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009 }
1010 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1011 {
1012 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1013 t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001014 t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1015 t1->fib_index =
1016 (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1017 (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1018 vec_elt (im->fib_index_by_sw_if_index,
1019 vnet_buffer (b1)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001020
Damjan Marionf1213b82016-03-13 02:22:06 +01001021 clib_memcpy (t1->packet_data,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001022 vlib_buffer_get_current (b1),
1023 sizeof (t1->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024 }
1025 from += 2;
1026 n_left -= 2;
1027 }
1028
1029 while (n_left >= 1)
1030 {
1031 u32 bi0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001032 vlib_buffer_t *b0;
1033 ip6_forward_next_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
1035 bi0 = from[0];
1036
1037 b0 = vlib_get_buffer (vm, bi0);
1038
1039 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1040 {
1041 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1042 t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001043 t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1044 t0->fib_index =
1045 (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1046 (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1047 vec_elt (im->fib_index_by_sw_if_index,
1048 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
Pierre Pfister0febaf12016-06-08 12:23:21 +01001049
Damjan Marionf1213b82016-03-13 02:22:06 +01001050 clib_memcpy (t0->packet_data,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001051 vlib_buffer_get_current (b0),
1052 sizeof (t0->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053 }
1054 from += 1;
1055 n_left -= 1;
1056 }
1057}
1058
1059static uword
1060ip6_drop_or_punt (vlib_main_t * vm,
1061 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001062 vlib_frame_t * frame, ip6_error_t error_code)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001064 u32 *buffers = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065 uword n_packets = frame->n_vectors;
1066
Dave Barachd7cb1b52016-12-09 09:52:16 -05001067 vlib_error_drop_buffers (vm, node, buffers,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068 /* stride */ 1,
1069 n_packets,
1070 /* next */ 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001071 ip6_input_node.index, error_code);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
1073 if (node->flags & VLIB_NODE_FLAG_TRACE)
1074 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1075
1076 return n_packets;
1077}
1078
1079static uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001080ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1081{
1082 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP);
1083}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
1085static uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001086ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1087{
1088 return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT);
1089}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090
Dave Barachd7cb1b52016-12-09 09:52:16 -05001091/* *INDENT-OFF* */
1092VLIB_REGISTER_NODE (ip6_drop_node, static) =
1093{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 .function = ip6_drop,
1095 .name = "ip6-drop",
1096 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 .n_next_nodes = 1,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001099 .next_nodes =
1100 {
1101 [0] = "error-drop",},
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001103/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
Dave Barachd7cb1b52016-12-09 09:52:16 -05001105VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
Damjan Marion1c80e832016-05-11 23:07:18 +02001106
Dave Barachd7cb1b52016-12-09 09:52:16 -05001107/* *INDENT-OFF* */
1108VLIB_REGISTER_NODE (ip6_punt_node, static) =
1109{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110 .function = ip6_punt,
1111 .name = "ip6-punt",
1112 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 .n_next_nodes = 1,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001115 .next_nodes =
1116 {
1117 [0] = "error-punt",},
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001119/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120
Dave Barachd7cb1b52016-12-09 09:52:16 -05001121VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
Damjan Marion1c80e832016-05-11 23:07:18 +02001122
Dave Barachd7cb1b52016-12-09 09:52:16 -05001123/* *INDENT-OFF* */
1124VLIB_REGISTER_NODE (ip6_multicast_node, static) =
1125{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 .function = ip6_drop,
1127 .name = "ip6-multicast",
1128 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130 .n_next_nodes = 1,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001131 .next_nodes =
1132 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133 [0] = "error-drop",
1134 },
1135};
1136
Dave Barachd7cb1b52016-12-09 09:52:16 -05001137/* *INDENT-ON* */
1138
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139/* Compute TCP/UDP/ICMP6 checksum in software. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001140u16
1141ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1142 ip6_header_t * ip0, int *bogus_lengthp)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143{
1144 ip_csum_t sum0;
1145 u16 sum16, payload_length_host_byte_order;
1146 u32 i, n_this_buffer, n_bytes_left;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001147 u32 headers_size = sizeof (ip0[0]);
1148 void *data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149
Dave Barachd7cb1b52016-12-09 09:52:16 -05001150 ASSERT (bogus_lengthp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151 *bogus_lengthp = 0;
1152
1153 /* Initialize checksum with ip header. */
1154 sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1155 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1156 data_this_buffer = (void *) (ip0 + 1);
Dave Barach75fc8542016-10-11 16:16:02 -04001157
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1159 {
1160 sum0 = ip_csum_with_carry (sum0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001161 clib_mem_unaligned (&ip0->
1162 src_address.as_uword[i],
1163 uword));
1164 sum0 =
1165 ip_csum_with_carry (sum0,
1166 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1167 uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168 }
1169
1170 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001171 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001172 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001173 u32 skip_bytes;
1174 ip6_hop_by_hop_ext_t *ext_hdr =
1175 (ip6_hop_by_hop_ext_t *) data_this_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176
1177 /* validate really icmp6 next */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001178 ASSERT (ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179
Dave Barachd7cb1b52016-12-09 09:52:16 -05001180 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1181 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
Dave Barach75fc8542016-10-11 16:16:02 -04001182
Dave Barachd7cb1b52016-12-09 09:52:16 -05001183 payload_length_host_byte_order -= skip_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 headers_size += skip_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001185 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
1187 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001188 if (p0 && n_this_buffer + headers_size > p0->current_length)
1189 n_this_buffer =
1190 p0->current_length >
1191 headers_size ? p0->current_length - headers_size : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 while (1)
1193 {
1194 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1195 n_bytes_left -= n_this_buffer;
1196 if (n_bytes_left == 0)
1197 break;
1198
1199 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001200 {
1201 *bogus_lengthp = 1;
1202 return 0xfefe;
1203 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204 p0 = vlib_get_buffer (vm, p0->next_buffer);
1205 data_this_buffer = vlib_buffer_get_current (p0);
1206 n_this_buffer = p0->current_length;
1207 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
Dave Barachd7cb1b52016-12-09 09:52:16 -05001209 sum16 = ~ip_csum_fold (sum0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210
1211 return sum16;
1212}
1213
Dave Barachd7cb1b52016-12-09 09:52:16 -05001214u32
1215ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001217 ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1218 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219 u16 sum16;
1220 int bogus_length;
1221
1222 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1223 ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1224 || ip0->protocol == IP_PROTOCOL_ICMP6
1225 || ip0->protocol == IP_PROTOCOL_UDP
Dave Barachd7cb1b52016-12-09 09:52:16 -05001226 || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227
1228 udp0 = (void *) (ip0 + 1);
1229 if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1230 {
1231 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1232 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1233 return p0->flags;
1234 }
1235
1236 sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1237
1238 p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1239 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1240
1241 return p0->flags;
1242}
1243
Shwethab78292e2016-09-13 11:51:00 +01001244/* ip6_locate_header
1245 *
1246 * This function is to search for the header specified by the find_hdr number.
1247 * 1. If the find_hdr < 0 then it finds and returns the protocol number and
1248 * offset stored in *offset of the transport or ESP header in the chain if
1249 * found.
1250 * 2. If a header with find_hdr > 0 protocol number is found then the
1251 * offset is stored in *offset and protocol number of the header is
1252 * returned.
1253 * 3. If find_hdr header is not found or packet is malformed or
1254 * it is a non-first fragment -1 is returned.
1255 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256always_inline int
1257ip6_locate_header (vlib_buffer_t * p0,
1258 ip6_header_t * ip0, int find_hdr, u32 * offset)
Shwethab78292e2016-09-13 11:51:00 +01001259{
1260 u8 next_proto = ip0->protocol;
1261 u8 *next_header;
1262 u8 done = 0;
1263 u32 cur_offset;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001264 u8 *temp_nxthdr = 0;
Shwethab78292e2016-09-13 11:51:00 +01001265 u32 exthdr_len = 0;
1266
Dave Barachd7cb1b52016-12-09 09:52:16 -05001267 next_header = ip6_next_header (ip0);
1268 cur_offset = sizeof (ip6_header_t);
1269 while (1)
Shwethab78292e2016-09-13 11:51:00 +01001270 {
1271 done = (next_proto == find_hdr);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001272 if (PREDICT_FALSE
1273 (next_header >=
1274 (u8 *) vlib_buffer_get_current (p0) + p0->current_length))
Shwethab78292e2016-09-13 11:51:00 +01001275 {
1276 //A malicious packet could set an extension header with a too big size
Dave Barachd7cb1b52016-12-09 09:52:16 -05001277 return (-1);
Shwethab78292e2016-09-13 11:51:00 +01001278 }
1279 if (done)
1280 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001281 if ((!ip6_ext_hdr (next_proto)) || next_proto == IP_PROTOCOL_IP6_NONXT)
Shwethab78292e2016-09-13 11:51:00 +01001282 {
1283 if (find_hdr < 0)
1284 break;
1285 return -1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001286 }
Shwethab78292e2016-09-13 11:51:00 +01001287 if (next_proto == IP_PROTOCOL_IPV6_FRAGMENTATION)
1288 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001289 ip6_frag_hdr_t *frag_hdr = (ip6_frag_hdr_t *) next_header;
1290 u16 frag_off = ip6_frag_hdr_offset (frag_hdr);
1291 /* Non first fragment return -1 */
Shwethab78292e2016-09-13 11:51:00 +01001292 if (frag_off)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001293 return (-1);
1294 exthdr_len = sizeof (ip6_frag_hdr_t);
1295 temp_nxthdr = next_header + exthdr_len;
Shwethab78292e2016-09-13 11:51:00 +01001296 }
1297 else if (next_proto == IP_PROTOCOL_IPSEC_AH)
1298 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001299 exthdr_len =
1300 ip6_ext_authhdr_len (((ip6_ext_header_t *) next_header));
Shwethab78292e2016-09-13 11:51:00 +01001301 temp_nxthdr = next_header + exthdr_len;
1302 }
1303 else
1304 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001305 exthdr_len =
1306 ip6_ext_header_len (((ip6_ext_header_t *) next_header));
Shwethab78292e2016-09-13 11:51:00 +01001307 temp_nxthdr = next_header + exthdr_len;
1308 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001309 next_proto = ((ip6_ext_header_t *) next_header)->next_hdr;
1310 next_header = temp_nxthdr;
1311 cur_offset += exthdr_len;
Shwethab78292e2016-09-13 11:51:00 +01001312 }
1313
1314 *offset = cur_offset;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001315 return (next_proto);
Shwethab78292e2016-09-13 11:51:00 +01001316}
1317
Ed Warnickecb9cada2015-12-08 15:45:58 -07001318static uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001319ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001320{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001321 ip6_main_t *im = &ip6_main;
1322 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323 ip_local_next_t next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001324 u32 *from, *to_next, n_left_from, n_left_to_next;
1325 vlib_node_runtime_t *error_node =
1326 vlib_node_get_runtime (vm, ip6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327
1328 from = vlib_frame_vector_args (frame);
1329 n_left_from = frame->n_vectors;
1330 next_index = node->cached_next_index;
Dave Barach75fc8542016-10-11 16:16:02 -04001331
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332 if (node->flags & VLIB_NODE_FLAG_TRACE)
1333 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1334
1335 while (n_left_from > 0)
1336 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001337 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338
1339 while (n_left_from >= 4 && n_left_to_next >= 2)
1340 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001341 vlib_buffer_t *p0, *p1;
1342 ip6_header_t *ip0, *ip1;
1343 udp_header_t *udp0, *udp1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001344 u32 pi0, ip_len0, udp_len0, flags0, next0;
1345 u32 pi1, ip_len1, udp_len1, flags1, next1;
1346 i32 len_diff0, len_diff1;
1347 u8 error0, type0, good_l4_checksum0;
1348 u8 error1, type1, good_l4_checksum1;
Shwethab78292e2016-09-13 11:51:00 +01001349 u32 udp_offset0, udp_offset1;
Dave Barach75fc8542016-10-11 16:16:02 -04001350
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351 pi0 = to_next[0] = from[0];
1352 pi1 = to_next[1] = from[1];
1353 from += 2;
1354 n_left_from -= 2;
1355 to_next += 2;
1356 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04001357
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358 p0 = vlib_get_buffer (vm, pi0);
1359 p1 = vlib_get_buffer (vm, pi1);
1360
1361 ip0 = vlib_buffer_get_current (p0);
1362 ip1 = vlib_buffer_get_current (p1);
1363
1364 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1365 type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1366
1367 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1368 next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1369
1370 flags0 = p0->flags;
1371 flags1 = p1->flags;
1372
1373 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1374 good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
Shwethab78292e2016-09-13 11:51:00 +01001375 len_diff0 = 0;
1376 len_diff1 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001377
Shwethab78292e2016-09-13 11:51:00 +01001378 /* Skip HBH local processing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001379 if (PREDICT_FALSE
1380 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Shwethab78292e2016-09-13 11:51:00 +01001381 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001382 ip6_hop_by_hop_ext_t *ext_hdr =
1383 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip0);
Shwethab78292e2016-09-13 11:51:00 +01001384 next0 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1385 type0 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1386 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001387 if (PREDICT_FALSE
1388 (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Shwethab78292e2016-09-13 11:51:00 +01001389 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001390 ip6_hop_by_hop_ext_t *ext_hdr =
1391 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip1);
Shwethab78292e2016-09-13 11:51:00 +01001392 next1 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1393 type1 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1394 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001395 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1396 IP_PROTOCOL_UDP,
1397 &udp_offset0)))
Shwethab78292e2016-09-13 11:51:00 +01001398 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001399 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
Shwethab78292e2016-09-13 11:51:00 +01001400 /* Don't verify UDP checksum for packets with explicit zero checksum. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001401 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1402 && udp0->checksum == 0;
Shwethab78292e2016-09-13 11:51:00 +01001403 /* Verify UDP length. */
1404 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1405 udp_len0 = clib_net_to_host_u16 (udp0->length);
1406 len_diff0 = ip_len0 - udp_len0;
1407 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001408 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p1, ip1,
1409 IP_PROTOCOL_UDP,
1410 &udp_offset1)))
Shwethab78292e2016-09-13 11:51:00 +01001411 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001412 udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
Shwethab78292e2016-09-13 11:51:00 +01001413 /* Don't verify UDP checksum for packets with explicit zero checksum. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001414 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1415 && udp1->checksum == 0;
Shwethab78292e2016-09-13 11:51:00 +01001416 /* Verify UDP length. */
1417 ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1418 udp_len1 = clib_net_to_host_u16 (udp1->length);
1419 len_diff1 = ip_len1 - udp_len1;
1420 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001421
1422 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1423 good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1424
Ed Warnickecb9cada2015-12-08 15:45:58 -07001425 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1426 len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1427
1428 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
Dave Barachd7cb1b52016-12-09 09:52:16 -05001429 && !good_l4_checksum0
1430 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001431 {
1432 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1433 good_l4_checksum0 =
1434 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1435 }
1436 if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
Dave Barachd7cb1b52016-12-09 09:52:16 -05001437 && !good_l4_checksum1
1438 && !(flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439 {
1440 flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1441 good_l4_checksum1 =
1442 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1443 }
1444
1445 error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1446
1447 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1448 error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1449
Dave Barachd7cb1b52016-12-09 09:52:16 -05001450 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1451 IP6_ERROR_UDP_CHECKSUM);
1452 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1453 IP6_ERROR_ICMP_CHECKSUM);
1454 error0 =
1455 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1456 error1 =
1457 (!good_l4_checksum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458
1459 /* Drop packets from unroutable hosts. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001460 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001461 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1462 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
Dave Barachd7cb1b52016-12-09 09:52:16 -05001463 !ip6_address_is_link_local_unicast (&ip0->src_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464 {
1465 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001466 error0 = (ADJ_INDEX_INVALID == src_adj_index0
Dave Barachd7cb1b52016-12-09 09:52:16 -05001467 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001469 if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1470 type1 != IP_BUILTIN_PROTOCOL_ICMP &&
Dave Barachd7cb1b52016-12-09 09:52:16 -05001471 !ip6_address_is_link_local_unicast (&ip1->src_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001472 {
1473 u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001474 error1 = (ADJ_INDEX_INVALID == src_adj_index1
Dave Barachd7cb1b52016-12-09 09:52:16 -05001475 ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476 }
1477
Dave Barachd7cb1b52016-12-09 09:52:16 -05001478 next0 =
1479 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1480 next1 =
1481 error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001482
1483 p0->error = error_node->errors[error0];
1484 p1->error = error_node->errors[error1];
1485
1486 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1487 to_next, n_left_to_next,
1488 pi0, pi1, next0, next1);
1489 }
1490
1491 while (n_left_from > 0 && n_left_to_next > 0)
1492 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001493 vlib_buffer_t *p0;
1494 ip6_header_t *ip0;
1495 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496 u32 pi0, ip_len0, udp_len0, flags0, next0;
1497 i32 len_diff0;
1498 u8 error0, type0, good_l4_checksum0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001499 u32 udp_offset0;
Dave Barach75fc8542016-10-11 16:16:02 -04001500
Ed Warnickecb9cada2015-12-08 15:45:58 -07001501 pi0 = to_next[0] = from[0];
1502 from += 1;
1503 n_left_from -= 1;
1504 to_next += 1;
1505 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04001506
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507 p0 = vlib_get_buffer (vm, pi0);
1508
1509 ip0 = vlib_buffer_get_current (p0);
1510
1511 type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1512 next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1513
1514 flags0 = p0->flags;
1515
1516 good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
Shwethab78292e2016-09-13 11:51:00 +01001517 len_diff0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001518
Shwethab78292e2016-09-13 11:51:00 +01001519 /* Skip HBH local processing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001520 if (PREDICT_FALSE
1521 (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
Shwethab78292e2016-09-13 11:51:00 +01001522 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001523 ip6_hop_by_hop_ext_t *ext_hdr =
1524 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip0);
Shwethab78292e2016-09-13 11:51:00 +01001525 next0 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1526 type0 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1527 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001528 if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1529 IP_PROTOCOL_UDP,
1530 &udp_offset0)))
Shwethab78292e2016-09-13 11:51:00 +01001531 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001532 udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
Shwethab78292e2016-09-13 11:51:00 +01001533 /* Don't verify UDP checksum for packets with explicit zero checksum. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001534 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1535 && udp0->checksum == 0;
Shwethab78292e2016-09-13 11:51:00 +01001536 /* Verify UDP length. */
1537 ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1538 udp_len0 = clib_net_to_host_u16 (udp0->length);
1539 len_diff0 = ip_len0 - udp_len0;
1540 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001541
1542 good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543 len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1544
1545 if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
Dave Barachd7cb1b52016-12-09 09:52:16 -05001546 && !good_l4_checksum0
1547 && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001548 {
1549 flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1550 good_l4_checksum0 =
1551 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1552 }
1553
1554 error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1555
1556 error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1557
Dave Barachd7cb1b52016-12-09 09:52:16 -05001558 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1559 IP6_ERROR_UDP_CHECKSUM);
1560 ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1561 IP6_ERROR_ICMP_CHECKSUM);
1562 error0 =
1563 (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564
Dave Barachd7cb1b52016-12-09 09:52:16 -05001565 /* If this is a neighbor solicitation (ICMP), skip source RPF check */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001566 if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1567 type0 != IP_BUILTIN_PROTOCOL_ICMP &&
Dave Barachd7cb1b52016-12-09 09:52:16 -05001568 !ip6_address_is_link_local_unicast (&ip0->src_address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001569 {
1570 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001571 error0 = (ADJ_INDEX_INVALID == src_adj_index0
Dave Barachd7cb1b52016-12-09 09:52:16 -05001572 ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001573 }
1574
Dave Barachd7cb1b52016-12-09 09:52:16 -05001575 next0 =
1576 error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001577
1578 p0->error = error_node->errors[error0];
1579
1580 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1581 to_next, n_left_to_next,
1582 pi0, next0);
1583 }
Dave Barach75fc8542016-10-11 16:16:02 -04001584
Ed Warnickecb9cada2015-12-08 15:45:58 -07001585 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1586 }
1587
1588 return frame->n_vectors;
1589}
1590
Dave Barachd7cb1b52016-12-09 09:52:16 -05001591/* *INDENT-OFF* */
1592VLIB_REGISTER_NODE (ip6_local_node, static) =
1593{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001594 .function = ip6_local,
1595 .name = "ip6-local",
1596 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001597 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598 .n_next_nodes = IP_LOCAL_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001599 .next_nodes =
1600 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001601 [IP_LOCAL_NEXT_DROP] = "error-drop",
1602 [IP_LOCAL_NEXT_PUNT] = "error-punt",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603 [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1604 [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1605 },
1606};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001607/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608
Dave Barachd7cb1b52016-12-09 09:52:16 -05001609VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
Damjan Marion1c80e832016-05-11 23:07:18 +02001610
Dave Barachd7cb1b52016-12-09 09:52:16 -05001611void
1612ip6_register_protocol (u32 protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001613{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001614 vlib_main_t *vm = vlib_get_main ();
1615 ip6_main_t *im = &ip6_main;
1616 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617
1618 ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001619 lm->local_next_by_ip_protocol[protocol] =
1620 vlib_node_add_next (vm, ip6_local_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001621}
1622
Dave Barachd7cb1b52016-12-09 09:52:16 -05001623typedef enum
1624{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001625 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
John Lod1f5d042016-04-12 18:20:39 -04001626 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627 IP6_DISCOVER_NEIGHBOR_N_NEXT,
1628} ip6_discover_neighbor_next_t;
1629
Dave Barachd7cb1b52016-12-09 09:52:16 -05001630typedef enum
1631{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001632 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1633 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
Pierre Pfisterd076f192016-06-22 12:58:30 +01001634 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001635} ip6_discover_neighbor_error_t;
1636
1637static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001638ip6_discover_neighbor_inline (vlib_main_t * vm,
1639 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001640 vlib_frame_t * frame, int is_glean)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001641{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001642 vnet_main_t *vnm = vnet_get_main ();
1643 ip6_main_t *im = &ip6_main;
1644 ip_lookup_main_t *lm = &im->lookup_main;
1645 u32 *from, *to_next_drop;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646 uword n_left_from, n_left_to_next_drop;
1647 static f64 time_last_seed_change = -1e100;
1648 static u32 hash_seeds[3];
Dave Barach75fc8542016-10-11 16:16:02 -04001649 static uword hash_bitmap[256 / BITS (uword)];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001650 f64 time_now;
1651 int bogus_length;
1652
1653 if (node->flags & VLIB_NODE_FLAG_TRACE)
1654 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1655
1656 time_now = vlib_time_now (vm);
1657 if (time_now - time_last_seed_change > 1e-3)
1658 {
1659 uword i;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001660 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1661 sizeof (hash_seeds));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001662 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1663 hash_seeds[i] = r[i];
1664
1665 /* Mark all hash keys as been not-seen before. */
1666 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1667 hash_bitmap[i] = 0;
1668
1669 time_last_seed_change = time_now;
1670 }
1671
1672 from = vlib_frame_vector_args (frame);
1673 n_left_from = frame->n_vectors;
1674
1675 while (n_left_from > 0)
1676 {
1677 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1678 to_next_drop, n_left_to_next_drop);
1679
1680 while (n_left_from > 0 && n_left_to_next_drop > 0)
1681 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001682 vlib_buffer_t *p0;
1683 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001684 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1685 uword bm0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001686 ip_adjacency_t *adj0;
1687 vnet_hw_interface_t *hw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001688 u32 next0;
1689
1690 pi0 = from[0];
1691
1692 p0 = vlib_get_buffer (vm, pi0);
1693
1694 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1695
1696 ip0 = vlib_buffer_get_current (p0);
1697
1698 adj0 = ip_get_adjacency (lm, adj_index0);
1699
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001700 if (!is_glean)
1701 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001702 ip0->dst_address.as_u64[0] =
1703 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1704 ip0->dst_address.as_u64[1] =
1705 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001706 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001707
Ed Warnickecb9cada2015-12-08 15:45:58 -07001708 a0 = hash_seeds[0];
1709 b0 = hash_seeds[1];
1710 c0 = hash_seeds[2];
1711
1712 sw_if_index0 = adj0->rewrite_header.sw_if_index;
1713 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1714
1715 a0 ^= sw_if_index0;
1716 b0 ^= ip0->dst_address.as_u32[0];
1717 c0 ^= ip0->dst_address.as_u32[1];
1718
1719 hash_v3_mix32 (a0, b0, c0);
1720
1721 b0 ^= ip0->dst_address.as_u32[2];
1722 c0 ^= ip0->dst_address.as_u32[3];
1723
1724 hash_v3_finalize32 (a0, b0, c0);
1725
1726 c0 &= BITS (hash_bitmap) - 1;
1727 c0 = c0 / BITS (uword);
1728 m0 = (uword) 1 << (c0 % BITS (uword));
1729
1730 bm0 = hash_bitmap[c0];
1731 drop0 = (bm0 & m0) != 0;
1732
1733 /* Mark it as seen. */
1734 hash_bitmap[c0] = bm0 | m0;
1735
1736 from += 1;
1737 n_left_from -= 1;
1738 to_next_drop[0] = pi0;
1739 to_next_drop += 1;
1740 n_left_to_next_drop -= 1;
1741
Dave Barachd7cb1b52016-12-09 09:52:16 -05001742 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743
Dave Barachd7cb1b52016-12-09 09:52:16 -05001744 /* If the interface is link-down, drop the pkt */
1745 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1746 drop0 = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001747
Dave Barach75fc8542016-10-11 16:16:02 -04001748 p0->error =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001749 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1750 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001751 if (drop0)
1752 continue;
1753
Neale Rannsb80c5362016-10-08 13:03:40 +01001754 /*
1755 * the adj has been updated to a rewrite but the node the DPO that got
1756 * us here hasn't - yet. no big deal. we'll drop while we wait.
1757 */
1758 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1759 continue;
1760
Ed Warnickecb9cada2015-12-08 15:45:58 -07001761 {
1762 u32 bi0 = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001763 icmp6_neighbor_solicitation_header_t *h0;
1764 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765
Dave Barach75fc8542016-10-11 16:16:02 -04001766 h0 = vlib_packet_template_get_packet
Dave Barachd7cb1b52016-12-09 09:52:16 -05001767 (vm, &im->discover_neighbor_packet_template, &bi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001768
Dave Barach75fc8542016-10-11 16:16:02 -04001769 /*
Dave Barachd7cb1b52016-12-09 09:52:16 -05001770 * Build ethernet header.
1771 * Choose source address based on destination lookup
1772 * adjacency.
1773 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001774 if (ip6_src_address_for_packet (lm,
1775 sw_if_index0,
1776 &h0->ip.src_address))
1777 {
1778 /* There is no address on the interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001779 p0->error =
1780 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1781 vlib_buffer_free (vm, &bi0, 1);
Pierre Pfisterd076f192016-06-22 12:58:30 +01001782 continue;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001783 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001784
Dave Barach75fc8542016-10-11 16:16:02 -04001785 /*
Dave Barachd7cb1b52016-12-09 09:52:16 -05001786 * Destination address is a solicited node multicast address.
1787 * We need to fill in
1788 * the low 24 bits with low 24 bits of target's address.
1789 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001790 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1791 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1792 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1793
1794 h0->neighbor.target_address = ip0->dst_address;
1795
Dave Barach75fc8542016-10-11 16:16:02 -04001796 clib_memcpy (h0->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001797 hw_if0->hw_address, vec_len (hw_if0->hw_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001798
Dave Barachd7cb1b52016-12-09 09:52:16 -05001799 /* $$$$ appears we need this; why is the checksum non-zero? */
1800 h0->neighbor.icmp.checksum = 0;
Dave Barach75fc8542016-10-11 16:16:02 -04001801 h0->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001802 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1803 &bogus_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001804
Dave Barachd7cb1b52016-12-09 09:52:16 -05001805 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001806
1807 vlib_buffer_copy_trace_flag (vm, p0, bi0);
1808 b0 = vlib_get_buffer (vm, bi0);
Dave Barach75fc8542016-10-11 16:16:02 -04001809 vnet_buffer (b0)->sw_if_index[VLIB_TX]
Dave Barachd7cb1b52016-12-09 09:52:16 -05001810 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001811
1812 /* Add rewrite/encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001813 vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001814 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1815
John Lod1f5d042016-04-12 18:20:39 -04001816 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001817
1818 vlib_set_next_frame_buffer (vm, node, next0, bi0);
1819 }
1820 }
1821
Dave Barach75fc8542016-10-11 16:16:02 -04001822 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001823 n_left_to_next_drop);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001824 }
1825
1826 return frame->n_vectors;
1827}
1828
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001829static uword
1830ip6_discover_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001831 vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001832{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001833 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001834}
1835
1836static uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001837ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001838{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001839 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001840}
1841
Dave Barachd7cb1b52016-12-09 09:52:16 -05001842static char *ip6_discover_neighbor_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001843 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001844 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
Pierre Pfisterd076f192016-06-22 12:58:30 +01001845 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1846 = "no source address for ND solicitation",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001847};
1848
Dave Barachd7cb1b52016-12-09 09:52:16 -05001849/* *INDENT-OFF* */
1850VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1851{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001852 .function = ip6_discover_neighbor,
1853 .name = "ip6-discover-neighbor",
1854 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001855 .format_trace = format_ip6_forward_next_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001856 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1857 .error_strings = ip6_discover_neighbor_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001858 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001859 .next_nodes =
1860 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001861 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
John Lod1f5d042016-04-12 18:20:39 -04001862 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001863 },
1864};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001865/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001866
Dave Barachd7cb1b52016-12-09 09:52:16 -05001867/* *INDENT-OFF* */
1868VLIB_REGISTER_NODE (ip6_glean_node) =
1869{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001870 .function = ip6_glean,
1871 .name = "ip6-glean",
1872 .vector_size = sizeof (u32),
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001873 .format_trace = format_ip6_forward_next_trace,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001874 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1875 .error_strings = ip6_discover_neighbor_error_strings,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001876 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001877 .next_nodes =
1878 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001879 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1880 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1881 },
1882};
Dave Barachd7cb1b52016-12-09 09:52:16 -05001883/* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001884
Ed Warnickecb9cada2015-12-08 15:45:58 -07001885clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1887{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001888 vnet_main_t *vnm = vnet_get_main ();
1889 ip6_main_t *im = &ip6_main;
1890 icmp6_neighbor_solicitation_header_t *h;
1891 ip6_address_t *src;
1892 ip_interface_address_t *ia;
1893 ip_adjacency_t *adj;
1894 vnet_hw_interface_t *hi;
1895 vnet_sw_interface_t *si;
1896 vlib_buffer_t *b;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001897 u32 bi = 0;
1898 int bogus_length;
1899
1900 si = vnet_get_sw_interface (vnm, sw_if_index);
1901
1902 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1903 {
1904 return clib_error_return (0, "%U: interface %U down",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001905 format_ip6_address, dst,
1906 format_vnet_sw_if_index_name, vnm,
1907 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001908 }
1909
Dave Barachd7cb1b52016-12-09 09:52:16 -05001910 src =
1911 ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1912 if (!src)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001913 {
1914 vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
Dave Barach75fc8542016-10-11 16:16:02 -04001915 return clib_error_return
Dave Barachd7cb1b52016-12-09 09:52:16 -05001916 (0, "no matching interface address for destination %U (interface %U)",
1917 format_ip6_address, dst,
1918 format_vnet_sw_if_index_name, vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001919 }
1920
Dave Barachd7cb1b52016-12-09 09:52:16 -05001921 h =
1922 vlib_packet_template_get_packet (vm,
1923 &im->discover_neighbor_packet_template,
1924 &bi);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001925
1926 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1927
1928 /* Destination address is a solicited node multicast address. We need to fill in
1929 the low 24 bits with low 24 bits of target's address. */
1930 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1931 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1932 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1933
1934 h->ip.src_address = src[0];
1935 h->neighbor.target_address = dst[0];
1936
Dave Barachd7cb1b52016-12-09 09:52:16 -05001937 clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1938 vec_len (hi->hw_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001939
Dave Barach75fc8542016-10-11 16:16:02 -04001940 h->neighbor.icmp.checksum =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001941 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001942 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001943
1944 b = vlib_get_buffer (vm, bi);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001945 vnet_buffer (b)->sw_if_index[VLIB_RX] =
1946 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001947
1948 /* Add encapsulation string for software interface (e.g. ethernet header). */
1949 adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
1950 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1951 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1952
1953 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001954 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1955 u32 *to_next = vlib_frame_vector_args (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001956 to_next[0] = bi;
1957 f->n_vectors = 1;
1958 vlib_put_frame_to_node (vm, hi->output_node_index, f);
1959 }
1960
1961 return /* no error */ 0;
1962}
1963
Dave Barachd7cb1b52016-12-09 09:52:16 -05001964typedef enum
1965{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001966 IP6_REWRITE_NEXT_DROP,
Chris Luke816f3e12016-06-14 16:24:47 -04001967 IP6_REWRITE_NEXT_ICMP_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001968} ip6_rewrite_next_t;
1969
1970always_inline uword
1971ip6_rewrite_inline (vlib_main_t * vm,
1972 vlib_node_runtime_t * node,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001973 vlib_frame_t * frame, int is_midchain)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001974{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001975 ip_lookup_main_t *lm = &ip6_main.lookup_main;
1976 u32 *from = vlib_frame_vector_args (frame);
1977 u32 n_left_from, n_left_to_next, *to_next, next_index;
1978 vlib_node_runtime_t *error_node =
1979 vlib_node_get_runtime (vm, ip6_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001980
1981 n_left_from = frame->n_vectors;
1982 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001983 u32 cpu_index = os_get_cpu_number ();
Dave Barach75fc8542016-10-11 16:16:02 -04001984
Ed Warnickecb9cada2015-12-08 15:45:58 -07001985 while (n_left_from > 0)
1986 {
1987 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1988
1989 while (n_left_from >= 4 && n_left_to_next >= 2)
1990 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001991 ip_adjacency_t *adj0, *adj1;
1992 vlib_buffer_t *p0, *p1;
1993 ip6_header_t *ip0, *ip1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001994 u32 pi0, rw_len0, next0, error0, adj_index0;
1995 u32 pi1, rw_len1, next1, error1, adj_index1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001996 u32 tx_sw_if_index0, tx_sw_if_index1;
Dave Barach75fc8542016-10-11 16:16:02 -04001997
Ed Warnickecb9cada2015-12-08 15:45:58 -07001998 /* Prefetch next iteration. */
1999 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002000 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002001
2002 p2 = vlib_get_buffer (vm, from[2]);
2003 p3 = vlib_get_buffer (vm, from[3]);
2004
2005 vlib_prefetch_buffer_header (p2, LOAD);
2006 vlib_prefetch_buffer_header (p3, LOAD);
2007
2008 CLIB_PREFETCH (p2->pre_data, 32, STORE);
2009 CLIB_PREFETCH (p3->pre_data, 32, STORE);
2010
2011 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2012 CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2013 }
2014
2015 pi0 = to_next[0] = from[0];
2016 pi1 = to_next[1] = from[1];
2017
2018 from += 2;
2019 n_left_from -= 2;
2020 to_next += 2;
2021 n_left_to_next -= 2;
Dave Barach75fc8542016-10-11 16:16:02 -04002022
Ed Warnickecb9cada2015-12-08 15:45:58 -07002023 p0 = vlib_get_buffer (vm, pi0);
2024 p1 = vlib_get_buffer (vm, pi1);
2025
Neale Rannsf06aea52016-11-29 06:51:37 -08002026 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2027 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002028
Dave Barachd7cb1b52016-12-09 09:52:16 -05002029 /* We should never rewrite a pkt using the MISS adjacency */
2030 ASSERT (adj_index0 && adj_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002031
2032 ip0 = vlib_buffer_get_current (p0);
2033 ip1 = vlib_buffer_get_current (p1);
2034
2035 error0 = error1 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002036 next0 = next1 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002037
Dave Barachd7cb1b52016-12-09 09:52:16 -05002038 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002039 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002040 i32 hop_limit0 = ip0->hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002041
2042 /* Input node should have reject packets with hop limit 0. */
2043 ASSERT (ip0->hop_limit > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002044
2045 hop_limit0 -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002046
2047 ip0->hop_limit = hop_limit0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002048
Dave Barachd7cb1b52016-12-09 09:52:16 -05002049 /*
2050 * If the hop count drops below 1 when forwarding, generate
2051 * an ICMP response.
2052 */
2053 if (PREDICT_FALSE (hop_limit0 <= 0))
2054 {
2055 error0 = IP6_ERROR_TIME_EXPIRED;
2056 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2057 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2058 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2059 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2060 0);
2061 }
Neale Rannsf06aea52016-11-29 06:51:37 -08002062 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002063 else
2064 {
2065 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2066 }
2067 if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2068 {
Neale Rannsf06aea52016-11-29 06:51:37 -08002069 i32 hop_limit1 = ip1->hop_limit;
2070
2071 /* Input node should have reject packets with hop limit 0. */
2072 ASSERT (ip1->hop_limit > 0);
2073
2074 hop_limit1 -= 1;
2075
2076 ip1->hop_limit = hop_limit1;
2077
Dave Barachd7cb1b52016-12-09 09:52:16 -05002078 /*
2079 * If the hop count drops below 1 when forwarding, generate
2080 * an ICMP response.
2081 */
2082 if (PREDICT_FALSE (hop_limit1 <= 0))
2083 {
2084 error1 = IP6_ERROR_TIME_EXPIRED;
2085 next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2086 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2087 icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2088 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2089 0);
2090 }
2091 }
2092 else
2093 {
2094 p1->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2095 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002096 adj0 = ip_get_adjacency (lm, adj_index0);
2097 adj1 = ip_get_adjacency (lm, adj_index1);
2098
Ed Warnickecb9cada2015-12-08 15:45:58 -07002099 rw_len0 = adj0[0].rewrite_header.data_bytes;
2100 rw_len1 = adj1[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002101 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2102 vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002103
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002104 vlib_increment_combined_counter (&adjacency_counters,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002105 cpu_index, adj_index0,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002106 /* packet increment */ 0,
2107 /* byte increment */ rw_len0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002108 vlib_increment_combined_counter (&adjacency_counters,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002109 cpu_index, adj_index1,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002110 /* packet increment */ 0,
2111 /* byte increment */ rw_len1);
2112
2113 /* Check MTU of outgoing interface. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002114 error0 =
2115 (vlib_buffer_length_in_chain (vm, p0) >
2116 adj0[0].
2117 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2118 error0);
2119 error1 =
2120 (vlib_buffer_length_in_chain (vm, p1) >
2121 adj1[0].
2122 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2123 error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002124
Dave Barachd7cb1b52016-12-09 09:52:16 -05002125 /* Don't adjust the buffer for hop count issue; icmp-error node
2126 * wants to see the IP headerr */
2127 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2128 {
2129 p0->current_data -= rw_len0;
2130 p0->current_length += rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002131
Dave Barachd7cb1b52016-12-09 09:52:16 -05002132 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2133 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2134 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04002135
Dave Barachd7cb1b52016-12-09 09:52:16 -05002136 vnet_feature_arc_start (lm->output_feature_arc_index,
2137 tx_sw_if_index0, &next0, p0);
2138 }
2139 if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2140 {
2141 p1->current_data -= rw_len1;
2142 p1->current_length += rw_len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002143
Dave Barachd7cb1b52016-12-09 09:52:16 -05002144 tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2145 vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2146 next1 = adj1[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04002147
Dave Barachd7cb1b52016-12-09 09:52:16 -05002148 vnet_feature_arc_start (lm->output_feature_arc_index,
2149 tx_sw_if_index1, &next1, p1);
2150 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002151
2152 /* Guess we are only writing on simple Ethernet header. */
2153 vnet_rewrite_two_headers (adj0[0], adj1[0],
Dave Barachd7cb1b52016-12-09 09:52:16 -05002154 ip0, ip1, sizeof (ethernet_header_t));
Dave Barach75fc8542016-10-11 16:16:02 -04002155
Neale Ranns5e575b12016-10-03 09:40:25 +01002156 if (is_midchain)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002157 {
2158 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2159 adj1->sub_type.midchain.fixup_func (vm, adj1, p1);
2160 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002161
Ed Warnickecb9cada2015-12-08 15:45:58 -07002162 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2163 to_next, n_left_to_next,
2164 pi0, pi1, next0, next1);
2165 }
2166
2167 while (n_left_from > 0 && n_left_to_next > 0)
2168 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002169 ip_adjacency_t *adj0;
2170 vlib_buffer_t *p0;
2171 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002172 u32 pi0, rw_len0;
2173 u32 adj_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002174 u32 tx_sw_if_index0;
Dave Barach75fc8542016-10-11 16:16:02 -04002175
Ed Warnickecb9cada2015-12-08 15:45:58 -07002176 pi0 = to_next[0] = from[0];
2177
2178 p0 = vlib_get_buffer (vm, pi0);
2179
Neale Rannsf06aea52016-11-29 06:51:37 -08002180 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002181
Dave Barachd7cb1b52016-12-09 09:52:16 -05002182 /* We should never rewrite a pkt using the MISS adjacency */
2183 ASSERT (adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002184
2185 adj0 = ip_get_adjacency (lm, adj_index0);
Dave Barach75fc8542016-10-11 16:16:02 -04002186
Ed Warnickecb9cada2015-12-08 15:45:58 -07002187 ip0 = vlib_buffer_get_current (p0);
2188
2189 error0 = IP6_ERROR_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002190 next0 = IP6_REWRITE_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002191
2192 /* Check hop limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002193 if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002194 {
2195 i32 hop_limit0 = ip0->hop_limit;
2196
2197 ASSERT (ip0->hop_limit > 0);
2198
2199 hop_limit0 -= 1;
2200
2201 ip0->hop_limit = hop_limit0;
2202
Dave Barachd7cb1b52016-12-09 09:52:16 -05002203 if (PREDICT_FALSE (hop_limit0 <= 0))
2204 {
2205 /*
2206 * If the hop count drops below 1 when forwarding, generate
2207 * an ICMP response.
2208 */
2209 error0 = IP6_ERROR_TIME_EXPIRED;
2210 next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2211 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2212 icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2213 ICMP6_time_exceeded_ttl_exceeded_in_transit,
2214 0);
2215 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002216 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002217 else
2218 {
2219 p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2220 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002221
Ed Warnickecb9cada2015-12-08 15:45:58 -07002222 /* Guess we are only writing on simple Ethernet header. */
2223 vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
Dave Barach75fc8542016-10-11 16:16:02 -04002224
Ed Warnickecb9cada2015-12-08 15:45:58 -07002225 /* Update packet buffer attributes/set output interface. */
2226 rw_len0 = adj0[0].rewrite_header.data_bytes;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002227 vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002228
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002229 vlib_increment_combined_counter (&adjacency_counters,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002230 cpu_index, adj_index0,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002231 /* packet increment */ 0,
2232 /* byte increment */ rw_len0);
2233
2234 /* Check MTU of outgoing interface. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002235 error0 =
2236 (vlib_buffer_length_in_chain (vm, p0) >
2237 adj0[0].
2238 rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2239 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002240
Dave Barachd7cb1b52016-12-09 09:52:16 -05002241 /* Don't adjust the buffer for hop count issue; icmp-error node
2242 * wants to see the IP headerr */
2243 if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2244 {
Chris Luke816f3e12016-06-14 16:24:47 -04002245 p0->current_data -= rw_len0;
2246 p0->current_length += rw_len0;
2247
Dave Barachd7cb1b52016-12-09 09:52:16 -05002248 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
Dave Barach5331c722016-08-17 11:54:30 -04002249
Dave Barachd7cb1b52016-12-09 09:52:16 -05002250 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2251 next0 = adj0[0].rewrite_header.next_index;
Dave Barach5331c722016-08-17 11:54:30 -04002252
Dave Barachd7cb1b52016-12-09 09:52:16 -05002253 vnet_feature_arc_start (lm->output_feature_arc_index,
2254 tx_sw_if_index0, &next0, p0);
2255 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002256
Neale Ranns5e575b12016-10-03 09:40:25 +01002257 if (is_midchain)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002258 {
2259 adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2260 }
Neale Ranns5e575b12016-10-03 09:40:25 +01002261
Ed Warnickecb9cada2015-12-08 15:45:58 -07002262 p0->error = error_node->errors[error0];
2263
2264 from += 1;
2265 n_left_from -= 1;
2266 to_next += 1;
2267 n_left_to_next -= 1;
Dave Barach75fc8542016-10-11 16:16:02 -04002268
Ed Warnickecb9cada2015-12-08 15:45:58 -07002269 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2270 to_next, n_left_to_next,
2271 pi0, next0);
2272 }
2273
2274 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2275 }
2276
2277 /* Need to do trace after rewrites to pick up new packet data. */
2278 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Rannsf06aea52016-11-29 06:51:37 -08002279 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002280
2281 return frame->n_vectors;
2282}
2283
2284static uword
Neale Rannsf06aea52016-11-29 06:51:37 -08002285ip6_rewrite (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002286 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002287{
2288 return ip6_rewrite_inline (vm, node, frame,
Neale Ranns5e575b12016-10-03 09:40:25 +01002289 /* midchain */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002290}
2291
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002292static uword
2293ip6_midchain (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002294 vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002295{
2296 return ip6_rewrite_inline (vm, node, frame,
Neale Ranns5e575b12016-10-03 09:40:25 +01002297 /* midchain */ 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002298}
2299
Dave Barachd7cb1b52016-12-09 09:52:16 -05002300/* *INDENT-OFF* */
2301VLIB_REGISTER_NODE (ip6_midchain_node) =
2302{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002303 .function = ip6_midchain,
2304 .name = "ip6-midchain",
2305 .vector_size = sizeof (u32),
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002306 .format_trace = format_ip6_forward_next_trace,
Neale Ranns5e575b12016-10-03 09:40:25 +01002307 .sibling_of = "ip6-rewrite",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002308 };
2309/* *INDENT-ON* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002310
Dave Barachd7cb1b52016-12-09 09:52:16 -05002311VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002312
Dave Barachd7cb1b52016-12-09 09:52:16 -05002313/* *INDENT-OFF* */
2314VLIB_REGISTER_NODE (ip6_rewrite_node) =
2315{
Neale Rannsf06aea52016-11-29 06:51:37 -08002316 .function = ip6_rewrite,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002317 .name = "ip6-rewrite",
2318 .vector_size = sizeof (u32),
Pierre Pfistera38c3df2016-06-13 10:28:09 +01002319 .format_trace = format_ip6_rewrite_trace,
Chris Luke816f3e12016-06-14 16:24:47 -04002320 .n_next_nodes = 2,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002321 .next_nodes =
2322 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002323 [IP6_REWRITE_NEXT_DROP] = "error-drop",
Chris Luke816f3e12016-06-14 16:24:47 -04002324 [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002325 },
2326};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002327/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002328
Neale Rannsf06aea52016-11-29 06:51:37 -08002329VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
Damjan Marion1c80e832016-05-11 23:07:18 +02002330
Ole Troan944f5482016-05-24 11:56:58 +02002331/*
2332 * Hop-by-Hop handling
2333 */
2334
2335ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2336
2337#define foreach_ip6_hop_by_hop_error \
2338_(PROCESSED, "pkts with ip6 hop-by-hop options") \
2339_(FORMAT, "incorrectly formatted hop-by-hop options") \
2340_(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2341
Dave Barachd7cb1b52016-12-09 09:52:16 -05002342typedef enum
2343{
Ole Troan944f5482016-05-24 11:56:58 +02002344#define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2345 foreach_ip6_hop_by_hop_error
2346#undef _
Dave Barachd7cb1b52016-12-09 09:52:16 -05002347 IP6_HOP_BY_HOP_N_ERROR,
Ole Troan944f5482016-05-24 11:56:58 +02002348} ip6_hop_by_hop_error_t;
2349
2350/*
2351 * Primary h-b-h handler trace support
2352 * We work pretty hard on the problem for obvious reasons
2353 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002354typedef struct
2355{
Ole Troan944f5482016-05-24 11:56:58 +02002356 u32 next_index;
2357 u32 trace_len;
2358 u8 option_data[256];
2359} ip6_hop_by_hop_trace_t;
2360
2361vlib_node_registration_t ip6_hop_by_hop_node;
2362
Dave Barachd7cb1b52016-12-09 09:52:16 -05002363static char *ip6_hop_by_hop_error_strings[] = {
Ole Troan944f5482016-05-24 11:56:58 +02002364#define _(sym,string) string,
2365 foreach_ip6_hop_by_hop_error
2366#undef _
2367};
2368
2369static u8 *
2370format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2371{
2372 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2373 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002374 ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
Ole Troan944f5482016-05-24 11:56:58 +02002375 ip6_hop_by_hop_header_t *hbh0;
2376 ip6_hop_by_hop_option_t *opt0, *limit0;
2377 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2378
2379 u8 type0;
2380
Dave Barachd7cb1b52016-12-09 09:52:16 -05002381 hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
Ole Troan944f5482016-05-24 11:56:58 +02002382
2383 s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05002384 t->next_index, (hbh0->length + 1) << 3, t->trace_len);
Ole Troan944f5482016-05-24 11:56:58 +02002385
Dave Barachd7cb1b52016-12-09 09:52:16 -05002386 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2387 limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
Ole Troan944f5482016-05-24 11:56:58 +02002388
Dave Barachd7cb1b52016-12-09 09:52:16 -05002389 while (opt0 < limit0)
2390 {
2391 type0 = opt0->type;
2392 switch (type0)
2393 {
2394 case 0: /* Pad, just stop */
2395 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2396 break;
Ole Troan944f5482016-05-24 11:56:58 +02002397
Dave Barachd7cb1b52016-12-09 09:52:16 -05002398 default:
2399 if (hm->trace[type0])
2400 {
2401 s = (*hm->trace[type0]) (s, opt0);
2402 }
2403 else
2404 {
2405 s =
2406 format (s, "\n unrecognized option %d length %d", type0,
2407 opt0->length);
2408 }
2409 opt0 =
2410 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2411 sizeof (ip6_hop_by_hop_option_t));
2412 break;
2413 }
Ole Troan944f5482016-05-24 11:56:58 +02002414 }
Ole Troan944f5482016-05-24 11:56:58 +02002415 return s;
2416}
2417
Dave Barachd7cb1b52016-12-09 09:52:16 -05002418always_inline u8
2419ip6_scan_hbh_options (vlib_buffer_t * b0,
2420 ip6_header_t * ip0,
2421 ip6_hop_by_hop_header_t * hbh0,
2422 ip6_hop_by_hop_option_t * opt0,
2423 ip6_hop_by_hop_option_t * limit0, u32 * next0)
Shwethaa91cbe62016-08-08 15:51:04 +01002424{
2425 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2426 u8 type0;
2427 u8 error0 = 0;
2428
2429 while (opt0 < limit0)
2430 {
2431 type0 = opt0->type;
2432 switch (type0)
2433 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002434 case 0: /* Pad1 */
2435 opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
Shwethaa91cbe62016-08-08 15:51:04 +01002436 continue;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002437 case 1: /* PadN */
Shwethaa91cbe62016-08-08 15:51:04 +01002438 break;
2439 default:
2440 if (hm->options[type0])
2441 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002442 if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2443 {
Shwethaa91cbe62016-08-08 15:51:04 +01002444 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002445 return (error0);
2446 }
Shwethaa91cbe62016-08-08 15:51:04 +01002447 }
2448 else
2449 {
2450 /* Unrecognized mandatory option, check the two high order bits */
2451 switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2452 {
2453 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2454 break;
2455 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2456 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2457 *next0 = IP_LOOKUP_NEXT_DROP;
2458 break;
2459 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2460 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2461 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002462 icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2463 ICMP6_parameter_problem_unrecognized_option,
2464 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002465 break;
2466 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2467 error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002468 if (!ip6_address_is_multicast (&ip0->dst_address))
Shwethaa91cbe62016-08-08 15:51:04 +01002469 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002470 *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2471 icmp6_error_set_vnet_buffer (b0,
2472 ICMP6_parameter_problem,
2473 ICMP6_parameter_problem_unrecognized_option,
2474 (u8 *) opt0 - (u8 *) ip0);
Shwethaa91cbe62016-08-08 15:51:04 +01002475 }
2476 else
2477 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002478 *next0 = IP_LOOKUP_NEXT_DROP;
Shwethaa91cbe62016-08-08 15:51:04 +01002479 }
2480 break;
2481 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002482 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002483 }
2484 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002485 opt0 =
2486 (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2487 sizeof (ip6_hop_by_hop_option_t));
Shwethaa91cbe62016-08-08 15:51:04 +01002488 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002489 return (error0);
Shwethaa91cbe62016-08-08 15:51:04 +01002490}
2491
Ole Troan944f5482016-05-24 11:56:58 +02002492/*
2493 * Process the Hop-by-Hop Options header
2494 */
2495static uword
2496ip6_hop_by_hop (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002497 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ole Troan944f5482016-05-24 11:56:58 +02002498{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002499 vlib_node_runtime_t *error_node =
2500 vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
Ole Troan944f5482016-05-24 11:56:58 +02002501 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2502 u32 n_left_from, *from, *to_next;
2503 ip_lookup_next_t next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002504 ip6_main_t *im = &ip6_main;
Ole Troan944f5482016-05-24 11:56:58 +02002505 ip_lookup_main_t *lm = &im->lookup_main;
2506
2507 from = vlib_frame_vector_args (frame);
2508 n_left_from = frame->n_vectors;
2509 next_index = node->cached_next_index;
2510
Dave Barachd7cb1b52016-12-09 09:52:16 -05002511 while (n_left_from > 0)
2512 {
2513 u32 n_left_to_next;
Ole Troan944f5482016-05-24 11:56:58 +02002514
Dave Barachd7cb1b52016-12-09 09:52:16 -05002515 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ole Troan944f5482016-05-24 11:56:58 +02002516
Dave Barachd7cb1b52016-12-09 09:52:16 -05002517 while (n_left_from >= 4 && n_left_to_next >= 2)
Shwethaa91cbe62016-08-08 15:51:04 +01002518 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002519 u32 bi0, bi1;
2520 vlib_buffer_t *b0, *b1;
2521 u32 next0, next1;
2522 ip6_header_t *ip0, *ip1;
2523 ip6_hop_by_hop_header_t *hbh0, *hbh1;
2524 ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2525 u8 error0 = 0, error1 = 0;
2526
2527 /* Prefetch next iteration. */
2528 {
2529 vlib_buffer_t *p2, *p3;
2530
2531 p2 = vlib_get_buffer (vm, from[2]);
2532 p3 = vlib_get_buffer (vm, from[3]);
2533
2534 vlib_prefetch_buffer_header (p2, LOAD);
2535 vlib_prefetch_buffer_header (p3, LOAD);
2536
2537 CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2538 CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Shwethaa91cbe62016-08-08 15:51:04 +01002539 }
2540
Dave Barachd7cb1b52016-12-09 09:52:16 -05002541 /* Speculatively enqueue b0, b1 to the current next frame */
2542 to_next[0] = bi0 = from[0];
2543 to_next[1] = bi1 = from[1];
2544 from += 2;
2545 to_next += 2;
2546 n_left_from -= 2;
2547 n_left_to_next -= 2;
2548
2549 b0 = vlib_get_buffer (vm, bi0);
2550 b1 = vlib_get_buffer (vm, bi1);
2551
2552 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2553 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2554 ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2555 u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2556 ip_adjacency_t *adj1 = ip_get_adjacency (lm, adj_index1);
2557
2558 /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2559 next0 = adj0->lookup_next_index;
2560 next1 = adj1->lookup_next_index;
2561
2562 ip0 = vlib_buffer_get_current (b0);
2563 ip1 = vlib_buffer_get_current (b1);
2564 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2565 hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2566 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2567 opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2568 limit0 =
2569 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2570 ((hbh0->length + 1) << 3));
2571 limit1 =
2572 (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2573 ((hbh1->length + 1) << 3));
2574
2575 /*
2576 * Basic validity checks
2577 */
2578 if ((hbh0->length + 1) << 3 >
2579 clib_net_to_host_u16 (ip0->payload_length))
2580 {
2581 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2582 next0 = IP_LOOKUP_NEXT_DROP;
2583 goto outdual;
2584 }
2585 /* Scan the set of h-b-h options, process ones that we understand */
2586 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2587
2588 if ((hbh1->length + 1) << 3 >
2589 clib_net_to_host_u16 (ip1->payload_length))
2590 {
2591 error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2592 next1 = IP_LOOKUP_NEXT_DROP;
2593 goto outdual;
2594 }
2595 /* Scan the set of h-b-h options, process ones that we understand */
2596 error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2597
2598 outdual:
2599 /* Has the classifier flagged this buffer for special treatment? */
2600 if (PREDICT_FALSE
2601 ((error0 == 0)
2602 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2603 next0 = hm->next_override;
2604
2605 /* Has the classifier flagged this buffer for special treatment? */
2606 if (PREDICT_FALSE
2607 ((error1 == 0)
2608 && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2609 next1 = hm->next_override;
2610
2611 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2612 {
2613 if (b0->flags & VLIB_BUFFER_IS_TRACED)
2614 {
2615 ip6_hop_by_hop_trace_t *t =
2616 vlib_add_trace (vm, node, b0, sizeof (*t));
2617 u32 trace_len = (hbh0->length + 1) << 3;
2618 t->next_index = next0;
2619 /* Capture the h-b-h option verbatim */
2620 trace_len =
2621 trace_len <
2622 ARRAY_LEN (t->option_data) ? trace_len :
2623 ARRAY_LEN (t->option_data);
2624 t->trace_len = trace_len;
2625 clib_memcpy (t->option_data, hbh0, trace_len);
2626 }
2627 if (b1->flags & VLIB_BUFFER_IS_TRACED)
2628 {
2629 ip6_hop_by_hop_trace_t *t =
2630 vlib_add_trace (vm, node, b1, sizeof (*t));
2631 u32 trace_len = (hbh1->length + 1) << 3;
2632 t->next_index = next1;
2633 /* Capture the h-b-h option verbatim */
2634 trace_len =
2635 trace_len <
2636 ARRAY_LEN (t->option_data) ? trace_len :
2637 ARRAY_LEN (t->option_data);
2638 t->trace_len = trace_len;
2639 clib_memcpy (t->option_data, hbh1, trace_len);
2640 }
2641
2642 }
2643
2644 b0->error = error_node->errors[error0];
2645 b1->error = error_node->errors[error1];
2646
2647 /* verify speculative enqueue, maybe switch current next frame */
2648 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2649 n_left_to_next, bi0, bi1, next0,
2650 next1);
Shwethaa91cbe62016-08-08 15:51:04 +01002651 }
2652
Dave Barachd7cb1b52016-12-09 09:52:16 -05002653 while (n_left_from > 0 && n_left_to_next > 0)
2654 {
2655 u32 bi0;
2656 vlib_buffer_t *b0;
2657 u32 next0;
2658 ip6_header_t *ip0;
2659 ip6_hop_by_hop_header_t *hbh0;
2660 ip6_hop_by_hop_option_t *opt0, *limit0;
2661 u8 error0 = 0;
Shwethaa91cbe62016-08-08 15:51:04 +01002662
Dave Barachd7cb1b52016-12-09 09:52:16 -05002663 /* Speculatively enqueue b0 to the current next frame */
2664 bi0 = from[0];
2665 to_next[0] = bi0;
2666 from += 1;
2667 to_next += 1;
2668 n_left_from -= 1;
2669 n_left_to_next -= 1;
2670
2671 b0 = vlib_get_buffer (vm, bi0);
2672 /*
2673 * Default use the next_index from the adjacency.
2674 * A HBH option rarely redirects to a different node
2675 */
2676 u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2677 ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2678 next0 = adj0->lookup_next_index;
2679
2680 ip0 = vlib_buffer_get_current (b0);
2681 hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2682 opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2683 limit0 =
2684 (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2685 ((hbh0->length + 1) << 3));
2686
2687 /*
2688 * Basic validity checks
2689 */
2690 if ((hbh0->length + 1) << 3 >
2691 clib_net_to_host_u16 (ip0->payload_length))
2692 {
2693 error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2694 next0 = IP_LOOKUP_NEXT_DROP;
2695 goto out0;
2696 }
2697
2698 /* Scan the set of h-b-h options, process ones that we understand */
2699 error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2700
2701 out0:
2702 /* Has the classifier flagged this buffer for special treatment? */
2703 if (PREDICT_FALSE
2704 ((error0 == 0)
2705 && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2706 next0 = hm->next_override;
2707
2708 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2709 {
2710 ip6_hop_by_hop_trace_t *t =
2711 vlib_add_trace (vm, node, b0, sizeof (*t));
2712 u32 trace_len = (hbh0->length + 1) << 3;
2713 t->next_index = next0;
2714 /* Capture the h-b-h option verbatim */
2715 trace_len =
2716 trace_len <
2717 ARRAY_LEN (t->option_data) ? trace_len :
2718 ARRAY_LEN (t->option_data);
2719 t->trace_len = trace_len;
2720 clib_memcpy (t->option_data, hbh0, trace_len);
2721 }
2722
2723 b0->error = error_node->errors[error0];
2724
2725 /* verify speculative enqueue, maybe switch current next frame */
2726 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2727 n_left_to_next, bi0, next0);
2728 }
2729 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Shwethaa91cbe62016-08-08 15:51:04 +01002730 }
Ole Troan944f5482016-05-24 11:56:58 +02002731 return frame->n_vectors;
2732}
2733
Dave Barachd7cb1b52016-12-09 09:52:16 -05002734/* *INDENT-OFF* */
2735VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2736{
Ole Troan944f5482016-05-24 11:56:58 +02002737 .function = ip6_hop_by_hop,
2738 .name = "ip6-hop-by-hop",
Ole Troan964f93e2016-06-10 13:22:36 +02002739 .sibling_of = "ip6-lookup",
Ole Troan944f5482016-05-24 11:56:58 +02002740 .vector_size = sizeof (u32),
2741 .format_trace = format_ip6_hop_by_hop_trace,
2742 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002743 .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
Ole Troan944f5482016-05-24 11:56:58 +02002744 .error_strings = ip6_hop_by_hop_error_strings,
Ole Troan964f93e2016-06-10 13:22:36 +02002745 .n_next_nodes = 0,
Ole Troan944f5482016-05-24 11:56:58 +02002746};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002747/* *INDENT-ON* */
Ole Troan944f5482016-05-24 11:56:58 +02002748
Dave Barach5331c722016-08-17 11:54:30 -04002749VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
Ole Troan944f5482016-05-24 11:56:58 +02002750
2751static clib_error_t *
2752ip6_hop_by_hop_init (vlib_main_t * vm)
2753{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002754 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2755 memset (hm->options, 0, sizeof (hm->options));
2756 memset (hm->trace, 0, sizeof (hm->trace));
Shwethaa91cbe62016-08-08 15:51:04 +01002757 hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
Ole Troan944f5482016-05-24 11:56:58 +02002758 return (0);
2759}
2760
2761VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2762
Dave Barachd7cb1b52016-12-09 09:52:16 -05002763void
2764ip6_hbh_set_next_override (uword next)
Shwethaa91cbe62016-08-08 15:51:04 +01002765{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002766 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Shwethaa91cbe62016-08-08 15:51:04 +01002767
2768 hm->next_override = next;
2769}
2770
Ole Troan944f5482016-05-24 11:56:58 +02002771int
2772ip6_hbh_register_option (u8 option,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002773 int options (vlib_buffer_t * b, ip6_header_t * ip,
2774 ip6_hop_by_hop_option_t * opt),
2775 u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
Ole Troan944f5482016-05-24 11:56:58 +02002776{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002777 ip6_main_t *im = &ip6_main;
2778 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002779
2780 ASSERT (option < ARRAY_LEN (hm->options));
2781
2782 /* Already registered */
2783 if (hm->options[option])
2784 return (-1);
2785
2786 hm->options[option] = options;
2787 hm->trace[option] = trace;
2788
2789 /* Set global variable */
2790 im->hbh_enabled = 1;
2791
2792 return (0);
2793}
2794
2795int
2796ip6_hbh_unregister_option (u8 option)
2797{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002798 ip6_main_t *im = &ip6_main;
2799 ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
Ole Troan944f5482016-05-24 11:56:58 +02002800
2801 ASSERT (option < ARRAY_LEN (hm->options));
2802
2803 /* Not registered */
2804 if (!hm->options[option])
2805 return (-1);
2806
2807 hm->options[option] = NULL;
2808 hm->trace[option] = NULL;
2809
2810 /* Disable global knob if this was the last option configured */
2811 int i;
2812 bool found = false;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002813 for (i = 0; i < 256; i++)
2814 {
2815 if (hm->options[option])
2816 {
2817 found = true;
2818 break;
2819 }
Ole Troan944f5482016-05-24 11:56:58 +02002820 }
Ole Troan944f5482016-05-24 11:56:58 +02002821 if (!found)
2822 im->hbh_enabled = 0;
2823
2824 return (0);
2825}
2826
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827/* Global IP6 main. */
2828ip6_main_t ip6_main;
2829
2830static clib_error_t *
2831ip6_lookup_init (vlib_main_t * vm)
2832{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002833 ip6_main_t *im = &ip6_main;
2834 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002835 uword i;
2836
Damjan Marion8b3191e2016-11-09 19:54:20 +01002837 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2838 return error;
2839
Ed Warnickecb9cada2015-12-08 15:45:58 -07002840 for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2841 {
2842 u32 j, i0, i1;
2843
2844 i0 = i / 32;
2845 i1 = i % 32;
2846
2847 for (j = 0; j < i0; j++)
2848 im->fib_masks[i].as_u32[j] = ~0;
2849
2850 if (i1)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002851 im->fib_masks[i].as_u32[i0] =
2852 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002853 }
2854
2855 ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2856
2857 if (im->lookup_table_nbuckets == 0)
2858 im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2859
Dave Barachd7cb1b52016-12-09 09:52:16 -05002860 im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002861
2862 if (im->lookup_table_size == 0)
2863 im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
Dave Barach75fc8542016-10-11 16:16:02 -04002864
Dave Barachd7cb1b52016-12-09 09:52:16 -05002865 BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2866 "ip6 FIB fwding table",
2867 im->lookup_table_nbuckets, im->lookup_table_size);
2868 BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2869 "ip6 FIB non-fwding table",
2870 im->lookup_table_nbuckets, im->lookup_table_size);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002871
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872 /* Create FIB with index 0 and table id of 0. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002873 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874
2875 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002876 pg_node_t *pn;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002877 pn = pg_get_node (ip6_lookup_node.index);
2878 pn->unformat_edit = unformat_pg_ip6_header;
2879 }
2880
Ole Troan944f5482016-05-24 11:56:58 +02002881 /* Unless explicitly configured, don't process HBH options */
2882 im->hbh_enabled = 0;
2883
Ed Warnickecb9cada2015-12-08 15:45:58 -07002884 {
2885 icmp6_neighbor_solicitation_header_t p;
2886
2887 memset (&p, 0, sizeof (p));
2888
Dave Barachd7cb1b52016-12-09 09:52:16 -05002889 p.ip.ip_version_traffic_class_and_flow_label =
2890 clib_host_to_net_u32 (0x6 << 28);
2891 p.ip.payload_length =
2892 clib_host_to_net_u16 (sizeof (p) -
2893 STRUCT_OFFSET_OF
2894 (icmp6_neighbor_solicitation_header_t, neighbor));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002895 p.ip.protocol = IP_PROTOCOL_ICMP6;
2896 p.ip.hop_limit = 255;
2897 ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2898
2899 p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2900
Dave Barachd7cb1b52016-12-09 09:52:16 -05002901 p.link_layer_option.header.type =
2902 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2903 p.link_layer_option.header.n_data_u64s =
2904 sizeof (p.link_layer_option) / sizeof (u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002905
2906 vlib_packet_template_init (vm,
2907 &im->discover_neighbor_packet_template,
2908 &p, sizeof (p),
2909 /* alloc chunk size */ 8,
2910 "ip6 neighbor discovery");
2911 }
2912
Dave Barach203c6322016-06-26 10:29:03 -04002913 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002914}
2915
2916VLIB_INIT_FUNCTION (ip6_lookup_init);
2917
2918static clib_error_t *
2919add_del_ip6_interface_table (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002920 unformat_input_t * input,
2921 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002922{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002923 vnet_main_t *vnm = vnet_get_main ();
2924 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925 u32 sw_if_index, table_id;
2926
2927 sw_if_index = ~0;
2928
Dave Barachd7cb1b52016-12-09 09:52:16 -05002929 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002930 {
2931 error = clib_error_return (0, "unknown interface `%U'",
2932 format_unformat_error, input);
2933 goto done;
2934 }
2935
2936 if (unformat (input, "%d", &table_id))
2937 ;
2938 else
2939 {
2940 error = clib_error_return (0, "expected table id `%U'",
2941 format_unformat_error, input);
2942 goto done;
2943 }
2944
2945 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002946 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2947 table_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002948
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002949 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
2950 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002951 }
2952
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002953
Dave Barachd7cb1b52016-12-09 09:52:16 -05002954done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002955 return error;
2956}
2957
Billy McFall0683c9c2016-10-13 08:27:31 -04002958/*?
2959 * Place the indicated interface into the supplied IPv6 FIB table (also known
2960 * as a VRF). If the FIB table does not exist, this command creates it. To
2961 * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
2962 * FIB table will only be displayed if a route has been added to the table, or
2963 * an IP Address is assigned to an interface in the table (which adds a route
2964 * automatically).
2965 *
2966 * @note IP addresses added after setting the interface IP table end up in
2967 * the indicated FIB table. If the IP address is added prior to adding the
2968 * interface to the FIB table, it will NOT be part of the FIB table. Predictable
2969 * but potentially counter-intuitive results occur if you provision interface
2970 * addresses in multiple FIBs. Upon RX, packets will be processed in the last
2971 * IP table ID provisioned. It might be marginally useful to evade source RPF
2972 * drops to put an interface address into multiple FIBs.
2973 *
2974 * @cliexpar
2975 * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
2976 * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
2977 ?*/
2978/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002979VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
2980{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002981 .path = "set interface ip6 table",
2982 .function = add_del_ip6_interface_table,
Billy McFall0683c9c2016-10-13 08:27:31 -04002983 .short_help = "set interface ip6 table <interface> <table-id>"
Ed Warnickecb9cada2015-12-08 15:45:58 -07002984};
Billy McFall0683c9c2016-10-13 08:27:31 -04002985/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002986
Dave Barach75fc8542016-10-11 16:16:02 -04002987void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002988ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
2989 u8 * mac)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002990{
2991 ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2992 /* Invert the "u" bit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002993 ip->as_u8[8] = mac[0] ^ (1 << 1);
2994 ip->as_u8[9] = mac[1];
2995 ip->as_u8[10] = mac[2];
2996 ip->as_u8[11] = 0xFF;
2997 ip->as_u8[12] = 0xFE;
2998 ip->as_u8[13] = mac[3];
2999 ip->as_u8[14] = mac[4];
3000 ip->as_u8[15] = mac[5];
Ed Warnickecb9cada2015-12-08 15:45:58 -07003001}
3002
Dave Barach75fc8542016-10-11 16:16:02 -04003003void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003004ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3005 ip6_address_t * ip)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003006{
3007 /* Invert the previously inverted "u" bit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003008 mac[0] = ip->as_u8[8] ^ (1 << 1);
3009 mac[1] = ip->as_u8[9];
3010 mac[2] = ip->as_u8[10];
3011 mac[3] = ip->as_u8[13];
3012 mac[4] = ip->as_u8[14];
3013 mac[5] = ip->as_u8[15];
Ed Warnickecb9cada2015-12-08 15:45:58 -07003014}
3015
Dave Barach75fc8542016-10-11 16:16:02 -04003016static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07003017test_ip6_link_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003018 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003019{
3020 u8 mac[6];
3021 ip6_address_t _a, *a = &_a;
3022
3023 if (unformat (input, "%U", unformat_ethernet_address, mac))
3024 {
3025 ip6_link_local_address_from_ethernet_mac_address (a, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003026 vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003027 ip6_ethernet_mac_address_from_link_local_address (mac, a);
3028 vlib_cli_output (vm, "Original MAC address: %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003029 format_ethernet_address, mac);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003030 }
Dave Barach75fc8542016-10-11 16:16:02 -04003031
Ed Warnickecb9cada2015-12-08 15:45:58 -07003032 return 0;
3033}
3034
Billy McFall0683c9c2016-10-13 08:27:31 -04003035/*?
3036 * This command converts the given MAC Address into an IPv6 link-local
3037 * address.
3038 *
3039 * @cliexpar
3040 * Example of how to create an IPv6 link-local address:
3041 * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3042 * Link local address: fe80::14d9:e0ff:fe91:7986
3043 * Original MAC address: 16:d9:e0:91:79:86
3044 * @cliexend
3045?*/
3046/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003047VLIB_CLI_COMMAND (test_link_command, static) =
3048{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003049 .path = "test ip6 link",
Dave Barach75fc8542016-10-11 16:16:02 -04003050 .function = test_ip6_link_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003051 .short_help = "test ip6 link <mac-address>",
3052};
Billy McFall0683c9c2016-10-13 08:27:31 -04003053/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003054
Dave Barachd7cb1b52016-12-09 09:52:16 -05003055int
3056vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003057{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003058 ip6_main_t *im6 = &ip6_main;
3059 ip6_fib_t *fib;
3060 uword *p = hash_get (im6->fib_index_by_table_id, table_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003061
3062 if (p == 0)
3063 return -1;
3064
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003065 fib = ip6_fib_get (p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003066
3067 fib->flow_hash_config = flow_hash_config;
3068 return 1;
3069}
3070
3071static clib_error_t *
3072set_ip6_flow_hash_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003073 unformat_input_t * input,
3074 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003075{
3076 int matched = 0;
3077 u32 table_id = 0;
3078 u32 flow_hash_config = 0;
3079 int rv;
3080
Dave Barachd7cb1b52016-12-09 09:52:16 -05003081 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3082 {
3083 if (unformat (input, "table %d", &table_id))
3084 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003085#define _(a,v) \
3086 else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
Dave Barachd7cb1b52016-12-09 09:52:16 -05003087 foreach_flow_hash_bit
Ed Warnickecb9cada2015-12-08 15:45:58 -07003088#undef _
Dave Barachd7cb1b52016-12-09 09:52:16 -05003089 else
3090 break;
3091 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003092
3093 if (matched == 0)
3094 return clib_error_return (0, "unknown input `%U'",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003095 format_unformat_error, input);
Dave Barach75fc8542016-10-11 16:16:02 -04003096
Ed Warnickecb9cada2015-12-08 15:45:58 -07003097 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3098 switch (rv)
3099 {
3100 case 1:
3101 break;
3102
3103 case -1:
3104 return clib_error_return (0, "no such FIB table %d", table_id);
Dave Barach75fc8542016-10-11 16:16:02 -04003105
Ed Warnickecb9cada2015-12-08 15:45:58 -07003106 default:
3107 clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3108 break;
3109 }
Dave Barach75fc8542016-10-11 16:16:02 -04003110
Ed Warnickecb9cada2015-12-08 15:45:58 -07003111 return 0;
3112}
3113
Billy McFall0683c9c2016-10-13 08:27:31 -04003114/*?
3115 * Configure the set of IPv6 fields used by the flow hash.
3116 *
3117 * @cliexpar
3118 * @parblock
3119 * Example of how to set the flow hash on a given table:
Billy McFallebb9a6a2016-10-17 11:35:32 -04003120 * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3121 *
Billy McFall0683c9c2016-10-13 08:27:31 -04003122 * Example of display the configured flow hash:
3123 * @cliexstart{show ip6 fib}
Billy McFallebb9a6a2016-10-17 11:35:32 -04003124 * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3125 * @::/0
3126 * unicast-ip6-chain
3127 * [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3128 * [0] [@0]: dpo-drop ip6
3129 * fe80::/10
3130 * unicast-ip6-chain
3131 * [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3132 * [0] [@2]: dpo-receive
3133 * ff02::1/128
3134 * unicast-ip6-chain
3135 * [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3136 * [0] [@2]: dpo-receive
3137 * ff02::2/128
3138 * unicast-ip6-chain
3139 * [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3140 * [0] [@2]: dpo-receive
3141 * ff02::16/128
3142 * unicast-ip6-chain
3143 * [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3144 * [0] [@2]: dpo-receive
3145 * ff02::1:ff00:0/104
3146 * unicast-ip6-chain
3147 * [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3148 * [0] [@2]: dpo-receive
3149 * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3150 * @::/0
3151 * unicast-ip6-chain
3152 * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3153 * [0] [@0]: dpo-drop ip6
3154 * @::a:1:1:0:4/126
3155 * unicast-ip6-chain
3156 * [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3157 * [0] [@4]: ipv6-glean: af_packet0
3158 * @::a:1:1:0:7/128
3159 * unicast-ip6-chain
3160 * [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3161 * [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3162 * fe80::/10
3163 * unicast-ip6-chain
3164 * [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3165 * [0] [@2]: dpo-receive
3166 * fe80::fe:3eff:fe3e:9222/128
3167 * unicast-ip6-chain
3168 * [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3169 * [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3170 * ff02::1/128
3171 * unicast-ip6-chain
3172 * [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3173 * [0] [@2]: dpo-receive
3174 * ff02::2/128
3175 * unicast-ip6-chain
3176 * [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3177 * [0] [@2]: dpo-receive
3178 * ff02::16/128
3179 * unicast-ip6-chain
3180 * [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3181 * [0] [@2]: dpo-receive
3182 * ff02::1:ff00:0/104
3183 * unicast-ip6-chain
3184 * [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3185 * [0] [@2]: dpo-receive
Billy McFall0683c9c2016-10-13 08:27:31 -04003186 * @cliexend
3187 * @endparblock
3188?*/
3189/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003190VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3191{
3192 .path = "set ip6 flow-hash",
3193 .short_help =
3194 "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3195 .function = set_ip6_flow_hash_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003196};
Billy McFall0683c9c2016-10-13 08:27:31 -04003197/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003198
3199static clib_error_t *
3200show_ip6_local_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003201 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003202{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003203 ip6_main_t *im = &ip6_main;
3204 ip_lookup_main_t *lm = &im->lookup_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003205 int i;
Dave Barach75fc8542016-10-11 16:16:02 -04003206
Ed Warnickecb9cada2015-12-08 15:45:58 -07003207 vlib_cli_output (vm, "Protocols handled by ip6_local");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003208 for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003209 {
3210 if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003211 vlib_cli_output (vm, "%d", i);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003212 }
3213 return 0;
3214}
3215
3216
3217
Billy McFall0683c9c2016-10-13 08:27:31 -04003218/*?
3219 * Display the set of protocols handled by the local IPv6 stack.
3220 *
3221 * @cliexpar
3222 * Example of how to display local protocol table:
3223 * @cliexstart{show ip6 local}
3224 * Protocols handled by ip6_local
3225 * 17
3226 * 43
3227 * 58
3228 * 115
3229 * @cliexend
3230?*/
3231/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003232VLIB_CLI_COMMAND (show_ip6_local, static) =
3233{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003234 .path = "show ip6 local",
3235 .function = show_ip6_local_command_fn,
Billy McFall0683c9c2016-10-13 08:27:31 -04003236 .short_help = "show ip6 local",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003237};
Billy McFall0683c9c2016-10-13 08:27:31 -04003238/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003239
Dave Barachd7cb1b52016-12-09 09:52:16 -05003240int
3241vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3242 u32 table_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003243{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003244 vnet_main_t *vnm = vnet_get_main ();
3245 vnet_interface_main_t *im = &vnm->interface_main;
3246 ip6_main_t *ipm = &ip6_main;
3247 ip_lookup_main_t *lm = &ipm->lookup_main;
3248 vnet_classify_main_t *cm = &vnet_classify_main;
Neale Rannsdf089a82016-10-02 16:39:06 +01003249 ip6_address_t *if_addr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003250
3251 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3252 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3253
3254 if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3255 return VNET_API_ERROR_NO_SUCH_ENTRY;
3256
3257 vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003258 lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003259
Neale Rannsdf089a82016-10-02 16:39:06 +01003260 if_addr = ip6_interface_first_address (ipm, sw_if_index, NULL);
3261
3262 if (NULL != if_addr)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003263 {
Neale Rannsdf089a82016-10-02 16:39:06 +01003264 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003265 .fp_len = 128,
3266 .fp_proto = FIB_PROTOCOL_IP6,
3267 .fp_addr.ip6 = *if_addr,
Neale Rannsdf089a82016-10-02 16:39:06 +01003268 };
3269 u32 fib_index;
3270
Dave Barachd7cb1b52016-12-09 09:52:16 -05003271 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3272 sw_if_index);
Neale Rannsdf089a82016-10-02 16:39:06 +01003273
3274
Dave Barachd7cb1b52016-12-09 09:52:16 -05003275 if (table_index != (u32) ~ 0)
3276 {
3277 dpo_id_t dpo = DPO_INVALID;
Neale Rannsdf089a82016-10-02 16:39:06 +01003278
Dave Barachd7cb1b52016-12-09 09:52:16 -05003279 dpo_set (&dpo,
3280 DPO_CLASSIFY,
3281 DPO_PROTO_IP6,
3282 classify_dpo_create (DPO_PROTO_IP6, table_index));
Neale Rannsdf089a82016-10-02 16:39:06 +01003283
Dave Barachd7cb1b52016-12-09 09:52:16 -05003284 fib_table_entry_special_dpo_add (fib_index,
3285 &pfx,
3286 FIB_SOURCE_CLASSIFY,
3287 FIB_ENTRY_FLAG_NONE, &dpo);
3288 dpo_reset (&dpo);
3289 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003290 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003291 {
3292 fib_table_entry_special_remove (fib_index,
3293 &pfx, FIB_SOURCE_CLASSIFY);
3294 }
3295 }
Neale Rannsdf089a82016-10-02 16:39:06 +01003296
Ed Warnickecb9cada2015-12-08 15:45:58 -07003297 return 0;
3298}
3299
3300static clib_error_t *
3301set_ip6_classify_command_fn (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003302 unformat_input_t * input,
3303 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003304{
3305 u32 table_index = ~0;
3306 int table_index_set = 0;
3307 u32 sw_if_index = ~0;
3308 int rv;
Dave Barach75fc8542016-10-11 16:16:02 -04003309
Dave Barachd7cb1b52016-12-09 09:52:16 -05003310 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3311 {
3312 if (unformat (input, "table-index %d", &table_index))
3313 table_index_set = 1;
3314 else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3315 vnet_get_main (), &sw_if_index))
3316 ;
3317 else
3318 break;
3319 }
Dave Barach75fc8542016-10-11 16:16:02 -04003320
Ed Warnickecb9cada2015-12-08 15:45:58 -07003321 if (table_index_set == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003322 return clib_error_return (0, "classify table-index must be specified");
Dave Barach75fc8542016-10-11 16:16:02 -04003323
Ed Warnickecb9cada2015-12-08 15:45:58 -07003324 if (sw_if_index == ~0)
3325 return clib_error_return (0, "interface / subif must be specified");
3326
3327 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3328
3329 switch (rv)
3330 {
3331 case 0:
3332 break;
3333
3334 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3335 return clib_error_return (0, "No such interface");
3336
3337 case VNET_API_ERROR_NO_SUCH_ENTRY:
3338 return clib_error_return (0, "No such classifier table");
3339 }
3340 return 0;
3341}
3342
Billy McFall0683c9c2016-10-13 08:27:31 -04003343/*?
3344 * Assign a classification table to an interface. The classification
3345 * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3346 * commands. Once the table is create, use this command to filter packets
3347 * on an interface.
3348 *
3349 * @cliexpar
3350 * Example of how to assign a classification table to an interface:
3351 * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3352?*/
3353/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003354VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3355{
3356 .path = "set ip6 classify",
3357 .short_help =
3358 "set ip6 classify intfc <interface> table-index <classify-idx>",
3359 .function = set_ip6_classify_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003360};
Billy McFall0683c9c2016-10-13 08:27:31 -04003361/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003362
3363static clib_error_t *
3364ip6_config (vlib_main_t * vm, unformat_input_t * input)
3365{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003366 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003367 uword heapsize = 0;
3368 u32 tmp;
3369 u32 nbuckets = 0;
3370
Dave Barachd7cb1b52016-12-09 09:52:16 -05003371 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3372 {
3373 if (unformat (input, "hash-buckets %d", &tmp))
3374 nbuckets = tmp;
3375 else if (unformat (input, "heap-size %dm", &tmp))
3376 heapsize = ((u64) tmp) << 20;
3377 else if (unformat (input, "heap-size %dM", &tmp))
3378 heapsize = ((u64) tmp) << 20;
3379 else if (unformat (input, "heap-size %dg", &tmp))
3380 heapsize = ((u64) tmp) << 30;
3381 else if (unformat (input, "heap-size %dG", &tmp))
3382 heapsize = ((u64) tmp) << 30;
3383 else
3384 return clib_error_return (0, "unknown input '%U'",
3385 format_unformat_error, input);
3386 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003387
3388 im->lookup_table_nbuckets = nbuckets;
3389 im->lookup_table_size = heapsize;
3390
3391 return 0;
3392}
3393
3394VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003395
3396/*
3397 * fd.io coding-style-patch-verification: ON
3398 *
3399 * Local Variables:
3400 * eval: (c-set-style "gnu")
3401 * End:
3402 */