FD.io VPP  v16.06
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
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 _ (6633, vpath6_3)
55 
56 typedef enum {
57 #define _(n,f) UDP_DST_PORT_##f = n,
60 #undef _
62 
63 typedef enum {
64 #define _(n,f) UDP6_DST_PORT_##f = n,
66 #undef _
68 
69 typedef struct {
70  /* Name (a c string). */
71  char * name;
72 
73  /* GRE protocol type in host byte order. */
75 
76  /* Node which handles this type. */
78 
79  /* Next index for this type. */
82 
83 typedef enum {
84  UDP_IP6 = 0,
85  UDP_IP4, /* the code is full of is_ip4... */
87 } udp_af_t;
88 
89 typedef struct {
90  udp_dst_port_info_t * dst_port_infos [N_UDP_AF];
91 
92  /* Hash tables mapping name/protocol to protocol info index. */
93  uword * dst_port_info_by_name[N_UDP_AF];
94  uword * dst_port_info_by_dst_port[N_UDP_AF];
95 
96  /* convenience */
98 } udp_main_t;
99 
102 {
103  uword * p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
104  return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
105 }
106 
109 
111 
113  udp_dst_port_t dst_port,
114  u32 node_index, u8 is_ip4);
115 
116 always_inline void
117 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
118  u8 is_ip4)
119 {
120  u16 new_l0;
121  udp_header_t * udp0;
122 
123  vlib_buffer_advance (b0, - ec_len);
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  /* Apply the encap string. */
134  clib_memcpy(ip0, ec0, ec_len);
135 
136  /* fix the <bleep>ing outer-IP checksum */
137  sum0 = ip0->checksum;
138  /* old_l0 always 0, see the rewrite setup */
139  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
140 
141  sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
142  length /* changed member */);
143  ip0->checksum = ip_csum_fold (sum0);
144  ip0->length = new_l0;
145 
146  /* Fix UDP length */
147  udp0 = (udp_header_t *)(ip0+1);
148  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
149  - sizeof (*ip0));
150  udp0->length = new_l0;
151  }
152  else
153  {
154  ip6_header_t * ip0;
155  int bogus0;
156 
157  ip0 = vlib_buffer_get_current(b0);
158 
159  /* Apply the encap string. */
160  clib_memcpy(ip0, ec0, ec_len);
161 
162  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
163  - sizeof (*ip0));
164  ip0->payload_length = new_l0;
165 
166  /* Fix UDP length */
167  udp0 = (udp_header_t *)(ip0+1);
168  udp0->length = new_l0;
169 
170  udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
171  ASSERT(bogus0 == 0);
172 
173  if (udp0->checksum == 0)
174  udp0->checksum = 0xffff;
175  }
176 }
177 
178 always_inline void
180  u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
181 {
182  u16 new_l0, new_l1;
183  udp_header_t * udp0, *udp1;
184 
185  ASSERT(_vec_len(ec0) == _vec_len(ec1));
186 
187  vlib_buffer_advance (b0, -ec_len);
188  vlib_buffer_advance (b1, -ec_len);
189 
190  if (is_v4)
191  {
192  ip4_header_t * ip0, *ip1;
193  ip_csum_t sum0, sum1;
194  u16 old_l0 = 0, old_l1 = 0;
195 
196  ip0 = vlib_buffer_get_current (b0);
197  ip1 = vlib_buffer_get_current (b1);
198 
199  /* Apply the encap string */
200  clib_memcpy (ip0, ec0, ec_len);
201  clib_memcpy (ip1, ec1, ec_len);
202 
203  /* fix the <bleep>ing outer-IP checksum */
204  sum0 = ip0->checksum;
205  sum1 = ip1->checksum;
206 
207  /* old_l0 always 0, see the rewrite setup */
208  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
209  new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
210 
211  sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
212  length /* changed member */);
213  sum1 = ip_csum_update(sum1, old_l1, new_l1, ip4_header_t,
214  length /* changed member */);
215 
216  ip0->checksum = ip_csum_fold (sum0);
217  ip1->checksum = ip_csum_fold (sum1);
218 
219  ip0->length = new_l0;
220  ip1->length = new_l1;
221 
222  /* Fix UDP length */
223  udp0 = (udp_header_t *) (ip0 + 1);
224  udp1 = (udp_header_t *) (ip1 + 1);
225 
226  new_l0 = clib_host_to_net_u16 (
227  vlib_buffer_length_in_chain (vm, b0) - sizeof(*ip0));
228  new_l1 = clib_host_to_net_u16 (
229  vlib_buffer_length_in_chain (vm, b1) - sizeof(*ip1));
230  udp0->length = new_l0;
231  udp1->length = new_l1;
232  }
233  else
234  {
235  ip6_header_t * ip0, * ip1;
236  int bogus0, bogus1;
237 
238  ip0 = vlib_buffer_get_current(b0);
239  ip1 = vlib_buffer_get_current(b1);
240 
241  /* Apply the encap string. */
242  clib_memcpy(ip0, ec0, ec_len);
243  clib_memcpy(ip1, ec1, ec_len);
244 
245  new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
246  - sizeof (*ip0));
247  new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
248  - sizeof (*ip1));
249  ip0->payload_length = new_l0;
250  ip1->payload_length = new_l1;
251 
252  /* Fix UDP length */
253  udp0 = (udp_header_t *)(ip0+1);
254  udp1 = (udp_header_t *)(ip1+1);
255 
256  udp0->length = new_l0;
257  udp1->length = new_l1;
258 
259  udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
260  udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
261  ASSERT(bogus0 == 0);
262  ASSERT(bogus1 == 0);
263 
264  if (udp0->checksum == 0)
265  udp0->checksum = 0xffff;
266  if (udp1->checksum == 0)
267  udp1->checksum = 0xffff;
268  }
269 }
270 
271 #endif /* included_udp_h */
unformat_function_t unformat_udp_header
Definition: udp.h:110
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:229
always_inline 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:179
uword * dst_port_info_by_dst_port[N_UDP_AF]
Definition: udp.h:94
Definition: udp.h:89
u32 node_index
Definition: udp.h:77
uword ip_csum_t
Definition: ip_packet.h:86
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
#define always_inline
Definition: clib.h:84
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
always_inline 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 foreach_udp6_dst_port
Definition: udp.h:49
always_inline u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:138
#define hash_get(h, key)
Definition: hash.h:231
udp_dst_port_info_t * dst_port_infos[N_UDP_AF]
Definition: udp.h:90
Definition: udp.h:84
format_function_t format_udp_rx_trace
Definition: udp.h:108
#define clib_memcpy(a, b, c)
Definition: string.h:63
udp_error_t
Definition: udp.h:29
udp_dst_port_t
Definition: udp.h:56
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:1544
always_inline 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:101
#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:83
udp_dst_port_t dst_port
Definition: udp.h:74
char * name
Definition: udp.h:71
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:284
i64 word
Definition: types.h:111
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
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:80
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:374
always_inline void ip_udp_encap_one(vlib_main_t *vm, vlib_buffer_t *b0, u8 *ec0, word ec_len, u8 is_ip4)
Definition: udp.h:117
vlib_main_t * vlib_main
Definition: udp.h:97
udp6_dst_port_t
Definition: udp.h:63
Definition: udp.h:85
Definition: udp.h:86
format_function_t format_udp_header
Definition: udp.h:107