📄 if_ether.c
字号:
sdl = SDL (rt->rt_gwroute->rt_gateway); } else sdl = SDL (rt->rt_gateway); if (sdl->sdl_family != AF_LINK) return (EINVAL); } else return (ENETUNREACH); bzero ( (char *)&rtmask, sizeof (rtmask)); rtmask.sin_len = 8; rtmask.sin_addr.s_addr = 0xffffffff; bzero ( (char *)&ipaddr, sizeof (ipaddr)); ipaddr.sin_len = sizeof (ipaddr); ipaddr.sin_family = AF_INET; ipaddr.sin_addr.s_addr = soInAddr->sin_addr.s_addr; bzero ( (char *)&arpaddr, sizeof (arpaddr)); arpaddr.sdl_len = sizeof (arpaddr); arpaddr.sdl_family = AF_LINK; arpaddr.sdl_type = sdl->sdl_type; arpaddr.sdl_index = sdl->sdl_index; bcopy((caddr_t)ar->arp_ha.sa_data, LLADDR(&arpaddr), arpaddr.sdl_alen = sizeof(struct ether_addr)); flags |= (RTF_HOST | RTF_STATIC); /* * A netmask of 0 compares each route entry against the entire * key during lookups. If an overlapping network route already * exists, set it to all ones so that queries with SIN_PROXY set * will still succeed. If a matching host route exists, set the * SIN_PROXY flag in the new entry instead. */ pMask = NULL; if (proxy) { if (export) ipaddr.sin_other = SIN_PROXY; else { pMask = &rtmask; flags &= ~RTF_HOST; } } error = rtrequest (RTM_ADD, (struct sockaddr *)&ipaddr, (struct sockaddr *)&arpaddr, (struct sockaddr *)pMask, flags, &pNewRt); if (error == 0 && pNewRt) { if (ar->arp_flags & ATF_PERM) pNewRt->rt_expire = 0; else pNewRt->rt_expire = tickGet() + (sysClkRateGet() * arpt_keep); pNewRt->rt_refcnt--; } break; case SIOCDARP: /* delete entry */ if ((la = arplookup(soInAddr->sin_addr.s_addr, 0, 0)) == NULL) return (EADDRNOTAVAIL); arptfree(la); break; case SIOCGARP: /* get entry */ if ((la = arplookup(soInAddr->sin_addr.s_addr, 0, 0)) == NULL) return (EADDRNOTAVAIL); rt = la->la_rt; sdl = SDL(rt->rt_gateway); ar->arp_flags = 0; /* initialize the flags */ if (sdl->sdl_alen) { bcopy(LLADDR(sdl), (caddr_t)ar->arp_ha.sa_data, sdl->sdl_alen); ar->arp_flags |= ATF_COM; } else return (EADDRNOTAVAIL); if (rt->rt_flags & RTF_UP) ar->arp_flags |= ATF_INUSE; if (rt->rt_flags & RTF_ANNOUNCE) ar->arp_flags |= ATF_PUBL; if (rt->rt_expire == 0) ar->arp_flags |= ATF_PERM; break; default : return (EINVAL); } return (error); }/* * Convert Ethernet address to printable (loggable) representation. */char *ether_sprintf(ap) register u_char *ap;{ register i; static char etherbuf[18]; register char *cp = etherbuf; for (i = 0; i < 6; i++) { *cp++ = digits[*ap >> 4]; *cp++ = digits[*ap++ & 0xf]; *cp++ = ':'; } *--cp = 0; return (etherbuf);}#ifdef INCLUDE_REVARP /* XXX currently not supported *//* * Called from 10 Mb/s Ethernet interrupt handlers * when ether packet type ETHERTYPE_REVARP * is received. Common length and type checks are done here, * then the protocol-specific routine is called. */voidrevarpinput(m) struct mbuf *m;{ struct arphdr *ar; int op, s; if (m->m_len < sizeof(struct arphdr)) goto out; ar = mtod(m, struct arphdr *); if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) goto out; if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln)) goto out; switch (ntohs(ar->ar_pro)) { case ETHERTYPE_IP: case ETHERTYPE_IPTRAILERS: in_revarpinput(m); return; default: break; }out: m_freem(m);}/* * RARP for Internet protocols on 10 Mb/s Ethernet. * Algorithm is that given in RFC 903. * We are only using for bootstrap purposes to get an ip address for one of * our interfaces. Thus we support no user-interface. * * Since the contents of the RARP reply are specific to the interface that * sent the request, this code must ensure that they are properly associated. * * Note: also supports ARP via RARP packets, per the RFC. */in_revarpinput(m) struct mbuf *m;{ struct ifnet *ifp; struct ether_arp *ar; int op, s; ar = mtod(m, struct ether_arp *); op = ntohs(ar->arp_op); switch (op) { case ARPOP_REQUEST: case ARPOP_REPLY: /* per RFC */ in_arpinput(m); return; case ARPOP_REVREPLY: break; case ARPOP_REVREQUEST: /* handled by rarpd(8) */ default: goto out; } if (!revarp_in_progress) goto out; ifp = m->m_pkthdr.rcvif; if (ifp != myip_ifp) /* !same interface */ goto out; if (myip_initialized) goto wake; if (bcmp((char *)ar->arp_tha, (char *)((struct arpcom *)ifp)->ac_enaddr, sizeof(ar->arp_tha))) goto out; bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip)); bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip)); myip_initialized = 1;wake: /* Do wakeup every time in case it was missed. */#if 0 /* XXX to be checked */ wakeup((caddr_t)&myip);#endif /* XXX to be checked */out: m_freem(m);}/* * Send a RARP request for the ip address of the specified interface. * The request should be RFC 903-compliant. */voidrevarprequest(ifp) struct ifnet *ifp;{ struct sockaddr sa; struct mbuf *m; struct ether_header *eh; struct ether_arp *ea; struct arpcom *ac = (struct arpcom *)ifp; if ((m = mHdrClGet(M_DONTWAIT, MT_DATA, sizeof(*ea), TRUE)) == 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)); bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, sizeof(eh->ether_dhost)); eh->ether_type = htons(ETHERTYPE_REVARP); 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_REVREQUEST); bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha)); bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha, sizeof(ea->arp_tha)); sa.sa_family = AF_UNSPEC; sa.sa_len = sizeof(sa); ifp->if_output(ifp, m, &sa, (struct rtentry *)0);}/* * RARP for the ip address of the specified interface, but also * save the ip address of the server that sent the answer. * Timeout if no response is received. */intrevarpwhoarewe(ifp, serv_in, clnt_in) struct ifnet *ifp; struct in_addr *serv_in; struct in_addr *clnt_in;{ int result, count = 20; if (myip_initialized) return EIO; myip_ifp = ifp; revarp_in_progress = 1; while (count--) { revarprequest(ifp); /* one second delay */ taskDelay (sysClkRateGet());#if 0 /* XXX WORKAROUND with TAskDelay provided is it OK??? */ result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2); if (result != EWOULDBLOCK) break;#endif /* XXX */ } revarp_in_progress = 0; if (!myip_initialized) return ENETUNREACH; bcopy((caddr_t)&srv_ip, (char *)serv_in, sizeof(*serv_in)); bcopy((caddr_t)&myip, (char *)clnt_in, sizeof(*clnt_in)); return 0;}/* For compatibility: only saves interface address. */intrevarpwhoami(in, ifp) struct in_addr *in; struct ifnet *ifp;{ struct in_addr server; return (revarpwhoarewe(ifp, &server, in));}#endif /* INCLUDE_REVARP XXX currently not supported */#define db_printf printfvoiddb_print_sa(sa) struct sockaddr *sa;{ int len; u_char *p; if (sa == 0) { db_printf("[NULL]"); return; } p = (u_char*)sa; len = sa->sa_len; db_printf("["); while (len > 0) { db_printf("%d", *p); p++; len--; if (len) db_printf(","); } db_printf("]\n");}#ifdef DDBstatic voiddb_print_ifa(ifa) struct ifaddr *ifa;{ if (ifa == 0) return; db_printf(" ifa_addr="); db_print_sa(ifa->ifa_addr); db_printf(" ifa_dsta="); db_print_sa(ifa->ifa_dstaddr); db_printf(" ifa_mask="); db_print_sa(ifa->ifa_netmask); db_printf(" flags=0x%x,refcnt=%d,metric=%d\n", ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric);}static voiddb_print_llinfo(li) caddr_t li;{ struct llinfo_arp *la; if (li == 0) return; la = (struct llinfo_arp *)li; db_printf(" la_rt=0x%x la_hold=0x%x, la_asked=0x%x\n", la->la_rt, la->la_hold, la->la_asked);}/* * Function to pass to rn_walktree(). * Return non-zero error to abort walk. */static intdb_show_radix_node(rn, w) struct radix_node *rn; void *w;{ struct rtentry *rt = (struct rtentry *)rn; db_printf("rtentry=0x%x", rt); db_printf(" flags=0x%x refcnt=%d use=%d expire=%d\n", rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire); db_printf(" key="); db_print_sa(rt_key(rt)); db_printf(" mask="); db_print_sa(rt_mask(rt)); db_printf(" gw="); db_print_sa(rt->rt_gateway); db_printf(" ifp=0x%x ", rt->rt_ifp); if (rt->rt_ifp) db_printf("(%s%d)", rt->rt_ifp->if_name, rt->rt_ifp->if_unit); else db_printf("(NULL)"); db_printf(" ifa=0x%x\n", rt->rt_ifa); db_print_ifa(rt->rt_ifa); db_printf(" genmask="); db_print_sa(rt->rt_genmask); db_printf(" gwroute=0x%x llinfo=0x%x\n", rt->rt_gwroute, rt->rt_llinfo); db_print_llinfo(rt->rt_llinfo); return (0);}/* * Function to print all the route trees. * Use this from ddb: "call db_show_arptab" */db_show_arptab(){ struct radix_node_head *rnh; rnh = rt_tables[AF_INET]; db_printf("Route tree for AF_INET\n"); if (rnh == NULL) { db_printf(" (not initialized)\n"); return (0); } rn_walktree(rnh, db_show_radix_node, NULL); return (0);}#endif#endif /* INET */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -