17790c8c1SConrad Meyer /*- 27790c8c1SConrad Meyer * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 37790c8c1SConrad Meyer * 47790c8c1SConrad Meyer * Copyright (c) 2019 Isilon Systems, LLC. 57790c8c1SConrad Meyer * Copyright (c) 2005-2014 Sandvine Incorporated. All rights reserved. 67790c8c1SConrad Meyer * Copyright (c) 2000 Darrell Anderson 77790c8c1SConrad Meyer * All rights reserved. 87790c8c1SConrad Meyer * 97790c8c1SConrad Meyer * Redistribution and use in source and binary forms, with or without 107790c8c1SConrad Meyer * modification, are permitted provided that the following conditions 117790c8c1SConrad Meyer * are met: 127790c8c1SConrad Meyer * 1. Redistributions of source code must retain the above copyright 137790c8c1SConrad Meyer * notice, this list of conditions and the following disclaimer. 147790c8c1SConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright 157790c8c1SConrad Meyer * notice, this list of conditions and the following disclaimer in the 167790c8c1SConrad Meyer * documentation and/or other materials provided with the distribution. 177790c8c1SConrad Meyer * 187790c8c1SConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 197790c8c1SConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 207790c8c1SConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 217790c8c1SConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 227790c8c1SConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 237790c8c1SConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 247790c8c1SConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 257790c8c1SConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 267790c8c1SConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 277790c8c1SConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 287790c8c1SConrad Meyer * SUCH DAMAGE. 297790c8c1SConrad Meyer */ 307790c8c1SConrad Meyer 317790c8c1SConrad Meyer #include <sys/cdefs.h> 327790c8c1SConrad Meyer __FBSDID("$FreeBSD$"); 337790c8c1SConrad Meyer 347790c8c1SConrad Meyer #include "opt_inet.h" 357790c8c1SConrad Meyer 367790c8c1SConrad Meyer #include <sys/param.h> 377790c8c1SConrad Meyer #include <sys/systm.h> 387790c8c1SConrad Meyer #include <sys/errno.h> 397790c8c1SConrad Meyer #include <sys/socket.h> 407790c8c1SConrad Meyer #include <sys/sysctl.h> 417790c8c1SConrad Meyer 427790c8c1SConrad Meyer #include <net/ethernet.h> 437790c8c1SConrad Meyer #include <net/if.h> 447790c8c1SConrad Meyer #include <net/if_arp.h> 457790c8c1SConrad Meyer #include <net/if_dl.h> 467790c8c1SConrad Meyer #include <net/if_types.h> 477790c8c1SConrad Meyer #include <net/if_var.h> 487790c8c1SConrad Meyer 497790c8c1SConrad Meyer #include <netinet/in.h> 507790c8c1SConrad Meyer #include <netinet/in_systm.h> 517790c8c1SConrad Meyer #include <netinet/in_var.h> 527790c8c1SConrad Meyer #include <netinet/ip.h> 537790c8c1SConrad Meyer #include <netinet/ip_var.h> 547790c8c1SConrad Meyer #include <netinet/ip_options.h> 557790c8c1SConrad Meyer #include <netinet/udp.h> 567790c8c1SConrad Meyer #include <netinet/udp_var.h> 577790c8c1SConrad Meyer 587790c8c1SConrad Meyer #include <machine/in_cksum.h> 597790c8c1SConrad Meyer #include <machine/pcb.h> 607790c8c1SConrad Meyer 617790c8c1SConrad Meyer #include <net/debugnet.h> 627790c8c1SConrad Meyer #define DEBUGNET_INTERNAL 637790c8c1SConrad Meyer #include <net/debugnet_int.h> 647790c8c1SConrad Meyer 657790c8c1SConrad Meyer int debugnet_arp_nretries = 3; 667790c8c1SConrad Meyer SYSCTL_INT(_net_debugnet, OID_AUTO, arp_nretries, CTLFLAG_RWTUN, 677790c8c1SConrad Meyer &debugnet_arp_nretries, 0, 687790c8c1SConrad Meyer "Number of ARP attempts before giving up"); 697790c8c1SConrad Meyer 707790c8c1SConrad Meyer /* 717790c8c1SConrad Meyer * Handler for IP packets: checks their sanity and then processes any debugnet 727790c8c1SConrad Meyer * ACK packets it finds. 737790c8c1SConrad Meyer * 747790c8c1SConrad Meyer * It needs to partially replicate the behaviour of ip_input() and udp_input(). 757790c8c1SConrad Meyer * 767790c8c1SConrad Meyer * Parameters: 777790c8c1SConrad Meyer * pcb a pointer to the live debugnet PCB 787790c8c1SConrad Meyer * mb a pointer to an mbuf * containing the packet received 797790c8c1SConrad Meyer * Updates *mb if m_pullup et al change the pointer 807790c8c1SConrad Meyer * Assumes the calling function will take care of freeing the mbuf 817790c8c1SConrad Meyer */ 827790c8c1SConrad Meyer void 837790c8c1SConrad Meyer debugnet_handle_ip(struct debugnet_pcb *pcb, struct mbuf **mb) 847790c8c1SConrad Meyer { 857790c8c1SConrad Meyer struct ip *ip; 867790c8c1SConrad Meyer struct mbuf *m; 877790c8c1SConrad Meyer unsigned short hlen; 887790c8c1SConrad Meyer 89*7cbf1de3SBryan Drewery if (pcb->dp_state < DN_STATE_HAVE_GW_MAC) 90*7cbf1de3SBryan Drewery return; 91*7cbf1de3SBryan Drewery 927790c8c1SConrad Meyer /* IP processing. */ 937790c8c1SConrad Meyer m = *mb; 947790c8c1SConrad Meyer if (m->m_pkthdr.len < sizeof(struct ip)) { 957790c8c1SConrad Meyer DNETDEBUG("dropping packet too small for IP header\n"); 967790c8c1SConrad Meyer return; 977790c8c1SConrad Meyer } 987790c8c1SConrad Meyer if (m->m_len < sizeof(struct ip)) { 997790c8c1SConrad Meyer m = m_pullup(m, sizeof(struct ip)); 1007790c8c1SConrad Meyer *mb = m; 1017790c8c1SConrad Meyer if (m == NULL) { 1027790c8c1SConrad Meyer DNETDEBUG("m_pullup failed\n"); 1037790c8c1SConrad Meyer return; 1047790c8c1SConrad Meyer } 1057790c8c1SConrad Meyer } 1067790c8c1SConrad Meyer ip = mtod(m, struct ip *); 1077790c8c1SConrad Meyer 1087790c8c1SConrad Meyer /* IP version. */ 1097790c8c1SConrad Meyer if (ip->ip_v != IPVERSION) { 1107790c8c1SConrad Meyer DNETDEBUG("bad IP version %d\n", ip->ip_v); 1117790c8c1SConrad Meyer return; 1127790c8c1SConrad Meyer } 1137790c8c1SConrad Meyer 1147790c8c1SConrad Meyer /* Header length. */ 1157790c8c1SConrad Meyer hlen = ip->ip_hl << 2; 1167790c8c1SConrad Meyer if (hlen < sizeof(struct ip)) { 1177790c8c1SConrad Meyer DNETDEBUG("bad IP header length (%hu)\n", hlen); 1187790c8c1SConrad Meyer return; 1197790c8c1SConrad Meyer } 1207790c8c1SConrad Meyer if (hlen > m->m_len) { 1217790c8c1SConrad Meyer m = m_pullup(m, hlen); 1227790c8c1SConrad Meyer *mb = m; 1237790c8c1SConrad Meyer if (m == NULL) { 1247790c8c1SConrad Meyer DNETDEBUG("m_pullup failed\n"); 1257790c8c1SConrad Meyer return; 1267790c8c1SConrad Meyer } 1277790c8c1SConrad Meyer ip = mtod(m, struct ip *); 1287790c8c1SConrad Meyer } 1297790c8c1SConrad Meyer /* Ignore packets with IP options. */ 1307790c8c1SConrad Meyer if (hlen > sizeof(struct ip)) { 1317790c8c1SConrad Meyer DNETDEBUG("drop packet with IP options\n"); 1327790c8c1SConrad Meyer return; 1337790c8c1SConrad Meyer } 1347790c8c1SConrad Meyer 1357790c8c1SConrad Meyer #ifdef INVARIANTS 1367790c8c1SConrad Meyer if ((IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) || 1377790c8c1SConrad Meyer IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) && 1387790c8c1SConrad Meyer (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 1397790c8c1SConrad Meyer DNETDEBUG("Bad IP header (RFC1122)\n"); 1407790c8c1SConrad Meyer return; 1417790c8c1SConrad Meyer } 1427790c8c1SConrad Meyer #endif 1437790c8c1SConrad Meyer 1447790c8c1SConrad Meyer /* Checksum. */ 1457790c8c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) != 0) { 1467790c8c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0) { 1477790c8c1SConrad Meyer DNETDEBUG("bad IP checksum\n"); 1487790c8c1SConrad Meyer return; 1497790c8c1SConrad Meyer } 1507790c8c1SConrad Meyer } else { 1517790c8c1SConrad Meyer /* XXX */ ; 1527790c8c1SConrad Meyer } 1537790c8c1SConrad Meyer 1547790c8c1SConrad Meyer /* Convert fields to host byte order. */ 1557790c8c1SConrad Meyer ip->ip_len = ntohs(ip->ip_len); 1567790c8c1SConrad Meyer if (ip->ip_len < hlen) { 1577790c8c1SConrad Meyer DNETDEBUG("IP packet smaller (%hu) than header (%hu)\n", 1587790c8c1SConrad Meyer ip->ip_len, hlen); 1597790c8c1SConrad Meyer return; 1607790c8c1SConrad Meyer } 1617790c8c1SConrad Meyer if (m->m_pkthdr.len < ip->ip_len) { 1627790c8c1SConrad Meyer DNETDEBUG("IP packet bigger (%hu) than ethernet packet (%d)\n", 1637790c8c1SConrad Meyer ip->ip_len, m->m_pkthdr.len); 1647790c8c1SConrad Meyer return; 1657790c8c1SConrad Meyer } 1667790c8c1SConrad Meyer if (m->m_pkthdr.len > ip->ip_len) { 1677790c8c1SConrad Meyer /* Truncate the packet to the IP length. */ 1687790c8c1SConrad Meyer if (m->m_len == m->m_pkthdr.len) { 1697790c8c1SConrad Meyer m->m_len = ip->ip_len; 1707790c8c1SConrad Meyer m->m_pkthdr.len = ip->ip_len; 1717790c8c1SConrad Meyer } else 1727790c8c1SConrad Meyer m_adj(m, ip->ip_len - m->m_pkthdr.len); 1737790c8c1SConrad Meyer } 1747790c8c1SConrad Meyer 1757790c8c1SConrad Meyer ip->ip_off = ntohs(ip->ip_off); 1767790c8c1SConrad Meyer 1777790c8c1SConrad Meyer /* Check that the source is the server's IP. */ 1787790c8c1SConrad Meyer if (ip->ip_src.s_addr != pcb->dp_server) { 1797790c8c1SConrad Meyer DNETDEBUG("drop packet not from server (from 0x%x)\n", 1807790c8c1SConrad Meyer ip->ip_src.s_addr); 1817790c8c1SConrad Meyer return; 1827790c8c1SConrad Meyer } 1837790c8c1SConrad Meyer 1847790c8c1SConrad Meyer /* Check if the destination IP is ours. */ 1857790c8c1SConrad Meyer if (ip->ip_dst.s_addr != pcb->dp_client) { 1867790c8c1SConrad Meyer DNETDEBUGV("drop packet not to our IP\n"); 1877790c8c1SConrad Meyer return; 1887790c8c1SConrad Meyer } 1897790c8c1SConrad Meyer 1907790c8c1SConrad Meyer if (ip->ip_p != IPPROTO_UDP) { 1917790c8c1SConrad Meyer DNETDEBUG("drop non-UDP packet\n"); 1927790c8c1SConrad Meyer return; 1937790c8c1SConrad Meyer } 1947790c8c1SConrad Meyer 1957790c8c1SConrad Meyer /* Do not deal with fragments. */ 1967790c8c1SConrad Meyer if ((ip->ip_off & (IP_MF | IP_OFFMASK)) != 0) { 1977790c8c1SConrad Meyer DNETDEBUG("drop fragmented packet\n"); 1987790c8c1SConrad Meyer return; 1997790c8c1SConrad Meyer } 2007790c8c1SConrad Meyer 201d39756c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) != 0) { 202d39756c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) == 0) { 203d39756c1SConrad Meyer DNETDEBUG("bad UDP checksum\n"); 204d39756c1SConrad Meyer return; 205d39756c1SConrad Meyer } 206d39756c1SConrad Meyer } else { 207d39756c1SConrad Meyer /* XXX */ ; 208d39756c1SConrad Meyer } 209d39756c1SConrad Meyer 2107790c8c1SConrad Meyer /* UDP custom is to have packet length not include IP header. */ 2117790c8c1SConrad Meyer ip->ip_len -= hlen; 2127790c8c1SConrad Meyer 2137790c8c1SConrad Meyer /* Checked above before decoding IP header. */ 2147790c8c1SConrad Meyer MPASS(m->m_pkthdr.len >= sizeof(struct ipovly)); 2157790c8c1SConrad Meyer 2167790c8c1SConrad Meyer /* Put the UDP header at start of chain. */ 2177790c8c1SConrad Meyer m_adj(m, sizeof(struct ipovly)); 2187790c8c1SConrad Meyer debugnet_handle_udp(pcb, mb); 2197790c8c1SConrad Meyer } 2207790c8c1SConrad Meyer 2217790c8c1SConrad Meyer /* 2227790c8c1SConrad Meyer * Builds and sends a single ARP request to locate the L2 address for a given 2237790c8c1SConrad Meyer * INET address. 2247790c8c1SConrad Meyer * 2257790c8c1SConrad Meyer * Return value: 2267790c8c1SConrad Meyer * 0 on success 2277790c8c1SConrad Meyer * errno on error 2287790c8c1SConrad Meyer */ 2297790c8c1SConrad Meyer static int 2307790c8c1SConrad Meyer debugnet_send_arp(struct debugnet_pcb *pcb, in_addr_t dst) 2317790c8c1SConrad Meyer { 2327790c8c1SConrad Meyer struct ether_addr bcast; 2337790c8c1SConrad Meyer struct arphdr *ah; 2347790c8c1SConrad Meyer struct ifnet *ifp; 2357790c8c1SConrad Meyer struct mbuf *m; 2367790c8c1SConrad Meyer int pktlen; 2377790c8c1SConrad Meyer 2387790c8c1SConrad Meyer ifp = pcb->dp_ifp; 2397790c8c1SConrad Meyer 2407790c8c1SConrad Meyer /* Fill-up a broadcast address. */ 2417790c8c1SConrad Meyer memset(&bcast, 0xFF, ETHER_ADDR_LEN); 2427790c8c1SConrad Meyer m = m_gethdr(M_NOWAIT, MT_DATA); 2437790c8c1SConrad Meyer if (m == NULL) { 2447790c8c1SConrad Meyer printf("%s: Out of mbufs\n", __func__); 2457790c8c1SConrad Meyer return (ENOBUFS); 2467790c8c1SConrad Meyer } 2477790c8c1SConrad Meyer pktlen = arphdr_len2(ETHER_ADDR_LEN, sizeof(struct in_addr)); 2487790c8c1SConrad Meyer m->m_len = pktlen; 2497790c8c1SConrad Meyer m->m_pkthdr.len = pktlen; 2507790c8c1SConrad Meyer MH_ALIGN(m, pktlen); 2517790c8c1SConrad Meyer ah = mtod(m, struct arphdr *); 2527790c8c1SConrad Meyer ah->ar_hrd = htons(ARPHRD_ETHER); 2537790c8c1SConrad Meyer ah->ar_pro = htons(ETHERTYPE_IP); 2547790c8c1SConrad Meyer ah->ar_hln = ETHER_ADDR_LEN; 2557790c8c1SConrad Meyer ah->ar_pln = sizeof(struct in_addr); 2567790c8c1SConrad Meyer ah->ar_op = htons(ARPOP_REQUEST); 2577790c8c1SConrad Meyer memcpy(ar_sha(ah), IF_LLADDR(ifp), ETHER_ADDR_LEN); 2587790c8c1SConrad Meyer ((struct in_addr *)ar_spa(ah))->s_addr = pcb->dp_client; 2597790c8c1SConrad Meyer bzero(ar_tha(ah), ETHER_ADDR_LEN); 2607790c8c1SConrad Meyer ((struct in_addr *)ar_tpa(ah))->s_addr = dst; 2617790c8c1SConrad Meyer return (debugnet_ether_output(m, ifp, bcast, ETHERTYPE_ARP)); 2627790c8c1SConrad Meyer } 2637790c8c1SConrad Meyer 2647790c8c1SConrad Meyer /* 2657790c8c1SConrad Meyer * Handler for ARP packets: checks their sanity and then 2667790c8c1SConrad Meyer * 1. If the ARP is a request for our IP, respond with our MAC address 2677790c8c1SConrad Meyer * 2. If the ARP is a response from our server, record its MAC address 2687790c8c1SConrad Meyer * 2697790c8c1SConrad Meyer * It needs to replicate partially the behaviour of arpintr() and 2707790c8c1SConrad Meyer * in_arpinput(). 2717790c8c1SConrad Meyer * 2727790c8c1SConrad Meyer * Parameters: 2737790c8c1SConrad Meyer * pcb a pointer to the live debugnet PCB 2747790c8c1SConrad Meyer * mb a pointer to an mbuf * containing the packet received 2757790c8c1SConrad Meyer * Updates *mb if m_pullup et al change the pointer 2767790c8c1SConrad Meyer * Assumes the calling function will take care of freeing the mbuf 2777790c8c1SConrad Meyer */ 2787790c8c1SConrad Meyer void 2797790c8c1SConrad Meyer debugnet_handle_arp(struct debugnet_pcb *pcb, struct mbuf **mb) 2807790c8c1SConrad Meyer { 2817790c8c1SConrad Meyer char buf[INET_ADDRSTRLEN]; 2827790c8c1SConrad Meyer struct in_addr isaddr, itaddr; 2837790c8c1SConrad Meyer struct ether_addr dst; 2847790c8c1SConrad Meyer struct mbuf *m; 2857790c8c1SConrad Meyer struct arphdr *ah; 2867790c8c1SConrad Meyer struct ifnet *ifp; 2877790c8c1SConrad Meyer uint8_t *enaddr; 2887790c8c1SConrad Meyer int req_len, op; 2897790c8c1SConrad Meyer 2907790c8c1SConrad Meyer m = *mb; 2917790c8c1SConrad Meyer ifp = m->m_pkthdr.rcvif; 2927790c8c1SConrad Meyer if (m->m_len < sizeof(struct arphdr)) { 2937790c8c1SConrad Meyer m = m_pullup(m, sizeof(struct arphdr)); 2947790c8c1SConrad Meyer *mb = m; 2957790c8c1SConrad Meyer if (m == NULL) { 2967790c8c1SConrad Meyer DNETDEBUG("runt packet: m_pullup failed\n"); 2977790c8c1SConrad Meyer return; 2987790c8c1SConrad Meyer } 2997790c8c1SConrad Meyer } 3007790c8c1SConrad Meyer 3017790c8c1SConrad Meyer ah = mtod(m, struct arphdr *); 3027790c8c1SConrad Meyer if (ntohs(ah->ar_hrd) != ARPHRD_ETHER) { 3037790c8c1SConrad Meyer DNETDEBUG("unknown hardware address 0x%2D)\n", 3047790c8c1SConrad Meyer (unsigned char *)&ah->ar_hrd, ""); 3057790c8c1SConrad Meyer return; 3067790c8c1SConrad Meyer } 3077790c8c1SConrad Meyer if (ntohs(ah->ar_pro) != ETHERTYPE_IP) { 3087790c8c1SConrad Meyer DNETDEBUG("drop ARP for unknown protocol %d\n", 3097790c8c1SConrad Meyer ntohs(ah->ar_pro)); 3107790c8c1SConrad Meyer return; 3117790c8c1SConrad Meyer } 3127790c8c1SConrad Meyer req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 3137790c8c1SConrad Meyer if (m->m_len < req_len) { 3147790c8c1SConrad Meyer m = m_pullup(m, req_len); 3157790c8c1SConrad Meyer *mb = m; 3167790c8c1SConrad Meyer if (m == NULL) { 3177790c8c1SConrad Meyer DNETDEBUG("runt packet: m_pullup failed\n"); 3187790c8c1SConrad Meyer return; 3197790c8c1SConrad Meyer } 3207790c8c1SConrad Meyer } 3217790c8c1SConrad Meyer ah = mtod(m, struct arphdr *); 3227790c8c1SConrad Meyer 3237790c8c1SConrad Meyer op = ntohs(ah->ar_op); 3247790c8c1SConrad Meyer memcpy(&isaddr, ar_spa(ah), sizeof(isaddr)); 3257790c8c1SConrad Meyer memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr)); 3267790c8c1SConrad Meyer enaddr = (uint8_t *)IF_LLADDR(ifp); 3277790c8c1SConrad Meyer 3287790c8c1SConrad Meyer if (memcmp(ar_sha(ah), enaddr, ifp->if_addrlen) == 0) { 3297790c8c1SConrad Meyer DNETDEBUG("ignoring ARP from myself\n"); 3307790c8c1SConrad Meyer return; 3317790c8c1SConrad Meyer } 3327790c8c1SConrad Meyer 3337790c8c1SConrad Meyer if (isaddr.s_addr == pcb->dp_client) { 3347790c8c1SConrad Meyer printf("%s: %*D is using my IP address %s!\n", __func__, 3357790c8c1SConrad Meyer ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 3367790c8c1SConrad Meyer inet_ntoa_r(isaddr, buf)); 3377790c8c1SConrad Meyer return; 3387790c8c1SConrad Meyer } 3397790c8c1SConrad Meyer 3407790c8c1SConrad Meyer if (memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen) == 0) { 3417790c8c1SConrad Meyer DNETDEBUG("ignoring ARP from broadcast address\n"); 3427790c8c1SConrad Meyer return; 3437790c8c1SConrad Meyer } 3447790c8c1SConrad Meyer 3457790c8c1SConrad Meyer if (op == ARPOP_REPLY) { 3467790c8c1SConrad Meyer if (isaddr.s_addr != pcb->dp_gateway && 3477790c8c1SConrad Meyer isaddr.s_addr != pcb->dp_server) { 3487790c8c1SConrad Meyer inet_ntoa_r(isaddr, buf); 3497790c8c1SConrad Meyer DNETDEBUG("ignoring ARP reply from %s (not configured" 3507790c8c1SConrad Meyer " server or gateway)\n", buf); 3517790c8c1SConrad Meyer return; 3527790c8c1SConrad Meyer } 353*7cbf1de3SBryan Drewery if (pcb->dp_state >= DN_STATE_HAVE_GW_MAC) { 354*7cbf1de3SBryan Drewery inet_ntoa_r(isaddr, buf); 355*7cbf1de3SBryan Drewery DNETDEBUG("ignoring server ARP reply from %s (already" 356*7cbf1de3SBryan Drewery " have gateway address)\n", buf); 357*7cbf1de3SBryan Drewery return; 358*7cbf1de3SBryan Drewery } 359*7cbf1de3SBryan Drewery MPASS(pcb->dp_state == DN_STATE_INIT); 3607790c8c1SConrad Meyer memcpy(pcb->dp_gw_mac.octet, ar_sha(ah), 3617790c8c1SConrad Meyer min(ah->ar_hln, ETHER_ADDR_LEN)); 3627790c8c1SConrad Meyer 3637790c8c1SConrad Meyer DNETDEBUG("got server MAC address %6D\n", 3647790c8c1SConrad Meyer pcb->dp_gw_mac.octet, ":"); 3657790c8c1SConrad Meyer 3667790c8c1SConrad Meyer pcb->dp_state = DN_STATE_HAVE_GW_MAC; 3677790c8c1SConrad Meyer return; 3687790c8c1SConrad Meyer } 3697790c8c1SConrad Meyer 3707790c8c1SConrad Meyer if (op != ARPOP_REQUEST) { 3717790c8c1SConrad Meyer DNETDEBUG("ignoring ARP non-request/reply\n"); 3727790c8c1SConrad Meyer return; 3737790c8c1SConrad Meyer } 3747790c8c1SConrad Meyer 3757790c8c1SConrad Meyer if (itaddr.s_addr != pcb->dp_client) { 3767790c8c1SConrad Meyer DNETDEBUG("ignoring ARP not to our IP\n"); 3777790c8c1SConrad Meyer return; 3787790c8c1SConrad Meyer } 3797790c8c1SConrad Meyer 3807790c8c1SConrad Meyer memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 3817790c8c1SConrad Meyer memcpy(ar_sha(ah), enaddr, ah->ar_hln); 3827790c8c1SConrad Meyer memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 3837790c8c1SConrad Meyer memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 3847790c8c1SConrad Meyer ah->ar_op = htons(ARPOP_REPLY); 3857790c8c1SConrad Meyer ah->ar_pro = htons(ETHERTYPE_IP); 3867790c8c1SConrad Meyer m->m_flags &= ~(M_BCAST|M_MCAST); 3877790c8c1SConrad Meyer m->m_len = arphdr_len(ah); 3887790c8c1SConrad Meyer m->m_pkthdr.len = m->m_len; 3897790c8c1SConrad Meyer 3907790c8c1SConrad Meyer memcpy(dst.octet, ar_tha(ah), ETHER_ADDR_LEN); 3917790c8c1SConrad Meyer debugnet_ether_output(m, ifp, dst, ETHERTYPE_ARP); 3927790c8c1SConrad Meyer *mb = NULL; 3937790c8c1SConrad Meyer } 3947790c8c1SConrad Meyer 3957790c8c1SConrad Meyer /* 3967790c8c1SConrad Meyer * Sends ARP requests to locate the server and waits for a response. 3977790c8c1SConrad Meyer * We first try to ARP the server itself, and fall back to the provided 3987790c8c1SConrad Meyer * gateway if the server appears to be off-link. 3997790c8c1SConrad Meyer * 4007790c8c1SConrad Meyer * Return value: 4017790c8c1SConrad Meyer * 0 on success 4027790c8c1SConrad Meyer * errno on error 4037790c8c1SConrad Meyer */ 4047790c8c1SConrad Meyer int 4057790c8c1SConrad Meyer debugnet_arp_gw(struct debugnet_pcb *pcb) 4067790c8c1SConrad Meyer { 4077790c8c1SConrad Meyer in_addr_t dst; 4087790c8c1SConrad Meyer int error, polls, retries; 4097790c8c1SConrad Meyer 4107790c8c1SConrad Meyer dst = pcb->dp_server; 4117790c8c1SConrad Meyer restart: 4127790c8c1SConrad Meyer for (retries = 0; retries < debugnet_arp_nretries; retries++) { 4137790c8c1SConrad Meyer error = debugnet_send_arp(pcb, dst); 4147790c8c1SConrad Meyer if (error != 0) 4157790c8c1SConrad Meyer return (error); 4167790c8c1SConrad Meyer for (polls = 0; polls < debugnet_npolls && 4177790c8c1SConrad Meyer pcb->dp_state < DN_STATE_HAVE_GW_MAC; polls++) { 418dda17b36SConrad Meyer debugnet_network_poll(pcb); 4197790c8c1SConrad Meyer DELAY(500); 4207790c8c1SConrad Meyer } 4217790c8c1SConrad Meyer if (pcb->dp_state >= DN_STATE_HAVE_GW_MAC) 4227790c8c1SConrad Meyer break; 4237790c8c1SConrad Meyer printf("(ARP retry)"); 4247790c8c1SConrad Meyer } 4257790c8c1SConrad Meyer if (pcb->dp_state >= DN_STATE_HAVE_GW_MAC) 4267790c8c1SConrad Meyer return (0); 4277790c8c1SConrad Meyer if (dst == pcb->dp_server) { 4287790c8c1SConrad Meyer printf("\nFailed to ARP server"); 4297790c8c1SConrad Meyer if (pcb->dp_gateway != INADDR_ANY) { 4307790c8c1SConrad Meyer printf(", trying to reach gateway...\n"); 4317790c8c1SConrad Meyer dst = pcb->dp_gateway; 4327790c8c1SConrad Meyer goto restart; 4337790c8c1SConrad Meyer } else 4347790c8c1SConrad Meyer printf(".\n"); 4357790c8c1SConrad Meyer } else 4367790c8c1SConrad Meyer printf("\nFailed to ARP gateway.\n"); 4377790c8c1SConrad Meyer 4387790c8c1SConrad Meyer return (ETIMEDOUT); 4397790c8c1SConrad Meyer } 4407790c8c1SConrad Meyer 4417790c8c1SConrad Meyer /* 4427790c8c1SConrad Meyer * Unreliable IPv4 transmission of an mbuf chain to the debugnet server 4437790c8c1SConrad Meyer * Note: can't handle fragmentation; fails if the packet is larger than 4447790c8c1SConrad Meyer * ifp->if_mtu after adding the UDP/IP headers 4457790c8c1SConrad Meyer * 4467790c8c1SConrad Meyer * Parameters: 4477790c8c1SConrad Meyer * pcb The debugnet context block 4487790c8c1SConrad Meyer * m mbuf chain 4497790c8c1SConrad Meyer * 4507790c8c1SConrad Meyer * Returns: 4517790c8c1SConrad Meyer * int see errno.h, 0 for success 4527790c8c1SConrad Meyer */ 4537790c8c1SConrad Meyer int 4547790c8c1SConrad Meyer debugnet_ip_output(struct debugnet_pcb *pcb, struct mbuf *m) 4557790c8c1SConrad Meyer { 4567790c8c1SConrad Meyer struct udphdr *udp; 4577790c8c1SConrad Meyer struct ifnet *ifp; 4587790c8c1SConrad Meyer struct ip *ip; 4597790c8c1SConrad Meyer 4607790c8c1SConrad Meyer MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC); 4617790c8c1SConrad Meyer 4627790c8c1SConrad Meyer ifp = pcb->dp_ifp; 4637790c8c1SConrad Meyer 4647790c8c1SConrad Meyer M_PREPEND(m, sizeof(*ip), M_NOWAIT); 4657790c8c1SConrad Meyer if (m == NULL) { 4667790c8c1SConrad Meyer printf("%s: out of mbufs\n", __func__); 4677790c8c1SConrad Meyer return (ENOBUFS); 4687790c8c1SConrad Meyer } 4697790c8c1SConrad Meyer 4707790c8c1SConrad Meyer if (m->m_pkthdr.len > ifp->if_mtu) { 4717790c8c1SConrad Meyer printf("%s: Packet is too big: %d > MTU %u\n", __func__, 4727790c8c1SConrad Meyer m->m_pkthdr.len, ifp->if_mtu); 4737790c8c1SConrad Meyer m_freem(m); 4747790c8c1SConrad Meyer return (ENOBUFS); 4757790c8c1SConrad Meyer } 4767790c8c1SConrad Meyer 4777790c8c1SConrad Meyer ip = mtod(m, void *); 4787790c8c1SConrad Meyer udp = (void *)(ip + 1); 4797790c8c1SConrad Meyer 4807790c8c1SConrad Meyer memset(ip, 0, offsetof(struct ip, ip_p)); 4817790c8c1SConrad Meyer ip->ip_p = IPPROTO_UDP; 4827790c8c1SConrad Meyer ip->ip_sum = udp->uh_ulen; 4837790c8c1SConrad Meyer ip->ip_src = (struct in_addr) { pcb->dp_client }; 4847790c8c1SConrad Meyer ip->ip_dst = (struct in_addr) { pcb->dp_server }; 4857790c8c1SConrad Meyer 4867790c8c1SConrad Meyer /* Compute UDP-IPv4 checksum. */ 4877790c8c1SConrad Meyer udp->uh_sum = in_cksum(m, m->m_pkthdr.len); 4887790c8c1SConrad Meyer if (udp->uh_sum == 0) 4897790c8c1SConrad Meyer udp->uh_sum = 0xffff; 4907790c8c1SConrad Meyer 4917790c8c1SConrad Meyer ip->ip_v = IPVERSION; 4927790c8c1SConrad Meyer ip->ip_hl = sizeof(*ip) >> 2; 4937790c8c1SConrad Meyer ip->ip_tos = 0; 4947790c8c1SConrad Meyer ip->ip_len = htons(m->m_pkthdr.len); 4957790c8c1SConrad Meyer ip->ip_id = 0; 4967790c8c1SConrad Meyer ip->ip_off = htons(IP_DF); 4977790c8c1SConrad Meyer ip->ip_ttl = 255; 4987790c8c1SConrad Meyer ip->ip_sum = 0; 4997790c8c1SConrad Meyer ip->ip_sum = in_cksum(m, sizeof(struct ip)); 5007790c8c1SConrad Meyer 5017790c8c1SConrad Meyer return (debugnet_ether_output(m, ifp, pcb->dp_gw_mac, ETHERTYPE_IP)); 5027790c8c1SConrad Meyer } 503