| 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 "icmp.h"
|
|---|
| 44 | #include "icmp_std.h"
|
|---|
| 45 | #include "inet.h"
|
|---|
| 46 | #include "pdu.h"
|
|---|
| 47 |
|
|---|
| 48 | /* XXX */
|
|---|
| 49 | #define INET_TTL_MAX 255
|
|---|
| 50 |
|
|---|
| 51 | static int icmp_echo_request(inet_dgram_t *);
|
|---|
| 52 |
|
|---|
| 53 | int icmp_recv(inet_dgram_t *dgram)
|
|---|
| 54 | {
|
|---|
| 55 | uint8_t type;
|
|---|
| 56 |
|
|---|
| 57 | log_msg(LVL_DEBUG, "icmp_recv()");
|
|---|
| 58 |
|
|---|
| 59 | if (dgram->size < 1)
|
|---|
| 60 | return EINVAL;
|
|---|
| 61 |
|
|---|
| 62 | type = *(uint8_t *)dgram->data;
|
|---|
| 63 |
|
|---|
| 64 | switch (type) {
|
|---|
| 65 | case ICMP_ECHO_REQUEST:
|
|---|
| 66 | return icmp_echo_request(dgram);
|
|---|
| 67 | default:
|
|---|
| 68 | break;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return EINVAL;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | static int icmp_echo_request(inet_dgram_t *dgram)
|
|---|
| 75 | {
|
|---|
| 76 | icmp_echo_t *request, *reply;
|
|---|
| 77 | uint16_t checksum;
|
|---|
| 78 | size_t size;
|
|---|
| 79 | inet_dgram_t rdgram;
|
|---|
| 80 | int rc;
|
|---|
| 81 |
|
|---|
| 82 | log_msg(LVL_DEBUG, "icmp_echo_request()");
|
|---|
| 83 |
|
|---|
| 84 | if (dgram->size < sizeof(icmp_echo_t))
|
|---|
| 85 | return EINVAL;
|
|---|
| 86 |
|
|---|
| 87 | request = (icmp_echo_t *)dgram->data;
|
|---|
| 88 | size = dgram->size;
|
|---|
| 89 |
|
|---|
| 90 | reply = calloc(size, 1);
|
|---|
| 91 | if (reply == NULL)
|
|---|
| 92 | return ENOMEM;
|
|---|
| 93 |
|
|---|
| 94 | memcpy(reply, request, size);
|
|---|
| 95 |
|
|---|
| 96 | reply->type = ICMP_ECHO_REPLY;
|
|---|
| 97 | reply->code = 0;
|
|---|
| 98 | reply->checksum = 0;
|
|---|
| 99 |
|
|---|
| 100 | checksum = inet_checksum_calc(INET_CHECKSUM_INIT, reply, size);
|
|---|
| 101 | reply->checksum = host2uint16_t_be(checksum);
|
|---|
| 102 |
|
|---|
| 103 | rdgram.src = dgram->dest;
|
|---|
| 104 | rdgram.dest = dgram->src;
|
|---|
| 105 | rdgram.tos = 0;
|
|---|
| 106 | rdgram.data = reply;
|
|---|
| 107 | rdgram.size = size;
|
|---|
| 108 |
|
|---|
| 109 | rc = inet_route_packet(&rdgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
|
|---|
| 110 |
|
|---|
| 111 | free(reply);
|
|---|
| 112 |
|
|---|
| 113 | return rc;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** @}
|
|---|
| 117 | */
|
|---|