2 * ip/udp.h: udp protocol
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef included_udp_h
19 #define included_udp_h
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>
31 #define udp_error(n,s) UDP_ERROR_##n,
32 #include <vnet/ip/udp_error.def>
37 #define foreach_udp4_dst_port \
38 _ (67, dhcp_to_server) \
39 _ (68, dhcp_to_client) \
52 #define foreach_udp6_dst_port \
53 _ (547, dhcpv6_to_server) \
54 _ (546, dhcpv6_to_client) \
59 _ (4790, vxlan6_gpe) \
64 #define _(n,f) UDP_DST_PORT_##f = n,
65 foreach_udp4_dst_port foreach_udp6_dst_port
71 #define _(n,f) UDP6_DST_PORT_##f = n,
78 /* Name (a c string). */
81 /* GRE protocol type in host byte order. */
82 udp_dst_port_t dst_port;
84 /* Node which handles this type. */
87 /* Next index for this type. */
89 } udp_dst_port_info_t;
94 UDP_IP4, /* the code is full of is_ip4... */
100 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
102 /* Hash tables mapping name/protocol to protocol info index. */
103 uword *dst_port_info_by_name[N_UDP_AF];
104 uword *dst_port_info_by_dst_port[N_UDP_AF];
107 vlib_main_t *vlib_main;
110 always_inline udp_dst_port_info_t *
111 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
113 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
114 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
117 format_function_t format_udp_header;
118 format_function_t format_udp_rx_trace;
120 unformat_function_t unformat_udp_header;
122 void udp_register_dst_port (vlib_main_t * vm,
123 udp_dst_port_t dst_port,
124 u32 node_index, u8 is_ip4);
126 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
129 ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
140 ip0 = vlib_buffer_get_current (b0);
142 /* fix the <bleep>ing outer-IP checksum */
143 sum0 = ip0->checksum;
144 /* old_l0 always 0, see the rewrite setup */
145 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
147 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
148 length /* changed member */ );
149 ip0->checksum = ip_csum_fold (sum0);
150 ip0->length = new_l0;
153 udp0 = (udp_header_t *) (ip0 + 1);
154 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
156 udp0->length = new_l0;
163 ip0 = vlib_buffer_get_current (b0);
165 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
167 ip0->payload_length = new_l0;
170 udp0 = (udp_header_t *) (ip0 + 1);
171 udp0->length = new_l0;
174 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
175 ASSERT (bogus0 == 0);
177 if (udp0->checksum == 0)
178 udp0->checksum = 0xffff;
183 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
186 vlib_buffer_advance (b0, -ec_len);
192 ip0 = vlib_buffer_get_current (b0);
194 /* Apply the encap string. */
195 clib_memcpy (ip0, ec0, ec_len);
196 ip_udp_fixup_one (vm, b0, 1);
202 ip0 = vlib_buffer_get_current (b0);
204 /* Apply the encap string. */
205 clib_memcpy (ip0, ec0, ec_len);
206 ip_udp_fixup_one (vm, b0, 0);
211 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
212 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
215 udp_header_t *udp0, *udp1;
217 ASSERT (_vec_len (ec0) == _vec_len (ec1));
219 vlib_buffer_advance (b0, -ec_len);
220 vlib_buffer_advance (b1, -ec_len);
224 ip4_header_t *ip0, *ip1;
225 ip_csum_t sum0, sum1;
226 u16 old_l0 = 0, old_l1 = 0;
228 ip0 = vlib_buffer_get_current (b0);
229 ip1 = vlib_buffer_get_current (b1);
231 /* Apply the encap string */
232 clib_memcpy (ip0, ec0, ec_len);
233 clib_memcpy (ip1, ec1, ec_len);
235 /* fix the <bleep>ing outer-IP checksum */
236 sum0 = ip0->checksum;
237 sum1 = ip1->checksum;
239 /* old_l0 always 0, see the rewrite setup */
240 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
241 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
243 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
244 length /* changed member */ );
245 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
246 length /* changed member */ );
248 ip0->checksum = ip_csum_fold (sum0);
249 ip1->checksum = ip_csum_fold (sum1);
251 ip0->length = new_l0;
252 ip1->length = new_l1;
255 udp0 = (udp_header_t *) (ip0 + 1);
256 udp1 = (udp_header_t *) (ip1 + 1);
259 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
262 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
264 udp0->length = new_l0;
265 udp1->length = new_l1;
269 ip6_header_t *ip0, *ip1;
272 ip0 = vlib_buffer_get_current (b0);
273 ip1 = vlib_buffer_get_current (b1);
275 /* Apply the encap string. */
276 clib_memcpy (ip0, ec0, ec_len);
277 clib_memcpy (ip1, ec1, ec_len);
279 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
281 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
283 ip0->payload_length = new_l0;
284 ip1->payload_length = new_l1;
287 udp0 = (udp_header_t *) (ip0 + 1);
288 udp1 = (udp_header_t *) (ip1 + 1);
290 udp0->length = new_l0;
291 udp1->length = new_l1;
294 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
296 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
297 ASSERT (bogus0 == 0);
298 ASSERT (bogus1 == 0);
300 if (udp0->checksum == 0)
301 udp0->checksum = 0xffff;
302 if (udp1->checksum == 0)
303 udp1->checksum = 0xffff;
307 #endif /* included_udp_h */
310 * fd.io coding-style-patch-verification: ON
313 * eval: (c-set-style "gnu")