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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f2f4c00 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
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 "arp.h"
47#include "ethip.h"
48#include "ethip_nic.h"
49#include "pdu.h"
50#include "std.h"
51
52#define NAME "ethip"
53
54static int ethip_open(iplink_srv_t *srv);
55static int ethip_close(iplink_srv_t *srv);
56static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
57static int ethip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu);
58static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu);
59static int ethip_get_mac48(iplink_srv_t *srv, addr48_t *mac);
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);
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,
69 .send6 = ethip_send6,
70 .get_mtu = ethip_get_mtu,
71 .get_mac48 = ethip_get_mac48,
72 .addr_add = ethip_addr_add,
73 .addr_remove = ethip_addr_remove
74};
75
76static int ethip_init(void)
77{
78 async_set_client_connection(ethip_client_conn);
79
80 int rc = loc_server_register(NAME);
81 if (rc != EOK) {
82 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
83 return rc;
84 }
85
86 rc = ethip_nic_discovery_start();
87 if (rc != EOK)
88 return rc;
89
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
101 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_iplink_init()");
102
103 iplink_srv_init(&nic->iplink);
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) {
109 log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
110 goto error;
111 }
112
113 rc = loc_service_register(svc_name, &sid);
114 if (rc != EOK) {
115 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name);
116 goto error;
117 }
118
119 nic->iplink_sid = sid;
120
121 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
122 if (rc != EOK) {
123 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
124 goto error;
125 }
126
127 rc = loc_service_add_to_cat(sid, iplink_cat);
128 if (rc != EOK) {
129 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name);
130 goto error;
131 }
132
133 return EOK;
134
135error:
136 if (svc_name != NULL)
137 free(svc_name);
138 return rc;
139}
140
141static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
142{
143 ethip_nic_t *nic;
144 service_id_t sid;
145
146 sid = (service_id_t)IPC_GET_ARG1(*icall);
147 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
148 nic = ethip_nic_find_by_iplink_sid(sid);
149 if (nic == NULL) {
150 log_msg(LOG_DEFAULT, LVL_WARN, "Uknown service ID.");
151 return;
152 }
153
154 iplink_conn(iid, icall, &nic->iplink);
155}
156
157static int ethip_open(iplink_srv_t *srv)
158{
159 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_open()");
160 return EOK;
161}
162
163static int ethip_close(iplink_srv_t *srv)
164{
165 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_close()");
166 return EOK;
167}
168
169static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
170{
171 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()");
172
173 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
174 eth_frame_t frame;
175
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 }
182
183 addr48(nic->mac_addr, frame.src);
184 frame.etype_len = ETYPE_IP;
185 frame.data = sdu->data;
186 frame.size = sdu->size;
187
188 void *data;
189 size_t size;
190 rc = eth_pdu_encode(&frame, &data, &size);
191 if (rc != EOK)
192 return rc;
193
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;
205 eth_frame_t frame;
206
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;
212
213 void *data;
214 size_t size;
215 int rc = eth_pdu_encode(&frame, &data, &size);
216 if (rc != EOK)
217 return rc;
218
219 rc = ethip_nic_send(nic, data, size);
220 free(data);
221
222 return rc;
223}
224
225int ethip_received(iplink_srv_t *srv, void *data, size_t size)
226{
227 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received(): srv=%p", srv);
228 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
229
230 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode");
231
232 eth_frame_t frame;
233 int rc = eth_pdu_decode(data, size, &frame);
234 if (rc != EOK) {
235 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode failed");
236 return rc;
237 }
238
239 iplink_recv_sdu_t sdu;
240
241 switch (frame.etype_len) {
242 case ETYPE_ARP:
243 arp_received(nic, &frame);
244 break;
245 case ETYPE_IP:
246 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU");
247 sdu.data = frame.data;
248 sdu.size = frame.size;
249 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
250 rc = iplink_ev_recv(&nic->iplink, &sdu, ip_v4);
251 break;
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");
257 rc = iplink_ev_recv(&nic->iplink, &sdu, ip_v6);
258 break;
259 default:
260 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ethertype 0x%" PRIx16,
261 frame.etype_len);
262 }
263
264 free(frame.data);
265 return rc;
266}
267
268static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu)
269{
270 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mtu()");
271 *mtu = 1500;
272 return EOK;
273}
274
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
285static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
286{
287 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
288
289 return ethip_nic_addr_add(nic, addr);
290}
291
292static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
293{
294 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
295
296 return ethip_nic_addr_remove(nic, addr);
297}
298
299int main(int argc, char *argv[])
300{
301 int rc;
302
303 printf(NAME ": HelenOS IP over Ethernet service\n");
304
305 if (log_init(NAME) != EOK) {
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.