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