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