netcmds.c revision 1.23 1 1.23 ozaki /* $NetBSD: netcmds.c,v 1.23 2022/10/28 05:24:08 ozaki-r Exp $ */
2 1.2 jtc
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1980, 1992, 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.19 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 jtc * may be used to endorse or promote products derived from this software
17 1.1 jtc * without specific prior written permission.
18 1.1 jtc *
19 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 jtc * SUCH DAMAGE.
30 1.1 jtc */
31 1.1 jtc
32 1.5 mrg #include <sys/cdefs.h>
33 1.1 jtc #ifndef lint
34 1.2 jtc #if 0
35 1.1 jtc static char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93";
36 1.2 jtc #endif
37 1.23 ozaki __RCSID("$NetBSD: netcmds.c,v 1.23 2022/10/28 05:24:08 ozaki-r Exp $");
38 1.1 jtc #endif /* not lint */
39 1.1 jtc
40 1.1 jtc /*
41 1.1 jtc * Common network command support routines.
42 1.1 jtc */
43 1.1 jtc #include <sys/param.h>
44 1.1 jtc #include <sys/mbuf.h>
45 1.1 jtc #include <sys/protosw.h>
46 1.1 jtc
47 1.1 jtc #include <net/route.h>
48 1.1 jtc #include <netinet/in.h>
49 1.1 jtc #include <netinet/in_systm.h>
50 1.1 jtc #include <netinet/ip.h>
51 1.1 jtc #include <netinet/in_pcb.h>
52 1.14 itojun #ifdef INET6
53 1.14 itojun #include <netinet/ip6.h>
54 1.14 itojun #include <netinet6/in6_pcb.h>
55 1.14 itojun #endif
56 1.1 jtc
57 1.3 cgd #include <arpa/inet.h>
58 1.3 cgd
59 1.17 simonb #include <ctype.h>
60 1.1 jtc #include <netdb.h>
61 1.1 jtc #include <stdlib.h>
62 1.1 jtc #include <string.h>
63 1.17 simonb
64 1.1 jtc #include "systat.h"
65 1.1 jtc #include "extern.h"
66 1.1 jtc
67 1.1 jtc #define streq(a,b) (strcmp(a,b)==0)
68 1.1 jtc
69 1.1 jtc static struct hitem {
70 1.14 itojun struct sockaddr_storage addr;
71 1.1 jtc int onoff;
72 1.12 itojun } *hosts = NULL;
73 1.1 jtc
74 1.1 jtc int nports, nhosts, protos;
75 1.1 jtc
76 1.16 ad static void changeitems(char *, int);
77 1.21 dsl static void selectproto(const char *);
78 1.16 ad static void showprotos(void);
79 1.16 ad static int selectport(long, int);
80 1.16 ad static void showports(void);
81 1.16 ad static int addrcmp(struct sockaddr *, struct sockaddr *);
82 1.16 ad static int selecthost(struct sockaddr *, int);
83 1.16 ad static void showhosts(void);
84 1.1 jtc
85 1.9 jwise /* please note: there are also some netstat commands in netstat.c */
86 1.9 jwise
87 1.9 jwise void
88 1.16 ad netstat_display(char *args)
89 1.1 jtc {
90 1.9 jwise changeitems(args, 1);
91 1.9 jwise }
92 1.9 jwise
93 1.9 jwise void
94 1.16 ad netstat_ignore(char *args)
95 1.9 jwise {
96 1.9 jwise changeitems(args, 0);
97 1.9 jwise }
98 1.1 jtc
99 1.9 jwise void
100 1.16 ad netstat_reset(char *args)
101 1.9 jwise {
102 1.9 jwise selectproto(0);
103 1.9 jwise selecthost(0, 0);
104 1.9 jwise selectport(-1, 0);
105 1.9 jwise }
106 1.9 jwise
107 1.9 jwise void
108 1.16 ad netstat_show(char *args)
109 1.9 jwise {
110 1.9 jwise move(CMDLINE, 0); clrtoeol();
111 1.13 itojun if (!args || *args == '\0') {
112 1.9 jwise showprotos();
113 1.9 jwise showhosts();
114 1.9 jwise showports();
115 1.9 jwise return;
116 1.1 jtc }
117 1.10 jwise if (strstr(args, "protos") == args)
118 1.9 jwise showprotos();
119 1.10 jwise else if (strstr(args, "hosts") == args)
120 1.9 jwise showhosts();
121 1.10 jwise else if (strstr(args, "ports") == args)
122 1.9 jwise showports();
123 1.9 jwise else
124 1.9 jwise addstr("show what?");
125 1.9 jwise }
126 1.9 jwise
127 1.9 jwise void
128 1.16 ad netstat_tcp(char *args)
129 1.9 jwise {
130 1.9 jwise selectproto("tcp");
131 1.1 jtc }
132 1.1 jtc
133 1.9 jwise void
134 1.16 ad netstat_udp(char *args)
135 1.9 jwise {
136 1.9 jwise selectproto("udp");
137 1.9 jwise }
138 1.1 jtc
139 1.1 jtc static void
140 1.16 ad changeitems(char *args, int onoff)
141 1.1 jtc {
142 1.6 lukem char *cp;
143 1.1 jtc struct servent *sp;
144 1.14 itojun struct addrinfo hints, *res, *res0;
145 1.1 jtc
146 1.6 lukem cp = strchr(args, '\n');
147 1.1 jtc if (cp)
148 1.1 jtc *cp = '\0';
149 1.1 jtc for (;;args = cp) {
150 1.20 dsl for (cp = args; *cp && isspace((unsigned char)*cp); cp++)
151 1.1 jtc ;
152 1.1 jtc args = cp;
153 1.20 dsl for (; *cp && !isspace((unsigned char)*cp); cp++)
154 1.1 jtc ;
155 1.1 jtc if (*cp)
156 1.1 jtc *cp++ = '\0';
157 1.1 jtc if (cp - args == 0)
158 1.1 jtc break;
159 1.1 jtc sp = getservbyname(args,
160 1.1 jtc protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
161 1.1 jtc if (sp) {
162 1.1 jtc selectport(sp->s_port, onoff);
163 1.1 jtc continue;
164 1.1 jtc }
165 1.14 itojun
166 1.14 itojun memset(&hints, 0, sizeof(hints));
167 1.14 itojun hints.ai_family = PF_UNSPEC;
168 1.14 itojun hints.ai_socktype = SOCK_DGRAM;
169 1.14 itojun if (getaddrinfo(args, "0", &hints, &res0) != 0) {
170 1.14 itojun error("%s: unknown host or port", args);
171 1.14 itojun continue;
172 1.4 mycroft }
173 1.14 itojun for (res = res0; res; res = res->ai_next)
174 1.14 itojun selecthost(res->ai_addr, onoff);
175 1.14 itojun freeaddrinfo(res0);
176 1.1 jtc }
177 1.1 jtc }
178 1.1 jtc
179 1.5 mrg static void
180 1.21 dsl selectproto(const char *proto)
181 1.1 jtc {
182 1.1 jtc
183 1.1 jtc if (proto == 0 || streq(proto, "all"))
184 1.5 mrg protos = TCP|UDP;
185 1.1 jtc else if (streq(proto, "tcp"))
186 1.5 mrg protos = TCP;
187 1.1 jtc else if (streq(proto, "udp"))
188 1.5 mrg protos = UDP;
189 1.1 jtc }
190 1.1 jtc
191 1.1 jtc static void
192 1.16 ad showprotos(void)
193 1.1 jtc {
194 1.1 jtc
195 1.8 mrg if ((protos & TCP) == 0)
196 1.1 jtc addch('!');
197 1.1 jtc addstr("tcp ");
198 1.8 mrg if ((protos & UDP) == 0)
199 1.1 jtc addch('!');
200 1.1 jtc addstr("udp ");
201 1.1 jtc }
202 1.1 jtc
203 1.1 jtc static struct pitem {
204 1.1 jtc long port;
205 1.1 jtc int onoff;
206 1.11 tron } *ports = NULL;
207 1.1 jtc
208 1.1 jtc static int
209 1.16 ad selectport(long port, int onoff)
210 1.1 jtc {
211 1.6 lukem struct pitem *p;
212 1.1 jtc
213 1.1 jtc if (port == -1) {
214 1.11 tron if (ports == NULL)
215 1.1 jtc return (0);
216 1.11 tron free(ports);
217 1.11 tron ports = NULL;
218 1.1 jtc nports = 0;
219 1.1 jtc return (1);
220 1.1 jtc }
221 1.1 jtc for (p = ports; p < ports+nports; p++)
222 1.1 jtc if (p->port == port) {
223 1.1 jtc p->onoff = onoff;
224 1.1 jtc return (0);
225 1.1 jtc }
226 1.22 nia if (reallocarr(&ports, nports + 1, sizeof(*p)) != 0) {
227 1.11 tron error("malloc failed");
228 1.11 tron die(0);
229 1.11 tron }
230 1.1 jtc p = &ports[nports++];
231 1.1 jtc p->port = port;
232 1.1 jtc p->onoff = onoff;
233 1.1 jtc return (1);
234 1.1 jtc }
235 1.1 jtc
236 1.1 jtc int
237 1.16 ad checkport(struct inpcb *inp)
238 1.1 jtc {
239 1.6 lukem struct pitem *p;
240 1.1 jtc
241 1.1 jtc if (ports)
242 1.1 jtc for (p = ports; p < ports+nports; p++)
243 1.1 jtc if (p->port == inp->inp_lport || p->port == inp->inp_fport)
244 1.1 jtc return (p->onoff);
245 1.1 jtc return (1);
246 1.1 jtc }
247 1.1 jtc
248 1.1 jtc static void
249 1.16 ad showports(void)
250 1.1 jtc {
251 1.6 lukem struct pitem *p;
252 1.1 jtc struct servent *sp;
253 1.1 jtc
254 1.1 jtc for (p = ports; p < ports+nports; p++) {
255 1.1 jtc sp = getservbyport(p->port,
256 1.5 mrg protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
257 1.1 jtc if (!p->onoff)
258 1.1 jtc addch('!');
259 1.1 jtc if (sp)
260 1.1 jtc printw("%s ", sp->s_name);
261 1.1 jtc else
262 1.15 jdc printw("%ld ", p->port);
263 1.1 jtc }
264 1.1 jtc }
265 1.1 jtc
266 1.1 jtc static int
267 1.16 ad addrcmp(struct sockaddr *sa1, struct sockaddr *sa2)
268 1.14 itojun {
269 1.14 itojun if (sa1->sa_family != sa2->sa_family)
270 1.14 itojun return 0;
271 1.14 itojun if (sa1->sa_len != sa2->sa_len)
272 1.14 itojun return 0;
273 1.14 itojun switch (sa1->sa_family) {
274 1.14 itojun case AF_INET:
275 1.14 itojun if (((struct sockaddr_in *)sa1)->sin_addr.s_addr ==
276 1.14 itojun ((struct sockaddr_in *)sa2)->sin_addr.s_addr)
277 1.14 itojun return 1;
278 1.14 itojun break;
279 1.14 itojun #ifdef INET6
280 1.14 itojun case AF_INET6:
281 1.14 itojun if (IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)sa1)->sin6_addr,
282 1.14 itojun &((struct sockaddr_in6 *)sa2)->sin6_addr))
283 1.14 itojun return 1;
284 1.14 itojun break;
285 1.14 itojun #endif
286 1.14 itojun default:
287 1.14 itojun if (memcmp(sa1, sa2, sa1->sa_len) == 0)
288 1.14 itojun return 1;
289 1.14 itojun break;
290 1.14 itojun }
291 1.14 itojun return 0;
292 1.14 itojun }
293 1.14 itojun
294 1.14 itojun static int
295 1.16 ad selecthost(struct sockaddr *sa, int onoff)
296 1.1 jtc {
297 1.6 lukem struct hitem *p;
298 1.1 jtc
299 1.14 itojun if (sa == 0) {
300 1.1 jtc if (hosts == 0)
301 1.1 jtc return (0);
302 1.1 jtc free((char *)hosts), hosts = 0;
303 1.1 jtc nhosts = 0;
304 1.1 jtc return (1);
305 1.1 jtc }
306 1.1 jtc for (p = hosts; p < hosts+nhosts; p++)
307 1.14 itojun if (addrcmp((struct sockaddr *)&p->addr, sa)) {
308 1.1 jtc p->onoff = onoff;
309 1.1 jtc return (0);
310 1.1 jtc }
311 1.14 itojun if (sa->sa_len > sizeof(struct sockaddr_storage))
312 1.14 itojun return (-1); /*XXX*/
313 1.22 nia if (reallocarr(&hosts, nhosts + 1, sizeof(*p)) != 0) {
314 1.12 itojun error("malloc failed");
315 1.12 itojun die(0);
316 1.12 itojun }
317 1.1 jtc p = &hosts[nhosts++];
318 1.14 itojun memcpy(&p->addr, sa, sa->sa_len);
319 1.1 jtc p->onoff = onoff;
320 1.1 jtc return (1);
321 1.1 jtc }
322 1.1 jtc
323 1.1 jtc int
324 1.16 ad checkhost(struct inpcb *inp)
325 1.1 jtc {
326 1.6 lukem struct hitem *p;
327 1.21 dsl struct sockaddr_in *s_in;
328 1.14 itojun
329 1.14 itojun if (hosts)
330 1.14 itojun for (p = hosts; p < hosts+nhosts; p++) {
331 1.14 itojun if (((struct sockaddr *)&p->addr)->sa_family != AF_INET)
332 1.14 itojun continue;
333 1.21 dsl s_in = (struct sockaddr_in *)&p->addr;
334 1.21 dsl if (s_in->sin_addr.s_addr == inp->inp_laddr.s_addr ||
335 1.21 dsl s_in->sin_addr.s_addr == inp->inp_faddr.s_addr)
336 1.14 itojun return (p->onoff);
337 1.14 itojun }
338 1.14 itojun return (1);
339 1.14 itojun }
340 1.14 itojun
341 1.14 itojun #ifdef INET6
342 1.14 itojun int
343 1.23 ozaki checkhost6(struct inpcb *inp)
344 1.14 itojun {
345 1.14 itojun struct hitem *p;
346 1.14 itojun struct sockaddr_in6 *sin6;
347 1.1 jtc
348 1.1 jtc if (hosts)
349 1.14 itojun for (p = hosts; p < hosts+nhosts; p++) {
350 1.14 itojun if (((struct sockaddr *)&p->addr)->sa_family != AF_INET6)
351 1.14 itojun continue;
352 1.14 itojun sin6 = (struct sockaddr_in6 *)&p->addr;
353 1.23 ozaki if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &inp->inp_laddr6) ||
354 1.23 ozaki IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &inp->inp_faddr6))
355 1.8 mrg return (p->onoff);
356 1.14 itojun }
357 1.1 jtc return (1);
358 1.1 jtc }
359 1.14 itojun #endif
360 1.1 jtc
361 1.1 jtc static void
362 1.16 ad showhosts(void)
363 1.1 jtc {
364 1.6 lukem struct hitem *p;
365 1.14 itojun char hbuf[NI_MAXHOST];
366 1.14 itojun struct sockaddr *sa;
367 1.14 itojun int flags;
368 1.1 jtc
369 1.14 itojun #if 0
370 1.14 itojun flags = nflag ? NI_NUMERICHOST : 0;
371 1.14 itojun #else
372 1.14 itojun flags = 0;
373 1.14 itojun #endif
374 1.1 jtc for (p = hosts; p < hosts+nhosts; p++) {
375 1.14 itojun sa = (struct sockaddr *)&p->addr;
376 1.14 itojun if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
377 1.14 itojun flags) != 0)
378 1.18 itojun strlcpy(hbuf, "(invalid)", sizeof(hbuf));
379 1.1 jtc if (!p->onoff)
380 1.1 jtc addch('!');
381 1.14 itojun printw("%s ", hbuf);
382 1.1 jtc }
383 1.1 jtc }
384