f8ff777e275171baa5571ea951d64280261cbca1
[vpp.git] / vnet / vnet / ip / udp.h
1 /*
2  * ip/udp.h: udp protocol
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef included_udp_h
19 #define included_udp_h
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/udp_packet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ip/ip4.h>
25 #include <vnet/ip/ip4_packet.h>
26 #include <vnet/pg/pg.h>
27 #include <vnet/ip/format.h>
28
29 typedef enum {
30 #define udp_error(n,s) UDP_ERROR_##n,
31 #include <vnet/ip/udp_error.def>
32 #undef udp_error
33   UDP_N_ERROR,
34 } udp_error_t;
35
36 #define foreach_udp4_dst_port                   \
37 _ (67, dhcp_to_server)                          \
38 _ (68, dhcp_to_client)                          \
39 _ (500, ikev2)                                  \
40 _ (3784, bfd4)                                   \
41 _ (4341, lisp_gpe)                              \
42 _ (4342, lisp_cp)                               \
43 _ (4739, ipfix)                                 \
44 _ (4789, vxlan)                                 \
45 _ (4789, vxlan6)                                \
46 _ (4790, vxlan_gpe)                             \
47 _ (6633, vpath_3)
48
49
50 #define foreach_udp6_dst_port                   \
51 _ (547, dhcpv6_to_server)                       \
52 _ (546, dhcpv6_to_client)                       \
53 _ (3784, bfd6)                                   \
54 _ (4341, lisp_gpe6)                             \
55 _ (4342, lisp_cp6)                              \
56 _ (4790, vxlan6_gpe)      \
57 _ (6633, vpath6_3)
58
59 typedef enum {
60 #define _(n,f) UDP_DST_PORT_##f = n,
61   foreach_udp4_dst_port
62   foreach_udp6_dst_port
63 #undef _
64 } udp_dst_port_t;
65
66 typedef enum {
67 #define _(n,f) UDP6_DST_PORT_##f = n,
68   foreach_udp6_dst_port
69 #undef _
70 } udp6_dst_port_t;
71
72 typedef struct {
73   /* Name (a c string). */
74   char * name;
75
76   /* GRE protocol type in host byte order. */
77   udp_dst_port_t dst_port;
78
79   /* Node which handles this type. */
80   u32 node_index;
81
82   /* Next index for this type. */
83   u32 next_index;
84 } udp_dst_port_info_t;
85
86 typedef enum {
87   UDP_IP6 = 0,
88   UDP_IP4,                      /* the code is full of is_ip4... */
89   N_UDP_AF,
90 } udp_af_t;
91
92 typedef struct {
93   udp_dst_port_info_t * dst_port_infos [N_UDP_AF];
94
95   /* Hash tables mapping name/protocol to protocol info index. */
96   uword * dst_port_info_by_name[N_UDP_AF];
97   uword * dst_port_info_by_dst_port[N_UDP_AF];
98
99   /* convenience */
100   vlib_main_t * vlib_main;
101 } udp_main_t;
102
103 always_inline udp_dst_port_info_t *
104 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
105 {
106   uword * p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
107   return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
108 }
109
110 format_function_t format_udp_header;
111 format_function_t format_udp_rx_trace;
112
113 unformat_function_t unformat_udp_header;
114
115 void udp_register_dst_port (vlib_main_t * vm,
116                             udp_dst_port_t dst_port,
117                             u32 node_index, u8 is_ip4);
118
119 void udp_punt_unknown(vlib_main_t * vm, u8 is_ip4, u8 is_add);
120
121 always_inline void
122 ip_udp_fixup_one (vlib_main_t * vm,
123                   vlib_buffer_t * b0,
124                   u8 is_ip4)
125 {
126   u16 new_l0;
127   udp_header_t * udp0;
128
129   if (is_ip4)
130     {
131       ip4_header_t * ip0;
132       ip_csum_t sum0;
133       u16 old_l0 = 0;
134
135       ip0 = vlib_buffer_get_current(b0);
136
137       /* fix the <bleep>ing outer-IP checksum */
138       sum0 = ip0->checksum;
139       /* old_l0 always 0, see the rewrite setup */
140       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
141
142       sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
143                             length /* changed member */);
144       ip0->checksum = ip_csum_fold (sum0);
145       ip0->length = new_l0;
146
147       /* Fix UDP length */
148       udp0 = (udp_header_t *)(ip0+1);
149       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
150                                      - sizeof (*ip0));
151       udp0->length = new_l0;
152     }
153   else
154     {
155       ip6_header_t * ip0;
156       int bogus0;
157
158       ip0 = vlib_buffer_get_current(b0);
159
160       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
161                                      - sizeof (*ip0));
162       ip0->payload_length = new_l0;
163
164       /* Fix UDP length */
165       udp0 = (udp_header_t *)(ip0+1);
166       udp0->length = new_l0;
167
168       udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
169       ASSERT(bogus0 == 0);
170
171       if (udp0->checksum == 0)
172           udp0->checksum = 0xffff;
173     }
174 }
175 always_inline void
176 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
177                   u8 is_ip4)
178 {
179   vlib_buffer_advance (b0, - ec_len);
180
181   if (is_ip4)
182     {
183       ip4_header_t * ip0;
184
185       ip0 = vlib_buffer_get_current(b0);
186
187       /* Apply the encap string. */
188       clib_memcpy(ip0, ec0, ec_len);
189       ip_udp_fixup_one(vm, b0, 1);
190     }
191   else
192     {
193       ip6_header_t * ip0;
194
195       ip0 = vlib_buffer_get_current(b0);
196
197       /* Apply the encap string. */
198       clib_memcpy(ip0, ec0, ec_len);
199       ip_udp_fixup_one(vm, b0, 0);
200     }
201 }
202
203 always_inline void
204 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
205                   u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
206 {
207   u16 new_l0, new_l1;
208   udp_header_t * udp0, *udp1;
209
210   ASSERT(_vec_len(ec0) == _vec_len(ec1));
211
212   vlib_buffer_advance (b0, -ec_len);
213   vlib_buffer_advance (b1, -ec_len);
214
215   if (is_v4)
216     {
217       ip4_header_t * ip0, *ip1;
218       ip_csum_t sum0, sum1;
219       u16 old_l0 = 0, old_l1 = 0;
220
221       ip0 = vlib_buffer_get_current (b0);
222       ip1 = vlib_buffer_get_current (b1);
223
224       /* Apply the encap string */
225       clib_memcpy (ip0, ec0, ec_len);
226       clib_memcpy (ip1, ec1, ec_len);
227
228       /* fix the <bleep>ing outer-IP checksum */
229       sum0 = ip0->checksum;
230       sum1 = ip1->checksum;
231
232       /* old_l0 always 0, see the rewrite setup */
233       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
234       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
235
236       sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
237                             length /* changed member */);
238       sum1 = ip_csum_update(sum1, old_l1, new_l1, ip4_header_t,
239                             length /* changed member */);
240
241       ip0->checksum = ip_csum_fold (sum0);
242       ip1->checksum = ip_csum_fold (sum1);
243
244       ip0->length = new_l0;
245       ip1->length = new_l1;
246
247       /* Fix UDP length */
248       udp0 = (udp_header_t *) (ip0 + 1);
249       udp1 = (udp_header_t *) (ip1 + 1);
250
251       new_l0 = clib_host_to_net_u16 (
252           vlib_buffer_length_in_chain (vm, b0) - sizeof(*ip0));
253       new_l1 = clib_host_to_net_u16 (
254           vlib_buffer_length_in_chain (vm, b1) - sizeof(*ip1));
255       udp0->length = new_l0;
256       udp1->length = new_l1;
257     }
258   else
259     {
260       ip6_header_t * ip0, * ip1;
261       int bogus0, bogus1;
262
263       ip0 = vlib_buffer_get_current(b0);
264       ip1 = vlib_buffer_get_current(b1);
265
266       /* Apply the encap string. */
267       clib_memcpy(ip0, ec0, ec_len);
268       clib_memcpy(ip1, ec1, ec_len);
269
270       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
271                                      - sizeof (*ip0));
272       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
273                                      - sizeof (*ip1));
274       ip0->payload_length = new_l0;
275       ip1->payload_length = new_l1;
276
277       /* Fix UDP length */
278       udp0 = (udp_header_t *)(ip0+1);
279       udp1 = (udp_header_t *)(ip1+1);
280
281       udp0->length = new_l0;
282       udp1->length = new_l1;
283
284       udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
285       udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
286       ASSERT(bogus0 == 0);
287       ASSERT(bogus1 == 0);
288
289       if (udp0->checksum == 0)
290           udp0->checksum = 0xffff;
291       if (udp1->checksum == 0)
292           udp1->checksum = 0xffff;
293     }
294 }
295
296 #endif /* included_udp_h */