IPIP: Add IP{v4,v6} over IP{v4,v6} configured tunnel support.
[vpp.git] / src / vnet / ipip / ipip.h
1 /*
2  * ipip.h: types/functions for ipip.
3  *
4  * Copyright (c) 2018 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 aipiped 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 #ifndef included_ipip_h
19 #define included_ipip_h
20
21 #include <vnet/adj/adj_types.h>
22 #include <vnet/ip/ip6_packet.h>
23 #include <vnet/ip/format.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/vnet.h>
26
27 extern vnet_hw_interface_class_t ipip_hw_interface_class;
28
29 #define foreach_ipip_error                      \
30   /* Must be first. */                          \
31   _(DECAP_PKTS, "packets decapsulated")         \
32   _(BAD_PROTOCOL, "bad protocol")               \
33   _(NO_TUNNEL, "no tunnel")
34
35 typedef enum
36 {
37 #define _(sym, str) IPIP_ERROR_##sym,
38   foreach_ipip_error
39 #undef _
40     IPIP_N_ERROR,
41 } ipip_error_t;
42
43 /**
44  * @brief IPIP Tunnel key
45  */
46 typedef enum
47 {
48   IPIP_TRANSPORT_IP4,
49   IPIP_TRANSPORT_IP6,
50 } ipip_transport_t;
51
52 typedef struct
53 {
54   ipip_transport_t transport;
55   u32 fib_index;
56   ip46_address_t src;
57   ip46_address_t dst;
58 } __attribute__ ((packed)) ipip_tunnel_key_t;
59
60 typedef enum
61 {
62   IPIP_MODE_P2P = 0,
63   IPIP_MODE_6RD,
64 } ipip_mode_t;
65
66 /**
67  * @brief A representation of a IPIP tunnel
68  */
69 typedef struct
70 {
71   ipip_mode_t mode;
72   ipip_transport_t transport;
73   ipip_tunnel_key_t *key;
74   ip46_address_t tunnel_src;
75   ip46_address_t tunnel_dst;
76   u32 fib_index;
77   u32 hw_if_index;
78   u32 sw_if_index;
79   u32 dev_instance;             /* Real device instance in tunnel vector */
80   u32 user_instance;            /* Instance name being shown to user */
81
82   union
83   {
84     struct
85     {
86       fib_node_t node;
87       fib_node_index_t fib_entry_index;
88       u32 sibling_index;
89     } p2p;
90     struct
91     {
92       ip6_address_t ip6_prefix;
93       ip4_address_t ip4_prefix;
94       u8 ip6_prefix_len;
95       u8 ip4_prefix_len;
96       u8 shift;
97       bool security_check;
98     } sixrd;
99   };
100 } ipip_tunnel_t;
101
102 typedef struct
103 {
104   ipip_tunnel_t *tunnels;
105   uword *tunnel_by_key;
106   u32 *tunnel_index_by_sw_if_index;
107   fib_node_type_t fib_node_type;
108
109   /* convenience */
110   vlib_main_t *vlib_main;
111   vnet_main_t *vnet_main;
112
113   /* Record used instances */
114   uword *instance_used;
115
116   bool ip4_protocol_registered;
117   bool ip6_protocol_registered;
118 } ipip_main_t;
119
120 extern ipip_main_t ipip_main;
121 extern vlib_node_registration_t ipip4_input_node;
122 extern vlib_node_registration_t ipip6_input_node;
123
124 /*
125  * sixrd_get_addr_net
126  */
127 static_always_inline u32
128 sixrd_get_addr_net (const ipip_tunnel_t * t, u64 dal)
129 {
130   /* 1:1 mode */
131   if (t->sixrd.ip4_prefix_len == 32)
132     return (t->sixrd.ip4_prefix.as_u32);
133
134   dal = clib_net_to_host_u64 (dal);
135
136   /* Grab 32 - ip4_prefix_len bits out of IPv6 address from offset
137    * ip6_prefix_len */
138   u32 mask = ~(~0ULL << (32 - t->sixrd.ip4_prefix_len));
139   u32 ip4 =
140     clib_net_to_host_u32 (t->sixrd.
141                           ip4_prefix.as_u32) | ((u32) (dal >> t->sixrd.
142                                                        shift) & mask);
143   return clib_host_to_net_u32 (ip4);
144 }
145
146 int ipip_add_tunnel (ipip_transport_t transport, u32 instance,
147                      ip46_address_t * src, ip46_address_t * dst,
148                      u32 fib_index, u32 * sw_if_indexp);
149 int ipip_del_tunnel (u32 sw_if_index);
150 int sixrd_add_tunnel (ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
151                       ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
152                       ip4_address_t * ip4_src, bool security_check,
153                       u32 fib_index, u32 * sw_if_index);
154 int sixrd_del_tunnel (u32 sw_if_index);
155 void ipip_tunnel_db_add (ipip_tunnel_t * t, ipip_tunnel_key_t * key);
156 void ipip_tunnel_db_remove (ipip_tunnel_t * t);
157 ipip_tunnel_t *ipip_tunnel_db_find (ipip_tunnel_key_t * key);
158 ipip_tunnel_t *ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index);
159
160 #endif
161
162 /*
163  * fd.io coding-style-patch-verification: ON
164  *
165  * Local Variables:
166  * eval: (c-set-style "gnu")
167  * End:
168  */