xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_ipsec.c (revision 410:51e39f6c6311)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*410Skcpoon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/time.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/socket.h>
400Sstevel@tonic-gate #include <sys/sockio.h>
410Sstevel@tonic-gate #include <net/if.h>
420Sstevel@tonic-gate #include <netinet/in_systm.h>
430Sstevel@tonic-gate #include <netinet/in.h>
440Sstevel@tonic-gate #include <netinet/ip.h>
450Sstevel@tonic-gate #include <netinet/ip_icmp.h>
460Sstevel@tonic-gate #include <netinet/icmp6.h>
470Sstevel@tonic-gate #include <netinet/if_ether.h>
480Sstevel@tonic-gate #include <inet/ipsecesp.h>
490Sstevel@tonic-gate #include <inet/ipsecah.h>
500Sstevel@tonic-gate #include "snoop.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate extern char *dlc_header;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate int
550Sstevel@tonic-gate interpret_esp(int flags, uint8_t *hdr, int iplen, int fraglen)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	esph_t *esph = (esph_t *)hdr;
580Sstevel@tonic-gate 	esph_t *aligned_esph;
590Sstevel@tonic-gate 	esph_t storage;	/* In case hdr isn't aligned. */
600Sstevel@tonic-gate 	char *line;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (fraglen < sizeof (esph_t))
63*410Skcpoon 		return (fraglen);	/* incomplete header */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (!IS_P2ALIGNED(hdr, 4)) {
660Sstevel@tonic-gate 		aligned_esph = &storage;
670Sstevel@tonic-gate 		bcopy(hdr, aligned_esph, sizeof (esph_t));
680Sstevel@tonic-gate 	} else {
690Sstevel@tonic-gate 		aligned_esph = esph;
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (flags & F_SUM) {
730Sstevel@tonic-gate 		line = (char *)get_sum_line();
740Sstevel@tonic-gate 		/*
750Sstevel@tonic-gate 		 * sprintf() is safe because line guarantees us 80 columns,
760Sstevel@tonic-gate 		 * and SPI and replay certainly won't exceed that.
770Sstevel@tonic-gate 		 */
780Sstevel@tonic-gate 		(void) sprintf(line, "ESP SPI=0x%x Replay=%u",
790Sstevel@tonic-gate 		    ntohl(aligned_esph->esph_spi),
800Sstevel@tonic-gate 		    ntohl(aligned_esph->esph_replay));
810Sstevel@tonic-gate 		line += strlen(line);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (flags & F_DTAIL) {
850Sstevel@tonic-gate 		show_header("ESP:  ", "Encapsulating Security Payload",
860Sstevel@tonic-gate 		    sizeof (esph_t));
870Sstevel@tonic-gate 		show_space();
880Sstevel@tonic-gate 		/*
890Sstevel@tonic-gate 		 * sprintf() is safe because get_line guarantees us 80 columns,
900Sstevel@tonic-gate 		 * and SPI and replay certainly won't exceed that.
910Sstevel@tonic-gate 		 */
920Sstevel@tonic-gate 		(void) sprintf(get_line((char *)&esph->esph_spi - dlc_header,
930Sstevel@tonic-gate 		    4), "SPI = 0x%x", ntohl(aligned_esph->esph_spi));
940Sstevel@tonic-gate 		(void) sprintf(get_line((char *)&esph->esph_replay -
950Sstevel@tonic-gate 		    dlc_header, 4), "Replay = %u",
960Sstevel@tonic-gate 		    ntohl(aligned_esph->esph_replay));
970Sstevel@tonic-gate 		(void) sprintf(get_line((char *)(esph + 1) - dlc_header,
980Sstevel@tonic-gate 		    4), "   ....ENCRYPTED DATA....");
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	return (sizeof (esph_t));
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate int
1050Sstevel@tonic-gate interpret_ah(int flags, uint8_t *hdr, int iplen, int fraglen)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	ah_t *ah = (ah_t *)hdr;
1080Sstevel@tonic-gate 	ah_t *aligned_ah;
1090Sstevel@tonic-gate 	ah_t storage;	/* In case hdr isn't aligned. */
1100Sstevel@tonic-gate 	char *line, *buff;
1110Sstevel@tonic-gate 	uint_t ahlen, auth_data_len;
1120Sstevel@tonic-gate 	uint8_t *auth_data, *data;
1130Sstevel@tonic-gate 	int new_iplen;
1140Sstevel@tonic-gate 	uint8_t proto;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (fraglen < sizeof (ah_t))
117*410Skcpoon 		return (fraglen);		/* incomplete header */
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (!IS_P2ALIGNED(hdr, 4)) {
1200Sstevel@tonic-gate 		aligned_ah = (ah_t *)&storage;
1210Sstevel@tonic-gate 		bcopy(hdr, storage, sizeof (ah_t));
1220Sstevel@tonic-gate 	} else {
1230Sstevel@tonic-gate 		aligned_ah = ah;
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * "+ 8" is for the "constant" part that's not included in the AH
1280Sstevel@tonic-gate 	 * length.
1290Sstevel@tonic-gate 	 *
1300Sstevel@tonic-gate 	 * The AH RFC specifies the length field in "length in 4-byte units,
1310Sstevel@tonic-gate 	 * not counting the first 8 bytes".  So if an AH is 24 bytes long,
1320Sstevel@tonic-gate 	 * the length field will contain "4".  (4 * 4 + 8 == 24).
1330Sstevel@tonic-gate 	 */
1340Sstevel@tonic-gate 	ahlen = (aligned_ah->ah_length << 2) + 8;
1350Sstevel@tonic-gate 	fraglen -= ahlen;
1360Sstevel@tonic-gate 	if (fraglen < 0)
137*410Skcpoon 		return (fraglen + ahlen);	/* incomplete header */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	auth_data_len = ahlen - sizeof (ah_t);
1400Sstevel@tonic-gate 	auth_data = (uint8_t *)(ah + 1);
1410Sstevel@tonic-gate 	data = auth_data + auth_data_len;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (flags & F_SUM) {
1440Sstevel@tonic-gate 		line = (char *)get_sum_line();
1450Sstevel@tonic-gate 		(void) sprintf(line, "AH SPI=0x%x Replay=%u",
1460Sstevel@tonic-gate 		    ntohl(aligned_ah->ah_spi), ntohl(aligned_ah->ah_replay));
1470Sstevel@tonic-gate 		line += strlen(line);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (flags & F_DTAIL) {
1510Sstevel@tonic-gate 		show_header("AH:  ", "Authentication Header", ahlen);
1520Sstevel@tonic-gate 		show_space();
1530Sstevel@tonic-gate 		(void) sprintf(get_line((char *)&ah->ah_nexthdr - dlc_header,
1540Sstevel@tonic-gate 		    1), "Next header = %d (%s)", aligned_ah->ah_nexthdr,
1550Sstevel@tonic-gate 		    getproto(aligned_ah->ah_nexthdr));
1560Sstevel@tonic-gate 		(void) sprintf(get_line((char *)&ah->ah_length - dlc_header, 1),
1570Sstevel@tonic-gate 		    "AH length = %d (%d bytes)", aligned_ah->ah_length, ahlen);
1580Sstevel@tonic-gate 		(void) sprintf(get_line((char *)&ah->ah_reserved - dlc_header,
1590Sstevel@tonic-gate 		    2), "<Reserved field = 0x%x>",
1600Sstevel@tonic-gate 		    ntohs(aligned_ah->ah_reserved));
1610Sstevel@tonic-gate 		(void) sprintf(get_line((char *)&ah->ah_spi - dlc_header, 4),
1620Sstevel@tonic-gate 		    "SPI = 0x%x", ntohl(aligned_ah->ah_spi));
1630Sstevel@tonic-gate 		(void) sprintf(get_line((char *)&ah->ah_replay - dlc_header, 4),
1640Sstevel@tonic-gate 		    "Replay = %u", ntohl(aligned_ah->ah_replay));
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 		/* * 2 for two hex digits per auth_data byte. */
1670Sstevel@tonic-gate 		buff = malloc(auth_data_len * 2);
1680Sstevel@tonic-gate 		if (buff != NULL) {
1690Sstevel@tonic-gate 			int i;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 			for (i = 0; i < auth_data_len; i++)
1720Sstevel@tonic-gate 				sprintf(buff + i * 2, "%02x", auth_data[i]);
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		(void) sprintf(get_line((char *)auth_data - dlc_header,
1760Sstevel@tonic-gate 		    auth_data_len), "ICV = %s",
1770Sstevel@tonic-gate 		    (buff == NULL) ? "<out of memory>" : buff);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		/* malloc(3c) says I can call free even if buff == NULL */
1800Sstevel@tonic-gate 		free(buff);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		show_space();
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	new_iplen = iplen - ahlen;
1860Sstevel@tonic-gate 	proto = aligned_ah->ah_nexthdr;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * Print IPv6 Extension Headers, or skip them in the summary case.
1900Sstevel@tonic-gate 	 */
1910Sstevel@tonic-gate 	if (proto == IPPROTO_HOPOPTS || proto == IPPROTO_DSTOPTS ||
1920Sstevel@tonic-gate 	    proto == IPPROTO_ROUTING || proto == IPPROTO_FRAGMENT) {
1930Sstevel@tonic-gate 		(void) print_ipv6_extensions(flags, &data, &proto, &iplen,
1940Sstevel@tonic-gate 		    &fraglen);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (fraglen > 0)
1980Sstevel@tonic-gate 		switch (proto) {
1990Sstevel@tonic-gate 			case IPPROTO_ENCAP:
2000Sstevel@tonic-gate 				(void) interpret_ip(flags, (struct ip *)data,
2010Sstevel@tonic-gate 				    new_iplen);
2020Sstevel@tonic-gate 				break;
2030Sstevel@tonic-gate 			case IPPROTO_IPV6:
2040Sstevel@tonic-gate 				(void) interpret_ipv6(flags, (ip6_t *)data,
2050Sstevel@tonic-gate 				    new_iplen);
2060Sstevel@tonic-gate 				break;
2070Sstevel@tonic-gate 			case IPPROTO_ICMP:
2080Sstevel@tonic-gate 				interpret_icmp(flags, (struct icmp *)data,
2090Sstevel@tonic-gate 				    new_iplen, fraglen);
2100Sstevel@tonic-gate 				break;
2110Sstevel@tonic-gate 			case IPPROTO_ICMPV6:
2120Sstevel@tonic-gate 				interpret_icmpv6(flags, (icmp6_t *)data,
2130Sstevel@tonic-gate 				    new_iplen, fraglen);
2140Sstevel@tonic-gate 				break;
2150Sstevel@tonic-gate 			case IPPROTO_TCP:
2160Sstevel@tonic-gate 				interpret_tcp(flags, data, new_iplen, fraglen);
2170Sstevel@tonic-gate 				break;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 			case IPPROTO_ESP:
2200Sstevel@tonic-gate 				interpret_esp(flags, data, new_iplen, fraglen);
2210Sstevel@tonic-gate 				break;
2220Sstevel@tonic-gate 			case IPPROTO_AH:
2230Sstevel@tonic-gate 				interpret_ah(flags, data, new_iplen, fraglen);
2240Sstevel@tonic-gate 				break;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 			case IPPROTO_UDP:
2280Sstevel@tonic-gate 				interpret_udp(flags, data, new_iplen, fraglen);
2290Sstevel@tonic-gate 				break;
2300Sstevel@tonic-gate 			/* default case is to not print anything else */
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	return (ahlen);
2340Sstevel@tonic-gate }
235