ad0c3a3a79bab751023d4651a97dd77f4dd71926
[vpp.git] / src / vnet / ip6-nd / ip6_nd_inline.h
1 /*
2  *
3  * ip6_nd_inline.h: ip6 neighbor discovery inline
4  *
5  * Copyright (c) 2021 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifndef __IP6_ND_INLINE_H__
20 #define __IP6_ND_INLINE_H__
21
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/ip/icmp46_packet.h>
24 #include <vnet/ip/ip6.h>
25
26 typedef enum
27 {
28   ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
29   ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
30   ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
31 } icmp6_neighbor_solicitation_or_advertisement_next_t;
32
33 static_always_inline void
34 icmp6_send_neighbor_advertisement (
35   vlib_main_t *vm, vlib_buffer_t *b, ip6_header_t *ip6_h,
36   icmp6_neighbor_solicitation_or_advertisement_header_t *icmp6_nsa,
37   icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
38     *icmp6_nd_ell_addr,
39   u32 sw_if_index0)
40 {
41   vnet_main_t *vnm = vnet_get_main ();
42   vnet_sw_interface_t *sw_if;
43   ethernet_interface_t *eth_if;
44   ethernet_header_t *eth;
45   int bogus_length;
46
47   /* dst address is either source address or the all-nodes mcast addr */
48   if (!ip6_address_is_unspecified (&ip6_h->src_address))
49     ip6_h->dst_address = ip6_h->src_address;
50   else
51     ip6_set_reserved_multicast_address (&ip6_h->dst_address,
52                                         IP6_MULTICAST_SCOPE_link_local,
53                                         IP6_MULTICAST_GROUP_ID_all_hosts);
54
55   ip6_h->src_address = icmp6_nsa->target_address;
56   ip6_h->hop_limit = 255;
57   icmp6_nsa->icmp.type = ICMP6_neighbor_advertisement;
58
59   sw_if = vnet_get_sup_sw_interface (vnm, sw_if_index0);
60   ASSERT (sw_if->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
61   eth_if = ethernet_get_interface (&ethernet_main, sw_if->hw_if_index);
62   if (eth_if && icmp6_nd_ell_addr)
63     {
64       clib_memcpy (icmp6_nd_ell_addr->ethernet_address, &eth_if->address, 6);
65       icmp6_nd_ell_addr->header.type =
66         ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
67     }
68
69   icmp6_nsa->advertisement_flags =
70     clib_host_to_net_u32 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
71                           ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
72
73   icmp6_nsa->icmp.checksum = 0;
74   icmp6_nsa->icmp.checksum =
75     ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6_h, &bogus_length);
76   ASSERT (bogus_length == 0);
77
78   /* Reuse current MAC header, copy SMAC to DMAC and
79    * interface MAC to SMAC */
80   vlib_buffer_advance (b, -ethernet_buffer_header_size (b));
81   eth = vlib_buffer_get_current (b);
82   clib_memcpy (eth->dst_address, eth->src_address, 6);
83   if (eth_if)
84     clib_memcpy (eth->src_address, &eth_if->address, 6);
85
86   /* Setup input and output sw_if_index for packet */
87   ASSERT (vnet_buffer (b)->sw_if_index[VLIB_RX] == sw_if_index0);
88   vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index0;
89   vnet_buffer (b)->sw_if_index[VLIB_RX] =
90     vnet_main.local_interface_sw_if_index;
91 }
92
93 #endif /* included_ip6_nd_inline_h */
94
95 /*
96  * fd.io coding-style-patch-verification: ON
97  *
98  * Local Variables:
99  * eval: (c-set-style "gnu")
100  * End:
101  */