| 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 <macros.h>
|
|---|
| 44 | #include <mem.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "inetsrv.h"
|
|---|
| 48 | #include "inet_std.h"
|
|---|
| 49 | #include "pdu.h"
|
|---|
| 50 |
|
|---|
| 51 | static FIBRIL_MUTEX_INITIALIZE(ip_ident_lock);
|
|---|
| 52 | static uint16_t ip_ident = 0;
|
|---|
| 53 |
|
|---|
| 54 | /** One's complement addition.
|
|---|
| 55 | *
|
|---|
| 56 | * Result is a + b + carry.
|
|---|
| 57 | */
|
|---|
| 58 | static uint16_t inet_ocadd16(uint16_t a, uint16_t b)
|
|---|
| 59 | {
|
|---|
| 60 | uint32_t s;
|
|---|
| 61 |
|
|---|
| 62 | s = (uint32_t)a + (uint32_t)b;
|
|---|
| 63 | return (s & 0xffff) + (s >> 16);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | uint16_t inet_checksum_calc(uint16_t ivalue, void *data, size_t size)
|
|---|
| 67 | {
|
|---|
| 68 | uint16_t sum;
|
|---|
| 69 | uint16_t w;
|
|---|
| 70 | size_t words, i;
|
|---|
| 71 | uint8_t *bdata;
|
|---|
| 72 |
|
|---|
| 73 | sum = ~ivalue;
|
|---|
| 74 | words = size / 2;
|
|---|
| 75 | bdata = (uint8_t *)data;
|
|---|
| 76 |
|
|---|
| 77 | for (i = 0; i < words; i++) {
|
|---|
| 78 | w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
|
|---|
| 79 | sum = inet_ocadd16(sum, w);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if (size % 2 != 0) {
|
|---|
| 83 | w = ((uint16_t)bdata[2*words] << 8);
|
|---|
| 84 | sum = inet_ocadd16(sum, w);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | return ~sum;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /** Encode Internet PDU.
|
|---|
| 91 | *
|
|---|
| 92 | * Encode internet packet into PDU (serialized form). Will encode a
|
|---|
| 93 | * fragment of the payload starting at offset @a offs. The resulting
|
|---|
| 94 | * PDU will have at most @a mtu bytes. @a *roffs will be set to the offset
|
|---|
| 95 | * of remaining payload. If some data is remaining, the MF flag will
|
|---|
| 96 | * be set in the header, otherwise the offset will equal @a packet->size.
|
|---|
| 97 | *
|
|---|
| 98 | * @param packet Packet to encode
|
|---|
| 99 | * @param offs Offset into packet payload (in bytes)
|
|---|
| 100 | * @param mtu MTU (Maximum Transmission Unit) in bytes
|
|---|
| 101 | * @param rdata Place to store pointer to allocated data buffer
|
|---|
| 102 | * @param rsize Place to store size of allocated data buffer
|
|---|
| 103 | * @param roffs Place to store offset of remaning data
|
|---|
| 104 | */
|
|---|
| 105 | int inet_pdu_encode(inet_packet_t *packet, size_t offs, size_t mtu,
|
|---|
| 106 | void **rdata, size_t *rsize, size_t *roffs)
|
|---|
| 107 | {
|
|---|
| 108 | uint32_t src_addr;
|
|---|
| 109 | int rc = inet_addr_pack(&packet->src, &src_addr);
|
|---|
| 110 | if (rc != EOK)
|
|---|
| 111 | return rc;
|
|---|
| 112 |
|
|---|
| 113 | uint32_t dest_addr;
|
|---|
| 114 | rc = inet_addr_pack(&packet->dest, &dest_addr);
|
|---|
| 115 | if (rc != EOK)
|
|---|
| 116 | return rc;
|
|---|
| 117 |
|
|---|
| 118 | /* Upper bound for fragment offset field */
|
|---|
| 119 | size_t fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l);
|
|---|
| 120 |
|
|---|
| 121 | /* Verify that total size of datagram is within reasonable bounds */
|
|---|
| 122 | if (offs + packet->size > FRAG_OFFS_UNIT * fragoff_limit)
|
|---|
| 123 | return ELIMIT;
|
|---|
| 124 |
|
|---|
| 125 | size_t hdr_size = sizeof(ip_header_t);
|
|---|
| 126 | size_t data_offs = ROUND_UP(hdr_size, 4);
|
|---|
| 127 |
|
|---|
| 128 | assert(offs % FRAG_OFFS_UNIT == 0);
|
|---|
| 129 | assert(offs / FRAG_OFFS_UNIT < fragoff_limit);
|
|---|
| 130 |
|
|---|
| 131 | /* Value for the fragment offset field */
|
|---|
| 132 | uint16_t foff = offs / FRAG_OFFS_UNIT;
|
|---|
| 133 |
|
|---|
| 134 | if (hdr_size >= mtu)
|
|---|
| 135 | return EINVAL;
|
|---|
| 136 |
|
|---|
| 137 | /* Amount of space in the PDU available for payload */
|
|---|
| 138 | size_t spc_avail = mtu - hdr_size;
|
|---|
| 139 | spc_avail -= (spc_avail % FRAG_OFFS_UNIT);
|
|---|
| 140 |
|
|---|
| 141 | /* Amount of data (payload) to transfer */
|
|---|
| 142 | size_t xfer_size = min(packet->size - offs, spc_avail);
|
|---|
| 143 |
|
|---|
| 144 | /* Total PDU size */
|
|---|
| 145 | size_t size = hdr_size + xfer_size;
|
|---|
| 146 |
|
|---|
| 147 | /* Offset of remaining payload */
|
|---|
| 148 | size_t rem_offs = offs + xfer_size;
|
|---|
| 149 |
|
|---|
| 150 | /* Flags */
|
|---|
| 151 | uint16_t flags_foff =
|
|---|
| 152 | (packet->df ? BIT_V(uint16_t, FF_FLAG_DF) : 0) +
|
|---|
| 153 | (rem_offs < packet->size ? BIT_V(uint16_t, FF_FLAG_MF) : 0) +
|
|---|
| 154 | (foff << FF_FRAGOFF_l);
|
|---|
| 155 |
|
|---|
| 156 | void *data = calloc(size, 1);
|
|---|
| 157 | if (data == NULL)
|
|---|
| 158 | return ENOMEM;
|
|---|
| 159 |
|
|---|
| 160 | /* Allocate identifier */
|
|---|
| 161 | fibril_mutex_lock(&ip_ident_lock);
|
|---|
| 162 | uint16_t ident = ++ip_ident;
|
|---|
| 163 | fibril_mutex_unlock(&ip_ident_lock);
|
|---|
| 164 |
|
|---|
| 165 | /* Encode header fields */
|
|---|
| 166 | ip_header_t *hdr = (ip_header_t *) data;
|
|---|
| 167 |
|
|---|
| 168 | hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
|
|---|
| 169 | hdr->tos = packet->tos;
|
|---|
| 170 | hdr->tot_len = host2uint16_t_be(size);
|
|---|
| 171 | hdr->id = host2uint16_t_be(ident);
|
|---|
| 172 | hdr->flags_foff = host2uint16_t_be(flags_foff);
|
|---|
| 173 | hdr->ttl = packet->ttl;
|
|---|
| 174 | hdr->proto = packet->proto;
|
|---|
| 175 | hdr->chksum = 0;
|
|---|
| 176 | hdr->src_addr = host2uint32_t_be(src_addr);
|
|---|
| 177 | hdr->dest_addr = host2uint32_t_be(dest_addr);
|
|---|
| 178 |
|
|---|
| 179 | /* Compute checksum */
|
|---|
| 180 | uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *) hdr,
|
|---|
| 181 | hdr_size);
|
|---|
| 182 | hdr->chksum = host2uint16_t_be(chksum);
|
|---|
| 183 |
|
|---|
| 184 | /* Copy payload */
|
|---|
| 185 | memcpy((uint8_t *) data + data_offs, packet->data + offs, xfer_size);
|
|---|
| 186 |
|
|---|
| 187 | *rdata = data;
|
|---|
| 188 | *rsize = size;
|
|---|
| 189 | *roffs = rem_offs;
|
|---|
| 190 |
|
|---|
| 191 | return EOK;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | int inet_pdu_decode(void *data, size_t size, inet_packet_t *packet)
|
|---|
| 195 | {
|
|---|
| 196 | ip_header_t *hdr;
|
|---|
| 197 | size_t tot_len;
|
|---|
| 198 | size_t data_offs;
|
|---|
| 199 | uint8_t version;
|
|---|
| 200 | uint16_t ident;
|
|---|
| 201 | uint16_t flags_foff;
|
|---|
| 202 | uint16_t foff;
|
|---|
| 203 |
|
|---|
| 204 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode()");
|
|---|
| 205 |
|
|---|
| 206 | if (size < sizeof(ip_header_t)) {
|
|---|
| 207 | log_msg(LOG_DEFAULT, LVL_DEBUG, "PDU too short (%zu)", size);
|
|---|
| 208 | return EINVAL;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | hdr = (ip_header_t *)data;
|
|---|
| 212 |
|
|---|
| 213 | version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h, VI_VERSION_l,
|
|---|
| 214 | hdr->ver_ihl);
|
|---|
| 215 | if (version != 4) {
|
|---|
| 216 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Version (%d) != 4", version);
|
|---|
| 217 | return EINVAL;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | tot_len = uint16_t_be2host(hdr->tot_len);
|
|---|
| 221 | if (tot_len < sizeof(ip_header_t)) {
|
|---|
| 222 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Total Length too small (%zu)", tot_len);
|
|---|
| 223 | return EINVAL;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | if (tot_len > size) {
|
|---|
| 227 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Total Length = %zu > PDU size = %zu",
|
|---|
| 228 | tot_len, size);
|
|---|
| 229 | return EINVAL;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | ident = uint16_t_be2host(hdr->id);
|
|---|
| 233 | flags_foff = uint16_t_be2host(hdr->flags_foff);
|
|---|
| 234 | foff = BIT_RANGE_EXTRACT(uint16_t, FF_FRAGOFF_h, FF_FRAGOFF_l,
|
|---|
| 235 | flags_foff);
|
|---|
| 236 | /* XXX Checksum */
|
|---|
| 237 |
|
|---|
| 238 | inet_addr_unpack(uint32_t_be2host(hdr->src_addr), &packet->src);
|
|---|
| 239 | inet_addr_unpack(uint32_t_be2host(hdr->dest_addr), &packet->dest);
|
|---|
| 240 | packet->tos = hdr->tos;
|
|---|
| 241 | packet->proto = hdr->proto;
|
|---|
| 242 | packet->ttl = hdr->ttl;
|
|---|
| 243 | packet->ident = ident;
|
|---|
| 244 |
|
|---|
| 245 | packet->df = (flags_foff & BIT_V(uint16_t, FF_FLAG_DF)) != 0;
|
|---|
| 246 | packet->mf = (flags_foff & BIT_V(uint16_t, FF_FLAG_MF)) != 0;
|
|---|
| 247 | packet->offs = foff * FRAG_OFFS_UNIT;
|
|---|
| 248 |
|
|---|
| 249 | /* XXX IP options */
|
|---|
| 250 | data_offs = sizeof(uint32_t) * BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h,
|
|---|
| 251 | VI_IHL_l, hdr->ver_ihl);
|
|---|
| 252 |
|
|---|
| 253 | packet->size = tot_len - data_offs;
|
|---|
| 254 | packet->data = calloc(packet->size, 1);
|
|---|
| 255 | if (packet->data == NULL) {
|
|---|
| 256 | log_msg(LOG_DEFAULT, LVL_WARN, "Out of memory.");
|
|---|
| 257 | return ENOMEM;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | memcpy(packet->data, (uint8_t *) data + data_offs, packet->size);
|
|---|
| 261 |
|
|---|
| 262 | return EOK;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | /** @}
|
|---|
| 266 | */
|
|---|