Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 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> |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 25 | #include <vnet/tunnel/tunnel.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 26 | |
| 27 | extern vnet_hw_interface_class_t ipip_hw_interface_class; |
| 28 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 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 | _(FRAGMENTED_PACKET, "fragmented outer packet") |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 35 | |
| 36 | typedef enum |
| 37 | { |
| 38 | #define _(sym, str) IPIP_ERROR_##sym, |
| 39 | foreach_ipip_error |
| 40 | #undef _ |
| 41 | IPIP_N_ERROR, |
| 42 | } ipip_error_t; |
| 43 | |
| 44 | /** |
| 45 | * @brief IPIP Tunnel key |
| 46 | */ |
| 47 | typedef enum |
| 48 | { |
| 49 | IPIP_TRANSPORT_IP4, |
| 50 | IPIP_TRANSPORT_IP6, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 51 | } __clib_packed ipip_transport_t; |
| 52 | |
| 53 | typedef enum |
| 54 | { |
| 55 | IPIP_MODE_P2P = 0, |
| 56 | IPIP_MODE_P2MP, |
| 57 | IPIP_MODE_6RD, |
| 58 | } __clib_packed ipip_mode_t; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 59 | |
| 60 | typedef struct |
| 61 | { |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 62 | ip46_address_t src; |
| 63 | ip46_address_t dst; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 64 | u32 fib_index; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 65 | ipip_transport_t transport; |
| 66 | ipip_mode_t mode; |
| 67 | u16 __pad; |
| 68 | } __clib_packed ipip_tunnel_key_t; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 69 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 70 | STATIC_ASSERT_SIZEOF (ipip_tunnel_key_t, 5 * sizeof (u64)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * @brief A representation of a IPIP tunnel |
| 74 | */ |
| 75 | typedef struct |
| 76 | { |
Dave Barach | eb987d3 | 2018-05-03 08:26:39 -0400 | [diff] [blame] | 77 | /* Required for pool_get_aligned */ |
| 78 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 79 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 80 | ipip_mode_t mode; |
| 81 | ipip_transport_t transport; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 82 | ip46_address_t tunnel_src; |
| 83 | ip46_address_t tunnel_dst; |
| 84 | u32 fib_index; |
| 85 | u32 hw_if_index; |
| 86 | u32 sw_if_index; |
| 87 | u32 dev_instance; /* Real device instance in tunnel vector */ |
| 88 | u32 user_instance; /* Instance name being shown to user */ |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 89 | tunnel_encap_decap_flags_t flags; |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 90 | ip_dscp_t dscp; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 91 | |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 92 | struct |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 93 | { |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 94 | ip6_address_t ip6_prefix; |
| 95 | ip4_address_t ip4_prefix; |
| 96 | u8 ip6_prefix_len; |
| 97 | u8 ip4_prefix_len; |
| 98 | u8 shift; |
| 99 | bool security_check; |
| 100 | u32 ip6_fib_index; |
| 101 | } sixrd; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 102 | } ipip_tunnel_t; |
| 103 | |
| 104 | typedef struct |
| 105 | { |
| 106 | ipip_tunnel_t *tunnels; |
| 107 | uword *tunnel_by_key; |
| 108 | u32 *tunnel_index_by_sw_if_index; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 109 | |
| 110 | /* convenience */ |
| 111 | vlib_main_t *vlib_main; |
| 112 | vnet_main_t *vnet_main; |
| 113 | |
| 114 | /* Record used instances */ |
| 115 | uword *instance_used; |
| 116 | |
| 117 | bool ip4_protocol_registered; |
| 118 | bool ip6_protocol_registered; |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 119 | |
| 120 | u16 msg_id_base; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 121 | } ipip_main_t; |
| 122 | |
| 123 | extern ipip_main_t ipip_main; |
| 124 | extern vlib_node_registration_t ipip4_input_node; |
| 125 | extern vlib_node_registration_t ipip6_input_node; |
| 126 | |
| 127 | /* |
| 128 | * sixrd_get_addr_net |
| 129 | */ |
| 130 | static_always_inline u32 |
| 131 | sixrd_get_addr_net (const ipip_tunnel_t * t, u64 dal) |
| 132 | { |
| 133 | /* 1:1 mode */ |
| 134 | if (t->sixrd.ip4_prefix_len == 32) |
| 135 | return (t->sixrd.ip4_prefix.as_u32); |
| 136 | |
| 137 | dal = clib_net_to_host_u64 (dal); |
| 138 | |
| 139 | /* Grab 32 - ip4_prefix_len bits out of IPv6 address from offset |
| 140 | * ip6_prefix_len */ |
| 141 | u32 mask = ~(~0ULL << (32 - t->sixrd.ip4_prefix_len)); |
| 142 | u32 ip4 = |
| 143 | clib_net_to_host_u32 (t->sixrd. |
| 144 | ip4_prefix.as_u32) | ((u32) (dal >> t->sixrd. |
| 145 | shift) & mask); |
| 146 | return clib_host_to_net_u32 (ip4); |
| 147 | } |
| 148 | |
| 149 | int ipip_add_tunnel (ipip_transport_t transport, u32 instance, |
| 150 | ip46_address_t * src, ip46_address_t * dst, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 151 | u32 fib_index, tunnel_encap_decap_flags_t flags, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 152 | ip_dscp_t dscp, tunnel_mode_t mode, u32 * sw_if_indexp); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 153 | int ipip_del_tunnel (u32 sw_if_index); |
| 154 | int sixrd_add_tunnel (ip6_address_t * ip6_prefix, u8 ip6_prefix_len, |
| 155 | ip4_address_t * ip4_prefix, u8 ip4_prefix_len, |
| 156 | ip4_address_t * ip4_src, bool security_check, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 157 | u32 ip4_fib_index, u32 ip6_fib_index, |
| 158 | u32 * sw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 159 | int sixrd_del_tunnel (u32 sw_if_index); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 160 | void ipip_tunnel_db_add (ipip_tunnel_t * t, const ipip_tunnel_key_t * key); |
| 161 | void ipip_tunnel_db_remove (ipip_tunnel_t * t, const ipip_tunnel_key_t * key); |
| 162 | ipip_tunnel_t *ipip_tunnel_db_find (const ipip_tunnel_key_t * key); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 163 | ipip_tunnel_t *ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 164 | void ipip_mk_key (const ipip_tunnel_t * t, ipip_tunnel_key_t * key); |
| 165 | void ipip_mk_key_i (ipip_transport_t transport, |
| 166 | ipip_mode_t mode, |
| 167 | const ip46_address_t * src, |
| 168 | const ip46_address_t * dst, |
| 169 | u32 fib_index, ipip_tunnel_key_t * key); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 170 | |
| 171 | #endif |
| 172 | |
| 173 | /* |
| 174 | * fd.io coding-style-patch-verification: ON |
| 175 | * |
| 176 | * Local Variables: |
| 177 | * eval: (c-set-style "gnu") |
| 178 | * End: |
| 179 | */ |