| 1 | /* |
| 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 | /* |
| 34 | * Copyright (c) 2004, 2005 Jeffrey M. Hsu. All rights reserved. |
| 35 | * |
| 36 | * License terms: all terms for the DragonFly license above plus the following: |
| 37 | * |
| 38 | * 4. All advertising materials mentioning features or use of this software |
| 39 | * must display the following acknowledgement: |
| 40 | * |
| 41 | * This product includes software developed by Jeffrey M. Hsu |
| 42 | * for the DragonFly Project. |
| 43 | * |
| 44 | * This requirement may be waived with permission from Jeffrey Hsu. |
| 45 | * Permission will be granted to any DragonFly user for free. |
| 46 | * This requirement will sunset and may be removed on Jan 31, 2006, |
| 47 | * after which the standard DragonFly license (as shown above) will |
| 48 | * apply. |
| 49 | */ |
| 50 | |
| 51 | /* |
| 52 | * Copyright (c) 1982, 1986, 1988, 1993 |
| 53 | * The Regents of the University of California. All rights reserved. |
| 54 | * |
| 55 | * Redistribution and use in source and binary forms, with or without |
| 56 | * modification, are permitted provided that the following conditions |
| 57 | * are met: |
| 58 | * 1. Redistributions of source code must retain the above copyright |
| 59 | * notice, this list of conditions and the following disclaimer. |
| 60 | * 2. Redistributions in binary form must reproduce the above copyright |
| 61 | * notice, this list of conditions and the following disclaimer in the |
| 62 | * documentation and/or other materials provided with the distribution. |
| 63 | * 3. All advertising materials mentioning features or use of this software |
| 64 | * must display the following acknowledgement: |
| 65 | * This product includes software developed by the University of |
| 66 | * California, Berkeley and its contributors. |
| 67 | * 4. Neither the name of the University nor the names of its contributors |
| 68 | * may be used to endorse or promote products derived from this software |
| 69 | * without specific prior written permission. |
| 70 | * |
| 71 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 72 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 73 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 74 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 75 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 76 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 77 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 78 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 79 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 80 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 81 | * SUCH DAMAGE. |
| 82 | * |
| 83 | * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 |
| 84 | * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $ |
| 85 | * $DragonFly: src/sys/netinet/if_ether.c,v 1.30 2005/12/21 16:40:25 corecode Exp $ |
| 86 | */ |
| 87 | |
| 88 | /* |
| 89 | * Ethernet address resolution protocol. |
| 90 | * TODO: |
| 91 | * add "inuse/lock" bit (or ref. count) along with valid bit |
| 92 | */ |
| 93 | |
| 94 | #include "opt_inet.h" |
| 95 | #include "opt_bdg.h" |
| 96 | |
| 97 | #include <sys/param.h> |
| 98 | #include <sys/kernel.h> |
| 99 | #include <sys/queue.h> |
| 100 | #include <sys/sysctl.h> |
| 101 | #include <sys/systm.h> |
| 102 | #include <sys/mbuf.h> |
| 103 | #include <sys/malloc.h> |
| 104 | #include <sys/socket.h> |
| 105 | #include <sys/syslog.h> |
| 106 | |
| 107 | #include <sys/thread2.h> |
| 108 | #include <sys/msgport2.h> |
| 109 | |
| 110 | #include <net/if.h> |
| 111 | #include <net/if_dl.h> |
| 112 | #include <net/if_types.h> |
| 113 | #include <net/route.h> |
| 114 | #include <net/netisr.h> |
| 115 | #include <net/if_llc.h> |
| 116 | #ifdef BRIDGE |
| 117 | #include <net/ethernet.h> |
| 118 | #include <net/oldbridge/bridge.h> |
| 119 | #endif |
| 120 | |
| 121 | #include <netinet/in.h> |
| 122 | #include <netinet/in_var.h> |
| 123 | #include <netinet/if_ether.h> |
| 124 | |
| 125 | #include <net/if_arc.h> |
| 126 | #include <net/iso88025.h> |
| 127 | |
| 128 | #define SIN(s) ((struct sockaddr_in *)s) |
| 129 | #define SDL(s) ((struct sockaddr_dl *)s) |
| 130 | |
| 131 | SYSCTL_DECL(_net_link_ether); |
| 132 | SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); |
| 133 | |
| 134 | /* timer values */ |
| 135 | static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ |
| 136 | static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ |
| 137 | static int arpt_down = 20; /* once declared down, don't send for 20 sec */ |
| 138 | |
| 139 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, |
| 140 | &arpt_prune, 0, ""); |
| 141 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, |
| 142 | &arpt_keep, 0, ""); |
| 143 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, |
| 144 | &arpt_down, 0, ""); |
| 145 | |
| 146 | #define rt_expire rt_rmx.rmx_expire |
| 147 | |
| 148 | struct llinfo_arp { |
| 149 | LIST_ENTRY(llinfo_arp) la_le; |
| 150 | struct rtentry *la_rt; |
| 151 | struct mbuf *la_hold; /* last packet until resolved/timeout */ |
| 152 | u_short la_preempt; /* countdown for pre-expiry arps */ |
| 153 | u_short la_asked; /* #times we QUERIED following expiration */ |
| 154 | }; |
| 155 | |
| 156 | static LIST_HEAD(, llinfo_arp) llinfo_arp; |
| 157 | |
| 158 | static int arp_maxtries = 5; |
| 159 | static int useloopback = 1; /* use loopback interface for local traffic */ |
| 160 | static int arp_proxyall = 0; |
| 161 | |
| 162 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, |
| 163 | &arp_maxtries, 0, ""); |
| 164 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, |
| 165 | &useloopback, 0, ""); |
| 166 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, |
| 167 | &arp_proxyall, 0, ""); |
| 168 | |
| 169 | static void arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *); |
| 170 | static void arprequest (struct ifnet *, |
| 171 | struct in_addr *, struct in_addr *, u_char *); |
| 172 | static int arpintr(struct netmsg *); |
| 173 | static void arptfree (struct llinfo_arp *); |
| 174 | static void arptimer (void *); |
| 175 | static struct llinfo_arp |
| 176 | *arplookup (in_addr_t addr, boolean_t create, boolean_t proxy); |
| 177 | #ifdef INET |
| 178 | static void in_arpinput (struct mbuf *); |
| 179 | #endif |
| 180 | |
| 181 | static struct callout arptimer_ch; |
| 182 | |
| 183 | /* |
| 184 | * Timeout routine. Age arp_tab entries periodically. |
| 185 | */ |
| 186 | /* ARGSUSED */ |
| 187 | static void |
| 188 | arptimer(void *ignored_arg) |
| 189 | { |
| 190 | struct llinfo_arp *la, *nla; |
| 191 | |
| 192 | crit_enter(); |
| 193 | LIST_FOREACH_MUTABLE(la, &llinfo_arp, la_le, nla) { |
| 194 | if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second) |
| 195 | arptfree(la); /* might remove la from llinfo_arp! */ |
| 196 | } |
| 197 | callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL); |
| 198 | crit_exit(); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Parallel to llc_rtrequest. |
| 203 | */ |
| 204 | static void |
| 205 | arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) |
| 206 | { |
| 207 | struct sockaddr *gate = rt->rt_gateway; |
| 208 | struct llinfo_arp *la = rt->rt_llinfo; |
| 209 | |
| 210 | struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK }; |
| 211 | static boolean_t arpinit_done; |
| 212 | |
| 213 | if (!arpinit_done) { |
| 214 | arpinit_done = TRUE; |
| 215 | callout_init(&arptimer_ch); |
| 216 | callout_reset(&arptimer_ch, hz, arptimer, NULL); |
| 217 | } |
| 218 | if (rt->rt_flags & RTF_GATEWAY) |
| 219 | return; |
| 220 | |
| 221 | switch (req) { |
| 222 | case RTM_ADD: |
| 223 | /* |
| 224 | * XXX: If this is a manually added route to interface |
| 225 | * such as older version of routed or gated might provide, |
| 226 | * restore cloning bit. |
| 227 | */ |
| 228 | if (!(rt->rt_flags & RTF_HOST) && |
| 229 | SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) |
| 230 | rt->rt_flags |= RTF_CLONING; |
| 231 | if (rt->rt_flags & RTF_CLONING) { |
| 232 | /* |
| 233 | * Case 1: This route should come from a route to iface. |
| 234 | */ |
| 235 | rt_setgate(rt, rt_key(rt), |
| 236 | (struct sockaddr *)&null_sdl); |
| 237 | gate = rt->rt_gateway; |
| 238 | SDL(gate)->sdl_type = rt->rt_ifp->if_type; |
| 239 | SDL(gate)->sdl_index = rt->rt_ifp->if_index; |
| 240 | rt->rt_expire = time_second; |
| 241 | break; |
| 242 | } |
| 243 | /* Announce a new entry if requested. */ |
| 244 | if (rt->rt_flags & RTF_ANNOUNCE) |
| 245 | arprequest(rt->rt_ifp, |
| 246 | &SIN(rt_key(rt))->sin_addr, |
| 247 | &SIN(rt_key(rt))->sin_addr, |
| 248 | LLADDR(SDL(gate))); |
| 249 | /*FALLTHROUGH*/ |
| 250 | case RTM_RESOLVE: |
| 251 | if (gate->sa_family != AF_LINK || |
| 252 | gate->sa_len < sizeof(struct sockaddr_dl)) { |
| 253 | log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); |
| 254 | break; |
| 255 | } |
| 256 | SDL(gate)->sdl_type = rt->rt_ifp->if_type; |
| 257 | SDL(gate)->sdl_index = rt->rt_ifp->if_index; |
| 258 | if (la != NULL) |
| 259 | break; /* This happens on a route change */ |
| 260 | /* |
| 261 | * Case 2: This route may come from cloning, or a manual route |
| 262 | * add with a LL address. |
| 263 | */ |
| 264 | R_Malloc(la, struct llinfo_arp *, sizeof *la); |
| 265 | rt->rt_llinfo = la; |
| 266 | if (la == NULL) { |
| 267 | log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); |
| 268 | break; |
| 269 | } |
| 270 | bzero(la, sizeof *la); |
| 271 | la->la_rt = rt; |
| 272 | rt->rt_flags |= RTF_LLINFO; |
| 273 | LIST_INSERT_HEAD(&llinfo_arp, la, la_le); |
| 274 | |
| 275 | #ifdef INET |
| 276 | /* |
| 277 | * This keeps the multicast addresses from showing up |
| 278 | * in `arp -a' listings as unresolved. It's not actually |
| 279 | * functional. Then the same for broadcast. |
| 280 | */ |
| 281 | if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && |
| 282 | rt->rt_ifp->if_type != IFT_ARCNET) { |
| 283 | ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, |
| 284 | LLADDR(SDL(gate))); |
| 285 | SDL(gate)->sdl_alen = 6; |
| 286 | rt->rt_expire = 0; |
| 287 | } |
| 288 | if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { |
| 289 | memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, |
| 290 | rt->rt_ifp->if_addrlen); |
| 291 | SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; |
| 292 | rt->rt_expire = 0; |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | if (SIN(rt_key(rt))->sin_addr.s_addr == |
| 297 | (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { |
| 298 | /* |
| 299 | * This test used to be |
| 300 | * if (loif.if_flags & IFF_UP) |
| 301 | * It allowed local traffic to be forced |
| 302 | * through the hardware by configuring the |
| 303 | * loopback down. However, it causes problems |
| 304 | * during network configuration for boards |
| 305 | * that can't receive packets they send. It |
| 306 | * is now necessary to clear "useloopback" and |
| 307 | * remove the route to force traffic out to |
| 308 | * the hardware. |
| 309 | */ |
| 310 | rt->rt_expire = 0; |
| 311 | bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), |
| 312 | SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); |
| 313 | if (useloopback) |
| 314 | rt->rt_ifp = loif; |
| 315 | } |
| 316 | break; |
| 317 | |
| 318 | case RTM_DELETE: |
| 319 | if (la == NULL) |
| 320 | break; |
| 321 | LIST_REMOVE(la, la_le); |
| 322 | rt->rt_llinfo = NULL; |
| 323 | rt->rt_flags &= ~RTF_LLINFO; |
| 324 | if (la->la_hold != NULL) |
| 325 | m_freem(la->la_hold); |
| 326 | Free(la); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Broadcast an ARP request. Caller specifies: |
| 332 | * - arp header source ip address |
| 333 | * - arp header target ip address |
| 334 | * - arp header source ethernet address |
| 335 | */ |
| 336 | static void |
| 337 | arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, |
| 338 | u_char *enaddr) |
| 339 | { |
| 340 | struct mbuf *m; |
| 341 | struct ether_header *eh; |
| 342 | struct arc_header *arh; |
| 343 | struct arphdr *ah; |
| 344 | struct sockaddr sa; |
| 345 | static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, |
| 346 | LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; |
| 347 | u_short ar_hrd; |
| 348 | |
| 349 | if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL) |
| 350 | return; |
| 351 | m->m_pkthdr.rcvif = (struct ifnet *)NULL; |
| 352 | |
| 353 | switch (ifp->if_type) { |
| 354 | case IFT_ARCNET: |
| 355 | ar_hrd = htons(ARPHRD_ARCNET); |
| 356 | |
| 357 | m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); |
| 358 | m->m_pkthdr.len = m->m_len; |
| 359 | MH_ALIGN(m, m->m_len); |
| 360 | |
| 361 | arh = (struct arc_header *)sa.sa_data; |
| 362 | arh->arc_dhost = ifp->if_broadcastaddr[0]; |
| 363 | arh->arc_type = ARCTYPE_ARP; |
| 364 | |
| 365 | ah = mtod(m, struct arphdr *); |
| 366 | break; |
| 367 | |
| 368 | case IFT_ISO88025: |
| 369 | ar_hrd = htons(ARPHRD_IEEE802); |
| 370 | |
| 371 | m->m_len = (sizeof llcx) + |
| 372 | arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); |
| 373 | m->m_pkthdr.len = m->m_len; |
| 374 | MH_ALIGN(m, m->m_len); |
| 375 | |
| 376 | memcpy(mtod(m, caddr_t), llcx, sizeof llcx); |
| 377 | memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen); |
| 378 | memcpy(sa.sa_data + 6, enaddr, 6); |
| 379 | sa.sa_data[6] |= TR_RII; |
| 380 | sa.sa_data[12] = TR_AC; |
| 381 | sa.sa_data[13] = TR_LLC_FRAME; |
| 382 | |
| 383 | ah = (struct arphdr *)(mtod(m, char *) + sizeof llcx); |
| 384 | break; |
| 385 | case IFT_FDDI: |
| 386 | case IFT_ETHER: |
| 387 | /* |
| 388 | * This may not be correct for types not explicitly |
| 389 | * listed, but this is our best guess |
| 390 | */ |
| 391 | default: |
| 392 | ar_hrd = htons(ARPHRD_ETHER); |
| 393 | |
| 394 | m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); |
| 395 | m->m_pkthdr.len = m->m_len; |
| 396 | MH_ALIGN(m, m->m_len); |
| 397 | |
| 398 | eh = (struct ether_header *)sa.sa_data; |
| 399 | /* if_output() will not swap */ |
| 400 | eh->ether_type = htons(ETHERTYPE_ARP); |
| 401 | memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen); |
| 402 | |
| 403 | ah = mtod(m, struct arphdr *); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | ah->ar_hrd = ar_hrd; |
| 408 | ah->ar_pro = htons(ETHERTYPE_IP); |
| 409 | ah->ar_hln = ifp->if_addrlen; /* hardware address length */ |
| 410 | ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ |
| 411 | ah->ar_op = htons(ARPOP_REQUEST); |
| 412 | memcpy(ar_sha(ah), enaddr, ah->ar_hln); |
| 413 | memset(ar_tha(ah), 0, ah->ar_hln); |
| 414 | memcpy(ar_spa(ah), sip, ah->ar_pln); |
| 415 | memcpy(ar_tpa(ah), tip, ah->ar_pln); |
| 416 | |
| 417 | sa.sa_family = AF_UNSPEC; |
| 418 | sa.sa_len = sizeof sa; |
| 419 | (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)NULL); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Resolve an IP address into an ethernet address. If success, |
| 424 | * desten is filled in. If there is no entry in arptab, |
| 425 | * set one up and broadcast a request for the IP address. |
| 426 | * Hold onto this mbuf and resend it once the address |
| 427 | * is finally resolved. A return value of 1 indicates |
| 428 | * that desten has been filled in and the packet should be sent |
| 429 | * normally; a 0 return indicates that the packet has been |
| 430 | * taken over here, either now or for later transmission. |
| 431 | */ |
| 432 | int |
| 433 | arpresolve( |
| 434 | struct ifnet *ifp, |
| 435 | struct rtentry *rt0, |
| 436 | struct mbuf *m, |
| 437 | struct sockaddr *dst, |
| 438 | u_char *desten) |
| 439 | { |
| 440 | struct rtentry *rt; |
| 441 | struct llinfo_arp *la = NULL; |
| 442 | struct sockaddr_dl *sdl; |
| 443 | |
| 444 | if (m->m_flags & M_BCAST) { /* broadcast */ |
| 445 | memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); |
| 446 | return (1); |
| 447 | } |
| 448 | if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ |
| 449 | ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); |
| 450 | return (1); |
| 451 | } |
| 452 | if (rt0 != NULL) { |
| 453 | if (rt_llroute(dst, rt0, &rt) != 0) { |
| 454 | m_freem(m); |
| 455 | return 0; |
| 456 | } |
| 457 | la = rt->rt_llinfo; |
| 458 | } |
| 459 | if (la == NULL) { |
| 460 | la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE); |
| 461 | if (la != NULL) |
| 462 | rt = la->la_rt; |
| 463 | } |
| 464 | if (la == NULL || rt == NULL) { |
| 465 | log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", |
| 466 | inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ", |
| 467 | rt ? "rt" : ""); |
| 468 | m_freem(m); |
| 469 | return (0); |
| 470 | } |
| 471 | sdl = SDL(rt->rt_gateway); |
| 472 | /* |
| 473 | * Check the address family and length is valid, the address |
| 474 | * is resolved; otherwise, try to resolve. |
| 475 | */ |
| 476 | if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && |
| 477 | sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { |
| 478 | /* |
| 479 | * If entry has an expiry time and it is approaching, |
| 480 | * see if we need to send an ARP request within this |
| 481 | * arpt_down interval. |
| 482 | */ |
| 483 | if ((rt->rt_expire != 0) && |
| 484 | (time_second + la->la_preempt > rt->rt_expire)) { |
| 485 | arprequest(ifp, |
| 486 | &SIN(rt->rt_ifa->ifa_addr)->sin_addr, |
| 487 | &SIN(dst)->sin_addr, |
| 488 | IF_LLADDR(ifp)); |
| 489 | la->la_preempt--; |
| 490 | } |
| 491 | |
| 492 | bcopy(LLADDR(sdl), desten, sdl->sdl_alen); |
| 493 | return 1; |
| 494 | } |
| 495 | /* |
| 496 | * If ARP is disabled on this interface, stop. |
| 497 | * XXX |
| 498 | * Probably should not allocate empty llinfo struct if we are |
| 499 | * not going to be sending out an arp request. |
| 500 | */ |
| 501 | if (ifp->if_flags & IFF_NOARP) { |
| 502 | m_freem(m); |
| 503 | return (0); |
| 504 | } |
| 505 | /* |
| 506 | * There is an arptab entry, but no ethernet address |
| 507 | * response yet. Replace the held mbuf with this |
| 508 | * latest one. |
| 509 | */ |
| 510 | if (la->la_hold != NULL) |
| 511 | m_freem(la->la_hold); |
| 512 | la->la_hold = m; |
| 513 | if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) { |
| 514 | rt->rt_flags &= ~RTF_REJECT; |
| 515 | if (la->la_asked == 0 || rt->rt_expire != time_second) { |
| 516 | rt->rt_expire = time_second; |
| 517 | if (la->la_asked++ < arp_maxtries) { |
| 518 | arprequest(ifp, |
| 519 | &SIN(rt->rt_ifa->ifa_addr)->sin_addr, |
| 520 | &SIN(dst)->sin_addr, |
| 521 | IF_LLADDR(ifp)); |
| 522 | } else { |
| 523 | rt->rt_flags |= RTF_REJECT; |
| 524 | rt->rt_expire += arpt_down; |
| 525 | la->la_asked = 0; |
| 526 | la->la_preempt = arp_maxtries; |
| 527 | } |
| 528 | |
| 529 | } |
| 530 | } |
| 531 | return (0); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Common length and type checks are done here, |
| 536 | * then the protocol-specific routine is called. |
| 537 | */ |
| 538 | static int |
| 539 | arpintr(struct netmsg *msg) |
| 540 | { |
| 541 | struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet; |
| 542 | struct arphdr *ar; |
| 543 | u_short ar_hrd; |
| 544 | |
| 545 | if (m->m_len < sizeof(struct arphdr) && |
| 546 | ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { |
| 547 | log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); |
| 548 | goto out2; |
| 549 | } |
| 550 | ar = mtod(m, struct arphdr *); |
| 551 | |
| 552 | ar_hrd = ntohs(ar->ar_hrd); |
| 553 | if (ar_hrd != ARPHRD_ETHER && |
| 554 | ar_hrd != ARPHRD_IEEE802 && |
| 555 | ar_hrd != ARPHRD_ARCNET) { |
| 556 | log(LOG_ERR, |
| 557 | "arp: unknown hardware address format (0x%2D)\n", |
| 558 | (unsigned char *)&ar->ar_hrd, ""); |
| 559 | goto out1; |
| 560 | } |
| 561 | |
| 562 | if (m->m_pkthdr.len < arphdr_len(ar) && |
| 563 | (m = m_pullup(m, arphdr_len(ar))) == NULL) { |
| 564 | log(LOG_ERR, "arp: runt packet\n"); |
| 565 | goto out1; |
| 566 | } |
| 567 | |
| 568 | switch (ntohs(ar->ar_pro)) { |
| 569 | #ifdef INET |
| 570 | case ETHERTYPE_IP: |
| 571 | in_arpinput(m); |
| 572 | goto out2; |
| 573 | #endif |
| 574 | } |
| 575 | out1: |
| 576 | m_freem(m); |
| 577 | out2: |
| 578 | lwkt_replymsg(&msg->nm_lmsg, 0); |
| 579 | return(EASYNC); |
| 580 | } |
| 581 | |
| 582 | #ifdef INET |
| 583 | /* |
| 584 | * ARP for Internet protocols on 10 Mb/s Ethernet. |
| 585 | * Algorithm is that given in RFC 826. |
| 586 | * In addition, a sanity check is performed on the sender |
| 587 | * protocol address, to catch impersonators. |
| 588 | * We no longer handle negotiations for use of trailer protocol: |
| 589 | * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent |
| 590 | * along with IP replies if we wanted trailers sent to us, |
| 591 | * and also sent them in response to IP replies. |
| 592 | * This allowed either end to announce the desire to receive |
| 593 | * trailer packets. |
| 594 | * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, |
| 595 | * but formerly didn't normally send requests. |
| 596 | */ |
| 597 | static int log_arp_wrong_iface = 1; |
| 598 | SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, |
| 599 | &log_arp_wrong_iface, 0, |
| 600 | "log arp packets arriving on the wrong interface"); |
| 601 | |
| 602 | static void |
| 603 | in_arpinput(struct mbuf *m) |
| 604 | { |
| 605 | struct arphdr *ah; |
| 606 | struct ifnet *ifp = m->m_pkthdr.rcvif; |
| 607 | struct ether_header *eh; |
| 608 | struct arc_header *arh; |
| 609 | struct iso88025_header *th = (struct iso88025_header *)NULL; |
| 610 | struct iso88025_sockaddr_dl_data *trld; |
| 611 | struct llinfo_arp *la = NULL; |
| 612 | struct rtentry *rt; |
| 613 | struct ifaddr *ifa; |
| 614 | struct in_ifaddr *ia; |
| 615 | struct sockaddr_dl *sdl; |
| 616 | struct sockaddr sa; |
| 617 | struct in_addr isaddr, itaddr, myaddr; |
| 618 | int op, rif_len; |
| 619 | int req_len; |
| 620 | |
| 621 | req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); |
| 622 | if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { |
| 623 | log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | ah = mtod(m, struct arphdr *); |
| 628 | op = ntohs(ah->ar_op); |
| 629 | memcpy(&isaddr, ar_spa(ah), sizeof isaddr); |
| 630 | memcpy(&itaddr, ar_tpa(ah), sizeof itaddr); |
| 631 | #ifdef BRIDGE |
| 632 | #define BRIDGE_TEST (do_bridge) |
| 633 | #else |
| 634 | #define BRIDGE_TEST (0) /* cc will optimise the test away */ |
| 635 | #endif |
| 636 | /* |
| 637 | * For a bridge, we want to check the address irrespective |
| 638 | * of the receive interface. (This will change slightly |
| 639 | * when we have clusters of interfaces). |
| 640 | */ |
| 641 | LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { |
| 642 | /* |
| 643 | * Old style bridging OBSOLETE |
| 644 | */ |
| 645 | if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && |
| 646 | itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { |
| 647 | goto match; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * New style bridging |
| 652 | */ |
| 653 | if (ifp->if_bridge && ia->ia_ifp && |
| 654 | ifp->if_bridge == ia->ia_ifp->if_bridge && |
| 655 | itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { |
| 656 | goto match; |
| 657 | } |
| 658 | } |
| 659 | LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) { |
| 660 | /* |
| 661 | * Old style bridging OBSOLETE |
| 662 | */ |
| 663 | if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && |
| 664 | isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { |
| 665 | goto match; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * New style bridging |
| 670 | */ |
| 671 | if (ifp->if_bridge && ia->ia_ifp && |
| 672 | ifp->if_bridge == ia->ia_ifp->if_bridge && |
| 673 | isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { |
| 674 | goto match; |
| 675 | } |
| 676 | } |
| 677 | /* |
| 678 | * No match, use the first inet address on the receive interface |
| 679 | * as a dummy address for the rest of the function. |
| 680 | */ |
| 681 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) |
| 682 | if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { |
| 683 | ia = ifatoia(ifa); |
| 684 | goto match; |
| 685 | } |
| 686 | /* |
| 687 | * If bridging, fall back to using any inet address. |
| 688 | * This is probably incorrect, the right way being try to match |
| 689 | * addresses for interfaces in the same cluster, so if we |
| 690 | * get here we should always drop the packet. |
| 691 | */ |
| 692 | if (!BRIDGE_TEST || |
| 693 | (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) { |
| 694 | m_freem(m); |
| 695 | return; |
| 696 | } |
| 697 | match: |
| 698 | myaddr = ia->ia_addr.sin_addr; |
| 699 | if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) { |
| 700 | m_freem(m); /* it's from me, ignore it. */ |
| 701 | return; |
| 702 | } |
| 703 | if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { |
| 704 | log(LOG_ERR, |
| 705 | "arp: link address is broadcast for IP address %s!\n", |
| 706 | inet_ntoa(isaddr)); |
| 707 | m_freem(m); |
| 708 | return; |
| 709 | } |
| 710 | if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { |
| 711 | log(LOG_ERR, |
| 712 | "arp: %*D is using my IP address %s!\n", |
| 713 | ifp->if_addrlen, (u_char *)ar_sha(ah), ":", |
| 714 | inet_ntoa(isaddr)); |
| 715 | itaddr = myaddr; |
| 716 | goto reply; |
| 717 | } |
| 718 | la = arplookup(isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr), FALSE); |
| 719 | if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { |
| 720 | /* the following is not an error when doing bridging */ |
| 721 | if (!BRIDGE_TEST && rt->rt_ifp != ifp) { |
| 722 | if (log_arp_wrong_iface) |
| 723 | log(LOG_ERR, |
| 724 | "arp: %s is on %s but got reply from %*D on %s\n", |
| 725 | inet_ntoa(isaddr), |
| 726 | rt->rt_ifp->if_xname, |
| 727 | ifp->if_addrlen, (u_char *)ar_sha(ah), ":", |
| 728 | ifp->if_xname); |
| 729 | goto reply; |
| 730 | } |
| 731 | if (sdl->sdl_alen && |
| 732 | bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { |
| 733 | if (rt->rt_expire) |
| 734 | log(LOG_INFO, |
| 735 | "arp: %s moved from %*D to %*D on %s\n", |
| 736 | inet_ntoa(isaddr), |
| 737 | ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", |
| 738 | ifp->if_addrlen, (u_char *)ar_sha(ah), ":", |
| 739 | ifp->if_xname); |
| 740 | else { |
| 741 | log(LOG_ERR, |
| 742 | "arp: %*D attempts to modify permanent entry for %s on %s\n", |
| 743 | ifp->if_addrlen, (u_char *)ar_sha(ah), ":", |
| 744 | inet_ntoa(isaddr), ifp->if_xname); |
| 745 | goto reply; |
| 746 | } |
| 747 | } |
| 748 | /* |
| 749 | * sanity check for the address length. |
| 750 | * XXX this does not work for protocols with variable address |
| 751 | * length. -is |
| 752 | */ |
| 753 | if (sdl->sdl_alen && |
| 754 | sdl->sdl_alen != ah->ar_hln) { |
| 755 | log(LOG_WARNING, |
| 756 | "arp from %*D: new addr len %d, was %d", |
| 757 | ifp->if_addrlen, (u_char *) ar_sha(ah), ":", |
| 758 | ah->ar_hln, sdl->sdl_alen); |
| 759 | } |
| 760 | if (ifp->if_addrlen != ah->ar_hln) { |
| 761 | log(LOG_WARNING, |
| 762 | "arp from %*D: addr len: new %d, i/f %d (ignored)", |
| 763 | ifp->if_addrlen, (u_char *) ar_sha(ah), ":", |
| 764 | ah->ar_hln, ifp->if_addrlen); |
| 765 | goto reply; |
| 766 | } |
| 767 | memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln); |
| 768 | /* |
| 769 | * If we receive an arp from a token-ring station over |
| 770 | * a token-ring nic then try to save the source |
| 771 | * routing info. |
| 772 | */ |
| 773 | if (ifp->if_type == IFT_ISO88025) { |
| 774 | th = (struct iso88025_header *)m->m_pkthdr.header; |
| 775 | trld = SDL_ISO88025(sdl); |
| 776 | rif_len = TR_RCF_RIFLEN(th->rcf); |
| 777 | if ((th->iso88025_shost[0] & TR_RII) && |
| 778 | (rif_len > 2)) { |
| 779 | trld->trld_rcf = th->rcf; |
| 780 | trld->trld_rcf ^= htons(TR_RCF_DIR); |
| 781 | memcpy(trld->trld_route, th->rd, rif_len - 2); |
| 782 | trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); |
| 783 | /* |
| 784 | * Set up source routing information for |
| 785 | * reply packet (XXX) |
| 786 | */ |
| 787 | m->m_data -= rif_len; |
| 788 | m->m_len += rif_len; |
| 789 | m->m_pkthdr.len += rif_len; |
| 790 | } else { |
| 791 | th->iso88025_shost[0] &= ~TR_RII; |
| 792 | trld->trld_rcf = 0; |
| 793 | } |
| 794 | m->m_data -= 8; |
| 795 | m->m_len += 8; |
| 796 | m->m_pkthdr.len += 8; |
| 797 | th->rcf = trld->trld_rcf; |
| 798 | } |
| 799 | if (rt->rt_expire) |
| 800 | rt->rt_expire = time_second + arpt_keep; |
| 801 | rt->rt_flags &= ~RTF_REJECT; |
| 802 | la->la_asked = 0; |
| 803 | la->la_preempt = arp_maxtries; |
| 804 | if (la->la_hold != NULL) { |
| 805 | m_adj(la->la_hold, sizeof(struct ether_header)); |
| 806 | lwkt_serialize_enter(ifp->if_serializer); |
| 807 | (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt); |
| 808 | lwkt_serialize_exit(ifp->if_serializer); |
| 809 | la->la_hold = NULL; |
| 810 | } |
| 811 | } |
| 812 | reply: |
| 813 | if (op != ARPOP_REQUEST) { |
| 814 | m_freem(m); |
| 815 | return; |
| 816 | } |
| 817 | if (itaddr.s_addr == myaddr.s_addr) { |
| 818 | /* I am the target */ |
| 819 | memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); |
| 820 | memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); |
| 821 | } else { |
| 822 | la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY); |
| 823 | if (la == NULL) { |
| 824 | struct sockaddr_in sin; |
| 825 | |
| 826 | if (!arp_proxyall) { |
| 827 | m_freem(m); |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | bzero(&sin, sizeof sin); |
| 832 | sin.sin_family = AF_INET; |
| 833 | sin.sin_len = sizeof sin; |
| 834 | sin.sin_addr = itaddr; |
| 835 | |
| 836 | rt = rtpurelookup((struct sockaddr *)&sin); |
| 837 | if (rt == NULL) { |
| 838 | m_freem(m); |
| 839 | return; |
| 840 | } |
| 841 | --rt->rt_refcnt; |
| 842 | /* |
| 843 | * Don't send proxies for nodes on the same interface |
| 844 | * as this one came out of, or we'll get into a fight |
| 845 | * over who claims what Ether address. |
| 846 | */ |
| 847 | if (rt->rt_ifp == ifp) { |
| 848 | m_freem(m); |
| 849 | return; |
| 850 | } |
| 851 | memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); |
| 852 | memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); |
| 853 | #ifdef DEBUG_PROXY |
| 854 | printf("arp: proxying for %s\n", inet_ntoa(itaddr)); |
| 855 | #endif |
| 856 | } else { |
| 857 | rt = la->la_rt; |
| 858 | memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); |
| 859 | sdl = SDL(rt->rt_gateway); |
| 860 | memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); |
| 865 | memcpy(ar_spa(ah), &itaddr, ah->ar_pln); |
| 866 | ah->ar_op = htons(ARPOP_REPLY); |
| 867 | ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ |
| 868 | switch (ifp->if_type) { |
| 869 | case IFT_ARCNET: |
| 870 | arh = (struct arc_header *)sa.sa_data; |
| 871 | arh->arc_dhost = *ar_tha(ah); |
| 872 | arh->arc_type = ARCTYPE_ARP; |
| 873 | break; |
| 874 | case IFT_ISO88025: |
| 875 | /* Re-arrange the source/dest address */ |
| 876 | memcpy(th->iso88025_dhost, th->iso88025_shost, |
| 877 | sizeof th->iso88025_dhost); |
| 878 | memcpy(th->iso88025_shost, IF_LLADDR(ifp), |
| 879 | sizeof th->iso88025_shost); |
| 880 | /* Set the source routing bit if neccesary */ |
| 881 | if (th->iso88025_dhost[0] & TR_RII) { |
| 882 | th->iso88025_dhost[0] &= ~TR_RII; |
| 883 | if (TR_RCF_RIFLEN(th->rcf) > 2) |
| 884 | th->iso88025_shost[0] |= TR_RII; |
| 885 | } |
| 886 | /* Copy the addresses, ac and fc into sa_data */ |
| 887 | memcpy(sa.sa_data, th->iso88025_dhost, |
| 888 | (sizeof th->iso88025_dhost) * 2); |
| 889 | sa.sa_data[(sizeof th->iso88025_dhost) * 2] = TR_AC; |
| 890 | sa.sa_data[(sizeof th->iso88025_dhost) * 2 + 1] = TR_LLC_FRAME; |
| 891 | break; |
| 892 | case IFT_ETHER: |
| 893 | case IFT_FDDI: |
| 894 | /* |
| 895 | * May not be correct for types not explictly |
| 896 | * listed, but it is our best guess. |
| 897 | */ |
| 898 | default: |
| 899 | eh = (struct ether_header *)sa.sa_data; |
| 900 | memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost); |
| 901 | eh->ether_type = htons(ETHERTYPE_ARP); |
| 902 | break; |
| 903 | } |
| 904 | sa.sa_family = AF_UNSPEC; |
| 905 | sa.sa_len = sizeof sa; |
| 906 | lwkt_serialize_enter(ifp->if_serializer); |
| 907 | (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); |
| 908 | lwkt_serialize_exit(ifp->if_serializer); |
| 909 | return; |
| 910 | } |
| 911 | #endif |
| 912 | |
| 913 | /* |
| 914 | * Free an arp entry. If the arp entry is actively referenced or represents |
| 915 | * a static entry we only clear it back to an unresolved state, otherwise |
| 916 | * we destroy the entry entirely. |
| 917 | * |
| 918 | * Note that static entries are created when route add ... -interface is used |
| 919 | * to create an interface route to a (direct) destination. |
| 920 | */ |
| 921 | static void |
| 922 | arptfree(struct llinfo_arp *la) |
| 923 | { |
| 924 | struct rtentry *rt = la->la_rt; |
| 925 | struct sockaddr_dl *sdl; |
| 926 | |
| 927 | if (rt == NULL) |
| 928 | panic("arptfree"); |
| 929 | sdl = SDL(rt->rt_gateway); |
| 930 | if (sdl != NULL && |
| 931 | ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) || |
| 932 | (rt->rt_flags & RTF_STATIC))) { |
| 933 | sdl->sdl_alen = 0; |
| 934 | la->la_preempt = la->la_asked = 0; |
| 935 | rt->rt_flags &= ~RTF_REJECT; |
| 936 | return; |
| 937 | } |
| 938 | rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL); |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Lookup or enter a new address in arptab. |
| 943 | */ |
| 944 | static struct llinfo_arp * |
| 945 | arplookup(in_addr_t addr, boolean_t create, boolean_t proxy) |
| 946 | { |
| 947 | struct rtentry *rt; |
| 948 | struct sockaddr_inarp sin = { sizeof sin, AF_INET }; |
| 949 | const char *why = NULL; |
| 950 | |
| 951 | sin.sin_addr.s_addr = addr; |
| 952 | sin.sin_other = proxy ? SIN_PROXY : 0; |
| 953 | if (create) |
| 954 | rt = rtlookup((struct sockaddr *)&sin); |
| 955 | else |
| 956 | rt = rtpurelookup((struct sockaddr *)&sin); |
| 957 | if (rt == NULL) |
| 958 | return (NULL); |
| 959 | rt->rt_refcnt--; |
| 960 | |
| 961 | if (rt->rt_flags & RTF_GATEWAY) |
| 962 | why = "host is not on local network"; |
| 963 | else if (!(rt->rt_flags & RTF_LLINFO)) |
| 964 | why = "could not allocate llinfo"; |
| 965 | else if (rt->rt_gateway->sa_family != AF_LINK) |
| 966 | why = "gateway route is not ours"; |
| 967 | |
| 968 | if (why) { |
| 969 | if (create) { |
| 970 | log(LOG_DEBUG, "arplookup %s failed: %s\n", |
| 971 | inet_ntoa(sin.sin_addr), why); |
| 972 | } |
| 973 | if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) { |
| 974 | /* No references to this route. Purge it. */ |
| 975 | rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, |
| 976 | rt_mask(rt), rt->rt_flags, NULL); |
| 977 | } |
| 978 | return (NULL); |
| 979 | } |
| 980 | return (rt->rt_llinfo); |
| 981 | } |
| 982 | |
| 983 | void |
| 984 | arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) |
| 985 | { |
| 986 | if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) |
| 987 | arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr, |
| 988 | IF_LLADDR(ifp)); |
| 989 | ifa->ifa_rtrequest = arp_rtrequest; |
| 990 | ifa->ifa_flags |= RTF_CLONING; |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | arp_init(void) |
| 995 | { |
| 996 | LIST_INIT(&llinfo_arp); |
| 997 | netisr_register(NETISR_ARP, cpu0_portfn, arpintr); |
| 998 | } |
| 999 | |
| 1000 | SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); |