vrrp: add plugin providing vrrp support
[vpp.git] / src / plugins / vrrp / vrrp_packet.c
1 /*
2  * vrrp.c - vrrp plugin action functions
3  *
4  * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  */
9
10 #include <vnet/vnet.h>
11 #include <vnet/plugin/plugin.h>
12 #include <vnet/mfib/mfib_entry.h>
13 #include <vnet/mfib/mfib_table.h>
14 #include <vnet/adj/adj.h>
15 #include <vnet/adj/adj_mcast.h>
16 #include <vnet/fib/fib_table.h>
17 #include <vnet/ip/igmp_packet.h>
18 #include <vnet/ip/ip6_link.h>
19 #include <vnet/ethernet/arp_packet.h>
20
21 #include <vrrp/vrrp.h>
22 #include <vrrp/vrrp_packet.h>
23
24 #include <vpp/app/version.h>
25
26 static const u8 vrrp4_dst_mac[6] = { 0x1, 0x0, 0x5e, 0x0, 0x0, 0x12 };
27 static const u8 vrrp6_dst_mac[6] = { 0x33, 0x33, 0x0, 0x0, 0x0, 0x12 };
28 static const u8 vrrp_src_mac_prefix[4] = { 0x0, 0x0, 0x5e, 0x0 };
29
30 static int
31 vrrp_adv_l2_build_multicast (vrrp_vr_t * vr, vlib_buffer_t * b)
32 {
33   vnet_main_t *vnm = vnet_get_main ();
34   vnet_link_t link_type;
35   ethernet_header_t *eth;
36   int n_bytes = 0;
37   const void *dst_mac;
38   u8 mac_byte_ipver;
39   u8 *rewrite;
40
41   eth = vlib_buffer_get_current (b);
42
43   if (vrrp_vr_is_ipv6 (vr))
44     {
45       dst_mac = vrrp6_dst_mac;
46       link_type = VNET_LINK_IP6;
47       mac_byte_ipver = 0x2;
48     }
49   else
50     {
51       dst_mac = vrrp4_dst_mac;
52       link_type = VNET_LINK_IP4;
53       mac_byte_ipver = 0x1;
54     }
55
56   rewrite = ethernet_build_rewrite (vnm, vr->config.sw_if_index, link_type,
57                                     dst_mac);
58   clib_memcpy (eth, rewrite, vec_len (rewrite));
59
60   /* change the source mac from the HW addr to the VRRP virtual MAC */
61   clib_memcpy
62     (eth->src_address, vrrp_src_mac_prefix, sizeof (vrrp_src_mac_prefix));
63   eth->src_address[4] = mac_byte_ipver;
64   eth->src_address[5] = vr->config.vr_id;
65
66   n_bytes += vec_len (rewrite);
67
68   vlib_buffer_chain_increase_length (b, b, n_bytes);
69   vlib_buffer_advance (b, n_bytes);
70
71   vec_free (rewrite);
72
73   return n_bytes;
74 }
75
76 #define VRRP4_MCAST_ADDR_AS_U8 { 224, 0, 0, 18 }
77 #define VRRP6_MCAST_ADDR_AS_U8 \
78 { 0xff, 0x2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12 }
79
80 static const ip46_address_t vrrp4_mcast_addr = {
81   .ip4 = {.as_u8 = VRRP4_MCAST_ADDR_AS_U8,},
82 };
83
84 static const ip46_address_t vrrp6_mcast_addr = {
85   .ip6 = {.as_u8 = VRRP6_MCAST_ADDR_AS_U8,},
86 };
87
88 /* size of static parts of header + (# addrs * addr length) */
89 always_inline u16
90 vrrp_adv_payload_len (vrrp_vr_t * vr)
91 {
92   u16 addr_len = vrrp_vr_is_ipv6 (vr) ? 16 : 4;
93
94   return sizeof (vrrp_header_t) + (vec_len (vr->config.vr_addrs) * addr_len);
95 }
96
97 static int
98 vrrp_adv_l3_build (vrrp_vr_t * vr, vlib_buffer_t * b,
99                    const ip46_address_t * dst)
100 {
101   if (!vrrp_vr_is_ipv6 (vr))    /* IPv4 */
102     {
103       ip4_header_t *ip4 = vlib_buffer_get_current (b);
104
105       clib_memset (ip4, 0, sizeof (*ip4));
106       ip4->ip_version_and_header_length = 0x45;
107       ip4->ttl = 255;
108       ip4->protocol = IP_PROTOCOL_VRRP;
109       clib_memcpy (&ip4->dst_address, &dst->ip4, sizeof (dst->ip4));
110       ip4_src_address_for_packet (&ip4_main.lookup_main,
111                                   vr->config.sw_if_index, &ip4->src_address);
112       ip4->length = clib_host_to_net_u16 (sizeof (*ip4) +
113                                           vrrp_adv_payload_len (vr));
114       ip4->checksum = ip4_header_checksum (ip4);
115
116       vlib_buffer_chain_increase_length (b, b, sizeof (*ip4));
117       vlib_buffer_advance (b, sizeof (*ip4));
118
119       return sizeof (*ip4);
120     }
121   else
122     {
123       ip6_header_t *ip6 = vlib_buffer_get_current (b);
124
125       clib_memset (ip6, 0, sizeof (*ip6));
126       ip6->ip_version_traffic_class_and_flow_label = 0x00000060;
127       ip6->hop_limit = 255;
128       ip6->protocol = IP_PROTOCOL_VRRP;
129       clib_memcpy (&ip6->dst_address, &dst->ip6, sizeof (dst->ip6));
130       ip6_address_copy (&ip6->src_address,
131                         ip6_get_link_local_address (vr->config.sw_if_index));
132       ip6->payload_length = clib_host_to_net_u16 (vrrp_adv_payload_len (vr));
133
134       vlib_buffer_chain_increase_length (b, b, sizeof (*ip6));
135       vlib_buffer_advance (b, sizeof (*ip6));
136
137       return sizeof (*ip6);
138     }
139 }
140
141
142 u16
143 vrrp_adv_csum (void *l3_hdr, void *payload, u8 is_ipv6, u16 len)
144 {
145   ip_csum_t csum = 0;
146   u8 proto = IP_PROTOCOL_VRRP;
147   int addr_len;
148   int word_size = sizeof (uword);
149   void *src_addr;
150   int i;
151
152   if (is_ipv6)
153     {
154       addr_len = 16;
155       src_addr = &(((ip6_header_t *) l3_hdr)->src_address);
156     }
157   else
158     {
159       addr_len = 4;
160       src_addr = &(((ip4_header_t *) l3_hdr)->src_address);
161     }
162
163   for (i = 0; i < (2 * addr_len); i += word_size)
164     {
165       if (word_size == sizeof (u64))
166         csum =
167           ip_csum_with_carry (csum, clib_mem_unaligned (src_addr + i, u64));
168       else
169         csum =
170           ip_csum_with_carry (csum, clib_mem_unaligned (src_addr + i, u32));
171     }
172
173   csum = ip_csum_with_carry (csum,
174                              clib_host_to_net_u32 (len + (proto << 16)));
175
176   /* now do the payload */
177   csum = ip_incremental_checksum (csum, payload, len);
178
179   csum = ~ip_csum_fold (csum);
180
181   return (u16) csum;
182 }
183
184 static int
185 vrrp_adv_payload_build (vrrp_vr_t * vr, vlib_buffer_t * b, int shutdown)
186 {
187   vrrp_header_t *vrrp = vlib_buffer_get_current (b);
188   void *l3_hdr;
189   ip46_address_t *vr_addr;
190   void *hdr_addr;
191   u8 is_ipv6;
192   u8 n_addrs;
193   int len;
194
195   n_addrs = vec_len (vr->config.vr_addrs);
196   is_ipv6 = vrrp_vr_is_ipv6 (vr);
197
198   if (is_ipv6)
199     {
200       ip6_header_t *ip6;
201
202       len = sizeof (*vrrp) + n_addrs * sizeof (ip6_address_t);;
203       l3_hdr = vlib_buffer_get_current (b) - sizeof (ip6_header_t);
204       ip6 = l3_hdr;
205       ip6->payload_length = clib_host_to_net_u16 (len);
206     }
207   else
208     {
209       len = sizeof (*vrrp) + n_addrs * sizeof (ip4_address_t);
210       l3_hdr = vlib_buffer_get_current (b) - sizeof (ip4_header_t);
211     }
212
213   vrrp->vrrp_version_and_type = 0x31;
214   vrrp->vr_id = vr->config.vr_id;
215   vrrp->priority = (shutdown) ? 0 : vrrp_vr_priority (vr);
216   vrrp->n_addrs = vec_len (vr->config.vr_addrs);
217   vrrp->rsvd_and_max_adv_int = clib_host_to_net_u16 (vr->config.adv_interval);
218   vrrp->checksum = 0;
219
220   hdr_addr = (void *) (vrrp + 1);
221
222   vec_foreach (vr_addr, vr->config.vr_addrs)
223   {
224     if (is_ipv6)
225       {
226         clib_memcpy (hdr_addr, &vr_addr->ip6, 16);
227         hdr_addr += 16;
228       }
229     else
230       {
231         clib_memcpy (hdr_addr, &vr_addr->ip4, 4);
232         hdr_addr += 4;
233       }
234   }
235
236   vlib_buffer_chain_increase_length (b, b, vrrp_adv_payload_len (vr));
237
238   vrrp->checksum =
239     vrrp_adv_csum (l3_hdr, vrrp, is_ipv6, vrrp_adv_payload_len (vr));
240
241   return len;
242 }
243
244 static_always_inline u32
245 vrrp_adv_next_node (vrrp_vr_t * vr)
246 {
247   if (vrrp_vr_is_unicast (vr))
248     {
249       if (vrrp_vr_is_ipv6 (vr))
250         return ip6_lookup_node.index;
251       else
252         return ip4_lookup_node.index;
253     }
254   else
255     {
256       vrrp_main_t *vmp = &vrrp_main;
257
258       return vmp->intf_output_node_idx;
259     }
260 }
261
262 static_always_inline const ip46_address_t *
263 vrrp_adv_mcast_addr (vrrp_vr_t * vr)
264 {
265   if (vrrp_vr_is_ipv6 (vr))
266     return &vrrp6_mcast_addr;
267
268   return &vrrp4_mcast_addr;
269 }
270
271 int
272 vrrp_adv_send (vrrp_vr_t * vr, int shutdown)
273 {
274   vlib_main_t *vm = vlib_get_main ();
275   vlib_frame_t *to_frame;
276   int i, n_buffers = 1;
277   u32 node_index, *to_next, *bi = 0;
278   u8 is_unicast = vrrp_vr_is_unicast (vr);
279
280   node_index = vrrp_adv_next_node (vr);
281
282   if (is_unicast)
283     n_buffers = vec_len (vr->config.peer_addrs);
284
285   vec_validate (bi, n_buffers - 1);
286   if (vlib_buffer_alloc (vm, bi, n_buffers) != n_buffers)
287     {
288       clib_warning ("Buffer allocation failed for %U", format_vrrp_vr_key,
289                     vr);
290       vec_free (bi);
291       return -1;
292     }
293
294   to_frame = vlib_get_frame_to_node (vm, node_index);
295   to_next = vlib_frame_vector_args (to_frame);
296
297   for (i = 0; i < n_buffers; i++)
298     {
299       vlib_buffer_t *b;
300       u32 bi0;
301       const ip46_address_t *dst = vrrp_adv_mcast_addr (vr);
302
303       bi0 = vec_elt (bi, i);
304       b = vlib_get_buffer (vm, bi0);
305
306       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
307       b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
308       vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
309       vnet_buffer (b)->sw_if_index[VLIB_TX] = vr->config.sw_if_index;
310
311       if (is_unicast)
312         {
313           dst = vec_elt_at_index (vr->config.peer_addrs, i);
314           vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
315         }
316       else
317         vrrp_adv_l2_build_multicast (vr, b);
318
319       vrrp_adv_l3_build (vr, b, dst);
320       vrrp_adv_payload_build (vr, b, shutdown);
321
322       vlib_buffer_reset (b);
323
324       to_next[i] = bi0;
325     }
326
327   to_frame->n_vectors = n_buffers;
328
329   vlib_put_frame_to_node (vm, node_index, to_frame);
330
331   vec_free (bi);
332
333   return 0;
334 }
335
336 static void
337 vrrp6_na_pkt_build (vrrp_vr_t * vr, vlib_buffer_t * b, ip6_address_t * addr6)
338 {
339   vnet_main_t *vnm = vnet_get_main ();
340   vlib_main_t *vm = vlib_get_main ();
341   ethernet_header_t *eth;
342   ip6_header_t *ip6;
343   icmp6_neighbor_solicitation_or_advertisement_header_t *na;
344   icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *ll_opt;
345   int payload_length, bogus_length;
346   int rewrite_bytes = 0;
347   u8 *rewrite;
348   u8 dst_mac[6];
349
350   /* L2 headers */
351   eth = vlib_buffer_get_current (b);
352
353   ip6_multicast_ethernet_address (dst_mac, IP6_MULTICAST_GROUP_ID_all_hosts);
354   rewrite =
355     ethernet_build_rewrite (vnm, vr->config.sw_if_index, VNET_LINK_IP6,
356                             dst_mac);
357   rewrite_bytes += vec_len (rewrite);
358   clib_memcpy (eth, rewrite, vec_len (rewrite));
359   vec_free (rewrite);
360
361   b->current_length += rewrite_bytes;
362   vlib_buffer_advance (b, rewrite_bytes);
363
364   /* IPv6 */
365   ip6 = vlib_buffer_get_current (b);
366
367   b->current_length += sizeof (*ip6);
368   clib_memset (ip6, 0, sizeof (*ip6));
369
370   ip6->ip_version_traffic_class_and_flow_label = 0x00000060;
371   ip6->protocol = IP_PROTOCOL_ICMP6;
372   ip6->hop_limit = 255;
373   ip6_set_reserved_multicast_address (&ip6->dst_address,
374                                       IP6_MULTICAST_SCOPE_link_local,
375                                       IP6_MULTICAST_GROUP_ID_all_hosts);
376   ip6_address_copy (&ip6->src_address,
377                     ip6_get_link_local_address (vr->config.sw_if_index));
378
379
380   /* ICMPv6 */
381   na = (icmp6_neighbor_solicitation_or_advertisement_header_t *) (ip6 + 1);
382   ll_opt =
383     (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *) (na +
384                                                                        1);
385
386   payload_length = sizeof (*na) + sizeof (*ll_opt);
387   b->current_length += payload_length;
388   clib_memset (na, 0, payload_length);
389
390   na->icmp.type = ICMP6_neighbor_advertisement; /* icmp code, csum are 0 */
391   na->target_address = *addr6;
392   na->advertisement_flags = clib_host_to_net_u32
393     (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE
394      | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_ROUTER);
395
396   ll_opt->header.type =
397     ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
398   ll_opt->header.n_data_u64s = 1;
399   clib_memcpy (ll_opt->ethernet_address, vr->runtime.mac.bytes,
400                sizeof (vr->runtime.mac));
401
402   ip6->payload_length = clib_host_to_net_u16 (payload_length);
403   na->icmp.checksum =
404     ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus_length);
405 }
406
407 const mac_address_t broadcast_mac = {
408   .bytes = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff,},
409 };
410
411 static void
412 vrrp4_garp_pkt_build (vrrp_vr_t * vr, vlib_buffer_t * b, ip4_address_t * ip4)
413 {
414   vnet_main_t *vnm = vnet_get_main ();
415   ethernet_header_t *eth;
416   ethernet_arp_header_t *arp;
417   int rewrite_bytes;
418   u8 *rewrite;
419
420   eth = vlib_buffer_get_current (b);
421
422   rewrite =
423     ethernet_build_rewrite (vnm, vr->config.sw_if_index, VNET_LINK_ARP,
424                             broadcast_mac.bytes);
425   rewrite_bytes = vec_len (rewrite);
426   clib_memcpy (eth, rewrite, rewrite_bytes);
427   vec_free (rewrite);
428
429   b->current_length += rewrite_bytes;
430   vlib_buffer_advance (b, rewrite_bytes);
431
432   arp = vlib_buffer_get_current (b);
433   b->current_length += sizeof (*arp);
434
435   clib_memset (arp, 0, sizeof (*arp));
436
437   arp->l2_type = clib_host_to_net_u16 (ETHERNET_ARP_HARDWARE_TYPE_ethernet);
438   arp->l3_type = clib_host_to_net_u16 (ETHERNET_TYPE_IP4);
439   arp->n_l2_address_bytes = 6;
440   arp->n_l3_address_bytes = 4;
441   arp->opcode = clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request);
442   arp->ip4_over_ethernet[0].mac = vr->runtime.mac;
443   arp->ip4_over_ethernet[0].ip4 = *ip4;
444   arp->ip4_over_ethernet[1].mac = broadcast_mac;
445   arp->ip4_over_ethernet[1].ip4 = *ip4;
446 }
447
448 int
449 vrrp_garp_or_na_send (vrrp_vr_t * vr)
450 {
451   vlib_main_t *vm = vlib_get_main ();
452   vrrp_main_t *vmp = &vrrp_main;
453   vlib_frame_t *to_frame;
454   u32 *bi = 0;
455   u32 n_buffers;
456   u32 *to_next;
457   int i;
458
459   if (vec_len (vr->config.peer_addrs))
460     return 0;                   /* unicast is used in routed environments - don't garp */
461
462   n_buffers = vec_len (vr->config.vr_addrs);
463   if (!n_buffers)
464     {
465       clib_warning ("Unable to send gratuitous ARP for VR %U - no addresses",
466                     format_vrrp_vr_key, vr);
467       return -1;
468     }
469
470   /* need to send a packet for each VR address */
471   vec_validate (bi, n_buffers - 1);
472
473   if (vlib_buffer_alloc (vm, bi, n_buffers) != n_buffers)
474     {
475       clib_warning ("Buffer allocation failed for %U", format_vrrp_vr_key,
476                     vr);
477       vec_free (bi);
478       return -1;
479     }
480
481   to_frame = vlib_get_frame_to_node (vm, vmp->intf_output_node_idx);
482   to_frame->n_vectors = 0;
483   to_next = vlib_frame_vector_args (to_frame);
484
485   for (i = 0; i < n_buffers; i++)
486     {
487       vlib_buffer_t *b;
488       ip46_address_t *addr;
489
490       addr = vec_elt_at_index (vr->config.vr_addrs, i);
491       b = vlib_get_buffer (vm, bi[i]);
492
493       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
494       b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
495       vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
496       vnet_buffer (b)->sw_if_index[VLIB_TX] = vr->config.sw_if_index;
497
498       if (vrrp_vr_is_ipv6 (vr))
499         vrrp6_na_pkt_build (vr, b, &addr->ip6);
500       else
501         vrrp4_garp_pkt_build (vr, b, &addr->ip4);
502
503       vlib_buffer_reset (b);
504
505       to_next[i] = bi[i];
506       to_frame->n_vectors++;
507     }
508
509   vlib_put_frame_to_node (vm, vmp->intf_output_node_idx, to_frame);
510
511   return 0;
512 }
513
514 #define IGMP4_MCAST_ADDR_AS_U8 { 224, 0, 0, 22 }
515
516 static const ip4_header_t igmp_ip4_mcast = {
517   .ip_version_and_header_length = 0x46, /* there's options! */
518   .ttl = 1,
519   .protocol = IP_PROTOCOL_IGMP,
520   .tos = 0xc0,
521   .dst_address = {.as_u8 = IGMP4_MCAST_ADDR_AS_U8,},
522 };
523
524 static void
525 vrrp_igmp_pkt_build (vrrp_vr_t * vr, vlib_buffer_t * b)
526 {
527   ip4_header_t *ip4;
528   u8 *ip4_options;
529   igmp_membership_report_v3_t *report;
530   igmp_membership_group_v3_t *group;
531
532   ip4 = vlib_buffer_get_current (b);
533   clib_memcpy (ip4, &igmp_ip4_mcast, sizeof (*ip4));
534   ip4_src_address_for_packet (&ip4_main.lookup_main, vr->config.sw_if_index,
535                               &ip4->src_address);
536
537   vlib_buffer_chain_increase_length (b, b, sizeof (*ip4));
538   vlib_buffer_advance (b, sizeof (*ip4));
539
540   ip4_options = (u8 *) (ip4 + 1);
541   ip4_options[0] = 0x94;        /* 10010100 == the router alert option */
542   ip4_options[1] = 0x04;        /* length == 4 bytes */
543   ip4_options[2] = 0x0;         /* value == Router shall examine packet */
544   ip4_options[3] = 0x0;         /* reserved */
545
546   vlib_buffer_chain_increase_length (b, b, 4);
547   vlib_buffer_advance (b, 4);
548
549   report = vlib_buffer_get_current (b);
550
551   report->header.type = IGMP_TYPE_membership_report_v3;
552   report->header.code = 0;
553   report->header.checksum = 0;
554   report->unused = 0;
555   report->n_groups = clib_host_to_net_u16 (1);
556
557   vlib_buffer_chain_increase_length (b, b, sizeof (*report));
558   vlib_buffer_advance (b, sizeof (*report));
559
560   group = vlib_buffer_get_current (b);
561   group->type = IGMP_MEMBERSHIP_GROUP_change_to_exclude;
562   group->n_aux_u32s = 0;
563   group->n_src_addresses = 0;
564   group->group_address.as_u32 = clib_host_to_net_u32 (0xe0000012);
565
566   vlib_buffer_chain_increase_length (b, b, sizeof (*group));
567   vlib_buffer_advance (b, sizeof (*group));
568
569   ip4->length = clib_host_to_net_u16 (b->current_data);
570   ip4->checksum = ip4_header_checksum (ip4);
571
572   int payload_len = vlib_buffer_get_current (b) - ((void *) report);
573   report->header.checksum =
574     ~ip_csum_fold (ip_incremental_checksum (0, report, payload_len));
575
576   vlib_buffer_reset (b);
577 }
578
579 /* multicast listener report packet format for ethernet. */
580 typedef CLIB_PACKED (struct
581                      {
582                      ip6_hop_by_hop_ext_t ext_hdr;
583                      ip6_router_alert_option_t alert;
584                      ip6_padN_option_t pad;
585                      icmp46_header_t icmp;
586                      u16 rsvd;
587                      u16 num_addr_records;
588                      icmp6_multicast_address_record_t records[0];
589                      }) icmp6_multicast_listener_report_header_t;
590
591 static void
592 vrrp_icmp6_mlr_pkt_build (vrrp_vr_t * vr, vlib_buffer_t * b)
593 {
594   vlib_main_t *vm = vlib_get_main ();
595   ip6_header_t *ip6;
596   icmp6_multicast_listener_report_header_t *rh;
597   icmp6_multicast_address_record_t *rr;
598   ip46_address_t *vr_addr;
599   int bogus_length, n_addrs;
600   u16 payload_length;
601
602   n_addrs = vec_len (vr->config.vr_addrs) + 1;
603   payload_length = sizeof (*rh) + (n_addrs * sizeof (*rr));
604   b->current_length = sizeof (*ip6) + payload_length;
605   b->error = ICMP6_ERROR_NONE;
606
607   ip6 = vlib_buffer_get_current (b);
608   rh = (icmp6_multicast_listener_report_header_t *) (ip6 + 1);
609   rr = (icmp6_multicast_address_record_t *) (rh + 1);
610
611   /* IP header */
612   clib_memset (ip6, 0, b->current_length);
613   ip6->ip_version_traffic_class_and_flow_label =
614     clib_host_to_net_u32 (0x60000000);
615   ip6->hop_limit = 1;
616   ip6->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
617   ip6_set_reserved_multicast_address (&ip6->dst_address,
618                                       IP6_MULTICAST_SCOPE_link_local,
619                                       IP6_MULTICAST_GROUP_ID_mldv2_routers);
620   ip6_address_copy (&ip6->src_address,
621                     ip6_get_link_local_address (vr->config.sw_if_index));
622
623   clib_memset (rh, 0, sizeof (*rh));
624
625   /* v6 hop by hop extension header */
626   rh->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
627   rh->ext_hdr.n_data_u64s = 0;
628
629   rh->alert.type = IP6_MLDP_ALERT_TYPE;
630   rh->alert.len = 2;
631   rh->alert.value = 0;
632
633   rh->pad.type = 1;
634   rh->pad.len = 0;
635
636   /* icmp6 header */
637   rh->icmp.type = ICMP6_multicast_listener_report_v2;
638   rh->icmp.checksum = 0;
639
640   rh->rsvd = 0;
641   rh->num_addr_records = clib_host_to_net_u16 (n_addrs);
642
643   /* group addresses */
644
645   /* All VRRP routers group */
646   rr->type = 4;
647   rr->aux_data_len_u32s = 0;
648   rr->num_sources = 0;
649   clib_memcpy
650     (&rr->mcast_addr, &vrrp6_mcast_addr.ip6, sizeof (ip6_address_t));
651
652   /* solicited node multicast addresses for VR addrs */
653   vec_foreach (vr_addr, vr->config.vr_addrs)
654   {
655     u32 id;
656
657     rr++;
658     rr->type = 4;
659     rr->aux_data_len_u32s = 0;
660     rr->num_sources = 0;
661
662     id = clib_net_to_host_u32 (vr_addr->ip6.as_u32[3]) & 0x00ffffff;
663     ip6_set_solicited_node_multicast_address (&rr->mcast_addr, id);
664   }
665
666   ip6->payload_length = clib_host_to_net_u16 (payload_length);
667   rh->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6,
668                                                          &bogus_length);
669 }
670
671 int
672 vrrp_vr_multicast_group_join (vrrp_vr_t * vr)
673 {
674   vlib_main_t *vm = vlib_get_main ();
675   vlib_buffer_t *b;
676   vlib_frame_t *f;
677   vnet_main_t *vnm = vnet_get_main ();
678   vrrp_intf_t *intf;
679   u32 bi = 0, *to_next;
680   int n_buffers = 1;
681   u8 is_ipv6;
682   u32 node_index;
683
684   if (!vnet_sw_interface_is_up (vnm, vr->config.sw_if_index))
685     return 0;
686
687   if (vlib_buffer_alloc (vm, &bi, n_buffers) != n_buffers)
688     {
689       clib_warning ("Buffer allocation failed for %U", format_vrrp_vr_key,
690                     vr);
691       return -1;
692     }
693
694   is_ipv6 = vrrp_vr_is_ipv6 (vr);
695
696   b = vlib_get_buffer (vm, bi);
697
698   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
699   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
700
701   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
702   vnet_buffer (b)->sw_if_index[VLIB_TX] = vr->config.sw_if_index;
703
704   intf = vrrp_intf_get (vr->config.sw_if_index);
705   vnet_buffer (b)->ip.adj_index[VLIB_TX] = intf->mcast_adj_index[is_ipv6];
706
707   if (is_ipv6)
708     {
709       vrrp_icmp6_mlr_pkt_build (vr, b);
710       node_index = ip6_rewrite_mcast_node.index;
711     }
712   else
713     {
714       vrrp_igmp_pkt_build (vr, b);
715       node_index = ip4_rewrite_mcast_node.index;
716     }
717
718   f = vlib_get_frame_to_node (vm, node_index);
719   to_next = vlib_frame_vector_args (f);
720   to_next[0] = bi;
721   f->n_vectors = 1;
722
723   vlib_put_frame_to_node (vm, node_index, f);
724
725   return f->n_vectors;
726 }
727
728
729 /*
730  * fd.io coding-style-patch-verification: ON
731  *
732  * Local Variables:
733  * eval: (c-set-style "gnu")
734  * End:
735  */