Fri Aug 30 18:52:00 2019 UTC ()
ARP: change default sysctl entry log_movements to 0

IP address sharing is a thing and shouldn't cause needless diagnostics
by default.


(roy)
diff -r1.285 -r1.286 src/sys/netinet/if_arp.c

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

--- src/sys/netinet/if_arp.c 2019/08/30 18:48:34 1.285
+++ src/sys/netinet/if_arp.c 2019/08/30 18:52:00 1.286
@@ -1,1211 +1,1211 @@ @@ -1,1211 +1,1211 @@
1/* $NetBSD: if_arp.c,v 1.285 2019/08/30 18:48:34 roy Exp $ */ 1/* $NetBSD: if_arp.c,v 1.286 2019/08/30 18:52:00 roy Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under 8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * Copyright (c) 1982, 1986, 1988, 1993 34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved. 35 * The Regents of the University of California. All rights reserved.
36 * 36 *
37 * Redistribution and use in source and binary forms, with or without 37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions 38 * modification, are permitted provided that the following conditions
39 * are met: 39 * are met:
40 * 1. Redistributions of source code must retain the above copyright 40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer. 41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright 42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the 43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution. 44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors 45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software 46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission. 47 * without specific prior written permission.
48 * 48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE. 59 * SUCH DAMAGE.
60 * 60 *
61 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94 61 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94
62 */ 62 */
63 63
64/* 64/*
65 * Ethernet address resolution protocol. 65 * Ethernet address resolution protocol.
66 * TODO: 66 * TODO:
67 * add "inuse/lock" bit (or ref. count) along with valid bit 67 * add "inuse/lock" bit (or ref. count) along with valid bit
68 */ 68 */
69 69
70#include <sys/cdefs.h> 70#include <sys/cdefs.h>
71__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.285 2019/08/30 18:48:34 roy Exp $"); 71__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.286 2019/08/30 18:52:00 roy Exp $");
72 72
73#ifdef _KERNEL_OPT 73#ifdef _KERNEL_OPT
74#include "opt_ddb.h" 74#include "opt_ddb.h"
75#include "opt_inet.h" 75#include "opt_inet.h"
76#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);
156 156
157static void arprequest(struct ifnet *, 157static void arprequest(struct ifnet *,
158 const struct in_addr *, const struct in_addr *, 158 const struct in_addr *, const struct in_addr *,
159 const uint8_t *); 159 const uint8_t *);
160static void arpannounce1(struct ifaddr *); 160static void arpannounce1(struct ifaddr *);
161static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *, 161static struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *,
162 const struct sockaddr *); 162 const struct sockaddr *);
163static void arptimer(void *); 163static void arptimer(void *);
164static void arp_settimer(struct llentry *, int); 164static void arp_settimer(struct llentry *, int);
165static struct llentry *arplookup(struct ifnet *, 165static struct llentry *arplookup(struct ifnet *,
166 const struct in_addr *, const struct sockaddr *, int); 166 const struct in_addr *, const struct sockaddr *, int);
167static struct llentry *arpcreate(struct ifnet *, 167static struct llentry *arpcreate(struct ifnet *,
168 const struct in_addr *, const struct sockaddr *, int); 168 const struct in_addr *, const struct sockaddr *, int);
169static void in_arpinput(struct mbuf *); 169static void in_arpinput(struct mbuf *);
170static void in_revarpinput(struct mbuf *); 170static void in_revarpinput(struct mbuf *);
171static void revarprequest(struct ifnet *); 171static void revarprequest(struct ifnet *);
172 172
173static void arp_drainstub(void); 173static void arp_drainstub(void);
174 174
175struct dadq; 175struct dadq;
176static void arp_dad_timer(struct dadq *); 176static void arp_dad_timer(struct dadq *);
177static void arp_dad_start(struct ifaddr *); 177static void arp_dad_start(struct ifaddr *);
178static void arp_dad_stop(struct ifaddr *); 178static void arp_dad_stop(struct ifaddr *);
179static void arp_dad_duplicated(struct ifaddr *, const struct sockaddr_dl *); 179static void arp_dad_duplicated(struct ifaddr *, const struct sockaddr_dl *);
180 180
181static void arp_init_llentry(struct ifnet *, struct llentry *); 181static void arp_init_llentry(struct ifnet *, struct llentry *);
182#if NTOKEN > 0 182#if NTOKEN > 0
183static void arp_free_llentry_tokenring(struct llentry *); 183static void arp_free_llentry_tokenring(struct llentry *);
184#endif 184#endif
185 185
186struct ifqueue arpintrq = { 186struct ifqueue arpintrq = {
187 .ifq_head = NULL, 187 .ifq_head = NULL,
188 .ifq_tail = NULL, 188 .ifq_tail = NULL,
189 .ifq_len = 0, 189 .ifq_len = 0,
190 .ifq_maxlen = 50, 190 .ifq_maxlen = 50,
191 .ifq_drops = 0, 191 .ifq_drops = 0,
192}; 192};
193static int arp_maxtries = 5; 193static int arp_maxtries = 5;
194static int useloopback = 1; /* use loopback interface for local traffic */ 194static int useloopback = 1; /* use loopback interface for local traffic */
195 195
196static percpu_t *arpstat_percpu; 196static percpu_t *arpstat_percpu;
197 197
198#define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu) 198#define ARP_STAT_GETREF() _NET_STAT_GETREF(arpstat_percpu)
199#define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu) 199#define ARP_STAT_PUTREF() _NET_STAT_PUTREF(arpstat_percpu)
200 200
201#define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x) 201#define ARP_STATINC(x) _NET_STATINC(arpstat_percpu, x)
202#define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v) 202#define ARP_STATADD(x, v) _NET_STATADD(arpstat_percpu, x, v)
203 203
204/* revarp state */ 204/* revarp state */
205static struct in_addr myip, srv_ip; 205static struct in_addr myip, srv_ip;
206static int myip_initialized = 0; 206static int myip_initialized = 0;
207static int revarp_in_progress = 0; 207static int revarp_in_progress = 0;
208static struct ifnet *myip_ifp = NULL; 208static struct ifnet *myip_ifp = NULL;
209 209
210static int arp_drainwanted; 210static int arp_drainwanted;
211 211
212static int log_movements = 1; 212static int log_movements = 0;
213static int log_permanent_modify = 1; 213static int log_permanent_modify = 1;
214static int log_wrong_iface = 1; 214static int log_wrong_iface = 1;
215 215
216DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */ 216DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */
217 217
218static void 218static void
219arp_fasttimo(void) 219arp_fasttimo(void)
220{ 220{
221 if (arp_drainwanted) { 221 if (arp_drainwanted) {
222 arp_drain(); 222 arp_drain();
223 arp_drainwanted = 0; 223 arp_drainwanted = 0;
224 } 224 }
225} 225}
226 226
227static const struct protosw arpsw[] = { 227static const struct protosw arpsw[] = {
228 { 228 {
229 .pr_type = 0, 229 .pr_type = 0,
230 .pr_domain = &arpdomain, 230 .pr_domain = &arpdomain,
231 .pr_protocol = 0, 231 .pr_protocol = 0,
232 .pr_flags = 0, 232 .pr_flags = 0,
233 .pr_input = 0, 233 .pr_input = 0,
234 .pr_ctlinput = 0, 234 .pr_ctlinput = 0,
235 .pr_ctloutput = 0, 235 .pr_ctloutput = 0,
236 .pr_usrreqs = 0, 236 .pr_usrreqs = 0,
237 .pr_init = arp_init, 237 .pr_init = arp_init,
238 .pr_fasttimo = arp_fasttimo, 238 .pr_fasttimo = arp_fasttimo,
239 .pr_slowtimo = 0, 239 .pr_slowtimo = 0,
240 .pr_drain = arp_drainstub, 240 .pr_drain = arp_drainstub,
241 } 241 }
242}; 242};
243 243
244struct domain arpdomain = { 244struct domain arpdomain = {
245 .dom_family = PF_ARP, 245 .dom_family = PF_ARP,
246 .dom_name = "arp", 246 .dom_name = "arp",
247 .dom_protosw = arpsw, 247 .dom_protosw = arpsw,
248 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)], 248 .dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)],
249#ifdef MBUFTRACE 249#ifdef MBUFTRACE
250 .dom_mowner = MOWNER_INIT("internet", "arp"), 250 .dom_mowner = MOWNER_INIT("internet", "arp"),
251#endif 251#endif
252}; 252};
253 253
254static void sysctl_net_inet_arp_setup(struct sysctllog **); 254static void sysctl_net_inet_arp_setup(struct sysctllog **);
255 255
256void 256void
257arp_init(void) 257arp_init(void)
258{ 258{
259 259
260 sysctl_net_inet_arp_setup(NULL); 260 sysctl_net_inet_arp_setup(NULL);
261 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS); 261 arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS);
262 IFQ_LOCK_INIT(&arpintrq); 262 IFQ_LOCK_INIT(&arpintrq);
263 263
264#ifdef MBUFTRACE 264#ifdef MBUFTRACE
265 MOWNER_ATTACH(&arpdomain.dom_mowner); 265 MOWNER_ATTACH(&arpdomain.dom_mowner);
266#endif 266#endif
267} 267}
268 268
269static void 269static void
270arp_drainstub(void) 270arp_drainstub(void)
271{ 271{
272 arp_drainwanted = 1; 272 arp_drainwanted = 1;
273} 273}
274 274
275/* 275/*
276 * ARP protocol drain routine. Called when memory is in short supply. 276 * ARP protocol drain routine. Called when memory is in short supply.
277 * Called at splvm(); don't acquire softnet_lock as can be called from 277 * Called at splvm(); don't acquire softnet_lock as can be called from
278 * hardware interrupt handlers. 278 * hardware interrupt handlers.
279 */ 279 */
280void 280void
281arp_drain(void) 281arp_drain(void)
282{ 282{
283 283
284 lltable_drain(AF_INET); 284 lltable_drain(AF_INET);
285} 285}
286 286
287static void 287static void
288arptimer(void *arg) 288arptimer(void *arg)
289{ 289{
290 struct llentry *lle = arg; 290 struct llentry *lle = arg;
291 struct ifnet *ifp; 291 struct ifnet *ifp;
292 292
293 KASSERT((lle->la_flags & LLE_STATIC) == 0); 293 KASSERT((lle->la_flags & LLE_STATIC) == 0);
294 294
295 LLE_WLOCK(lle); 295 LLE_WLOCK(lle);
296 296
297 /* 297 /*
298 * This shortcut is required to avoid trying to touch ifp that may be 298 * This shortcut is required to avoid trying to touch ifp that may be
299 * being destroyed. 299 * being destroyed.
300 */ 300 */
301 if ((lle->la_flags & LLE_LINKED) == 0) { 301 if ((lle->la_flags & LLE_LINKED) == 0) {
302 LLE_FREE_LOCKED(lle); 302 LLE_FREE_LOCKED(lle);
303 return; 303 return;
304 } 304 }
305 305
306 ifp = lle->lle_tbl->llt_ifp; 306 ifp = lle->lle_tbl->llt_ifp;
307 307
308 /* XXX: LOR avoidance. We still have ref on lle. */ 308 /* XXX: LOR avoidance. We still have ref on lle. */
309 LLE_WUNLOCK(lle); 309 LLE_WUNLOCK(lle);
310 310
311 IF_AFDATA_LOCK(ifp); 311 IF_AFDATA_LOCK(ifp);
312 LLE_WLOCK(lle); 312 LLE_WLOCK(lle);
313 313
314 /* Guard against race with other llentry_free(). */ 314 /* Guard against race with other llentry_free(). */
315 if (lle->la_flags & LLE_LINKED) { 315 if (lle->la_flags & LLE_LINKED) {
316 size_t pkts_dropped; 316 size_t pkts_dropped;
317 317
318 LLE_REMREF(lle); 318 LLE_REMREF(lle);
319 pkts_dropped = llentry_free(lle); 319 pkts_dropped = llentry_free(lle);
320 ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped); 320 ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped);
321 ARP_STATADD(ARP_STAT_DFRTOTAL, pkts_dropped); 321 ARP_STATADD(ARP_STAT_DFRTOTAL, pkts_dropped);
322 } else { 322 } else {
323 LLE_FREE_LOCKED(lle); 323 LLE_FREE_LOCKED(lle);
324 } 324 }
325 325
326 IF_AFDATA_UNLOCK(ifp); 326 IF_AFDATA_UNLOCK(ifp);
327} 327}
328 328
329static void 329static void
330arp_settimer(struct llentry *la, int sec) 330arp_settimer(struct llentry *la, int sec)
331{ 331{
332 332
333 LLE_WLOCK_ASSERT(la); 333 LLE_WLOCK_ASSERT(la);
334 KASSERT((la->la_flags & LLE_STATIC) == 0); 334 KASSERT((la->la_flags & LLE_STATIC) == 0);
335 335
336 /* 336 /*
337 * We have to take care of a reference leak which occurs if 337 * We have to take care of a reference leak which occurs if
338 * callout_reset overwrites a pending callout schedule. Unfortunately 338 * callout_reset overwrites a pending callout schedule. Unfortunately
339 * we don't have a mean to know the overwrite, so we need to know it 339 * we don't have a mean to know the overwrite, so we need to know it
340 * using callout_stop. We need to call callout_pending first to exclude 340 * using callout_stop. We need to call callout_pending first to exclude
341 * the case that the callout has never been scheduled. 341 * the case that the callout has never been scheduled.
342 */ 342 */
343 if (callout_pending(&la->la_timer)) { 343 if (callout_pending(&la->la_timer)) {
344 bool expired = callout_stop(&la->la_timer); 344 bool expired = callout_stop(&la->la_timer);
345 if (!expired) 345 if (!expired)
346 /* A pending callout schedule is canceled. */ 346 /* A pending callout schedule is canceled. */
347 LLE_REMREF(la); 347 LLE_REMREF(la);
348 } 348 }
349 LLE_ADDREF(la); 349 LLE_ADDREF(la);
350 callout_reset(&la->la_timer, hz * sec, arptimer, la); 350 callout_reset(&la->la_timer, hz * sec, arptimer, la);
351} 351}
352 352
353/* 353/*
354 * We set the gateway for RTF_CLONING routes to a "prototype" 354 * We set the gateway for RTF_CLONING routes to a "prototype"
355 * link-layer sockaddr whose interface type (if_type) and interface 355 * link-layer sockaddr whose interface type (if_type) and interface
356 * index (if_index) fields are prepared. 356 * index (if_index) fields are prepared.
357 */ 357 */
358static struct sockaddr * 358static struct sockaddr *
359arp_setgate(struct rtentry *rt, struct sockaddr *gate, 359arp_setgate(struct rtentry *rt, struct sockaddr *gate,
360 const struct sockaddr *netmask) 360 const struct sockaddr *netmask)
361{ 361{
362 const struct ifnet *ifp = rt->rt_ifp; 362 const struct ifnet *ifp = rt->rt_ifp;
363 uint8_t namelen = strlen(ifp->if_xname); 363 uint8_t namelen = strlen(ifp->if_xname);
364 uint8_t addrlen = ifp->if_addrlen; 364 uint8_t addrlen = ifp->if_addrlen;
365 365
366 /* 366 /*
367 * XXX: If this is a manually added route to interface 367 * XXX: If this is a manually added route to interface
368 * such as older version of routed or gated might provide, 368 * such as older version of routed or gated might provide,
369 * restore cloning bit. 369 * restore cloning bit.
370 */ 370 */
371 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL && 371 if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL &&
372 satocsin(netmask)->sin_addr.s_addr != 0xffffffff) 372 satocsin(netmask)->sin_addr.s_addr != 0xffffffff)
373 rt->rt_flags |= RTF_CONNECTED; 373 rt->rt_flags |= RTF_CONNECTED;
374 374
375 if ((rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL))) { 375 if ((rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL))) {
376 union { 376 union {
377 struct sockaddr sa; 377 struct sockaddr sa;
378 struct sockaddr_storage ss; 378 struct sockaddr_storage ss;
379 struct sockaddr_dl sdl; 379 struct sockaddr_dl sdl;
380 } u; 380 } u;
381 /* 381 /*
382 * Case 1: This route should come from a route to iface. 382 * Case 1: This route should come from a route to iface.
383 */ 383 */
384 sockaddr_dl_init(&u.sdl, sizeof(u.ss), 384 sockaddr_dl_init(&u.sdl, sizeof(u.ss),
385 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen); 385 ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen);
386 rt_setgate(rt, &u.sa); 386 rt_setgate(rt, &u.sa);
387 gate = rt->rt_gateway; 387 gate = rt->rt_gateway;
388 } 388 }
389 return gate; 389 return gate;
390} 390}
391 391
392static void 392static void
393arp_init_llentry(struct ifnet *ifp, struct llentry *lle) 393arp_init_llentry(struct ifnet *ifp, struct llentry *lle)
394{ 394{
395 395
396 switch (ifp->if_type) { 396 switch (ifp->if_type) {
397#if NTOKEN > 0 397#if NTOKEN > 0
398 case IFT_ISO88025: 398 case IFT_ISO88025:
399 lle->la_opaque = kmem_intr_alloc(sizeof(struct token_rif), 399 lle->la_opaque = kmem_intr_alloc(sizeof(struct token_rif),
400 KM_NOSLEEP); 400 KM_NOSLEEP);
401 lle->lle_ll_free = arp_free_llentry_tokenring; 401 lle->lle_ll_free = arp_free_llentry_tokenring;
402 break; 402 break;
403#endif 403#endif
404 } 404 }
405} 405}
406 406
407#if NTOKEN > 0 407#if NTOKEN > 0
408static void 408static void
409arp_free_llentry_tokenring(struct llentry *lle) 409arp_free_llentry_tokenring(struct llentry *lle)
410{ 410{
411 411
412 kmem_intr_free(lle->la_opaque, sizeof(struct token_rif)); 412 kmem_intr_free(lle->la_opaque, sizeof(struct token_rif));
413} 413}
414#endif 414#endif
415 415
416/* 416/*
417 * Parallel to llc_rtrequest. 417 * Parallel to llc_rtrequest.
418 */ 418 */
419void 419void
420arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 420arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
421{ 421{
422 struct sockaddr *gate = rt->rt_gateway; 422 struct sockaddr *gate = rt->rt_gateway;
423 struct in_ifaddr *ia; 423 struct in_ifaddr *ia;
424 struct ifaddr *ifa; 424 struct ifaddr *ifa;
425 struct ifnet *ifp = rt->rt_ifp; 425 struct ifnet *ifp = rt->rt_ifp;
426 int bound; 426 int bound;
427 int s; 427 int s;
428 428
429 if (req == RTM_LLINFO_UPD) { 429 if (req == RTM_LLINFO_UPD) {
430 if ((ifa = info->rti_ifa) != NULL) 430 if ((ifa = info->rti_ifa) != NULL)
431 arpannounce1(ifa); 431 arpannounce1(ifa);
432 return; 432 return;
433 } 433 }
434 434
435 if ((rt->rt_flags & RTF_GATEWAY) != 0) { 435 if ((rt->rt_flags & RTF_GATEWAY) != 0) {
436 if (req != RTM_ADD) 436 if (req != RTM_ADD)
437 return; 437 return;
438 438
439 /* 439 /*
440 * linklayers with particular link MTU limitation. 440 * linklayers with particular link MTU limitation.
441 */ 441 */
442 switch(ifp->if_type) { 442 switch(ifp->if_type) {
443#if NFDDI > 0 443#if NFDDI > 0
444 case IFT_FDDI: 444 case IFT_FDDI:
445 if (ifp->if_mtu > FDDIIPMTU) 445 if (ifp->if_mtu > FDDIIPMTU)
446 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 446 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
447 break; 447 break;
448#endif 448#endif
449#if NARCNET > 0 449#if NARCNET > 0
450 case IFT_ARCNET: 450 case IFT_ARCNET:
451 { 451 {
452 int arcipifmtu; 452 int arcipifmtu;
453 453
454 if (ifp->if_flags & IFF_LINK0) 454 if (ifp->if_flags & IFF_LINK0)
455 arcipifmtu = arc_ipmtu; 455 arcipifmtu = arc_ipmtu;
456 else 456 else
457 arcipifmtu = ARCMTU; 457 arcipifmtu = ARCMTU;
458 if (ifp->if_mtu > arcipifmtu) 458 if (ifp->if_mtu > arcipifmtu)
459 rt->rt_rmx.rmx_mtu = arcipifmtu; 459 rt->rt_rmx.rmx_mtu = arcipifmtu;
460 break; 460 break;
461 } 461 }
462#endif 462#endif
463 } 463 }
464 return; 464 return;
465 } 465 }
466 466
467 switch (req) { 467 switch (req) {
468 case RTM_SETGATE: 468 case RTM_SETGATE:
469 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 469 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
470 break; 470 break;
471 case RTM_ADD: 471 case RTM_ADD:
472 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]); 472 gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
473 if (gate == NULL) { 473 if (gate == NULL) {
474 log(LOG_ERR, "%s: arp_setgate failed\n", __func__); 474 log(LOG_ERR, "%s: arp_setgate failed\n", __func__);
475 break; 475 break;
476 } 476 }
477 if ((rt->rt_flags & RTF_CONNECTED) || 477 if ((rt->rt_flags & RTF_CONNECTED) ||
478 (rt->rt_flags & RTF_LOCAL)) { 478 (rt->rt_flags & RTF_LOCAL)) {
479 /* 479 /*
480 * Give this route an expiration time, even though 480 * Give this route an expiration time, even though
481 * it's a "permanent" route, so that routes cloned 481 * it's a "permanent" route, so that routes cloned
482 * from it do not need their expiration time set. 482 * from it do not need their expiration time set.
483 */ 483 */
484 KASSERT(time_uptime != 0); 484 KASSERT(time_uptime != 0);
485 rt->rt_expire = time_uptime; 485 rt->rt_expire = time_uptime;
486 /* 486 /*
487 * linklayers with particular link MTU limitation. 487 * linklayers with particular link MTU limitation.
488 */ 488 */
489 switch (ifp->if_type) { 489 switch (ifp->if_type) {
490#if NFDDI > 0 490#if NFDDI > 0
491 case IFT_FDDI: 491 case IFT_FDDI:
492 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 492 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
493 (rt->rt_rmx.rmx_mtu > FDDIIPMTU || 493 (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
494 (rt->rt_rmx.rmx_mtu == 0 && 494 (rt->rt_rmx.rmx_mtu == 0 &&
495 ifp->if_mtu > FDDIIPMTU))) 495 ifp->if_mtu > FDDIIPMTU)))
496 rt->rt_rmx.rmx_mtu = FDDIIPMTU; 496 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
497 break; 497 break;
498#endif 498#endif
499#if NARCNET > 0 499#if NARCNET > 0
500 case IFT_ARCNET: 500 case IFT_ARCNET:
501 { 501 {
502 int arcipifmtu; 502 int arcipifmtu;
503 if (ifp->if_flags & IFF_LINK0) 503 if (ifp->if_flags & IFF_LINK0)
504 arcipifmtu = arc_ipmtu; 504 arcipifmtu = arc_ipmtu;
505 else 505 else
506 arcipifmtu = ARCMTU; 506 arcipifmtu = ARCMTU;
507 507
508 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 && 508 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
509 (rt->rt_rmx.rmx_mtu > arcipifmtu || 509 (rt->rt_rmx.rmx_mtu > arcipifmtu ||
510 (rt->rt_rmx.rmx_mtu == 0 && 510 (rt->rt_rmx.rmx_mtu == 0 &&
511 ifp->if_mtu > arcipifmtu))) 511 ifp->if_mtu > arcipifmtu)))
512 rt->rt_rmx.rmx_mtu = arcipifmtu; 512 rt->rt_rmx.rmx_mtu = arcipifmtu;
513 break; 513 break;
514 } 514 }
515#endif 515#endif
516 } 516 }
517 if (rt->rt_flags & RTF_CONNECTED) 517 if (rt->rt_flags & RTF_CONNECTED)
518 break; 518 break;
519 } 519 }
520 520
521 bound = curlwp_bind(); 521 bound = curlwp_bind();
522 /* Announce a new entry if requested. */ 522 /* Announce a new entry if requested. */
523 if (rt->rt_flags & RTF_ANNOUNCE) { 523 if (rt->rt_flags & RTF_ANNOUNCE) {
524 struct psref psref; 524 struct psref psref;
525 ia = in_get_ia_on_iface_psref( 525 ia = in_get_ia_on_iface_psref(
526 satocsin(rt_getkey(rt))->sin_addr, ifp, &psref); 526 satocsin(rt_getkey(rt))->sin_addr, ifp, &psref);
527 if (ia != NULL) { 527 if (ia != NULL) {
528 arpannounce(ifp, &ia->ia_ifa, 528 arpannounce(ifp, &ia->ia_ifa,
529 CLLADDR(satocsdl(gate))); 529 CLLADDR(satocsdl(gate)));
530 ia4_release(ia, &psref); 530 ia4_release(ia, &psref);
531 } 531 }
532 } 532 }
533 533
534 if (gate->sa_family != AF_LINK || 534 if (gate->sa_family != AF_LINK ||
535 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) { 535 gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) {
536 log(LOG_DEBUG, "%s: bad gateway value\n", __func__); 536 log(LOG_DEBUG, "%s: bad gateway value\n", __func__);
537 goto out; 537 goto out;
538 } 538 }
539 539
540 satosdl(gate)->sdl_type = ifp->if_type; 540 satosdl(gate)->sdl_type = ifp->if_type;
541 satosdl(gate)->sdl_index = ifp->if_index; 541 satosdl(gate)->sdl_index = ifp->if_index;
542 542
543 /* 543 /*
544 * If the route is for a broadcast address mark it as such. 544 * If the route is for a broadcast address mark it as such.
545 * This way we can avoid an expensive call to in_broadcast() 545 * This way we can avoid an expensive call to in_broadcast()
546 * in ip_output() most of the time (because the route passed 546 * in ip_output() most of the time (because the route passed
547 * to ip_output() is almost always a host route). 547 * to ip_output() is almost always a host route).
548 */ 548 */
549 if (rt->rt_flags & RTF_HOST && 549 if (rt->rt_flags & RTF_HOST &&
550 !(rt->rt_flags & RTF_BROADCAST) && 550 !(rt->rt_flags & RTF_BROADCAST) &&
551 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp)) 551 in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp))
552 rt->rt_flags |= RTF_BROADCAST; 552 rt->rt_flags |= RTF_BROADCAST;
553 /* There is little point in resolving the broadcast address */ 553 /* There is little point in resolving the broadcast address */
554 if (rt->rt_flags & RTF_BROADCAST) 554 if (rt->rt_flags & RTF_BROADCAST)
555 goto out; 555 goto out;
556 556
557 /* 557 /*
558 * When called from rt_ifa_addlocal, we cannot depend on that 558 * When called from rt_ifa_addlocal, we cannot depend on that
559 * the address (rt_getkey(rt)) exits in the address list of the 559 * the address (rt_getkey(rt)) exits in the address list of the
560 * interface. So check RTF_LOCAL instead. 560 * interface. So check RTF_LOCAL instead.
561 */ 561 */
562 if (rt->rt_flags & RTF_LOCAL) { 562 if (rt->rt_flags & RTF_LOCAL) {
563 rt->rt_expire = 0; 563 rt->rt_expire = 0;
564 if (useloopback) { 564 if (useloopback) {
565 rt->rt_ifp = lo0ifp; 565 rt->rt_ifp = lo0ifp;
566 rt->rt_rmx.rmx_mtu = 0; 566 rt->rt_rmx.rmx_mtu = 0;
567 } 567 }
568 goto out; 568 goto out;
569 } 569 }
570 570
571 s = pserialize_read_enter(); 571 s = pserialize_read_enter();
572 ia = in_get_ia_on_iface(satocsin(rt_getkey(rt))->sin_addr, ifp); 572 ia = in_get_ia_on_iface(satocsin(rt_getkey(rt))->sin_addr, ifp);
573 if (ia == NULL) { 573 if (ia == NULL) {
574 pserialize_read_exit(s); 574 pserialize_read_exit(s);
575 goto out; 575 goto out;
576 } 576 }
577 577
578 rt->rt_expire = 0; 578 rt->rt_expire = 0;
579 if (useloopback) { 579 if (useloopback) {
580 rt->rt_ifp = lo0ifp; 580 rt->rt_ifp = lo0ifp;
581 rt->rt_rmx.rmx_mtu = 0; 581 rt->rt_rmx.rmx_mtu = 0;
582 } 582 }
583 rt->rt_flags |= RTF_LOCAL; 583 rt->rt_flags |= RTF_LOCAL;
584 584
585 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) { 585 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) {
586 pserialize_read_exit(s); 586 pserialize_read_exit(s);
587 goto out; 587 goto out;
588 } 588 }
589 /* 589 /*
590 * make sure to set rt->rt_ifa to the interface 590 * make sure to set rt->rt_ifa to the interface
591 * address we are using, otherwise we will have trouble 591 * address we are using, otherwise we will have trouble
592 * with source address selection. 592 * with source address selection.
593 */ 593 */
594 ifa = &ia->ia_ifa; 594 ifa = &ia->ia_ifa;
595 if (ifa != rt->rt_ifa) 595 if (ifa != rt->rt_ifa)
596 /* Assume it doesn't sleep */ 596 /* Assume it doesn't sleep */
597 rt_replace_ifa(rt, ifa); 597 rt_replace_ifa(rt, ifa);
598 pserialize_read_exit(s); 598 pserialize_read_exit(s);
599 out: 599 out:
600 curlwp_bindx(bound); 600 curlwp_bindx(bound);
601 break; 601 break;
602 } 602 }
603} 603}
604 604
605/* 605/*
606 * Broadcast an ARP request. Caller specifies: 606 * Broadcast an ARP request. Caller specifies:
607 * - arp header source ip address 607 * - arp header source ip address
608 * - arp header target ip address 608 * - arp header target ip address
609 * - arp header source ethernet address 609 * - arp header source ethernet address
610 */ 610 */
611static void 611static void
612arprequest(struct ifnet *ifp, 612arprequest(struct ifnet *ifp,
613 const struct in_addr *sip, const struct in_addr *tip, 613 const struct in_addr *sip, const struct in_addr *tip,
614 const uint8_t *enaddr) 614 const uint8_t *enaddr)
615{ 615{
616 struct mbuf *m; 616 struct mbuf *m;
617 struct arphdr *ah; 617 struct arphdr *ah;
618 struct sockaddr sa; 618 struct sockaddr sa;
619 uint64_t *arps; 619 uint64_t *arps;
620 620
621 KASSERT(sip != NULL); 621 KASSERT(sip != NULL);
622 KASSERT(tip != NULL); 622 KASSERT(tip != NULL);
623 KASSERT(enaddr != NULL); 623 KASSERT(enaddr != NULL);
624 624
625 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 625 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
626 return; 626 return;
627 MCLAIM(m, &arpdomain.dom_mowner); 627 MCLAIM(m, &arpdomain.dom_mowner);
628 switch (ifp->if_type) { 628 switch (ifp->if_type) {
629 case IFT_IEEE1394: 629 case IFT_IEEE1394:
630 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 630 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
631 ifp->if_addrlen; 631 ifp->if_addrlen;
632 break; 632 break;
633 default: 633 default:
634 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 634 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
635 2 * ifp->if_addrlen; 635 2 * ifp->if_addrlen;
636 break; 636 break;
637 } 637 }
638 m->m_pkthdr.len = m->m_len; 638 m->m_pkthdr.len = m->m_len;
639 m_align(m, m->m_len); 639 m_align(m, m->m_len);
640 ah = mtod(m, struct arphdr *); 640 ah = mtod(m, struct arphdr *);
641 memset(ah, 0, m->m_len); 641 memset(ah, 0, m->m_len);
642 switch (ifp->if_type) { 642 switch (ifp->if_type) {
643 case IFT_IEEE1394: /* RFC2734 */ 643 case IFT_IEEE1394: /* RFC2734 */
644 /* fill it now for ar_tpa computation */ 644 /* fill it now for ar_tpa computation */
645 ah->ar_hrd = htons(ARPHRD_IEEE1394); 645 ah->ar_hrd = htons(ARPHRD_IEEE1394);
646 break; 646 break;
647 default: 647 default:
648 /* ifp->if_output will fill ar_hrd */ 648 /* ifp->if_output will fill ar_hrd */
649 break; 649 break;
650 } 650 }
651 ah->ar_pro = htons(ETHERTYPE_IP); 651 ah->ar_pro = htons(ETHERTYPE_IP);
652 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 652 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
653 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 653 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
654 ah->ar_op = htons(ARPOP_REQUEST); 654 ah->ar_op = htons(ARPOP_REQUEST);
655 memcpy(ar_sha(ah), enaddr, ah->ar_hln); 655 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
656 memcpy(ar_spa(ah), sip, ah->ar_pln); 656 memcpy(ar_spa(ah), sip, ah->ar_pln);
657 memcpy(ar_tpa(ah), tip, ah->ar_pln); 657 memcpy(ar_tpa(ah), tip, ah->ar_pln);
658 sa.sa_family = AF_ARP; 658 sa.sa_family = AF_ARP;
659 sa.sa_len = 2; 659 sa.sa_len = 2;
660 m->m_flags |= M_BCAST; 660 m->m_flags |= M_BCAST;
661 arps = ARP_STAT_GETREF(); 661 arps = ARP_STAT_GETREF();
662 arps[ARP_STAT_SNDTOTAL]++; 662 arps[ARP_STAT_SNDTOTAL]++;
663 arps[ARP_STAT_SENDREQUEST]++; 663 arps[ARP_STAT_SENDREQUEST]++;
664 ARP_STAT_PUTREF(); 664 ARP_STAT_PUTREF();
665 if_output_lock(ifp, ifp, m, &sa, NULL); 665 if_output_lock(ifp, ifp, m, &sa, NULL);
666} 666}
667 667
668void 668void
669arpannounce(struct ifnet *ifp, struct ifaddr *ifa, const uint8_t *enaddr) 669arpannounce(struct ifnet *ifp, struct ifaddr *ifa, const uint8_t *enaddr)
670{ 670{
671 struct in_ifaddr *ia = ifatoia(ifa); 671 struct in_ifaddr *ia = ifatoia(ifa);
672 struct in_addr *ip = &IA_SIN(ifa)->sin_addr; 672 struct in_addr *ip = &IA_SIN(ifa)->sin_addr;
673 673
674 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) { 674 if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) {
675 ARPLOG(LOG_DEBUG, "%s not ready\n", ARPLOGADDR(ip)); 675 ARPLOG(LOG_DEBUG, "%s not ready\n", ARPLOGADDR(ip));
676 return; 676 return;
677 } 677 }
678 arprequest(ifp, ip, ip, enaddr); 678 arprequest(ifp, ip, ip, enaddr);
679} 679}
680 680
681static void 681static void
682arpannounce1(struct ifaddr *ifa) 682arpannounce1(struct ifaddr *ifa)
683{ 683{
684 684
685 arpannounce(ifa->ifa_ifp, ifa, CLLADDR(ifa->ifa_ifp->if_sadl)); 685 arpannounce(ifa->ifa_ifp, ifa, CLLADDR(ifa->ifa_ifp->if_sadl));
686} 686}
687 687
688/* 688/*
689 * Resolve an IP address into an ethernet address. If success, desten is 689 * Resolve an IP address into an ethernet address. If success, desten is
690 * filled in. If there is no entry in arptab, set one up and broadcast a 690 * filled in. If there is no entry in arptab, set one up and broadcast a
691 * request for the IP address. Hold onto this mbuf and resend it once the 691 * request for the IP address. Hold onto this mbuf and resend it once the
692 * address is finally resolved. 692 * address is finally resolved.
693 * 693 *
694 * A return value of 0 indicates that desten has been filled in and the packet 694 * A return value of 0 indicates that desten has been filled in and the packet
695 * should be sent normally; a return value of EWOULDBLOCK indicates that the 695 * should be sent normally; a return value of EWOULDBLOCK indicates that the
696 * packet has been held pending resolution. Any other value indicates an 696 * packet has been held pending resolution. Any other value indicates an
697 * error. 697 * error.
698 */ 698 */
699int 699int
700arpresolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m, 700arpresolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
701 const struct sockaddr *dst, void *desten, size_t destlen) 701 const struct sockaddr *dst, void *desten, size_t destlen)
702{ 702{
703 struct llentry *la; 703 struct llentry *la;
704 const char *create_lookup; 704 const char *create_lookup;
705 bool renew; 705 bool renew;
706 int error; 706 int error;
707 struct ifnet *origifp = ifp; 707 struct ifnet *origifp = ifp;
708 708
709#if NCARP > 0 709#if NCARP > 0
710 if (rt != NULL && rt->rt_ifp->if_type == IFT_CARP) 710 if (rt != NULL && rt->rt_ifp->if_type == IFT_CARP)
711 ifp = rt->rt_ifp; 711 ifp = rt->rt_ifp;
712#endif 712#endif
713 713
714 KASSERT(m != NULL); 714 KASSERT(m != NULL);
715 715
716 la = arplookup(ifp, NULL, dst, 0); 716 la = arplookup(ifp, NULL, dst, 0);
717 if (la == NULL) 717 if (la == NULL)
718 goto notfound; 718 goto notfound;
719 719
720 if ((la->la_flags & LLE_VALID) && 720 if ((la->la_flags & LLE_VALID) &&
721 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 721 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
722 KASSERT(destlen >= ifp->if_addrlen); 722 KASSERT(destlen >= ifp->if_addrlen);
723 memcpy(desten, &la->ll_addr, ifp->if_addrlen); 723 memcpy(desten, &la->ll_addr, ifp->if_addrlen);
724 LLE_RUNLOCK(la); 724 LLE_RUNLOCK(la);
725 return 0; 725 return 0;
726 } 726 }
727 727
728notfound: 728notfound:
729 if (ifp->if_flags & IFF_NOARP) { 729 if (ifp->if_flags & IFF_NOARP) {
730 if (la != NULL) 730 if (la != NULL)
731 LLE_RUNLOCK(la); 731 LLE_RUNLOCK(la);
732 error = ENOTSUP; 732 error = ENOTSUP;
733 goto bad; 733 goto bad;
734 } 734 }
735 735
736 if (la == NULL) { 736 if (la == NULL) {
737 struct rtentry *_rt; 737 struct rtentry *_rt;
738 738
739 create_lookup = "create"; 739 create_lookup = "create";
740 _rt = rtalloc1(dst, 0); 740 _rt = rtalloc1(dst, 0);
741 IF_AFDATA_WLOCK(ifp); 741 IF_AFDATA_WLOCK(ifp);
742 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst, _rt); 742 la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst, _rt);
743 IF_AFDATA_WUNLOCK(ifp); 743 IF_AFDATA_WUNLOCK(ifp);
744 if (_rt != NULL) 744 if (_rt != NULL)
745 rt_unref(_rt); 745 rt_unref(_rt);
746 if (la == NULL) 746 if (la == NULL)
747 ARP_STATINC(ARP_STAT_ALLOCFAIL); 747 ARP_STATINC(ARP_STAT_ALLOCFAIL);
748 else { 748 else {
749 struct sockaddr_in sin; 749 struct sockaddr_in sin;
750 750
751 arp_init_llentry(ifp, la); 751 arp_init_llentry(ifp, la);
752 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 752 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
753 if (rt != NULL) 753 if (rt != NULL)
754 rt_clonedmsg(RTM_ADD, sintosa(&sin), NULL, ifp); 754 rt_clonedmsg(RTM_ADD, sintosa(&sin), NULL, ifp);
755 } 755 }
756 } else if (LLE_TRY_UPGRADE(la) == 0) { 756 } else if (LLE_TRY_UPGRADE(la) == 0) {
757 create_lookup = "lookup"; 757 create_lookup = "lookup";
758 LLE_RUNLOCK(la); 758 LLE_RUNLOCK(la);
759 IF_AFDATA_RLOCK(ifp); 759 IF_AFDATA_RLOCK(ifp);
760 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst); 760 la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
761 IF_AFDATA_RUNLOCK(ifp); 761 IF_AFDATA_RUNLOCK(ifp);
762 } 762 }
763 763
764 error = EINVAL; 764 error = EINVAL;
765 if (la == NULL) { 765 if (la == NULL) {
766 log(LOG_DEBUG, 766 log(LOG_DEBUG,
767 "%s: failed to %s llentry for %s on %s\n", 767 "%s: failed to %s llentry for %s on %s\n",
768 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr), 768 __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr),
769 ifp->if_xname); 769 ifp->if_xname);
770 goto bad; 770 goto bad;
771 } 771 }
772 772
773 if ((la->la_flags & LLE_VALID) && 773 if ((la->la_flags & LLE_VALID) &&
774 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) 774 ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime))
775 { 775 {
776 KASSERT(destlen >= ifp->if_addrlen); 776 KASSERT(destlen >= ifp->if_addrlen);
777 memcpy(desten, &la->ll_addr, ifp->if_addrlen); 777 memcpy(desten, &la->ll_addr, ifp->if_addrlen);
778 renew = false; 778 renew = false;
779 /* 779 /*
780 * If entry has an expiry time and it is approaching, 780 * If entry has an expiry time and it is approaching,
781 * see if we need to send an ARP request within this 781 * see if we need to send an ARP request within this
782 * arpt_down interval. 782 * arpt_down interval.
783 */ 783 */
784 if (!(la->la_flags & LLE_STATIC) && 784 if (!(la->la_flags & LLE_STATIC) &&
785 time_uptime + la->la_preempt > la->la_expire) 785 time_uptime + la->la_preempt > la->la_expire)
786 { 786 {
787 renew = true; 787 renew = true;
788 la->la_preempt--; 788 la->la_preempt--;
789 } 789 }
790 790
791 LLE_WUNLOCK(la); 791 LLE_WUNLOCK(la);
792 792
793 if (renew) { 793 if (renew) {
794 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 794 const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
795 arprequest(origifp, 795 arprequest(origifp,
796 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 796 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
797 &satocsin(dst)->sin_addr, enaddr); 797 &satocsin(dst)->sin_addr, enaddr);
798 } 798 }
799 799
800 return 0; 800 return 0;
801 } 801 }
802 802
803 if (la->la_flags & LLE_STATIC) { /* should not happen! */ 803 if (la->la_flags & LLE_STATIC) { /* should not happen! */
804 LLE_RUNLOCK(la); 804 LLE_RUNLOCK(la);
805 log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n", 805 log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n",
806 __func__, inet_ntoa(satocsin(dst)->sin_addr)); 806 __func__, inet_ntoa(satocsin(dst)->sin_addr));
807 error = EINVAL; 807 error = EINVAL;
808 goto bad; 808 goto bad;
809 } 809 }
810 810
811 renew = (la->la_asked == 0 || la->la_expire != time_uptime); 811 renew = (la->la_asked == 0 || la->la_expire != time_uptime);
812 812
813 /* 813 /*
814 * There is an arptab entry, but no ethernet address 814 * There is an arptab entry, but no ethernet address
815 * response yet. Add the mbuf to the list, dropping 815 * response yet. Add the mbuf to the list, dropping
816 * the oldest packet if we have exceeded the system 816 * the oldest packet if we have exceeded the system
817 * setting. 817 * setting.
818 */ 818 */
819 LLE_WLOCK_ASSERT(la); 819 LLE_WLOCK_ASSERT(la);
820 if (la->la_numheld >= arp_maxhold) { 820 if (la->la_numheld >= arp_maxhold) {
821 if (la->la_hold != NULL) { 821 if (la->la_hold != NULL) {
822 struct mbuf *next = la->la_hold->m_nextpkt; 822 struct mbuf *next = la->la_hold->m_nextpkt;
823 m_freem(la->la_hold); 823 m_freem(la->la_hold);
824 la->la_hold = next; 824 la->la_hold = next;
825 la->la_numheld--; 825 la->la_numheld--;
826 ARP_STATINC(ARP_STAT_DFRDROPPED); 826 ARP_STATINC(ARP_STAT_DFRDROPPED);
827 ARP_STATINC(ARP_STAT_DFRTOTAL); 827 ARP_STATINC(ARP_STAT_DFRTOTAL);
828 } 828 }
829 } 829 }
830 if (la->la_hold != NULL) { 830 if (la->la_hold != NULL) {
831 struct mbuf *curr = la->la_hold; 831 struct mbuf *curr = la->la_hold;
832 while (curr->m_nextpkt != NULL) 832 while (curr->m_nextpkt != NULL)
833 curr = curr->m_nextpkt; 833 curr = curr->m_nextpkt;
834 curr->m_nextpkt = m; 834 curr->m_nextpkt = m;
835 } else 835 } else
836 la->la_hold = m; 836 la->la_hold = m;
837 la->la_numheld++; 837 la->la_numheld++;
838 if (!renew) 838 if (!renew)
839 LLE_DOWNGRADE(la); 839 LLE_DOWNGRADE(la);
840 840
841 /* 841 /*
842 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 842 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
843 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 843 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
844 * if we have already sent arp_maxtries ARP requests. Retransmit the 844 * if we have already sent arp_maxtries ARP requests. Retransmit the
845 * ARP request, but not faster than one request per second. 845 * ARP request, but not faster than one request per second.
846 */ 846 */
847 if (la->la_asked < arp_maxtries) 847 if (la->la_asked < arp_maxtries)
848 error = EWOULDBLOCK; /* First request. */ 848 error = EWOULDBLOCK; /* First request. */
849 else 849 else
850 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ? 850 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
851 EHOSTUNREACH : EHOSTDOWN; 851 EHOSTUNREACH : EHOSTDOWN;
852 852
853 if (renew) { 853 if (renew) {
854 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 854 const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
855 la->la_expire = time_uptime; 855 la->la_expire = time_uptime;
856 arp_settimer(la, arpt_down); 856 arp_settimer(la, arpt_down);
857 la->la_asked++; 857 la->la_asked++;
858 LLE_WUNLOCK(la); 858 LLE_WUNLOCK(la);
859 859
860 if (rt != NULL) { 860 if (rt != NULL) {
861 arprequest(origifp, 861 arprequest(origifp,
862 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr, 862 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
863 &satocsin(dst)->sin_addr, enaddr); 863 &satocsin(dst)->sin_addr, enaddr);
864 } else { 864 } else {
865 struct sockaddr_in sin; 865 struct sockaddr_in sin;
866 struct rtentry *_rt; 866 struct rtentry *_rt;
867 867
868 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0); 868 sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
869 869
870 /* XXX */ 870 /* XXX */
871 _rt = rtalloc1((struct sockaddr *)&sin, 0); 871 _rt = rtalloc1((struct sockaddr *)&sin, 0);
872 if (_rt == NULL) 872 if (_rt == NULL)
873 goto bad; 873 goto bad;
874 arprequest(origifp, 874 arprequest(origifp,
875 &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr, 875 &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr,
876 &satocsin(dst)->sin_addr, enaddr); 876 &satocsin(dst)->sin_addr, enaddr);
877 rt_unref(_rt); 877 rt_unref(_rt);
878 } 878 }
879 return error; 879 return error;
880 } 880 }
881 881
882 LLE_RUNLOCK(la); 882 LLE_RUNLOCK(la);
883 return error; 883 return error;
884 884
885bad: 885bad:
886 m_freem(m); 886 m_freem(m);
887 return error; 887 return error;
888} 888}
889 889
890/* 890/*
891 * Common length and type checks are done here, 891 * Common length and type checks are done here,
892 * then the protocol-specific routine is called. 892 * then the protocol-specific routine is called.
893 */ 893 */
894void 894void
895arpintr(void) 895arpintr(void)
896{ 896{
897 struct mbuf *m; 897 struct mbuf *m;
898 struct arphdr *ar; 898 struct arphdr *ar;
899 int s; 899 int s;
900 int arplen; 900 int arplen;
901 901
902 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE(); 902 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
903 for (;;) { 903 for (;;) {
904 struct ifnet *rcvif; 904 struct ifnet *rcvif;
905 905
906 IFQ_LOCK(&arpintrq); 906 IFQ_LOCK(&arpintrq);
907 IF_DEQUEUE(&arpintrq, m); 907 IF_DEQUEUE(&arpintrq, m);
908 IFQ_UNLOCK(&arpintrq); 908 IFQ_UNLOCK(&arpintrq);
909 if (m == NULL) 909 if (m == NULL)
910 goto out; 910 goto out;
911 if ((m->m_flags & M_PKTHDR) == 0) 911 if ((m->m_flags & M_PKTHDR) == 0)
912 panic("arpintr"); 912 panic("arpintr");
913 913
914 MCLAIM(m, &arpdomain.dom_mowner); 914 MCLAIM(m, &arpdomain.dom_mowner);
915 ARP_STATINC(ARP_STAT_RCVTOTAL); 915 ARP_STATINC(ARP_STAT_RCVTOTAL);
916 916
917 arplen = sizeof(struct arphdr); 917 arplen = sizeof(struct arphdr);
918 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 918 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
919 goto badlen; 919 goto badlen;
920 ar = mtod(m, struct arphdr *); 920 ar = mtod(m, struct arphdr *);
921 921
922 rcvif = m_get_rcvif(m, &s); 922 rcvif = m_get_rcvif(m, &s);
923 if (__predict_false(rcvif == NULL)) { 923 if (__predict_false(rcvif == NULL)) {
924 ARP_STATINC(ARP_STAT_RCVNOINT); 924 ARP_STATINC(ARP_STAT_RCVNOINT);
925 goto free; 925 goto free;
926 } 926 }
927 927
928 /* 928 /*
929 * We don't want non-IEEE1394 ARP packets on IEEE1394 929 * We don't want non-IEEE1394 ARP packets on IEEE1394
930 * interfaces, and vice versa. Our life depends on that. 930 * interfaces, and vice versa. Our life depends on that.
931 */ 931 */
932 switch (rcvif->if_type) { 932 switch (rcvif->if_type) {
933 case IFT_IEEE1394: 933 case IFT_IEEE1394:
934 if (ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 934 if (ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
935 m_put_rcvif(rcvif, &s); 935 m_put_rcvif(rcvif, &s);
936 ARP_STATINC(ARP_STAT_RCVBADPROTO); 936 ARP_STATINC(ARP_STAT_RCVBADPROTO);
937 goto free; 937 goto free;
938 } 938 }
939 939
940 arplen = sizeof(struct arphdr) + 940 arplen = sizeof(struct arphdr) +
941 ar->ar_hln + 2 * ar->ar_pln; 941 ar->ar_hln + 2 * ar->ar_pln;
942 break; 942 break;
943 default: 943 default:
944 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) { 944 if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) {
945 m_put_rcvif(rcvif, &s); 945 m_put_rcvif(rcvif, &s);
946 ARP_STATINC(ARP_STAT_RCVBADPROTO); 946 ARP_STATINC(ARP_STAT_RCVBADPROTO);
947 goto free; 947 goto free;
948 } 948 }
949 949
950 arplen = sizeof(struct arphdr) + 950 arplen = sizeof(struct arphdr) +
951 2 * ar->ar_hln + 2 * ar->ar_pln; 951 2 * ar->ar_hln + 2 * ar->ar_pln;
952 break; 952 break;
953 } 953 }
954 954
955 m_put_rcvif(rcvif, &s); 955 m_put_rcvif(rcvif, &s);
956 956
957 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL) 957 if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
958 goto badlen; 958 goto badlen;
959 ar = mtod(m, struct arphdr *); 959 ar = mtod(m, struct arphdr *);
960 960
961 switch (ntohs(ar->ar_pro)) { 961 switch (ntohs(ar->ar_pro)) {
962 case ETHERTYPE_IP: 962 case ETHERTYPE_IP:
963 case ETHERTYPE_IPTRAILERS: 963 case ETHERTYPE_IPTRAILERS:
964 in_arpinput(m); 964 in_arpinput(m);
965 continue; 965 continue;
966 default: 966 default:
967 ARP_STATINC(ARP_STAT_RCVBADPROTO); 967 ARP_STATINC(ARP_STAT_RCVBADPROTO);
968 goto free; 968 goto free;
969 } 969 }
970 970
971badlen: 971badlen:
972 ARP_STATINC(ARP_STAT_RCVBADLEN); 972 ARP_STATINC(ARP_STAT_RCVBADLEN);
973free: 973free:
974 m_freem(m); 974 m_freem(m);
975 } 975 }
976 976
977out: 977out:
978 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 978 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
979 return; /* XXX gcc */ 979 return; /* XXX gcc */
980} 980}
981 981
982/* 982/*
983 * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in 983 * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in
984 * RFC 826. In addition, a sanity check is performed on the sender protocol 984 * RFC 826. In addition, a sanity check is performed on the sender protocol
985 * address, to catch impersonators. 985 * address, to catch impersonators.
986 * 986 *
987 * We no longer handle negotiations for use of trailer protocol: formerly, ARP 987 * We no longer handle negotiations for use of trailer protocol: formerly, ARP
988 * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we 988 * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we
989 * wanted trailers sent to us, and also sent them in response to IP replies. 989 * wanted trailers sent to us, and also sent them in response to IP replies.
990 * This allowed either end to announce the desire to receive trailer packets. 990 * This allowed either end to announce the desire to receive trailer packets.
991 * 991 *
992 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but 992 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but
993 * formerly didn't normally send requests. 993 * formerly didn't normally send requests.
994 */ 994 */
995static void 995static void
996in_arpinput(struct mbuf *m) 996in_arpinput(struct mbuf *m)
997{ 997{
998 struct arphdr *ah; 998 struct arphdr *ah;
999 struct ifnet *ifp, *rcvif = NULL; 999 struct ifnet *ifp, *rcvif = NULL;
1000 struct llentry *la = NULL; 1000 struct llentry *la = NULL;
1001 struct in_ifaddr *ia = NULL; 1001 struct in_ifaddr *ia = NULL;
1002#if NBRIDGE > 0 1002#if NBRIDGE > 0
1003 struct in_ifaddr *bridge_ia = NULL; 1003 struct in_ifaddr *bridge_ia = NULL;
1004#endif 1004#endif
1005#if NCARP > 0 1005#if NCARP > 0
1006 uint32_t count = 0, index = 0; 1006 uint32_t count = 0, index = 0;
1007#endif 1007#endif
1008 struct sockaddr sa; 1008 struct sockaddr sa;
1009 struct in_addr isaddr, itaddr, myaddr; 1009 struct in_addr isaddr, itaddr, myaddr;
1010 int op; 1010 int op;
1011 void *tha; 1011 void *tha;
1012 uint64_t *arps; 1012 uint64_t *arps;
1013 struct psref psref, psref_ia; 1013 struct psref psref, psref_ia;
1014 int s; 1014 int s;
1015 char ipbuf[INET_ADDRSTRLEN]; 1015 char ipbuf[INET_ADDRSTRLEN];
1016 bool do_dad; 1016 bool do_dad;
1017 1017
1018 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT))) 1018 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
1019 goto out; 1019 goto out;
1020 ah = mtod(m, struct arphdr *); 1020 ah = mtod(m, struct arphdr *);
1021 op = ntohs(ah->ar_op); 1021 op = ntohs(ah->ar_op);
1022 1022
1023 if (ah->ar_pln != sizeof(struct in_addr)) 1023 if (ah->ar_pln != sizeof(struct in_addr))
1024 goto out; 1024 goto out;
1025 1025
1026 ifp = if_get_bylla(ar_sha(ah), ah->ar_hln, &psref); 1026 ifp = if_get_bylla(ar_sha(ah), ah->ar_hln, &psref);
1027 if (ifp) { 1027 if (ifp) {
1028 /* it's from me, ignore it. */ 1028 /* it's from me, ignore it. */
1029 if_put(ifp, &psref); 1029 if_put(ifp, &psref);
1030 ARP_STATINC(ARP_STAT_RCVLOCALSHA); 1030 ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1031 goto out; 1031 goto out;
1032 } 1032 }
1033 1033
1034 rcvif = ifp = m_get_rcvif_psref(m, &psref); 1034 rcvif = ifp = m_get_rcvif_psref(m, &psref);
1035 if (__predict_false(rcvif == NULL)) 1035 if (__predict_false(rcvif == NULL))
1036 goto out; 1036 goto out;
1037 if (rcvif->if_flags & IFF_NOARP) 1037 if (rcvif->if_flags & IFF_NOARP)
1038 goto out; 1038 goto out;
1039 1039
1040 memcpy(&isaddr, ar_spa(ah), sizeof(isaddr)); 1040 memcpy(&isaddr, ar_spa(ah), sizeof(isaddr));
1041 memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr)); 1041 memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr));
1042 1042
1043 if (m->m_flags & (M_BCAST|M_MCAST)) 1043 if (m->m_flags & (M_BCAST|M_MCAST))
1044 ARP_STATINC(ARP_STAT_RCVMCAST); 1044 ARP_STATINC(ARP_STAT_RCVMCAST);
1045 1045
1046 /* 1046 /*
1047 * Search for a matching interface address 1047 * Search for a matching interface address
1048 * or any address on the interface to use 1048 * or any address on the interface to use
1049 * as a dummy address in the rest of this function. 1049 * as a dummy address in the rest of this function.
1050 * 1050 *
1051 * If the target IP address is zero then try and find 1051 * If the target IP address is zero then try and find
1052 * the sender address for DAD. 1052 * the sender address for DAD.
1053 */ 1053 */
1054 myaddr = in_nullhost(itaddr) ? isaddr : itaddr; 1054 myaddr = in_nullhost(itaddr) ? isaddr : itaddr;
1055 s = pserialize_read_enter(); 1055 s = pserialize_read_enter();
1056 IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) { 1056 IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) {
1057 if (!in_hosteq(ia->ia_addr.sin_addr, myaddr)) 1057 if (!in_hosteq(ia->ia_addr.sin_addr, myaddr))
1058 continue; 1058 continue;
1059#if NCARP > 0 1059#if NCARP > 0
1060 if (ia->ia_ifp->if_type == IFT_CARP && 1060 if (ia->ia_ifp->if_type == IFT_CARP &&
1061 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) == 1061 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1062 (IFF_UP|IFF_RUNNING))) { 1062 (IFF_UP|IFF_RUNNING))) {
1063 index++; 1063 index++;
1064 /* XXX: ar_hln? */ 1064 /* XXX: ar_hln? */
1065 if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) && 1065 if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) &&
1066 carp_iamatch(ia, ar_sha(ah), 1066 carp_iamatch(ia, ar_sha(ah),
1067 &count, index)) { 1067 &count, index)) {
1068 break; 1068 break;
1069 } 1069 }
1070 } else 1070 } else
1071#endif 1071#endif
1072 if (ia->ia_ifp == rcvif) 1072 if (ia->ia_ifp == rcvif)
1073 break; 1073 break;
1074#if NBRIDGE > 0 1074#if NBRIDGE > 0
1075 /* 1075 /*
1076 * If the interface we received the packet on 1076 * If the interface we received the packet on
1077 * is part of a bridge, check to see if we need 1077 * is part of a bridge, check to see if we need
1078 * to "bridge" the packet to ourselves at this 1078 * to "bridge" the packet to ourselves at this
1079 * layer. Note we still prefer a perfect match, 1079 * layer. Note we still prefer a perfect match,
1080 * but allow this weaker match if necessary. 1080 * but allow this weaker match if necessary.
1081 */ 1081 */
1082 if (rcvif->if_bridge != NULL && 1082 if (rcvif->if_bridge != NULL &&
1083 rcvif->if_bridge == ia->ia_ifp->if_bridge) 1083 rcvif->if_bridge == ia->ia_ifp->if_bridge)
1084 bridge_ia = ia; 1084 bridge_ia = ia;
1085#endif 1085#endif
1086 } 1086 }
1087 1087
1088#if NBRIDGE > 0 1088#if NBRIDGE > 0
1089 if (ia == NULL && bridge_ia != NULL) { 1089 if (ia == NULL && bridge_ia != NULL) {
1090 ia = bridge_ia; 1090 ia = bridge_ia;
1091 m_put_rcvif_psref(rcvif, &psref); 1091 m_put_rcvif_psref(rcvif, &psref);
1092 rcvif = NULL; 1092 rcvif = NULL;
1093 /* FIXME */ 1093 /* FIXME */
1094 ifp = bridge_ia->ia_ifp; 1094 ifp = bridge_ia->ia_ifp;
1095 } 1095 }
1096#endif 1096#endif
1097 if (ia != NULL) 1097 if (ia != NULL)
1098 ia4_acquire(ia, &psref_ia); 1098 ia4_acquire(ia, &psref_ia);
1099 pserialize_read_exit(s); 1099 pserialize_read_exit(s);
1100 1100
1101 if (ah->ar_hln != ifp->if_addrlen) { 1101 if (ah->ar_hln != ifp->if_addrlen) {
1102 ARP_STATINC(ARP_STAT_RCVBADLEN); 1102 ARP_STATINC(ARP_STAT_RCVBADLEN);
1103 log(LOG_WARNING, 1103 log(LOG_WARNING,
1104 "arp from %s: addr len: new %d, i/f %d (ignored)\n", 1104 "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1105 IN_PRINT(ipbuf, &isaddr), ah->ar_hln, ifp->if_addrlen); 1105 IN_PRINT(ipbuf, &isaddr), ah->ar_hln, ifp->if_addrlen);
1106 goto out; 1106 goto out;
1107 } 1107 }
1108 1108
1109 /* Only do DaD if we have a matching address. */ 1109 /* Only do DaD if we have a matching address. */
1110 do_dad = (ia != NULL); 1110 do_dad = (ia != NULL);
1111 1111
1112 if (ia == NULL) { 1112 if (ia == NULL) {
1113 ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia); 1113 ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia);
1114 if (ia == NULL) { 1114 if (ia == NULL) {
1115 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia); 1115 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
1116 if (ia == NULL) { 1116 if (ia == NULL) {
1117 ARP_STATINC(ARP_STAT_RCVNOINT); 1117 ARP_STATINC(ARP_STAT_RCVNOINT);
1118 goto out; 1118 goto out;
1119 } 1119 }
1120 } 1120 }
1121 } 1121 }
1122 1122
1123 myaddr = ia->ia_addr.sin_addr; 1123 myaddr = ia->ia_addr.sin_addr;
1124 1124
1125 /* XXX checks for bridge case? */ 1125 /* XXX checks for bridge case? */
1126 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 1126 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1127 ARP_STATINC(ARP_STAT_RCVBCASTSHA); 1127 ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1128 log(LOG_ERR, 1128 log(LOG_ERR,
1129 "%s: arp: link address is broadcast for IP address %s!\n", 1129 "%s: arp: link address is broadcast for IP address %s!\n",
1130 ifp->if_xname, IN_PRINT(ipbuf, &isaddr)); 1130 ifp->if_xname, IN_PRINT(ipbuf, &isaddr));
1131 goto out; 1131 goto out;
1132 } 1132 }
1133 1133
1134 /* 1134 /*
1135 * If the source IP address is zero, this is an RFC 5227 ARP probe 1135 * If the source IP address is zero, this is an RFC 5227 ARP probe
1136 */ 1136 */
1137 if (in_nullhost(isaddr)) 1137 if (in_nullhost(isaddr))
1138 ARP_STATINC(ARP_STAT_RCVZEROSPA); 1138 ARP_STATINC(ARP_STAT_RCVZEROSPA);
1139 else if (in_hosteq(isaddr, myaddr)) 1139 else if (in_hosteq(isaddr, myaddr))
1140 ARP_STATINC(ARP_STAT_RCVLOCALSPA); 1140 ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1141 1141
1142 if (in_nullhost(itaddr)) 1142 if (in_nullhost(itaddr))
1143 ARP_STATINC(ARP_STAT_RCVZEROTPA); 1143 ARP_STATINC(ARP_STAT_RCVZEROTPA);
1144 1144
1145 /* 1145 /*
1146 * DAD check, RFC 5227. 1146 * DAD check, RFC 5227.
1147 * Collision on sender address is always a duplicate. 1147 * Collision on sender address is always a duplicate.
1148 * Collision on target address is only a duplicate IF 1148 * Collision on target address is only a duplicate IF
1149 * the sender address is the null host (ie a DAD probe) AND 1149 * the sender address is the null host (ie a DAD probe) AND
1150 * the message was broadcast - if it's unicast then it's 1150 * the message was broadcast - if it's unicast then it's
1151 * a valid Unicast Poll from RFC 1122. 1151 * a valid Unicast Poll from RFC 1122.
1152 */ 1152 */
1153 if (do_dad && 1153 if (do_dad &&
1154 (in_hosteq(isaddr, myaddr) || 1154 (in_hosteq(isaddr, myaddr) ||
1155 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr) && 1155 (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr) &&
1156 m->m_flags & M_BCAST))) 1156 m->m_flags & M_BCAST)))
1157 { 1157 {
1158 struct sockaddr_dl sdl, *sdlp; 1158 struct sockaddr_dl sdl, *sdlp;
1159 1159
1160 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl), 1160 sdlp = sockaddr_dl_init(&sdl, sizeof(sdl),
1161 ifp->if_index, ifp->if_type, 1161 ifp->if_index, ifp->if_type,
1162 NULL, 0, ar_sha(ah), ah->ar_hln); 1162 NULL, 0, ar_sha(ah), ah->ar_hln);
1163 arp_dad_duplicated((struct ifaddr *)ia, sdlp); 1163 arp_dad_duplicated((struct ifaddr *)ia, sdlp);
1164 goto out; 1164 goto out;
1165 } 1165 }
1166 1166
1167 /* 1167 /*
1168 * If the target IP address is zero, ignore the packet. 1168 * If the target IP address is zero, ignore the packet.
1169 * This prevents the code below from trying to answer 1169 * This prevents the code below from trying to answer
1170 * when we are using IP address zero (booting). 1170 * when we are using IP address zero (booting).
1171 */ 1171 */
1172 if (in_nullhost(itaddr)) 1172 if (in_nullhost(itaddr))
1173 goto out; 1173 goto out;
1174 1174
1175 if (in_nullhost(isaddr)) 1175 if (in_nullhost(isaddr))
1176 goto reply; 1176 goto reply;
1177 1177
1178 if (in_hosteq(itaddr, myaddr)) 1178 if (in_hosteq(itaddr, myaddr))
1179 la = arpcreate(ifp, &isaddr, NULL, 1); 1179 la = arpcreate(ifp, &isaddr, NULL, 1);
1180 else 1180 else
1181 la = arplookup(ifp, &isaddr, NULL, 1); 1181 la = arplookup(ifp, &isaddr, NULL, 1);
1182 if (la == NULL) 1182 if (la == NULL)
1183 goto reply; 1183 goto reply;
1184 1184
1185 if ((la->la_flags & LLE_VALID) && 1185 if ((la->la_flags & LLE_VALID) &&
1186 memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) 1186 memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen))
1187 { 1187 {
1188 char llabuf[LLA_ADDRSTRLEN], *llastr; 1188 char llabuf[LLA_ADDRSTRLEN], *llastr;
1189 1189
1190 llastr = lla_snprintf(llabuf, sizeof(llabuf), 1190 llastr = lla_snprintf(llabuf, sizeof(llabuf),
1191 ar_sha(ah), ah->ar_hln); 1191 ar_sha(ah), ah->ar_hln);
1192 1192
1193 if (la->la_flags & LLE_STATIC) { 1193 if (la->la_flags & LLE_STATIC) {
1194 ARP_STATINC(ARP_STAT_RCVOVERPERM); 1194 ARP_STATINC(ARP_STAT_RCVOVERPERM);
1195 if (!log_permanent_modify) 1195 if (!log_permanent_modify)
1196 goto out; 1196 goto out;
1197 log(LOG_INFO, 1197 log(LOG_INFO,
1198 "%s tried to overwrite permanent arp info" 1198 "%s tried to overwrite permanent arp info"
1199 " for %s\n", llastr, IN_PRINT(ipbuf, &isaddr)); 1199 " for %s\n", llastr, IN_PRINT(ipbuf, &isaddr));
1200 goto out; 1200 goto out;
1201 } else if (la->lle_tbl->llt_ifp != ifp) { 1201 } else if (la->lle_tbl->llt_ifp != ifp) {
1202 /* XXX should not happen? */ 1202 /* XXX should not happen? */
1203 ARP_STATINC(ARP_STAT_RCVOVERINT); 1203 ARP_STATINC(ARP_STAT_RCVOVERINT);
1204 if (!log_wrong_iface) 1204 if (!log_wrong_iface)
1205 goto out; 1205 goto out;
1206 log(LOG_INFO, 1206 log(LOG_INFO,
1207 "%s on %s tried to overwrite " 1207 "%s on %s tried to overwrite "
1208 "arp info for %s on %s\n", 1208 "arp info for %s on %s\n",
1209 llastr, 1209 llastr,
1210 ifp->if_xname, IN_PRINT(ipbuf, &isaddr), 1210 ifp->if_xname, IN_PRINT(ipbuf, &isaddr),
1211 la->lle_tbl->llt_ifp->if_xname); 1211 la->lle_tbl->llt_ifp->if_xname);