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

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

more IPv6 stub code

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