source: mainline/uspace/srv/net/tcp/pdu.c@ 4c14b88

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4c14b88 was 5a324d99, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Remove include where not needed anymore.

  • Property mode set to 100644
File size: 8.4 KB
Line 
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 "pdu.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/** One's complement addition.
51 *
52 * Result is a + b + carry.
53 */
54static uint16_t tcp_ocadd16(uint16_t a, uint16_t b)
55{
56 uint32_t s;
57
58 s = (uint32_t)a + (uint32_t)b;
59 return (s & 0xffff) + (s >> 16);
60}
61
62static uint16_t tcp_checksum_calc(uint16_t ivalue, void *data, size_t size)
63{
64 uint16_t sum;
65 uint16_t w;
66 size_t words, i;
67 uint8_t *bdata;
68
69 sum = ~ivalue;
70 words = size / 2;
71 bdata = (uint8_t *)data;
72
73 for (i = 0; i < words; i++) {
74 w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
75 sum = tcp_ocadd16(sum, w);
76 }
77
78 if (size % 2 != 0) {
79 w = ((uint16_t)bdata[2*words] << 8);
80 sum = tcp_ocadd16(sum, w);
81 }
82
83 return ~sum;
84}
85
86static void tcp_header_decode_flags(uint16_t doff_flags, tcp_control_t *rctl)
87{
88 tcp_control_t ctl;
89
90 ctl = 0;
91
92 if ((doff_flags & BIT_V(uint16_t, DF_URG)) != 0)
93 ctl |= 0 /* XXX */;
94 if ((doff_flags & BIT_V(uint16_t, DF_ACK)) != 0)
95 ctl |= CTL_ACK;
96 if ((doff_flags & BIT_V(uint16_t, DF_PSH)) != 0)
97 ctl |= 0 /* XXX */;
98 if ((doff_flags & BIT_V(uint16_t, DF_RST)) != 0)
99 ctl |= CTL_RST;
100 if ((doff_flags & BIT_V(uint16_t, DF_SYN)) != 0)
101 ctl |= CTL_SYN;
102 if ((doff_flags & BIT_V(uint16_t, DF_FIN)) != 0)
103 ctl |= CTL_FIN;
104
105 *rctl = ctl;
106}
107
108static void tcp_header_encode_flags(tcp_control_t ctl, uint16_t doff_flags0,
109 uint16_t *rdoff_flags)
110{
111 uint16_t doff_flags;
112
113 doff_flags = doff_flags0;
114
115 if ((ctl & CTL_ACK) != 0)
116 doff_flags |= BIT_V(uint16_t, DF_ACK);
117 if ((ctl & CTL_RST) != 0)
118 doff_flags |= BIT_V(uint16_t, DF_RST);
119 if ((ctl & CTL_SYN) != 0)
120 doff_flags |= BIT_V(uint16_t, DF_SYN);
121 if ((ctl & CTL_FIN) != 0)
122 doff_flags |= BIT_V(uint16_t, DF_FIN);
123
124 *rdoff_flags = doff_flags;
125}
126
127static void tcp_header_setup(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_header_t *hdr)
128{
129 uint16_t doff_flags;
130 uint16_t doff;
131
132 hdr->src_port = host2uint16_t_be(sp->local.port);
133 hdr->dest_port = host2uint16_t_be(sp->foreign.port);
134 hdr->seq = host2uint32_t_be(seg->seq);
135 hdr->ack = host2uint32_t_be(seg->ack);
136
137 doff = (sizeof(tcp_header_t) / sizeof(uint32_t)) << DF_DATA_OFFSET_l;
138 tcp_header_encode_flags(seg->ctrl, doff, &doff_flags);
139
140 hdr->doff_flags = host2uint16_t_be(doff_flags);
141 hdr->window = host2uint16_t_be(seg->wnd);
142 hdr->checksum = 0;
143 hdr->urg_ptr = host2uint16_t_be(seg->up);
144}
145
146static ip_ver_t tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr,
147 tcp_phdr6_t *phdr6)
148{
149 addr32_t src_v4;
150 addr128_t src_v6;
151 uint16_t src_ver = inet_addr_get(&pdu->src, &src_v4, &src_v6);
152
153 addr32_t dest_v4;
154 addr128_t dest_v6;
155 uint16_t dest_ver = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
156
157 assert(src_ver == dest_ver);
158
159 switch (src_ver) {
160 case ip_v4:
161 phdr->src = host2uint32_t_be(src_v4);
162 phdr->dest = host2uint32_t_be(dest_v4);
163 phdr->zero = 0;
164 phdr->protocol = IP_PROTO_TCP;
165 phdr->tcp_length =
166 host2uint16_t_be(pdu->header_size + pdu->text_size);
167 break;
168 case ip_v6:
169 host2addr128_t_be(src_v6, phdr6->src);
170 host2addr128_t_be(dest_v6, phdr6->dest);
171 phdr6->tcp_length =
172 host2uint32_t_be(pdu->header_size + pdu->text_size);
173 memset(phdr6->zeroes, 0, 3);
174 phdr6->next = IP_PROTO_TCP;
175 break;
176 default:
177 assert(false);
178 }
179
180 return src_ver;
181}
182
183static void tcp_header_decode(tcp_header_t *hdr, tcp_segment_t *seg)
184{
185 tcp_header_decode_flags(uint16_t_be2host(hdr->doff_flags), &seg->ctrl);
186 seg->seq = uint32_t_be2host(hdr->seq);
187 seg->ack = uint32_t_be2host(hdr->ack);
188 seg->wnd = uint16_t_be2host(hdr->window);
189 seg->up = uint16_t_be2host(hdr->urg_ptr);
190}
191
192static int tcp_header_encode(tcp_sockpair_t *sp, tcp_segment_t *seg,
193 void **header, size_t *size)
194{
195 tcp_header_t *hdr;
196
197 hdr = calloc(1, sizeof(tcp_header_t));
198 if (hdr == NULL)
199 return ENOMEM;
200
201 tcp_header_setup(sp, seg, hdr);
202 *header = hdr;
203 *size = sizeof(tcp_header_t);
204
205 return EOK;
206}
207
208static tcp_pdu_t *tcp_pdu_new(void)
209{
210 return calloc(1, sizeof(tcp_pdu_t));
211}
212
213/** Create PDU with the specified header and text data.
214 *
215 * Note that you still need to set addresses in the returned PDU.
216 *
217 * @param hdr Header data
218 * @param hdr_size Header size in bytes
219 * @param text Text data
220 * @param text_size Text size in bytes
221 * @return New PDU
222 */
223tcp_pdu_t *tcp_pdu_create(void *hdr, size_t hdr_size, void *text,
224 size_t text_size)
225{
226 tcp_pdu_t *pdu;
227
228 pdu = tcp_pdu_new();
229 if (pdu == NULL)
230 return NULL;
231
232 pdu->header = malloc(hdr_size);
233 pdu->text = malloc(text_size);
234 if (pdu->header == NULL || pdu->text == NULL)
235 goto error;
236
237 memcpy(pdu->header, hdr, hdr_size);
238 memcpy(pdu->text, text, text_size);
239
240 pdu->header_size = hdr_size;
241 pdu->text_size = text_size;
242
243 return pdu;
244
245error:
246 if (pdu->header != NULL)
247 free(pdu->header);
248 if (pdu->text != NULL)
249 free(pdu->text);
250
251 return NULL;
252}
253
254void tcp_pdu_delete(tcp_pdu_t *pdu)
255{
256 free(pdu->header);
257 free(pdu->text);
258 free(pdu);
259}
260
261static uint16_t tcp_pdu_checksum_calc(tcp_pdu_t *pdu)
262{
263 uint16_t cs_phdr;
264 uint16_t cs_headers;
265 tcp_phdr_t phdr;
266 tcp_phdr6_t phdr6;
267
268 ip_ver_t ver = tcp_phdr_setup(pdu, &phdr, &phdr6);
269 switch (ver) {
270 case ip_v4:
271 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr,
272 sizeof(tcp_phdr_t));
273 break;
274 case ip_v6:
275 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr6,
276 sizeof(tcp_phdr6_t));
277 break;
278 default:
279 assert(false);
280 }
281
282 cs_headers = tcp_checksum_calc(cs_phdr, pdu->header, pdu->header_size);
283 return tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size);
284}
285
286static void tcp_pdu_set_checksum(tcp_pdu_t *pdu, uint16_t checksum)
287{
288 tcp_header_t *hdr;
289
290 hdr = (tcp_header_t *)pdu->header;
291 hdr->checksum = host2uint16_t_be(checksum);
292}
293
294/** Decode incoming PDU */
295int tcp_pdu_decode(tcp_pdu_t *pdu, tcp_sockpair_t *sp, tcp_segment_t **seg)
296{
297 tcp_segment_t *nseg;
298 tcp_header_t *hdr;
299
300 nseg = tcp_segment_make_data(0, pdu->text, pdu->text_size);
301 if (nseg == NULL)
302 return ENOMEM;
303
304 tcp_header_decode(pdu->header, nseg);
305 nseg->len += seq_no_control_len(nseg->ctrl);
306
307 hdr = (tcp_header_t *)pdu->header;
308
309 sp->local.port = uint16_t_be2host(hdr->dest_port);
310 sp->local.addr = pdu->dest;
311 sp->foreign.port = uint16_t_be2host(hdr->src_port);
312 sp->foreign.addr = pdu->src;
313
314 *seg = nseg;
315 return EOK;
316}
317
318/** Encode outgoing PDU */
319int tcp_pdu_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_pdu_t **pdu)
320{
321 tcp_pdu_t *npdu;
322 size_t text_size;
323 uint16_t checksum;
324
325 npdu = tcp_pdu_new();
326 if (npdu == NULL)
327 return ENOMEM;
328
329 npdu->src = sp->local.addr;
330 npdu->dest = sp->foreign.addr;
331 tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size);
332
333 text_size = tcp_segment_text_size(seg);
334 npdu->text = calloc(1, text_size);
335 if (npdu->text == NULL)
336 return ENOMEM;
337
338 npdu->text_size = text_size;
339 memcpy(npdu->text, seg->data, text_size);
340
341 /* Checksum calculation */
342 checksum = tcp_pdu_checksum_calc(npdu);
343 tcp_pdu_set_checksum(npdu, checksum);
344
345 *pdu = npdu;
346 return EOK;
347}
348
349/**
350 * @}
351 */
Note: See TracBrowser for help on using the repository browser.