ipsec: support UDP encap/decap for NAT traversal
[vpp.git] / src / vnet / udp / udp.h
1 /*
2  * Copyright (c) 2017 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 #ifndef __included_udp_h__
16 #define __included_udp_h__
17
18 #include <vnet/vnet.h>
19 #include <vnet/udp/udp_packet.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ip/ip4.h>
22 #include <vnet/ip/ip4_packet.h>
23 #include <vnet/pg/pg.h>
24 #include <vnet/ip/format.h>
25
26 #include <vnet/ip/ip.h>
27 #include <vnet/session/transport.h>
28
29 typedef enum
30 {
31 #define udp_error(n,s) UDP_ERROR_##n,
32 #include <vnet/udp/udp_error.def>
33 #undef udp_error
34   UDP_N_ERROR,
35 } udp_error_t;
36
37 typedef struct
38 {
39   /** Required for pool_get_aligned */
40   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
41   transport_connection_t connection;    /**< must be first */
42   clib_spinlock_t rx_lock;              /**< rx fifo lock */
43   u8 is_connected;                      /**< connected mode */
44 } udp_connection_t;
45
46 #define foreach_udp4_dst_port                   \
47 _ (53, dns)                                     \
48 _ (67, dhcp_to_server)                          \
49 _ (68, dhcp_to_client)                          \
50 _ (500, ikev2)                                  \
51 _ (2152, GTPU)                                  \
52 _ (3784, bfd4)                                  \
53 _ (3785, bfd_echo4)                             \
54 _ (4341, lisp_gpe)                              \
55 _ (4342, lisp_cp)                               \
56 _ (4500, ipsec)                                 \
57 _ (4739, ipfix)                                 \
58 _ (4789, vxlan)                                 \
59 _ (4789, vxlan6)                                \
60 _ (4790, VXLAN_GPE)                             \
61 _ (6633, vpath_3)                               \
62 _ (6081, geneve)                                \
63 _ (53053, dns_reply)
64
65
66 #define foreach_udp6_dst_port                   \
67 _ (53, dns6)                                    \
68 _ (547, dhcpv6_to_server)                       \
69 _ (546, dhcpv6_to_client)                       \
70 _ (2152, GTPU6)                                 \
71 _ (3784, bfd6)                                  \
72 _ (3785, bfd_echo6)                             \
73 _ (4341, lisp_gpe6)                             \
74 _ (4342, lisp_cp6)                              \
75 _ (4790, VXLAN6_GPE)                            \
76 _ (6633, vpath6_3)                              \
77 _ (6081, geneve6)                               \
78 _ (8138, BIER)                                  \
79 _ (53053, dns_reply6)
80
81 typedef enum
82 {
83 #define _(n,f) UDP_DST_PORT_##f = n,
84   foreach_udp4_dst_port foreach_udp6_dst_port
85 #undef _
86 } udp_dst_port_t;
87
88 typedef enum
89 {
90 #define _(n,f) UDP6_DST_PORT_##f = n,
91   foreach_udp6_dst_port
92 #undef _
93 } udp6_dst_port_t;
94
95 typedef struct
96 {
97   /* Name (a c string). */
98   char *name;
99
100   /* GRE protocol type in host byte order. */
101   udp_dst_port_t dst_port;
102
103   /* Node which handles this type. */
104   u32 node_index;
105
106   /* Next index for this type. */
107   u32 next_index;
108 } udp_dst_port_info_t;
109
110 typedef enum
111 {
112   UDP_IP6 = 0,
113   UDP_IP4,                      /* the code is full of is_ip4... */
114   N_UDP_AF,
115 } udp_af_t;
116
117 typedef struct
118 {
119   udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
120
121   /* Hash tables mapping name/protocol to protocol info index. */
122   uword *dst_port_info_by_name[N_UDP_AF];
123   uword *dst_port_info_by_dst_port[N_UDP_AF];
124
125   /* Sparse vector mapping udp dst_port in network byte order
126      to next index. */
127   u16 *next_by_dst_port4;
128   u16 *next_by_dst_port6;
129   u8 punt_unknown4;
130   u8 punt_unknown6;
131
132   /*
133    * Per-worker thread udp connection pools used with session layer
134    */
135   udp_connection_t **connections;
136   u32 *connection_peekers;
137   clib_spinlock_t *peekers_readers_locks;
138   clib_spinlock_t *peekers_write_locks;
139   udp_connection_t *listener_pool;
140
141 } udp_main_t;
142
143 extern udp_main_t udp_main;
144 extern vlib_node_registration_t udp4_input_node;
145 extern vlib_node_registration_t udp6_input_node;
146
147 always_inline udp_connection_t *
148 udp_connection_get (u32 conn_index, u32 thread_index)
149 {
150   if (pool_is_free_index (udp_main.connections[thread_index], conn_index))
151     return 0;
152   return pool_elt_at_index (udp_main.connections[thread_index], conn_index);
153 }
154
155 always_inline udp_connection_t *
156 udp_listener_get (u32 conn_index)
157 {
158   return pool_elt_at_index (udp_main.listener_pool, conn_index);
159 }
160
161 always_inline udp_main_t *
162 vnet_get_udp_main ()
163 {
164   return &udp_main;
165 }
166
167 always_inline udp_connection_t *
168 udp_get_connection_from_transport (transport_connection_t * tc)
169 {
170   return ((udp_connection_t *) tc);
171 }
172
173 always_inline u32
174 udp_connection_index (udp_connection_t * uc)
175 {
176   return (uc - udp_main.connections[uc->c_thread_index]);
177 }
178
179 udp_connection_t *udp_connection_alloc (u32 thread_index);
180
181 /**
182  * Acquires a lock that blocks a connection pool from expanding.
183  */
184 always_inline void
185 udp_pool_add_peeker (u32 thread_index)
186 {
187   if (thread_index != vlib_get_thread_index ())
188     return;
189   clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
190   udp_main.connection_peekers[thread_index] += 1;
191   if (udp_main.connection_peekers[thread_index] == 1)
192     clib_spinlock_lock_if_init (&udp_main.peekers_write_locks[thread_index]);
193   clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
194                                 [thread_index]);
195 }
196
197 always_inline void
198 udp_pool_remove_peeker (u32 thread_index)
199 {
200   if (thread_index != vlib_get_thread_index ())
201     return;
202   ASSERT (udp_main.connection_peekers[thread_index] > 0);
203   clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
204   udp_main.connection_peekers[thread_index] -= 1;
205   if (udp_main.connection_peekers[thread_index] == 0)
206     clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
207                                   [thread_index]);
208   clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
209                                 [thread_index]);
210 }
211
212 always_inline udp_connection_t *
213 udp_connection_clone_safe (u32 connection_index, u32 thread_index)
214 {
215   udp_connection_t *old_c, *new_c;
216   u32 current_thread_index = vlib_get_thread_index ();
217   new_c = udp_connection_alloc (current_thread_index);
218
219   /* If during the memcpy pool is reallocated AND the memory allocator
220    * decides to give the old chunk of memory to somebody in a hurry to
221    * scribble something on it, we have a problem. So add this thread as
222    * a session pool peeker.
223    */
224   udp_pool_add_peeker (thread_index);
225   old_c = udp_main.connections[thread_index] + connection_index;
226   clib_memcpy (new_c, old_c, sizeof (*new_c));
227   udp_pool_remove_peeker (thread_index);
228   new_c->c_thread_index = current_thread_index;
229   new_c->c_c_index = udp_connection_index (new_c);
230   return new_c;
231 }
232
233
234 always_inline udp_dst_port_info_t *
235 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
236 {
237   uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
238   return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
239 }
240
241 format_function_t format_udp_header;
242 format_function_t format_udp_rx_trace;
243 unformat_function_t unformat_udp_header;
244
245 void udp_register_dst_port (vlib_main_t * vm,
246                             udp_dst_port_t dst_port,
247                             u32 node_index, u8 is_ip4);
248 void udp_unregister_dst_port (vlib_main_t * vm,
249                               udp_dst_port_t dst_port, u8 is_ip4);
250
251 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
252
253 always_inline void *
254 vlib_buffer_push_udp (vlib_buffer_t * b, u16 sp, u16 dp, u8 offload_csum)
255 {
256   udp_header_t *uh;
257
258   uh = vlib_buffer_push_uninit (b, sizeof (udp_header_t));
259   uh->src_port = sp;
260   uh->dst_port = dp;
261   uh->checksum = 0;
262   uh->length = clib_host_to_net_u16 (b->current_length);
263   if (offload_csum)
264     {
265       b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
266       vnet_buffer (b)->l4_hdr_offset = (u8 *) uh - b->data;
267     }
268   return uh;
269 }
270
271 always_inline void
272 ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
273 {
274   u16 new_l0;
275   udp_header_t *udp0;
276
277   if (is_ip4)
278     {
279       ip4_header_t *ip0;
280       ip_csum_t sum0;
281       u16 old_l0 = 0;
282
283       ip0 = vlib_buffer_get_current (b0);
284
285       /* fix the <bleep>ing outer-IP checksum */
286       sum0 = ip0->checksum;
287       /* old_l0 always 0, see the rewrite setup */
288       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
289
290       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
291                              length /* changed member */ );
292       ip0->checksum = ip_csum_fold (sum0);
293       ip0->length = new_l0;
294
295       /* Fix UDP length */
296       udp0 = (udp_header_t *) (ip0 + 1);
297       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
298                                      - sizeof (*ip0));
299       udp0->length = new_l0;
300     }
301   else
302     {
303       ip6_header_t *ip0;
304       int bogus0;
305
306       ip0 = vlib_buffer_get_current (b0);
307
308       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
309                                      - sizeof (*ip0));
310       ip0->payload_length = new_l0;
311
312       /* Fix UDP length */
313       udp0 = (udp_header_t *) (ip0 + 1);
314       udp0->length = new_l0;
315
316       udp0->checksum =
317         ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
318       ASSERT (bogus0 == 0);
319
320       if (udp0->checksum == 0)
321         udp0->checksum = 0xffff;
322     }
323 }
324
325 always_inline void
326 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
327                   u8 is_ip4)
328 {
329   vlib_buffer_advance (b0, -ec_len);
330
331   if (is_ip4)
332     {
333       ip4_header_t *ip0;
334
335       ip0 = vlib_buffer_get_current (b0);
336
337       /* Apply the encap string. */
338       clib_memcpy (ip0, ec0, ec_len);
339       ip_udp_fixup_one (vm, b0, 1);
340     }
341   else
342     {
343       ip6_header_t *ip0;
344
345       ip0 = vlib_buffer_get_current (b0);
346
347       /* Apply the encap string. */
348       clib_memcpy (ip0, ec0, ec_len);
349       ip_udp_fixup_one (vm, b0, 0);
350     }
351 }
352
353 always_inline void
354 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
355                   u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
356 {
357   u16 new_l0, new_l1;
358   udp_header_t *udp0, *udp1;
359
360   ASSERT (_vec_len (ec0) == _vec_len (ec1));
361
362   vlib_buffer_advance (b0, -ec_len);
363   vlib_buffer_advance (b1, -ec_len);
364
365   if (is_v4)
366     {
367       ip4_header_t *ip0, *ip1;
368       ip_csum_t sum0, sum1;
369       u16 old_l0 = 0, old_l1 = 0;
370
371       ip0 = vlib_buffer_get_current (b0);
372       ip1 = vlib_buffer_get_current (b1);
373
374       /* Apply the encap string */
375       clib_memcpy (ip0, ec0, ec_len);
376       clib_memcpy (ip1, ec1, ec_len);
377
378       /* fix the <bleep>ing outer-IP checksum */
379       sum0 = ip0->checksum;
380       sum1 = ip1->checksum;
381
382       /* old_l0 always 0, see the rewrite setup */
383       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
384       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
385
386       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
387                              length /* changed member */ );
388       sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
389                              length /* changed member */ );
390
391       ip0->checksum = ip_csum_fold (sum0);
392       ip1->checksum = ip_csum_fold (sum1);
393
394       ip0->length = new_l0;
395       ip1->length = new_l1;
396
397       /* Fix UDP length */
398       udp0 = (udp_header_t *) (ip0 + 1);
399       udp1 = (udp_header_t *) (ip1 + 1);
400
401       new_l0 =
402         clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
403                               sizeof (*ip0));
404       new_l1 =
405         clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
406                               sizeof (*ip1));
407       udp0->length = new_l0;
408       udp1->length = new_l1;
409     }
410   else
411     {
412       ip6_header_t *ip0, *ip1;
413       int bogus0, bogus1;
414
415       ip0 = vlib_buffer_get_current (b0);
416       ip1 = vlib_buffer_get_current (b1);
417
418       /* Apply the encap string. */
419       clib_memcpy (ip0, ec0, ec_len);
420       clib_memcpy (ip1, ec1, ec_len);
421
422       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
423                                      - sizeof (*ip0));
424       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
425                                      - sizeof (*ip1));
426       ip0->payload_length = new_l0;
427       ip1->payload_length = new_l1;
428
429       /* Fix UDP length */
430       udp0 = (udp_header_t *) (ip0 + 1);
431       udp1 = (udp_header_t *) (ip1 + 1);
432
433       udp0->length = new_l0;
434       udp1->length = new_l1;
435
436       udp0->checksum =
437         ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
438       udp1->checksum =
439         ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
440       ASSERT (bogus0 == 0);
441       ASSERT (bogus1 == 0);
442
443       if (udp0->checksum == 0)
444         udp0->checksum = 0xffff;
445       if (udp1->checksum == 0)
446         udp1->checksum = 0xffff;
447     }
448 }
449
450 /*
451  * fd.io coding-style-patch-verification: ON
452  *
453  * Local Variables:
454  * eval: (c-set-style "gnu")
455  * End:
456  */
457
458 #endif /* __included_udp_h__ */