source: mainline/uspace/srv/net/ethip/ethip.c@ 80d9d83

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

iplink IPv6 datagram support
rudimentary IPv6 support (sans fragmentation)

  • Property mode set to 100644
File size: 7.8 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);
[a17356fd]58static int ethip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu);
[4f64a523]59static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu);
[a17356fd]60static int ethip_get_mac48(iplink_srv_t *srv, addr48_t *mac);
[02a09ed]61static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr);
62static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr);
[e2e56e67]63
64static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
65
66static iplink_ops_t ethip_iplink_ops = {
67 .open = ethip_open,
68 .close = ethip_close,
69 .send = ethip_send,
[a17356fd]70 .send6 = ethip_send6,
[962f03b]71 .get_mtu = ethip_get_mtu,
[a17356fd]72 .get_mac48 = ethip_get_mac48,
[962f03b]73 .addr_add = ethip_addr_add,
74 .addr_remove = ethip_addr_remove
[e2e56e67]75};
76
77static int ethip_init(void)
78{
79 async_set_client_connection(ethip_client_conn);
[1038a9c]80
81 int rc = loc_server_register(NAME);
[e2e56e67]82 if (rc != EOK) {
[a1a101d]83 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
[e2e56e67]84 return rc;
85 }
[1038a9c]86
[06295a9]87 rc = ethip_nic_discovery_start();
88 if (rc != EOK)
[e2e56e67]89 return rc;
[1038a9c]90
[06295a9]91 return EOK;
92}
93
94int ethip_iplink_init(ethip_nic_t *nic)
95{
96 int rc;
97 service_id_t sid;
98 category_id_t iplink_cat;
99 static unsigned link_num = 0;
100 char *svc_name = NULL;
101
[a1a101d]102 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_iplink_init()");
[06295a9]103
[4f64a523]104 iplink_srv_init(&nic->iplink);
[06295a9]105 nic->iplink.ops = &ethip_iplink_ops;
106 nic->iplink.arg = nic;
107
108 rc = asprintf(&svc_name, "net/eth%u", ++link_num);
109 if (rc < 0) {
[a1a101d]110 log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
[06295a9]111 goto error;
112 }
113
114 rc = loc_service_register(svc_name, &sid);
115 if (rc != EOK) {
[a1a101d]116 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name);
[06295a9]117 goto error;
[e2e56e67]118 }
119
[bc38578]120 nic->iplink_sid = sid;
121
[e2e56e67]122 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
123 if (rc != EOK) {
[a1a101d]124 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
[06295a9]125 goto error;
[e2e56e67]126 }
127
128 rc = loc_service_add_to_cat(sid, iplink_cat);
129 if (rc != EOK) {
[a1a101d]130 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name);
[06295a9]131 goto error;
[e2e56e67]132 }
133
134 return EOK;
[06295a9]135
136error:
137 if (svc_name != NULL)
138 free(svc_name);
139 return rc;
[e2e56e67]140}
141
142static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
143{
[bc38578]144 ethip_nic_t *nic;
145 service_id_t sid;
146
147 sid = (service_id_t)IPC_GET_ARG1(*icall);
[a1a101d]148 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
[bc38578]149 nic = ethip_nic_find_by_iplink_sid(sid);
150 if (nic == NULL) {
[a1a101d]151 log_msg(LOG_DEFAULT, LVL_WARN, "Uknown service ID.");
[bc38578]152 return;
153 }
154
155 iplink_conn(iid, icall, &nic->iplink);
[e2e56e67]156}
157
[4f64a523]158static int ethip_open(iplink_srv_t *srv)
[e2e56e67]159{
[a1a101d]160 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_open()");
[e2e56e67]161 return EOK;
162}
163
[4f64a523]164static int ethip_close(iplink_srv_t *srv)
[e2e56e67]165{
[a1a101d]166 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_close()");
[e2e56e67]167 return EOK;
168}
169
[02a09ed]170static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
[e2e56e67]171{
[02a09ed]172 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()");
173
174 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
[a17356fd]175 eth_frame_t frame;
[02a09ed]176
[a17356fd]177 int rc = arp_translate(nic, sdu->src, sdu->dest, frame.dest);
178 if (rc != EOK) {
179 log_msg(LOG_DEFAULT, LVL_WARN, "Failed to look up IPv4 address 0x%"
180 PRIx32, sdu->dest);
181 return rc;
182 }
[02a09ed]183
[a17356fd]184 addr48(nic->mac_addr, frame.src);
185 frame.etype_len = ETYPE_IP;
186 frame.data = sdu->data;
187 frame.size = sdu->size;
[02a09ed]188
[a17356fd]189 void *data;
190 size_t size;
191 rc = eth_pdu_encode(&frame, &data, &size);
192 if (rc != EOK)
193 return rc;
[02a09ed]194
[a17356fd]195 rc = ethip_nic_send(nic, data, size);
196 free(data);
197
198 return rc;
199}
200
201static int ethip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
202{
203 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send6()");
204
205 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
[1493811]206 eth_frame_t frame;
[02a09ed]207
[a17356fd]208 addr48(sdu->dest, frame.dest);
209 addr48(nic->mac_addr, frame.src);
210 frame.etype_len = ETYPE_IPV6;
211 frame.data = sdu->data;
212 frame.size = sdu->size;
[02a09ed]213
[1493811]214 void *data;
215 size_t size;
[a17356fd]216 int rc = eth_pdu_encode(&frame, &data, &size);
[1493811]217 if (rc != EOK)
218 return rc;
[257feec]219
[1493811]220 rc = ethip_nic_send(nic, data, size);
221 free(data);
[257feec]222
[1493811]223 return rc;
224}
225
226int ethip_received(iplink_srv_t *srv, void *data, size_t size)
227{
[a1a101d]228 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received(): srv=%p", srv);
[02a09ed]229 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
230
[a1a101d]231 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode");
[02a09ed]232
233 eth_frame_t frame;
234 int rc = eth_pdu_decode(data, size, &frame);
[4f64a523]235 if (rc != EOK) {
[a1a101d]236 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode failed");
[1493811]237 return rc;
[4f64a523]238 }
[02a09ed]239
240 iplink_recv_sdu_t sdu;
241
[87e5658c]242 switch (frame.etype_len) {
243 case ETYPE_ARP:
244 arp_received(nic, &frame);
245 break;
246 case ETYPE_IP:
[a1a101d]247 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU");
[87e5658c]248 sdu.data = frame.data;
249 sdu.size = frame.size;
[a1a101d]250 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
[02a09ed]251 rc = iplink_ev_recv(&nic->iplink, &sdu, AF_INET);
[87e5658c]252 break;
[1d24ad3]253 case ETYPE_IPV6:
254 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU IPv6");
255 sdu.data = frame.data;
256 sdu.size = frame.size;
257 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
258 rc = iplink_ev_recv(&nic->iplink, &sdu, AF_INET6);
259 break;
[87e5658c]260 default:
[a1a101d]261 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ethertype 0x%" PRIx16,
[87e5658c]262 frame.etype_len);
263 }
[257feec]264
[1493811]265 free(frame.data);
266 return rc;
[e2e56e67]267}
268
[4f64a523]269static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu)
[e2e56e67]270{
[a1a101d]271 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mtu()");
[e2e56e67]272 *mtu = 1500;
273 return EOK;
274}
275
[a17356fd]276static int ethip_get_mac48(iplink_srv_t *srv, addr48_t *mac)
277{
278 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mac48()");
279
280 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
281 addr48(nic->mac_addr, *mac);
282
283 return EOK;
284}
285
[02a09ed]286static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
[962f03b]287{
[a2e3ee6]288 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
289
[962f03b]290 return ethip_nic_addr_add(nic, addr);
291}
292
[02a09ed]293static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
[962f03b]294{
[02a09ed]295 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
[a2e3ee6]296
[02a09ed]297 return ethip_nic_addr_remove(nic, addr);
[962f03b]298}
299
[e2e56e67]300int main(int argc, char *argv[])
301{
302 int rc;
303
304 printf(NAME ": HelenOS IP over Ethernet service\n");
305
[267f235]306 if (log_init(NAME) != EOK) {
[e2e56e67]307 printf(NAME ": Failed to initialize logging.\n");
308 return 1;
309 }
310
311 rc = ethip_init();
312 if (rc != EOK)
313 return 1;
314
315 printf(NAME ": Accepting connections.\n");
316 task_retval(0);
317 async_manager();
318
319 /* Not reached */
320 return 0;
321}
322
323/** @}
324 */
Note: See TracBrowser for help on using the repository browser.