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