| 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 <byteorder.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <io/log.h>
|
|---|
| 40 | #include <mem.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "ethip.h"
|
|---|
| 44 | #include "std.h"
|
|---|
| 45 | #include "pdu.h"
|
|---|
| 46 |
|
|---|
| 47 | static void mac48_encode(mac48_addr_t *addr, void *buf);
|
|---|
| 48 | static void mac48_decode(void *data, mac48_addr_t *addr);
|
|---|
| 49 |
|
|---|
| 50 | #define MAC48_BYTES 6
|
|---|
| 51 |
|
|---|
| 52 | /** Encode Ethernet PDU. */
|
|---|
| 53 | int eth_pdu_encode(eth_frame_t *frame, void **rdata, size_t *rsize)
|
|---|
| 54 | {
|
|---|
| 55 | void *data;
|
|---|
| 56 | size_t size;
|
|---|
| 57 | eth_header_t *hdr;
|
|---|
| 58 |
|
|---|
| 59 | size = sizeof(eth_header_t) + frame->size;
|
|---|
| 60 |
|
|---|
| 61 | data = calloc(size, 1);
|
|---|
| 62 | if (data == NULL)
|
|---|
| 63 | return ENOMEM;
|
|---|
| 64 |
|
|---|
| 65 | hdr = (eth_header_t *)data;
|
|---|
| 66 | mac48_encode(&frame->src, hdr->src);
|
|---|
| 67 | mac48_encode(&frame->dest, hdr->dest);
|
|---|
| 68 | hdr->etype_len = host2uint16_t_be(frame->etype_len);
|
|---|
| 69 |
|
|---|
| 70 | memcpy((uint8_t *)data + sizeof(eth_header_t), frame->data,
|
|---|
| 71 | frame->size);
|
|---|
| 72 |
|
|---|
| 73 | *rdata = data;
|
|---|
| 74 | *rsize = size;
|
|---|
| 75 | return EOK;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | #include <stdio.h>
|
|---|
| 79 | /** Decode Ethernet PDU. */
|
|---|
| 80 | int eth_pdu_decode(void *data, size_t size, eth_frame_t *frame)
|
|---|
| 81 | {
|
|---|
| 82 | eth_header_t *hdr;
|
|---|
| 83 |
|
|---|
| 84 | log_msg(LVL_DEBUG, "eth_pdu_decode()");
|
|---|
| 85 |
|
|---|
| 86 | if (size < sizeof(eth_header_t)) {
|
|---|
| 87 | log_msg(LVL_DEBUG, "PDU too short (%zu)", size);
|
|---|
| 88 | return EINVAL;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | hdr = (eth_header_t *)data;
|
|---|
| 92 |
|
|---|
| 93 | frame->size = size - sizeof(eth_header_t);
|
|---|
| 94 | frame->data = calloc(frame->size, 1);
|
|---|
| 95 | if (frame->data == NULL)
|
|---|
| 96 | return ENOMEM;
|
|---|
| 97 |
|
|---|
| 98 | mac48_decode(hdr->src, &frame->src);
|
|---|
| 99 | mac48_decode(hdr->dest, &frame->dest);
|
|---|
| 100 | frame->etype_len = uint16_t_be2host(hdr->etype_len);
|
|---|
| 101 |
|
|---|
| 102 | memcpy(frame->data, (uint8_t *)data + sizeof(eth_header_t),
|
|---|
| 103 | frame->size);
|
|---|
| 104 |
|
|---|
| 105 | log_msg(LVL_DEBUG, "Ethernet frame src=%llx dest=%llx etype=%x",
|
|---|
| 106 | frame->src, frame->dest, frame->etype_len);
|
|---|
| 107 | log_msg(LVL_DEBUG, "Ethernet frame payload (%zu bytes)", frame->size);
|
|---|
| 108 | size_t i;
|
|---|
| 109 | for (i = 0; i < frame->size; i++) {
|
|---|
| 110 | printf("%02x ", ((uint8_t *)(frame->data))[i]);
|
|---|
| 111 | }
|
|---|
| 112 | printf("\n");
|
|---|
| 113 |
|
|---|
| 114 | return EOK;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | static void mac48_encode(mac48_addr_t *addr, void *buf)
|
|---|
| 118 | {
|
|---|
| 119 | uint64_t val;
|
|---|
| 120 | uint8_t *bbuf = (uint8_t *)buf;
|
|---|
| 121 | int i;
|
|---|
| 122 |
|
|---|
| 123 | val = addr->addr;
|
|---|
| 124 | for (i = 0; i < MAC48_BYTES; i++) {
|
|---|
| 125 | bbuf[i] = (val >> (8 * (MAC48_BYTES - i - 1))) & 0xff;
|
|---|
| 126 | val = val >> 8;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static void mac48_decode(void *data, mac48_addr_t *addr)
|
|---|
| 131 | {
|
|---|
| 132 | uint64_t val;
|
|---|
| 133 | uint8_t *bdata = (uint8_t *)data;
|
|---|
| 134 | int i;
|
|---|
| 135 |
|
|---|
| 136 | val = 0;
|
|---|
| 137 | for (i = 0; i < MAC48_BYTES; i++)
|
|---|
| 138 | val |= (uint64_t)bdata[i] << (8 * (MAC48_BYTES - i - 1));
|
|---|
| 139 |
|
|---|
| 140 | addr->addr = val;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** @}
|
|---|
| 144 | */
|
|---|