source: mainline/uspace/srv/net/inetsrv/ndp.c@ b8b1adb1

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

setup NIC multicast address filter based on the configured IPv6 addresses
(multicast functionality is required for NDP)

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