FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
udp.h
Go to the documentation of this file.
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 _ (4341, lisp_gpe) \
41 _ (4342, lisp_cp) \
42 _ (4739, ipfix) \
43 _ (4789, vxlan) \
44 _ (4789, vxlan6) \
45 _ (4790, vxlan_gpe) \
46 _ (6633, vpath_3)
47 
48 
49 #define foreach_udp6_dst_port \
50 _ (547, dhcpv6_to_server) \
51 _ (546, dhcpv6_to_client) \
52 _ (4341, lisp_gpe6) \
53 _ (4342, lisp_cp6) \
54 _ (4790, vxlan6_gpe) \
55 _ (6633, vpath6_3)
56 
57 typedef enum {
58 #define _(n,f) UDP_DST_PORT_##f = n,
61 #undef _
63 
64 typedef enum {
65 #define _(n,f) UDP6_DST_PORT_##f = n,
67 #undef _
69 
70 typedef struct {
71  /* Name (a c string). */
72  char * name;
73 
74  /* GRE protocol type in host byte order. */
76 
77  /* Node which handles this type. */
79 
80  /* Next index for this type. */
83 
84 typedef enum {
85  UDP_IP6 = 0,
86  UDP_IP4, /* the code is full of is_ip4... */
88 } udp_af_t;
89 
90 typedef struct {
91  udp_dst_port_info_t * dst_port_infos [N_UDP_AF];
92 
93  /* Hash tables mapping name/protocol to protocol info index. */
94  uword * dst_port_info_by_name[N_UDP_AF];
95  uword * dst_port_info_by_dst_port[N_UDP_AF];
96 
97  /* convenience */
99 } udp_main_t;
100 
103 {
104  uword * p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
105  return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
106 }
107 
110 
112 
114  udp_dst_port_t dst_port,
115  u32 node_index, u8 is_ip4);
116 
117 always_inline void
119  vlib_buffer_t * b0,
120  u8 is_ip4)
121 {
122  u16 new_l0;
123  udp_header_t * udp0;
124 
125  if (is_ip4)
126  {
127  ip4_header_t * ip0;
128  ip_csum_t sum0;
129  u16 old_l0 = 0;
130 
131  ip0 = vlib_buffer_get_current(b0);
132 
133  /* fix the <bleep>ing outer-IP checksum */
134  sum0 = ip0->checksum;
135  /* old_l0 always 0, see the rewrite setup */
136  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
137 
138  sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
139  length /* changed member */);
140  ip0->checksum = ip_csum_fold (sum0);
141  ip0->length = new_l0;
142 
143  /* Fix UDP length */
144  udp0 = (udp_header_t *)(ip0+1);
145  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
146  - sizeof (*ip0));
147  udp0->length = new_l0;
148  }
149  else
150  {
151  ip6_header_t * ip0;
152  int bogus0;
153 
154  ip0 = vlib_buffer_get_current(b0);
155 
156  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
157  - sizeof (*ip0));
158  ip0->payload_length = new_l0;
159 
160  /* Fix UDP length */
161  udp0 = (udp_header_t *)(ip0+1);
162  udp0->length = new_l0;
163 
164  udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
165  ASSERT(bogus0 == 0);
166 
167  if (udp0->checksum == 0)
168  udp0->checksum = 0xffff;
169  }
170 }
171 always_inline void
172 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
173  u8 is_ip4)
174 {
175  vlib_buffer_advance (b0, - ec_len);
176 
177  if (is_ip4)
178  {
179  ip4_header_t * ip0;
180 
181  ip0 = vlib_buffer_get_current(b0);
182 
183  /* Apply the encap string. */
184  clib_memcpy(ip0, ec0, ec_len);
185  ip_udp_fixup_one(vm, b0, 1);
186  }
187  else
188  {
189  ip6_header_t * ip0;
190 
191  ip0 = vlib_buffer_get_current(b0);
192 
193  /* Apply the encap string. */
194  clib_memcpy(ip0, ec0, ec_len);
195  ip_udp_fixup_one(vm, b0, 0);
196  }
197 }
198 
199 always_inline void
201  u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
202 {
203  u16 new_l0, new_l1;
204  udp_header_t * udp0, *udp1;
205 
206  ASSERT(_vec_len(ec0) == _vec_len(ec1));
207 
208  vlib_buffer_advance (b0, -ec_len);
209  vlib_buffer_advance (b1, -ec_len);
210 
211  if (is_v4)
212  {
213  ip4_header_t * ip0, *ip1;
214  ip_csum_t sum0, sum1;
215  u16 old_l0 = 0, old_l1 = 0;
216 
217  ip0 = vlib_buffer_get_current (b0);
218  ip1 = vlib_buffer_get_current (b1);
219 
220  /* Apply the encap string */
221  clib_memcpy (ip0, ec0, ec_len);
222  clib_memcpy (ip1, ec1, ec_len);
223 
224  /* fix the <bleep>ing outer-IP checksum */
225  sum0 = ip0->checksum;
226  sum1 = ip1->checksum;
227 
228  /* old_l0 always 0, see the rewrite setup */
229  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
230  new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
231 
232  sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
233  length /* changed member */);
234  sum1 = ip_csum_update(sum1, old_l1, new_l1, ip4_header_t,
235  length /* changed member */);
236 
237  ip0->checksum = ip_csum_fold (sum0);
238  ip1->checksum = ip_csum_fold (sum1);
239 
240  ip0->length = new_l0;
241  ip1->length = new_l1;
242 
243  /* Fix UDP length */
244  udp0 = (udp_header_t *) (ip0 + 1);
245  udp1 = (udp_header_t *) (ip1 + 1);
246 
247  new_l0 = clib_host_to_net_u16 (
248  vlib_buffer_length_in_chain (vm, b0) - sizeof(*ip0));
249  new_l1 = clib_host_to_net_u16 (
250  vlib_buffer_length_in_chain (vm, b1) - sizeof(*ip1));
251  udp0->length = new_l0;
252  udp1->length = new_l1;
253  }
254  else
255  {
256  ip6_header_t * ip0, * ip1;
257  int bogus0, bogus1;
258 
259  ip0 = vlib_buffer_get_current(b0);
260  ip1 = vlib_buffer_get_current(b1);
261 
262  /* Apply the encap string. */
263  clib_memcpy(ip0, ec0, ec_len);
264  clib_memcpy(ip1, ec1, ec_len);
265 
266  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
267  - sizeof (*ip0));
268  new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
269  - sizeof (*ip1));
270  ip0->payload_length = new_l0;
271  ip1->payload_length = new_l1;
272 
273  /* Fix UDP length */
274  udp0 = (udp_header_t *)(ip0+1);
275  udp1 = (udp_header_t *)(ip1+1);
276 
277  udp0->length = new_l0;
278  udp1->length = new_l1;
279 
280  udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
281  udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
282  ASSERT(bogus0 == 0);
283  ASSERT(bogus1 == 0);
284 
285  if (udp0->checksum == 0)
286  udp0->checksum = 0xffff;
287  if (udp1->checksum == 0)
288  udp1->checksum = 0xffff;
289  }
290 }
291 
292 #endif /* included_udp_h */
unformat_function_t unformat_udp_header
Definition: udp.h:111
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:231
uword * dst_port_info_by_dst_port[N_UDP_AF]
Definition: udp.h:95
Definition: udp.h:90
u32 node_index
Definition: udp.h:78
static udp_dst_port_info_t * udp_get_dst_port_info(udp_main_t *um, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp.h:102
uword ip_csum_t
Definition: ip_packet.h:86
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:112
#define always_inline
Definition: clib.h:84
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:190
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define foreach_udp6_dst_port
Definition: udp.h:49
#define hash_get(h, key)
Definition: hash.h:248
udp_dst_port_info_t * dst_port_infos[N_UDP_AF]
Definition: udp.h:91
static void ip_udp_encap_one(vlib_main_t *vm, vlib_buffer_t *b0, u8 *ec0, word ec_len, u8 is_ip4)
Definition: udp.h:172
Definition: udp.h:85
static void ip_udp_fixup_one(vlib_main_t *vm, vlib_buffer_t *b0, u8 is_ip4)
Definition: udp.h:118
format_function_t format_udp_rx_trace
Definition: udp.h:109
static void ip_udp_encap_two(vlib_main_t *vm, vlib_buffer_t *b0, vlib_buffer_t *b1, u8 *ec0, u8 *ec1, word ec_len, u8 is_v4)
Definition: udp.h:200
#define clib_memcpy(a, b, c)
Definition: string.h:64
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:203
udp_error_t
Definition: udp.h:29
udp_dst_port_t
Definition: udp.h:57
u16 ip6_tcp_udp_icmp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip6_header_t *ip0, int *bogus_lengthp)
Definition: ip6_forward.c:1105
#define foreach_udp4_dst_port
Definition: udp.h:36
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
udp_af_t
Definition: udp.h:84
udp_dst_port_t dst_port
Definition: udp.h:75
char * name
Definition: udp.h:72
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
u16 payload_length
Definition: ip6_packet.h:291
i64 word
Definition: types.h:111
unsigned char u8
Definition: types.h:56
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:133
u32 next_index
Definition: udp.h:81
void udp_register_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
Definition: udp_local.c:458
vlib_main_t * vlib_main
Definition: udp.h:98
udp6_dst_port_t
Definition: udp.h:64
Definition: udp.h:86
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:138
Definition: udp.h:87
format_function_t format_udp_header
Definition: udp.h:108