[87e5658c] | 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 inet
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <io/log.h>
|
---|
| 39 | #include <inet/iplink_srv.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
| 41 |
|
---|
| 42 | #include "arp.h"
|
---|
[f9d3dd4] | 43 | #include "atrans.h"
|
---|
[87e5658c] | 44 | #include "ethip.h"
|
---|
| 45 | #include "ethip_nic.h"
|
---|
| 46 | #include "pdu.h"
|
---|
| 47 | #include "std.h"
|
---|
| 48 |
|
---|
[3d016ac] | 49 | /** Time to wait for ARP reply in microseconds */
|
---|
| 50 | #define ARP_REQUEST_TIMEOUT (3 * 1000 * 1000)
|
---|
| 51 |
|
---|
[87e5658c] | 52 | static int arp_send_packet(ethip_nic_t *nic, arp_eth_packet_t *packet);
|
---|
| 53 |
|
---|
| 54 | void arp_received(ethip_nic_t *nic, eth_frame_t *frame)
|
---|
| 55 | {
|
---|
| 56 | int rc;
|
---|
| 57 | arp_eth_packet_t packet;
|
---|
| 58 | arp_eth_packet_t reply;
|
---|
[962f03b] | 59 | ethip_link_addr_t *laddr;
|
---|
[87e5658c] | 60 |
|
---|
[a1a101d] | 61 | log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_received()");
|
---|
[87e5658c] | 62 |
|
---|
| 63 | rc = arp_pdu_decode(frame->data, frame->size, &packet);
|
---|
| 64 | if (rc != EOK)
|
---|
| 65 | return;
|
---|
| 66 |
|
---|
[a1a101d] | 67 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU decoded, opcode=%d, tpa=%x",
|
---|
[87e5658c] | 68 | packet.opcode, packet.target_proto_addr.ipv4);
|
---|
| 69 |
|
---|
[3d016ac] | 70 | laddr = ethip_nic_addr_find(nic, &packet.target_proto_addr);
|
---|
| 71 | if (laddr != NULL) {
|
---|
[a1a101d] | 72 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Request/reply to my address");
|
---|
[87e5658c] | 73 |
|
---|
[3d016ac] | 74 | (void) atrans_add(&packet.sender_proto_addr,
|
---|
| 75 | &packet.sender_hw_addr);
|
---|
[f9d3dd4] | 76 |
|
---|
[3d016ac] | 77 | if (packet.opcode == aop_request) {
|
---|
| 78 | reply.opcode = aop_reply;
|
---|
| 79 | reply.sender_hw_addr = nic->mac_addr;
|
---|
[962f03b] | 80 | reply.sender_proto_addr = laddr->addr;
|
---|
| 81 | reply.target_hw_addr = packet.sender_hw_addr;
|
---|
| 82 | reply.target_proto_addr = packet.sender_proto_addr;
|
---|
| 83 |
|
---|
| 84 | arp_send_packet(nic, &reply);
|
---|
| 85 | }
|
---|
[87e5658c] | 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[3d016ac] | 89 | int arp_translate(ethip_nic_t *nic, iplink_srv_addr_t *src_addr,
|
---|
| 90 | iplink_srv_addr_t *ip_addr, mac48_addr_t *mac_addr)
|
---|
| 91 | {
|
---|
| 92 | int rc;
|
---|
| 93 | arp_eth_packet_t packet;
|
---|
| 94 |
|
---|
| 95 | rc = atrans_lookup(ip_addr, mac_addr);
|
---|
| 96 | if (rc == EOK)
|
---|
| 97 | return EOK;
|
---|
| 98 |
|
---|
| 99 | packet.opcode = aop_request;
|
---|
| 100 | packet.sender_hw_addr = nic->mac_addr;
|
---|
| 101 | packet.sender_proto_addr = *src_addr;
|
---|
| 102 | packet.target_hw_addr.addr = MAC48_BROADCAST;
|
---|
| 103 | packet.target_proto_addr = *ip_addr;
|
---|
| 104 |
|
---|
| 105 | rc = arp_send_packet(nic, &packet);
|
---|
| 106 | if (rc != EOK)
|
---|
| 107 | return rc;
|
---|
| 108 |
|
---|
| 109 | (void) atrans_wait_timeout(ARP_REQUEST_TIMEOUT);
|
---|
| 110 |
|
---|
| 111 | return atrans_lookup(ip_addr, mac_addr);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[87e5658c] | 114 | static int arp_send_packet(ethip_nic_t *nic, arp_eth_packet_t *packet)
|
---|
| 115 | {
|
---|
| 116 | int rc;
|
---|
| 117 | void *pdata;
|
---|
| 118 | size_t psize;
|
---|
| 119 |
|
---|
| 120 | eth_frame_t frame;
|
---|
| 121 | void *fdata;
|
---|
| 122 | size_t fsize;
|
---|
| 123 |
|
---|
[a1a101d] | 124 | log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_send_packet()");
|
---|
[87e5658c] | 125 |
|
---|
| 126 | rc = arp_pdu_encode(packet, &pdata, &psize);
|
---|
| 127 | if (rc != EOK)
|
---|
| 128 | return rc;
|
---|
| 129 |
|
---|
| 130 | frame.dest.addr = packet->target_hw_addr.addr;
|
---|
| 131 | frame.src.addr = packet->sender_hw_addr.addr;
|
---|
| 132 | frame.etype_len = ETYPE_ARP;
|
---|
| 133 | frame.data = pdata;
|
---|
| 134 | frame.size = psize;
|
---|
| 135 |
|
---|
| 136 | rc = eth_pdu_encode(&frame, &fdata, &fsize);
|
---|
| 137 | if (rc != EOK) {
|
---|
| 138 | free(pdata);
|
---|
| 139 | return rc;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | rc = ethip_nic_send(nic, fdata, fsize);
|
---|
| 143 | free(fdata);
|
---|
| 144 | free(pdata);
|
---|
| 145 |
|
---|
| 146 | return rc;
|
---|
| 147 |
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /** @}
|
---|
| 151 | */
|
---|