FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ip.h
Go to the documentation of this file.
1 /*
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>
44 #include <vppinfra/heap.h> /* adjacency heap */
45 #include <vppinfra/ptclosure.h>
46 
47 #include <vnet/vnet.h>
48 
49 #include <vnet/ip/ip_types.h>
50 #include <vnet/ip/format.h>
51 #include <vnet/ip/ip_packet.h>
52 #include <vnet/ip/lookup.h>
53 #include <vnet/ip/ip_interface.h>
54 
55 #include <vnet/tcp/tcp_packet.h>
56 #include <vnet/udp/udp_packet.h>
57 #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>
62 #include <vnet/ip/icmp4.h>
63 
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>
68 
69 /* Per protocol info. */
70 typedef struct
71 {
72  /* Protocol name (also used as hash key). */
73  u8 *name;
74 
75  /* Protocol number. */
77 
78  /* Format function for this IP protocol. */
80 
81  /* Parser for header. */
83 
84  /* Parser for per-protocol matches. */
86 
87  /* Parser for packet generator edits for this protocol. */
90 
91 /* Per TCP/UDP port info. */
92 typedef struct
93 {
94  /* Port name (used as hash key). */
95  u8 *name;
96 
97  /* UDP/TCP port number in network byte order. */
99 
100  /* Port specific format function. */
102 
103  /* Parser for packet generator edits for this protocol. */
106 
107 typedef struct
108 {
109  /* Per IP protocol info. */
111 
112  /* Protocol info index hashed by 8 bit IP protocol. */
114 
115  /* Hash table mapping IP protocol name (see protocols.def)
116  to protocol number. */
118 
119  /* Per TCP/UDP port info. */
121 
122  /* Hash table from network-byte-order port to port info index. */
124 
125  /* Hash table mapping TCP/UDP name to port info index. */
127 } ip_main_t;
128 
129 extern ip_main_t ip_main;
130 
132 
133 static inline ip_protocol_info_t *
135 {
136  uword *p;
137 
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 
142 static inline tcp_udp_port_info_t *
144 {
145  uword *p;
146 
147  p = hash_get (im->port_info_by_port, port);
148  return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
149 }
150 
153  vlib_buffer_t * first_buffer,
154  u32 first_buffer_offset,
155  u32 n_bytes_to_checksum, ip_csum_t sum)
156 {
157  vlib_buffer_t *b = first_buffer;
158  u32 n_bytes_left = n_bytes_to_checksum;
159  ASSERT (b->current_length >= first_buffer_offset);
160  void *h;
161  u32 n;
162 
163  n = clib_min (n_bytes_left, b->current_length - first_buffer_offset);
164  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;
174  n = clib_min (n_bytes_left, b->current_length);
176  sum = ip_incremental_checksum (sum, h, n);
177  }
178  }
179 
180  return sum;
181 }
182 
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 */
216  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  {
237  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 
261 
264 
265 void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api,
266  const u8 * name);
267 
268 void ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api);
269 
271  u32 table_id, u8 is_api);
272 
273 u8 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4);
274 u8 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4);
276 u8 ip6_is_local_host (ip6_address_t * ip6_address);
277 u8 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4);
278 void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4);
279 void ip_set (ip46_address_t * dst, void *src, u8 is_ip4);
280 
284  const char *feature,
285  u32 sw_if_index, int enable_disable,
286  void *feature_config,
287  u32 n_feature_config_bytes);
288 
293 {
294  return (is_ip4 ? vlib_buffer_get_ip4_fib_index
296 }
297 
298 #endif /* included_ip_main_h */
299 
300 /*
301  * fd.io coding-style-patch-verification: ON
302  *
303  * Local Variables:
304  * eval: (c-set-style "gnu")
305  * End:
306  */
vlib_buffer_t::next_buffer
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:149
im
vnet_interface_main_t * im
Definition: interface_output.c:395
ip_table_delete
void ip_table_delete(fib_protocol_t fproto, u32 table_id, u8 is_api)
Definition: ip_api.c:574
tcp_packet.h
ip_protocol_info_t::name
u8 * name
Definition: ip.h:73
ip_copy
void ip_copy(ip46_address_t *dst, ip46_address_t *src, u8 is_ip4)
Definition: ip.c:83
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
ip_protocol_info_t::unformat_match
unformat_function_t * unformat_match
Definition: ip.h:85
name
string name[64]
Definition: fib.api:25
ip6_is_local_host
u8 ip6_is_local_host(ip6_address_t *ip6_address)
Definition: ip.c:47
ip_main
ip_main_t ip_main
Definition: ip_init.c:42
icmp4.h
ip_get_protocol_info
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:134
ip_protocol_info_t::format_header
format_function_t * format_header
Definition: ip.h:79
u16
unsigned short u16
Definition: types.h:57
ip_incremental_checksum_buffer
static ip_csum_t ip_incremental_checksum_buffer(vlib_main_t *vm, vlib_buffer_t *first_buffer, u32 first_buffer_offset, u32 n_bytes_to_checksum, ip_csum_t sum)
Definition: ip.h:152
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
ip_main_t::protocol_infos
ip_protocol_info_t * protocol_infos
Definition: ip.h:110
ip_is_local_host
u8 ip_is_local_host(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:31
port
u16 port
Definition: lb_types.api:73
ip_packet.h
ip_calculate_l4_checksum
static u16 ip_calculate_l4_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip_csum_t sum0, u32 payload_length, u8 *iph, u32 ip_header_size, u8 *l4h)
Definition: ip.h:184
ip_set
void ip_set(ip46_address_t *dst, void *src, u8 is_ip4)
Definition: ip.c:95
ip_main_t::port_infos
tcp_udp_port_info_t * port_infos
Definition: ip.h:120
h
h
Definition: flowhash_template.h:372
ip6_packet.h
ip_main_t::protocol_info_by_protocol
uword * protocol_info_by_protocol
Definition: ip.h:113
vlib_buffer_t::current_data
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:119
ip_protocol_info_t::protocol
ip_protocol_t protocol
Definition: ip.h:76
ip_main_t::protocol_info_by_name
uword * protocol_info_by_name
Definition: ip.h:117
vlib_buffer_get_ip_fib_index
static u32 vlib_buffer_get_ip_fib_index(vlib_buffer_t *b, u8 is_ip4)
Definition: ip.h:292
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
format.h
icmp6.h
ip_incremental_checksum
static ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_packet.h:319
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ip4_is_local_host
u8 ip4_is_local_host(ip4_address_t *ip4_address)
Definition: ip.c:41
tcp_udp_port_info_t::format_header
format_function_t * format_header
Definition: ip.h:101
ip4_inacl_node
vlib_node_registration_t ip4_inacl_node
(constructor) VLIB_REGISTER_NODE (ip4_inacl_node)
Definition: ip_in_out_acl.c:833
udp_packet.h
uword
u64 uword
Definition: types.h:112
ip_is_local
u8 ip_is_local(u32 fib_index, ip46_address_t *ip46_address, u8 is_ip4)
Checks that an ip is local to the requested fib.
Definition: ip.c:57
hash_get
#define hash_get(h, key)
Definition: hash.h:249
src
vl_api_address_t src
Definition: gre.api:54
fib_protocol_t
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
ptclosure.h
ip_types.h
ip4_address_t
Definition: ip4_packet.h:50
clib_min
#define clib_min(x, y)
Definition: clib.h:342
vlib_buffer_get_ip6_fib_index
static u32 vlib_buffer_get_ip6_fib_index(vlib_buffer_t *b)
ip_main_init
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vlib_buffer_t::current_length
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
ip_protocol_info_t::unformat_header
unformat_function_t * unformat_header
Definition: ip.h:82
ip_get_tcp_udp_port_info
static tcp_udp_port_info_t * ip_get_tcp_udp_port_info(ip_main_t *im, u32 port)
Definition: ip.h:143
ip_feature_enable_disable
void ip_feature_enable_disable(ip_address_family_t af, ip_sub_address_family_t safi, ip_feature_location_t loc, const char *feature, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: ip.c:163
ip_table_create
void ip_table_create(fib_protocol_t fproto, u32 table_id, u8 is_api, const u8 *name)
Definition: ip_api.c:884
tcp_udp_port_info_t::name
u8 * name
Definition: ip.h:95
ip4_packet.h
format_function_t
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
ip6_error.h
ip_main_t::port_info_by_port
uword * port_info_by_port
Definition: ip.h:123
tcp_udp_port_info_t
Definition: ip.h:92
ip6_inacl_node
vlib_node_registration_t ip6_inacl_node
(constructor) VLIB_REGISTER_NODE (ip6_inacl_node)
Definition: ip_in_out_acl.c:911
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
ip_protocol_info_t
Definition: ip.h:70
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
ip_table_bind
int ip_table_bind(fib_protocol_t fproto, u32 sw_if_index, u32 table_id, u8 is_api)
Definition: interface_api.c:482
table_id
u32 table_id
Definition: wireguard.api:102
ip4_address
manual_print typedef u8 ip4_address[4]
Definition: ip_types.api:18
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
ip6_address
manual_print typedef u8 ip6_address[16]
Definition: ip_types.api:19
tcp_udp_port_info_t::port
u16 port
Definition: ip.h:98
unformat_function_t
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:225
ip_is_zero
u8 ip_is_zero(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:22
ip4.h
ip_protocol_t
enum ip_protocol ip_protocol_t
ip_interface.h
IP prefix management on interfaces.
vlib_main_t
Definition: main.h:102
hash.h
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
lookup.h
icmp46_packet.h
ip_csum_t
uword ip_csum_t
Definition: ip_packet.h:245
ip_sub_address_family_t
enum ip_sub_address_family_t_ ip_sub_address_family_t
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
ip4_error.h
tcp_udp_port_info_t::unformat_pg_edit
unformat_function_t * unformat_pg_edit
Definition: ip.h:104
ip_main_t
Definition: ip.h:107
vnet.h
ip_feature_location_t
enum ip_feature_location_t_ ip_feature_location_t
ip_main_t::port_info_by_name
uword * port_info_by_name
Definition: ip.h:126
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
vlib_buffer_get_ip4_fib_index
static u32 vlib_buffer_get_ip4_fib_index(vlib_buffer_t *b)
ip6.h
ip_csum_fold
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:301
ip_protocol_info_t::unformat_pg_edit
unformat_function_t * unformat_pg_edit
Definition: ip.h:88
ip_del_all_interface_addresses
void ip_del_all_interface_addresses(vlib_main_t *vm, u32 sw_if_index)
Definition: ip46_cli.c:82
heap.h
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
ip_address_family_t
enum ip_address_family_t_ ip_address_family_t
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111