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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/lisp-cp/packets.h>
17 #include <vnet/lisp-cp/lisp_cp_messages.h>
18 #include <vnet/ip/udp_packet.h>
20 ///* Returns IP ID for the packet */
21 //static u16 ip_id = 0;
30 udp_ip4_checksum (const void *b, u32 len, u8 *src, u8 *dst)
33 u16 *ip_src = (u16 *) src;
34 u16 *ip_dst = (u16 *) dst;
42 sum = (sum & 0xFFFF) + (sum >> 16);
46 /* Add the padding if the packet length is odd */
50 /* Add the pseudo-header */
57 sum += clib_host_to_net_u16 (IP_PROTOCOL_UDP);
58 sum += clib_host_to_net_u16 (length);
62 sum = (sum & 0xFFFF) + (sum >> 16);
64 /* Return the one's complement of sum */
65 return ((u16) (~sum));
69 udp_ip6_checksum (ip6_header_t *ip6, udp_header_t *up, u32 len)
72 register const u16 *sp;
88 memset (&phu, 0, sizeof(phu));
89 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;
95 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
98 sp = (const u16 *) up;
100 for (i = 0; i < (len & ~1); i += 2)
104 sum += clib_host_to_net_u16 ((*(const u8 *) sp) << 8);
107 sum = (sum & 0xffff) + (sum >> 16);
114 udp_checksum (udp_header_t *uh, u32 udp_len, void *ih, u8 version)
119 return (udp_ip4_checksum (uh, udp_len,
120 ((ip4_header_t *) ih)->src_address.as_u8,
121 ((ip4_header_t *) ih)->dst_address.as_u8));
123 return (udp_ip6_checksum (ih, uh, udp_len));
130 pkt_push_udp (vlib_main_t * vm, vlib_buffer_t *b, u16 sp, u16 dp)
133 u16 udp_len = sizeof(udp_header_t) + vlib_buffer_length_in_chain (vm, b);
135 uh = vlib_buffer_push_uninit (b, sizeof(*uh));
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);
145 pkt_push_ipv4 (vlib_main_t * vm, vlib_buffer_t *b, ip4_address_t *src,
146 ip4_address_t *dst, int proto)
151 ih = vlib_buffer_push_uninit(b, sizeof(ip4_header_t));
153 ih->ip_version_and_header_length = 0x45;
155 ih->length = clib_host_to_net_u16(vlib_buffer_length_in_chain (vm, b));
157 // iph->fragment_id = clib_host_to_net_u16(get_IP_ID ());
159 /* TODO: decide if we allow fragments in case of control */
160 ih->flags_and_fragment_offset = clib_host_to_net_u16(IP_DF);
162 ih->protocol = proto;
163 ih->src_address.as_u32 = src->as_u32;
164 ih->dst_address.as_u32 = dst->as_u32;
166 ih->checksum = ip4_header_checksum (ih);
171 pkt_push_ipv6 (vlib_main_t * vm, vlib_buffer_t *b, ip6_address_t *src,
172 ip6_address_t *dst, int proto)
175 // struct ip6_hdr *ip6h;
178 // len = lbuf_size(b);
179 // ip6h = lbuf_push_uninit(b, sizeof(struct ip6_hdr));
181 // ip6h->ip6_hops = 255;
182 // ip6h->ip6_vfc = (IP6VERSION << 4);
183 // ip6h->ip6_nxt = proto;
184 // ip6h->ip6_plen = clib_host_to_net_u16(len);
185 // clib_memcpy(ip6h->ip6_src.s6_addr, src->s6_addr, sizeof(struct in6_addr));
186 // clib_memcpy(ip6h->ip6_dst.s6_addr, dst->s6_addr, sizeof(struct in6_addr));
191 pkt_push_ip (vlib_main_t * vm, vlib_buffer_t *b, ip_address_t *src,
192 ip_address_t *dst, u32 proto)
194 if (ip_addr_version (src) != ip_addr_version(dst))
196 clib_warning("src %s and dst %s IP have different AFI! Discarding!",
197 format_ip_address, src, format_ip_address, dst);
201 switch (ip_addr_version(src))
204 return pkt_push_ipv4 (vm, b, &ip_addr_v4(src), &ip_addr_v4(dst), proto);
207 return pkt_push_ipv6 (vm, b, &ip_addr_v6(src), &ip_addr_v6(dst), proto);
215 pkt_push_udp_and_ip (vlib_main_t * vm, vlib_buffer_t *b, u16 sp, u16 dp,
216 ip_address_t *sip, ip_address_t *dip)
222 uh = pkt_push_udp (vm, b, sp, dp);
224 ih = pkt_push_ip (vm, b, sip, dip, IP_PROTOCOL_UDP);
226 udpsum = udp_checksum (uh, clib_net_to_host_u16 (uh->length), ih,
227 ip_addr_version(sip));
230 clib_warning("Failed UDP checksum! Discarding");
233 uh->checksum = udpsum;
238 pkt_push_ecm_hdr (vlib_buffer_t *b)
241 h = vlib_buffer_push_uninit (b, sizeof (h[0]));
243 memset(h, 0, sizeof(h[0]));
244 h->type = LISP_ENCAP_CONTROL_TYPE;
245 memset (h->reserved2, 0, sizeof(h->reserved2));