Tue Oct 13 11:13:37 2015 UTC ()
Move the NOARP check up a bit so that it works when an la is created
but hasn't been resolved yet.
Fixes PR kern/17611.


(roy)
diff -r1.185 -r1.186 src/sys/netinet/if_arp.c

cvs diff -r1.185 -r1.186 src/sys/netinet/if_arp.c (switch to unified diff)

--- src/sys/netinet/if_arp.c 2015/10/13 09:33:35 1.185
+++ src/sys/netinet/if_arp.c 2015/10/13 11:13:37 1.186
@@ -1,1841 +1,1843 @@ @@ -1,1841 +1,1843 @@
1/* $NetBSD: if_arp.c,v 1.185 2015/10/13 09:33:35 roy Exp $ */ 1/* $NetBSD: if_arp.c,v 1.186 2015/10/13 11:13:37 roy Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under 8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * Copyright (c) 1982, 1986, 1988, 1993 34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved. 35 * The Regents of the University of California. All rights reserved.
36 * 36 *
37 * Redistribution and use in source and binary forms, with or without 37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions 38 * modification, are permitted provided that the following conditions
39 * are met: 39 * are met:
40 * 1. Redistributions of source code must retain the above copyright 40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer. 41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright 42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the 43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution. 44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors 45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software 46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission. 47 * without specific prior written permission.
48 * 48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE. 59 * SUCH DAMAGE.
60 * 60 *
61 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94 61 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94
62 */ 62 */
63 63
64/* 64/*
65 * Ethernet address resolution protocol. 65 * Ethernet address resolution protocol.
66 * TODO: 66 * TODO:
67 * add "inuse/lock" bit (or ref. count) along with valid bit 67 * add "inuse/lock" bit (or ref. count) along with valid bit
68 */ 68 */
69 69
70#include <sys/cdefs.h> 70#include <sys/cdefs.h>
71__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.185 2015/10/13 09:33:35 roy Exp $"); 71__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.186 2015/10/13 11:13:37 roy Exp $");
72 72
73#ifdef _KERNEL_OPT 73#ifdef _KERNEL_OPT
74#include "opt_ddb.h" 74#include "opt_ddb.h"
75#include "opt_inet.h" 75#include "opt_inet.h"
76#endif 76#endif
77 77
78#ifdef INET 78#ifdef INET
79 79
80#include "bridge.h" 80#include "bridge.h"
81 81
82#include <sys/param.h> 82#include <sys/param.h>
83#include <sys/systm.h> 83#include <sys/systm.h>
84#include <sys/callout.h> 84#include <sys/callout.h>
85#include <sys/malloc.h> 85#include <sys/malloc.h>
86#include <sys/mbuf.h> 86#include <sys/mbuf.h>
87#include <sys/socket.h> 87#include <sys/socket.h>
88#include <sys/time.h> 88#include <sys/time.h>
89#include <sys/timetc.h> 89#include <sys/timetc.h>
90#include <sys/kernel.h> 90#include <sys/kernel.h>
91#include <sys/errno.h> 91#include <sys/errno.h>
92#include <sys/ioctl.h> 92#include <sys/ioctl.h>
93#include <sys/syslog.h> 93#include <sys/syslog.h>
94#include <sys/proc.h> 94#include <sys/proc.h>
95#include <sys/protosw.h> 95#include <sys/protosw.h>
96#include <sys/domain.h> 96#include <sys/domain.h>
97#include <sys/sysctl.h> 97#include <sys/sysctl.h>
98#include <sys/socketvar.h> 98#include <sys/socketvar.h>
99#include <sys/percpu.h> 99#include <sys/percpu.h>
100#include <sys/cprng.h> 100#include <sys/cprng.h>
101#include <sys/kmem.h> 101#include <sys/kmem.h>
102 102
103#include <net/ethertypes.h> 103#include <net/ethertypes.h>
104#include <net/if.h> 104#include <net/if.h>
105#include <net/if_dl.h> 105#include <net/if_dl.h>
106#include <net/if_token.h> 106#include <net/if_token.h>
107#include <net/if_types.h> 107#include <net/if_types.h>
108#include <net/if_ether.h> 108#include <net/if_ether.h>
109#include <net/if_llatbl.h> 109#include <net/if_llatbl.h>
110#include <net/net_osdep.h> 110#include <net/net_osdep.h>
111#include <net/route.h> 111#include <net/route.h>
112#include <net/net_stats.h> 112#include <net/net_stats.h>
113 113
114#include <netinet/in.h> 114#include <netinet/in.h>
115#include <netinet/in_systm.h> 115#include <netinet/in_systm.h>
116#include <netinet/in_var.h> 116#include <netinet/in_var.h>
117#include <netinet/ip.h> 117#include <netinet/ip.h>
118#include <netinet/if_inarp.h> 118#include <netinet/if_inarp.h>
119 119
120#include "arcnet.h" 120#include "arcnet.h"
121#if NARCNET > 0 121#if NARCNET > 0
122#include <net/if_arc.h> 122#include <net/if_arc.h>
123#endif 123#endif
124#include "fddi.h" 124#include "fddi.h"
125#if NFDDI > 0 125#if NFDDI > 0
126#include <net/if_fddi.h> 126#include <net/if_fddi.h>
127#endif 127#endif
128#include "token.h" 128#include "token.h"
129#include "carp.h" 129#include "carp.h"
130#if NCARP > 0 130#if NCARP > 0
131#include <netinet/ip_carp.h> 131#include <netinet/ip_carp.h>
132#endif 132#endif
133 133
134#define SIN(s) ((struct sockaddr_in *)s) 134#define SIN(s) ((struct sockaddr_in *)s)
135#define SRP(s) ((struct sockaddr_inarp *)s) 135#define SRP(s) ((struct sockaddr_inarp *)s)
136 136
137/* 137/*
138 * ARP trailer negotiation. Trailer protocol is not IP specific, 138 * ARP trailer negotiation. Trailer protocol is not IP specific,
139 * but ARP request/response use IP addresses. 139 * but ARP request/response use IP addresses.
140 */ 140 */
141#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 141#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
142 142
143/* timer values */ 143/* timer values */
144static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 144static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
145static int arpt_down = 20; /* once declared down, don't send for 20 secs */ 145static int arpt_down = 20; /* once declared down, don't send for 20 secs */
146static int arp_maxhold = 1; /* number of packets to hold per ARP entry */ 146static int arp_maxhold = 1; /* number of packets to hold per ARP entry */
147#define rt_expire rt_rmx.rmx_expire 147#define rt_expire rt_rmx.rmx_expire
148#define rt_pksent rt_rmx.rmx_pksent 148#define rt_pksent rt_rmx.rmx_pksent
149 149
150int ip_dad_count = PROBE_NUM; 150int ip_dad_count = PROBE_NUM;
151#ifdef ARP_DEBUG 151#ifdef ARP_DEBUG
152static int arp_debug = 1; 152static int arp_debug = 1;
153#else 153#else
154static int arp_debug = 0; 154static int arp_debug = 0;
155#endif 155#endif
156#define arplog(x) do { if (arp_debug) log x; } while (/*CONSTCOND*/ 0) 156#define arplog(x) do { if (arp_debug) log x; } while (/*CONSTCOND*/ 0)
157 157
158static void arp_init(void); 158static void arp_init(void);
159 159
160static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *, 160static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *,
161 const struct sockaddr *); 161 const struct sockaddr *);
162static void arptfree(struct rtentry *); 162static void arptfree(struct rtentry *);
163static void arptimer(void *); 163static void arptimer(void *);
164static struct llentry *arplookup(struct ifnet *, struct mbuf *, 164static struct llentry *arplookup(struct ifnet *, struct mbuf *,
165 const struct in_addr *, int, int, int, struct rtentry *); 165 const struct in_addr *, int, int, int, struct rtentry *);
166static void in_arpinput(struct mbuf *); 166static void in_arpinput(struct mbuf *);
167static void in_revarpinput(struct mbuf *); 167static void in_revarpinput(struct mbuf *);
168static void revarprequest(struct ifnet *); 168static void revarprequest(struct ifnet *);
169 169
170static void arp_drainstub(void); 170static void arp_drainstub(void);
171 171
172static void arp_dad_timer(struct ifaddr *); 172static void arp_dad_timer(struct ifaddr *);
173static void arp_dad_start(struct ifaddr *); 173static void arp_dad_start(struct ifaddr *);
174static void arp_dad_stop(struct ifaddr *); 174static void arp_dad_stop(struct ifaddr *);
175static void arp_dad_duplicated(struct ifaddr *); 175static void arp_dad_duplicated(struct ifaddr *);
176 176
177struct ifqueue arpintrq = { 177struct ifqueue arpintrq = {
178 .ifq_head = NULL, 178 .ifq_head = NULL,
179 .ifq_tail = NULL, 179 .ifq_tail = NULL,
180 .ifq_len = 0, 180 .ifq_len = 0,
181 .ifq_maxlen = 50, 181 .ifq_maxlen = 50,
182 .ifq_drops = 0, 182 .ifq_drops = 0,
183}; 183};
184static int arp_inuse, arp_allocated; 184static int arp_inuse, arp_allocated;
185static int arp_maxtries = 5; 185static int arp_maxtries = 5;
186static int useloopback = 1; /* use loopback interface for local traffic */ 186static int useloopback = 1; /* use loopback interface for local traffic */
187 187
188static percpu_t *arpstat_percpu; 188static percpu_t *arpstat_percpu;
189 189
190#define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu) 190#define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu)
191#define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu) 191#define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu)
192 192
193#define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x) 193#define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x)
194#define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v) 194#define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v)
195 195
196/* revarp state */ 196/* revarp state */
197static struct in_addr myip, srv_ip; 197static struct in_addr myip, srv_ip;
198static int myip_initialized = 0; 198static int myip_initialized = 0;
199static int revarp_in_progress = 0; 199static int revarp_in_progress = 0;
200static struct ifnet *myip_ifp = NULL; 200static struct ifnet *myip_ifp = NULL;
201 201
202#ifdef DDB 202#ifdef DDB
203static void db_print_sa(const struct sockaddr *); 203static void db_print_sa(const struct sockaddr *);
204static void db_print_ifa(struct ifaddr *); 204static void db_print_ifa(struct ifaddr *);
205static void db_print_llinfo(void *); 205static void db_print_llinfo(void *);
206static int db_show_rtentry(struct rtentry *, void *); 206static int db_show_rtentry(struct rtentry *, void *);
207#endif 207#endif
208 208
209static int arp_drainwanted; 209static int arp_drainwanted;
210 210
211static int log_movements = 1; 211static int log_movements = 1;
212static int log_permanent_modify = 1; 212static int log_permanent_modify = 1;
213static int log_wrong_iface = 1; 213static int log_wrong_iface = 1;
214static int log_unknown_network = 1; 214static int log_unknown_network = 1;
215 215
216/* 216/*
217 * this should be elsewhere. 217 * this should be elsewhere.
218 */ 218 */
219 219
220static char * 220static char *
221lla_snprintf(u_int8_t *, int); 221lla_snprintf(u_int8_t *, int);
222 222
223static char * 223static char *
224lla_snprintf(u_int8_t *adrp, int len) 224lla_snprintf(u_int8_t *adrp, int len)
225{ 225{
226#define NUMBUFS 3 226#define NUMBUFS 3
227 static char buf[NUMBUFS][16*3]; 227 static char buf[NUMBUFS][16*3];
228 static int bnum = 0; 228 static int bnum = 0;
229 229
230 int i; 230 int i;
231 char *p; 231 char *p;
232 232
233 p = buf[bnum]; 233 p = buf[bnum];
234 234
235 *p++ = hexdigits[(*adrp)>>4]; 235 *p++ = hexdigits[(*adrp)>>4];
236 *p++ = hexdigits[(*adrp++)&0xf]; 236 *p++ = hexdigits[(*adrp++)&0xf];
237 237
238 for (i=1; i<len && i<16; i++) { 238 for (i=1; i<len && i<16; i++) {
239 *p++ = ':'; 239 *p++ = ':';
240 *p++ = hexdigits[(*adrp)>>4]; 240 *p++ = hexdigits[(*adrp)>>4];
241 *p++ = hexdigits[(*adrp++)&0xf]; 241 *p++ = hexdigits[(*adrp++)&0xf];
242 } 242 }
243 243
244 *p = 0; 244 *p = 0;
245 p = buf[bnum]; 245 p = buf[bnum];
246 bnum = (bnum + 1) % NUMBUFS; 246 bnum = (bnum + 1) % NUMBUFS;
247 return p; 247 return p;
248} 248}
249 249
250DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */ 250DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */
251 251
252static void 252static void
253arp_fasttimo(void) 253arp_fasttimo(void)
254{ 254{
255 if (arp_drainwanted) { 255 if (arp_drainwanted) {
256 arp_drain(); 256 arp_drain();
257 arp_drainwanted = 0; 257 arp_drainwanted = 0;
258 } 258 }
259} 259}
260 260
261const struct protosw arpsw[] = { 261const struct protosw arpsw[] = {
262 { .pr_type = 0, 262 { .pr_type = 0,
263 .pr_domain = &arpdomain, 263 .pr_domain = &arpdomain,
264 .pr_protocol = 0, 264 .pr_protocol = 0,
265 .pr_flags = 0, 265 .pr_flags = 0,
266 .pr_input = 0, 266 .pr_input = 0,
267 .pr_output = 0, 267 .pr_output = 0,
268 .pr_ctlinput = 0, 268 .pr_ctlinput = 0,
269 .pr_ctloutput = 0, 269 .pr_ctloutput = 0,
270 .pr_usrreqs = 0, 270 .pr_usrreqs = 0,
271 .pr_init = arp_init, 271 .pr_init = arp_init,
272 .pr_fasttimo = arp_fasttimo, 272 .pr_fasttimo = arp_fasttimo,
273 .pr_slowtimo = 0, 273 .pr_slowtimo = 0,
274 .pr_drain = arp_drainstub, 274 .pr_drain = arp_drainstub,
275 } 275 }
276}; 276};
277 277
278struct domain arpdomain = { 278struct domain arpdomain = {
279 .dom_family = PF_ARP, 279 .dom_family = PF_ARP,
280 .dom_name = "arp", 280 .dom_name = "arp",
281 .dom_protosw = arpsw, 281 .dom_protosw = arpsw,
282 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)], 282 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)],
283}; 283};
284 284
285static void sysctl_net_inet_arp_setup(struct sysctllog **); 285static void sysctl_net_inet_arp_setup(struct sysctllog **);
286 286
287void 287void
288arp_init(void) 288arp_init(void)
289{ 289{
290 290
291 sysctl_net_inet_arp_setup(NULL); 291 sysctl_net_inet_arp_setup(NULL);
292 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS); 292 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS);
293} 293}
294 294
295static void 295static void
296arp_drainstub(void) 296arp_drainstub(void)
297{ 297{
298 arp_drainwanted = 1; 298 arp_drainwanted = 1;
299} 299}
300 300
301/* 301/*
302 * ARP protocol drain routine. Called when memory is in short supply. 302 * ARP protocol drain routine. Called when memory is in short supply.
303 * Called at splvm(); don't acquire softnet_lock as can be called from 303 * Called at splvm(); don't acquire softnet_lock as can be called from
304 * hardware interrupt handlers. 304 * hardware interrupt handlers.
305 */ 305 */
306void 306void
307arp_drain(void) 307arp_drain(void)
308{ 308{
309 309
310 lltable_drain(AF_INET); 310 lltable_drain(AF_INET);
311} 311}
312 312
313static void 313static void
314arptimer(void *arg) 314arptimer(void *arg)
315{ 315{
316 struct llentry *lle = arg; 316 struct llentry *lle = arg;
317 struct ifnet *ifp; 317 struct ifnet *ifp;
318 318
319 mutex_enter(softnet_lock); 319 mutex_enter(softnet_lock);
320 320
321 if (lle == NULL) 321 if (lle == NULL)
322 goto out; 322 goto out;
323 323
324 if (lle->la_flags & LLE_STATIC) 324 if (lle->la_flags & LLE_STATIC)
325 goto out; 325 goto out;
326 326
327 LLE_WLOCK(lle); 327 LLE_WLOCK(lle);
328 if (callout_pending(&lle->la_timer)) { 328 if (callout_pending(&lle->la_timer)) {
329 /* 329 /*
330 * Here we are a bit odd here in the treatment of 330 * Here we are a bit odd here in the treatment of
331 * active/pending. If the pending bit is set, it got 331 * active/pending. If the pending bit is set, it got
332 * rescheduled before I ran. The active 332 * rescheduled before I ran. The active
333 * bit we ignore, since if it was stopped 333 * bit we ignore, since if it was stopped
334 * in ll_tablefree() and was currently running 334 * in ll_tablefree() and was currently running
335 * it would have return 0 so the code would 335 * it would have return 0 so the code would
336 * not have deleted it since the callout could 336 * not have deleted it since the callout could
337 * not be stopped so we want to go through 337 * not be stopped so we want to go through
338 * with the delete here now. If the callout 338 * with the delete here now. If the callout
339 * was restarted, the pending bit will be back on and 339 * was restarted, the pending bit will be back on and
340 * we just want to bail since the callout_reset would 340 * we just want to bail since the callout_reset would
341 * return 1 and our reference would have been removed 341 * return 1 and our reference would have been removed
342 * by arpresolve() below. 342 * by arpresolve() below.
343 */ 343 */
344 LLE_WUNLOCK(lle); 344 LLE_WUNLOCK(lle);
345 goto out; 345 goto out;
346 } 346 }
347 ifp = lle->lle_tbl->llt_ifp; 347 ifp = lle->lle_tbl->llt_ifp;
348 348
349 callout_stop(&lle->la_timer); 349 callout_stop(&lle->la_timer);
350 350
351 /* XXX: LOR avoidance. We still have ref on lle. */ 351 /* XXX: LOR avoidance. We still have ref on lle. */
352 LLE_WUNLOCK(lle); 352 LLE_WUNLOCK(lle);
353 353
354 if (lle->la_rt != NULL) { 354 if (lle->la_rt != NULL) {
355 /* We have to call arptfree w/o IF_AFDATA_LOCK */ 355 /* We have to call arptfree w/o IF_AFDATA_LOCK */
356 arptfree(lle->la_rt); 356 arptfree(lle->la_rt);
357 lle->la_rt = NULL; 357 lle->la_rt = NULL;
358 } 358 }
359 359
360 IF_AFDATA_LOCK(ifp); 360 IF_AFDATA_LOCK(ifp);
361 LLE_WLOCK(lle); 361 LLE_WLOCK(lle);
362 362
363 /* Guard against race with other llentry_free(). */ 363 /* Guard against race with other llentry_free(). */
364 if (lle->la_flags & LLE_LINKED) { 364 if (lle->la_flags & LLE_LINKED) {
365 size_t pkts_dropped; 365 size_t pkts_dropped;
366 366
367 LLE_REMREF(lle); 367 LLE_REMREF(lle);
368 pkts_dropped = llentry_free(lle); 368 pkts_dropped = llentry_free(lle);
369 ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped); 369 ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped);
370 } else { 370 } else {
371 LLE_FREE_LOCKED(lle); 371 LLE_FREE_LOCKED(lle);
372 } 372 }
373 373
374 IF_AFDATA_UNLOCK(ifp); 374 IF_AFDATA_UNLOCK(ifp);
375 375
376out: 376out:
377 mutex_exit(softnet_lock); 377 mutex_exit(softnet_lock);
378} 378}
379 379
380/* 380/*
381 * We set the gateway for RTF_CLONING routes to a "prototype" 381 * We set the gateway for RTF_CLONING routes to a "prototype"
382 * link-layer sockaddr whose interface type (if_type) and interface 382 * link-layer sockaddr whose interface type (if_type) and interface
383 * index (if_index) fields are prepared. 383 * index (if_index) fields are prepared.
384 */ 384 */
385static struct sockaddr * 385static struct sockaddr *
386arp_setgate(struct rtentry *rt, struct sockaddr *gate, 386arp_setgate(struct rtentry *rt, struct sockaddr *gate,
387 const struct sockaddr *netmask) 387 const struct sockaddr *netmask)
388{ 388{
389 const struct ifnet *ifp = rt->rt_ifp; 389 const struct ifnet *ifp = rt->rt_ifp;
390 uint8_t namelen = strlen(ifp->if_xname); 390 uint8_t namelen = strlen(ifp->if_xname);
391 uint8_t addrlen = ifp->if_addrlen; 391 uint8_t addrlen = ifp->if_addrlen;
392 392
393 /* 393 /*
394 * XXX: If this is a manually added route to interface 394 * XXX: If this is a manually added route to interface
395 * such as older version of routed or gated might provide, 395 * such as older version of routed or gated might provide,
396 * restore cloning bit. 396 * restore cloning bit.
397 */ 397 */
398 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL && 398 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL &&
399 satocsin(netmask)->sin_addr.s_addr != 0xffffffff) 399 satocsin(netmask)->sin_addr.s_addr != 0xffffffff)
400 rt->rt_flags |= RTF_CLONING; 400 rt->rt_flags |= RTF_CLONING;
401 if (rt->rt_flags & RTF_CLONING || 401 if (rt->rt_flags & RTF_CLONING ||
402 ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !rt->rt_llinfo)) 402 ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !rt->rt_llinfo))
403 { 403 {
404 union { 404 union {
405 struct sockaddr sa; 405 struct sockaddr sa;
406 struct sockaddr_storage ss; 406 struct sockaddr_storage ss;
407 struct sockaddr_dl sdl; 407 struct sockaddr_dl sdl;
408 } u; 408 } u;
409 /* 409 /*
410 * Case 1: This route should come from a route to iface. 410 * Case 1: This route should come from a route to iface.
411 */ 411 */
412 sockaddr_dl_init(&u.sdl, sizeof(u.ss), 412 sockaddr_dl_init(&u.sdl, sizeof(u.ss),
413 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen); 413 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen);
414 rt_setgate(rt, &u.sa); 414 rt_setgate(rt, &u.sa);
415 gate = rt->rt_gateway; 415 gate = rt->rt_gateway;
416 } 416 }
417 return gate; 417 return gate;
418} 418}
419 419
420/* 420/*
421 * Parallel to llc_rtrequest. 421 * Parallel to llc_rtrequest.
422 */ 422 */
423void 423void
424arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 424arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
425{ 425{
426 struct sockaddr *gate = rt->rt_gateway; 426 struct sockaddr *gate = rt->rt_gateway;
427 struct llentry *la = NULL; 427 struct llentry *la = NULL;
428 struct in_ifaddr *ia; 428 struct in_ifaddr *ia;
429 struct ifaddr *ifa; 429 struct ifaddr *ifa;
430 struct ifnet *ifp = rt->rt_ifp; 430 struct ifnet *ifp = rt->rt_ifp;
431 int flags = 0; 431 int flags = 0;
432 432
433 if (req == RTM_LLINFO_UPD) { 433 if (req == RTM_LLINFO_UPD) {
434 struct in_addr *in; 434 struct in_addr *in;
435 435
436 if ((ifa = info->rti_ifa) == NULL) 436 if ((ifa = info->rti_ifa) == NULL)
437 return; 437 return;
438 438
439 in = &ifatoia(ifa)->ia_addr.sin_addr; 439 in = &ifatoia(ifa)->ia_addr.sin_addr;
440 440
441 if (ifatoia(ifa)->ia4_flags & 441 if (ifatoia(ifa)->ia4_flags &
442 (IN_IFF_NOTREADY | IN_IFF_DETACHED)) 442 (IN_IFF_NOTREADY | IN_IFF_DETACHED))
443 { 443 {
444 arplog((LOG_DEBUG, "arp_request: %s not ready\n", 444 arplog((LOG_DEBUG, "arp_request: %s not ready\n",
445 in_fmtaddr(*in))); 445 in_fmtaddr(*in)));
446 return; 446 return;
447 } 447 }
448 448
449 arprequest(ifa->ifa_ifp, in, in, 449 arprequest(ifa->ifa_ifp, in, in,
450 CLLADDR(ifa->ifa_ifp->if_sadl)); 450 CLLADDR(ifa->ifa_ifp->if_sadl));
451 return; 451 return;
452 } 452 }
453 453
454 if ((rt->rt_flags & RTF_GATEWAY) != 0) { 454 if ((rt->rt_flags & RTF_GATEWAY) != 0) {
455 if (req != RTM_ADD) 455 if (req != RTM_ADD)
456 return; 456 return;
457 457
458 /* 458 /*
459 * linklayers with particular link MTU limitation. 459 * linklayers with particular link MTU limitation.
460 */ 460 */
461 switch(ifp->if_type) { 461 switch(ifp->if_type) {
462#if NFDDI > 0 462#if NFDDI > 0
463 case IFT_FDDI: 463 case IFT_FDDI:
464 if (ifp->if_mtu > FDDIIPMTU) 464 if (ifp->if_mtu > FDDIIPMTU)
465 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 465 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
466 break; 466 break;
467#endif 467#endif
468#if NARCNET > 0 468#if NARCNET > 0
469 case IFT_ARCNET: 469 case IFT_ARCNET:
470 { 470 {
471 int arcipifmtu; 471 int arcipifmtu;
472 472
473 if (ifp->if_flags & IFF_LINK0) 473 if (ifp->if_flags & IFF_LINK0)
474 arcipifmtu = arc_ipmtu; 474 arcipifmtu = arc_ipmtu;
475 else 475 else
476 arcipifmtu = ARCMTU; 476 arcipifmtu = ARCMTU;
477 if (ifp->if_mtu > arcipifmtu) 477 if (ifp->if_mtu > arcipifmtu)
478 rt->rt_rmx.rmx_mtu = arcipifmtu; 478 rt->rt_rmx.rmx_mtu = arcipifmtu;
479 break; 479 break;
480 } 480 }
481#endif 481#endif
482 } 482 }
483 return; 483 return;
484 } 484 }
485 485
486 IF_AFDATA_RLOCK(ifp); 486 IF_AFDATA_RLOCK(ifp);
487 la = lla_lookup(LLTABLE(ifp), flags, rt_getkey(rt)); 487 la = lla_lookup(LLTABLE(ifp), flags, rt_getkey(rt));
488 IF_AFDATA_RUNLOCK(ifp); 488 IF_AFDATA_RUNLOCK(ifp);
489 489
490 switch (req) { 490 switch (req) {
491 case RTM_SETGATE: 491 case RTM_SETGATE:
492 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 492 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
493 break; 493 break;
494 case RTM_ADD: 494 case RTM_ADD:
495 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 495 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
496 if (rt->rt_flags & RTF_CLONING || 496 if (rt->rt_flags & RTF_CLONING ||
497 ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !la)) 497 ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !la))
498 { 498 {
499 /* 499 /*
500 * Give this route an expiration time, even though 500 * Give this route an expiration time, even though
501 * it's a "permanent" route, so that routes cloned 501 * it's a "permanent" route, so that routes cloned
502 * from it do not need their expiration time set. 502 * from it do not need their expiration time set.
503 */ 503 */
504 KASSERT(time_uptime != 0); 504 KASSERT(time_uptime != 0);
505 rt->rt_expire = time_uptime; 505 rt->rt_expire = time_uptime;
506 /* 506 /*
507 * linklayers with particular link MTU limitation. 507 * linklayers with particular link MTU limitation.
508 */ 508 */
509 switch (ifp->if_type) { 509 switch (ifp->if_type) {
510#if NFDDI > 0 510#if NFDDI > 0
511 case IFT_FDDI: 511 case IFT_FDDI:
512 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 512 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
513 (rt->rt_rmx.rmx_mtu > FDDIIPMTU || 513 (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
514 (rt->rt_rmx.rmx_mtu == 0 && 514 (rt->rt_rmx.rmx_mtu == 0 &&
515 ifp->if_mtu > FDDIIPMTU))) 515 ifp->if_mtu > FDDIIPMTU)))
516 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 516 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
517 break; 517 break;
518#endif 518#endif
519#if NARCNET > 0 519#if NARCNET > 0
520 case IFT_ARCNET: 520 case IFT_ARCNET:
521 { 521 {
522 int arcipifmtu; 522 int arcipifmtu;
523 if (ifp->if_flags & IFF_LINK0) 523 if (ifp->if_flags & IFF_LINK0)
524 arcipifmtu = arc_ipmtu; 524 arcipifmtu = arc_ipmtu;
525 else 525 else
526 arcipifmtu = ARCMTU; 526 arcipifmtu = ARCMTU;
527 527
528 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 528 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
529 (rt->rt_rmx.rmx_mtu > arcipifmtu || 529 (rt->rt_rmx.rmx_mtu > arcipifmtu ||
530 (rt->rt_rmx.rmx_mtu == 0 && 530 (rt->rt_rmx.rmx_mtu == 0 &&
531 ifp->if_mtu > arcipifmtu))) 531 ifp->if_mtu > arcipifmtu)))
532 rt->rt_rmx.rmx_mtu = arcipifmtu; 532 rt->rt_rmx.rmx_mtu = arcipifmtu;
533 break; 533 break;
534 } 534 }
535#endif 535#endif
536 } 536 }
537 if (rt->rt_flags & RTF_CLONING) 537 if (rt->rt_flags & RTF_CLONING)
538 break; 538 break;
539 } 539 }
540 /* Announce a new entry if requested. */ 540 /* Announce a new entry if requested. */
541 if (rt->rt_flags & RTF_ANNOUNCE) { 541 if (rt->rt_flags & RTF_ANNOUNCE) {
542 INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia); 542 INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia);
543 while (ia && ia->ia_ifp != ifp) 543 while (ia && ia->ia_ifp != ifp)
544 NEXT_IA_WITH_SAME_ADDR(ia); 544 NEXT_IA_WITH_SAME_ADDR(ia);
545 if (ia == NULL || 545 if (ia == NULL ||
546 ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) 546 ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
547 ; 547 ;
548 else 548 else
549 arprequest(ifp, 549 arprequest(ifp,
550 &satocsin(rt_getkey(rt))->sin_addr, 550 &satocsin(rt_getkey(rt))->sin_addr,
551 &satocsin(rt_getkey(rt))->sin_addr, 551 &satocsin(rt_getkey(rt))->sin_addr,
552 CLLADDR(satocsdl(gate))); 552 CLLADDR(satocsdl(gate)));
553 } 553 }
554 /*FALLTHROUGH*/ 554 /*FALLTHROUGH*/
555 case RTM_RESOLVE: 555 case RTM_RESOLVE:
556 if (gate->sa_family != AF_LINK || 556 if (gate->sa_family != AF_LINK ||
557 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) { 557 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) {
558 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 558 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
559 break; 559 break;
560 } 560 }
561 561
562 satosdl(gate)->sdl_type = ifp->if_type; 562 satosdl(gate)->sdl_type = ifp->if_type;
563 satosdl(gate)->sdl_index = ifp->if_index; 563 satosdl(gate)->sdl_index = ifp->if_index;
564 if (la != NULL) 564 if (la != NULL)
565 break; /* This happens on a route change */ 565 break; /* This happens on a route change */
566 566
567 /* If the route is for a broadcast address mark it as such. 567 /* If the route is for a broadcast address mark it as such.
568 * This way we can avoid an expensive call to in_broadcast() 568 * This way we can avoid an expensive call to in_broadcast()
569 * in ip_output() most of the time (because the route passed 569 * in ip_output() most of the time (because the route passed
570 * to ip_output() is almost always a host route). */ 570 * to ip_output() is almost always a host route). */
571 if (rt->rt_flags & RTF_HOST && 571 if (rt->rt_flags & RTF_HOST &&
572 !(rt->rt_flags & RTF_BROADCAST) && 572 !(rt->rt_flags & RTF_BROADCAST) &&
573 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp)) 573 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp))
574 rt->rt_flags |= RTF_BROADCAST; 574 rt->rt_flags |= RTF_BROADCAST;
575 /* There is little point in resolving the broadcast address */ 575 /* There is little point in resolving the broadcast address */
576 if (rt->rt_flags & RTF_BROADCAST) 576 if (rt->rt_flags & RTF_BROADCAST)
577 break; 577 break;
578 578
579 INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia); 579 INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia);
580 while (ia && ia->ia_ifp != ifp) 580 while (ia && ia->ia_ifp != ifp)
581 NEXT_IA_WITH_SAME_ADDR(ia); 581 NEXT_IA_WITH_SAME_ADDR(ia);
582 if (ia) { 582 if (ia) {
583 /* 583 /*
584 * This test used to be 584 * This test used to be
585 * if (lo0ifp->if_flags & IFF_UP) 585 * if (lo0ifp->if_flags & IFF_UP)
586 * It allowed local traffic to be forced through 586 * It allowed local traffic to be forced through
587 * the hardware by configuring the loopback down. 587 * the hardware by configuring the loopback down.
588 * However, it causes problems during network 588 * However, it causes problems during network
589 * configuration for boards that can't receive 589 * configuration for boards that can't receive
590 * packets they send. It is now necessary to clear 590 * packets they send. It is now necessary to clear
591 * "useloopback" and remove the route to force 591 * "useloopback" and remove the route to force
592 * traffic out to the hardware. 592 * traffic out to the hardware.
593 * 593 *
594 * In 4.4BSD, the above "if" statement checked 594 * In 4.4BSD, the above "if" statement checked
595 * rt->rt_ifa against rt_getkey(rt). It was changed 595 * rt->rt_ifa against rt_getkey(rt). It was changed
596 * to the current form so that we can provide a 596 * to the current form so that we can provide a
597 * better support for multiple IPv4 addresses on a 597 * better support for multiple IPv4 addresses on a
598 * interface. 598 * interface.
599 */ 599 */
600 rt->rt_expire = 0; 600 rt->rt_expire = 0;
601 if (sockaddr_dl_init(satosdl(gate), gate->sa_len, 601 if (sockaddr_dl_init(satosdl(gate), gate->sa_len,
602 ifp->if_index, ifp->if_type, NULL, 0, 602 ifp->if_index, ifp->if_type, NULL, 0,
603 CLLADDR(ifp->if_sadl), ifp->if_addrlen) == NULL) { 603 CLLADDR(ifp->if_sadl), ifp->if_addrlen) == NULL) {
604 panic("%s(%s): sockaddr_dl_init cannot fail", 604 panic("%s(%s): sockaddr_dl_init cannot fail",
605 __func__, ifp->if_xname); 605 __func__, ifp->if_xname);
606 } 606 }
607 if (useloopback) { 607 if (useloopback) {
608 ifp = rt->rt_ifp = lo0ifp; 608 ifp = rt->rt_ifp = lo0ifp;
609 rt->rt_rmx.rmx_mtu = 0; 609 rt->rt_rmx.rmx_mtu = 0;
610 } 610 }
611 rt->rt_flags |= RTF_LOCAL; 611 rt->rt_flags |= RTF_LOCAL;
612 /* 612 /*
613 * make sure to set rt->rt_ifa to the interface 613 * make sure to set rt->rt_ifa to the interface
614 * address we are using, otherwise we will have trouble 614 * address we are using, otherwise we will have trouble
615 * with source address selection. 615 * with source address selection.
616 */ 616 */
617 ifa = &ia->ia_ifa; 617 ifa = &ia->ia_ifa;
618 if (ifa != rt->rt_ifa) 618 if (ifa != rt->rt_ifa)
619 rt_replace_ifa(rt, ifa); 619 rt_replace_ifa(rt, ifa);
620 } 620 }
621 621
622 /* 622 /*
623 * Case 2: This route may come from cloning, or a manual route 623 * Case 2: This route may come from cloning, or a manual route
624 * add with a LL address. 624 * add with a LL address.
625 */ 625 */
626 flags = LLE_EXCLUSIVE; 626 flags = LLE_EXCLUSIVE;
627 if ((rt->rt_flags & RTF_CLONED) == 0) 627 if ((rt->rt_flags & RTF_CLONED) == 0)
628 flags |= LLE_IFADDR; 628 flags |= LLE_IFADDR;
629 629
630 IF_AFDATA_WLOCK(ifp); 630 IF_AFDATA_WLOCK(ifp);
631 la = lla_create(LLTABLE(ifp), flags, rt_getkey(rt)); 631 la = lla_create(LLTABLE(ifp), flags, rt_getkey(rt));
632 IF_AFDATA_WUNLOCK(ifp); 632 IF_AFDATA_WUNLOCK(ifp);
633 633
634 if (la == NULL) { 634 if (la == NULL) {
635 log(LOG_DEBUG, "%s: lla_create failed\n", 635 log(LOG_DEBUG, "%s: lla_create failed\n",
636 __func__); 636 __func__);
637 rt->rt_llinfo = NULL; 637 rt->rt_llinfo = NULL;
638 break; 638 break;
639 } 639 }
640 rt->rt_llinfo = la; 640 rt->rt_llinfo = la;
641 switch (ifp->if_type) { 641 switch (ifp->if_type) {
642#if NTOKEN > 0 642#if NTOKEN > 0
643 case IFT_ISO88025: 643 case IFT_ISO88025:
644 la->la_opaque = kmem_alloc(sizeof(struct token_rif), 644 la->la_opaque = kmem_alloc(sizeof(struct token_rif),
645 KM_SLEEP); 645 KM_SLEEP);
646 break; 646 break;
647#endif /* NTOKEN > 0 */ 647#endif /* NTOKEN > 0 */
648 default: 648 default:
649 break; 649 break;
650 } 650 }
651 la->la_rt = rt; 651 la->la_rt = rt;
652 rt->rt_refcnt++; 652 rt->rt_refcnt++;
653 rt->rt_flags |= RTF_LLINFO; 653 rt->rt_flags |= RTF_LLINFO;
654 arp_inuse++, arp_allocated++; 654 arp_inuse++, arp_allocated++;
655 655
656 LLE_WUNLOCK(la); 656 LLE_WUNLOCK(la);
657 la = NULL; 657 la = NULL;
658 658
659 break; 659 break;
660 660
661 case RTM_DELETE: 661 case RTM_DELETE:
662 if (la == NULL) 662 if (la == NULL)
663 break; 663 break;
664 arp_inuse--; 664 arp_inuse--;
665 rt->rt_llinfo = NULL; 665 rt->rt_llinfo = NULL;
666 rt->rt_flags &= ~RTF_LLINFO; 666 rt->rt_flags &= ~RTF_LLINFO;
667 667
668 LLE_RUNLOCK(la); 668 LLE_RUNLOCK(la);
669 669
670 flags |= LLE_EXCLUSIVE; 670 flags |= LLE_EXCLUSIVE;
671 IF_AFDATA_WLOCK(ifp); 671 IF_AFDATA_WLOCK(ifp);
672 672
673 la = lla_lookup(LLTABLE(ifp), flags, rt_getkey(rt)); 673 la = lla_lookup(LLTABLE(ifp), flags, rt_getkey(rt));
674 /* This shouldn't happen */ 674 /* This shouldn't happen */
675 if (la == NULL) { 675 if (la == NULL) {
676 IF_AFDATA_WUNLOCK(ifp); 676 IF_AFDATA_WUNLOCK(ifp);
677 break; 677 break;
678 } 678 }
679 679
680 if (la->la_opaque != NULL) { 680 if (la->la_opaque != NULL) {
681 switch (ifp->if_type) { 681 switch (ifp->if_type) {
682#if NTOKEN > 0 682#if NTOKEN > 0
683 case IFT_ISO88025: 683 case IFT_ISO88025:
684 kmem_free(la->la_opaque, 684 kmem_free(la->la_opaque,
685 sizeof(struct token_rif)); 685 sizeof(struct token_rif));
686 break; 686 break;
687#endif /* NTOKEN > 0 */ 687#endif /* NTOKEN > 0 */
688 default: 688 default:
689 break; 689 break;
690 } 690 }
691 } 691 }
692 692
693 if (la->la_rt != NULL) { 693 if (la->la_rt != NULL) {
694 /* 694 /*
695 * Don't rtfree (may actually free objects) here. 695 * Don't rtfree (may actually free objects) here.
696 * Leave it to rtrequest1. 696 * Leave it to rtrequest1.
697 */ 697 */
698 la->la_rt->rt_refcnt--; 698 la->la_rt->rt_refcnt--;
699 la->la_rt = NULL; 699 la->la_rt = NULL;
700 } 700 }
701 llentry_free(la); 701 llentry_free(la);
702 702
703 IF_AFDATA_WUNLOCK(ifp); 703 IF_AFDATA_WUNLOCK(ifp);
704 la = NULL; 704 la = NULL;
705 } 705 }
706 706
707 if (la != NULL) { 707 if (la != NULL) {
708 if (flags & LLE_EXCLUSIVE) 708 if (flags & LLE_EXCLUSIVE)
709 LLE_WUNLOCK(la); 709 LLE_WUNLOCK(la);
710 else 710 else
711 LLE_RUNLOCK(la); 711 LLE_RUNLOCK(la);
712 } 712 }
713} 713}
714 714
715/* 715/*
716 * Broadcast an ARP request. Caller specifies: 716 * Broadcast an ARP request. Caller specifies:
717 * - arp header source ip address 717 * - arp header source ip address
718 * - arp header target ip address 718 * - arp header target ip address
719 * - arp header source ethernet address 719 * - arp header source ethernet address
720 */ 720 */
721void 721void
722arprequest(struct ifnet *ifp, 722arprequest(struct ifnet *ifp,
723 const struct in_addr *sip, const struct in_addr *tip, 723 const struct in_addr *sip, const struct in_addr *tip,
724 const u_int8_t *enaddr) 724 const u_int8_t *enaddr)
725{ 725{
726 struct mbuf *m; 726 struct mbuf *m;
727 struct arphdr *ah; 727 struct arphdr *ah;
728 struct sockaddr sa; 728 struct sockaddr sa;
729 uint64_t *arps; 729 uint64_t *arps;
730 730
731 KASSERT(sip != NULL); 731 KASSERT(sip != NULL);
732 KASSERT(tip != NULL); 732 KASSERT(tip != NULL);
733 KASSERT(enaddr != NULL); 733 KASSERT(enaddr != NULL);
734 734
735 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 735 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
736 return; 736 return;
737 MCLAIM(m, &arpdomain.dom_mowner); 737 MCLAIM(m, &arpdomain.dom_mowner);
738 switch (ifp->if_type) { 738 switch (ifp->if_type) {
739 case IFT_IEEE1394: 739 case IFT_IEEE1394:
740 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 740 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
741 ifp->if_addrlen; 741 ifp->if_addrlen;
742 break; 742 break;
743 default: 743 default:
744 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 744 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
745 2 * ifp->if_addrlen; 745 2 * ifp->if_addrlen;
746 break; 746 break;
747 } 747 }
748 m->m_pkthdr.len = m->m_len; 748 m->m_pkthdr.len = m->m_len;
749 MH_ALIGN(m, m->m_len); 749 MH_ALIGN(m, m->m_len);
750 ah = mtod(m, struct arphdr *); 750 ah = mtod(m, struct arphdr *);
751 memset(ah, 0, m->m_len); 751 memset(ah, 0, m->m_len);
752 switch (ifp->if_type) { 752 switch (ifp->if_type) {
753 case IFT_IEEE1394: /* RFC2734 */ 753 case IFT_IEEE1394: /* RFC2734 */
754 /* fill it now for ar_tpa computation */ 754 /* fill it now for ar_tpa computation */
755 ah->ar_hrd = htons(ARPHRD_IEEE1394); 755 ah->ar_hrd = htons(ARPHRD_IEEE1394);
756 break; 756 break;
757 default: 757 default:
758 /* ifp->if_output will fill ar_hrd */ 758 /* ifp->if_output will fill ar_hrd */
759 break; 759 break;
760 } 760 }
761 ah->ar_pro = htons(ETHERTYPE_IP); 761 ah->ar_pro = htons(ETHERTYPE_IP);
762 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 762 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
763 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 763 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
764 ah->ar_op = htons(ARPOP_REQUEST); 764 ah->ar_op = htons(ARPOP_REQUEST);
765 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 765 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
766 memcpy(ar_spa(ah), sip, ah->ar_pln); 766 memcpy(ar_spa(ah), sip, ah->ar_pln);
767 memcpy(ar_tpa(ah), tip, ah->ar_pln); 767 memcpy(ar_tpa(ah), tip, ah->ar_pln);
768 sa.sa_family = AF_ARP; 768 sa.sa_family = AF_ARP;
769 sa.sa_len = 2; 769 sa.sa_len = 2;
770 m->m_flags |= M_BCAST; 770 m->m_flags |= M_BCAST;
771 arps = ARP_STAT_GETREF(); 771 arps = ARP_STAT_GETREF();
772 arps[ARP_STAT_SNDTOTAL]++; 772 arps[ARP_STAT_SNDTOTAL]++;
773 arps[ARP_STAT_SENDREQUEST]++; 773 arps[ARP_STAT_SENDREQUEST]++;
774 ARP_STAT_PUTREF(); 774 ARP_STAT_PUTREF();
775 (*ifp->if_output)(ifp, m, &sa, NULL); 775 (*ifp->if_output)(ifp, m, &sa, NULL);
776} 776}
777 777
778/* 778/*
779 * Resolve an IP address into an ethernet address. If success, 779 * Resolve an IP address into an ethernet address. If success,
780 * desten is filled in. If there is no entry in arptab, 780 * desten is filled in. If there is no entry in arptab,
781 * set one up and broadcast a request for the IP address. 781 * set one up and broadcast a request for the IP address.
782 * Hold onto this mbuf and resend it once the address 782 * Hold onto this mbuf and resend it once the address
783 * is finally resolved. A return value of 1 indicates 783 * is finally resolved. A return value of 1 indicates
784 * that desten has been filled in and the packet should be sent 784 * that desten has been filled in and the packet should be sent
785 * normally; a 0 return indicates that the packet has been 785 * normally; a 0 return indicates that the packet has been
786 * taken over here, either now or for later transmission. 786 * taken over here, either now or for later transmission.
787 */ 787 */
788int 788int
789arpresolve(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m, 789arpresolve(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
790 const struct sockaddr *dst, u_char *desten) 790 const struct sockaddr *dst, u_char *desten)
791{ 791{
792 struct llentry *la; 792 struct llentry *la;
793 const struct sockaddr_dl *sdl; 793 const struct sockaddr_dl *sdl;
794 const char *create_lookup; 794 const char *create_lookup;
795 bool renew; 795 bool renew;
796 int error; 796 int error;
797 797
798 KASSERT(m != NULL); 798 KASSERT(m != NULL);
799 799
800 la = arplookup(ifp, m, &satocsin(dst)->sin_addr, 0, 0, 0, rt); 800 la = arplookup(ifp, m, &satocsin(dst)->sin_addr, 0, 0, 0, rt);
801 if (la == NULL || la->la_rt == NULL) 801 if (la == NULL || la->la_rt == NULL)
802 goto notfound; 802 goto notfound;
803 803
804 rt = la->la_rt; 804 rt = la->la_rt;
805 sdl = satocsdl(rt->rt_gateway); 805 sdl = satocsdl(rt->rt_gateway);
806 /* 806 /*
807 * Check the address family and length is valid, the address 807 * Check the address family and length is valid, the address
808 * is resolved; otherwise, try to resolve. 808 * is resolved; otherwise, try to resolve.
809 */ 809 */
810 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 810 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
811 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 811 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
812 memcpy(desten, CLLADDR(sdl), 812 memcpy(desten, CLLADDR(sdl),
813 min(sdl->sdl_alen, ifp->if_addrlen)); 813 min(sdl->sdl_alen, ifp->if_addrlen));
814 rt->rt_pksent = time_uptime; /* Time for last pkt sent */ 814 rt->rt_pksent = time_uptime; /* Time for last pkt sent */
815 LLE_RUNLOCK(la); 815 LLE_RUNLOCK(la);
816 return 1; 816 return 1;
817 } 817 }
818 818
819 /* 819 /*
820 * Re-send the ARP request when appropriate. 820 * Re-send the ARP request when appropriate.
821 */ 821 */
822#ifdef DIAGNOSTIC 822#ifdef DIAGNOSTIC
823 if (rt->rt_expire == 0) { 823 if (rt->rt_expire == 0) {
824 /* This should never happen. (Should it? -gwr) */ 824 /* This should never happen. (Should it? -gwr) */
825 printf("arpresolve: unresolved and rt_expire == 0\n"); 825 printf("arpresolve: unresolved and rt_expire == 0\n");
826 /* Set expiration time to now (expired). */ 826 /* Set expiration time to now (expired). */
827 rt->rt_expire = time_uptime; 827 rt->rt_expire = time_uptime;
828 } 828 }
829#endif 829#endif
830 830
831notfound: 831notfound:
832 if (la == NULL) { 
833#ifdef IFF_STATICARP /* FreeBSD */ 832#ifdef IFF_STATICARP /* FreeBSD */
834#define _IFF_NOARP (IFF_NOARP | IFF_STATICARP) 833#define _IFF_NOARP (IFF_NOARP | IFF_STATICARP)
835#else 834#else
836#define _IFF_NOARP IFF_NOARP 835#define _IFF_NOARP IFF_NOARP
837#endif 836#endif
838 if (ifp->if_flags & _IFF_NOARP) { 837 if (ifp->if_flags & _IFF_NOARP) {
839 m_freem(m); 838 if (la != NULL)
840 return 0; 839 LLE_RUNLOCK(la);
841 } 840 m_freem(m);
 841 return 0;
 842 }
842#undef _IFF_NOARP 843#undef _IFF_NOARP
 844 if (la == NULL) {
843 create_lookup = "create"; 845 create_lookup = "create";
844 IF_AFDATA_WLOCK(ifp); 846 IF_AFDATA_WLOCK(ifp);
845 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 847 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
846 IF_AFDATA_WUNLOCK(ifp); 848 IF_AFDATA_WUNLOCK(ifp);
847 if (la == NULL) 849 if (la == NULL)
848 ARP_STATINC(ARP_STAT_ALLOCFAIL); 850 ARP_STATINC(ARP_STAT_ALLOCFAIL);
849 } else if (LLE_TRY_UPGRADE(la) == 0) { 851 } else if (LLE_TRY_UPGRADE(la) == 0) {
850 create_lookup = "lookup"; 852 create_lookup = "lookup";
851 LLE_RUNLOCK(la); 853 LLE_RUNLOCK(la);
852 IF_AFDATA_RLOCK(ifp); 854 IF_AFDATA_RLOCK(ifp);
853 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 855 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
854 IF_AFDATA_RUNLOCK(ifp); 856 IF_AFDATA_RUNLOCK(ifp);
855 } 857 }
856 858
857 if (la == NULL) { 859 if (la == NULL) {
858 log(LOG_DEBUG, 860 log(LOG_DEBUG,
859 "%s: failed to %s llentry for %s on %s\n", 861 "%s: failed to %s llentry for %s on %s\n",
860 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr), 862 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr),
861 ifp->if_xname); 863 ifp->if_xname);
862 m_freem(m); 864 m_freem(m);
863 return 0; 865 return 0;
864 } 866 }
865 867
866 /* Just in case */ 868 /* Just in case */
867 if (la->la_rt == NULL) { 869 if (la->la_rt == NULL) {
868 log(LOG_DEBUG, 870 log(LOG_DEBUG,
869 "%s: valid llentry has no rtentry for %s on %s\n", 871 "%s: valid llentry has no rtentry for %s on %s\n",
870 __func__, inet_ntoa(satocsin(dst)->sin_addr), 872 __func__, inet_ntoa(satocsin(dst)->sin_addr),
871 ifp->if_xname); 873 ifp->if_xname);
872 m_freem(m); 874 m_freem(m);
873 return 0; 875 return 0;
874 } 876 }
875 rt = la->la_rt; 877 rt = la->la_rt;
876 878
877 if ((la->la_flags & LLE_VALID) && 879 if ((la->la_flags & LLE_VALID) &&
878 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) 880 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime))
879 { 881 {
880 sdl = satocsdl(rt->rt_gateway); 882 sdl = satocsdl(rt->rt_gateway);
881 memcpy(desten, CLLADDR(sdl), 883 memcpy(desten, CLLADDR(sdl),
882 min(sdl->sdl_alen, ifp->if_addrlen)); 884 min(sdl->sdl_alen, ifp->if_addrlen));
883 renew = false; 885 renew = false;
884 /* 886 /*
885 * If entry has an expiry time and it is approaching, 887 * If entry has an expiry time and it is approaching,
886 * see if we need to send an ARP request within this 888 * see if we need to send an ARP request within this
887 * arpt_down interval. 889 * arpt_down interval.
888 */ 890 */
889 if (!(la->la_flags & LLE_STATIC) && 891 if (!(la->la_flags & LLE_STATIC) &&
890 time_uptime + la->la_preempt > la->la_expire) 892 time_uptime + la->la_preempt > la->la_expire)
891 { 893 {
892 renew = true; 894 renew = true;
893 la->la_preempt--; 895 la->la_preempt--;
894 } 896 }
895 897
896 LLE_WUNLOCK(la); 898 LLE_WUNLOCK(la);
897 899
898 if (renew) { 900 if (renew) {
899 const u_int8_t *enaddr = 901 const u_int8_t *enaddr =
900#if NCARP > 0 902#if NCARP > 0
901 (rt->rt_ifp->if_type == IFT_CARP) ? 903 (rt->rt_ifp->if_type == IFT_CARP) ?
902 CLLADDR(rt->rt_ifp->if_sadl): 904 CLLADDR(rt->rt_ifp->if_sadl):
903#endif 905#endif
904 CLLADDR(ifp->if_sadl); 906 CLLADDR(ifp->if_sadl);
905 arprequest(ifp, 907 arprequest(ifp,
906 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 908 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
907 &satocsin(dst)->sin_addr, enaddr); 909 &satocsin(dst)->sin_addr, enaddr);
908 } 910 }
909 911
910 return 1; 912 return 1;
911 } 913 }
912 914
913 if (la->la_flags & LLE_STATIC) { /* should not happen! */ 915 if (la->la_flags & LLE_STATIC) { /* should not happen! */
914 log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n", 916 log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n",
915 inet_ntoa(satocsin(dst)->sin_addr)); 917 inet_ntoa(satocsin(dst)->sin_addr));
916 m_freem(m); 918 m_freem(m);
917 error = EINVAL; 919 error = EINVAL;
918 goto done; 920 goto done;
919 } 921 }
920 922
921 renew = (la->la_asked == 0 || la->la_expire != time_uptime); 923 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
922 924
923 /* 925 /*
924 * There is an arptab entry, but no ethernet address 926 * There is an arptab entry, but no ethernet address
925 * response yet. Add the mbuf to the list, dropping 927 * response yet. Add the mbuf to the list, dropping
926 * the oldest packet if we have exceeded the system 928 * the oldest packet if we have exceeded the system
927 * setting. 929 * setting.
928 */ 930 */
929 LLE_WLOCK_ASSERT(la); 931 LLE_WLOCK_ASSERT(la);
930 if (la->la_numheld >= arp_maxhold) { 932 if (la->la_numheld >= arp_maxhold) {
931 if (la->la_hold != NULL) { 933 if (la->la_hold != NULL) {
932 struct mbuf *next = la->la_hold->m_nextpkt; 934 struct mbuf *next = la->la_hold->m_nextpkt;
933 m_freem(la->la_hold); 935 m_freem(la->la_hold);
934 la->la_hold = next; 936 la->la_hold = next;
935 la->la_numheld--; 937 la->la_numheld--;
936 ARP_STATINC(ARP_STAT_DFRDROPPED); 938 ARP_STATINC(ARP_STAT_DFRDROPPED);
937 } 939 }
938 } 940 }
939 if (la->la_hold != NULL) { 941 if (la->la_hold != NULL) {
940 struct mbuf *curr = la->la_hold; 942 struct mbuf *curr = la->la_hold;
941 while (curr->m_nextpkt != NULL) 943 while (curr->m_nextpkt != NULL)
942 curr = curr->m_nextpkt; 944 curr = curr->m_nextpkt;
943 curr->m_nextpkt = m; 945 curr->m_nextpkt = m;
944 } else 946 } else
945 la->la_hold = m; 947 la->la_hold = m;
946 la->la_numheld++; 948 la->la_numheld++;
947 if (!renew) 949 if (!renew)
948 LLE_DOWNGRADE(la); 950 LLE_DOWNGRADE(la);
949 951
950 /* 952 /*
951 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 953 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
952 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 954 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
953 * if we have already sent arp_maxtries ARP requests. Retransmit the 955 * if we have already sent arp_maxtries ARP requests. Retransmit the
954 * ARP request, but not faster than one request per second. 956 * ARP request, but not faster than one request per second.
955 */ 957 */
956 if (la->la_asked < arp_maxtries) 958 if (la->la_asked < arp_maxtries)
957 error = EWOULDBLOCK; /* First request. */ 959 error = EWOULDBLOCK; /* First request. */
958 else 960 else
959 error = (rt->rt_flags & RTF_GATEWAY) ? 961 error = (rt->rt_flags & RTF_GATEWAY) ?
960 EHOSTUNREACH : EHOSTDOWN; 962 EHOSTUNREACH : EHOSTDOWN;
961 963
962 if (renew) { 964 if (renew) {
963 const u_int8_t *enaddr = 965 const u_int8_t *enaddr =
964#if NCARP > 0 966#if NCARP > 0
965 (rt->rt_ifp->if_type == IFT_CARP) ? 967 (rt->rt_ifp->if_type == IFT_CARP) ?
966 CLLADDR(rt->rt_ifp->if_sadl): 968 CLLADDR(rt->rt_ifp->if_sadl):
967#endif 969#endif
968 CLLADDR(ifp->if_sadl); 970 CLLADDR(ifp->if_sadl);
969 LLE_ADDREF(la); 971 LLE_ADDREF(la);
970 la->la_expire = time_uptime; 972 la->la_expire = time_uptime;
971 callout_reset(&la->la_timer, hz * arpt_down, 973 callout_reset(&la->la_timer, hz * arpt_down,
972 arptimer, la); 974 arptimer, la);
973 la->la_asked++; 975 la->la_asked++;
974 LLE_WUNLOCK(la); 976 LLE_WUNLOCK(la);
975 977
976 arprequest(ifp, &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 978 arprequest(ifp, &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
977 &satocsin(dst)->sin_addr, enaddr); 979 &satocsin(dst)->sin_addr, enaddr);
978 return error == 0; 980 return error == 0;
979 } 981 }
980done: 982done:
981 LLE_RUNLOCK(la); 983 LLE_RUNLOCK(la);
982 984
983 return error == 0; 985 return error == 0;
984} 986}
985 987
986/* 988/*
987 * Common length and type checks are done here, 989 * Common length and type checks are done here,
988 * then the protocol-specific routine is called. 990 * then the protocol-specific routine is called.
989 */ 991 */
990void 992void
991arpintr(void) 993arpintr(void)
992{ 994{
993 struct mbuf *m; 995 struct mbuf *m;
994 struct arphdr *ar; 996 struct arphdr *ar;
995 int s; 997 int s;
996 int arplen; 998 int arplen;
997 999
998 mutex_enter(softnet_lock); 1000 mutex_enter(softnet_lock);
999 KERNEL_LOCK(1, NULL); 1001 KERNEL_LOCK(1, NULL);
1000 while (arpintrq.ifq_head) { 1002 while (arpintrq.ifq_head) {
1001 s = splnet(); 1003 s = splnet();
1002 IF_DEQUEUE(&arpintrq, m); 1004 IF_DEQUEUE(&arpintrq, m);
1003 splx(s); 1005 splx(s);
1004 if (m == NULL || (m->m_flags & M_PKTHDR) == 0) 1006 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
1005 panic("arpintr"); 1007 panic("arpintr");
1006 1008
1007 MCLAIM(m, &arpdomain.dom_mowner); 1009 MCLAIM(m, &arpdomain.dom_mowner);
1008 ARP_STATINC(ARP_STAT_RCVTOTAL); 1010 ARP_STATINC(ARP_STAT_RCVTOTAL);
1009 1011
1010 /* 1012 /*
1011 * First, make sure we have at least struct arphdr. 1013 * First, make sure we have at least struct arphdr.
1012 */ 1014 */
1013 if (m->m_len < sizeof(struct arphdr) || 1015 if (m->m_len < sizeof(struct arphdr) ||
1014 (ar = mtod(m, struct arphdr *)) == NULL) 1016 (ar = mtod(m, struct arphdr *)) == NULL)
1015 goto badlen; 1017 goto badlen;
1016 1018
1017 switch (m->m_pkthdr.rcvif->if_type) { 1019 switch (m->m_pkthdr.rcvif->if_type) {
1018 case IFT_IEEE1394: 1020 case IFT_IEEE1394:
1019 arplen = sizeof(struct arphdr) + 1021 arplen = sizeof(struct arphdr) +
1020 ar->ar_hln + 2 * ar->ar_pln; 1022 ar->ar_hln + 2 * ar->ar_pln;
1021 break; 1023 break;
1022 default: 1024 default:
1023 arplen = sizeof(struct arphdr) + 1025 arplen = sizeof(struct arphdr) +
1024 2 * ar->ar_hln + 2 * ar->ar_pln; 1026 2 * ar->ar_hln + 2 * ar->ar_pln;
1025 break; 1027 break;
1026 } 1028 }
1027 1029
1028 if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */ 1030 if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
1029 m->m_len >= arplen) 1031 m->m_len >= arplen)
1030 switch (ntohs(ar->ar_pro)) { 1032 switch (ntohs(ar->ar_pro)) {
1031 case ETHERTYPE_IP: 1033 case ETHERTYPE_IP:
1032 case ETHERTYPE_IPTRAILERS: 1034 case ETHERTYPE_IPTRAILERS:
1033 in_arpinput(m); 1035 in_arpinput(m);
1034 continue; 1036 continue;
1035 default: 1037 default:
1036 ARP_STATINC(ARP_STAT_RCVBADPROTO); 1038 ARP_STATINC(ARP_STAT_RCVBADPROTO);
1037 } 1039 }
1038 else { 1040 else {
1039badlen: 1041badlen:
1040 ARP_STATINC(ARP_STAT_RCVBADLEN); 1042 ARP_STATINC(ARP_STAT_RCVBADLEN);
1041 } 1043 }
1042 m_freem(m); 1044 m_freem(m);
1043 } 1045 }
1044 KERNEL_UNLOCK_ONE(NULL); 1046 KERNEL_UNLOCK_ONE(NULL);
1045 mutex_exit(softnet_lock); 1047 mutex_exit(softnet_lock);
1046} 1048}
1047 1049
1048/* 1050/*
1049 * ARP for Internet protocols on 10 Mb/s Ethernet. 1051 * ARP for Internet protocols on 10 Mb/s Ethernet.
1050 * Algorithm is that given in RFC 826. 1052 * Algorithm is that given in RFC 826.
1051 * In addition, a sanity check is performed on the sender 1053 * In addition, a sanity check is performed on the sender
1052 * protocol address, to catch impersonators. 1054 * protocol address, to catch impersonators.
1053 * We no longer handle negotiations for use of trailer protocol: 1055 * We no longer handle negotiations for use of trailer protocol:
1054 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 1056 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
1055 * along with IP replies if we wanted trailers sent to us, 1057 * along with IP replies if we wanted trailers sent to us,
1056 * and also sent them in response to IP replies. 1058 * and also sent them in response to IP replies.
1057 * This allowed either end to announce the desire to receive 1059 * This allowed either end to announce the desire to receive
1058 * trailer packets. 1060 * trailer packets.
1059 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 1061 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
1060 * but formerly didn't normally send requests. 1062 * but formerly didn't normally send requests.
1061 */ 1063 */
1062static void 1064static void
1063in_arpinput(struct mbuf *m) 1065in_arpinput(struct mbuf *m)
1064{ 1066{
1065 struct arphdr *ah; 1067 struct arphdr *ah;
1066 struct ifnet *ifp = m->m_pkthdr.rcvif; 1068 struct ifnet *ifp = m->m_pkthdr.rcvif;
1067 struct llentry *la = NULL; 1069 struct llentry *la = NULL;
1068 struct rtentry *rt = NULL; 1070 struct rtentry *rt = NULL;
1069 struct in_ifaddr *ia; 1071 struct in_ifaddr *ia;
1070#if NBRIDGE > 0 1072#if NBRIDGE > 0
1071 struct in_ifaddr *bridge_ia = NULL; 1073 struct in_ifaddr *bridge_ia = NULL;
1072#endif 1074#endif
1073#if NCARP > 0 1075#if NCARP > 0
1074 u_int32_t count = 0, index = 0; 1076 u_int32_t count = 0, index = 0;
1075#endif 1077#endif
1076 struct sockaddr_dl *sdl = NULL; 1078 struct sockaddr_dl *sdl = NULL;
1077 struct sockaddr sa; 1079 struct sockaddr sa;
1078 struct in_addr isaddr, itaddr, myaddr; 1080 struct in_addr isaddr, itaddr, myaddr;
1079 int op; 1081 int op;
1080 void *tha; 1082 void *tha;
1081 uint64_t *arps; 1083 uint64_t *arps;
1082 1084
1083 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT))) 1085 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
1084 goto out; 1086 goto out;
1085 ah = mtod(m, struct arphdr *); 1087 ah = mtod(m, struct arphdr *);
1086 op = ntohs(ah->ar_op); 1088 op = ntohs(ah->ar_op);
1087 1089
1088 /* 1090 /*
1089 * Fix up ah->ar_hrd if necessary, before using ar_tha() or 1091 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
1090 * ar_tpa(). 1092 * ar_tpa().
1091 */ 1093 */
1092 switch (ifp->if_type) { 1094 switch (ifp->if_type) {
1093 case IFT_IEEE1394: 1095 case IFT_IEEE1394:
1094 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394) 1096 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
1095 ; 1097 ;
1096 else { 1098 else {
1097 /* XXX this is to make sure we compute ar_tha right */ 1099 /* XXX this is to make sure we compute ar_tha right */
1098 /* XXX check ar_hrd more strictly? */ 1100 /* XXX check ar_hrd more strictly? */
1099 ah->ar_hrd = htons(ARPHRD_IEEE1394); 1101 ah->ar_hrd = htons(ARPHRD_IEEE1394);
1100 } 1102 }
1101 break; 1103 break;
1102 default: 1104 default:
1103 /* XXX check ar_hrd? */ 1105 /* XXX check ar_hrd? */
1104 break; 1106 break;
1105 } 1107 }
1106 1108
1107 memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 1109 memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
1108 memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 1110 memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
1109 1111
1110 if (m->m_flags & (M_BCAST|M_MCAST)) 1112 if (m->m_flags & (M_BCAST|M_MCAST))
1111 ARP_STATINC(ARP_STAT_RCVMCAST); 1113 ARP_STATINC(ARP_STAT_RCVMCAST);
1112 1114
1113 1115
1114 /* 1116 /*
1115 * Search for a matching interface address 1117 * Search for a matching interface address
1116 * or any address on the interface to use 1118 * or any address on the interface to use
1117 * as a dummy address in the rest of this function 1119 * as a dummy address in the rest of this function
1118 */ 1120 */
1119  1121
1120 INADDR_TO_IA(itaddr, ia); 1122 INADDR_TO_IA(itaddr, ia);
1121 while (ia != NULL) { 1123 while (ia != NULL) {
1122#if NCARP > 0 1124#if NCARP > 0
1123 if (ia->ia_ifp->if_type == IFT_CARP && 1125 if (ia->ia_ifp->if_type == IFT_CARP &&
1124 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) == 1126 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1125 (IFF_UP|IFF_RUNNING))) { 1127 (IFF_UP|IFF_RUNNING))) {
1126 index++; 1128 index++;
1127 if (ia->ia_ifp == m->m_pkthdr.rcvif && 1129 if (ia->ia_ifp == m->m_pkthdr.rcvif &&
1128 carp_iamatch(ia, ar_sha(ah), 1130 carp_iamatch(ia, ar_sha(ah),
1129 &count, index)) { 1131 &count, index)) {
1130 break; 1132 break;
1131 } 1133 }
1132 } else 1134 } else
1133#endif 1135#endif
1134 if (ia->ia_ifp == m->m_pkthdr.rcvif) 1136 if (ia->ia_ifp == m->m_pkthdr.rcvif)
1135 break; 1137 break;
1136#if NBRIDGE > 0 1138#if NBRIDGE > 0
1137 /* 1139 /*
1138 * If the interface we received the packet on 1140 * If the interface we received the packet on
1139 * is part of a bridge, check to see if we need 1141 * is part of a bridge, check to see if we need
1140 * to "bridge" the packet to ourselves at this 1142 * to "bridge" the packet to ourselves at this
1141 * layer. Note we still prefer a perfect match, 1143 * layer. Note we still prefer a perfect match,
1142 * but allow this weaker match if necessary. 1144 * but allow this weaker match if necessary.
1143 */ 1145 */
1144 if (m->m_pkthdr.rcvif->if_bridge != NULL && 1146 if (m->m_pkthdr.rcvif->if_bridge != NULL &&
1145 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge) 1147 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
1146 bridge_ia = ia; 1148 bridge_ia = ia;
1147#endif /* NBRIDGE > 0 */ 1149#endif /* NBRIDGE > 0 */
1148 1150
1149 NEXT_IA_WITH_SAME_ADDR(ia); 1151 NEXT_IA_WITH_SAME_ADDR(ia);
1150 } 1152 }
1151 1153
1152#if NBRIDGE > 0 1154#if NBRIDGE > 0
1153 if (ia == NULL && bridge_ia != NULL) { 1155 if (ia == NULL && bridge_ia != NULL) {
1154 ia = bridge_ia; 1156 ia = bridge_ia;
1155 ifp = bridge_ia->ia_ifp; 1157 ifp = bridge_ia->ia_ifp;
1156 } 1158 }
1157#endif 1159#endif
1158 1160
1159 if (ia == NULL) { 1161 if (ia == NULL) {
1160 INADDR_TO_IA(isaddr, ia); 1162 INADDR_TO_IA(isaddr, ia);
1161 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif) 1163 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
1162 NEXT_IA_WITH_SAME_ADDR(ia); 1164 NEXT_IA_WITH_SAME_ADDR(ia);
1163 1165
1164 if (ia == NULL) { 1166 if (ia == NULL) {
1165 IFP_TO_IA(ifp, ia); 1167 IFP_TO_IA(ifp, ia);
1166 if (ia == NULL) { 1168 if (ia == NULL) {
1167 ARP_STATINC(ARP_STAT_RCVNOINT); 1169 ARP_STATINC(ARP_STAT_RCVNOINT);
1168 goto out; 1170 goto out;
1169 } 1171 }
1170 } 1172 }
1171 } 1173 }
1172 1174
1173 myaddr = ia->ia_addr.sin_addr; 1175 myaddr = ia->ia_addr.sin_addr;
1174 1176
1175 /* XXX checks for bridge case? */ 1177 /* XXX checks for bridge case? */
1176 if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) { 1178 if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
1177 ARP_STATINC(ARP_STAT_RCVLOCALSHA); 1179 ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1178 goto out; /* it's from me, ignore it. */ 1180 goto out; /* it's from me, ignore it. */
1179 } 1181 }
1180 1182
1181 /* XXX checks for bridge case? */ 1183 /* XXX checks for bridge case? */
1182 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 1184 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1183 ARP_STATINC(ARP_STAT_RCVBCASTSHA); 1185 ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1184 log(LOG_ERR, 1186 log(LOG_ERR,
1185 "%s: arp: link address is broadcast for IP address %s!\n", 1187 "%s: arp: link address is broadcast for IP address %s!\n",
1186 ifp->if_xname, in_fmtaddr(isaddr)); 1188 ifp->if_xname, in_fmtaddr(isaddr));
1187 goto out; 1189 goto out;
1188 } 1190 }
1189 1191
1190 /* 1192 /*
1191 * If the source IP address is zero, this is an RFC 5227 ARP probe 1193 * If the source IP address is zero, this is an RFC 5227 ARP probe
1192 */ 1194 */
1193 if (in_nullhost(isaddr)) 1195 if (in_nullhost(isaddr))
1194 ARP_STATINC(ARP_STAT_RCVZEROSPA); 1196 ARP_STATINC(ARP_STAT_RCVZEROSPA);
1195 else if (in_hosteq(isaddr, myaddr)) 1197 else if (in_hosteq(isaddr, myaddr))
1196 ARP_STATINC(ARP_STAT_RCVLOCALSPA); 1198 ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1197 1199
1198 if (in_nullhost(itaddr)) 1200 if (in_nullhost(itaddr))
1199 ARP_STATINC(ARP_STAT_RCVZEROTPA); 1201 ARP_STATINC(ARP_STAT_RCVZEROTPA);
1200 1202
1201 /* DAD check, RFC 5227 2.1.1, Probe Details */ 1203 /* DAD check, RFC 5227 2.1.1, Probe Details */
1202 if (in_hosteq(isaddr, myaddr) || 1204 if (in_hosteq(isaddr, myaddr) ||
1203 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr))) 1205 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr)))
1204 { 1206 {
1205 /* If our address is tentative, mark it as duplicated */ 1207 /* If our address is tentative, mark it as duplicated */
1206 if (ia->ia4_flags & IN_IFF_TENTATIVE) 1208 if (ia->ia4_flags & IN_IFF_TENTATIVE)
1207 arp_dad_duplicated((struct ifaddr *)ia); 1209 arp_dad_duplicated((struct ifaddr *)ia);
1208 /* If our address is unuseable, don't reply */ 1210 /* If our address is unuseable, don't reply */
1209 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) 1211 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1210 goto out; 1212 goto out;
1211 } 1213 }
1212 1214
1213 /* 1215 /*
1214 * If the target IP address is zero, ignore the packet. 1216 * If the target IP address is zero, ignore the packet.
1215 * This prevents the code below from tring to answer 1217 * This prevents the code below from tring to answer
1216 * when we are using IP address zero (booting). 1218 * when we are using IP address zero (booting).
1217 */ 1219 */
1218 if (in_nullhost(itaddr)) 1220 if (in_nullhost(itaddr))
1219 goto out; 1221 goto out;
1220 1222
1221 if (in_nullhost(isaddr)) 1223 if (in_nullhost(isaddr))
1222 goto reply; 1224 goto reply;
1223 1225
1224 if (in_hosteq(isaddr, myaddr)) { 1226 if (in_hosteq(isaddr, myaddr)) {
1225 log(LOG_ERR, 1227 log(LOG_ERR,
1226 "duplicate IP address %s sent from link address %s\n", 1228 "duplicate IP address %s sent from link address %s\n",
1227 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln)); 1229 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
1228 itaddr = myaddr; 1230 itaddr = myaddr;
1229 goto reply; 1231 goto reply;
1230 } 1232 }
1231 1233
1232 la = arplookup(ifp, m, &isaddr, in_hosteq(itaddr, myaddr), 0, 1, NULL); 1234 la = arplookup(ifp, m, &isaddr, in_hosteq(itaddr, myaddr), 0, 1, NULL);
1233 if (la != NULL) { 1235 if (la != NULL) {
1234 rt = la->la_rt; 1236 rt = la->la_rt;
1235 if (rt != NULL) 1237 if (rt != NULL)
1236 sdl = satosdl(rt->rt_gateway); 1238 sdl = satosdl(rt->rt_gateway);
1237 } 1239 }
1238 if (sdl != NULL) { 1240 if (sdl != NULL) {
1239 if (sdl->sdl_alen && 1241 if (sdl->sdl_alen &&
1240 memcmp(ar_sha(ah), CLLADDR(sdl), sdl->sdl_alen)) { 1242 memcmp(ar_sha(ah), CLLADDR(sdl), sdl->sdl_alen)) {
1241 if (rt->rt_flags & RTF_STATIC) { 1243 if (rt->rt_flags & RTF_STATIC) {
1242 ARP_STATINC(ARP_STAT_RCVOVERPERM); 1244 ARP_STATINC(ARP_STAT_RCVOVERPERM);
1243 if (!log_permanent_modify) 1245 if (!log_permanent_modify)
1244 goto out; 1246 goto out;
1245 log(LOG_INFO, 1247 log(LOG_INFO,
1246 "%s tried to overwrite permanent arp info" 1248 "%s tried to overwrite permanent arp info"
1247 " for %s\n", 1249 " for %s\n",
1248 lla_snprintf(ar_sha(ah), ah->ar_hln), 1250 lla_snprintf(ar_sha(ah), ah->ar_hln),
1249 in_fmtaddr(isaddr)); 1251 in_fmtaddr(isaddr));
1250 goto out; 1252 goto out;
1251 } else if (rt->rt_ifp != ifp) { 1253 } else if (rt->rt_ifp != ifp) {
1252 ARP_STATINC(ARP_STAT_RCVOVERINT); 1254 ARP_STATINC(ARP_STAT_RCVOVERINT);
1253 if (!log_wrong_iface) 1255 if (!log_wrong_iface)
1254 goto out; 1256 goto out;
1255 log(LOG_INFO, 1257 log(LOG_INFO,
1256 "%s on %s tried to overwrite " 1258 "%s on %s tried to overwrite "
1257 "arp info for %s on %s\n", 1259 "arp info for %s on %s\n",
1258 lla_snprintf(ar_sha(ah), ah->ar_hln), 1260 lla_snprintf(ar_sha(ah), ah->ar_hln),
1259 ifp->if_xname, in_fmtaddr(isaddr), 1261 ifp->if_xname, in_fmtaddr(isaddr),
1260 rt->rt_ifp->if_xname); 1262 rt->rt_ifp->if_xname);
1261 goto out; 1263 goto out;
1262 } else { 1264 } else {
1263 ARP_STATINC(ARP_STAT_RCVOVER); 1265 ARP_STATINC(ARP_STAT_RCVOVER);
1264 if (log_movements) 1266 if (log_movements)
1265 log(LOG_INFO, "arp info overwritten " 1267 log(LOG_INFO, "arp info overwritten "
1266 "for %s by %s\n", 1268 "for %s by %s\n",
1267 in_fmtaddr(isaddr), 1269 in_fmtaddr(isaddr),
1268 lla_snprintf(ar_sha(ah), 1270 lla_snprintf(ar_sha(ah),
1269 ah->ar_hln)); 1271 ah->ar_hln));
1270 } 1272 }
1271 } 1273 }
1272 /* 1274 /*
1273 * sanity check for the address length. 1275 * sanity check for the address length.
1274 * XXX this does not work for protocols with variable address 1276 * XXX this does not work for protocols with variable address
1275 * length. -is 1277 * length. -is
1276 */ 1278 */
1277 if (sdl->sdl_alen && 1279 if (sdl->sdl_alen &&
1278 sdl->sdl_alen != ah->ar_hln) { 1280 sdl->sdl_alen != ah->ar_hln) {
1279 ARP_STATINC(ARP_STAT_RCVLENCHG); 1281 ARP_STATINC(ARP_STAT_RCVLENCHG);
1280 log(LOG_WARNING, 1282 log(LOG_WARNING,
1281 "arp from %s: new addr len %d, was %d\n", 1283 "arp from %s: new addr len %d, was %d\n",
1282 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen); 1284 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1283 } 1285 }
1284 if (ifp->if_addrlen != ah->ar_hln) { 1286 if (ifp->if_addrlen != ah->ar_hln) {
1285 ARP_STATINC(ARP_STAT_RCVBADLEN); 1287 ARP_STATINC(ARP_STAT_RCVBADLEN);
1286 log(LOG_WARNING, 1288 log(LOG_WARNING,
1287 "arp from %s: addr len: new %d, i/f %d (ignored)\n", 1289 "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1288 in_fmtaddr(isaddr), ah->ar_hln, 1290 in_fmtaddr(isaddr), ah->ar_hln,
1289 ifp->if_addrlen); 1291 ifp->if_addrlen);
1290 goto reply; 1292 goto reply;
1291 } 1293 }
1292#if NTOKEN > 0 1294#if NTOKEN > 0
1293 /* 1295 /*
1294 * XXX uses m_data and assumes the complete answer including 1296 * XXX uses m_data and assumes the complete answer including
1295 * XXX token-ring headers is in the same buf 1297 * XXX token-ring headers is in the same buf
1296 */ 1298 */
1297 if (ifp->if_type == IFT_ISO88025) { 1299 if (ifp->if_type == IFT_ISO88025) {
1298 struct token_header *trh; 1300 struct token_header *trh;
1299 1301
1300 trh = (struct token_header *)M_TRHSTART(m); 1302 trh = (struct token_header *)M_TRHSTART(m);
1301 if (trh->token_shost[0] & TOKEN_RI_PRESENT) { 1303 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1302 struct token_rif *rif; 1304 struct token_rif *rif;
1303 size_t riflen; 1305 size_t riflen;
1304 1306
1305 rif = TOKEN_RIF(trh); 1307 rif = TOKEN_RIF(trh);
1306 riflen = (ntohs(rif->tr_rcf) & 1308 riflen = (ntohs(rif->tr_rcf) &
1307 TOKEN_RCF_LEN_MASK) >> 8; 1309 TOKEN_RCF_LEN_MASK) >> 8;
1308 1310
1309 if (riflen > 2 && 1311 if (riflen > 2 &&
1310 riflen < sizeof(struct token_rif) && 1312 riflen < sizeof(struct token_rif) &&
1311 (riflen & 1) == 0) { 1313 (riflen & 1) == 0) {
1312 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION); 1314 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1313 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK); 1315 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1314 memcpy(TOKEN_RIF(la), rif, riflen); 1316 memcpy(TOKEN_RIF(la), rif, riflen);
1315 } 1317 }
1316 } 1318 }
1317 } 1319 }
1318#endif /* NTOKEN > 0 */ 1320#endif /* NTOKEN > 0 */
1319 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, ar_sha(ah), 1321 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, ar_sha(ah),
1320 ah->ar_hln); 1322 ah->ar_hln);
1321 if (rt->rt_expire) { 1323 if (rt->rt_expire) {
1322 rt->rt_expire = time_uptime + arpt_keep; 1324 rt->rt_expire = time_uptime + arpt_keep;
1323 1325
1324 KASSERT((la->la_flags & LLE_STATIC) == 0); 1326 KASSERT((la->la_flags & LLE_STATIC) == 0);
1325 LLE_ADDREF(la); 1327 LLE_ADDREF(la);
1326 callout_reset(&la->la_timer, hz * arpt_keep, arptimer, la); 1328 callout_reset(&la->la_timer, hz * arpt_keep, arptimer, la);
1327 } 1329 }
1328 rt->rt_flags &= ~RTF_REJECT; 1330 rt->rt_flags &= ~RTF_REJECT;
1329 la->la_asked = 0; 1331 la->la_asked = 0;
1330 1332
1331 if (la->la_hold != NULL) { 1333 if (la->la_hold != NULL) {
1332 int n = la->la_numheld; 1334 int n = la->la_numheld;
1333 struct mbuf *m_hold, *m_hold_next; 1335 struct mbuf *m_hold, *m_hold_next;
1334 1336
1335 m_hold = la->la_hold; 1337 m_hold = la->la_hold;
1336 la->la_hold = NULL; 1338 la->la_hold = NULL;
1337 la->la_numheld = 0; 1339 la->la_numheld = 0;
1338 /* 1340 /*
1339 * We have to unlock here because if_output would call 1341 * We have to unlock here because if_output would call
1340 * arpresolve 1342 * arpresolve
1341 */ 1343 */
1342 LLE_WUNLOCK(la); 1344 LLE_WUNLOCK(la);
1343 ARP_STATADD(ARP_STAT_DFRSENT, n); 1345 ARP_STATADD(ARP_STAT_DFRSENT, n);
1344 for (; m_hold != NULL; m_hold = m_hold_next) { 1346 for (; m_hold != NULL; m_hold = m_hold_next) {
1345 m_hold_next = m_hold->m_nextpkt; 1347 m_hold_next = m_hold->m_nextpkt;
1346 m_hold->m_nextpkt = NULL; 1348 m_hold->m_nextpkt = NULL;
1347 (*ifp->if_output)(ifp, m_hold, rt_getkey(rt), rt); 1349 (*ifp->if_output)(ifp, m_hold, rt_getkey(rt), rt);
1348 } 1350 }
1349 } else 1351 } else
1350 LLE_WUNLOCK(la); 1352 LLE_WUNLOCK(la);
1351 la = NULL; 1353 la = NULL;
1352 } 1354 }
1353reply: 1355reply:
1354 if (la != NULL) { 1356 if (la != NULL) {
1355 LLE_WUNLOCK(la); 1357 LLE_WUNLOCK(la);
1356 la = NULL; 1358 la = NULL;
1357 } 1359 }
1358 if (op != ARPOP_REQUEST) { 1360 if (op != ARPOP_REQUEST) {
1359 if (op == ARPOP_REPLY) 1361 if (op == ARPOP_REPLY)
1360 ARP_STATINC(ARP_STAT_RCVREPLY); 1362 ARP_STATINC(ARP_STAT_RCVREPLY);
1361 goto out; 1363 goto out;
1362 } 1364 }
1363 ARP_STATINC(ARP_STAT_RCVREQUEST); 1365 ARP_STATINC(ARP_STAT_RCVREQUEST);
1364 if (in_hosteq(itaddr, myaddr)) { 1366 if (in_hosteq(itaddr, myaddr)) {
1365 /* If our address is unuseable, don't reply */ 1367 /* If our address is unuseable, don't reply */
1366 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) 1368 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1367 goto out; 1369 goto out;
1368 /* I am the target */ 1370 /* I am the target */
1369 tha = ar_tha(ah); 1371 tha = ar_tha(ah);
1370 if (tha) 1372 if (tha)
1371 memcpy(tha, ar_sha(ah), ah->ar_hln); 1373 memcpy(tha, ar_sha(ah), ah->ar_hln);
1372 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln); 1374 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1373 } else { 1375 } else {
1374 la = arplookup(ifp, m, &itaddr, 0, SIN_PROXY, 0, NULL); 1376 la = arplookup(ifp, m, &itaddr, 0, SIN_PROXY, 0, NULL);
1375 if (la == NULL) 1377 if (la == NULL)
1376 goto out; 1378 goto out;
1377 rt = la->la_rt; 1379 rt = la->la_rt;
1378 LLE_RUNLOCK(la); 1380 LLE_RUNLOCK(la);
1379 la = NULL; 1381 la = NULL;
1380 if (rt->rt_ifp->if_type == IFT_CARP && 1382 if (rt->rt_ifp->if_type == IFT_CARP &&
1381 m->m_pkthdr.rcvif->if_type != IFT_CARP) 1383 m->m_pkthdr.rcvif->if_type != IFT_CARP)
1382 goto out; 1384 goto out;
1383 tha = ar_tha(ah); 1385 tha = ar_tha(ah);
1384 if (tha) 1386 if (tha)
1385 memcpy(tha, ar_sha(ah), ah->ar_hln); 1387 memcpy(tha, ar_sha(ah), ah->ar_hln);
1386 sdl = satosdl(rt->rt_gateway); 1388 sdl = satosdl(rt->rt_gateway);
1387 memcpy(ar_sha(ah), CLLADDR(sdl), ah->ar_hln); 1389 memcpy(ar_sha(ah), CLLADDR(sdl), ah->ar_hln);
1388 } 1390 }
1389 1391
1390 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 1392 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1391 memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 1393 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1392 ah->ar_op = htons(ARPOP_REPLY); 1394 ah->ar_op = htons(ARPOP_REPLY);
1393 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 1395 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1394 switch (ifp->if_type) { 1396 switch (ifp->if_type) {
1395 case IFT_IEEE1394: 1397 case IFT_IEEE1394:
1396 /* 1398 /*
1397 * ieee1394 arp reply is broadcast 1399 * ieee1394 arp reply is broadcast
1398 */ 1400 */
1399 m->m_flags &= ~M_MCAST; 1401 m->m_flags &= ~M_MCAST;
1400 m->m_flags |= M_BCAST; 1402 m->m_flags |= M_BCAST;
1401 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln; 1403 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1402 break; 1404 break;
1403 1405
1404 default: 1406 default:
1405 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 1407 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1406 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 1408 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1407 break; 1409 break;
1408 } 1410 }
1409 m->m_pkthdr.len = m->m_len; 1411 m->m_pkthdr.len = m->m_len;
1410 sa.sa_family = AF_ARP; 1412 sa.sa_family = AF_ARP;
1411 sa.sa_len = 2; 1413 sa.sa_len = 2;
1412 arps = ARP_STAT_GETREF(); 1414 arps = ARP_STAT_GETREF();
1413 arps[ARP_STAT_SNDTOTAL]++; 1415 arps[ARP_STAT_SNDTOTAL]++;
1414 arps[ARP_STAT_SNDREPLY]++; 1416 arps[ARP_STAT_SNDREPLY]++;
1415 ARP_STAT_PUTREF(); 1417 ARP_STAT_PUTREF();
1416 (*ifp->if_output)(ifp, m, &sa, NULL); 1418 (*ifp->if_output)(ifp, m, &sa, NULL);
1417 return; 1419 return;
1418 1420
1419out: 1421out:
1420 if (la != NULL) 1422 if (la != NULL)
1421 LLE_WUNLOCK(la); 1423 LLE_WUNLOCK(la);
1422 m_freem(m); 1424 m_freem(m);
1423} 1425}
1424 1426
1425/* 1427/*
1426 * Free an arp entry. 1428 * Free an arp entry.
1427 */ 1429 */
1428static void arptfree(struct rtentry *rt) 1430static void arptfree(struct rtentry *rt)
1429{ 1431{
1430 1432
1431 rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL); 1433 rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL);
1432 rtfree(rt); 1434 rtfree(rt);
1433} 1435}
1434 1436
1435/* 1437/*
1436 * Lookup or enter a new address in arptab. 1438 * Lookup or enter a new address in arptab.
1437 */ 1439 */
1438static struct llentry * 1440static struct llentry *
1439arplookup(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr, 1441arplookup(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr,
1440 int create, int proxy, int wlock, struct rtentry *rt0) 1442 int create, int proxy, int wlock, struct rtentry *rt0)
1441{ 1443{
1442 struct arphdr *ah; 1444 struct arphdr *ah;
1443 struct rtentry *rt; 1445 struct rtentry *rt;
1444 struct sockaddr_inarp sin; 1446 struct sockaddr_inarp sin;
1445 const char *why = NULL; 1447 const char *why = NULL;
1446 1448
1447 ah = mtod(m, struct arphdr *); 1449 ah = mtod(m, struct arphdr *);
1448 if (rt0 == NULL) { 1450 if (rt0 == NULL) {
1449 memset(&sin, 0, sizeof(sin)); 1451 memset(&sin, 0, sizeof(sin));
1450 sin.sin_len = sizeof(sin); 1452 sin.sin_len = sizeof(sin);
1451 sin.sin_family = AF_INET; 1453 sin.sin_family = AF_INET;
1452 sin.sin_addr = *addr; 1454 sin.sin_addr = *addr;
1453 sin.sin_other = proxy ? SIN_PROXY : 0; 1455 sin.sin_other = proxy ? SIN_PROXY : 0;
1454 rt = rtalloc1(sintosa(&sin), create); 1456 rt = rtalloc1(sintosa(&sin), create);
1455 if (rt == NULL) 1457 if (rt == NULL)
1456 return NULL; 1458 return NULL;
1457 rt->rt_refcnt--; 1459 rt->rt_refcnt--;
1458 } else 1460 } else
1459 rt = rt0; 1461 rt = rt0;
1460 1462
1461#define IS_LLINFO(__rt) \ 1463#define IS_LLINFO(__rt) \
1462 (((__rt)->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO && \ 1464 (((__rt)->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO && \
1463 (__rt)->rt_gateway->sa_family == AF_LINK) 1465 (__rt)->rt_gateway->sa_family == AF_LINK)
1464 1466
1465 1467
1466 if (IS_LLINFO(rt)) { 1468 if (IS_LLINFO(rt)) {
1467 struct llentry *la; 1469 struct llentry *la;
1468 int flags = wlock ? LLE_EXCLUSIVE : 0; 1470 int flags = wlock ? LLE_EXCLUSIVE : 0;
1469 1471
1470 IF_AFDATA_RLOCK(ifp); 1472 IF_AFDATA_RLOCK(ifp);
1471 la = lla_lookup(LLTABLE(ifp), flags, rt_getkey(rt)); 1473 la = lla_lookup(LLTABLE(ifp), flags, rt_getkey(rt));
1472 IF_AFDATA_RUNLOCK(ifp); 1474 IF_AFDATA_RUNLOCK(ifp);
1473 1475
1474 if (la == NULL && create) { 1476 if (la == NULL && create) {
1475 IF_AFDATA_WLOCK(ifp); 1477 IF_AFDATA_WLOCK(ifp);
1476 la = lla_create(LLTABLE(ifp), flags, rt_getkey(rt)); 1478 la = lla_create(LLTABLE(ifp), flags, rt_getkey(rt));
1477 IF_AFDATA_WUNLOCK(ifp); 1479 IF_AFDATA_WUNLOCK(ifp);
1478 } 1480 }
1479 1481
1480 return la; 1482 return la;
1481 } 1483 }
1482 1484
1483 if (create) { 1485 if (create) {
1484 if (rt->rt_flags & RTF_GATEWAY) { 1486 if (rt->rt_flags & RTF_GATEWAY) {
1485 if (log_unknown_network) 1487 if (log_unknown_network)
1486 why = "host is not on local network"; 1488 why = "host is not on local network";
1487 } else if ((rt->rt_flags & RTF_LLINFO) == 0) { 1489 } else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1488 ARP_STATINC(ARP_STAT_ALLOCFAIL); 1490 ARP_STATINC(ARP_STAT_ALLOCFAIL);
1489 why = "could not allocate llinfo"; 1491 why = "could not allocate llinfo";
1490 } else 1492 } else
1491 why = "gateway route is not ours"; 1493 why = "gateway route is not ours";
1492 if (why) { 1494 if (why) {
1493 log(LOG_DEBUG, "arplookup: unable to enter address" 1495 log(LOG_DEBUG, "arplookup: unable to enter address"
1494 " for %s@%s on %s (%s)\n", in_fmtaddr(*addr), 1496 " for %s@%s on %s (%s)\n", in_fmtaddr(*addr),
1495 lla_snprintf(ar_sha(ah), ah->ar_hln), 1497 lla_snprintf(ar_sha(ah), ah->ar_hln),
1496 (ifp) ? ifp->if_xname : "null", why); 1498 (ifp) ? ifp->if_xname : "null", why);
1497 } 1499 }
1498 if ((rt->rt_flags & RTF_CLONED) != 0) { 1500 if ((rt->rt_flags & RTF_CLONED) != 0) {
1499 rtrequest(RTM_DELETE, rt_getkey(rt), 1501 rtrequest(RTM_DELETE, rt_getkey(rt),
1500 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL); 1502 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
1501 } 1503 }
1502 } 1504 }
1503 return NULL; 1505 return NULL;
1504} 1506}
1505 1507
1506int 1508int
1507arpioctl(u_long cmd, void *data) 1509arpioctl(u_long cmd, void *data)
1508{ 1510{
1509 1511
1510 return EOPNOTSUPP; 1512 return EOPNOTSUPP;
1511} 1513}
1512 1514
1513void 1515void
1514arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1516arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1515{ 1517{
1516 struct in_addr *ip; 1518 struct in_addr *ip;
1517 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1519 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1518 1520
1519 /* 1521 /*
1520 * Warn the user if another station has this IP address, 1522 * Warn the user if another station has this IP address,
1521 * but only if the interface IP address is not zero. 1523 * but only if the interface IP address is not zero.
1522 */ 1524 */
1523 ip = &IA_SIN(ifa)->sin_addr; 1525 ip = &IA_SIN(ifa)->sin_addr;
1524 if (!in_nullhost(*ip) && 1526 if (!in_nullhost(*ip) &&
1525 (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) == 0) 1527 (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) == 0)
1526 arprequest(ifp, ip, ip, CLLADDR(ifp->if_sadl)); 1528 arprequest(ifp, ip, ip, CLLADDR(ifp->if_sadl));
1527 1529
1528 ifa->ifa_rtrequest = arp_rtrequest; 1530 ifa->ifa_rtrequest = arp_rtrequest;
1529 ifa->ifa_flags |= RTF_CLONING; 1531 ifa->ifa_flags |= RTF_CLONING;
1530 1532
1531 /* ARP will handle DAD for this address. */ 1533 /* ARP will handle DAD for this address. */
1532 if (ia->ia4_flags & IN_IFF_TRYTENTATIVE) { 1534 if (ia->ia4_flags & IN_IFF_TRYTENTATIVE) {
1533 ia->ia4_flags |= IN_IFF_TENTATIVE; 1535 ia->ia4_flags |= IN_IFF_TENTATIVE;
1534 ia->ia_dad_start = arp_dad_start; 1536 ia->ia_dad_start = arp_dad_start;
1535 ia->ia_dad_stop = arp_dad_stop; 1537 ia->ia_dad_stop = arp_dad_stop;
1536 } 1538 }
1537} 1539}
1538 1540
1539TAILQ_HEAD(dadq_head, dadq); 1541TAILQ_HEAD(dadq_head, dadq);
1540struct dadq { 1542struct dadq {
1541 TAILQ_ENTRY(dadq) dad_list; 1543 TAILQ_ENTRY(dadq) dad_list;
1542 struct ifaddr *dad_ifa; 1544 struct ifaddr *dad_ifa;
1543 int dad_count; /* max ARP to send */ 1545 int dad_count; /* max ARP to send */
1544 int dad_arp_tcount; /* # of trials to send ARP */ 1546 int dad_arp_tcount; /* # of trials to send ARP */
1545 int dad_arp_ocount; /* ARP sent so far */ 1547 int dad_arp_ocount; /* ARP sent so far */
1546 int dad_arp_announce; /* max ARP announcements */ 1548 int dad_arp_announce; /* max ARP announcements */
1547 int dad_arp_acount; /* # of announcements */ 1549 int dad_arp_acount; /* # of announcements */
1548 struct callout dad_timer_ch; 1550 struct callout dad_timer_ch;
1549}; 1551};
1550MALLOC_JUSTDEFINE(M_IPARP, "ARP DAD", "ARP DAD Structure"); 1552MALLOC_JUSTDEFINE(M_IPARP, "ARP DAD", "ARP DAD Structure");
1551 1553
1552static struct dadq_head dadq; 1554static struct dadq_head dadq;
1553static int dad_init = 0; 1555static int dad_init = 0;
1554static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */ 1556static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
1555 1557
1556static struct dadq * 1558static struct dadq *
1557arp_dad_find(struct ifaddr *ifa) 1559arp_dad_find(struct ifaddr *ifa)
1558{ 1560{
1559 struct dadq *dp; 1561 struct dadq *dp;
1560 1562
1561 TAILQ_FOREACH(dp, &dadq, dad_list) { 1563 TAILQ_FOREACH(dp, &dadq, dad_list) {
1562 if (dp->dad_ifa == ifa) 1564 if (dp->dad_ifa == ifa)
1563 return dp; 1565 return dp;
1564 } 1566 }
1565 return NULL; 1567 return NULL;
1566} 1568}
1567 1569
1568static void 1570static void
1569arp_dad_starttimer(struct dadq *dp, int ticks) 1571arp_dad_starttimer(struct dadq *dp, int ticks)
1570{ 1572{
1571 1573
1572 callout_reset(&dp->dad_timer_ch, ticks, 1574 callout_reset(&dp->dad_timer_ch, ticks,
1573 (void (*)(void *))arp_dad_timer, (void *)dp->dad_ifa); 1575 (void (*)(void *))arp_dad_timer, (void *)dp->dad_ifa);
1574} 1576}
1575 1577
1576static void 1578static void
1577arp_dad_stoptimer(struct dadq *dp) 1579arp_dad_stoptimer(struct dadq *dp)
1578{ 1580{
1579 1581
1580 callout_stop(&dp->dad_timer_ch); 1582 callout_stop(&dp->dad_timer_ch);
1581} 1583}
1582 1584
1583static void 1585static void
1584arp_dad_output(struct dadq *dp, struct ifaddr *ifa) 1586arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
1585{ 1587{
1586 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1588 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1587 struct ifnet *ifp = ifa->ifa_ifp; 1589 struct ifnet *ifp = ifa->ifa_ifp;
1588 struct in_addr sip; 1590 struct in_addr sip;
1589 1591
1590 dp->dad_arp_tcount++; 1592 dp->dad_arp_tcount++;
1591 if ((ifp->if_flags & IFF_UP) == 0) 1593 if ((ifp->if_flags & IFF_UP) == 0)
1592 return; 1594 return;
1593 if ((ifp->if_flags & IFF_RUNNING) == 0) 1595 if ((ifp->if_flags & IFF_RUNNING) == 0)
1594 return; 1596 return;
1595 1597
1596 dp->dad_arp_tcount = 0; 1598 dp->dad_arp_tcount = 0;
1597 dp->dad_arp_ocount++; 1599 dp->dad_arp_ocount++;
1598 1600
1599 memset(&sip, 0, sizeof(sip)); 1601 memset(&sip, 0, sizeof(sip));
1600 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr, 1602 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
1601 CLLADDR(ifa->ifa_ifp->if_sadl)); 1603 CLLADDR(ifa->ifa_ifp->if_sadl));
1602} 1604}
1603 1605
1604/* 1606/*
1605 * Start Duplicate Address Detection (DAD) for specified interface address. 1607 * Start Duplicate Address Detection (DAD) for specified interface address.
1606 */ 1608 */
1607static void 1609static void
1608arp_dad_start(struct ifaddr *ifa) 1610arp_dad_start(struct ifaddr *ifa)
1609{ 1611{
1610 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1612 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1611 struct dadq *dp; 1613 struct dadq *dp;
1612 1614
1613 if (!dad_init) { 1615 if (!dad_init) {
1614 TAILQ_INIT(&dadq); 1616 TAILQ_INIT(&dadq);
1615 dad_init++; 1617 dad_init++;
1616 } 1618 }
1617 1619
1618 /* 1620 /*
1619 * If we don't need DAD, don't do it. 1621 * If we don't need DAD, don't do it.
1620 * - DAD is disabled (ip_dad_count == 0) 1622 * - DAD is disabled (ip_dad_count == 0)
1621 */ 1623 */
1622 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) { 1624 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
1623 log(LOG_DEBUG, 1625 log(LOG_DEBUG,
1624 "arp_dad_start: called with non-tentative address " 1626 "arp_dad_start: called with non-tentative address "
1625 "%s(%s)\n", 1627 "%s(%s)\n",
1626 in_fmtaddr(ia->ia_addr.sin_addr), 1628 in_fmtaddr(ia->ia_addr.sin_addr),
1627 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1629 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1628 return; 1630 return;
1629 } 1631 }
1630 if (!ip_dad_count) { 1632 if (!ip_dad_count) {
1631 struct in_addr *ip = &IA_SIN(ifa)->sin_addr; 1633 struct in_addr *ip = &IA_SIN(ifa)->sin_addr;
1632 1634
1633 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1635 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1634 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 1636 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1635 arprequest(ifa->ifa_ifp, ip, ip, 1637 arprequest(ifa->ifa_ifp, ip, ip,
1636 CLLADDR(ifa->ifa_ifp->if_sadl)); 1638 CLLADDR(ifa->ifa_ifp->if_sadl));
1637 return; 1639 return;
1638 } 1640 }
1639 if (ifa->ifa_ifp == NULL) 1641 if (ifa->ifa_ifp == NULL)
1640 panic("arp_dad_start: ifa->ifa_ifp == NULL"); 1642 panic("arp_dad_start: ifa->ifa_ifp == NULL");
1641 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) 1643 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1642 return; 1644 return;
1643 if (arp_dad_find(ifa) != NULL) { 1645 if (arp_dad_find(ifa) != NULL) {
1644 /* DAD already in progress */ 1646 /* DAD already in progress */
1645 return; 1647 return;
1646 } 1648 }
1647 1649
1648 dp = malloc(sizeof(*dp), M_IPARP, M_NOWAIT); 1650 dp = malloc(sizeof(*dp), M_IPARP, M_NOWAIT);
1649 if (dp == NULL) { 1651 if (dp == NULL) {
1650 log(LOG_ERR, "arp_dad_start: memory allocation failed for " 1652 log(LOG_ERR, "arp_dad_start: memory allocation failed for "
1651 "%s(%s)\n", 1653 "%s(%s)\n",
1652 in_fmtaddr(ia->ia_addr.sin_addr), 1654 in_fmtaddr(ia->ia_addr.sin_addr),
1653 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1655 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1654 return; 1656 return;
1655 } 1657 }
1656 memset(dp, 0, sizeof(*dp)); 1658 memset(dp, 0, sizeof(*dp));
1657 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE); 1659 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1658 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list); 1660 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1659 1661
1660 arplog((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), 1662 arplog((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1661 in_fmtaddr(ia->ia_addr.sin_addr))); 1663 in_fmtaddr(ia->ia_addr.sin_addr)));
1662 1664
1663 /* 1665 /*
1664 * Send ARP packet for DAD, ip_dad_count times. 1666 * Send ARP packet for DAD, ip_dad_count times.
1665 * Note that we must delay the first transmission. 1667 * Note that we must delay the first transmission.
1666 */ 1668 */
1667 dp->dad_ifa = ifa; 1669 dp->dad_ifa = ifa;
1668 ifaref(ifa); /* just for safety */ 1670 ifaref(ifa); /* just for safety */
1669 dp->dad_count = ip_dad_count; 1671 dp->dad_count = ip_dad_count;
1670 dp->dad_arp_announce = 0; /* Will be set when starting to announce */ 1672 dp->dad_arp_announce = 0; /* Will be set when starting to announce */
1671 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0; 1673 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
1672 1674
1673 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz)); 1675 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
1674} 1676}
1675 1677
1676/* 1678/*
1677 * terminate DAD unconditionally. used for address removals. 1679 * terminate DAD unconditionally. used for address removals.
1678 */ 1680 */
1679static void 1681static void
1680arp_dad_stop(struct ifaddr *ifa) 1682arp_dad_stop(struct ifaddr *ifa)
1681{ 1683{
1682 struct dadq *dp; 1684 struct dadq *dp;
1683 1685
1684 if (!dad_init) 1686 if (!dad_init)
1685 return; 1687 return;
1686 dp = arp_dad_find(ifa); 1688 dp = arp_dad_find(ifa);
1687 if (dp == NULL) { 1689 if (dp == NULL) {
1688 /* DAD wasn't started yet */ 1690 /* DAD wasn't started yet */
1689 return; 1691 return;
1690 } 1692 }
1691 1693
1692 arp_dad_stoptimer(dp); 1694 arp_dad_stoptimer(dp);
1693 1695
1694 TAILQ_REMOVE(&dadq, dp, dad_list); 1696 TAILQ_REMOVE(&dadq, dp, dad_list);
1695 free(dp, M_IPARP); 1697 free(dp, M_IPARP);
1696 dp = NULL; 1698 dp = NULL;
1697 ifafree(ifa); 1699 ifafree(ifa);
1698} 1700}
1699 1701
1700static void 1702static void
1701arp_dad_timer(struct ifaddr *ifa) 1703arp_dad_timer(struct ifaddr *ifa)
1702{ 1704{
1703 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1705 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1704 struct dadq *dp; 1706 struct dadq *dp;
1705 struct in_addr *ip; 1707 struct in_addr *ip;
1706 1708
1707 mutex_enter(softnet_lock); 1709 mutex_enter(softnet_lock);
1708 KERNEL_LOCK(1, NULL); 1710 KERNEL_LOCK(1, NULL);
1709 1711
1710 /* Sanity check */ 1712 /* Sanity check */
1711 if (ia == NULL) { 1713 if (ia == NULL) {
1712 log(LOG_ERR, "arp_dad_timer: called with null parameter\n"); 1714 log(LOG_ERR, "arp_dad_timer: called with null parameter\n");
1713 goto done; 1715 goto done;
1714 } 1716 }
1715 dp = arp_dad_find(ifa); 1717 dp = arp_dad_find(ifa);
1716 if (dp == NULL) { 1718 if (dp == NULL) {
1717 log(LOG_ERR, "arp_dad_timer: DAD structure not found\n"); 1719 log(LOG_ERR, "arp_dad_timer: DAD structure not found\n");
1718 goto done; 1720 goto done;
1719 } 1721 }
1720 if (ia->ia4_flags & IN_IFF_DUPLICATED) { 1722 if (ia->ia4_flags & IN_IFF_DUPLICATED) {
1721 log(LOG_ERR, "nd4_dad_timer: called with duplicate address " 1723 log(LOG_ERR, "nd4_dad_timer: called with duplicate address "
1722 "%s(%s)\n", 1724 "%s(%s)\n",
1723 in_fmtaddr(ia->ia_addr.sin_addr), 1725 in_fmtaddr(ia->ia_addr.sin_addr),
1724 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1726 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1725 goto done; 1727 goto done;
1726 } 1728 }
1727 if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0){ 1729 if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0){
1728 log(LOG_ERR, "arp_dad_timer: called with non-tentative address " 1730 log(LOG_ERR, "arp_dad_timer: called with non-tentative address "
1729 "%s(%s)\n", 1731 "%s(%s)\n",
1730 in_fmtaddr(ia->ia_addr.sin_addr), 1732 in_fmtaddr(ia->ia_addr.sin_addr),
1731 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1733 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1732 goto done; 1734 goto done;
1733 } 1735 }
1734 1736
1735 /* timeouted with IFF_{RUNNING,UP} check */ 1737 /* timeouted with IFF_{RUNNING,UP} check */
1736 if (dp->dad_arp_tcount > dad_maxtry) { 1738 if (dp->dad_arp_tcount > dad_maxtry) {
1737 arplog((LOG_INFO, "%s: could not run DAD, driver problem?\n", 1739 arplog((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1738 if_name(ifa->ifa_ifp))); 1740 if_name(ifa->ifa_ifp)));
1739 1741
1740 TAILQ_REMOVE(&dadq, dp, dad_list); 1742 TAILQ_REMOVE(&dadq, dp, dad_list);
1741 free(dp, M_IPARP); 1743 free(dp, M_IPARP);
1742 dp = NULL; 1744 dp = NULL;
1743 ifafree(ifa); 1745 ifafree(ifa);
1744 goto done; 1746 goto done;
1745 } 1747 }
1746 1748
1747 /* Need more checks? */ 1749 /* Need more checks? */
1748 if (dp->dad_arp_ocount < dp->dad_count) { 1750 if (dp->dad_arp_ocount < dp->dad_count) {
1749 int adelay; 1751 int adelay;
1750 1752
1751 /* 1753 /*
1752 * We have more ARP to go. Send ARP packet for DAD. 1754 * We have more ARP to go. Send ARP packet for DAD.
1753 */ 1755 */
1754 arp_dad_output(dp, ifa); 1756 arp_dad_output(dp, ifa);
1755 if (dp->dad_arp_ocount < dp->dad_count) 1757 if (dp->dad_arp_ocount < dp->dad_count)
1756 adelay = (PROBE_MIN * hz) + 1758 adelay = (PROBE_MIN * hz) +
1757 (cprng_fast32() % 1759 (cprng_fast32() %
1758 ((PROBE_MAX * hz) - (PROBE_MIN * hz))); 1760 ((PROBE_MAX * hz) - (PROBE_MIN * hz)));
1759 else 1761 else
1760 adelay = ANNOUNCE_WAIT * hz; 1762 adelay = ANNOUNCE_WAIT * hz;
1761 arp_dad_starttimer(dp, adelay); 1763 arp_dad_starttimer(dp, adelay);
1762 goto done; 1764 goto done;
1763 } else if (dp->dad_arp_acount == 0) { 1765 } else if (dp->dad_arp_acount == 0) {
1764 /* 1766 /*
1765 * We are done with DAD. 1767 * We are done with DAD.
1766 * No duplicate address found. 1768 * No duplicate address found.
1767 */ 1769 */
1768 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1770 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1769 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 1771 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1770 arplog((LOG_DEBUG, 1772 arplog((LOG_DEBUG,
1771 "%s: DAD complete for %s - no duplicates found\n", 1773 "%s: DAD complete for %s - no duplicates found\n",
1772 if_name(ifa->ifa_ifp), 1774 if_name(ifa->ifa_ifp),
1773 in_fmtaddr(ia->ia_addr.sin_addr))); 1775 in_fmtaddr(ia->ia_addr.sin_addr)));
1774 dp->dad_arp_announce = ANNOUNCE_NUM; 1776 dp->dad_arp_announce = ANNOUNCE_NUM;
1775 goto announce; 1777 goto announce;
1776 } else if (dp->dad_arp_acount < dp->dad_arp_announce) { 1778 } else if (dp->dad_arp_acount < dp->dad_arp_announce) {
1777announce: 1779announce:
1778 /* 1780 /*
1779 * Announce the address. 1781 * Announce the address.
1780 */ 1782 */
1781 ip = &IA_SIN(ifa)->sin_addr; 1783 ip = &IA_SIN(ifa)->sin_addr;
1782 arprequest(ifa->ifa_ifp, ip, ip, 1784 arprequest(ifa->ifa_ifp, ip, ip,
1783 CLLADDR(ifa->ifa_ifp->if_sadl)); 1785 CLLADDR(ifa->ifa_ifp->if_sadl));
1784 dp->dad_arp_acount++; 1786 dp->dad_arp_acount++;
1785 if (dp->dad_arp_acount < dp->dad_arp_announce) { 1787 if (dp->dad_arp_acount < dp->dad_arp_announce) {
1786 arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz); 1788 arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz);
1787 goto done; 1789 goto done;
1788 } 1790 }
1789 arplog((LOG_DEBUG, 1791 arplog((LOG_DEBUG,
1790 "%s: ARP announcement complete for %s\n", 1792 "%s: ARP announcement complete for %s\n",
1791 if_name(ifa->ifa_ifp), 1793 if_name(ifa->ifa_ifp),
1792 in_fmtaddr(ia->ia_addr.sin_addr))); 1794 in_fmtaddr(ia->ia_addr.sin_addr)));
1793 } 1795 }
1794 1796
1795 TAILQ_REMOVE(&dadq, dp, dad_list); 1797 TAILQ_REMOVE(&dadq, dp, dad_list);
1796 free(dp, M_IPARP); 1798 free(dp, M_IPARP);
1797 dp = NULL; 1799 dp = NULL;
1798 ifafree(ifa); 1800 ifafree(ifa);
1799 1801
1800done: 1802done:
1801 KERNEL_UNLOCK_ONE(NULL); 1803 KERNEL_UNLOCK_ONE(NULL);
1802 mutex_exit(softnet_lock); 1804 mutex_exit(softnet_lock);
1803} 1805}
1804 1806
1805static void 1807static void
1806arp_dad_duplicated(struct ifaddr *ifa) 1808arp_dad_duplicated(struct ifaddr *ifa)
1807{ 1809{
1808 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1810 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1809 struct ifnet *ifp; 1811 struct ifnet *ifp;
1810 struct dadq *dp; 1812 struct dadq *dp;
1811 1813
1812 dp = arp_dad_find(ifa); 1814 dp = arp_dad_find(ifa);
1813 if (dp == NULL) { 1815 if (dp == NULL) {
1814 log(LOG_ERR, "arp_dad_duplicated: DAD structure not found\n"); 1816 log(LOG_ERR, "arp_dad_duplicated: DAD structure not found\n");
1815 return; 1817 return;
1816 } 1818 }
1817 1819
1818 ifp = ifa->ifa_ifp; 1820 ifp = ifa->ifa_ifp;
1819 log(LOG_ERR, "%s: DAD detected duplicate IPv4 address %s: " 1821 log(LOG_ERR, "%s: DAD detected duplicate IPv4 address %s: "
1820 "ARP out=%d\n", 1822 "ARP out=%d\n",
1821 if_name(ifp), in_fmtaddr(ia->ia_addr.sin_addr), 1823 if_name(ifp), in_fmtaddr(ia->ia_addr.sin_addr),
1822 dp->dad_arp_ocount); 1824 dp->dad_arp_ocount);
1823 1825
1824 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1826 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1825 ia->ia4_flags |= IN_IFF_DUPLICATED; 1827 ia->ia4_flags |= IN_IFF_DUPLICATED;
1826 1828
1827 /* We are done with DAD, with duplicated address found. (failure) */ 1829 /* We are done with DAD, with duplicated address found. (failure) */
1828 arp_dad_stoptimer(dp); 1830 arp_dad_stoptimer(dp);
1829 1831
1830 /* Inform the routing socket that DAD has completed */ 1832 /* Inform the routing socket that DAD has completed */
1831 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 1833 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1832 1834
1833 TAILQ_REMOVE(&dadq, dp, dad_list); 1835 TAILQ_REMOVE(&dadq, dp, dad_list);
1834 free(dp, M_IPARP); 1836 free(dp, M_IPARP);
1835 dp = NULL; 1837 dp = NULL;
1836 ifafree(ifa); 1838 ifafree(ifa);
1837} 1839}
1838 1840
1839/* 1841/*
1840 * Called from 10 Mb/s Ethernet interrupt handlers 1842 * Called from 10 Mb/s Ethernet interrupt handlers
1841 * when ether packet type ETHERTYPE_REVARP 1843 * when ether packet type ETHERTYPE_REVARP