[e2e56e67] | 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 |
|
---|
[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>
|
---|
| 41 | #include <inet/iplink_srv.h>
|
---|
| 42 | #include <io/log.h>
|
---|
| 43 | #include <loc.h>
|
---|
| 44 | #include <stdio.h>
|
---|
[06295a9] | 45 | #include <stdlib.h>
|
---|
| 46 |
|
---|
[87e5658c] | 47 | #include "arp.h"
|
---|
[06295a9] | 48 | #include "ethip.h"
|
---|
| 49 | #include "ethip_nic.h"
|
---|
[1493811] | 50 | #include "pdu.h"
|
---|
| 51 | #include "std.h"
|
---|
[e2e56e67] | 52 |
|
---|
[06a1d077] | 53 | #define NAME "ethip"
|
---|
[e2e56e67] | 54 |
|
---|
[4f64a523] | 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);
|
---|
[962f03b] | 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);
|
---|
[e2e56e67] | 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,
|
---|
[962f03b] | 68 | .get_mtu = ethip_get_mtu,
|
---|
| 69 | .addr_add = ethip_addr_add,
|
---|
| 70 | .addr_remove = ethip_addr_remove
|
---|
[e2e56e67] | 71 | };
|
---|
| 72 |
|
---|
| 73 | static int ethip_init(void)
|
---|
| 74 | {
|
---|
| 75 | async_set_client_connection(ethip_client_conn);
|
---|
[1038a9c] | 76 |
|
---|
| 77 | int rc = loc_server_register(NAME);
|
---|
[e2e56e67] | 78 | if (rc != EOK) {
|
---|
[a1a101d] | 79 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
|
---|
[e2e56e67] | 80 | return rc;
|
---|
| 81 | }
|
---|
[1038a9c] | 82 |
|
---|
[06295a9] | 83 | rc = ethip_nic_discovery_start();
|
---|
| 84 | if (rc != EOK)
|
---|
[e2e56e67] | 85 | return rc;
|
---|
[1038a9c] | 86 |
|
---|
[06295a9] | 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 |
|
---|
[a1a101d] | 98 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_iplink_init()");
|
---|
[06295a9] | 99 |
|
---|
[4f64a523] | 100 | iplink_srv_init(&nic->iplink);
|
---|
[06295a9] | 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) {
|
---|
[a1a101d] | 106 | log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
|
---|
[06295a9] | 107 | goto error;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | rc = loc_service_register(svc_name, &sid);
|
---|
| 111 | if (rc != EOK) {
|
---|
[a1a101d] | 112 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name);
|
---|
[06295a9] | 113 | goto error;
|
---|
[e2e56e67] | 114 | }
|
---|
| 115 |
|
---|
[bc38578] | 116 | nic->iplink_sid = sid;
|
---|
| 117 |
|
---|
[e2e56e67] | 118 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
---|
| 119 | if (rc != EOK) {
|
---|
[a1a101d] | 120 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
|
---|
[06295a9] | 121 | goto error;
|
---|
[e2e56e67] | 122 | }
|
---|
| 123 |
|
---|
| 124 | rc = loc_service_add_to_cat(sid, iplink_cat);
|
---|
| 125 | if (rc != EOK) {
|
---|
[a1a101d] | 126 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name);
|
---|
[06295a9] | 127 | goto error;
|
---|
[e2e56e67] | 128 | }
|
---|
| 129 |
|
---|
| 130 | return EOK;
|
---|
[06295a9] | 131 |
|
---|
| 132 | error:
|
---|
| 133 | if (svc_name != NULL)
|
---|
| 134 | free(svc_name);
|
---|
| 135 | return rc;
|
---|
[e2e56e67] | 136 | }
|
---|
| 137 |
|
---|
| 138 | static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 139 | {
|
---|
[bc38578] | 140 | ethip_nic_t *nic;
|
---|
| 141 | service_id_t sid;
|
---|
| 142 |
|
---|
| 143 | sid = (service_id_t)IPC_GET_ARG1(*icall);
|
---|
[a1a101d] | 144 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
|
---|
[bc38578] | 145 | nic = ethip_nic_find_by_iplink_sid(sid);
|
---|
| 146 | if (nic == NULL) {
|
---|
[a1a101d] | 147 | log_msg(LOG_DEFAULT, LVL_WARN, "Uknown service ID.");
|
---|
[bc38578] | 148 | return;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | iplink_conn(iid, icall, &nic->iplink);
|
---|
[e2e56e67] | 152 | }
|
---|
| 153 |
|
---|
[4f64a523] | 154 | static int ethip_open(iplink_srv_t *srv)
|
---|
[e2e56e67] | 155 | {
|
---|
[a1a101d] | 156 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_open()");
|
---|
[e2e56e67] | 157 | return EOK;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[4f64a523] | 160 | static int ethip_close(iplink_srv_t *srv)
|
---|
[e2e56e67] | 161 | {
|
---|
[a1a101d] | 162 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_close()");
|
---|
[e2e56e67] | 163 | return EOK;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[4f64a523] | 166 | static int ethip_send(iplink_srv_t *srv, iplink_srv_sdu_t *sdu)
|
---|
[e2e56e67] | 167 | {
|
---|
[4f64a523] | 168 | ethip_nic_t *nic = (ethip_nic_t *)srv->arg;
|
---|
[1493811] | 169 | eth_frame_t frame;
|
---|
| 170 | void *data;
|
---|
| 171 | size_t size;
|
---|
[f9d3dd4] | 172 | mac48_addr_t dest_mac_addr;
|
---|
[1493811] | 173 | int rc;
|
---|
| 174 |
|
---|
[a1a101d] | 175 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()");
|
---|
[1493811] | 176 |
|
---|
[3d016ac] | 177 | rc = arp_translate(nic, &sdu->lsrc, &sdu->ldest, &dest_mac_addr);
|
---|
[f9d3dd4] | 178 | if (rc != EOK) {
|
---|
[a1a101d] | 179 | log_msg(LOG_DEFAULT, LVL_WARN, "Failed to look up IP address 0x%" PRIx32,
|
---|
[f9d3dd4] | 180 | sdu->ldest.ipv4);
|
---|
| 181 | return rc;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | frame.dest = dest_mac_addr;
|
---|
[56792a2] | 185 | frame.src = nic->mac_addr;
|
---|
[1493811] | 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 | {
|
---|
[a1a101d] | 202 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received(): srv=%p", srv);
|
---|
[4f64a523] | 203 | ethip_nic_t *nic = (ethip_nic_t *)srv->arg;
|
---|
[1493811] | 204 | eth_frame_t frame;
|
---|
| 205 | iplink_srv_sdu_t sdu;
|
---|
| 206 | int rc;
|
---|
| 207 |
|
---|
[a1a101d] | 208 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received()");
|
---|
[4f64a523] | 209 |
|
---|
[a1a101d] | 210 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode");
|
---|
[1493811] | 211 | rc = eth_pdu_decode(data, size, &frame);
|
---|
[4f64a523] | 212 | if (rc != EOK) {
|
---|
[a1a101d] | 213 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode failed");
|
---|
[1493811] | 214 | return rc;
|
---|
[4f64a523] | 215 | }
|
---|
[1493811] | 216 |
|
---|
[87e5658c] | 217 | switch (frame.etype_len) {
|
---|
| 218 | case ETYPE_ARP:
|
---|
| 219 | arp_received(nic, &frame);
|
---|
| 220 | break;
|
---|
| 221 | case ETYPE_IP:
|
---|
[a1a101d] | 222 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU");
|
---|
[87e5658c] | 223 | sdu.lsrc.ipv4 = (192 << 24) | (168 << 16) | (0 << 8) | 1;
|
---|
| 224 | sdu.ldest.ipv4 = (192 << 24) | (168 << 16) | (0 << 8) | 4;
|
---|
| 225 | sdu.data = frame.data;
|
---|
| 226 | sdu.size = frame.size;
|
---|
[a1a101d] | 227 | log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
|
---|
[87e5658c] | 228 | rc = iplink_ev_recv(&nic->iplink, &sdu);
|
---|
| 229 | break;
|
---|
| 230 | default:
|
---|
[a1a101d] | 231 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ethertype 0x%" PRIx16,
|
---|
[87e5658c] | 232 | frame.etype_len);
|
---|
| 233 | }
|
---|
[1493811] | 234 |
|
---|
| 235 | free(frame.data);
|
---|
| 236 | return rc;
|
---|
[e2e56e67] | 237 | }
|
---|
| 238 |
|
---|
[4f64a523] | 239 | static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu)
|
---|
[e2e56e67] | 240 | {
|
---|
[a1a101d] | 241 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mtu()");
|
---|
[e2e56e67] | 242 | *mtu = 1500;
|
---|
| 243 | return EOK;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[962f03b] | 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 |
|
---|
[a1a101d] | 250 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_addr_add(0x%" PRIx32 ")", addr->ipv4);
|
---|
[962f03b] | 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 |
|
---|
[a1a101d] | 258 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_addr_remove(0x%" PRIx32 ")", addr->ipv4);
|
---|
[962f03b] | 259 | return ethip_nic_addr_add(nic, addr);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[e2e56e67] | 262 | int main(int argc, char *argv[])
|
---|
| 263 | {
|
---|
| 264 | int rc;
|
---|
| 265 |
|
---|
| 266 | printf(NAME ": HelenOS IP over Ethernet service\n");
|
---|
| 267 |
|
---|
[267f235] | 268 | if (log_init(NAME) != EOK) {
|
---|
[e2e56e67] | 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 | */
|
---|