2009-04-12 20:08:37 +04:00
|
|
|
/* $NetBSD: atalk.c,v 1.14 2009/04/12 16:08:37 lukem Exp $ */
|
1997-04-03 08:46:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1988, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1997-04-03 08:46:44 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 09:49:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-04-03 08:46:44 +04:00
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "from @(#)atalk.c 1.1 (Whistle) 6/6/96";
|
|
|
|
#else
|
2009-04-12 20:08:37 +04:00
|
|
|
__RCSID("$NetBSD: atalk.c,v 1.14 2009/04/12 16:08:37 lukem Exp $");
|
1997-04-03 08:46:44 +04:00
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/protosw.h>
|
2008-04-23 19:17:42 +04:00
|
|
|
#include <sys/sysctl.h>
|
1997-04-03 08:46:44 +04:00
|
|
|
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include <netinet/tcp_fsm.h>
|
|
|
|
|
|
|
|
#include <netatalk/at.h>
|
|
|
|
#include <netatalk/ddp_var.h>
|
|
|
|
|
2008-04-24 08:09:27 +04:00
|
|
|
#include <err.h>
|
1997-04-03 08:46:44 +04:00
|
|
|
#include <nlist.h>
|
2005-08-04 23:41:28 +04:00
|
|
|
#include <kvm.h>
|
1997-04-03 08:46:44 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "netstat.h"
|
|
|
|
|
|
|
|
struct ddpcb ddpcb;
|
|
|
|
struct socket sockb;
|
|
|
|
|
|
|
|
static int first = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a summary of connections related to a Network Systems
|
|
|
|
* protocol. For XXX, also give state of connection.
|
|
|
|
* Listening processes (aflag) are suppressed unless the
|
|
|
|
* -a (all) flag is specified.
|
|
|
|
*/
|
|
|
|
|
2009-04-12 20:08:37 +04:00
|
|
|
static const char *
|
|
|
|
at_pr_net(const struct sockaddr_at *sat, int numeric)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
|
|
|
static char mybuf[50];
|
|
|
|
|
|
|
|
if (!numeric) {
|
|
|
|
switch (sat->sat_addr.s_net) {
|
|
|
|
case 0xffff:
|
|
|
|
return "????";
|
|
|
|
case ATADDR_ANYNET:
|
|
|
|
return ("*");
|
|
|
|
}
|
|
|
|
}
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%hu", ntohs(sat->sat_addr.s_net));
|
|
|
|
return (mybuf);
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-12 20:08:37 +04:00
|
|
|
static const char *
|
|
|
|
at_pr_host(const struct sockaddr_at *sat, int numeric)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
|
|
|
static char mybuf[50];
|
|
|
|
|
|
|
|
if (!numeric) {
|
|
|
|
switch (sat->sat_addr.s_node) {
|
|
|
|
case ATADDR_BCAST:
|
|
|
|
return "bcast";
|
|
|
|
case ATADDR_ANYNODE:
|
|
|
|
return ("*");
|
|
|
|
}
|
|
|
|
}
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%d",
|
|
|
|
(unsigned int)sat->sat_addr.s_node);
|
|
|
|
return (mybuf);
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-12 20:08:37 +04:00
|
|
|
static const char *
|
|
|
|
at_pr_port(const struct sockaddr_at *sat)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
|
|
|
static char mybuf[50];
|
|
|
|
|
|
|
|
switch (sat->sat_port) {
|
|
|
|
case ATADDR_ANYPORT:
|
|
|
|
return ("*");
|
|
|
|
case 0xff:
|
|
|
|
return "????";
|
|
|
|
default:
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%d",
|
|
|
|
(unsigned int)sat->sat_port);
|
|
|
|
return (mybuf);
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-12 20:08:37 +04:00
|
|
|
static const char *
|
|
|
|
at_pr_range(const struct sockaddr_at *sat)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
|
|
|
static char mybuf[50];
|
|
|
|
|
|
|
|
if (sat->sat_range.r_netrange.nr_firstnet
|
|
|
|
!= sat->sat_range.r_netrange.nr_lastnet) {
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%d-%d",
|
1997-04-03 08:46:44 +04:00
|
|
|
ntohs(sat->sat_range.r_netrange.nr_firstnet),
|
|
|
|
ntohs(sat->sat_range.r_netrange.nr_lastnet));
|
|
|
|
} else {
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%d",
|
1997-04-03 08:46:44 +04:00
|
|
|
ntohs(sat->sat_range.r_netrange.nr_firstnet));
|
|
|
|
}
|
1998-07-12 07:20:13 +04:00
|
|
|
return (mybuf);
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* what == 0 for addr only == 3
|
|
|
|
* 1 for net
|
|
|
|
* 2 for host
|
|
|
|
* 4 for port
|
|
|
|
* 8 for numeric only
|
|
|
|
*/
|
2009-04-12 20:08:37 +04:00
|
|
|
const char *
|
2008-04-23 19:17:42 +04:00
|
|
|
atalk_print(const struct sockaddr *sa, int what)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
2009-04-12 20:08:37 +04:00
|
|
|
const struct sockaddr_at *sat = (const struct sockaddr_at *) sa;
|
1997-04-03 08:46:44 +04:00
|
|
|
static char mybuf[50];
|
|
|
|
int numeric = (what & 0x08);
|
|
|
|
|
|
|
|
mybuf[0] = 0;
|
|
|
|
switch (what & 0x13) {
|
|
|
|
case 0:
|
|
|
|
mybuf[0] = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%s",
|
1997-04-03 08:46:44 +04:00
|
|
|
at_pr_net(sat, numeric));
|
|
|
|
break;
|
|
|
|
case 2:
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%s",
|
1997-04-03 08:46:44 +04:00
|
|
|
at_pr_host(sat, numeric));
|
|
|
|
break;
|
|
|
|
case 3:
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%s.%s",
|
1997-04-03 08:46:44 +04:00
|
|
|
at_pr_net(sat, numeric),
|
|
|
|
at_pr_host(sat, numeric));
|
|
|
|
break;
|
|
|
|
case 0x10:
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat));
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
|
|
|
if (what & 4) {
|
1998-07-12 07:20:13 +04:00
|
|
|
(void)snprintf(mybuf + strlen(mybuf),
|
1997-04-03 08:46:44 +04:00
|
|
|
sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat));
|
|
|
|
}
|
1998-07-12 07:20:13 +04:00
|
|
|
return (mybuf);
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-12 20:08:37 +04:00
|
|
|
const char *
|
2008-04-23 19:17:42 +04:00
|
|
|
atalk_print2(const struct sockaddr *sa, const struct sockaddr *mask, int what)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
2006-04-06 22:30:31 +04:00
|
|
|
int n, l;
|
1997-04-03 08:46:44 +04:00
|
|
|
static char buf[100];
|
2009-04-12 20:08:37 +04:00
|
|
|
const struct sockaddr_at *sat1, *sat2;
|
1997-04-03 08:46:44 +04:00
|
|
|
struct sockaddr_at thesockaddr;
|
|
|
|
struct sockaddr *sa2;
|
|
|
|
|
2009-04-12 20:08:37 +04:00
|
|
|
sat1 = (const struct sockaddr_at *) sa;
|
|
|
|
sat2 = (const struct sockaddr_at *) mask;
|
1997-04-03 08:46:44 +04:00
|
|
|
sa2 = (struct sockaddr *) & thesockaddr;
|
|
|
|
|
|
|
|
thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net &
|
|
|
|
sat2->sat_addr.s_net;
|
|
|
|
n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8)));
|
2009-04-12 20:08:37 +04:00
|
|
|
if (n >= (int)sizeof(buf))
|
2001-08-19 06:01:24 +04:00
|
|
|
n = sizeof(buf) - 1;
|
|
|
|
else if (n == -1)
|
|
|
|
n = 0; /* What else can be done ? */
|
1997-04-03 08:46:44 +04:00
|
|
|
if (sat2->sat_addr.s_net != 0xFFFF) {
|
|
|
|
thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net |
|
|
|
|
~sat2->sat_addr.s_net;
|
2001-08-19 06:01:24 +04:00
|
|
|
l = snprintf(buf + n, sizeof(buf) - n,
|
1997-04-03 08:46:44 +04:00
|
|
|
"-%s", atalk_print(sa2, 1 | (what & 8)));
|
2009-04-12 20:08:37 +04:00
|
|
|
if (l >= (int)(sizeof(buf) - n))
|
2001-08-19 06:01:24 +04:00
|
|
|
l = sizeof(buf) - n - 1;
|
|
|
|
if (l > 0)
|
|
|
|
n += l;
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
2001-08-19 06:01:24 +04:00
|
|
|
if (what & 2) {
|
|
|
|
l = snprintf(buf + n, sizeof(buf) - n, ".%s",
|
1997-04-03 08:46:44 +04:00
|
|
|
atalk_print(sa, what & (~1)));
|
2009-04-12 20:08:37 +04:00
|
|
|
if (l >= (int)(sizeof(buf) - n))
|
2001-08-19 06:01:24 +04:00
|
|
|
l = sizeof(buf) - n - 1;
|
|
|
|
if (l > 0)
|
|
|
|
n += l;
|
|
|
|
}
|
1997-04-03 08:46:44 +04:00
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-12 20:08:37 +04:00
|
|
|
atalkprotopr(u_long off, const char *name)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
|
|
|
struct ddpcb cb;
|
1997-10-19 09:49:56 +04:00
|
|
|
struct ddpcb *prev, *next;
|
1997-04-03 08:46:44 +04:00
|
|
|
struct ddpcb *initial;
|
2000-10-11 18:46:00 +04:00
|
|
|
int width = 22;
|
1997-04-03 08:46:44 +04:00
|
|
|
if (off == 0)
|
|
|
|
return;
|
1998-07-12 07:20:13 +04:00
|
|
|
if (kread(off, (char *)&initial, sizeof(struct ddpcb *)) < 0)
|
1997-04-03 08:46:44 +04:00
|
|
|
return;
|
|
|
|
ddpcb = cb;
|
1998-07-12 07:20:13 +04:00
|
|
|
prev = (struct ddpcb *)off;
|
1997-04-03 08:46:44 +04:00
|
|
|
for (next = initial; next != NULL; prev = next) {
|
1998-07-12 07:20:13 +04:00
|
|
|
u_long ppcb = (u_long)next;
|
1997-04-03 08:46:44 +04:00
|
|
|
|
1998-07-12 07:20:13 +04:00
|
|
|
if (kread((u_long)next, (char *)&ddpcb, sizeof(ddpcb)) < 0)
|
1997-04-03 08:46:44 +04:00
|
|
|
return;
|
|
|
|
next = ddpcb.ddp_next;
|
|
|
|
#if 0
|
|
|
|
if (!aflag && atalk_nullhost(ddpcb.ddp_lsat)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
1998-07-12 07:20:13 +04:00
|
|
|
if (kread((u_long)ddpcb.ddp_socket,
|
|
|
|
(char *)&sockb, sizeof(sockb)) < 0)
|
1997-04-03 08:46:44 +04:00
|
|
|
return;
|
|
|
|
if (first) {
|
|
|
|
printf("Active ATALK connections");
|
|
|
|
if (aflag)
|
|
|
|
printf(" (including servers)");
|
|
|
|
putchar('\n');
|
2000-10-11 18:46:00 +04:00
|
|
|
if (Aflag) {
|
|
|
|
width = 18;
|
1997-04-03 08:46:44 +04:00
|
|
|
printf("%-8.8s ", "PCB");
|
2000-10-11 18:46:00 +04:00
|
|
|
}
|
|
|
|
printf("%-5.5s %-6.6s %-6.6s %*.*s %*.*s %s\n",
|
1997-04-03 08:46:44 +04:00
|
|
|
"Proto", "Recv-Q", "Send-Q",
|
2000-10-11 18:46:00 +04:00
|
|
|
-width, width, "Local Address",
|
|
|
|
-width, width, "Foreign Address", "(state)");
|
1997-04-03 08:46:44 +04:00
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
if (Aflag)
|
1997-05-22 21:21:26 +04:00
|
|
|
printf("%8lx ", ppcb);
|
1997-04-03 08:46:44 +04:00
|
|
|
printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
|
|
|
|
sockb.so_snd.sb_cc);
|
2000-10-11 18:46:00 +04:00
|
|
|
printf(" %*.*s", -width, width,
|
|
|
|
atalk_print((struct sockaddr *)&ddpcb.ddp_lsat, 7));
|
|
|
|
printf(" %*.*s", -width, width,
|
|
|
|
atalk_print((struct sockaddr *)&ddpcb.ddp_fsat, 7));
|
1997-04-03 08:46:44 +04:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define ANY(x,y,z) \
|
2008-04-23 19:17:42 +04:00
|
|
|
((sflag==1 || (x)) ? printf("\t%llu %s%s%s\n",(unsigned long long)x,y,plural(x),z) : 0)
|
1997-04-03 08:46:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump DDP statistics structure.
|
|
|
|
*/
|
|
|
|
void
|
2009-04-12 20:08:37 +04:00
|
|
|
ddp_stats(u_long off, const char *name)
|
1997-04-03 08:46:44 +04:00
|
|
|
{
|
2008-04-23 19:17:42 +04:00
|
|
|
uint64_t ddpstat[DDP_NSTATS];
|
|
|
|
|
|
|
|
if (use_sysctl) {
|
|
|
|
size_t size = sizeof(ddpstat);
|
|
|
|
|
2008-04-24 08:09:50 +04:00
|
|
|
if (sysctlbyname("net.atalk.ddp.stats", ddpstat, &size,
|
2008-04-23 19:17:42 +04:00
|
|
|
NULL, 0) == -1)
|
|
|
|
return;
|
|
|
|
} else {
|
2008-04-24 08:09:27 +04:00
|
|
|
warnx("%s stats not available via KVM.", name);
|
|
|
|
return;
|
2008-04-23 19:17:42 +04:00
|
|
|
}
|
1997-04-03 08:46:44 +04:00
|
|
|
|
|
|
|
printf("%s:\n", name);
|
2008-04-23 19:17:42 +04:00
|
|
|
|
|
|
|
ANY(ddpstat[DDP_STAT_SHORT], "packet", " with short headers ");
|
|
|
|
ANY(ddpstat[DDP_STAT_LONG], "packet", " with long headers ");
|
|
|
|
ANY(ddpstat[DDP_STAT_NOSUM], "packet", " with no checksum ");
|
|
|
|
ANY(ddpstat[DDP_STAT_TOOSHORT], "packet", " too short ");
|
|
|
|
ANY(ddpstat[DDP_STAT_BADSUM], "packet", " with bad checksum ");
|
|
|
|
ANY(ddpstat[DDP_STAT_TOOSMALL], "packet", " with not enough data ");
|
|
|
|
ANY(ddpstat[DDP_STAT_FORWARD], "packet", " forwarded ");
|
|
|
|
ANY(ddpstat[DDP_STAT_ENCAP], "packet", " encapsulated ");
|
|
|
|
ANY(ddpstat[DDP_STAT_CANTFORWARD], "packet", " rcvd for unreachable dest ");
|
|
|
|
ANY(ddpstat[DDP_STAT_NOSOCKSPACE], "packet", " dropped due to no socket space ");
|
1997-04-03 08:46:44 +04:00
|
|
|
}
|
|
|
|
#undef ANY
|