Fri Oct 11 18:22:14 2019 UTC ()
Pull up following revision(s) (requested by roy in ticket #300):

	sys/netinet/if_arp.c: revision 1.289

ARP: Don't defend ARP probes.

We should let the nature of ARP takes it's course here when our address
is neither tentative nor duplicated.
This allows the host to work with ARP ping, which was broken in r1.279.


(martin)
diff -r1.282.2.4 -r1.282.2.5 src/sys/netinet/if_arp.c

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

--- src/sys/netinet/if_arp.c 2019/09/30 15:55:40 1.282.2.4
+++ src/sys/netinet/if_arp.c 2019/10/11 18:22:14 1.282.2.5
@@ -1,2105 +1,2107 @@ @@ -1,2105 +1,2107 @@
1/* $NetBSD: if_arp.c,v 1.282.2.4 2019/09/30 15:55:40 martin Exp $ */ 1/* $NetBSD: if_arp.c,v 1.282.2.5 2019/10/11 18:22:14 martin 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.282.2.4 2019/09/30 15:55:40 martin Exp $"); 71__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.282.2.5 2019/10/11 18:22:14 martin 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#include "opt_net_mpsafe.h" 76#include "opt_net_mpsafe.h"
77#endif 77#endif
78 78
79#ifdef INET 79#ifdef INET
80 80
81#include "arp.h" 81#include "arp.h"
82#include "bridge.h" 82#include "bridge.h"
83 83
84#include <sys/param.h> 84#include <sys/param.h>
85#include <sys/systm.h> 85#include <sys/systm.h>
86#include <sys/callout.h> 86#include <sys/callout.h>
87#include <sys/kmem.h> 87#include <sys/kmem.h>
88#include <sys/mbuf.h> 88#include <sys/mbuf.h>
89#include <sys/socket.h> 89#include <sys/socket.h>
90#include <sys/time.h> 90#include <sys/time.h>
91#include <sys/timetc.h> 91#include <sys/timetc.h>
92#include <sys/kernel.h> 92#include <sys/kernel.h>
93#include <sys/errno.h> 93#include <sys/errno.h>
94#include <sys/ioctl.h> 94#include <sys/ioctl.h>
95#include <sys/syslog.h> 95#include <sys/syslog.h>
96#include <sys/proc.h> 96#include <sys/proc.h>
97#include <sys/protosw.h> 97#include <sys/protosw.h>
98#include <sys/domain.h> 98#include <sys/domain.h>
99#include <sys/sysctl.h> 99#include <sys/sysctl.h>
100#include <sys/socketvar.h> 100#include <sys/socketvar.h>
101#include <sys/percpu.h> 101#include <sys/percpu.h>
102#include <sys/cprng.h> 102#include <sys/cprng.h>
103#include <sys/kmem.h> 103#include <sys/kmem.h>
104 104
105#include <net/ethertypes.h> 105#include <net/ethertypes.h>
106#include <net/if.h> 106#include <net/if.h>
107#include <net/if_dl.h> 107#include <net/if_dl.h>
108#include <net/if_token.h> 108#include <net/if_token.h>
109#include <net/if_types.h> 109#include <net/if_types.h>
110#include <net/if_ether.h> 110#include <net/if_ether.h>
111#include <net/if_llatbl.h> 111#include <net/if_llatbl.h>
112#include <net/route.h> 112#include <net/route.h>
113#include <net/net_stats.h> 113#include <net/net_stats.h>
114 114
115#include <netinet/in.h> 115#include <netinet/in.h>
116#include <netinet/in_systm.h> 116#include <netinet/in_systm.h>
117#include <netinet/in_var.h> 117#include <netinet/in_var.h>
118#include <netinet/ip.h> 118#include <netinet/ip.h>
119#include <netinet/if_inarp.h> 119#include <netinet/if_inarp.h>
120 120
121#include "arcnet.h" 121#include "arcnet.h"
122#if NARCNET > 0 122#if NARCNET > 0
123#include <net/if_arc.h> 123#include <net/if_arc.h>
124#endif 124#endif
125#include "fddi.h" 125#include "fddi.h"
126#if NFDDI > 0 126#if NFDDI > 0
127#include <net/if_fddi.h> 127#include <net/if_fddi.h>
128#endif 128#endif
129#include "token.h" 129#include "token.h"
130#include "carp.h" 130#include "carp.h"
131#if NCARP > 0 131#if NCARP > 0
132#include <netinet/ip_carp.h> 132#include <netinet/ip_carp.h>
133#endif 133#endif
134 134
135/* 135/*
136 * ARP trailer negotiation. Trailer protocol is not IP specific, 136 * ARP trailer negotiation. Trailer protocol is not IP specific,
137 * but ARP request/response use IP addresses. 137 * but ARP request/response use IP addresses.
138 */ 138 */
139#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 139#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
140 140
141/* timer values */ 141/* timer values */
142static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 142static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
143static int arpt_down = 20; /* once declared down, don't send for 20 secs */ 143static int arpt_down = 20; /* once declared down, don't send for 20 secs */
144static int arp_maxhold = 1; /* number of packets to hold per ARP entry */ 144static int arp_maxhold = 1; /* number of packets to hold per ARP entry */
145#define rt_expire rt_rmx.rmx_expire 145#define rt_expire rt_rmx.rmx_expire
146#define rt_pksent rt_rmx.rmx_pksent 146#define rt_pksent rt_rmx.rmx_pksent
147 147
148int ip_dad_count = PROBE_NUM; 148int ip_dad_count = PROBE_NUM;
149#ifdef ARP_DEBUG 149#ifdef ARP_DEBUG
150int arp_debug = 1; 150int arp_debug = 1;
151#else 151#else
152int arp_debug = 0; 152int arp_debug = 0;
153#endif 153#endif
154 154
155static void arp_init(void); 155static void arp_init(void);
156static void arp_dad_init(void); 156static void arp_dad_init(void);
157 157
158static void arprequest(struct ifnet *, 158static void arprequest(struct ifnet *,
159 const struct in_addr *, const struct in_addr *, 159 const struct in_addr *, const struct in_addr *,
160 const uint8_t *); 160 const uint8_t *);
161static void arpannounce1(struct ifaddr *); 161static void arpannounce1(struct ifaddr *);
162static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *, 162static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *,
163 const struct sockaddr *); 163 const struct sockaddr *);
164static void arptimer(void *); 164static void arptimer(void *);
165static void arp_settimer(struct llentry *, int); 165static void arp_settimer(struct llentry *, int);
166static struct llentry *arplookup(struct ifnet *, 166static struct llentry *arplookup(struct ifnet *,
167 const struct in_addr *, const struct sockaddr *, int); 167 const struct in_addr *, const struct sockaddr *, int);
168static struct llentry *arpcreate(struct ifnet *, 168static struct llentry *arpcreate(struct ifnet *,
169 const struct in_addr *, const struct sockaddr *, int); 169 const struct in_addr *, const struct sockaddr *, int);
170static void in_arpinput(struct mbuf *); 170static void in_arpinput(struct mbuf *);
171static void in_revarpinput(struct mbuf *); 171static void in_revarpinput(struct mbuf *);
172static void revarprequest(struct ifnet *); 172static void revarprequest(struct ifnet *);
173 173
174static void arp_drainstub(void); 174static void arp_drainstub(void);
175 175
176struct dadq; 176struct dadq;
177static void arp_dad_timer(struct dadq *); 177static void arp_dad_timer(struct dadq *);
178static void arp_dad_start(struct ifaddr *); 178static void arp_dad_start(struct ifaddr *);
179static void arp_dad_stop(struct ifaddr *); 179static void arp_dad_stop(struct ifaddr *);
180static void arp_dad_duplicated(struct ifaddr *, const struct sockaddr_dl *); 180static void arp_dad_duplicated(struct ifaddr *, const struct sockaddr_dl *);
181 181
182static void arp_init_llentry(struct ifnet *, struct llentry *); 182static void arp_init_llentry(struct ifnet *, struct llentry *);
183#if NTOKEN > 0 183#if NTOKEN > 0
184static void arp_free_llentry_tokenring(struct llentry *); 184static void arp_free_llentry_tokenring(struct llentry *);
185#endif 185#endif
186 186
187struct ifqueue arpintrq = { 187struct ifqueue arpintrq = {
188 .ifq_head = NULL, 188 .ifq_head = NULL,
189 .ifq_tail = NULL, 189 .ifq_tail = NULL,
190 .ifq_len = 0, 190 .ifq_len = 0,
191 .ifq_maxlen = 50, 191 .ifq_maxlen = 50,
192 .ifq_drops = 0, 192 .ifq_drops = 0,
193}; 193};
194static int arp_maxtries = 5; 194static int arp_maxtries = 5;
195static int useloopback = 1; /* use loopback interface for local traffic */ 195static int useloopback = 1; /* use loopback interface for local traffic */
196 196
197static percpu_t *arpstat_percpu; 197static percpu_t *arpstat_percpu;
198 198
199#define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu) 199#define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu)
200#define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu) 200#define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu)
201 201
202#define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x) 202#define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x)
203#define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v) 203#define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v)
204 204
205/* revarp state */ 205/* revarp state */
206static struct in_addr myip, srv_ip; 206static struct in_addr myip, srv_ip;
207static int myip_initialized = 0; 207static int myip_initialized = 0;
208static int revarp_in_progress = 0; 208static int revarp_in_progress = 0;
209static struct ifnet *myip_ifp = NULL; 209static struct ifnet *myip_ifp = NULL;
210 210
211static int arp_drainwanted; 211static int arp_drainwanted;
212 212
213static int log_movements = 0; 213static int log_movements = 0;
214static int log_permanent_modify = 1; 214static int log_permanent_modify = 1;
215static int log_wrong_iface = 1; 215static int log_wrong_iface = 1;
216 216
217DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */ 217DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */
218 218
219static void 219static void
220arp_fasttimo(void) 220arp_fasttimo(void)
221{ 221{
222 if (arp_drainwanted) { 222 if (arp_drainwanted) {
223 arp_drain(); 223 arp_drain();
224 arp_drainwanted = 0; 224 arp_drainwanted = 0;
225 } 225 }
226} 226}
227 227
228static const struct protosw arpsw[] = { 228static const struct protosw arpsw[] = {
229 { 229 {
230 .pr_type = 0, 230 .pr_type = 0,
231 .pr_domain = &arpdomain, 231 .pr_domain = &arpdomain,
232 .pr_protocol = 0, 232 .pr_protocol = 0,
233 .pr_flags = 0, 233 .pr_flags = 0,
234 .pr_input = 0, 234 .pr_input = 0,
235 .pr_ctlinput = 0, 235 .pr_ctlinput = 0,
236 .pr_ctloutput = 0, 236 .pr_ctloutput = 0,
237 .pr_usrreqs = 0, 237 .pr_usrreqs = 0,
238 .pr_init = arp_init, 238 .pr_init = arp_init,
239 .pr_fasttimo = arp_fasttimo, 239 .pr_fasttimo = arp_fasttimo,
240 .pr_slowtimo = 0, 240 .pr_slowtimo = 0,
241 .pr_drain = arp_drainstub, 241 .pr_drain = arp_drainstub,
242 } 242 }
243}; 243};
244 244
245struct domain arpdomain = { 245struct domain arpdomain = {
246 .dom_family = PF_ARP, 246 .dom_family = PF_ARP,
247 .dom_name = "arp", 247 .dom_name = "arp",
248 .dom_protosw = arpsw, 248 .dom_protosw = arpsw,
249 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)], 249 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)],
250#ifdef MBUFTRACE 250#ifdef MBUFTRACE
251 .dom_mowner = MOWNER_INIT("internet", "arp"), 251 .dom_mowner = MOWNER_INIT("internet", "arp"),
252#endif 252#endif
253}; 253};
254 254
255static void sysctl_net_inet_arp_setup(struct sysctllog **); 255static void sysctl_net_inet_arp_setup(struct sysctllog **);
256 256
257void 257void
258arp_init(void) 258arp_init(void)
259{ 259{
260 260
261 sysctl_net_inet_arp_setup(NULL); 261 sysctl_net_inet_arp_setup(NULL);
262 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS); 262 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS);
263 IFQ_LOCK_INIT(&arpintrq); 263 IFQ_LOCK_INIT(&arpintrq);
264 264
265#ifdef MBUFTRACE 265#ifdef MBUFTRACE
266 MOWNER_ATTACH(&arpdomain.dom_mowner); 266 MOWNER_ATTACH(&arpdomain.dom_mowner);
267#endif 267#endif
268 268
269 arp_dad_init(); 269 arp_dad_init();
270} 270}
271 271
272static void 272static void
273arp_drainstub(void) 273arp_drainstub(void)
274{ 274{
275 arp_drainwanted = 1; 275 arp_drainwanted = 1;
276} 276}
277 277
278/* 278/*
279 * ARP protocol drain routine. Called when memory is in short supply. 279 * ARP protocol drain routine. Called when memory is in short supply.
280 * Called at splvm(); don't acquire softnet_lock as can be called from 280 * Called at splvm(); don't acquire softnet_lock as can be called from
281 * hardware interrupt handlers. 281 * hardware interrupt handlers.
282 */ 282 */
283void 283void
284arp_drain(void) 284arp_drain(void)
285{ 285{
286 286
287 lltable_drain(AF_INET); 287 lltable_drain(AF_INET);
288} 288}
289 289
290static void 290static void
291arptimer(void *arg) 291arptimer(void *arg)
292{ 292{
293 struct llentry *lle = arg; 293 struct llentry *lle = arg;
294 struct ifnet *ifp; 294 struct ifnet *ifp;
295 295
296 KASSERT((lle->la_flags & LLE_STATIC) == 0); 296 KASSERT((lle->la_flags & LLE_STATIC) == 0);
297 297
298 LLE_WLOCK(lle); 298 LLE_WLOCK(lle);
299 299
300 /* 300 /*
301 * This shortcut is required to avoid trying to touch ifp that may be 301 * This shortcut is required to avoid trying to touch ifp that may be
302 * being destroyed. 302 * being destroyed.
303 */ 303 */
304 if ((lle->la_flags & LLE_LINKED) == 0) { 304 if ((lle->la_flags & LLE_LINKED) == 0) {
305 LLE_FREE_LOCKED(lle); 305 LLE_FREE_LOCKED(lle);
306 return; 306 return;
307 } 307 }
308 308
309 ifp = lle->lle_tbl->llt_ifp; 309 ifp = lle->lle_tbl->llt_ifp;
310 310
311 /* XXX: LOR avoidance. We still have ref on lle. */ 311 /* XXX: LOR avoidance. We still have ref on lle. */
312 LLE_WUNLOCK(lle); 312 LLE_WUNLOCK(lle);
313 313
314 IF_AFDATA_LOCK(ifp); 314 IF_AFDATA_LOCK(ifp);
315 LLE_WLOCK(lle); 315 LLE_WLOCK(lle);
316 316
317 /* Guard against race with other llentry_free(). */ 317 /* Guard against race with other llentry_free(). */
318 if (lle->la_flags & LLE_LINKED) { 318 if (lle->la_flags & LLE_LINKED) {
319 size_t pkts_dropped; 319 size_t pkts_dropped;
320 320
321 if (lle->la_flags & LLE_VALID) { 321 if (lle->la_flags & LLE_VALID) {
322 struct in_addr *in; 322 struct in_addr *in;
323 struct sockaddr_in sin; 323 struct sockaddr_in sin;
324 const char *lladdr; 324 const char *lladdr;
325 325
326 in = &lle->r_l3addr.addr4; 326 in = &lle->r_l3addr.addr4;
327 sockaddr_in_init(&sin, in, 0); 327 sockaddr_in_init(&sin, in, 0);
328 lladdr = (const char *)&lle->ll_addr; 328 lladdr = (const char *)&lle->ll_addr;
329 rt_clonedmsg(RTM_DELETE, sintosa(&sin), lladdr, ifp); 329 rt_clonedmsg(RTM_DELETE, sintosa(&sin), lladdr, ifp);
330 } 330 }
331 331
332 LLE_REMREF(lle); 332 LLE_REMREF(lle);
333 pkts_dropped = llentry_free(lle); 333 pkts_dropped = llentry_free(lle);
334 ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped); 334 ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped);
335 ARP_STATADD(ARP_STAT_DFRTOTAL, pkts_dropped); 335 ARP_STATADD(ARP_STAT_DFRTOTAL, pkts_dropped);
336 } else { 336 } else {
337 LLE_FREE_LOCKED(lle); 337 LLE_FREE_LOCKED(lle);
338 } 338 }
339 339
340 IF_AFDATA_UNLOCK(ifp); 340 IF_AFDATA_UNLOCK(ifp);
341} 341}
342 342
343static void 343static void
344arp_settimer(struct llentry *la, int sec) 344arp_settimer(struct llentry *la, int sec)
345{ 345{
346 346
347 LLE_WLOCK_ASSERT(la); 347 LLE_WLOCK_ASSERT(la);
348 KASSERT((la->la_flags & LLE_STATIC) == 0); 348 KASSERT((la->la_flags & LLE_STATIC) == 0);
349 349
350 /* 350 /*
351 * We have to take care of a reference leak which occurs if 351 * We have to take care of a reference leak which occurs if
352 * callout_reset overwrites a pending callout schedule. Unfortunately 352 * callout_reset overwrites a pending callout schedule. Unfortunately
353 * we don't have a mean to know the overwrite, so we need to know it 353 * we don't have a mean to know the overwrite, so we need to know it
354 * using callout_stop. We need to call callout_pending first to exclude 354 * using callout_stop. We need to call callout_pending first to exclude
355 * the case that the callout has never been scheduled. 355 * the case that the callout has never been scheduled.
356 */ 356 */
357 if (callout_pending(&la->la_timer)) { 357 if (callout_pending(&la->la_timer)) {
358 bool expired = callout_stop(&la->la_timer); 358 bool expired = callout_stop(&la->la_timer);
359 if (!expired) 359 if (!expired)
360 /* A pending callout schedule is canceled. */ 360 /* A pending callout schedule is canceled. */
361 LLE_REMREF(la); 361 LLE_REMREF(la);
362 } 362 }
363 LLE_ADDREF(la); 363 LLE_ADDREF(la);
364 callout_reset(&la->la_timer, hz * sec, arptimer, la); 364 callout_reset(&la->la_timer, hz * sec, arptimer, la);
365} 365}
366 366
367/* 367/*
368 * We set the gateway for RTF_CLONING routes to a "prototype" 368 * We set the gateway for RTF_CLONING routes to a "prototype"
369 * link-layer sockaddr whose interface type (if_type) and interface 369 * link-layer sockaddr whose interface type (if_type) and interface
370 * index (if_index) fields are prepared. 370 * index (if_index) fields are prepared.
371 */ 371 */
372static struct sockaddr * 372static struct sockaddr *
373arp_setgate(struct rtentry *rt, struct sockaddr *gate, 373arp_setgate(struct rtentry *rt, struct sockaddr *gate,
374 const struct sockaddr *netmask) 374 const struct sockaddr *netmask)
375{ 375{
376 const struct ifnet *ifp = rt->rt_ifp; 376 const struct ifnet *ifp = rt->rt_ifp;
377 uint8_t namelen = strlen(ifp->if_xname); 377 uint8_t namelen = strlen(ifp->if_xname);
378 uint8_t addrlen = ifp->if_addrlen; 378 uint8_t addrlen = ifp->if_addrlen;
379 379
380 /* 380 /*
381 * XXX: If this is a manually added route to interface 381 * XXX: If this is a manually added route to interface
382 * such as older version of routed or gated might provide, 382 * such as older version of routed or gated might provide,
383 * restore cloning bit. 383 * restore cloning bit.
384 */ 384 */
385 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL && 385 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL &&
386 satocsin(netmask)->sin_addr.s_addr != 0xffffffff) 386 satocsin(netmask)->sin_addr.s_addr != 0xffffffff)
387 rt->rt_flags |= RTF_CONNECTED; 387 rt->rt_flags |= RTF_CONNECTED;
388 388
389 if ((rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL))) { 389 if ((rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL))) {
390 union { 390 union {
391 struct sockaddr sa; 391 struct sockaddr sa;
392 struct sockaddr_storage ss; 392 struct sockaddr_storage ss;
393 struct sockaddr_dl sdl; 393 struct sockaddr_dl sdl;
394 } u; 394 } u;
395 /* 395 /*
396 * Case 1: This route should come from a route to iface. 396 * Case 1: This route should come from a route to iface.
397 */ 397 */
398 sockaddr_dl_init(&u.sdl, sizeof(u.ss), 398 sockaddr_dl_init(&u.sdl, sizeof(u.ss),
399 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen); 399 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen);
400 rt_setgate(rt, &u.sa); 400 rt_setgate(rt, &u.sa);
401 gate = rt->rt_gateway; 401 gate = rt->rt_gateway;
402 } 402 }
403 return gate; 403 return gate;
404} 404}
405 405
406static void 406static void
407arp_init_llentry(struct ifnet *ifp, struct llentry *lle) 407arp_init_llentry(struct ifnet *ifp, struct llentry *lle)
408{ 408{
409 409
410 switch (ifp->if_type) { 410 switch (ifp->if_type) {
411#if NTOKEN > 0 411#if NTOKEN > 0
412 case IFT_ISO88025: 412 case IFT_ISO88025:
413 lle->la_opaque = kmem_intr_alloc(sizeof(struct token_rif), 413 lle->la_opaque = kmem_intr_alloc(sizeof(struct token_rif),
414 KM_NOSLEEP); 414 KM_NOSLEEP);
415 lle->lle_ll_free = arp_free_llentry_tokenring; 415 lle->lle_ll_free = arp_free_llentry_tokenring;
416 break; 416 break;
417#endif 417#endif
418 } 418 }
419} 419}
420 420
421#if NTOKEN > 0 421#if NTOKEN > 0
422static void 422static void
423arp_free_llentry_tokenring(struct llentry *lle) 423arp_free_llentry_tokenring(struct llentry *lle)
424{ 424{
425 425
426 kmem_intr_free(lle->la_opaque, sizeof(struct token_rif)); 426 kmem_intr_free(lle->la_opaque, sizeof(struct token_rif));
427} 427}
428#endif 428#endif
429 429
430/* 430/*
431 * Parallel to llc_rtrequest. 431 * Parallel to llc_rtrequest.
432 */ 432 */
433void 433void
434arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 434arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
435{ 435{
436 struct sockaddr *gate = rt->rt_gateway; 436 struct sockaddr *gate = rt->rt_gateway;
437 struct in_ifaddr *ia; 437 struct in_ifaddr *ia;
438 struct ifaddr *ifa; 438 struct ifaddr *ifa;
439 struct ifnet *ifp = rt->rt_ifp; 439 struct ifnet *ifp = rt->rt_ifp;
440 int bound; 440 int bound;
441 int s; 441 int s;
442 442
443 if (req == RTM_LLINFO_UPD) { 443 if (req == RTM_LLINFO_UPD) {
444 if ((ifa = info->rti_ifa) != NULL) 444 if ((ifa = info->rti_ifa) != NULL)
445 arpannounce1(ifa); 445 arpannounce1(ifa);
446 return; 446 return;
447 } 447 }
448 448
449 if ((rt->rt_flags & RTF_GATEWAY) != 0) { 449 if ((rt->rt_flags & RTF_GATEWAY) != 0) {
450 if (req != RTM_ADD) 450 if (req != RTM_ADD)
451 return; 451 return;
452 452
453 /* 453 /*
454 * linklayers with particular link MTU limitation. 454 * linklayers with particular link MTU limitation.
455 */ 455 */
456 switch(ifp->if_type) { 456 switch(ifp->if_type) {
457#if NFDDI > 0 457#if NFDDI > 0
458 case IFT_FDDI: 458 case IFT_FDDI:
459 if (ifp->if_mtu > FDDIIPMTU) 459 if (ifp->if_mtu > FDDIIPMTU)
460 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 460 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
461 break; 461 break;
462#endif 462#endif
463#if NARCNET > 0 463#if NARCNET > 0
464 case IFT_ARCNET: 464 case IFT_ARCNET:
465 { 465 {
466 int arcipifmtu; 466 int arcipifmtu;
467 467
468 if (ifp->if_flags & IFF_LINK0) 468 if (ifp->if_flags & IFF_LINK0)
469 arcipifmtu = arc_ipmtu; 469 arcipifmtu = arc_ipmtu;
470 else 470 else
471 arcipifmtu = ARCMTU; 471 arcipifmtu = ARCMTU;
472 if (ifp->if_mtu > arcipifmtu) 472 if (ifp->if_mtu > arcipifmtu)
473 rt->rt_rmx.rmx_mtu = arcipifmtu; 473 rt->rt_rmx.rmx_mtu = arcipifmtu;
474 break; 474 break;
475 } 475 }
476#endif 476#endif
477 } 477 }
478 return; 478 return;
479 } 479 }
480 480
481 switch (req) { 481 switch (req) {
482 case RTM_SETGATE: 482 case RTM_SETGATE:
483 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 483 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
484 break; 484 break;
485 case RTM_ADD: 485 case RTM_ADD:
486 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 486 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
487 if (gate == NULL) { 487 if (gate == NULL) {
488 log(LOG_ERR, "%s: arp_setgate failed\n", __func__); 488 log(LOG_ERR, "%s: arp_setgate failed\n", __func__);
489 break; 489 break;
490 } 490 }
491 if ((rt->rt_flags & RTF_CONNECTED) || 491 if ((rt->rt_flags & RTF_CONNECTED) ||
492 (rt->rt_flags & RTF_LOCAL)) { 492 (rt->rt_flags & RTF_LOCAL)) {
493 /* 493 /*
494 * Give this route an expiration time, even though 494 * Give this route an expiration time, even though
495 * it's a "permanent" route, so that routes cloned 495 * it's a "permanent" route, so that routes cloned
496 * from it do not need their expiration time set. 496 * from it do not need their expiration time set.
497 */ 497 */
498 KASSERT(time_uptime != 0); 498 KASSERT(time_uptime != 0);
499 rt->rt_expire = time_uptime; 499 rt->rt_expire = time_uptime;
500 /* 500 /*
501 * linklayers with particular link MTU limitation. 501 * linklayers with particular link MTU limitation.
502 */ 502 */
503 switch (ifp->if_type) { 503 switch (ifp->if_type) {
504#if NFDDI > 0 504#if NFDDI > 0
505 case IFT_FDDI: 505 case IFT_FDDI:
506 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 506 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
507 (rt->rt_rmx.rmx_mtu > FDDIIPMTU || 507 (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
508 (rt->rt_rmx.rmx_mtu == 0 && 508 (rt->rt_rmx.rmx_mtu == 0 &&
509 ifp->if_mtu > FDDIIPMTU))) 509 ifp->if_mtu > FDDIIPMTU)))
510 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 510 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
511 break; 511 break;
512#endif 512#endif
513#if NARCNET > 0 513#if NARCNET > 0
514 case IFT_ARCNET: 514 case IFT_ARCNET:
515 { 515 {
516 int arcipifmtu; 516 int arcipifmtu;
517 if (ifp->if_flags & IFF_LINK0) 517 if (ifp->if_flags & IFF_LINK0)
518 arcipifmtu = arc_ipmtu; 518 arcipifmtu = arc_ipmtu;
519 else 519 else
520 arcipifmtu = ARCMTU; 520 arcipifmtu = ARCMTU;
521 521
522 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 522 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
523 (rt->rt_rmx.rmx_mtu > arcipifmtu || 523 (rt->rt_rmx.rmx_mtu > arcipifmtu ||
524 (rt->rt_rmx.rmx_mtu == 0 && 524 (rt->rt_rmx.rmx_mtu == 0 &&
525 ifp->if_mtu > arcipifmtu))) 525 ifp->if_mtu > arcipifmtu)))
526 rt->rt_rmx.rmx_mtu = arcipifmtu; 526 rt->rt_rmx.rmx_mtu = arcipifmtu;
527 break; 527 break;
528 } 528 }
529#endif 529#endif
530 } 530 }
531 if (rt->rt_flags & RTF_CONNECTED) 531 if (rt->rt_flags & RTF_CONNECTED)
532 break; 532 break;
533 } 533 }
534 534
535 bound = curlwp_bind(); 535 bound = curlwp_bind();
536 /* Announce a new entry if requested. */ 536 /* Announce a new entry if requested. */
537 if (rt->rt_flags & RTF_ANNOUNCE) { 537 if (rt->rt_flags & RTF_ANNOUNCE) {
538 struct psref psref; 538 struct psref psref;
539 ia = in_get_ia_on_iface_psref( 539 ia = in_get_ia_on_iface_psref(
540 satocsin(rt_getkey(rt))->sin_addr, ifp, &psref); 540 satocsin(rt_getkey(rt))->sin_addr, ifp, &psref);
541 if (ia != NULL) { 541 if (ia != NULL) {
542 arpannounce(ifp, &ia->ia_ifa, 542 arpannounce(ifp, &ia->ia_ifa,
543 CLLADDR(satocsdl(gate))); 543 CLLADDR(satocsdl(gate)));
544 ia4_release(ia, &psref); 544 ia4_release(ia, &psref);
545 } 545 }
546 } 546 }
547 547
548 if (gate->sa_family != AF_LINK || 548 if (gate->sa_family != AF_LINK ||
549 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) { 549 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) {
550 log(LOG_DEBUG, "%s: bad gateway value\n", __func__); 550 log(LOG_DEBUG, "%s: bad gateway value\n", __func__);
551 goto out; 551 goto out;
552 } 552 }
553 553
554 satosdl(gate)->sdl_type = ifp->if_type; 554 satosdl(gate)->sdl_type = ifp->if_type;
555 satosdl(gate)->sdl_index = ifp->if_index; 555 satosdl(gate)->sdl_index = ifp->if_index;
556 556
557 /* 557 /*
558 * If the route is for a broadcast address mark it as such. 558 * If the route is for a broadcast address mark it as such.
559 * This way we can avoid an expensive call to in_broadcast() 559 * This way we can avoid an expensive call to in_broadcast()
560 * in ip_output() most of the time (because the route passed 560 * in ip_output() most of the time (because the route passed
561 * to ip_output() is almost always a host route). 561 * to ip_output() is almost always a host route).
562 */ 562 */
563 if (rt->rt_flags & RTF_HOST && 563 if (rt->rt_flags & RTF_HOST &&
564 !(rt->rt_flags & RTF_BROADCAST) && 564 !(rt->rt_flags & RTF_BROADCAST) &&
565 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp)) 565 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp))
566 rt->rt_flags |= RTF_BROADCAST; 566 rt->rt_flags |= RTF_BROADCAST;
567 /* There is little point in resolving the broadcast address */ 567 /* There is little point in resolving the broadcast address */
568 if (rt->rt_flags & RTF_BROADCAST) 568 if (rt->rt_flags & RTF_BROADCAST)
569 goto out; 569 goto out;
570 570
571 /* 571 /*
572 * When called from rt_ifa_addlocal, we cannot depend on that 572 * When called from rt_ifa_addlocal, we cannot depend on that
573 * the address (rt_getkey(rt)) exits in the address list of the 573 * the address (rt_getkey(rt)) exits in the address list of the
574 * interface. So check RTF_LOCAL instead. 574 * interface. So check RTF_LOCAL instead.
575 */ 575 */
576 if (rt->rt_flags & RTF_LOCAL) { 576 if (rt->rt_flags & RTF_LOCAL) {
577 rt->rt_expire = 0; 577 rt->rt_expire = 0;
578 if (useloopback) { 578 if (useloopback) {
579 rt->rt_ifp = lo0ifp; 579 rt->rt_ifp = lo0ifp;
580 rt->rt_rmx.rmx_mtu = 0; 580 rt->rt_rmx.rmx_mtu = 0;
581 } 581 }
582 goto out; 582 goto out;
583 } 583 }
584 584
585 s = pserialize_read_enter(); 585 s = pserialize_read_enter();
586 ia = in_get_ia_on_iface(satocsin(rt_getkey(rt))->sin_addr, ifp); 586 ia = in_get_ia_on_iface(satocsin(rt_getkey(rt))->sin_addr, ifp);
587 if (ia == NULL) { 587 if (ia == NULL) {
588 pserialize_read_exit(s); 588 pserialize_read_exit(s);
589 goto out; 589 goto out;
590 } 590 }
591 591
592 rt->rt_expire = 0; 592 rt->rt_expire = 0;
593 if (useloopback) { 593 if (useloopback) {
594 rt->rt_ifp = lo0ifp; 594 rt->rt_ifp = lo0ifp;
595 rt->rt_rmx.rmx_mtu = 0; 595 rt->rt_rmx.rmx_mtu = 0;
596 } 596 }
597 rt->rt_flags |= RTF_LOCAL; 597 rt->rt_flags |= RTF_LOCAL;
598 598
599 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) { 599 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) {
600 pserialize_read_exit(s); 600 pserialize_read_exit(s);
601 goto out; 601 goto out;
602 } 602 }
603 /* 603 /*
604 * make sure to set rt->rt_ifa to the interface 604 * make sure to set rt->rt_ifa to the interface
605 * address we are using, otherwise we will have trouble 605 * address we are using, otherwise we will have trouble
606 * with source address selection. 606 * with source address selection.
607 */ 607 */
608 ifa = &ia->ia_ifa; 608 ifa = &ia->ia_ifa;
609 if (ifa != rt->rt_ifa) 609 if (ifa != rt->rt_ifa)
610 /* Assume it doesn't sleep */ 610 /* Assume it doesn't sleep */
611 rt_replace_ifa(rt, ifa); 611 rt_replace_ifa(rt, ifa);
612 pserialize_read_exit(s); 612 pserialize_read_exit(s);
613 out: 613 out:
614 curlwp_bindx(bound); 614 curlwp_bindx(bound);
615 break; 615 break;
616 } 616 }
617} 617}
618 618
619/* 619/*
620 * Broadcast an ARP request. Caller specifies: 620 * Broadcast an ARP request. Caller specifies:
621 * - arp header source ip address 621 * - arp header source ip address
622 * - arp header target ip address 622 * - arp header target ip address
623 * - arp header source ethernet address 623 * - arp header source ethernet address
624 */ 624 */
625static void 625static void
626arprequest(struct ifnet *ifp, 626arprequest(struct ifnet *ifp,
627 const struct in_addr *sip, const struct in_addr *tip, 627 const struct in_addr *sip, const struct in_addr *tip,
628 const uint8_t *enaddr) 628 const uint8_t *enaddr)
629{ 629{
630 struct mbuf *m; 630 struct mbuf *m;
631 struct arphdr *ah; 631 struct arphdr *ah;
632 struct sockaddr sa; 632 struct sockaddr sa;
633 uint64_t *arps; 633 uint64_t *arps;
634 634
635 KASSERT(sip != NULL); 635 KASSERT(sip != NULL);
636 KASSERT(tip != NULL); 636 KASSERT(tip != NULL);
637 KASSERT(enaddr != NULL); 637 KASSERT(enaddr != NULL);
638 638
639 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 639 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
640 return; 640 return;
641 MCLAIM(m, &arpdomain.dom_mowner); 641 MCLAIM(m, &arpdomain.dom_mowner);
642 switch (ifp->if_type) { 642 switch (ifp->if_type) {
643 case IFT_IEEE1394: 643 case IFT_IEEE1394:
644 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 644 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
645 ifp->if_addrlen; 645 ifp->if_addrlen;
646 break; 646 break;
647 default: 647 default:
648 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 648 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
649 2 * ifp->if_addrlen; 649 2 * ifp->if_addrlen;
650 break; 650 break;
651 } 651 }
652 m->m_pkthdr.len = m->m_len; 652 m->m_pkthdr.len = m->m_len;
653 m_align(m, m->m_len); 653 m_align(m, m->m_len);
654 ah = mtod(m, struct arphdr *); 654 ah = mtod(m, struct arphdr *);
655 memset(ah, 0, m->m_len); 655 memset(ah, 0, m->m_len);
656 switch (ifp->if_type) { 656 switch (ifp->if_type) {
657 case IFT_IEEE1394: /* RFC2734 */ 657 case IFT_IEEE1394: /* RFC2734 */
658 /* fill it now for ar_tpa computation */ 658 /* fill it now for ar_tpa computation */
659 ah->ar_hrd = htons(ARPHRD_IEEE1394); 659 ah->ar_hrd = htons(ARPHRD_IEEE1394);
660 break; 660 break;
661 default: 661 default:
662 /* ifp->if_output will fill ar_hrd */ 662 /* ifp->if_output will fill ar_hrd */
663 break; 663 break;
664 } 664 }
665 ah->ar_pro = htons(ETHERTYPE_IP); 665 ah->ar_pro = htons(ETHERTYPE_IP);
666 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 666 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
667 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 667 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
668 ah->ar_op = htons(ARPOP_REQUEST); 668 ah->ar_op = htons(ARPOP_REQUEST);
669 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 669 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
670 memcpy(ar_spa(ah), sip, ah->ar_pln); 670 memcpy(ar_spa(ah), sip, ah->ar_pln);
671 memcpy(ar_tpa(ah), tip, ah->ar_pln); 671 memcpy(ar_tpa(ah), tip, ah->ar_pln);
672 sa.sa_family = AF_ARP; 672 sa.sa_family = AF_ARP;
673 sa.sa_len = 2; 673 sa.sa_len = 2;
674 m->m_flags |= M_BCAST; 674 m->m_flags |= M_BCAST;
675 arps = ARP_STAT_GETREF(); 675 arps = ARP_STAT_GETREF();
676 arps[ARP_STAT_SNDTOTAL]++; 676 arps[ARP_STAT_SNDTOTAL]++;
677 arps[ARP_STAT_SENDREQUEST]++; 677 arps[ARP_STAT_SENDREQUEST]++;
678 ARP_STAT_PUTREF(); 678 ARP_STAT_PUTREF();
679 if_output_lock(ifp, ifp, m, &sa, NULL); 679 if_output_lock(ifp, ifp, m, &sa, NULL);
680} 680}
681 681
682void 682void
683arpannounce(struct ifnet *ifp, struct ifaddr *ifa, const uint8_t *enaddr) 683arpannounce(struct ifnet *ifp, struct ifaddr *ifa, const uint8_t *enaddr)
684{ 684{
685 struct in_ifaddr *ia = ifatoia(ifa); 685 struct in_ifaddr *ia = ifatoia(ifa);
686 struct in_addr *ip = &IA_SIN(ifa)->sin_addr; 686 struct in_addr *ip = &IA_SIN(ifa)->sin_addr;
687 687
688 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) { 688 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) {
689 ARPLOG(LOG_DEBUG, "%s not ready\n", ARPLOGADDR(ip)); 689 ARPLOG(LOG_DEBUG, "%s not ready\n", ARPLOGADDR(ip));
690 return; 690 return;
691 } 691 }
692 arprequest(ifp, ip, ip, enaddr); 692 arprequest(ifp, ip, ip, enaddr);
693} 693}
694 694
695static void 695static void
696arpannounce1(struct ifaddr *ifa) 696arpannounce1(struct ifaddr *ifa)
697{ 697{
698 698
699 arpannounce(ifa->ifa_ifp, ifa, CLLADDR(ifa->ifa_ifp->if_sadl)); 699 arpannounce(ifa->ifa_ifp, ifa, CLLADDR(ifa->ifa_ifp->if_sadl));
700} 700}
701 701
702/* 702/*
703 * Resolve an IP address into an ethernet address. If success, desten is 703 * Resolve an IP address into an ethernet address. If success, desten is
704 * filled in. If there is no entry in arptab, set one up and broadcast a 704 * filled in. If there is no entry in arptab, set one up and broadcast a
705 * request for the IP address. Hold onto this mbuf and resend it once the 705 * request for the IP address. Hold onto this mbuf and resend it once the
706 * address is finally resolved. 706 * address is finally resolved.
707 * 707 *
708 * A return value of 0 indicates that desten has been filled in and the packet 708 * A return value of 0 indicates that desten has been filled in and the packet
709 * should be sent normally; a return value of EWOULDBLOCK indicates that the 709 * should be sent normally; a return value of EWOULDBLOCK indicates that the
710 * packet has been held pending resolution. Any other value indicates an 710 * packet has been held pending resolution. Any other value indicates an
711 * error. 711 * error.
712 */ 712 */
713int 713int
714arpresolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m, 714arpresolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
715 const struct sockaddr *dst, void *desten, size_t destlen) 715 const struct sockaddr *dst, void *desten, size_t destlen)
716{ 716{
717 struct llentry *la; 717 struct llentry *la;
718 const char *create_lookup; 718 const char *create_lookup;
719 bool renew; 719 bool renew;
720 int error; 720 int error;
721 struct ifnet *origifp = ifp; 721 struct ifnet *origifp = ifp;
722 722
723#if NCARP > 0 723#if NCARP > 0
724 if (rt != NULL && rt->rt_ifp->if_type == IFT_CARP) 724 if (rt != NULL && rt->rt_ifp->if_type == IFT_CARP)
725 ifp = rt->rt_ifp; 725 ifp = rt->rt_ifp;
726#endif 726#endif
727 727
728 KASSERT(m != NULL); 728 KASSERT(m != NULL);
729 729
730 la = arplookup(ifp, NULL, dst, 0); 730 la = arplookup(ifp, NULL, dst, 0);
731 if (la == NULL) 731 if (la == NULL)
732 goto notfound; 732 goto notfound;
733 733
734 if ((la->la_flags & LLE_VALID) && 734 if ((la->la_flags & LLE_VALID) &&
735 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 735 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
736 KASSERT(destlen >= ifp->if_addrlen); 736 KASSERT(destlen >= ifp->if_addrlen);
737 memcpy(desten, &la->ll_addr, ifp->if_addrlen); 737 memcpy(desten, &la->ll_addr, ifp->if_addrlen);
738 LLE_RUNLOCK(la); 738 LLE_RUNLOCK(la);
739 return 0; 739 return 0;
740 } 740 }
741 741
742notfound: 742notfound:
743 if (ifp->if_flags & IFF_NOARP) { 743 if (ifp->if_flags & IFF_NOARP) {
744 if (la != NULL) 744 if (la != NULL)
745 LLE_RUNLOCK(la); 745 LLE_RUNLOCK(la);
746 error = ENOTSUP; 746 error = ENOTSUP;
747 goto bad; 747 goto bad;
748 } 748 }
749 749
750 if (la == NULL) { 750 if (la == NULL) {
751 struct rtentry *_rt; 751 struct rtentry *_rt;
752 752
753 create_lookup = "create"; 753 create_lookup = "create";
754 _rt = rtalloc1(dst, 0); 754 _rt = rtalloc1(dst, 0);
755 IF_AFDATA_WLOCK(ifp); 755 IF_AFDATA_WLOCK(ifp);
756 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst, _rt); 756 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst, _rt);
757 IF_AFDATA_WUNLOCK(ifp); 757 IF_AFDATA_WUNLOCK(ifp);
758 if (_rt != NULL) 758 if (_rt != NULL)
759 rt_unref(_rt); 759 rt_unref(_rt);
760 if (la == NULL) 760 if (la == NULL)
761 ARP_STATINC(ARP_STAT_ALLOCFAIL); 761 ARP_STATINC(ARP_STAT_ALLOCFAIL);
762 else 762 else
763 arp_init_llentry(ifp, la); 763 arp_init_llentry(ifp, la);
764 } else if (LLE_TRY_UPGRADE(la) == 0) { 764 } else if (LLE_TRY_UPGRADE(la) == 0) {
765 create_lookup = "lookup"; 765 create_lookup = "lookup";
766 LLE_RUNLOCK(la); 766 LLE_RUNLOCK(la);
767 IF_AFDATA_RLOCK(ifp); 767 IF_AFDATA_RLOCK(ifp);
768 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 768 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
769 IF_AFDATA_RUNLOCK(ifp); 769 IF_AFDATA_RUNLOCK(ifp);
770 } 770 }
771 771
772 error = EINVAL; 772 error = EINVAL;
773 if (la == NULL) { 773 if (la == NULL) {
774 log(LOG_DEBUG, 774 log(LOG_DEBUG,
775 "%s: failed to %s llentry for %s on %s\n", 775 "%s: failed to %s llentry for %s on %s\n",
776 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr), 776 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr),
777 ifp->if_xname); 777 ifp->if_xname);
778 goto bad; 778 goto bad;
779 } 779 }
780 780
781 if ((la->la_flags & LLE_VALID) && 781 if ((la->la_flags & LLE_VALID) &&
782 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) 782 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime))
783 { 783 {
784 KASSERT(destlen >= ifp->if_addrlen); 784 KASSERT(destlen >= ifp->if_addrlen);
785 memcpy(desten, &la->ll_addr, ifp->if_addrlen); 785 memcpy(desten, &la->ll_addr, ifp->if_addrlen);
786 renew = false; 786 renew = false;
787 /* 787 /*
788 * If entry has an expiry time and it is approaching, 788 * If entry has an expiry time and it is approaching,
789 * see if we need to send an ARP request within this 789 * see if we need to send an ARP request within this
790 * arpt_down interval. 790 * arpt_down interval.
791 */ 791 */
792 if (!(la->la_flags & LLE_STATIC) && 792 if (!(la->la_flags & LLE_STATIC) &&
793 time_uptime + la->la_preempt > la->la_expire) 793 time_uptime + la->la_preempt > la->la_expire)
794 { 794 {
795 renew = true; 795 renew = true;
796 la->la_preempt--; 796 la->la_preempt--;
797 } 797 }
798 798
799 LLE_WUNLOCK(la); 799 LLE_WUNLOCK(la);
800 800
801 if (renew) { 801 if (renew) {
802 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 802 const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
803 arprequest(origifp, 803 arprequest(origifp,
804 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 804 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
805 &satocsin(dst)->sin_addr, enaddr); 805 &satocsin(dst)->sin_addr, enaddr);
806 } 806 }
807 807
808 return 0; 808 return 0;
809 } 809 }
810 810
811 if (la->la_flags & LLE_STATIC) { /* should not happen! */ 811 if (la->la_flags & LLE_STATIC) { /* should not happen! */
812 LLE_RUNLOCK(la); 812 LLE_RUNLOCK(la);
813 log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n", 813 log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n",
814 __func__, inet_ntoa(satocsin(dst)->sin_addr)); 814 __func__, inet_ntoa(satocsin(dst)->sin_addr));
815 error = EINVAL; 815 error = EINVAL;
816 goto bad; 816 goto bad;
817 } 817 }
818 818
819 renew = (la->la_asked == 0 || la->la_expire != time_uptime); 819 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
820 820
821 /* 821 /*
822 * There is an arptab entry, but no ethernet address 822 * There is an arptab entry, but no ethernet address
823 * response yet. Add the mbuf to the list, dropping 823 * response yet. Add the mbuf to the list, dropping
824 * the oldest packet if we have exceeded the system 824 * the oldest packet if we have exceeded the system
825 * setting. 825 * setting.
826 */ 826 */
827 LLE_WLOCK_ASSERT(la); 827 LLE_WLOCK_ASSERT(la);
828 if (la->la_numheld >= arp_maxhold) { 828 if (la->la_numheld >= arp_maxhold) {
829 if (la->la_hold != NULL) { 829 if (la->la_hold != NULL) {
830 struct mbuf *next = la->la_hold->m_nextpkt; 830 struct mbuf *next = la->la_hold->m_nextpkt;
831 m_freem(la->la_hold); 831 m_freem(la->la_hold);
832 la->la_hold = next; 832 la->la_hold = next;
833 la->la_numheld--; 833 la->la_numheld--;
834 ARP_STATINC(ARP_STAT_DFRDROPPED); 834 ARP_STATINC(ARP_STAT_DFRDROPPED);
835 ARP_STATINC(ARP_STAT_DFRTOTAL); 835 ARP_STATINC(ARP_STAT_DFRTOTAL);
836 } 836 }
837 } 837 }
838 if (la->la_hold != NULL) { 838 if (la->la_hold != NULL) {
839 struct mbuf *curr = la->la_hold; 839 struct mbuf *curr = la->la_hold;
840 while (curr->m_nextpkt != NULL) 840 while (curr->m_nextpkt != NULL)
841 curr = curr->m_nextpkt; 841 curr = curr->m_nextpkt;
842 curr->m_nextpkt = m; 842 curr->m_nextpkt = m;
843 } else 843 } else
844 la->la_hold = m; 844 la->la_hold = m;
845 la->la_numheld++; 845 la->la_numheld++;
846 if (!renew) 846 if (!renew)
847 LLE_DOWNGRADE(la); 847 LLE_DOWNGRADE(la);
848 848
849 /* 849 /*
850 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 850 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
851 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 851 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
852 * if we have already sent arp_maxtries ARP requests. Retransmit the 852 * if we have already sent arp_maxtries ARP requests. Retransmit the
853 * ARP request, but not faster than one request per second. 853 * ARP request, but not faster than one request per second.
854 */ 854 */
855 if (la->la_asked < arp_maxtries) 855 if (la->la_asked < arp_maxtries)
856 error = EWOULDBLOCK; /* First request. */ 856 error = EWOULDBLOCK; /* First request. */
857 else 857 else
858 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ? 858 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
859 EHOSTUNREACH : EHOSTDOWN; 859 EHOSTUNREACH : EHOSTDOWN;
860 860
861 if (renew) { 861 if (renew) {
862 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 862 const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
863 struct sockaddr_in sin; 863 struct sockaddr_in sin;
864 864
865 la->la_expire = time_uptime; 865 la->la_expire = time_uptime;
866 arp_settimer(la, arpt_down); 866 arp_settimer(la, arpt_down);
867 la->la_asked++; 867 la->la_asked++;
868 868
869 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 869 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
870 if (error != EWOULDBLOCK) 870 if (error != EWOULDBLOCK)
871 rt_clonedmsg(RTM_MISS, sintosa(&sin), NULL, ifp); 871 rt_clonedmsg(RTM_MISS, sintosa(&sin), NULL, ifp);
872 872
873 LLE_WUNLOCK(la); 873 LLE_WUNLOCK(la);
874 874
875 if (rt != NULL) { 875 if (rt != NULL) {
876 arprequest(origifp, 876 arprequest(origifp,
877 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 877 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
878 &satocsin(dst)->sin_addr, enaddr); 878 &satocsin(dst)->sin_addr, enaddr);
879 } else { 879 } else {
880 struct rtentry *_rt; 880 struct rtentry *_rt;
881 881
882 /* XXX */ 882 /* XXX */
883 _rt = rtalloc1((struct sockaddr *)&sin, 0); 883 _rt = rtalloc1((struct sockaddr *)&sin, 0);
884 if (_rt == NULL) 884 if (_rt == NULL)
885 goto bad; 885 goto bad;
886 arprequest(origifp, 886 arprequest(origifp,
887 &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr, 887 &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr,
888 &satocsin(dst)->sin_addr, enaddr); 888 &satocsin(dst)->sin_addr, enaddr);
889 rt_unref(_rt); 889 rt_unref(_rt);
890 } 890 }
891 return error; 891 return error;
892 } 892 }
893 893
894 LLE_RUNLOCK(la); 894 LLE_RUNLOCK(la);
895 return error; 895 return error;
896 896
897bad: 897bad:
898 m_freem(m); 898 m_freem(m);
899 return error; 899 return error;
900} 900}
901 901
902/* 902/*
903 * Common length and type checks are done here, 903 * Common length and type checks are done here,
904 * then the protocol-specific routine is called. 904 * then the protocol-specific routine is called.
905 */ 905 */
906void 906void
907arpintr(void) 907arpintr(void)
908{ 908{
909 struct mbuf *m; 909 struct mbuf *m;
910 struct arphdr *ar; 910 struct arphdr *ar;
911 int s; 911 int s;
912 int arplen; 912 int arplen;
913 913
914 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE(); 914 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
915 for (;;) { 915 for (;;) {
916 struct ifnet *rcvif; 916 struct ifnet *rcvif;
917 917
918 IFQ_LOCK(&arpintrq); 918 IFQ_LOCK(&arpintrq);
919 IF_DEQUEUE(&arpintrq, m); 919 IF_DEQUEUE(&arpintrq, m);
920 IFQ_UNLOCK(&arpintrq); 920 IFQ_UNLOCK(&arpintrq);
921 if (m == NULL) 921 if (m == NULL)
922 goto out; 922 goto out;
923 if ((m->m_flags & M_PKTHDR) == 0) 923 if ((m->m_flags & M_PKTHDR) == 0)
924 panic("arpintr"); 924 panic("arpintr");
925 925
926 MCLAIM(m, &arpdomain.dom_mowner); 926 MCLAIM(m, &arpdomain.dom_mowner);
927 ARP_STATINC(ARP_STAT_RCVTOTAL); 927 ARP_STATINC(ARP_STAT_RCVTOTAL);
928 928
929 arplen = sizeof(struct arphdr); 929 arplen = sizeof(struct arphdr);
930 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 930 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
931 goto badlen; 931 goto badlen;
932 ar = mtod(m, struct arphdr *); 932 ar = mtod(m, struct arphdr *);
933 933
934 rcvif = m_get_rcvif(m, &s); 934 rcvif = m_get_rcvif(m, &s);
935 if (__predict_false(rcvif == NULL)) { 935 if (__predict_false(rcvif == NULL)) {
936 ARP_STATINC(ARP_STAT_RCVNOINT); 936 ARP_STATINC(ARP_STAT_RCVNOINT);
937 goto free; 937 goto free;
938 } 938 }
939 939
940 /* 940 /*
941 * We don't want non-IEEE1394 ARP packets on IEEE1394 941 * We don't want non-IEEE1394 ARP packets on IEEE1394
942 * interfaces, and vice versa. Our life depends on that. 942 * interfaces, and vice versa. Our life depends on that.
943 */ 943 */
944 switch (rcvif->if_type) { 944 switch (rcvif->if_type) {
945 case IFT_IEEE1394: 945 case IFT_IEEE1394:
946 if (ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 946 if (ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
947 m_put_rcvif(rcvif, &s); 947 m_put_rcvif(rcvif, &s);
948 ARP_STATINC(ARP_STAT_RCVBADPROTO); 948 ARP_STATINC(ARP_STAT_RCVBADPROTO);
949 goto free; 949 goto free;
950 } 950 }
951 951
952 arplen = sizeof(struct arphdr) + 952 arplen = sizeof(struct arphdr) +
953 ar->ar_hln + 2 * ar->ar_pln; 953 ar->ar_hln + 2 * ar->ar_pln;
954 break; 954 break;
955 default: 955 default:
956 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) { 956 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) {
957 m_put_rcvif(rcvif, &s); 957 m_put_rcvif(rcvif, &s);
958 ARP_STATINC(ARP_STAT_RCVBADPROTO); 958 ARP_STATINC(ARP_STAT_RCVBADPROTO);
959 goto free; 959 goto free;
960 } 960 }
961 961
962 arplen = sizeof(struct arphdr) + 962 arplen = sizeof(struct arphdr) +
963 2 * ar->ar_hln + 2 * ar->ar_pln; 963 2 * ar->ar_hln + 2 * ar->ar_pln;
964 break; 964 break;
965 } 965 }
966 966
967 m_put_rcvif(rcvif, &s); 967 m_put_rcvif(rcvif, &s);
968 968
969 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 969 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
970 goto badlen; 970 goto badlen;
971 ar = mtod(m, struct arphdr *); 971 ar = mtod(m, struct arphdr *);
972 972
973 switch (ntohs(ar->ar_pro)) { 973 switch (ntohs(ar->ar_pro)) {
974 case ETHERTYPE_IP: 974 case ETHERTYPE_IP:
975 case ETHERTYPE_IPTRAILERS: 975 case ETHERTYPE_IPTRAILERS:
976 in_arpinput(m); 976 in_arpinput(m);
977 continue; 977 continue;
978 default: 978 default:
979 ARP_STATINC(ARP_STAT_RCVBADPROTO); 979 ARP_STATINC(ARP_STAT_RCVBADPROTO);
980 goto free; 980 goto free;
981 } 981 }
982 982
983badlen: 983badlen:
984 ARP_STATINC(ARP_STAT_RCVBADLEN); 984 ARP_STATINC(ARP_STAT_RCVBADLEN);
985free: 985free:
986 m_freem(m); 986 m_freem(m);
987 } 987 }
988 988
989out: 989out:
990 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 990 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
991 return; /* XXX gcc */ 991 return; /* XXX gcc */
992} 992}
993 993
994/* 994/*
995 * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in 995 * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in
996 * RFC 826. In addition, a sanity check is performed on the sender protocol 996 * RFC 826. In addition, a sanity check is performed on the sender protocol
997 * address, to catch impersonators. 997 * address, to catch impersonators.
998 * 998 *
999 * We no longer handle negotiations for use of trailer protocol: formerly, ARP 999 * We no longer handle negotiations for use of trailer protocol: formerly, ARP
1000 * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we 1000 * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we
1001 * wanted trailers sent to us, and also sent them in response to IP replies. 1001 * wanted trailers sent to us, and also sent them in response to IP replies.
1002 * This allowed either end to announce the desire to receive trailer packets. 1002 * This allowed either end to announce the desire to receive trailer packets.
1003 * 1003 *
1004 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but 1004 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but
1005 * formerly didn't normally send requests. 1005 * formerly didn't normally send requests.
1006 */ 1006 */
1007static void 1007static void
1008in_arpinput(struct mbuf *m) 1008in_arpinput(struct mbuf *m)
1009{ 1009{
1010 struct arphdr *ah; 1010 struct arphdr *ah;
1011 struct ifnet *ifp, *rcvif = NULL; 1011 struct ifnet *ifp, *rcvif = NULL;
1012 struct llentry *la = NULL; 1012 struct llentry *la = NULL;
1013 struct in_ifaddr *ia = NULL; 1013 struct in_ifaddr *ia = NULL;
1014#if NBRIDGE > 0 1014#if NBRIDGE > 0
1015 struct in_ifaddr *bridge_ia = NULL; 1015 struct in_ifaddr *bridge_ia = NULL;
1016#endif 1016#endif
1017#if NCARP > 0 1017#if NCARP > 0
1018 uint32_t count = 0, index = 0; 1018 uint32_t count = 0, index = 0;
1019#endif 1019#endif
1020 struct sockaddr sa; 1020 struct sockaddr sa;
1021 struct in_addr isaddr, itaddr, myaddr; 1021 struct in_addr isaddr, itaddr, myaddr;
1022 int op, rt_cmd; 1022 int op, rt_cmd;
1023 void *tha; 1023 void *tha;
1024 uint64_t *arps; 1024 uint64_t *arps;
1025 struct psref psref, psref_ia; 1025 struct psref psref, psref_ia;
1026 int s; 1026 int s;
1027 char ipbuf[INET_ADDRSTRLEN]; 1027 char ipbuf[INET_ADDRSTRLEN];
1028 bool do_dad; 1028 bool do_dad;
1029 1029
1030 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT))) 1030 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
1031 goto out; 1031 goto out;
1032 ah = mtod(m, struct arphdr *); 1032 ah = mtod(m, struct arphdr *);
1033 op = ntohs(ah->ar_op); 1033 op = ntohs(ah->ar_op);
1034 1034
1035 if (ah->ar_pln != sizeof(struct in_addr)) 1035 if (ah->ar_pln != sizeof(struct in_addr))
1036 goto out; 1036 goto out;
1037 1037
1038 ifp = if_get_bylla(ar_sha(ah), ah->ar_hln, &psref); 1038 ifp = if_get_bylla(ar_sha(ah), ah->ar_hln, &psref);
1039 if (ifp) { 1039 if (ifp) {
1040 /* it's from me, ignore it. */ 1040 /* it's from me, ignore it. */
1041 if_put(ifp, &psref); 1041 if_put(ifp, &psref);
1042 ARP_STATINC(ARP_STAT_RCVLOCALSHA); 1042 ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1043 goto out; 1043 goto out;
1044 } 1044 }
1045 1045
1046 rcvif = ifp = m_get_rcvif_psref(m, &psref); 1046 rcvif = ifp = m_get_rcvif_psref(m, &psref);
1047 if (__predict_false(rcvif == NULL)) 1047 if (__predict_false(rcvif == NULL))
1048 goto out; 1048 goto out;
1049 if (rcvif->if_flags & IFF_NOARP) 1049 if (rcvif->if_flags & IFF_NOARP)
1050 goto out; 1050 goto out;
1051 1051
1052 memcpy(&isaddr, ar_spa(ah), sizeof(isaddr)); 1052 memcpy(&isaddr, ar_spa(ah), sizeof(isaddr));
1053 memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr)); 1053 memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr));
1054 1054
1055 if (m->m_flags & (M_BCAST|M_MCAST)) 1055 if (m->m_flags & (M_BCAST|M_MCAST))
1056 ARP_STATINC(ARP_STAT_RCVMCAST); 1056 ARP_STATINC(ARP_STAT_RCVMCAST);
1057 1057
1058 /* 1058 /*
1059 * Search for a matching interface address 1059 * Search for a matching interface address
1060 * or any address on the interface to use 1060 * or any address on the interface to use
1061 * as a dummy address in the rest of this function. 1061 * as a dummy address in the rest of this function.
1062 * 1062 *
1063 * If the target IP address is zero then try and find 1063 * If the target IP address is zero then try and find
1064 * the sender address for DAD. 1064 * the sender address for DAD.
1065 */ 1065 */
1066 myaddr = in_nullhost(itaddr) ? isaddr : itaddr; 1066 myaddr = in_nullhost(itaddr) ? isaddr : itaddr;
1067 s = pserialize_read_enter(); 1067 s = pserialize_read_enter();
1068 IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) { 1068 IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) {
1069 if (!in_hosteq(ia->ia_addr.sin_addr, myaddr)) 1069 if (!in_hosteq(ia->ia_addr.sin_addr, myaddr))
1070 continue; 1070 continue;
1071#if NCARP > 0 1071#if NCARP > 0
1072 if (ia->ia_ifp->if_type == IFT_CARP && 1072 if (ia->ia_ifp->if_type == IFT_CARP &&
1073 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) == 1073 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1074 (IFF_UP|IFF_RUNNING))) { 1074 (IFF_UP|IFF_RUNNING))) {
1075 index++; 1075 index++;
1076 /* XXX: ar_hln? */ 1076 /* XXX: ar_hln? */
1077 if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) && 1077 if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) &&
1078 carp_iamatch(ia, ar_sha(ah), 1078 carp_iamatch(ia, ar_sha(ah),
1079 &count, index)) { 1079 &count, index)) {
1080 break; 1080 break;
1081 } 1081 }
1082 } else 1082 } else
1083#endif 1083#endif
1084 if (ia->ia_ifp == rcvif) 1084 if (ia->ia_ifp == rcvif)
1085 break; 1085 break;
1086#if NBRIDGE > 0 1086#if NBRIDGE > 0
1087 /* 1087 /*
1088 * If the interface we received the packet on 1088 * If the interface we received the packet on
1089 * is part of a bridge, check to see if we need 1089 * is part of a bridge, check to see if we need
1090 * to "bridge" the packet to ourselves at this 1090 * to "bridge" the packet to ourselves at this
1091 * layer. Note we still prefer a perfect match, 1091 * layer. Note we still prefer a perfect match,
1092 * but allow this weaker match if necessary. 1092 * but allow this weaker match if necessary.
1093 */ 1093 */
1094 if (rcvif->if_bridge != NULL && 1094 if (rcvif->if_bridge != NULL &&
1095 rcvif->if_bridge == ia->ia_ifp->if_bridge) 1095 rcvif->if_bridge == ia->ia_ifp->if_bridge)
1096 bridge_ia = ia; 1096 bridge_ia = ia;
1097#endif 1097#endif
1098 } 1098 }
1099 1099
1100#if NBRIDGE > 0 1100#if NBRIDGE > 0
1101 if (ia == NULL && bridge_ia != NULL) { 1101 if (ia == NULL && bridge_ia != NULL) {
1102 ia = bridge_ia; 1102 ia = bridge_ia;
1103 m_put_rcvif_psref(rcvif, &psref); 1103 m_put_rcvif_psref(rcvif, &psref);
1104 rcvif = NULL; 1104 rcvif = NULL;
1105 /* FIXME */ 1105 /* FIXME */
1106 ifp = bridge_ia->ia_ifp; 1106 ifp = bridge_ia->ia_ifp;
1107 } 1107 }
1108#endif 1108#endif
1109 if (ia != NULL) 1109 if (ia != NULL)
1110 ia4_acquire(ia, &psref_ia); 1110 ia4_acquire(ia, &psref_ia);
1111 pserialize_read_exit(s); 1111 pserialize_read_exit(s);
1112 1112
1113 if (ah->ar_hln != ifp->if_addrlen) { 1113 if (ah->ar_hln != ifp->if_addrlen) {
1114 ARP_STATINC(ARP_STAT_RCVBADLEN); 1114 ARP_STATINC(ARP_STAT_RCVBADLEN);
1115 log(LOG_WARNING, 1115 log(LOG_WARNING,
1116 "arp from %s: addr len: new %d, i/f %d (ignored)\n", 1116 "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1117 IN_PRINT(ipbuf, &isaddr), ah->ar_hln, ifp->if_addrlen); 1117 IN_PRINT(ipbuf, &isaddr), ah->ar_hln, ifp->if_addrlen);
1118 goto out; 1118 goto out;
1119 } 1119 }
1120 1120
1121 /* Only do DaD if we have a matching address. */ 1121 /* Only do DaD if we have a matching address. */
1122 do_dad = (ia != NULL); 1122 do_dad = (ia != NULL);
1123 1123
1124 if (ia == NULL) { 1124 if (ia == NULL) {
1125 ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia); 1125 ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia);
1126 if (ia == NULL) { 1126 if (ia == NULL) {
1127 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia); 1127 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
1128 if (ia == NULL) { 1128 if (ia == NULL) {
1129 ARP_STATINC(ARP_STAT_RCVNOINT); 1129 ARP_STATINC(ARP_STAT_RCVNOINT);
1130 goto out; 1130 goto out;
1131 } 1131 }
1132 } 1132 }
1133 } 1133 }
1134 1134
1135 myaddr = ia->ia_addr.sin_addr; 1135 myaddr = ia->ia_addr.sin_addr;
1136 1136
1137 /* XXX checks for bridge case? */ 1137 /* XXX checks for bridge case? */
1138 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 1138 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1139 ARP_STATINC(ARP_STAT_RCVBCASTSHA); 1139 ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1140 log(LOG_ERR, 1140 log(LOG_ERR,
1141 "%s: arp: link address is broadcast for IP address %s!\n", 1141 "%s: arp: link address is broadcast for IP address %s!\n",
1142 ifp->if_xname, IN_PRINT(ipbuf, &isaddr)); 1142 ifp->if_xname, IN_PRINT(ipbuf, &isaddr));
1143 goto out; 1143 goto out;
1144 } 1144 }
1145 1145
1146 /* 1146 /*
1147 * If the source IP address is zero, this is an RFC 5227 ARP probe 1147 * If the source IP address is zero, this is an RFC 5227 ARP probe
1148 */ 1148 */
1149 if (in_nullhost(isaddr)) 1149 if (in_nullhost(isaddr))
1150 ARP_STATINC(ARP_STAT_RCVZEROSPA); 1150 ARP_STATINC(ARP_STAT_RCVZEROSPA);
1151 else if (in_hosteq(isaddr, myaddr)) 1151 else if (in_hosteq(isaddr, myaddr))
1152 ARP_STATINC(ARP_STAT_RCVLOCALSPA); 1152 ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1153 1153
1154 if (in_nullhost(itaddr)) 1154 if (in_nullhost(itaddr))
1155 ARP_STATINC(ARP_STAT_RCVZEROTPA); 1155 ARP_STATINC(ARP_STAT_RCVZEROTPA);
1156 1156
1157 /* 1157 /*
1158 * DAD check, RFC 5227. 1158 * DAD check, RFC 5227.
1159 * Collision on sender address is always a duplicate. 1159 * Collision on sender address is always a duplicate.
1160 * Collision on target address is only a duplicate IF 1160 * Collision on target address is only a duplicate
1161 * the sender address is the null host (ie a DAD probe) AND 1161 * IF the sender address is the null host (ie a DAD probe)
1162 * the message was broadcast - if it's unicast then it's 1162 * AND the message was broadcast
1163 * a valid Unicast Poll from RFC 1122. 1163 * AND our address is either tentative or duplicated
 1164 * If it was unicast then it's a valid Unicast Poll from RFC 1122.
1164 */ 1165 */
1165 if (do_dad && 1166 if (do_dad &&
1166 (in_hosteq(isaddr, myaddr) || 1167 (in_hosteq(isaddr, myaddr) ||
1167 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr) && 1168 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr) &&
1168 m->m_flags & M_BCAST))) 1169 m->m_flags & M_BCAST &&
 1170 ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED))))
1169 { 1171 {
1170 struct sockaddr_dl sdl, *sdlp; 1172 struct sockaddr_dl sdl, *sdlp;
1171 1173
1172 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl), 1174 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl),
1173 ifp->if_index, ifp->if_type, 1175 ifp->if_index, ifp->if_type,
1174 NULL, 0, ar_sha(ah), ah->ar_hln); 1176 NULL, 0, ar_sha(ah), ah->ar_hln);
1175 arp_dad_duplicated((struct ifaddr *)ia, sdlp); 1177 arp_dad_duplicated((struct ifaddr *)ia, sdlp);
1176 goto out; 1178 goto out;
1177 } 1179 }
1178 1180
1179 /* 1181 /*
1180 * If the target IP address is zero, ignore the packet. 1182 * If the target IP address is zero, ignore the packet.
1181 * This prevents the code below from trying to answer 1183 * This prevents the code below from trying to answer
1182 * when we are using IP address zero (booting). 1184 * when we are using IP address zero (booting).
1183 */ 1185 */
1184 if (in_nullhost(itaddr)) 1186 if (in_nullhost(itaddr))
1185 goto out; 1187 goto out;
1186 1188
1187 if (in_nullhost(isaddr)) 1189 if (in_nullhost(isaddr))
1188 goto reply; 1190 goto reply;
1189 1191
1190 if (in_hosteq(itaddr, myaddr)) 1192 if (in_hosteq(itaddr, myaddr))
1191 la = arpcreate(ifp, &isaddr, NULL, 1); 1193 la = arpcreate(ifp, &isaddr, NULL, 1);
1192 else 1194 else
1193 la = arplookup(ifp, &isaddr, NULL, 1); 1195 la = arplookup(ifp, &isaddr, NULL, 1);
1194 if (la == NULL) 1196 if (la == NULL)
1195 goto reply; 1197 goto reply;
1196 1198
1197 if ((la->la_flags & LLE_VALID) && 1199 if ((la->la_flags & LLE_VALID) &&
1198 memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) 1200 memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen))
1199 { 1201 {
1200 char llabuf[LLA_ADDRSTRLEN], *llastr; 1202 char llabuf[LLA_ADDRSTRLEN], *llastr;
1201 1203
1202 llastr = lla_snprintf(llabuf, sizeof(llabuf), 1204 llastr = lla_snprintf(llabuf, sizeof(llabuf),
1203 ar_sha(ah), ah->ar_hln); 1205 ar_sha(ah), ah->ar_hln);
1204 1206
1205 if (la->la_flags & LLE_STATIC) { 1207 if (la->la_flags & LLE_STATIC) {
1206 ARP_STATINC(ARP_STAT_RCVOVERPERM); 1208 ARP_STATINC(ARP_STAT_RCVOVERPERM);
1207 if (!log_permanent_modify) 1209 if (!log_permanent_modify)
1208 goto out; 1210 goto out;
1209 log(LOG_INFO, 1211 log(LOG_INFO,
1210 "%s tried to overwrite permanent arp info" 1212 "%s tried to overwrite permanent arp info"
1211 " for %s\n", llastr, IN_PRINT(ipbuf, &isaddr)); 1213 " for %s\n", llastr, IN_PRINT(ipbuf, &isaddr));
1212 goto out; 1214 goto out;
1213 } else if (la->lle_tbl->llt_ifp != ifp) { 1215 } else if (la->lle_tbl->llt_ifp != ifp) {
1214 /* XXX should not happen? */ 1216 /* XXX should not happen? */
1215 ARP_STATINC(ARP_STAT_RCVOVERINT); 1217 ARP_STATINC(ARP_STAT_RCVOVERINT);
1216 if (!log_wrong_iface) 1218 if (!log_wrong_iface)
1217 goto out; 1219 goto out;
1218 log(LOG_INFO, 1220 log(LOG_INFO,
1219 "%s on %s tried to overwrite " 1221 "%s on %s tried to overwrite "
1220 "arp info for %s on %s\n", 1222 "arp info for %s on %s\n",
1221 llastr, 1223 llastr,
1222 ifp->if_xname, IN_PRINT(ipbuf, &isaddr), 1224 ifp->if_xname, IN_PRINT(ipbuf, &isaddr),
1223 la->lle_tbl->llt_ifp->if_xname); 1225 la->lle_tbl->llt_ifp->if_xname);
1224 goto out; 1226 goto out;
1225 } else { 1227 } else {
1226 ARP_STATINC(ARP_STAT_RCVOVER); 1228 ARP_STATINC(ARP_STAT_RCVOVER);
1227 if (log_movements) 1229 if (log_movements)
1228 log(LOG_INFO, "arp info overwritten " 1230 log(LOG_INFO, "arp info overwritten "
1229 "for %s by %s\n", 1231 "for %s by %s\n",
1230 IN_PRINT(ipbuf, &isaddr), llastr); 1232 IN_PRINT(ipbuf, &isaddr), llastr);
1231 } 1233 }
1232 rt_cmd = RTM_CHANGE; 1234 rt_cmd = RTM_CHANGE;
1233 } else 1235 } else
1234 rt_cmd = la->la_flags & LLE_VALID ? 0 : RTM_ADD; 1236 rt_cmd = la->la_flags & LLE_VALID ? 0 : RTM_ADD;
1235 1237
1236 KASSERT(ifp->if_sadl->sdl_alen == ifp->if_addrlen); 1238 KASSERT(ifp->if_sadl->sdl_alen == ifp->if_addrlen);
1237 1239
1238#if NTOKEN > 0 1240#if NTOKEN > 0
1239 /* 1241 /*
1240 * XXX uses m_data and assumes the complete answer including 1242 * XXX uses m_data and assumes the complete answer including
1241 * XXX token-ring headers is in the same buf 1243 * XXX token-ring headers is in the same buf
1242 */ 1244 */
1243 if (ifp->if_type == IFT_ISO88025) { 1245 if (ifp->if_type == IFT_ISO88025) {
1244 struct token_header *trh; 1246 struct token_header *trh;
1245 1247
1246 trh = (struct token_header *)M_TRHSTART(m); 1248 trh = (struct token_header *)M_TRHSTART(m);
1247 if (trh->token_shost[0] & TOKEN_RI_PRESENT) { 1249 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1248 struct token_rif *rif; 1250 struct token_rif *rif;
1249 size_t riflen; 1251 size_t riflen;
1250 1252
1251 rif = TOKEN_RIF(trh); 1253 rif = TOKEN_RIF(trh);
1252 riflen = (ntohs(rif->tr_rcf) & 1254 riflen = (ntohs(rif->tr_rcf) &
1253 TOKEN_RCF_LEN_MASK) >> 8; 1255 TOKEN_RCF_LEN_MASK) >> 8;
1254 1256
1255 if (riflen > 2 && 1257 if (riflen > 2 &&
1256 riflen < sizeof(struct token_rif) && 1258 riflen < sizeof(struct token_rif) &&
1257 (riflen & 1) == 0) { 1259 (riflen & 1) == 0) {
1258 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION); 1260 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1259 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK); 1261 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1260 memcpy(TOKEN_RIF_LLE(la), rif, riflen); 1262 memcpy(TOKEN_RIF_LLE(la), rif, riflen);
1261 } 1263 }
1262 } 1264 }
1263 } 1265 }
1264#endif 1266#endif
1265 1267
1266 KASSERT(sizeof(la->ll_addr) >= ifp->if_addrlen); 1268 KASSERT(sizeof(la->ll_addr) >= ifp->if_addrlen);
1267 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 1269 memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1268 la->la_flags |= LLE_VALID; 1270 la->la_flags |= LLE_VALID;
1269 if ((la->la_flags & LLE_STATIC) == 0) { 1271 if ((la->la_flags & LLE_STATIC) == 0) {
1270 la->la_expire = time_uptime + arpt_keep; 1272 la->la_expire = time_uptime + arpt_keep;
1271 arp_settimer(la, arpt_keep); 1273 arp_settimer(la, arpt_keep);
1272 } 1274 }
1273 la->la_asked = 0; 1275 la->la_asked = 0;
1274 /* rt->rt_flags &= ~RTF_REJECT; */ 1276 /* rt->rt_flags &= ~RTF_REJECT; */
1275 1277
1276 if (rt_cmd != 0) { 1278 if (rt_cmd != 0) {
1277 struct sockaddr_in sin; 1279 struct sockaddr_in sin;
1278 1280
1279 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 1281 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
1280 rt_clonedmsg(rt_cmd, sintosa(&sin), ar_sha(ah), ifp); 1282 rt_clonedmsg(rt_cmd, sintosa(&sin), ar_sha(ah), ifp);
1281 } 1283 }
1282 1284
1283 if (la->la_hold != NULL) { 1285 if (la->la_hold != NULL) {
1284 int n = la->la_numheld; 1286 int n = la->la_numheld;
1285 struct mbuf *m_hold, *m_hold_next; 1287 struct mbuf *m_hold, *m_hold_next;
1286 struct sockaddr_in sin; 1288 struct sockaddr_in sin;
1287 1289
1288 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 1290 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
1289 1291
1290 m_hold = la->la_hold; 1292 m_hold = la->la_hold;
1291 la->la_hold = NULL; 1293 la->la_hold = NULL;
1292 la->la_numheld = 0; 1294 la->la_numheld = 0;
1293 /* 1295 /*
1294 * We have to unlock here because if_output would call 1296 * We have to unlock here because if_output would call
1295 * arpresolve 1297 * arpresolve
1296 */ 1298 */
1297 LLE_WUNLOCK(la); 1299 LLE_WUNLOCK(la);
1298 ARP_STATADD(ARP_STAT_DFRSENT, n); 1300 ARP_STATADD(ARP_STAT_DFRSENT, n);
1299 ARP_STATADD(ARP_STAT_DFRTOTAL, n); 1301 ARP_STATADD(ARP_STAT_DFRTOTAL, n);
1300 for (; m_hold != NULL; m_hold = m_hold_next) { 1302 for (; m_hold != NULL; m_hold = m_hold_next) {
1301 m_hold_next = m_hold->m_nextpkt; 1303 m_hold_next = m_hold->m_nextpkt;
1302 m_hold->m_nextpkt = NULL; 1304 m_hold->m_nextpkt = NULL;
1303 if_output_lock(ifp, ifp, m_hold, sintosa(&sin), NULL); 1305 if_output_lock(ifp, ifp, m_hold, sintosa(&sin), NULL);
1304 } 1306 }
1305 } else 1307 } else
1306 LLE_WUNLOCK(la); 1308 LLE_WUNLOCK(la);
1307 la = NULL; 1309 la = NULL;
1308 1310
1309reply: 1311reply:
1310 if (la != NULL) { 1312 if (la != NULL) {
1311 LLE_WUNLOCK(la); 1313 LLE_WUNLOCK(la);
1312 la = NULL; 1314 la = NULL;
1313 } 1315 }
1314 if (op != ARPOP_REQUEST) { 1316 if (op != ARPOP_REQUEST) {
1315 if (op == ARPOP_REPLY) 1317 if (op == ARPOP_REPLY)
1316 ARP_STATINC(ARP_STAT_RCVREPLY); 1318 ARP_STATINC(ARP_STAT_RCVREPLY);
1317 goto out; 1319 goto out;
1318 } 1320 }
1319 ARP_STATINC(ARP_STAT_RCVREQUEST); 1321 ARP_STATINC(ARP_STAT_RCVREQUEST);
1320 if (in_hosteq(itaddr, myaddr)) { 1322 if (in_hosteq(itaddr, myaddr)) {
1321 /* If our address is unusable, don't reply */ 1323 /* If our address is unusable, don't reply */
1322 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) 1324 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1323 goto out; 1325 goto out;
1324 /* I am the target */ 1326 /* I am the target */
1325 tha = ar_tha(ah); 1327 tha = ar_tha(ah);
1326 if (tha) 1328 if (tha)
1327 memcpy(tha, ar_sha(ah), ah->ar_hln); 1329 memcpy(tha, ar_sha(ah), ah->ar_hln);
1328 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln); 1330 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1329 } else { 1331 } else {
1330 /* Proxy ARP */ 1332 /* Proxy ARP */
1331 struct llentry *lle = NULL; 1333 struct llentry *lle = NULL;
1332 struct sockaddr_in sin; 1334 struct sockaddr_in sin;
1333 1335
1334#if NCARP > 0 1336#if NCARP > 0
1335 if (ifp->if_type == IFT_CARP) { 1337 if (ifp->if_type == IFT_CARP) {
1336 struct ifnet *_rcvif = m_get_rcvif(m, &s); 1338 struct ifnet *_rcvif = m_get_rcvif(m, &s);
1337 int iftype = 0; 1339 int iftype = 0;
1338 if (__predict_true(_rcvif != NULL)) 1340 if (__predict_true(_rcvif != NULL))
1339 iftype = _rcvif->if_type; 1341 iftype = _rcvif->if_type;
1340 m_put_rcvif(_rcvif, &s); 1342 m_put_rcvif(_rcvif, &s);
1341 if (iftype != IFT_CARP) 1343 if (iftype != IFT_CARP)
1342 goto out; 1344 goto out;
1343 } 1345 }
1344#endif 1346#endif
1345 1347
1346 tha = ar_tha(ah); 1348 tha = ar_tha(ah);
1347 1349
1348 sockaddr_in_init(&sin, &itaddr, 0); 1350 sockaddr_in_init(&sin, &itaddr, 0);
1349 1351
1350 IF_AFDATA_RLOCK(ifp); 1352 IF_AFDATA_RLOCK(ifp);
1351 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 1353 lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
1352 IF_AFDATA_RUNLOCK(ifp); 1354 IF_AFDATA_RUNLOCK(ifp);
1353 1355
1354 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) { 1356 if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
1355 if (tha) 1357 if (tha)
1356 memcpy(tha, ar_sha(ah), ah->ar_hln); 1358 memcpy(tha, ar_sha(ah), ah->ar_hln);
1357 memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 1359 memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
1358 LLE_RUNLOCK(lle); 1360 LLE_RUNLOCK(lle);
1359 } else { 1361 } else {
1360 if (lle != NULL) 1362 if (lle != NULL)
1361 LLE_RUNLOCK(lle); 1363 LLE_RUNLOCK(lle);
1362 goto out; 1364 goto out;
1363 } 1365 }
1364 } 1366 }
1365 ia4_release(ia, &psref_ia); 1367 ia4_release(ia, &psref_ia);
1366 1368
1367 /* 1369 /*
1368 * XXX XXX: Here we're recycling the mbuf. But the mbuf could have 1370 * XXX XXX: Here we're recycling the mbuf. But the mbuf could have
1369 * other mbufs in its chain, and just overwriting m->m_pkthdr.len 1371 * other mbufs in its chain, and just overwriting m->m_pkthdr.len
1370 * would be wrong in this case (the length becomes smaller than the 1372 * would be wrong in this case (the length becomes smaller than the
1371 * real chain size). 1373 * real chain size).
1372 * 1374 *
1373 * This can theoretically cause bugs in the lower layers (drivers, 1375 * This can theoretically cause bugs in the lower layers (drivers,
1374 * and L2encap), in some corner cases. 1376 * and L2encap), in some corner cases.
1375 */ 1377 */
1376 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 1378 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1377 memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 1379 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1378 ah->ar_op = htons(ARPOP_REPLY); 1380 ah->ar_op = htons(ARPOP_REPLY);
1379 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 1381 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1380 switch (ifp->if_type) { 1382 switch (ifp->if_type) {
1381 case IFT_IEEE1394: 1383 case IFT_IEEE1394:
1382 /* ieee1394 arp reply is broadcast */ 1384 /* ieee1394 arp reply is broadcast */
1383 m->m_flags &= ~M_MCAST; 1385 m->m_flags &= ~M_MCAST;
1384 m->m_flags |= M_BCAST; 1386 m->m_flags |= M_BCAST;
1385 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln; 1387 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1386 break; 1388 break;
1387 default: 1389 default:
1388 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 1390 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1389 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 1391 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1390 break; 1392 break;
1391 } 1393 }
1392 m->m_pkthdr.len = m->m_len; 1394 m->m_pkthdr.len = m->m_len;
1393 sa.sa_family = AF_ARP; 1395 sa.sa_family = AF_ARP;
1394 sa.sa_len = 2; 1396 sa.sa_len = 2;
1395 arps = ARP_STAT_GETREF(); 1397 arps = ARP_STAT_GETREF();
1396 arps[ARP_STAT_SNDTOTAL]++; 1398 arps[ARP_STAT_SNDTOTAL]++;
1397 arps[ARP_STAT_SNDREPLY]++; 1399 arps[ARP_STAT_SNDREPLY]++;
1398 ARP_STAT_PUTREF(); 1400 ARP_STAT_PUTREF();
1399 if_output_lock(ifp, ifp, m, &sa, NULL); 1401 if_output_lock(ifp, ifp, m, &sa, NULL);
1400 if (rcvif != NULL) 1402 if (rcvif != NULL)
1401 m_put_rcvif_psref(rcvif, &psref); 1403 m_put_rcvif_psref(rcvif, &psref);
1402 return; 1404 return;
1403 1405
1404out: 1406out:
1405 if (la != NULL) 1407 if (la != NULL)
1406 LLE_WUNLOCK(la); 1408 LLE_WUNLOCK(la);
1407 if (ia != NULL) 1409 if (ia != NULL)
1408 ia4_release(ia, &psref_ia); 1410 ia4_release(ia, &psref_ia);
1409 if (rcvif != NULL) 1411 if (rcvif != NULL)
1410 m_put_rcvif_psref(rcvif, &psref); 1412 m_put_rcvif_psref(rcvif, &psref);
1411 m_freem(m); 1413 m_freem(m);
1412} 1414}
1413 1415
1414/* 1416/*
1415 * Lookup or a new address in arptab. 1417 * Lookup or a new address in arptab.
1416 */ 1418 */
1417static struct llentry * 1419static struct llentry *
1418arplookup(struct ifnet *ifp, const struct in_addr *addr, 1420arplookup(struct ifnet *ifp, const struct in_addr *addr,
1419 const struct sockaddr *sa, int wlock) 1421 const struct sockaddr *sa, int wlock)
1420{ 1422{
1421 struct sockaddr_in sin; 1423 struct sockaddr_in sin;
1422 struct llentry *la; 1424 struct llentry *la;
1423 int flags = wlock ? LLE_EXCLUSIVE : 0; 1425 int flags = wlock ? LLE_EXCLUSIVE : 0;
1424 1426
1425 if (sa == NULL) { 1427 if (sa == NULL) {
1426 KASSERT(addr != NULL); 1428 KASSERT(addr != NULL);
1427 sockaddr_in_init(&sin, addr, 0); 1429 sockaddr_in_init(&sin, addr, 0);
1428 sa = sintocsa(&sin); 1430 sa = sintocsa(&sin);
1429 } 1431 }
1430 1432
1431 IF_AFDATA_RLOCK(ifp); 1433 IF_AFDATA_RLOCK(ifp);
1432 la = lla_lookup(LLTABLE(ifp), flags, sa); 1434 la = lla_lookup(LLTABLE(ifp), flags, sa);
1433 IF_AFDATA_RUNLOCK(ifp); 1435 IF_AFDATA_RUNLOCK(ifp);
1434 1436
1435 return la; 1437 return la;
1436} 1438}
1437 1439
1438static struct llentry * 1440static struct llentry *
1439arpcreate(struct ifnet *ifp, const struct in_addr *addr, 1441arpcreate(struct ifnet *ifp, const struct in_addr *addr,
1440 const struct sockaddr *sa, int wlock) 1442 const struct sockaddr *sa, int wlock)
1441{ 1443{
1442 struct sockaddr_in sin; 1444 struct sockaddr_in sin;
1443 struct llentry *la; 1445 struct llentry *la;
1444 int flags = wlock ? LLE_EXCLUSIVE : 0; 1446 int flags = wlock ? LLE_EXCLUSIVE : 0;
1445 1447
1446 if (sa == NULL) { 1448 if (sa == NULL) {
1447 KASSERT(addr != NULL); 1449 KASSERT(addr != NULL);
1448 sockaddr_in_init(&sin, addr, 0); 1450 sockaddr_in_init(&sin, addr, 0);
1449 sa = sintocsa(&sin); 1451 sa = sintocsa(&sin);
1450 } 1452 }
1451 1453
1452 la = arplookup(ifp, addr, sa, wlock); 1454 la = arplookup(ifp, addr, sa, wlock);
1453 1455
1454 if (la == NULL) { 1456 if (la == NULL) {
1455 struct rtentry *rt; 1457 struct rtentry *rt;
1456 1458
1457 rt = rtalloc1(sa, 0); 1459 rt = rtalloc1(sa, 0);
1458 IF_AFDATA_WLOCK(ifp); 1460 IF_AFDATA_WLOCK(ifp);
1459 la = lla_create(LLTABLE(ifp), flags, sa, rt); 1461 la = lla_create(LLTABLE(ifp), flags, sa, rt);
1460 IF_AFDATA_WUNLOCK(ifp); 1462 IF_AFDATA_WUNLOCK(ifp);
1461 if (rt != NULL) 1463 if (rt != NULL)
1462 rt_unref(rt); 1464 rt_unref(rt);
1463 1465
1464 if (la != NULL) 1466 if (la != NULL)
1465 arp_init_llentry(ifp, la); 1467 arp_init_llentry(ifp, la);
1466 } 1468 }
1467 1469
1468 return la; 1470 return la;
1469} 1471}
1470 1472
1471int 1473int
1472arpioctl(u_long cmd, void *data) 1474arpioctl(u_long cmd, void *data)
1473{ 1475{
1474 1476
1475 return EOPNOTSUPP; 1477 return EOPNOTSUPP;
1476} 1478}
1477 1479
1478void 1480void
1479arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1481arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1480{ 1482{
1481 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1483 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1482 1484
1483 ifa->ifa_rtrequest = arp_rtrequest; 1485 ifa->ifa_rtrequest = arp_rtrequest;
1484 ifa->ifa_flags |= RTF_CONNECTED; 1486 ifa->ifa_flags |= RTF_CONNECTED;
1485 1487
1486 /* ARP will handle DAD for this address. */ 1488 /* ARP will handle DAD for this address. */
1487 if (in_nullhost(IA_SIN(ifa)->sin_addr)) { 1489 if (in_nullhost(IA_SIN(ifa)->sin_addr)) {
1488 if (ia->ia_dad_stop != NULL) /* safety */ 1490 if (ia->ia_dad_stop != NULL) /* safety */
1489 ia->ia_dad_stop(ifa); 1491 ia->ia_dad_stop(ifa);
1490 ia->ia_dad_start = NULL; 1492 ia->ia_dad_start = NULL;
1491 ia->ia_dad_stop = NULL; 1493 ia->ia_dad_stop = NULL;
1492 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1494 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1493 } else { 1495 } else {
1494 ia->ia_dad_start = arp_dad_start; 1496 ia->ia_dad_start = arp_dad_start;
1495 ia->ia_dad_stop = arp_dad_stop; 1497 ia->ia_dad_stop = arp_dad_stop;
1496 if (ia->ia4_flags & IN_IFF_TRYTENTATIVE && ip_dad_enabled()) 1498 if (ia->ia4_flags & IN_IFF_TRYTENTATIVE && ip_dad_enabled())
1497 ia->ia4_flags |= IN_IFF_TENTATIVE; 1499 ia->ia4_flags |= IN_IFF_TENTATIVE;
1498 else 1500 else
1499 arpannounce1(ifa); 1501 arpannounce1(ifa);
1500 } 1502 }
1501} 1503}
1502 1504
1503TAILQ_HEAD(dadq_head, dadq); 1505TAILQ_HEAD(dadq_head, dadq);
1504struct dadq { 1506struct dadq {
1505 TAILQ_ENTRY(dadq) dad_list; 1507 TAILQ_ENTRY(dadq) dad_list;
1506 struct ifaddr *dad_ifa; 1508 struct ifaddr *dad_ifa;
1507 int dad_count; /* max ARP to send */ 1509 int dad_count; /* max ARP to send */
1508 int dad_arp_tcount; /* # of trials to send ARP */ 1510 int dad_arp_tcount; /* # of trials to send ARP */
1509 int dad_arp_ocount; /* ARP sent so far */ 1511 int dad_arp_ocount; /* ARP sent so far */
1510 int dad_arp_announce; /* max ARP announcements */ 1512 int dad_arp_announce; /* max ARP announcements */
1511 int dad_arp_acount; /* # of announcements */ 1513 int dad_arp_acount; /* # of announcements */
1512 struct callout dad_timer_ch; 1514 struct callout dad_timer_ch;
1513}; 1515};
1514 1516
1515static struct dadq_head dadq; 1517static struct dadq_head dadq;
1516static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */ 1518static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
1517static kmutex_t arp_dad_lock; 1519static kmutex_t arp_dad_lock;
1518 1520
1519static void 1521static void
1520arp_dad_init(void) 1522arp_dad_init(void)
1521{ 1523{
1522 1524
1523 TAILQ_INIT(&dadq); 1525 TAILQ_INIT(&dadq);
1524 mutex_init(&arp_dad_lock, MUTEX_DEFAULT, IPL_NONE); 1526 mutex_init(&arp_dad_lock, MUTEX_DEFAULT, IPL_NONE);
1525} 1527}
1526 1528
1527static struct dadq * 1529static struct dadq *
1528arp_dad_find(struct ifaddr *ifa) 1530arp_dad_find(struct ifaddr *ifa)
1529{ 1531{
1530 struct dadq *dp; 1532 struct dadq *dp;
1531 1533
1532 KASSERT(mutex_owned(&arp_dad_lock)); 1534 KASSERT(mutex_owned(&arp_dad_lock));
1533 1535
1534 TAILQ_FOREACH(dp, &dadq, dad_list) { 1536 TAILQ_FOREACH(dp, &dadq, dad_list) {
1535 if (dp->dad_ifa == ifa) 1537 if (dp->dad_ifa == ifa)
1536 return dp; 1538 return dp;
1537 } 1539 }
1538 return NULL; 1540 return NULL;
1539} 1541}
1540 1542
1541static void 1543static void
1542arp_dad_starttimer(struct dadq *dp, int ticks) 1544arp_dad_starttimer(struct dadq *dp, int ticks)
1543{ 1545{
1544 1546
1545 callout_reset(&dp->dad_timer_ch, ticks, 1547 callout_reset(&dp->dad_timer_ch, ticks,
1546 (void (*)(void *))arp_dad_timer, dp); 1548 (void (*)(void *))arp_dad_timer, dp);
1547} 1549}
1548 1550
1549static void 1551static void
1550arp_dad_stoptimer(struct dadq *dp) 1552arp_dad_stoptimer(struct dadq *dp)
1551{ 1553{
1552 1554
1553 KASSERT(mutex_owned(&arp_dad_lock)); 1555 KASSERT(mutex_owned(&arp_dad_lock));
1554 1556
1555 TAILQ_REMOVE(&dadq, dp, dad_list); 1557 TAILQ_REMOVE(&dadq, dp, dad_list);
1556 /* Tell the timer that dp is being destroyed. */ 1558 /* Tell the timer that dp is being destroyed. */
1557 dp->dad_ifa = NULL; 1559 dp->dad_ifa = NULL;
1558 callout_halt(&dp->dad_timer_ch, &arp_dad_lock); 1560 callout_halt(&dp->dad_timer_ch, &arp_dad_lock);
1559} 1561}
1560 1562
1561static void 1563static void
1562arp_dad_destroytimer(struct dadq *dp) 1564arp_dad_destroytimer(struct dadq *dp)
1563{ 1565{
1564 1566
1565 callout_destroy(&dp->dad_timer_ch); 1567 callout_destroy(&dp->dad_timer_ch);
1566 KASSERT(dp->dad_ifa == NULL); 1568 KASSERT(dp->dad_ifa == NULL);
1567 kmem_intr_free(dp, sizeof(*dp)); 1569 kmem_intr_free(dp, sizeof(*dp));
1568} 1570}
1569 1571
1570static void 1572static void
1571arp_dad_output(struct dadq *dp, struct ifaddr *ifa) 1573arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
1572{ 1574{
1573 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1575 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1574 struct ifnet *ifp = ifa->ifa_ifp; 1576 struct ifnet *ifp = ifa->ifa_ifp;
1575 struct in_addr sip; 1577 struct in_addr sip;
1576 1578
1577 dp->dad_arp_tcount++; 1579 dp->dad_arp_tcount++;
1578 if ((ifp->if_flags & IFF_UP) == 0) 1580 if ((ifp->if_flags & IFF_UP) == 0)
1579 return; 1581 return;
1580 if ((ifp->if_flags & IFF_RUNNING) == 0) 1582 if ((ifp->if_flags & IFF_RUNNING) == 0)
1581 return; 1583 return;
1582 1584
1583 dp->dad_arp_tcount = 0; 1585 dp->dad_arp_tcount = 0;
1584 dp->dad_arp_ocount++; 1586 dp->dad_arp_ocount++;
1585 1587
1586 memset(&sip, 0, sizeof(sip)); 1588 memset(&sip, 0, sizeof(sip));
1587 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr, 1589 arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
1588 CLLADDR(ifa->ifa_ifp->if_sadl)); 1590 CLLADDR(ifa->ifa_ifp->if_sadl));
1589} 1591}
1590 1592
1591/* 1593/*
1592 * Start Duplicate Address Detection (DAD) for specified interface address. 1594 * Start Duplicate Address Detection (DAD) for specified interface address.
1593 */ 1595 */
1594static void 1596static void
1595arp_dad_start(struct ifaddr *ifa) 1597arp_dad_start(struct ifaddr *ifa)
1596{ 1598{
1597 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1599 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1598 struct dadq *dp; 1600 struct dadq *dp;
1599 char ipbuf[INET_ADDRSTRLEN]; 1601 char ipbuf[INET_ADDRSTRLEN];
1600 1602
1601 /* 1603 /*
1602 * If we don't need DAD, don't do it. 1604 * If we don't need DAD, don't do it.
1603 * - DAD is disabled 1605 * - DAD is disabled
1604 */ 1606 */
1605 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) { 1607 if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
1606 log(LOG_DEBUG, 1608 log(LOG_DEBUG,
1607 "%s: called with non-tentative address %s(%s)\n", __func__, 1609 "%s: called with non-tentative address %s(%s)\n", __func__,
1608 IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1610 IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1609 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1611 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1610 return; 1612 return;
1611 } 1613 }
1612 if (!ip_dad_enabled()) { 1614 if (!ip_dad_enabled()) {
1613 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1615 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1614 rt_addrmsg(RTM_NEWADDR, ifa); 1616 rt_addrmsg(RTM_NEWADDR, ifa);
1615 arpannounce1(ifa); 1617 arpannounce1(ifa);
1616 return; 1618 return;
1617 } 1619 }
1618 KASSERT(ifa->ifa_ifp != NULL); 1620 KASSERT(ifa->ifa_ifp != NULL);
1619 if (!(ifa->ifa_ifp->if_flags & IFF_UP)) 1621 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1620 return; 1622 return;
1621 1623
1622 dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP); 1624 dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP);
1623 1625
1624 mutex_enter(&arp_dad_lock); 1626 mutex_enter(&arp_dad_lock);
1625 if (arp_dad_find(ifa) != NULL) { 1627 if (arp_dad_find(ifa) != NULL) {
1626 mutex_exit(&arp_dad_lock); 1628 mutex_exit(&arp_dad_lock);
1627 /* DAD already in progress */ 1629 /* DAD already in progress */
1628 if (dp != NULL) 1630 if (dp != NULL)
1629 kmem_intr_free(dp, sizeof(*dp)); 1631 kmem_intr_free(dp, sizeof(*dp));
1630 return; 1632 return;
1631 } 1633 }
1632 1634
1633 if (dp == NULL) { 1635 if (dp == NULL) {
1634 mutex_exit(&arp_dad_lock); 1636 mutex_exit(&arp_dad_lock);
1635 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n", 1637 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
1636 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1638 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1637 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1639 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1638 return; 1640 return;
1639 } 1641 }
1640 1642
1641 /* 1643 /*
1642 * Send ARP packet for DAD, ip_dad_count times. 1644 * Send ARP packet for DAD, ip_dad_count times.
1643 * Note that we must delay the first transmission. 1645 * Note that we must delay the first transmission.
1644 */ 1646 */
1645 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE); 1647 callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1646 dp->dad_ifa = ifa; 1648 dp->dad_ifa = ifa;
1647 ifaref(ifa); /* just for safety */ 1649 ifaref(ifa); /* just for safety */
1648 dp->dad_count = ip_dad_count; 1650 dp->dad_count = ip_dad_count;
1649 dp->dad_arp_announce = 0; /* Will be set when starting to announce */ 1651 dp->dad_arp_announce = 0; /* Will be set when starting to announce */
1650 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0; 1652 dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
1651 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list); 1653 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1652 1654
1653 ARPLOG(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), 1655 ARPLOG(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1654 ARPLOGADDR(&ia->ia_addr.sin_addr)); 1656 ARPLOGADDR(&ia->ia_addr.sin_addr));
1655 1657
1656 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz)); 1658 arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
1657 1659
1658 mutex_exit(&arp_dad_lock); 1660 mutex_exit(&arp_dad_lock);
1659} 1661}
1660 1662
1661/* 1663/*
1662 * terminate DAD unconditionally. used for address removals. 1664 * terminate DAD unconditionally. used for address removals.
1663 */ 1665 */
1664static void 1666static void
1665arp_dad_stop(struct ifaddr *ifa) 1667arp_dad_stop(struct ifaddr *ifa)
1666{ 1668{
1667 struct dadq *dp; 1669 struct dadq *dp;
1668 1670
1669 mutex_enter(&arp_dad_lock); 1671 mutex_enter(&arp_dad_lock);
1670 dp = arp_dad_find(ifa); 1672 dp = arp_dad_find(ifa);
1671 if (dp == NULL) { 1673 if (dp == NULL) {
1672 mutex_exit(&arp_dad_lock); 1674 mutex_exit(&arp_dad_lock);
1673 /* DAD wasn't started yet */ 1675 /* DAD wasn't started yet */
1674 return; 1676 return;
1675 } 1677 }
1676 1678
1677 arp_dad_stoptimer(dp); 1679 arp_dad_stoptimer(dp);
1678 1680
1679 mutex_exit(&arp_dad_lock); 1681 mutex_exit(&arp_dad_lock);
1680 1682
1681 arp_dad_destroytimer(dp); 1683 arp_dad_destroytimer(dp);
1682 ifafree(ifa); 1684 ifafree(ifa);
1683} 1685}
1684 1686
1685static void 1687static void
1686arp_dad_timer(struct dadq *dp) 1688arp_dad_timer(struct dadq *dp)
1687{ 1689{
1688 struct ifaddr *ifa; 1690 struct ifaddr *ifa;
1689 struct in_ifaddr *ia; 1691 struct in_ifaddr *ia;
1690 char ipbuf[INET_ADDRSTRLEN]; 1692 char ipbuf[INET_ADDRSTRLEN];
1691 bool need_free = false; 1693 bool need_free = false;
1692 1694
1693 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 1695 KERNEL_LOCK_UNLESS_NET_MPSAFE();
1694 mutex_enter(&arp_dad_lock); 1696 mutex_enter(&arp_dad_lock);
1695 1697
1696 ifa = dp->dad_ifa; 1698 ifa = dp->dad_ifa;
1697 if (ifa == NULL) { 1699 if (ifa == NULL) {
1698 /* dp is being destroyed by someone. Do nothing. */ 1700 /* dp is being destroyed by someone. Do nothing. */
1699 goto done; 1701 goto done;
1700 } 1702 }
1701 1703
1702 ia = (struct in_ifaddr *)ifa; 1704 ia = (struct in_ifaddr *)ifa;
1703 if (ia->ia4_flags & IN_IFF_DUPLICATED) { 1705 if (ia->ia4_flags & IN_IFF_DUPLICATED) {
1704 log(LOG_ERR, "%s: called with duplicate address %s(%s)\n", 1706 log(LOG_ERR, "%s: called with duplicate address %s(%s)\n",
1705 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1707 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1706 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1708 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1707 goto done; 1709 goto done;
1708 } 1710 }
1709 if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0) 1711 if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0)
1710 { 1712 {
1711 log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n", 1713 log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
1712 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr), 1714 __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
1713 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 1715 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1714 goto done; 1716 goto done;
1715 } 1717 }
1716 1718
1717 /* timeouted with IFF_{RUNNING,UP} check */ 1719 /* timeouted with IFF_{RUNNING,UP} check */
1718 if (dp->dad_arp_tcount > dad_maxtry) { 1720 if (dp->dad_arp_tcount > dad_maxtry) {
1719 ARPLOG(LOG_INFO, "%s: could not run DAD, driver problem?\n", 1721 ARPLOG(LOG_INFO, "%s: could not run DAD, driver problem?\n",
1720 if_name(ifa->ifa_ifp)); 1722 if_name(ifa->ifa_ifp));
1721 1723
1722 arp_dad_stoptimer(dp); 1724 arp_dad_stoptimer(dp);
1723 need_free = true; 1725 need_free = true;
1724 goto done; 1726 goto done;
1725 } 1727 }
1726 1728
1727 /* Need more checks? */ 1729 /* Need more checks? */
1728 if (dp->dad_arp_ocount < dp->dad_count) { 1730 if (dp->dad_arp_ocount < dp->dad_count) {
1729 int adelay; 1731 int adelay;
1730 1732
1731 /* 1733 /*
1732 * We have more ARP to go. Send ARP packet for DAD. 1734 * We have more ARP to go. Send ARP packet for DAD.
1733 */ 1735 */
1734 arp_dad_output(dp, ifa); 1736 arp_dad_output(dp, ifa);
1735 if (dp->dad_arp_ocount < dp->dad_count) 1737 if (dp->dad_arp_ocount < dp->dad_count)
1736 adelay = (PROBE_MIN * hz) + 1738 adelay = (PROBE_MIN * hz) +
1737 (cprng_fast32() % 1739 (cprng_fast32() %
1738 ((PROBE_MAX * hz) - (PROBE_MIN * hz))); 1740 ((PROBE_MAX * hz) - (PROBE_MIN * hz)));
1739 else 1741 else
1740 adelay = ANNOUNCE_WAIT * hz; 1742 adelay = ANNOUNCE_WAIT * hz;
1741 arp_dad_starttimer(dp, adelay); 1743 arp_dad_starttimer(dp, adelay);
1742 goto done; 1744 goto done;
1743 } else if (dp->dad_arp_acount == 0) { 1745 } else if (dp->dad_arp_acount == 0) {
1744 /* 1746 /*
1745 * We are done with DAD. 1747 * We are done with DAD.
1746 * No duplicate address found. 1748 * No duplicate address found.
1747 */ 1749 */
1748 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1750 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1749 rt_addrmsg(RTM_NEWADDR, ifa); 1751 rt_addrmsg(RTM_NEWADDR, ifa);
1750 ARPLOG(LOG_DEBUG, 1752 ARPLOG(LOG_DEBUG,
1751 "%s: DAD complete for %s - no duplicates found\n", 1753 "%s: DAD complete for %s - no duplicates found\n",
1752 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr)); 1754 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr));
1753 dp->dad_arp_announce = ANNOUNCE_NUM; 1755 dp->dad_arp_announce = ANNOUNCE_NUM;
1754 goto announce; 1756 goto announce;
1755 } else if (dp->dad_arp_acount < dp->dad_arp_announce) { 1757 } else if (dp->dad_arp_acount < dp->dad_arp_announce) {
1756announce: 1758announce:
1757 /* 1759 /*
1758 * Announce the address. 1760 * Announce the address.
1759 */ 1761 */
1760 arpannounce1(ifa); 1762 arpannounce1(ifa);
1761 dp->dad_arp_acount++; 1763 dp->dad_arp_acount++;
1762 if (dp->dad_arp_acount < dp->dad_arp_announce) { 1764 if (dp->dad_arp_acount < dp->dad_arp_announce) {
1763 arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz); 1765 arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz);
1764 goto done; 1766 goto done;
1765 } 1767 }
1766 ARPLOG(LOG_DEBUG, 1768 ARPLOG(LOG_DEBUG,
1767 "%s: ARP announcement complete for %s\n", 1769 "%s: ARP announcement complete for %s\n",
1768 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr)); 1770 if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr));
1769 } 1771 }
1770 1772
1771 arp_dad_stoptimer(dp); 1773 arp_dad_stoptimer(dp);
1772 need_free = true; 1774 need_free = true;
1773done: 1775done:
1774 mutex_exit(&arp_dad_lock); 1776 mutex_exit(&arp_dad_lock);
1775 1777
1776 if (need_free) { 1778 if (need_free) {
1777 arp_dad_destroytimer(dp); 1779 arp_dad_destroytimer(dp);
1778 KASSERT(ifa != NULL); 1780 KASSERT(ifa != NULL);
1779 ifafree(ifa); 1781 ifafree(ifa);
1780 } 1782 }
1781 1783
1782 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 1784 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1783} 1785}
1784 1786
1785static void 1787static void
1786arp_dad_duplicated(struct ifaddr *ifa, const struct sockaddr_dl *from) 1788arp_dad_duplicated(struct ifaddr *ifa, const struct sockaddr_dl *from)
1787{ 1789{
1788 struct in_ifaddr *ia = (struct in_ifaddr *)ifa; 1790 struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1789 struct ifnet *ifp = ifa->ifa_ifp; 1791 struct ifnet *ifp = ifa->ifa_ifp;
1790 char ipbuf[INET_ADDRSTRLEN], llabuf[LLA_ADDRSTRLEN]; 1792 char ipbuf[INET_ADDRSTRLEN], llabuf[LLA_ADDRSTRLEN];
1791 const char *iastr, *llastr; 1793 const char *iastr, *llastr;
1792 1794
1793 iastr = IN_PRINT(ipbuf, &ia->ia_addr.sin_addr); 1795 iastr = IN_PRINT(ipbuf, &ia->ia_addr.sin_addr);
1794 if (__predict_false(from == NULL)) 1796 if (__predict_false(from == NULL))
1795 llastr = NULL; 1797 llastr = NULL;
1796 else 1798 else
1797 llastr = lla_snprintf(llabuf, sizeof(llabuf), 1799 llastr = lla_snprintf(llabuf, sizeof(llabuf),
1798 CLLADDR(from), from->sdl_alen); 1800 CLLADDR(from), from->sdl_alen);
1799 1801
1800 if (ia->ia4_flags & (IN_IFF_TENTATIVE|IN_IFF_DUPLICATED)) { 1802 if (ia->ia4_flags & (IN_IFF_TENTATIVE|IN_IFF_DUPLICATED)) {
1801 log(LOG_ERR, 1803 log(LOG_ERR,
1802 "%s: DAD duplicate address %s from %s\n", 1804 "%s: DAD duplicate address %s from %s\n",
1803 if_name(ifp), iastr, llastr); 1805 if_name(ifp), iastr, llastr);
1804 } else if (ia->ia_dad_defended == 0 || 1806 } else if (ia->ia_dad_defended == 0 ||
1805 ia->ia_dad_defended < time_uptime - DEFEND_INTERVAL) { 1807 ia->ia_dad_defended < time_uptime - DEFEND_INTERVAL) {
1806 ia->ia_dad_defended = time_uptime; 1808 ia->ia_dad_defended = time_uptime;
1807 arpannounce1(ifa); 1809 arpannounce1(ifa);
1808 log(LOG_ERR, 1810 log(LOG_ERR,
1809 "%s: DAD defended address %s from %s\n", 1811 "%s: DAD defended address %s from %s\n",
1810 if_name(ifp), iastr, llastr); 1812 if_name(ifp), iastr, llastr);
1811 return; 1813 return;
1812 } else { 1814 } else {
1813 /* If DAD is disabled, just report the duplicate. */ 1815 /* If DAD is disabled, just report the duplicate. */
1814 if (!ip_dad_enabled()) { 1816 if (!ip_dad_enabled()) {
1815 log(LOG_ERR, 1817 log(LOG_ERR,
1816 "%s: DAD ignoring duplicate address %s from %s\n", 1818 "%s: DAD ignoring duplicate address %s from %s\n",
1817 if_name(ifp), iastr, llastr); 1819 if_name(ifp), iastr, llastr);
1818 return; 1820 return;
1819 } 1821 }
1820 log(LOG_ERR, 1822 log(LOG_ERR,
1821 "%s: DAD defence failed for %s from %s\n", 1823 "%s: DAD defence failed for %s from %s\n",
1822 if_name(ifp), iastr, llastr); 1824 if_name(ifp), iastr, llastr);
1823 } 1825 }
1824 1826
1825 arp_dad_stop(ifa); 1827 arp_dad_stop(ifa);
1826 1828
1827 ia->ia4_flags &= ~IN_IFF_TENTATIVE; 1829 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1828 if ((ia->ia4_flags & IN_IFF_DUPLICATED) == 0) { 1830 if ((ia->ia4_flags & IN_IFF_DUPLICATED) == 0) {
1829 ia->ia4_flags |= IN_IFF_DUPLICATED; 1831 ia->ia4_flags |= IN_IFF_DUPLICATED;
1830 /* Inform the routing socket of the duplicate address */ 1832 /* Inform the routing socket of the duplicate address */
1831 rt_addrmsg_src(RTM_NEWADDR, ifa, (const struct sockaddr *)from); 1833 rt_addrmsg_src(RTM_NEWADDR, ifa, (const struct sockaddr *)from);
1832 } 1834 }
1833} 1835}
1834 1836
1835/* 1837/*
1836 * Called from 10 Mb/s Ethernet interrupt handlers 1838 * Called from 10 Mb/s Ethernet interrupt handlers
1837 * when ether packet type ETHERTYPE_REVARP 1839 * when ether packet type ETHERTYPE_REVARP
1838 * is received. Common length and type checks are done here, 1840 * is received. Common length and type checks are done here,
1839 * then the protocol-specific routine is called. 1841 * then the protocol-specific routine is called.
1840 */ 1842 */
1841void 1843void
1842revarpinput(struct mbuf *m) 1844revarpinput(struct mbuf *m)
1843{ 1845{
1844 struct arphdr *ar; 1846 struct arphdr *ar;
1845 int arplen; 1847 int arplen;
1846 1848
1847 arplen = sizeof(struct arphdr); 1849 arplen = sizeof(struct arphdr);
1848 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 1850 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
1849 return; 1851 return;
1850 ar = mtod(m, struct arphdr *); 1852 ar = mtod(m, struct arphdr *);
1851 1853
1852 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) { 1854 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) {
1853 goto out; 1855 goto out;
1854 } 1856 }
1855 1857
1856 arplen = sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln); 1858 arplen = sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln);
1857 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 1859 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
1858 return; 1860 return;
1859 ar = mtod(m, struct arphdr *); 1861 ar = mtod(m, struct arphdr *);
1860 1862
1861 switch (ntohs(ar->ar_pro)) { 1863 switch (ntohs(ar->ar_pro)) {
1862 case ETHERTYPE_IP: 1864 case ETHERTYPE_IP:
1863 case ETHERTYPE_IPTRAILERS: 1865 case ETHERTYPE_IPTRAILERS:
1864 in_revarpinput(m); 1866 in_revarpinput(m);
1865 return; 1867 return;
1866 1868
1867 default: 1869 default:
1868 break; 1870 break;
1869 } 1871 }
1870 1872
1871out: 1873out:
1872 m_freem(m); 1874 m_freem(m);
1873} 1875}
1874 1876
1875/* 1877/*
1876 * RARP for Internet protocols on 10 Mb/s Ethernet. 1878 * RARP for Internet protocols on 10 Mb/s Ethernet.
1877 * Algorithm is that given in RFC 903. 1879 * Algorithm is that given in RFC 903.
1878 * We are only using for bootstrap purposes to get an ip address for one of 1880 * We are only using for bootstrap purposes to get an ip address for one of
1879 * our interfaces. Thus we support no user-interface. 1881 * our interfaces. Thus we support no user-interface.
1880 * 1882 *
1881 * Since the contents of the RARP reply are specific to the interface that 1883 * Since the contents of the RARP reply are specific to the interface that
1882 * sent the request, this code must ensure that they are properly associated. 1884 * sent the request, this code must ensure that they are properly associated.
1883 * 1885 *
1884 * Note: also supports ARP via RARP packets, per the RFC. 1886 * Note: also supports ARP via RARP packets, per the RFC.
1885 */ 1887 */
1886void 1888void
1887in_revarpinput(struct mbuf *m) 1889in_revarpinput(struct mbuf *m)
1888{ 1890{
1889 struct arphdr *ah; 1891 struct arphdr *ah;
1890 void *tha; 1892 void *tha;
1891 int op; 1893 int op;
1892 struct ifnet *rcvif; 1894 struct ifnet *rcvif;
1893 int s; 1895 int s;
1894 1896
1895 ah = mtod(m, struct arphdr *); 1897 ah = mtod(m, struct arphdr *);
1896 op = ntohs(ah->ar_op); 1898 op = ntohs(ah->ar_op);
1897 1899
1898 rcvif = m_get_rcvif(m, &s); 1900 rcvif = m_get_rcvif(m, &s);
1899 if (__predict_false(rcvif == NULL)) 1901 if (__predict_false(rcvif == NULL))
1900 goto out; 1902 goto out;
1901 if (rcvif->if_flags & IFF_NOARP) 1903 if (rcvif->if_flags & IFF_NOARP)
1902 goto out; 1904 goto out;
1903 1905
1904 switch (rcvif->if_type) { 1906 switch (rcvif->if_type) {
1905 case IFT_IEEE1394: 1907 case IFT_IEEE1394:
1906 /* ARP without target hardware address is not supported */ 1908 /* ARP without target hardware address is not supported */
1907 goto out; 1909 goto out;
1908 default: 1910 default:
1909 break; 1911 break;
1910 } 1912 }
1911 1913
1912 switch (op) { 1914 switch (op) {
1913 case ARPOP_REQUEST: 1915 case ARPOP_REQUEST:
1914 case ARPOP_REPLY: /* per RFC */ 1916 case ARPOP_REPLY: /* per RFC */
1915 m_put_rcvif(rcvif, &s); 1917 m_put_rcvif(rcvif, &s);
1916 in_arpinput(m); 1918 in_arpinput(m);
1917 return; 1919 return;
1918 case ARPOP_REVREPLY: 1920 case ARPOP_REVREPLY:
1919 break; 1921 break;
1920 case ARPOP_REVREQUEST: /* handled by rarpd(8) */ 1922 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
1921 default: 1923 default:
1922 goto out; 1924 goto out;
1923 } 1925 }
1924 if (!revarp_in_progress) 1926 if (!revarp_in_progress)
1925 goto out; 1927 goto out;
1926 if (rcvif != myip_ifp) /* !same interface */ 1928 if (rcvif != myip_ifp) /* !same interface */
1927 goto out; 1929 goto out;
1928 if (myip_initialized) 1930 if (myip_initialized)
1929 goto wake; 1931 goto wake;
1930 tha = ar_tha(ah); 1932 tha = ar_tha(ah);
1931 if (tha == NULL) 1933 if (tha == NULL)
1932 goto out; 1934 goto out;
1933 if (ah->ar_pln != sizeof(struct in_addr)) 1935 if (ah->ar_pln != sizeof(struct in_addr))
1934 goto out; 1936 goto out;
1935 if (ah->ar_hln != rcvif->if_sadl->sdl_alen) 1937 if (ah->ar_hln != rcvif->if_sadl->sdl_alen)
1936 goto out; 1938 goto out;
1937 if (memcmp(tha, CLLADDR(rcvif->if_sadl), rcvif->if_sadl->sdl_alen)) 1939 if (memcmp(tha, CLLADDR(rcvif->if_sadl), rcvif->if_sadl->sdl_alen))
1938 goto out; 1940 goto out;
1939 memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip)); 1941 memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
1940 memcpy(&myip, ar_tpa(ah), sizeof(myip)); 1942 memcpy(&myip, ar_tpa(ah), sizeof(myip));
1941 myip_initialized = 1; 1943 myip_initialized = 1;
1942wake: /* Do wakeup every time in case it was missed. */ 1944wake: /* Do wakeup every time in case it was missed. */
1943 wakeup((void *)&myip); 1945 wakeup((void *)&myip);
1944 1946
1945out: 1947out:
1946 m_put_rcvif(rcvif, &s); 1948 m_put_rcvif(rcvif, &s);
1947 m_freem(m); 1949 m_freem(m);
1948} 1950}
1949 1951
1950/* 1952/*
1951 * Send a RARP request for the ip address of the specified interface. 1953 * Send a RARP request for the ip address of the specified interface.
1952 * The request should be RFC 903-compliant. 1954 * The request should be RFC 903-compliant.
1953 */ 1955 */
1954static void 1956static void
1955revarprequest(struct ifnet *ifp) 1957revarprequest(struct ifnet *ifp)
1956{ 1958{
1957 struct sockaddr sa; 1959 struct sockaddr sa;
1958 struct mbuf *m; 1960 struct mbuf *m;
1959 struct arphdr *ah; 1961 struct arphdr *ah;
1960 void *tha; 1962 void *tha;
1961 1963
1962 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 1964 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1963 return; 1965 return;
1964 MCLAIM(m, &arpdomain.dom_mowner); 1966 MCLAIM(m, &arpdomain.dom_mowner);
1965 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 1967 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1966 2*ifp->if_addrlen; 1968 2*ifp->if_addrlen;
1967 m->m_pkthdr.len = m->m_len; 1969 m->m_pkthdr.len = m->m_len;
1968 m_align(m, m->m_len); 1970 m_align(m, m->m_len);
1969 ah = mtod(m, struct arphdr *); 1971 ah = mtod(m, struct arphdr *);
1970 memset(ah, 0, m->m_len); 1972 memset(ah, 0, m->m_len);
1971 ah->ar_pro = htons(ETHERTYPE_IP); 1973 ah->ar_pro = htons(ETHERTYPE_IP);
1972 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 1974 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
1973 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 1975 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
1974 ah->ar_op = htons(ARPOP_REVREQUEST); 1976 ah->ar_op = htons(ARPOP_REVREQUEST);
1975 1977
1976 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln); 1978 memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1977 tha = ar_tha(ah); 1979 tha = ar_tha(ah);
1978 if (tha == NULL) { 1980 if (tha == NULL) {
1979 m_free(m); 1981 m_free(m);
1980 return; 1982 return;
1981 } 1983 }
1982 memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln); 1984 memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
1983 1985
1984 sa.sa_family = AF_ARP; 1986 sa.sa_family = AF_ARP;
1985 sa.sa_len = 2; 1987 sa.sa_len = 2;
1986 m->m_flags |= M_BCAST; 1988 m->m_flags |= M_BCAST;
1987 1989
1988 if_output_lock(ifp, ifp, m, &sa, NULL); 1990 if_output_lock(ifp, ifp, m, &sa, NULL);
1989} 1991}
1990 1992
1991/* 1993/*
1992 * RARP for the ip address of the specified interface, but also 1994 * RARP for the ip address of the specified interface, but also
1993 * save the ip address of the server that sent the answer. 1995 * save the ip address of the server that sent the answer.
1994 * Timeout if no response is received. 1996 * Timeout if no response is received.
1995 */ 1997 */
1996int 1998int
1997revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in, 1999revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1998 struct in_addr *clnt_in) 2000 struct in_addr *clnt_in)
1999{ 2001{
2000 int result, count = 20; 2002 int result, count = 20;
2001 2003
2002 myip_initialized = 0; 2004 myip_initialized = 0;
2003 myip_ifp = ifp; 2005 myip_ifp = ifp;
2004 2006
2005 revarp_in_progress = 1; 2007 revarp_in_progress = 1;
2006 while (count--) { 2008 while (count--) {
2007 revarprequest(ifp); 2009 revarprequest(ifp);
2008 result = tsleep((void *)&myip, PSOCK, "revarp", hz/2); 2010 result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
2009 if (result != EWOULDBLOCK) 2011 if (result != EWOULDBLOCK)
2010 break; 2012 break;
2011 } 2013 }
2012 revarp_in_progress = 0; 2014 revarp_in_progress = 0;
2013 2015
2014 if (!myip_initialized) 2016 if (!myip_initialized)
2015 return ENETUNREACH; 2017 return ENETUNREACH;
2016 2018
2017 memcpy(serv_in, &srv_ip, sizeof(*serv_in)); 2019 memcpy(serv_in, &srv_ip, sizeof(*serv_in));
2018 memcpy(clnt_in, &myip, sizeof(*clnt_in)); 2020 memcpy(clnt_in, &myip, sizeof(*clnt_in));
2019 return 0; 2021 return 0;
2020} 2022}
2021 2023
2022void 2024void
2023arp_stat_add(int type, uint64_t count) 2025arp_stat_add(int type, uint64_t count)
2024{ 2026{
2025 ARP_STATADD(type, count); 2027 ARP_STATADD(type, count);
2026} 2028}
2027 2029
2028static int 2030static int
2029sysctl_net_inet_arp_stats(SYSCTLFN_ARGS) 2031sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
2030{ 2032{
2031 2033
2032 return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS); 2034 return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
2033} 2035}
2034 2036
2035static void 2037static void
2036sysctl_net_inet_arp_setup(struct sysctllog **clog) 2038sysctl_net_inet_arp_setup(struct sysctllog **clog)
2037{ 2039{
2038 const struct sysctlnode *node; 2040 const struct sysctlnode *node;
2039 2041
2040 sysctl_createv(clog, 0, NULL, NULL, 2042 sysctl_createv(clog, 0, NULL, NULL,
2041 CTLFLAG_PERMANENT, 2043 CTLFLAG_PERMANENT,
2042 CTLTYPE_NODE, "inet", NULL, 2044 CTLTYPE_NODE, "inet", NULL,
2043 NULL, 0, NULL, 0, 2045 NULL, 0, NULL, 0,
2044 CTL_NET, PF_INET, CTL_EOL); 2046 CTL_NET, PF_INET, CTL_EOL);
2045 sysctl_createv(clog, 0, NULL, &node, 2047 sysctl_createv(clog, 0, NULL, &node,
2046 CTLFLAG_PERMANENT, 2048 CTLFLAG_PERMANENT,
2047 CTLTYPE_NODE, "arp", 2049 CTLTYPE_NODE, "arp",
2048 SYSCTL_DESCR("Address Resolution Protocol"), 2050 SYSCTL_DESCR("Address Resolution Protocol"),
2049 NULL, 0, NULL, 0, 2051 NULL, 0, NULL, 0,
2050 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL); 2052 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
2051 2053
2052 sysctl_createv(clog, 0, NULL, NULL, 2054 sysctl_createv(clog, 0, NULL, NULL,
2053 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2055 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2054 CTLTYPE_INT, "keep", 2056 CTLTYPE_INT, "keep",
2055 SYSCTL_DESCR("Valid ARP entry lifetime in seconds"), 2057 SYSCTL_DESCR("Valid ARP entry lifetime in seconds"),
2056 NULL, 0, &arpt_keep, 0, 2058 NULL, 0, &arpt_keep, 0,
2057 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2059 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2058 2060
2059 sysctl_createv(clog, 0, NULL, NULL, 2061 sysctl_createv(clog, 0, NULL, NULL,
2060 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2062 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2061 CTLTYPE_INT, "down", 2063 CTLTYPE_INT, "down",
2062 SYSCTL_DESCR("Failed ARP entry lifetime in seconds"), 2064 SYSCTL_DESCR("Failed ARP entry lifetime in seconds"),
2063 NULL, 0, &arpt_down, 0, 2065 NULL, 0, &arpt_down, 0,
2064 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2066 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2065 2067
2066 sysctl_createv(clog, 0, NULL, NULL, 2068 sysctl_createv(clog, 0, NULL, NULL,
2067 CTLFLAG_PERMANENT, 2069 CTLFLAG_PERMANENT,
2068 CTLTYPE_STRUCT, "stats", 2070 CTLTYPE_STRUCT, "stats",
2069 SYSCTL_DESCR("ARP statistics"), 2071 SYSCTL_DESCR("ARP statistics"),
2070 sysctl_net_inet_arp_stats, 0, NULL, 0, 2072 sysctl_net_inet_arp_stats, 0, NULL, 0,
2071 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2073 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2072 2074
2073 sysctl_createv(clog, 0, NULL, NULL, 2075 sysctl_createv(clog, 0, NULL, NULL,
2074 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2076 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2075 CTLTYPE_INT, "log_movements", 2077 CTLTYPE_INT, "log_movements",
2076 SYSCTL_DESCR("log ARP replies from MACs different than" 2078 SYSCTL_DESCR("log ARP replies from MACs different than"
2077 " the one in the cache"), 2079 " the one in the cache"),
2078 NULL, 0, &log_movements, 0, 2080 NULL, 0, &log_movements, 0,
2079 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2081 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2080 2082
2081 sysctl_createv(clog, 0, NULL, NULL, 2083 sysctl_createv(clog, 0, NULL, NULL,
2082 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2084 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2083 CTLTYPE_INT, "log_permanent_modify", 2085 CTLTYPE_INT, "log_permanent_modify",
2084 SYSCTL_DESCR("log ARP replies from MACs different than" 2086 SYSCTL_DESCR("log ARP replies from MACs different than"
2085 " the one in the permanent arp entry"), 2087 " the one in the permanent arp entry"),
2086 NULL, 0, &log_permanent_modify, 0, 2088 NULL, 0, &log_permanent_modify, 0,
2087 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2089 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2088 2090
2089 sysctl_createv(clog, 0, NULL, NULL, 2091 sysctl_createv(clog, 0, NULL, NULL,
2090 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2092 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2091 CTLTYPE_INT, "log_wrong_iface", 2093 CTLTYPE_INT, "log_wrong_iface",
2092 SYSCTL_DESCR("log ARP packets arriving on the wrong" 2094 SYSCTL_DESCR("log ARP packets arriving on the wrong"
2093 " interface"), 2095 " interface"),
2094 NULL, 0, &log_wrong_iface, 0, 2096 NULL, 0, &log_wrong_iface, 0,
2095 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2097 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2096 2098
2097 sysctl_createv(clog, 0, NULL, NULL, 2099 sysctl_createv(clog, 0, NULL, NULL,
2098 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2100 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2099 CTLTYPE_INT, "debug", 2101 CTLTYPE_INT, "debug",
2100 SYSCTL_DESCR("Enable ARP DAD debug output"), 2102 SYSCTL_DESCR("Enable ARP DAD debug output"),
2101 NULL, 0, &arp_debug, 0, 2103 NULL, 0, &arp_debug, 0,
2102 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2104 CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2103} 2105}
2104 2106
2105#endif /* INET */ 2107#endif /* INET */