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