atalk.c revision 1.18 1 1.18 joerg /* $NetBSD: atalk.c,v 1.18 2020/04/23 00:23:31 joerg 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.18 joerg __RCSID("$NetBSD: atalk.c,v 1.18 2020/04/23 00:23:31 joerg 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.17 kamil #include "prog_ops.h"
65 1.1 christos
66 1.1 christos static int first = 1;
67 1.1 christos
68 1.1 christos /*
69 1.1 christos * Print a summary of connections related to a Network Systems
70 1.1 christos * protocol. For XXX, also give state of connection.
71 1.1 christos * Listening processes (aflag) are suppressed unless the
72 1.1 christos * -a (all) flag is specified.
73 1.1 christos */
74 1.1 christos
75 1.14 lukem static const char *
76 1.14 lukem at_pr_net(const struct sockaddr_at *sat, int numeric)
77 1.1 christos {
78 1.1 christos static char mybuf[50];
79 1.1 christos
80 1.1 christos if (!numeric) {
81 1.1 christos switch (sat->sat_addr.s_net) {
82 1.1 christos case 0xffff:
83 1.1 christos return "????";
84 1.1 christos case ATADDR_ANYNET:
85 1.1 christos return ("*");
86 1.1 christos }
87 1.1 christos }
88 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%hu", ntohs(sat->sat_addr.s_net));
89 1.4 mrg return (mybuf);
90 1.1 christos }
91 1.1 christos
92 1.14 lukem static const char *
93 1.14 lukem at_pr_host(const struct sockaddr_at *sat, int numeric)
94 1.1 christos {
95 1.1 christos static char mybuf[50];
96 1.1 christos
97 1.1 christos if (!numeric) {
98 1.1 christos switch (sat->sat_addr.s_node) {
99 1.1 christos case ATADDR_BCAST:
100 1.1 christos return "bcast";
101 1.1 christos case ATADDR_ANYNODE:
102 1.1 christos return ("*");
103 1.1 christos }
104 1.1 christos }
105 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%d",
106 1.4 mrg (unsigned int)sat->sat_addr.s_node);
107 1.4 mrg return (mybuf);
108 1.1 christos }
109 1.1 christos
110 1.14 lukem static const char *
111 1.14 lukem at_pr_port(const struct sockaddr_at *sat)
112 1.1 christos {
113 1.1 christos static char mybuf[50];
114 1.1 christos
115 1.1 christos switch (sat->sat_port) {
116 1.1 christos case ATADDR_ANYPORT:
117 1.1 christos return ("*");
118 1.1 christos case 0xff:
119 1.1 christos return "????";
120 1.1 christos default:
121 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%d",
122 1.4 mrg (unsigned int)sat->sat_port);
123 1.4 mrg return (mybuf);
124 1.1 christos }
125 1.1 christos }
126 1.1 christos
127 1.14 lukem static const char *
128 1.14 lukem at_pr_range(const struct sockaddr_at *sat)
129 1.1 christos {
130 1.1 christos static char mybuf[50];
131 1.1 christos
132 1.1 christos if (sat->sat_range.r_netrange.nr_firstnet
133 1.1 christos != sat->sat_range.r_netrange.nr_lastnet) {
134 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%d-%d",
135 1.1 christos ntohs(sat->sat_range.r_netrange.nr_firstnet),
136 1.1 christos ntohs(sat->sat_range.r_netrange.nr_lastnet));
137 1.1 christos } else {
138 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%d",
139 1.1 christos ntohs(sat->sat_range.r_netrange.nr_firstnet));
140 1.1 christos }
141 1.4 mrg return (mybuf);
142 1.1 christos }
143 1.1 christos
144 1.1 christos
145 1.1 christos /* what == 0 for addr only == 3
146 1.1 christos * 1 for net
147 1.1 christos * 2 for host
148 1.1 christos * 4 for port
149 1.1 christos * 8 for numeric only
150 1.1 christos */
151 1.14 lukem const char *
152 1.11 thorpej atalk_print(const struct sockaddr *sa, int what)
153 1.1 christos {
154 1.14 lukem const struct sockaddr_at *sat = (const struct sockaddr_at *) sa;
155 1.1 christos static char mybuf[50];
156 1.1 christos int numeric = (what & 0x08);
157 1.1 christos
158 1.1 christos mybuf[0] = 0;
159 1.1 christos switch (what & 0x13) {
160 1.1 christos case 0:
161 1.1 christos mybuf[0] = 0;
162 1.1 christos break;
163 1.1 christos case 1:
164 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%s",
165 1.1 christos at_pr_net(sat, numeric));
166 1.1 christos break;
167 1.1 christos case 2:
168 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%s",
169 1.1 christos at_pr_host(sat, numeric));
170 1.1 christos break;
171 1.1 christos case 3:
172 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%s.%s",
173 1.1 christos at_pr_net(sat, numeric),
174 1.1 christos at_pr_host(sat, numeric));
175 1.1 christos break;
176 1.1 christos case 0x10:
177 1.4 mrg (void)snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat));
178 1.1 christos }
179 1.1 christos if (what & 4) {
180 1.4 mrg (void)snprintf(mybuf + strlen(mybuf),
181 1.1 christos sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat));
182 1.1 christos }
183 1.4 mrg return (mybuf);
184 1.1 christos }
185 1.1 christos
186 1.14 lukem const char *
187 1.11 thorpej atalk_print2(const struct sockaddr *sa, const struct sockaddr *mask, int what)
188 1.1 christos {
189 1.10 rpaulo int n, l;
190 1.1 christos static char buf[100];
191 1.14 lukem const struct sockaddr_at *sat1, *sat2;
192 1.1 christos struct sockaddr_at thesockaddr;
193 1.1 christos struct sockaddr *sa2;
194 1.1 christos
195 1.14 lukem sat1 = (const struct sockaddr_at *) sa;
196 1.14 lukem sat2 = (const struct sockaddr_at *) mask;
197 1.1 christos sa2 = (struct sockaddr *) & thesockaddr;
198 1.1 christos
199 1.1 christos thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net &
200 1.1 christos sat2->sat_addr.s_net;
201 1.1 christos n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8)));
202 1.14 lukem if (n >= (int)sizeof(buf))
203 1.7 itojun n = sizeof(buf) - 1;
204 1.7 itojun else if (n == -1)
205 1.7 itojun n = 0; /* What else can be done ? */
206 1.1 christos if (sat2->sat_addr.s_net != 0xFFFF) {
207 1.1 christos thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net |
208 1.1 christos ~sat2->sat_addr.s_net;
209 1.7 itojun l = snprintf(buf + n, sizeof(buf) - n,
210 1.1 christos "-%s", atalk_print(sa2, 1 | (what & 8)));
211 1.14 lukem if (l >= (int)(sizeof(buf) - n))
212 1.7 itojun l = sizeof(buf) - n - 1;
213 1.7 itojun if (l > 0)
214 1.7 itojun n += l;
215 1.1 christos }
216 1.7 itojun if (what & 2) {
217 1.7 itojun l = snprintf(buf + n, sizeof(buf) - n, ".%s",
218 1.1 christos atalk_print(sa, what & (~1)));
219 1.14 lukem if (l >= (int)(sizeof(buf) - n))
220 1.7 itojun l = sizeof(buf) - n - 1;
221 1.7 itojun if (l > 0)
222 1.7 itojun n += l;
223 1.7 itojun }
224 1.1 christos return (buf);
225 1.1 christos }
226 1.1 christos
227 1.1 christos void
228 1.14 lukem atalkprotopr(u_long off, const char *name)
229 1.1 christos {
230 1.18 joerg struct ddpcb ddpcb;
231 1.18 joerg struct socket sockb;
232 1.15 christos struct ddpcb *next;
233 1.15 christos struct ddpcb *initial;
234 1.6 is int width = 22;
235 1.1 christos if (off == 0)
236 1.1 christos return;
237 1.4 mrg if (kread(off, (char *)&initial, sizeof(struct ddpcb *)) < 0)
238 1.1 christos return;
239 1.15 christos for (next = initial; next != NULL;) {
240 1.4 mrg u_long ppcb = (u_long)next;
241 1.1 christos
242 1.4 mrg if (kread((u_long)next, (char *)&ddpcb, sizeof(ddpcb)) < 0)
243 1.1 christos return;
244 1.1 christos next = ddpcb.ddp_next;
245 1.1 christos #if 0
246 1.1 christos if (!aflag && atalk_nullhost(ddpcb.ddp_lsat)) {
247 1.1 christos continue;
248 1.1 christos }
249 1.1 christos #endif
250 1.4 mrg if (kread((u_long)ddpcb.ddp_socket,
251 1.4 mrg (char *)&sockb, sizeof(sockb)) < 0)
252 1.1 christos return;
253 1.1 christos if (first) {
254 1.1 christos printf("Active ATALK connections");
255 1.1 christos if (aflag)
256 1.1 christos printf(" (including servers)");
257 1.1 christos putchar('\n');
258 1.6 is if (Aflag) {
259 1.6 is width = 18;
260 1.1 christos printf("%-8.8s ", "PCB");
261 1.6 is }
262 1.6 is printf("%-5.5s %-6.6s %-6.6s %*.*s %*.*s %s\n",
263 1.1 christos "Proto", "Recv-Q", "Send-Q",
264 1.6 is -width, width, "Local Address",
265 1.6 is -width, width, "Foreign Address", "(state)");
266 1.1 christos first = 0;
267 1.1 christos }
268 1.1 christos if (Aflag)
269 1.2 christos printf("%8lx ", ppcb);
270 1.1 christos printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
271 1.1 christos sockb.so_snd.sb_cc);
272 1.6 is printf(" %*.*s", -width, width,
273 1.6 is atalk_print((struct sockaddr *)&ddpcb.ddp_lsat, 7));
274 1.6 is printf(" %*.*s", -width, width,
275 1.6 is atalk_print((struct sockaddr *)&ddpcb.ddp_fsat, 7));
276 1.1 christos putchar('\n');
277 1.1 christos }
278 1.1 christos }
279 1.1 christos #define ANY(x,y,z) \
280 1.11 thorpej ((sflag==1 || (x)) ? printf("\t%llu %s%s%s\n",(unsigned long long)x,y,plural(x),z) : 0)
281 1.1 christos
282 1.1 christos /*
283 1.1 christos * Dump DDP statistics structure.
284 1.1 christos */
285 1.1 christos void
286 1.14 lukem ddp_stats(u_long off, const char *name)
287 1.1 christos {
288 1.11 thorpej uint64_t ddpstat[DDP_NSTATS];
289 1.11 thorpej
290 1.11 thorpej if (use_sysctl) {
291 1.11 thorpej size_t size = sizeof(ddpstat);
292 1.11 thorpej
293 1.17 kamil if (prog_sysctlbyname("net.atalk.ddp.stats", ddpstat, &size,
294 1.11 thorpej NULL, 0) == -1)
295 1.11 thorpej return;
296 1.11 thorpej } else {
297 1.12 thorpej warnx("%s stats not available via KVM.", name);
298 1.12 thorpej return;
299 1.11 thorpej }
300 1.1 christos
301 1.1 christos printf("%s:\n", name);
302 1.11 thorpej
303 1.11 thorpej ANY(ddpstat[DDP_STAT_SHORT], "packet", " with short headers ");
304 1.11 thorpej ANY(ddpstat[DDP_STAT_LONG], "packet", " with long headers ");
305 1.11 thorpej ANY(ddpstat[DDP_STAT_NOSUM], "packet", " with no checksum ");
306 1.11 thorpej ANY(ddpstat[DDP_STAT_TOOSHORT], "packet", " too short ");
307 1.11 thorpej ANY(ddpstat[DDP_STAT_BADSUM], "packet", " with bad checksum ");
308 1.11 thorpej ANY(ddpstat[DDP_STAT_TOOSMALL], "packet", " with not enough data ");
309 1.11 thorpej ANY(ddpstat[DDP_STAT_FORWARD], "packet", " forwarded ");
310 1.11 thorpej ANY(ddpstat[DDP_STAT_ENCAP], "packet", " encapsulated ");
311 1.11 thorpej ANY(ddpstat[DDP_STAT_CANTFORWARD], "packet", " rcvd for unreachable dest ");
312 1.11 thorpej ANY(ddpstat[DDP_STAT_NOSOCKSPACE], "packet", " dropped due to no socket space ");
313 1.1 christos }
314 1.1 christos #undef ANY
315