xref: /freebsd/sys/net/debugnet.c (revision e9c6962599fa512b8dcbb4a131f9c945c8448a3e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Isilon Systems, LLC.
5  * Copyright (c) 2005-2014 Sandvine Incorporated. All rights reserved.
6  * Copyright (c) 2000 Darrell Anderson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_ddb.h"
35 #include "opt_inet.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/endian.h>
40 #include <sys/errno.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 
44 #ifdef DDB
45 #include <ddb/ddb.h>
46 #include <ddb/db_lex.h>
47 #endif
48 
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/if_var.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_options.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64 
65 #include <machine/in_cksum.h>
66 #include <machine/pcb.h>
67 
68 #include <net/debugnet.h>
69 #define	DEBUGNET_INTERNAL
70 #include <net/debugnet_int.h>
71 
72 FEATURE(debugnet, "Debugnet support");
73 
74 SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD, NULL,
75     "debugnet parameters");
76 
77 unsigned debugnet_debug;
78 SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
79     &debugnet_debug, 0,
80     "Debug message verbosity (0: off; 1: on; 2: verbose)");
81 
82 int debugnet_npolls = 2000;
83 SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
84     &debugnet_npolls, 0,
85     "Number of times to poll before assuming packet loss (0.5ms per poll)");
86 int debugnet_nretries = 10;
87 SYSCTL_INT(_net_debugnet, OID_AUTO, nretries, CTLFLAG_RWTUN,
88     &debugnet_nretries, 0,
89     "Number of retransmit attempts before giving up");
90 
91 static bool g_debugnet_pcb_inuse;
92 static struct debugnet_pcb g_dnet_pcb;
93 
94 /*
95  * Simple accessors for opaque PCB.
96  */
97 const unsigned char *
98 debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
99 {
100 	MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
101 	    pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
102 	return (pcb->dp_gw_mac.octet);
103 }
104 
105 /*
106  * Start of network primitives, beginning with output primitives.
107  */
108 
109 /*
110  * Handles creation of the ethernet header, then places outgoing packets into
111  * the tx buffer for the NIC
112  *
113  * Parameters:
114  *	m	The mbuf containing the packet to be sent (will be freed by
115  *		this function or the NIC driver)
116  *	ifp	The interface to send on
117  *	dst	The destination ethernet address (source address will be looked
118  *		up using ifp)
119  *	etype	The ETHERTYPE_* value for the protocol that is being sent
120  *
121  * Returns:
122  *	int	see errno.h, 0 for success
123  */
124 int
125 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
126     u_short etype)
127 {
128 	struct ether_header *eh;
129 
130 	if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
131 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
132 		if_printf(ifp, "%s: interface isn't up\n", __func__);
133 		m_freem(m);
134 		return (ENETDOWN);
135 	}
136 
137 	/* Fill in the ethernet header. */
138 	M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
139 	if (m == NULL) {
140 		printf("%s: out of mbufs\n", __func__);
141 		return (ENOBUFS);
142 	}
143 	eh = mtod(m, struct ether_header *);
144 	memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
145 	memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
146 	eh->ether_type = htons(etype);
147 	return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
148 }
149 
150 /*
151  * Unreliable transmission of an mbuf chain to the debugnet server
152  * Note: can't handle fragmentation; fails if the packet is larger than
153  *	 ifp->if_mtu after adding the UDP/IP headers
154  *
155  * Parameters:
156  *	pcb	The debugnet context block
157  *	m	mbuf chain
158  *
159  * Returns:
160  *	int	see errno.h, 0 for success
161  */
162 static int
163 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
164 {
165 	struct udphdr *udp;
166 
167 	MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
168 
169 	M_PREPEND(m, sizeof(*udp), M_NOWAIT);
170 	if (m == NULL) {
171 		printf("%s: out of mbufs\n", __func__);
172 		return (ENOBUFS);
173 	}
174 
175 	udp = mtod(m, void *);
176 	udp->uh_ulen = htons(m->m_pkthdr.len);
177 	/* Use this src port so that the server can connect() the socket */
178 	udp->uh_sport = htons(pcb->dp_client_port);
179 	udp->uh_dport = htons(pcb->dp_server_port);
180 	/* Computed later (protocol-dependent). */
181 	udp->uh_sum = 0;
182 
183 	return (debugnet_ip_output(pcb, m));
184 }
185 
186 static int
187 debugnet_ack_output(struct debugnet_pcb *pcb, uint32_t seqno /* net endian */)
188 {
189 	struct debugnet_ack *dn_ack;
190 	struct mbuf *m;
191 
192 	DNETDEBUG("Acking with seqno %u\n", ntohl(seqno));
193 
194 	m = m_gethdr(M_NOWAIT, MT_DATA);
195 	if (m == NULL) {
196 		printf("%s: Out of mbufs\n", __func__);
197 		return (ENOBUFS);
198 	}
199 	m->m_len = sizeof(*dn_ack);
200 	m->m_pkthdr.len = sizeof(*dn_ack);
201 	MH_ALIGN(m, sizeof(*dn_ack));
202 	dn_ack = mtod(m, void *);
203 	dn_ack->da_seqno = seqno;
204 
205 	return (debugnet_udp_output(pcb, m));
206 }
207 
208 /*
209  * Dummy free function for debugnet clusters.
210  */
211 static void
212 debugnet_mbuf_free(struct mbuf *m __unused)
213 {
214 }
215 
216 /*
217  * Construct and reliably send a debugnet packet.  May fail from a resource
218  * shortage or extreme number of unacknowledged retransmissions.  Wait for
219  * an acknowledgement before returning.  Splits packets into chunks small
220  * enough to be sent without fragmentation (looks up the interface MTU)
221  *
222  * Parameters:
223  *	type	debugnet packet type (HERALD, FINISHED, ...)
224  *	data	data
225  *	datalen	data size (bytes)
226  *	auxdata	optional auxiliary information
227  *
228  * Returns:
229  *	int see errno.h, 0 for success
230  */
231 int
232 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
233     uint32_t datalen, const struct debugnet_proto_aux *auxdata)
234 {
235 	struct debugnet_msg_hdr *dn_msg_hdr;
236 	struct mbuf *m, *m2;
237 	uint64_t want_acks;
238 	uint32_t i, pktlen, sent_so_far;
239 	int retries, polls, error;
240 
241 	if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
242 		return (ECONNRESET);
243 
244 	want_acks = 0;
245 	pcb->dp_rcvd_acks = 0;
246 	retries = 0;
247 
248 retransmit:
249 	/* Chunks can be too big to fit in packets. */
250 	for (i = sent_so_far = 0; sent_so_far < datalen ||
251 	    (i == 0 && datalen == 0); i++) {
252 		pktlen = datalen - sent_so_far;
253 
254 		/* Bound: the interface MTU (assume no IP options). */
255 		pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
256 		    sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
257 
258 		/*
259 		 * Check if it is retransmitting and this has been ACKed
260 		 * already.
261 		 */
262 		if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
263 			sent_so_far += pktlen;
264 			continue;
265 		}
266 
267 		/*
268 		 * Get and fill a header mbuf, then chain data as an extended
269 		 * mbuf.
270 		 */
271 		m = m_gethdr(M_NOWAIT, MT_DATA);
272 		if (m == NULL) {
273 			printf("%s: Out of mbufs\n", __func__);
274 			return (ENOBUFS);
275 		}
276 		m->m_len = sizeof(struct debugnet_msg_hdr);
277 		m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
278 		MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
279 		dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
280 		dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
281 		dn_msg_hdr->mh_type = htonl(type);
282 		dn_msg_hdr->mh_len = htonl(pktlen);
283 
284 		if (auxdata != NULL) {
285 			dn_msg_hdr->mh_offset =
286 			    htobe64(auxdata->dp_offset_start + sent_so_far);
287 			dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
288 		} else {
289 			dn_msg_hdr->mh_offset = htobe64(sent_so_far);
290 			dn_msg_hdr->mh_aux2 = 0;
291 		}
292 
293 		if (pktlen != 0) {
294 			m2 = m_get(M_NOWAIT, MT_DATA);
295 			if (m2 == NULL) {
296 				m_freem(m);
297 				printf("%s: Out of mbufs\n", __func__);
298 				return (ENOBUFS);
299 			}
300 			MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
301 			    pktlen, debugnet_mbuf_free, NULL, NULL, 0,
302 			    EXT_DISPOSABLE);
303 			m2->m_len = pktlen;
304 
305 			m_cat(m, m2);
306 			m->m_pkthdr.len += pktlen;
307 		}
308 		error = debugnet_udp_output(pcb, m);
309 		if (error != 0)
310 			return (error);
311 
312 		/* Note that we're waiting for this packet in the bitfield. */
313 		want_acks |= (1 << i);
314 		sent_so_far += pktlen;
315 	}
316 	if (i >= DEBUGNET_MAX_IN_FLIGHT)
317 		printf("Warning: Sent more than %d packets (%d). "
318 		    "Acknowledgements will fail unless the size of "
319 		    "rcvd_acks/want_acks is increased.\n",
320 		    DEBUGNET_MAX_IN_FLIGHT, i);
321 
322 	/*
323 	 * Wait for acks.  A *real* window would speed things up considerably.
324 	 */
325 	polls = 0;
326 	while (pcb->dp_rcvd_acks != want_acks) {
327 		if (polls++ > debugnet_npolls) {
328 			if (retries++ > debugnet_nretries)
329 				return (ETIMEDOUT);
330 			printf(". ");
331 			goto retransmit;
332 		}
333 		debugnet_network_poll(pcb->dp_ifp);
334 		DELAY(500);
335 		if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
336 			return (ECONNRESET);
337 	}
338 	pcb->dp_seqno += i;
339 	return (0);
340 }
341 
342 /*
343  * Network input primitives.
344  */
345 
346 /*
347  * Just introspect the header enough to fire off a seqno ack and validate
348  * length fits.
349  */
350 static void
351 debugnet_handle_rx_msg(struct debugnet_pcb *pcb, struct mbuf **mb)
352 {
353 	const struct debugnet_msg_hdr *dnh;
354 	struct mbuf *m;
355 	int error;
356 
357 	m = *mb;
358 
359 	if (m->m_pkthdr.len < sizeof(*dnh)) {
360 		DNETDEBUG("ignoring small debugnet_msg packet\n");
361 		return;
362 	}
363 
364 	/* Get ND header. */
365 	if (m->m_len < sizeof(*dnh)) {
366 		m = m_pullup(m, sizeof(*dnh));
367 		*mb = m;
368 		if (m == NULL) {
369 			DNETDEBUG("m_pullup failed\n");
370 			return;
371 		}
372 	}
373 	dnh = mtod(m, const void *);
374 
375 	if (ntohl(dnh->mh_len) + sizeof(*dnh) > m->m_pkthdr.len) {
376 		DNETDEBUG("Dropping short packet.\n");
377 		return;
378 	}
379 
380 	/*
381 	 * If the issue is transient (ENOBUFS), sender should resend.  If
382 	 * non-transient (like driver objecting to rx -> tx from the same
383 	 * thread), not much else we can do.
384 	 */
385 	error = debugnet_ack_output(pcb, dnh->mh_seqno);
386 	if (error != 0)
387 		return;
388 
389 	if (ntohl(dnh->mh_type) == DEBUGNET_FINISHED) {
390 		printf("Remote shut down the connection on us!\n");
391 		pcb->dp_state = DN_STATE_REMOTE_CLOSED;
392 
393 		/*
394 		 * Continue through to the user handler so they are signalled
395 		 * not to wait for further rx.
396 		 */
397 	}
398 
399 	pcb->dp_rx_handler(pcb, mb);
400 }
401 
402 static void
403 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
404 {
405 	const struct debugnet_ack *dn_ack;
406 	struct mbuf *m;
407 	uint32_t rcv_ackno;
408 
409 	m = *mb;
410 
411 	/* Get Ack. */
412 	if (m->m_len < sizeof(*dn_ack)) {
413 		m = m_pullup(m, sizeof(*dn_ack));
414 		*mb = m;
415 		if (m == NULL) {
416 			DNETDEBUG("m_pullup failed\n");
417 			return;
418 		}
419 	}
420 	dn_ack = mtod(m, const void *);
421 
422 	/* Debugnet processing. */
423 	/*
424 	 * Packet is meant for us.  Extract the ack sequence number and the
425 	 * port number if necessary.
426 	 */
427 	rcv_ackno = ntohl(dn_ack->da_seqno);
428 	if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
429 		pcb->dp_server_port = sport;
430 		pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
431 	}
432 	if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
433 		printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
434 	else if (rcv_ackno >= pcb->dp_seqno) {
435 		/* We're interested in this ack. Record it. */
436 		pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
437 	}
438 }
439 
440 void
441 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
442 {
443 	const struct udphdr *udp;
444 	struct mbuf *m;
445 	uint16_t sport, ulen;
446 
447 	/* UDP processing. */
448 
449 	m = *mb;
450 	if (m->m_pkthdr.len < sizeof(*udp)) {
451 		DNETDEBUG("ignoring small UDP packet\n");
452 		return;
453 	}
454 
455 	/* Get UDP headers. */
456 	if (m->m_len < sizeof(*udp)) {
457 		m = m_pullup(m, sizeof(*udp));
458 		*mb = m;
459 		if (m == NULL) {
460 			DNETDEBUG("m_pullup failed\n");
461 			return;
462 		}
463 	}
464 	udp = mtod(m, const void *);
465 
466 	/* We expect to receive UDP packets on the configured client port. */
467 	if (ntohs(udp->uh_dport) != pcb->dp_client_port) {
468 		DNETDEBUG("not on the expected port.\n");
469 		return;
470 	}
471 
472 	/* Check that ulen does not exceed actual size of data. */
473 	ulen = ntohs(udp->uh_ulen);
474 	if (m->m_pkthdr.len < ulen) {
475 		DNETDEBUG("ignoring runt UDP packet\n");
476 		return;
477 	}
478 
479 	sport = ntohs(udp->uh_sport);
480 
481 	m_adj(m, sizeof(*udp));
482 	ulen -= sizeof(*udp);
483 
484 	if (ulen == sizeof(struct debugnet_ack)) {
485 		debugnet_handle_ack(pcb, mb, sport);
486 		return;
487 	}
488 
489 	if (pcb->dp_rx_handler == NULL) {
490 		if (ulen < sizeof(struct debugnet_ack))
491 			DNETDEBUG("ignoring small ACK packet\n");
492 		else
493 			DNETDEBUG("ignoring unexpected non-ACK packet on "
494 			    "half-duplex connection.\n");
495 		return;
496 	}
497 
498 	debugnet_handle_rx_msg(pcb, mb);
499 }
500 
501 /*
502  * Handler for incoming packets directly from the network adapter
503  * Identifies the packet type (IP or ARP) and passes it along to one of the
504  * helper functions debugnet_handle_ip or debugnet_handle_arp.
505  *
506  * It needs to partially replicate the behaviour of ether_input() and
507  * ether_demux().
508  *
509  * Parameters:
510  *	ifp	the interface the packet came from
511  *	m	an mbuf containing the packet received
512  */
513 static void
514 debugnet_pkt_in(struct ifnet *ifp, struct mbuf *m)
515 {
516 	struct ifreq ifr;
517 	struct ether_header *eh;
518 	u_short etype;
519 
520 	/* Ethernet processing. */
521 	if ((m->m_flags & M_PKTHDR) == 0) {
522 		DNETDEBUG_IF(ifp, "discard frame without packet header\n");
523 		goto done;
524 	}
525 	if (m->m_len < ETHER_HDR_LEN) {
526 		DNETDEBUG_IF(ifp,
527 	    "discard frame without leading eth header (len %u pktlen %u)\n",
528 		    m->m_len, m->m_pkthdr.len);
529 		goto done;
530 	}
531 	if ((m->m_flags & M_HASFCS) != 0) {
532 		m_adj(m, -ETHER_CRC_LEN);
533 		m->m_flags &= ~M_HASFCS;
534 	}
535 	eh = mtod(m, struct ether_header *);
536 	etype = ntohs(eh->ether_type);
537 	if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
538 		DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
539 		goto done;
540 	}
541 	if (if_gethwaddr(ifp, &ifr) != 0) {
542 		DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
543 		goto done;
544 	}
545 	if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
546 	    ETHER_ADDR_LEN) != 0 &&
547 	    (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
548 		DNETDEBUG_IF(ifp,
549 		    "discard frame with incorrect destination addr\n");
550 		goto done;
551 	}
552 
553 	MPASS(g_debugnet_pcb_inuse);
554 
555 	/* Done ethernet processing. Strip off the ethernet header. */
556 	m_adj(m, ETHER_HDR_LEN);
557 	switch (etype) {
558 	case ETHERTYPE_ARP:
559 		debugnet_handle_arp(&g_dnet_pcb, &m);
560 		break;
561 	case ETHERTYPE_IP:
562 		debugnet_handle_ip(&g_dnet_pcb, &m);
563 		break;
564 	default:
565 		DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
566 		break;
567 	}
568 done:
569 	if (m != NULL)
570 		m_freem(m);
571 }
572 
573 /*
574  * Network polling primitive.
575  *
576  * Instead of assuming that most of the network stack is sane, we just poll the
577  * driver directly for packets.
578  */
579 void
580 debugnet_network_poll(struct ifnet *ifp)
581 {
582 	ifp->if_debugnet_methods->dn_poll(ifp, 1000);
583 }
584 
585 /*
586  * Start of consumer API surface.
587  */
588 void
589 debugnet_free(struct debugnet_pcb *pcb)
590 {
591 	struct ifnet *ifp;
592 
593 	MPASS(g_debugnet_pcb_inuse);
594 	MPASS(pcb == &g_dnet_pcb);
595 
596 	ifp = pcb->dp_ifp;
597 	if (ifp != NULL) {
598 		if (pcb->dp_drv_input != NULL)
599 			ifp->if_input = pcb->dp_drv_input;
600 		if (pcb->dp_event_started)
601 			ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
602 	}
603 	debugnet_mbuf_finish();
604 
605 	g_debugnet_pcb_inuse = false;
606 	memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
607 }
608 
609 int
610 debugnet_connect(const struct debugnet_conn_params *dcp,
611     struct debugnet_pcb **pcb_out)
612 {
613 	struct debugnet_pcb *pcb;
614 	struct ifnet *ifp;
615 	int error;
616 
617 	if (g_debugnet_pcb_inuse) {
618 		printf("%s: Only one connection at a time.\n", __func__);
619 		return (EBUSY);
620 	}
621 
622 	pcb = &g_dnet_pcb;
623 	*pcb = (struct debugnet_pcb) {
624 		.dp_state = DN_STATE_INIT,
625 		.dp_client = dcp->dc_client,
626 		.dp_server = dcp->dc_server,
627 		.dp_gateway = dcp->dc_gateway,
628 		.dp_server_port = dcp->dc_herald_port,	/* Initially */
629 		.dp_client_port = dcp->dc_client_port,
630 		.dp_seqno = 1,
631 		.dp_ifp = dcp->dc_ifp,
632 		.dp_rx_handler = dcp->dc_rx_handler,
633 	};
634 
635 	/* Switch to the debugnet mbuf zones. */
636 	debugnet_mbuf_start();
637 
638 	/* At least one needed parameter is missing; infer it. */
639 	if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
640 	    pcb->dp_ifp == NULL) {
641 		struct sockaddr_in dest_sin, *gw_sin, *local_sin;
642 		struct rtentry *dest_rt;
643 		struct ifnet *rt_ifp;
644 
645 		memset(&dest_sin, 0, sizeof(dest_sin));
646 		dest_sin = (struct sockaddr_in) {
647 			.sin_len = sizeof(dest_sin),
648 			.sin_family = AF_INET,
649 			.sin_addr.s_addr = pcb->dp_server,
650 		};
651 
652 		CURVNET_SET(vnet0);
653 		dest_rt = rtalloc1((struct sockaddr *)&dest_sin, 0,
654 		    RTF_RNH_LOCKED);
655 		CURVNET_RESTORE();
656 
657 		if (dest_rt == NULL) {
658 			db_printf("%s: Could not get route for that server.\n",
659 			    __func__);
660 			error = ENOENT;
661 			goto cleanup;
662 		}
663 
664 		if (dest_rt->rt_gateway->sa_family == AF_INET)
665 			gw_sin = (struct sockaddr_in *)dest_rt->rt_gateway;
666 		else {
667 			if (dest_rt->rt_gateway->sa_family == AF_LINK)
668 				DNETDEBUG("Destination address is on link.\n");
669 			gw_sin = NULL;
670 		}
671 
672 		MPASS(dest_rt->rt_ifa->ifa_addr->sa_family == AF_INET);
673 		local_sin = (struct sockaddr_in *)dest_rt->rt_ifa->ifa_addr;
674 
675 		rt_ifp = dest_rt->rt_ifp;
676 
677 		if (pcb->dp_client == INADDR_ANY)
678 			pcb->dp_client = local_sin->sin_addr.s_addr;
679 		if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
680 			pcb->dp_gateway = gw_sin->sin_addr.s_addr;
681 		if (pcb->dp_ifp == NULL)
682 			pcb->dp_ifp = rt_ifp;
683 
684 		RTFREE_LOCKED(dest_rt);
685 	}
686 
687 	ifp = pcb->dp_ifp;
688 
689 	if (debugnet_debug > 0) {
690 		char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
691 		    gwbuf[INET_ADDRSTRLEN];
692 		inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
693 		inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
694 		if (pcb->dp_gateway != INADDR_ANY)
695 			inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
696 		DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
697 		    serbuf, pcb->dp_server_port,
698 		    (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
699 		    (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
700 		    clibuf, pcb->dp_client_port, if_name(ifp));
701 	}
702 
703 	/* Validate iface is online and supported. */
704 	if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
705 		printf("%s: interface '%s' does not support debugnet\n",
706 		    __func__, if_name(ifp));
707 		error = ENODEV;
708 		goto cleanup;
709 	}
710 	if ((if_getflags(ifp) & IFF_UP) == 0) {
711 		printf("%s: interface '%s' link is down\n", __func__,
712 		    if_name(ifp));
713 		error = ENXIO;
714 		goto cleanup;
715 	}
716 
717 	ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
718 	pcb->dp_event_started = true;
719 
720 	/*
721 	 * We maintain the invariant that g_debugnet_pcb_inuse is always true
722 	 * while the debugnet ifp's if_input is overridden with
723 	 * debugnet_pkt_in.
724 	 */
725 	g_debugnet_pcb_inuse = true;
726 
727 	/* Make the card use *our* receive callback. */
728 	pcb->dp_drv_input = ifp->if_input;
729 	ifp->if_input = debugnet_pkt_in;
730 
731 	printf("%s: searching for %s MAC...\n", __func__,
732 	    (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
733 
734 	error = debugnet_arp_gw(pcb);
735 	if (error != 0) {
736 		printf("%s: failed to locate MAC address\n", __func__);
737 		goto cleanup;
738 	}
739 	MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
740 
741 	error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
742 	    dcp->dc_herald_datalen, NULL);
743 	if (error != 0) {
744 		printf("%s: failed to herald debugnet server\n", __func__);
745 		goto cleanup;
746 	}
747 
748 	*pcb_out = pcb;
749 	return (0);
750 
751 cleanup:
752 	debugnet_free(pcb);
753 	return (error);
754 }
755 
756 /*
757  * Pre-allocated dump-time mbuf tracking.
758  *
759  * We just track the high water mark we've ever seen and allocate appropriately
760  * for that iface/mtu combo.
761  */
762 static struct {
763 	int nmbuf;
764 	int ncl;
765 	int clsize;
766 } dn_hwm;
767 static struct mtx dn_hwm_lk;
768 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
769 
770 static void
771 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
772 {
773 	bool any;
774 
775 	any = false;
776 	mtx_lock(&dn_hwm_lk);
777 
778 	if (nmbuf > dn_hwm.nmbuf) {
779 		any = true;
780 		dn_hwm.nmbuf = nmbuf;
781 	} else
782 		nmbuf = dn_hwm.nmbuf;
783 
784 	if (ncl > dn_hwm.ncl) {
785 		any = true;
786 		dn_hwm.ncl = ncl;
787 	} else
788 		ncl = dn_hwm.ncl;
789 
790 	if (clsize > dn_hwm.clsize) {
791 		any = true;
792 		dn_hwm.clsize = clsize;
793 	} else
794 		clsize = dn_hwm.clsize;
795 
796 	mtx_unlock(&dn_hwm_lk);
797 
798 	if (any)
799 		debugnet_mbuf_reinit(nmbuf, ncl, clsize);
800 }
801 
802 void
803 debugnet_any_ifnet_update(struct ifnet *ifp)
804 {
805 	int clsize, nmbuf, ncl, nrxr;
806 
807 	if (!DEBUGNET_SUPPORTED_NIC(ifp))
808 		return;
809 
810 	ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
811 	KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
812 
813 	/*
814 	 * We need two headers per message on the transmit side. Multiply by
815 	 * four to give us some breathing room.
816 	 */
817 	nmbuf = ncl * (4 + nrxr);
818 	ncl *= nrxr;
819 
820 	dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
821 }
822 
823 /*
824  * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
825  * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
826  *
827  * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
828  * because the driver is still in attach.  Since we cannot use down interfaces,
829  * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient?  ... Nope, at least
830  * with vtnet and dhcpclient that event just never occurs.
831  *
832  * So that's how I've landed on the lower level ifnet_link_event.
833  */
834 
835 static void
836 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
837 {
838 	if (link_state == LINK_STATE_UP)
839 		debugnet_any_ifnet_update(ifp);
840 }
841 
842 static eventhandler_tag dn_attach_cookie;
843 static void
844 dn_evh_init(void *ctx __unused)
845 {
846 	dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
847 	    dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
848 }
849 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
850 
851 /*
852  * DDB parsing helpers for debugnet(4) consumers.
853  */
854 #ifdef DDB
855 struct my_inet_opt {
856 	bool has_opt;
857 	const char *printname;
858 	in_addr_t *result;
859 };
860 
861 static int
862 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
863 {
864 	in_addr_t tmp;
865 	unsigned octet;
866 	int t;
867 
868 	tmp = 0;
869 	for (octet = 0; octet < 4; octet++) {
870 		t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
871 		if (t != tNUMBER) {
872 			db_printf("%s:%s: octet %u expected number; found %d\n",
873 			    __func__, opt->printname, octet, t);
874 			return (EINVAL);
875 		}
876 		/*
877 		 * db_lex lexes '-' distinctly from the number itself, but
878 		 * let's document that invariant.
879 		 */
880 		MPASS(db_tok_number >= 0);
881 
882 		if (db_tok_number > UINT8_MAX) {
883 			db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
884 			    opt->printname, octet, (intmax_t)db_tok_number);
885 			return (EDOM);
886 		}
887 
888 		/* Constructed host-endian and converted to network later. */
889 		tmp = (tmp << 8) | db_tok_number;
890 
891 		if (octet < 3) {
892 			t = db_read_token_flags(DRT_WSPACE);
893 			if (t != tDOT) {
894 				db_printf("%s:%s: octet %u expected '.'; found"
895 				    " %d\n", __func__, opt->printname, octet,
896 				    t);
897 				return (EINVAL);
898 			}
899 		}
900 	}
901 
902 	*opt->result = htonl(tmp);
903 	opt->has_opt = true;
904 	return (0);
905 }
906 
907 int
908 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
909 {
910 	struct ifnet *ifp;
911 	int t, error;
912 	bool want_ifp;
913 	char ch;
914 
915 	struct my_inet_opt opt_client = {
916 		.printname = "client",
917 		.result = &result->dd_client,
918 	},
919 	opt_server = {
920 		.printname = "server",
921 		.result = &result->dd_server,
922 	},
923 	opt_gateway = {
924 		.printname = "gateway",
925 		.result = &result->dd_gateway,
926 	},
927 	*cur_inet_opt;
928 
929 	ifp = NULL;
930 	memset(result, 0, sizeof(*result));
931 
932 	/*
933 	 * command [space] [-] [opt] [[space] [optarg]] ...
934 	 *
935 	 * db_command has already lexed 'command' for us.
936 	 */
937 	t = db_read_token_flags(DRT_WSPACE);
938 	if (t == tWSPACE)
939 		t = db_read_token_flags(DRT_WSPACE);
940 
941 	while (t != tEOL) {
942 		if (t != tMINUS) {
943 			db_printf("%s: Bad syntax; expected '-', got %d\n",
944 			    cmd, t);
945 			goto usage;
946 		}
947 
948 		t = db_read_token_flags(DRT_WSPACE);
949 		if (t != tIDENT) {
950 			db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
951 			    cmd, t);
952 			goto usage;
953 		}
954 
955 		if (strlen(db_tok_string) > 1) {
956 			db_printf("%s: Bad syntax; expected single option "
957 			    "flag, got '%s'\n", cmd, db_tok_string);
958 			goto usage;
959 		}
960 
961 		want_ifp = false;
962 		cur_inet_opt = NULL;
963 		switch ((ch = db_tok_string[0])) {
964 		default:
965 			DNETDEBUG("Unexpected: '%c'\n", ch);
966 			/* FALLTHROUGH */
967 		case 'h':
968 			goto usage;
969 		case 'c':
970 			cur_inet_opt = &opt_client;
971 			break;
972 		case 'g':
973 			cur_inet_opt = &opt_gateway;
974 			break;
975 		case 's':
976 			cur_inet_opt = &opt_server;
977 			break;
978 		case 'i':
979 			want_ifp = true;
980 			break;
981 		}
982 
983 		t = db_read_token_flags(DRT_WSPACE);
984 		if (t != tWSPACE) {
985 			db_printf("%s: Bad syntax; expected space after "
986 			    "flag %c, got %d\n", cmd, ch, t);
987 			goto usage;
988 		}
989 
990 		if (want_ifp) {
991 			t = db_read_token_flags(DRT_WSPACE);
992 			if (t != tIDENT) {
993 				db_printf("%s: Expected interface but got %d\n",
994 				    cmd, t);
995 				goto usage;
996 			}
997 
998 			CURVNET_SET(vnet0);
999 			/*
1000 			 * We *don't* take a ref here because the only current
1001 			 * consumer, db_netdump_cmd, does not need it.  It
1002 			 * (somewhat redundantly) extracts the if_name(),
1003 			 * re-lookups the ifp, and takes its own reference.
1004 			 */
1005 			ifp = ifunit(db_tok_string);
1006 			CURVNET_RESTORE();
1007 			if (ifp == NULL) {
1008 				db_printf("Could not locate interface %s\n",
1009 				    db_tok_string);
1010 				goto cleanup;
1011 			}
1012 		} else {
1013 			MPASS(cur_inet_opt != NULL);
1014 			/* Assume IPv4 for now. */
1015 			error = dn_parse_optarg_ipv4(cur_inet_opt);
1016 			if (error != 0)
1017 				goto cleanup;
1018 		}
1019 
1020 		/* Skip (mandatory) whitespace after option, if not EOL. */
1021 		t = db_read_token_flags(DRT_WSPACE);
1022 		if (t == tEOL)
1023 			break;
1024 		if (t != tWSPACE) {
1025 			db_printf("%s: Bad syntax; expected space after "
1026 			    "flag %c option; got %d\n", cmd, ch, t);
1027 			goto usage;
1028 		}
1029 		t = db_read_token_flags(DRT_WSPACE);
1030 	}
1031 
1032 	if (!opt_server.has_opt) {
1033 		db_printf("%s: need a destination server address\n", cmd);
1034 		goto usage;
1035 	}
1036 
1037 	result->dd_has_client = opt_client.has_opt;
1038 	result->dd_has_gateway = opt_gateway.has_opt;
1039 	result->dd_ifp = ifp;
1040 
1041 	/* We parsed the full line to tEOL already, or bailed with an error. */
1042 	return (0);
1043 
1044 usage:
1045 	db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
1046 	    "-i <interface>]\n", cmd);
1047 	error = EINVAL;
1048 	/* FALLTHROUGH */
1049 cleanup:
1050 	db_skip_to_eol();
1051 	return (error);
1052 }
1053 #endif /* DDB */
1054