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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fafb8e5 was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

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