blob: cacdd71e3818f8fc6db1a71186d2ec84eca3be9f [file] [log] [blame]
Neale Rannscbe25aa2019-09-30 10:53:31 +00001/*
2 * ethernet/arp.c: IP v4 ARP node
3 *
4 * Copyright (c) 2010 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/arp/arp.h>
19#include <vnet/arp/arp_packet.h>
20
21#include <vnet/fib/ip4_fib.h>
22#include <vnet/fib/fib_entry_src.h>
23#include <vnet/adj/adj_nbr.h>
24#include <vnet/adj/adj_mcast.h>
Neale Ranns68d48d92021-06-03 14:59:47 +000025#include <vnet/pg/pg.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000026
27#include <vnet/ip-neighbor/ip_neighbor.h>
Neale Rannsfd2417b2021-07-16 14:00:16 +000028#include <vnet/ip-neighbor/ip4_neighbor.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000029#include <vnet/ip-neighbor/ip_neighbor_dp.h>
30
31#include <vlibmemory/api.h>
32
33/**
34 * @file
35 * @brief IPv4 ARP.
36 *
37 * This file contains code to manage the IPv4 ARP tables (IP Address
38 * to MAC Address lookup).
39 */
40
41/**
42 * @brief Per-interface ARP configuration and state
43 */
44typedef struct ethernet_arp_interface_t_
45{
46 /**
47 * Is ARP enabled on this interface
48 */
49 u32 enabled;
50} ethernet_arp_interface_t;
51
52typedef struct
53{
54 /* Hash tables mapping name to opcode. */
55 uword *opcode_by_name;
56
57 /** Per interface state */
58 ethernet_arp_interface_t *ethernet_arp_by_sw_if_index;
59
60 /* ARP feature arc index */
61 u8 feature_arc_index;
62} ethernet_arp_main_t;
63
64static ethernet_arp_main_t ethernet_arp_main;
65
66static const u8 vrrp_prefix[] = { 0x00, 0x00, 0x5E, 0x00, 0x01 };
67
68static uword
69unformat_ethernet_arp_opcode_host_byte_order (unformat_input_t * input,
70 va_list * args)
71{
72 int *result = va_arg (*args, int *);
73 ethernet_arp_main_t *am = &ethernet_arp_main;
74 int x, i;
75
76 /* Numeric opcode. */
77 if (unformat (input, "0x%x", &x) || unformat (input, "%d", &x))
78 {
79 if (x >= (1 << 16))
80 return 0;
81 *result = x;
82 return 1;
83 }
84
85 /* Named type. */
86 if (unformat_user (input, unformat_vlib_number_by_name,
87 am->opcode_by_name, &i))
88 {
89 *result = i;
90 return 1;
91 }
92
93 return 0;
94}
95
96static uword
97unformat_ethernet_arp_opcode_net_byte_order (unformat_input_t * input,
98 va_list * args)
99{
100 int *result = va_arg (*args, int *);
101 if (!unformat_user
102 (input, unformat_ethernet_arp_opcode_host_byte_order, result))
103 return 0;
104
105 *result = clib_host_to_net_u16 ((u16) * result);
106 return 1;
107}
108
109typedef struct
110{
111 u8 packet_data[64];
112} ethernet_arp_input_trace_t;
113
114static u8 *
115format_ethernet_arp_input_trace (u8 * s, va_list * va)
116{
117 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
118 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
119 ethernet_arp_input_trace_t *t = va_arg (*va, ethernet_arp_input_trace_t *);
120
121 s = format (s, "%U",
122 format_ethernet_arp_header,
123 t->packet_data, sizeof (t->packet_data));
124
125 return s;
126}
127
128static int
129arp_is_enabled (ethernet_arp_main_t * am, u32 sw_if_index)
130{
131 if (vec_len (am->ethernet_arp_by_sw_if_index) <= sw_if_index)
132 return 0;
133
134 return (am->ethernet_arp_by_sw_if_index[sw_if_index].enabled);
135}
136
137static void
138arp_enable (ethernet_arp_main_t * am, u32 sw_if_index)
139{
140 if (arp_is_enabled (am, sw_if_index))
141 return;
142
143 vec_validate (am->ethernet_arp_by_sw_if_index, sw_if_index);
144
145 am->ethernet_arp_by_sw_if_index[sw_if_index].enabled = 1;
146
147 vnet_feature_enable_disable ("arp", "arp-reply", sw_if_index, 1, NULL, 0);
148 vnet_feature_enable_disable ("arp", "arp-disabled", sw_if_index, 0, NULL,
149 0);
150}
151
152static void
153arp_disable (ethernet_arp_main_t * am, u32 sw_if_index)
154{
155 if (!arp_is_enabled (am, sw_if_index))
156 return;
157
158 vnet_feature_enable_disable ("arp", "arp-disabled", sw_if_index, 1, NULL,
159 0);
160 vnet_feature_enable_disable ("arp", "arp-reply", sw_if_index, 0, NULL, 0);
161
162 am->ethernet_arp_by_sw_if_index[sw_if_index].enabled = 0;
163}
164
165static int
166arp_unnumbered (vlib_buffer_t * p0,
167 u32 input_sw_if_index, u32 conn_sw_if_index)
168{
169 vnet_main_t *vnm = vnet_get_main ();
170 vnet_interface_main_t *vim = &vnm->interface_main;
171 vnet_sw_interface_t *si;
172
173 /* verify that the input interface is unnumbered to the connected.
174 * the connected interface is the interface on which the subnet is
175 * configured */
176 si = &vim->sw_interfaces[input_sw_if_index];
177
178 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED &&
179 (si->unnumbered_sw_if_index == conn_sw_if_index)))
180 {
181 /* the input interface is not unnumbered to the interface on which
182 * the sub-net is configured that covers the ARP request.
183 * So this is not the case for unnumbered.. */
184 return 0;
185 }
186
187 return !0;
188}
189
190always_inline u32
191arp_learn (u32 sw_if_index,
192 const ethernet_arp_ip4_over_ethernet_address_t * addr)
193{
194 ip_neighbor_learn_t l = {
Neale Rannsdc617b82020-08-20 08:22:56 +0000195 .ip = {
196 .ip.ip4 = addr->ip4,
197 .version = AF_IP4,
198 },
Neale Rannscbe25aa2019-09-30 10:53:31 +0000199 .mac = addr->mac,
200 .sw_if_index = sw_if_index,
201 };
202
203 ip_neighbor_learn_dp (&l);
204
Neale Ranns13a74ae2022-08-09 00:59:37 +0000205 return (ARP_ERROR_L3_SRC_ADDRESS_LEARNED);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000206}
207
208typedef enum arp_input_next_t_
209{
210 ARP_INPUT_NEXT_DROP,
211 ARP_INPUT_NEXT_DISABLED,
212 ARP_INPUT_N_NEXT,
213} arp_input_next_t;
214
215static uword
216arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
217{
218 u32 n_left_from, next_index, *from, *to_next, n_left_to_next;
219 ethernet_arp_main_t *am = &ethernet_arp_main;
220
221 from = vlib_frame_vector_args (frame);
222 n_left_from = frame->n_vectors;
223 next_index = node->cached_next_index;
224
225 if (node->flags & VLIB_NODE_FLAG_TRACE)
226 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
227 /* stride */ 1,
228 sizeof (ethernet_arp_input_trace_t));
229
230 while (n_left_from > 0)
231 {
232 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
233
234 while (n_left_from > 0 && n_left_to_next > 0)
235 {
236 const ethernet_arp_header_t *arp0;
237 arp_input_next_t next0;
238 vlib_buffer_t *p0;
239 u32 pi0, error0;
240
241 pi0 = to_next[0] = from[0];
242 from += 1;
243 to_next += 1;
244 n_left_from -= 1;
245 n_left_to_next -= 1;
246
247 p0 = vlib_get_buffer (vm, pi0);
248 arp0 = vlib_buffer_get_current (p0);
249
Neale Ranns13a74ae2022-08-09 00:59:37 +0000250 error0 = ARP_ERROR_REPLIES_SENT;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000251 next0 = ARP_INPUT_NEXT_DROP;
252
Neale Ranns13a74ae2022-08-09 00:59:37 +0000253 error0 = (arp0->l2_type != clib_net_to_host_u16 (
254 ETHERNET_ARP_HARDWARE_TYPE_ethernet) ?
255 ARP_ERROR_L2_TYPE_NOT_ETHERNET :
256 error0);
257 error0 = (arp0->l3_type != clib_net_to_host_u16 (ETHERNET_TYPE_IP4) ?
258 ARP_ERROR_L3_TYPE_NOT_IP4 :
259 error0);
260 error0 = (0 == arp0->ip4_over_ethernet[0].ip4.as_u32 ?
261 ARP_ERROR_L3_DST_ADDRESS_UNSET :
262 error0);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000263
Neale Ranns13a74ae2022-08-09 00:59:37 +0000264 if (ARP_ERROR_REPLIES_SENT == error0)
Neale Rannscbe25aa2019-09-30 10:53:31 +0000265 {
266 next0 = ARP_INPUT_NEXT_DISABLED;
267 vnet_feature_arc_start (am->feature_arc_index,
268 vnet_buffer (p0)->sw_if_index[VLIB_RX],
269 &next0, p0);
270 }
271 else
272 p0->error = node->errors[error0];
273
274 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
275 n_left_to_next, pi0, next0);
276 }
277
278 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
279 }
280
281 return frame->n_vectors;
282}
283
284typedef enum arp_disabled_next_t_
285{
286 ARP_DISABLED_NEXT_DROP,
287 ARP_DISABLED_N_NEXT,
288} arp_disabled_next_t;
289
Neale Rannscbe25aa2019-09-30 10:53:31 +0000290static uword
291arp_disabled (vlib_main_t * vm,
292 vlib_node_runtime_t * node, vlib_frame_t * frame)
293{
294 u32 n_left_from, next_index, *from, *to_next, n_left_to_next;
295
296 from = vlib_frame_vector_args (frame);
297 n_left_from = frame->n_vectors;
298 next_index = node->cached_next_index;
299
300 if (node->flags & VLIB_NODE_FLAG_TRACE)
301 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
302 /* stride */ 1,
303 sizeof (ethernet_arp_input_trace_t));
304
305 while (n_left_from > 0)
306 {
307 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
308
309 while (n_left_from > 0 && n_left_to_next > 0)
310 {
311 arp_disabled_next_t next0 = ARP_DISABLED_NEXT_DROP;
312 vlib_buffer_t *p0;
313 u32 pi0, error0;
314
315 next0 = ARP_DISABLED_NEXT_DROP;
Neale Ranns13a74ae2022-08-09 00:59:37 +0000316 error0 = ARP_ERROR_DISABLED;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000317
318 pi0 = to_next[0] = from[0];
319 from += 1;
320 to_next += 1;
321 n_left_from -= 1;
322 n_left_to_next -= 1;
323
324 p0 = vlib_get_buffer (vm, pi0);
325 p0->error = node->errors[error0];
326
327 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
328 n_left_to_next, pi0, next0);
329 }
330
331 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
332 }
333
334 return frame->n_vectors;
335}
336
337enum arp_dst_fib_type
338{
339 ARP_DST_FIB_NONE,
340 ARP_DST_FIB_ADJ,
341 ARP_DST_FIB_CONN
342};
343
344/*
345 * we're looking for FIB sources that indicate the destination
346 * is attached. There may be interposed DPO prior to the one
347 * we are looking for
348 */
349static enum arp_dst_fib_type
350arp_dst_fib_check (const fib_node_index_t fei, fib_entry_flag_t * flags)
351{
352 const fib_entry_t *entry = fib_entry_get (fei);
353 const fib_entry_src_t *entry_src;
354 fib_source_t src;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000355 FOR_EACH_SRC_ADDED(entry, entry_src, src,
356 ({
357 *flags = fib_entry_get_flags_for_source (fei, src);
358 if (fib_entry_is_sourced (fei, FIB_SOURCE_ADJ))
359 return ARP_DST_FIB_ADJ;
360 else if (FIB_ENTRY_FLAG_CONNECTED & *flags)
361 return ARP_DST_FIB_CONN;
362 }))
Neale Rannscbe25aa2019-09-30 10:53:31 +0000363
364 return ARP_DST_FIB_NONE;
365}
366
367static uword
368arp_reply (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
369{
370 vnet_main_t *vnm = vnet_get_main ();
371 u32 n_left_from, next_index, *from, *to_next;
372 u32 n_replies_sent = 0;
373
374 from = vlib_frame_vector_args (frame);
375 n_left_from = frame->n_vectors;
376 next_index = node->cached_next_index;
377
378 if (node->flags & VLIB_NODE_FLAG_TRACE)
379 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
380 /* stride */ 1,
381 sizeof (ethernet_arp_input_trace_t));
382
383 while (n_left_from > 0)
384 {
385 u32 n_left_to_next;
386
387 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
388
389 while (n_left_from > 0 && n_left_to_next > 0)
390 {
391 vlib_buffer_t *p0;
392 ethernet_arp_header_t *arp0;
393 ethernet_header_t *eth_rx;
394 const ip4_address_t *if_addr0;
395 u32 pi0, error0, next0, sw_if_index0, conn_sw_if_index0, fib_index0;
396 u8 dst_is_local0, is_vrrp_reply0;
397 fib_node_index_t dst_fei, src_fei;
398 const fib_prefix_t *pfx0;
399 fib_entry_flag_t src_flags, dst_flags;
400
401 pi0 = from[0];
402 to_next[0] = pi0;
403 from += 1;
404 to_next += 1;
405 n_left_from -= 1;
406 n_left_to_next -= 1;
407
408 p0 = vlib_get_buffer (vm, pi0);
409 arp0 = vlib_buffer_get_current (p0);
410 /* Fill in ethernet header. */
411 eth_rx = ethernet_buffer_get_header (p0);
412
413 next0 = ARP_REPLY_NEXT_DROP;
Neale Ranns13a74ae2022-08-09 00:59:37 +0000414 error0 = ARP_ERROR_REPLIES_SENT;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000415 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
416
417 /* Check that IP address is local and matches incoming interface. */
418 fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
419 if (~0 == fib_index0)
420 {
Neale Ranns13a74ae2022-08-09 00:59:37 +0000421 error0 = ARP_ERROR_INTERFACE_NO_TABLE;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000422 goto drop;
423
424 }
425
426 {
427 /*
428 * we're looking for FIB entries that indicate the source
429 * is attached. There may be more specific non-attached
430 * routes that match the source, but these do not influence
431 * whether we respond to an ARP request, i.e. they do not
432 * influence whether we are the correct way for the sender
433 * to reach us, they only affect how we reach the sender.
434 */
435 fib_entry_t *src_fib_entry;
436 const fib_prefix_t *pfx;
437 fib_entry_src_t *src;
438 fib_source_t source;
439 int attached;
440 int mask;
441
442 mask = 32;
443 attached = 0;
444
445 do
446 {
447 src_fei = ip4_fib_table_lookup (ip4_fib_get (fib_index0),
448 &arp0->
449 ip4_over_ethernet[0].ip4,
450 mask);
451 src_fib_entry = fib_entry_get (src_fei);
452
453 /*
454 * It's possible that the source that provides the
455 * flags we need, or the flags we must not have,
456 * is not the best source, so check then all.
457 */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000458 FOR_EACH_SRC_ADDED(src_fib_entry, src, source,
459 ({
460 src_flags = fib_entry_get_flags_for_source (src_fei, source);
461
462 /* Reject requests/replies with our local interface
463 address. */
464 if (FIB_ENTRY_FLAG_LOCAL & src_flags)
465 {
Neale Ranns13a74ae2022-08-09 00:59:37 +0000466 error0 = ARP_ERROR_L3_SRC_ADDRESS_IS_LOCAL;
467 /*
468 * When VPP has an interface whose address is also
469 * applied to a TAP interface on the host, then VPP's
470 * TAP interface will be unnumbered to the 'real'
471 * interface and do proxy ARP from the host.
472 * The curious aspect of this setup is that ARP requests
473 * from the host will come from the VPP's own address.
474 * So don't drop immediately here, instead go see if this
475 * is a proxy ARP case.
476 */
477 goto next_feature;
478 }
479 /* A Source must also be local to subnet of matching
480 * interface address. */
481 if ((FIB_ENTRY_FLAG_ATTACHED & src_flags) ||
482 (FIB_ENTRY_FLAG_CONNECTED & src_flags))
483 {
484 attached = 1;
485 break;
486 }
487 /*
488 * else
489 * The packet was sent from an address that is not
490 * connected nor attached i.e. it is not from an
491 * address that is covered by a link's sub-net,
492 * nor is it a already learned host resp.
493 */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000494 }));
Neale Rannscbe25aa2019-09-30 10:53:31 +0000495
496 /*
497 * shorter mask lookup for the next iteration.
498 */
499 pfx = fib_entry_get_prefix (src_fei);
500 mask = pfx->fp_len - 1;
501
502 /*
503 * continue until we hit the default route or we find
504 * the attached we are looking for. The most likely
505 * outcome is we find the attached with the first source
506 * on the first lookup.
507 */
508 }
509 while (!attached &&
510 !fib_entry_is_sourced (src_fei, FIB_SOURCE_DEFAULT_ROUTE));
511
512 if (!attached)
513 {
514 /*
515 * the matching route is a not attached, i.e. it was
516 * added as a result of routing, rather than interface/ARP
517 * configuration. If the matching route is not a host route
518 * (i.e. a /32)
519 */
Neale Ranns13a74ae2022-08-09 00:59:37 +0000520 error0 = ARP_ERROR_L3_SRC_ADDRESS_NOT_LOCAL;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000521 goto drop;
522 }
523 }
524
525 dst_fei = ip4_fib_table_lookup (ip4_fib_get (fib_index0),
526 &arp0->ip4_over_ethernet[1].ip4,
527 32);
Neale Rannscc4f7e12020-10-26 10:44:54 +0000528 conn_sw_if_index0 = fib_entry_get_any_resolving_interface (dst_fei);
Neale Ranns22eefd72020-09-23 11:25:21 +0000529
Neale Rannscbe25aa2019-09-30 10:53:31 +0000530 switch (arp_dst_fib_check (dst_fei, &dst_flags))
531 {
532 case ARP_DST_FIB_ADJ:
533 /*
534 * We matched an adj-fib on ths source subnet (a /32 previously
535 * added as a result of ARP). If this request is a gratuitous
536 * ARP, then learn from it.
537 * The check for matching an adj-fib, is to prevent hosts
538 * from spamming us with gratuitous ARPS that might otherwise
539 * blow our ARP cache
540 */
Neale Ranns22eefd72020-09-23 11:25:21 +0000541 if (conn_sw_if_index0 != sw_if_index0)
Neale Ranns13a74ae2022-08-09 00:59:37 +0000542 error0 = ARP_ERROR_L3_DST_ADDRESS_NOT_LOCAL;
Neale Ranns22eefd72020-09-23 11:25:21 +0000543 else if (arp0->ip4_over_ethernet[0].ip4.as_u32 ==
544 arp0->ip4_over_ethernet[1].ip4.as_u32)
Neale Rannsfd2417b2021-07-16 14:00:16 +0000545 {
546 vlib_increment_simple_counter (
547 &ip_neighbor_counters[AF_IP4]
548 .ipnc[VLIB_RX][IP_NEIGHBOR_CTR_GRAT],
549 vm->thread_index, sw_if_index0, 1);
550 error0 =
551 arp_learn (sw_if_index0, &arp0->ip4_over_ethernet[0]);
552 }
BenoƮt Ganne6b4c6e12022-09-12 15:06:22 +0200553 goto next_feature;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000554 case ARP_DST_FIB_CONN:
555 /* destination is connected, continue to process */
556 break;
557 case ARP_DST_FIB_NONE:
558 /* destination is not connected, stop here */
Neale Ranns13a74ae2022-08-09 00:59:37 +0000559 error0 = ARP_ERROR_L3_DST_ADDRESS_NOT_LOCAL;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000560 goto next_feature;
561 }
562
563 dst_is_local0 = (FIB_ENTRY_FLAG_LOCAL & dst_flags);
564 pfx0 = fib_entry_get_prefix (dst_fei);
565 if_addr0 = &pfx0->fp_addr.ip4;
566
567 is_vrrp_reply0 =
568 ((arp0->opcode ==
569 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply))
570 &&
571 (!memcmp
572 (arp0->ip4_over_ethernet[0].mac.bytes, vrrp_prefix,
573 sizeof (vrrp_prefix))));
574
575 /* Trash ARP packets whose ARP-level source addresses do not
576 match their L2-frame-level source addresses, unless it's
577 a reply from a VRRP virtual router */
578 if (!ethernet_mac_address_equal
579 (eth_rx->src_address,
580 arp0->ip4_over_ethernet[0].mac.bytes) && !is_vrrp_reply0)
581 {
Neale Ranns13a74ae2022-08-09 00:59:37 +0000582 error0 = ARP_ERROR_L2_ADDRESS_MISMATCH;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000583 goto drop;
584 }
585
Neale Rannsfd2417b2021-07-16 14:00:16 +0000586 vlib_increment_simple_counter (
587 &ip_neighbor_counters[AF_IP4]
588 .ipnc[VLIB_RX][arp0->opcode == clib_host_to_net_u16 (
589 ETHERNET_ARP_OPCODE_reply) ?
590 IP_NEIGHBOR_CTR_REPLY :
591 IP_NEIGHBOR_CTR_REQUEST],
592 vm->thread_index, sw_if_index0, 1);
593
Neale Rannscbe25aa2019-09-30 10:53:31 +0000594 /* Learn or update sender's mapping only for replies to addresses
595 * that are local to the subnet */
596 if (arp0->opcode ==
597 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply))
598 {
599 if (dst_is_local0)
600 error0 =
601 arp_learn (sw_if_index0, &arp0->ip4_over_ethernet[0]);
602 else
603 /* a reply for a non-local destination could be a GARP.
604 * GARPs for hosts we know were handled above, so this one
605 * we drop */
Neale Ranns13a74ae2022-08-09 00:59:37 +0000606 error0 = ARP_ERROR_L3_DST_ADDRESS_NOT_LOCAL;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000607
608 goto next_feature;
609 }
610 else if (arp0->opcode ==
611 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request) &&
612 (dst_is_local0 == 0))
613 {
614 goto next_feature;
615 }
616
617 /* Honor unnumbered interface, if any */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000618 if (sw_if_index0 != conn_sw_if_index0 ||
619 sw_if_index0 != fib_entry_get_resolving_interface (src_fei))
620 {
621 /*
622 * The interface the ARP is sent to or was received on is not the
623 * interface on which the covering prefix is configured.
624 * Maybe this is a case for unnumbered.
625 */
626 if (!arp_unnumbered (p0, sw_if_index0, conn_sw_if_index0))
627 {
Neale Ranns13a74ae2022-08-09 00:59:37 +0000628 error0 = ARP_ERROR_UNNUMBERED_MISMATCH;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000629 goto drop;
630 }
631 }
632 if (arp0->ip4_over_ethernet[0].ip4.as_u32 ==
633 arp0->ip4_over_ethernet[1].ip4.as_u32)
634 {
Neale Ranns13a74ae2022-08-09 00:59:37 +0000635 error0 = ARP_ERROR_GRATUITOUS_ARP;
Neale Rannscbe25aa2019-09-30 10:53:31 +0000636 goto drop;
637 }
638
639 next0 = arp_mk_reply (vnm, p0, sw_if_index0,
640 if_addr0, arp0, eth_rx);
641
642 /* We are going to reply to this request, so, in the absence of
643 errors, learn the sender */
644 if (!error0)
645 error0 = arp_learn (sw_if_index0, &arp0->ip4_over_ethernet[1]);
646
Neale Rannsfd2417b2021-07-16 14:00:16 +0000647 vlib_increment_simple_counter (
648 &ip_neighbor_counters[AF_IP4].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_REPLY],
649 vm->thread_index, sw_if_index0, 1);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000650 n_replies_sent += 1;
651 goto enqueue;
652
653 next_feature:
654 vnet_feature_next (&next0, p0);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000655
656 drop:
657 p0->error = node->errors[error0];
658
659 enqueue:
660 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
661 n_left_to_next, pi0, next0);
662 }
663
664 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
665 }
666
Neale Ranns13a74ae2022-08-09 00:59:37 +0000667 vlib_error_count (vm, node->node_index, ARP_ERROR_REPLIES_SENT,
668 n_replies_sent);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000669
670 return frame->n_vectors;
671}
672
673
Neale Rannscbe25aa2019-09-30 10:53:31 +0000674
675VLIB_REGISTER_NODE (arp_input_node, static) =
676{
677 .function = arp_input,
678 .name = "arp-input",
679 .vector_size = sizeof (u32),
Neale Ranns13a74ae2022-08-09 00:59:37 +0000680 .n_errors = ARP_N_ERROR,
681 .error_counters = arp_error_counters,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000682 .n_next_nodes = ARP_INPUT_N_NEXT,
683 .next_nodes = {
684 [ARP_INPUT_NEXT_DROP] = "error-drop",
685 [ARP_INPUT_NEXT_DISABLED] = "arp-disabled",
686 },
687 .format_buffer = format_ethernet_arp_header,
688 .format_trace = format_ethernet_arp_input_trace,
689};
690
691VLIB_REGISTER_NODE (arp_disabled_node, static) =
692{
693 .function = arp_disabled,
694 .name = "arp-disabled",
695 .vector_size = sizeof (u32),
Neale Ranns13a74ae2022-08-09 00:59:37 +0000696 .n_errors = ARP_N_ERROR,
697 .error_counters = arp_error_counters,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000698 .n_next_nodes = ARP_DISABLED_N_NEXT,
699 .next_nodes = {
700 [ARP_INPUT_NEXT_DROP] = "error-drop",
701 },
702 .format_buffer = format_ethernet_arp_header,
703 .format_trace = format_ethernet_arp_input_trace,
704};
705
706VLIB_REGISTER_NODE (arp_reply_node, static) =
707{
708 .function = arp_reply,
709 .name = "arp-reply",
710 .vector_size = sizeof (u32),
Neale Ranns13a74ae2022-08-09 00:59:37 +0000711 .n_errors = ARP_N_ERROR,
712 .error_counters = arp_error_counters,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000713 .n_next_nodes = ARP_REPLY_N_NEXT,
714 .next_nodes = {
715 [ARP_REPLY_NEXT_DROP] = "error-drop",
716 [ARP_REPLY_NEXT_REPLY_TX] = "interface-output",
717 },
718 .format_buffer = format_ethernet_arp_header,
719 .format_trace = format_ethernet_arp_input_trace,
720};
721
722/* Built-in ARP rx feature path definition */
723VNET_FEATURE_ARC_INIT (arp_feat, static) =
724{
725 .arc_name = "arp",
726 .start_nodes = VNET_FEATURES ("arp-input"),
727 .last_in_arc = "error-drop",
728 .arc_index_ptr = &ethernet_arp_main.feature_arc_index,
729};
730
731VNET_FEATURE_INIT (arp_reply_feat_node, static) =
732{
733 .arc_name = "arp",
734 .node_name = "arp-reply",
735 .runs_before = VNET_FEATURES ("arp-disabled"),
736};
737
738VNET_FEATURE_INIT (arp_proxy_feat_node, static) =
739{
740 .arc_name = "arp",
741 .node_name = "arp-proxy",
742 .runs_after = VNET_FEATURES ("arp-reply"),
743 .runs_before = VNET_FEATURES ("arp-disabled"),
744};
745
746VNET_FEATURE_INIT (arp_disabled_feat_node, static) =
747{
748 .arc_name = "arp",
749 .node_name = "arp-disabled",
750 .runs_before = VNET_FEATURES ("error-drop"),
751};
752
753VNET_FEATURE_INIT (arp_drop_feat_node, static) =
754{
755 .arc_name = "arp",
756 .node_name = "error-drop",
757 .runs_before = 0, /* last feature */
758};
759
Neale Rannscbe25aa2019-09-30 10:53:31 +0000760
761typedef struct
762{
763 pg_edit_t l2_type, l3_type;
764 pg_edit_t n_l2_address_bytes, n_l3_address_bytes;
765 pg_edit_t opcode;
766 struct
767 {
768 pg_edit_t mac;
769 pg_edit_t ip4;
770 } ip4_over_ethernet[2];
771} pg_ethernet_arp_header_t;
772
773static inline void
774pg_ethernet_arp_header_init (pg_ethernet_arp_header_t * p)
775{
776 /* Initialize fields that are not bit fields in the IP header. */
777#define _(f) pg_edit_init (&p->f, ethernet_arp_header_t, f);
778 _(l2_type);
779 _(l3_type);
780 _(n_l2_address_bytes);
781 _(n_l3_address_bytes);
782 _(opcode);
783 _(ip4_over_ethernet[0].mac);
784 _(ip4_over_ethernet[0].ip4);
785 _(ip4_over_ethernet[1].mac);
786 _(ip4_over_ethernet[1].ip4);
787#undef _
788}
789
790uword
791unformat_pg_arp_header (unformat_input_t * input, va_list * args)
792{
793 pg_stream_t *s = va_arg (*args, pg_stream_t *);
794 pg_ethernet_arp_header_t *p;
795 u32 group_index;
796
797 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ethernet_arp_header_t),
798 &group_index);
799 pg_ethernet_arp_header_init (p);
800
801 /* Defaults. */
802 pg_edit_set_fixed (&p->l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet);
803 pg_edit_set_fixed (&p->l3_type, ETHERNET_TYPE_IP4);
804 pg_edit_set_fixed (&p->n_l2_address_bytes, 6);
805 pg_edit_set_fixed (&p->n_l3_address_bytes, 4);
806
807 if (!unformat (input, "%U: %U/%U -> %U/%U",
808 unformat_pg_edit,
809 unformat_ethernet_arp_opcode_net_byte_order, &p->opcode,
810 unformat_pg_edit,
811 unformat_mac_address_t, &p->ip4_over_ethernet[0].mac,
812 unformat_pg_edit,
813 unformat_ip4_address, &p->ip4_over_ethernet[0].ip4,
814 unformat_pg_edit,
815 unformat_mac_address_t, &p->ip4_over_ethernet[1].mac,
816 unformat_pg_edit,
817 unformat_ip4_address, &p->ip4_over_ethernet[1].ip4))
818 {
819 /* Free up any edits we may have added. */
820 pg_free_edit_group (s);
821 return 0;
822 }
823 return 1;
824}
825
826/*
827 * callback when an interface address is added or deleted
828 */
829static void
830arp_enable_disable_interface (ip4_main_t * im,
831 uword opaque, u32 sw_if_index, u32 is_enable)
832{
833 ethernet_arp_main_t *am = &ethernet_arp_main;
834
835 if (is_enable)
836 arp_enable (am, sw_if_index);
837 else
838 arp_disable (am, sw_if_index);
839}
840
841/*
842 * Remove any arp entries associated with the specified interface
843 */
844static clib_error_t *
845vnet_arp_add_del_sw_interface (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
846{
847 ethernet_arp_main_t *am = &ethernet_arp_main;
BenoƮt Ganne6178bda2020-11-04 10:02:03 +0100848 if (is_add)
849 arp_disable (am, sw_if_index);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000850 return (NULL);
851}
852
853VNET_SW_INTERFACE_ADD_DEL_FUNCTION (vnet_arp_add_del_sw_interface);
854
855const static ip_neighbor_vft_t arp_vft = {
856 .inv_proxy4_add = arp_proxy_add,
857 .inv_proxy4_del = arp_proxy_del,
Mercuryd0892062021-12-12 08:45:38 +0800858 .inv_proxy4_enable = arp_proxy_enable,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000859 .inv_proxy4_disable = arp_proxy_disable,
860};
861
862static clib_error_t *
863ethernet_arp_init (vlib_main_t * vm)
864{
865 ethernet_arp_main_t *am = &ethernet_arp_main;
866 ip4_main_t *im = &ip4_main;
867 pg_node_t *pn;
868
869 ethernet_register_input_type (vm, ETHERNET_TYPE_ARP, arp_input_node.index);
870
871 pn = pg_get_node (arp_input_node.index);
872 pn->unformat_edit = unformat_pg_arp_header;
873
874 am->opcode_by_name = hash_create_string (0, sizeof (uword));
875#define _(o) hash_set_mem (am->opcode_by_name, #o, ETHERNET_ARP_OPCODE_##o);
876 foreach_ethernet_arp_opcode;
877#undef _
878
879 /* don't trace ARP error packets */
880 {
881 vlib_node_runtime_t *rt =
882 vlib_node_get_runtime (vm, arp_input_node.index);
883
Neale Ranns13a74ae2022-08-09 00:59:37 +0000884 vnet_pcap_drop_trace_filter_add_del (rt->errors[ARP_ERROR_REPLIES_SENT],
885 1);
886 vnet_pcap_drop_trace_filter_add_del (rt->errors[ARP_ERROR_DISABLED], 1);
887 vnet_pcap_drop_trace_filter_add_del (
888 rt->errors[ARP_ERROR_L2_TYPE_NOT_ETHERNET], 1);
889 vnet_pcap_drop_trace_filter_add_del (rt->errors[ARP_ERROR_L3_TYPE_NOT_IP4],
890 1);
891 vnet_pcap_drop_trace_filter_add_del (
892 rt->errors[ARP_ERROR_L3_SRC_ADDRESS_NOT_LOCAL], 1);
893 vnet_pcap_drop_trace_filter_add_del (
894 rt->errors[ARP_ERROR_L3_DST_ADDRESS_NOT_LOCAL], 1);
895 vnet_pcap_drop_trace_filter_add_del (
896 rt->errors[ARP_ERROR_L3_DST_ADDRESS_UNSET], 1);
897 vnet_pcap_drop_trace_filter_add_del (
898 rt->errors[ARP_ERROR_L3_SRC_ADDRESS_IS_LOCAL], 1);
899 vnet_pcap_drop_trace_filter_add_del (
900 rt->errors[ARP_ERROR_L3_SRC_ADDRESS_LEARNED], 1);
901 vnet_pcap_drop_trace_filter_add_del (
902 rt->errors[ARP_ERROR_REPLIES_RECEIVED], 1);
903 vnet_pcap_drop_trace_filter_add_del (
904 rt->errors[ARP_ERROR_OPCODE_NOT_REQUEST], 1);
905 vnet_pcap_drop_trace_filter_add_del (
906 rt->errors[ARP_ERROR_PROXY_ARP_REPLIES_SENT], 1);
907 vnet_pcap_drop_trace_filter_add_del (
908 rt->errors[ARP_ERROR_L2_ADDRESS_MISMATCH], 1);
909 vnet_pcap_drop_trace_filter_add_del (rt->errors[ARP_ERROR_GRATUITOUS_ARP],
910 1);
911 vnet_pcap_drop_trace_filter_add_del (
912 rt->errors[ARP_ERROR_INTERFACE_NO_TABLE], 1);
913 vnet_pcap_drop_trace_filter_add_del (
914 rt->errors[ARP_ERROR_INTERFACE_NOT_IP_ENABLED], 1);
915 vnet_pcap_drop_trace_filter_add_del (
916 rt->errors[ARP_ERROR_UNNUMBERED_MISMATCH], 1);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000917 }
918
919 {
920 ip4_enable_disable_interface_callback_t cb = {
921 .function = arp_enable_disable_interface,
922 };
923 vec_add1 (im->enable_disable_interface_callbacks, cb);
924 }
925
Neale Rannsdc617b82020-08-20 08:22:56 +0000926 ip_neighbor_register (AF_IP4, &arp_vft);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000927
928 return 0;
929}
930
Neale Rannscbe25aa2019-09-30 10:53:31 +0000931VLIB_INIT_FUNCTION (ethernet_arp_init) =
932{
933 .runs_after = VLIB_INITS("ethernet_init",
934 "ip_neighbor_init"),
935};
Neale Rannscbe25aa2019-09-30 10:53:31 +0000936
937/*
938 * fd.io coding-style-patch-verification: ON
939 *
940 * Local Variables:
941 * eval: (c-set-style "gnu")
942 * End:
943 */