netcmds.c revision 1.21 1 1.21 dsl /* $NetBSD: netcmds.c,v 1.21 2005/02/26 22:12:33 dsl 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.21 dsl __RCSID("$NetBSD: netcmds.c,v 1.21 2005/02/26 22:12:33 dsl 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.11 tron p = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
227 1.11 tron if (p == NULL) {
228 1.11 tron error("malloc failed");
229 1.11 tron die(0);
230 1.11 tron }
231 1.11 tron ports = p;
232 1.1 jtc p = &ports[nports++];
233 1.1 jtc p->port = port;
234 1.1 jtc p->onoff = onoff;
235 1.1 jtc return (1);
236 1.1 jtc }
237 1.1 jtc
238 1.1 jtc int
239 1.16 ad checkport(struct inpcb *inp)
240 1.1 jtc {
241 1.6 lukem struct pitem *p;
242 1.1 jtc
243 1.1 jtc if (ports)
244 1.1 jtc for (p = ports; p < ports+nports; p++)
245 1.1 jtc if (p->port == inp->inp_lport || p->port == inp->inp_fport)
246 1.1 jtc return (p->onoff);
247 1.1 jtc return (1);
248 1.1 jtc }
249 1.1 jtc
250 1.14 itojun #ifdef INET6
251 1.14 itojun int
252 1.16 ad checkport6(struct in6pcb *in6p)
253 1.14 itojun {
254 1.14 itojun struct pitem *p;
255 1.14 itojun
256 1.14 itojun if (ports)
257 1.14 itojun for (p = ports; p < ports+nports; p++)
258 1.14 itojun if (p->port == in6p->in6p_lport || p->port == in6p->in6p_fport)
259 1.14 itojun return (p->onoff);
260 1.14 itojun return (1);
261 1.14 itojun }
262 1.14 itojun #endif
263 1.14 itojun
264 1.1 jtc static void
265 1.16 ad showports(void)
266 1.1 jtc {
267 1.6 lukem struct pitem *p;
268 1.1 jtc struct servent *sp;
269 1.1 jtc
270 1.1 jtc for (p = ports; p < ports+nports; p++) {
271 1.1 jtc sp = getservbyport(p->port,
272 1.5 mrg protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
273 1.1 jtc if (!p->onoff)
274 1.1 jtc addch('!');
275 1.1 jtc if (sp)
276 1.1 jtc printw("%s ", sp->s_name);
277 1.1 jtc else
278 1.15 jdc printw("%ld ", p->port);
279 1.1 jtc }
280 1.1 jtc }
281 1.1 jtc
282 1.1 jtc static int
283 1.16 ad addrcmp(struct sockaddr *sa1, struct sockaddr *sa2)
284 1.14 itojun {
285 1.14 itojun if (sa1->sa_family != sa2->sa_family)
286 1.14 itojun return 0;
287 1.14 itojun if (sa1->sa_len != sa2->sa_len)
288 1.14 itojun return 0;
289 1.14 itojun switch (sa1->sa_family) {
290 1.14 itojun case AF_INET:
291 1.14 itojun if (((struct sockaddr_in *)sa1)->sin_addr.s_addr ==
292 1.14 itojun ((struct sockaddr_in *)sa2)->sin_addr.s_addr)
293 1.14 itojun return 1;
294 1.14 itojun break;
295 1.14 itojun #ifdef INET6
296 1.14 itojun case AF_INET6:
297 1.14 itojun if (IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)sa1)->sin6_addr,
298 1.14 itojun &((struct sockaddr_in6 *)sa2)->sin6_addr))
299 1.14 itojun return 1;
300 1.14 itojun break;
301 1.14 itojun #endif
302 1.14 itojun default:
303 1.14 itojun if (memcmp(sa1, sa2, sa1->sa_len) == 0)
304 1.14 itojun return 1;
305 1.14 itojun break;
306 1.14 itojun }
307 1.14 itojun return 0;
308 1.14 itojun }
309 1.14 itojun
310 1.14 itojun static int
311 1.16 ad selecthost(struct sockaddr *sa, int onoff)
312 1.1 jtc {
313 1.6 lukem struct hitem *p;
314 1.1 jtc
315 1.14 itojun if (sa == 0) {
316 1.1 jtc if (hosts == 0)
317 1.1 jtc return (0);
318 1.1 jtc free((char *)hosts), hosts = 0;
319 1.1 jtc nhosts = 0;
320 1.1 jtc return (1);
321 1.1 jtc }
322 1.1 jtc for (p = hosts; p < hosts+nhosts; p++)
323 1.14 itojun if (addrcmp((struct sockaddr *)&p->addr, sa)) {
324 1.1 jtc p->onoff = onoff;
325 1.1 jtc return (0);
326 1.1 jtc }
327 1.14 itojun if (sa->sa_len > sizeof(struct sockaddr_storage))
328 1.14 itojun return (-1); /*XXX*/
329 1.12 itojun p = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
330 1.12 itojun if (p == NULL) {
331 1.12 itojun error("malloc failed");
332 1.12 itojun die(0);
333 1.12 itojun }
334 1.12 itojun hosts = p;
335 1.1 jtc p = &hosts[nhosts++];
336 1.14 itojun memcpy(&p->addr, sa, sa->sa_len);
337 1.1 jtc p->onoff = onoff;
338 1.1 jtc return (1);
339 1.1 jtc }
340 1.1 jtc
341 1.1 jtc int
342 1.16 ad checkhost(struct inpcb *inp)
343 1.1 jtc {
344 1.6 lukem struct hitem *p;
345 1.21 dsl struct sockaddr_in *s_in;
346 1.14 itojun
347 1.14 itojun if (hosts)
348 1.14 itojun for (p = hosts; p < hosts+nhosts; p++) {
349 1.14 itojun if (((struct sockaddr *)&p->addr)->sa_family != AF_INET)
350 1.14 itojun continue;
351 1.21 dsl s_in = (struct sockaddr_in *)&p->addr;
352 1.21 dsl if (s_in->sin_addr.s_addr == inp->inp_laddr.s_addr ||
353 1.21 dsl s_in->sin_addr.s_addr == inp->inp_faddr.s_addr)
354 1.14 itojun return (p->onoff);
355 1.14 itojun }
356 1.14 itojun return (1);
357 1.14 itojun }
358 1.14 itojun
359 1.14 itojun #ifdef INET6
360 1.14 itojun int
361 1.16 ad checkhost6(struct in6pcb *in6p)
362 1.14 itojun {
363 1.14 itojun struct hitem *p;
364 1.14 itojun struct sockaddr_in6 *sin6;
365 1.1 jtc
366 1.1 jtc if (hosts)
367 1.14 itojun for (p = hosts; p < hosts+nhosts; p++) {
368 1.14 itojun if (((struct sockaddr *)&p->addr)->sa_family != AF_INET6)
369 1.14 itojun continue;
370 1.14 itojun sin6 = (struct sockaddr_in6 *)&p->addr;
371 1.14 itojun if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_laddr) ||
372 1.14 itojun IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6p->in6p_faddr))
373 1.8 mrg return (p->onoff);
374 1.14 itojun }
375 1.1 jtc return (1);
376 1.1 jtc }
377 1.14 itojun #endif
378 1.1 jtc
379 1.1 jtc static void
380 1.16 ad showhosts(void)
381 1.1 jtc {
382 1.6 lukem struct hitem *p;
383 1.14 itojun char hbuf[NI_MAXHOST];
384 1.14 itojun struct sockaddr *sa;
385 1.14 itojun int flags;
386 1.1 jtc
387 1.14 itojun #if 0
388 1.14 itojun flags = nflag ? NI_NUMERICHOST : 0;
389 1.14 itojun #else
390 1.14 itojun flags = 0;
391 1.14 itojun #endif
392 1.1 jtc for (p = hosts; p < hosts+nhosts; p++) {
393 1.14 itojun sa = (struct sockaddr *)&p->addr;
394 1.14 itojun if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
395 1.14 itojun flags) != 0)
396 1.18 itojun strlcpy(hbuf, "(invalid)", sizeof(hbuf));
397 1.1 jtc if (!p->onoff)
398 1.1 jtc addch('!');
399 1.14 itojun printw("%s ", hbuf);
400 1.1 jtc }
401 1.1 jtc }
402