Deleted Added
full compact
if_ether.c (32350) if_ether.c (34961)
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. 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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. 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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
34 * $Id: if_ether.c,v 1.42 1997/12/20 00:07:11 bde Exp $
34 * $Id: if_ether.c,v 1.43 1998/01/08 23:41:43 eivind Exp $
35 */
36
37/*
38 * Ethernet address resolution protocol.
39 * TODO:
40 * add "inuse/lock" bit (or ref. count) along with valid bit
41 */
42
43#include "opt_inet.h"
44
45#include <sys/param.h>
46#include <sys/kernel.h>
47#include <sys/sysctl.h>
48#include <sys/systm.h>
49#include <sys/mbuf.h>
50#include <sys/malloc.h>
51#include <sys/socket.h>
52#include <sys/syslog.h>
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/route.h>
57#include <net/netisr.h>
58
59#include <netinet/in.h>
60#include <netinet/in_var.h>
61#include <netinet/if_ether.h>
62
63#define SIN(s) ((struct sockaddr_in *)s)
64#define SDL(s) ((struct sockaddr_dl *)s)
65
66SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
67
68/* timer values */
69static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
70static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
71static int arpt_down = 20; /* once declared down, don't send for 20 sec */
72
73SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
74 &arpt_prune, 0, "");
75SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
76 &arpt_keep, 0, "");
77SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
78 &arpt_down, 0, "");
79
80#define rt_expire rt_rmx.rmx_expire
81
82struct llinfo_arp {
83 LIST_ENTRY(llinfo_arp) la_le;
84 struct rtentry *la_rt;
85 struct mbuf *la_hold; /* last packet until resolved/timeout */
86 long la_asked; /* last time we QUERIED for this addr */
87#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
88};
89
90static LIST_HEAD(, llinfo_arp) llinfo_arp;
91
92struct ifqueue arpintrq = {0, 0, 0, 50};
93static int arp_inuse, arp_allocated;
94
95static int arp_maxtries = 5;
96static int useloopback = 1; /* use loopback interface for local traffic */
97static int arp_proxyall = 0;
98
99SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
100 &arp_maxtries, 0, "");
101SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
102 &useloopback, 0, "");
103SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
104 &arp_proxyall, 0, "");
105
106static void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
107static void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
108static void arpintr __P((void));
109static void arptfree __P((struct llinfo_arp *));
110static void arptimer __P((void *));
111static struct llinfo_arp
112 *arplookup __P((u_long, int, int));
113#ifdef INET
114static void in_arpinput __P((struct mbuf *));
115#endif
116
117/*
118 * Timeout routine. Age arp_tab entries periodically.
119 */
120/* ARGSUSED */
121static void
122arptimer(ignored_arg)
123 void *ignored_arg;
124{
125 int s = splnet();
126 register struct llinfo_arp *la = llinfo_arp.lh_first;
127 struct llinfo_arp *ola;
128
129 timeout(arptimer, (caddr_t)0, arpt_prune * hz);
130 while ((ola = la) != 0) {
131 register struct rtentry *rt = la->la_rt;
132 la = la->la_le.le_next;
35 */
36
37/*
38 * Ethernet address resolution protocol.
39 * TODO:
40 * add "inuse/lock" bit (or ref. count) along with valid bit
41 */
42
43#include "opt_inet.h"
44
45#include <sys/param.h>
46#include <sys/kernel.h>
47#include <sys/sysctl.h>
48#include <sys/systm.h>
49#include <sys/mbuf.h>
50#include <sys/malloc.h>
51#include <sys/socket.h>
52#include <sys/syslog.h>
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/route.h>
57#include <net/netisr.h>
58
59#include <netinet/in.h>
60#include <netinet/in_var.h>
61#include <netinet/if_ether.h>
62
63#define SIN(s) ((struct sockaddr_in *)s)
64#define SDL(s) ((struct sockaddr_dl *)s)
65
66SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
67
68/* timer values */
69static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
70static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
71static int arpt_down = 20; /* once declared down, don't send for 20 sec */
72
73SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
74 &arpt_prune, 0, "");
75SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
76 &arpt_keep, 0, "");
77SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
78 &arpt_down, 0, "");
79
80#define rt_expire rt_rmx.rmx_expire
81
82struct llinfo_arp {
83 LIST_ENTRY(llinfo_arp) la_le;
84 struct rtentry *la_rt;
85 struct mbuf *la_hold; /* last packet until resolved/timeout */
86 long la_asked; /* last time we QUERIED for this addr */
87#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
88};
89
90static LIST_HEAD(, llinfo_arp) llinfo_arp;
91
92struct ifqueue arpintrq = {0, 0, 0, 50};
93static int arp_inuse, arp_allocated;
94
95static int arp_maxtries = 5;
96static int useloopback = 1; /* use loopback interface for local traffic */
97static int arp_proxyall = 0;
98
99SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
100 &arp_maxtries, 0, "");
101SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
102 &useloopback, 0, "");
103SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
104 &arp_proxyall, 0, "");
105
106static void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
107static void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
108static void arpintr __P((void));
109static void arptfree __P((struct llinfo_arp *));
110static void arptimer __P((void *));
111static struct llinfo_arp
112 *arplookup __P((u_long, int, int));
113#ifdef INET
114static void in_arpinput __P((struct mbuf *));
115#endif
116
117/*
118 * Timeout routine. Age arp_tab entries periodically.
119 */
120/* ARGSUSED */
121static void
122arptimer(ignored_arg)
123 void *ignored_arg;
124{
125 int s = splnet();
126 register struct llinfo_arp *la = llinfo_arp.lh_first;
127 struct llinfo_arp *ola;
128
129 timeout(arptimer, (caddr_t)0, arpt_prune * hz);
130 while ((ola = la) != 0) {
131 register struct rtentry *rt = la->la_rt;
132 la = la->la_le.le_next;
133 if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
133 if (rt->rt_expire && rt->rt_expire <= time_second)
134 arptfree(ola); /* timer has expired, clear */
135 }
136 splx(s);
137}
138
139/*
140 * Parallel to llc_rtrequest.
141 */
142static void
143arp_rtrequest(req, rt, sa)
144 int req;
145 register struct rtentry *rt;
146 struct sockaddr *sa;
147{
148 register struct sockaddr *gate = rt->rt_gateway;
149 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
150 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
151 static int arpinit_done;
152
153 if (!arpinit_done) {
154 arpinit_done = 1;
155 LIST_INIT(&llinfo_arp);
156 timeout(arptimer, (caddr_t)0, hz);
157 }
158 if (rt->rt_flags & RTF_GATEWAY)
159 return;
160 switch (req) {
161
162 case RTM_ADD:
163 /*
164 * XXX: If this is a manually added route to interface
165 * such as older version of routed or gated might provide,
166 * restore cloning bit.
167 */
168 if ((rt->rt_flags & RTF_HOST) == 0 &&
169 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
170 rt->rt_flags |= RTF_CLONING;
171 if (rt->rt_flags & RTF_CLONING) {
172 /*
173 * Case 1: This route should come from a route to iface.
174 */
175 rt_setgate(rt, rt_key(rt),
176 (struct sockaddr *)&null_sdl);
177 gate = rt->rt_gateway;
178 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
179 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
134 arptfree(ola); /* timer has expired, clear */
135 }
136 splx(s);
137}
138
139/*
140 * Parallel to llc_rtrequest.
141 */
142static void
143arp_rtrequest(req, rt, sa)
144 int req;
145 register struct rtentry *rt;
146 struct sockaddr *sa;
147{
148 register struct sockaddr *gate = rt->rt_gateway;
149 register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
150 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
151 static int arpinit_done;
152
153 if (!arpinit_done) {
154 arpinit_done = 1;
155 LIST_INIT(&llinfo_arp);
156 timeout(arptimer, (caddr_t)0, hz);
157 }
158 if (rt->rt_flags & RTF_GATEWAY)
159 return;
160 switch (req) {
161
162 case RTM_ADD:
163 /*
164 * XXX: If this is a manually added route to interface
165 * such as older version of routed or gated might provide,
166 * restore cloning bit.
167 */
168 if ((rt->rt_flags & RTF_HOST) == 0 &&
169 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
170 rt->rt_flags |= RTF_CLONING;
171 if (rt->rt_flags & RTF_CLONING) {
172 /*
173 * Case 1: This route should come from a route to iface.
174 */
175 rt_setgate(rt, rt_key(rt),
176 (struct sockaddr *)&null_sdl);
177 gate = rt->rt_gateway;
178 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
179 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
180 rt->rt_expire = time.tv_sec;
180 rt->rt_expire = time_second;
181 break;
182 }
183 /* Announce a new entry if requested. */
184 if (rt->rt_flags & RTF_ANNOUNCE)
185 arprequest((struct arpcom *)rt->rt_ifp,
186 &SIN(rt_key(rt))->sin_addr.s_addr,
187 &SIN(rt_key(rt))->sin_addr.s_addr,
188 (u_char *)LLADDR(SDL(gate)));
189 /*FALLTHROUGH*/
190 case RTM_RESOLVE:
191 if (gate->sa_family != AF_LINK ||
192 gate->sa_len < sizeof(null_sdl)) {
193 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
194 break;
195 }
196 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
197 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
198 if (la != 0)
199 break; /* This happens on a route change */
200 /*
201 * Case 2: This route may come from cloning, or a manual route
202 * add with a LL address.
203 */
204 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
205 rt->rt_llinfo = (caddr_t)la;
206 if (la == 0) {
207 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
208 break;
209 }
210 arp_inuse++, arp_allocated++;
211 Bzero(la, sizeof(*la));
212 la->la_rt = rt;
213 rt->rt_flags |= RTF_LLINFO;
214 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
215
216#ifdef INET
217 /*
218 * This keeps the multicast addresses from showing up
219 * in `arp -a' listings as unresolved. It's not actually
220 * functional. Then the same for broadcast.
221 */
222 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
223 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
224 LLADDR(SDL(gate)));
225 SDL(gate)->sdl_alen = 6;
226 rt->rt_expire = 0;
227 }
228 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
229 memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
230 SDL(gate)->sdl_alen = 6;
231 rt->rt_expire = 0;
232 }
233#endif
234
235 if (SIN(rt_key(rt))->sin_addr.s_addr ==
236 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
237 /*
238 * This test used to be
239 * if (loif.if_flags & IFF_UP)
240 * It allowed local traffic to be forced
241 * through the hardware by configuring the loopback down.
242 * However, it causes problems during network configuration
243 * for boards that can't receive packets they send.
244 * It is now necessary to clear "useloopback" and remove
245 * the route to force traffic out to the hardware.
246 */
247 rt->rt_expire = 0;
248 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
249 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
250 if (useloopback)
251 rt->rt_ifp = loif;
252
253 }
254 break;
255
256 case RTM_DELETE:
257 if (la == 0)
258 break;
259 arp_inuse--;
260 LIST_REMOVE(la, la_le);
261 rt->rt_llinfo = 0;
262 rt->rt_flags &= ~RTF_LLINFO;
263 if (la->la_hold)
264 m_freem(la->la_hold);
265 Free((caddr_t)la);
266 }
267}
268
269/*
270 * Broadcast an ARP request. Caller specifies:
271 * - arp header source ip address
272 * - arp header target ip address
273 * - arp header source ethernet address
274 */
275static void
276arprequest(ac, sip, tip, enaddr)
277 register struct arpcom *ac;
278 register u_long *sip, *tip;
279 register u_char *enaddr;
280{
281 register struct mbuf *m;
282 register struct ether_header *eh;
283 register struct ether_arp *ea;
284 struct sockaddr sa;
285
286 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
287 return;
288 m->m_len = sizeof(*ea);
289 m->m_pkthdr.len = sizeof(*ea);
290 MH_ALIGN(m, sizeof(*ea));
291 ea = mtod(m, struct ether_arp *);
292 eh = (struct ether_header *)sa.sa_data;
293 bzero((caddr_t)ea, sizeof (*ea));
294 (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
295 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */
296 ea->arp_hrd = htons(ARPHRD_ETHER);
297 ea->arp_pro = htons(ETHERTYPE_IP);
298 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */
299 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */
300 ea->arp_op = htons(ARPOP_REQUEST);
301 (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
302 (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
303 (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
304 sa.sa_family = AF_UNSPEC;
305 sa.sa_len = sizeof(sa);
306 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
307}
308
309/*
310 * Resolve an IP address into an ethernet address. If success,
311 * desten is filled in. If there is no entry in arptab,
312 * set one up and broadcast a request for the IP address.
313 * Hold onto this mbuf and resend it once the address
314 * is finally resolved. A return value of 1 indicates
315 * that desten has been filled in and the packet should be sent
316 * normally; a 0 return indicates that the packet has been
317 * taken over here, either now or for later transmission.
318 */
319int
320arpresolve(ac, rt, m, dst, desten, rt0)
321 register struct arpcom *ac;
322 register struct rtentry *rt;
323 struct mbuf *m;
324 register struct sockaddr *dst;
325 register u_char *desten;
326 struct rtentry *rt0;
327{
328 register struct llinfo_arp *la;
329 struct sockaddr_dl *sdl;
330
331 if (m->m_flags & M_BCAST) { /* broadcast */
332 (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
333 return (1);
334 }
335 if (m->m_flags & M_MCAST) { /* multicast */
336 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
337 return(1);
338 }
339 if (rt)
340 la = (struct llinfo_arp *)rt->rt_llinfo;
341 else {
342 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
343 if (la)
344 rt = la->la_rt;
345 }
346 if (la == 0 || rt == 0) {
347 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s\n",
348 inet_ntoa(SIN(dst)->sin_addr));
349 m_freem(m);
350 return (0);
351 }
352 sdl = SDL(rt->rt_gateway);
353 /*
354 * Check the address family and length is valid, the address
355 * is resolved; otherwise, try to resolve.
356 */
181 break;
182 }
183 /* Announce a new entry if requested. */
184 if (rt->rt_flags & RTF_ANNOUNCE)
185 arprequest((struct arpcom *)rt->rt_ifp,
186 &SIN(rt_key(rt))->sin_addr.s_addr,
187 &SIN(rt_key(rt))->sin_addr.s_addr,
188 (u_char *)LLADDR(SDL(gate)));
189 /*FALLTHROUGH*/
190 case RTM_RESOLVE:
191 if (gate->sa_family != AF_LINK ||
192 gate->sa_len < sizeof(null_sdl)) {
193 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
194 break;
195 }
196 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
197 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
198 if (la != 0)
199 break; /* This happens on a route change */
200 /*
201 * Case 2: This route may come from cloning, or a manual route
202 * add with a LL address.
203 */
204 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
205 rt->rt_llinfo = (caddr_t)la;
206 if (la == 0) {
207 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
208 break;
209 }
210 arp_inuse++, arp_allocated++;
211 Bzero(la, sizeof(*la));
212 la->la_rt = rt;
213 rt->rt_flags |= RTF_LLINFO;
214 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
215
216#ifdef INET
217 /*
218 * This keeps the multicast addresses from showing up
219 * in `arp -a' listings as unresolved. It's not actually
220 * functional. Then the same for broadcast.
221 */
222 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
223 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
224 LLADDR(SDL(gate)));
225 SDL(gate)->sdl_alen = 6;
226 rt->rt_expire = 0;
227 }
228 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
229 memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
230 SDL(gate)->sdl_alen = 6;
231 rt->rt_expire = 0;
232 }
233#endif
234
235 if (SIN(rt_key(rt))->sin_addr.s_addr ==
236 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
237 /*
238 * This test used to be
239 * if (loif.if_flags & IFF_UP)
240 * It allowed local traffic to be forced
241 * through the hardware by configuring the loopback down.
242 * However, it causes problems during network configuration
243 * for boards that can't receive packets they send.
244 * It is now necessary to clear "useloopback" and remove
245 * the route to force traffic out to the hardware.
246 */
247 rt->rt_expire = 0;
248 Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
249 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
250 if (useloopback)
251 rt->rt_ifp = loif;
252
253 }
254 break;
255
256 case RTM_DELETE:
257 if (la == 0)
258 break;
259 arp_inuse--;
260 LIST_REMOVE(la, la_le);
261 rt->rt_llinfo = 0;
262 rt->rt_flags &= ~RTF_LLINFO;
263 if (la->la_hold)
264 m_freem(la->la_hold);
265 Free((caddr_t)la);
266 }
267}
268
269/*
270 * Broadcast an ARP request. Caller specifies:
271 * - arp header source ip address
272 * - arp header target ip address
273 * - arp header source ethernet address
274 */
275static void
276arprequest(ac, sip, tip, enaddr)
277 register struct arpcom *ac;
278 register u_long *sip, *tip;
279 register u_char *enaddr;
280{
281 register struct mbuf *m;
282 register struct ether_header *eh;
283 register struct ether_arp *ea;
284 struct sockaddr sa;
285
286 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
287 return;
288 m->m_len = sizeof(*ea);
289 m->m_pkthdr.len = sizeof(*ea);
290 MH_ALIGN(m, sizeof(*ea));
291 ea = mtod(m, struct ether_arp *);
292 eh = (struct ether_header *)sa.sa_data;
293 bzero((caddr_t)ea, sizeof (*ea));
294 (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
295 eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */
296 ea->arp_hrd = htons(ARPHRD_ETHER);
297 ea->arp_pro = htons(ETHERTYPE_IP);
298 ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */
299 ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */
300 ea->arp_op = htons(ARPOP_REQUEST);
301 (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
302 (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
303 (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
304 sa.sa_family = AF_UNSPEC;
305 sa.sa_len = sizeof(sa);
306 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
307}
308
309/*
310 * Resolve an IP address into an ethernet address. If success,
311 * desten is filled in. If there is no entry in arptab,
312 * set one up and broadcast a request for the IP address.
313 * Hold onto this mbuf and resend it once the address
314 * is finally resolved. A return value of 1 indicates
315 * that desten has been filled in and the packet should be sent
316 * normally; a 0 return indicates that the packet has been
317 * taken over here, either now or for later transmission.
318 */
319int
320arpresolve(ac, rt, m, dst, desten, rt0)
321 register struct arpcom *ac;
322 register struct rtentry *rt;
323 struct mbuf *m;
324 register struct sockaddr *dst;
325 register u_char *desten;
326 struct rtentry *rt0;
327{
328 register struct llinfo_arp *la;
329 struct sockaddr_dl *sdl;
330
331 if (m->m_flags & M_BCAST) { /* broadcast */
332 (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
333 return (1);
334 }
335 if (m->m_flags & M_MCAST) { /* multicast */
336 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
337 return(1);
338 }
339 if (rt)
340 la = (struct llinfo_arp *)rt->rt_llinfo;
341 else {
342 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
343 if (la)
344 rt = la->la_rt;
345 }
346 if (la == 0 || rt == 0) {
347 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s\n",
348 inet_ntoa(SIN(dst)->sin_addr));
349 m_freem(m);
350 return (0);
351 }
352 sdl = SDL(rt->rt_gateway);
353 /*
354 * Check the address family and length is valid, the address
355 * is resolved; otherwise, try to resolve.
356 */
357 if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
357 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
358 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
359 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
360 return 1;
361 }
362 /*
363 * There is an arptab entry, but no ethernet address
364 * response yet. Replace the held mbuf with this
365 * latest one.
366 */
367 if (la->la_hold)
368 m_freem(la->la_hold);
369 la->la_hold = m;
370 if (rt->rt_expire) {
371 rt->rt_flags &= ~RTF_REJECT;
358 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
359 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
360 return 1;
361 }
362 /*
363 * There is an arptab entry, but no ethernet address
364 * response yet. Replace the held mbuf with this
365 * latest one.
366 */
367 if (la->la_hold)
368 m_freem(la->la_hold);
369 la->la_hold = m;
370 if (rt->rt_expire) {
371 rt->rt_flags &= ~RTF_REJECT;
372 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
373 rt->rt_expire = time.tv_sec;
372 if (la->la_asked == 0 || rt->rt_expire != time_second) {
373 rt->rt_expire = time_second;
374 if (la->la_asked++ < arp_maxtries)
375 arprequest(ac,
376 &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr),
377 &(SIN(dst)->sin_addr.s_addr),
378 ac->ac_enaddr);
379 else {
380 rt->rt_flags |= RTF_REJECT;
381 rt->rt_expire += arpt_down;
382 la->la_asked = 0;
383 }
384
385 }
386 }
387 return (0);
388}
389
390/*
391 * Common length and type checks are done here,
392 * then the protocol-specific routine is called.
393 */
394static void
395arpintr()
396{
397 register struct mbuf *m;
398 register struct arphdr *ar;
399 int s;
400
401 while (arpintrq.ifq_head) {
402 s = splimp();
403 IF_DEQUEUE(&arpintrq, m);
404 splx(s);
405 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
406 panic("arpintr");
407 if (m->m_len >= sizeof(struct arphdr) &&
408 (ar = mtod(m, struct arphdr *)) &&
409 ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
410 m->m_len >=
411 sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
412
413 switch (ntohs(ar->ar_pro)) {
414
415#ifdef INET
416 case ETHERTYPE_IP:
417 in_arpinput(m);
418 continue;
419#endif
420 }
421 m_freem(m);
422 }
423}
424
425NETISR_SET(NETISR_ARP, arpintr);
426
427
428#ifdef INET
429/*
430 * ARP for Internet protocols on 10 Mb/s Ethernet.
431 * Algorithm is that given in RFC 826.
432 * In addition, a sanity check is performed on the sender
433 * protocol address, to catch impersonators.
434 * We no longer handle negotiations for use of trailer protocol:
435 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
436 * along with IP replies if we wanted trailers sent to us,
437 * and also sent them in response to IP replies.
438 * This allowed either end to announce the desire to receive
439 * trailer packets.
440 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
441 * but formerly didn't normally send requests.
442 */
443static void
444in_arpinput(m)
445 struct mbuf *m;
446{
447 register struct ether_arp *ea;
448 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
449 struct ether_header *eh;
450 register struct llinfo_arp *la = 0;
451 register struct rtentry *rt;
452 struct in_ifaddr *ia, *maybe_ia = 0;
453 struct sockaddr_dl *sdl;
454 struct sockaddr sa;
455 struct in_addr isaddr, itaddr, myaddr;
456 int op;
457
458 ea = mtod(m, struct ether_arp *);
459 op = ntohs(ea->arp_op);
460 (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
461 (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
462 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
463 if (ia->ia_ifp == &ac->ac_if) {
464 maybe_ia = ia;
465 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
466 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
467 break;
468 }
469 if (maybe_ia == 0) {
470 m_freem(m);
471 return;
472 }
473 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
474 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
475 sizeof (ea->arp_sha))) {
476 m_freem(m); /* it's from me, ignore it. */
477 return;
478 }
479 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
480 sizeof (ea->arp_sha))) {
481 log(LOG_ERR,
482 "arp: ether address is broadcast for IP address %s!\n",
483 inet_ntoa(isaddr));
484 m_freem(m);
485 return;
486 }
487 if (isaddr.s_addr == myaddr.s_addr) {
488 log(LOG_ERR,
489 "arp: %6D is using my IP address %s!\n",
490 ea->arp_sha, ":", inet_ntoa(isaddr));
491 itaddr = myaddr;
492 goto reply;
493 }
494 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
495 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
496 if (sdl->sdl_alen &&
497 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
498 log(LOG_INFO, "arp: %s moved from %6D to %6D\n",
499 inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
500 ea->arp_sha, ":");
501 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
502 sdl->sdl_alen = sizeof(ea->arp_sha);
503 if (rt->rt_expire)
374 if (la->la_asked++ < arp_maxtries)
375 arprequest(ac,
376 &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr),
377 &(SIN(dst)->sin_addr.s_addr),
378 ac->ac_enaddr);
379 else {
380 rt->rt_flags |= RTF_REJECT;
381 rt->rt_expire += arpt_down;
382 la->la_asked = 0;
383 }
384
385 }
386 }
387 return (0);
388}
389
390/*
391 * Common length and type checks are done here,
392 * then the protocol-specific routine is called.
393 */
394static void
395arpintr()
396{
397 register struct mbuf *m;
398 register struct arphdr *ar;
399 int s;
400
401 while (arpintrq.ifq_head) {
402 s = splimp();
403 IF_DEQUEUE(&arpintrq, m);
404 splx(s);
405 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
406 panic("arpintr");
407 if (m->m_len >= sizeof(struct arphdr) &&
408 (ar = mtod(m, struct arphdr *)) &&
409 ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
410 m->m_len >=
411 sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
412
413 switch (ntohs(ar->ar_pro)) {
414
415#ifdef INET
416 case ETHERTYPE_IP:
417 in_arpinput(m);
418 continue;
419#endif
420 }
421 m_freem(m);
422 }
423}
424
425NETISR_SET(NETISR_ARP, arpintr);
426
427
428#ifdef INET
429/*
430 * ARP for Internet protocols on 10 Mb/s Ethernet.
431 * Algorithm is that given in RFC 826.
432 * In addition, a sanity check is performed on the sender
433 * protocol address, to catch impersonators.
434 * We no longer handle negotiations for use of trailer protocol:
435 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
436 * along with IP replies if we wanted trailers sent to us,
437 * and also sent them in response to IP replies.
438 * This allowed either end to announce the desire to receive
439 * trailer packets.
440 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
441 * but formerly didn't normally send requests.
442 */
443static void
444in_arpinput(m)
445 struct mbuf *m;
446{
447 register struct ether_arp *ea;
448 register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
449 struct ether_header *eh;
450 register struct llinfo_arp *la = 0;
451 register struct rtentry *rt;
452 struct in_ifaddr *ia, *maybe_ia = 0;
453 struct sockaddr_dl *sdl;
454 struct sockaddr sa;
455 struct in_addr isaddr, itaddr, myaddr;
456 int op;
457
458 ea = mtod(m, struct ether_arp *);
459 op = ntohs(ea->arp_op);
460 (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
461 (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
462 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
463 if (ia->ia_ifp == &ac->ac_if) {
464 maybe_ia = ia;
465 if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
466 (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
467 break;
468 }
469 if (maybe_ia == 0) {
470 m_freem(m);
471 return;
472 }
473 myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
474 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
475 sizeof (ea->arp_sha))) {
476 m_freem(m); /* it's from me, ignore it. */
477 return;
478 }
479 if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
480 sizeof (ea->arp_sha))) {
481 log(LOG_ERR,
482 "arp: ether address is broadcast for IP address %s!\n",
483 inet_ntoa(isaddr));
484 m_freem(m);
485 return;
486 }
487 if (isaddr.s_addr == myaddr.s_addr) {
488 log(LOG_ERR,
489 "arp: %6D is using my IP address %s!\n",
490 ea->arp_sha, ":", inet_ntoa(isaddr));
491 itaddr = myaddr;
492 goto reply;
493 }
494 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
495 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
496 if (sdl->sdl_alen &&
497 bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
498 log(LOG_INFO, "arp: %s moved from %6D to %6D\n",
499 inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
500 ea->arp_sha, ":");
501 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
502 sdl->sdl_alen = sizeof(ea->arp_sha);
503 if (rt->rt_expire)
504 rt->rt_expire = time.tv_sec + arpt_keep;
504 rt->rt_expire = time_second + arpt_keep;
505 rt->rt_flags &= ~RTF_REJECT;
506 la->la_asked = 0;
507 if (la->la_hold) {
508 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
509 rt_key(rt), rt);
510 la->la_hold = 0;
511 }
512 }
513reply:
514 if (op != ARPOP_REQUEST) {
515 m_freem(m);
516 return;
517 }
518 if (itaddr.s_addr == myaddr.s_addr) {
519 /* I am the target */
520 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
521 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
522 } else {
523 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
524 if (la == NULL) {
525 struct sockaddr_in sin;
526
527 if (!arp_proxyall) {
528 m_freem(m);
529 return;
530 }
531
532 bzero(&sin, sizeof sin);
533 sin.sin_family = AF_INET;
534 sin.sin_len = sizeof sin;
535 sin.sin_addr = itaddr;
536
537 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
538 if (!rt) {
539 m_freem(m);
540 return;
541 }
542 /*
543 * Don't send proxies for nodes on the same interface
544 * as this one came out of, or we'll get into a fight
545 * over who claims what Ether address.
546 */
547 if (rt->rt_ifp == &ac->ac_if) {
548 rtfree(rt);
549 m_freem(m);
550 return;
551 }
552 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
553 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
554 rtfree(rt);
555#ifdef DEBUG_PROXY
556 printf("arp: proxying for %s\n",
557 inet_ntoa(itaddr));
558#endif
559 } else {
560 rt = la->la_rt;
561 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
562 sdl = SDL(rt->rt_gateway);
563 (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
564 }
565 }
566
567 (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
568 (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
569 ea->arp_op = htons(ARPOP_REPLY);
570 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
571 eh = (struct ether_header *)sa.sa_data;
572 (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
573 eh->ether_type = htons(ETHERTYPE_ARP);
574 sa.sa_family = AF_UNSPEC;
575 sa.sa_len = sizeof(sa);
576 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
577 return;
578}
579#endif
580
581/*
582 * Free an arp entry.
583 */
584static void
585arptfree(la)
586 register struct llinfo_arp *la;
587{
588 register struct rtentry *rt = la->la_rt;
589 register struct sockaddr_dl *sdl;
590 if (rt == 0)
591 panic("arptfree");
592 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
593 sdl->sdl_family == AF_LINK) {
594 sdl->sdl_alen = 0;
595 la->la_asked = 0;
596 rt->rt_flags &= ~RTF_REJECT;
597 return;
598 }
599 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
600 0, (struct rtentry **)0);
601}
602/*
603 * Lookup or enter a new address in arptab.
604 */
605static struct llinfo_arp *
606arplookup(addr, create, proxy)
607 u_long addr;
608 int create, proxy;
609{
610 register struct rtentry *rt;
611 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
612 const char *why = 0;
613
614 sin.sin_addr.s_addr = addr;
615 sin.sin_other = proxy ? SIN_PROXY : 0;
616 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
617 if (rt == 0)
618 return (0);
619 rt->rt_refcnt--;
620
621 if (rt->rt_flags & RTF_GATEWAY)
622 why = "host is not on local network";
623 else if ((rt->rt_flags & RTF_LLINFO) == 0)
624 why = "could not allocate llinfo";
625 else if (rt->rt_gateway->sa_family != AF_LINK)
626 why = "gateway route is not ours";
627
628 if (why && create) {
629 log(LOG_DEBUG, "arplookup %s failed: %s\n",
630 inet_ntoa(sin.sin_addr), why);
631 return 0;
632 } else if (why) {
633 return 0;
634 }
635 return ((struct llinfo_arp *)rt->rt_llinfo);
636}
637
638void
639arp_ifinit(ac, ifa)
640 struct arpcom *ac;
641 struct ifaddr *ifa;
642{
643 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
644 arprequest(ac, &(IA_SIN(ifa)->sin_addr.s_addr),
645 &(IA_SIN(ifa)->sin_addr.s_addr), ac->ac_enaddr);
646 ifa->ifa_rtrequest = arp_rtrequest;
647 ifa->ifa_flags |= RTF_CLONING;
648}
505 rt->rt_flags &= ~RTF_REJECT;
506 la->la_asked = 0;
507 if (la->la_hold) {
508 (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
509 rt_key(rt), rt);
510 la->la_hold = 0;
511 }
512 }
513reply:
514 if (op != ARPOP_REQUEST) {
515 m_freem(m);
516 return;
517 }
518 if (itaddr.s_addr == myaddr.s_addr) {
519 /* I am the target */
520 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
521 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
522 } else {
523 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
524 if (la == NULL) {
525 struct sockaddr_in sin;
526
527 if (!arp_proxyall) {
528 m_freem(m);
529 return;
530 }
531
532 bzero(&sin, sizeof sin);
533 sin.sin_family = AF_INET;
534 sin.sin_len = sizeof sin;
535 sin.sin_addr = itaddr;
536
537 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
538 if (!rt) {
539 m_freem(m);
540 return;
541 }
542 /*
543 * Don't send proxies for nodes on the same interface
544 * as this one came out of, or we'll get into a fight
545 * over who claims what Ether address.
546 */
547 if (rt->rt_ifp == &ac->ac_if) {
548 rtfree(rt);
549 m_freem(m);
550 return;
551 }
552 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
553 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
554 rtfree(rt);
555#ifdef DEBUG_PROXY
556 printf("arp: proxying for %s\n",
557 inet_ntoa(itaddr));
558#endif
559 } else {
560 rt = la->la_rt;
561 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
562 sdl = SDL(rt->rt_gateway);
563 (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
564 }
565 }
566
567 (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
568 (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
569 ea->arp_op = htons(ARPOP_REPLY);
570 ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
571 eh = (struct ether_header *)sa.sa_data;
572 (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
573 eh->ether_type = htons(ETHERTYPE_ARP);
574 sa.sa_family = AF_UNSPEC;
575 sa.sa_len = sizeof(sa);
576 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
577 return;
578}
579#endif
580
581/*
582 * Free an arp entry.
583 */
584static void
585arptfree(la)
586 register struct llinfo_arp *la;
587{
588 register struct rtentry *rt = la->la_rt;
589 register struct sockaddr_dl *sdl;
590 if (rt == 0)
591 panic("arptfree");
592 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
593 sdl->sdl_family == AF_LINK) {
594 sdl->sdl_alen = 0;
595 la->la_asked = 0;
596 rt->rt_flags &= ~RTF_REJECT;
597 return;
598 }
599 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
600 0, (struct rtentry **)0);
601}
602/*
603 * Lookup or enter a new address in arptab.
604 */
605static struct llinfo_arp *
606arplookup(addr, create, proxy)
607 u_long addr;
608 int create, proxy;
609{
610 register struct rtentry *rt;
611 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
612 const char *why = 0;
613
614 sin.sin_addr.s_addr = addr;
615 sin.sin_other = proxy ? SIN_PROXY : 0;
616 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
617 if (rt == 0)
618 return (0);
619 rt->rt_refcnt--;
620
621 if (rt->rt_flags & RTF_GATEWAY)
622 why = "host is not on local network";
623 else if ((rt->rt_flags & RTF_LLINFO) == 0)
624 why = "could not allocate llinfo";
625 else if (rt->rt_gateway->sa_family != AF_LINK)
626 why = "gateway route is not ours";
627
628 if (why && create) {
629 log(LOG_DEBUG, "arplookup %s failed: %s\n",
630 inet_ntoa(sin.sin_addr), why);
631 return 0;
632 } else if (why) {
633 return 0;
634 }
635 return ((struct llinfo_arp *)rt->rt_llinfo);
636}
637
638void
639arp_ifinit(ac, ifa)
640 struct arpcom *ac;
641 struct ifaddr *ifa;
642{
643 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
644 arprequest(ac, &(IA_SIN(ifa)->sin_addr.s_addr),
645 &(IA_SIN(ifa)->sin_addr.s_addr), ac->ac_enaddr);
646 ifa->ifa_rtrequest = arp_rtrequest;
647 ifa->ifa_flags |= RTF_CLONING;
648}