📄 if_ether.c
字号:
/* * Copyright (c) 1982, 1986, 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 * $Id: if_ether.c,v 1.2 2003/01/03 18:09:25 joel Exp $ *//* * Ethernet address resolution protocol. * TODO: * add "inuse/lock" bit (or ref. count) along with valid bit */#include <sys/param.h>#include <sys/kernel.h>#include <sys/sysctl.h>#include <sys/queue.h>#include <sys/systm.h>#include <sys/mbuf.h>#include <sys/malloc.h>#include <sys/socket.h>#include <sys/syslog.h>#include <net/if.h>#include <net/if_dl.h>#include <net/route.h>#include <net/netisr.h>#include <netinet/in.h>#include <netinet/in_var.h>#include <netinet/if_ether.h>#define SIN(s) ((struct sockaddr_in *)s)#define SDL(s) ((struct sockaddr_dl *)s)SYSCTL_DECL(_net_link_ether);SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");/* timer values */static int arpt_prune = (5*60*1); /* walk list every 5 minutes */static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */static int arpt_down = 20; /* once declared down, don't send for 20 sec */SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, &arpt_prune, 0, "");SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, &arpt_keep, 0, "");SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, &arpt_down, 0, "");#define rt_expire rt_rmx.rmx_expirestruct llinfo_arp { LIST_ENTRY(llinfo_arp) la_le; struct rtentry *la_rt; struct mbuf *la_hold; /* last packet until resolved/timeout */ long la_asked; /* last time we QUERIED for this addr */#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */};static LIST_HEAD(, llinfo_arp) llinfo_arp;struct ifqueue arpintrq = {0, 0, 0, 50};static int arp_inuse, arp_allocated;static int arp_maxtries = 5;static int useloopback = 1; /* use loopback interface for local traffic */static int arp_proxyall = 0;SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, &arp_maxtries, 0, "");SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, &useloopback, 0, "");SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, &arp_proxyall, 0, "");static void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));static void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));void arpintr __P((void));static void arptfree __P((struct llinfo_arp *));static void arptimer __P((void *));static struct llinfo_arp *arplookup __P((u_long, int, int));static void in_arpinput __P((struct mbuf *));/* * Timeout routine. Age arp_tab entries periodically. *//* ARGSUSED */static voidarptimer(ignored_arg) void *ignored_arg;{ int s = splnet(); register struct llinfo_arp *la = llinfo_arp.lh_first; struct llinfo_arp *ola; timeout(arptimer, (caddr_t)0, arpt_prune * hz); while ((ola = la) != 0) { register struct rtentry *rt = la->la_rt; la = la->la_le.le_next; if (rt->rt_expire && rt->rt_expire <= rtems_bsdnet_seconds_since_boot()) arptfree(ola); /* timer has expired, clear */ } splx(s);}/* * Parallel to llc_rtrequest. */static voidarp_rtrequest(req, rt, sa) int req; register struct rtentry *rt; struct sockaddr *sa;{ register struct sockaddr *gate = rt->rt_gateway; register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; static int arpinit_done; if (!arpinit_done) { arpinit_done = 1; LIST_INIT(&llinfo_arp); timeout(arptimer, (caddr_t)0, hz); } if (rt->rt_flags & RTF_GATEWAY) return; switch (req) { case RTM_ADD: /* * XXX: If this is a manually added route to interface * such as older version of routed or gated might provide, * restore cloning bit. */ if ((rt->rt_flags & RTF_HOST) == 0 && SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) rt->rt_flags |= RTF_CLONING; if (rt->rt_flags & RTF_CLONING) { /* * Case 1: This route should come from a route to iface. */ rt_setgate(rt, rt_key(rt), (struct sockaddr *)&null_sdl); gate = rt->rt_gateway; SDL(gate)->sdl_type = rt->rt_ifp->if_type; SDL(gate)->sdl_index = rt->rt_ifp->if_index; rt->rt_expire = rtems_bsdnet_seconds_since_boot(); break; } /* Announce a new entry if requested. */ if (rt->rt_flags & RTF_ANNOUNCE) arprequest((struct arpcom *)rt->rt_ifp, &SIN(rt_key(rt))->sin_addr.s_addr, &SIN(rt_key(rt))->sin_addr.s_addr, (u_char *)LLADDR(SDL(gate))); /*FALLTHROUGH*/ case RTM_RESOLVE: if (gate->sa_family != AF_LINK || gate->sa_len < sizeof(null_sdl)) { log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); break; } SDL(gate)->sdl_type = rt->rt_ifp->if_type; SDL(gate)->sdl_index = rt->rt_ifp->if_index; if (la != 0) break; /* This happens on a route change */ /* * Case 2: This route may come from cloning, or a manual route * add with a LL address. */ R_Malloc(la, struct llinfo_arp *, sizeof(*la)); rt->rt_llinfo = (caddr_t)la; if (la == 0) { log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); break; } arp_inuse++, arp_allocated++; Bzero(la, sizeof(*la)); la->la_rt = rt; rt->rt_flags |= RTF_LLINFO; LIST_INSERT_HEAD(&llinfo_arp, la, la_le); /* * This keeps the multicast addresses from showing up * in `arp -a' listings as unresolved. It's not actually * functional. Then the same for broadcast. */ if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, LLADDR(SDL(gate))); SDL(gate)->sdl_alen = 6; rt->rt_expire = 0; } if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6); SDL(gate)->sdl_alen = 6; rt->rt_expire = 0; } if (SIN(rt_key(rt))->sin_addr.s_addr == (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { /* * This test used to be * if (loif.if_flags & IFF_UP) * It allowed local traffic to be forced * through the hardware by configuring the loopback down. * However, it causes problems during network configuration * for boards that can't receive packets they send. * It is now necessary to clear "useloopback" and remove * the route to force traffic out to the hardware. */ rt->rt_expire = 0; Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); if (useloopback) rt->rt_ifp = loif; } break; case RTM_DELETE: if (la == 0) break; arp_inuse--; LIST_REMOVE(la, la_le); rt->rt_llinfo = 0; rt->rt_flags &= ~RTF_LLINFO; if (la->la_hold) m_freem(la->la_hold); Free((caddr_t)la); }}/* * Broadcast an ARP request. Caller specifies: * - arp header source ip address * - arp header target ip address * - arp header source ethernet address */static voidarprequest(ac, sip, tip, enaddr) register struct arpcom *ac; register u_long *sip, *tip; register u_char *enaddr;{ register struct mbuf *m; register struct ether_header *eh; register struct ether_arp *ea; struct sockaddr sa; if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) return; m->m_len = sizeof(*ea); m->m_pkthdr.len = sizeof(*ea); MH_ALIGN(m, sizeof(*ea)); ea = mtod(m, struct ether_arp *); eh = (struct ether_header *)sa.sa_data; bzero((caddr_t)ea, sizeof (*ea)); (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost)); eh->ether_type = htons(ETHERTYPE_ARP); /* if_output will not swap */ ea->arp_hrd = htons(ARPHRD_ETHER); ea->arp_pro = htons(ETHERTYPE_IP); ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ ea->arp_op = htons(ARPOP_REQUEST); (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); sa.sa_family = AF_UNSPEC; sa.sa_len = sizeof(sa); (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);}/* * Resolve an IP address into an ethernet address. If success, * desten is filled in. If there is no entry in arptab, * set one up and broadcast a request for the IP address. * Hold onto this mbuf and resend it once the address * is finally resolved. A return value of 1 indicates * that desten has been filled in and the packet should be sent * normally; a 0 return indicates that the packet has been * taken over here, either now or for later transmission. */intarpresolve(ac, rt, m, dst, desten, rt0) register struct arpcom *ac; register struct rtentry *rt; struct mbuf *m; register struct sockaddr *dst;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -