arp.c (4246B)
1 /* $Id: //depot/blt/srv/network/protocol/arp/arp.c#1 $ 2 ** 3 ** Copyright 1999 Sidney Cammeresi 4 ** All rights reserved. 5 ** 6 ** Redistribution and use in source and binary forms, with or without 7 ** modification, are permitted provided that the following conditions 8 ** are met: 9 ** 1. Redistributions of source code must retain the above copyright 10 ** notice, this list of conditions, and the following disclaimer. 11 ** 2. 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 ** 3. 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 #include <blt/network/mbuf.h> 30 #include <blt/network/eth.h> 31 #include <blt/network/ipv4.h> 32 33 void arp_input (struct mbuf *mbuf); 34 static eth_prot_t protocol = { "arp", 0, arp_input, NULL }; 35 36 int _init (void) 37 { 38 protocol.num = htons (ETH_PROT_ARP); 39 register_eth_protocol (&protocol); 40 return 0; 41 } 42 43 void arp_print_addrs (arp_packet *arp) 44 { 45 printf ("%X:%X:%X:%X:%X:%X %X:%X:%X:%X:%X:%X\n", arp->header.src[0], 46 arp->header.src[1], arp->header.src[2], arp->header.src[3], 47 arp->header.src[4], arp->header.src[5], arp->header.dst[0], 48 arp->header.dst[1], arp->header.dst[2], arp->header.dst[3], 49 arp->header.dst[4], arp->header.dst[5]); 50 } 51 52 /* 53 * this doesn't take too long, so we do it synchronously. 54 */ 55 void arp_input (struct mbuf *mbuf) 56 { 57 arp_packet *arp_request, *arp_reply; 58 ipv4_iface_t *iface; 59 eth_dev_t *dev; 60 struct mbuf *reply_header, *reply; 61 62 arp_request = (arp_packet *) mbuf->m_next->m_data; 63 iface = iflist; 64 while (iface != NULL) 65 if (!memcmp (iface->addr, arp_request->targ_ip_addr, 4)) 66 { 67 printf ("ARP: who-has %d.%d.%d.%d tell %d.%d.%d.%d\n", 68 arp_request->targ_ip_addr[0], arp_request->targ_ip_addr[1], 69 arp_request->targ_ip_addr[2], arp_request->targ_ip_addr[3], 70 arp_request->send_ip_addr[0], arp_request->send_ip_addr[1], 71 arp_request->send_ip_addr[2], arp_request->send_ip_addr[3]); 72 73 dev = *((eth_dev_t **) mbuf->m_data); 74 reply_header = mget (); 75 reply_header->m_next = reply = mget (); 76 arp_reply = (arp_packet *) (reply->m_data = reply->m_databuf); 77 memcpy (arp_reply->header.dst, arp_request->header.src, 6); 78 memcpy (arp_reply->header.src, dev->dev_addr, 6); 79 arp_reply->header.frame_type = htons (ETH_PROT_ARP); 80 arp_reply->hard_type = htons (1); 81 arp_reply->prot_type = htons (ETH_PROT_IP); 82 arp_reply->hard_size = 6; 83 arp_reply->prot_size = 4; 84 arp_reply->op = htons (ETH_ARP_OP_REPLY); 85 memcpy (arp_reply->send_eth_addr, dev->dev_addr, 6); 86 memcpy (arp_reply->send_ip_addr, iface->addr, 4); 87 memcpy (arp_reply->targ_eth_addr, arp_request->header.src, 6); 88 memcpy (arp_reply->targ_ip_addr, arp_request->send_ip_addr, 4); 89 90 printf ("ARP: reply %d.%d.%d.%d is-at %X:%X:%X:%X:%X:%X\n", 91 arp_reply->send_ip_addr[0], arp_reply->send_ip_addr[1], 92 arp_reply->send_ip_addr[2], arp_reply->send_ip_addr[3], 93 arp_reply->send_eth_addr[0], arp_reply->send_eth_addr[1], 94 arp_reply->send_eth_addr[2], arp_reply->send_eth_addr[3], 95 arp_reply->send_eth_addr[4], arp_reply->send_eth_addr[5]); 96 97 arp_print_addrs (arp_request); 98 arp_print_addrs (arp_reply); 99 reply->m_len = sizeof (arp_packet); 100 dev->dev_driver->output (dev->dev_num, reply_header); 101 mput (mbuf->m_next); 102 mput (mbuf); 103 return; 104 } 105 else 106 iface = iface->next; 107 } 108