blob: 2dfa979e70c8f13ab48b8bb71128fabe6cb57733 [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
49#include <vnet/ip/format.h>
50#include <vnet/ip/ip_packet.h>
51#include <vnet/ip/lookup.h>
52
Dave Barach68b0fb02017-02-28 15:15:56 -050053#include <vnet/tcp/tcp_packet.h>
54#include <vnet/udp/udp_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070055#include <vnet/ip/icmp46_packet.h>
56
57#include <vnet/ip/ip4.h>
58#include <vnet/ip/ip4_error.h>
59#include <vnet/ip/ip4_packet.h>
Ole Troan92eade12016-01-13 20:17:08 +010060#include <vnet/ip/icmp4.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
62#include <vnet/ip/ip6.h>
63#include <vnet/ip/ip6_packet.h>
64#include <vnet/ip/ip6_error.h>
65#include <vnet/ip/icmp6.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070066#include <vnet/classify/vnet_classify.h>
67
Ed Warnickecb9cada2015-12-08 15:45:58 -070068/* Per protocol info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050069typedef struct
70{
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 /* Protocol name (also used as hash key). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050072 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
74 /* Protocol number. */
75 ip_protocol_t protocol;
76
77 /* Format function for this IP protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050078 format_function_t *format_header;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
80 /* Parser for header. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050081 unformat_function_t *unformat_header;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 /* Parser for per-protocol matches. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050084 unformat_function_t *unformat_match;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 /* Parser for packet generator edits for this protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050087 unformat_function_t *unformat_pg_edit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088} ip_protocol_info_t;
89
90/* Per TCP/UDP port info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -050091typedef struct
92{
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 /* Port name (used as hash key). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050094 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 /* UDP/TCP port number in network byte order. */
97 u16 port;
98
99 /* Port specific format function. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500100 format_function_t *format_header;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
102 /* Parser for packet generator edits for this protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500103 unformat_function_t *unformat_pg_edit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104} tcp_udp_port_info_t;
105
Dave Barachd7cb1b52016-12-09 09:52:16 -0500106typedef struct
107{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 /* Per IP protocol info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500109 ip_protocol_info_t *protocol_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
111 /* Protocol info index hashed by 8 bit IP protocol. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112 uword *protocol_info_by_protocol;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
114 /* Hash table mapping IP protocol name (see protocols.def)
115 to protocol number. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500116 uword *protocol_info_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
118 /* Per TCP/UDP port info. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500119 tcp_udp_port_info_t *port_infos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
121 /* Hash table from network-byte-order port to port info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122 uword *port_info_by_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
124 /* Hash table mapping TCP/UDP name to port info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500125 uword *port_info_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126} ip_main_t;
127
128extern ip_main_t ip_main;
129
Dave Barachd7cb1b52016-12-09 09:52:16 -0500130clib_error_t *ip_main_init (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
132static inline ip_protocol_info_t *
133ip_get_protocol_info (ip_main_t * im, u32 protocol)
134{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500135 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136
137 p = hash_get (im->protocol_info_by_protocol, protocol);
138 return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0;
139}
140
141static inline tcp_udp_port_info_t *
142ip_get_tcp_udp_port_info (ip_main_t * im, u32 port)
143{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500144 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
146 p = hash_get (im->port_info_by_port, port);
147 return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
148}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500149
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150always_inline ip_csum_t
Dave Barachd7cb1b52016-12-09 09:52:16 -0500151ip_incremental_checksum_buffer (vlib_main_t * vm,
152 vlib_buffer_t * first_buffer,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 u32 first_buffer_offset,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500154 u32 n_bytes_to_checksum, ip_csum_t sum)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500156 vlib_buffer_t *b = first_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 u32 n_bytes_left = n_bytes_to_checksum;
158 ASSERT (b->current_length >= first_buffer_offset);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159 void *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 u32 n;
161
162 n = clib_min (n_bytes_left, b->current_length);
163 h = vlib_buffer_get_current (b) + first_buffer_offset;
164 sum = ip_incremental_checksum (sum, h, n);
165 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
166 {
167 while (1)
168 {
169 n_bytes_left -= n;
170 if (n_bytes_left == 0)
171 break;
172 b = vlib_get_buffer (vm, b->next_buffer);
173 n = clib_min (n_bytes_left, b->current_length);
174 h = vlib_buffer_get_current (b);
175 sum = ip_incremental_checksum (sum, h, n);
176 }
177 }
178
179 return sum;
180}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182void ip_del_all_interface_addresses (vlib_main_t * vm, u32 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Dave Barachf39ff742016-03-20 10:14:45 -0400184extern vlib_node_registration_t ip4_inacl_node;
185extern vlib_node_registration_t ip6_inacl_node;
186
Neale Ranns2297af02017-09-12 09:45:04 -0700187void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api,
188 const u8 * name);
Neale Ranns15002542017-09-10 04:39:11 -0700189
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700190void ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api);
Neale Ranns15002542017-09-10 04:39:11 -0700191
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700192int ip_table_bind (fib_protocol_t fproto, u32 sw_if_index,
193 u32 table_id, u8 is_api);
Neale Ranns15002542017-09-10 04:39:11 -0700194
Florin Corascea194d2017-10-02 00:18:51 -0700195u8 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4);
196u8 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4);
197u8 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4);
198u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);
199void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4);
200void ip_set (ip46_address_t * dst, void *src, u8 is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700201void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -0700202void ip4_address_normalize (ip4_address_t * ip4, u8 preflen);
203void ip6_address_normalize (ip6_address_t * ip6, u8 preflen);
204void ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip);
205u32 ip4_mask_to_preflen (ip4_address_t * mask);
206void ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
207 ip4_address_t * res);
208void ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
209 ip6_address_t * res);
210void ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask);
211u32 ip6_mask_to_preflen (ip6_address_t * mask);
Florin Corascea194d2017-10-02 00:18:51 -0700212
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213#endif /* included_ip_main_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500214
215/*
216 * fd.io coding-style-patch-verification: ON
217 *
218 * Local Variables:
219 * eval: (c-set-style "gnu")
220 * End:
221 */