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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3fafe5e0 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 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
RevLine 
[1d24ad3]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>
[9749e47]42#include <types/inetping.h>
[1d24ad3]43#include "icmpv6.h"
44#include "icmpv6_std.h"
45#include "inetsrv.h"
[9749e47]46#include "inetping.h"
[1d24ad3]47#include "pdu.h"
48
[b7fd2a0]49static errno_t icmpv6_recv_echo_request(inet_dgram_t *dgram)
[1d24ad3]50{
51 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv_echo_request()");
[a35b458]52
[1d24ad3]53 if (dgram->size < sizeof(icmpv6_message_t))
54 return EINVAL;
[a35b458]55
[1d24ad3]56 icmpv6_message_t *request = (icmpv6_message_t *) dgram->data;
57 size_t size = dgram->size;
[a35b458]58
[1d24ad3]59 addr128_t src_v6;
[f023251]60 ip_ver_t src_ver = inet_addr_get(&dgram->src, NULL, &src_v6);
[a35b458]61
[1d24ad3]62 addr128_t dest_v6;
[f023251]63 ip_ver_t dest_ver = inet_addr_get(&dgram->dest, NULL, &dest_v6);
[a35b458]64
[f023251]65 if ((src_ver != dest_ver) || (src_ver != ip_v6))
[1d24ad3]66 return EINVAL;
[a35b458]67
[1d24ad3]68 icmpv6_message_t *reply = calloc(1, size);
69 if (reply == NULL)
70 return ENOMEM;
[a35b458]71
[1d24ad3]72 memcpy(reply, request, size);
[a35b458]73
[1d24ad3]74 reply->type = ICMPV6_ECHO_REPLY;
75 reply->code = 0;
76 reply->checksum = 0;
[a35b458]77
[1d24ad3]78 inet_dgram_t rdgram;
[a35b458]79
[1d94e21]80 inet_get_srcaddr(&dgram->src, 0, &rdgram.src);
[1d24ad3]81 rdgram.dest = dgram->src;
[159ad6d]82 rdgram.iplink = 0;
[1d24ad3]83 rdgram.tos = 0;
84 rdgram.data = reply;
85 rdgram.size = size;
[a35b458]86
[12df1f1]87 icmpv6_phdr_t phdr;
[a35b458]88
[1d24ad3]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;
[a35b458]94
[1d24ad3]95 uint16_t cs_phdr =
96 inet_checksum_calc(INET_CHECKSUM_INIT, &phdr,
[12df1f1]97 sizeof(icmpv6_phdr_t));
[a35b458]98
[1d24ad3]99 uint16_t cs_all = inet_checksum_calc(cs_phdr, reply, size);
[a35b458]100
[1d24ad3]101 reply->checksum = host2uint16_t_be(cs_all);
[a35b458]102
[b7fd2a0]103 errno_t rc = inet_route_packet(&rdgram, IP_PROTO_ICMPV6,
[1d24ad3]104 INET6_HOP_LIMIT_MAX, 0);
[a35b458]105
[1d24ad3]106 free(reply);
[a35b458]107
[1d24ad3]108 return rc;
109}
110
[b7fd2a0]111static errno_t icmpv6_recv_echo_reply(inet_dgram_t *dgram)
[1d24ad3]112{
113 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv_echo_reply()");
[a35b458]114
[1d24ad3]115 if (dgram->size < sizeof(icmpv6_message_t))
116 return EINVAL;
[a35b458]117
[9749e47]118 inetping_sdu_t sdu;
[a35b458]119
[9749e47]120 sdu.src = dgram->src;
121 sdu.dest = dgram->dest;
[a35b458]122
[1d24ad3]123 icmpv6_message_t *reply = (icmpv6_message_t *) dgram->data;
[a35b458]124
[1d24ad3]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);
[a35b458]128
[1d24ad3]129 uint16_t ident = uint16_t_be2host(reply->un.echo.ident);
[a35b458]130
[9749e47]131 return inetping_recv(ident, &sdu);
[1d24ad3]132}
133
[b7fd2a0]134errno_t icmpv6_recv(inet_dgram_t *dgram)
[1d24ad3]135{
136 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv()");
[a35b458]137
[1d24ad3]138 if (dgram->size < 1)
139 return EINVAL;
[a35b458]140
[1d24ad3]141 uint8_t type = *(uint8_t *) dgram->data;
[a35b458]142
[1d24ad3]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 }
[a35b458]155
[1d24ad3]156 return EINVAL;
157}
158
[b7fd2a0]159errno_t icmpv6_ping_send(uint16_t ident, inetping_sdu_t *sdu)
[1d24ad3]160{
161 size_t rsize = sizeof(icmpv6_message_t) + sdu->size;
162 void *rdata = calloc(1, rsize);
163 if (rdata == NULL)
164 return ENOMEM;
[a35b458]165
[1d24ad3]166 icmpv6_message_t *request = (icmpv6_message_t *) rdata;
[a35b458]167
[1d24ad3]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);
[a35b458]173
[1d24ad3]174 memcpy(rdata + sizeof(icmpv6_message_t), sdu->data, sdu->size);
[a35b458]175
[1d24ad3]176 inet_dgram_t dgram;
[a35b458]177
[9749e47]178 dgram.src = sdu->src;
179 dgram.dest = sdu->dest;
[159ad6d]180 dgram.iplink = 0;
[1d24ad3]181 dgram.tos = 0;
182 dgram.data = rdata;
183 dgram.size = rsize;
[a35b458]184
[12df1f1]185 icmpv6_phdr_t phdr;
[a35b458]186
[9749e47]187 assert(sdu->src.version == ip_v6);
188 assert(sdu->dest.version == ip_v6);
[a35b458]189
[9749e47]190 host2addr128_t_be(sdu->src.addr6, phdr.src_addr);
191 host2addr128_t_be(sdu->dest.addr6, phdr.dest_addr);
[1d24ad3]192 phdr.length = host2uint32_t_be(dgram.size);
193 memset(phdr.zeroes, 0, 3);
194 phdr.next = IP_PROTO_ICMPV6;
[a35b458]195
[1d24ad3]196 uint16_t cs_phdr =
197 inet_checksum_calc(INET_CHECKSUM_INIT, &phdr,
[12df1f1]198 sizeof(icmpv6_phdr_t));
[a35b458]199
[1d24ad3]200 uint16_t cs_all = inet_checksum_calc(cs_phdr, rdata, rsize);
[a35b458]201
[1d24ad3]202 request->checksum = host2uint16_t_be(cs_all);
[a35b458]203
[b7fd2a0]204 errno_t rc = inet_route_packet(&dgram, IP_PROTO_ICMPV6,
[1d24ad3]205 INET6_HOP_LIMIT_MAX, 0);
[a35b458]206
[1d24ad3]207 free(rdata);
[a35b458]208
[1d24ad3]209 return rc;
210}
211
212/** @}
213 */
Note: See TracBrowser for help on using the repository browser.