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 <types/inetping6.h>
|
---|
44 | #include "icmpv6.h"
|
---|
45 | #include "icmpv6_std.h"
|
---|
46 | #include "inetsrv.h"
|
---|
47 | #include "inetping6.h"
|
---|
48 | #include "pdu.h"
|
---|
49 |
|
---|
50 | static int icmpv6_recv_echo_request(inet_dgram_t *dgram)
|
---|
51 | {
|
---|
52 | log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv_echo_request()");
|
---|
53 |
|
---|
54 | if (dgram->size < sizeof(icmpv6_message_t))
|
---|
55 | return EINVAL;
|
---|
56 |
|
---|
57 | icmpv6_message_t *request = (icmpv6_message_t *) dgram->data;
|
---|
58 | size_t size = dgram->size;
|
---|
59 |
|
---|
60 | addr128_t src_v6;
|
---|
61 | uint16_t src_af = inet_addr_get(&dgram->src, NULL, &src_v6);
|
---|
62 |
|
---|
63 | addr128_t dest_v6;
|
---|
64 | uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &dest_v6);
|
---|
65 |
|
---|
66 | if ((src_af != dest_af) || (src_af != AF_INET6))
|
---|
67 | return EINVAL;
|
---|
68 |
|
---|
69 | icmpv6_message_t *reply = calloc(1, size);
|
---|
70 | if (reply == NULL)
|
---|
71 | return ENOMEM;
|
---|
72 |
|
---|
73 | memcpy(reply, request, size);
|
---|
74 |
|
---|
75 | reply->type = ICMPV6_ECHO_REPLY;
|
---|
76 | reply->code = 0;
|
---|
77 | reply->checksum = 0;
|
---|
78 |
|
---|
79 | inet_dgram_t rdgram;
|
---|
80 |
|
---|
81 | inet_get_srcaddr(&dgram->src, 0, &rdgram.src);
|
---|
82 | rdgram.dest = dgram->src;
|
---|
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 | int rc = inet_route_packet(&rdgram, IP_PROTO_ICMPV6,
|
---|
104 | INET6_HOP_LIMIT_MAX, 0);
|
---|
105 |
|
---|
106 | free(reply);
|
---|
107 |
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | static int 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 | inetping6_sdu_t sdu;
|
---|
119 |
|
---|
120 | uint16_t src_af = inet_addr_get(&dgram->src, NULL, &sdu.src);
|
---|
121 | uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &sdu.dest);
|
---|
122 |
|
---|
123 | if ((src_af != dest_af) || (src_af != AF_INET6))
|
---|
124 | return EINVAL;
|
---|
125 |
|
---|
126 | icmpv6_message_t *reply = (icmpv6_message_t *) dgram->data;
|
---|
127 |
|
---|
128 | sdu.seq_no = uint16_t_be2host(reply->un.echo.seq_no);
|
---|
129 | sdu.data = reply + sizeof(icmpv6_message_t);
|
---|
130 | sdu.size = dgram->size - sizeof(icmpv6_message_t);
|
---|
131 |
|
---|
132 | uint16_t ident = uint16_t_be2host(reply->un.echo.ident);
|
---|
133 |
|
---|
134 | return inetping6_recv(ident, &sdu);
|
---|
135 | }
|
---|
136 |
|
---|
137 | int icmpv6_recv(inet_dgram_t *dgram)
|
---|
138 | {
|
---|
139 | log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv()");
|
---|
140 |
|
---|
141 | if (dgram->size < 1)
|
---|
142 | return EINVAL;
|
---|
143 |
|
---|
144 | uint8_t type = *(uint8_t *) dgram->data;
|
---|
145 |
|
---|
146 | switch (type) {
|
---|
147 | case ICMPV6_ECHO_REQUEST:
|
---|
148 | return icmpv6_recv_echo_request(dgram);
|
---|
149 | case ICMPV6_ECHO_REPLY:
|
---|
150 | return icmpv6_recv_echo_reply(dgram);
|
---|
151 | case ICMPV6_NEIGHBOUR_SOLICITATION:
|
---|
152 | case ICMPV6_NEIGHBOUR_ADVERTISEMENT:
|
---|
153 | case ICMPV6_ROUTER_ADVERTISEMENT:
|
---|
154 | return ndp_received(dgram);
|
---|
155 | default:
|
---|
156 | break;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return EINVAL;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int icmpv6_ping_send(uint16_t ident, inetping6_sdu_t *sdu)
|
---|
163 | {
|
---|
164 | size_t rsize = sizeof(icmpv6_message_t) + sdu->size;
|
---|
165 | void *rdata = calloc(1, rsize);
|
---|
166 | if (rdata == NULL)
|
---|
167 | return ENOMEM;
|
---|
168 |
|
---|
169 | icmpv6_message_t *request = (icmpv6_message_t *) rdata;
|
---|
170 |
|
---|
171 | request->type = ICMPV6_ECHO_REQUEST;
|
---|
172 | request->code = 0;
|
---|
173 | request->checksum = 0;
|
---|
174 | request->un.echo.ident = host2uint16_t_be(ident);
|
---|
175 | request->un.echo.seq_no = host2uint16_t_be(sdu->seq_no);
|
---|
176 |
|
---|
177 | memcpy(rdata + sizeof(icmpv6_message_t), sdu->data, sdu->size);
|
---|
178 |
|
---|
179 | inet_dgram_t dgram;
|
---|
180 |
|
---|
181 | inet_addr_set6(sdu->src, &dgram.src);
|
---|
182 | inet_addr_set6(sdu->dest, &dgram.dest);
|
---|
183 | dgram.tos = 0;
|
---|
184 | dgram.data = rdata;
|
---|
185 | dgram.size = rsize;
|
---|
186 |
|
---|
187 | icmpv6_phdr_t phdr;
|
---|
188 |
|
---|
189 | host2addr128_t_be(sdu->src, phdr.src_addr);
|
---|
190 | host2addr128_t_be(sdu->dest, phdr.dest_addr);
|
---|
191 | phdr.length = host2uint32_t_be(dgram.size);
|
---|
192 | memset(phdr.zeroes, 0, 3);
|
---|
193 | phdr.next = IP_PROTO_ICMPV6;
|
---|
194 |
|
---|
195 | uint16_t cs_phdr =
|
---|
196 | inet_checksum_calc(INET_CHECKSUM_INIT, &phdr,
|
---|
197 | sizeof(icmpv6_phdr_t));
|
---|
198 |
|
---|
199 | uint16_t cs_all = inet_checksum_calc(cs_phdr, rdata, rsize);
|
---|
200 |
|
---|
201 | request->checksum = host2uint16_t_be(cs_all);
|
---|
202 |
|
---|
203 | int rc = inet_route_packet(&dgram, IP_PROTO_ICMPV6,
|
---|
204 | INET6_HOP_LIMIT_MAX, 0);
|
---|
205 |
|
---|
206 | free(rdata);
|
---|
207 |
|
---|
208 | return rc;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** @}
|
---|
212 | */
|
---|