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