xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_rport.c (revision c81d47afd05baeb768e2f032636019b717899efd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <strings.h>
33 #include <sys/sysmacros.h>
34 #include <sys/types.h>
35 #include <sys/errno.h>
36 #include <setjmp.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/in.h>
41 #include <netinet/ip.h>
42 #include <netinet/if_ether.h>
43 #include "snoop.h"
44 
45 struct porttable {
46 	int	pt_num;
47 	char	*pt_short;
48 };
49 
50 static const struct porttable pt_udp[] = {
51 	{ IPPORT_ECHO,		"ECHO" },
52 	{ IPPORT_DISCARD,	"DISCARD" },
53 	{ IPPORT_DAYTIME,	"DAYTIME" },
54 	{ IPPORT_CHARGEN,	"CHARGEN" },
55 	{ IPPORT_TIMESERVER,	"TIME" },
56 	{ IPPORT_NAMESERVER,	"NAME" },
57 	{ IPPORT_DOMAIN,	"DNS" },
58 	{ IPPORT_BOOTPS,	"BOOTPS" },
59 	{ IPPORT_BOOTPC,	"BOOTPC" },
60 	{ IPPORT_TFTP,		"TFTP" },
61 	{ IPPORT_FINGER,	"FINGER" },
62 /*	{ 111,			"PORTMAP" }, Just Sun RPC */
63 	{ IPPORT_NTP,		"NTP" },
64 	{ IPPORT_NETBIOS_NS,	"NBNS" },
65 	{ IPPORT_NETBIOS_DGM,	"NBDG" },
66 	{ IPPORT_LDAP,		"LDAP" },
67 	{ IPPORT_SLP,		"SLP" },
68 /* Mobile IP defines a set of new control messages sent over UDP port 434 */
69 	{ IPPORT_MIP,		"Mobile IP" },
70 	{ IPPORT_BIFFUDP,	"BIFF" },
71 	{ IPPORT_WHOSERVER,	"WHO" },
72 	{ IPPORT_SYSLOG,	"SYSLOG" },
73 	{ IPPORT_TALK,		"TALK" },
74 	{ IPPORT_ROUTESERVER,	"RIP" },
75 	{ IPPORT_RIPNG,		"RIPng" },
76 	{ IPPORT_DHCPV6C,	"DHCPv6C" },
77 	{ IPPORT_DHCPV6S,	"DHCPv6S" },
78 	{ 550,			"NEW-RWHO" },
79 	{ 560,			"RMONITOR" },
80 	{ 561,			"MONITOR" },
81 	{ IPPORT_SOCKS,		"SOCKS" },
82 	{ 0,			NULL }
83 };
84 
85 static struct porttable pt_tcp[] = {
86 	{ 1,			"TCPMUX" },
87 	{ IPPORT_ECHO,		"ECHO" },
88 	{ IPPORT_DISCARD,	"DISCARD" },
89 	{ IPPORT_SYSTAT,	"SYSTAT" },
90 	{ IPPORT_DAYTIME,	"DAYTIME" },
91 	{ IPPORT_NETSTAT,	"NETSTAT" },
92 	{ IPPORT_CHARGEN,	"CHARGEN" },
93 	{ 20,			"FTP-DATA" },
94 	{ IPPORT_FTP,		"FTP" },
95 	{ IPPORT_TELNET,	"TELNET" },
96 	{ IPPORT_SMTP,		"SMTP" },
97 	{ IPPORT_TIMESERVER,	"TIME" },
98 	{ 39,			"RLP" },
99 	{ IPPORT_NAMESERVER,	"NAMESERVER" },
100 	{ IPPORT_WHOIS,		"NICNAME" },
101 	{ IPPORT_DOMAIN,	"DNS" },
102 	{ 70,			"GOPHER" },
103 	{ IPPORT_RJE,		"RJE" },
104 	{ IPPORT_FINGER,	"FINGER" },
105 	{ IPPORT_HTTP,		"HTTP" },
106 	{ IPPORT_TTYLINK,	"LINK" },
107 	{ IPPORT_SUPDUP,	"SUPDUP" },
108 	{ 101,			"HOSTNAME" },
109 	{ 102,			"ISO-TSAP" },
110 	{ 103,			"X400" },
111 	{ 104,			"X400-SND" },
112 	{ 105,			"CSNET-NS" },
113 	{ 109,			"POP-2" },
114 /*	{ 111,			"PORTMAP" }, Just Sun RPC */
115 	{ 113,			"AUTH" },
116 	{ 117,			"UUCP-PATH" },
117 	{ 119,			"NNTP" },
118 	{ IPPORT_NTP,		"NTP" },
119 	{ IPPORT_NETBIOS_SSN,	"NBT" },
120 	{ 143,			"IMAP" },
121 	{ 144,			"NeWS" },
122 	{ IPPORT_LDAP,		"LDAP" },
123 	{ IPPORT_SLP,		"SLP" },
124 	{ 443,			"HTTPS" },
125 	{ 445,			"SMB" },
126 	{ IPPORT_EXECSERVER,	"EXEC" },
127 	{ IPPORT_LOGINSERVER,	"RLOGIN" },
128 	{ IPPORT_CMDSERVER,	"RSHELL" },
129 	{ IPPORT_PRINTER,	"PRINTER" },
130 	{ 530,			"COURIER" },
131 	{ 540,			"UUCP" },
132 	{ 600,			"PCSERVER" },
133 	{ IPPORT_SOCKS,		"SOCKS" },
134 	{ 1524,			"INGRESLOCK" },
135 	{ 2904,			"M2UA" },
136 	{ 2905,			"M3UA" },
137 	{ 6000,			"XWIN" },
138 	{ IPPORT_HTTP_ALT,	"HTTP (proxy)" },
139 	{ 9900,			"IUA" },
140 	{ 0,			NULL },
141 };
142 
143 char *
144 getportname(int proto, in_port_t port)
145 {
146 	const struct porttable *p, *pt;
147 
148 	switch (proto) {
149 	case IPPROTO_SCTP: /* fallthru */
150 	case IPPROTO_TCP: pt = pt_tcp; break;
151 	case IPPROTO_UDP: pt = pt_udp; break;
152 	default: return (NULL);
153 	}
154 
155 	for (p = pt; p->pt_num; p++) {
156 		if (port == p->pt_num)
157 			return (p->pt_short);
158 	}
159 	return (NULL);
160 }
161 
162 int
163 reservedport(int proto, int port)
164 {
165 	const struct porttable *p, *pt;
166 
167 	switch (proto) {
168 	case IPPROTO_TCP: pt = pt_tcp; break;
169 	case IPPROTO_UDP: pt = pt_udp; break;
170 	default: return (NULL);
171 	}
172 	for (p = pt; p->pt_num; p++) {
173 		if (port == p->pt_num)
174 			return (1);
175 	}
176 	return (0);
177 }
178 
179 /*
180  * Need to be able to register an
181  * interpreter for transient ports.
182  * See TFTP interpreter.
183  */
184 #define	MAXTRANS 64
185 static struct ttable {
186 	int t_port;
187 	int (*t_proc)(int, char *, int);
188 } transients [MAXTRANS];
189 
190 int
191 add_transient(int port, int (*proc)(int, char *, int))
192 {
193 	static struct ttable *next = transients;
194 
195 	next->t_port = port;
196 	next->t_proc = proc;
197 
198 	if (++next >= &transients[MAXTRANS])
199 		next = transients;
200 
201 	return (1);
202 }
203 
204 static struct ttable *
205 is_transient(int port)
206 {
207 	struct ttable *p;
208 
209 	for (p = transients; p->t_port && p < &transients[MAXTRANS]; p++) {
210 		if (port == p->t_port)
211 			return (p);
212 	}
213 
214 	return (NULL);
215 }
216 
217 void
218 del_transient(int port)
219 {
220 	struct ttable *p;
221 
222 	for (p = transients; p->t_port && p < &transients[MAXTRANS]; p++) {
223 		if (port == p->t_port)
224 			p->t_port = -1;
225 	}
226 }
227 
228 static void
229 interpret_syslog(int flags, char dir, int port, const char *syslogstr,
230     int dlen)
231 {
232 	static const char *pris[] = {
233 	    "emerg", "alert", "crit", "error", "warn", "notice", "info", "debug"
234 	};
235 	static const char *facs[] = {
236 	    "kern", "user", "mail", "daemon", "auth", "syslog", "lpr", "news",
237 	    "uucp", NULL, NULL, NULL, NULL, "audit", NULL, "cron", "local0",
238 	    "local1", "local2", "local3", "local4", "local5", "local6", "local7"
239 	};
240 
241 	int composit;
242 	int pri = -1;
243 	int facil = -1;
244 	boolean_t bogus = B_TRUE;
245 	int priostrlen = 0;
246 	int datalen = dlen;
247 	char unknown[4];	/* for unrecognized ones */
248 	const char *facilstr = "BAD";
249 	const char *pristr = "FMT";
250 	const char *data = syslogstr;
251 
252 	/*
253 	 * Is there enough data to interpret (left bracket + at least 3 chars
254 	 * which could be digits, right bracket, or space)?
255 	 */
256 	if (datalen >= 4 && data != NULL) {
257 		if (*data == '<') {
258 			const int FACS_LEN = sizeof (facs) / sizeof (facs[0]);
259 			char buffer[4];
260 			char *end;
261 
262 			data++;
263 			datalen--;
264 
265 			(void) strlcpy(buffer, data, sizeof (buffer));
266 			composit = strtoul(buffer, &end, 0);
267 			data += end - buffer;
268 			if (*data == '>') {
269 				data++;
270 				datalen -= end - buffer + 1;
271 
272 				pri = composit & 0x7;
273 				facil = (composit & 0xF8) >> 3;
274 
275 				if ((facil >= FACS_LEN) ||
276 				    (facs[facil] == NULL)) {
277 					snprintf(unknown, sizeof (unknown),
278 					    "%d", facil);
279 					facilstr = unknown;
280 				} else {
281 					facilstr = facs[facil];
282 				}
283 				pristr = pris[pri];
284 				priostrlen = dlen - datalen;
285 				bogus = B_FALSE;
286 			} else {
287 				data = syslogstr;
288 				datalen = dlen;
289 			}
290 		}
291 	}
292 
293 	if (flags & F_SUM) {
294 		(void) snprintf(get_sum_line(), MAXLINE,
295 		    "SYSLOG %c port=%d %s.%s: %s",
296 		    dir, port, facilstr, pristr,
297 		    show_string(syslogstr, dlen, 20));
298 
299 	}
300 
301 	if (flags & F_DTAIL) {
302 		static char syslog[] = "SYSLOG:  ";
303 		show_header(syslog, syslog, dlen);
304 		show_space();
305 		(void) snprintf(get_detail_line(0, 0), MAXLINE,
306 		    "%s%sPriority: %.*s%s(%s.%s)", prot_nest_prefix, syslog,
307 		    priostrlen, syslogstr, bogus ? "" : " ",
308 		    facilstr, pristr);
309 		(void) snprintf(get_line(0, 0), get_line_remain(),
310 			"\"%s\"",
311 			show_string(syslogstr, dlen, 60));
312 		show_trailer();
313 	}
314 }
315 
316 int src_port, dst_port, curr_proto;
317 
318 int
319 interpret_reserved(int flags, int proto, in_port_t src, in_port_t dst,
320     char *data, int dlen)
321 {
322 	const char *pn;
323 	int dir, port, which;
324 	char pbuff[16], hbuff[32];
325 	struct ttable *ttabp;
326 
327 	src_port = src;
328 	dst_port = dst;
329 	curr_proto = proto;
330 
331 	pn = getportname(proto, src);
332 	if (pn != NULL) {
333 		dir = 'R';
334 		port = dst;
335 		which = src;
336 	} else {
337 		pn = getportname(proto, dst);
338 		if (pn == NULL) {
339 			ttabp = is_transient(src);
340 			if (ttabp) {
341 				(ttabp->t_proc)(flags, data, dlen);
342 				return (1);
343 			}
344 			ttabp = is_transient(dst);
345 			if (ttabp) {
346 				(ttabp->t_proc)(flags, data, dlen);
347 				return (1);
348 			}
349 			return (0);
350 		}
351 
352 		dir = 'C';
353 		port = src;
354 		which = dst;
355 	}
356 
357 	if ((dst == IPPORT_DOMAIN || src == IPPORT_DOMAIN) &&
358 	    proto != IPPROTO_TCP) {
359 		interpret_dns(flags, proto, (uchar_t *)data, dlen);
360 		return (1);
361 	}
362 
363 	if (dst == IPPORT_SYSLOG && proto != IPPROTO_TCP) {
364 		/*
365 		 * TCP port 514 is rshell.  UDP port 514 is syslog.
366 		 */
367 		interpret_syslog(flags, dir, port, (const char *)data, dlen);
368 		return (1);
369 	}
370 
371 	if (dlen > 0) {
372 		switch (which) {
373 		case  IPPORT_BOOTPS:
374 		case  IPPORT_BOOTPC:
375 			(void) interpret_dhcp(flags, (struct dhcp *)data,
376 			    dlen);
377 			return (1);
378 		case IPPORT_DHCPV6S:
379 		case IPPORT_DHCPV6C:
380 			(void) interpret_dhcpv6(flags, (uint8_t *)data, dlen);
381 			return (1);
382 		case  IPPORT_TFTP:
383 			(void) interpret_tftp(flags, (struct tftphdr *)data,
384 			    dlen);
385 			return (1);
386 		case  IPPORT_HTTP:
387 		case  IPPORT_HTTP_ALT:
388 			(void) interpret_http(flags, data, dlen);
389 			return (1);
390 		case IPPORT_NTP:
391 			(void) interpret_ntp(flags, (struct ntpdata *)data,
392 			    dlen);
393 			return (1);
394 		case IPPORT_NETBIOS_NS:
395 			interpret_netbios_ns(flags, (uchar_t *)data, dlen);
396 			return (1);
397 		case IPPORT_NETBIOS_DGM:
398 			interpret_netbios_datagram(flags, (uchar_t *)data,
399 			    dlen);
400 			return (1);
401 		case IPPORT_NETBIOS_SSN:
402 		case 445:
403 			/*
404 			 * SMB on port 445 is a subset of NetBIOS SMB
405 			 * on port 139.  The same interpreter can be used
406 			 * for both.
407 			 */
408 			interpret_netbios_ses(flags, (uchar_t *)data, dlen);
409 			return (1);
410 		case IPPORT_LDAP:
411 			interpret_ldap(flags, data, dlen, src, dst);
412 			return (1);
413 		case IPPORT_SLP:
414 			interpret_slp(flags, data, dlen);
415 			return (1);
416 		case IPPORT_MIP:
417 			interpret_mip_cntrlmsg(flags, (uchar_t *)data, dlen);
418 			return (1);
419 		case IPPORT_ROUTESERVER:
420 			(void) interpret_rip(flags, (struct rip *)data, dlen);
421 			return (1);
422 		case IPPORT_RIPNG:
423 			(void) interpret_rip6(flags, (struct rip6 *)data,
424 			    dlen);
425 			return (1);
426 		case IPPORT_SOCKS:
427 			if (dir == 'C')
428 				(void) interpret_socks_call(flags, data, dlen);
429 			else
430 				(void) interpret_socks_reply(flags, data,
431 				    dlen);
432 			return (1);
433 		}
434 	}
435 
436 	if (flags & F_SUM) {
437 		(void) snprintf(get_sum_line(), MAXLINE,
438 			"%s %c port=%d %s",
439 			pn, dir, port,
440 			show_string(data, dlen, 20));
441 	}
442 
443 	if (flags & F_DTAIL) {
444 		(void) snprintf(pbuff, sizeof (pbuff), "%s:  ", pn);
445 		(void) snprintf(hbuff, sizeof (hbuff), "%s:  ", pn);
446 		show_header(pbuff, hbuff, dlen);
447 		show_space();
448 		(void) snprintf(get_line(0, 0), get_line_remain(),
449 			"\"%s\"",
450 			show_string(data, dlen, 60));
451 		show_trailer();
452 	}
453 	return (1);
454 }
455 
456 char *
457 show_string(const char *str, int dlen, int maxlen)
458 /*
459  *   Prints len bytes from str enclosed in quotes.
460  *   If len is negative, length is taken from strlen(str).
461  *   No more than maxlen bytes will be printed.  Longer
462  *   strings are flagged with ".." after the closing quote.
463  *   Non-printing characters are converted to C-style escape
464  *   codes or octal digits.
465  */
466 {
467 #define	TBSIZE	256
468 	static char tbuff[TBSIZE];
469 	const char *p;
470 	char *pp;
471 	int printable = 0;
472 	int c, len;
473 
474 	len = dlen > maxlen ? maxlen : dlen;
475 	dlen = len;
476 
477 	for (p = str, pp = tbuff; len; p++, len--) {
478 		switch (c = *p & 0xFF) {
479 		case '\n': (void) strcpy(pp, "\\n"); pp += 2; break;
480 		case '\b': (void) strcpy(pp, "\\b"); pp += 2; break;
481 		case '\t': (void) strcpy(pp, "\\t"); pp += 2; break;
482 		case '\r': (void) strcpy(pp, "\\r"); pp += 2; break;
483 		case '\f': (void) strcpy(pp, "\\f"); pp += 2; break;
484 		default:
485 			if (isascii(c) && isprint(c)) {
486 				*pp++ = c;
487 				printable++;
488 			} else {
489 				(void) snprintf(pp, TBSIZE - (pp - tbuff),
490 					isdigit(*(p + 1)) ?
491 					"\\%03o" : "\\%o", c);
492 				pp += strlen(pp);
493 			}
494 			break;
495 		}
496 		*pp = '\0';
497 		/*
498 		 * Check for overflow of temporary buffer.  Allow for
499 		 * the next character to be a \nnn followed by a trailing
500 		 * null.  If not, then just bail with what we have.
501 		 */
502 		if (pp + 5 >= &tbuff[TBSIZE]) {
503 			break;
504 		}
505 	}
506 	return (printable > dlen / 2 ? tbuff : "");
507 }
508