source: mainline/uspace/srv/net/inetsrv/icmp.c@ 8d48c7e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8d48c7e was 9749e47, checked in by Jiri Svoboda <jiri@…>, 12 years ago

ping6 - resistance is futile.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[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>
[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]52static int icmp_recv_echo_request(inet_dgram_t *);
53static int icmp_recv_echo_reply(inet_dgram_t *);
[637a3b4]54
55int 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]78static 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
[695b6ff]107 rdgram.iplink = 0;
[637a3b4]108 rdgram.src = dgram->dest;
109 rdgram.dest = dgram->src;
[6428115]110 rdgram.tos = ICMP_TOS;
[637a3b4]111 rdgram.data = reply;
112 rdgram.size = size;
113
114 rc = inet_route_packet(&rdgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
115
116 free(reply);
117
118 return rc;
119}
120
[6428115]121static int icmp_recv_echo_reply(inet_dgram_t *dgram)
122{
[a1a101d]123 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_reply()");
[f023251]124
[6428115]125 if (dgram->size < sizeof(icmp_echo_t))
126 return EINVAL;
[f023251]127
[a2e3ee6]128 icmp_echo_t *reply = (icmp_echo_t *) dgram->data;
[f023251]129
[a2e3ee6]130 inetping_sdu_t sdu;
[f023251]131
[9749e47]132 sdu.src = dgram->src;
133 sdu.dest = dgram->dest;
[6428115]134 sdu.seq_no = uint16_t_be2host(reply->seq_no);
135 sdu.data = reply + sizeof(icmp_echo_t);
136 sdu.size = dgram->size - sizeof(icmp_echo_t);
[f023251]137
[a2e3ee6]138 uint16_t ident = uint16_t_be2host(reply->ident);
[6428115]139
140 return inetping_recv(ident, &sdu);
141}
142
143int icmp_ping_send(uint16_t ident, inetping_sdu_t *sdu)
144{
[a2e3ee6]145 size_t rsize = sizeof(icmp_echo_t) + sdu->size;
146 void *rdata = calloc(rsize, 1);
[6428115]147 if (rdata == NULL)
148 return ENOMEM;
[f023251]149
[c0f3460]150 icmp_echo_t *request = (icmp_echo_t *) rdata;
[f023251]151
[6428115]152 request->type = ICMP_ECHO_REQUEST;
153 request->code = 0;
154 request->checksum = 0;
155 request->ident = host2uint16_t_be(ident);
156 request->seq_no = host2uint16_t_be(sdu->seq_no);
[f023251]157
[6428115]158 memcpy(rdata + sizeof(icmp_echo_t), sdu->data, sdu->size);
[f023251]159
[a2e3ee6]160 uint16_t checksum = inet_checksum_calc(INET_CHECKSUM_INIT, rdata, rsize);
[6428115]161 request->checksum = host2uint16_t_be(checksum);
[f023251]162
[a2e3ee6]163 inet_dgram_t dgram;
[f023251]164
[9749e47]165 dgram.src = sdu->src;
166 dgram.dest = sdu->dest;
[695b6ff]167 dgram.iplink = 0;
[6428115]168 dgram.tos = ICMP_TOS;
169 dgram.data = rdata;
170 dgram.size = rsize;
[f023251]171
[a2e3ee6]172 int rc = inet_route_packet(&dgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
[f023251]173
[6428115]174 free(rdata);
175 return rc;
176}
177
[637a3b4]178/** @}
179 */
Note: See TracBrowser for help on using the repository browser.