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