http: fix client parse error handling
[vpp.git] / src / plugins / vrrp / node.c
1 /*
2  * node.c - vrrp packet handling node definitions
3  *
4  * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  */
9 #include <vlib/vlib.h>
10 #include <vlibmemory/api.h>
11 #include <vnet/vnet.h>
12 #include <vnet/ip/ip4_packet.h>
13 #include <vnet/ip/ip6_link.h>
14 #include <vnet/ethernet/arp_packet.h>
15 #include <vnet/fib/fib_sas.h>
16 #include <vppinfra/error.h>
17 #include <vrrp/vrrp.h>
18 #include <vrrp/vrrp_packet.h>
19
20 typedef struct
21 {
22   u32 sw_if_index;
23   u8 is_ipv6;
24   vrrp_header_t vrrp;
25   u8 addrs[256];                /* print up to 64 IPv4 or 16 IPv6 addresses */
26 } vrrp_trace_t;
27
28 /* packet trace format function */
29 static u8 *
30 format_vrrp_trace (u8 * s, va_list * args)
31 {
32   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34   vrrp_trace_t *t = va_arg (*args, vrrp_trace_t *);
35   int i;
36
37   s = format (s, "VRRP: sw_if_index %d IPv%d\n",
38               t->sw_if_index, (t->is_ipv6) ? 6 : 4);
39   s = format (s, "    %U\n", format_vrrp_packet_hdr, &t->vrrp);
40   s = format (s, "    addresses: ");
41
42   for (i = 0; i < t->vrrp.n_addrs; i++)
43     {
44       if (t->is_ipv6)
45         s = format (s, "%U ", format_ip6_address,
46                     (ip6_address_t *) (t->addrs + i * 16));
47       else
48         s = format (s, "%U ", format_ip4_address,
49                     (ip4_address_t *) (t->addrs + i * 4));
50     }
51
52   return s;
53 }
54
55 extern vlib_node_registration_t vrrp4_input_node;
56 extern vlib_node_registration_t vrrp6_input_node;
57 extern vlib_node_registration_t vrrp4_arp_input_node;
58 extern vlib_node_registration_t vrrp6_nd_input_node;
59
60 #define foreach_vrrp_error                                        \
61 _(RECEIVED, "VRRP packets processed")                             \
62 _(BAD_TTL, "VRRP advertisement TTL is not 255")                   \
63 _(NOT_VERSION_3, "VRRP version is not 3")                         \
64 _(INCOMPLETE_PKT, "VRRP packet has wrong size")                   \
65 _(BAD_CHECKSUM, "VRRP checksum is invalid")                       \
66 _(UNKNOWN_VR, "VRRP message does not match known VRs")            \
67 _(ADDR_MISMATCH, "VR addrs do not match configuration")
68
69 typedef enum
70 {
71 #define _(sym,str) VRRP_ERROR_##sym,
72   foreach_vrrp_error
73 #undef _
74     VRRP_N_ERROR,
75 } vrrp_error_t;
76
77 static char *vrrp_error_strings[] = {
78 #define _(sym,string) string,
79   foreach_vrrp_error
80 #undef _
81 };
82
83 typedef enum
84 {
85   VRRP_INPUT_NEXT_DROP,
86   VRRP_INPUT_N_NEXT,
87 } vrrp_next_t;
88
89 /* Given a VR and a pointer to the VRRP header of an incoming packet,
90  * compare the local src address to the peers. Return < 0 if the local
91  * address < the peer address, 0 if they're equal, > 0 if
92  * the local address > the peer address
93  */
94 static int
95 vrrp_vr_addr_cmp (vrrp_vr_t *vr, ip46_address_t *peer_addr)
96 {
97   vrrp_vr_config_t *vrc = &vr->config;
98   void *peer_addr_bytes, *local_addr;
99   ip46_address_t addr;
100   int addr_size;
101
102   clib_memset (&addr, 0, sizeof (addr));
103
104   if (vrrp_vr_is_ipv6 (vr))
105     {
106       peer_addr_bytes = &peer_addr->ip6;
107       local_addr = &addr.ip6;
108       addr_size = 16;
109       ip6_address_copy (local_addr,
110                         ip6_get_link_local_address (vrc->sw_if_index));
111     }
112   else
113     {
114       peer_addr_bytes = &peer_addr->ip4;
115       local_addr = &addr.ip4;
116       addr_size = 4;
117       fib_sas4_get (vrc->sw_if_index, NULL, local_addr);
118     }
119
120   return memcmp (local_addr, peer_addr_bytes, addr_size);
121 }
122
123 static void
124 vrrp_input_process_master (vrrp_vr_t *vr, vrrp_input_process_args_t *args)
125 {
126   /* received priority 0, another VR is shutting down. send an adv and
127    * remain in the master state
128    */
129   if (args->priority == 0)
130     {
131       clib_warning ("Received shutdown message from a peer on VR %U",
132                     format_vrrp_vr_key, vr);
133       vrrp_incr_stat_counter (VRRP_STAT_COUNTER_PRIO0_RCVD, vr->stat_index);
134       vrrp_adv_send (vr, 0);
135       vrrp_vr_timer_set (vr, VRRP_VR_TIMER_ADV);
136       return;
137     }
138
139   /* if either:
140    * - received priority > adjusted priority, or
141    * - received priority == adjusted priority and peer addr > local addr
142    * allow the local VR to be preempted by the peer
143    */
144   if ((args->priority > vrrp_vr_priority (vr)) ||
145       ((args->priority == vrrp_vr_priority (vr)) &&
146        (vrrp_vr_addr_cmp (vr, &args->src_addr) < 0)))
147     {
148       vrrp_vr_transition (vr, VRRP_VR_STATE_BACKUP, args);
149
150       return;
151     }
152
153   /* if we made it this far, eiher received prority < adjusted priority or
154    * received == adjusted and local addr > peer addr. Ignore.
155    */
156   return;
157 }
158
159 /* RFC 5798 section 6.4.2 */
160 static void
161 vrrp_input_process_backup (vrrp_vr_t *vr, vrrp_input_process_args_t *args)
162 {
163   vrrp_vr_config_t *vrc = &vr->config;
164   vrrp_vr_runtime_t *vrt = &vr->runtime;
165
166   /* master shutting down, ready for election */
167   if (args->priority == 0)
168     {
169       clib_warning ("Master for VR %U is shutting down", format_vrrp_vr_key,
170                     vr);
171       vrrp_incr_stat_counter (VRRP_STAT_COUNTER_PRIO0_RCVD, vr->stat_index);
172       vrt->master_down_int = vrt->skew;
173       vrrp_vr_timer_set (vr, VRRP_VR_TIMER_MASTER_DOWN);
174       return;
175     }
176
177   /* no preempt set or adv from a higher priority router, update timers */
178   if (!(vrc->flags & VRRP_VR_PREEMPT) ||
179       (args->priority >= vrrp_vr_priority (vr)))
180     {
181       vrt->master_adv_int = args->max_adv_int;
182
183       vrrp_vr_skew_compute (vr);
184       vrrp_vr_master_down_compute (vr);
185       vrrp_vr_timer_set (vr, VRRP_VR_TIMER_MASTER_DOWN);
186       return;
187     }
188
189   /* preempt set or our priority > received, continue to wait on master down */
190   return;
191 }
192
193 always_inline void
194 vrrp_input_process (vrrp_input_process_args_t * args)
195 {
196   vrrp_vr_t *vr;
197
198   vr = vrrp_vr_lookup_index (args->vr_index);
199
200   if (!vr)
201     {
202       clib_warning ("Error retrieving VR with index %u", args->vr_index);
203       return;
204     }
205
206   vrrp_incr_stat_counter (VRRP_STAT_COUNTER_ADV_RCVD, vr->stat_index);
207
208   switch (vr->runtime.state)
209     {
210     case VRRP_VR_STATE_INIT:
211       return;
212     case VRRP_VR_STATE_BACKUP:
213       /* this is usually the only state an advertisement should be received */
214       vrrp_input_process_backup (vr, args);
215       break;
216     case VRRP_VR_STATE_MASTER:
217       /* might be getting preempted. or have a misbehaving peer */
218       clib_warning ("Received advertisement for master VR %U",
219                     format_vrrp_vr_key, vr);
220       vrrp_input_process_master (vr, args);
221       break;
222     default:
223       clib_warning ("Received advertisement for VR %U in unknown state %d",
224                     format_vrrp_vr_key, vr, vr->runtime.state);
225       break;
226     }
227
228   return;
229 }
230
231 typedef struct
232 {
233   ip46_address_t ip;
234   u32 vr_index;
235   u8 vr_id;
236   u8 is_ipv6;
237 } vrrp_arp_nd_trace_t;
238
239
240 static u8 *
241 format_vrrp_arp_nd_input_trace (u8 * s, va_list * va)
242 {
243   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
244   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
245   vrrp_arp_nd_trace_t *t = va_arg (*va, vrrp_arp_nd_trace_t *);
246
247   s = format (s, "address %U",
248               (t->is_ipv6) ? format_ip6_address : format_ip4_address,
249               (t->is_ipv6) ? (void *) &t->ip.ip6 : (void *) &t->ip.ip4);
250
251   if (t->vr_index != ~0)
252     s = format (s, ": vr_index %u vr_id %u", t->vr_index, t->vr_id);
253
254   return s;
255 }
256
257 typedef enum
258 {
259   VRRP_ARP_INPUT_NEXT_DROP,
260   VRRP_ARP_INPUT_NEXT_REPLY_TX,
261   VRRP_ARP_N_NEXT,
262 } vrrp_arp_next_t;
263
264 typedef enum
265 {
266   VRRP_ND_INPUT_NEXT_DROP,
267   VRRP_ND_INPUT_NEXT_REPLY_TX,
268   VRRP_ND_N_NEXT,
269 } vrrp_nd_next_t;
270
271 static_always_inline void
272 vrrp_arp_nd_next (vlib_buffer_t * b, u32 * next_index, u32 * vr_index,
273                   u8 is_ipv6)
274 {
275   vnet_main_t *vnm = vnet_get_main ();
276   vlib_main_t *vm = vlib_get_main ();
277   ethernet_header_t *eth, *eth_new;
278   void *lookup_addr = 0;
279   vrrp_vr_t *vr;
280   u32 sw_if_index;
281   vnet_link_t link_type;
282   u8 *rewrite, rewrite_len;
283   int bogus_length;
284   /* ND vars */
285   ip6_header_t *ip6 = 0;
286   icmp6_neighbor_solicitation_or_advertisement_header_t *sol_adv = 0;
287   icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *lladdr = 0;
288   /* ARP vars */
289   ethernet_arp_header_t *arp;
290   ip4_address_t ip4_addr;
291
292   if (is_ipv6)
293     {
294       ip6 = vlib_buffer_get_current (b);
295
296       /* we only care about about ICMP6 neighbor solicitiations */
297       if (ip6->protocol != IP_PROTOCOL_ICMP6)
298         return;
299
300       sol_adv = ip6_next_header (ip6);
301       lladdr = (void *) (sol_adv + 1);
302
303       /* skip anything other than neighbor solicitations */
304       if (sol_adv->icmp.type != ICMP6_neighbor_solicitation)
305         return;
306
307       lookup_addr = &sol_adv->target_address;
308       link_type = VNET_LINK_IP6;
309     }
310   else
311     {
312       arp = vlib_buffer_get_current (b);
313
314       /* skip non-request packets */
315       if (arp->opcode != clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request))
316         return;
317
318       lookup_addr = &arp->ip4_over_ethernet[1].ip4;
319       link_type = VNET_LINK_ARP;
320     }
321
322   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
323
324   /* Don't bother with a hash lookup if no VRs configured on this interface */
325   if (!vrrp_intf_num_vrs (sw_if_index, is_ipv6))
326     return;
327
328   /* skip requests that are not for VRRP addresses */
329   *vr_index = vrrp_vr_lookup_address (sw_if_index, is_ipv6, lookup_addr);
330   if (*vr_index == ~0)
331     return;
332
333   vr = vrrp_vr_lookup_index (*vr_index);
334   if (!vr || vr->runtime.state != VRRP_VR_STATE_MASTER)
335     {
336       /* RFC 5798 - section 6.4.2 - Backup "MUST NOT respond" to ARP/ND.
337        * So we must drop the request rather than allowing it to continue
338        * on the feature arc.
339        */
340       *next_index = VRRP_ARP_INPUT_NEXT_DROP;
341       return;
342     }
343
344   /* RFC 5798 section 6.4.3: Master "MUST respond" to ARP/ND. */
345   eth = ethernet_buffer_get_header (b);
346   rewrite = ethernet_build_rewrite (vnm, sw_if_index, link_type,
347                                     eth->src_address);
348   rewrite_len = vec_len (rewrite);
349   if (rewrite_len == 0)
350     return;
351
352   /* send the reply out the incoming interface */
353   *next_index = VRRP_ARP_INPUT_NEXT_REPLY_TX;
354   vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
355
356   /* the outbound ethernet & vlan headers may have a different length than
357    * the received header, so get a pointer to the new start of the packet
358    * and write the header there.
359    */
360   vlib_buffer_advance (b, -rewrite_len);
361   eth_new = vlib_buffer_get_current (b);
362   clib_memcpy_fast (eth_new, rewrite, rewrite_len);
363   vec_free (rewrite);
364
365   if (is_ipv6)
366     {
367       if (ip6_address_is_unspecified (&ip6->src_address))
368         ip6_set_reserved_multicast_address (&ip6->dst_address,
369                                             IP6_MULTICAST_SCOPE_link_local,
370                                             IP6_MULTICAST_GROUP_ID_all_hosts);
371       else
372         ip6->dst_address = ip6->src_address;
373
374       ip6->src_address = sol_adv->target_address;
375       ip6->hop_limit = 255;
376       sol_adv->icmp.type = ICMP6_neighbor_advertisement;
377       sol_adv->icmp.checksum = 0;
378       sol_adv->advertisement_flags =
379         clib_host_to_net_u32 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_ROUTER
380                               | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
381                               | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
382
383       clib_memcpy (lladdr->ethernet_address, vr->runtime.mac.bytes,
384                    sizeof (mac_address_t));
385       lladdr->header.type =
386         ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
387
388       sol_adv->icmp.checksum =
389         ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus_length);
390
391     }
392   else
393     {
394       ip4_addr = arp->ip4_over_ethernet[1].ip4;
395
396       arp->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
397       arp->ip4_over_ethernet[1] = arp->ip4_over_ethernet[0];
398
399       arp->ip4_over_ethernet[0].mac = vr->runtime.mac;
400       arp->ip4_over_ethernet[0].ip4 = ip4_addr;
401     }
402 }
403
404 static_always_inline uword
405 vrrp_arp_nd_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
406                           vlib_frame_t * frame, u8 is_ipv6)
407 {
408   u32 n_left_from, *from, next_index, *to_next;
409
410   from = vlib_frame_vector_args (frame);
411   n_left_from = frame->n_vectors;
412   next_index = node->cached_next_index;
413
414   while (n_left_from > 0)
415     {
416       u32 n_left_to_next;
417
418       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
419
420       while (n_left_from > 0 && n_left_to_next > 0)
421         {
422
423           vlib_buffer_t *b0;
424           u32 bi0;
425           u32 next0;
426           u32 vr_index = ~0;
427
428           bi0 = from[0];
429           to_next[0] = bi0;
430           from += 1;
431           to_next += 1;
432           n_left_from -= 1;
433           n_left_to_next -= 1;
434
435           b0 = vlib_get_buffer (vm, bi0);
436
437           vnet_feature_next (&next0, b0);
438           vrrp_arp_nd_next (b0, &next0, &vr_index, is_ipv6);
439
440           if (b0->flags & VLIB_BUFFER_IS_TRACED)
441             {
442               vrrp_arp_nd_trace_t *t =
443                 vlib_add_trace (vm, node, b0, sizeof (*t));
444               vrrp_vr_t *vr;
445
446               if (is_ipv6)
447                 {
448                   ip6_header_t *ip0;
449                   icmp6_neighbor_solicitation_or_advertisement_header_t
450                     * sol_adv0;
451
452                   ip0 = vlib_buffer_get_current (b0);
453                   sol_adv0 = ip6_next_header (ip0);
454                   t->ip.ip6 = sol_adv0->target_address;
455                 }
456               else
457                 {
458                   ethernet_arp_header_t *arp0;
459
460                   arp0 = vlib_buffer_get_current (b0);
461                   t->ip.ip4 = arp0->ip4_over_ethernet[0].ip4;
462                 }
463
464               vr = vrrp_vr_lookup_index (vr_index);
465               if (vr)
466                 t->vr_id = vr->config.vr_id;
467
468               t->vr_index = vr_index;
469               t->is_ipv6 = is_ipv6;
470             }
471
472           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
473                                            n_left_to_next, bi0, next0);
474         }
475
476       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
477     }
478
479   return frame->n_vectors;
480 }
481
482 VLIB_NODE_FN (vrrp4_arp_input_node) (vlib_main_t * vm,
483                                      vlib_node_runtime_t * node,
484                                      vlib_frame_t * frame)
485 {
486   return vrrp_arp_nd_input_inline (vm, node, frame, 0 /* is_ipv6 */ );
487 }
488
489 VLIB_REGISTER_NODE (vrrp4_arp_input_node) =
490 {
491   .name = "vrrp4-arp-input",
492   .vector_size = sizeof (u32),
493   .format_trace = format_vrrp_arp_nd_input_trace,
494   .type = VLIB_NODE_TYPE_INTERNAL,
495
496   .n_errors = ARRAY_LEN(vrrp_error_strings),
497   .error_strings = vrrp_error_strings,
498
499   .n_next_nodes = VRRP_ARP_N_NEXT,
500
501   .next_nodes = {
502         [VRRP_ARP_INPUT_NEXT_DROP] = "error-drop",
503         [VRRP_ARP_INPUT_NEXT_REPLY_TX] = "interface-output",
504   },
505 };
506
507 VNET_FEATURE_INIT (vrrp4_arp_feat_node, static) =
508 {
509   .arc_name = "arp",
510   .node_name = "vrrp4-arp-input",
511   .runs_before = VNET_FEATURES ("arp-reply"),
512 };
513
514 VLIB_NODE_FN (vrrp6_nd_input_node) (vlib_main_t * vm,
515                                      vlib_node_runtime_t * node,
516                                      vlib_frame_t * frame)
517 {
518   return vrrp_arp_nd_input_inline (vm, node, frame, 1 /* is_ipv6 */);
519 }
520
521 VLIB_REGISTER_NODE (vrrp6_nd_input_node) =
522 {
523   .name = "vrrp6-nd-input",
524   .vector_size = sizeof (u32),
525   .format_trace = format_vrrp_arp_nd_input_trace,
526   .type = VLIB_NODE_TYPE_INTERNAL,
527
528   .n_errors = ARRAY_LEN(vrrp_error_strings),
529   .error_strings = vrrp_error_strings,
530
531   .n_next_nodes = VRRP_ND_N_NEXT,
532
533   .next_nodes = {
534         [VRRP_ND_INPUT_NEXT_DROP] = "error-drop",
535         [VRRP_ND_INPUT_NEXT_REPLY_TX] = "interface-output",
536   },
537 };
538
539 VNET_FEATURE_INIT (vrrp6_nd_feat_node, static) =
540 {
541   .arc_name = "ip6-local",
542   .node_name = "vrrp6-nd-input",
543   .runs_before = VNET_FEATURES ("ip6-local-end-of-arc"),
544 };
545
546 static_always_inline uword
547 vrrp_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
548                     vlib_frame_t * frame, u8 is_ipv6)
549 {
550   u32 n_left_from, *from;
551   vrrp_main_t *vmp = &vrrp_main;
552
553   from = vlib_frame_vector_args (frame);
554   n_left_from = frame->n_vectors;
555
556   while (n_left_from > 0)
557     {
558       u32 bi0;
559       vlib_buffer_t *b0;
560       u32 next0, error0;
561       void *ip0;
562       vrrp_header_t *vrrp0;
563       vrrp_vr_t *vr0;
564       vrrp_input_process_args_t args0;
565       u8 *ttl0;
566       u16 rx_csum0;
567       u16 payload_len0;
568       int addr_len;
569
570       bi0 = from[0];
571       b0 = vlib_get_buffer (vm, bi0);
572
573       ip0 = vlib_buffer_get_current (b0);
574
575       if (is_ipv6)
576         {
577           ip6_header_t *ip6 = ip0;
578
579           vrrp0 = (vrrp_header_t *) (ip6 + 1);
580           ttl0 = &ip6->hop_limit;
581           addr_len = 16;
582           payload_len0 = clib_net_to_host_u16 (ip6->payload_length);
583           vlib_buffer_advance (b0, sizeof (*ip6));
584           clib_memcpy_fast (&args0.src_addr.ip6, &ip6->src_address, addr_len);
585         }
586       else
587         {
588           ip4_header_t *ip4 = ip0;
589
590           vrrp0 = (vrrp_header_t *) (ip4 + 1);
591           ttl0 = &ip4->ttl;
592           addr_len = 4;
593           payload_len0 = clib_net_to_host_u16 (ip4->length) - sizeof(*ip4);
594           vlib_buffer_advance (b0, sizeof (*ip4));
595           clib_memcpy_fast (&args0.src_addr.ip4, &ip4->src_address, addr_len);
596         }
597
598       next0 = VRRP_INPUT_NEXT_DROP;
599
600       error0 = VRRP_ERROR_RECEIVED;
601
602       /* Validation from RFC 5798 sec 7.1 */
603
604       /* checksum set to 0 for calculation, save original value */
605       rx_csum0 = vrrp0->checksum;
606       vrrp0->checksum = 0;
607
608       /* Mandatory - TTL/hop limit must be 255 */
609       if (*ttl0 != 255)
610         {
611           error0 = VRRP_ERROR_BAD_TTL;
612           vrrp_incr_err_counter (VRRP_ERR_COUNTER_TTL);
613           goto trace;
614         }
615
616       /* Mandatory - VRRP version must be 3 */
617       if ((vrrp0->vrrp_version_and_type >> 4) != 3)
618         {
619           error0 = VRRP_ERROR_NOT_VERSION_3;
620           vrrp_incr_err_counter (VRRP_ERR_COUNTER_VERSION);
621           goto trace;
622         }
623
624       /* Mandatory - packet must be complete */
625       if (b0->current_length < sizeof (*vrrp0) +
626           ((u32) vrrp0->n_addrs) * addr_len)
627         {
628           error0 = VRRP_ERROR_INCOMPLETE_PKT;
629           vrrp_incr_err_counter (VRRP_ERR_COUNTER_PKT_LEN);
630           goto trace;
631         }
632
633       /* Mandatory - checksum must be correct */
634       if (rx_csum0 != vrrp_adv_csum (ip0, vrrp0, is_ipv6, payload_len0))
635         {
636           error0 = VRRP_ERROR_BAD_CHECKSUM;
637           vrrp_incr_err_counter (VRRP_ERR_COUNTER_CHKSUM);
638           goto trace;
639         }
640
641       /* Mandatory - VR must be configured on the interface adv received on */
642       if (!(vr0 =
643               vrrp_vr_lookup (vnet_buffer(b0)->sw_if_index[VLIB_RX],
644                               vrrp0->vr_id, is_ipv6)))
645         {
646           error0 = VRRP_ERROR_UNKNOWN_VR;
647           vrrp_incr_err_counter (VRRP_ERR_COUNTER_VRID);
648           goto trace;
649         }
650
651       /* Optional - count of addresses should match configuration */
652       /* Could also check that addresses match, but likely to be O(n^2) */
653       if (vrrp0->n_addrs != vec_len (vr0->config.vr_addrs))
654         {
655           error0 = VRRP_ERROR_ADDR_MISMATCH;
656           vrrp_incr_err_counter (VRRP_ERR_COUNTER_ADDR_LIST);
657           goto trace;
658         }
659
660       /* signal main thread to process contents of packet */
661       args0.vr_index = vr0 - vmp->vrs;
662       args0.priority = vrrp0->priority;
663       args0.max_adv_int = vrrp_adv_int_from_packet (vrrp0);
664
665       vl_api_rpc_call_main_thread (vrrp_input_process, (u8 *) &args0,
666                                    sizeof (args0));
667
668     trace:
669       vrrp0->checksum = rx_csum0; /* restore csum for correct trace output */
670       b0->error = node->errors[error0];
671
672       if (b0->flags & VLIB_BUFFER_IS_TRACED)
673         {
674           vrrp_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
675           size_t addr_len = (is_ipv6 ? 16 : 4);
676
677           t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
678           t->is_ipv6 = is_ipv6;
679           clib_memcpy_fast (&t->vrrp, vrrp0, sizeof (*vrrp0));
680           clib_memcpy_fast (t->addrs, (void *) (vrrp0 + 1),
681                             (size_t) vrrp0->n_addrs * addr_len);
682         }
683
684       /* always drop, never forward or reply here */
685       vlib_set_next_frame_buffer (vm, node, next0, bi0);
686
687       from += 1;
688       n_left_from -= 1;
689     }
690
691   return frame->n_vectors;
692 }
693
694 VLIB_NODE_FN (vrrp4_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
695                                 vlib_frame_t * frame)
696 {
697   return vrrp_input_inline (vm, node, frame, 0);
698 }
699
700 VLIB_REGISTER_NODE (vrrp4_input_node) =
701 {
702   .name = "vrrp4-input",
703   .vector_size = sizeof (u32),
704   .format_trace = format_vrrp_trace,
705   .type = VLIB_NODE_TYPE_INTERNAL,
706
707   .n_errors = ARRAY_LEN(vrrp_error_strings),
708   .error_strings = vrrp_error_strings,
709
710   .n_next_nodes = VRRP_INPUT_N_NEXT,
711
712   .next_nodes = {
713         [VRRP_INPUT_NEXT_DROP] = "error-drop",
714   },
715 };
716
717 VLIB_NODE_FN (vrrp6_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
718                                 vlib_frame_t * frame)
719 {
720   return vrrp_input_inline (vm, node, frame, 1);
721 }
722
723 VLIB_REGISTER_NODE (vrrp6_input_node) =
724 {
725   .name = "vrrp6-input",
726   .vector_size = sizeof (u32),
727   .format_trace = format_vrrp_trace,
728   .type = VLIB_NODE_TYPE_INTERNAL,
729
730   .n_errors = ARRAY_LEN(vrrp_error_strings),
731   .error_strings = vrrp_error_strings,
732
733   .n_next_nodes = VRRP_INPUT_N_NEXT,
734
735   .next_nodes = {
736         [VRRP_INPUT_NEXT_DROP] = "error-drop",
737   },
738 };
739
740 typedef struct
741 {
742   u32 sw_if_index;
743   u8 is_ipv6;
744   ip46_address_t src, dst;
745 } vrrp_accept_owner_trace_t;
746
747 /* packet trace format function */
748 static u8 *
749 format_vrrp_accept_owner_trace (u8 * s, va_list * args)
750 {
751   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
752   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
753   vrrp_accept_owner_trace_t *t = va_arg (*args, vrrp_accept_owner_trace_t *);
754   int ip_ver = 4, ip_type = IP46_TYPE_IP4;
755
756   if (t->is_ipv6)
757     {
758       ip_ver = 6;
759       ip_type = IP46_TYPE_IP6;
760     }
761
762   s = format (s, "IPv%d sw_if_index %d %U -> %U",
763               ip_ver, t->sw_if_index,
764               format_ip46_address, &t->src, ip_type,
765               format_ip46_address, &t->dst, ip_type);
766
767   return s;
768 }
769
770 #define foreach_vrrp_accept_owner_error                           \
771 _(RECEIVED, "VRRP owner accept packets received")                 \
772 _(PROCESSED, "VRRP owner accept advertisements processed")
773
774 typedef enum
775 {
776 #define _(sym,str) VRRP_ACCEPT_OWNER_ERROR_##sym,
777   foreach_vrrp_accept_owner_error
778 #undef _
779     VRRP_ACCEPT_OWNER_N_ERROR,
780 } vrrp_accept_owner_error_t;
781
782 static char *vrrp_accept_owner_error_strings[] = {
783 #define _(sym,string) string,
784   foreach_vrrp_accept_owner_error
785 #undef _
786 };
787
788 typedef enum
789 {
790   VRRP_ACCEPT_OWNER_NEXT_PROCESS,
791   VRRP_ACCEPT_OWNER_N_NEXT,
792 } vrrp_accept_owner_next_t;
793
794 static_always_inline void
795 vrrp_accept_owner_next_node (u32 sw_if_index, u8 vr_id, u8 is_ipv6,
796                              u32 *next_index, u32 *error)
797 {
798   vrrp_vr_t *vr = vrrp_vr_lookup (sw_if_index, vr_id, is_ipv6);
799
800   if (vr && (vr->runtime.state == VRRP_VR_STATE_MASTER) &&
801       (vr->config.flags & VRRP_VR_ACCEPT))
802     {
803       *next_index = VRRP_ACCEPT_OWNER_NEXT_PROCESS;
804       *error = VRRP_ACCEPT_OWNER_ERROR_PROCESSED;
805     }
806 }
807
808 static_always_inline uword
809 vrrp_accept_owner_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
810                                 vlib_frame_t * frame, u8 is_ipv6)
811 {
812   u32 n_left_from, *from, *to_next;
813   u32 next_index = node->cached_next_index;
814
815   from = vlib_frame_vector_args (frame);
816   n_left_from = frame->n_vectors;
817
818   while (n_left_from > 0)
819     {
820       u32 n_left_to_next;
821
822       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
823
824       while (n_left_from >= 2 && n_left_to_next >= 2)
825         {
826           u32 bi0, bi1;
827           vlib_buffer_t *b0, *b1;
828           u32 next0, next1;
829           u32 error0, error1;
830           vrrp_header_t *vrrp0, *vrrp1;
831           ip4_header_t *ip40, *ip41;
832           ip6_header_t *ip60, *ip61;
833           u32 sw_if_index0, sw_if_index1;
834
835           bi0 = from[0];
836           bi1 = from[1];
837
838           to_next[0] = bi0;
839           to_next[1] = bi1;
840
841           b0 = vlib_get_buffer (vm, bi0);
842           b1 = vlib_get_buffer (vm, bi1);
843
844           /* most packets will follow feature arc */
845           vnet_feature_next (&next0, b0);
846           vnet_feature_next (&next1, b1);
847
848           error0 = error1 = VRRP_ACCEPT_OWNER_ERROR_RECEIVED;
849
850           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
851           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
852
853           /* find VRRP advertisements which should be sent to VRRP node */
854           if (is_ipv6)
855             {
856               ip60 = vlib_buffer_get_current (b0);
857               ip61 = vlib_buffer_get_current (b1);
858
859               if (PREDICT_FALSE (ip60->protocol == IP_PROTOCOL_VRRP))
860                 {
861                   vrrp0 = (vrrp_header_t *) (ip60 + 1);
862                   vrrp_accept_owner_next_node (sw_if_index0, vrrp0->vr_id,
863                                                is_ipv6, &next0, &error0);
864                 }
865               if (PREDICT_FALSE (ip61->protocol == IP_PROTOCOL_VRRP))
866                 {
867                   vrrp1 = (vrrp_header_t *) (ip61 + 1);
868                   vrrp_accept_owner_next_node (sw_if_index1, vrrp1->vr_id,
869                                                is_ipv6, &next1, &error1);
870                 }
871             }
872           else
873             {
874               ip40 = vlib_buffer_get_current (b0);
875               ip41 = vlib_buffer_get_current (b1);
876
877               if (PREDICT_FALSE (ip40->protocol == IP_PROTOCOL_VRRP))
878                 {
879                   vrrp0 = (vrrp_header_t *) (ip40 + 1);
880                   vrrp_accept_owner_next_node (sw_if_index0, vrrp0->vr_id,
881                                                is_ipv6, &next0, &error0);
882                 }
883               if (PREDICT_FALSE (ip41->protocol == IP_PROTOCOL_VRRP))
884                 {
885                   vrrp1 = (vrrp_header_t *) (ip41 + 1);
886                   vrrp_accept_owner_next_node (sw_if_index1, vrrp1->vr_id,
887                                                is_ipv6, &next1, &error1);
888                 }
889             }
890
891           b0->error = node->errors[error0];
892           b1->error = node->errors[error1];
893
894           if (b0->flags & VLIB_BUFFER_IS_TRACED)
895             {
896               vrrp_accept_owner_trace_t *t =
897                 vlib_add_trace (vm, node, b0, sizeof (*t));
898
899               t->sw_if_index = sw_if_index0;
900               t->is_ipv6 = is_ipv6;
901               if (is_ipv6)
902                 {
903                   ip6_address_copy (&t->src.ip6, &ip60->src_address);
904                   ip6_address_copy (&t->dst.ip6, &ip60->dst_address);
905                 }
906               else
907                 {
908                   t->src.ip4.as_u32 = ip40->src_address.as_u32;
909                   t->dst.ip4.as_u32 = ip40->dst_address.as_u32;
910                 }
911             }
912
913           if (b1->flags & VLIB_BUFFER_IS_TRACED)
914             {
915               vrrp_accept_owner_trace_t *t =
916                 vlib_add_trace (vm, node, b1, sizeof (*t));
917
918               t->sw_if_index = sw_if_index1;
919               t->is_ipv6 = is_ipv6;
920               if (is_ipv6)
921                 {
922                   ip6_address_copy (&t->src.ip6, &ip61->src_address);
923                   ip6_address_copy (&t->dst.ip6, &ip61->dst_address);
924                 }
925               else
926                 {
927                   t->src.ip4.as_u32 = ip41->src_address.as_u32;
928                   t->dst.ip4.as_u32 = ip41->dst_address.as_u32;
929                 }
930             }
931
932           from += 2;
933           n_left_from -= 2;
934           to_next += 2;
935           n_left_to_next -= 2;
936
937           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
938                                            to_next, n_left_to_next,
939                                            bi0, bi1, next0, next1);
940         }
941
942       while (n_left_from > 0 && n_left_to_next > 0)
943         {
944           u32 bi0;
945           vlib_buffer_t *b0;
946           u32 next0;
947           u32 error0;
948           vrrp_header_t *vrrp0;
949           ip4_header_t *ip4;
950           ip6_header_t *ip6;
951           u32 sw_if_index0;
952
953           bi0 = from[0];
954           to_next[0] = bi0;
955
956           b0 = vlib_get_buffer (vm, bi0);
957
958           /* most packets will follow feature arc */
959           vnet_feature_next (&next0, b0);
960
961           error0 = VRRP_ACCEPT_OWNER_ERROR_RECEIVED;
962
963           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
964
965           /* find VRRP advertisements which should be sent to VRRP node */
966           if (is_ipv6)
967             {
968               ip6 = vlib_buffer_get_current (b0);
969
970               if (PREDICT_FALSE (ip6->protocol == IP_PROTOCOL_VRRP))
971                 {
972                   vrrp0 = (vrrp_header_t *) (ip6 + 1);
973                   vrrp_accept_owner_next_node (sw_if_index0, vrrp0->vr_id,
974                                                is_ipv6, &next0, &error0);
975                 }
976             }
977           else
978             {
979               ip4 = vlib_buffer_get_current (b0);
980
981               if (PREDICT_FALSE (ip4->protocol == IP_PROTOCOL_VRRP))
982                 {
983                   vrrp0 = (vrrp_header_t *) (ip4 + 1);
984                   vrrp_accept_owner_next_node (sw_if_index0, vrrp0->vr_id,
985                                                is_ipv6, &next0, &error0);
986                 }
987             }
988
989           b0->error = node->errors[error0];
990
991           if (b0->flags & VLIB_BUFFER_IS_TRACED)
992             {
993               vrrp_accept_owner_trace_t *t =
994                 vlib_add_trace (vm, node, b0, sizeof (*t));
995
996               t->sw_if_index = sw_if_index0;
997               t->is_ipv6 = is_ipv6;
998               if (is_ipv6)
999                 {
1000                   ip6_address_copy (&t->src.ip6, &ip6->src_address);
1001                   ip6_address_copy (&t->dst.ip6, &ip6->dst_address);
1002                 }
1003               else
1004                 {
1005                   t->src.ip4.as_u32 = ip4->src_address.as_u32;
1006                   t->dst.ip4.as_u32 = ip4->dst_address.as_u32;
1007                 }
1008             }
1009
1010           from += 1;
1011           n_left_from -= 1;
1012           to_next += 1;
1013           n_left_to_next -= 1;
1014
1015           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1016                                            to_next, n_left_to_next,
1017                                            bi0, next0);
1018         }
1019
1020       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1021     }
1022
1023   return frame->n_vectors;
1024 }
1025
1026 VLIB_NODE_FN (vrrp4_accept_owner_input_node) (vlib_main_t * vm,
1027                                               vlib_node_runtime_t * node,
1028                                               vlib_frame_t * frame)
1029 {
1030   return vrrp_accept_owner_input_inline (vm, node, frame, 0);
1031 }
1032
1033 VLIB_REGISTER_NODE (vrrp4_accept_owner_input_node) =
1034 {
1035   .name = "vrrp4-accept-owner-input",
1036   .vector_size = sizeof (u32),
1037   .format_trace = format_vrrp_accept_owner_trace,
1038   .type = VLIB_NODE_TYPE_INTERNAL,
1039
1040   .n_errors = ARRAY_LEN(vrrp_accept_owner_error_strings),
1041   .error_strings = vrrp_accept_owner_error_strings,
1042
1043   .n_next_nodes = VRRP_ACCEPT_OWNER_N_NEXT,
1044
1045   .next_nodes = {
1046         [VRRP_ACCEPT_OWNER_NEXT_PROCESS] = "vrrp4-input",
1047   },
1048 };
1049
1050 VNET_FEATURE_INIT (vrrp4_accept_owner_mc, static) =
1051 {
1052   .arc_name = "ip4-multicast",
1053   .node_name = "vrrp4-accept-owner-input",
1054   .runs_before = VNET_FEATURES ("ip4-mfib-forward-lookup"),
1055 };
1056
1057 VLIB_NODE_FN (vrrp6_accept_owner_input_node) (vlib_main_t * vm,
1058                                            vlib_node_runtime_t * node,
1059                                            vlib_frame_t * frame)
1060 {
1061   return vrrp_accept_owner_input_inline (vm, node, frame, 1);
1062 }
1063
1064 VLIB_REGISTER_NODE (vrrp6_accept_owner_input_node) =
1065 {
1066   .name = "vrrp6-accept-owner-input",
1067   .vector_size = sizeof (u32),
1068   .format_trace = format_vrrp_accept_owner_trace,
1069   .type = VLIB_NODE_TYPE_INTERNAL,
1070
1071   .n_errors = ARRAY_LEN(vrrp_accept_owner_error_strings),
1072   .error_strings = vrrp_accept_owner_error_strings,
1073
1074   .n_next_nodes = VRRP_ACCEPT_OWNER_N_NEXT,
1075
1076   .next_nodes = {
1077         [VRRP_ACCEPT_OWNER_NEXT_PROCESS] = "vrrp6-input",
1078   },
1079 };
1080
1081 VNET_FEATURE_INIT (vrrp6_accept_owner_mc, static) =
1082 {
1083   .arc_name = "ip6-multicast",
1084   .node_name = "vrrp6-accept-owner-input",
1085   .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
1086 };
1087
1088 static clib_error_t *
1089 vrrp_input_init (vlib_main_t *vm)
1090 {
1091   clib_error_t *error;
1092
1093   if ((error = vlib_call_init_function (vm, vrrp_init)))
1094     return error;
1095
1096   ip4_register_protocol (IP_PROTOCOL_VRRP, vrrp4_input_node.index);
1097   ip6_register_protocol (IP_PROTOCOL_VRRP, vrrp6_input_node.index);
1098
1099   return 0;
1100 }
1101
1102 VLIB_INIT_FUNCTION (vrrp_input_init);
1103
1104
1105 /*
1106  * fd.io coding-style-patch-verification: ON
1107  *
1108  * Local Variables:
1109  * eval: (c-set-style "gnu")
1110  * End:
1111  */