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>
30 #define udp_error(n,s) UDP_ERROR_##n,
31 #include <vnet/ip/udp_error.def>
36 #define foreach_udp4_dst_port \
37 _ (67, dhcp_to_server) \
38 _ (68, dhcp_to_client) \
49 #define foreach_udp6_dst_port \
50 _ (547, dhcpv6_to_server) \
51 _ (546, dhcpv6_to_client) \
54 _ (4790, vxlan6_gpe) \
58 #define _(n,f) UDP_DST_PORT_##f = n,
65 #define _(n,f) UDP6_DST_PORT_##f = n,
71 /* Name (a c string). */
74 /* GRE protocol type in host byte order. */
75 udp_dst_port_t dst_port;
77 /* Node which handles this type. */
80 /* Next index for this type. */
82 } udp_dst_port_info_t;
86 UDP_IP4, /* the code is full of is_ip4... */
91 udp_dst_port_info_t * dst_port_infos [N_UDP_AF];
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];
98 vlib_main_t * vlib_main;
101 always_inline udp_dst_port_info_t *
102 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
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;
108 format_function_t format_udp_header;
109 format_function_t format_udp_rx_trace;
111 unformat_function_t unformat_udp_header;
113 void udp_register_dst_port (vlib_main_t * vm,
114 udp_dst_port_t dst_port,
115 u32 node_index, u8 is_ip4);
118 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
124 vlib_buffer_advance (b0, - ec_len);
132 ip0 = vlib_buffer_get_current(b0);
134 /* Apply the encap string. */
135 clib_memcpy(ip0, ec0, ec_len);
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));
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;
148 udp0 = (udp_header_t *)(ip0+1);
149 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
151 udp0->length = new_l0;
158 ip0 = vlib_buffer_get_current(b0);
160 /* Apply the encap string. */
161 clib_memcpy(ip0, ec0, ec_len);
163 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
165 ip0->payload_length = new_l0;
168 udp0 = (udp_header_t *)(ip0+1);
169 udp0->length = new_l0;
171 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
174 if (udp0->checksum == 0)
175 udp0->checksum = 0xffff;
180 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
181 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
184 udp_header_t * udp0, *udp1;
186 ASSERT(_vec_len(ec0) == _vec_len(ec1));
188 vlib_buffer_advance (b0, -ec_len);
189 vlib_buffer_advance (b1, -ec_len);
193 ip4_header_t * ip0, *ip1;
194 ip_csum_t sum0, sum1;
195 u16 old_l0 = 0, old_l1 = 0;
197 ip0 = vlib_buffer_get_current (b0);
198 ip1 = vlib_buffer_get_current (b1);
200 /* Apply the encap string */
201 clib_memcpy (ip0, ec0, ec_len);
202 clib_memcpy (ip1, ec1, ec_len);
204 /* fix the <bleep>ing outer-IP checksum */
205 sum0 = ip0->checksum;
206 sum1 = ip1->checksum;
208 /* old_l0 always 0, see the rewrite setup */
209 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
210 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
212 sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
213 length /* changed member */);
214 sum1 = ip_csum_update(sum1, old_l1, new_l1, ip4_header_t,
215 length /* changed member */);
217 ip0->checksum = ip_csum_fold (sum0);
218 ip1->checksum = ip_csum_fold (sum1);
220 ip0->length = new_l0;
221 ip1->length = new_l1;
224 udp0 = (udp_header_t *) (ip0 + 1);
225 udp1 = (udp_header_t *) (ip1 + 1);
227 new_l0 = clib_host_to_net_u16 (
228 vlib_buffer_length_in_chain (vm, b0) - sizeof(*ip0));
229 new_l1 = clib_host_to_net_u16 (
230 vlib_buffer_length_in_chain (vm, b1) - sizeof(*ip1));
231 udp0->length = new_l0;
232 udp1->length = new_l1;
236 ip6_header_t * ip0, * ip1;
239 ip0 = vlib_buffer_get_current(b0);
240 ip1 = vlib_buffer_get_current(b1);
242 /* Apply the encap string. */
243 clib_memcpy(ip0, ec0, ec_len);
244 clib_memcpy(ip1, ec1, ec_len);
246 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
248 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
250 ip0->payload_length = new_l0;
251 ip1->payload_length = new_l1;
254 udp0 = (udp_header_t *)(ip0+1);
255 udp1 = (udp_header_t *)(ip1+1);
257 udp0->length = new_l0;
258 udp1->length = new_l1;
260 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
261 udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
265 if (udp0->checksum == 0)
266 udp0->checksum = 0xffff;
267 if (udp1->checksum == 0)
268 udp1->checksum = 0xffff;
272 #endif /* included_udp_h */