tcp: custom checksum calculations for Ipv4/Ipv6
[vpp.git] / src / vnet / ip / ip.h
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/format.h>
50 #include <vnet/ip/ip_packet.h>
51 #include <vnet/ip/lookup.h>
52
53 #include <vnet/tcp/tcp_packet.h>
54 #include <vnet/udp/udp_packet.h>
55 #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>
60 #include <vnet/ip/icmp4.h>
61
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>
66 #include <vnet/classify/vnet_classify.h>
67
68 typedef enum ip_address_family_t_
69 {
70   AF_IP4,
71   AF_IP6,
72 } ip_address_family_t;
73
74 extern uword unformat_ip_address_family (unformat_input_t * input,
75                                          va_list * args);
76 extern u8 *format_ip_address_family (u8 * s, va_list * args);
77
78 #define FOR_EACH_IP_ADDRESS_FAMILY(_af) \
79   for (_af = AF_IP4; _af <= AF_IP6; _af++)
80
81 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
82 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
83
84 /* Per protocol info. */
85 typedef struct
86 {
87   /* Protocol name (also used as hash key). */
88   u8 *name;
89
90   /* Protocol number. */
91   ip_protocol_t protocol;
92
93   /* Format function for this IP protocol. */
94   format_function_t *format_header;
95
96   /* Parser for header. */
97   unformat_function_t *unformat_header;
98
99   /* Parser for per-protocol matches. */
100   unformat_function_t *unformat_match;
101
102   /* Parser for packet generator edits for this protocol. */
103   unformat_function_t *unformat_pg_edit;
104 } ip_protocol_info_t;
105
106 /* Per TCP/UDP port info. */
107 typedef struct
108 {
109   /* Port name (used as hash key). */
110   u8 *name;
111
112   /* UDP/TCP port number in network byte order. */
113   u16 port;
114
115   /* Port specific format function. */
116   format_function_t *format_header;
117
118   /* Parser for packet generator edits for this protocol. */
119   unformat_function_t *unformat_pg_edit;
120 } tcp_udp_port_info_t;
121
122 typedef struct
123 {
124   /* Per IP protocol info. */
125   ip_protocol_info_t *protocol_infos;
126
127   /* Protocol info index hashed by 8 bit IP protocol. */
128   uword *protocol_info_by_protocol;
129
130   /* Hash table mapping IP protocol name (see protocols.def)
131      to protocol number. */
132   uword *protocol_info_by_name;
133
134   /* Per TCP/UDP port info. */
135   tcp_udp_port_info_t *port_infos;
136
137   /* Hash table from network-byte-order port to port info index. */
138   uword *port_info_by_port;
139
140   /* Hash table mapping TCP/UDP name to port info index. */
141   uword *port_info_by_name;
142 } ip_main_t;
143
144 extern ip_main_t ip_main;
145
146 clib_error_t *ip_main_init (vlib_main_t * vm);
147
148 static inline ip_protocol_info_t *
149 ip_get_protocol_info (ip_main_t * im, u32 protocol)
150 {
151   uword *p;
152
153   p = hash_get (im->protocol_info_by_protocol, protocol);
154   return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0;
155 }
156
157 static inline tcp_udp_port_info_t *
158 ip_get_tcp_udp_port_info (ip_main_t * im, u32 port)
159 {
160   uword *p;
161
162   p = hash_get (im->port_info_by_port, port);
163   return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
164 }
165
166 always_inline ip_csum_t
167 ip_incremental_checksum_buffer (vlib_main_t * vm,
168                                 vlib_buffer_t * first_buffer,
169                                 u32 first_buffer_offset,
170                                 u32 n_bytes_to_checksum, ip_csum_t sum)
171 {
172   vlib_buffer_t *b = first_buffer;
173   u32 n_bytes_left = n_bytes_to_checksum;
174   ASSERT (b->current_length >= first_buffer_offset);
175   void *h;
176   u32 n;
177
178   n = clib_min (n_bytes_left, b->current_length - first_buffer_offset);
179   h = vlib_buffer_get_current (b) + first_buffer_offset;
180   sum = ip_incremental_checksum (sum, h, n);
181   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
182     {
183       while (1)
184         {
185           n_bytes_left -= n;
186           if (n_bytes_left == 0)
187             break;
188           b = vlib_get_buffer (vm, b->next_buffer);
189           n = clib_min (n_bytes_left, b->current_length);
190           h = vlib_buffer_get_current (b);
191           sum = ip_incremental_checksum (sum, h, n);
192         }
193     }
194
195   return sum;
196 }
197
198 always_inline u16
199 ip_calculate_l4_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
200                           ip_csum_t sum0, u32 payload_length,
201                           u8 * iph, u32 ip_header_size, u8 * l4h)
202 {
203   u16 sum16;
204   u8 *data_this_buffer, length_odd;
205   u32 n_bytes_left, n_this_buffer, n_ip_bytes_this_buffer;
206
207   n_bytes_left = payload_length;
208
209   if (l4h)                      /* packet l4 header and no buffer chain involved */
210     {
211       ASSERT (p0 == NULL);
212       n_this_buffer = payload_length;
213       data_this_buffer = l4h;
214     }
215   else
216     {
217       ASSERT (p0);
218       if (iph)                  /* ip header pointer set to packet in buffer */
219         {
220           ASSERT (ip_header_size);
221           n_this_buffer = payload_length;
222           data_this_buffer = iph + ip_header_size;      /* at l4 header */
223           n_ip_bytes_this_buffer =
224             p0->current_length - (((u8 *) iph - p0->data) - p0->current_data);
225           if (PREDICT_FALSE (payload_length + ip_header_size >
226                              n_ip_bytes_this_buffer))
227             {
228               n_this_buffer = n_ip_bytes_this_buffer - ip_header_size;
229               if (PREDICT_FALSE (n_this_buffer >> 31))
230                 {               /*  error - ip header don't fit this buffer */
231                   ASSERT (0);
232                   return 0xfefe;
233                 }
234             }
235         }
236       else                      /* packet in buffer with no ip header  */
237         {                       /* buffer current pointer at l4 header */
238           n_this_buffer = p0->current_length;
239           data_this_buffer = vlib_buffer_get_current (p0);
240         }
241       n_this_buffer = clib_min (n_this_buffer, n_bytes_left);
242     }
243
244   while (1)
245     {
246       sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
247       n_bytes_left -= n_this_buffer;
248       if (n_bytes_left == 0)
249         break;
250
251       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
252         {
253           ASSERT (0);           /* error - more buffer expected */
254           return 0xfefe;
255         }
256
257       length_odd = (n_this_buffer & 1);
258
259       p0 = vlib_get_buffer (vm, p0->next_buffer);
260       data_this_buffer = vlib_buffer_get_current (p0);
261       n_this_buffer = clib_min (p0->current_length, n_bytes_left);
262
263       if (PREDICT_FALSE (length_odd))
264         {
265           /* Prepend a 0 byte to maintain 2-byte checksum alignment */
266           data_this_buffer--;
267           n_this_buffer++;
268           n_bytes_left++;
269           data_this_buffer[0] = 0;
270         }
271     }
272
273   sum16 = ~ip_csum_fold (sum0);
274   return sum16;
275 }
276
277 void ip_del_all_interface_addresses (vlib_main_t * vm, u32 sw_if_index);
278
279 extern vlib_node_registration_t ip4_inacl_node;
280 extern vlib_node_registration_t ip6_inacl_node;
281
282 void ip_table_create (fib_protocol_t fproto, u32 table_id, u8 is_api,
283                       const u8 * name);
284
285 void ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api);
286
287 int ip_table_bind (fib_protocol_t fproto, u32 sw_if_index,
288                    u32 table_id, u8 is_api);
289
290 u8 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4);
291 u8 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4);
292 u8 ip4_is_local_host (ip4_address_t * ip4_address);
293 u8 ip6_is_local_host (ip6_address_t * ip6_address);
294 u8 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4);
295 u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);
296 void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4);
297 void ip_set (ip46_address_t * dst, void *src, u8 is_ip4);
298 void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4);
299 void ip4_address_normalize (ip4_address_t * ip4, u8 preflen);
300 void ip6_address_normalize (ip6_address_t * ip6, u8 preflen);
301 void ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip);
302 u32 ip4_mask_to_preflen (ip4_address_t * mask);
303 void ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
304                                         ip4_address_t * res);
305 void ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
306                                         ip6_address_t * res);
307 void ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask);
308 u32 ip6_mask_to_preflen (ip6_address_t * mask);
309
310 #endif /* included_ip_main_h */
311
312 /*
313  * fd.io coding-style-patch-verification: ON
314  *
315  * Local Variables:
316  * eval: (c-set-style "gnu")
317  * End:
318  */