source: mainline/uspace/srv/net/ethip/arp.c@ 226f72e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 226f72e was 695b6ff, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Crude DHCP client prototype.

  • Property mode set to 100644
File size: 4.3 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 /* Broadcast address */
101 if (ip_addr == addr32_broadcast_all_hosts) {
102 addr48(addr48_broadcast, mac_addr);
103 return EOK;
104 }
105
106 int rc = atrans_lookup(ip_addr, mac_addr);
107 if (rc == EOK)
108 return EOK;
109
110 arp_eth_packet_t packet;
111
112 packet.opcode = aop_request;
113 addr48(nic->mac_addr, packet.sender_hw_addr);
114 packet.sender_proto_addr = src_addr;
115 addr48(addr48_broadcast, packet.target_hw_addr);
116 packet.target_proto_addr = ip_addr;
117
118 rc = arp_send_packet(nic, &packet);
119 if (rc != EOK)
120 return rc;
121
122 (void) atrans_wait_timeout(ARP_REQUEST_TIMEOUT);
123
124 return atrans_lookup(ip_addr, mac_addr);
125}
126
127static int arp_send_packet(ethip_nic_t *nic, arp_eth_packet_t *packet)
128{
129 int rc;
130 void *pdata;
131 size_t psize;
132
133 eth_frame_t frame;
134 void *fdata;
135 size_t fsize;
136
137 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_send_packet()");
138
139 rc = arp_pdu_encode(packet, &pdata, &psize);
140 if (rc != EOK)
141 return rc;
142
143 addr48(packet->target_hw_addr, frame.dest);
144 addr48(packet->sender_hw_addr, frame.src);
145 frame.etype_len = ETYPE_ARP;
146 frame.data = pdata;
147 frame.size = psize;
148
149 rc = eth_pdu_encode(&frame, &fdata, &fsize);
150 if (rc != EOK) {
151 free(pdata);
152 return rc;
153 }
154
155 rc = ethip_nic_send(nic, fdata, fsize);
156 free(fdata);
157 free(pdata);
158
159 return rc;
160
161}
162
163/** @}
164 */
Note: See TracBrowser for help on using the repository browser.