| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 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 | /**
|
|---|
| 34 | * @file TCP header encoding and decoding
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <bitops.h>
|
|---|
| 38 | #include <byteorder.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <mem.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| 42 | #include "header.h"
|
|---|
| 43 | #include "segment.h"
|
|---|
| 44 | #include "seq_no.h"
|
|---|
| 45 | #include "std.h"
|
|---|
| 46 | #include "tcp_type.h"
|
|---|
| 47 |
|
|---|
| 48 | #define TCP_CHECKSUM_INIT 0xffff
|
|---|
| 49 |
|
|---|
| 50 | static uint16_t tcp_checksum_calc(uint16_t ivalue, void *data, size_t size)
|
|---|
| 51 | {
|
|---|
| 52 | uint16_t sum;
|
|---|
| 53 | size_t words, i;
|
|---|
| 54 | uint8_t *bdata;
|
|---|
| 55 |
|
|---|
| 56 | sum = ~ivalue;
|
|---|
| 57 | words = size / 2;
|
|---|
| 58 | bdata = (uint8_t *)data;
|
|---|
| 59 |
|
|---|
| 60 | for (i = 0; i < words; i++) {
|
|---|
| 61 | sum += ~(((uint16_t)bdata[2*i] << 8) + bdata[2*i + 1]);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | if (size % 2 != 0) {
|
|---|
| 65 | sum += ~((uint16_t)bdata[2*words] << 8);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | return ~sum;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | static void tcp_header_decode_flags(uint16_t doff_flags, tcp_control_t *rctl)
|
|---|
| 72 | {
|
|---|
| 73 | tcp_control_t ctl;
|
|---|
| 74 |
|
|---|
| 75 | ctl = 0;
|
|---|
| 76 |
|
|---|
| 77 | if ((doff_flags & BIT_V(uint16_t, DF_URG)) != 0)
|
|---|
| 78 | ctl |= 0 /* XXX */;
|
|---|
| 79 | if ((doff_flags & BIT_V(uint16_t, DF_ACK)) != 0)
|
|---|
| 80 | ctl |= CTL_ACK;
|
|---|
| 81 | if ((doff_flags & BIT_V(uint16_t, DF_PSH)) != 0)
|
|---|
| 82 | ctl |= 0 /* XXX */;
|
|---|
| 83 | if ((doff_flags & BIT_V(uint16_t, DF_RST)) != 0)
|
|---|
| 84 | ctl |= CTL_RST;
|
|---|
| 85 | if ((doff_flags & BIT_V(uint16_t, DF_SYN)) != 0)
|
|---|
| 86 | ctl |= CTL_SYN;
|
|---|
| 87 | if ((doff_flags & BIT_V(uint16_t, DF_FIN)) != 0)
|
|---|
| 88 | ctl |= CTL_FIN;
|
|---|
| 89 |
|
|---|
| 90 | *rctl = ctl;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static void tcp_header_encode_flags(tcp_control_t ctl, uint16_t doff_flags0,
|
|---|
| 94 | uint16_t *rdoff_flags)
|
|---|
| 95 | {
|
|---|
| 96 | uint16_t doff_flags;
|
|---|
| 97 |
|
|---|
| 98 | doff_flags = doff_flags0;
|
|---|
| 99 |
|
|---|
| 100 | if ((ctl & CTL_ACK) != 0)
|
|---|
| 101 | doff_flags |= BIT_V(uint16_t, DF_ACK);
|
|---|
| 102 | if ((ctl & CTL_RST) != 0)
|
|---|
| 103 | doff_flags |= BIT_V(uint16_t, DF_RST);
|
|---|
| 104 | if ((ctl & CTL_SYN) != 0)
|
|---|
| 105 | doff_flags |= BIT_V(uint16_t, DF_SYN);
|
|---|
| 106 | if ((ctl & CTL_FIN) != 0)
|
|---|
| 107 | doff_flags |= BIT_V(uint16_t, DF_FIN);
|
|---|
| 108 |
|
|---|
| 109 | *rdoff_flags = doff_flags;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | static void tcp_header_setup(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_header_t *hdr)
|
|---|
| 113 | {
|
|---|
| 114 | uint16_t doff_flags;
|
|---|
| 115 |
|
|---|
| 116 | hdr->src_port = host2uint16_t_be(sp->local.port);
|
|---|
| 117 | hdr->dest_port = host2uint16_t_be(sp->foreign.port);
|
|---|
| 118 | hdr->seq = host2uint32_t_be(seg->seq);
|
|---|
| 119 | hdr->ack = host2uint32_t_be(seg->ack);
|
|---|
| 120 | tcp_header_encode_flags(seg->ctrl, 0, &doff_flags);
|
|---|
| 121 | hdr->doff_flags = host2uint16_t_be(doff_flags);
|
|---|
| 122 | hdr->window = host2uint16_t_be(seg->wnd);
|
|---|
| 123 | hdr->checksum = 0;
|
|---|
| 124 | hdr->urg_ptr = host2uint16_t_be(seg->up);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static void tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr)
|
|---|
| 128 | {
|
|---|
| 129 | phdr->src_addr = host2uint32_t_be(pdu->src_addr.ipv4);
|
|---|
| 130 | phdr->dest_addr = host2uint32_t_be(pdu->dest_addr.ipv4);
|
|---|
| 131 | phdr->zero = 0;
|
|---|
| 132 | phdr->protocol = 6; /* XXX Magic number */
|
|---|
| 133 | phdr->tcp_length = host2uint16_t_be(pdu->header_size + pdu->text_size);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static void tcp_header_decode(tcp_header_t *hdr, tcp_segment_t *seg)
|
|---|
| 137 | {
|
|---|
| 138 | tcp_header_decode_flags(uint16_t_be2host(hdr->doff_flags), &seg->ctrl);
|
|---|
| 139 | seg->seq = uint32_t_be2host(hdr->seq);
|
|---|
| 140 | seg->ack = uint32_t_be2host(hdr->ack);
|
|---|
| 141 | seg->wnd = uint16_t_be2host(hdr->window);
|
|---|
| 142 | seg->up = uint16_t_be2host(hdr->urg_ptr);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | static int tcp_header_encode(tcp_sockpair_t *sp, tcp_segment_t *seg,
|
|---|
| 146 | void **header, size_t *size)
|
|---|
| 147 | {
|
|---|
| 148 | tcp_header_t *hdr;
|
|---|
| 149 |
|
|---|
| 150 | hdr = calloc(1, sizeof(tcp_header_t));
|
|---|
| 151 | if (hdr == NULL)
|
|---|
| 152 | return ENOMEM;
|
|---|
| 153 |
|
|---|
| 154 | tcp_header_setup(sp, seg, hdr);
|
|---|
| 155 | *header = hdr;
|
|---|
| 156 | *size = sizeof(tcp_header_t);
|
|---|
| 157 |
|
|---|
| 158 | return EOK;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | static tcp_pdu_t *tcp_pdu_new(void)
|
|---|
| 162 | {
|
|---|
| 163 | return calloc(1, sizeof(tcp_pdu_t));
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** Create PDU with the specified header and text data.
|
|---|
| 167 | *
|
|---|
| 168 | * Note that you still need to set addresses in the returned PDU.
|
|---|
| 169 | *
|
|---|
| 170 | * @param hdr Header data
|
|---|
| 171 | * @param hdr_size Header size in bytes
|
|---|
| 172 | * @param text Text data
|
|---|
| 173 | * @param text_size Text size in bytes
|
|---|
| 174 | * @return New PDU
|
|---|
| 175 | */
|
|---|
| 176 | tcp_pdu_t *tcp_pdu_create(void *hdr, size_t hdr_size, void *text,
|
|---|
| 177 | size_t text_size)
|
|---|
| 178 | {
|
|---|
| 179 | tcp_pdu_t *pdu;
|
|---|
| 180 |
|
|---|
| 181 | pdu = tcp_pdu_new();
|
|---|
| 182 | if (pdu == NULL)
|
|---|
| 183 | return NULL;
|
|---|
| 184 |
|
|---|
| 185 | pdu->header = malloc(hdr_size);
|
|---|
| 186 | pdu->text = malloc(text_size);
|
|---|
| 187 | if (pdu->header == NULL || pdu->text == NULL)
|
|---|
| 188 | goto error;
|
|---|
| 189 |
|
|---|
| 190 | memcpy(pdu->header, hdr, hdr_size);
|
|---|
| 191 | memcpy(pdu->text, text, text_size);
|
|---|
| 192 |
|
|---|
| 193 | pdu->header_size = hdr_size;
|
|---|
| 194 | pdu->text_size = text_size;
|
|---|
| 195 |
|
|---|
| 196 | return pdu;
|
|---|
| 197 |
|
|---|
| 198 | error:
|
|---|
| 199 | if (pdu->header != NULL)
|
|---|
| 200 | free(pdu->header);
|
|---|
| 201 | if (pdu->text != NULL)
|
|---|
| 202 | free(pdu->text);
|
|---|
| 203 |
|
|---|
| 204 | return NULL;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | void tcp_pdu_delete(tcp_pdu_t *pdu)
|
|---|
| 208 | {
|
|---|
| 209 | free(pdu->header);
|
|---|
| 210 | free(pdu->text);
|
|---|
| 211 | free(pdu);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | static uint16_t tcp_pdu_checksum_calc(tcp_pdu_t *pdu)
|
|---|
| 215 | {
|
|---|
| 216 | uint16_t cs_phdr;
|
|---|
| 217 | uint16_t cs_headers;
|
|---|
| 218 | uint16_t cs_all;
|
|---|
| 219 | tcp_phdr_t phdr;
|
|---|
| 220 |
|
|---|
| 221 | tcp_phdr_setup(pdu, &phdr);
|
|---|
| 222 | cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *)&phdr,
|
|---|
| 223 | sizeof(tcp_phdr_t));
|
|---|
| 224 | cs_headers = tcp_checksum_calc(cs_phdr, pdu->header, pdu->header_size);
|
|---|
| 225 | cs_all = tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size);
|
|---|
| 226 |
|
|---|
| 227 | return cs_all;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | static void tcp_pdu_set_checksum(tcp_pdu_t *pdu, uint16_t checksum)
|
|---|
| 231 | {
|
|---|
| 232 | tcp_header_t *hdr;
|
|---|
| 233 |
|
|---|
| 234 | hdr = (tcp_header_t *)pdu->header;
|
|---|
| 235 | hdr->checksum = host2uint16_t_le(checksum);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /** Encode outgoing PDU */
|
|---|
| 239 | int tcp_pdu_decode(tcp_pdu_t *pdu, tcp_sockpair_t *sp, tcp_segment_t **seg)
|
|---|
| 240 | {
|
|---|
| 241 | tcp_segment_t *nseg;
|
|---|
| 242 | tcp_header_t *hdr;
|
|---|
| 243 |
|
|---|
| 244 | nseg = tcp_segment_make_data(0, pdu->text, pdu->text_size);
|
|---|
| 245 | if (nseg == NULL)
|
|---|
| 246 | return ENOMEM;
|
|---|
| 247 |
|
|---|
| 248 | tcp_header_decode(pdu->header, nseg);
|
|---|
| 249 | nseg->len += seq_no_control_len(nseg->ctrl);
|
|---|
| 250 |
|
|---|
| 251 | hdr = (tcp_header_t *)pdu->header;
|
|---|
| 252 |
|
|---|
| 253 | sp->local.port = uint16_t_be2host(hdr->dest_port);
|
|---|
| 254 | sp->local.addr = pdu->dest_addr;
|
|---|
| 255 | sp->foreign.port = uint16_t_be2host(hdr->src_port);
|
|---|
| 256 | sp->foreign.addr = pdu->src_addr;
|
|---|
| 257 |
|
|---|
| 258 | *seg = nseg;
|
|---|
| 259 | return EOK;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /** Decode incoming PDU */
|
|---|
| 263 | int tcp_pdu_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_pdu_t **pdu)
|
|---|
| 264 | {
|
|---|
| 265 | tcp_pdu_t *npdu;
|
|---|
| 266 | size_t text_size;
|
|---|
| 267 | uint16_t checksum;
|
|---|
| 268 |
|
|---|
| 269 | npdu = tcp_pdu_new();
|
|---|
| 270 | if (npdu == NULL)
|
|---|
| 271 | return ENOMEM;
|
|---|
| 272 |
|
|---|
| 273 | npdu->src_addr = sp->local.addr;
|
|---|
| 274 | npdu->dest_addr = sp->foreign.addr;
|
|---|
| 275 | tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size);
|
|---|
| 276 |
|
|---|
| 277 | text_size = tcp_segment_text_size(seg);
|
|---|
| 278 | npdu->text = calloc(1, text_size);
|
|---|
| 279 | if (npdu->text == NULL)
|
|---|
| 280 | return ENOMEM;
|
|---|
| 281 |
|
|---|
| 282 | npdu->text_size = text_size;
|
|---|
| 283 | memcpy(npdu->text, seg->data, text_size);
|
|---|
| 284 |
|
|---|
| 285 | /* Checksum calculation */
|
|---|
| 286 | checksum = tcp_pdu_checksum_calc(npdu);
|
|---|
| 287 | tcp_pdu_set_checksum(npdu, checksum);
|
|---|
| 288 |
|
|---|
| 289 | *pdu = npdu;
|
|---|
| 290 | return EOK;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /**
|
|---|
| 294 | * @}
|
|---|
| 295 | */
|
|---|