source: mainline/uspace/srv/net/ethip/ethip.c@ fc6abbe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fc6abbe was 417a2ba1, checked in by Jiri Svoboda <jiri@…>, 12 years ago

iplink_ev_recv() should use ip_ver_t instead of AF.

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