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