Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 1 | /* |
| 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> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 18 | #include <vnet/udp/udp_packet.h> |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 19 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 20 | /* Returns IP ID for the packet */ |
| 21 | /* static u16 ip_id = 0; |
| 22 | static inline u16 |
| 23 | get_IP_ID() |
| 24 | { |
| 25 | ip_id++; |
| 26 | return (ip_id); |
| 27 | } */ |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 28 | |
| 29 | u16 |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 30 | udp_ip4_checksum (const void *b, u32 len, u8 * src, u8 * dst) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 31 | { |
| 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 Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 42 | sum = (sum & 0xFFFF) + (sum >> 16); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 43 | 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 | |
| 68 | u16 |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 69 | udp_ip6_checksum (ip6_header_t * ip6, udp_header_t * up, u32 len) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 70 | { |
| 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 */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 88 | clib_memset (&phu, 0, sizeof (phu)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 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; |
| 93 | |
| 94 | sum = 0; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 95 | for (i = 0; i < sizeof (phu.pa) / sizeof (phu.pa[0]); i++) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 96 | 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 | |
| 113 | u16 |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 114 | udp_checksum (udp_header_t * uh, u32 udp_len, void *ih, u8 version) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 115 | { |
| 116 | switch (version) |
| 117 | { |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 118 | case AF_IP4: |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 119 | return (udp_ip4_checksum (uh, udp_len, |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 120 | ((ip4_header_t *) ih)->src_address.as_u8, |
| 121 | ((ip4_header_t *) ih)->dst_address.as_u8)); |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 122 | case AF_IP6: |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 123 | return (udp_ip6_checksum (ih, uh, udp_len)); |
| 124 | default: |
| 125 | return ~0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 130 | pkt_push_udp (vlib_main_t * vm, vlib_buffer_t * b, u16 sp, u16 dp) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 131 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 132 | udp_header_t *uh; |
| 133 | u16 udp_len = sizeof (udp_header_t) + vlib_buffer_length_in_chain (vm, b); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 134 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 135 | uh = vlib_buffer_push_uninit (b, sizeof (*uh)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 136 | |
| 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 | |
| 144 | void * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 145 | pkt_push_ip (vlib_main_t * vm, vlib_buffer_t * b, ip_address_t * src, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 146 | ip_address_t * dst, u32 proto, u8 csum_offload) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 147 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 148 | if (ip_addr_version (src) != ip_addr_version (dst)) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 149 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 150 | clib_warning ("src %U and dst %U IP have different AFI! Discarding!", |
| 151 | format_ip_address, src, format_ip_address, dst); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 155 | switch (ip_addr_version (src)) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 156 | { |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 157 | case AF_IP4: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 158 | return vlib_buffer_push_ip4 (vm, b, &ip_addr_v4 (src), |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 159 | &ip_addr_v4 (dst), proto, csum_offload); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 160 | break; |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 161 | case AF_IP6: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 162 | return vlib_buffer_push_ip6 (vm, b, &ip_addr_v6 (src), |
| 163 | &ip_addr_v6 (dst), proto); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | void * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 171 | pkt_push_udp_and_ip (vlib_main_t * vm, vlib_buffer_t * b, u16 sp, u16 dp, |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 172 | ip_address_t * sip, ip_address_t * dip, u8 csum_offload) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 173 | { |
| 174 | u16 udpsum; |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 175 | udp_header_t *uh; |
| 176 | void *ih; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 177 | |
| 178 | uh = pkt_push_udp (vm, b, sp, dp); |
| 179 | |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 180 | if (csum_offload) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 181 | { |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 182 | ih = pkt_push_ip (vm, b, sip, dip, IP_PROTOCOL_UDP, 1); |
Florin Coras | 7c35f19 | 2017-07-25 11:35:44 -0700 | [diff] [blame] | 183 | b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM; |
| 184 | vnet_buffer (b)->l3_hdr_offset = (u8 *) ih - b->data; |
| 185 | vnet_buffer (b)->l4_hdr_offset = (u8 *) uh - b->data; |
| 186 | uh->checksum = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 187 | } |
Florin Coras | 7c35f19 | 2017-07-25 11:35:44 -0700 | [diff] [blame] | 188 | else |
| 189 | { |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 190 | ih = pkt_push_ip (vm, b, sip, dip, IP_PROTOCOL_UDP, 0); |
Florin Coras | 7c35f19 | 2017-07-25 11:35:44 -0700 | [diff] [blame] | 191 | udpsum = udp_checksum (uh, clib_net_to_host_u16 (uh->length), ih, |
| 192 | ip_addr_version (sip)); |
| 193 | if (udpsum == (u16) ~ 0) |
| 194 | { |
| 195 | clib_warning ("Failed UDP checksum! Discarding"); |
| 196 | return 0; |
| 197 | } |
Florin Coras | fdbc382 | 2017-07-27 00:34:12 -0700 | [diff] [blame] | 198 | /* clear flags used for csum since we're not offloading */ |
| 199 | b->flags &= ~(VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_IS_IP6); |
Florin Coras | 7c35f19 | 2017-07-25 11:35:44 -0700 | [diff] [blame] | 200 | uh->checksum = udpsum; |
| 201 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 202 | return ih; |
| 203 | } |
| 204 | |
| 205 | void * |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 206 | pkt_push_ecm_hdr (vlib_buffer_t * b) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 207 | { |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 208 | ecm_hdr_t *h; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 209 | h = vlib_buffer_push_uninit (b, sizeof (h[0])); |
| 210 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 211 | clib_memset (h, 0, sizeof (h[0])); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 212 | h->type = LISP_ENCAP_CONTROL_TYPE; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 213 | clib_memset (h->reserved2, 0, sizeof (h->reserved2)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 214 | |
| 215 | return h; |
| 216 | } |
Florin Coras | a2157cf | 2016-08-16 21:09:14 +0200 | [diff] [blame] | 217 | |
| 218 | /* *INDENT-ON* */ |
| 219 | |
| 220 | /* |
| 221 | * fd.io coding-style-patch-verification: ON |
| 222 | * |
| 223 | * Local Variables: |
| 224 | * eval: (c-set-style "gnu") |
| 225 | * End: |
| 226 | */ |