17790c8c1SConrad Meyer /*-
24d846d26SWarner 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>
328270d35eSConrad Meyer #include "opt_ddb.h"
337790c8c1SConrad Meyer #include "opt_inet.h"
347790c8c1SConrad Meyer
357790c8c1SConrad Meyer #include <sys/param.h>
367790c8c1SConrad Meyer #include <sys/systm.h>
377790c8c1SConrad Meyer #include <sys/endian.h>
387790c8c1SConrad Meyer #include <sys/errno.h>
394ad8cb68SMichael Tuexen #include <sys/eventhandler.h>
40b1746faaSMark Johnston #include <sys/kernel.h>
41b1746faaSMark Johnston #include <sys/lock.h>
42b1746faaSMark Johnston #include <sys/mutex.h>
437790c8c1SConrad Meyer #include <sys/socket.h>
447790c8c1SConrad Meyer #include <sys/sysctl.h>
457790c8c1SConrad Meyer
468270d35eSConrad Meyer #ifdef DDB
478270d35eSConrad Meyer #include <ddb/ddb.h>
488270d35eSConrad Meyer #include <ddb/db_lex.h>
498270d35eSConrad Meyer #endif
508270d35eSConrad Meyer
517790c8c1SConrad Meyer #include <net/ethernet.h>
527790c8c1SConrad Meyer #include <net/if.h>
537790c8c1SConrad Meyer #include <net/if_arp.h>
547790c8c1SConrad Meyer #include <net/if_dl.h>
557790c8c1SConrad Meyer #include <net/if_types.h>
567790c8c1SConrad Meyer #include <net/if_var.h>
572c2b37adSJustin Hibbits #include <net/if_private.h>
58b1746faaSMark Johnston #include <net/vnet.h>
5955f57ca9SAlexander V. Chernikov #include <net/route.h>
6055f57ca9SAlexander V. Chernikov #include <net/route/nhop.h>
617790c8c1SConrad Meyer
627790c8c1SConrad Meyer #include <netinet/in.h>
6355f57ca9SAlexander V. Chernikov #include <netinet/in_fib.h>
647790c8c1SConrad Meyer #include <netinet/in_systm.h>
657790c8c1SConrad Meyer #include <netinet/in_var.h>
667790c8c1SConrad Meyer #include <netinet/ip.h>
677790c8c1SConrad Meyer #include <netinet/ip_var.h>
687790c8c1SConrad Meyer #include <netinet/ip_options.h>
697790c8c1SConrad Meyer #include <netinet/udp.h>
707790c8c1SConrad Meyer #include <netinet/udp_var.h>
717790c8c1SConrad Meyer
727790c8c1SConrad Meyer #include <machine/in_cksum.h>
737790c8c1SConrad Meyer #include <machine/pcb.h>
747790c8c1SConrad Meyer
757790c8c1SConrad Meyer #include <net/debugnet.h>
767790c8c1SConrad Meyer #define DEBUGNET_INTERNAL
777790c8c1SConrad Meyer #include <net/debugnet_int.h>
787790c8c1SConrad Meyer
797790c8c1SConrad Meyer FEATURE(debugnet, "Debugnet support");
807790c8c1SConrad Meyer
817029da5cSPawel Biernacki SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
827790c8c1SConrad Meyer "debugnet parameters");
837790c8c1SConrad Meyer
847790c8c1SConrad Meyer unsigned debugnet_debug;
857790c8c1SConrad Meyer SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
867790c8c1SConrad Meyer &debugnet_debug, 0,
877790c8c1SConrad Meyer "Debug message verbosity (0: off; 1: on; 2: verbose)");
887790c8c1SConrad Meyer
897790c8c1SConrad Meyer int debugnet_npolls = 2000;
907790c8c1SConrad Meyer SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
917790c8c1SConrad Meyer &debugnet_npolls, 0,
927790c8c1SConrad Meyer "Number of times to poll before assuming packet loss (0.5ms per poll)");
937790c8c1SConrad Meyer int debugnet_nretries = 10;
947790c8c1SConrad Meyer SYSCTL_INT(_net_debugnet, OID_AUTO, nretries, CTLFLAG_RWTUN,
957790c8c1SConrad Meyer &debugnet_nretries, 0,
967790c8c1SConrad Meyer "Number of retransmit attempts before giving up");
97caf2f627SAlexander V. Chernikov int debugnet_fib = RT_DEFAULT_FIB;
98caf2f627SAlexander V. Chernikov SYSCTL_INT(_net_debugnet, OID_AUTO, fib, CTLFLAG_RWTUN,
99caf2f627SAlexander V. Chernikov &debugnet_fib, 0,
100caf2f627SAlexander V. Chernikov "Fib to use when sending dump");
1017790c8c1SConrad Meyer
1027790c8c1SConrad Meyer static bool g_debugnet_pcb_inuse;
1037790c8c1SConrad Meyer static struct debugnet_pcb g_dnet_pcb;
1047790c8c1SConrad Meyer
1057790c8c1SConrad Meyer /*
1067790c8c1SConrad Meyer * Simple accessors for opaque PCB.
1077790c8c1SConrad Meyer */
1087790c8c1SConrad Meyer const unsigned char *
debugnet_get_gw_mac(const struct debugnet_pcb * pcb)1097790c8c1SConrad Meyer debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
1107790c8c1SConrad Meyer {
1117790c8c1SConrad Meyer MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
1127790c8c1SConrad Meyer pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
1137790c8c1SConrad Meyer return (pcb->dp_gw_mac.octet);
1147790c8c1SConrad Meyer }
1157790c8c1SConrad Meyer
116b498331bSJohn Reimer const in_addr_t *
debugnet_get_server_addr(const struct debugnet_pcb * pcb)117b498331bSJohn Reimer debugnet_get_server_addr(const struct debugnet_pcb *pcb)
118b498331bSJohn Reimer {
119b498331bSJohn Reimer MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
120b498331bSJohn Reimer pcb->dp_state >= DN_STATE_GOT_HERALD_PORT);
121b498331bSJohn Reimer return (&pcb->dp_server);
122b498331bSJohn Reimer }
123b498331bSJohn Reimer
124b498331bSJohn Reimer const uint16_t
debugnet_get_server_port(const struct debugnet_pcb * pcb)125b498331bSJohn Reimer debugnet_get_server_port(const struct debugnet_pcb *pcb)
126b498331bSJohn Reimer {
127b498331bSJohn Reimer MPASS(g_debugnet_pcb_inuse && pcb == &g_dnet_pcb &&
128b498331bSJohn Reimer pcb->dp_state >= DN_STATE_GOT_HERALD_PORT);
129b498331bSJohn Reimer return (pcb->dp_server_port);
130b498331bSJohn Reimer }
131b498331bSJohn Reimer
1327790c8c1SConrad Meyer /*
1337790c8c1SConrad Meyer * Start of network primitives, beginning with output primitives.
1347790c8c1SConrad Meyer */
1357790c8c1SConrad Meyer
1367790c8c1SConrad Meyer /*
1377790c8c1SConrad Meyer * Handles creation of the ethernet header, then places outgoing packets into
1387790c8c1SConrad Meyer * the tx buffer for the NIC
1397790c8c1SConrad Meyer *
1407790c8c1SConrad Meyer * Parameters:
1417790c8c1SConrad Meyer * m The mbuf containing the packet to be sent (will be freed by
1427790c8c1SConrad Meyer * this function or the NIC driver)
1437790c8c1SConrad Meyer * ifp The interface to send on
1447790c8c1SConrad Meyer * dst The destination ethernet address (source address will be looked
1457790c8c1SConrad Meyer * up using ifp)
1467790c8c1SConrad Meyer * etype The ETHERTYPE_* value for the protocol that is being sent
1477790c8c1SConrad Meyer *
1487790c8c1SConrad Meyer * Returns:
1497790c8c1SConrad Meyer * int see errno.h, 0 for success
1507790c8c1SConrad Meyer */
1517790c8c1SConrad Meyer int
debugnet_ether_output(struct mbuf * m,struct ifnet * ifp,struct ether_addr dst,u_short etype)1527790c8c1SConrad Meyer debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
1537790c8c1SConrad Meyer u_short etype)
1547790c8c1SConrad Meyer {
1557790c8c1SConrad Meyer struct ether_header *eh;
1567790c8c1SConrad Meyer
1577790c8c1SConrad Meyer if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
1587790c8c1SConrad Meyer (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
1597790c8c1SConrad Meyer if_printf(ifp, "%s: interface isn't up\n", __func__);
1607790c8c1SConrad Meyer m_freem(m);
1617790c8c1SConrad Meyer return (ENETDOWN);
1627790c8c1SConrad Meyer }
1637790c8c1SConrad Meyer
1647790c8c1SConrad Meyer /* Fill in the ethernet header. */
1657790c8c1SConrad Meyer M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
1667790c8c1SConrad Meyer if (m == NULL) {
1677790c8c1SConrad Meyer printf("%s: out of mbufs\n", __func__);
1687790c8c1SConrad Meyer return (ENOBUFS);
1697790c8c1SConrad Meyer }
1707790c8c1SConrad Meyer eh = mtod(m, struct ether_header *);
1717790c8c1SConrad Meyer memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
1727790c8c1SConrad Meyer memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
1737790c8c1SConrad Meyer eh->ether_type = htons(etype);
1747790c8c1SConrad Meyer return (ifp->if_debugnet_methods->dn_transmit(ifp, m));
1757790c8c1SConrad Meyer }
1767790c8c1SConrad Meyer
1777790c8c1SConrad Meyer /*
1787790c8c1SConrad Meyer * Unreliable transmission of an mbuf chain to the debugnet server
1797790c8c1SConrad Meyer * Note: can't handle fragmentation; fails if the packet is larger than
1807790c8c1SConrad Meyer * ifp->if_mtu after adding the UDP/IP headers
1817790c8c1SConrad Meyer *
1827790c8c1SConrad Meyer * Parameters:
1837790c8c1SConrad Meyer * pcb The debugnet context block
1847790c8c1SConrad Meyer * m mbuf chain
1857790c8c1SConrad Meyer *
1867790c8c1SConrad Meyer * Returns:
1877790c8c1SConrad Meyer * int see errno.h, 0 for success
1887790c8c1SConrad Meyer */
1897790c8c1SConrad Meyer static int
debugnet_udp_output(struct debugnet_pcb * pcb,struct mbuf * m)1907790c8c1SConrad Meyer debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
1917790c8c1SConrad Meyer {
1927790c8c1SConrad Meyer struct udphdr *udp;
1937790c8c1SConrad Meyer
1947790c8c1SConrad Meyer MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
1957790c8c1SConrad Meyer
1967790c8c1SConrad Meyer M_PREPEND(m, sizeof(*udp), M_NOWAIT);
1977790c8c1SConrad Meyer if (m == NULL) {
1987790c8c1SConrad Meyer printf("%s: out of mbufs\n", __func__);
1997790c8c1SConrad Meyer return (ENOBUFS);
2007790c8c1SConrad Meyer }
2017790c8c1SConrad Meyer
202*5c385a54SMark Johnston udp = mtod(m, struct udphdr *);
2037790c8c1SConrad Meyer udp->uh_ulen = htons(m->m_pkthdr.len);
2047790c8c1SConrad Meyer /* Use this src port so that the server can connect() the socket */
205e9c69625SConrad Meyer udp->uh_sport = htons(pcb->dp_client_port);
2067790c8c1SConrad Meyer udp->uh_dport = htons(pcb->dp_server_port);
2077790c8c1SConrad Meyer /* Computed later (protocol-dependent). */
2087790c8c1SConrad Meyer udp->uh_sum = 0;
2097790c8c1SConrad Meyer
2107790c8c1SConrad Meyer return (debugnet_ip_output(pcb, m));
2117790c8c1SConrad Meyer }
2127790c8c1SConrad Meyer
213dda17b36SConrad Meyer int
debugnet_ack_output(struct debugnet_pcb * pcb,uint32_t seqno)214e9c69625SConrad Meyer debugnet_ack_output(struct debugnet_pcb *pcb, uint32_t seqno /* net endian */)
215e9c69625SConrad Meyer {
216e9c69625SConrad Meyer struct debugnet_ack *dn_ack;
217e9c69625SConrad Meyer struct mbuf *m;
218e9c69625SConrad Meyer
219e9c69625SConrad Meyer DNETDEBUG("Acking with seqno %u\n", ntohl(seqno));
220e9c69625SConrad Meyer
221e9c69625SConrad Meyer m = m_gethdr(M_NOWAIT, MT_DATA);
222e9c69625SConrad Meyer if (m == NULL) {
223e9c69625SConrad Meyer printf("%s: Out of mbufs\n", __func__);
224e9c69625SConrad Meyer return (ENOBUFS);
225e9c69625SConrad Meyer }
226e9c69625SConrad Meyer m->m_len = sizeof(*dn_ack);
227e9c69625SConrad Meyer m->m_pkthdr.len = sizeof(*dn_ack);
228e9c69625SConrad Meyer MH_ALIGN(m, sizeof(*dn_ack));
229*5c385a54SMark Johnston dn_ack = mtod(m, struct debugnet_ack *);
230e9c69625SConrad Meyer dn_ack->da_seqno = seqno;
231e9c69625SConrad Meyer
232e9c69625SConrad Meyer return (debugnet_udp_output(pcb, m));
233e9c69625SConrad Meyer }
234e9c69625SConrad Meyer
2357790c8c1SConrad Meyer /*
2367790c8c1SConrad Meyer * Dummy free function for debugnet clusters.
2377790c8c1SConrad Meyer */
2387790c8c1SConrad Meyer static void
debugnet_mbuf_free(struct mbuf * m __unused)2397790c8c1SConrad Meyer debugnet_mbuf_free(struct mbuf *m __unused)
2407790c8c1SConrad Meyer {
2417790c8c1SConrad Meyer }
2427790c8c1SConrad Meyer
2437790c8c1SConrad Meyer /*
2447790c8c1SConrad Meyer * Construct and reliably send a debugnet packet. May fail from a resource
2457790c8c1SConrad Meyer * shortage or extreme number of unacknowledged retransmissions. Wait for
2467790c8c1SConrad Meyer * an acknowledgement before returning. Splits packets into chunks small
2477790c8c1SConrad Meyer * enough to be sent without fragmentation (looks up the interface MTU)
2487790c8c1SConrad Meyer *
2497790c8c1SConrad Meyer * Parameters:
2507790c8c1SConrad Meyer * type debugnet packet type (HERALD, FINISHED, ...)
2517790c8c1SConrad Meyer * data data
2527790c8c1SConrad Meyer * datalen data size (bytes)
2537790c8c1SConrad Meyer * auxdata optional auxiliary information
2547790c8c1SConrad Meyer *
2557790c8c1SConrad Meyer * Returns:
2567790c8c1SConrad Meyer * int see errno.h, 0 for success
2577790c8c1SConrad Meyer */
2587790c8c1SConrad Meyer int
debugnet_send(struct debugnet_pcb * pcb,uint32_t type,const void * data,uint32_t datalen,const struct debugnet_proto_aux * auxdata)2597790c8c1SConrad Meyer debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
2607790c8c1SConrad Meyer uint32_t datalen, const struct debugnet_proto_aux *auxdata)
2617790c8c1SConrad Meyer {
2627790c8c1SConrad Meyer struct debugnet_msg_hdr *dn_msg_hdr;
2637790c8c1SConrad Meyer struct mbuf *m, *m2;
2647790c8c1SConrad Meyer uint64_t want_acks;
2657790c8c1SConrad Meyer uint32_t i, pktlen, sent_so_far;
2667790c8c1SConrad Meyer int retries, polls, error;
2677790c8c1SConrad Meyer
268e9c69625SConrad Meyer if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
269e9c69625SConrad Meyer return (ECONNRESET);
270e9c69625SConrad Meyer
2717790c8c1SConrad Meyer want_acks = 0;
2727790c8c1SConrad Meyer pcb->dp_rcvd_acks = 0;
2737790c8c1SConrad Meyer retries = 0;
2747790c8c1SConrad Meyer
2757790c8c1SConrad Meyer retransmit:
2767790c8c1SConrad Meyer /* Chunks can be too big to fit in packets. */
2777790c8c1SConrad Meyer for (i = sent_so_far = 0; sent_so_far < datalen ||
2787790c8c1SConrad Meyer (i == 0 && datalen == 0); i++) {
2797790c8c1SConrad Meyer pktlen = datalen - sent_so_far;
2807790c8c1SConrad Meyer
2817790c8c1SConrad Meyer /* Bound: the interface MTU (assume no IP options). */
2827790c8c1SConrad Meyer pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
2837790c8c1SConrad Meyer sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
2847790c8c1SConrad Meyer
2857790c8c1SConrad Meyer /*
2867790c8c1SConrad Meyer * Check if it is retransmitting and this has been ACKed
2877790c8c1SConrad Meyer * already.
2887790c8c1SConrad Meyer */
2897790c8c1SConrad Meyer if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
2907790c8c1SConrad Meyer sent_so_far += pktlen;
2917790c8c1SConrad Meyer continue;
2927790c8c1SConrad Meyer }
2937790c8c1SConrad Meyer
2947790c8c1SConrad Meyer /*
2957790c8c1SConrad Meyer * Get and fill a header mbuf, then chain data as an extended
2967790c8c1SConrad Meyer * mbuf.
2977790c8c1SConrad Meyer */
2987790c8c1SConrad Meyer m = m_gethdr(M_NOWAIT, MT_DATA);
2997790c8c1SConrad Meyer if (m == NULL) {
3007790c8c1SConrad Meyer printf("%s: Out of mbufs\n", __func__);
3017790c8c1SConrad Meyer return (ENOBUFS);
3027790c8c1SConrad Meyer }
3037790c8c1SConrad Meyer m->m_len = sizeof(struct debugnet_msg_hdr);
3047790c8c1SConrad Meyer m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
3057790c8c1SConrad Meyer MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
3067790c8c1SConrad Meyer dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
3077790c8c1SConrad Meyer dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
3087790c8c1SConrad Meyer dn_msg_hdr->mh_type = htonl(type);
3097790c8c1SConrad Meyer dn_msg_hdr->mh_len = htonl(pktlen);
3107790c8c1SConrad Meyer
3117790c8c1SConrad Meyer if (auxdata != NULL) {
3127790c8c1SConrad Meyer dn_msg_hdr->mh_offset =
3137790c8c1SConrad Meyer htobe64(auxdata->dp_offset_start + sent_so_far);
3147790c8c1SConrad Meyer dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
3157790c8c1SConrad Meyer } else {
3167790c8c1SConrad Meyer dn_msg_hdr->mh_offset = htobe64(sent_so_far);
3177790c8c1SConrad Meyer dn_msg_hdr->mh_aux2 = 0;
3187790c8c1SConrad Meyer }
3197790c8c1SConrad Meyer
3207790c8c1SConrad Meyer if (pktlen != 0) {
3217790c8c1SConrad Meyer m2 = m_get(M_NOWAIT, MT_DATA);
3227790c8c1SConrad Meyer if (m2 == NULL) {
3237790c8c1SConrad Meyer m_freem(m);
3247790c8c1SConrad Meyer printf("%s: Out of mbufs\n", __func__);
3257790c8c1SConrad Meyer return (ENOBUFS);
3267790c8c1SConrad Meyer }
3277790c8c1SConrad Meyer MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
3287790c8c1SConrad Meyer pktlen, debugnet_mbuf_free, NULL, NULL, 0,
3297790c8c1SConrad Meyer EXT_DISPOSABLE);
3307790c8c1SConrad Meyer m2->m_len = pktlen;
3317790c8c1SConrad Meyer
3327790c8c1SConrad Meyer m_cat(m, m2);
3337790c8c1SConrad Meyer m->m_pkthdr.len += pktlen;
3347790c8c1SConrad Meyer }
3357790c8c1SConrad Meyer error = debugnet_udp_output(pcb, m);
3367790c8c1SConrad Meyer if (error != 0)
3377790c8c1SConrad Meyer return (error);
3387790c8c1SConrad Meyer
3397790c8c1SConrad Meyer /* Note that we're waiting for this packet in the bitfield. */
3407790c8c1SConrad Meyer want_acks |= (1 << i);
3417790c8c1SConrad Meyer sent_so_far += pktlen;
3427790c8c1SConrad Meyer }
3437790c8c1SConrad Meyer if (i >= DEBUGNET_MAX_IN_FLIGHT)
3447790c8c1SConrad Meyer printf("Warning: Sent more than %d packets (%d). "
3457790c8c1SConrad Meyer "Acknowledgements will fail unless the size of "
3467790c8c1SConrad Meyer "rcvd_acks/want_acks is increased.\n",
3477790c8c1SConrad Meyer DEBUGNET_MAX_IN_FLIGHT, i);
3487790c8c1SConrad Meyer
3497790c8c1SConrad Meyer /*
3507790c8c1SConrad Meyer * Wait for acks. A *real* window would speed things up considerably.
3517790c8c1SConrad Meyer */
3527790c8c1SConrad Meyer polls = 0;
3537790c8c1SConrad Meyer while (pcb->dp_rcvd_acks != want_acks) {
3547790c8c1SConrad Meyer if (polls++ > debugnet_npolls) {
3557790c8c1SConrad Meyer if (retries++ > debugnet_nretries)
3567790c8c1SConrad Meyer return (ETIMEDOUT);
3577790c8c1SConrad Meyer printf(". ");
3587790c8c1SConrad Meyer goto retransmit;
3597790c8c1SConrad Meyer }
360dda17b36SConrad Meyer debugnet_network_poll(pcb);
3617790c8c1SConrad Meyer DELAY(500);
362e9c69625SConrad Meyer if (pcb->dp_state == DN_STATE_REMOTE_CLOSED)
363e9c69625SConrad Meyer return (ECONNRESET);
3647790c8c1SConrad Meyer }
3657790c8c1SConrad Meyer pcb->dp_seqno += i;
3667790c8c1SConrad Meyer return (0);
3677790c8c1SConrad Meyer }
3687790c8c1SConrad Meyer
3697790c8c1SConrad Meyer /*
3707790c8c1SConrad Meyer * Network input primitives.
3717790c8c1SConrad Meyer */
3727790c8c1SConrad Meyer
373e9c69625SConrad Meyer /*
374e9c69625SConrad Meyer * Just introspect the header enough to fire off a seqno ack and validate
375e9c69625SConrad Meyer * length fits.
376e9c69625SConrad Meyer */
377e9c69625SConrad Meyer static void
debugnet_handle_rx_msg(struct debugnet_pcb * pcb,struct mbuf ** mb)378e9c69625SConrad Meyer debugnet_handle_rx_msg(struct debugnet_pcb *pcb, struct mbuf **mb)
379e9c69625SConrad Meyer {
380e9c69625SConrad Meyer const struct debugnet_msg_hdr *dnh;
381e9c69625SConrad Meyer struct mbuf *m;
382b498331bSJohn Reimer uint32_t hdr_type;
383b498331bSJohn Reimer uint32_t seqno;
384e9c69625SConrad Meyer int error;
385e9c69625SConrad Meyer
386e9c69625SConrad Meyer m = *mb;
387e9c69625SConrad Meyer
388e9c69625SConrad Meyer if (m->m_pkthdr.len < sizeof(*dnh)) {
389e9c69625SConrad Meyer DNETDEBUG("ignoring small debugnet_msg packet\n");
390e9c69625SConrad Meyer return;
391e9c69625SConrad Meyer }
392e9c69625SConrad Meyer
393e9c69625SConrad Meyer /* Get ND header. */
394e9c69625SConrad Meyer if (m->m_len < sizeof(*dnh)) {
395e9c69625SConrad Meyer m = m_pullup(m, sizeof(*dnh));
396e9c69625SConrad Meyer *mb = m;
397e9c69625SConrad Meyer if (m == NULL) {
398e9c69625SConrad Meyer DNETDEBUG("m_pullup failed\n");
399e9c69625SConrad Meyer return;
400e9c69625SConrad Meyer }
401e9c69625SConrad Meyer }
402e9c69625SConrad Meyer
403*5c385a54SMark Johnston dnh = mtod(m, const struct debugnet_msg_hdr *);
404e9c69625SConrad Meyer if (ntohl(dnh->mh_len) + sizeof(*dnh) > m->m_pkthdr.len) {
405e9c69625SConrad Meyer DNETDEBUG("Dropping short packet.\n");
406e9c69625SConrad Meyer return;
407e9c69625SConrad Meyer }
408e9c69625SConrad Meyer
409b498331bSJohn Reimer hdr_type = ntohl(dnh->mh_type);
410b498331bSJohn Reimer if (hdr_type != DEBUGNET_DATA) {
411b498331bSJohn Reimer if (hdr_type == DEBUGNET_FINISHED) {
412b498331bSJohn Reimer printf("Remote shut down the connection on us!\n");
413b498331bSJohn Reimer pcb->dp_state = DN_STATE_REMOTE_CLOSED;
414b498331bSJohn Reimer if (pcb->dp_finish_handler != NULL) {
415b498331bSJohn Reimer pcb->dp_finish_handler();
416b498331bSJohn Reimer }
417b498331bSJohn Reimer } else {
418b498331bSJohn Reimer DNETDEBUG("Got unexpected debugnet message %u\n", hdr_type);
419b498331bSJohn Reimer }
420b498331bSJohn Reimer return;
421b498331bSJohn Reimer }
422b498331bSJohn Reimer
423e9c69625SConrad Meyer /*
424e9c69625SConrad Meyer * If the issue is transient (ENOBUFS), sender should resend. If
425e9c69625SConrad Meyer * non-transient (like driver objecting to rx -> tx from the same
426e9c69625SConrad Meyer * thread), not much else we can do.
427e9c69625SConrad Meyer */
428b498331bSJohn Reimer seqno = dnh->mh_seqno; /* net endian */
429b498331bSJohn Reimer m_adj(m, sizeof(*dnh));
430b498331bSJohn Reimer dnh = NULL;
431b498331bSJohn Reimer error = pcb->dp_rx_handler(m);
432b498331bSJohn Reimer if (error != 0) {
433b498331bSJohn Reimer DNETDEBUG("RX handler was not able to accept message, error %d. "
434b498331bSJohn Reimer "Skipping ack.\n", error);
435e9c69625SConrad Meyer return;
436e9c69625SConrad Meyer }
437e9c69625SConrad Meyer
438b498331bSJohn Reimer error = debugnet_ack_output(pcb, seqno);
439b498331bSJohn Reimer if (error != 0) {
440b498331bSJohn Reimer DNETDEBUG("Couldn't ACK rx packet %u; %d\n", ntohl(seqno), error);
441b498331bSJohn Reimer }
442e9c69625SConrad Meyer }
443e9c69625SConrad Meyer
4447790c8c1SConrad Meyer static void
debugnet_handle_ack(struct debugnet_pcb * pcb,struct mbuf ** mb,uint16_t sport)4457790c8c1SConrad Meyer debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
4467790c8c1SConrad Meyer {
4477790c8c1SConrad Meyer const struct debugnet_ack *dn_ack;
4487790c8c1SConrad Meyer struct mbuf *m;
4497790c8c1SConrad Meyer uint32_t rcv_ackno;
4507790c8c1SConrad Meyer
4517790c8c1SConrad Meyer m = *mb;
4527790c8c1SConrad Meyer
4537790c8c1SConrad Meyer /* Get Ack. */
4547790c8c1SConrad Meyer if (m->m_len < sizeof(*dn_ack)) {
4557790c8c1SConrad Meyer m = m_pullup(m, sizeof(*dn_ack));
4567790c8c1SConrad Meyer *mb = m;
4577790c8c1SConrad Meyer if (m == NULL) {
4587790c8c1SConrad Meyer DNETDEBUG("m_pullup failed\n");
4597790c8c1SConrad Meyer return;
4607790c8c1SConrad Meyer }
4617790c8c1SConrad Meyer }
462*5c385a54SMark Johnston dn_ack = mtod(m, const struct debugnet_ack *);
4637790c8c1SConrad Meyer
4647790c8c1SConrad Meyer /* Debugnet processing. */
4657790c8c1SConrad Meyer /*
4667790c8c1SConrad Meyer * Packet is meant for us. Extract the ack sequence number and the
4677790c8c1SConrad Meyer * port number if necessary.
4687790c8c1SConrad Meyer */
4697790c8c1SConrad Meyer rcv_ackno = ntohl(dn_ack->da_seqno);
4707790c8c1SConrad Meyer if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
4717790c8c1SConrad Meyer pcb->dp_server_port = sport;
4727790c8c1SConrad Meyer pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
4737790c8c1SConrad Meyer }
4747790c8c1SConrad Meyer if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
4757790c8c1SConrad Meyer printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
4767790c8c1SConrad Meyer else if (rcv_ackno >= pcb->dp_seqno) {
4777790c8c1SConrad Meyer /* We're interested in this ack. Record it. */
4787790c8c1SConrad Meyer pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
4797790c8c1SConrad Meyer }
4807790c8c1SConrad Meyer }
4817790c8c1SConrad Meyer
4827790c8c1SConrad Meyer void
debugnet_handle_udp(struct debugnet_pcb * pcb,struct mbuf ** mb)4837790c8c1SConrad Meyer debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
4847790c8c1SConrad Meyer {
4857790c8c1SConrad Meyer const struct udphdr *udp;
4867790c8c1SConrad Meyer struct mbuf *m;
487e9c69625SConrad Meyer uint16_t sport, ulen;
4887790c8c1SConrad Meyer
4897790c8c1SConrad Meyer /* UDP processing. */
4907790c8c1SConrad Meyer
4917790c8c1SConrad Meyer m = *mb;
4927790c8c1SConrad Meyer if (m->m_pkthdr.len < sizeof(*udp)) {
4937790c8c1SConrad Meyer DNETDEBUG("ignoring small UDP packet\n");
4947790c8c1SConrad Meyer return;
4957790c8c1SConrad Meyer }
4967790c8c1SConrad Meyer
4977790c8c1SConrad Meyer /* Get UDP headers. */
4987790c8c1SConrad Meyer if (m->m_len < sizeof(*udp)) {
4997790c8c1SConrad Meyer m = m_pullup(m, sizeof(*udp));
5007790c8c1SConrad Meyer *mb = m;
5017790c8c1SConrad Meyer if (m == NULL) {
5027790c8c1SConrad Meyer DNETDEBUG("m_pullup failed\n");
5037790c8c1SConrad Meyer return;
5047790c8c1SConrad Meyer }
5057790c8c1SConrad Meyer }
506*5c385a54SMark Johnston udp = mtod(m, const struct udphdr *);
5077790c8c1SConrad Meyer
508e9c69625SConrad Meyer /* We expect to receive UDP packets on the configured client port. */
509e9c69625SConrad Meyer if (ntohs(udp->uh_dport) != pcb->dp_client_port) {
510e9c69625SConrad Meyer DNETDEBUG("not on the expected port.\n");
5117790c8c1SConrad Meyer return;
5127790c8c1SConrad Meyer }
513e9c69625SConrad Meyer
514e9c69625SConrad Meyer /* Check that ulen does not exceed actual size of data. */
515e9c69625SConrad Meyer ulen = ntohs(udp->uh_ulen);
516e9c69625SConrad Meyer if (m->m_pkthdr.len < ulen) {
517e9c69625SConrad Meyer DNETDEBUG("ignoring runt UDP packet\n");
518e9c69625SConrad Meyer return;
519e9c69625SConrad Meyer }
520e9c69625SConrad Meyer
5217790c8c1SConrad Meyer sport = ntohs(udp->uh_sport);
5227790c8c1SConrad Meyer
5237790c8c1SConrad Meyer m_adj(m, sizeof(*udp));
524e9c69625SConrad Meyer ulen -= sizeof(*udp);
525e9c69625SConrad Meyer
526e9c69625SConrad Meyer if (ulen == sizeof(struct debugnet_ack)) {
5277790c8c1SConrad Meyer debugnet_handle_ack(pcb, mb, sport);
528e9c69625SConrad Meyer return;
529e9c69625SConrad Meyer }
530e9c69625SConrad Meyer
531e9c69625SConrad Meyer if (pcb->dp_rx_handler == NULL) {
532e9c69625SConrad Meyer if (ulen < sizeof(struct debugnet_ack))
533e9c69625SConrad Meyer DNETDEBUG("ignoring small ACK packet\n");
534e9c69625SConrad Meyer else
535e9c69625SConrad Meyer DNETDEBUG("ignoring unexpected non-ACK packet on "
536e9c69625SConrad Meyer "half-duplex connection.\n");
537e9c69625SConrad Meyer return;
538e9c69625SConrad Meyer }
539e9c69625SConrad Meyer
540e9c69625SConrad Meyer debugnet_handle_rx_msg(pcb, mb);
5417790c8c1SConrad Meyer }
5427790c8c1SConrad Meyer
5437790c8c1SConrad Meyer /*
5447790c8c1SConrad Meyer * Handler for incoming packets directly from the network adapter
5457790c8c1SConrad Meyer * Identifies the packet type (IP or ARP) and passes it along to one of the
5467790c8c1SConrad Meyer * helper functions debugnet_handle_ip or debugnet_handle_arp.
5477790c8c1SConrad Meyer *
5487790c8c1SConrad Meyer * It needs to partially replicate the behaviour of ether_input() and
5497790c8c1SConrad Meyer * ether_demux().
5507790c8c1SConrad Meyer *
5517790c8c1SConrad Meyer * Parameters:
5527790c8c1SConrad Meyer * ifp the interface the packet came from
5537790c8c1SConrad Meyer * m an mbuf containing the packet received
5547790c8c1SConrad Meyer */
5557790c8c1SConrad Meyer static void
debugnet_input_one(struct ifnet * ifp,struct mbuf * m)55684143314SMark Johnston debugnet_input_one(struct ifnet *ifp, struct mbuf *m)
5577790c8c1SConrad Meyer {
5587790c8c1SConrad Meyer struct ifreq ifr;
5597790c8c1SConrad Meyer struct ether_header *eh;
5607790c8c1SConrad Meyer u_short etype;
5617790c8c1SConrad Meyer
5627790c8c1SConrad Meyer /* Ethernet processing. */
5637790c8c1SConrad Meyer if ((m->m_flags & M_PKTHDR) == 0) {
5647790c8c1SConrad Meyer DNETDEBUG_IF(ifp, "discard frame without packet header\n");
5657790c8c1SConrad Meyer goto done;
5667790c8c1SConrad Meyer }
5677790c8c1SConrad Meyer if (m->m_len < ETHER_HDR_LEN) {
5687790c8c1SConrad Meyer DNETDEBUG_IF(ifp,
5696fe4d839SZhenlei Huang "discard frame without leading eth header (len %d pktlen %d)\n",
5707790c8c1SConrad Meyer m->m_len, m->m_pkthdr.len);
5717790c8c1SConrad Meyer goto done;
5727790c8c1SConrad Meyer }
5737790c8c1SConrad Meyer eh = mtod(m, struct ether_header *);
5747790c8c1SConrad Meyer etype = ntohs(eh->ether_type);
5757790c8c1SConrad Meyer if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
5767790c8c1SConrad Meyer DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
5777790c8c1SConrad Meyer goto done;
5787790c8c1SConrad Meyer }
5797790c8c1SConrad Meyer if (if_gethwaddr(ifp, &ifr) != 0) {
5807790c8c1SConrad Meyer DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
5817790c8c1SConrad Meyer goto done;
5827790c8c1SConrad Meyer }
5837790c8c1SConrad Meyer if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
5846d567ec2SConrad Meyer ETHER_ADDR_LEN) != 0 &&
5856d567ec2SConrad Meyer (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
5867790c8c1SConrad Meyer DNETDEBUG_IF(ifp,
5877790c8c1SConrad Meyer "discard frame with incorrect destination addr\n");
5887790c8c1SConrad Meyer goto done;
5897790c8c1SConrad Meyer }
5907790c8c1SConrad Meyer
5917790c8c1SConrad Meyer MPASS(g_debugnet_pcb_inuse);
5927790c8c1SConrad Meyer
5937790c8c1SConrad Meyer /* Done ethernet processing. Strip off the ethernet header. */
5947790c8c1SConrad Meyer m_adj(m, ETHER_HDR_LEN);
5957790c8c1SConrad Meyer switch (etype) {
5967790c8c1SConrad Meyer case ETHERTYPE_ARP:
5977790c8c1SConrad Meyer debugnet_handle_arp(&g_dnet_pcb, &m);
5987790c8c1SConrad Meyer break;
5997790c8c1SConrad Meyer case ETHERTYPE_IP:
6007790c8c1SConrad Meyer debugnet_handle_ip(&g_dnet_pcb, &m);
6017790c8c1SConrad Meyer break;
6027790c8c1SConrad Meyer default:
6037790c8c1SConrad Meyer DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
6047790c8c1SConrad Meyer break;
6057790c8c1SConrad Meyer }
6067790c8c1SConrad Meyer done:
6077790c8c1SConrad Meyer if (m != NULL)
6087790c8c1SConrad Meyer m_freem(m);
6097790c8c1SConrad Meyer }
6107790c8c1SConrad Meyer
61184143314SMark Johnston static void
debugnet_input(struct ifnet * ifp,struct mbuf * m)61284143314SMark Johnston debugnet_input(struct ifnet *ifp, struct mbuf *m)
61384143314SMark Johnston {
61484143314SMark Johnston struct mbuf *n;
61584143314SMark Johnston
61684143314SMark Johnston do {
61784143314SMark Johnston n = m->m_nextpkt;
61884143314SMark Johnston m->m_nextpkt = NULL;
61984143314SMark Johnston debugnet_input_one(ifp, m);
62084143314SMark Johnston m = n;
62184143314SMark Johnston } while (m != NULL);
62284143314SMark Johnston }
62384143314SMark Johnston
6247790c8c1SConrad Meyer /*
6257790c8c1SConrad Meyer * Network polling primitive.
6267790c8c1SConrad Meyer *
6277790c8c1SConrad Meyer * Instead of assuming that most of the network stack is sane, we just poll the
6287790c8c1SConrad Meyer * driver directly for packets.
6297790c8c1SConrad Meyer */
6307790c8c1SConrad Meyer void
debugnet_network_poll(struct debugnet_pcb * pcb)631dda17b36SConrad Meyer debugnet_network_poll(struct debugnet_pcb *pcb)
6327790c8c1SConrad Meyer {
633dda17b36SConrad Meyer struct ifnet *ifp;
634dda17b36SConrad Meyer
635dda17b36SConrad Meyer ifp = pcb->dp_ifp;
6367790c8c1SConrad Meyer ifp->if_debugnet_methods->dn_poll(ifp, 1000);
6377790c8c1SConrad Meyer }
6387790c8c1SConrad Meyer
6397790c8c1SConrad Meyer /*
6407790c8c1SConrad Meyer * Start of consumer API surface.
6417790c8c1SConrad Meyer */
6427790c8c1SConrad Meyer void
debugnet_free(struct debugnet_pcb * pcb)6437790c8c1SConrad Meyer debugnet_free(struct debugnet_pcb *pcb)
6447790c8c1SConrad Meyer {
6457790c8c1SConrad Meyer struct ifnet *ifp;
6467790c8c1SConrad Meyer
6477790c8c1SConrad Meyer MPASS(pcb == &g_dnet_pcb);
648a84bf5eaSMitchell Horne MPASS(pcb->dp_drv_input == NULL || g_debugnet_pcb_inuse);
6497790c8c1SConrad Meyer
6507790c8c1SConrad Meyer ifp = pcb->dp_ifp;
651fde2cf65SConrad Meyer if (ifp != NULL) {
652fde2cf65SConrad Meyer if (pcb->dp_drv_input != NULL)
6537790c8c1SConrad Meyer ifp->if_input = pcb->dp_drv_input;
654fde2cf65SConrad Meyer if (pcb->dp_event_started)
6557790c8c1SConrad Meyer ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
656fde2cf65SConrad Meyer }
6577790c8c1SConrad Meyer debugnet_mbuf_finish();
6587790c8c1SConrad Meyer
6597790c8c1SConrad Meyer g_debugnet_pcb_inuse = false;
6607790c8c1SConrad Meyer memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
6617790c8c1SConrad Meyer }
6627790c8c1SConrad Meyer
6637790c8c1SConrad Meyer int
debugnet_connect(const struct debugnet_conn_params * dcp,struct debugnet_pcb ** pcb_out)6647790c8c1SConrad Meyer debugnet_connect(const struct debugnet_conn_params *dcp,
6657790c8c1SConrad Meyer struct debugnet_pcb **pcb_out)
6667790c8c1SConrad Meyer {
667dda17b36SConrad Meyer struct debugnet_proto_aux herald_auxdata;
6687790c8c1SConrad Meyer struct debugnet_pcb *pcb;
6697790c8c1SConrad Meyer struct ifnet *ifp;
6707790c8c1SConrad Meyer int error;
6717790c8c1SConrad Meyer
6727790c8c1SConrad Meyer if (g_debugnet_pcb_inuse) {
6737790c8c1SConrad Meyer printf("%s: Only one connection at a time.\n", __func__);
6747790c8c1SConrad Meyer return (EBUSY);
6757790c8c1SConrad Meyer }
6767790c8c1SConrad Meyer
6777790c8c1SConrad Meyer pcb = &g_dnet_pcb;
6787790c8c1SConrad Meyer *pcb = (struct debugnet_pcb) {
6797790c8c1SConrad Meyer .dp_state = DN_STATE_INIT,
6807790c8c1SConrad Meyer .dp_client = dcp->dc_client,
6817790c8c1SConrad Meyer .dp_server = dcp->dc_server,
6827790c8c1SConrad Meyer .dp_gateway = dcp->dc_gateway,
6837790c8c1SConrad Meyer .dp_server_port = dcp->dc_herald_port, /* Initially */
684e9c69625SConrad Meyer .dp_client_port = dcp->dc_client_port,
6857790c8c1SConrad Meyer .dp_seqno = 1,
6867790c8c1SConrad Meyer .dp_ifp = dcp->dc_ifp,
687e9c69625SConrad Meyer .dp_rx_handler = dcp->dc_rx_handler,
688a84bf5eaSMitchell Horne .dp_drv_input = NULL,
6897790c8c1SConrad Meyer };
6907790c8c1SConrad Meyer
6917790c8c1SConrad Meyer /* Switch to the debugnet mbuf zones. */
6927790c8c1SConrad Meyer debugnet_mbuf_start();
6937790c8c1SConrad Meyer
694fde2cf65SConrad Meyer /* At least one needed parameter is missing; infer it. */
695fde2cf65SConrad Meyer if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
696fde2cf65SConrad Meyer pcb->dp_ifp == NULL) {
697fde2cf65SConrad Meyer struct sockaddr_in dest_sin, *gw_sin, *local_sin;
698fde2cf65SConrad Meyer struct ifnet *rt_ifp;
69955f57ca9SAlexander V. Chernikov struct nhop_object *nh;
700fde2cf65SConrad Meyer
701fde2cf65SConrad Meyer memset(&dest_sin, 0, sizeof(dest_sin));
702fde2cf65SConrad Meyer dest_sin = (struct sockaddr_in) {
703fde2cf65SConrad Meyer .sin_len = sizeof(dest_sin),
704fde2cf65SConrad Meyer .sin_family = AF_INET,
705fde2cf65SConrad Meyer .sin_addr.s_addr = pcb->dp_server,
706fde2cf65SConrad Meyer };
707fde2cf65SConrad Meyer
708fde2cf65SConrad Meyer CURVNET_SET(vnet0);
709caf2f627SAlexander V. Chernikov nh = fib4_lookup_debugnet(debugnet_fib, dest_sin.sin_addr, 0,
71055f57ca9SAlexander V. Chernikov NHR_NONE);
711fde2cf65SConrad Meyer CURVNET_RESTORE();
712fde2cf65SConrad Meyer
71355f57ca9SAlexander V. Chernikov if (nh == NULL) {
7140634308dSConrad Meyer printf("%s: Could not get route for that server.\n",
715fde2cf65SConrad Meyer __func__);
716fde2cf65SConrad Meyer error = ENOENT;
717fde2cf65SConrad Meyer goto cleanup;
718fde2cf65SConrad Meyer }
719fde2cf65SConrad Meyer
72062e1a437SZhenlei Huang /* TODO support AF_INET6 */
72155f57ca9SAlexander V. Chernikov if (nh->gw_sa.sa_family == AF_INET)
72255f57ca9SAlexander V. Chernikov gw_sin = &nh->gw4_sa;
723fde2cf65SConrad Meyer else {
72455f57ca9SAlexander V. Chernikov if (nh->gw_sa.sa_family == AF_LINK)
725fde2cf65SConrad Meyer DNETDEBUG("Destination address is on link.\n");
726fde2cf65SConrad Meyer gw_sin = NULL;
727fde2cf65SConrad Meyer }
728fde2cf65SConrad Meyer
72955f57ca9SAlexander V. Chernikov MPASS(nh->nh_ifa->ifa_addr->sa_family == AF_INET);
73055f57ca9SAlexander V. Chernikov local_sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr;
731fde2cf65SConrad Meyer
73255f57ca9SAlexander V. Chernikov rt_ifp = nh->nh_ifp;
733fde2cf65SConrad Meyer
734fde2cf65SConrad Meyer if (pcb->dp_client == INADDR_ANY)
735fde2cf65SConrad Meyer pcb->dp_client = local_sin->sin_addr.s_addr;
736fde2cf65SConrad Meyer if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
737fde2cf65SConrad Meyer pcb->dp_gateway = gw_sin->sin_addr.s_addr;
738fde2cf65SConrad Meyer if (pcb->dp_ifp == NULL)
739fde2cf65SConrad Meyer pcb->dp_ifp = rt_ifp;
740fde2cf65SConrad Meyer }
741fde2cf65SConrad Meyer
7427790c8c1SConrad Meyer ifp = pcb->dp_ifp;
743fde2cf65SConrad Meyer
744fde2cf65SConrad Meyer if (debugnet_debug > 0) {
745fde2cf65SConrad Meyer char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
746fde2cf65SConrad Meyer gwbuf[INET_ADDRSTRLEN];
747fde2cf65SConrad Meyer inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
748fde2cf65SConrad Meyer inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
749fde2cf65SConrad Meyer if (pcb->dp_gateway != INADDR_ANY)
750fde2cf65SConrad Meyer inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
751fde2cf65SConrad Meyer DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
752fde2cf65SConrad Meyer serbuf, pcb->dp_server_port,
753fde2cf65SConrad Meyer (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
754fde2cf65SConrad Meyer (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
755e9c69625SConrad Meyer clibuf, pcb->dp_client_port, if_name(ifp));
756fde2cf65SConrad Meyer }
757fde2cf65SConrad Meyer
758fde2cf65SConrad Meyer /* Validate iface is online and supported. */
759fde2cf65SConrad Meyer if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
760fde2cf65SConrad Meyer printf("%s: interface '%s' does not support debugnet\n",
761fde2cf65SConrad Meyer __func__, if_name(ifp));
762fde2cf65SConrad Meyer error = ENODEV;
763fde2cf65SConrad Meyer goto cleanup;
764fde2cf65SConrad Meyer }
765fde2cf65SConrad Meyer if ((if_getflags(ifp) & IFF_UP) == 0) {
766fde2cf65SConrad Meyer printf("%s: interface '%s' link is down\n", __func__,
767fde2cf65SConrad Meyer if_name(ifp));
768fde2cf65SConrad Meyer error = ENXIO;
769fde2cf65SConrad Meyer goto cleanup;
770fde2cf65SConrad Meyer }
771fde2cf65SConrad Meyer
7727790c8c1SConrad Meyer ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
773fde2cf65SConrad Meyer pcb->dp_event_started = true;
7747790c8c1SConrad Meyer
7757790c8c1SConrad Meyer /*
7767790c8c1SConrad Meyer * We maintain the invariant that g_debugnet_pcb_inuse is always true
7777790c8c1SConrad Meyer * while the debugnet ifp's if_input is overridden with
77884143314SMark Johnston * debugnet_input().
7797790c8c1SConrad Meyer */
7807790c8c1SConrad Meyer g_debugnet_pcb_inuse = true;
7817790c8c1SConrad Meyer
7827790c8c1SConrad Meyer /* Make the card use *our* receive callback. */
7837790c8c1SConrad Meyer pcb->dp_drv_input = ifp->if_input;
78484143314SMark Johnston ifp->if_input = debugnet_input;
7857790c8c1SConrad Meyer
7867790c8c1SConrad Meyer printf("%s: searching for %s MAC...\n", __func__,
7877790c8c1SConrad Meyer (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
7887790c8c1SConrad Meyer
7897790c8c1SConrad Meyer error = debugnet_arp_gw(pcb);
7907790c8c1SConrad Meyer if (error != 0) {
7917790c8c1SConrad Meyer printf("%s: failed to locate MAC address\n", __func__);
7927790c8c1SConrad Meyer goto cleanup;
7937790c8c1SConrad Meyer }
7947790c8c1SConrad Meyer MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
7957790c8c1SConrad Meyer
796dda17b36SConrad Meyer herald_auxdata = (struct debugnet_proto_aux) {
797dda17b36SConrad Meyer .dp_offset_start = dcp->dc_herald_offset,
798dda17b36SConrad Meyer .dp_aux2 = dcp->dc_herald_aux2,
799dda17b36SConrad Meyer };
8007790c8c1SConrad Meyer error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
801dda17b36SConrad Meyer dcp->dc_herald_datalen, &herald_auxdata);
8027790c8c1SConrad Meyer if (error != 0) {
8037790c8c1SConrad Meyer printf("%s: failed to herald debugnet server\n", __func__);
8047790c8c1SConrad Meyer goto cleanup;
8057790c8c1SConrad Meyer }
8067790c8c1SConrad Meyer
8077790c8c1SConrad Meyer *pcb_out = pcb;
8087790c8c1SConrad Meyer return (0);
8097790c8c1SConrad Meyer
8107790c8c1SConrad Meyer cleanup:
8117790c8c1SConrad Meyer debugnet_free(pcb);
8127790c8c1SConrad Meyer return (error);
8137790c8c1SConrad Meyer }
8147790c8c1SConrad Meyer
8157790c8c1SConrad Meyer /*
8167790c8c1SConrad Meyer * Pre-allocated dump-time mbuf tracking.
8177790c8c1SConrad Meyer *
8187790c8c1SConrad Meyer * We just track the high water mark we've ever seen and allocate appropriately
8197790c8c1SConrad Meyer * for that iface/mtu combo.
8207790c8c1SConrad Meyer */
8217790c8c1SConrad Meyer static struct {
8227790c8c1SConrad Meyer int nmbuf;
8237790c8c1SConrad Meyer int ncl;
8247790c8c1SConrad Meyer int clsize;
8257790c8c1SConrad Meyer } dn_hwm;
8267790c8c1SConrad Meyer static struct mtx dn_hwm_lk;
8277790c8c1SConrad Meyer MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
8287790c8c1SConrad Meyer
8297790c8c1SConrad Meyer static void
dn_maybe_reinit_mbufs(int nmbuf,int ncl,int clsize)8307790c8c1SConrad Meyer dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
8317790c8c1SConrad Meyer {
8327790c8c1SConrad Meyer bool any;
8337790c8c1SConrad Meyer
8347790c8c1SConrad Meyer any = false;
8357790c8c1SConrad Meyer mtx_lock(&dn_hwm_lk);
8367790c8c1SConrad Meyer
8377790c8c1SConrad Meyer if (nmbuf > dn_hwm.nmbuf) {
8387790c8c1SConrad Meyer any = true;
8397790c8c1SConrad Meyer dn_hwm.nmbuf = nmbuf;
8407790c8c1SConrad Meyer } else
8417790c8c1SConrad Meyer nmbuf = dn_hwm.nmbuf;
8427790c8c1SConrad Meyer
8437790c8c1SConrad Meyer if (ncl > dn_hwm.ncl) {
8447790c8c1SConrad Meyer any = true;
8457790c8c1SConrad Meyer dn_hwm.ncl = ncl;
8467790c8c1SConrad Meyer } else
8477790c8c1SConrad Meyer ncl = dn_hwm.ncl;
8487790c8c1SConrad Meyer
8497790c8c1SConrad Meyer if (clsize > dn_hwm.clsize) {
8507790c8c1SConrad Meyer any = true;
8517790c8c1SConrad Meyer dn_hwm.clsize = clsize;
8527790c8c1SConrad Meyer } else
8537790c8c1SConrad Meyer clsize = dn_hwm.clsize;
8547790c8c1SConrad Meyer
8557790c8c1SConrad Meyer mtx_unlock(&dn_hwm_lk);
8567790c8c1SConrad Meyer
8577790c8c1SConrad Meyer if (any)
8587790c8c1SConrad Meyer debugnet_mbuf_reinit(nmbuf, ncl, clsize);
8597790c8c1SConrad Meyer }
8607790c8c1SConrad Meyer
8617790c8c1SConrad Meyer void
debugnet_any_ifnet_update(struct ifnet * ifp)8627790c8c1SConrad Meyer debugnet_any_ifnet_update(struct ifnet *ifp)
8637790c8c1SConrad Meyer {
8647790c8c1SConrad Meyer int clsize, nmbuf, ncl, nrxr;
8657790c8c1SConrad Meyer
8667790c8c1SConrad Meyer if (!DEBUGNET_SUPPORTED_NIC(ifp))
8677790c8c1SConrad Meyer return;
8687790c8c1SConrad Meyer
8697790c8c1SConrad Meyer ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
8707790c8c1SConrad Meyer KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
8717790c8c1SConrad Meyer
8727790c8c1SConrad Meyer /*
8737790c8c1SConrad Meyer * We need two headers per message on the transmit side. Multiply by
8747790c8c1SConrad Meyer * four to give us some breathing room.
8757790c8c1SConrad Meyer */
8767790c8c1SConrad Meyer nmbuf = ncl * (4 + nrxr);
8777790c8c1SConrad Meyer ncl *= nrxr;
8787790c8c1SConrad Meyer
87965366903SConrad Meyer /*
88065366903SConrad Meyer * Bandaid for drivers that (incorrectly) advertise LinkUp before their
88165366903SConrad Meyer * dn_init method is available.
88265366903SConrad Meyer */
88365366903SConrad Meyer if (nmbuf == 0 || ncl == 0 || clsize == 0) {
884346b8b25SFranco Fichtner #ifndef INVARIANTS
885346b8b25SFranco Fichtner if (bootverbose)
886346b8b25SFranco Fichtner #endif
88765366903SConrad Meyer printf("%s: Bad dn_init result from %s (ifp %p), ignoring.\n",
88865366903SConrad Meyer __func__, if_name(ifp), ifp);
88965366903SConrad Meyer return;
89065366903SConrad Meyer }
8917790c8c1SConrad Meyer dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
8927790c8c1SConrad Meyer }
8937790c8c1SConrad Meyer
8947790c8c1SConrad Meyer /*
8957790c8c1SConrad Meyer * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
8967790c8c1SConrad Meyer * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
8977790c8c1SConrad Meyer *
8987790c8c1SConrad Meyer * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
8997790c8c1SConrad Meyer * because the driver is still in attach. Since we cannot use down interfaces,
9007790c8c1SConrad Meyer * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient? ... Nope, at least
9017790c8c1SConrad Meyer * with vtnet and dhcpclient that event just never occurs.
9027790c8c1SConrad Meyer *
9037790c8c1SConrad Meyer * So that's how I've landed on the lower level ifnet_link_event.
9047790c8c1SConrad Meyer */
9057790c8c1SConrad Meyer
9067790c8c1SConrad Meyer static void
dn_ifnet_event(void * arg __unused,struct ifnet * ifp,int link_state)9077790c8c1SConrad Meyer dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
9087790c8c1SConrad Meyer {
9097790c8c1SConrad Meyer if (link_state == LINK_STATE_UP)
9107790c8c1SConrad Meyer debugnet_any_ifnet_update(ifp);
9117790c8c1SConrad Meyer }
9127790c8c1SConrad Meyer
9137790c8c1SConrad Meyer static eventhandler_tag dn_attach_cookie;
9147790c8c1SConrad Meyer static void
dn_evh_init(void * ctx __unused)9157790c8c1SConrad Meyer dn_evh_init(void *ctx __unused)
9167790c8c1SConrad Meyer {
9177790c8c1SConrad Meyer dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
9187790c8c1SConrad Meyer dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
9197790c8c1SConrad Meyer }
9207790c8c1SConrad Meyer SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
9218270d35eSConrad Meyer
9228270d35eSConrad Meyer /*
9238270d35eSConrad Meyer * DDB parsing helpers for debugnet(4) consumers.
9248270d35eSConrad Meyer */
9258270d35eSConrad Meyer #ifdef DDB
9268270d35eSConrad Meyer struct my_inet_opt {
9278270d35eSConrad Meyer bool has_opt;
9288270d35eSConrad Meyer const char *printname;
9298270d35eSConrad Meyer in_addr_t *result;
9308270d35eSConrad Meyer };
9318270d35eSConrad Meyer
9328270d35eSConrad Meyer static int
dn_parse_optarg_ipv4(struct my_inet_opt * opt)9338270d35eSConrad Meyer dn_parse_optarg_ipv4(struct my_inet_opt *opt)
9348270d35eSConrad Meyer {
9358270d35eSConrad Meyer in_addr_t tmp;
9368270d35eSConrad Meyer unsigned octet;
9378270d35eSConrad Meyer int t;
9388270d35eSConrad Meyer
9398270d35eSConrad Meyer tmp = 0;
9408270d35eSConrad Meyer for (octet = 0; octet < 4; octet++) {
9418270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
9428270d35eSConrad Meyer if (t != tNUMBER) {
9438270d35eSConrad Meyer db_printf("%s:%s: octet %u expected number; found %d\n",
9448270d35eSConrad Meyer __func__, opt->printname, octet, t);
9458270d35eSConrad Meyer return (EINVAL);
9468270d35eSConrad Meyer }
9478270d35eSConrad Meyer /*
9488270d35eSConrad Meyer * db_lex lexes '-' distinctly from the number itself, but
9498270d35eSConrad Meyer * let's document that invariant.
9508270d35eSConrad Meyer */
9518270d35eSConrad Meyer MPASS(db_tok_number >= 0);
9528270d35eSConrad Meyer
9538270d35eSConrad Meyer if (db_tok_number > UINT8_MAX) {
9548270d35eSConrad Meyer db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
9558270d35eSConrad Meyer opt->printname, octet, (intmax_t)db_tok_number);
9568270d35eSConrad Meyer return (EDOM);
9578270d35eSConrad Meyer }
9588270d35eSConrad Meyer
9598270d35eSConrad Meyer /* Constructed host-endian and converted to network later. */
9608270d35eSConrad Meyer tmp = (tmp << 8) | db_tok_number;
9618270d35eSConrad Meyer
9628270d35eSConrad Meyer if (octet < 3) {
9638270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
9648270d35eSConrad Meyer if (t != tDOT) {
9658270d35eSConrad Meyer db_printf("%s:%s: octet %u expected '.'; found"
9668270d35eSConrad Meyer " %d\n", __func__, opt->printname, octet,
9678270d35eSConrad Meyer t);
9688270d35eSConrad Meyer return (EINVAL);
9698270d35eSConrad Meyer }
9708270d35eSConrad Meyer }
9718270d35eSConrad Meyer }
9728270d35eSConrad Meyer
9738270d35eSConrad Meyer *opt->result = htonl(tmp);
9748270d35eSConrad Meyer opt->has_opt = true;
9758270d35eSConrad Meyer return (0);
9768270d35eSConrad Meyer }
9778270d35eSConrad Meyer
9788270d35eSConrad Meyer int
debugnet_parse_ddb_cmd(const char * cmd,struct debugnet_ddb_config * result)9798270d35eSConrad Meyer debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
9808270d35eSConrad Meyer {
9818270d35eSConrad Meyer struct ifnet *ifp;
9828270d35eSConrad Meyer int t, error;
9838270d35eSConrad Meyer bool want_ifp;
9848270d35eSConrad Meyer char ch;
9858270d35eSConrad Meyer
9868270d35eSConrad Meyer struct my_inet_opt opt_client = {
9878270d35eSConrad Meyer .printname = "client",
9888270d35eSConrad Meyer .result = &result->dd_client,
9898270d35eSConrad Meyer },
9908270d35eSConrad Meyer opt_server = {
9918270d35eSConrad Meyer .printname = "server",
9928270d35eSConrad Meyer .result = &result->dd_server,
9938270d35eSConrad Meyer },
9948270d35eSConrad Meyer opt_gateway = {
9958270d35eSConrad Meyer .printname = "gateway",
9968270d35eSConrad Meyer .result = &result->dd_gateway,
9978270d35eSConrad Meyer },
9988270d35eSConrad Meyer *cur_inet_opt;
9998270d35eSConrad Meyer
10008270d35eSConrad Meyer ifp = NULL;
10018270d35eSConrad Meyer memset(result, 0, sizeof(*result));
10028270d35eSConrad Meyer
10038270d35eSConrad Meyer /*
10048270d35eSConrad Meyer * command [space] [-] [opt] [[space] [optarg]] ...
10058270d35eSConrad Meyer *
10068270d35eSConrad Meyer * db_command has already lexed 'command' for us.
10078270d35eSConrad Meyer */
10088270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
10098270d35eSConrad Meyer if (t == tWSPACE)
10108270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
10118270d35eSConrad Meyer
10128270d35eSConrad Meyer while (t != tEOL) {
10138270d35eSConrad Meyer if (t != tMINUS) {
10148270d35eSConrad Meyer db_printf("%s: Bad syntax; expected '-', got %d\n",
10158270d35eSConrad Meyer cmd, t);
10168270d35eSConrad Meyer goto usage;
10178270d35eSConrad Meyer }
10188270d35eSConrad Meyer
10198270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
10208270d35eSConrad Meyer if (t != tIDENT) {
10218270d35eSConrad Meyer db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
10228270d35eSConrad Meyer cmd, t);
10238270d35eSConrad Meyer goto usage;
10248270d35eSConrad Meyer }
10258270d35eSConrad Meyer
10268270d35eSConrad Meyer if (strlen(db_tok_string) > 1) {
10278270d35eSConrad Meyer db_printf("%s: Bad syntax; expected single option "
10288270d35eSConrad Meyer "flag, got '%s'\n", cmd, db_tok_string);
10298270d35eSConrad Meyer goto usage;
10308270d35eSConrad Meyer }
10318270d35eSConrad Meyer
10328270d35eSConrad Meyer want_ifp = false;
10338270d35eSConrad Meyer cur_inet_opt = NULL;
10348270d35eSConrad Meyer switch ((ch = db_tok_string[0])) {
10358270d35eSConrad Meyer default:
10368270d35eSConrad Meyer DNETDEBUG("Unexpected: '%c'\n", ch);
10378270d35eSConrad Meyer /* FALLTHROUGH */
10388270d35eSConrad Meyer case 'h':
10398270d35eSConrad Meyer goto usage;
10408270d35eSConrad Meyer case 'c':
10418270d35eSConrad Meyer cur_inet_opt = &opt_client;
10428270d35eSConrad Meyer break;
10438270d35eSConrad Meyer case 'g':
10448270d35eSConrad Meyer cur_inet_opt = &opt_gateway;
10458270d35eSConrad Meyer break;
10468270d35eSConrad Meyer case 's':
10478270d35eSConrad Meyer cur_inet_opt = &opt_server;
10488270d35eSConrad Meyer break;
10498270d35eSConrad Meyer case 'i':
10508270d35eSConrad Meyer want_ifp = true;
10518270d35eSConrad Meyer break;
10528270d35eSConrad Meyer }
10538270d35eSConrad Meyer
10548270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
10558270d35eSConrad Meyer if (t != tWSPACE) {
10568270d35eSConrad Meyer db_printf("%s: Bad syntax; expected space after "
10578270d35eSConrad Meyer "flag %c, got %d\n", cmd, ch, t);
10588270d35eSConrad Meyer goto usage;
10598270d35eSConrad Meyer }
10608270d35eSConrad Meyer
10618270d35eSConrad Meyer if (want_ifp) {
10628270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
10638270d35eSConrad Meyer if (t != tIDENT) {
10648270d35eSConrad Meyer db_printf("%s: Expected interface but got %d\n",
10658270d35eSConrad Meyer cmd, t);
10668270d35eSConrad Meyer goto usage;
10678270d35eSConrad Meyer }
10688270d35eSConrad Meyer
10698270d35eSConrad Meyer CURVNET_SET(vnet0);
10708270d35eSConrad Meyer /*
10718270d35eSConrad Meyer * We *don't* take a ref here because the only current
10728270d35eSConrad Meyer * consumer, db_netdump_cmd, does not need it. It
10738270d35eSConrad Meyer * (somewhat redundantly) extracts the if_name(),
10748270d35eSConrad Meyer * re-lookups the ifp, and takes its own reference.
10758270d35eSConrad Meyer */
10768270d35eSConrad Meyer ifp = ifunit(db_tok_string);
10778270d35eSConrad Meyer CURVNET_RESTORE();
10788270d35eSConrad Meyer if (ifp == NULL) {
10798270d35eSConrad Meyer db_printf("Could not locate interface %s\n",
10808270d35eSConrad Meyer db_tok_string);
1081c262d5e8SMark Johnston error = ENOENT;
10828270d35eSConrad Meyer goto cleanup;
10838270d35eSConrad Meyer }
10848270d35eSConrad Meyer } else {
10858270d35eSConrad Meyer MPASS(cur_inet_opt != NULL);
10868270d35eSConrad Meyer /* Assume IPv4 for now. */
10878270d35eSConrad Meyer error = dn_parse_optarg_ipv4(cur_inet_opt);
10888270d35eSConrad Meyer if (error != 0)
10898270d35eSConrad Meyer goto cleanup;
10908270d35eSConrad Meyer }
10918270d35eSConrad Meyer
10928270d35eSConrad Meyer /* Skip (mandatory) whitespace after option, if not EOL. */
10938270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
10948270d35eSConrad Meyer if (t == tEOL)
10958270d35eSConrad Meyer break;
10968270d35eSConrad Meyer if (t != tWSPACE) {
10978270d35eSConrad Meyer db_printf("%s: Bad syntax; expected space after "
10988270d35eSConrad Meyer "flag %c option; got %d\n", cmd, ch, t);
10998270d35eSConrad Meyer goto usage;
11008270d35eSConrad Meyer }
11018270d35eSConrad Meyer t = db_read_token_flags(DRT_WSPACE);
11028270d35eSConrad Meyer }
11038270d35eSConrad Meyer
1104fde2cf65SConrad Meyer if (!opt_server.has_opt) {
1105fde2cf65SConrad Meyer db_printf("%s: need a destination server address\n", cmd);
11068270d35eSConrad Meyer goto usage;
11078270d35eSConrad Meyer }
11088270d35eSConrad Meyer
1109fde2cf65SConrad Meyer result->dd_has_client = opt_client.has_opt;
11108270d35eSConrad Meyer result->dd_has_gateway = opt_gateway.has_opt;
11118270d35eSConrad Meyer result->dd_ifp = ifp;
11128270d35eSConrad Meyer
11138270d35eSConrad Meyer /* We parsed the full line to tEOL already, or bailed with an error. */
11148270d35eSConrad Meyer return (0);
11158270d35eSConrad Meyer
11168270d35eSConrad Meyer usage:
1117fde2cf65SConrad Meyer db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
1118fde2cf65SConrad Meyer "-i <interface>]\n", cmd);
11198270d35eSConrad Meyer error = EINVAL;
11208270d35eSConrad Meyer /* FALLTHROUGH */
11218270d35eSConrad Meyer cleanup:
11228270d35eSConrad Meyer db_skip_to_eol();
11238270d35eSConrad Meyer return (error);
11248270d35eSConrad Meyer }
11258270d35eSConrad Meyer #endif /* DDB */
1126