[c5808b41] | 1 | /*
|
---|
[2f19103] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[c5808b41] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup tcp
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[032bbe7] | 34 | * @file TCP header encoding and decoding
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
[6896409c] | 37 | #include <bitops.h>
|
---|
[c5808b41] | 38 | #include <byteorder.h>
|
---|
[eea65f4] | 39 | #include <errno.h>
|
---|
[2f19103] | 40 | #include <inet/endpoint.h>
|
---|
[eea65f4] | 41 | #include <mem.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
[762b48a] | 43 | #include "pdu.h"
|
---|
[c5808b41] | 44 | #include "segment.h"
|
---|
[6896409c] | 45 | #include "seq_no.h"
|
---|
[c5808b41] | 46 | #include "std.h"
|
---|
| 47 | #include "tcp_type.h"
|
---|
| 48 |
|
---|
[eea65f4] | 49 | #define TCP_CHECKSUM_INIT 0xffff
|
---|
| 50 |
|
---|
[0ac2158] | 51 | /** One's complement addition.
|
---|
| 52 | *
|
---|
| 53 | * Result is a + b + carry.
|
---|
| 54 | */
|
---|
| 55 | static uint16_t tcp_ocadd16(uint16_t a, uint16_t b)
|
---|
| 56 | {
|
---|
| 57 | uint32_t s;
|
---|
| 58 |
|
---|
| 59 | s = (uint32_t)a + (uint32_t)b;
|
---|
| 60 | return (s & 0xffff) + (s >> 16);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[eea65f4] | 63 | static uint16_t tcp_checksum_calc(uint16_t ivalue, void *data, size_t size)
|
---|
| 64 | {
|
---|
| 65 | uint16_t sum;
|
---|
[0ac2158] | 66 | uint16_t w;
|
---|
[eea65f4] | 67 | size_t words, i;
|
---|
| 68 | uint8_t *bdata;
|
---|
| 69 |
|
---|
| 70 | sum = ~ivalue;
|
---|
| 71 | words = size / 2;
|
---|
| 72 | bdata = (uint8_t *)data;
|
---|
| 73 |
|
---|
| 74 | for (i = 0; i < words; i++) {
|
---|
[0ac2158] | 75 | w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
|
---|
| 76 | sum = tcp_ocadd16(sum, w);
|
---|
[eea65f4] | 77 | }
|
---|
| 78 |
|
---|
| 79 | if (size % 2 != 0) {
|
---|
[0ac2158] | 80 | w = ((uint16_t)bdata[2*words] << 8);
|
---|
| 81 | sum = tcp_ocadd16(sum, w);
|
---|
[eea65f4] | 82 | }
|
---|
| 83 |
|
---|
| 84 | return ~sum;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | static void tcp_header_decode_flags(uint16_t doff_flags, tcp_control_t *rctl)
|
---|
[c5808b41] | 88 | {
|
---|
[eea65f4] | 89 | tcp_control_t ctl;
|
---|
| 90 |
|
---|
| 91 | ctl = 0;
|
---|
| 92 |
|
---|
[6896409c] | 93 | if ((doff_flags & BIT_V(uint16_t, DF_URG)) != 0)
|
---|
[eea65f4] | 94 | ctl |= 0 /* XXX */;
|
---|
[6896409c] | 95 | if ((doff_flags & BIT_V(uint16_t, DF_ACK)) != 0)
|
---|
[eea65f4] | 96 | ctl |= CTL_ACK;
|
---|
[6896409c] | 97 | if ((doff_flags & BIT_V(uint16_t, DF_PSH)) != 0)
|
---|
[eea65f4] | 98 | ctl |= 0 /* XXX */;
|
---|
[6896409c] | 99 | if ((doff_flags & BIT_V(uint16_t, DF_RST)) != 0)
|
---|
[eea65f4] | 100 | ctl |= CTL_RST;
|
---|
[6896409c] | 101 | if ((doff_flags & BIT_V(uint16_t, DF_SYN)) != 0)
|
---|
[eea65f4] | 102 | ctl |= CTL_SYN;
|
---|
[6896409c] | 103 | if ((doff_flags & BIT_V(uint16_t, DF_FIN)) != 0)
|
---|
[eea65f4] | 104 | ctl |= CTL_FIN;
|
---|
| 105 |
|
---|
| 106 | *rctl = ctl;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | static void tcp_header_encode_flags(tcp_control_t ctl, uint16_t doff_flags0,
|
---|
| 110 | uint16_t *rdoff_flags)
|
---|
| 111 | {
|
---|
| 112 | uint16_t doff_flags;
|
---|
| 113 |
|
---|
| 114 | doff_flags = doff_flags0;
|
---|
| 115 |
|
---|
| 116 | if ((ctl & CTL_ACK) != 0)
|
---|
[6896409c] | 117 | doff_flags |= BIT_V(uint16_t, DF_ACK);
|
---|
[eea65f4] | 118 | if ((ctl & CTL_RST) != 0)
|
---|
[6896409c] | 119 | doff_flags |= BIT_V(uint16_t, DF_RST);
|
---|
[eea65f4] | 120 | if ((ctl & CTL_SYN) != 0)
|
---|
[6896409c] | 121 | doff_flags |= BIT_V(uint16_t, DF_SYN);
|
---|
[eea65f4] | 122 | if ((ctl & CTL_FIN) != 0)
|
---|
[6896409c] | 123 | doff_flags |= BIT_V(uint16_t, DF_FIN);
|
---|
[eea65f4] | 124 |
|
---|
| 125 | *rdoff_flags = doff_flags;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[2f19103] | 128 | static void tcp_header_setup(inet_ep2_t *epp, tcp_segment_t *seg, tcp_header_t *hdr)
|
---|
[eea65f4] | 129 | {
|
---|
| 130 | uint16_t doff_flags;
|
---|
[5f9ecd3] | 131 | uint16_t doff;
|
---|
[eea65f4] | 132 |
|
---|
[2f19103] | 133 | hdr->src_port = host2uint16_t_be(epp->local.port);
|
---|
| 134 | hdr->dest_port = host2uint16_t_be(epp->remote.port);
|
---|
[eea65f4] | 135 | hdr->seq = host2uint32_t_be(seg->seq);
|
---|
| 136 | hdr->ack = host2uint32_t_be(seg->ack);
|
---|
[5f9ecd3] | 137 |
|
---|
| 138 | doff = (sizeof(tcp_header_t) / sizeof(uint32_t)) << DF_DATA_OFFSET_l;
|
---|
| 139 | tcp_header_encode_flags(seg->ctrl, doff, &doff_flags);
|
---|
| 140 |
|
---|
[eea65f4] | 141 | hdr->doff_flags = host2uint16_t_be(doff_flags);
|
---|
| 142 | hdr->window = host2uint16_t_be(seg->wnd);
|
---|
[c5808b41] | 143 | hdr->checksum = 0;
|
---|
[eea65f4] | 144 | hdr->urg_ptr = host2uint16_t_be(seg->up);
|
---|
[c5808b41] | 145 | }
|
---|
| 146 |
|
---|
[f023251] | 147 | static ip_ver_t tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr,
|
---|
[1d24ad3] | 148 | tcp_phdr6_t *phdr6)
|
---|
[c5808b41] | 149 | {
|
---|
[02a09ed] | 150 | addr32_t src_v4;
|
---|
| 151 | addr128_t src_v6;
|
---|
[f023251] | 152 | uint16_t src_ver = inet_addr_get(&pdu->src, &src_v4, &src_v6);
|
---|
| 153 |
|
---|
[02a09ed] | 154 | addr32_t dest_v4;
|
---|
| 155 | addr128_t dest_v6;
|
---|
[f023251] | 156 | uint16_t dest_ver = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
|
---|
| 157 |
|
---|
| 158 | assert(src_ver == dest_ver);
|
---|
| 159 |
|
---|
| 160 | switch (src_ver) {
|
---|
| 161 | case ip_v4:
|
---|
[02a09ed] | 162 | phdr->src = host2uint32_t_be(src_v4);
|
---|
| 163 | phdr->dest = host2uint32_t_be(dest_v4);
|
---|
| 164 | phdr->zero = 0;
|
---|
| 165 | phdr->protocol = IP_PROTO_TCP;
|
---|
| 166 | phdr->tcp_length =
|
---|
| 167 | host2uint16_t_be(pdu->header_size + pdu->text_size);
|
---|
| 168 | break;
|
---|
[f023251] | 169 | case ip_v6:
|
---|
[1d24ad3] | 170 | host2addr128_t_be(src_v6, phdr6->src);
|
---|
| 171 | host2addr128_t_be(dest_v6, phdr6->dest);
|
---|
| 172 | phdr6->tcp_length =
|
---|
| 173 | host2uint32_t_be(pdu->header_size + pdu->text_size);
|
---|
[12df1f1] | 174 | memset(phdr6->zeroes, 0, 3);
|
---|
[1d24ad3] | 175 | phdr6->next = IP_PROTO_TCP;
|
---|
| 176 | break;
|
---|
[02a09ed] | 177 | default:
|
---|
| 178 | assert(false);
|
---|
| 179 | }
|
---|
[f023251] | 180 |
|
---|
| 181 | return src_ver;
|
---|
[eea65f4] | 182 | }
|
---|
| 183 |
|
---|
| 184 | static void tcp_header_decode(tcp_header_t *hdr, tcp_segment_t *seg)
|
---|
| 185 | {
|
---|
| 186 | tcp_header_decode_flags(uint16_t_be2host(hdr->doff_flags), &seg->ctrl);
|
---|
| 187 | seg->seq = uint32_t_be2host(hdr->seq);
|
---|
| 188 | seg->ack = uint32_t_be2host(hdr->ack);
|
---|
| 189 | seg->wnd = uint16_t_be2host(hdr->window);
|
---|
| 190 | seg->up = uint16_t_be2host(hdr->urg_ptr);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[2f19103] | 193 | static int tcp_header_encode(inet_ep2_t *epp, tcp_segment_t *seg,
|
---|
[eea65f4] | 194 | void **header, size_t *size)
|
---|
| 195 | {
|
---|
| 196 | tcp_header_t *hdr;
|
---|
| 197 |
|
---|
| 198 | hdr = calloc(1, sizeof(tcp_header_t));
|
---|
| 199 | if (hdr == NULL)
|
---|
| 200 | return ENOMEM;
|
---|
| 201 |
|
---|
[2f19103] | 202 | tcp_header_setup(epp, seg, hdr);
|
---|
[eea65f4] | 203 | *header = hdr;
|
---|
| 204 | *size = sizeof(tcp_header_t);
|
---|
| 205 |
|
---|
| 206 | return EOK;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | static tcp_pdu_t *tcp_pdu_new(void)
|
---|
| 210 | {
|
---|
| 211 | return calloc(1, sizeof(tcp_pdu_t));
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[1812a0d] | 214 | /** Create PDU with the specified header and text data.
|
---|
| 215 | *
|
---|
| 216 | * Note that you still need to set addresses in the returned PDU.
|
---|
| 217 | *
|
---|
| 218 | * @param hdr Header data
|
---|
| 219 | * @param hdr_size Header size in bytes
|
---|
| 220 | * @param text Text data
|
---|
| 221 | * @param text_size Text size in bytes
|
---|
| 222 | * @return New PDU
|
---|
| 223 | */
|
---|
| 224 | tcp_pdu_t *tcp_pdu_create(void *hdr, size_t hdr_size, void *text,
|
---|
| 225 | size_t text_size)
|
---|
| 226 | {
|
---|
| 227 | tcp_pdu_t *pdu;
|
---|
| 228 |
|
---|
| 229 | pdu = tcp_pdu_new();
|
---|
| 230 | if (pdu == NULL)
|
---|
| 231 | return NULL;
|
---|
| 232 |
|
---|
| 233 | pdu->header = malloc(hdr_size);
|
---|
| 234 | pdu->text = malloc(text_size);
|
---|
| 235 | if (pdu->header == NULL || pdu->text == NULL)
|
---|
| 236 | goto error;
|
---|
| 237 |
|
---|
| 238 | memcpy(pdu->header, hdr, hdr_size);
|
---|
| 239 | memcpy(pdu->text, text, text_size);
|
---|
| 240 |
|
---|
| 241 | pdu->header_size = hdr_size;
|
---|
| 242 | pdu->text_size = text_size;
|
---|
| 243 |
|
---|
| 244 | return pdu;
|
---|
| 245 |
|
---|
| 246 | error:
|
---|
| 247 | if (pdu->header != NULL)
|
---|
| 248 | free(pdu->header);
|
---|
| 249 | if (pdu->text != NULL)
|
---|
| 250 | free(pdu->text);
|
---|
| 251 |
|
---|
| 252 | return NULL;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[eea65f4] | 255 | void tcp_pdu_delete(tcp_pdu_t *pdu)
|
---|
| 256 | {
|
---|
[1812a0d] | 257 | free(pdu->header);
|
---|
[eea65f4] | 258 | free(pdu->text);
|
---|
| 259 | free(pdu);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | static uint16_t tcp_pdu_checksum_calc(tcp_pdu_t *pdu)
|
---|
| 263 | {
|
---|
| 264 | uint16_t cs_phdr;
|
---|
| 265 | uint16_t cs_headers;
|
---|
| 266 | tcp_phdr_t phdr;
|
---|
[1d24ad3] | 267 | tcp_phdr6_t phdr6;
|
---|
[f023251] | 268 |
|
---|
| 269 | ip_ver_t ver = tcp_phdr_setup(pdu, &phdr, &phdr6);
|
---|
| 270 | switch (ver) {
|
---|
| 271 | case ip_v4:
|
---|
[02a09ed] | 272 | cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr,
|
---|
| 273 | sizeof(tcp_phdr_t));
|
---|
| 274 | break;
|
---|
[f023251] | 275 | case ip_v6:
|
---|
[1d24ad3] | 276 | cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr6,
|
---|
| 277 | sizeof(tcp_phdr6_t));
|
---|
| 278 | break;
|
---|
[02a09ed] | 279 | default:
|
---|
| 280 | assert(false);
|
---|
| 281 | }
|
---|
[f023251] | 282 |
|
---|
[eea65f4] | 283 | cs_headers = tcp_checksum_calc(cs_phdr, pdu->header, pdu->header_size);
|
---|
[02a09ed] | 284 | return tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size);
|
---|
[eea65f4] | 285 | }
|
---|
| 286 |
|
---|
| 287 | static void tcp_pdu_set_checksum(tcp_pdu_t *pdu, uint16_t checksum)
|
---|
| 288 | {
|
---|
| 289 | tcp_header_t *hdr;
|
---|
| 290 |
|
---|
| 291 | hdr = (tcp_header_t *)pdu->header;
|
---|
[0ac2158] | 292 | hdr->checksum = host2uint16_t_be(checksum);
|
---|
[eea65f4] | 293 | }
|
---|
| 294 |
|
---|
[66a272f8] | 295 | /** Decode incoming PDU */
|
---|
[2f19103] | 296 | int tcp_pdu_decode(tcp_pdu_t *pdu, inet_ep2_t *epp, tcp_segment_t **seg)
|
---|
[eea65f4] | 297 | {
|
---|
| 298 | tcp_segment_t *nseg;
|
---|
| 299 | tcp_header_t *hdr;
|
---|
| 300 |
|
---|
| 301 | nseg = tcp_segment_make_data(0, pdu->text, pdu->text_size);
|
---|
| 302 | if (nseg == NULL)
|
---|
| 303 | return ENOMEM;
|
---|
| 304 |
|
---|
| 305 | tcp_header_decode(pdu->header, nseg);
|
---|
[6896409c] | 306 | nseg->len += seq_no_control_len(nseg->ctrl);
|
---|
[eea65f4] | 307 |
|
---|
| 308 | hdr = (tcp_header_t *)pdu->header;
|
---|
| 309 |
|
---|
[2f19103] | 310 | epp->local.port = uint16_t_be2host(hdr->dest_port);
|
---|
| 311 | epp->local.addr = pdu->dest;
|
---|
| 312 | epp->remote.port = uint16_t_be2host(hdr->src_port);
|
---|
| 313 | epp->remote.addr = pdu->src;
|
---|
[eea65f4] | 314 |
|
---|
| 315 | *seg = nseg;
|
---|
| 316 | return EOK;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[66a272f8] | 319 | /** Encode outgoing PDU */
|
---|
[2f19103] | 320 | int tcp_pdu_encode(inet_ep2_t *epp, tcp_segment_t *seg, tcp_pdu_t **pdu)
|
---|
[eea65f4] | 321 | {
|
---|
| 322 | tcp_pdu_t *npdu;
|
---|
| 323 | size_t text_size;
|
---|
| 324 | uint16_t checksum;
|
---|
| 325 |
|
---|
| 326 | npdu = tcp_pdu_new();
|
---|
| 327 | if (npdu == NULL)
|
---|
| 328 | return ENOMEM;
|
---|
| 329 |
|
---|
[2f19103] | 330 | npdu->src = epp->local.addr;
|
---|
| 331 | npdu->dest = epp->remote.addr;
|
---|
| 332 | tcp_header_encode(epp, seg, &npdu->header, &npdu->header_size);
|
---|
[eea65f4] | 333 |
|
---|
| 334 | text_size = tcp_segment_text_size(seg);
|
---|
| 335 | npdu->text = calloc(1, text_size);
|
---|
| 336 | if (npdu->text == NULL)
|
---|
| 337 | return ENOMEM;
|
---|
| 338 |
|
---|
| 339 | npdu->text_size = text_size;
|
---|
| 340 | memcpy(npdu->text, seg->data, text_size);
|
---|
| 341 |
|
---|
| 342 | /* Checksum calculation */
|
---|
| 343 | checksum = tcp_pdu_checksum_calc(npdu);
|
---|
| 344 | tcp_pdu_set_checksum(npdu, checksum);
|
---|
[c5808b41] | 345 |
|
---|
[eea65f4] | 346 | *pdu = npdu;
|
---|
| 347 | return EOK;
|
---|
[c5808b41] | 348 | }
|
---|
| 349 |
|
---|
| 350 | /**
|
---|
| 351 | * @}
|
---|
| 352 | */
|
---|