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

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

more IPv6 stub code

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[e2e56e67]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
[1493811]29/** @addtogroup ethip
[e2e56e67]30 * @{
31 */
32/**
33 * @file
34 * @brief IP link provider for Ethernet
[1493811]35 *
36 * Based on the IETF RFC 894 standard.
[e2e56e67]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>
[06295a9]45#include <stdlib.h>
[02a09ed]46#include <net/socket_codes.h>
[87e5658c]47#include "arp.h"
[06295a9]48#include "ethip.h"
49#include "ethip_nic.h"
[1493811]50#include "pdu.h"
51#include "std.h"
[e2e56e67]52
[06a1d077]53#define NAME "ethip"
[e2e56e67]54
[4f64a523]55static int ethip_open(iplink_srv_t *srv);
56static int ethip_close(iplink_srv_t *srv);
[02a09ed]57static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
[4f64a523]58static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu);
[02a09ed]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);
[e2e56e67]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,
[962f03b]68 .get_mtu = ethip_get_mtu,
69 .addr_add = ethip_addr_add,
70 .addr_remove = ethip_addr_remove
[e2e56e67]71};
72
73static int ethip_init(void)
74{
75 async_set_client_connection(ethip_client_conn);
[1038a9c]76
77 int rc = loc_server_register(NAME);
[e2e56e67]78 if (rc != EOK) {
[a1a101d]79 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
[e2e56e67]80 return rc;
81 }
[1038a9c]82
[06295a9]83 rc = ethip_nic_discovery_start();
84 if (rc != EOK)
[e2e56e67]85 return rc;
[1038a9c]86
[06295a9]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
[a1a101d]98 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_iplink_init()");
[06295a9]99
[4f64a523]100 iplink_srv_init(&nic->iplink);
[06295a9]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) {
[a1a101d]106 log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
[06295a9]107 goto error;
108 }
109
110 rc = loc_service_register(svc_name, &sid);
111 if (rc != EOK) {
[a1a101d]112 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name);
[06295a9]113 goto error;
[e2e56e67]114 }
115
[bc38578]116 nic->iplink_sid = sid;
117
[e2e56e67]118 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
119 if (rc != EOK) {
[a1a101d]120 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
[06295a9]121 goto error;
[e2e56e67]122 }
123
124 rc = loc_service_add_to_cat(sid, iplink_cat);
125 if (rc != EOK) {
[a1a101d]126 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name);
[06295a9]127 goto error;
[e2e56e67]128 }
129
130 return EOK;
[06295a9]131
132error:
133 if (svc_name != NULL)
134 free(svc_name);
135 return rc;
[e2e56e67]136}
137
138static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
139{
[bc38578]140 ethip_nic_t *nic;
141 service_id_t sid;
142
143 sid = (service_id_t)IPC_GET_ARG1(*icall);
[a1a101d]144 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
[bc38578]145 nic = ethip_nic_find_by_iplink_sid(sid);
146 if (nic == NULL) {
[a1a101d]147 log_msg(LOG_DEFAULT, LVL_WARN, "Uknown service ID.");
[bc38578]148 return;
149 }
150
151 iplink_conn(iid, icall, &nic->iplink);
[e2e56e67]152}
153
[4f64a523]154static int ethip_open(iplink_srv_t *srv)
[e2e56e67]155{
[a1a101d]156 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_open()");
[e2e56e67]157 return EOK;
158}
159
[4f64a523]160static int ethip_close(iplink_srv_t *srv)
[e2e56e67]161{
[a1a101d]162 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_close()");
[e2e56e67]163 return EOK;
164}
165
[02a09ed]166static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
[e2e56e67]167{
[02a09ed]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;
[1493811]184 eth_frame_t frame;
[02a09ed]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
[1493811]208 void *data;
209 size_t size;
210 rc = eth_pdu_encode(&frame, &data, &size);
211 if (rc != EOK)
212 return rc;
[257feec]213
[1493811]214 rc = ethip_nic_send(nic, data, size);
215 free(data);
[257feec]216
[1493811]217 return rc;
218}
219
220int ethip_received(iplink_srv_t *srv, void *data, size_t size)
221{
[a1a101d]222 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received(): srv=%p", srv);
[02a09ed]223 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
224
[a1a101d]225 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode");
[02a09ed]226
227 eth_frame_t frame;
228 int rc = eth_pdu_decode(data, size, &frame);
[4f64a523]229 if (rc != EOK) {
[a1a101d]230 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode failed");
[1493811]231 return rc;
[4f64a523]232 }
[02a09ed]233
234 iplink_recv_sdu_t sdu;
235
[87e5658c]236 switch (frame.etype_len) {
237 case ETYPE_ARP:
238 arp_received(nic, &frame);
239 break;
240 case ETYPE_IP:
[a1a101d]241 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU");
[87e5658c]242 sdu.data = frame.data;
243 sdu.size = frame.size;
[a1a101d]244 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
[02a09ed]245 rc = iplink_ev_recv(&nic->iplink, &sdu, AF_INET);
[87e5658c]246 break;
[1d24ad3]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;
[87e5658c]254 default:
[a1a101d]255 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ethertype 0x%" PRIx16,
[87e5658c]256 frame.etype_len);
257 }
[257feec]258
[1493811]259 free(frame.data);
260 return rc;
[e2e56e67]261}
262
[4f64a523]263static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu)
[e2e56e67]264{
[a1a101d]265 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mtu()");
[e2e56e67]266 *mtu = 1500;
267 return EOK;
268}
269
[02a09ed]270static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
[962f03b]271{
[a2e3ee6]272 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
273
[962f03b]274 return ethip_nic_addr_add(nic, addr);
275}
276
[02a09ed]277static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
[962f03b]278{
[02a09ed]279 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
[a2e3ee6]280
[02a09ed]281 return ethip_nic_addr_remove(nic, addr);
[962f03b]282}
283
[e2e56e67]284int main(int argc, char *argv[])
285{
286 int rc;
287
288 printf(NAME ": HelenOS IP over Ethernet service\n");
289
[267f235]290 if (log_init(NAME) != EOK) {
[e2e56e67]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.