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 897790c8c1SConrad Meyer /* IP processing. */ 907790c8c1SConrad Meyer m = *mb; 917790c8c1SConrad Meyer if (m->m_pkthdr.len < sizeof(struct ip)) { 927790c8c1SConrad Meyer DNETDEBUG("dropping packet too small for IP header\n"); 937790c8c1SConrad Meyer return; 947790c8c1SConrad Meyer } 957790c8c1SConrad Meyer if (m->m_len < sizeof(struct ip)) { 967790c8c1SConrad Meyer m = m_pullup(m, sizeof(struct ip)); 977790c8c1SConrad Meyer *mb = m; 987790c8c1SConrad Meyer if (m == NULL) { 997790c8c1SConrad Meyer DNETDEBUG("m_pullup failed\n"); 1007790c8c1SConrad Meyer return; 1017790c8c1SConrad Meyer } 1027790c8c1SConrad Meyer } 1037790c8c1SConrad Meyer ip = mtod(m, struct ip *); 1047790c8c1SConrad Meyer 1057790c8c1SConrad Meyer /* IP version. */ 1067790c8c1SConrad Meyer if (ip->ip_v != IPVERSION) { 1077790c8c1SConrad Meyer DNETDEBUG("bad IP version %d\n", ip->ip_v); 1087790c8c1SConrad Meyer return; 1097790c8c1SConrad Meyer } 1107790c8c1SConrad Meyer 1117790c8c1SConrad Meyer /* Header length. */ 1127790c8c1SConrad Meyer hlen = ip->ip_hl << 2; 1137790c8c1SConrad Meyer if (hlen < sizeof(struct ip)) { 1147790c8c1SConrad Meyer DNETDEBUG("bad IP header length (%hu)\n", hlen); 1157790c8c1SConrad Meyer return; 1167790c8c1SConrad Meyer } 1177790c8c1SConrad Meyer if (hlen > m->m_len) { 1187790c8c1SConrad Meyer m = m_pullup(m, hlen); 1197790c8c1SConrad Meyer *mb = m; 1207790c8c1SConrad Meyer if (m == NULL) { 1217790c8c1SConrad Meyer DNETDEBUG("m_pullup failed\n"); 1227790c8c1SConrad Meyer return; 1237790c8c1SConrad Meyer } 1247790c8c1SConrad Meyer ip = mtod(m, struct ip *); 1257790c8c1SConrad Meyer } 1267790c8c1SConrad Meyer /* Ignore packets with IP options. */ 1277790c8c1SConrad Meyer if (hlen > sizeof(struct ip)) { 1287790c8c1SConrad Meyer DNETDEBUG("drop packet with IP options\n"); 1297790c8c1SConrad Meyer return; 1307790c8c1SConrad Meyer } 1317790c8c1SConrad Meyer 1327790c8c1SConrad Meyer #ifdef INVARIANTS 1337790c8c1SConrad Meyer if ((IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) || 1347790c8c1SConrad Meyer IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) && 1357790c8c1SConrad Meyer (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 1367790c8c1SConrad Meyer DNETDEBUG("Bad IP header (RFC1122)\n"); 1377790c8c1SConrad Meyer return; 1387790c8c1SConrad Meyer } 1397790c8c1SConrad Meyer #endif 1407790c8c1SConrad Meyer 1417790c8c1SConrad Meyer /* Checksum. */ 1427790c8c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) != 0) { 1437790c8c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0) { 1447790c8c1SConrad Meyer DNETDEBUG("bad IP checksum\n"); 1457790c8c1SConrad Meyer return; 1467790c8c1SConrad Meyer } 1477790c8c1SConrad Meyer } else { 1487790c8c1SConrad Meyer /* XXX */ ; 1497790c8c1SConrad Meyer } 1507790c8c1SConrad Meyer 1517790c8c1SConrad Meyer /* Convert fields to host byte order. */ 1527790c8c1SConrad Meyer ip->ip_len = ntohs(ip->ip_len); 1537790c8c1SConrad Meyer if (ip->ip_len < hlen) { 1547790c8c1SConrad Meyer DNETDEBUG("IP packet smaller (%hu) than header (%hu)\n", 1557790c8c1SConrad Meyer ip->ip_len, hlen); 1567790c8c1SConrad Meyer return; 1577790c8c1SConrad Meyer } 1587790c8c1SConrad Meyer if (m->m_pkthdr.len < ip->ip_len) { 1597790c8c1SConrad Meyer DNETDEBUG("IP packet bigger (%hu) than ethernet packet (%d)\n", 1607790c8c1SConrad Meyer ip->ip_len, m->m_pkthdr.len); 1617790c8c1SConrad Meyer return; 1627790c8c1SConrad Meyer } 1637790c8c1SConrad Meyer if (m->m_pkthdr.len > ip->ip_len) { 1647790c8c1SConrad Meyer 1657790c8c1SConrad Meyer /* Truncate the packet to the IP length. */ 1667790c8c1SConrad Meyer if (m->m_len == m->m_pkthdr.len) { 1677790c8c1SConrad Meyer m->m_len = ip->ip_len; 1687790c8c1SConrad Meyer m->m_pkthdr.len = ip->ip_len; 1697790c8c1SConrad Meyer } else 1707790c8c1SConrad Meyer m_adj(m, ip->ip_len - m->m_pkthdr.len); 1717790c8c1SConrad Meyer } 1727790c8c1SConrad Meyer 1737790c8c1SConrad Meyer ip->ip_off = ntohs(ip->ip_off); 1747790c8c1SConrad Meyer 1757790c8c1SConrad Meyer /* Check that the source is the server's IP. */ 1767790c8c1SConrad Meyer if (ip->ip_src.s_addr != pcb->dp_server) { 1777790c8c1SConrad Meyer DNETDEBUG("drop packet not from server (from 0x%x)\n", 1787790c8c1SConrad Meyer ip->ip_src.s_addr); 1797790c8c1SConrad Meyer return; 1807790c8c1SConrad Meyer } 1817790c8c1SConrad Meyer 1827790c8c1SConrad Meyer /* Check if the destination IP is ours. */ 1837790c8c1SConrad Meyer if (ip->ip_dst.s_addr != pcb->dp_client) { 1847790c8c1SConrad Meyer DNETDEBUGV("drop packet not to our IP\n"); 1857790c8c1SConrad Meyer return; 1867790c8c1SConrad Meyer } 1877790c8c1SConrad Meyer 1887790c8c1SConrad Meyer if (ip->ip_p != IPPROTO_UDP) { 1897790c8c1SConrad Meyer DNETDEBUG("drop non-UDP packet\n"); 1907790c8c1SConrad Meyer return; 1917790c8c1SConrad Meyer } 1927790c8c1SConrad Meyer 1937790c8c1SConrad Meyer /* Do not deal with fragments. */ 1947790c8c1SConrad Meyer if ((ip->ip_off & (IP_MF | IP_OFFMASK)) != 0) { 1957790c8c1SConrad Meyer DNETDEBUG("drop fragmented packet\n"); 1967790c8c1SConrad Meyer return; 1977790c8c1SConrad Meyer } 1987790c8c1SConrad Meyer 199*d39756c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) != 0) { 200*d39756c1SConrad Meyer if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) == 0) { 201*d39756c1SConrad Meyer DNETDEBUG("bad UDP checksum\n"); 202*d39756c1SConrad Meyer return; 203*d39756c1SConrad Meyer } 204*d39756c1SConrad Meyer } else { 205*d39756c1SConrad Meyer /* XXX */ ; 206*d39756c1SConrad Meyer } 207*d39756c1SConrad Meyer 2087790c8c1SConrad Meyer /* UDP custom is to have packet length not include IP header. */ 2097790c8c1SConrad Meyer ip->ip_len -= hlen; 2107790c8c1SConrad Meyer 2117790c8c1SConrad Meyer /* Checked above before decoding IP header. */ 2127790c8c1SConrad Meyer MPASS(m->m_pkthdr.len >= sizeof(struct ipovly)); 2137790c8c1SConrad Meyer 2147790c8c1SConrad Meyer /* Put the UDP header at start of chain. */ 2157790c8c1SConrad Meyer m_adj(m, sizeof(struct ipovly)); 2167790c8c1SConrad Meyer debugnet_handle_udp(pcb, mb); 2177790c8c1SConrad Meyer } 2187790c8c1SConrad Meyer 2197790c8c1SConrad Meyer /* 2207790c8c1SConrad Meyer * Builds and sends a single ARP request to locate the L2 address for a given 2217790c8c1SConrad Meyer * INET address. 2227790c8c1SConrad Meyer * 2237790c8c1SConrad Meyer * Return value: 2247790c8c1SConrad Meyer * 0 on success 2257790c8c1SConrad Meyer * errno on error 2267790c8c1SConrad Meyer */ 2277790c8c1SConrad Meyer static int 2287790c8c1SConrad Meyer debugnet_send_arp(struct debugnet_pcb *pcb, in_addr_t dst) 2297790c8c1SConrad Meyer { 2307790c8c1SConrad Meyer struct ether_addr bcast; 2317790c8c1SConrad Meyer struct arphdr *ah; 2327790c8c1SConrad Meyer struct ifnet *ifp; 2337790c8c1SConrad Meyer struct mbuf *m; 2347790c8c1SConrad Meyer int pktlen; 2357790c8c1SConrad Meyer 2367790c8c1SConrad Meyer ifp = pcb->dp_ifp; 2377790c8c1SConrad Meyer 2387790c8c1SConrad Meyer /* Fill-up a broadcast address. */ 2397790c8c1SConrad Meyer memset(&bcast, 0xFF, ETHER_ADDR_LEN); 2407790c8c1SConrad Meyer m = m_gethdr(M_NOWAIT, MT_DATA); 2417790c8c1SConrad Meyer if (m == NULL) { 2427790c8c1SConrad Meyer printf("%s: Out of mbufs\n", __func__); 2437790c8c1SConrad Meyer return (ENOBUFS); 2447790c8c1SConrad Meyer } 2457790c8c1SConrad Meyer pktlen = arphdr_len2(ETHER_ADDR_LEN, sizeof(struct in_addr)); 2467790c8c1SConrad Meyer m->m_len = pktlen; 2477790c8c1SConrad Meyer m->m_pkthdr.len = pktlen; 2487790c8c1SConrad Meyer MH_ALIGN(m, pktlen); 2497790c8c1SConrad Meyer ah = mtod(m, struct arphdr *); 2507790c8c1SConrad Meyer ah->ar_hrd = htons(ARPHRD_ETHER); 2517790c8c1SConrad Meyer ah->ar_pro = htons(ETHERTYPE_IP); 2527790c8c1SConrad Meyer ah->ar_hln = ETHER_ADDR_LEN; 2537790c8c1SConrad Meyer ah->ar_pln = sizeof(struct in_addr); 2547790c8c1SConrad Meyer ah->ar_op = htons(ARPOP_REQUEST); 2557790c8c1SConrad Meyer memcpy(ar_sha(ah), IF_LLADDR(ifp), ETHER_ADDR_LEN); 2567790c8c1SConrad Meyer ((struct in_addr *)ar_spa(ah))->s_addr = pcb->dp_client; 2577790c8c1SConrad Meyer bzero(ar_tha(ah), ETHER_ADDR_LEN); 2587790c8c1SConrad Meyer ((struct in_addr *)ar_tpa(ah))->s_addr = dst; 2597790c8c1SConrad Meyer return (debugnet_ether_output(m, ifp, bcast, ETHERTYPE_ARP)); 2607790c8c1SConrad Meyer } 2617790c8c1SConrad Meyer 2627790c8c1SConrad Meyer /* 2637790c8c1SConrad Meyer * Handler for ARP packets: checks their sanity and then 2647790c8c1SConrad Meyer * 1. If the ARP is a request for our IP, respond with our MAC address 2657790c8c1SConrad Meyer * 2. If the ARP is a response from our server, record its MAC address 2667790c8c1SConrad Meyer * 2677790c8c1SConrad Meyer * It needs to replicate partially the behaviour of arpintr() and 2687790c8c1SConrad Meyer * in_arpinput(). 2697790c8c1SConrad Meyer * 2707790c8c1SConrad Meyer * Parameters: 2717790c8c1SConrad Meyer * pcb a pointer to the live debugnet PCB 2727790c8c1SConrad Meyer * mb a pointer to an mbuf * containing the packet received 2737790c8c1SConrad Meyer * Updates *mb if m_pullup et al change the pointer 2747790c8c1SConrad Meyer * Assumes the calling function will take care of freeing the mbuf 2757790c8c1SConrad Meyer */ 2767790c8c1SConrad Meyer void 2777790c8c1SConrad Meyer debugnet_handle_arp(struct debugnet_pcb *pcb, struct mbuf **mb) 2787790c8c1SConrad Meyer { 2797790c8c1SConrad Meyer char buf[INET_ADDRSTRLEN]; 2807790c8c1SConrad Meyer struct in_addr isaddr, itaddr; 2817790c8c1SConrad Meyer struct ether_addr dst; 2827790c8c1SConrad Meyer struct mbuf *m; 2837790c8c1SConrad Meyer struct arphdr *ah; 2847790c8c1SConrad Meyer struct ifnet *ifp; 2857790c8c1SConrad Meyer uint8_t *enaddr; 2867790c8c1SConrad Meyer int req_len, op; 2877790c8c1SConrad Meyer 2887790c8c1SConrad Meyer m = *mb; 2897790c8c1SConrad Meyer ifp = m->m_pkthdr.rcvif; 2907790c8c1SConrad Meyer if (m->m_len < sizeof(struct arphdr)) { 2917790c8c1SConrad Meyer m = m_pullup(m, sizeof(struct arphdr)); 2927790c8c1SConrad Meyer *mb = m; 2937790c8c1SConrad Meyer if (m == NULL) { 2947790c8c1SConrad Meyer DNETDEBUG("runt packet: m_pullup failed\n"); 2957790c8c1SConrad Meyer return; 2967790c8c1SConrad Meyer } 2977790c8c1SConrad Meyer } 2987790c8c1SConrad Meyer 2997790c8c1SConrad Meyer ah = mtod(m, struct arphdr *); 3007790c8c1SConrad Meyer if (ntohs(ah->ar_hrd) != ARPHRD_ETHER) { 3017790c8c1SConrad Meyer DNETDEBUG("unknown hardware address 0x%2D)\n", 3027790c8c1SConrad Meyer (unsigned char *)&ah->ar_hrd, ""); 3037790c8c1SConrad Meyer return; 3047790c8c1SConrad Meyer } 3057790c8c1SConrad Meyer if (ntohs(ah->ar_pro) != ETHERTYPE_IP) { 3067790c8c1SConrad Meyer DNETDEBUG("drop ARP for unknown protocol %d\n", 3077790c8c1SConrad Meyer ntohs(ah->ar_pro)); 3087790c8c1SConrad Meyer return; 3097790c8c1SConrad Meyer } 3107790c8c1SConrad Meyer req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 3117790c8c1SConrad Meyer if (m->m_len < req_len) { 3127790c8c1SConrad Meyer m = m_pullup(m, req_len); 3137790c8c1SConrad Meyer *mb = m; 3147790c8c1SConrad Meyer if (m == NULL) { 3157790c8c1SConrad Meyer DNETDEBUG("runt packet: m_pullup failed\n"); 3167790c8c1SConrad Meyer return; 3177790c8c1SConrad Meyer } 3187790c8c1SConrad Meyer } 3197790c8c1SConrad Meyer ah = mtod(m, struct arphdr *); 3207790c8c1SConrad Meyer 3217790c8c1SConrad Meyer op = ntohs(ah->ar_op); 3227790c8c1SConrad Meyer memcpy(&isaddr, ar_spa(ah), sizeof(isaddr)); 3237790c8c1SConrad Meyer memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr)); 3247790c8c1SConrad Meyer enaddr = (uint8_t *)IF_LLADDR(ifp); 3257790c8c1SConrad Meyer 3267790c8c1SConrad Meyer if (memcmp(ar_sha(ah), enaddr, ifp->if_addrlen) == 0) { 3277790c8c1SConrad Meyer DNETDEBUG("ignoring ARP from myself\n"); 3287790c8c1SConrad Meyer return; 3297790c8c1SConrad Meyer } 3307790c8c1SConrad Meyer 3317790c8c1SConrad Meyer if (isaddr.s_addr == pcb->dp_client) { 3327790c8c1SConrad Meyer printf("%s: %*D is using my IP address %s!\n", __func__, 3337790c8c1SConrad Meyer ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 3347790c8c1SConrad Meyer inet_ntoa_r(isaddr, buf)); 3357790c8c1SConrad Meyer return; 3367790c8c1SConrad Meyer } 3377790c8c1SConrad Meyer 3387790c8c1SConrad Meyer if (memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen) == 0) { 3397790c8c1SConrad Meyer DNETDEBUG("ignoring ARP from broadcast address\n"); 3407790c8c1SConrad Meyer return; 3417790c8c1SConrad Meyer } 3427790c8c1SConrad Meyer 3437790c8c1SConrad Meyer if (op == ARPOP_REPLY) { 3447790c8c1SConrad Meyer if (isaddr.s_addr != pcb->dp_gateway && 3457790c8c1SConrad Meyer isaddr.s_addr != pcb->dp_server) { 3467790c8c1SConrad Meyer inet_ntoa_r(isaddr, buf); 3477790c8c1SConrad Meyer DNETDEBUG("ignoring ARP reply from %s (not configured" 3487790c8c1SConrad Meyer " server or gateway)\n", buf); 3497790c8c1SConrad Meyer return; 3507790c8c1SConrad Meyer } 3517790c8c1SConrad Meyer memcpy(pcb->dp_gw_mac.octet, ar_sha(ah), 3527790c8c1SConrad Meyer min(ah->ar_hln, ETHER_ADDR_LEN)); 3537790c8c1SConrad Meyer 3547790c8c1SConrad Meyer DNETDEBUG("got server MAC address %6D\n", 3557790c8c1SConrad Meyer pcb->dp_gw_mac.octet, ":"); 3567790c8c1SConrad Meyer 3577790c8c1SConrad Meyer MPASS(pcb->dp_state == DN_STATE_INIT); 3587790c8c1SConrad Meyer pcb->dp_state = DN_STATE_HAVE_GW_MAC; 3597790c8c1SConrad Meyer return; 3607790c8c1SConrad Meyer } 3617790c8c1SConrad Meyer 3627790c8c1SConrad Meyer if (op != ARPOP_REQUEST) { 3637790c8c1SConrad Meyer DNETDEBUG("ignoring ARP non-request/reply\n"); 3647790c8c1SConrad Meyer return; 3657790c8c1SConrad Meyer } 3667790c8c1SConrad Meyer 3677790c8c1SConrad Meyer if (itaddr.s_addr != pcb->dp_client) { 3687790c8c1SConrad Meyer DNETDEBUG("ignoring ARP not to our IP\n"); 3697790c8c1SConrad Meyer return; 3707790c8c1SConrad Meyer } 3717790c8c1SConrad Meyer 3727790c8c1SConrad Meyer memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 3737790c8c1SConrad Meyer memcpy(ar_sha(ah), enaddr, ah->ar_hln); 3747790c8c1SConrad Meyer memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 3757790c8c1SConrad Meyer memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 3767790c8c1SConrad Meyer ah->ar_op = htons(ARPOP_REPLY); 3777790c8c1SConrad Meyer ah->ar_pro = htons(ETHERTYPE_IP); 3787790c8c1SConrad Meyer m->m_flags &= ~(M_BCAST|M_MCAST); 3797790c8c1SConrad Meyer m->m_len = arphdr_len(ah); 3807790c8c1SConrad Meyer m->m_pkthdr.len = m->m_len; 3817790c8c1SConrad Meyer 3827790c8c1SConrad Meyer memcpy(dst.octet, ar_tha(ah), ETHER_ADDR_LEN); 3837790c8c1SConrad Meyer debugnet_ether_output(m, ifp, dst, ETHERTYPE_ARP); 3847790c8c1SConrad Meyer *mb = NULL; 3857790c8c1SConrad Meyer } 3867790c8c1SConrad Meyer 3877790c8c1SConrad Meyer /* 3887790c8c1SConrad Meyer * Sends ARP requests to locate the server and waits for a response. 3897790c8c1SConrad Meyer * We first try to ARP the server itself, and fall back to the provided 3907790c8c1SConrad Meyer * gateway if the server appears to be off-link. 3917790c8c1SConrad Meyer * 3927790c8c1SConrad Meyer * Return value: 3937790c8c1SConrad Meyer * 0 on success 3947790c8c1SConrad Meyer * errno on error 3957790c8c1SConrad Meyer */ 3967790c8c1SConrad Meyer int 3977790c8c1SConrad Meyer debugnet_arp_gw(struct debugnet_pcb *pcb) 3987790c8c1SConrad Meyer { 3997790c8c1SConrad Meyer in_addr_t dst; 4007790c8c1SConrad Meyer int error, polls, retries; 4017790c8c1SConrad Meyer 4027790c8c1SConrad Meyer dst = pcb->dp_server; 4037790c8c1SConrad Meyer restart: 4047790c8c1SConrad Meyer for (retries = 0; retries < debugnet_arp_nretries; retries++) { 4057790c8c1SConrad Meyer error = debugnet_send_arp(pcb, dst); 4067790c8c1SConrad Meyer if (error != 0) 4077790c8c1SConrad Meyer return (error); 4087790c8c1SConrad Meyer for (polls = 0; polls < debugnet_npolls && 4097790c8c1SConrad Meyer pcb->dp_state < DN_STATE_HAVE_GW_MAC; polls++) { 4107790c8c1SConrad Meyer debugnet_network_poll(pcb->dp_ifp); 4117790c8c1SConrad Meyer DELAY(500); 4127790c8c1SConrad Meyer } 4137790c8c1SConrad Meyer if (pcb->dp_state >= DN_STATE_HAVE_GW_MAC) 4147790c8c1SConrad Meyer break; 4157790c8c1SConrad Meyer printf("(ARP retry)"); 4167790c8c1SConrad Meyer } 4177790c8c1SConrad Meyer if (pcb->dp_state >= DN_STATE_HAVE_GW_MAC) 4187790c8c1SConrad Meyer return (0); 4197790c8c1SConrad Meyer if (dst == pcb->dp_server) { 4207790c8c1SConrad Meyer printf("\nFailed to ARP server"); 4217790c8c1SConrad Meyer if (pcb->dp_gateway != INADDR_ANY) { 4227790c8c1SConrad Meyer printf(", trying to reach gateway...\n"); 4237790c8c1SConrad Meyer dst = pcb->dp_gateway; 4247790c8c1SConrad Meyer goto restart; 4257790c8c1SConrad Meyer } else 4267790c8c1SConrad Meyer printf(".\n"); 4277790c8c1SConrad Meyer } else 4287790c8c1SConrad Meyer printf("\nFailed to ARP gateway.\n"); 4297790c8c1SConrad Meyer 4307790c8c1SConrad Meyer return (ETIMEDOUT); 4317790c8c1SConrad Meyer } 4327790c8c1SConrad Meyer 4337790c8c1SConrad Meyer /* 4347790c8c1SConrad Meyer * Unreliable IPv4 transmission of an mbuf chain to the debugnet server 4357790c8c1SConrad Meyer * Note: can't handle fragmentation; fails if the packet is larger than 4367790c8c1SConrad Meyer * ifp->if_mtu after adding the UDP/IP headers 4377790c8c1SConrad Meyer * 4387790c8c1SConrad Meyer * Parameters: 4397790c8c1SConrad Meyer * pcb The debugnet context block 4407790c8c1SConrad Meyer * m mbuf chain 4417790c8c1SConrad Meyer * 4427790c8c1SConrad Meyer * Returns: 4437790c8c1SConrad Meyer * int see errno.h, 0 for success 4447790c8c1SConrad Meyer */ 4457790c8c1SConrad Meyer int 4467790c8c1SConrad Meyer debugnet_ip_output(struct debugnet_pcb *pcb, struct mbuf *m) 4477790c8c1SConrad Meyer { 4487790c8c1SConrad Meyer struct udphdr *udp; 4497790c8c1SConrad Meyer struct ifnet *ifp; 4507790c8c1SConrad Meyer struct ip *ip; 4517790c8c1SConrad Meyer 4527790c8c1SConrad Meyer MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC); 4537790c8c1SConrad Meyer 4547790c8c1SConrad Meyer ifp = pcb->dp_ifp; 4557790c8c1SConrad Meyer 4567790c8c1SConrad Meyer M_PREPEND(m, sizeof(*ip), M_NOWAIT); 4577790c8c1SConrad Meyer if (m == NULL) { 4587790c8c1SConrad Meyer printf("%s: out of mbufs\n", __func__); 4597790c8c1SConrad Meyer return (ENOBUFS); 4607790c8c1SConrad Meyer } 4617790c8c1SConrad Meyer 4627790c8c1SConrad Meyer if (m->m_pkthdr.len > ifp->if_mtu) { 4637790c8c1SConrad Meyer printf("%s: Packet is too big: %d > MTU %u\n", __func__, 4647790c8c1SConrad Meyer m->m_pkthdr.len, ifp->if_mtu); 4657790c8c1SConrad Meyer m_freem(m); 4667790c8c1SConrad Meyer return (ENOBUFS); 4677790c8c1SConrad Meyer } 4687790c8c1SConrad Meyer 4697790c8c1SConrad Meyer ip = mtod(m, void *); 4707790c8c1SConrad Meyer udp = (void *)(ip + 1); 4717790c8c1SConrad Meyer 4727790c8c1SConrad Meyer memset(ip, 0, offsetof(struct ip, ip_p)); 4737790c8c1SConrad Meyer ip->ip_p = IPPROTO_UDP; 4747790c8c1SConrad Meyer ip->ip_sum = udp->uh_ulen; 4757790c8c1SConrad Meyer ip->ip_src = (struct in_addr) { pcb->dp_client }; 4767790c8c1SConrad Meyer ip->ip_dst = (struct in_addr) { pcb->dp_server }; 4777790c8c1SConrad Meyer 4787790c8c1SConrad Meyer /* Compute UDP-IPv4 checksum. */ 4797790c8c1SConrad Meyer udp->uh_sum = in_cksum(m, m->m_pkthdr.len); 4807790c8c1SConrad Meyer if (udp->uh_sum == 0) 4817790c8c1SConrad Meyer udp->uh_sum = 0xffff; 4827790c8c1SConrad Meyer 4837790c8c1SConrad Meyer ip->ip_v = IPVERSION; 4847790c8c1SConrad Meyer ip->ip_hl = sizeof(*ip) >> 2; 4857790c8c1SConrad Meyer ip->ip_tos = 0; 4867790c8c1SConrad Meyer ip->ip_len = htons(m->m_pkthdr.len); 4877790c8c1SConrad Meyer ip->ip_id = 0; 4887790c8c1SConrad Meyer ip->ip_off = htons(IP_DF); 4897790c8c1SConrad Meyer ip->ip_ttl = 255; 4907790c8c1SConrad Meyer ip->ip_sum = 0; 4917790c8c1SConrad Meyer ip->ip_sum = in_cksum(m, sizeof(struct ip)); 4927790c8c1SConrad Meyer 4937790c8c1SConrad Meyer return (debugnet_ether_output(m, ifp, pcb->dp_gw_mac, ETHERTYPE_IP)); 4947790c8c1SConrad Meyer } 495