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