1 /* $NetBSD: atalk.c,v 1.17 2019/08/18 04:14:40 kamil 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.17 2019/08/18 04:14:40 kamil 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 struct ddpcb ddpcb; 67 struct socket sockb; 68 69 static int first = 1; 70 71 /* 72 * Print a summary of connections related to a Network Systems 73 * protocol. For XXX, also give state of connection. 74 * Listening processes (aflag) are suppressed unless the 75 * -a (all) flag is specified. 76 */ 77 78 static const char * 79 at_pr_net(const struct sockaddr_at *sat, int numeric) 80 { 81 static char mybuf[50]; 82 83 if (!numeric) { 84 switch (sat->sat_addr.s_net) { 85 case 0xffff: 86 return "????"; 87 case ATADDR_ANYNET: 88 return ("*"); 89 } 90 } 91 (void)snprintf(mybuf, sizeof(mybuf), "%hu", ntohs(sat->sat_addr.s_net)); 92 return (mybuf); 93 } 94 95 static const char * 96 at_pr_host(const struct sockaddr_at *sat, int numeric) 97 { 98 static char mybuf[50]; 99 100 if (!numeric) { 101 switch (sat->sat_addr.s_node) { 102 case ATADDR_BCAST: 103 return "bcast"; 104 case ATADDR_ANYNODE: 105 return ("*"); 106 } 107 } 108 (void)snprintf(mybuf, sizeof(mybuf), "%d", 109 (unsigned int)sat->sat_addr.s_node); 110 return (mybuf); 111 } 112 113 static const char * 114 at_pr_port(const struct sockaddr_at *sat) 115 { 116 static char mybuf[50]; 117 118 switch (sat->sat_port) { 119 case ATADDR_ANYPORT: 120 return ("*"); 121 case 0xff: 122 return "????"; 123 default: 124 (void)snprintf(mybuf, sizeof(mybuf), "%d", 125 (unsigned int)sat->sat_port); 126 return (mybuf); 127 } 128 } 129 130 static const char * 131 at_pr_range(const struct sockaddr_at *sat) 132 { 133 static char mybuf[50]; 134 135 if (sat->sat_range.r_netrange.nr_firstnet 136 != sat->sat_range.r_netrange.nr_lastnet) { 137 (void)snprintf(mybuf, sizeof(mybuf), "%d-%d", 138 ntohs(sat->sat_range.r_netrange.nr_firstnet), 139 ntohs(sat->sat_range.r_netrange.nr_lastnet)); 140 } else { 141 (void)snprintf(mybuf, sizeof(mybuf), "%d", 142 ntohs(sat->sat_range.r_netrange.nr_firstnet)); 143 } 144 return (mybuf); 145 } 146 147 148 /* what == 0 for addr only == 3 149 * 1 for net 150 * 2 for host 151 * 4 for port 152 * 8 for numeric only 153 */ 154 const char * 155 atalk_print(const struct sockaddr *sa, int what) 156 { 157 const struct sockaddr_at *sat = (const struct sockaddr_at *) sa; 158 static char mybuf[50]; 159 int numeric = (what & 0x08); 160 161 mybuf[0] = 0; 162 switch (what & 0x13) { 163 case 0: 164 mybuf[0] = 0; 165 break; 166 case 1: 167 (void)snprintf(mybuf, sizeof(mybuf), "%s", 168 at_pr_net(sat, numeric)); 169 break; 170 case 2: 171 (void)snprintf(mybuf, sizeof(mybuf), "%s", 172 at_pr_host(sat, numeric)); 173 break; 174 case 3: 175 (void)snprintf(mybuf, sizeof(mybuf), "%s.%s", 176 at_pr_net(sat, numeric), 177 at_pr_host(sat, numeric)); 178 break; 179 case 0x10: 180 (void)snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat)); 181 } 182 if (what & 4) { 183 (void)snprintf(mybuf + strlen(mybuf), 184 sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat)); 185 } 186 return (mybuf); 187 } 188 189 const char * 190 atalk_print2(const struct sockaddr *sa, const struct sockaddr *mask, int what) 191 { 192 int n, l; 193 static char buf[100]; 194 const struct sockaddr_at *sat1, *sat2; 195 struct sockaddr_at thesockaddr; 196 struct sockaddr *sa2; 197 198 sat1 = (const struct sockaddr_at *) sa; 199 sat2 = (const struct sockaddr_at *) mask; 200 sa2 = (struct sockaddr *) & thesockaddr; 201 202 thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net & 203 sat2->sat_addr.s_net; 204 n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8))); 205 if (n >= (int)sizeof(buf)) 206 n = sizeof(buf) - 1; 207 else if (n == -1) 208 n = 0; /* What else can be done ? */ 209 if (sat2->sat_addr.s_net != 0xFFFF) { 210 thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net | 211 ~sat2->sat_addr.s_net; 212 l = snprintf(buf + n, sizeof(buf) - n, 213 "-%s", atalk_print(sa2, 1 | (what & 8))); 214 if (l >= (int)(sizeof(buf) - n)) 215 l = sizeof(buf) - n - 1; 216 if (l > 0) 217 n += l; 218 } 219 if (what & 2) { 220 l = snprintf(buf + n, sizeof(buf) - n, ".%s", 221 atalk_print(sa, what & (~1))); 222 if (l >= (int)(sizeof(buf) - n)) 223 l = sizeof(buf) - n - 1; 224 if (l > 0) 225 n += l; 226 } 227 return (buf); 228 } 229 230 void 231 atalkprotopr(u_long off, const char *name) 232 { 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 } 250 #endif 251 if (kread((u_long)ddpcb.ddp_socket, 252 (char *)&sockb, sizeof(sockb)) < 0) 253 return; 254 if (first) { 255 printf("Active ATALK connections"); 256 if (aflag) 257 printf(" (including servers)"); 258 putchar('\n'); 259 if (Aflag) { 260 width = 18; 261 printf("%-8.8s ", "PCB"); 262 } 263 printf("%-5.5s %-6.6s %-6.6s %*.*s %*.*s %s\n", 264 "Proto", "Recv-Q", "Send-Q", 265 -width, width, "Local Address", 266 -width, width, "Foreign Address", "(state)"); 267 first = 0; 268 } 269 if (Aflag) 270 printf("%8lx ", ppcb); 271 printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc, 272 sockb.so_snd.sb_cc); 273 printf(" %*.*s", -width, width, 274 atalk_print((struct sockaddr *)&ddpcb.ddp_lsat, 7)); 275 printf(" %*.*s", -width, width, 276 atalk_print((struct sockaddr *)&ddpcb.ddp_fsat, 7)); 277 putchar('\n'); 278 } 279 } 280 #define ANY(x,y,z) \ 281 ((sflag==1 || (x)) ? 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) 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", " rcvd for unreachable dest "); 313 ANY(ddpstat[DDP_STAT_NOSOCKSPACE], "packet", " dropped due to no socket space "); 314 } 315 #undef ANY 316