source: mainline/uspace/srv/net/ethip/ethip.c@ 1d24ad3

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

more IPv6 stub code

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
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 IP link provider for Ethernet
35 *
36 * Based on the IETF RFC 894 standard.
37 */
38
39#include <async.h>
40#include <errno.h>
41#include <inet/iplink_srv.h>
42#include <io/log.h>
43#include <loc.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <net/socket_codes.h>
47#include "arp.h"
48#include "ethip.h"
49#include "ethip_nic.h"
50#include "pdu.h"
51#include "std.h"
52
53#define NAME "ethip"
54
55static int ethip_open(iplink_srv_t *srv);
56static int ethip_close(iplink_srv_t *srv);
57static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
58static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu);
59static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr);
60static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr);
61
62static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
63
64static iplink_ops_t ethip_iplink_ops = {
65 .open = ethip_open,
66 .close = ethip_close,
67 .send = ethip_send,
68 .get_mtu = ethip_get_mtu,
69 .addr_add = ethip_addr_add,
70 .addr_remove = ethip_addr_remove
71};
72
73static int ethip_init(void)
74{
75 async_set_client_connection(ethip_client_conn);
76
77 int rc = loc_server_register(NAME);
78 if (rc != EOK) {
79 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
80 return rc;
81 }
82
83 rc = ethip_nic_discovery_start();
84 if (rc != EOK)
85 return rc;
86
87 return EOK;
88}
89
90int ethip_iplink_init(ethip_nic_t *nic)
91{
92 int rc;
93 service_id_t sid;
94 category_id_t iplink_cat;
95 static unsigned link_num = 0;
96 char *svc_name = NULL;
97
98 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_iplink_init()");
99
100 iplink_srv_init(&nic->iplink);
101 nic->iplink.ops = &ethip_iplink_ops;
102 nic->iplink.arg = nic;
103
104 rc = asprintf(&svc_name, "net/eth%u", ++link_num);
105 if (rc < 0) {
106 log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
107 goto error;
108 }
109
110 rc = loc_service_register(svc_name, &sid);
111 if (rc != EOK) {
112 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name);
113 goto error;
114 }
115
116 nic->iplink_sid = sid;
117
118 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
119 if (rc != EOK) {
120 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
121 goto error;
122 }
123
124 rc = loc_service_add_to_cat(sid, iplink_cat);
125 if (rc != EOK) {
126 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name);
127 goto error;
128 }
129
130 return EOK;
131
132error:
133 if (svc_name != NULL)
134 free(svc_name);
135 return rc;
136}
137
138static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
139{
140 ethip_nic_t *nic;
141 service_id_t sid;
142
143 sid = (service_id_t)IPC_GET_ARG1(*icall);
144 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
145 nic = ethip_nic_find_by_iplink_sid(sid);
146 if (nic == NULL) {
147 log_msg(LOG_DEFAULT, LVL_WARN, "Uknown service ID.");
148 return;
149 }
150
151 iplink_conn(iid, icall, &nic->iplink);
152}
153
154static int ethip_open(iplink_srv_t *srv)
155{
156 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_open()");
157 return EOK;
158}
159
160static int ethip_close(iplink_srv_t *srv)
161{
162 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_close()");
163 return EOK;
164}
165
166static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
167{
168 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()");
169
170 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
171
172 addr32_t src_v4;
173 addr128_t src_v6;
174 uint16_t src_af = inet_addr_get(&sdu->src, &src_v4, &src_v6);
175
176 addr32_t dest_v4;
177 addr128_t dest_v6;
178 uint16_t dest_af = inet_addr_get(&sdu->dest, &dest_v4, &dest_v6);
179
180 if (src_af != dest_af)
181 return EINVAL;
182
183 int rc;
184 eth_frame_t frame;
185
186 switch (src_af) {
187 case AF_INET:
188 rc = arp_translate(nic, src_v4, dest_v4, frame.dest);
189 if (rc != EOK) {
190 log_msg(LOG_DEFAULT, LVL_WARN, "Failed to look up IPv4 address 0x%"
191 PRIx32, dest_v4);
192 return rc;
193 }
194
195 addr48(nic->mac_addr, frame.src);
196 frame.etype_len = ETYPE_IP;
197 frame.data = sdu->data;
198 frame.size = sdu->size;
199
200 break;
201 case AF_INET6:
202 // FIXME TODO
203 return ENOTSUP;
204 default:
205 return EINVAL;
206 }
207
208 void *data;
209 size_t size;
210 rc = eth_pdu_encode(&frame, &data, &size);
211 if (rc != EOK)
212 return rc;
213
214 rc = ethip_nic_send(nic, data, size);
215 free(data);
216
217 return rc;
218}
219
220int ethip_received(iplink_srv_t *srv, void *data, size_t size)
221{
222 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received(): srv=%p", srv);
223 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
224
225 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode");
226
227 eth_frame_t frame;
228 int rc = eth_pdu_decode(data, size, &frame);
229 if (rc != EOK) {
230 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode failed");
231 return rc;
232 }
233
234 iplink_recv_sdu_t sdu;
235
236 switch (frame.etype_len) {
237 case ETYPE_ARP:
238 arp_received(nic, &frame);
239 break;
240 case ETYPE_IP:
241 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU");
242 sdu.data = frame.data;
243 sdu.size = frame.size;
244 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
245 rc = iplink_ev_recv(&nic->iplink, &sdu, AF_INET);
246 break;
247 case ETYPE_IPV6:
248 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU IPv6");
249 sdu.data = frame.data;
250 sdu.size = frame.size;
251 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
252 rc = iplink_ev_recv(&nic->iplink, &sdu, AF_INET6);
253 break;
254 default:
255 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ethertype 0x%" PRIx16,
256 frame.etype_len);
257 }
258
259 free(frame.data);
260 return rc;
261}
262
263static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu)
264{
265 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mtu()");
266 *mtu = 1500;
267 return EOK;
268}
269
270static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
271{
272 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
273
274 return ethip_nic_addr_add(nic, addr);
275}
276
277static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
278{
279 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
280
281 return ethip_nic_addr_remove(nic, addr);
282}
283
284int main(int argc, char *argv[])
285{
286 int rc;
287
288 printf(NAME ": HelenOS IP over Ethernet service\n");
289
290 if (log_init(NAME) != EOK) {
291 printf(NAME ": Failed to initialize logging.\n");
292 return 1;
293 }
294
295 rc = ethip_init();
296 if (rc != EOK)
297 return 1;
298
299 printf(NAME ": Accepting connections.\n");
300 task_retval(0);
301 async_manager();
302
303 /* Not reached */
304 return 0;
305}
306
307/** @}
308 */
Note: See TracBrowser for help on using the repository browser.