VPP251 - Code style vnet/vnet/ethernet
[vpp.git] / vnet / vnet / ethernet / arp.c
1 /*
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/ip/ip.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/ethernet/arp_packet.h>
21 #include <vnet/l2/l2_input.h>
22 #include <vppinfra/mhash.h>
23
24 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
25
26 typedef struct
27 {
28   u32 sw_if_index;
29   u32 fib_index;
30   ip4_address_t ip4_address;
31 } ethernet_arp_ip4_key_t;
32
33 typedef struct
34 {
35   ethernet_arp_ip4_key_t key;
36   u8 ethernet_address[6];
37
38   u16 flags;
39 #define ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC (1 << 0)
40 #define ETHERNET_ARP_IP4_ENTRY_FLAG_GLEAN  (2 << 0)
41
42   u64 cpu_time_last_updated;
43
44   u32 *adjacencies;
45 } ethernet_arp_ip4_entry_t;
46
47 typedef struct
48 {
49   u32 lo_addr;
50   u32 hi_addr;
51   u32 fib_index;
52 } ethernet_proxy_arp_t;
53
54 typedef struct
55 {
56   u32 next_index;
57   uword node_index;
58   uword type_opaque;
59   uword data;
60   /* Used for arp event notification only */
61   void *data_callback;
62   u32 pid;
63 } pending_resolution_t;
64
65 typedef struct
66 {
67   /* Hash tables mapping name to opcode. */
68   uword *opcode_by_name;
69
70   /* lite beer "glean" adjacency handling */
71   uword *pending_resolutions_by_address;
72   pending_resolution_t *pending_resolutions;
73
74   /* Mac address change notification */
75   uword *mac_changes_by_address;
76   pending_resolution_t *mac_changes;
77
78   ethernet_arp_ip4_entry_t *ip4_entry_pool;
79
80   mhash_t ip4_entry_by_key;
81
82   /* ARP attack mitigation */
83   u32 arp_delete_rotor;
84   u32 limit_arp_cache_size;
85
86   /* Proxy arp vector */
87   ethernet_proxy_arp_t *proxy_arps;
88 } ethernet_arp_main_t;
89
90 static ethernet_arp_main_t ethernet_arp_main;
91
92 static u8 *
93 format_ethernet_arp_hardware_type (u8 * s, va_list * va)
94 {
95   ethernet_arp_hardware_type_t h = va_arg (*va, ethernet_arp_hardware_type_t);
96   char *t = 0;
97   switch (h)
98     {
99 #define _(n,f) case n: t = #f; break;
100       foreach_ethernet_arp_hardware_type;
101 #undef _
102
103     default:
104       return format (s, "unknown 0x%x", h);
105     }
106
107   return format (s, "%s", t);
108 }
109
110 static u8 *
111 format_ethernet_arp_opcode (u8 * s, va_list * va)
112 {
113   ethernet_arp_opcode_t o = va_arg (*va, ethernet_arp_opcode_t);
114   char *t = 0;
115   switch (o)
116     {
117 #define _(f) case ETHERNET_ARP_OPCODE_##f: t = #f; break;
118       foreach_ethernet_arp_opcode;
119 #undef _
120
121     default:
122       return format (s, "unknown 0x%x", o);
123     }
124
125   return format (s, "%s", t);
126 }
127
128 static uword
129 unformat_ethernet_arp_opcode_host_byte_order (unformat_input_t * input,
130                                               va_list * args)
131 {
132   int *result = va_arg (*args, int *);
133   ethernet_arp_main_t *am = &ethernet_arp_main;
134   int x, i;
135
136   /* Numeric opcode. */
137   if (unformat (input, "0x%x", &x) || unformat (input, "%d", &x))
138     {
139       if (x >= (1 << 16))
140         return 0;
141       *result = x;
142       return 1;
143     }
144
145   /* Named type. */
146   if (unformat_user (input, unformat_vlib_number_by_name,
147                      am->opcode_by_name, &i))
148     {
149       *result = i;
150       return 1;
151     }
152
153   return 0;
154 }
155
156 static uword
157 unformat_ethernet_arp_opcode_net_byte_order (unformat_input_t * input,
158                                              va_list * args)
159 {
160   int *result = va_arg (*args, int *);
161   if (!unformat_user
162       (input, unformat_ethernet_arp_opcode_host_byte_order, result))
163     return 0;
164
165   *result = clib_host_to_net_u16 ((u16) * result);
166   return 1;
167 }
168
169 static u8 *
170 format_ethernet_arp_header (u8 * s, va_list * va)
171 {
172   ethernet_arp_header_t *a = va_arg (*va, ethernet_arp_header_t *);
173   u32 max_header_bytes = va_arg (*va, u32);
174   uword indent;
175   u16 l2_type, l3_type;
176
177   if (max_header_bytes != 0 && sizeof (a[0]) > max_header_bytes)
178     return format (s, "ARP header truncated");
179
180   l2_type = clib_net_to_host_u16 (a->l2_type);
181   l3_type = clib_net_to_host_u16 (a->l3_type);
182
183   indent = format_get_indent (s);
184
185   s = format (s, "%U, type %U/%U, address size %d/%d",
186               format_ethernet_arp_opcode, clib_net_to_host_u16 (a->opcode),
187               format_ethernet_arp_hardware_type, l2_type,
188               format_ethernet_type, l3_type,
189               a->n_l2_address_bytes, a->n_l3_address_bytes);
190
191   if (l2_type == ETHERNET_ARP_HARDWARE_TYPE_ethernet
192       && l3_type == ETHERNET_TYPE_IP4)
193     {
194       s = format (s, "\n%U%U/%U -> %U/%U",
195                   format_white_space, indent,
196                   format_ethernet_address, a->ip4_over_ethernet[0].ethernet,
197                   format_ip4_address, &a->ip4_over_ethernet[0].ip4,
198                   format_ethernet_address, a->ip4_over_ethernet[1].ethernet,
199                   format_ip4_address, &a->ip4_over_ethernet[1].ip4);
200     }
201   else
202     {
203       uword n2 = a->n_l2_address_bytes;
204       uword n3 = a->n_l3_address_bytes;
205       s = format (s, "\n%U%U/%U -> %U/%U",
206                   format_white_space, indent,
207                   format_hex_bytes, a->data + 0 * n2 + 0 * n3, n2,
208                   format_hex_bytes, a->data + 1 * n2 + 0 * n3, n3,
209                   format_hex_bytes, a->data + 1 * n2 + 1 * n3, n2,
210                   format_hex_bytes, a->data + 2 * n2 + 1 * n3, n3);
211     }
212
213   return s;
214 }
215
216 static u8 *
217 format_ethernet_arp_ip4_entry (u8 * s, va_list * va)
218 {
219   vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
220   ethernet_arp_ip4_entry_t *e = va_arg (*va, ethernet_arp_ip4_entry_t *);
221   vnet_sw_interface_t *si;
222   ip4_fib_t *fib;
223   u8 *flags = 0;
224
225   if (!e)
226     return format (s, "%=12s%=6s%=16s%=6s%=20s%=24s", "Time", "FIB", "IP4",
227                    "Flags", "Ethernet", "Interface");
228
229   fib = find_ip4_fib_by_table_index_or_id (&ip4_main, e->key.fib_index,
230                                            IP4_ROUTE_FLAG_FIB_INDEX);
231   si = vnet_get_sw_interface (vnm, e->key.sw_if_index);
232
233   if (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_GLEAN)
234     flags = format (flags, "G");
235
236   if (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC)
237     flags = format (flags, "S");
238
239   s = format (s, "%=12U%=6u%=16U%=6s%=20U%=24U",
240               format_vlib_cpu_time, vnm->vlib_main, e->cpu_time_last_updated,
241               fib->table_id,
242               format_ip4_address, &e->key.ip4_address,
243               flags ? (char *) flags : "",
244               format_ethernet_address, e->ethernet_address,
245               format_vnet_sw_interface_name, vnm, si);
246
247   vec_free (flags);
248   return s;
249 }
250
251 typedef struct
252 {
253   u8 packet_data[64];
254 } ethernet_arp_input_trace_t;
255
256 static u8 *
257 format_ethernet_arp_input_trace (u8 * s, va_list * va)
258 {
259   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
260   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
261   ethernet_arp_input_trace_t *t = va_arg (*va, ethernet_arp_input_trace_t *);
262
263   s = format (s, "%U",
264               format_ethernet_arp_header,
265               t->packet_data, sizeof (t->packet_data));
266
267   return s;
268 }
269
270 clib_error_t *
271 ethernet_arp_sw_interface_up_down (vnet_main_t * vnm,
272                                    u32 sw_if_index, u32 flags)
273 {
274   ethernet_arp_main_t *am = &ethernet_arp_main;
275   ethernet_arp_ip4_entry_t *e;
276   u32 i;
277   u32 *to_add_del = 0;
278
279   /* *INDENT-OFF* */
280  pool_foreach (e, am->ip4_entry_pool, ({
281     if (e->key.sw_if_index == sw_if_index)
282         vec_add1 (to_add_del, e - am->ip4_entry_pool);
283   }));
284  /* *INDENT-ON* */
285
286   for (i = 0; i < vec_len (to_add_del); i++)
287     {
288       ethernet_arp_ip4_over_ethernet_address_t arp_add;
289       e = pool_elt_at_index (am->ip4_entry_pool, to_add_del[i]);
290
291       clib_memcpy (&arp_add.ethernet, e->ethernet_address, 6);
292       arp_add.ip4.as_u32 = e->key.ip4_address.as_u32;
293
294       if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
295         {
296           vnet_arp_set_ip4_over_ethernet (vnm,
297                                           e->key.sw_if_index,
298                                           e->key.fib_index, &arp_add,
299                                           e->flags &
300                                           ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC);
301         }
302       else if ((e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) == 0)
303         {
304           vnet_arp_unset_ip4_over_ethernet (vnm,
305                                             e->key.sw_if_index,
306                                             e->key.fib_index, &arp_add);
307         }
308     }
309
310   vec_free (to_add_del);
311   return 0;
312 }
313
314 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_arp_sw_interface_up_down);
315
316 static int
317 vnet_arp_set_ip4_over_ethernet_internal (vnet_main_t * vnm,
318                                          u32 sw_if_index,
319                                          u32 fib_index,
320                                          void *a_arg, int is_static);
321
322 static int
323 vnet_arp_unset_ip4_over_ethernet_internal (vnet_main_t * vnm,
324                                            u32 sw_if_index,
325                                            u32 fib_index, void *a_arg);
326
327 typedef struct
328 {
329   u32 sw_if_index;
330   u32 fib_index;
331   ethernet_arp_ip4_over_ethernet_address_t a;
332   int is_static;
333   int is_remove;                /* set is_remove=1 to clear arp entry */
334 } vnet_arp_set_ip4_over_ethernet_rpc_args_t;
335
336 static void set_ip4_over_ethernet_rpc_callback
337   (vnet_arp_set_ip4_over_ethernet_rpc_args_t * a)
338 {
339   vnet_main_t *vm = vnet_get_main ();
340   ASSERT (os_get_cpu_number () == 0);
341
342   if (a->is_remove)
343     vnet_arp_unset_ip4_over_ethernet_internal (vm,
344                                                a->sw_if_index,
345                                                a->fib_index, &(a->a));
346   else
347     vnet_arp_set_ip4_over_ethernet_internal (vm,
348                                              a->sw_if_index,
349                                              a->fib_index,
350                                              &(a->a), a->is_static);
351 }
352
353 int
354 vnet_arp_set_ip4_over_ethernet (vnet_main_t * vnm,
355                                 u32 sw_if_index,
356                                 u32 fib_index, void *a_arg, int is_static)
357 {
358   ethernet_arp_ip4_over_ethernet_address_t *a = a_arg;
359   vnet_arp_set_ip4_over_ethernet_rpc_args_t args;
360
361   args.sw_if_index = sw_if_index;
362   args.fib_index = fib_index;
363   args.is_static = is_static;
364   args.is_remove = 0;
365   clib_memcpy (&args.a, a, sizeof (*a));
366
367   vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback,
368                                (u8 *) & args, sizeof (args));
369   return 0;
370 }
371
372 int
373 vnet_arp_set_ip4_over_ethernet_internal (vnet_main_t * vnm,
374                                          u32 sw_if_index,
375                                          u32 fib_index,
376                                          void *a_arg, int is_static)
377 {
378   ethernet_arp_ip4_key_t k;
379   ethernet_arp_ip4_entry_t *e = 0;
380   ethernet_arp_main_t *am = &ethernet_arp_main;
381   ethernet_arp_ip4_over_ethernet_address_t *a = a_arg;
382   vlib_main_t *vm = vlib_get_main ();
383   ip4_main_t *im = &ip4_main;
384   ip_lookup_main_t *lm = &im->lookup_main;
385   int make_new_arp_cache_entry = 1;
386   uword *p;
387   ip4_add_del_route_args_t args;
388   ip_adjacency_t adj, *existing_adj;
389   pending_resolution_t *pr, *mc;
390
391   u32 next_index;
392   u32 adj_index;
393
394   fib_index = (fib_index != (u32) ~ 0)
395     ? fib_index : im->fib_index_by_sw_if_index[sw_if_index];
396
397   k.sw_if_index = sw_if_index;
398   k.ip4_address = a->ip4;
399   k.fib_index = fib_index;
400
401   p = mhash_get (&am->ip4_entry_by_key, &k);
402   if (p)
403     {
404       e = pool_elt_at_index (am->ip4_entry_pool, p[0]);
405
406       /* Refuse to over-write static arp. */
407       if (!is_static && (e->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC))
408         return -2;
409       make_new_arp_cache_entry = 0;
410     }
411
412   /* Note: always install the route. It might have been deleted */
413   memset (&adj, 0, sizeof (adj));
414   adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
415   adj.n_adj = 1;                /*  otherwise signature compare fails */
416
417   vnet_rewrite_for_sw_interface (vnm, VNET_L3_PACKET_TYPE_IP4, sw_if_index, ip4_rewrite_node.index, a->ethernet,        /* destination address */
418                                  &adj.rewrite_header,
419                                  sizeof (adj.rewrite_data));
420
421   /* result of this lookup should be next-hop adjacency */
422   adj_index = ip4_fib_lookup_with_table (im, fib_index, &a->ip4, 0);
423   existing_adj = ip_get_adjacency (lm, adj_index);
424
425   if (existing_adj->lookup_next_index == IP_LOOKUP_NEXT_ARP &&
426       existing_adj->arp.next_hop.ip4.as_u32 == a->ip4.as_u32)
427     {
428       u32 *ai;
429       u32 *adjs = vec_dup (e->adjacencies);
430       /* Update all adj assigned to this arp entry */
431       vec_foreach (ai, adjs)
432       {
433         int i;
434         ip_adjacency_t *uadj = ip_get_adjacency (lm, *ai);
435         for (i = 0; i < uadj->n_adj; i++)
436           if (uadj[i].lookup_next_index == IP_LOOKUP_NEXT_ARP &&
437               uadj[i].arp.next_hop.ip4.as_u32 == a->ip4.as_u32)
438             ip_update_adjacency (lm, *ai + i, &adj);
439       }
440       vec_free (adjs);
441     }
442   else
443     {
444       /* Check that new adjacency actually isn't exactly the same as
445        *  what is already there. If we over-write the adjacency with
446        *  exactly the same info, its technically a new adjacency with
447        *  new counters, but to user it appears as counters reset.
448        */
449       if (vnet_ip_adjacency_share_compare (&adj, existing_adj) == 0)
450         {
451           /* create new adj */
452           args.table_index_or_table_id = fib_index;
453           args.flags =
454             IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_ADD |
455             IP4_ROUTE_FLAG_NEIGHBOR;
456           args.dst_address = a->ip4;
457           args.dst_address_length = 32;
458           args.adj_index = ~0;
459           args.add_adj = &adj;
460           args.n_add_adj = 1;
461           ip4_add_del_route (im, &args);
462         }
463     }
464
465   if (make_new_arp_cache_entry)
466     {
467       pool_get (am->ip4_entry_pool, e);
468       mhash_set (&am->ip4_entry_by_key, &k, e - am->ip4_entry_pool,
469                  /* old value */ 0);
470       e->key = k;
471     }
472
473   /* Update time stamp and ethernet address. */
474   clib_memcpy (e->ethernet_address, a->ethernet,
475                sizeof (e->ethernet_address));
476   e->cpu_time_last_updated = clib_cpu_time_now ();
477   if (is_static)
478     e->flags |= ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC;
479
480   /* Customer(s) waiting for this address to be resolved? */
481   p = hash_get (am->pending_resolutions_by_address, a->ip4.as_u32);
482   if (p)
483     {
484       next_index = p[0];
485
486       while (next_index != (u32) ~ 0)
487         {
488           pr = pool_elt_at_index (am->pending_resolutions, next_index);
489           vlib_process_signal_event (vm, pr->node_index,
490                                      pr->type_opaque, pr->data);
491           next_index = pr->next_index;
492           pool_put (am->pending_resolutions, pr);
493         }
494
495       hash_unset (am->pending_resolutions_by_address, a->ip4.as_u32);
496     }
497
498   /* Customer(s) requesting ARP event for this address? */
499   p = hash_get (am->mac_changes_by_address, a->ip4.as_u32);
500   if (p)
501     {
502       next_index = p[0];
503
504       while (next_index != (u32) ~ 0)
505         {
506           int (*fp) (u32, u8 *, u32, u32);
507           int rv = 1;
508           mc = pool_elt_at_index (am->mac_changes, next_index);
509           fp = mc->data_callback;
510
511           /* Call the user's data callback, return 1 to suppress dup events */
512           if (fp)
513             rv = (*fp) (mc->data, a->ethernet, sw_if_index, 0);
514
515           /* 
516            * Signal the resolver process, as long as the user
517            * says they want to be notified
518            */
519           if (rv == 0)
520             vlib_process_signal_event (vm, mc->node_index,
521                                        mc->type_opaque, mc->data);
522           next_index = mc->next_index;
523         }
524     }
525
526   return 0;
527 }
528
529 void
530 vnet_register_ip4_arp_resolution_event (vnet_main_t * vnm,
531                                         void *address_arg,
532                                         uword node_index,
533                                         uword type_opaque, uword data)
534 {
535   ethernet_arp_main_t *am = &ethernet_arp_main;
536   ip4_address_t *address = address_arg;
537   uword *p;
538   pending_resolution_t *pr;
539
540   pool_get (am->pending_resolutions, pr);
541
542   pr->next_index = ~0;
543   pr->node_index = node_index;
544   pr->type_opaque = type_opaque;
545   pr->data = data;
546   pr->data_callback = 0;
547
548   p = hash_get (am->pending_resolutions_by_address, address->as_u32);
549   if (p)
550     {
551       /* Insert new resolution at the head of the list */
552       pr->next_index = p[0];
553       hash_unset (am->pending_resolutions_by_address, address->as_u32);
554     }
555
556   hash_set (am->pending_resolutions_by_address, address->as_u32,
557             pr - am->pending_resolutions);
558 }
559
560 int
561 vnet_add_del_ip4_arp_change_event (vnet_main_t * vnm,
562                                    void *data_callback,
563                                    u32 pid,
564                                    void *address_arg,
565                                    uword node_index,
566                                    uword type_opaque, uword data, int is_add)
567 {
568   ethernet_arp_main_t *am = &ethernet_arp_main;
569   ip4_address_t *address = address_arg;
570   uword *p;
571   pending_resolution_t *mc;
572   void (*fp) (u32, u8 *) = data_callback;
573
574   if (is_add)
575     {
576       pool_get (am->mac_changes, mc);
577
578       mc->next_index = ~0;
579       mc->node_index = node_index;
580       mc->type_opaque = type_opaque;
581       mc->data = data;
582       mc->data_callback = data_callback;
583       mc->pid = pid;
584
585       p = hash_get (am->mac_changes_by_address, address->as_u32);
586       if (p)
587         {
588           /* Insert new resolution at the head of the list */
589           mc->next_index = p[0];
590           hash_unset (am->mac_changes_by_address, address->as_u32);
591         }
592
593       hash_set (am->mac_changes_by_address, address->as_u32,
594                 mc - am->mac_changes);
595       return 0;
596     }
597   else
598     {
599       u32 index;
600       pending_resolution_t *mc_last = 0;
601
602       p = hash_get (am->mac_changes_by_address, address->as_u32);
603       if (p == 0)
604         return VNET_API_ERROR_NO_SUCH_ENTRY;
605
606       index = p[0];
607
608       while (index != (u32) ~ 0)
609         {
610           mc = pool_elt_at_index (am->mac_changes, index);
611           if (mc->node_index == node_index &&
612               mc->type_opaque == type_opaque && mc->pid == pid)
613             {
614               /* Clients may need to clean up pool entries, too */
615               if (fp)
616                 (*fp) (mc->data, 0 /* no new mac addrs */ );
617               if (index == p[0])
618                 {
619                   hash_unset (am->mac_changes_by_address, address->as_u32);
620                   if (mc->next_index != ~0)
621                     hash_set (am->mac_changes_by_address, address->as_u32,
622                               mc->next_index);
623                   pool_put (am->mac_changes, mc);
624                   return 0;
625                 }
626               else
627                 {
628                   ASSERT (mc_last);
629                   mc_last->next_index = mc->next_index;
630                   pool_put (am->mac_changes, mc);
631                   return 0;
632                 }
633             }
634           mc_last = mc;
635           index = mc->next_index;
636         }
637
638       return VNET_API_ERROR_NO_SUCH_ENTRY;
639     }
640 }
641
642 /* Either we drop the packet or we send a reply to the sender. */
643 typedef enum
644 {
645   ARP_INPUT_NEXT_DROP,
646   ARP_INPUT_NEXT_REPLY_TX,
647   ARP_INPUT_N_NEXT,
648 } arp_input_next_t;
649
650 #define foreach_ethernet_arp_error                                      \
651   _ (replies_sent, "ARP replies sent")                                  \
652   _ (l2_type_not_ethernet, "L2 type not ethernet")                      \
653   _ (l3_type_not_ip4, "L3 type not IP4")                                \
654   _ (l3_src_address_not_local, "IP4 source address not local to subnet") \
655   _ (l3_dst_address_not_local, "IP4 destination address not local to subnet") \
656   _ (l3_src_address_is_local, "IP4 source address matches local interface") \
657   _ (l3_src_address_learned, "ARP request IP4 source address learned")  \
658   _ (replies_received, "ARP replies received")                          \
659   _ (opcode_not_request, "ARP opcode not request")                      \
660   _ (proxy_arp_replies_sent, "Proxy ARP replies sent")                  \
661   _ (l2_address_mismatch, "ARP hw addr does not match L2 frame src addr") \
662   _ (missing_interface_address, "ARP missing interface address") \
663   _ (gratuitous_arp, "ARP probe or announcement dropped") \
664
665 typedef enum
666 {
667 #define _(sym,string) ETHERNET_ARP_ERROR_##sym,
668   foreach_ethernet_arp_error
669 #undef _
670     ETHERNET_ARP_N_ERROR,
671 } ethernet_arp_input_error_t;
672
673 /* get first interface address */
674 ip4_address_t *
675 ip4_interface_first_address (ip4_main_t * im, u32 sw_if_index,
676                              ip_interface_address_t ** result_ia)
677 {
678   ip_lookup_main_t *lm = &im->lookup_main;
679   ip_interface_address_t *ia = 0;
680   ip4_address_t *result = 0;
681
682   /* *INDENT-OFF* */
683   foreach_ip_interface_address (lm, ia, sw_if_index,
684                                 1 /* honor unnumbered */ ,
685   ({
686     ip4_address_t * a =
687       ip_interface_address_get_address (lm, ia);
688     result = a; break;
689   }));
690   /* *INDENT-ON* */
691
692   if (result_ia)
693     *result_ia = result ? ia : 0;
694   return result;
695 }
696
697 static void
698 unset_random_arp_entry (void)
699 {
700   ethernet_arp_main_t *am = &ethernet_arp_main;
701   ethernet_arp_ip4_entry_t *e;
702   vnet_main_t *vnm = vnet_get_main ();
703   ethernet_arp_ip4_over_ethernet_address_t delme;
704   u32 index;
705
706   index = pool_next_index (am->ip4_entry_pool, am->arp_delete_rotor);
707   am->arp_delete_rotor = index;
708
709   /* Try again from elt 0, could happen if an intfc goes down */
710   if (index == ~0)
711     {
712       index = pool_next_index (am->ip4_entry_pool, am->arp_delete_rotor);
713       am->arp_delete_rotor = index;
714     }
715
716   /* Nothing left in the pool */
717   if (index == ~0)
718     return;
719
720   e = pool_elt_at_index (am->ip4_entry_pool, index);
721
722   clib_memcpy (&delme.ethernet, e->ethernet_address, 6);
723   delme.ip4.as_u32 = e->key.ip4_address.as_u32;
724
725   vnet_arp_unset_ip4_over_ethernet (vnm, e->key.sw_if_index,
726                                     e->key.fib_index, &delme);
727 }
728
729 static void
730 arp_unnumbered (vlib_buffer_t * p0,
731                 u32 pi0,
732                 ethernet_header_t * eth0, ip_interface_address_t * ifa0)
733 {
734   vlib_main_t *vm = vlib_get_main ();
735   vnet_main_t *vnm = vnet_get_main ();
736   vnet_interface_main_t *vim = &vnm->interface_main;
737   vnet_sw_interface_t *si;
738   vnet_hw_interface_t *hi;
739   u32 unnum_src_sw_if_index;
740   u32 *broadcast_swifs = 0;
741   u32 *buffers = 0;
742   u32 n_alloc = 0;
743   vlib_buffer_t *b0;
744   int i;
745   u8 dst_mac_address[6];
746   i16 header_size;
747   ethernet_arp_header_t *arp0;
748
749   /* Save the dst mac address */
750   clib_memcpy (dst_mac_address, eth0->dst_address, sizeof (dst_mac_address));
751
752   /* Figure out which sw_if_index supplied the address */
753   unnum_src_sw_if_index = ifa0->sw_if_index;
754
755   /* Track down all users of the unnumbered source */
756   /* *INDENT-OFF* */
757   pool_foreach (si, vim->sw_interfaces,
758   ({
759     if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED &&
760         (si->unnumbered_sw_if_index == unnum_src_sw_if_index))
761       {
762         vec_add1 (broadcast_swifs, si->sw_if_index);
763       }
764   }));
765   /* *INDENT-ON* */
766
767   ASSERT (vec_len (broadcast_swifs));
768
769   /* Allocate buffering if we need it */
770   if (vec_len (broadcast_swifs) > 1)
771     {
772       vec_validate (buffers, vec_len (broadcast_swifs) - 2);
773       n_alloc = vlib_buffer_alloc (vm, buffers, vec_len (buffers));
774       _vec_len (buffers) = n_alloc;
775       for (i = 0; i < n_alloc; i++)
776         {
777           b0 = vlib_get_buffer (vm, buffers[i]);
778
779           /* xerox (partially built) ARP pkt */
780           clib_memcpy (b0->data, p0->data,
781                        p0->current_length + p0->current_data);
782           b0->current_data = p0->current_data;
783           b0->current_length = p0->current_length;
784           vnet_buffer (b0)->sw_if_index[VLIB_RX] =
785             vnet_buffer (p0)->sw_if_index[VLIB_RX];
786         }
787     }
788
789   vec_insert (buffers, 1, 0);
790   buffers[0] = pi0;
791
792   for (i = 0; i < vec_len (buffers); i++)
793     {
794       b0 = vlib_get_buffer (vm, buffers[i]);
795       arp0 = vlib_buffer_get_current (b0);
796
797       hi = vnet_get_sup_hw_interface (vnm, broadcast_swifs[i]);
798       si = vnet_get_sw_interface (vnm, broadcast_swifs[i]);
799
800       /* For decoration, most likely */
801       vnet_buffer (b0)->sw_if_index[VLIB_TX] = hi->sw_if_index;
802
803       /* Fix ARP pkt src address */
804       clib_memcpy (arp0->ip4_over_ethernet[0].ethernet, hi->hw_address, 6);
805
806       /* Build L2 encaps for this swif */
807       header_size = sizeof (ethernet_header_t);
808       if (si->sub.eth.flags.one_tag)
809         header_size += 4;
810       else if (si->sub.eth.flags.two_tags)
811         header_size += 8;
812
813       vlib_buffer_advance (b0, -header_size);
814       eth0 = vlib_buffer_get_current (b0);
815
816       if (si->sub.eth.flags.one_tag)
817         {
818           ethernet_vlan_header_t *outer = (void *) (eth0 + 1);
819
820           eth0->type = si->sub.eth.flags.dot1ad ?
821             clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
822             clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
823           outer->priority_cfi_and_id =
824             clib_host_to_net_u16 (si->sub.eth.outer_vlan_id);
825           outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_ARP);
826
827         }
828       else if (si->sub.eth.flags.two_tags)
829         {
830           ethernet_vlan_header_t *outer = (void *) (eth0 + 1);
831           ethernet_vlan_header_t *inner = (void *) (outer + 1);
832
833           eth0->type = si->sub.eth.flags.dot1ad ?
834             clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
835             clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
836           outer->priority_cfi_and_id =
837             clib_host_to_net_u16 (si->sub.eth.outer_vlan_id);
838           outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
839           inner->priority_cfi_and_id =
840             clib_host_to_net_u16 (si->sub.eth.inner_vlan_id);
841           inner->type = clib_host_to_net_u16 (ETHERNET_TYPE_ARP);
842
843         }
844       else
845         {
846           eth0->type = clib_host_to_net_u16 (ETHERNET_TYPE_ARP);
847         }
848
849       /* Restore the original dst address, set src address */
850       clib_memcpy (eth0->dst_address, dst_mac_address,
851                    sizeof (eth0->dst_address));
852       clib_memcpy (eth0->src_address, hi->hw_address,
853                    sizeof (eth0->src_address));
854
855       /* Transmit replicas */
856       if (i > 0)
857         {
858           vlib_frame_t *f =
859             vlib_get_frame_to_node (vm, hi->output_node_index);
860           u32 *to_next = vlib_frame_vector_args (f);
861           to_next[0] = buffers[i];
862           f->n_vectors = 1;
863           vlib_put_frame_to_node (vm, hi->output_node_index, f);
864         }
865     }
866
867   /* The regular path outputs the original pkt.. */
868   vnet_buffer (p0)->sw_if_index[VLIB_TX] = broadcast_swifs[0];
869
870   vec_free (broadcast_swifs);
871   vec_free (buffers);
872 }
873
874 static uword
875 arp_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
876 {
877   ethernet_arp_main_t *am = &ethernet_arp_main;
878   vnet_main_t *vnm = vnet_get_main ();
879   ip4_main_t *im4 = &ip4_main;
880   u32 n_left_from, next_index, *from, *to_next;
881   u32 n_replies_sent = 0, n_proxy_arp_replies_sent = 0;
882
883   from = vlib_frame_vector_args (frame);
884   n_left_from = frame->n_vectors;
885   next_index = node->cached_next_index;
886
887   if (node->flags & VLIB_NODE_FLAG_TRACE)
888     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
889                                    /* stride */ 1,
890                                    sizeof (ethernet_arp_input_trace_t));
891
892   while (n_left_from > 0)
893     {
894       u32 n_left_to_next;
895
896       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
897
898       while (n_left_from > 0 && n_left_to_next > 0)
899         {
900           vlib_buffer_t *p0;
901           vnet_hw_interface_t *hw_if0;
902           ethernet_arp_header_t *arp0;
903           ethernet_header_t *eth0;
904           ip_interface_address_t *ifa0;
905           ip_adjacency_t *adj0;
906           ip4_address_t *if_addr0;
907           ip4_address_t proxy_src;
908           u32 pi0, error0, next0, sw_if_index0;
909           u8 is_request0, src_is_local0, dst_is_local0, is_unnum0;
910           ethernet_proxy_arp_t *pa;
911
912           pi0 = from[0];
913           to_next[0] = pi0;
914           from += 1;
915           to_next += 1;
916           n_left_from -= 1;
917           n_left_to_next -= 1;
918
919           p0 = vlib_get_buffer (vm, pi0);
920           arp0 = vlib_buffer_get_current (p0);
921
922           is_request0 = arp0->opcode
923             == clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request);
924
925           error0 = ETHERNET_ARP_ERROR_replies_sent;
926
927           error0 =
928             (arp0->l2_type !=
929              clib_net_to_host_u16 (ETHERNET_ARP_HARDWARE_TYPE_ethernet) ?
930              ETHERNET_ARP_ERROR_l2_type_not_ethernet : error0);
931           error0 =
932             (arp0->l3_type !=
933              clib_net_to_host_u16 (ETHERNET_TYPE_IP4) ?
934              ETHERNET_ARP_ERROR_l3_type_not_ip4 : error0);
935
936           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
937
938           if (error0)
939             goto drop1;
940
941           /* Check that IP address is local and matches incoming interface. */
942           if_addr0 =
943             ip4_interface_address_matching_destination (im4,
944                                                         &arp0->
945                                                         ip4_over_ethernet[1].
946                                                         ip4, sw_if_index0,
947                                                         &ifa0);
948           if (!if_addr0)
949             {
950               error0 = ETHERNET_ARP_ERROR_l3_dst_address_not_local;
951               goto drop1;
952             }
953
954           /* Honor unnumbered interface, if any */
955           is_unnum0 = sw_if_index0 != ifa0->sw_if_index;
956
957           /* Source must also be local to subnet of matching interface address. */
958           if (!ip4_destination_matches_interface
959               (im4, &arp0->ip4_over_ethernet[0].ip4, ifa0))
960             {
961               error0 = ETHERNET_ARP_ERROR_l3_src_address_not_local;
962               goto drop1;
963             }
964
965           /* Reject requests/replies with our local interface address. */
966           src_is_local0 =
967             if_addr0->as_u32 == arp0->ip4_over_ethernet[0].ip4.as_u32;
968           if (src_is_local0)
969             {
970               error0 = ETHERNET_ARP_ERROR_l3_src_address_is_local;
971               goto drop1;
972             }
973
974           dst_is_local0 =
975             if_addr0->as_u32 == arp0->ip4_over_ethernet[1].ip4.as_u32;
976
977           /* Fill in ethernet header. */
978           eth0 = ethernet_buffer_get_header (p0);
979
980           /* Trash ARP packets whose ARP-level source addresses do not
981              match their L2-frame-level source addresses */
982           if (memcmp (eth0->src_address, arp0->ip4_over_ethernet[0].ethernet,
983                       sizeof (eth0->src_address)))
984             {
985               error0 = ETHERNET_ARP_ERROR_l2_address_mismatch;
986               goto drop2;
987             }
988
989           /* Learn or update sender's mapping only for requests or unicasts
990              that don't match local interface address. */
991           if (ethernet_address_cast (eth0->dst_address) ==
992               ETHERNET_ADDRESS_UNICAST || is_request0)
993             {
994               if (am->limit_arp_cache_size &&
995                   pool_elts (am->ip4_entry_pool) >= am->limit_arp_cache_size)
996                 unset_random_arp_entry ();
997
998               vnet_arp_set_ip4_over_ethernet (vnm, sw_if_index0,
999                                               (u32) ~ 0 /* default fib */ ,
1000                                               &arp0->ip4_over_ethernet[0],
1001                                               0 /* is_static */ );
1002               error0 = ETHERNET_ARP_ERROR_l3_src_address_learned;
1003             }
1004
1005           /* Only send a reply for requests sent which match a local interface. */
1006           if (!(is_request0 && dst_is_local0))
1007             {
1008               error0 =
1009                 (arp0->opcode ==
1010                  clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply) ?
1011                  ETHERNET_ARP_ERROR_replies_received : error0);
1012               goto drop1;
1013             }
1014
1015           /* Send a reply. */
1016         send_reply:
1017           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1018           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1019
1020           /* Send reply back through input interface */
1021           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1022           next0 = ARP_INPUT_NEXT_REPLY_TX;
1023
1024           arp0->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
1025
1026           arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
1027
1028           clib_memcpy (arp0->ip4_over_ethernet[0].ethernet,
1029                        hw_if0->hw_address, 6);
1030           clib_mem_unaligned (&arp0->ip4_over_ethernet[0].ip4.data_u32, u32) =
1031             if_addr0->data_u32;
1032
1033           /* Hardware must be ethernet-like. */
1034           ASSERT (vec_len (hw_if0->hw_address) == 6);
1035
1036           clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1037           clib_memcpy (eth0->src_address, hw_if0->hw_address, 6);
1038
1039           /* Figure out how much to rewind current data from adjacency. */
1040           if (ifa0)
1041             {
1042               adj0 = ip_get_adjacency (&ip4_main.lookup_main,
1043                                        ifa0->neighbor_probe_adj_index);
1044               if (adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP)
1045                 {
1046                   error0 = ETHERNET_ARP_ERROR_missing_interface_address;
1047                   goto drop2;
1048                 }
1049               if (is_unnum0)
1050                 arp_unnumbered (p0, pi0, eth0, ifa0);
1051               else
1052                 vlib_buffer_advance (p0, -adj0->rewrite_header.data_bytes);
1053             }
1054
1055           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1056                                            n_left_to_next, pi0, next0);
1057
1058           n_replies_sent += 1;
1059           continue;
1060
1061         drop1:
1062           if (0 == arp0->ip4_over_ethernet[0].ip4.as_u32 ||
1063               (arp0->ip4_over_ethernet[0].ip4.as_u32 ==
1064                arp0->ip4_over_ethernet[1].ip4.as_u32))
1065             {
1066               error0 = ETHERNET_ARP_ERROR_gratuitous_arp;
1067               goto drop2;
1068             }
1069           /* See if proxy arp is configured for the address */
1070           if (is_request0)
1071             {
1072               vnet_sw_interface_t *si;
1073               u32 this_addr = clib_net_to_host_u32
1074                 (arp0->ip4_over_ethernet[1].ip4.as_u32);
1075               u32 fib_index0;
1076
1077               si = vnet_get_sw_interface (vnm, sw_if_index0);
1078
1079               if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
1080                 goto drop2;
1081
1082               fib_index0 = vec_elt (im4->fib_index_by_sw_if_index,
1083                                     sw_if_index0);
1084
1085               vec_foreach (pa, am->proxy_arps)
1086               {
1087                 u32 lo_addr = clib_net_to_host_u32 (pa->lo_addr);
1088                 u32 hi_addr = clib_net_to_host_u32 (pa->hi_addr);
1089
1090                 /* an ARP request hit in the proxy-arp table? */
1091                 if ((this_addr >= lo_addr && this_addr <= hi_addr) &&
1092                     (fib_index0 == pa->fib_index))
1093                   {
1094                     eth0 = ethernet_buffer_get_header (p0);
1095                     proxy_src.as_u32 =
1096                       arp0->ip4_over_ethernet[1].ip4.data_u32;
1097
1098                     /* 
1099                      * Rewind buffer, direct code above not to
1100                      * think too hard about it. 
1101                      * $$$ is the answer ever anything other than
1102                      * vlib_buffer_reset(..)?
1103                      */
1104                     ifa0 = 0;
1105                     if_addr0 = &proxy_src;
1106                     vlib_buffer_reset (p0);
1107                     n_proxy_arp_replies_sent++;
1108                     goto send_reply;
1109                   }
1110               }
1111             }
1112
1113         drop2:
1114
1115           next0 = ARP_INPUT_NEXT_DROP;
1116           p0->error = node->errors[error0];
1117
1118           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1119                                            n_left_to_next, pi0, next0);
1120         }
1121
1122       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1123     }
1124
1125   vlib_error_count (vm, node->node_index,
1126                     ETHERNET_ARP_ERROR_replies_sent,
1127                     n_replies_sent - n_proxy_arp_replies_sent);
1128
1129   vlib_error_count (vm, node->node_index,
1130                     ETHERNET_ARP_ERROR_proxy_arp_replies_sent,
1131                     n_proxy_arp_replies_sent);
1132   return frame->n_vectors;
1133 }
1134
1135 static char *ethernet_arp_error_strings[] = {
1136 #define _(sym,string) string,
1137   foreach_ethernet_arp_error
1138 #undef _
1139 };
1140
1141 /* *INDENT-OFF* */
1142 VLIB_REGISTER_NODE (arp_input_node, static) =
1143 {
1144   .function = arp_input,
1145   .name = "arp-input",
1146   .vector_size = sizeof (u32),
1147   .n_errors = ETHERNET_ARP_N_ERROR,
1148   .error_strings = ethernet_arp_error_strings,
1149   .n_next_nodes = ARP_INPUT_N_NEXT,
1150   .next_nodes = {
1151     [ARP_INPUT_NEXT_DROP] = "error-drop",
1152     [ARP_INPUT_NEXT_REPLY_TX] = "interface-output",
1153   },
1154   .format_buffer = format_ethernet_arp_header,
1155   .format_trace = format_ethernet_arp_input_trace,
1156 };
1157 /* *INDENT-ON* */
1158
1159 static int
1160 ip4_arp_entry_sort (void *a1, void *a2)
1161 {
1162   ethernet_arp_ip4_entry_t *e1 = a1;
1163   ethernet_arp_ip4_entry_t *e2 = a2;
1164
1165   int cmp;
1166   vnet_main_t *vnm = vnet_get_main ();
1167
1168   cmp = vnet_sw_interface_compare
1169     (vnm, e1->key.sw_if_index, e2->key.sw_if_index);
1170   if (!cmp)
1171     cmp = ip4_address_compare (&e1->key.ip4_address, &e2->key.ip4_address);
1172   return cmp;
1173 }
1174
1175 static clib_error_t *
1176 show_ip4_arp (vlib_main_t * vm,
1177               unformat_input_t * input, vlib_cli_command_t * cmd)
1178 {
1179   vnet_main_t *vnm = vnet_get_main ();
1180   ethernet_arp_main_t *am = &ethernet_arp_main;
1181   ethernet_arp_ip4_entry_t *e, *es;
1182   ethernet_proxy_arp_t *pa;
1183   clib_error_t *error = 0;
1184   u32 sw_if_index;
1185
1186   /* Filter entries by interface if given. */
1187   sw_if_index = ~0;
1188   (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1189
1190   es = 0;
1191   /* *INDENT-OFF* */
1192   pool_foreach (e, am->ip4_entry_pool,
1193   ({
1194     vec_add1 (es, e[0]);
1195   }));
1196   /* *INDENT-ON* */
1197
1198   if (es)
1199     {
1200       vec_sort_with_function (es, ip4_arp_entry_sort);
1201       vlib_cli_output (vm, "%U", format_ethernet_arp_ip4_entry, vnm, 0);
1202       vec_foreach (e, es)
1203       {
1204         if (sw_if_index != ~0 && e->key.sw_if_index != sw_if_index)
1205           continue;
1206         vlib_cli_output (vm, "%U", format_ethernet_arp_ip4_entry, vnm, e);
1207       }
1208       vec_free (es);
1209     }
1210
1211   if (vec_len (am->proxy_arps))
1212     {
1213       vlib_cli_output (vm, "Proxy arps enabled for:");
1214       vec_foreach (pa, am->proxy_arps)
1215       {
1216         vlib_cli_output (vm, "Fib_index %d   %U - %U ",
1217                          pa->fib_index,
1218                          format_ip4_address, &pa->lo_addr,
1219                          format_ip4_address, &pa->hi_addr);
1220       }
1221     }
1222
1223   return error;
1224 }
1225
1226 /* *INDENT-OFF* */
1227 VLIB_CLI_COMMAND (show_ip4_arp_command, static) = {
1228   .path = "show ip arp",
1229   .function = show_ip4_arp,
1230   .short_help = "Show ARP table",
1231 };
1232 /* *INDENT-ON* */
1233
1234 typedef struct
1235 {
1236   pg_edit_t l2_type, l3_type;
1237   pg_edit_t n_l2_address_bytes, n_l3_address_bytes;
1238   pg_edit_t opcode;
1239   struct
1240   {
1241     pg_edit_t ethernet;
1242     pg_edit_t ip4;
1243   } ip4_over_ethernet[2];
1244 } pg_ethernet_arp_header_t;
1245
1246 static inline void
1247 pg_ethernet_arp_header_init (pg_ethernet_arp_header_t * p)
1248 {
1249   /* Initialize fields that are not bit fields in the IP header. */
1250 #define _(f) pg_edit_init (&p->f, ethernet_arp_header_t, f);
1251   _(l2_type);
1252   _(l3_type);
1253   _(n_l2_address_bytes);
1254   _(n_l3_address_bytes);
1255   _(opcode);
1256   _(ip4_over_ethernet[0].ethernet);
1257   _(ip4_over_ethernet[0].ip4);
1258   _(ip4_over_ethernet[1].ethernet);
1259   _(ip4_over_ethernet[1].ip4);
1260 #undef _
1261 }
1262
1263 uword
1264 unformat_pg_arp_header (unformat_input_t * input, va_list * args)
1265 {
1266   pg_stream_t *s = va_arg (*args, pg_stream_t *);
1267   pg_ethernet_arp_header_t *p;
1268   u32 group_index;
1269
1270   p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ethernet_arp_header_t),
1271                             &group_index);
1272   pg_ethernet_arp_header_init (p);
1273
1274   /* Defaults. */
1275   pg_edit_set_fixed (&p->l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet);
1276   pg_edit_set_fixed (&p->l3_type, ETHERNET_TYPE_IP4);
1277   pg_edit_set_fixed (&p->n_l2_address_bytes, 6);
1278   pg_edit_set_fixed (&p->n_l3_address_bytes, 4);
1279
1280   if (!unformat (input, "%U: %U/%U -> %U/%U",
1281                  unformat_pg_edit,
1282                  unformat_ethernet_arp_opcode_net_byte_order, &p->opcode,
1283                  unformat_pg_edit,
1284                  unformat_ethernet_address, &p->ip4_over_ethernet[0].ethernet,
1285                  unformat_pg_edit,
1286                  unformat_ip4_address, &p->ip4_over_ethernet[0].ip4,
1287                  unformat_pg_edit,
1288                  unformat_ethernet_address, &p->ip4_over_ethernet[1].ethernet,
1289                  unformat_pg_edit,
1290                  unformat_ip4_address, &p->ip4_over_ethernet[1].ip4))
1291     {
1292       /* Free up any edits we may have added. */
1293       pg_free_edit_group (s);
1294       return 0;
1295     }
1296   return 1;
1297 }
1298
1299 clib_error_t *
1300 ip4_set_arp_limit (u32 arp_limit)
1301 {
1302   ethernet_arp_main_t *am = &ethernet_arp_main;
1303
1304   am->limit_arp_cache_size = arp_limit;
1305   return 0;
1306 }
1307
1308 static void
1309 arp_ip4_entry_del_adj (ethernet_arp_ip4_entry_t * e, u32 adj_index)
1310 {
1311   int done = 0;
1312   int i;
1313
1314   while (!done)
1315     {
1316       vec_foreach_index (i, e->adjacencies)
1317         if (vec_elt (e->adjacencies, i) == adj_index)
1318         {
1319           vec_del1 (e->adjacencies, i);
1320           continue;
1321         }
1322       done = 1;
1323     }
1324 }
1325
1326 static void
1327 arp_ip4_entry_add_adj (ethernet_arp_ip4_entry_t * e, u32 adj_index)
1328 {
1329   int i;
1330   vec_foreach_index (i, e->adjacencies)
1331     if (vec_elt (e->adjacencies, i) == adj_index)
1332     return;
1333   vec_add1 (e->adjacencies, adj_index);
1334 }
1335
1336 static void
1337 arp_add_del_adj_cb (struct ip_lookup_main_t *lm,
1338                     u32 adj_index, ip_adjacency_t * adj, u32 is_del)
1339 {
1340   ethernet_arp_main_t *am = &ethernet_arp_main;
1341   ip4_main_t *im = &ip4_main;
1342   ethernet_arp_ip4_key_t k;
1343   ethernet_arp_ip4_entry_t *e = 0;
1344   uword *p;
1345   u32 ai;
1346
1347   for (ai = adj->heap_handle; ai < adj->heap_handle + adj->n_adj; ai++)
1348     {
1349       adj = ip_get_adjacency (lm, ai);
1350       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
1351           && adj->arp.next_hop.ip4.as_u32)
1352         {
1353           k.sw_if_index = adj->rewrite_header.sw_if_index;
1354           k.ip4_address.as_u32 = adj->arp.next_hop.ip4.as_u32;
1355           k.fib_index =
1356             im->fib_index_by_sw_if_index[adj->rewrite_header.sw_if_index];
1357           p = mhash_get (&am->ip4_entry_by_key, &k);
1358           if (p)
1359             e = pool_elt_at_index (am->ip4_entry_pool, p[0]);
1360         }
1361       else
1362         continue;
1363
1364       if (is_del)
1365         {
1366           if (!e)
1367             clib_warning ("Adjacency contains unknown ARP next hop %U (del)",
1368                           format_ip46_address, &adj->arp.next_hop,
1369                           IP46_TYPE_IP4);
1370           else
1371             arp_ip4_entry_del_adj (e, adj->heap_handle);
1372         }
1373       else                      /* add */
1374         {
1375           if (!e)
1376             clib_warning ("Adjacency contains unknown ARP next hop %U (add)",
1377                           format_ip46_address, &adj->arp.next_hop,
1378                           IP46_TYPE_IP4);
1379           else
1380             arp_ip4_entry_add_adj (e, adj->heap_handle);
1381         }
1382     }
1383 }
1384
1385 static clib_error_t *
1386 ethernet_arp_init (vlib_main_t * vm)
1387 {
1388   ethernet_arp_main_t *am = &ethernet_arp_main;
1389   pg_node_t *pn;
1390   clib_error_t *error;
1391   ip4_main_t *im = &ip4_main;
1392   ip_lookup_main_t *lm = &im->lookup_main;
1393
1394   if ((error = vlib_call_init_function (vm, ethernet_init)))
1395     return error;
1396
1397   ethernet_register_input_type (vm, ETHERNET_TYPE_ARP, arp_input_node.index);
1398
1399   pn = pg_get_node (arp_input_node.index);
1400   pn->unformat_edit = unformat_pg_arp_header;
1401
1402   am->opcode_by_name = hash_create_string (0, sizeof (uword));
1403 #define _(o) hash_set_mem (am->opcode_by_name, #o, ETHERNET_ARP_OPCODE_##o);
1404   foreach_ethernet_arp_opcode;
1405 #undef _
1406
1407   mhash_init (&am->ip4_entry_by_key,
1408               /* value size */ sizeof (uword),
1409               /* key size */ sizeof (ethernet_arp_ip4_key_t));
1410
1411   /* $$$ configurable */
1412   am->limit_arp_cache_size = 50000;
1413
1414   am->pending_resolutions_by_address = hash_create (0, sizeof (uword));
1415   am->mac_changes_by_address = hash_create (0, sizeof (uword));
1416
1417   /* don't trace ARP error packets */
1418   {
1419     vlib_node_runtime_t *rt =
1420       vlib_node_get_runtime (vm, arp_input_node.index);
1421
1422 #define _(a,b)                                  \
1423     vnet_pcap_drop_trace_filter_add_del         \
1424         (rt->errors[ETHERNET_ARP_ERROR_##a],    \
1425          1 /* is_add */);
1426     foreach_ethernet_arp_error
1427 #undef _
1428   }
1429
1430   ip_register_add_del_adjacency_callback (lm, arp_add_del_adj_cb);
1431
1432   return 0;
1433 }
1434
1435 VLIB_INIT_FUNCTION (ethernet_arp_init);
1436
1437 int
1438 vnet_arp_unset_ip4_over_ethernet (vnet_main_t * vnm,
1439                                   u32 sw_if_index, u32 fib_index, void *a_arg)
1440 {
1441   ethernet_arp_ip4_over_ethernet_address_t *a = a_arg;
1442   vnet_arp_set_ip4_over_ethernet_rpc_args_t args;
1443
1444   args.sw_if_index = sw_if_index;
1445   args.fib_index = fib_index;
1446   args.is_remove = 1;
1447   clib_memcpy (&args.a, a, sizeof (*a));
1448
1449   vl_api_rpc_call_main_thread (set_ip4_over_ethernet_rpc_callback,
1450                                (u8 *) & args, sizeof (args));
1451   return 0;
1452 }
1453
1454 static inline int
1455 vnet_arp_unset_ip4_over_ethernet_internal (vnet_main_t * vnm,
1456                                            u32 sw_if_index,
1457                                            u32 fib_index, void *a_arg)
1458 {
1459   ethernet_arp_ip4_entry_t *e;
1460   ethernet_arp_main_t *am = &ethernet_arp_main;
1461   ethernet_arp_ip4_over_ethernet_address_t *a = a_arg;
1462   ethernet_arp_ip4_key_t k;
1463   uword *p;
1464   ip4_add_del_route_args_t args;
1465   ip4_main_t *im = &ip4_main;
1466   ip_lookup_main_t *lm = &im->lookup_main;
1467   u32 adj_index;
1468   ip_adjacency_t *adj;
1469
1470   k.sw_if_index = sw_if_index;
1471   k.ip4_address = a->ip4;
1472   k.fib_index = fib_index;
1473   p = mhash_get (&am->ip4_entry_by_key, &k);
1474   if (!p)
1475     return -1;
1476
1477   memset (&args, 0, sizeof (args));
1478
1479   /* 
1480    * Make sure that the route actually exists before we try to delete it,
1481    * and make sure that it's a rewrite adjacency.
1482    *
1483    * If we point 1-N unnumbered interfaces at a loopback interface and 
1484    * shut down the loopback before shutting down 1-N unnumbered 
1485    * interfaces, the ARP cache will still have an entry, 
1486    * but the route will have disappeared.
1487    * 
1488    * See also ip4_del_interface_routes (...) 
1489    *            -> ip4_delete_matching_routes (...).
1490    */
1491
1492   adj_index = ip4_fib_lookup_with_table
1493     (im, fib_index, &a->ip4, 1 /* disable default route */ );
1494
1495   /* Miss adj? Forget it... */
1496   if (adj_index != lm->miss_adj_index)
1497     {
1498       adj = ip_get_adjacency (lm, adj_index);
1499       /* 
1500        * Stupid control-plane trick:
1501        * admin down an interface (removes arp routes from fib),
1502        * bring the interface back up (does not reinstall them)
1503        * then remove the arp cache entry (yuck). When that happens,
1504        * the adj we find here will be the interface subnet ARP adj.
1505        */
1506       if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1507         {
1508           args.table_index_or_table_id = fib_index;
1509           args.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL
1510             | IP4_ROUTE_FLAG_NEIGHBOR;
1511           args.dst_address = a->ip4;
1512           args.dst_address_length = 32;
1513           ip4_add_del_route (im, &args);
1514           ip4_maybe_remap_adjacencies (im, fib_index, args.flags);
1515         }
1516     }
1517
1518   e = pool_elt_at_index (am->ip4_entry_pool, p[0]);
1519   mhash_unset (&am->ip4_entry_by_key, &e->key, 0);
1520   pool_put (am->ip4_entry_pool, e);
1521   return 0;
1522 }
1523
1524 static void
1525 increment_ip4_and_mac_address (ethernet_arp_ip4_over_ethernet_address_t * a)
1526 {
1527   u8 old;
1528   int i;
1529
1530   for (i = 3; i >= 0; i--)
1531     {
1532       old = a->ip4.as_u8[i];
1533       a->ip4.as_u8[i] += 1;
1534       if (old < a->ip4.as_u8[i])
1535         break;
1536     }
1537
1538   for (i = 5; i >= 0; i--)
1539     {
1540       old = a->ethernet[i];
1541       a->ethernet[i] += 1;
1542       if (old < a->ethernet[i])
1543         break;
1544     }
1545 }
1546
1547 int
1548 vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1549                         ip4_address_t * hi_addr, u32 fib_index, int is_del)
1550 {
1551   ethernet_arp_main_t *am = &ethernet_arp_main;
1552   ethernet_proxy_arp_t *pa;
1553   u32 found_at_index = ~0;
1554
1555   vec_foreach (pa, am->proxy_arps)
1556   {
1557     if (pa->lo_addr == lo_addr->as_u32
1558         && pa->hi_addr == hi_addr->as_u32 && pa->fib_index == fib_index)
1559       {
1560         found_at_index = pa - am->proxy_arps;
1561         break;
1562       }
1563   }
1564
1565   if (found_at_index != ~0)
1566     {
1567       /* Delete, otherwise it's already in the table */
1568       if (is_del)
1569         vec_delete (am->proxy_arps, 1, found_at_index);
1570       return 0;
1571     }
1572   /* delete, no such entry */
1573   if (is_del)
1574     return VNET_API_ERROR_NO_SUCH_ENTRY;
1575
1576   /* add, not in table */
1577   vec_add2 (am->proxy_arps, pa, 1);
1578   pa->lo_addr = lo_addr->as_u32;
1579   pa->hi_addr = hi_addr->as_u32;
1580   pa->fib_index = fib_index;
1581   return 0;
1582 }
1583
1584 /*
1585  * Remove any proxy arp entries asdociated with the 
1586  * specificed fib.
1587  */
1588 int
1589 vnet_proxy_arp_fib_reset (u32 fib_id)
1590 {
1591   ip4_main_t *im = &ip4_main;
1592   ethernet_arp_main_t *am = &ethernet_arp_main;
1593   ethernet_proxy_arp_t *pa;
1594   u32 *entries_to_delete = 0;
1595   u32 fib_index;
1596   uword *p;
1597   int i;
1598
1599   p = hash_get (im->fib_index_by_table_id, fib_id);
1600   if (!p)
1601     return VNET_API_ERROR_NO_SUCH_ENTRY;
1602   fib_index = p[0];
1603
1604   vec_foreach (pa, am->proxy_arps)
1605   {
1606     if (pa->fib_index == fib_index)
1607       {
1608         vec_add1 (entries_to_delete, pa - am->proxy_arps);
1609       }
1610   }
1611
1612   for (i = 0; i < vec_len (entries_to_delete); i++)
1613     {
1614       vec_delete (am->proxy_arps, 1, entries_to_delete[i]);
1615     }
1616
1617   vec_free (entries_to_delete);
1618
1619   return 0;
1620 }
1621
1622 u32
1623 vnet_arp_glean_add (u32 fib_index, void *next_hop_arg)
1624 {
1625   ethernet_arp_main_t *am = &ethernet_arp_main;
1626   ip4_main_t *im = &ip4_main;
1627   ip_lookup_main_t *lm = &im->lookup_main;
1628   ip4_address_t *next_hop = next_hop_arg;
1629   ip_adjacency_t add_adj, *adj;
1630   ip4_add_del_route_args_t args;
1631   ethernet_arp_ip4_entry_t *e;
1632   ethernet_arp_ip4_key_t k;
1633   u32 adj_index;
1634
1635   adj_index = ip4_fib_lookup_with_table (im, fib_index, next_hop, 0);
1636   adj = ip_get_adjacency (lm, adj_index);
1637
1638   if (!adj || adj->lookup_next_index != IP_LOOKUP_NEXT_ARP)
1639     return ~0;
1640
1641   if (adj->arp.next_hop.ip4.as_u32 != 0)
1642     return adj_index;
1643
1644   k.sw_if_index = adj->rewrite_header.sw_if_index;
1645   k.fib_index = fib_index;
1646   k.ip4_address.as_u32 = next_hop->as_u32;
1647
1648   if (mhash_get (&am->ip4_entry_by_key, &k))
1649     return adj_index;
1650
1651   pool_get (am->ip4_entry_pool, e);
1652   mhash_set (&am->ip4_entry_by_key, &k, e - am->ip4_entry_pool,
1653              /* old value */ 0);
1654   e->key = k;
1655   e->cpu_time_last_updated = clib_cpu_time_now ();
1656   e->flags = ETHERNET_ARP_IP4_ENTRY_FLAG_GLEAN;
1657
1658   memset (&args, 0, sizeof (args));
1659   clib_memcpy (&add_adj, adj, sizeof (add_adj));
1660   ip46_address_set_ip4 (&add_adj.arp.next_hop, next_hop);       /* install neighbor /32 route */
1661   args.table_index_or_table_id = fib_index;
1662   args.flags =
1663     IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_ADD | IP4_ROUTE_FLAG_NEIGHBOR;
1664   args.dst_address.as_u32 = next_hop->as_u32;
1665   args.dst_address_length = 32;
1666   args.adj_index = ~0;
1667   args.add_adj = &add_adj;
1668   args.n_add_adj = 1;
1669   ip4_add_del_route (im, &args);
1670   return ip4_fib_lookup_with_table (im, fib_index, next_hop, 0);
1671 }
1672
1673 static clib_error_t *
1674 ip_arp_add_del_command_fn (vlib_main_t * vm,
1675                            unformat_input_t * input, vlib_cli_command_t * cmd)
1676 {
1677   vnet_main_t *vnm = vnet_get_main ();
1678   u32 sw_if_index;
1679   ethernet_arp_ip4_over_ethernet_address_t lo_addr, hi_addr, addr;
1680   int addr_valid = 0;
1681   int is_del = 0;
1682   int count = 1;
1683   u32 fib_index = 0;
1684   u32 fib_id;
1685   int is_static = 0;
1686   int is_proxy = 0;
1687
1688   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1689     {
1690       /* set ip arp TenGigE1/1/0/1 1.2.3.4 aa:bb:... or aabb.ccdd... */
1691       if (unformat (input, "%U %U %U",
1692                     unformat_vnet_sw_interface, vnm, &sw_if_index,
1693                     unformat_ip4_address, &addr.ip4,
1694                     unformat_ethernet_address, &addr.ethernet))
1695         addr_valid = 1;
1696
1697       else if (unformat (input, "delete") || unformat (input, "del"))
1698         is_del = 1;
1699
1700       else if (unformat (input, "static"))
1701         is_static = 1;
1702
1703       else if (unformat (input, "count %d", &count))
1704         ;
1705
1706       else if (unformat (input, "fib-id %d", &fib_id))
1707         {
1708           ip4_main_t *im = &ip4_main;
1709           uword *p = hash_get (im->fib_index_by_table_id, fib_id);
1710           if (!p)
1711             return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
1712           fib_index = p[0];
1713         }
1714
1715       else if (unformat (input, "proxy %U - %U",
1716                          unformat_ip4_address, &lo_addr.ip4,
1717                          unformat_ip4_address, &hi_addr.ip4))
1718         is_proxy = 1;
1719       else
1720         break;
1721     }
1722
1723   if (is_proxy)
1724     {
1725       (void) vnet_proxy_arp_add_del (&lo_addr.ip4, &hi_addr.ip4,
1726                                      fib_index, is_del);
1727       return 0;
1728     }
1729
1730   if (addr_valid)
1731     {
1732       int i;
1733
1734       for (i = 0; i < count; i++)
1735         {
1736           if (is_del == 0)
1737             {
1738               uword event_type, *event_data = 0;
1739
1740               /* Park the debug CLI until the arp entry is installed */
1741               vnet_register_ip4_arp_resolution_event
1742                 (vnm, &addr.ip4, vlib_current_process (vm),
1743                  1 /* type */ , 0 /* data */ );
1744
1745               vnet_arp_set_ip4_over_ethernet
1746                 (vnm, sw_if_index, fib_index, &addr, is_static);
1747
1748               vlib_process_wait_for_event (vm);
1749               event_type = vlib_process_get_events (vm, &event_data);
1750               vec_reset_length (event_data);
1751               if (event_type != 1)
1752                 clib_warning ("event type %d unexpected", event_type);
1753             }
1754           else
1755             vnet_arp_unset_ip4_over_ethernet
1756               (vnm, sw_if_index, fib_index, &addr);
1757
1758           increment_ip4_and_mac_address (&addr);
1759         }
1760     }
1761   else
1762     {
1763       return clib_error_return (0, "unknown input `%U'",
1764                                 format_unformat_error, input);
1765     }
1766
1767   return 0;
1768 }
1769
1770 /* *INDENT-OFF* */
1771 VLIB_CLI_COMMAND (ip_arp_add_del_command, static) = {
1772   .path = "set ip arp",
1773   .short_help =
1774     "set ip arp [del] <intfc> <ip-address> <mac-address> [static] [count <count>] [fib-id <fib-id>] [proxy <lo-addr> - <hi-addr>]",
1775   .function = ip_arp_add_del_command_fn,
1776 };
1777 /* *INDENT-ON* */
1778
1779 static clib_error_t *
1780 set_int_proxy_arp_command_fn (vlib_main_t * vm,
1781                               unformat_input_t * input,
1782                               vlib_cli_command_t * cmd)
1783 {
1784   vnet_main_t *vnm = vnet_get_main ();
1785   u32 sw_if_index;
1786   vnet_sw_interface_t *si;
1787   int enable = 0;
1788   int intfc_set = 0;
1789
1790   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1791     {
1792       if (unformat (input, "%U", unformat_vnet_sw_interface,
1793                     vnm, &sw_if_index))
1794         intfc_set = 1;
1795       else if (unformat (input, "enable") || unformat (input, "on"))
1796         enable = 1;
1797       else if (unformat (input, "disable") || unformat (input, "off"))
1798         enable = 0;
1799       else
1800         break;
1801     }
1802
1803   if (intfc_set == 0)
1804     return clib_error_return (0, "unknown input '%U'",
1805                               format_unformat_error, input);
1806
1807   si = vnet_get_sw_interface (vnm, sw_if_index);
1808   ASSERT (si);
1809   if (enable)
1810     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1811   else
1812     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1813
1814   return 0;
1815 }
1816
1817 /* *INDENT-OFF* */
1818 VLIB_CLI_COMMAND (set_int_proxy_enable_command, static) = {
1819   .path = "set interface proxy-arp",
1820   .short_help =
1821     "set interface proxy-arp <intfc> [enable|disable]",
1822   .function = set_int_proxy_arp_command_fn,
1823 };
1824 /* *INDENT-ON* */
1825
1826
1827 /*
1828  * ARP Termination in a L2 Bridge Domain based on an
1829  * IP4 to MAC hash table mac_by_ip4 for each BD.
1830  */
1831 typedef enum
1832 {
1833   ARP_TERM_NEXT_L2_OUTPUT,
1834   ARP_TERM_NEXT_DROP,
1835   ARP_TERM_N_NEXT,
1836 } arp_term_next_t;
1837
1838 u32 arp_term_next_node_index[32];
1839
1840 static uword
1841 arp_term_l2bd (vlib_main_t * vm,
1842                vlib_node_runtime_t * node, vlib_frame_t * frame)
1843 {
1844   l2input_main_t *l2im = &l2input_main;
1845   u32 n_left_from, next_index, *from, *to_next;
1846   u32 n_replies_sent = 0;
1847   u16 last_bd_index = ~0;
1848   l2_bridge_domain_t *last_bd_config = 0;
1849   l2_input_config_t *cfg0;
1850
1851   from = vlib_frame_vector_args (frame);
1852   n_left_from = frame->n_vectors;
1853   next_index = node->cached_next_index;
1854
1855   while (n_left_from > 0)
1856     {
1857       u32 n_left_to_next;
1858
1859       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1860
1861       while (n_left_from > 0 && n_left_to_next > 0)
1862         {
1863           vlib_buffer_t *p0;
1864           ethernet_header_t *eth0;
1865           ethernet_arp_header_t *arp0;
1866           u8 *l3h0;
1867           u32 pi0, error0, next0, sw_if_index0;
1868           u16 ethertype0;
1869           u16 bd_index0;
1870           u32 ip0;
1871           u8 *macp0;
1872
1873           pi0 = from[0];
1874           to_next[0] = pi0;
1875           from += 1;
1876           to_next += 1;
1877           n_left_from -= 1;
1878           n_left_to_next -= 1;
1879
1880           p0 = vlib_get_buffer (vm, pi0);
1881           eth0 = vlib_buffer_get_current (p0);
1882           l3h0 = (u8 *) eth0 + vnet_buffer (p0)->l2.l2_len;
1883           ethertype0 = clib_net_to_host_u16 (*(u16 *) (l3h0 - 2));
1884           arp0 = (ethernet_arp_header_t *) l3h0;
1885
1886           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
1887                              (p0->flags & VLIB_BUFFER_IS_TRACED)))
1888             {
1889               u8 *t0 = vlib_add_trace (vm, node, p0,
1890                                        sizeof (ethernet_arp_input_trace_t));
1891               clib_memcpy (t0, l3h0, sizeof (ethernet_arp_input_trace_t));
1892             }
1893
1894           if (PREDICT_FALSE ((ethertype0 != ETHERNET_TYPE_ARP) ||
1895                              (arp0->opcode !=
1896                               clib_host_to_net_u16
1897                               (ETHERNET_ARP_OPCODE_request))))
1898             goto next_l2_feature;
1899
1900           error0 = ETHERNET_ARP_ERROR_replies_sent;
1901           error0 =
1902             (arp0->l2_type !=
1903              clib_net_to_host_u16 (ETHERNET_ARP_HARDWARE_TYPE_ethernet) ?
1904              ETHERNET_ARP_ERROR_l2_type_not_ethernet : error0);
1905           error0 =
1906             (arp0->l3_type !=
1907              clib_net_to_host_u16 (ETHERNET_TYPE_IP4) ?
1908              ETHERNET_ARP_ERROR_l3_type_not_ip4 : error0);
1909
1910           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1911
1912           if (error0)
1913             goto drop;
1914
1915           // Trash ARP packets whose ARP-level source addresses do not
1916           // match their L2-frame-level source addresses */
1917           if (PREDICT_FALSE
1918               (memcmp
1919                (eth0->src_address, arp0->ip4_over_ethernet[0].ethernet,
1920                 sizeof (eth0->src_address))))
1921             {
1922               error0 = ETHERNET_ARP_ERROR_l2_address_mismatch;
1923               goto drop;
1924             }
1925
1926           // Check if anyone want ARP request events for L2 BDs
1927           {
1928             pending_resolution_t *mc;
1929             ethernet_arp_main_t *am = &ethernet_arp_main;
1930             uword *p = hash_get (am->mac_changes_by_address, 0);
1931             if (p && (vnet_buffer (p0)->l2.shg == 0))
1932               {                 // Only SHG 0 interface which is more likely local
1933                 u32 next_index = p[0];
1934                 while (next_index != (u32) ~ 0)
1935                   {
1936                     int (*fp) (u32, u8 *, u32, u32);
1937                     int rv = 1;
1938                     mc = pool_elt_at_index (am->mac_changes, next_index);
1939                     fp = mc->data_callback;
1940                     // Call the callback, return 1 to suppress dup events */
1941                     if (fp)
1942                       rv = (*fp) (mc->data,
1943                                   arp0->ip4_over_ethernet[0].ethernet,
1944                                   sw_if_index0,
1945                                   arp0->ip4_over_ethernet[0].ip4.as_u32);
1946                     // Signal the resolver process
1947                     if (rv == 0)
1948                       vlib_process_signal_event (vm, mc->node_index,
1949                                                  mc->type_opaque, mc->data);
1950                     next_index = mc->next_index;
1951                   }
1952               }
1953           }
1954
1955           // lookup BD mac_by_ip4 hash table for MAC entry
1956           ip0 = arp0->ip4_over_ethernet[1].ip4.as_u32;
1957           bd_index0 = vnet_buffer (p0)->l2.bd_index;
1958           if (PREDICT_FALSE ((bd_index0 != last_bd_index)
1959                              || (last_bd_index == (u16) ~ 0)))
1960             {
1961               last_bd_index = bd_index0;
1962               last_bd_config = vec_elt_at_index (l2im->bd_configs, bd_index0);
1963             }
1964           macp0 = (u8 *) hash_get (last_bd_config->mac_by_ip4, ip0);
1965
1966           if (PREDICT_FALSE (!macp0))
1967             goto next_l2_feature;       // MAC not found 
1968
1969           // MAC found, send ARP reply -
1970           // Convert ARP request packet to ARP reply
1971           arp0->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
1972           arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
1973           arp0->ip4_over_ethernet[0].ip4.as_u32 = ip0;
1974           clib_memcpy (arp0->ip4_over_ethernet[0].ethernet, macp0, 6);
1975           clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1976           clib_memcpy (eth0->src_address, macp0, 6);
1977           n_replies_sent += 1;
1978
1979           // For BVI, need to use l2-fwd node to send ARP reply as 
1980           // l2-output node cannot output packet to BVI properly
1981           cfg0 = vec_elt_at_index (l2im->configs, sw_if_index0);
1982           if (PREDICT_FALSE (cfg0->bvi))
1983             {
1984               vnet_buffer (p0)->l2.feature_bitmap |= L2INPUT_FEAT_FWD;
1985               vnet_buffer (p0)->sw_if_index[VLIB_RX] = 0;
1986               goto next_l2_feature;
1987             }
1988
1989           // Send ARP reply back out input interface through l2-output
1990           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1991           next0 = ARP_TERM_NEXT_L2_OUTPUT;
1992           // Note that output to VXLAN tunnel will fail due to SHG which
1993           // is probably desireable since ARP termination is not intended
1994           // for ARP requests from other hosts. If output to VXLAN tunnel is
1995           // required, however, can just clear the SHG in packet as follows:
1996           //   vnet_buffer(p0)->l2.shg = 0;
1997
1998           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1999                                            n_left_to_next, pi0, next0);
2000           continue;
2001
2002         next_l2_feature:
2003           {
2004             u32 feature_bitmap0 =
2005               vnet_buffer (p0)->l2.feature_bitmap & ~L2INPUT_FEAT_ARP_TERM;
2006             vnet_buffer (p0)->l2.feature_bitmap = feature_bitmap0;
2007             next0 = feat_bitmap_get_next_node_index (arp_term_next_node_index,
2008                                                      feature_bitmap0);
2009             vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2010                                              n_left_to_next, pi0, next0);
2011             continue;
2012           }
2013
2014         drop:
2015           if (0 == arp0->ip4_over_ethernet[0].ip4.as_u32 ||
2016               (arp0->ip4_over_ethernet[0].ip4.as_u32 ==
2017                arp0->ip4_over_ethernet[1].ip4.as_u32))
2018             {
2019               error0 = ETHERNET_ARP_ERROR_gratuitous_arp;
2020             }
2021           next0 = ARP_TERM_NEXT_DROP;
2022           p0->error = node->errors[error0];
2023
2024           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2025                                            n_left_to_next, pi0, next0);
2026         }
2027
2028       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2029     }
2030
2031   vlib_error_count (vm, node->node_index,
2032                     ETHERNET_ARP_ERROR_replies_sent, n_replies_sent);
2033   return frame->n_vectors;
2034 }
2035
2036 /* *INDENT-OFF* */
2037 VLIB_REGISTER_NODE (arp_term_l2bd_node, static) = {
2038   .function = arp_term_l2bd,
2039   .name = "arp-term-l2bd",
2040   .vector_size = sizeof (u32),
2041   .n_errors = ETHERNET_ARP_N_ERROR,
2042   .error_strings = ethernet_arp_error_strings,
2043   .n_next_nodes = ARP_TERM_N_NEXT,
2044   .next_nodes = {
2045     [ARP_TERM_NEXT_L2_OUTPUT] = "l2-output",
2046     [ARP_TERM_NEXT_DROP] = "error-drop",
2047   },
2048   .format_buffer = format_ethernet_arp_header,
2049   .format_trace = format_ethernet_arp_input_trace,
2050 };
2051 /* *INDENT-ON* */
2052
2053 clib_error_t *
2054 arp_term_init (vlib_main_t * vm)
2055 {                               // Initialize the feature next-node indexes 
2056   feat_bitmap_init_next_nodes (vm,
2057                                arp_term_l2bd_node.index,
2058                                L2INPUT_N_FEAT,
2059                                l2input_get_feat_names (),
2060                                arp_term_next_node_index);
2061   return 0;
2062 }
2063
2064 VLIB_INIT_FUNCTION (arp_term_init);
2065
2066 /*
2067  * fd.io coding-style-patch-verification: ON
2068  *
2069  * Local Variables:
2070  * eval: (c-set-style "gnu")
2071  * End:
2072  */