blob: 5765101701d55b754b5dd1dceff99ca7437d3f2c [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{
Neale Rannsdc617b82020-08-20 08:22:56 +0000194 /* *INDENT-OFF* */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000195 ip_neighbor_learn_t l = {
Neale Rannsdc617b82020-08-20 08:22:56 +0000196 .ip = {
197 .ip.ip4 = addr->ip4,
198 .version = AF_IP4,
199 },
Neale Rannscbe25aa2019-09-30 10:53:31 +0000200 .mac = addr->mac,
201 .sw_if_index = sw_if_index,
202 };
Neale Rannsdc617b82020-08-20 08:22:56 +0000203 /* *INDENT-ON* */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000204
205 ip_neighbor_learn_dp (&l);
206
207 return (ETHERNET_ARP_ERROR_l3_src_address_learned);
208}
209
210typedef enum arp_input_next_t_
211{
212 ARP_INPUT_NEXT_DROP,
213 ARP_INPUT_NEXT_DISABLED,
214 ARP_INPUT_N_NEXT,
215} arp_input_next_t;
216
217static uword
218arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
219{
220 u32 n_left_from, next_index, *from, *to_next, n_left_to_next;
221 ethernet_arp_main_t *am = &ethernet_arp_main;
222
223 from = vlib_frame_vector_args (frame);
224 n_left_from = frame->n_vectors;
225 next_index = node->cached_next_index;
226
227 if (node->flags & VLIB_NODE_FLAG_TRACE)
228 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
229 /* stride */ 1,
230 sizeof (ethernet_arp_input_trace_t));
231
232 while (n_left_from > 0)
233 {
234 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
235
236 while (n_left_from > 0 && n_left_to_next > 0)
237 {
238 const ethernet_arp_header_t *arp0;
239 arp_input_next_t next0;
240 vlib_buffer_t *p0;
241 u32 pi0, error0;
242
243 pi0 = to_next[0] = from[0];
244 from += 1;
245 to_next += 1;
246 n_left_from -= 1;
247 n_left_to_next -= 1;
248
249 p0 = vlib_get_buffer (vm, pi0);
250 arp0 = vlib_buffer_get_current (p0);
251
252 error0 = ETHERNET_ARP_ERROR_replies_sent;
253 next0 = ARP_INPUT_NEXT_DROP;
254
255 error0 =
256 (arp0->l2_type !=
257 clib_net_to_host_u16 (ETHERNET_ARP_HARDWARE_TYPE_ethernet) ?
258 ETHERNET_ARP_ERROR_l2_type_not_ethernet : error0);
259 error0 =
260 (arp0->l3_type !=
261 clib_net_to_host_u16 (ETHERNET_TYPE_IP4) ?
262 ETHERNET_ARP_ERROR_l3_type_not_ip4 : error0);
263 error0 =
264 (0 == arp0->ip4_over_ethernet[0].ip4.as_u32 ?
265 ETHERNET_ARP_ERROR_l3_dst_address_unset : error0);
266
267 if (ETHERNET_ARP_ERROR_replies_sent == error0)
268 {
269 next0 = ARP_INPUT_NEXT_DISABLED;
270 vnet_feature_arc_start (am->feature_arc_index,
271 vnet_buffer (p0)->sw_if_index[VLIB_RX],
272 &next0, p0);
273 }
274 else
275 p0->error = node->errors[error0];
276
277 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
278 n_left_to_next, pi0, next0);
279 }
280
281 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
282 }
283
284 return frame->n_vectors;
285}
286
287typedef enum arp_disabled_next_t_
288{
289 ARP_DISABLED_NEXT_DROP,
290 ARP_DISABLED_N_NEXT,
291} arp_disabled_next_t;
292
293#define foreach_arp_disabled_error \
294 _ (DISABLED, "ARP Disabled on this interface") \
295
296typedef enum
297{
298#define _(sym,string) ARP_DISABLED_ERROR_##sym,
299 foreach_arp_disabled_error
300#undef _
301 ARP_DISABLED_N_ERROR,
302} arp_disabled_error_t;
303
304static char *arp_disabled_error_strings[] = {
305#define _(sym,string) string,
306 foreach_arp_disabled_error
307#undef _
308};
309
310static uword
311arp_disabled (vlib_main_t * vm,
312 vlib_node_runtime_t * node, vlib_frame_t * frame)
313{
314 u32 n_left_from, next_index, *from, *to_next, n_left_to_next;
315
316 from = vlib_frame_vector_args (frame);
317 n_left_from = frame->n_vectors;
318 next_index = node->cached_next_index;
319
320 if (node->flags & VLIB_NODE_FLAG_TRACE)
321 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
322 /* stride */ 1,
323 sizeof (ethernet_arp_input_trace_t));
324
325 while (n_left_from > 0)
326 {
327 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
328
329 while (n_left_from > 0 && n_left_to_next > 0)
330 {
331 arp_disabled_next_t next0 = ARP_DISABLED_NEXT_DROP;
332 vlib_buffer_t *p0;
333 u32 pi0, error0;
334
335 next0 = ARP_DISABLED_NEXT_DROP;
336 error0 = ARP_DISABLED_ERROR_DISABLED;
337
338 pi0 = to_next[0] = from[0];
339 from += 1;
340 to_next += 1;
341 n_left_from -= 1;
342 n_left_to_next -= 1;
343
344 p0 = vlib_get_buffer (vm, pi0);
345 p0->error = node->errors[error0];
346
347 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
348 n_left_to_next, pi0, next0);
349 }
350
351 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
352 }
353
354 return frame->n_vectors;
355}
356
357enum arp_dst_fib_type
358{
359 ARP_DST_FIB_NONE,
360 ARP_DST_FIB_ADJ,
361 ARP_DST_FIB_CONN
362};
363
364/*
365 * we're looking for FIB sources that indicate the destination
366 * is attached. There may be interposed DPO prior to the one
367 * we are looking for
368 */
369static enum arp_dst_fib_type
370arp_dst_fib_check (const fib_node_index_t fei, fib_entry_flag_t * flags)
371{
372 const fib_entry_t *entry = fib_entry_get (fei);
373 const fib_entry_src_t *entry_src;
374 fib_source_t src;
375 /* *INDENT-OFF* */
376 FOR_EACH_SRC_ADDED(entry, entry_src, src,
377 ({
378 *flags = fib_entry_get_flags_for_source (fei, src);
379 if (fib_entry_is_sourced (fei, FIB_SOURCE_ADJ))
380 return ARP_DST_FIB_ADJ;
381 else if (FIB_ENTRY_FLAG_CONNECTED & *flags)
382 return ARP_DST_FIB_CONN;
383 }))
384 /* *INDENT-ON* */
385
386 return ARP_DST_FIB_NONE;
387}
388
389static uword
390arp_reply (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
391{
392 vnet_main_t *vnm = vnet_get_main ();
393 u32 n_left_from, next_index, *from, *to_next;
394 u32 n_replies_sent = 0;
395
396 from = vlib_frame_vector_args (frame);
397 n_left_from = frame->n_vectors;
398 next_index = node->cached_next_index;
399
400 if (node->flags & VLIB_NODE_FLAG_TRACE)
401 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
402 /* stride */ 1,
403 sizeof (ethernet_arp_input_trace_t));
404
405 while (n_left_from > 0)
406 {
407 u32 n_left_to_next;
408
409 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
410
411 while (n_left_from > 0 && n_left_to_next > 0)
412 {
413 vlib_buffer_t *p0;
414 ethernet_arp_header_t *arp0;
415 ethernet_header_t *eth_rx;
416 const ip4_address_t *if_addr0;
417 u32 pi0, error0, next0, sw_if_index0, conn_sw_if_index0, fib_index0;
418 u8 dst_is_local0, is_vrrp_reply0;
419 fib_node_index_t dst_fei, src_fei;
420 const fib_prefix_t *pfx0;
421 fib_entry_flag_t src_flags, dst_flags;
422
423 pi0 = from[0];
424 to_next[0] = pi0;
425 from += 1;
426 to_next += 1;
427 n_left_from -= 1;
428 n_left_to_next -= 1;
429
430 p0 = vlib_get_buffer (vm, pi0);
431 arp0 = vlib_buffer_get_current (p0);
432 /* Fill in ethernet header. */
433 eth_rx = ethernet_buffer_get_header (p0);
434
435 next0 = ARP_REPLY_NEXT_DROP;
436 error0 = ETHERNET_ARP_ERROR_replies_sent;
437 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
438
439 /* Check that IP address is local and matches incoming interface. */
440 fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
441 if (~0 == fib_index0)
442 {
443 error0 = ETHERNET_ARP_ERROR_interface_no_table;
444 goto drop;
445
446 }
447
448 {
449 /*
450 * we're looking for FIB entries that indicate the source
451 * is attached. There may be more specific non-attached
452 * routes that match the source, but these do not influence
453 * whether we respond to an ARP request, i.e. they do not
454 * influence whether we are the correct way for the sender
455 * to reach us, they only affect how we reach the sender.
456 */
457 fib_entry_t *src_fib_entry;
458 const fib_prefix_t *pfx;
459 fib_entry_src_t *src;
460 fib_source_t source;
461 int attached;
462 int mask;
463
464 mask = 32;
465 attached = 0;
466
467 do
468 {
469 src_fei = ip4_fib_table_lookup (ip4_fib_get (fib_index0),
470 &arp0->
471 ip4_over_ethernet[0].ip4,
472 mask);
473 src_fib_entry = fib_entry_get (src_fei);
474
475 /*
476 * It's possible that the source that provides the
477 * flags we need, or the flags we must not have,
478 * is not the best source, so check then all.
479 */
480 /* *INDENT-OFF* */
481 FOR_EACH_SRC_ADDED(src_fib_entry, src, source,
482 ({
483 src_flags = fib_entry_get_flags_for_source (src_fei, source);
484
485 /* Reject requests/replies with our local interface
486 address. */
487 if (FIB_ENTRY_FLAG_LOCAL & src_flags)
488 {
489 error0 = ETHERNET_ARP_ERROR_l3_src_address_is_local;
490 /*
491 * When VPP has an interface whose address is also
492 * applied to a TAP interface on the host, then VPP's
493 * TAP interface will be unnumbered to the 'real'
494 * interface and do proxy ARP from the host.
495 * The curious aspect of this setup is that ARP requests
496 * from the host will come from the VPP's own address.
497 * So don't drop immediately here, instead go see if this
498 * is a proxy ARP case.
499 */
500 goto next_feature;
501 }
502 /* A Source must also be local to subnet of matching
503 * interface address. */
504 if ((FIB_ENTRY_FLAG_ATTACHED & src_flags) ||
505 (FIB_ENTRY_FLAG_CONNECTED & src_flags))
506 {
507 attached = 1;
508 break;
509 }
510 /*
511 * else
512 * The packet was sent from an address that is not
513 * connected nor attached i.e. it is not from an
514 * address that is covered by a link's sub-net,
515 * nor is it a already learned host resp.
516 */
517 }));
518 /* *INDENT-ON* */
519
520 /*
521 * shorter mask lookup for the next iteration.
522 */
523 pfx = fib_entry_get_prefix (src_fei);
524 mask = pfx->fp_len - 1;
525
526 /*
527 * continue until we hit the default route or we find
528 * the attached we are looking for. The most likely
529 * outcome is we find the attached with the first source
530 * on the first lookup.
531 */
532 }
533 while (!attached &&
534 !fib_entry_is_sourced (src_fei, FIB_SOURCE_DEFAULT_ROUTE));
535
536 if (!attached)
537 {
538 /*
539 * the matching route is a not attached, i.e. it was
540 * added as a result of routing, rather than interface/ARP
541 * configuration. If the matching route is not a host route
542 * (i.e. a /32)
543 */
544 error0 = ETHERNET_ARP_ERROR_l3_src_address_not_local;
545 goto drop;
546 }
547 }
548
549 dst_fei = ip4_fib_table_lookup (ip4_fib_get (fib_index0),
550 &arp0->ip4_over_ethernet[1].ip4,
551 32);
Neale Rannscc4f7e12020-10-26 10:44:54 +0000552 conn_sw_if_index0 = fib_entry_get_any_resolving_interface (dst_fei);
Neale Ranns22eefd72020-09-23 11:25:21 +0000553
Neale Rannscbe25aa2019-09-30 10:53:31 +0000554 switch (arp_dst_fib_check (dst_fei, &dst_flags))
555 {
556 case ARP_DST_FIB_ADJ:
557 /*
558 * We matched an adj-fib on ths source subnet (a /32 previously
559 * added as a result of ARP). If this request is a gratuitous
560 * ARP, then learn from it.
561 * The check for matching an adj-fib, is to prevent hosts
562 * from spamming us with gratuitous ARPS that might otherwise
563 * blow our ARP cache
564 */
Neale Ranns22eefd72020-09-23 11:25:21 +0000565 if (conn_sw_if_index0 != sw_if_index0)
566 error0 = ETHERNET_ARP_ERROR_l3_dst_address_not_local;
567 else if (arp0->ip4_over_ethernet[0].ip4.as_u32 ==
568 arp0->ip4_over_ethernet[1].ip4.as_u32)
Neale Rannsfd2417b2021-07-16 14:00:16 +0000569 {
570 vlib_increment_simple_counter (
571 &ip_neighbor_counters[AF_IP4]
572 .ipnc[VLIB_RX][IP_NEIGHBOR_CTR_GRAT],
573 vm->thread_index, sw_if_index0, 1);
574 error0 =
575 arp_learn (sw_if_index0, &arp0->ip4_over_ethernet[0]);
576 }
Neale Rannscbe25aa2019-09-30 10:53:31 +0000577 goto drop;
578 case ARP_DST_FIB_CONN:
579 /* destination is connected, continue to process */
580 break;
581 case ARP_DST_FIB_NONE:
582 /* destination is not connected, stop here */
583 error0 = ETHERNET_ARP_ERROR_l3_dst_address_not_local;
584 goto next_feature;
585 }
586
587 dst_is_local0 = (FIB_ENTRY_FLAG_LOCAL & dst_flags);
588 pfx0 = fib_entry_get_prefix (dst_fei);
589 if_addr0 = &pfx0->fp_addr.ip4;
590
591 is_vrrp_reply0 =
592 ((arp0->opcode ==
593 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply))
594 &&
595 (!memcmp
596 (arp0->ip4_over_ethernet[0].mac.bytes, vrrp_prefix,
597 sizeof (vrrp_prefix))));
598
599 /* Trash ARP packets whose ARP-level source addresses do not
600 match their L2-frame-level source addresses, unless it's
601 a reply from a VRRP virtual router */
602 if (!ethernet_mac_address_equal
603 (eth_rx->src_address,
604 arp0->ip4_over_ethernet[0].mac.bytes) && !is_vrrp_reply0)
605 {
606 error0 = ETHERNET_ARP_ERROR_l2_address_mismatch;
607 goto drop;
608 }
609
Neale Rannsfd2417b2021-07-16 14:00:16 +0000610 vlib_increment_simple_counter (
611 &ip_neighbor_counters[AF_IP4]
612 .ipnc[VLIB_RX][arp0->opcode == clib_host_to_net_u16 (
613 ETHERNET_ARP_OPCODE_reply) ?
614 IP_NEIGHBOR_CTR_REPLY :
615 IP_NEIGHBOR_CTR_REQUEST],
616 vm->thread_index, sw_if_index0, 1);
617
Neale Rannscbe25aa2019-09-30 10:53:31 +0000618 /* Learn or update sender's mapping only for replies to addresses
619 * that are local to the subnet */
620 if (arp0->opcode ==
621 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply))
622 {
623 if (dst_is_local0)
624 error0 =
625 arp_learn (sw_if_index0, &arp0->ip4_over_ethernet[0]);
626 else
627 /* a reply for a non-local destination could be a GARP.
628 * GARPs for hosts we know were handled above, so this one
629 * we drop */
630 error0 = ETHERNET_ARP_ERROR_l3_dst_address_not_local;
631
632 goto next_feature;
633 }
634 else if (arp0->opcode ==
635 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request) &&
636 (dst_is_local0 == 0))
637 {
638 goto next_feature;
639 }
640
641 /* Honor unnumbered interface, if any */
Neale Rannscbe25aa2019-09-30 10:53:31 +0000642 if (sw_if_index0 != conn_sw_if_index0 ||
643 sw_if_index0 != fib_entry_get_resolving_interface (src_fei))
644 {
645 /*
646 * The interface the ARP is sent to or was received on is not the
647 * interface on which the covering prefix is configured.
648 * Maybe this is a case for unnumbered.
649 */
650 if (!arp_unnumbered (p0, sw_if_index0, conn_sw_if_index0))
651 {
652 error0 = ETHERNET_ARP_ERROR_unnumbered_mismatch;
653 goto drop;
654 }
655 }
656 if (arp0->ip4_over_ethernet[0].ip4.as_u32 ==
657 arp0->ip4_over_ethernet[1].ip4.as_u32)
658 {
659 error0 = ETHERNET_ARP_ERROR_gratuitous_arp;
660 goto drop;
661 }
662
663 next0 = arp_mk_reply (vnm, p0, sw_if_index0,
664 if_addr0, arp0, eth_rx);
665
666 /* We are going to reply to this request, so, in the absence of
667 errors, learn the sender */
668 if (!error0)
669 error0 = arp_learn (sw_if_index0, &arp0->ip4_over_ethernet[1]);
670
Neale Rannsfd2417b2021-07-16 14:00:16 +0000671 vlib_increment_simple_counter (
672 &ip_neighbor_counters[AF_IP4].ipnc[VLIB_TX][IP_NEIGHBOR_CTR_REPLY],
673 vm->thread_index, sw_if_index0, 1);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000674 n_replies_sent += 1;
675 goto enqueue;
676
677 next_feature:
678 vnet_feature_next (&next0, p0);
679 goto enqueue;
680
681 drop:
682 p0->error = node->errors[error0];
683
684 enqueue:
685 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
686 n_left_to_next, pi0, next0);
687 }
688
689 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
690 }
691
692 vlib_error_count (vm, node->node_index,
693 ETHERNET_ARP_ERROR_replies_sent, n_replies_sent);
694
695 return frame->n_vectors;
696}
697
698
699static char *ethernet_arp_error_strings[] = {
700#define _(sym,string) string,
701 foreach_ethernet_arp_error
702#undef _
703};
704
705/* *INDENT-OFF* */
706
707VLIB_REGISTER_NODE (arp_input_node, static) =
708{
709 .function = arp_input,
710 .name = "arp-input",
711 .vector_size = sizeof (u32),
712 .n_errors = ETHERNET_ARP_N_ERROR,
713 .error_strings = ethernet_arp_error_strings,
714 .n_next_nodes = ARP_INPUT_N_NEXT,
715 .next_nodes = {
716 [ARP_INPUT_NEXT_DROP] = "error-drop",
717 [ARP_INPUT_NEXT_DISABLED] = "arp-disabled",
718 },
719 .format_buffer = format_ethernet_arp_header,
720 .format_trace = format_ethernet_arp_input_trace,
721};
722
723VLIB_REGISTER_NODE (arp_disabled_node, static) =
724{
725 .function = arp_disabled,
726 .name = "arp-disabled",
727 .vector_size = sizeof (u32),
728 .n_errors = ARP_DISABLED_N_ERROR,
729 .error_strings = arp_disabled_error_strings,
730 .n_next_nodes = ARP_DISABLED_N_NEXT,
731 .next_nodes = {
732 [ARP_INPUT_NEXT_DROP] = "error-drop",
733 },
734 .format_buffer = format_ethernet_arp_header,
735 .format_trace = format_ethernet_arp_input_trace,
736};
737
738VLIB_REGISTER_NODE (arp_reply_node, static) =
739{
740 .function = arp_reply,
741 .name = "arp-reply",
742 .vector_size = sizeof (u32),
743 .n_errors = ETHERNET_ARP_N_ERROR,
744 .error_strings = ethernet_arp_error_strings,
745 .n_next_nodes = ARP_REPLY_N_NEXT,
746 .next_nodes = {
747 [ARP_REPLY_NEXT_DROP] = "error-drop",
748 [ARP_REPLY_NEXT_REPLY_TX] = "interface-output",
749 },
750 .format_buffer = format_ethernet_arp_header,
751 .format_trace = format_ethernet_arp_input_trace,
752};
753
754/* Built-in ARP rx feature path definition */
755VNET_FEATURE_ARC_INIT (arp_feat, static) =
756{
757 .arc_name = "arp",
758 .start_nodes = VNET_FEATURES ("arp-input"),
759 .last_in_arc = "error-drop",
760 .arc_index_ptr = &ethernet_arp_main.feature_arc_index,
761};
762
763VNET_FEATURE_INIT (arp_reply_feat_node, static) =
764{
765 .arc_name = "arp",
766 .node_name = "arp-reply",
767 .runs_before = VNET_FEATURES ("arp-disabled"),
768};
769
770VNET_FEATURE_INIT (arp_proxy_feat_node, static) =
771{
772 .arc_name = "arp",
773 .node_name = "arp-proxy",
774 .runs_after = VNET_FEATURES ("arp-reply"),
775 .runs_before = VNET_FEATURES ("arp-disabled"),
776};
777
778VNET_FEATURE_INIT (arp_disabled_feat_node, static) =
779{
780 .arc_name = "arp",
781 .node_name = "arp-disabled",
782 .runs_before = VNET_FEATURES ("error-drop"),
783};
784
785VNET_FEATURE_INIT (arp_drop_feat_node, static) =
786{
787 .arc_name = "arp",
788 .node_name = "error-drop",
789 .runs_before = 0, /* last feature */
790};
791
792/* *INDENT-ON* */
793
794typedef struct
795{
796 pg_edit_t l2_type, l3_type;
797 pg_edit_t n_l2_address_bytes, n_l3_address_bytes;
798 pg_edit_t opcode;
799 struct
800 {
801 pg_edit_t mac;
802 pg_edit_t ip4;
803 } ip4_over_ethernet[2];
804} pg_ethernet_arp_header_t;
805
806static inline void
807pg_ethernet_arp_header_init (pg_ethernet_arp_header_t * p)
808{
809 /* Initialize fields that are not bit fields in the IP header. */
810#define _(f) pg_edit_init (&p->f, ethernet_arp_header_t, f);
811 _(l2_type);
812 _(l3_type);
813 _(n_l2_address_bytes);
814 _(n_l3_address_bytes);
815 _(opcode);
816 _(ip4_over_ethernet[0].mac);
817 _(ip4_over_ethernet[0].ip4);
818 _(ip4_over_ethernet[1].mac);
819 _(ip4_over_ethernet[1].ip4);
820#undef _
821}
822
823uword
824unformat_pg_arp_header (unformat_input_t * input, va_list * args)
825{
826 pg_stream_t *s = va_arg (*args, pg_stream_t *);
827 pg_ethernet_arp_header_t *p;
828 u32 group_index;
829
830 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ethernet_arp_header_t),
831 &group_index);
832 pg_ethernet_arp_header_init (p);
833
834 /* Defaults. */
835 pg_edit_set_fixed (&p->l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet);
836 pg_edit_set_fixed (&p->l3_type, ETHERNET_TYPE_IP4);
837 pg_edit_set_fixed (&p->n_l2_address_bytes, 6);
838 pg_edit_set_fixed (&p->n_l3_address_bytes, 4);
839
840 if (!unformat (input, "%U: %U/%U -> %U/%U",
841 unformat_pg_edit,
842 unformat_ethernet_arp_opcode_net_byte_order, &p->opcode,
843 unformat_pg_edit,
844 unformat_mac_address_t, &p->ip4_over_ethernet[0].mac,
845 unformat_pg_edit,
846 unformat_ip4_address, &p->ip4_over_ethernet[0].ip4,
847 unformat_pg_edit,
848 unformat_mac_address_t, &p->ip4_over_ethernet[1].mac,
849 unformat_pg_edit,
850 unformat_ip4_address, &p->ip4_over_ethernet[1].ip4))
851 {
852 /* Free up any edits we may have added. */
853 pg_free_edit_group (s);
854 return 0;
855 }
856 return 1;
857}
858
859/*
860 * callback when an interface address is added or deleted
861 */
862static void
863arp_enable_disable_interface (ip4_main_t * im,
864 uword opaque, u32 sw_if_index, u32 is_enable)
865{
866 ethernet_arp_main_t *am = &ethernet_arp_main;
867
868 if (is_enable)
869 arp_enable (am, sw_if_index);
870 else
871 arp_disable (am, sw_if_index);
872}
873
874/*
875 * Remove any arp entries associated with the specified interface
876 */
877static clib_error_t *
878vnet_arp_add_del_sw_interface (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
879{
880 ethernet_arp_main_t *am = &ethernet_arp_main;
BenoƮt Ganne6178bda2020-11-04 10:02:03 +0100881 if (is_add)
882 arp_disable (am, sw_if_index);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000883 return (NULL);
884}
885
886VNET_SW_INTERFACE_ADD_DEL_FUNCTION (vnet_arp_add_del_sw_interface);
887
888const static ip_neighbor_vft_t arp_vft = {
889 .inv_proxy4_add = arp_proxy_add,
890 .inv_proxy4_del = arp_proxy_del,
Mercuryd0892062021-12-12 08:45:38 +0800891 .inv_proxy4_enable = arp_proxy_enable,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000892 .inv_proxy4_disable = arp_proxy_disable,
893};
894
895static clib_error_t *
896ethernet_arp_init (vlib_main_t * vm)
897{
898 ethernet_arp_main_t *am = &ethernet_arp_main;
899 ip4_main_t *im = &ip4_main;
900 pg_node_t *pn;
901
902 ethernet_register_input_type (vm, ETHERNET_TYPE_ARP, arp_input_node.index);
903
904 pn = pg_get_node (arp_input_node.index);
905 pn->unformat_edit = unformat_pg_arp_header;
906
907 am->opcode_by_name = hash_create_string (0, sizeof (uword));
908#define _(o) hash_set_mem (am->opcode_by_name, #o, ETHERNET_ARP_OPCODE_##o);
909 foreach_ethernet_arp_opcode;
910#undef _
911
912 /* don't trace ARP error packets */
913 {
914 vlib_node_runtime_t *rt =
915 vlib_node_get_runtime (vm, arp_input_node.index);
916
917#define _(a,b) \
918 vnet_pcap_drop_trace_filter_add_del \
919 (rt->errors[ETHERNET_ARP_ERROR_##a], \
920 1 /* is_add */);
921 foreach_ethernet_arp_error
922#undef _
923 }
924
925 {
926 ip4_enable_disable_interface_callback_t cb = {
927 .function = arp_enable_disable_interface,
928 };
929 vec_add1 (im->enable_disable_interface_callbacks, cb);
930 }
931
Neale Rannsdc617b82020-08-20 08:22:56 +0000932 ip_neighbor_register (AF_IP4, &arp_vft);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000933
934 return 0;
935}
936
937/* *INDENT-OFF* */
938VLIB_INIT_FUNCTION (ethernet_arp_init) =
939{
940 .runs_after = VLIB_INITS("ethernet_init",
941 "ip_neighbor_init"),
942};
943/* *INDENT-ON* */
944
945/*
946 * fd.io coding-style-patch-verification: ON
947 *
948 * Local Variables:
949 * eval: (c-set-style "gnu")
950 * End:
951 */