source: mainline/uspace/srv/inet/pdu.c@ 0e25780

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0e25780 was 1cc8b42, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Correct IP checksum and identification.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 * Copyright (c) 2012 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 inet
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <align.h>
38#include <bitops.h>
39#include <byteorder.h>
40#include <errno.h>
41#include <fibril_synch.h>
42#include <io/log.h>
43#include <mem.h>
44#include <stdlib.h>
45
46#include "inet.h"
47#include "inet_std.h"
48#include "pdu.h"
49
50static FIBRIL_MUTEX_INITIALIZE(ip_ident_lock);
51static uint16_t ip_ident = 0;
52
53#define INET_CHECKSUM_INIT 0xffff
54
55/** One's complement addition.
56 *
57 * Result is a + b + carry.
58 */
59static uint16_t inet_ocadd16(uint16_t a, uint16_t b)
60{
61 uint32_t s;
62
63 s = (uint32_t)a + (uint32_t)b;
64 return (s & 0xffff) + (s >> 16);
65}
66
67static uint16_t inet_checksum_calc(uint16_t ivalue, void *data, size_t size)
68{
69 uint16_t sum;
70 uint16_t w;
71 size_t words, i;
72 uint8_t *bdata;
73
74 sum = ~ivalue;
75 words = size / 2;
76 bdata = (uint8_t *)data;
77
78 for (i = 0; i < words; i++) {
79 w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
80 sum = inet_ocadd16(sum, w);
81 }
82
83 if (size % 2 != 0) {
84 w = ((uint16_t)bdata[2*words] << 8);
85 sum = inet_ocadd16(sum, w);
86 }
87
88 return ~sum;
89}
90
91/** Encode Internet PDU.
92 */
93int inet_pdu_encode(inet_packet_t *packet, void **rdata, size_t *rsize)
94{
95 void *data;
96 size_t size;
97 ip_header_t *hdr;
98 size_t hdr_size;
99 size_t data_offs;
100 uint16_t chksum;
101 uint16_t ident;
102
103 fibril_mutex_lock(&ip_ident_lock);
104 ident = ++ip_ident;
105 fibril_mutex_unlock(&ip_ident_lock);
106
107 hdr_size = sizeof(ip_header_t);
108 size = hdr_size + packet->size;
109 data_offs = ROUND_UP(hdr_size, 4);
110
111 data = calloc(size, 1);
112 if (data == NULL)
113 return ENOMEM;
114
115 hdr = (ip_header_t *)data;
116 hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
117 hdr->tos = packet->tos;
118 hdr->tot_len = host2uint16_t_be(size);
119 hdr->id = host2uint16_t_be(ident);
120 hdr->flags_foff = host2uint16_t_be(packet->df ?
121 BIT_V(uint16_t, FF_FLAG_DF) : 0);
122 hdr->ttl = packet->ttl;
123 hdr->proto = packet->proto;
124 hdr->chksum = 0;
125 hdr->src_addr = host2uint32_t_be(packet->src.ipv4);
126 hdr->dest_addr = host2uint32_t_be(packet->dest.ipv4);
127
128 chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *)hdr, hdr_size);
129 hdr->chksum = host2uint16_t_be(chksum);
130
131 memcpy((uint8_t *)data + data_offs, packet->data, packet->size);
132
133 *rdata = data;
134 *rsize = size;
135 return EOK;
136}
137
138int inet_pdu_decode(void *data, size_t size, inet_packet_t *packet)
139{
140 ip_header_t *hdr;
141 size_t tot_len;
142 size_t data_offs;
143 uint8_t version;
144 uint16_t ident;
145
146 log_msg(LVL_DEBUG, "inet_pdu_decode()");
147
148 if (size < sizeof(ip_header_t)) {
149 log_msg(LVL_DEBUG, "PDU too short (%zu)", size);
150 return EINVAL;
151 }
152
153 hdr = (ip_header_t *)data;
154
155 version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h, VI_VERSION_l,
156 hdr->ver_ihl);
157 if (version != 4) {
158 log_msg(LVL_DEBUG, "Version (%d) != 4", version);
159 return EINVAL;
160 }
161
162 tot_len = uint16_t_be2host(hdr->tot_len);
163 if (tot_len < sizeof(ip_header_t)) {
164 log_msg(LVL_DEBUG, "Total Length too small (%zu)", tot_len);
165 return EINVAL;
166 }
167
168 if (tot_len > size) {
169 log_msg(LVL_DEBUG, "Total Length = %zu > PDU size = %zu",
170 tot_len, size);
171 return EINVAL;
172 }
173
174 ident = uint16_t_be2host(hdr->id);
175 (void)ident;
176 /* XXX Flags */
177 /* XXX Fragment offset */
178 /* XXX Checksum */
179
180 packet->src.ipv4 = uint32_t_be2host(hdr->src_addr);
181 packet->dest.ipv4 = uint32_t_be2host(hdr->dest_addr);
182 packet->tos = hdr->tos;
183 packet->proto = hdr->proto;
184 packet->ttl = hdr->ttl;
185 packet->df = (uint16_t_be2host(hdr->tos) & BIT_V(uint16_t, FF_FLAG_DF))
186 ? 1 : 0;
187
188 /* XXX IP options */
189 data_offs = sizeof(uint32_t) * BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h,
190 VI_IHL_l, hdr->ver_ihl);
191
192 packet->size = tot_len - data_offs;
193 packet->data = calloc(packet->size, 1);
194 if (packet->data == NULL) {
195 log_msg(LVL_WARN, "Out of memory.");
196 return ENOMEM;
197 }
198
199 memcpy(packet->data, (uint8_t *)data + data_offs, packet->size);
200
201 return EOK;
202}
203
204/** @}
205 */
Note: See TracBrowser for help on using the repository browser.