rarp.c revision 1.3 1 1.3 brezak /*
2 1.3 brezak * Copyright (c) 1992 Regents of the University of California.
3 1.3 brezak * All rights reserved.
4 1.3 brezak *
5 1.3 brezak * This software was developed by the Computer Systems Engineering group
6 1.3 brezak * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 1.3 brezak * contributed to Berkeley.
8 1.3 brezak *
9 1.3 brezak * Redistribution and use in source and binary forms, with or without
10 1.3 brezak * modification, are permitted provided that the following conditions
11 1.3 brezak * are met:
12 1.3 brezak * 1. Redistributions of source code must retain the above copyright
13 1.3 brezak * notice, this list of conditions and the following disclaimer.
14 1.3 brezak * 2. Redistributions in binary form must reproduce the above copyright
15 1.3 brezak * notice, this list of conditions and the following disclaimer in the
16 1.3 brezak * documentation and/or other materials provided with the distribution.
17 1.3 brezak * 3. All advertising materials mentioning features or use of this software
18 1.3 brezak * must display the following acknowledgement:
19 1.3 brezak * This product includes software developed by the University of
20 1.3 brezak * California, Lawrence Berkeley Laboratory and its contributors.
21 1.3 brezak * 4. Neither the name of the University nor the names of its contributors
22 1.3 brezak * may be used to endorse or promote products derived from this software
23 1.3 brezak * without specific prior written permission.
24 1.3 brezak *
25 1.3 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.3 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.3 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.3 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.3 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.3 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.3 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.3 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.3 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.3 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.3 brezak * SUCH DAMAGE.
36 1.3 brezak *
37 1.3 brezak * from @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
38 1.3 brezak * $Id: rarp.c,v 1.3 1994/08/04 19:39:37 brezak Exp $
39 1.3 brezak */
40 1.1 brezak #include <sys/param.h>
41 1.1 brezak #include <sys/socket.h>
42 1.1 brezak #include <net/if.h>
43 1.1 brezak #include <netinet/in.h>
44 1.1 brezak
45 1.1 brezak #include <netinet/if_ether.h>
46 1.1 brezak #include <netinet/in_systm.h>
47 1.1 brezak
48 1.1 brezak #include <string.h>
49 1.1 brezak
50 1.1 brezak #include "stand.h"
51 1.1 brezak #include "net.h"
52 1.1 brezak #include "netif.h"
53 1.1 brezak
54 1.1 brezak static int rarpsend(struct iodesc *, void *, int);
55 1.1 brezak static int rarprecv(struct iodesc *, void *, int);
56 1.1 brezak
57 1.1 brezak /*
58 1.1 brezak * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
59 1.1 brezak */
60 1.1 brezak n_long
61 1.1 brezak rarp_getipaddress(sock)
62 1.1 brezak int sock;
63 1.1 brezak {
64 1.1 brezak struct iodesc *d;
65 1.1 brezak register struct ether_arp *ap;
66 1.1 brezak register void *pkt;
67 1.1 brezak struct {
68 1.1 brezak u_char header[HEADER_SIZE];
69 1.1 brezak struct ether_arp wrarp;
70 1.1 brezak } wbuf;
71 1.1 brezak union {
72 1.1 brezak u_char buffer[RECV_SIZE];
73 1.1 brezak struct {
74 1.1 brezak u_char header[HEADER_SIZE];
75 1.1 brezak struct ether_arp xrrarp;
76 1.1 brezak }xrbuf;
77 1.1 brezak #define rrarp xrbuf.xrrarp
78 1.1 brezak } rbuf;
79 1.1 brezak
80 1.1 brezak #ifdef RARP_DEBUG
81 1.1 brezak if (debug)
82 1.1 brezak printf("rarp: socket=%d\n", sock);
83 1.1 brezak #endif
84 1.1 brezak if (!(d = socktodesc(sock))) {
85 1.1 brezak printf("rarp: bad socket. %d\n", sock);
86 1.1 brezak return(INADDR_ANY);
87 1.1 brezak }
88 1.1 brezak #ifdef RARP_DEBUG
89 1.1 brezak if (debug)
90 1.1 brezak printf("rarp: d=%x\n", (u_int)d);
91 1.1 brezak #endif
92 1.1 brezak ap = &wbuf.wrarp;
93 1.1 brezak pkt = &rbuf.rrarp;
94 1.1 brezak pkt -= HEADER_SIZE;
95 1.1 brezak
96 1.1 brezak bzero(ap, sizeof(*ap));
97 1.1 brezak
98 1.1 brezak ap->arp_hrd = htons(ARPHRD_ETHER);
99 1.1 brezak ap->arp_pro = htons(ETHERTYPE_IP);
100 1.1 brezak ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
101 1.1 brezak ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
102 1.2 hpeyerl ap->arp_op = htons(ARPOP_REQUEST);
103 1.1 brezak bcopy(d->myea, ap->arp_sha, 6);
104 1.1 brezak bcopy(d->myea, ap->arp_tha, 6);
105 1.1 brezak
106 1.1 brezak if (sendrecv(d,
107 1.1 brezak rarpsend, ap, sizeof(*ap),
108 1.1 brezak rarprecv, pkt, RECV_SIZE) < 0) {
109 1.1 brezak printf("No response for RARP request\n");
110 1.1 brezak return(INADDR_ANY);
111 1.1 brezak }
112 1.1 brezak
113 1.1 brezak return(myip);
114 1.1 brezak }
115 1.1 brezak
116 1.1 brezak /*
117 1.1 brezak * Broadcast a RARP request (i.e. who knows who I am)
118 1.1 brezak */
119 1.1 brezak static int
120 1.1 brezak rarpsend(d, pkt, len)
121 1.1 brezak register struct iodesc *d;
122 1.1 brezak register void *pkt;
123 1.1 brezak register int len;
124 1.1 brezak {
125 1.1 brezak #ifdef RARP_DEBUG
126 1.1 brezak if (debug)
127 1.1 brezak printf("rarpsend: called\n");
128 1.1 brezak #endif
129 1.1 brezak return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
130 1.1 brezak }
131 1.1 brezak
132 1.1 brezak /*
133 1.1 brezak * Called when packet containing RARP is received
134 1.1 brezak */
135 1.1 brezak static int
136 1.1 brezak rarprecv(d, pkt, len)
137 1.1 brezak register struct iodesc *d;
138 1.1 brezak register void *pkt;
139 1.1 brezak register int len;
140 1.1 brezak {
141 1.1 brezak register struct ether_header *ep;
142 1.1 brezak register struct ether_arp *ap;
143 1.1 brezak
144 1.1 brezak #ifdef RARP_DEBUG
145 1.1 brezak if (debug)
146 1.1 brezak printf("rarprecv: called\n");
147 1.1 brezak #endif
148 1.1 brezak if (len < sizeof(struct ether_header) + sizeof(struct ether_arp)) {
149 1.1 brezak errno = 0;
150 1.1 brezak return (-1);
151 1.1 brezak }
152 1.1 brezak
153 1.1 brezak ep = (struct ether_header *)pkt;
154 1.1 brezak if (ntohs(ep->ether_type) != ETHERTYPE_REVARP) {
155 1.1 brezak errno = 0;
156 1.1 brezak return (-1);
157 1.1 brezak }
158 1.1 brezak
159 1.1 brezak ap = (struct ether_arp *)(ep + 1);
160 1.2 hpeyerl if (ntohs(ap->arp_op) != ARPOP_REPLY ||
161 1.1 brezak ntohs(ap->arp_pro) != ETHERTYPE_IP) {
162 1.1 brezak errno = 0;
163 1.1 brezak return (-1);
164 1.1 brezak }
165 1.1 brezak
166 1.1 brezak if (bcmp(ap->arp_tha, d->myea, 6)) {
167 1.1 brezak errno = 0;
168 1.1 brezak return (-1);
169 1.1 brezak }
170 1.1 brezak
171 1.1 brezak bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
172 1.1 brezak bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
173 1.1 brezak
174 1.1 brezak return(0);
175 1.1 brezak }
176