VPP-1027: DNS name resolver
[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 struct
30 {
31   transport_connection_t connection;          /** must be first */
32
33   /** ersatz MTU to limit fifo pushes to test data size */
34   u32 mtu;
35 } udp_connection_t;
36
37 typedef struct _udp_uri_main
38 {
39   /* Per-worker thread udp connection pools */
40   udp_connection_t **udp_sessions;
41   udp_connection_t *udp_listeners;
42
43   /* convenience */
44   vlib_main_t *vlib_main;
45   vnet_main_t *vnet_main;
46   ip4_main_t *ip4_main;
47   ip6_main_t *ip6_main;
48 } udp_uri_main_t;
49
50 extern udp_uri_main_t udp_uri_main;
51 extern vlib_node_registration_t udp4_uri_input_node;
52
53 always_inline udp_uri_main_t *
54 vnet_get_udp_main ()
55 {
56   return &udp_uri_main;
57 }
58
59 always_inline udp_connection_t *
60 udp_connection_get (u32 conn_index, u32 thread_index)
61 {
62   return pool_elt_at_index (udp_uri_main.udp_sessions[thread_index],
63                             conn_index);
64 }
65
66 always_inline udp_connection_t *
67 udp_listener_get (u32 conn_index)
68 {
69   return pool_elt_at_index (udp_uri_main.udp_listeners, conn_index);
70 }
71
72 typedef enum
73 {
74 #define udp_error(n,s) UDP_ERROR_##n,
75 #include <vnet/udp/udp_error.def>
76 #undef udp_error
77   UDP_N_ERROR,
78 } udp_error_t;
79
80 #define foreach_udp4_dst_port                   \
81 _ (53, dns)                                     \
82 _ (67, dhcp_to_server)                          \
83 _ (68, dhcp_to_client)                          \
84 _ (500, ikev2)                                  \
85 _ (2152, GTPU)                                  \
86 _ (3784, bfd4)                                  \
87 _ (3785, bfd_echo4)                             \
88 _ (4341, lisp_gpe)                              \
89 _ (4342, lisp_cp)                               \
90 _ (4739, ipfix)                                 \
91 _ (4789, vxlan)                                 \
92 _ (4789, vxlan6)                                \
93 _ (4790, VXLAN_GPE)                             \
94 _ (6633, vpath_3)                               \
95 _ (6081, geneve)                                \
96 _ (53053, dns_reply)
97
98
99 #define foreach_udp6_dst_port                   \
100 _ (53, dns6)                                    \
101 _ (547, dhcpv6_to_server)                       \
102 _ (546, dhcpv6_to_client)                       \
103 _ (2152, GTPU6)                                 \
104 _ (3784, bfd6)                                  \
105 _ (3785, bfd_echo6)                             \
106 _ (4341, lisp_gpe6)                             \
107 _ (4342, lisp_cp6)                              \
108 _ (4790, VXLAN6_GPE)                            \
109 _ (6633, vpath6_3)                              \
110 _ (6081, geneve6)                               \
111 _ (53053, dns_reply6)
112
113 typedef enum
114 {
115 #define _(n,f) UDP_DST_PORT_##f = n,
116   foreach_udp4_dst_port foreach_udp6_dst_port
117 #undef _
118 } udp_dst_port_t;
119
120 typedef enum
121 {
122 #define _(n,f) UDP6_DST_PORT_##f = n,
123   foreach_udp6_dst_port
124 #undef _
125 } udp6_dst_port_t;
126
127 typedef struct
128 {
129   /* Name (a c string). */
130   char *name;
131
132   /* GRE protocol type in host byte order. */
133   udp_dst_port_t dst_port;
134
135   /* Node which handles this type. */
136   u32 node_index;
137
138   /* Next index for this type. */
139   u32 next_index;
140 } udp_dst_port_info_t;
141
142 typedef enum
143 {
144   UDP_IP6 = 0,
145   UDP_IP4,                      /* the code is full of is_ip4... */
146   N_UDP_AF,
147 } udp_af_t;
148
149 typedef struct
150 {
151   udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
152
153   /* Hash tables mapping name/protocol to protocol info index. */
154   uword *dst_port_info_by_name[N_UDP_AF];
155   uword *dst_port_info_by_dst_port[N_UDP_AF];
156
157   /* Sparse vector mapping udp dst_port in network byte order
158      to next index. */
159   u16 *next_by_dst_port4;
160   u16 *next_by_dst_port6;
161   u8 punt_unknown4;
162   u8 punt_unknown6;
163
164   /* convenience */
165   vlib_main_t *vlib_main;
166 } udp_main_t;
167
168 always_inline udp_dst_port_info_t *
169 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
170 {
171   uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
172   return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
173 }
174
175 format_function_t format_udp_header;
176 format_function_t format_udp_rx_trace;
177
178 unformat_function_t unformat_udp_header;
179
180 void udp_register_dst_port (vlib_main_t * vm,
181                             udp_dst_port_t dst_port,
182                             u32 node_index, u8 is_ip4);
183
184 void
185 udp_unregister_dst_port (vlib_main_t * vm,
186                          udp_dst_port_t dst_port, u8 is_ip4);
187
188 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
189
190 always_inline void
191 ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
192 {
193   u16 new_l0;
194   udp_header_t *udp0;
195
196   if (is_ip4)
197     {
198       ip4_header_t *ip0;
199       ip_csum_t sum0;
200       u16 old_l0 = 0;
201
202       ip0 = vlib_buffer_get_current (b0);
203
204       /* fix the <bleep>ing outer-IP checksum */
205       sum0 = ip0->checksum;
206       /* old_l0 always 0, see the rewrite setup */
207       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
208
209       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
210                              length /* changed member */ );
211       ip0->checksum = ip_csum_fold (sum0);
212       ip0->length = new_l0;
213
214       /* Fix UDP length */
215       udp0 = (udp_header_t *) (ip0 + 1);
216       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
217                                      - sizeof (*ip0));
218       udp0->length = new_l0;
219     }
220   else
221     {
222       ip6_header_t *ip0;
223       int bogus0;
224
225       ip0 = vlib_buffer_get_current (b0);
226
227       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
228                                      - sizeof (*ip0));
229       ip0->payload_length = new_l0;
230
231       /* Fix UDP length */
232       udp0 = (udp_header_t *) (ip0 + 1);
233       udp0->length = new_l0;
234
235       udp0->checksum =
236         ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
237       ASSERT (bogus0 == 0);
238
239       if (udp0->checksum == 0)
240         udp0->checksum = 0xffff;
241     }
242 }
243
244 always_inline void
245 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
246                   u8 is_ip4)
247 {
248   vlib_buffer_advance (b0, -ec_len);
249
250   if (is_ip4)
251     {
252       ip4_header_t *ip0;
253
254       ip0 = vlib_buffer_get_current (b0);
255
256       /* Apply the encap string. */
257       clib_memcpy (ip0, ec0, ec_len);
258       ip_udp_fixup_one (vm, b0, 1);
259     }
260   else
261     {
262       ip6_header_t *ip0;
263
264       ip0 = vlib_buffer_get_current (b0);
265
266       /* Apply the encap string. */
267       clib_memcpy (ip0, ec0, ec_len);
268       ip_udp_fixup_one (vm, b0, 0);
269     }
270 }
271
272 always_inline void
273 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
274                   u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
275 {
276   u16 new_l0, new_l1;
277   udp_header_t *udp0, *udp1;
278
279   ASSERT (_vec_len (ec0) == _vec_len (ec1));
280
281   vlib_buffer_advance (b0, -ec_len);
282   vlib_buffer_advance (b1, -ec_len);
283
284   if (is_v4)
285     {
286       ip4_header_t *ip0, *ip1;
287       ip_csum_t sum0, sum1;
288       u16 old_l0 = 0, old_l1 = 0;
289
290       ip0 = vlib_buffer_get_current (b0);
291       ip1 = vlib_buffer_get_current (b1);
292
293       /* Apply the encap string */
294       clib_memcpy (ip0, ec0, ec_len);
295       clib_memcpy (ip1, ec1, ec_len);
296
297       /* fix the <bleep>ing outer-IP checksum */
298       sum0 = ip0->checksum;
299       sum1 = ip1->checksum;
300
301       /* old_l0 always 0, see the rewrite setup */
302       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
303       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
304
305       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
306                              length /* changed member */ );
307       sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
308                              length /* changed member */ );
309
310       ip0->checksum = ip_csum_fold (sum0);
311       ip1->checksum = ip_csum_fold (sum1);
312
313       ip0->length = new_l0;
314       ip1->length = new_l1;
315
316       /* Fix UDP length */
317       udp0 = (udp_header_t *) (ip0 + 1);
318       udp1 = (udp_header_t *) (ip1 + 1);
319
320       new_l0 =
321         clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
322                               sizeof (*ip0));
323       new_l1 =
324         clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
325                               sizeof (*ip1));
326       udp0->length = new_l0;
327       udp1->length = new_l1;
328     }
329   else
330     {
331       ip6_header_t *ip0, *ip1;
332       int bogus0, bogus1;
333
334       ip0 = vlib_buffer_get_current (b0);
335       ip1 = vlib_buffer_get_current (b1);
336
337       /* Apply the encap string. */
338       clib_memcpy (ip0, ec0, ec_len);
339       clib_memcpy (ip1, ec1, ec_len);
340
341       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
342                                      - sizeof (*ip0));
343       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
344                                      - sizeof (*ip1));
345       ip0->payload_length = new_l0;
346       ip1->payload_length = new_l1;
347
348       /* Fix UDP length */
349       udp0 = (udp_header_t *) (ip0 + 1);
350       udp1 = (udp_header_t *) (ip1 + 1);
351
352       udp0->length = new_l0;
353       udp1->length = new_l1;
354
355       udp0->checksum =
356         ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
357       udp1->checksum =
358         ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
359       ASSERT (bogus0 == 0);
360       ASSERT (bogus1 == 0);
361
362       if (udp0->checksum == 0)
363         udp0->checksum = 0xffff;
364       if (udp1->checksum == 0)
365         udp1->checksum = 0xffff;
366     }
367 }
368
369 /*
370  * fd.io coding-style-patch-verification: ON
371  *
372  * Local Variables:
373  * eval: (c-set-style "gnu")
374  * End:
375  */
376
377 #endif /* __included_udp_h__ */