source: mainline/uspace/srv/net/inetsrv/icmp.c@ 758c79d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 758c79d was f0bc6f6, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Fix pointer computation for ping reply

Under the right circumstances (such as amd64+shared libs) the pointer
which was offset a bit too far would run into an unmapped page and
cause async_data_write_start() to return EPERM in inetsrv and the
client would never receive the answer.

  • Property mode set to 100644
File size: 4.4 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 <types/inetping.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 errno_t icmp_recv_echo_request(inet_dgram_t *);
53static errno_t icmp_recv_echo_reply(inet_dgram_t *);
54
55errno_t 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 errno_t 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 errno_t 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.iplink = 0;
108 rdgram.src = dgram->dest;
109 rdgram.dest = dgram->src;
110 rdgram.tos = ICMP_TOS;
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
121static errno_t icmp_recv_echo_reply(inet_dgram_t *dgram)
122{
123 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_reply()");
124
125 if (dgram->size < sizeof(icmp_echo_t))
126 return EINVAL;
127
128 icmp_echo_t *reply = (icmp_echo_t *) dgram->data;
129
130 inetping_sdu_t sdu;
131
132 sdu.src = dgram->src;
133 sdu.dest = dgram->dest;
134 sdu.seq_no = uint16_t_be2host(reply->seq_no);
135 sdu.data = dgram->data + sizeof(icmp_echo_t);
136 sdu.size = dgram->size - sizeof(icmp_echo_t);
137
138 uint16_t ident = uint16_t_be2host(reply->ident);
139
140 return inetping_recv(ident, &sdu);
141}
142
143errno_t icmp_ping_send(uint16_t ident, inetping_sdu_t *sdu)
144{
145 size_t rsize = sizeof(icmp_echo_t) + sdu->size;
146 void *rdata = calloc(rsize, 1);
147 if (rdata == NULL)
148 return ENOMEM;
149
150 icmp_echo_t *request = (icmp_echo_t *) rdata;
151
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);
157
158 memcpy(rdata + sizeof(icmp_echo_t), sdu->data, sdu->size);
159
160 uint16_t checksum = inet_checksum_calc(INET_CHECKSUM_INIT, rdata, rsize);
161 request->checksum = host2uint16_t_be(checksum);
162
163 inet_dgram_t dgram;
164
165 dgram.src = sdu->src;
166 dgram.dest = sdu->dest;
167 dgram.iplink = 0;
168 dgram.tos = ICMP_TOS;
169 dgram.data = rdata;
170 dgram.size = rsize;
171
172 errno_t rc = inet_route_packet(&dgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
173
174 free(rdata);
175 return rc;
176}
177
178/** @}
179 */
Note: See TracBrowser for help on using the repository browser.