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 ethip
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <mem.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include "ntrans.h"
|
---|
42 | #include "addrobj.h"
|
---|
43 | #include "pdu.h"
|
---|
44 | #include "inet_link.h"
|
---|
45 | #include "ndp.h"
|
---|
46 |
|
---|
47 | /** Time to wait for NDP reply in microseconds */
|
---|
48 | #define NDP_REQUEST_TIMEOUT (3 * 1000 * 1000)
|
---|
49 |
|
---|
50 | static addr128_t solicited_node_ip =
|
---|
51 | { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0xff, 0, 0, 0 };
|
---|
52 |
|
---|
53 | /** Compute solicited node IPv6 multicast address from target IPv6 address
|
---|
54 | *
|
---|
55 | * @param ip_addr Target IPv6 address
|
---|
56 | * @param ip_solicited Solicited IPv6 address to be assigned
|
---|
57 | *
|
---|
58 | */
|
---|
59 | static void ndp_solicited_node_ip(addr128_t ip_addr,
|
---|
60 | addr128_t ip_solicited)
|
---|
61 | {
|
---|
62 | memcpy(ip_solicited, solicited_node_ip, 13);
|
---|
63 | memcpy(ip_solicited + 13, ip_addr + 13, 3);
|
---|
64 | }
|
---|
65 |
|
---|
66 | static errno_t ndp_send_packet(inet_link_t *link, ndp_packet_t *packet)
|
---|
67 | {
|
---|
68 | inet_dgram_t dgram;
|
---|
69 | ndp_pdu_encode(packet, &dgram);
|
---|
70 |
|
---|
71 | inet_link_send_dgram6(link, packet->target_hw_addr, &dgram,
|
---|
72 | IP_PROTO_ICMPV6, INET6_HOP_LIMIT_MAX, 0);
|
---|
73 |
|
---|
74 | free(dgram.data);
|
---|
75 |
|
---|
76 | return EOK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static errno_t ndp_router_advertisement(inet_dgram_t *dgram, inet_addr_t *router)
|
---|
80 | {
|
---|
81 | // FIXME TODO
|
---|
82 | return ENOTSUP;
|
---|
83 | }
|
---|
84 |
|
---|
85 | errno_t ndp_received(inet_dgram_t *dgram)
|
---|
86 | {
|
---|
87 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ndp_received()");
|
---|
88 |
|
---|
89 | ndp_packet_t packet;
|
---|
90 | errno_t rc = ndp_pdu_decode(dgram, &packet);
|
---|
91 | if (rc != EOK)
|
---|
92 | return rc;
|
---|
93 |
|
---|
94 | inet_addr_t sender;
|
---|
95 | inet_addr_set6(packet.sender_proto_addr, &sender);
|
---|
96 |
|
---|
97 | inet_addr_t target;
|
---|
98 | inet_addr_set6(packet.target_proto_addr, &target);
|
---|
99 |
|
---|
100 | inet_addrobj_t *laddr;
|
---|
101 |
|
---|
102 | log_msg(LOG_DEFAULT, LVL_DEBUG, "NDP PDU decoded; opcode: %d",
|
---|
103 | packet.opcode);
|
---|
104 |
|
---|
105 | switch (packet.opcode) {
|
---|
106 | case ICMPV6_NEIGHBOUR_SOLICITATION:
|
---|
107 | laddr = inet_addrobj_find(&target, iaf_addr);
|
---|
108 | if (laddr != NULL) {
|
---|
109 | rc = ntrans_add(packet.sender_proto_addr,
|
---|
110 | packet.sender_hw_addr);
|
---|
111 | if (rc != EOK)
|
---|
112 | return rc;
|
---|
113 |
|
---|
114 | ndp_packet_t reply;
|
---|
115 |
|
---|
116 | reply.opcode = ICMPV6_NEIGHBOUR_ADVERTISEMENT;
|
---|
117 | addr48(laddr->ilink->mac, reply.sender_hw_addr);
|
---|
118 | addr128(packet.target_proto_addr, reply.sender_proto_addr);
|
---|
119 | addr48(packet.sender_hw_addr, reply.target_hw_addr);
|
---|
120 | addr128(packet.sender_proto_addr, reply.target_proto_addr);
|
---|
121 |
|
---|
122 | ndp_send_packet(laddr->ilink, &reply);
|
---|
123 | }
|
---|
124 |
|
---|
125 | break;
|
---|
126 | case ICMPV6_NEIGHBOUR_ADVERTISEMENT:
|
---|
127 | laddr = inet_addrobj_find(&dgram->dest, iaf_addr);
|
---|
128 | if (laddr != NULL)
|
---|
129 | return ntrans_add(packet.sender_proto_addr,
|
---|
130 | packet.sender_hw_addr);
|
---|
131 |
|
---|
132 | break;
|
---|
133 | case ICMPV6_ROUTER_ADVERTISEMENT:
|
---|
134 | return ndp_router_advertisement(dgram, &sender);
|
---|
135 | default:
|
---|
136 | return ENOTSUP;
|
---|
137 | }
|
---|
138 |
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Translate IPv6 to MAC address
|
---|
143 | *
|
---|
144 | * @param src Source IPv6 address
|
---|
145 | * @param dest Destination IPv6 address
|
---|
146 | * @param mac Target MAC address to be assigned
|
---|
147 | * @param link Network interface
|
---|
148 | *
|
---|
149 | * @return EOK on success
|
---|
150 | * @return ENOENT when NDP translation failed
|
---|
151 | *
|
---|
152 | */
|
---|
153 | errno_t ndp_translate(addr128_t src_addr, addr128_t ip_addr, addr48_t mac_addr,
|
---|
154 | inet_link_t *ilink)
|
---|
155 | {
|
---|
156 | if (!ilink->mac_valid) {
|
---|
157 | /* The link does not support NDP */
|
---|
158 | memset(mac_addr, 0, 6);
|
---|
159 | return EOK;
|
---|
160 | }
|
---|
161 |
|
---|
162 | errno_t rc = ntrans_lookup(ip_addr, mac_addr);
|
---|
163 | if (rc == EOK)
|
---|
164 | return EOK;
|
---|
165 |
|
---|
166 | ndp_packet_t packet;
|
---|
167 |
|
---|
168 | packet.opcode = ICMPV6_NEIGHBOUR_SOLICITATION;
|
---|
169 | addr48(ilink->mac, packet.sender_hw_addr);
|
---|
170 | addr128(src_addr, packet.sender_proto_addr);
|
---|
171 | addr128(ip_addr, packet.solicited_ip);
|
---|
172 | addr48_solicited_node(ip_addr, packet.target_hw_addr);
|
---|
173 | ndp_solicited_node_ip(ip_addr, packet.target_proto_addr);
|
---|
174 |
|
---|
175 | rc = ndp_send_packet(ilink, &packet);
|
---|
176 | if (rc != EOK)
|
---|
177 | return rc;
|
---|
178 |
|
---|
179 | (void) ntrans_wait_timeout(NDP_REQUEST_TIMEOUT);
|
---|
180 |
|
---|
181 | return ntrans_lookup(ip_addr, mac_addr);
|
---|
182 | }
|
---|