source: mainline/uspace/srv/net/ethip/ethip.c@ 32d96e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 32d96e1 was 06a1d077, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Start network services automatically.

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