blob: c2a38016ba5acc713a3c95f3f054239591a250e9 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * ip/ip.h: ip generic (4 or 6) main
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#ifndef included_ip_main_h
41#define included_ip_main_h
42
43#include <vppinfra/hash.h>
Dave Barachd7cb1b52016-12-09 09:52:16 -050044#include <vppinfra/heap.h> /* adjacency heap */
Dave Barachd6534602016-06-14 18:38:02 -040045#include <vppinfra/ptclosure.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47#include <vnet/vnet.h>
48
Neale Rannsea93e482019-11-12 17:16:47 +000049#include <vnet/ip/ip_types.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070050#include <vnet/ip/format.h>
51#include <vnet/ip/ip_packet.h>
52#include <vnet/ip/lookup.h>
Neale Ranns59f71132020-04-08 12:19:38 +000053#include <vnet/ip/ip_interface.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
Dave Barach68b0fb02017-02-28 15:15:56 -050055#include <vnet/tcp/tcp_packet.h>
56#include <vnet/udp/udp_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#include <vnet/ip/icmp46_packet.h>
58
59#include <vnet/ip/ip4.h>
60#include <vnet/ip/ip4_error.h>
61#include <vnet/ip/ip4_packet.h>
Ole Troan92eade12016-01-13 20:17:08 +010062#include <vnet/ip/icmp4.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
64#include <vnet/ip/ip6.h>
65#include <vnet/ip/ip6_packet.h>
66#include <vnet/ip/ip6_error.h>
67#include <vnet/ip/icmp6.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Ed Warnickecb9cada2015-12-08 15:45:58 -070069/* Per protocol info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050070typedef struct
71{
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 /* Protocol name (also used as hash key). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050073 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75 /* Protocol number. */
76 ip_protocol_t protocol;
77
78 /* Format function for this IP protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050079 format_function_t *format_header;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 /* Parser for header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050082 unformat_function_t *unformat_header;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
84 /* Parser for per-protocol matches. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 unformat_function_t *unformat_match;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87 /* Parser for packet generator edits for this protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050088 unformat_function_t *unformat_pg_edit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089} ip_protocol_info_t;
90
91/* Per TCP/UDP port info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050092typedef struct
93{
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 /* Port name (used as hash key). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050095 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 /* UDP/TCP port number in network byte order. */
98 u16 port;
99
100 /* Port specific format function. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500101 format_function_t *format_header;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
103 /* Parser for packet generator edits for this protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500104 unformat_function_t *unformat_pg_edit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105} tcp_udp_port_info_t;
106
Dave Barachd7cb1b52016-12-09 09:52:16 -0500107typedef struct
108{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 /* Per IP protocol info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500110 ip_protocol_info_t *protocol_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
112 /* Protocol info index hashed by 8 bit IP protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500113 uword *protocol_info_by_protocol;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
115 /* Hash table mapping IP protocol name (see protocols.def)
116 to protocol number. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500117 uword *protocol_info_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
119 /* Per TCP/UDP port info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500120 tcp_udp_port_info_t *port_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 /* Hash table from network-byte-order port to port info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500123 uword *port_info_by_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
125 /* Hash table mapping TCP/UDP name to port info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500126 uword *port_info_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127} ip_main_t;
128
129extern ip_main_t ip_main;
130
Dave Barachd7cb1b52016-12-09 09:52:16 -0500131clib_error_t *ip_main_init (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
133static inline ip_protocol_info_t *
134ip_get_protocol_info (ip_main_t * im, u32 protocol)
135{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500136 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
138 p = hash_get (im->protocol_info_by_protocol, protocol);
139 return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0;
140}
141
142static inline tcp_udp_port_info_t *
143ip_get_tcp_udp_port_info (ip_main_t * im, u32 port)
144{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500145 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 p = hash_get (im->port_info_by_port, port);
148 return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
149}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500150
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151always_inline ip_csum_t
Dave Barachd7cb1b52016-12-09 09:52:16 -0500152ip_incremental_checksum_buffer (vlib_main_t * vm,
153 vlib_buffer_t * first_buffer,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 u32 first_buffer_offset,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500155 u32 n_bytes_to_checksum, ip_csum_t sum)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500157 vlib_buffer_t *b = first_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 u32 n_bytes_left = n_bytes_to_checksum;
159 ASSERT (b->current_length >= first_buffer_offset);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500160 void *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u32 n;
162
Juraj Sloboda8f39d552018-09-26 14:25:32 +0200163 n = clib_min (n_bytes_left, b->current_length - first_buffer_offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 h = vlib_buffer_get_current (b) + first_buffer_offset;
165 sum = ip_incremental_checksum (sum, h, n);
166 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
167 {
168 while (1)
169 {
170 n_bytes_left -= n;
171 if (n_bytes_left == 0)
172 break;
173 b = vlib_get_buffer (vm, b->next_buffer);
174 n = clib_min (n_bytes_left, b->current_length);
175 h = vlib_buffer_get_current (b);
176 sum = ip_incremental_checksum (sum, h, n);
177 }
178 }
179
180 return sum;
181}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Srikanth A02833ff2019-10-02 17:48:58 -0700183always_inline u16
184ip_calculate_l4_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
185 ip_csum_t sum0, u32 payload_length,
186 u8 * iph, u32 ip_header_size, u8 * l4h)
187{
188 u16 sum16;
189 u8 *data_this_buffer, length_odd;
190 u32 n_bytes_left, n_this_buffer, n_ip_bytes_this_buffer;
191
192 n_bytes_left = payload_length;
193
194 if (l4h) /* packet l4 header and no buffer chain involved */
195 {
196 ASSERT (p0 == NULL);
197 n_this_buffer = payload_length;
198 data_this_buffer = l4h;
199 }
200 else
201 {
202 ASSERT (p0);
203 if (iph) /* ip header pointer set to packet in buffer */
204 {
205 ASSERT (ip_header_size);
206 n_this_buffer = payload_length;
207 data_this_buffer = iph + ip_header_size; /* at l4 header */
208 n_ip_bytes_this_buffer =
209 p0->current_length - (((u8 *) iph - p0->data) - p0->current_data);
210 if (PREDICT_FALSE (payload_length + ip_header_size >
211 n_ip_bytes_this_buffer))
212 {
213 n_this_buffer = n_ip_bytes_this_buffer - ip_header_size;
214 if (PREDICT_FALSE (n_this_buffer >> 31))
215 { /* error - ip header don't fit this buffer */
Srikanth A02833ff2019-10-02 17:48:58 -0700216 return 0xfefe;
217 }
218 }
219 }
220 else /* packet in buffer with no ip header */
221 { /* buffer current pointer at l4 header */
222 n_this_buffer = p0->current_length;
223 data_this_buffer = vlib_buffer_get_current (p0);
224 }
225 n_this_buffer = clib_min (n_this_buffer, n_bytes_left);
226 }
227
228 while (1)
229 {
230 sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
231 n_bytes_left -= n_this_buffer;
232 if (n_bytes_left == 0)
233 break;
234
235 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
236 {
Srikanth A02833ff2019-10-02 17:48:58 -0700237 return 0xfefe;
238 }
239
240 length_odd = (n_this_buffer & 1);
241
242 p0 = vlib_get_buffer (vm, p0->next_buffer);
243 data_this_buffer = vlib_buffer_get_current (p0);
244 n_this_buffer = clib_min (p0->current_length, n_bytes_left);
245
246 if (PREDICT_FALSE (length_odd))
247 {
248 /* Prepend a 0 byte to maintain 2-byte checksum alignment */
249 data_this_buffer--;
250 n_this_buffer++;
251 n_bytes_left++;
252 data_this_buffer[0] = 0;
253 }
254 }
255
256 sum16 = ~ip_csum_fold (sum0);
257 return sum16;
258}
259
Dave Barachd7cb1b52016-12-09 09:52:16 -0500260void ip_del_all_interface_addresses (vlib_main_t * vm, u32 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Dave Barachf39ff742016-03-20 10:14:45 -0400262extern vlib_node_registration_t ip4_inacl_node;
263extern vlib_node_registration_t ip6_inacl_node;
264
Neale Ranns2297af02017-09-12 09:45:04 -0700265void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api,
266 const u8 * name);
Neale Ranns15002542017-09-10 04:39:11 -0700267
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700268void ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api);
Neale Ranns15002542017-09-10 04:39:11 -0700269
Nathan Skrzypczaka424dd12021-08-20 15:53:43 +0200270void fib_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 fib_index);
271void mfib_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 mfib_index);
Nathan Skrzypczak275bd792021-09-17 17:29:14 +0200272int ip_table_bind (fib_protocol_t fproto, u32 sw_if_index, u32 table_id);
Neale Ranns15002542017-09-10 04:39:11 -0700273
Aloys Augustin6e4cfb52021-09-16 20:53:14 +0200274u32 ip_table_get_unused_id (fib_protocol_t fproto);
275
Florin Corascea194d2017-10-02 00:18:51 -0700276u8 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4);
277u8 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4);
Florin Coras477e91a2018-02-27 10:05:57 -0800278u8 ip4_is_local_host (ip4_address_t * ip4_address);
279u8 ip6_is_local_host (ip6_address_t * ip6_address);
Florin Corascea194d2017-10-02 00:18:51 -0700280u8 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4);
Florin Corascea194d2017-10-02 00:18:51 -0700281void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4);
282void ip_set (ip46_address_t * dst, void *src, u8 is_ip4);
283
Neale Ranns6dc0c8d2020-11-26 14:15:33 +0000284void ip_feature_enable_disable (ip_address_family_t af,
285 ip_sub_address_family_t safi,
286 ip_feature_location_t loc,
287 const char *feature,
288 u32 sw_if_index, int enable_disable,
289 void *feature_config,
290 u32 n_feature_config_bytes);
291
Neale Rannsb28652e2021-10-25 09:47:09 +0000292ethernet_type_t ip_address_family_to_ether_type (ip_address_family_t af);
293
Nick Zavaritsky27518c22020-02-27 15:54:58 +0000294always_inline u32 vlib_buffer_get_ip4_fib_index (vlib_buffer_t * b);
295always_inline u32 vlib_buffer_get_ip6_fib_index (vlib_buffer_t * b);
296always_inline u32
297vlib_buffer_get_ip_fib_index (vlib_buffer_t * b, u8 is_ip4)
298{
299 return (is_ip4 ? vlib_buffer_get_ip4_fib_index
300 : vlib_buffer_get_ip6_fib_index) (b);
301}
302
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303#endif /* included_ip_main_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304
305/*
306 * fd.io coding-style-patch-verification: ON
307 *
308 * Local Variables:
309 * eval: (c-set-style "gnu")
310 * End:
311 */