blob: 3a4f421b02ae440d41397614326beb7d6ff81171 [file] [log] [blame]
Florin Corase127a7e2016-02-18 22:20:01 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/lisp-cp/packets.h>
17#include <vnet/lisp-cp/lisp_cp_messages.h>
18#include <vnet/ip/udp_packet.h>
19
Florin Corasa2157cf2016-08-16 21:09:14 +020020/* Returns IP ID for the packet */
21/* static u16 ip_id = 0;
22static inline u16
23get_IP_ID()
24{
25 ip_id++;
26 return (ip_id);
27} */
Florin Corase127a7e2016-02-18 22:20:01 +010028
29u16
Florin Corasa2157cf2016-08-16 21:09:14 +020030udp_ip4_checksum (const void *b, u32 len, u8 * src, u8 * dst)
Florin Corase127a7e2016-02-18 22:20:01 +010031{
32 const u16 *buf = b;
33 u16 *ip_src = (u16 *) src;
34 u16 *ip_dst = (u16 *) dst;
35 u32 length = len;
36 u32 sum = 0;
37
38 while (len > 1)
39 {
40 sum += *buf++;
41 if (sum & 0x80000000)
Florin Corasa2157cf2016-08-16 21:09:14 +020042 sum = (sum & 0xFFFF) + (sum >> 16);
Florin Corase127a7e2016-02-18 22:20:01 +010043 len -= 2;
44 }
45
46 /* Add the padding if the packet length is odd */
47 if (len & 1)
48 sum += *((u8 *) buf);
49
50 /* Add the pseudo-header */
51 sum += *(ip_src++);
52 sum += *ip_src;
53
54 sum += *(ip_dst++);
55 sum += *ip_dst;
56
57 sum += clib_host_to_net_u16 (IP_PROTOCOL_UDP);
58 sum += clib_host_to_net_u16 (length);
59
60 /* Add the carries */
61 while (sum >> 16)
62 sum = (sum & 0xFFFF) + (sum >> 16);
63
64 /* Return the one's complement of sum */
65 return ((u16) (~sum));
66}
67
68u16
Florin Corasa2157cf2016-08-16 21:09:14 +020069udp_ip6_checksum (ip6_header_t * ip6, udp_header_t * up, u32 len)
Florin Corase127a7e2016-02-18 22:20:01 +010070{
71 size_t i;
72 register const u16 *sp;
73 u32 sum;
74 union
75 {
76 struct
77 {
78 ip6_address_t ph_src;
79 ip6_address_t ph_dst;
80 u32 ph_len;
81 u8 ph_zero[3];
82 u8 ph_nxt;
83 } ph;
84 u16 pa[20];
85 } phu;
86
87 /* pseudo-header */
Florin Corasa2157cf2016-08-16 21:09:14 +020088 memset (&phu, 0, sizeof (phu));
Florin Corase127a7e2016-02-18 22:20:01 +010089 phu.ph.ph_src = ip6->src_address;
90 phu.ph.ph_dst = ip6->dst_address;
91 phu.ph.ph_len = clib_host_to_net_u32 (len);
92 phu.ph.ph_nxt = IP_PROTOCOL_UDP;
93
94 sum = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +020095 for (i = 0; i < sizeof (phu.pa) / sizeof (phu.pa[0]); i++)
Florin Corase127a7e2016-02-18 22:20:01 +010096 sum += phu.pa[i];
97
98 sp = (const u16 *) up;
99
100 for (i = 0; i < (len & ~1); i += 2)
101 sum += *sp++;
102
103 if (len & 1)
104 sum += clib_host_to_net_u16 ((*(const u8 *) sp) << 8);
105
106 while (sum > 0xffff)
107 sum = (sum & 0xffff) + (sum >> 16);
108 sum = ~sum & 0xffff;
109
110 return (sum);
111}
112
113u16
Florin Corasa2157cf2016-08-16 21:09:14 +0200114udp_checksum (udp_header_t * uh, u32 udp_len, void *ih, u8 version)
Florin Corase127a7e2016-02-18 22:20:01 +0100115{
116 switch (version)
117 {
118 case IP4:
119 return (udp_ip4_checksum (uh, udp_len,
Florin Corasa2157cf2016-08-16 21:09:14 +0200120 ((ip4_header_t *) ih)->src_address.as_u8,
121 ((ip4_header_t *) ih)->dst_address.as_u8));
Florin Corase127a7e2016-02-18 22:20:01 +0100122 case IP6:
123 return (udp_ip6_checksum (ih, uh, udp_len));
124 default:
125 return ~0;
126 }
127}
128
129void *
Florin Corasa2157cf2016-08-16 21:09:14 +0200130pkt_push_udp (vlib_main_t * vm, vlib_buffer_t * b, u16 sp, u16 dp)
Florin Corase127a7e2016-02-18 22:20:01 +0100131{
Florin Corasa2157cf2016-08-16 21:09:14 +0200132 udp_header_t *uh;
133 u16 udp_len = sizeof (udp_header_t) + vlib_buffer_length_in_chain (vm, b);
Florin Corase127a7e2016-02-18 22:20:01 +0100134
Florin Corasa2157cf2016-08-16 21:09:14 +0200135 uh = vlib_buffer_push_uninit (b, sizeof (*uh));
Florin Corase127a7e2016-02-18 22:20:01 +0100136
137 uh->src_port = clib_host_to_net_u16 (sp);
138 uh->dst_port = clib_host_to_net_u16 (dp);
139 uh->length = clib_host_to_net_u16 (udp_len);
140 uh->checksum = 0;
141 return uh;
142}
143
144void *
Florin Corasa2157cf2016-08-16 21:09:14 +0200145pkt_push_ipv4 (vlib_main_t * vm, vlib_buffer_t * b, ip4_address_t * src,
146 ip4_address_t * dst, int proto)
Florin Corase127a7e2016-02-18 22:20:01 +0100147{
Florin Corasa2157cf2016-08-16 21:09:14 +0200148 ip4_header_t *ih;
Florin Corase127a7e2016-02-18 22:20:01 +0100149
150 /* make some room */
Florin Corasa2157cf2016-08-16 21:09:14 +0200151 ih = vlib_buffer_push_uninit (b, sizeof (ip4_header_t));
Florin Corase127a7e2016-02-18 22:20:01 +0100152
153 ih->ip_version_and_header_length = 0x45;
154 ih->tos = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +0200155 ih->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
Florin Corase127a7e2016-02-18 22:20:01 +0100156
Florin Corasa2157cf2016-08-16 21:09:14 +0200157 /* iph->fragment_id = clib_host_to_net_u16(get_IP_ID ()); */
Florin Corase127a7e2016-02-18 22:20:01 +0100158
159 /* TODO: decide if we allow fragments in case of control */
Florin Corasa2157cf2016-08-16 21:09:14 +0200160 ih->flags_and_fragment_offset = clib_host_to_net_u16 (IP_DF);
Florin Corase127a7e2016-02-18 22:20:01 +0100161 ih->ttl = 255;
162 ih->protocol = proto;
163 ih->src_address.as_u32 = src->as_u32;
164 ih->dst_address.as_u32 = dst->as_u32;
165
166 ih->checksum = ip4_header_checksum (ih);
167 return ih;
168}
169
170void *
Florin Corasa2157cf2016-08-16 21:09:14 +0200171pkt_push_ipv6 (vlib_main_t * vm, vlib_buffer_t * b, ip6_address_t * src,
172 ip6_address_t * dst, int proto)
Florin Corase127a7e2016-02-18 22:20:01 +0100173{
Andrej Kozemcak499929d2016-04-26 08:18:33 +0200174 ip6_header_t *ip6h;
175 u16 payload_length;
176
177 /* make some room */
Florin Corasa2157cf2016-08-16 21:09:14 +0200178 ip6h = vlib_buffer_push_uninit (b, sizeof (ip6_header_t));
Andrej Kozemcak499929d2016-04-26 08:18:33 +0200179
180 ip6h->ip_version_traffic_class_and_flow_label =
Florin Corasa2157cf2016-08-16 21:09:14 +0200181 clib_host_to_net_u32 (0x6 << 28);
Andrej Kozemcak499929d2016-04-26 08:18:33 +0200182
Florin Corasa2157cf2016-08-16 21:09:14 +0200183 /* calculate ip6 payload length */
184 payload_length = vlib_buffer_length_in_chain (vm, b);
Andrej Kozemcak499929d2016-04-26 08:18:33 +0200185 payload_length -= sizeof (*ip6h);
186
Florin Corasa2157cf2016-08-16 21:09:14 +0200187 ip6h->payload_length = clib_host_to_net_u16 (payload_length);
Andrej Kozemcak499929d2016-04-26 08:18:33 +0200188
189 ip6h->hop_limit = 0xff;
190 ip6h->protocol = proto;
Florin Corasa2157cf2016-08-16 21:09:14 +0200191 clib_memcpy (ip6h->src_address.as_u8, src->as_u8,
192 sizeof (ip6h->src_address));
193 clib_memcpy (ip6h->dst_address.as_u8, dst->as_u8,
194 sizeof (ip6h->src_address));
Andrej Kozemcak499929d2016-04-26 08:18:33 +0200195
196 return ip6h;
Florin Corase127a7e2016-02-18 22:20:01 +0100197}
198
199void *
Florin Corasa2157cf2016-08-16 21:09:14 +0200200pkt_push_ip (vlib_main_t * vm, vlib_buffer_t * b, ip_address_t * src,
201 ip_address_t * dst, u32 proto)
Florin Corase127a7e2016-02-18 22:20:01 +0100202{
Florin Corasa2157cf2016-08-16 21:09:14 +0200203 if (ip_addr_version (src) != ip_addr_version (dst))
Florin Corase127a7e2016-02-18 22:20:01 +0100204 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200205 clib_warning ("src %U and dst %U IP have different AFI! Discarding!",
206 format_ip_address, src, format_ip_address, dst);
Florin Corase127a7e2016-02-18 22:20:01 +0100207 return 0;
208 }
209
Florin Corasa2157cf2016-08-16 21:09:14 +0200210 switch (ip_addr_version (src))
Florin Corase127a7e2016-02-18 22:20:01 +0100211 {
212 case IP4:
Florin Corasa2157cf2016-08-16 21:09:14 +0200213 return pkt_push_ipv4 (vm, b, &ip_addr_v4 (src), &ip_addr_v4 (dst),
214 proto);
Florin Corase127a7e2016-02-18 22:20:01 +0100215 break;
216 case IP6:
Florin Corasa2157cf2016-08-16 21:09:14 +0200217 return pkt_push_ipv6 (vm, b, &ip_addr_v6 (src), &ip_addr_v6 (dst),
218 proto);
Florin Corase127a7e2016-02-18 22:20:01 +0100219 break;
220 }
221
222 return 0;
223}
224
225void *
Florin Corasa2157cf2016-08-16 21:09:14 +0200226pkt_push_udp_and_ip (vlib_main_t * vm, vlib_buffer_t * b, u16 sp, u16 dp,
227 ip_address_t * sip, ip_address_t * dip)
Florin Corase127a7e2016-02-18 22:20:01 +0100228{
229 u16 udpsum;
Florin Corasa2157cf2016-08-16 21:09:14 +0200230 udp_header_t *uh;
231 void *ih;
Florin Corase127a7e2016-02-18 22:20:01 +0100232
233 uh = pkt_push_udp (vm, b, sp, dp);
234
235 ih = pkt_push_ip (vm, b, sip, dip, IP_PROTOCOL_UDP);
236
237 udpsum = udp_checksum (uh, clib_net_to_host_u16 (uh->length), ih,
Florin Corasa2157cf2016-08-16 21:09:14 +0200238 ip_addr_version (sip));
239 if (udpsum == (u16) ~ 0)
Florin Corase127a7e2016-02-18 22:20:01 +0100240 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200241 clib_warning ("Failed UDP checksum! Discarding");
Florin Corase127a7e2016-02-18 22:20:01 +0100242 return 0;
243 }
244 uh->checksum = udpsum;
245 return ih;
246}
247
248void *
Florin Corasa2157cf2016-08-16 21:09:14 +0200249pkt_push_ecm_hdr (vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +0100250{
Florin Corasa2157cf2016-08-16 21:09:14 +0200251 ecm_hdr_t *h;
Florin Corase127a7e2016-02-18 22:20:01 +0100252 h = vlib_buffer_push_uninit (b, sizeof (h[0]));
253
Florin Corasa2157cf2016-08-16 21:09:14 +0200254 memset (h, 0, sizeof (h[0]));
Florin Corase127a7e2016-02-18 22:20:01 +0100255 h->type = LISP_ENCAP_CONTROL_TYPE;
Florin Corasa2157cf2016-08-16 21:09:14 +0200256 memset (h->reserved2, 0, sizeof (h->reserved2));
Florin Corase127a7e2016-02-18 22:20:01 +0100257
258 return h;
259}
Florin Corasa2157cf2016-08-16 21:09:14 +0200260
261/* *INDENT-ON* */
262
263/*
264 * fd.io coding-style-patch-verification: ON
265 *
266 * Local Variables:
267 * eval: (c-set-style "gnu")
268 * End:
269 */