[637a3b4] | 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>
|
---|
[02a09ed] | 42 | #include <net/socket_codes.h>
|
---|
[637a3b4] | 43 | #include "icmp.h"
|
---|
| 44 | #include "icmp_std.h"
|
---|
[b4ec1ea] | 45 | #include "inetsrv.h"
|
---|
[6428115] | 46 | #include "inetping.h"
|
---|
[637a3b4] | 47 | #include "pdu.h"
|
---|
| 48 |
|
---|
| 49 | /* XXX */
|
---|
| 50 | #define INET_TTL_MAX 255
|
---|
| 51 |
|
---|
[6428115] | 52 | static int icmp_recv_echo_request(inet_dgram_t *);
|
---|
| 53 | static int icmp_recv_echo_reply(inet_dgram_t *);
|
---|
[637a3b4] | 54 |
|
---|
| 55 | int icmp_recv(inet_dgram_t *dgram)
|
---|
| 56 | {
|
---|
| 57 | uint8_t type;
|
---|
| 58 |
|
---|
[a1a101d] | 59 | log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv()");
|
---|
[637a3b4] | 60 |
|
---|
| 61 | if (dgram->size < 1)
|
---|
| 62 | return EINVAL;
|
---|
| 63 |
|
---|
| 64 | type = *(uint8_t *)dgram->data;
|
---|
| 65 |
|
---|
| 66 | switch (type) {
|
---|
| 67 | case ICMP_ECHO_REQUEST:
|
---|
[6428115] | 68 | return icmp_recv_echo_request(dgram);
|
---|
| 69 | case ICMP_ECHO_REPLY:
|
---|
| 70 | return icmp_recv_echo_reply(dgram);
|
---|
[637a3b4] | 71 | default:
|
---|
| 72 | break;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return EINVAL;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[6428115] | 78 | static int icmp_recv_echo_request(inet_dgram_t *dgram)
|
---|
[637a3b4] | 79 | {
|
---|
| 80 | icmp_echo_t *request, *reply;
|
---|
| 81 | uint16_t checksum;
|
---|
| 82 | size_t size;
|
---|
| 83 | inet_dgram_t rdgram;
|
---|
| 84 | int rc;
|
---|
| 85 |
|
---|
[a1a101d] | 86 | log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_request()");
|
---|
[637a3b4] | 87 |
|
---|
| 88 | if (dgram->size < sizeof(icmp_echo_t))
|
---|
| 89 | return EINVAL;
|
---|
| 90 |
|
---|
| 91 | request = (icmp_echo_t *)dgram->data;
|
---|
| 92 | size = dgram->size;
|
---|
| 93 |
|
---|
| 94 | reply = calloc(size, 1);
|
---|
| 95 | if (reply == NULL)
|
---|
| 96 | return ENOMEM;
|
---|
| 97 |
|
---|
| 98 | memcpy(reply, request, size);
|
---|
| 99 |
|
---|
| 100 | reply->type = ICMP_ECHO_REPLY;
|
---|
| 101 | reply->code = 0;
|
---|
| 102 | reply->checksum = 0;
|
---|
| 103 |
|
---|
| 104 | checksum = inet_checksum_calc(INET_CHECKSUM_INIT, reply, size);
|
---|
| 105 | reply->checksum = host2uint16_t_be(checksum);
|
---|
| 106 |
|
---|
| 107 | rdgram.src = dgram->dest;
|
---|
| 108 | rdgram.dest = dgram->src;
|
---|
[6428115] | 109 | rdgram.tos = ICMP_TOS;
|
---|
[637a3b4] | 110 | rdgram.data = reply;
|
---|
| 111 | rdgram.size = size;
|
---|
| 112 |
|
---|
| 113 | rc = inet_route_packet(&rdgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
|
---|
| 114 |
|
---|
| 115 | free(reply);
|
---|
| 116 |
|
---|
| 117 | return rc;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[6428115] | 120 | static int icmp_recv_echo_reply(inet_dgram_t *dgram)
|
---|
| 121 | {
|
---|
[a1a101d] | 122 | log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_reply()");
|
---|
[a2e3ee6] | 123 |
|
---|
[6428115] | 124 | if (dgram->size < sizeof(icmp_echo_t))
|
---|
| 125 | return EINVAL;
|
---|
[a2e3ee6] | 126 |
|
---|
| 127 | icmp_echo_t *reply = (icmp_echo_t *) dgram->data;
|
---|
| 128 |
|
---|
| 129 | inetping_sdu_t sdu;
|
---|
| 130 |
|
---|
[02a09ed] | 131 | uint16_t family = inet_addr_get(&dgram->src, &sdu.src, NULL);
|
---|
| 132 | if (family != AF_INET)
|
---|
| 133 | return EINVAL;
|
---|
[a2e3ee6] | 134 |
|
---|
[02a09ed] | 135 | family = inet_addr_get(&dgram->dest, &sdu.dest, NULL);
|
---|
| 136 | if (family != AF_INET)
|
---|
| 137 | return EINVAL;
|
---|
[a2e3ee6] | 138 |
|
---|
[6428115] | 139 | sdu.seq_no = uint16_t_be2host(reply->seq_no);
|
---|
| 140 | sdu.data = reply + sizeof(icmp_echo_t);
|
---|
| 141 | sdu.size = dgram->size - sizeof(icmp_echo_t);
|
---|
[a2e3ee6] | 142 |
|
---|
| 143 | uint16_t ident = uint16_t_be2host(reply->ident);
|
---|
[6428115] | 144 |
|
---|
| 145 | return inetping_recv(ident, &sdu);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | int icmp_ping_send(uint16_t ident, inetping_sdu_t *sdu)
|
---|
| 149 | {
|
---|
[a2e3ee6] | 150 | size_t rsize = sizeof(icmp_echo_t) + sdu->size;
|
---|
| 151 | void *rdata = calloc(rsize, 1);
|
---|
[6428115] | 152 | if (rdata == NULL)
|
---|
| 153 | return ENOMEM;
|
---|
[a2e3ee6] | 154 |
|
---|
| 155 | icmp_echo_t *request = (icmp_echo_t *)rdata;
|
---|
| 156 |
|
---|
[6428115] | 157 | request->type = ICMP_ECHO_REQUEST;
|
---|
| 158 | request->code = 0;
|
---|
| 159 | request->checksum = 0;
|
---|
| 160 | request->ident = host2uint16_t_be(ident);
|
---|
| 161 | request->seq_no = host2uint16_t_be(sdu->seq_no);
|
---|
[a2e3ee6] | 162 |
|
---|
[6428115] | 163 | memcpy(rdata + sizeof(icmp_echo_t), sdu->data, sdu->size);
|
---|
[a2e3ee6] | 164 |
|
---|
| 165 | uint16_t checksum = inet_checksum_calc(INET_CHECKSUM_INIT, rdata, rsize);
|
---|
[6428115] | 166 | request->checksum = host2uint16_t_be(checksum);
|
---|
[a2e3ee6] | 167 |
|
---|
| 168 | inet_dgram_t dgram;
|
---|
| 169 |
|
---|
[02a09ed] | 170 | inet_addr_set(sdu->src, &dgram.src);
|
---|
| 171 | inet_addr_set(sdu->dest, &dgram.dest);
|
---|
[a2e3ee6] | 172 |
|
---|
[6428115] | 173 | dgram.tos = ICMP_TOS;
|
---|
| 174 | dgram.data = rdata;
|
---|
| 175 | dgram.size = rsize;
|
---|
[a2e3ee6] | 176 |
|
---|
| 177 | int rc = inet_route_packet(&dgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
|
---|
| 178 |
|
---|
[6428115] | 179 | free(rdata);
|
---|
| 180 | return rc;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[637a3b4] | 183 | /** @}
|
---|
| 184 | */
|
---|