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