source: mainline/uspace/srv/net/ethip/ethip.c@ 3a4c6d9

Last change on this file since 3a4c6d9 was 3a4c6d9, checked in by Jiri Svoboda <jiri@…>, 2 months ago

Packet capture (thx Nataliia Korop)

  • Property mode set to 100644
File size: 8.3 KB
Line 
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/** Interface for dumping packets */
56
57static errno_t ethip_open(iplink_srv_t *srv);
58static errno_t ethip_close(iplink_srv_t *srv);
59static errno_t ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
60static errno_t ethip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu);
61static errno_t ethip_get_mtu(iplink_srv_t *srv, size_t *mtu);
62static errno_t ethip_get_mac48(iplink_srv_t *srv, eth_addr_t *mac);
63static errno_t ethip_set_mac48(iplink_srv_t *srv, eth_addr_t *mac);
64static errno_t ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr);
65static errno_t ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr);
66
67static void ethip_client_conn(ipc_call_t *icall, void *arg);
68
69static iplink_ops_t ethip_iplink_ops = {
70 .open = ethip_open,
71 .close = ethip_close,
72 .send = ethip_send,
73 .send6 = ethip_send6,
74 .get_mtu = ethip_get_mtu,
75 .get_mac48 = ethip_get_mac48,
76 .set_mac48 = ethip_set_mac48,
77 .addr_add = ethip_addr_add,
78 .addr_remove = ethip_addr_remove
79};
80
81static loc_srv_t *ethip_srv;
82
83static errno_t ethip_init(void)
84{
85 async_set_fallback_port_handler(ethip_client_conn, NULL);
86
87 errno_t rc = loc_server_register(NAME, &ethip_srv);
88 if (rc != EOK) {
89 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
90 return rc;
91 }
92
93 rc = ethip_nic_discovery_start();
94 if (rc != EOK)
95 return rc;
96
97 return EOK;
98}
99
100errno_t ethip_iplink_init(ethip_nic_t *nic)
101{
102 errno_t rc;
103 service_id_t sid;
104 category_id_t iplink_cat;
105 static unsigned link_num = 0;
106 char *svc_name = NULL;
107
108 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_iplink_init()");
109
110 iplink_srv_init(&nic->iplink);
111 nic->iplink.ops = &ethip_iplink_ops;
112 nic->iplink.arg = nic;
113
114 if (asprintf(&svc_name, "net/eth%u", ++link_num) < 0) {
115 log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
116 rc = ENOMEM;
117 goto error;
118 }
119
120 rc = loc_service_register(ethip_srv, svc_name, fallback_port_id, &sid);
121 if (rc != EOK) {
122 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name);
123 goto error;
124 }
125
126 nic->iplink_sid = sid;
127
128 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
129 if (rc != EOK) {
130 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
131 goto error;
132 }
133
134 rc = loc_service_add_to_cat(ethip_srv, sid, iplink_cat);
135 if (rc != EOK) {
136 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name);
137 goto error;
138 }
139
140 return EOK;
141
142error:
143 if (svc_name != NULL)
144 free(svc_name);
145 return rc;
146}
147
148static void ethip_client_conn(ipc_call_t *icall, void *arg)
149{
150 ethip_nic_t *nic;
151 service_id_t sid;
152
153 sid = (service_id_t) ipc_get_arg2(icall);
154 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
155 nic = ethip_nic_find_by_iplink_sid(sid);
156 if (nic == NULL) {
157 log_msg(LOG_DEFAULT, LVL_WARN, "Uknown service ID.");
158 return;
159 }
160
161 iplink_conn(icall, &nic->iplink);
162}
163
164static errno_t ethip_open(iplink_srv_t *srv)
165{
166 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_open()");
167 return EOK;
168}
169
170static errno_t ethip_close(iplink_srv_t *srv)
171{
172 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_close()");
173 return EOK;
174}
175
176static errno_t ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
177{
178 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()");
179
180 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
181 eth_frame_t frame;
182
183 errno_t rc = arp_translate(nic, sdu->src, sdu->dest, &frame.dest);
184 if (rc != EOK) {
185 log_msg(LOG_DEFAULT, LVL_WARN, "Failed to look up IPv4 address 0x%"
186 PRIx32, sdu->dest);
187 return rc;
188 }
189
190 frame.src = nic->mac_addr;
191 frame.etype_len = ETYPE_IP;
192 frame.data = sdu->data;
193 frame.size = sdu->size;
194
195 void *data;
196 size_t size;
197 rc = eth_pdu_encode(&frame, &data, &size);
198 if (rc != EOK)
199 return rc;
200 rc = ethip_nic_send(nic, data, size);
201 free(data);
202
203 return rc;
204}
205
206static 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
231errno_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
274static 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
281static 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
291static 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
301static 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
308static 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
315int 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 */
Note: See TracBrowser for help on using the repository browser.