Home | History | Annotate | Line # | Download | only in netstat
      1 /*	$NetBSD: atalk.c,v 1.21 2022/09/02 06:25:43 msaitoh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from @(#)atalk.c	1.1 (Whistle) 6/6/96";
     36 #else
     37 __RCSID("$NetBSD: atalk.c,v 1.21 2022/09/02 06:25:43 msaitoh Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/queue.h>
     43 #include <sys/socket.h>
     44 #include <sys/socketvar.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/protosw.h>
     47 #include <sys/sysctl.h>
     48 
     49 #include <net/route.h>
     50 #include <net/if.h>
     51 
     52 #include <netinet/tcp_fsm.h>
     53 
     54 #include <netatalk/at.h>
     55 #include <netatalk/ddp_var.h>
     56 
     57 #include <err.h>
     58 #include <nlist.h>
     59 #include <kvm.h>
     60 #include <errno.h>
     61 #include <stdio.h>
     62 #include <string.h>
     63 #include "netstat.h"
     64 #include "prog_ops.h"
     65 
     66 static int first = 1;
     67 
     68 /*
     69  * Print a summary of connections related to a Network Systems
     70  * protocol.  For XXX, also give state of connection.
     71  * Listening processes (aflag) are suppressed unless the
     72  * -a (all) flag is specified.
     73  */
     74 
     75 static const char *
     76 at_pr_net(const struct sockaddr_at *sat, int numeric)
     77 {
     78 	static char mybuf[50];
     79 
     80 	if (!numeric) {
     81 		switch (sat->sat_addr.s_net) {
     82 		case 0xffff:
     83 			return "????";
     84 		case ATADDR_ANYNET:
     85 			return "*";
     86 		}
     87 	}
     88 	(void)snprintf(mybuf, sizeof(mybuf), "%hu",
     89 	    ntohs(sat->sat_addr.s_net));
     90 	return mybuf;
     91 }
     92 
     93 static const char *
     94 at_pr_host(const struct sockaddr_at *sat, int numeric)
     95 {
     96 	static char mybuf[50];
     97 
     98 	if (!numeric) {
     99 		switch (sat->sat_addr.s_node) {
    100 		case ATADDR_BCAST:
    101 			return "bcast";
    102 		case ATADDR_ANYNODE:
    103 			return "*";
    104 		}
    105 	}
    106 	(void)snprintf(mybuf, sizeof(mybuf), "%d",
    107 	    (unsigned int)sat->sat_addr.s_node);
    108 	return mybuf;
    109 }
    110 
    111 static const char *
    112 at_pr_port(const struct sockaddr_at *sat)
    113 {
    114 	static char mybuf[50];
    115 
    116 	switch (sat->sat_port) {
    117 	case ATADDR_ANYPORT:
    118 		return "*";
    119 	case 0xff:
    120 		return "????";
    121 	default:
    122 		(void)snprintf(mybuf, sizeof(mybuf), "%d",
    123 		    (unsigned int)sat->sat_port);
    124 		return mybuf;
    125 	}
    126 }
    127 
    128 static const char *
    129 at_pr_range(const struct sockaddr_at *sat)
    130 {
    131 	static char mybuf[50];
    132 
    133 	if (sat->sat_range.r_netrange.nr_firstnet
    134 	    != sat->sat_range.r_netrange.nr_lastnet) {
    135 		(void)snprintf(mybuf, sizeof(mybuf), "%d-%d",
    136 			ntohs(sat->sat_range.r_netrange.nr_firstnet),
    137 			ntohs(sat->sat_range.r_netrange.nr_lastnet));
    138 	} else {
    139 		(void)snprintf(mybuf, sizeof(mybuf), "%d",
    140 			ntohs(sat->sat_range.r_netrange.nr_firstnet));
    141 	}
    142 	return mybuf;
    143 }
    144 
    145 
    146 /* what == 0 for addr only == 3
    147  *	1 for net
    148  *	2 for host
    149  *	4 for port
    150  *	8 for numeric only
    151  */
    152 const char *
    153 atalk_print(const struct sockaddr *sa, int what)
    154 {
    155 	const struct sockaddr_at *sat = (const struct sockaddr_at *) sa;
    156 	static char mybuf[50];
    157 	int numeric = (what & 0x08);
    158 
    159 	mybuf[0] = 0;
    160 	switch (what & 0x13) {
    161 	case 0:
    162 		mybuf[0] = 0;
    163 		break;
    164 	case 1:
    165 		(void)snprintf(mybuf, sizeof(mybuf), "%s",
    166 		    at_pr_net(sat, numeric));
    167 		break;
    168 	case 2:
    169 		(void)snprintf(mybuf, sizeof(mybuf), "%s",
    170 		    at_pr_host(sat, numeric));
    171 		break;
    172 	case 3:
    173 		(void)snprintf(mybuf, sizeof(mybuf), "%s.%s",
    174 			at_pr_net(sat, numeric),
    175 			at_pr_host(sat, numeric));
    176 		break;
    177 	case 0x10:
    178 		(void)snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat));
    179 	}
    180 	if (what & 4) {
    181 		(void)snprintf(mybuf + strlen(mybuf),
    182 		    sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat));
    183 	}
    184 	return mybuf;
    185 }
    186 
    187 const char *
    188 atalk_print2(const struct sockaddr *sa, const struct sockaddr *mask, int what)
    189 {
    190 	int		n, l;
    191 	static char     buf[100];
    192 	const struct sockaddr_at *sat1, *sat2;
    193 	struct sockaddr_at thesockaddr;
    194 	struct sockaddr *sa2;
    195 
    196 	sat1 = (const struct sockaddr_at *) sa;
    197 	sat2 = (const struct sockaddr_at *) mask;
    198 	sa2 = (struct sockaddr *) & thesockaddr;
    199 
    200 	thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net &
    201 	    sat2->sat_addr.s_net;
    202 	n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8)));
    203 	if (n >= (int)sizeof(buf))
    204 		n = sizeof(buf) - 1;
    205 	else if (n == -1)
    206 		n = 0;	/* What else can be done ? */
    207 	if (sat2->sat_addr.s_net != 0xFFFF) {
    208 		thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net |
    209 		    ~sat2->sat_addr.s_net;
    210 		l = snprintf(buf + n, sizeof(buf) - n,
    211 		    "-%s", atalk_print(sa2, 1 | (what & 8)));
    212 		if (l >= (int)(sizeof(buf) - n))
    213 			l = sizeof(buf) - n - 1;
    214 		if (l > 0)
    215 			n += l;
    216 	}
    217 	if (what & 2) {
    218 		l = snprintf(buf + n, sizeof(buf) - n, ".%s",
    219 		    atalk_print(sa, what & (~1)));
    220 		if (l >= (int)(sizeof(buf) - n))
    221 			l = sizeof(buf) - n - 1;
    222 		if (l > 0)
    223 			n += l;
    224 	}
    225 	return buf;
    226 }
    227 
    228 void
    229 atalkprotopr(u_long off, const char *name)
    230 {
    231 	struct ddpcb ddpcb;
    232 	struct socket sockb;
    233 	struct ddpcb *next;
    234 	struct ddpcb *initial;
    235 	int width = 22;
    236 	if (off == 0)
    237 		return;
    238 	if (kread(off, (char *)&initial, sizeof(struct ddpcb *)) < 0)
    239 		return;
    240 	for (next = initial; next != NULL;) {
    241 		u_long	ppcb = (u_long)next;
    242 
    243 		if (kread((u_long)next, (char *)&ddpcb, sizeof(ddpcb)) < 0)
    244 			return;
    245 		next = ddpcb.ddp_next;
    246 #if 0
    247 		if (!aflag && atalk_nullhost(ddpcb.ddp_lsat))
    248 			continue;
    249 #endif
    250 		if (kread((u_long)ddpcb.ddp_socket,
    251 			  (char *)&sockb, sizeof(sockb)) < 0)
    252 			return;
    253 		if (first) {
    254 			printf("Active ATALK connections");
    255 			if (aflag)
    256 				printf(" (including servers)");
    257 			putchar('\n');
    258 			if (Aflag) {
    259 				width = 18;
    260 				printf("%-8.8s ", "PCB");
    261 			}
    262 			printf("%-5.5s %-6.6s %-6.6s  %*.*s %*.*s %s\n",
    263 			       "Proto", "Recv-Q", "Send-Q",
    264 			       -width, width, "Local Address",
    265 			       -width, width, "Foreign Address", "(state)");
    266 			first = 0;
    267 		}
    268 		if (Aflag)
    269 			printf("%8lx ", ppcb);
    270 		printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
    271 		       sockb.so_snd.sb_cc);
    272 		printf(" %*.*s", -width, width,
    273 		       atalk_print((struct sockaddr *)&ddpcb.ddp_lsat, 7));
    274 		printf(" %*.*s", -width, width,
    275 		       atalk_print((struct sockaddr *)&ddpcb.ddp_fsat, 7));
    276 		putchar('\n');
    277 	}
    278 }
    279 #define ANY(x,y,z)							      \
    280 	((sflag==1 || (x)) ?						      \
    281 	    printf("\t%llu %s%s%s\n",(unsigned long long)x,y,plural(x),z) : 0)
    282 
    283 /*
    284  * Dump DDP statistics structure.
    285  */
    286 void
    287 ddp_stats(u_long off, const char *name)
    288 {
    289 	uint64_t ddpstat[DDP_NSTATS];
    290 
    291 	if (use_sysctl) {
    292 		size_t size = sizeof(ddpstat);
    293 
    294 		if (prog_sysctlbyname("net.atalk.ddp.stats", ddpstat, &size,
    295 				 NULL, 0) == -1 && errno != ENOMEM)
    296 			return;
    297 	} else {
    298 		warnx("%s stats not available via KVM.", name);
    299 		return;
    300 	}
    301 
    302 	printf("%s:\n", name);
    303 
    304 	ANY(ddpstat[DDP_STAT_SHORT], "packet", " with short headers ");
    305 	ANY(ddpstat[DDP_STAT_LONG], "packet", " with long headers ");
    306 	ANY(ddpstat[DDP_STAT_NOSUM], "packet", " with no checksum ");
    307 	ANY(ddpstat[DDP_STAT_TOOSHORT], "packet", " too short ");
    308 	ANY(ddpstat[DDP_STAT_BADSUM], "packet", " with bad checksum ");
    309 	ANY(ddpstat[DDP_STAT_TOOSMALL], "packet", " with not enough data ");
    310 	ANY(ddpstat[DDP_STAT_FORWARD], "packet", " forwarded ");
    311 	ANY(ddpstat[DDP_STAT_ENCAP], "packet", " encapsulated ");
    312 	ANY(ddpstat[DDP_STAT_CANTFORWARD], "packet",
    313 	    " rcvd for unreachable dest ");
    314 	ANY(ddpstat[DDP_STAT_NOSOCKSPACE], "packet",
    315 	    " dropped due to no socket space ");
    316 }
    317 #undef ANY
    318