source: mainline/uspace/srv/net/inetsrv/icmp.c@ 02a09ed

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02a09ed was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 4.5 KB
Line 
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#include <net/socket_codes.h>
43#include "icmp.h"
44#include "icmp_std.h"
45#include "inetsrv.h"
46#include "inetping.h"
47#include "pdu.h"
48
49/* XXX */
50#define INET_TTL_MAX 255
51
52static int icmp_recv_echo_request(inet_dgram_t *);
53static int icmp_recv_echo_reply(inet_dgram_t *);
54
55int icmp_recv(inet_dgram_t *dgram)
56{
57 uint8_t type;
58
59 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv()");
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:
68 return icmp_recv_echo_request(dgram);
69 case ICMP_ECHO_REPLY:
70 return icmp_recv_echo_reply(dgram);
71 default:
72 break;
73 }
74
75 return EINVAL;
76}
77
78static int icmp_recv_echo_request(inet_dgram_t *dgram)
79{
80 icmp_echo_t *request, *reply;
81 uint16_t checksum;
82 size_t size;
83 inet_dgram_t rdgram;
84 int rc;
85
86 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_request()");
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;
109 rdgram.tos = ICMP_TOS;
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
120static int icmp_recv_echo_reply(inet_dgram_t *dgram)
121{
122 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_reply()");
123
124 if (dgram->size < sizeof(icmp_echo_t))
125 return EINVAL;
126
127 icmp_echo_t *reply = (icmp_echo_t *) dgram->data;
128
129 inetping_sdu_t sdu;
130
131 uint16_t family = inet_addr_get(&dgram->src, &sdu.src, NULL);
132 if (family != AF_INET)
133 return EINVAL;
134
135 family = inet_addr_get(&dgram->dest, &sdu.dest, NULL);
136 if (family != AF_INET)
137 return EINVAL;
138
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);
142
143 uint16_t ident = uint16_t_be2host(reply->ident);
144
145 return inetping_recv(ident, &sdu);
146}
147
148int icmp_ping_send(uint16_t ident, inetping_sdu_t *sdu)
149{
150 size_t rsize = sizeof(icmp_echo_t) + sdu->size;
151 void *rdata = calloc(rsize, 1);
152 if (rdata == NULL)
153 return ENOMEM;
154
155 icmp_echo_t *request = (icmp_echo_t *)rdata;
156
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);
162
163 memcpy(rdata + sizeof(icmp_echo_t), sdu->data, sdu->size);
164
165 uint16_t checksum = inet_checksum_calc(INET_CHECKSUM_INIT, rdata, rsize);
166 request->checksum = host2uint16_t_be(checksum);
167
168 inet_dgram_t dgram;
169
170 inet_addr_set(sdu->src, &dgram.src);
171 inet_addr_set(sdu->dest, &dgram.dest);
172
173 dgram.tos = ICMP_TOS;
174 dgram.data = rdata;
175 dgram.size = rsize;
176
177 int rc = inet_route_packet(&dgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
178
179 free(rdata);
180 return rc;
181}
182
183/** @}
184 */
Note: See TracBrowser for help on using the repository browser.