source: mainline/uspace/srv/net/inetsrv/icmpv6.c@ eec201d

Last change on this file since eec201d was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 * Copyright (c) 2013 Antonin Steinhauser
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 "icmpv6.h"
44#include "icmpv6_std.h"
45#include "inetsrv.h"
46#include "inetping.h"
47#include "pdu.h"
48
49static errno_t icmpv6_recv_echo_request(inet_dgram_t *dgram)
50{
51 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv_echo_request()");
52
53 if (dgram->size < sizeof(icmpv6_message_t))
54 return EINVAL;
55
56 icmpv6_message_t *request = (icmpv6_message_t *) dgram->data;
57 size_t size = dgram->size;
58
59 addr128_t src_v6;
60 ip_ver_t src_ver = inet_addr_get(&dgram->src, NULL, &src_v6);
61
62 addr128_t dest_v6;
63 ip_ver_t dest_ver = inet_addr_get(&dgram->dest, NULL, &dest_v6);
64
65 if ((src_ver != dest_ver) || (src_ver != ip_v6))
66 return EINVAL;
67
68 icmpv6_message_t *reply = calloc(1, size);
69 if (reply == NULL)
70 return ENOMEM;
71
72 memcpy(reply, request, size);
73
74 reply->type = ICMPV6_ECHO_REPLY;
75 reply->code = 0;
76 reply->checksum = 0;
77
78 inet_dgram_t rdgram;
79
80 inet_get_srcaddr(&dgram->src, 0, &rdgram.src);
81 rdgram.dest = dgram->src;
82 rdgram.iplink = 0;
83 rdgram.tos = 0;
84 rdgram.data = reply;
85 rdgram.size = size;
86
87 icmpv6_phdr_t phdr;
88
89 host2addr128_t_be(dest_v6, phdr.src_addr);
90 host2addr128_t_be(src_v6, phdr.dest_addr);
91 phdr.length = host2uint32_t_be(dgram->size);
92 memset(phdr.zeroes, 0, 3);
93 phdr.next = IP_PROTO_ICMPV6;
94
95 uint16_t cs_phdr =
96 inet_checksum_calc(INET_CHECKSUM_INIT, &phdr,
97 sizeof(icmpv6_phdr_t));
98
99 uint16_t cs_all = inet_checksum_calc(cs_phdr, reply, size);
100
101 reply->checksum = host2uint16_t_be(cs_all);
102
103 errno_t rc = inet_route_packet(&rdgram, IP_PROTO_ICMPV6,
104 INET6_HOP_LIMIT_MAX, 0);
105
106 free(reply);
107
108 return rc;
109}
110
111static errno_t icmpv6_recv_echo_reply(inet_dgram_t *dgram)
112{
113 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv_echo_reply()");
114
115 if (dgram->size < sizeof(icmpv6_message_t))
116 return EINVAL;
117
118 inetping_sdu_t sdu;
119
120 sdu.src = dgram->src;
121 sdu.dest = dgram->dest;
122
123 icmpv6_message_t *reply = (icmpv6_message_t *) dgram->data;
124
125 sdu.seq_no = uint16_t_be2host(reply->un.echo.seq_no);
126 sdu.data = reply + sizeof(icmpv6_message_t);
127 sdu.size = dgram->size - sizeof(icmpv6_message_t);
128
129 uint16_t ident = uint16_t_be2host(reply->un.echo.ident);
130
131 return inetping_recv(ident, &sdu);
132}
133
134errno_t icmpv6_recv(inet_dgram_t *dgram)
135{
136 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv()");
137
138 if (dgram->size < 1)
139 return EINVAL;
140
141 uint8_t type = *(uint8_t *) dgram->data;
142
143 switch (type) {
144 case ICMPV6_ECHO_REQUEST:
145 return icmpv6_recv_echo_request(dgram);
146 case ICMPV6_ECHO_REPLY:
147 return icmpv6_recv_echo_reply(dgram);
148 case ICMPV6_NEIGHBOUR_SOLICITATION:
149 case ICMPV6_NEIGHBOUR_ADVERTISEMENT:
150 case ICMPV6_ROUTER_ADVERTISEMENT:
151 return ndp_received(dgram);
152 default:
153 break;
154 }
155
156 return EINVAL;
157}
158
159errno_t icmpv6_ping_send(uint16_t ident, inetping_sdu_t *sdu)
160{
161 size_t rsize = sizeof(icmpv6_message_t) + sdu->size;
162 void *rdata = calloc(1, rsize);
163 if (rdata == NULL)
164 return ENOMEM;
165
166 icmpv6_message_t *request = (icmpv6_message_t *) rdata;
167
168 request->type = ICMPV6_ECHO_REQUEST;
169 request->code = 0;
170 request->checksum = 0;
171 request->un.echo.ident = host2uint16_t_be(ident);
172 request->un.echo.seq_no = host2uint16_t_be(sdu->seq_no);
173
174 memcpy(rdata + sizeof(icmpv6_message_t), sdu->data, sdu->size);
175
176 inet_dgram_t dgram;
177
178 dgram.src = sdu->src;
179 dgram.dest = sdu->dest;
180 dgram.iplink = 0;
181 dgram.tos = 0;
182 dgram.data = rdata;
183 dgram.size = rsize;
184
185 icmpv6_phdr_t phdr;
186
187 assert(sdu->src.version == ip_v6);
188 assert(sdu->dest.version == ip_v6);
189
190 host2addr128_t_be(sdu->src.addr6, phdr.src_addr);
191 host2addr128_t_be(sdu->dest.addr6, phdr.dest_addr);
192 phdr.length = host2uint32_t_be(dgram.size);
193 memset(phdr.zeroes, 0, 3);
194 phdr.next = IP_PROTO_ICMPV6;
195
196 uint16_t cs_phdr =
197 inet_checksum_calc(INET_CHECKSUM_INIT, &phdr,
198 sizeof(icmpv6_phdr_t));
199
200 uint16_t cs_all = inet_checksum_calc(cs_phdr, rdata, rsize);
201
202 request->checksum = host2uint16_t_be(cs_all);
203
204 errno_t rc = inet_route_packet(&dgram, IP_PROTO_ICMPV6,
205 INET6_HOP_LIMIT_MAX, 0);
206
207 free(rdata);
208
209 return rc;
210}
211
212/** @}
213 */
Note: See TracBrowser for help on using the repository browser.