source: mainline/uspace/srv/net/ethip/arp.c@ 02a09ed

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02a09ed was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 4.2 KB
Line 
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 <inet/addr.h>
41#include <stdlib.h>
42#include <net/socket_codes.h>
43#include "arp.h"
44#include "atrans.h"
45#include "ethip.h"
46#include "ethip_nic.h"
47#include "pdu.h"
48#include "std.h"
49
50/** Time to wait for ARP reply in microseconds */
51#define ARP_REQUEST_TIMEOUT (3 * 1000 * 1000)
52
53static int arp_send_packet(ethip_nic_t *nic, arp_eth_packet_t *packet);
54
55void arp_received(ethip_nic_t *nic, eth_frame_t *frame)
56{
57 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_received()");
58
59 arp_eth_packet_t packet;
60 int rc = arp_pdu_decode(frame->data, frame->size, &packet);
61 if (rc != EOK)
62 return;
63
64 log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU decoded, opcode=%d, tpa=%x",
65 packet.opcode, packet.target_proto_addr);
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);
94 }
95}
96
97int arp_translate(ethip_nic_t *nic, addr32_t src_addr, addr32_t ip_addr,
98 addr48_t mac_addr)
99{
100 int rc = atrans_lookup(ip_addr, mac_addr);
101 if (rc == EOK)
102 return EOK;
103
104 arp_eth_packet_t packet;
105
106 packet.opcode = aop_request;
107 addr48(nic->mac_addr, packet.sender_hw_addr);
108 packet.sender_proto_addr = src_addr;
109 addr48(addr48_broadcast, packet.target_hw_addr);
110 packet.target_proto_addr = ip_addr;
111
112 rc = arp_send_packet(nic, &packet);
113 if (rc != EOK)
114 return rc;
115
116 (void) atrans_wait_timeout(ARP_REQUEST_TIMEOUT);
117
118 return atrans_lookup(ip_addr, mac_addr);
119}
120
121static 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
131 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_send_packet()");
132
133 rc = arp_pdu_encode(packet, &pdata, &psize);
134 if (rc != EOK)
135 return rc;
136
137 addr48(packet->target_hw_addr, frame.dest);
138 addr48(packet->sender_hw_addr, frame.src);
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 */
Note: See TracBrowser for help on using the repository browser.