2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Isilon Systems, LLC.
5 * Copyright (c) 2005-2014 Sandvine Incorporated. All rights reserved.
6 * Copyright (c) 2000 Darrell Anderson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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>
46 #include <ddb/db_lex.h>
49 #include <net/ethernet.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>
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>
65 #include <machine/in_cksum.h>
66 #include <machine/pcb.h>
68 #include <net/debugnet.h>
69 #define DEBUGNET_INTERNAL
70 #include <net/debugnet_int.h>
72 FEATURE(debugnet, "Debugnet support");
74 SYSCTL_NODE(_net, OID_AUTO, debugnet, CTLFLAG_RD, NULL,
75 "debugnet parameters");
77 unsigned debugnet_debug;
78 SYSCTL_UINT(_net_debugnet, OID_AUTO, debug, CTLFLAG_RWTUN,
80 "Debug message verbosity (0: off; 1: on; 2: verbose)");
82 int debugnet_npolls = 2000;
83 SYSCTL_INT(_net_debugnet, OID_AUTO, npolls, CTLFLAG_RWTUN,
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");
91 static bool g_debugnet_pcb_inuse;
92 static struct debugnet_pcb g_dnet_pcb;
95 * Simple accessors for opaque PCB.
98 debugnet_get_gw_mac(const struct debugnet_pcb *pcb)
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);
106 * Start of network primitives, beginning with output primitives.
110 * Handles creation of the ethernet header, then places outgoing packets into
111 * the tx buffer for the NIC
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
119 * etype The ETHERTYPE_* value for the protocol that is being sent
122 * int see errno.h, 0 for success
125 debugnet_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
128 struct ether_header *eh;
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__);
137 /* Fill in the ethernet header. */
138 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
140 printf("%s: out of mbufs\n", __func__);
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));
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
156 * pcb The debugnet context block
160 * int see errno.h, 0 for success
163 debugnet_udp_output(struct debugnet_pcb *pcb, struct mbuf *m)
167 MPASS(pcb->dp_state >= DN_STATE_HAVE_GW_MAC);
169 M_PREPEND(m, sizeof(*udp), M_NOWAIT);
171 printf("%s: out of mbufs\n", __func__);
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_ack_port);
179 udp->uh_dport = htons(pcb->dp_server_port);
180 /* Computed later (protocol-dependent). */
183 return (debugnet_ip_output(pcb, m));
187 * Dummy free function for debugnet clusters.
190 debugnet_mbuf_free(struct mbuf *m __unused)
195 * Construct and reliably send a debugnet packet. May fail from a resource
196 * shortage or extreme number of unacknowledged retransmissions. Wait for
197 * an acknowledgement before returning. Splits packets into chunks small
198 * enough to be sent without fragmentation (looks up the interface MTU)
201 * type debugnet packet type (HERALD, FINISHED, ...)
203 * datalen data size (bytes)
204 * auxdata optional auxiliary information
207 * int see errno.h, 0 for success
210 debugnet_send(struct debugnet_pcb *pcb, uint32_t type, const void *data,
211 uint32_t datalen, const struct debugnet_proto_aux *auxdata)
213 struct debugnet_msg_hdr *dn_msg_hdr;
216 uint32_t i, pktlen, sent_so_far;
217 int retries, polls, error;
220 pcb->dp_rcvd_acks = 0;
224 /* Chunks can be too big to fit in packets. */
225 for (i = sent_so_far = 0; sent_so_far < datalen ||
226 (i == 0 && datalen == 0); i++) {
227 pktlen = datalen - sent_so_far;
229 /* Bound: the interface MTU (assume no IP options). */
230 pktlen = min(pktlen, pcb->dp_ifp->if_mtu -
231 sizeof(struct udpiphdr) - sizeof(struct debugnet_msg_hdr));
234 * Check if it is retransmitting and this has been ACKed
237 if ((pcb->dp_rcvd_acks & (1 << i)) != 0) {
238 sent_so_far += pktlen;
243 * Get and fill a header mbuf, then chain data as an extended
246 m = m_gethdr(M_NOWAIT, MT_DATA);
248 printf("%s: Out of mbufs\n", __func__);
251 m->m_len = sizeof(struct debugnet_msg_hdr);
252 m->m_pkthdr.len = sizeof(struct debugnet_msg_hdr);
253 MH_ALIGN(m, sizeof(struct debugnet_msg_hdr));
254 dn_msg_hdr = mtod(m, struct debugnet_msg_hdr *);
255 dn_msg_hdr->mh_seqno = htonl(pcb->dp_seqno + i);
256 dn_msg_hdr->mh_type = htonl(type);
257 dn_msg_hdr->mh_len = htonl(pktlen);
259 if (auxdata != NULL) {
260 dn_msg_hdr->mh_offset =
261 htobe64(auxdata->dp_offset_start + sent_so_far);
262 dn_msg_hdr->mh_aux2 = htobe32(auxdata->dp_aux2);
264 dn_msg_hdr->mh_offset = htobe64(sent_so_far);
265 dn_msg_hdr->mh_aux2 = 0;
269 m2 = m_get(M_NOWAIT, MT_DATA);
272 printf("%s: Out of mbufs\n", __func__);
275 MEXTADD(m2, __DECONST(char *, data) + sent_so_far,
276 pktlen, debugnet_mbuf_free, NULL, NULL, 0,
281 m->m_pkthdr.len += pktlen;
283 error = debugnet_udp_output(pcb, m);
287 /* Note that we're waiting for this packet in the bitfield. */
288 want_acks |= (1 << i);
289 sent_so_far += pktlen;
291 if (i >= DEBUGNET_MAX_IN_FLIGHT)
292 printf("Warning: Sent more than %d packets (%d). "
293 "Acknowledgements will fail unless the size of "
294 "rcvd_acks/want_acks is increased.\n",
295 DEBUGNET_MAX_IN_FLIGHT, i);
298 * Wait for acks. A *real* window would speed things up considerably.
301 while (pcb->dp_rcvd_acks != want_acks) {
302 if (polls++ > debugnet_npolls) {
303 if (retries++ > debugnet_nretries)
308 debugnet_network_poll(pcb->dp_ifp);
316 * Network input primitives.
320 debugnet_handle_ack(struct debugnet_pcb *pcb, struct mbuf **mb, uint16_t sport)
322 const struct debugnet_ack *dn_ack;
328 if (m->m_pkthdr.len < sizeof(*dn_ack)) {
329 DNETDEBUG("ignoring small ACK packet\n");
333 if (m->m_len < sizeof(*dn_ack)) {
334 m = m_pullup(m, sizeof(*dn_ack));
337 DNETDEBUG("m_pullup failed\n");
341 dn_ack = mtod(m, const void *);
343 /* Debugnet processing. */
345 * Packet is meant for us. Extract the ack sequence number and the
346 * port number if necessary.
348 rcv_ackno = ntohl(dn_ack->da_seqno);
349 if (pcb->dp_state < DN_STATE_GOT_HERALD_PORT) {
350 pcb->dp_server_port = sport;
351 pcb->dp_state = DN_STATE_GOT_HERALD_PORT;
353 if (rcv_ackno >= pcb->dp_seqno + DEBUGNET_MAX_IN_FLIGHT)
354 printf("%s: ACK %u too far in future!\n", __func__, rcv_ackno);
355 else if (rcv_ackno >= pcb->dp_seqno) {
356 /* We're interested in this ack. Record it. */
357 pcb->dp_rcvd_acks |= 1 << (rcv_ackno - pcb->dp_seqno);
362 debugnet_handle_udp(struct debugnet_pcb *pcb, struct mbuf **mb)
364 const struct udphdr *udp;
368 /* UDP processing. */
371 if (m->m_pkthdr.len < sizeof(*udp)) {
372 DNETDEBUG("ignoring small UDP packet\n");
376 /* Get UDP headers. */
377 if (m->m_len < sizeof(*udp)) {
378 m = m_pullup(m, sizeof(*udp));
381 DNETDEBUG("m_pullup failed\n");
385 udp = mtod(m, const void *);
387 /* For now, the only UDP packets we expect to receive are acks. */
388 if (ntohs(udp->uh_dport) != pcb->dp_client_ack_port) {
389 DNETDEBUG("not on the expected ACK port.\n");
392 sport = ntohs(udp->uh_sport);
394 m_adj(m, sizeof(*udp));
395 debugnet_handle_ack(pcb, mb, sport);
399 * Handler for incoming packets directly from the network adapter
400 * Identifies the packet type (IP or ARP) and passes it along to one of the
401 * helper functions debugnet_handle_ip or debugnet_handle_arp.
403 * It needs to partially replicate the behaviour of ether_input() and
407 * ifp the interface the packet came from
408 * m an mbuf containing the packet received
411 debugnet_pkt_in(struct ifnet *ifp, struct mbuf *m)
414 struct ether_header *eh;
417 /* Ethernet processing. */
418 if ((m->m_flags & M_PKTHDR) == 0) {
419 DNETDEBUG_IF(ifp, "discard frame without packet header\n");
422 if (m->m_len < ETHER_HDR_LEN) {
424 "discard frame without leading eth header (len %u pktlen %u)\n",
425 m->m_len, m->m_pkthdr.len);
428 if ((m->m_flags & M_HASFCS) != 0) {
429 m_adj(m, -ETHER_CRC_LEN);
430 m->m_flags &= ~M_HASFCS;
432 eh = mtod(m, struct ether_header *);
433 etype = ntohs(eh->ether_type);
434 if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
435 DNETDEBUG_IF(ifp, "ignoring vlan packets\n");
438 if (if_gethwaddr(ifp, &ifr) != 0) {
439 DNETDEBUG_IF(ifp, "failed to get hw addr for interface\n");
442 if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
443 ETHER_ADDR_LEN) != 0 &&
444 (etype != ETHERTYPE_ARP || !ETHER_IS_BROADCAST(eh->ether_dhost))) {
446 "discard frame with incorrect destination addr\n");
450 MPASS(g_debugnet_pcb_inuse);
452 /* Done ethernet processing. Strip off the ethernet header. */
453 m_adj(m, ETHER_HDR_LEN);
456 debugnet_handle_arp(&g_dnet_pcb, &m);
459 debugnet_handle_ip(&g_dnet_pcb, &m);
462 DNETDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
471 * Network polling primitive.
473 * Instead of assuming that most of the network stack is sane, we just poll the
474 * driver directly for packets.
477 debugnet_network_poll(struct ifnet *ifp)
479 ifp->if_debugnet_methods->dn_poll(ifp, 1000);
483 * Start of consumer API surface.
486 debugnet_free(struct debugnet_pcb *pcb)
490 MPASS(g_debugnet_pcb_inuse);
491 MPASS(pcb == &g_dnet_pcb);
495 if (pcb->dp_drv_input != NULL)
496 ifp->if_input = pcb->dp_drv_input;
497 if (pcb->dp_event_started)
498 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_END);
500 debugnet_mbuf_finish();
502 g_debugnet_pcb_inuse = false;
503 memset(&g_dnet_pcb, 0xfd, sizeof(g_dnet_pcb));
507 debugnet_connect(const struct debugnet_conn_params *dcp,
508 struct debugnet_pcb **pcb_out)
510 struct debugnet_pcb *pcb;
514 if (g_debugnet_pcb_inuse) {
515 printf("%s: Only one connection at a time.\n", __func__);
520 *pcb = (struct debugnet_pcb) {
521 .dp_state = DN_STATE_INIT,
522 .dp_client = dcp->dc_client,
523 .dp_server = dcp->dc_server,
524 .dp_gateway = dcp->dc_gateway,
525 .dp_server_port = dcp->dc_herald_port, /* Initially */
526 .dp_client_ack_port = dcp->dc_client_ack_port,
528 .dp_ifp = dcp->dc_ifp,
531 /* Switch to the debugnet mbuf zones. */
532 debugnet_mbuf_start();
534 /* At least one needed parameter is missing; infer it. */
535 if (pcb->dp_client == INADDR_ANY || pcb->dp_gateway == INADDR_ANY ||
536 pcb->dp_ifp == NULL) {
537 struct sockaddr_in dest_sin, *gw_sin, *local_sin;
538 struct rtentry *dest_rt;
539 struct ifnet *rt_ifp;
541 memset(&dest_sin, 0, sizeof(dest_sin));
542 dest_sin = (struct sockaddr_in) {
543 .sin_len = sizeof(dest_sin),
544 .sin_family = AF_INET,
545 .sin_addr.s_addr = pcb->dp_server,
549 dest_rt = rtalloc1((struct sockaddr *)&dest_sin, 0,
553 if (dest_rt == NULL) {
554 db_printf("%s: Could not get route for that server.\n",
560 if (dest_rt->rt_gateway->sa_family == AF_INET)
561 gw_sin = (struct sockaddr_in *)dest_rt->rt_gateway;
563 if (dest_rt->rt_gateway->sa_family == AF_LINK)
564 DNETDEBUG("Destination address is on link.\n");
568 MPASS(dest_rt->rt_ifa->ifa_addr->sa_family == AF_INET);
569 local_sin = (struct sockaddr_in *)dest_rt->rt_ifa->ifa_addr;
571 rt_ifp = dest_rt->rt_ifp;
573 if (pcb->dp_client == INADDR_ANY)
574 pcb->dp_client = local_sin->sin_addr.s_addr;
575 if (pcb->dp_gateway == INADDR_ANY && gw_sin != NULL)
576 pcb->dp_gateway = gw_sin->sin_addr.s_addr;
577 if (pcb->dp_ifp == NULL)
578 pcb->dp_ifp = rt_ifp;
580 RTFREE_LOCKED(dest_rt);
585 if (debugnet_debug > 0) {
586 char serbuf[INET_ADDRSTRLEN], clibuf[INET_ADDRSTRLEN],
587 gwbuf[INET_ADDRSTRLEN];
588 inet_ntop(AF_INET, &pcb->dp_server, serbuf, sizeof(serbuf));
589 inet_ntop(AF_INET, &pcb->dp_client, clibuf, sizeof(clibuf));
590 if (pcb->dp_gateway != INADDR_ANY)
591 inet_ntop(AF_INET, &pcb->dp_gateway, gwbuf, sizeof(gwbuf));
592 DNETDEBUG("Connecting to %s:%d%s%s from %s:%d on %s\n",
593 serbuf, pcb->dp_server_port,
594 (pcb->dp_gateway == INADDR_ANY) ? "" : " via ",
595 (pcb->dp_gateway == INADDR_ANY) ? "" : gwbuf,
596 clibuf, pcb->dp_client_ack_port, if_name(ifp));
599 /* Validate iface is online and supported. */
600 if (!DEBUGNET_SUPPORTED_NIC(ifp)) {
601 printf("%s: interface '%s' does not support debugnet\n",
602 __func__, if_name(ifp));
606 if ((if_getflags(ifp) & IFF_UP) == 0) {
607 printf("%s: interface '%s' link is down\n", __func__,
613 ifp->if_debugnet_methods->dn_event(ifp, DEBUGNET_START);
614 pcb->dp_event_started = true;
617 * We maintain the invariant that g_debugnet_pcb_inuse is always true
618 * while the debugnet ifp's if_input is overridden with
621 g_debugnet_pcb_inuse = true;
623 /* Make the card use *our* receive callback. */
624 pcb->dp_drv_input = ifp->if_input;
625 ifp->if_input = debugnet_pkt_in;
627 printf("%s: searching for %s MAC...\n", __func__,
628 (dcp->dc_gateway == INADDR_ANY) ? "server" : "gateway");
630 error = debugnet_arp_gw(pcb);
632 printf("%s: failed to locate MAC address\n", __func__);
635 MPASS(pcb->dp_state == DN_STATE_HAVE_GW_MAC);
637 error = debugnet_send(pcb, DEBUGNET_HERALD, dcp->dc_herald_data,
638 dcp->dc_herald_datalen, NULL);
640 printf("%s: failed to herald debugnet server\n", __func__);
653 * Pre-allocated dump-time mbuf tracking.
655 * We just track the high water mark we've ever seen and allocate appropriately
656 * for that iface/mtu combo.
663 static struct mtx dn_hwm_lk;
664 MTX_SYSINIT(debugnet_hwm_lock, &dn_hwm_lk, "Debugnet HWM lock", MTX_DEF);
667 dn_maybe_reinit_mbufs(int nmbuf, int ncl, int clsize)
672 mtx_lock(&dn_hwm_lk);
674 if (nmbuf > dn_hwm.nmbuf) {
676 dn_hwm.nmbuf = nmbuf;
678 nmbuf = dn_hwm.nmbuf;
680 if (ncl > dn_hwm.ncl) {
686 if (clsize > dn_hwm.clsize) {
688 dn_hwm.clsize = clsize;
690 clsize = dn_hwm.clsize;
692 mtx_unlock(&dn_hwm_lk);
695 debugnet_mbuf_reinit(nmbuf, ncl, clsize);
699 debugnet_any_ifnet_update(struct ifnet *ifp)
701 int clsize, nmbuf, ncl, nrxr;
703 if (!DEBUGNET_SUPPORTED_NIC(ifp))
706 ifp->if_debugnet_methods->dn_init(ifp, &nrxr, &ncl, &clsize);
707 KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
710 * We need two headers per message on the transmit side. Multiply by
711 * four to give us some breathing room.
713 nmbuf = ncl * (4 + nrxr);
716 dn_maybe_reinit_mbufs(nmbuf, ncl, clsize);
720 * Unfortunately, the ifnet_arrival_event eventhandler hook is mostly useless
721 * for us because drivers tend to if_attach before invoking DEBUGNET_SET().
723 * On the other hand, hooking DEBUGNET_SET() itself may still be too early,
724 * because the driver is still in attach. Since we cannot use down interfaces,
725 * maybe hooking ifnet_event:IFNET_EVENT_UP is sufficient? ... Nope, at least
726 * with vtnet and dhcpclient that event just never occurs.
728 * So that's how I've landed on the lower level ifnet_link_event.
732 dn_ifnet_event(void *arg __unused, struct ifnet *ifp, int link_state)
734 if (link_state == LINK_STATE_UP)
735 debugnet_any_ifnet_update(ifp);
738 static eventhandler_tag dn_attach_cookie;
740 dn_evh_init(void *ctx __unused)
742 dn_attach_cookie = EVENTHANDLER_REGISTER(ifnet_link_event,
743 dn_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
745 SYSINIT(dn_evh_init, SI_SUB_EVENTHANDLER + 1, SI_ORDER_ANY, dn_evh_init, NULL);
748 * DDB parsing helpers for debugnet(4) consumers.
753 const char *printname;
758 dn_parse_optarg_ipv4(struct my_inet_opt *opt)
765 for (octet = 0; octet < 4; octet++) {
766 t = db_read_token_flags(DRT_WSPACE | DRT_DECIMAL);
768 db_printf("%s:%s: octet %u expected number; found %d\n",
769 __func__, opt->printname, octet, t);
773 * db_lex lexes '-' distinctly from the number itself, but
774 * let's document that invariant.
776 MPASS(db_tok_number >= 0);
778 if (db_tok_number > UINT8_MAX) {
779 db_printf("%s:%s: octet %u out of range: %jd\n", __func__,
780 opt->printname, octet, (intmax_t)db_tok_number);
784 /* Constructed host-endian and converted to network later. */
785 tmp = (tmp << 8) | db_tok_number;
788 t = db_read_token_flags(DRT_WSPACE);
790 db_printf("%s:%s: octet %u expected '.'; found"
791 " %d\n", __func__, opt->printname, octet,
798 *opt->result = htonl(tmp);
804 debugnet_parse_ddb_cmd(const char *cmd, struct debugnet_ddb_config *result)
811 struct my_inet_opt opt_client = {
812 .printname = "client",
813 .result = &result->dd_client,
816 .printname = "server",
817 .result = &result->dd_server,
820 .printname = "gateway",
821 .result = &result->dd_gateway,
826 memset(result, 0, sizeof(*result));
829 * command [space] [-] [opt] [[space] [optarg]] ...
831 * db_command has already lexed 'command' for us.
833 t = db_read_token_flags(DRT_WSPACE);
835 t = db_read_token_flags(DRT_WSPACE);
839 db_printf("%s: Bad syntax; expected '-', got %d\n",
844 t = db_read_token_flags(DRT_WSPACE);
846 db_printf("%s: Bad syntax; expected tIDENT, got %d\n",
851 if (strlen(db_tok_string) > 1) {
852 db_printf("%s: Bad syntax; expected single option "
853 "flag, got '%s'\n", cmd, db_tok_string);
859 switch ((ch = db_tok_string[0])) {
861 DNETDEBUG("Unexpected: '%c'\n", ch);
866 cur_inet_opt = &opt_client;
869 cur_inet_opt = &opt_gateway;
872 cur_inet_opt = &opt_server;
879 t = db_read_token_flags(DRT_WSPACE);
881 db_printf("%s: Bad syntax; expected space after "
882 "flag %c, got %d\n", cmd, ch, t);
887 t = db_read_token_flags(DRT_WSPACE);
889 db_printf("%s: Expected interface but got %d\n",
896 * We *don't* take a ref here because the only current
897 * consumer, db_netdump_cmd, does not need it. It
898 * (somewhat redundantly) extracts the if_name(),
899 * re-lookups the ifp, and takes its own reference.
901 ifp = ifunit(db_tok_string);
904 db_printf("Could not locate interface %s\n",
909 MPASS(cur_inet_opt != NULL);
910 /* Assume IPv4 for now. */
911 error = dn_parse_optarg_ipv4(cur_inet_opt);
916 /* Skip (mandatory) whitespace after option, if not EOL. */
917 t = db_read_token_flags(DRT_WSPACE);
921 db_printf("%s: Bad syntax; expected space after "
922 "flag %c option; got %d\n", cmd, ch, t);
925 t = db_read_token_flags(DRT_WSPACE);
928 if (!opt_server.has_opt) {
929 db_printf("%s: need a destination server address\n", cmd);
933 result->dd_has_client = opt_client.has_opt;
934 result->dd_has_gateway = opt_gateway.has_opt;
935 result->dd_ifp = ifp;
937 /* We parsed the full line to tEOL already, or bailed with an error. */
941 db_printf("Usage: %s -s <server> [-g <gateway> -c <localip> "
942 "-i <interface>]\n", cmd);