[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>
|
---|
| 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 |
|
---|
| 49 | static int 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 | uint16_t src_af = inet_addr_get(&dgram->src, NULL, &src_v6);
|
---|
| 61 |
|
---|
| 62 | addr128_t dest_v6;
|
---|
| 63 | uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &dest_v6);
|
---|
| 64 |
|
---|
| 65 | if ((src_af != dest_af) || (src_af != AF_INET6))
|
---|
| 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 | rdgram.src = dgram->dest;
|
---|
| 81 | rdgram.dest = dgram->src;
|
---|
| 82 | rdgram.tos = 0;
|
---|
| 83 | rdgram.data = reply;
|
---|
| 84 | rdgram.size = size;
|
---|
| 85 |
|
---|
[12df1f1] | 86 | icmpv6_phdr_t phdr;
|
---|
[1d24ad3] | 87 |
|
---|
| 88 | host2addr128_t_be(dest_v6, phdr.src_addr);
|
---|
| 89 | host2addr128_t_be(src_v6, phdr.dest_addr);
|
---|
| 90 | phdr.length = host2uint32_t_be(dgram->size);
|
---|
| 91 | memset(phdr.zeroes, 0, 3);
|
---|
| 92 | phdr.next = IP_PROTO_ICMPV6;
|
---|
| 93 |
|
---|
| 94 | uint16_t cs_phdr =
|
---|
| 95 | inet_checksum_calc(INET_CHECKSUM_INIT, &phdr,
|
---|
[12df1f1] | 96 | sizeof(icmpv6_phdr_t));
|
---|
[1d24ad3] | 97 |
|
---|
| 98 | uint16_t cs_all = inet_checksum_calc(cs_phdr, reply, size);
|
---|
| 99 |
|
---|
| 100 | reply->checksum = host2uint16_t_be(cs_all);
|
---|
| 101 |
|
---|
| 102 | int rc = inet_route_packet(&rdgram, IP_PROTO_ICMPV6,
|
---|
| 103 | INET6_HOP_LIMIT_MAX, 0);
|
---|
| 104 |
|
---|
| 105 | free(reply);
|
---|
| 106 |
|
---|
| 107 | return rc;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | static int icmpv6_recv_echo_reply(inet_dgram_t *dgram)
|
---|
| 111 | {
|
---|
| 112 | log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv_echo_reply()");
|
---|
| 113 |
|
---|
| 114 | if (dgram->size < sizeof(icmpv6_message_t))
|
---|
| 115 | return EINVAL;
|
---|
| 116 |
|
---|
| 117 | inetping6_sdu_t sdu;
|
---|
| 118 |
|
---|
| 119 | uint16_t src_af = inet_addr_get(&dgram->src, NULL, &sdu.src);
|
---|
| 120 | uint16_t dest_af = inet_addr_get(&dgram->dest, NULL, &sdu.dest);
|
---|
| 121 |
|
---|
| 122 | if ((src_af != dest_af) || (src_af != AF_INET6))
|
---|
| 123 | return EINVAL;
|
---|
| 124 |
|
---|
| 125 | icmpv6_message_t *reply = (icmpv6_message_t *) dgram->data;
|
---|
| 126 |
|
---|
| 127 | sdu.seq_no = uint16_t_be2host(reply->un.echo.seq_no);
|
---|
| 128 | sdu.data = reply + sizeof(icmpv6_message_t);
|
---|
| 129 | sdu.size = dgram->size - sizeof(icmpv6_message_t);
|
---|
| 130 |
|
---|
| 131 | uint16_t ident = uint16_t_be2host(reply->un.echo.ident);
|
---|
| 132 |
|
---|
| 133 | return inetping6_recv(ident, &sdu);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | int icmpv6_recv(inet_dgram_t *dgram)
|
---|
| 137 | {
|
---|
| 138 | log_msg(LOG_DEFAULT, LVL_DEBUG, "icmpv6_recv()");
|
---|
| 139 |
|
---|
| 140 | if (dgram->size < 1)
|
---|
| 141 | return EINVAL;
|
---|
| 142 |
|
---|
| 143 | uint8_t type = *(uint8_t *) dgram->data;
|
---|
| 144 |
|
---|
| 145 | switch (type) {
|
---|
| 146 | case ICMPV6_ECHO_REQUEST:
|
---|
| 147 | return icmpv6_recv_echo_request(dgram);
|
---|
| 148 | case ICMPV6_ECHO_REPLY:
|
---|
| 149 | return icmpv6_recv_echo_reply(dgram);
|
---|
| 150 | case ICMPV6_NEIGHBOUR_SOLICITATION:
|
---|
| 151 | case ICMPV6_NEIGHBOUR_ADVERTISEMENT:
|
---|
| 152 | #ifdef ACCEPT_RA
|
---|
| 153 | case ICMPV6_ROUTER_ADVERTISEMENT:
|
---|
| 154 | #endif
|
---|
| 155 | return ndp_received(dgram);
|
---|
| 156 | default:
|
---|
| 157 | break;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return EINVAL;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | int icmpv6_ping_send(uint16_t ident, inetping6_sdu_t *sdu)
|
---|
| 164 | {
|
---|
| 165 | size_t rsize = sizeof(icmpv6_message_t) + sdu->size;
|
---|
| 166 | void *rdata = calloc(1, rsize);
|
---|
| 167 | if (rdata == NULL)
|
---|
| 168 | return ENOMEM;
|
---|
| 169 |
|
---|
| 170 | icmpv6_message_t *request = (icmpv6_message_t *) rdata;
|
---|
| 171 |
|
---|
| 172 | request->type = ICMPV6_ECHO_REQUEST;
|
---|
| 173 | request->code = 0;
|
---|
| 174 | request->checksum = 0;
|
---|
| 175 | request->un.echo.ident = host2uint16_t_be(ident);
|
---|
| 176 | request->un.echo.seq_no = host2uint16_t_be(sdu->seq_no);
|
---|
| 177 |
|
---|
| 178 | memcpy(rdata + sizeof(icmpv6_message_t), sdu->data, sdu->size);
|
---|
| 179 |
|
---|
| 180 | inet_dgram_t dgram;
|
---|
| 181 |
|
---|
| 182 | inet_addr_set6(sdu->src, &dgram.src);
|
---|
| 183 | inet_addr_set6(sdu->dest, &dgram.dest);
|
---|
| 184 | dgram.tos = 0;
|
---|
| 185 | dgram.data = rdata;
|
---|
| 186 | dgram.size = rsize;
|
---|
| 187 |
|
---|
[12df1f1] | 188 | icmpv6_phdr_t phdr;
|
---|
[1d24ad3] | 189 |
|
---|
| 190 | host2addr128_t_be(sdu->src, phdr.src_addr);
|
---|
| 191 | host2addr128_t_be(sdu->dest, 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,
|
---|
[12df1f1] | 198 | sizeof(icmpv6_phdr_t));
|
---|
[1d24ad3] | 199 |
|
---|
| 200 | uint16_t cs_all = inet_checksum_calc(cs_phdr, rdata, rsize);
|
---|
| 201 |
|
---|
| 202 | request->checksum = host2uint16_t_be(cs_all);
|
---|
| 203 |
|
---|
| 204 | int 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 | */
|
---|