2 * Copyright (c) 2005-2014 Sandvine Incorporated. All rights reserved.
3 * Copyright (c) 2000 Darrell Anderson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * FreeBSD subsystem supporting netdump network dumps.
31 * A dedicated server must be running to accept client dumps.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
40 #include <sys/endian.h>
42 #include <sys/kernel.h>
43 #include <sys/kerneldump.h>
45 #include <sys/module.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
53 #include <net/ethernet.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_var.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/ip_options.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
68 #include <netinet/netdump/netdump.h>
70 #include <machine/in_cksum.h>
71 #include <machine/pcb.h>
73 #define NETDDEBUG(f, ...) do { \
75 printf(("%s: " f), __func__, ## __VA_ARGS__); \
77 #define NETDDEBUG_IF(i, f, ...) do { \
79 if_printf((i), ("%s: " f), __func__, ## __VA_ARGS__); \
81 #define NETDDEBUGV(f, ...) do { \
83 printf(("%s: " f), __func__, ## __VA_ARGS__); \
85 #define NETDDEBUGV_IF(i, f, ...) do { \
87 if_printf((i), ("%s: " f), __func__, ## __VA_ARGS__); \
90 static int netdump_arp_gw(void);
91 static void netdump_cleanup(void);
92 static int netdump_configure(struct netdump_conf *, struct thread *);
93 static int netdump_dumper(void *priv __unused, void *virtual,
94 vm_offset_t physical __unused, off_t offset, size_t length);
95 static int netdump_ether_output(struct mbuf *m, struct ifnet *ifp,
96 struct ether_addr dst, u_short etype);
97 static void netdump_handle_arp(struct mbuf **mb);
98 static void netdump_handle_ip(struct mbuf **mb);
99 static int netdump_ioctl(struct cdev *dev __unused, u_long cmd,
100 caddr_t addr, int flags __unused, struct thread *td);
101 static int netdump_modevent(module_t mod, int type, void *priv);
102 static void netdump_network_poll(void);
103 static void netdump_pkt_in(struct ifnet *ifp, struct mbuf *m);
104 static int netdump_send(uint32_t type, off_t offset, unsigned char *data,
106 static int netdump_send_arp(in_addr_t dst);
107 static int netdump_start(struct dumperinfo *di);
108 static int netdump_udp_output(struct mbuf *m);
110 /* Must be at least as big as the chunks dumpsys() gives us. */
111 static unsigned char nd_buf[MAXDUMPPGS * PAGE_SIZE];
112 static uint32_t nd_seqno;
113 static int dump_failed, have_gw_mac;
114 static void (*drv_if_input)(struct ifnet *, struct mbuf *);
115 static int restore_gw_addr;
117 static uint64_t rcvd_acks;
118 CTASSERT(sizeof(rcvd_acks) * NBBY == NETDUMP_MAX_IN_FLIGHT);
121 * Times to poll the NIC (0.5ms each poll) before assuming packetloss
122 * occurred (default to 1s).
124 static int nd_polls = 2000;
126 /* Times to retransmit lost packets. */
127 static int nd_retries = 10;
129 /* Number of ARP retries. */
130 static int nd_arp_retries = 3;
132 /* Configuration parameters. */
133 static struct netdump_conf nd_conf;
134 #define nd_server nd_conf.ndc_server
135 #define nd_client nd_conf.ndc_client
136 #define nd_gateway nd_conf.ndc_gateway
138 /* General dynamic settings. */
139 static struct ether_addr nd_gw_mac;
140 static struct ifnet *nd_ifp;
141 static uint16_t nd_server_port = NETDUMP_PORT;
143 FEATURE(netdump, "Netdump client support");
145 static SYSCTL_NODE(_net, OID_AUTO, netdump, CTLFLAG_RD, NULL,
146 "netdump parameters");
149 SYSCTL_INT(_net_netdump, OID_AUTO, debug, CTLFLAG_RWTUN,
151 "Debug message verbosity");
152 static int nd_enabled;
153 SYSCTL_INT(_net_netdump, OID_AUTO, enabled, CTLFLAG_RD,
155 "netdump configuration status");
156 static char nd_path[MAXPATHLEN];
157 SYSCTL_STRING(_net_netdump, OID_AUTO, path, CTLFLAG_RW,
158 nd_path, sizeof(nd_path),
159 "Server path for output files");
162 * Checks for netdump support on a network interface
165 * ifp The network interface that is being tested for support
168 * int 1 if the interface is supported, 0 if not
171 netdump_supported_nic(struct ifnet *ifp)
174 return (ifp->if_netdump_methods != NULL);
178 * Network specific primitives.
179 * Following down the code they are divided ordered as:
180 * - Packet buffer primitives
181 * - Output primitives
183 * - Polling primitives
187 * Handles creation of the ethernet header, then places outgoing packets into
188 * the tx buffer for the NIC
191 * m The mbuf containing the packet to be sent (will be freed by
192 * this function or the NIC driver)
193 * ifp The interface to send on
194 * dst The destination ethernet address (source address will be looked
196 * etype The ETHERTYPE_* value for the protocol that is being sent
199 * int see errno.h, 0 for success
202 netdump_ether_output(struct mbuf *m, struct ifnet *ifp, struct ether_addr dst,
205 struct ether_header *eh;
207 if (((ifp->if_flags & (IFF_MONITOR | IFF_UP)) != IFF_UP) ||
208 (ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
209 if_printf(ifp, "netdump_ether_output: interface isn't up\n");
214 /* Fill in the ethernet header. */
215 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
217 printf("%s: out of mbufs\n", __func__);
220 eh = mtod(m, struct ether_header *);
221 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
222 memcpy(eh->ether_dhost, dst.octet, ETHER_ADDR_LEN);
223 eh->ether_type = htons(etype);
224 return ((ifp->if_netdump_methods->nd_transmit)(ifp, m));
228 * Unreliable transmission of an mbuf chain to the netdump server
229 * Note: can't handle fragmentation; fails if the packet is larger than
230 * nd_ifp->if_mtu after adding the UDP/IP headers
236 * int see errno.h, 0 for success
239 netdump_udp_output(struct mbuf *m)
244 MPASS(nd_ifp != NULL);
246 M_PREPEND(m, sizeof(struct udpiphdr), M_NOWAIT);
248 printf("%s: out of mbufs\n", __func__);
252 if (m->m_pkthdr.len > nd_ifp->if_mtu) {
253 printf("netdump_udp_output: Packet is too big: %d > MTU %u\n",
254 m->m_pkthdr.len, nd_ifp->if_mtu);
259 ui = mtod(m, struct udpiphdr *);
260 bzero(ui->ui_x1, sizeof(ui->ui_x1));
261 ui->ui_pr = IPPROTO_UDP;
262 ui->ui_len = htons(m->m_pkthdr.len - sizeof(struct ip));
263 ui->ui_ulen = ui->ui_len;
264 ui->ui_src = nd_client;
265 ui->ui_dst = nd_server;
266 /* Use this src port so that the server can connect() the socket */
267 ui->ui_sport = htons(NETDUMP_ACKPORT);
268 ui->ui_dport = htons(nd_server_port);
270 if ((ui->ui_sum = in_cksum(m, m->m_pkthdr.len)) == 0)
273 ip = mtod(m, struct ip *);
274 ip->ip_v = IPVERSION;
275 ip->ip_hl = sizeof(struct ip) >> 2;
277 ip->ip_len = htons(m->m_pkthdr.len);
279 ip->ip_off = htons(IP_DF);
282 ip->ip_sum = in_cksum(m, sizeof(struct ip));
284 return (netdump_ether_output(m, nd_ifp, nd_gw_mac, ETHERTYPE_IP));
288 * Builds and sends a single ARP request to locate the server
295 netdump_send_arp(in_addr_t dst)
297 struct ether_addr bcast;
302 MPASS(nd_ifp != NULL);
304 /* Fill-up a broadcast address. */
305 memset(&bcast, 0xFF, ETHER_ADDR_LEN);
306 m = m_gethdr(M_NOWAIT, MT_DATA);
308 printf("netdump_send_arp: Out of mbufs\n");
311 pktlen = arphdr_len2(ETHER_ADDR_LEN, sizeof(struct in_addr));
313 m->m_pkthdr.len = pktlen;
315 ah = mtod(m, struct arphdr *);
316 ah->ar_hrd = htons(ARPHRD_ETHER);
317 ah->ar_pro = htons(ETHERTYPE_IP);
318 ah->ar_hln = ETHER_ADDR_LEN;
319 ah->ar_pln = sizeof(struct in_addr);
320 ah->ar_op = htons(ARPOP_REQUEST);
321 memcpy(ar_sha(ah), IF_LLADDR(nd_ifp), ETHER_ADDR_LEN);
322 ((struct in_addr *)ar_spa(ah))->s_addr = nd_client.s_addr;
323 bzero(ar_tha(ah), ETHER_ADDR_LEN);
324 ((struct in_addr *)ar_tpa(ah))->s_addr = dst;
325 return (netdump_ether_output(m, nd_ifp, bcast, ETHERTYPE_ARP));
329 * Sends ARP requests to locate the server and waits for a response.
330 * We first try to ARP the server itself, and fall back to the provided
331 * gateway if the server appears to be off-link.
341 int error, polls, retries;
343 dst = nd_server.s_addr;
345 for (retries = 0; retries < nd_arp_retries && have_gw_mac == 0;
347 error = netdump_send_arp(dst);
350 for (polls = 0; polls < nd_polls && have_gw_mac == 0; polls++) {
351 netdump_network_poll();
354 if (have_gw_mac == 0)
355 printf("(ARP retry)");
357 if (have_gw_mac != 0)
359 if (dst == nd_server.s_addr && nd_server.s_addr != nd_gateway.s_addr) {
360 printf("Failed to ARP server, trying to reach gateway...\n");
361 dst = nd_gateway.s_addr;
365 printf("\nARP timed out.\n");
370 * Dummy free function for netdump clusters.
373 netdump_mbuf_free(struct mbuf *m __unused)
378 * Construct and reliably send a netdump packet. May fail from a resource
379 * shortage or extreme number of unacknowledged retransmissions. Wait for
380 * an acknowledgement before returning. Splits packets into chunks small
381 * enough to be sent without fragmentation (looks up the interface MTU)
384 * type netdump packet type (HERALD, FINISHED, or VMCORE)
385 * offset vmcore data offset (bytes)
387 * datalen vmcore data size (bytes)
390 * int see errno.h, 0 for success
393 netdump_send(uint32_t type, off_t offset, unsigned char *data, uint32_t datalen)
395 struct netdump_msg_hdr *nd_msg_hdr;
398 uint32_t i, pktlen, sent_so_far;
399 int retries, polls, error;
405 MPASS(nd_ifp != NULL);
408 /* Chunks can be too big to fit in packets. */
409 for (i = sent_so_far = 0; sent_so_far < datalen ||
410 (i == 0 && datalen == 0); i++) {
411 pktlen = datalen - sent_so_far;
413 /* First bound: the packet structure. */
414 pktlen = min(pktlen, NETDUMP_DATASIZE);
416 /* Second bound: the interface MTU (assume no IP options). */
417 pktlen = min(pktlen, nd_ifp->if_mtu - sizeof(struct udpiphdr) -
418 sizeof(struct netdump_msg_hdr));
421 * Check if it is retransmitting and this has been ACKed
424 if ((rcvd_acks & (1 << i)) != 0) {
425 sent_so_far += pktlen;
430 * Get and fill a header mbuf, then chain data as an extended
433 m = m_gethdr(M_NOWAIT, MT_DATA);
435 printf("netdump_send: Out of mbufs\n");
438 m->m_len = sizeof(struct netdump_msg_hdr);
439 m->m_pkthdr.len = sizeof(struct netdump_msg_hdr);
440 MH_ALIGN(m, sizeof(struct netdump_msg_hdr));
441 nd_msg_hdr = mtod(m, struct netdump_msg_hdr *);
442 nd_msg_hdr->mh_seqno = htonl(nd_seqno + i);
443 nd_msg_hdr->mh_type = htonl(type);
444 nd_msg_hdr->mh_offset = htobe64(offset + sent_so_far);
445 nd_msg_hdr->mh_len = htonl(pktlen);
446 nd_msg_hdr->mh__pad = 0;
449 m2 = m_get(M_NOWAIT, MT_DATA);
452 printf("netdump_send: Out of mbufs\n");
455 MEXTADD(m2, data + sent_so_far, pktlen,
456 netdump_mbuf_free, NULL, NULL, 0, EXT_DISPOSABLE);
460 m->m_pkthdr.len += pktlen;
462 error = netdump_udp_output(m);
466 /* Note that we're waiting for this packet in the bitfield. */
467 want_acks |= (1 << i);
468 sent_so_far += pktlen;
470 if (i >= NETDUMP_MAX_IN_FLIGHT)
471 printf("Warning: Sent more than %d packets (%d). "
472 "Acknowledgements will fail unless the size of "
473 "rcvd_acks/want_acks is increased.\n",
474 NETDUMP_MAX_IN_FLIGHT, i);
477 * Wait for acks. A *real* window would speed things up considerably.
480 while (rcvd_acks != want_acks) {
481 if (polls++ > nd_polls) {
482 if (retries++ > nd_retries)
487 netdump_network_poll();
495 * Handler for IP packets: checks their sanity and then processes any netdump
496 * ACK packets it finds.
498 * It needs to replicate partially the behaviour of ip_input() and
502 * mb a pointer to an mbuf * containing the packet received
503 * Updates *mb if m_pullup et al change the pointer
504 * Assumes the calling function will take care of freeing the mbuf
507 netdump_handle_ip(struct mbuf **mb)
510 struct udpiphdr *udp;
511 struct netdump_ack *nd_ack;
518 if (m->m_pkthdr.len < sizeof(struct ip)) {
519 NETDDEBUG("dropping packet too small for IP header\n");
522 if (m->m_len < sizeof(struct ip)) {
523 m = m_pullup(m, sizeof(struct ip));
526 NETDDEBUG("m_pullup failed\n");
530 ip = mtod(m, struct ip *);
533 if (ip->ip_v != IPVERSION) {
534 NETDDEBUG("bad IP version %d\n", ip->ip_v);
539 hlen = ip->ip_hl << 2;
540 if (hlen < sizeof(struct ip)) {
541 NETDDEBUG("bad IP header length (%hu)\n", hlen);
544 if (hlen > m->m_len) {
545 m = m_pullup(m, hlen);
548 NETDDEBUG("m_pullup failed\n");
551 ip = mtod(m, struct ip *);
553 /* Ignore packets with IP options. */
554 if (hlen > sizeof(struct ip)) {
555 NETDDEBUG("drop packet with IP options\n");
560 if (((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
561 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) &&
562 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
563 NETDDEBUG("Bad IP header (RFC1122)\n");
569 if ((m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) != 0) {
570 if ((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0) {
571 NETDDEBUG("bad IP checksum\n");
578 /* Convert fields to host byte order. */
579 ip->ip_len = ntohs(ip->ip_len);
580 if (ip->ip_len < hlen) {
581 NETDDEBUG("IP packet smaller (%hu) than header (%hu)\n",
585 if (m->m_pkthdr.len < ip->ip_len) {
586 NETDDEBUG("IP packet bigger (%hu) than ethernet packet (%d)\n",
587 ip->ip_len, m->m_pkthdr.len);
590 if (m->m_pkthdr.len > ip->ip_len) {
592 /* Truncate the packet to the IP length. */
593 if (m->m_len == m->m_pkthdr.len) {
594 m->m_len = ip->ip_len;
595 m->m_pkthdr.len = ip->ip_len;
597 m_adj(m, ip->ip_len - m->m_pkthdr.len);
600 ip->ip_off = ntohs(ip->ip_off);
602 /* Check that the source is the server's IP. */
603 if (ip->ip_src.s_addr != nd_server.s_addr) {
604 NETDDEBUG("drop packet not from server (from 0x%x)\n",
609 /* Check if the destination IP is ours. */
610 if (ip->ip_dst.s_addr != nd_client.s_addr) {
611 NETDDEBUGV("drop packet not to our IP\n");
615 if (ip->ip_p != IPPROTO_UDP) {
616 NETDDEBUG("drop non-UDP packet\n");
620 /* Do not deal with fragments. */
621 if ((ip->ip_off & (IP_MF | IP_OFFMASK)) != 0) {
622 NETDDEBUG("drop fragmented packet\n");
626 /* UDP custom is to have packet length not include IP header. */
629 /* UDP processing. */
631 /* Get IP and UDP headers together, along with the netdump packet. */
632 if (m->m_pkthdr.len <
633 sizeof(struct udpiphdr) + sizeof(struct netdump_ack)) {
634 NETDDEBUG("ignoring small packet\n");
637 if (m->m_len < sizeof(struct udpiphdr) + sizeof(struct netdump_ack)) {
638 m = m_pullup(m, sizeof(struct udpiphdr) +
639 sizeof(struct netdump_ack));
642 NETDDEBUG("m_pullup failed\n");
646 udp = mtod(m, struct udpiphdr *);
648 if (ntohs(udp->ui_u.uh_dport) != NETDUMP_ACKPORT) {
649 NETDDEBUG("not on the netdump port.\n");
653 /* Netdump processing. */
656 * Packet is meant for us. Extract the ack sequence number and the
657 * port number if necessary.
659 nd_ack = (struct netdump_ack *)(mtod(m, caddr_t) +
660 sizeof(struct udpiphdr));
661 rcv_ackno = ntohl(nd_ack->na_seqno);
662 if (nd_server_port == NETDUMP_PORT)
663 nd_server_port = ntohs(udp->ui_u.uh_sport);
664 if (rcv_ackno >= nd_seqno + NETDUMP_MAX_IN_FLIGHT)
665 printf("%s: ACK %d too far in future!\n", __func__, rcv_ackno);
666 else if (rcv_ackno >= nd_seqno) {
667 /* We're interested in this ack. Record it. */
668 rcvd_acks |= 1 << (rcv_ackno - nd_seqno);
673 * Handler for ARP packets: checks their sanity and then
674 * 1. If the ARP is a request for our IP, respond with our MAC address
675 * 2. If the ARP is a response from our server, record its MAC address
677 * It needs to replicate partially the behaviour of arpintr() and
681 * mb a pointer to an mbuf * containing the packet received
682 * Updates *mb if m_pullup et al change the pointer
683 * Assumes the calling function will take care of freeing the mbuf
686 netdump_handle_arp(struct mbuf **mb)
688 char buf[INET_ADDRSTRLEN];
689 struct in_addr isaddr, itaddr, myaddr;
690 struct ether_addr dst;
698 ifp = m->m_pkthdr.rcvif;
699 if (m->m_len < sizeof(struct arphdr)) {
700 m = m_pullup(m, sizeof(struct arphdr));
703 NETDDEBUG("runt packet: m_pullup failed\n");
708 ah = mtod(m, struct arphdr *);
709 if (ntohs(ah->ar_hrd) != ARPHRD_ETHER) {
710 NETDDEBUG("unknown hardware address 0x%2D)\n",
711 (unsigned char *)&ah->ar_hrd, "");
714 if (ntohs(ah->ar_pro) != ETHERTYPE_IP) {
715 NETDDEBUG("drop ARP for unknown protocol %d\n",
719 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
720 if (m->m_len < req_len) {
721 m = m_pullup(m, req_len);
724 NETDDEBUG("runt packet: m_pullup failed\n");
728 ah = mtod(m, struct arphdr *);
730 op = ntohs(ah->ar_op);
731 memcpy(&isaddr, ar_spa(ah), sizeof(isaddr));
732 memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr));
733 enaddr = (uint8_t *)IF_LLADDR(ifp);
736 if (memcmp(ar_sha(ah), enaddr, ifp->if_addrlen) == 0) {
737 NETDDEBUG("ignoring ARP from myself\n");
741 if (isaddr.s_addr == nd_client.s_addr) {
742 printf("%s: %*D is using my IP address %s!\n", __func__,
743 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
744 inet_ntoa_r(isaddr, buf));
748 if (memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen) == 0) {
749 NETDDEBUG("ignoring ARP from broadcast address\n");
753 if (op == ARPOP_REPLY) {
754 if (isaddr.s_addr != nd_gateway.s_addr &&
755 isaddr.s_addr != nd_server.s_addr) {
756 inet_ntoa_r(isaddr, buf);
758 "ignoring ARP reply from %s (not netdump server)\n",
762 memcpy(nd_gw_mac.octet, ar_sha(ah),
763 min(ah->ar_hln, ETHER_ADDR_LEN));
765 NETDDEBUG("got server MAC address %6D\n", nd_gw_mac.octet, ":");
769 if (op != ARPOP_REQUEST) {
770 NETDDEBUG("ignoring ARP non-request/reply\n");
774 if (itaddr.s_addr != nd_client.s_addr) {
775 NETDDEBUG("ignoring ARP not to our IP\n");
779 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
780 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
781 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
782 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
783 ah->ar_op = htons(ARPOP_REPLY);
784 ah->ar_pro = htons(ETHERTYPE_IP);
785 m->m_flags &= ~(M_BCAST|M_MCAST);
786 m->m_len = arphdr_len(ah);
787 m->m_pkthdr.len = m->m_len;
789 memcpy(dst.octet, ar_tha(ah), ETHER_ADDR_LEN);
790 netdump_ether_output(m, ifp, dst, ETHERTYPE_ARP);
795 * Handler for incoming packets directly from the network adapter
796 * Identifies the packet type (IP or ARP) and passes it along to one of the
797 * helper functions netdump_handle_ip or netdump_handle_arp.
799 * It needs to replicate partially the behaviour of ether_input() and
803 * ifp the interface the packet came from (should be nd_ifp)
804 * m an mbuf containing the packet received
807 netdump_pkt_in(struct ifnet *ifp, struct mbuf *m)
810 struct ether_header *eh;
813 /* Ethernet processing. */
814 if ((m->m_flags & M_PKTHDR) == 0) {
815 NETDDEBUG_IF(ifp, "discard frame without packet header\n");
818 if (m->m_len < ETHER_HDR_LEN) {
820 "discard frame without leading eth header (len %u pktlen %u)\n",
821 m->m_len, m->m_pkthdr.len);
824 if ((m->m_flags & M_HASFCS) != 0) {
825 m_adj(m, -ETHER_CRC_LEN);
826 m->m_flags &= ~M_HASFCS;
828 eh = mtod(m, struct ether_header *);
829 etype = ntohs(eh->ether_type);
830 if ((m->m_flags & M_VLANTAG) != 0 || etype == ETHERTYPE_VLAN) {
831 NETDDEBUG_IF(ifp, "ignoring vlan packets\n");
834 if (if_gethwaddr(ifp, &ifr) != 0) {
835 NETDDEBUG_IF(ifp, "failed to get hw addr for interface\n");
838 if (memcmp(ifr.ifr_addr.sa_data, eh->ether_dhost,
839 ETHER_ADDR_LEN) != 0) {
841 "discard frame with incorrect destination addr\n");
845 /* Done ethernet processing. Strip off the ethernet header. */
846 m_adj(m, ETHER_HDR_LEN);
849 netdump_handle_arp(&m);
852 netdump_handle_ip(&m);
855 NETDDEBUG_IF(ifp, "dropping unknown ethertype %hu\n", etype);
864 * After trapping, instead of assuming that most of the network stack is sane,
865 * we just poll the driver directly for packets.
868 netdump_network_poll(void)
871 MPASS(nd_ifp != NULL);
873 nd_ifp->if_netdump_methods->nd_poll(nd_ifp, 1000);
877 * Dumping specific primitives.
881 * Callback from dumpsys() to dump a chunk of memory.
882 * Copies it out to our static buffer then sends it across the network.
883 * Detects the initial KDH and makes sure it is given a special packet type.
886 * priv Unused. Optional private pointer.
887 * virtual Virtual address (where to read the data from)
888 * physical Unused. Physical memory address.
889 * offset Offset from start of core file
897 netdump_dumper(void *priv __unused, void *virtual,
898 vm_offset_t physical __unused, off_t offset, size_t length)
902 NETDDEBUGV("netdump_dumper(NULL, %p, NULL, %ju, %zu)\n",
903 virtual, (uintmax_t)offset, length);
905 if (virtual == NULL) {
906 if (dump_failed != 0)
907 printf("failed to dump the kernel core\n");
908 else if (netdump_send(NETDUMP_FINISHED, 0, NULL, 0) != 0)
909 printf("failed to close the transaction\n");
911 printf("\nnetdump finished.\n");
915 if (length > sizeof(nd_buf))
918 memmove(nd_buf, virtual, length);
919 error = netdump_send(NETDUMP_VMCORE, offset, nd_buf, length);
928 * Perform any initalization needed prior to transmitting the kernel core.
931 netdump_start(struct dumperinfo *di)
934 char buf[INET_ADDRSTRLEN];
940 /* Check if the dumping is allowed to continue. */
944 if (panicstr == NULL) {
946 "netdump_start: netdump may only be used after a panic\n");
950 MPASS(nd_ifp != NULL);
952 if (nd_server.s_addr == INADDR_ANY) {
953 printf("netdump_start: can't netdump; no server IP given\n");
956 if (nd_client.s_addr == INADDR_ANY) {
957 printf("netdump_start: can't netdump; no client IP given\n");
961 /* We start dumping at offset 0. */
967 * nd_server_port could have switched after the first ack the
968 * first time it gets called. Adjust it accordingly.
970 nd_server_port = NETDUMP_PORT;
972 /* Switch to the netdump mbuf zones. */
975 nd_ifp->if_netdump_methods->nd_event(nd_ifp, NETDUMP_START);
977 /* Make the card use *our* receive callback. */
978 drv_if_input = nd_ifp->if_input;
979 nd_ifp->if_input = netdump_pkt_in;
981 if (nd_gateway.s_addr == INADDR_ANY) {
983 nd_gateway.s_addr = nd_server.s_addr;
986 printf("netdump in progress. searching for server...\n");
987 if (netdump_arp_gw()) {
988 printf("failed to locate server MAC address\n");
993 if (nd_path[0] != '\0') {
995 len = strlen(path) + 1;
1000 if (netdump_send(NETDUMP_HERALD, 0, path, len) != 0) {
1001 printf("failed to contact netdump server\n");
1005 printf("netdumping to %s (%6D)\n", inet_ntoa_r(nd_server, buf),
1006 nd_gw_mac.octet, ":");
1015 netdump_write_headers(struct dumperinfo *di, struct kerneldumpheader *kdh,
1016 void *key, uint32_t keysize)
1020 memcpy(nd_buf, kdh, sizeof(*kdh));
1021 error = netdump_send(NETDUMP_KDH, 0, nd_buf, sizeof(*kdh));
1022 if (error == 0 && keysize > 0) {
1023 if (keysize > sizeof(nd_buf))
1025 memcpy(nd_buf, key, keysize);
1026 error = netdump_send(NETDUMP_EKCD_KEY, 0, nd_buf, keysize);
1032 * Cleanup routine for a possibly failed netdump.
1035 netdump_cleanup(void)
1038 if (restore_gw_addr != 0) {
1039 nd_gateway.s_addr = INADDR_ANY;
1040 restore_gw_addr = 0;
1042 if (drv_if_input != NULL) {
1043 nd_ifp->if_input = drv_if_input;
1044 drv_if_input = NULL;
1046 nd_ifp->if_netdump_methods->nd_event(nd_ifp, NETDUMP_END);
1050 * KLD specific code.
1053 static struct cdevsw netdump_cdevsw = {
1054 .d_version = D_VERSION,
1055 .d_ioctl = netdump_ioctl,
1056 .d_name = "netdump",
1059 static struct cdev *netdump_cdev;
1062 netdump_configure(struct netdump_conf *conf, struct thread *td)
1066 CURVNET_SET(TD_TO_VNET(td));
1067 if (!IS_DEFAULT_VNET(curvnet)) {
1071 IFNET_RLOCK_NOSLEEP();
1072 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1073 if (strcmp(ifp->if_xname, conf->ndc_iface) == 0)
1077 IFNET_RUNLOCK_NOSLEEP();
1082 if ((if_getflags(ifp) & IFF_UP) == 0)
1084 if (!netdump_supported_nic(ifp) || ifp->if_type != IFT_ETHER)
1088 netdump_reinit(ifp);
1089 memcpy(&nd_conf, conf, sizeof(nd_conf));
1095 * Reinitialize the mbuf pool used by drivers while dumping. This is called
1096 * from the generic ioctl handler for SIOCSIFMTU after the driver has
1097 * reconfigured itself.
1100 netdump_reinit(struct ifnet *ifp)
1102 int clsize, nmbuf, ncl, nrxr;
1107 ifp->if_netdump_methods->nd_init(ifp, &nrxr, &ncl, &clsize);
1108 KASSERT(nrxr > 0, ("invalid receive ring count %d", nrxr));
1111 * We need two headers per message on the transmit side. Multiply by
1112 * four to give us some breathing room.
1114 nmbuf = ncl * (4 + nrxr);
1116 netdump_mbuf_reinit(nmbuf, ncl, clsize);
1120 * ioctl(2) handler for the netdump device. This is currently only used to
1121 * register netdump as a dump device.
1125 * cmd, The ioctl to be handled.
1126 * addr, The parameter for the ioctl.
1128 * td, The thread invoking this ioctl.
1131 * 0 on success, and an errno value on failure.
1134 netdump_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr,
1135 int flags __unused, struct thread *td)
1137 struct diocskerneldump_arg *kda;
1138 struct dumperinfo dumper;
1139 struct netdump_conf *conf;
1140 uint8_t *encryptedkey;
1146 case DIOCSKERNELDUMP:
1155 netdump_mbuf_drain();
1159 conf = (struct netdump_conf *)addr;
1165 strlcpy(conf->ndc_iface, nd_ifp->if_xname,
1166 sizeof(conf->ndc_iface));
1167 memcpy(&conf->ndc_server, &nd_server, sizeof(nd_server));
1168 memcpy(&conf->ndc_client, &nd_client, sizeof(nd_client));
1169 memcpy(&conf->ndc_gateway, &nd_gateway, sizeof(nd_gateway));
1172 conf = (struct netdump_conf *)addr;
1173 encryptedkey = NULL;
1174 kda = &conf->ndc_kda;
1176 conf->ndc_iface[sizeof(conf->ndc_iface) - 1] = '\0';
1177 if (kda->kda_enable == 0) {
1179 error = clear_dumper(td);
1182 netdump_mbuf_drain();
1188 error = netdump_configure(conf, td);
1192 if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
1193 if (kda->kda_encryptedkeysize <= 0 ||
1194 kda->kda_encryptedkeysize >
1195 KERNELDUMP_ENCKEY_MAX_SIZE)
1197 encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP,
1199 error = copyin(kda->kda_encryptedkey, encryptedkey,
1200 kda->kda_encryptedkeysize);
1202 free(encryptedkey, M_TEMP);
1207 memset(&dumper, 0, sizeof(dumper));
1208 dumper.dumper_start = netdump_start;
1209 dumper.dumper_hdr = netdump_write_headers;
1210 dumper.dumper = netdump_dumper;
1212 dumper.blocksize = NETDUMP_DATASIZE;
1213 dumper.maxiosize = MAXDUMPPGS * PAGE_SIZE;
1214 dumper.mediaoffset = 0;
1215 dumper.mediasize = 0;
1217 error = set_dumper(&dumper, conf->ndc_iface, td,
1218 kda->kda_compression, kda->kda_encryption,
1219 kda->kda_key, kda->kda_encryptedkeysize,
1221 if (encryptedkey != NULL) {
1222 explicit_bzero(encryptedkey, kda->kda_encryptedkeysize);
1223 free(encryptedkey, M_TEMP);
1227 netdump_mbuf_drain();
1238 * Called upon system init or kld load. Initializes the netdump parameters to
1239 * sane defaults (locates the first available NIC and uses the first IPv4 IP on
1240 * that card as the client IP). Leaves the server IP unconfigured.
1244 * what, The module event type.
1248 * int, An errno value if an error occured, 0 otherwise.
1251 netdump_modevent(module_t mod __unused, int what, void *priv __unused)
1253 struct netdump_conf conf;
1260 error = make_dev_p(MAKEDEV_WAITOK, &netdump_cdev,
1261 &netdump_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "netdump");
1265 if ((arg = kern_getenv("net.dump.iface")) != NULL) {
1266 strlcpy(conf.ndc_iface, arg, sizeof(conf.ndc_iface));
1269 if ((arg = kern_getenv("net.dump.server")) != NULL) {
1270 inet_aton(arg, &conf.ndc_server);
1273 if ((arg = kern_getenv("net.dump.client")) != NULL) {
1274 inet_aton(arg, &conf.ndc_server);
1277 if ((arg = kern_getenv("net.dump.gateway")) != NULL) {
1278 inet_aton(arg, &conf.ndc_server);
1282 /* Ignore errors; we print a message to the console. */
1283 (void)netdump_configure(&conf, curthread);
1287 destroy_dev(netdump_cdev);
1289 printf("netdump: disabling dump device for unload\n");
1290 (void)clear_dumper(curthread);
1301 static moduledata_t netdump_mod = {
1307 MODULE_VERSION(netdump, 1);
1308 DECLARE_MODULE(netdump, netdump_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);