| Commit | Line | Data |
|---|---|---|
| 984263bc | 1 | /* |
| f3ed2586 JH |
2 | * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved. |
| 3 | * | |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| 5 | * by Jeffrey M. Hsu. | |
| 6 | * | |
| 7 | * Redistribution and use in source and binary forms, with or without | |
| 8 | * modification, are permitted provided that the following conditions | |
| 9 | * are met: | |
| 10 | * 1. Redistributions of source code must retain the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer. | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 16 | * contributors may be used to endorse or promote products derived | |
| 17 | * from this software without specific, prior written permission. | |
| 18 | * | |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 22 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 23 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 25 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 29 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 30 | * SUCH DAMAGE. | |
| 31 | */ | |
| 32 | ||
| 33 | /* | |
| 984263bc MD |
34 | * Copyright (c) 1982, 1986, 1988, 1993 |
| 35 | * The Regents of the University of California. All rights reserved. | |
| 36 | * | |
| 37 | * Redistribution and use in source and binary forms, with or without | |
| 38 | * modification, are permitted provided that the following conditions | |
| 39 | * are met: | |
| 40 | * 1. Redistributions of source code must retain the above copyright | |
| 41 | * notice, this list of conditions and the following disclaimer. | |
| 42 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 43 | * notice, this list of conditions and the following disclaimer in the | |
| 44 | * documentation and/or other materials provided with the distribution. | |
| 45 | * 3. All advertising materials mentioning features or use of this software | |
| 46 | * must display the following acknowledgement: | |
| 47 | * This product includes software developed by the University of | |
| 48 | * California, Berkeley and its contributors. | |
| 49 | * 4. Neither the name of the University nor the names of its contributors | |
| 50 | * may be used to endorse or promote products derived from this software | |
| 51 | * without specific prior written permission. | |
| 52 | * | |
| 53 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 54 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 55 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 56 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 57 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 58 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 59 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 60 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 61 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 62 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 63 | * SUCH DAMAGE. | |
| 64 | * | |
| 65 | * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 | |
| 66 | * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $ | |
| b95665d8 | 67 | * $DragonFly: src/sys/netinet/if_ether.c,v 1.59 2008/11/22 11:03:35 sephe Exp $ |
| 984263bc MD |
68 | */ |
| 69 | ||
| 70 | /* | |
| 71 | * Ethernet address resolution protocol. | |
| 72 | * TODO: | |
| 73 | * add "inuse/lock" bit (or ref. count) along with valid bit | |
| 74 | */ | |
| 75 | ||
| 76 | #include "opt_inet.h" | |
| 0d16ba1d | 77 | #include "opt_carp.h" |
| 984263bc MD |
78 | |
| 79 | #include <sys/param.h> | |
| 80 | #include <sys/kernel.h> | |
| 81 | #include <sys/queue.h> | |
| 82 | #include <sys/sysctl.h> | |
| 83 | #include <sys/systm.h> | |
| 84 | #include <sys/mbuf.h> | |
| 85 | #include <sys/malloc.h> | |
| 86 | #include <sys/socket.h> | |
| 87 | #include <sys/syslog.h> | |
| 65fd4168 | 88 | #include <sys/lock.h> |
| 984263bc MD |
89 | |
| 90 | #include <net/if.h> | |
| 91 | #include <net/if_dl.h> | |
| 92 | #include <net/if_types.h> | |
| 93 | #include <net/route.h> | |
| 94 | #include <net/netisr.h> | |
| 95 | #include <net/if_llc.h> | |
| 984263bc MD |
96 | |
| 97 | #include <netinet/in.h> | |
| 98 | #include <netinet/in_var.h> | |
| 99 | #include <netinet/if_ether.h> | |
| 100 | ||
| 4599cf19 MD |
101 | #include <sys/thread2.h> |
| 102 | #include <sys/msgport2.h> | |
| 103 | #include <net/netmsg2.h> | |
| 684a93c4 | 104 | #include <sys/mplock2.h> |
| 4599cf19 | 105 | |
| 0d16ba1d MD |
106 | #ifdef CARP |
| 107 | #include <netinet/ip_carp.h> | |
| 108 | #endif | |
| 109 | ||
| 984263bc MD |
110 | #define SIN(s) ((struct sockaddr_in *)s) |
| 111 | #define SDL(s) ((struct sockaddr_dl *)s) | |
| 112 | ||
| 113 | SYSCTL_DECL(_net_link_ether); | |
| 114 | SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); | |
| 115 | ||
| 116 | /* timer values */ | |
| 117 | static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ | |
| 118 | static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ | |
| 119 | static int arpt_down = 20; /* once declared down, don't send for 20 sec */ | |
| 120 | ||
| 121 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, | |
| 122 | &arpt_prune, 0, ""); | |
| f23061d4 | 123 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, |
| 984263bc MD |
124 | &arpt_keep, 0, ""); |
| 125 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, | |
| 126 | &arpt_down, 0, ""); | |
| 127 | ||
| f23061d4 | 128 | #define rt_expire rt_rmx.rmx_expire |
| 984263bc MD |
129 | |
| 130 | struct llinfo_arp { | |
| 131 | LIST_ENTRY(llinfo_arp) la_le; | |
| 132 | struct rtentry *la_rt; | |
| 133 | struct mbuf *la_hold; /* last packet until resolved/timeout */ | |
| a6f79f3a | 134 | struct lwkt_port *la_msgport; /* last packet's msgport */ |
| a916e847 | 135 | u_short la_preempt; /* countdown for pre-expiry arps */ |
| 984263bc | 136 | u_short la_asked; /* #times we QUERIED following expiration */ |
| 984263bc MD |
137 | }; |
| 138 | ||
| ecdefdda | 139 | static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU]; |
| 984263bc | 140 | |
| 984263bc MD |
141 | static int arp_maxtries = 5; |
| 142 | static int useloopback = 1; /* use loopback interface for local traffic */ | |
| 143 | static int arp_proxyall = 0; | |
| 70d9a675 MD |
144 | static int arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */ |
| 145 | static int arp_restricted_match = 0; | |
| 984263bc MD |
146 | |
| 147 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, | |
| 19a2058e | 148 | &arp_maxtries, 0, "ARP resolution attempts before returning error"); |
| 984263bc | 149 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, |
| 19a2058e | 150 | &useloopback, 0, "Use the loopback interface for local traffic"); |
| 984263bc | 151 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, |
| 19a2058e | 152 | &arp_proxyall, 0, "Enable proxy ARP for all suitable requests"); |
| 70d9a675 MD |
153 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW, |
| 154 | &arp_restricted_match, 0, "Only match against the sender"); | |
| 155 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW, | |
| 156 | &arp_refresh, 0, "Preemptively refresh the ARP"); | |
| 984263bc | 157 | |
| 799975d0 | 158 | static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); |
| 1dfeb73c SZ |
159 | static void arprequest(struct ifnet *, const struct in_addr *, |
| 160 | const struct in_addr *, const u_char *); | |
| 161 | static void arprequest_async(struct ifnet *, const struct in_addr *, | |
| 162 | const struct in_addr *, const u_char *); | |
| 002c1265 | 163 | static void arpintr(netmsg_t msg); |
| 799975d0 SZ |
164 | static void arptfree(struct llinfo_arp *); |
| 165 | static void arptimer(void *); | |
| 166 | static struct llinfo_arp * | |
| 125186e0 | 167 | arplookup(in_addr_t, boolean_t, boolean_t, boolean_t); |
| 984263bc | 168 | #ifdef INET |
| 799975d0 | 169 | static void in_arpinput(struct mbuf *); |
| 984263bc MD |
170 | #endif |
| 171 | ||
| ecdefdda | 172 | static struct callout arptimer_ch[MAXCPU]; |
| ac311540 | 173 | |
| 984263bc MD |
174 | /* |
| 175 | * Timeout routine. Age arp_tab entries periodically. | |
| 176 | */ | |
| 177 | /* ARGSUSED */ | |
| 178 | static void | |
| f23061d4 | 179 | arptimer(void *ignored_arg) |
| 984263bc | 180 | { |
| 2e9572df | 181 | struct llinfo_arp *la, *nla; |
| 984263bc | 182 | |
| 1cae611f | 183 | crit_enter(); |
| ecdefdda | 184 | LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) { |
| 2e9572df | 185 | if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second) |
| 799975d0 | 186 | arptfree(la); |
| 984263bc | 187 | } |
| ecdefdda | 188 | callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL); |
| 1cae611f | 189 | crit_exit(); |
| 984263bc MD |
190 | } |
| 191 | ||
| 192 | /* | |
| 193 | * Parallel to llc_rtrequest. | |
| 194 | */ | |
| 195 | static void | |
| f23061d4 | 196 | arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) |
| 984263bc | 197 | { |
| 2256ba69 | 198 | struct sockaddr *gate = rt->rt_gateway; |
| f23061d4 JH |
199 | struct llinfo_arp *la = rt->rt_llinfo; |
| 200 | ||
| 5fe66e68 | 201 | struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK }; |
| ecdefdda | 202 | static boolean_t arpinit_done[MAXCPU]; |
| 984263bc | 203 | |
| ecdefdda MD |
204 | if (!arpinit_done[mycpuid]) { |
| 205 | arpinit_done[mycpuid] = TRUE; | |
| 206 | callout_init(&arptimer_ch[mycpuid]); | |
| 207 | callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL); | |
| 984263bc MD |
208 | } |
| 209 | if (rt->rt_flags & RTF_GATEWAY) | |
| 210 | return; | |
| 984263bc | 211 | |
| f23061d4 | 212 | switch (req) { |
| 984263bc MD |
213 | case RTM_ADD: |
| 214 | /* | |
| 215 | * XXX: If this is a manually added route to interface | |
| 216 | * such as older version of routed or gated might provide, | |
| 217 | * restore cloning bit. | |
| 218 | */ | |
| f23061d4 | 219 | if (!(rt->rt_flags & RTF_HOST) && |
| 984263bc MD |
220 | SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) |
| 221 | rt->rt_flags |= RTF_CLONING; | |
| 222 | if (rt->rt_flags & RTF_CLONING) { | |
| 223 | /* | |
| 224 | * Case 1: This route should come from a route to iface. | |
| 225 | */ | |
| 226 | rt_setgate(rt, rt_key(rt), | |
| afe44b68 SZ |
227 | (struct sockaddr *)&null_sdl, |
| 228 | RTL_DONTREPORT); | |
| 984263bc MD |
229 | gate = rt->rt_gateway; |
| 230 | SDL(gate)->sdl_type = rt->rt_ifp->if_type; | |
| 231 | SDL(gate)->sdl_index = rt->rt_ifp->if_index; | |
| 232 | rt->rt_expire = time_second; | |
| 233 | break; | |
| 234 | } | |
| 235 | /* Announce a new entry if requested. */ | |
| 9c94ab97 | 236 | if (rt->rt_flags & RTF_ANNOUNCE) { |
| 77e3dc77 | 237 | arprequest_async(rt->rt_ifp, |
| 984263bc MD |
238 | &SIN(rt_key(rt))->sin_addr, |
| 239 | &SIN(rt_key(rt))->sin_addr, | |
| f23061d4 | 240 | LLADDR(SDL(gate))); |
| 9c94ab97 | 241 | } |
| 984263bc MD |
242 | /*FALLTHROUGH*/ |
| 243 | case RTM_RESOLVE: | |
| 244 | if (gate->sa_family != AF_LINK || | |
| f23061d4 | 245 | gate->sa_len < sizeof(struct sockaddr_dl)) { |
| 984263bc MD |
246 | log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); |
| 247 | break; | |
| 248 | } | |
| 249 | SDL(gate)->sdl_type = rt->rt_ifp->if_type; | |
| 250 | SDL(gate)->sdl_index = rt->rt_ifp->if_index; | |
| f23061d4 | 251 | if (la != NULL) |
| 984263bc MD |
252 | break; /* This happens on a route change */ |
| 253 | /* | |
| 254 | * Case 2: This route may come from cloning, or a manual route | |
| 255 | * add with a LL address. | |
| 256 | */ | |
| 0c3c561c | 257 | R_Malloc(la, struct llinfo_arp *, sizeof *la); |
| f23061d4 JH |
258 | rt->rt_llinfo = la; |
| 259 | if (la == NULL) { | |
| 984263bc MD |
260 | log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); |
| 261 | break; | |
| 262 | } | |
| f23061d4 | 263 | bzero(la, sizeof *la); |
| 984263bc MD |
264 | la->la_rt = rt; |
| 265 | rt->rt_flags |= RTF_LLINFO; | |
| ecdefdda | 266 | LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le); |
| 984263bc MD |
267 | |
| 268 | #ifdef INET | |
| 269 | /* | |
| 270 | * This keeps the multicast addresses from showing up | |
| 271 | * in `arp -a' listings as unresolved. It's not actually | |
| 272 | * functional. Then the same for broadcast. | |
| 273 | */ | |
| ce6da440 | 274 | if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { |
| 984263bc MD |
275 | ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, |
| 276 | LLADDR(SDL(gate))); | |
| 277 | SDL(gate)->sdl_alen = 6; | |
| 278 | rt->rt_expire = 0; | |
| 279 | } | |
| 280 | if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { | |
| c401f0fd | 281 | memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, |
| 984263bc MD |
282 | rt->rt_ifp->if_addrlen); |
| 283 | SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; | |
| 284 | rt->rt_expire = 0; | |
| 285 | } | |
| 286 | #endif | |
| 287 | ||
| 288 | if (SIN(rt_key(rt))->sin_addr.s_addr == | |
| 289 | (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { | |
| f23061d4 JH |
290 | /* |
| 291 | * This test used to be | |
| 292 | * if (loif.if_flags & IFF_UP) | |
| 293 | * It allowed local traffic to be forced | |
| 294 | * through the hardware by configuring the | |
| 295 | * loopback down. However, it causes problems | |
| 296 | * during network configuration for boards | |
| 297 | * that can't receive packets they send. It | |
| 298 | * is now necessary to clear "useloopback" and | |
| 299 | * remove the route to force traffic out to | |
| 300 | * the hardware. | |
| 301 | */ | |
| 984263bc | 302 | rt->rt_expire = 0; |
| 2e9572df | 303 | bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), |
| 984263bc MD |
304 | SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); |
| 305 | if (useloopback) | |
| 306 | rt->rt_ifp = loif; | |
| 984263bc MD |
307 | } |
| 308 | break; | |
| 309 | ||
| 310 | case RTM_DELETE: | |
| f23061d4 | 311 | if (la == NULL) |
| 984263bc | 312 | break; |
| 984263bc | 313 | LIST_REMOVE(la, la_le); |
| f23061d4 | 314 | rt->rt_llinfo = NULL; |
| 984263bc | 315 | rt->rt_flags &= ~RTF_LLINFO; |
| f23061d4 | 316 | if (la->la_hold != NULL) |
| 984263bc | 317 | m_freem(la->la_hold); |
| f23061d4 | 318 | Free(la); |
| 799975d0 | 319 | break; |
| 984263bc MD |
320 | } |
| 321 | } | |
| 322 | ||
| 77e3dc77 | 323 | static struct mbuf * |
| 1dfeb73c SZ |
324 | arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip, |
| 325 | const struct in_addr *tip, const u_char *enaddr) | |
| 984263bc | 326 | { |
| 2256ba69 | 327 | struct mbuf *m; |
| 2256ba69 | 328 | struct arphdr *ah; |
| 984263bc MD |
329 | u_short ar_hrd; |
| 330 | ||
| 74f1caca | 331 | if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL) |
| 77e3dc77 | 332 | return NULL; |
| 69e8ff02 | 333 | m->m_pkthdr.rcvif = NULL; |
| f23061d4 | 334 | |
| 984263bc | 335 | switch (ifp->if_type) { |
| 984263bc MD |
336 | case IFT_ETHER: |
| 337 | /* | |
| 338 | * This may not be correct for types not explicitly | |
| 339 | * listed, but this is our best guess | |
| 340 | */ | |
| 341 | default: | |
| 342 | ar_hrd = htons(ARPHRD_ETHER); | |
| 343 | ||
| 344 | m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); | |
| 345 | m->m_pkthdr.len = m->m_len; | |
| 346 | MH_ALIGN(m, m->m_len); | |
| 347 | ||
| 984263bc MD |
348 | ah = mtod(m, struct arphdr *); |
| 349 | break; | |
| 350 | } | |
| 351 | ||
| 352 | ah->ar_hrd = ar_hrd; | |
| 353 | ah->ar_pro = htons(ETHERTYPE_IP); | |
| 354 | ah->ar_hln = ifp->if_addrlen; /* hardware address length */ | |
| 355 | ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ | |
| 356 | ah->ar_op = htons(ARPOP_REQUEST); | |
| f23061d4 | 357 | memcpy(ar_sha(ah), enaddr, ah->ar_hln); |
| 984263bc | 358 | memset(ar_tha(ah), 0, ah->ar_hln); |
| f23061d4 JH |
359 | memcpy(ar_spa(ah), sip, ah->ar_pln); |
| 360 | memcpy(ar_tpa(ah), tip, ah->ar_pln); | |
| 984263bc | 361 | |
| 77e3dc77 SZ |
362 | return m; |
| 363 | } | |
| 364 | ||
| 365 | static void | |
| 366 | arpreq_send(struct ifnet *ifp, struct mbuf *m) | |
| 367 | { | |
| 368 | struct sockaddr sa; | |
| 369 | struct ether_header *eh; | |
| 370 | ||
| 371 | switch (ifp->if_type) { | |
| 372 | case IFT_ETHER: | |
| 373 | /* | |
| 374 | * This may not be correct for types not explicitly | |
| 375 | * listed, but this is our best guess | |
| 376 | */ | |
| 377 | default: | |
| 378 | eh = (struct ether_header *)sa.sa_data; | |
| 379 | /* if_output() will not swap */ | |
| 380 | eh->ether_type = htons(ETHERTYPE_ARP); | |
| 381 | memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen); | |
| 382 | break; | |
| 383 | } | |
| 384 | ||
| 984263bc | 385 | sa.sa_family = AF_UNSPEC; |
| 77e3dc77 | 386 | sa.sa_len = sizeof(sa); |
| 799975d0 | 387 | ifp->if_output(ifp, m, &sa, NULL); |
| 984263bc MD |
388 | } |
| 389 | ||
| 77e3dc77 | 390 | static void |
| 002c1265 | 391 | arpreq_send_handler(netmsg_t msg) |
| 77e3dc77 | 392 | { |
| 002c1265 MD |
393 | struct mbuf *m = msg->packet.nm_packet; |
| 394 | struct ifnet *ifp = msg->lmsg.u.ms_resultp; | |
| 77e3dc77 SZ |
395 | |
| 396 | arpreq_send(ifp, m); | |
| 397 | /* nmsg was embedded in the mbuf, do not reply! */ | |
| 398 | } | |
| 399 | ||
| 400 | /* | |
| 401 | * Broadcast an ARP request. Caller specifies: | |
| 402 | * - arp header source ip address | |
| 403 | * - arp header target ip address | |
| 404 | * - arp header source ethernet address | |
| 405 | * | |
| 406 | * NOTE: Caller MUST NOT hold ifp's serializer | |
| 407 | */ | |
| 408 | static void | |
| 1dfeb73c SZ |
409 | arprequest(struct ifnet *ifp, const struct in_addr *sip, |
| 410 | const struct in_addr *tip, const u_char *enaddr) | |
| 77e3dc77 SZ |
411 | { |
| 412 | struct mbuf *m; | |
| 413 | ||
| 70d9a675 MD |
414 | if (enaddr == NULL) { |
| 415 | if (ifp->if_bridge) { | |
| 416 | enaddr = IF_LLADDR(ether_bridge_interface(ifp)); | |
| 417 | } else { | |
| 418 | enaddr = IF_LLADDR(ifp); | |
| 419 | } | |
| 420 | } | |
| 421 | ||
| 77e3dc77 SZ |
422 | m = arpreq_alloc(ifp, sip, tip, enaddr); |
| 423 | if (m == NULL) | |
| 424 | return; | |
| 425 | arpreq_send(ifp, m); | |
| 426 | } | |
| 427 | ||
| 428 | /* | |
| 429 | * Same as arprequest(), except: | |
| 430 | * - Caller is allowed to hold ifp's serializer | |
| c3c96e44 | 431 | * - Network output is done in protocol thead |
| 77e3dc77 SZ |
432 | */ |
| 433 | static void | |
| 1dfeb73c SZ |
434 | arprequest_async(struct ifnet *ifp, const struct in_addr *sip, |
| 435 | const struct in_addr *tip, const u_char *enaddr) | |
| 77e3dc77 SZ |
436 | { |
| 437 | struct mbuf *m; | |
| 438 | struct netmsg_packet *pmsg; | |
| 439 | ||
| 70d9a675 MD |
440 | if (enaddr == NULL) { |
| 441 | if (ifp->if_bridge) { | |
| 442 | enaddr = IF_LLADDR(ether_bridge_interface(ifp)); | |
| 443 | } else { | |
| 444 | enaddr = IF_LLADDR(ifp); | |
| 445 | } | |
| 446 | } | |
| 77e3dc77 SZ |
447 | m = arpreq_alloc(ifp, sip, tip, enaddr); |
| 448 | if (m == NULL) | |
| 449 | return; | |
| 450 | ||
| 451 | pmsg = &m->m_hdr.mh_netmsg; | |
| 002c1265 | 452 | netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, |
| 48e7b118 | 453 | 0, arpreq_send_handler); |
| 77e3dc77 | 454 | pmsg->nm_packet = m; |
| 002c1265 | 455 | pmsg->base.lmsg.u.ms_resultp = ifp; |
| 77e3dc77 | 456 | |
| 002c1265 | 457 | lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->base.lmsg); |
| 77e3dc77 SZ |
458 | } |
| 459 | ||
| 984263bc MD |
460 | /* |
| 461 | * Resolve an IP address into an ethernet address. If success, | |
| 462 | * desten is filled in. If there is no entry in arptab, | |
| 463 | * set one up and broadcast a request for the IP address. | |
| 464 | * Hold onto this mbuf and resend it once the address | |
| 465 | * is finally resolved. A return value of 1 indicates | |
| 466 | * that desten has been filled in and the packet should be sent | |
| 467 | * normally; a 0 return indicates that the packet has been | |
| 468 | * taken over here, either now or for later transmission. | |
| 469 | */ | |
| 470 | int | |
| 799975d0 SZ |
471 | arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, |
| 472 | struct sockaddr *dst, u_char *desten) | |
| 984263bc | 473 | { |
| f23061d4 JH |
474 | struct rtentry *rt; |
| 475 | struct llinfo_arp *la = NULL; | |
| 984263bc MD |
476 | struct sockaddr_dl *sdl; |
| 477 | ||
| 478 | if (m->m_flags & M_BCAST) { /* broadcast */ | |
| c401f0fd | 479 | memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); |
| 984263bc MD |
480 | return (1); |
| 481 | } | |
| ce6da440 | 482 | if (m->m_flags & M_MCAST) {/* multicast */ |
| 984263bc | 483 | ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); |
| f23061d4 | 484 | return (1); |
| 984263bc | 485 | } |
| f23061d4 JH |
486 | if (rt0 != NULL) { |
| 487 | if (rt_llroute(dst, rt0, &rt) != 0) { | |
| 488 | m_freem(m); | |
| 489 | return 0; | |
| 490 | } | |
| 491 | la = rt->rt_llinfo; | |
| 492 | } | |
| 493 | if (la == NULL) { | |
| 125186e0 SZ |
494 | la = arplookup(SIN(dst)->sin_addr.s_addr, |
| 495 | TRUE, RTL_REPORTMSG, FALSE); | |
| f23061d4 | 496 | if (la != NULL) |
| 984263bc MD |
497 | rt = la->la_rt; |
| 498 | } | |
| f23061d4 | 499 | if (la == NULL || rt == NULL) { |
| 984263bc | 500 | log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", |
| 5fe66e68 JH |
501 | inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ", |
| 502 | rt ? "rt" : ""); | |
| 984263bc MD |
503 | m_freem(m); |
| 504 | return (0); | |
| 505 | } | |
| 506 | sdl = SDL(rt->rt_gateway); | |
| 507 | /* | |
| 508 | * Check the address family and length is valid, the address | |
| 509 | * is resolved; otherwise, try to resolve. | |
| 510 | */ | |
| 511 | if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && | |
| 512 | sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { | |
| 513 | /* | |
| 514 | * If entry has an expiry time and it is approaching, | |
| 515 | * see if we need to send an ARP request within this | |
| 516 | * arpt_down interval. | |
| 517 | */ | |
| 518 | if ((rt->rt_expire != 0) && | |
| a916e847 | 519 | (time_second + la->la_preempt > rt->rt_expire)) { |
| 984263bc MD |
520 | arprequest(ifp, |
| 521 | &SIN(rt->rt_ifa->ifa_addr)->sin_addr, | |
| 522 | &SIN(dst)->sin_addr, | |
| 70d9a675 | 523 | NULL); |
| a916e847 | 524 | la->la_preempt--; |
| f23061d4 | 525 | } |
| 984263bc MD |
526 | |
| 527 | bcopy(LLADDR(sdl), desten, sdl->sdl_alen); | |
| 528 | return 1; | |
| 529 | } | |
| 530 | /* | |
| 07813904 | 531 | * If ARP is disabled or static on this interface, stop. |
| 984263bc MD |
532 | * XXX |
| 533 | * Probably should not allocate empty llinfo struct if we are | |
| 534 | * not going to be sending out an arp request. | |
| 535 | */ | |
| 07813904 | 536 | if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { |
| 984263bc MD |
537 | m_freem(m); |
| 538 | return (0); | |
| 539 | } | |
| 540 | /* | |
| 541 | * There is an arptab entry, but no ethernet address | |
| 542 | * response yet. Replace the held mbuf with this | |
| 543 | * latest one. | |
| 544 | */ | |
| f23061d4 | 545 | if (la->la_hold != NULL) |
| 984263bc MD |
546 | m_freem(la->la_hold); |
| 547 | la->la_hold = m; | |
| c3c96e44 | 548 | la->la_msgport = cur_netport(); |
| 03f869ef | 549 | if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) { |
| 984263bc MD |
550 | rt->rt_flags &= ~RTF_REJECT; |
| 551 | if (la->la_asked == 0 || rt->rt_expire != time_second) { | |
| 552 | rt->rt_expire = time_second; | |
| 553 | if (la->la_asked++ < arp_maxtries) { | |
| 554 | arprequest(ifp, | |
| 555 | &SIN(rt->rt_ifa->ifa_addr)->sin_addr, | |
| 556 | &SIN(dst)->sin_addr, | |
| 70d9a675 | 557 | NULL); |
| 984263bc MD |
558 | } else { |
| 559 | rt->rt_flags |= RTF_REJECT; | |
| 560 | rt->rt_expire += arpt_down; | |
| a916e847 JH |
561 | la->la_asked = 0; |
| 562 | la->la_preempt = arp_maxtries; | |
| 984263bc | 563 | } |
| 984263bc MD |
564 | } |
| 565 | } | |
| 566 | return (0); | |
| 567 | } | |
| 568 | ||
| 569 | /* | |
| 570 | * Common length and type checks are done here, | |
| 571 | * then the protocol-specific routine is called. | |
| 572 | */ | |
| 4599cf19 | 573 | static void |
| 002c1265 | 574 | arpintr(netmsg_t msg) |
| 984263bc | 575 | { |
| 002c1265 | 576 | struct mbuf *m = msg->packet.nm_packet; |
| 2256ba69 | 577 | struct arphdr *ar; |
| 8bde602d | 578 | u_short ar_hrd; |
| 984263bc | 579 | |
| 8bde602d | 580 | if (m->m_len < sizeof(struct arphdr) && |
| db597c8a | 581 | (m = m_pullup(m, sizeof(struct arphdr))) == NULL) { |
| 8bde602d | 582 | log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); |
| db597c8a | 583 | return; |
| 8bde602d JH |
584 | } |
| 585 | ar = mtod(m, struct arphdr *); | |
| 586 | ||
| 587 | ar_hrd = ntohs(ar->ar_hrd); | |
| db597c8a SZ |
588 | if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) { |
| 589 | log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", | |
| 8bde602d | 590 | (unsigned char *)&ar->ar_hrd, ""); |
| db597c8a SZ |
591 | m_freem(m); |
| 592 | return; | |
| 8bde602d | 593 | } |
| 984263bc | 594 | |
| db597c8a SZ |
595 | if (m->m_pkthdr.len < arphdr_len(ar)) { |
| 596 | if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { | |
| 597 | log(LOG_ERR, "arp: runt packet\n"); | |
| 598 | return; | |
| 599 | } | |
| 600 | ar = mtod(m, struct arphdr *); | |
| 8bde602d JH |
601 | } |
| 602 | ||
| 603 | switch (ntohs(ar->ar_pro)) { | |
| 984263bc | 604 | #ifdef INET |
| db597c8a SZ |
605 | case ETHERTYPE_IP: |
| 606 | in_arpinput(m); | |
| 607 | return; | |
| 984263bc | 608 | #endif |
| 984263bc | 609 | } |
| 8bde602d | 610 | m_freem(m); |
| 6aad077d | 611 | /* msg was embedded in the mbuf, do not reply! */ |
| 984263bc MD |
612 | } |
| 613 | ||
| 614 | #ifdef INET | |
| 615 | /* | |
| 616 | * ARP for Internet protocols on 10 Mb/s Ethernet. | |
| 617 | * Algorithm is that given in RFC 826. | |
| 618 | * In addition, a sanity check is performed on the sender | |
| 619 | * protocol address, to catch impersonators. | |
| 620 | * We no longer handle negotiations for use of trailer protocol: | |
| 621 | * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent | |
| 622 | * along with IP replies if we wanted trailers sent to us, | |
| 623 | * and also sent them in response to IP replies. | |
| 624 | * This allowed either end to announce the desire to receive | |
| 625 | * trailer packets. | |
| 626 | * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, | |
| 627 | * but formerly didn't normally send requests. | |
| 628 | */ | |
| 87ccd6a5 SZ |
629 | |
| 630 | static int log_arp_wrong_iface = 1; | |
| 631 | static int log_arp_movements = 1; | |
| 632 | static int log_arp_permanent_modify = 1; | |
| 633 | ||
| 984263bc | 634 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, |
| 799975d0 | 635 | &log_arp_wrong_iface, 0, |
| 87ccd6a5 SZ |
636 | "Log arp packets arriving on the wrong interface"); |
| 637 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, | |
| 638 | &log_arp_movements, 0, | |
| 639 | "Log arp replies from MACs different than the one in the cache"); | |
| 640 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, | |
| 641 | &log_arp_permanent_modify, 0, | |
| 642 | "Log arp replies from MACs different than the one " | |
| 643 | "in the permanent arp entry"); | |
| 644 | ||
| 984263bc | 645 | |
| ecdefdda | 646 | static void |
| 002c1265 | 647 | arp_hold_output(netmsg_t msg) |
| a6f79f3a | 648 | { |
| 002c1265 | 649 | struct mbuf *m = msg->packet.nm_packet; |
| a6f79f3a SZ |
650 | struct rtentry *rt; |
| 651 | struct ifnet *ifp; | |
| 652 | ||
| 002c1265 | 653 | rt = msg->lmsg.u.ms_resultp; |
| a6f79f3a SZ |
654 | ifp = m->m_pkthdr.rcvif; |
| 655 | m->m_pkthdr.rcvif = NULL; | |
| 656 | ||
| 657 | ifp->if_output(ifp, m, rt_key(rt), rt); | |
| 658 | ||
| 659 | /* Drop the reference count bumped by the sender */ | |
| 660 | RTFREE(rt); | |
| 661 | ||
| 662 | /* nmsg was embedded in the mbuf, do not reply! */ | |
| 663 | } | |
| 664 | ||
| 665 | static void | |
| ecdefdda | 666 | arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create, |
| 125186e0 | 667 | boolean_t generate_report, boolean_t dologging) |
| ecdefdda MD |
668 | { |
| 669 | struct arphdr *ah = mtod(m, struct arphdr *); | |
| 670 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
| 671 | struct llinfo_arp *la; | |
| 672 | struct sockaddr_dl *sdl; | |
| 673 | struct rtentry *rt; | |
| ecdefdda | 674 | |
| 125186e0 | 675 | la = arplookup(saddr, create, generate_report, FALSE); |
| ecdefdda MD |
676 | if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { |
| 677 | struct in_addr isaddr = { saddr }; | |
| 678 | ||
| 70d9a675 MD |
679 | /* |
| 680 | * Normally arps coming in on the wrong interface are ignored, | |
| 681 | * but if we are bridging and the two interfaces belong to | |
| 682 | * the same bridge, or one is a member of the bridge which | |
| 683 | * is the other, then it isn't an error. | |
| 684 | */ | |
| a8d45119 | 685 | if (rt->rt_ifp != ifp) { |
| 70d9a675 MD |
686 | /* |
| 687 | * (1) ifp and rt_ifp both members of same bridge | |
| 688 | * (2) rt_ifp member of bridge ifp | |
| 689 | * (3) ifp member of bridge rt_ifp | |
| 690 | * | |
| 691 | * Always replace rt_ifp with the bridge ifc. | |
| 692 | */ | |
| 693 | struct ifnet *nifp; | |
| 694 | ||
| 4ee4f753 MD |
695 | if (ifp->if_bridge && |
| 696 | rt->rt_ifp->if_bridge == ifp->if_bridge) { | |
| 70d9a675 MD |
697 | nifp = ether_bridge_interface(ifp); |
| 698 | } else if (rt->rt_ifp->if_bridge && | |
| 699 | ether_bridge_interface(rt->rt_ifp) == ifp) { | |
| 700 | nifp = ifp; | |
| 701 | } else if (ifp->if_bridge && | |
| 702 | ether_bridge_interface(ifp) == rt->rt_ifp) { | |
| 703 | nifp = rt->rt_ifp; | |
| 704 | } else { | |
| 705 | nifp = NULL; | |
| 4ee4f753 | 706 | } |
| 4ee4f753 | 707 | |
| 70d9a675 MD |
708 | if ((log_arp_wrong_iface == 1 && nifp == NULL) || |
| 709 | log_arp_wrong_iface == 2) { | |
| ecdefdda MD |
710 | log(LOG_ERR, |
| 711 | "arp: %s is on %s " | |
| 712 | "but got reply from %*D on %s\n", | |
| 713 | inet_ntoa(isaddr), | |
| 714 | rt->rt_ifp->if_xname, | |
| 715 | ifp->if_addrlen, (u_char *)ar_sha(ah), ":", | |
| 716 | ifp->if_xname); | |
| 717 | } | |
| 70d9a675 MD |
718 | if (nifp == NULL) |
| 719 | return; | |
| 720 | ||
| 721 | /* | |
| 722 | * nifp is our man! Replace rt_ifp and adjust | |
| 723 | * the sdl. | |
| 724 | */ | |
| 725 | ifp = rt->rt_ifp = nifp; | |
| 726 | sdl->sdl_type = ifp->if_type; | |
| 727 | sdl->sdl_index = ifp->if_index; | |
| ecdefdda MD |
728 | } |
| 729 | if (sdl->sdl_alen && | |
| 730 | bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { | |
| 731 | if (rt->rt_expire != 0) { | |
| 87ccd6a5 | 732 | if (dologging && log_arp_movements) { |
| ecdefdda MD |
733 | log(LOG_INFO, |
| 734 | "arp: %s moved from %*D to %*D on %s\n", | |
| 735 | inet_ntoa(isaddr), | |
| 736 | ifp->if_addrlen, (u_char *)LLADDR(sdl), | |
| 737 | ":", ifp->if_addrlen, | |
| 738 | (u_char *)ar_sha(ah), ":", | |
| 739 | ifp->if_xname); | |
| 740 | } | |
| 741 | } else { | |
| 87ccd6a5 | 742 | if (dologging && log_arp_permanent_modify) { |
| ecdefdda | 743 | log(LOG_ERR, |
| 799975d0 SZ |
744 | "arp: %*D attempts to modify " |
| 745 | "permanent entry for %s on %s\n", | |
| 746 | ifp->if_addrlen, (u_char *)ar_sha(ah), | |
| 747 | ":", inet_ntoa(isaddr), ifp->if_xname); | |
| ecdefdda MD |
748 | } |
| 749 | return; | |
| 750 | } | |
| 751 | } | |
| 752 | /* | |
| 753 | * sanity check for the address length. | |
| 754 | * XXX this does not work for protocols with variable address | |
| 755 | * length. -is | |
| 756 | */ | |
| e090f4c1 | 757 | if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) { |
| ecdefdda MD |
758 | log(LOG_WARNING, |
| 759 | "arp from %*D: new addr len %d, was %d", | |
| 760 | ifp->if_addrlen, (u_char *) ar_sha(ah), ":", | |
| 761 | ah->ar_hln, sdl->sdl_alen); | |
| 762 | } | |
| 763 | if (ifp->if_addrlen != ah->ar_hln) { | |
| e090f4c1 | 764 | if (dologging) { |
| ecdefdda | 765 | log(LOG_WARNING, |
| 799975d0 SZ |
766 | "arp from %*D: addr len: new %d, i/f %d " |
| 767 | "(ignored)", | |
| ecdefdda MD |
768 | ifp->if_addrlen, (u_char *) ar_sha(ah), ":", |
| 769 | ah->ar_hln, ifp->if_addrlen); | |
| 770 | } | |
| 771 | return; | |
| 772 | } | |
| 773 | memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln); | |
| 70d9a675 | 774 | if (rt->rt_expire != 0) { |
| ecdefdda | 775 | rt->rt_expire = time_second + arpt_keep; |
| 70d9a675 | 776 | } |
| ecdefdda MD |
777 | rt->rt_flags &= ~RTF_REJECT; |
| 778 | la->la_asked = 0; | |
| 779 | la->la_preempt = arp_maxtries; | |
| 780 | ||
| 781 | /* | |
| 782 | * This particular cpu might have been holding an mbuf | |
| 783 | * pending ARP resolution. If so, transmit the mbuf now. | |
| 784 | */ | |
| 785 | if (la->la_hold != NULL) { | |
| a6f79f3a SZ |
786 | struct mbuf *m = la->la_hold; |
| 787 | struct lwkt_port *port = la->la_msgport; | |
| 788 | struct netmsg_packet *pmsg; | |
| 789 | ||
| ecdefdda | 790 | la->la_hold = NULL; |
| a6f79f3a SZ |
791 | la->la_msgport = NULL; |
| 792 | ||
| 793 | m_adj(m, sizeof(struct ether_header)); | |
| 794 | ||
| 795 | /* | |
| 796 | * Make sure that this rtentry will not be freed | |
| 797 | * before the packet is processed on the target | |
| 798 | * msgport. The reference count will be dropped | |
| 799 | * in the handler associated with this packet. | |
| 800 | */ | |
| 801 | rt->rt_refcnt++; | |
| 802 | ||
| 803 | pmsg = &m->m_hdr.mh_netmsg; | |
| 002c1265 | 804 | netmsg_init(&pmsg->base, NULL, |
| 48e7b118 | 805 | &netisr_apanic_rport, |
| c3c96e44 | 806 | MSGF_PRIORITY, arp_hold_output); |
| a6f79f3a SZ |
807 | pmsg->nm_packet = m; |
| 808 | ||
| 809 | /* Record necessary information */ | |
| 810 | m->m_pkthdr.rcvif = ifp; | |
| 002c1265 | 811 | pmsg->base.lmsg.u.ms_resultp = rt; |
| a6f79f3a | 812 | |
| 002c1265 | 813 | lwkt_sendmsg(port, &pmsg->base.lmsg); |
| ecdefdda MD |
814 | } |
| 815 | } | |
| 816 | } | |
| 817 | ||
| 818 | #ifdef SMP | |
| 819 | ||
| 820 | struct netmsg_arp_update { | |
| 002c1265 | 821 | struct netmsg_base base; |
| ecdefdda MD |
822 | struct mbuf *m; |
| 823 | in_addr_t saddr; | |
| 824 | boolean_t create; | |
| 825 | }; | |
| 826 | ||
| 002c1265 | 827 | static void arp_update_msghandler(netmsg_t msg); |
| ecdefdda MD |
828 | |
| 829 | #endif | |
| 830 | ||
| 831 | /* | |
| 832 | * Called from arpintr() - this routine is run from a single cpu. | |
| 833 | */ | |
| 984263bc | 834 | static void |
| f23061d4 | 835 | in_arpinput(struct mbuf *m) |
| 984263bc | 836 | { |
| 2256ba69 RG |
837 | struct arphdr *ah; |
| 838 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
| 984263bc | 839 | struct ether_header *eh; |
| 2256ba69 | 840 | struct rtentry *rt; |
| b2632176 | 841 | struct ifaddr_container *ifac; |
| f8983475 | 842 | struct in_ifaddr_container *iac; |
| 5bd4422e | 843 | struct in_ifaddr *ia = NULL; |
| 984263bc MD |
844 | struct sockaddr sa; |
| 845 | struct in_addr isaddr, itaddr, myaddr; | |
| ecdefdda MD |
846 | #ifdef SMP |
| 847 | struct netmsg_arp_update msg; | |
| 848 | #endif | |
| 5bd4422e | 849 | uint8_t *enaddr = NULL; |
| ecdefdda | 850 | int op; |
| 984263bc MD |
851 | int req_len; |
| 852 | ||
| 853 | req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); | |
| 854 | if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { | |
| 855 | log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); | |
| 856 | return; | |
| 857 | } | |
| 858 | ||
| 859 | ah = mtod(m, struct arphdr *); | |
| 860 | op = ntohs(ah->ar_op); | |
| f23061d4 JH |
861 | memcpy(&isaddr, ar_spa(ah), sizeof isaddr); |
| 862 | memcpy(&itaddr, ar_tpa(ah), sizeof itaddr); | |
| 5bd4422e SZ |
863 | |
| 864 | myaddr.s_addr = INADDR_ANY; | |
| 865 | #ifdef CARP | |
| 866 | if (ifp->if_carp != NULL) { | |
| 867 | get_mplock(); | |
| 868 | if (ifp->if_carp != NULL && | |
| 869 | carp_iamatch(ifp->if_carp, &itaddr, &isaddr, &enaddr)) { | |
| 870 | rel_mplock(); | |
| 871 | myaddr = itaddr; | |
| 872 | goto match; | |
| 873 | } | |
| 874 | rel_mplock(); | |
| 875 | } | |
| 876 | #endif | |
| 877 | ||
| 984263bc | 878 | /* |
| ed5bc924 SS |
879 | * Check both target and sender IP addresses: |
| 880 | * | |
| 881 | * If we receive the packet on the interface owning the address, | |
| 882 | * then accept the address. | |
| 883 | * | |
| 884 | * For a bridge, we accept the address if the receive interface and | |
| 70d9a675 MD |
885 | * the interface owning the address are on the same bridge, and |
| 886 | * use the bridge MAC as the is-at response. The bridge will be | |
| 887 | * responsible for handling the packet. | |
| 888 | * | |
| 889 | * (1) Check target IP against our local IPs | |
| 984263bc | 890 | */ |
| f8983475 SZ |
891 | LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) { |
| 892 | ia = iac->ia; | |
| 893 | ||
| ed5bc924 SS |
894 | /* Skip all ia's which don't match */ |
| 895 | if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr) | |
| 896 | continue; | |
| 5bd4422e SZ |
897 | #ifdef CARP |
| 898 | if (ia->ia_ifp->if_type == IFT_CARP) | |
| 899 | continue; | |
| 900 | #endif | |
| 70d9a675 MD |
901 | if (ifp->if_bridge && ia->ia_ifp && |
| 902 | ifp->if_bridge == ia->ia_ifp->if_bridge) { | |
| 903 | ifp = ether_bridge_interface(ifp); | |
| 904 | goto match; | |
| 905 | } | |
| 906 | if (ia->ia_ifp && ia->ia_ifp->if_bridge && | |
| 907 | ether_bridge_interface(ia->ia_ifp) == ifp) { | |
| 908 | goto match; | |
| 909 | } | |
| 910 | if (ifp->if_bridge && ether_bridge_interface(ifp) == | |
| 911 | ia->ia_ifp) { | |
| 912 | goto match; | |
| 913 | } | |
| ed5bc924 SS |
914 | if (ia->ia_ifp == ifp) |
| 915 | goto match; | |
| 916 | ||
| ac93838f | 917 | } |
| 70d9a675 MD |
918 | |
| 919 | /* | |
| 920 | * (2) Check sender IP against our local IPs | |
| 921 | */ | |
| f8983475 SZ |
922 | LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) { |
| 923 | ia = iac->ia; | |
| 924 | ||
| ed5bc924 SS |
925 | /* Skip all ia's which don't match */ |
| 926 | if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr) | |
| 927 | continue; | |
| 5bd4422e SZ |
928 | #ifdef CARP |
| 929 | if (ia->ia_ifp->if_type == IFT_CARP) | |
| 930 | continue; | |
| 931 | #endif | |
| 70d9a675 MD |
932 | if (ifp->if_bridge && ia->ia_ifp && |
| 933 | ifp->if_bridge == ia->ia_ifp->if_bridge) { | |
| 934 | ifp = ether_bridge_interface(ifp); | |
| ed5bc924 | 935 | goto match; |
| 70d9a675 MD |
936 | } |
| 937 | if (ia->ia_ifp && ia->ia_ifp->if_bridge && | |
| 938 | ether_bridge_interface(ia->ia_ifp) == ifp) { | |
| 939 | goto match; | |
| 940 | } | |
| 941 | if (ifp->if_bridge && ether_bridge_interface(ifp) == | |
| 942 | ia->ia_ifp) { | |
| 943 | goto match; | |
| 944 | } | |
| ed5bc924 | 945 | |
| 70d9a675 | 946 | if (ia->ia_ifp == ifp) |
| ac93838f | 947 | goto match; |
| ac93838f | 948 | } |
| 70d9a675 | 949 | |
| 984263bc MD |
950 | /* |
| 951 | * No match, use the first inet address on the receive interface | |
| 952 | * as a dummy address for the rest of the function. | |
| 953 | */ | |
| b2632176 SZ |
954 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 955 | struct ifaddr *ifa = ifac->ifa; | |
| 956 | ||
| 984263bc MD |
957 | if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { |
| 958 | ia = ifatoia(ifa); | |
| 959 | goto match; | |
| 960 | } | |
| 984263bc | 961 | } |
| 70d9a675 | 962 | |
| ed5bc924 SS |
963 | /* |
| 964 | * If we got here, we didn't find any suitable interface, | |
| 965 | * so drop the packet. | |
| 966 | */ | |
| 967 | m_freem(m); | |
| 968 | return; | |
| 969 | ||
| 984263bc | 970 | match: |
| 0d16ba1d | 971 | if (!enaddr) |
| 5bd4422e SZ |
972 | enaddr = (uint8_t *)IF_LLADDR(ifp); |
| 973 | if (myaddr.s_addr == INADDR_ANY) | |
| 974 | myaddr = ia->ia_addr.sin_addr; | |
| 0d16ba1d | 975 | if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) { |
| 984263bc MD |
976 | m_freem(m); /* it's from me, ignore it. */ |
| 977 | return; | |
| 978 | } | |
| c401f0fd | 979 | if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { |
| 984263bc MD |
980 | log(LOG_ERR, |
| 981 | "arp: link address is broadcast for IP address %s!\n", | |
| 982 | inet_ntoa(isaddr)); | |
| 983 | m_freem(m); | |
| 984 | return; | |
| 985 | } | |
| c3749f74 | 986 | if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { |
| 984263bc MD |
987 | log(LOG_ERR, |
| 988 | "arp: %*D is using my IP address %s!\n", | |
| 989 | ifp->if_addrlen, (u_char *)ar_sha(ah), ":", | |
| 990 | inet_ntoa(isaddr)); | |
| 991 | itaddr = myaddr; | |
| 992 | goto reply; | |
| 993 | } | |
| 07813904 SZ |
994 | if (ifp->if_flags & IFF_STATICARP) |
| 995 | goto reply; | |
| 70d9a675 MD |
996 | |
| 997 | /* | |
| 998 | * When arp_restricted_match is true and the ARP response is not | |
| 999 | * specifically targetted to me, ignore it. Otherwise the entry | |
| 1000 | * timeout may be updated for an old MAC. | |
| 1001 | */ | |
| 1002 | if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) { | |
| 1003 | m_freem(m); | |
| 1004 | return; | |
| 1005 | } | |
| 1006 | ||
| ecdefdda | 1007 | #ifdef SMP |
| 002c1265 | 1008 | netmsg_init(&msg.base, NULL, &curthread->td_msgport, |
| 48e7b118 | 1009 | 0, arp_update_msghandler); |
| ecdefdda MD |
1010 | msg.m = m; |
| 1011 | msg.saddr = isaddr.s_addr; | |
| 1012 | msg.create = (itaddr.s_addr == myaddr.s_addr); | |
| 002c1265 | 1013 | lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0); |
| e090f4c1 | 1014 | #else |
| ecdefdda | 1015 | arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr), |
| 125186e0 | 1016 | RTL_REPORTMSG, TRUE); |
| e090f4c1 | 1017 | #endif |
| 984263bc MD |
1018 | reply: |
| 1019 | if (op != ARPOP_REQUEST) { | |
| 1020 | m_freem(m); | |
| 1021 | return; | |
| 1022 | } | |
| 1023 | if (itaddr.s_addr == myaddr.s_addr) { | |
| 1024 | /* I am the target */ | |
| f23061d4 | 1025 | memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); |
| 0d16ba1d | 1026 | memcpy(ar_sha(ah), enaddr, ah->ar_hln); |
| 984263bc | 1027 | } else { |
| ecdefdda MD |
1028 | struct llinfo_arp *la; |
| 1029 | ||
| 125186e0 | 1030 | la = arplookup(itaddr.s_addr, FALSE, RTL_DONTREPORT, SIN_PROXY); |
| 984263bc MD |
1031 | if (la == NULL) { |
| 1032 | struct sockaddr_in sin; | |
| 1033 | ||
| 1034 | if (!arp_proxyall) { | |
| 1035 | m_freem(m); | |
| 1036 | return; | |
| 1037 | } | |
| 1038 | ||
| 1039 | bzero(&sin, sizeof sin); | |
| 1040 | sin.sin_family = AF_INET; | |
| 1041 | sin.sin_len = sizeof sin; | |
| 1042 | sin.sin_addr = itaddr; | |
| 1043 | ||
| f3ed2586 | 1044 | rt = rtpurelookup((struct sockaddr *)&sin); |
| 0c3c561c | 1045 | if (rt == NULL) { |
| 984263bc MD |
1046 | m_freem(m); |
| 1047 | return; | |
| 1048 | } | |
| be23faf1 | 1049 | --rt->rt_refcnt; |
| 984263bc MD |
1050 | /* |
| 1051 | * Don't send proxies for nodes on the same interface | |
| 1052 | * as this one came out of, or we'll get into a fight | |
| 1053 | * over who claims what Ether address. | |
| 1054 | */ | |
| 1055 | if (rt->rt_ifp == ifp) { | |
| 984263bc MD |
1056 | m_freem(m); |
| 1057 | return; | |
| 1058 | } | |
| f23061d4 | 1059 | memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); |
| 0d16ba1d | 1060 | memcpy(ar_sha(ah), enaddr, ah->ar_hln); |
| 984263bc | 1061 | #ifdef DEBUG_PROXY |
| a6ec04bc | 1062 | kprintf("arp: proxying for %s\n", inet_ntoa(itaddr)); |
| 984263bc MD |
1063 | #endif |
| 1064 | } else { | |
| ecdefdda MD |
1065 | struct sockaddr_dl *sdl; |
| 1066 | ||
| 984263bc | 1067 | rt = la->la_rt; |
| f23061d4 | 1068 | memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); |
| 984263bc | 1069 | sdl = SDL(rt->rt_gateway); |
| f23061d4 | 1070 | memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); |
| 984263bc MD |
1071 | } |
| 1072 | } | |
| 1073 | ||
| f23061d4 JH |
1074 | memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); |
| 1075 | memcpy(ar_spa(ah), &itaddr, ah->ar_pln); | |
| 984263bc MD |
1076 | ah->ar_op = htons(ARPOP_REPLY); |
| 1077 | ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ | |
| 1078 | switch (ifp->if_type) { | |
| 984263bc | 1079 | case IFT_ETHER: |
| 70d9a675 MD |
1080 | /* |
| 1081 | * May not be correct for types not explictly | |
| 1082 | * listed, but it is our best guess. | |
| 1083 | */ | |
| 984263bc MD |
1084 | default: |
| 1085 | eh = (struct ether_header *)sa.sa_data; | |
| 0c3c561c | 1086 | memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost); |
| 984263bc MD |
1087 | eh->ether_type = htons(ETHERTYPE_ARP); |
| 1088 | break; | |
| 1089 | } | |
| 1090 | sa.sa_family = AF_UNSPEC; | |
| 0c3c561c | 1091 | sa.sa_len = sizeof sa; |
| 799975d0 | 1092 | ifp->if_output(ifp, m, &sa, NULL); |
| 984263bc | 1093 | } |
| ecdefdda MD |
1094 | |
| 1095 | #ifdef SMP | |
| 1096 | ||
| 799975d0 | 1097 | static void |
| 002c1265 | 1098 | arp_update_msghandler(netmsg_t msg) |
| ecdefdda | 1099 | { |
| 002c1265 | 1100 | struct netmsg_arp_update *rmsg = (struct netmsg_arp_update *)msg; |
| ecdefdda MD |
1101 | int nextcpu; |
| 1102 | ||
| 125186e0 SZ |
1103 | /* |
| 1104 | * This message handler will be called on all of the CPUs, | |
| 1105 | * however, we only need to generate rtmsg on CPU0. | |
| 1106 | */ | |
| 002c1265 | 1107 | arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->create, |
| 125186e0 SZ |
1108 | mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT, |
| 1109 | mycpuid == 0); | |
| ecdefdda MD |
1110 | |
| 1111 | nextcpu = mycpuid + 1; | |
| 799975d0 | 1112 | if (nextcpu < ncpus) |
| 002c1265 | 1113 | lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg); |
| 799975d0 | 1114 | else |
| 002c1265 | 1115 | lwkt_replymsg(&rmsg->base.lmsg, 0); |
| ecdefdda MD |
1116 | } |
| 1117 | ||
| 799975d0 | 1118 | #endif /* SMP */ |
| ecdefdda | 1119 | |
| 799975d0 | 1120 | #endif /* INET */ |
| 984263bc MD |
1121 | |
| 1122 | /* | |
| 03f869ef MD |
1123 | * Free an arp entry. If the arp entry is actively referenced or represents |
| 1124 | * a static entry we only clear it back to an unresolved state, otherwise | |
| 1125 | * we destroy the entry entirely. | |
| 1126 | * | |
| 1127 | * Note that static entries are created when route add ... -interface is used | |
| 1128 | * to create an interface route to a (direct) destination. | |
| 984263bc MD |
1129 | */ |
| 1130 | static void | |
| 5fe66e68 | 1131 | arptfree(struct llinfo_arp *la) |
| 984263bc | 1132 | { |
| 2256ba69 RG |
1133 | struct rtentry *rt = la->la_rt; |
| 1134 | struct sockaddr_dl *sdl; | |
| f23061d4 | 1135 | |
| 03f869ef | 1136 | if (rt == NULL) |
| 984263bc | 1137 | panic("arptfree"); |
| 03f869ef | 1138 | sdl = SDL(rt->rt_gateway); |
| 5fe66e68 JH |
1139 | if (sdl != NULL && |
| 1140 | ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) || | |
| 1141 | (rt->rt_flags & RTF_STATIC))) { | |
| 984263bc MD |
1142 | sdl->sdl_alen = 0; |
| 1143 | la->la_preempt = la->la_asked = 0; | |
| 1144 | rt->rt_flags &= ~RTF_REJECT; | |
| 1145 | return; | |
| 1146 | } | |
| f23061d4 | 1147 | rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL); |
| 984263bc | 1148 | } |
| 0c3c561c | 1149 | |
| 984263bc MD |
1150 | /* |
| 1151 | * Lookup or enter a new address in arptab. | |
| 1152 | */ | |
| 1153 | static struct llinfo_arp * | |
| 125186e0 SZ |
1154 | arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report, |
| 1155 | boolean_t proxy) | |
| 984263bc | 1156 | { |
| 2256ba69 | 1157 | struct rtentry *rt; |
| f3ed2586 | 1158 | struct sockaddr_inarp sin = { sizeof sin, AF_INET }; |
| f23061d4 | 1159 | const char *why = NULL; |
| 984263bc MD |
1160 | |
| 1161 | sin.sin_addr.s_addr = addr; | |
| 1162 | sin.sin_other = proxy ? SIN_PROXY : 0; | |
| 125186e0 SZ |
1163 | if (create) { |
| 1164 | rt = _rtlookup((struct sockaddr *)&sin, | |
| 1165 | generate_report, RTL_DOCLONE); | |
| 1166 | } else { | |
| f3ed2586 | 1167 | rt = rtpurelookup((struct sockaddr *)&sin); |
| 125186e0 | 1168 | } |
| f23061d4 JH |
1169 | if (rt == NULL) |
| 1170 | return (NULL); | |
| 984263bc MD |
1171 | rt->rt_refcnt--; |
| 1172 | ||
| 1173 | if (rt->rt_flags & RTF_GATEWAY) | |
| 1174 | why = "host is not on local network"; | |
| f23061d4 | 1175 | else if (!(rt->rt_flags & RTF_LLINFO)) |
| 984263bc MD |
1176 | why = "could not allocate llinfo"; |
| 1177 | else if (rt->rt_gateway->sa_family != AF_LINK) | |
| 1178 | why = "gateway route is not ours"; | |
| 1179 | ||
| 8fd3e627 MD |
1180 | if (why) { |
| 1181 | if (create) { | |
| 1182 | log(LOG_DEBUG, "arplookup %s failed: %s\n", | |
| 1183 | inet_ntoa(sin.sin_addr), why); | |
| 1184 | } | |
| f23061d4 JH |
1185 | if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) { |
| 1186 | /* No references to this route. Purge it. */ | |
| 1187 | rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, | |
| be23faf1 | 1188 | rt_mask(rt), rt->rt_flags, NULL); |
| 8fd3e627 | 1189 | } |
| f23061d4 | 1190 | return (NULL); |
| 984263bc | 1191 | } |
| f23061d4 | 1192 | return (rt->rt_llinfo); |
| 984263bc MD |
1193 | } |
| 1194 | ||
| 1195 | void | |
| f23061d4 | 1196 | arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) |
| 984263bc | 1197 | { |
| 57dff79c | 1198 | if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) { |
| 77e3dc77 | 1199 | arprequest_async(ifp, &IA_SIN(ifa)->sin_addr, |
| 70d9a675 | 1200 | &IA_SIN(ifa)->sin_addr, NULL); |
| 57dff79c | 1201 | } |
| 984263bc MD |
1202 | ifa->ifa_rtrequest = arp_rtrequest; |
| 1203 | ifa->ifa_flags |= RTF_CLONING; | |
| 1204 | } | |
| 1205 | ||
| 0d16ba1d | 1206 | void |
| 5bd4422e | 1207 | arp_iainit(struct ifnet *ifp, const struct in_addr *addr, const u_char *enaddr) |
| 0d16ba1d | 1208 | { |
| 5bd4422e SZ |
1209 | if (addr->s_addr != INADDR_ANY) |
| 1210 | arprequest_async(ifp, addr, addr, enaddr); | |
| 0d16ba1d MD |
1211 | } |
| 1212 | ||
| 984263bc MD |
1213 | static void |
| 1214 | arp_init(void) | |
| 1215 | { | |
| ecdefdda MD |
1216 | int cpu; |
| 1217 | ||
| 1218 | for (cpu = 0; cpu < ncpus2; cpu++) | |
| 1219 | LIST_INIT(&llinfo_arp_list[cpu]); | |
| ed2327fe | 1220 | |
| c3c96e44 | 1221 | netisr_register(NETISR_ARP, arpintr, NULL); |
| 984263bc MD |
1222 | } |
| 1223 | ||
| 1224 | SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); |