xref: /freebsd/contrib/tcpdump/print-ip.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
14edb46e9SPaul Traina /*
2699fc314SBill Fenner  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina  */
214edb46e9SPaul Traina 
223340d773SGleb Smirnoff /* \summary: IP printer */
233340d773SGleb Smirnoff 
24ee67461eSJoseph Mingrone #include <config.h>
254edb46e9SPaul Traina 
26ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
274edb46e9SPaul Traina 
283340d773SGleb Smirnoff #include "netdissect.h"
293c602fabSXin LI #include "addrtoname.h"
303340d773SGleb Smirnoff #include "extract.h"
314edb46e9SPaul Traina 
32943ee2b1SBill Fenner #include "ip.h"
33cc391cceSBruce M Simpson #include "ipproto.h"
344edb46e9SPaul Traina 
353c602fabSXin LI 
363c602fabSXin LI static const struct tok ip_option_values[] = {
37c1ad1296SSam Leffler     { IPOPT_EOL, "EOL" },
38c1ad1296SSam Leffler     { IPOPT_NOP, "NOP" },
39c1ad1296SSam Leffler     { IPOPT_TS, "timestamp" },
40c1ad1296SSam Leffler     { IPOPT_SECURITY, "security" },
41c1ad1296SSam Leffler     { IPOPT_RR, "RR" },
42c1ad1296SSam Leffler     { IPOPT_SSRR, "SSRR" },
43c1ad1296SSam Leffler     { IPOPT_LSRR, "LSRR" },
44c1ad1296SSam Leffler     { IPOPT_RA, "RA" },
45abf25193SMax Laier     { IPOPT_RFC1393, "traceroute" },
46c1ad1296SSam Leffler     { 0, NULL }
47c1ad1296SSam Leffler };
48c1ad1296SSam Leffler 
494edb46e9SPaul Traina /*
504edb46e9SPaul Traina  * print the recorded route in an IP RR, LSRR or SSRR option.
514edb46e9SPaul Traina  */
520bff6a5aSEd Maste static int
ip_printroute(netdissect_options * ndo,const u_char * cp,u_int length)533c602fabSXin LI ip_printroute(netdissect_options *ndo,
54ee67461eSJoseph Mingrone               const u_char *cp, u_int length)
554edb46e9SPaul Traina {
56ee67461eSJoseph Mingrone 	u_int ptr;
57ee67461eSJoseph Mingrone 	u_int len;
584edb46e9SPaul Traina 
59cc391cceSBruce M Simpson 	if (length < 3) {
60ee67461eSJoseph Mingrone 		ND_PRINT(" [bad length %u]", length);
610bff6a5aSEd Maste 		return (0);
62cc391cceSBruce M Simpson 	}
634edb46e9SPaul Traina 	if ((length + 1) & 3)
64ee67461eSJoseph Mingrone 		ND_PRINT(" [bad length %u]", length);
65ee67461eSJoseph Mingrone 	ptr = GET_U_1(cp + 2) - 1;
664edb46e9SPaul Traina 	if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
67ee67461eSJoseph Mingrone 		ND_PRINT(" [bad ptr %u]", GET_U_1(cp + 2));
684edb46e9SPaul Traina 
694edb46e9SPaul Traina 	for (len = 3; len < length; len += 4) {
70ee67461eSJoseph Mingrone 		ND_TCHECK_4(cp + len);	/* Needed to print the IP addresses */
71ee67461eSJoseph Mingrone 		ND_PRINT(" %s", GET_IPADDR_STRING(cp + len));
72c1ad1296SSam Leffler 		if (ptr > len)
73ee67461eSJoseph Mingrone 			ND_PRINT(",");
744edb46e9SPaul Traina 	}
750bff6a5aSEd Maste 	return (0);
760bff6a5aSEd Maste 
770bff6a5aSEd Maste trunc:
780bff6a5aSEd Maste 	return (-1);
794edb46e9SPaul Traina }
804edb46e9SPaul Traina 
81cc391cceSBruce M Simpson /*
82c1ad1296SSam Leffler  * If source-routing is present and valid, return the final destination.
83cc391cceSBruce M Simpson  * Otherwise, return IP destination.
84cc391cceSBruce M Simpson  *
85cc391cceSBruce M Simpson  * This is used for UDP and TCP pseudo-header in the checksum
86cc391cceSBruce M Simpson  * calculation.
87cc391cceSBruce M Simpson  */
883c602fabSXin LI static uint32_t
ip_finddst(netdissect_options * ndo,const struct ip * ip)893c602fabSXin LI ip_finddst(netdissect_options *ndo,
903c602fabSXin LI            const struct ip *ip)
91cc391cceSBruce M Simpson {
92ee67461eSJoseph Mingrone 	u_int length;
93ee67461eSJoseph Mingrone 	u_int len;
94cc391cceSBruce M Simpson 	const u_char *cp;
95cc391cceSBruce M Simpson 
96cc391cceSBruce M Simpson 	cp = (const u_char *)(ip + 1);
97ee67461eSJoseph Mingrone 	length = IP_HL(ip) * 4;
98ee67461eSJoseph Mingrone 	if (length < sizeof(struct ip))
99ee67461eSJoseph Mingrone 		goto trunc;
100ee67461eSJoseph Mingrone 	length -= sizeof(struct ip);
101cc391cceSBruce M Simpson 
102ee67461eSJoseph Mingrone 	for (; length != 0; cp += len, length -= len) {
103cc391cceSBruce M Simpson 		int tt;
104cc391cceSBruce M Simpson 
105ee67461eSJoseph Mingrone 		tt = GET_U_1(cp);
106c1ad1296SSam Leffler 		if (tt == IPOPT_EOL)
107c1ad1296SSam Leffler 			break;
108c1ad1296SSam Leffler 		else if (tt == IPOPT_NOP)
109cc391cceSBruce M Simpson 			len = 1;
110cc391cceSBruce M Simpson 		else {
111ee67461eSJoseph Mingrone 			len = GET_U_1(cp + 1);
112c1ad1296SSam Leffler 			if (len < 2)
113c1ad1296SSam Leffler 				break;
114cc391cceSBruce M Simpson 		}
115ee67461eSJoseph Mingrone 		if (length < len)
116ee67461eSJoseph Mingrone 			goto trunc;
117ee67461eSJoseph Mingrone 		ND_TCHECK_LEN(cp, len);
118cc391cceSBruce M Simpson 		switch (tt) {
119cc391cceSBruce M Simpson 
120cc391cceSBruce M Simpson 		case IPOPT_SSRR:
121cc391cceSBruce M Simpson 		case IPOPT_LSRR:
122cc391cceSBruce M Simpson 			if (len < 7)
123c1ad1296SSam Leffler 				break;
124ee67461eSJoseph Mingrone 			return (GET_IPV4_TO_NETWORK_ORDER(cp + len - 4));
125cc391cceSBruce M Simpson 		}
126cc391cceSBruce M Simpson 	}
127cc391cceSBruce M Simpson trunc:
128ee67461eSJoseph Mingrone 	return (GET_IPV4_TO_NETWORK_ORDER(ip->ip_dst));
129cc391cceSBruce M Simpson }
130cc391cceSBruce M Simpson 
131cac3dcd5SXin LI /*
132cac3dcd5SXin LI  * Compute a V4-style checksum by building a pseudoheader.
133cac3dcd5SXin LI  */
134ee67461eSJoseph Mingrone uint16_t
nextproto4_cksum(netdissect_options * ndo,const struct ip * ip,const uint8_t * data,u_int len,u_int covlen,uint8_t next_proto)1353c602fabSXin LI nextproto4_cksum(netdissect_options *ndo,
1363c602fabSXin LI                  const struct ip *ip, const uint8_t *data,
137ee67461eSJoseph Mingrone                  u_int len, u_int covlen, uint8_t next_proto)
138cac3dcd5SXin LI {
139cac3dcd5SXin LI 	struct phdr {
1403c602fabSXin LI 		uint32_t src;
1413c602fabSXin LI 		uint32_t dst;
142ee67461eSJoseph Mingrone 		uint8_t mbz;
143ee67461eSJoseph Mingrone 		uint8_t proto;
1443c602fabSXin LI 		uint16_t len;
145cac3dcd5SXin LI 	} ph;
146cac3dcd5SXin LI 	struct cksum_vec vec[2];
147cac3dcd5SXin LI 
148cac3dcd5SXin LI 	/* pseudo-header.. */
1493c602fabSXin LI 	ph.len = htons((uint16_t)len);
150cac3dcd5SXin LI 	ph.mbz = 0;
151cac3dcd5SXin LI 	ph.proto = next_proto;
152ee67461eSJoseph Mingrone 	ph.src = GET_IPV4_TO_NETWORK_ORDER(ip->ip_src);
153cac3dcd5SXin LI 	if (IP_HL(ip) == 5)
154ee67461eSJoseph Mingrone 		ph.dst = GET_IPV4_TO_NETWORK_ORDER(ip->ip_dst);
155cac3dcd5SXin LI 	else
1563c602fabSXin LI 		ph.dst = ip_finddst(ndo, ip);
157cac3dcd5SXin LI 
1583c602fabSXin LI 	vec[0].ptr = (const uint8_t *)(void *)&ph;
159cac3dcd5SXin LI 	vec[0].len = sizeof(ph);
160cac3dcd5SXin LI 	vec[1].ptr = data;
1613c602fabSXin LI 	vec[1].len = covlen;
162cac3dcd5SXin LI 	return (in_cksum(vec, 2));
163cac3dcd5SXin LI }
164cac3dcd5SXin LI 
1650bff6a5aSEd Maste static int
ip_printts(netdissect_options * ndo,const u_char * cp,u_int length)1663c602fabSXin LI ip_printts(netdissect_options *ndo,
167ee67461eSJoseph Mingrone            const u_char *cp, u_int length)
168a88113a8SBill Fenner {
169ee67461eSJoseph Mingrone 	u_int ptr;
170ee67461eSJoseph Mingrone 	u_int len;
171ee67461eSJoseph Mingrone 	u_int hoplen;
172cc391cceSBruce M Simpson 	const char *type;
173a88113a8SBill Fenner 
174cc391cceSBruce M Simpson 	if (length < 4) {
175ee67461eSJoseph Mingrone 		ND_PRINT("[bad length %u]", length);
1760bff6a5aSEd Maste 		return (0);
177cc391cceSBruce M Simpson 	}
178ee67461eSJoseph Mingrone 	ND_PRINT(" TS{");
179ee67461eSJoseph Mingrone 	hoplen = ((GET_U_1(cp + 3) & 0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
180a88113a8SBill Fenner 	if ((length - 4) & (hoplen-1))
181ee67461eSJoseph Mingrone 		ND_PRINT("[bad length %u]", length);
182ee67461eSJoseph Mingrone 	ptr = GET_U_1(cp + 2) - 1;
183cc391cceSBruce M Simpson 	len = 0;
184a88113a8SBill Fenner 	if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
185ee67461eSJoseph Mingrone 		ND_PRINT("[bad ptr %u]", GET_U_1(cp + 2));
186ee67461eSJoseph Mingrone 	switch (GET_U_1(cp + 3)&0xF) {
187a88113a8SBill Fenner 	case IPOPT_TS_TSONLY:
188ee67461eSJoseph Mingrone 		ND_PRINT("TSONLY");
189a88113a8SBill Fenner 		break;
190a88113a8SBill Fenner 	case IPOPT_TS_TSANDADDR:
191ee67461eSJoseph Mingrone 		ND_PRINT("TS+ADDR");
192a88113a8SBill Fenner 		break;
193ee67461eSJoseph Mingrone 	case IPOPT_TS_PRESPEC:
194ee67461eSJoseph Mingrone 		ND_PRINT("PRESPEC");
195a88113a8SBill Fenner 		break;
196a88113a8SBill Fenner 	default:
197ee67461eSJoseph Mingrone 		ND_PRINT("[bad ts type %u]", GET_U_1(cp + 3)&0xF);
198a88113a8SBill Fenner 		goto done;
199a88113a8SBill Fenner 	}
200a88113a8SBill Fenner 
201a88113a8SBill Fenner 	type = " ";
202a88113a8SBill Fenner 	for (len = 4; len < length; len += hoplen) {
203a88113a8SBill Fenner 		if (ptr == len)
204a88113a8SBill Fenner 			type = " ^ ";
205ee67461eSJoseph Mingrone 		ND_TCHECK_LEN(cp + len, hoplen);
206ee67461eSJoseph Mingrone 		ND_PRINT("%s%u@%s", type, GET_BE_U_4(cp + len + hoplen - 4),
207ee67461eSJoseph Mingrone 			  hoplen!=8 ? "" : GET_IPADDR_STRING(cp + len));
208a88113a8SBill Fenner 		type = " ";
209a88113a8SBill Fenner 	}
210a88113a8SBill Fenner 
211a88113a8SBill Fenner done:
212ee67461eSJoseph Mingrone 	ND_PRINT("%s", ptr == len ? " ^ " : "");
213a88113a8SBill Fenner 
214ee67461eSJoseph Mingrone 	if (GET_U_1(cp + 3) >> 4)
215ee67461eSJoseph Mingrone 		ND_PRINT(" [%u hops not recorded]} ", GET_U_1(cp + 3)>>4);
216a88113a8SBill Fenner 	else
217ee67461eSJoseph Mingrone 		ND_PRINT("}");
2180bff6a5aSEd Maste 	return (0);
2190bff6a5aSEd Maste 
2200bff6a5aSEd Maste trunc:
2210bff6a5aSEd Maste 	return (-1);
222a88113a8SBill Fenner }
223a88113a8SBill Fenner 
2244edb46e9SPaul Traina /*
2254edb46e9SPaul Traina  * print IP options.
226ee67461eSJoseph Mingrone    If truncated return -1, else 0.
2274edb46e9SPaul Traina  */
228ee67461eSJoseph Mingrone static int
ip_optprint(netdissect_options * ndo,const u_char * cp,u_int length)2293c602fabSXin LI ip_optprint(netdissect_options *ndo,
230ee67461eSJoseph Mingrone             const u_char *cp, u_int length)
2314edb46e9SPaul Traina {
232ee67461eSJoseph Mingrone 	u_int option_len;
233abf25193SMax Laier 	const char *sep = "";
2344edb46e9SPaul Traina 
235c1ad1296SSam Leffler 	for (; length > 0; cp += option_len, length -= option_len) {
236c1ad1296SSam Leffler 		u_int option_code;
2374edb46e9SPaul Traina 
238ee67461eSJoseph Mingrone 		ND_PRINT("%s", sep);
239abf25193SMax Laier 		sep = ",";
240abf25193SMax Laier 
241ee67461eSJoseph Mingrone 		option_code = GET_U_1(cp);
242c1ad1296SSam Leffler 
243ee67461eSJoseph Mingrone 		ND_PRINT("%s",
244ee67461eSJoseph Mingrone 		          tok2str(ip_option_values,"unknown %u",option_code));
245abf25193SMax Laier 
246c1ad1296SSam Leffler 		if (option_code == IPOPT_NOP ||
247c1ad1296SSam Leffler                     option_code == IPOPT_EOL)
248c1ad1296SSam Leffler 			option_len = 1;
249c1ad1296SSam Leffler 
250943ee2b1SBill Fenner 		else {
251ee67461eSJoseph Mingrone 			option_len = GET_U_1(cp + 1);
252abf25193SMax Laier 			if (option_len < 2) {
253ee67461eSJoseph Mingrone 				ND_PRINT(" [bad length %u]", option_len);
254ee67461eSJoseph Mingrone 				return 0;
255abf25193SMax Laier 			}
2564edb46e9SPaul Traina 		}
2574edb46e9SPaul Traina 
258abf25193SMax Laier 		if (option_len > length) {
259ee67461eSJoseph Mingrone 			ND_PRINT(" [bad length %u]", option_len);
260ee67461eSJoseph Mingrone 			return 0;
261abf25193SMax Laier 		}
262c1ad1296SSam Leffler 
263ee67461eSJoseph Mingrone 		ND_TCHECK_LEN(cp, option_len);
264c1ad1296SSam Leffler 
265c1ad1296SSam Leffler 		switch (option_code) {
2664edb46e9SPaul Traina 		case IPOPT_EOL:
267ee67461eSJoseph Mingrone 			return 0;
2684edb46e9SPaul Traina 
2694edb46e9SPaul Traina 		case IPOPT_TS:
2700bff6a5aSEd Maste 			if (ip_printts(ndo, cp, option_len) == -1)
2710bff6a5aSEd Maste 				goto trunc;
2724edb46e9SPaul Traina 			break;
2734edb46e9SPaul Traina 
274c1ad1296SSam Leffler 		case IPOPT_RR:       /* fall through */
2754edb46e9SPaul Traina 		case IPOPT_SSRR:
2764edb46e9SPaul Traina 		case IPOPT_LSRR:
2770bff6a5aSEd Maste 			if (ip_printroute(ndo, cp, option_len) == -1)
2780bff6a5aSEd Maste 				goto trunc;
2794edb46e9SPaul Traina 			break;
2804edb46e9SPaul Traina 
2819cc97c3aSPaul Traina 		case IPOPT_RA:
282abf25193SMax Laier 			if (option_len < 4) {
283ee67461eSJoseph Mingrone 				ND_PRINT(" [bad length %u]", option_len);
284abf25193SMax Laier 				break;
285abf25193SMax Laier 			}
286ee67461eSJoseph Mingrone 			ND_TCHECK_1(cp + 3);
287ee67461eSJoseph Mingrone 			if (GET_BE_U_2(cp + 2) != 0)
288ee67461eSJoseph Mingrone 				ND_PRINT(" value %u", GET_BE_U_2(cp + 2));
2899cc97c3aSPaul Traina 			break;
2909cc97c3aSPaul Traina 
291c1ad1296SSam Leffler 		case IPOPT_NOP:       /* nothing to print - fall through */
292c1ad1296SSam Leffler 		case IPOPT_SECURITY:
2934edb46e9SPaul Traina 		default:
2944edb46e9SPaul Traina 			break;
2954edb46e9SPaul Traina 		}
2964edb46e9SPaul Traina 	}
297ee67461eSJoseph Mingrone 	return 0;
298cc391cceSBruce M Simpson 
299cc391cceSBruce M Simpson trunc:
300ee67461eSJoseph Mingrone 	return -1;
3014edb46e9SPaul Traina }
3024edb46e9SPaul Traina 
303cc391cceSBruce M Simpson #define IP_RES 0x8000
304cc391cceSBruce M Simpson 
3053c602fabSXin LI static const struct tok ip_frag_values[] = {
306cc391cceSBruce M Simpson         { IP_MF,        "+" },
307cc391cceSBruce M Simpson         { IP_DF,        "DF" },
308cc391cceSBruce M Simpson 	{ IP_RES,       "rsvd" }, /* The RFC3514 evil ;-) bit */
309cc391cceSBruce M Simpson         { 0,            NULL }
310cc391cceSBruce M Simpson };
311cc391cceSBruce M Simpson 
312c1ad1296SSam Leffler 
313cc391cceSBruce M Simpson /*
3144edb46e9SPaul Traina  * print an IP datagram.
3154edb46e9SPaul Traina  */
3164edb46e9SPaul Traina void
ip_print(netdissect_options * ndo,const u_char * bp,const u_int length)317c1ad1296SSam Leffler ip_print(netdissect_options *ndo,
318c1ad1296SSam Leffler 	 const u_char *bp,
319*0a7e5f1fSJoseph Mingrone 	 const u_int length)
3204edb46e9SPaul Traina {
321ee67461eSJoseph Mingrone 	const struct ip *ip;
322ee67461eSJoseph Mingrone 	u_int off;
323c1ad1296SSam Leffler 	u_int hlen;
324ee67461eSJoseph Mingrone 	u_int len;
325cac3dcd5SXin LI 	struct cksum_vec vec[1];
326ee67461eSJoseph Mingrone 	uint8_t ip_tos, ip_ttl, ip_proto;
3273c602fabSXin LI 	uint16_t sum, ip_sum;
3280bff6a5aSEd Maste 	const char *p_name;
329ee67461eSJoseph Mingrone 	int truncated = 0;
330*0a7e5f1fSJoseph Mingrone 	int presumed_tso = 0;
3314edb46e9SPaul Traina 
332ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "ip";
333ee67461eSJoseph Mingrone 	ip = (const struct ip *)bp;
3344edb46e9SPaul Traina 
335*0a7e5f1fSJoseph Mingrone 	if (!ndo->ndo_eflag) {
336*0a7e5f1fSJoseph Mingrone 		nd_print_protocol_caps(ndo);
337*0a7e5f1fSJoseph Mingrone 		ND_PRINT(" ");
3384edb46e9SPaul Traina 	}
339*0a7e5f1fSJoseph Mingrone 
340*0a7e5f1fSJoseph Mingrone 	ND_ICHECK_ZU(length, <, sizeof (struct ip));
341*0a7e5f1fSJoseph Mingrone 	ND_ICHECKMSG_U("version", IP_V(ip), !=, 4);
342*0a7e5f1fSJoseph Mingrone 
343ee67461eSJoseph Mingrone 	hlen = IP_HL(ip) * 4;
344*0a7e5f1fSJoseph Mingrone 	ND_ICHECKMSG_ZU("header length", hlen, <, sizeof (struct ip));
3454edb46e9SPaul Traina 
346ee67461eSJoseph Mingrone 	len = GET_BE_U_2(ip->ip_len);
347*0a7e5f1fSJoseph Mingrone 	if (len > length) {
348*0a7e5f1fSJoseph Mingrone 		ND_PRINT("[total length %u > length %u]", len, length);
349*0a7e5f1fSJoseph Mingrone 		nd_print_invalid(ndo);
350*0a7e5f1fSJoseph Mingrone 		ND_PRINT(" ");
351cc391cceSBruce M Simpson 	}
352*0a7e5f1fSJoseph Mingrone 	if (len == 0) {
353c1ad1296SSam Leffler 		/* we guess that it is a TSO send */
354ee67461eSJoseph Mingrone 		len = length;
355*0a7e5f1fSJoseph Mingrone 		presumed_tso = 1;
356*0a7e5f1fSJoseph Mingrone 	} else
357*0a7e5f1fSJoseph Mingrone 		ND_ICHECKMSG_U("total length", len, <, hlen);
358cc391cceSBruce M Simpson 
359*0a7e5f1fSJoseph Mingrone 	ND_TCHECK_SIZE(ip);
360cc391cceSBruce M Simpson 	/*
361cc391cceSBruce M Simpson 	 * Cut off the snapshot length to the end of the IP payload.
362cc391cceSBruce M Simpson 	 */
363ee67461eSJoseph Mingrone 	if (!nd_push_snaplen(ndo, bp, len)) {
364ee67461eSJoseph Mingrone 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
365ee67461eSJoseph Mingrone 			"%s: can't push snaplen on buffer stack", __func__);
366ee67461eSJoseph Mingrone 	}
367cc391cceSBruce M Simpson 
368ee67461eSJoseph Mingrone 	len -= hlen;
3694edb46e9SPaul Traina 
370ee67461eSJoseph Mingrone 	off = GET_BE_U_2(ip->ip_off);
371ee67461eSJoseph Mingrone 
372ee67461eSJoseph Mingrone         ip_proto = GET_U_1(ip->ip_p);
373cc391cceSBruce M Simpson 
3743c602fabSXin LI         if (ndo->ndo_vflag) {
375ee67461eSJoseph Mingrone             ip_tos = GET_U_1(ip->ip_tos);
376ee67461eSJoseph Mingrone             ND_PRINT("(tos 0x%x", ip_tos);
377cc391cceSBruce M Simpson             /* ECN bits */
378ee67461eSJoseph Mingrone             switch (ip_tos & 0x03) {
3793340d773SGleb Smirnoff 
3803340d773SGleb Smirnoff             case 0:
3813340d773SGleb Smirnoff                 break;
3823340d773SGleb Smirnoff 
383cc391cceSBruce M Simpson             case 1:
384ee67461eSJoseph Mingrone                 ND_PRINT(",ECT(1)");
385cc391cceSBruce M Simpson                 break;
3863340d773SGleb Smirnoff 
387cc391cceSBruce M Simpson             case 2:
388ee67461eSJoseph Mingrone                 ND_PRINT(",ECT(0)");
389cc391cceSBruce M Simpson                 break;
3903340d773SGleb Smirnoff 
391cc391cceSBruce M Simpson             case 3:
392ee67461eSJoseph Mingrone                 ND_PRINT(",CE");
3933340d773SGleb Smirnoff                 break;
394cc391cceSBruce M Simpson             }
395cc391cceSBruce M Simpson 
396ee67461eSJoseph Mingrone             ip_ttl = GET_U_1(ip->ip_ttl);
397ee67461eSJoseph Mingrone             if (ip_ttl >= 1)
398ee67461eSJoseph Mingrone                 ND_PRINT(", ttl %u", ip_ttl);
399cc391cceSBruce M Simpson 
400cc391cceSBruce M Simpson 	    /*
401cc391cceSBruce M Simpson 	     * for the firewall guys, print id, offset.
402cc391cceSBruce M Simpson              * On all but the last stick a "+" in the flags portion.
403cc391cceSBruce M Simpson 	     * For unfragmented datagrams, note the don't fragment flag.
404cc391cceSBruce M Simpson 	     */
405ee67461eSJoseph Mingrone 	    ND_PRINT(", id %u, offset %u, flags [%s], proto %s (%u)",
406ee67461eSJoseph Mingrone                          GET_BE_U_2(ip->ip_id),
407ee67461eSJoseph Mingrone                          (off & IP_OFFMASK) * 8,
408ee67461eSJoseph Mingrone                          bittok2str(ip_frag_values, "none", off & (IP_RES|IP_DF|IP_MF)),
409ee67461eSJoseph Mingrone                          tok2str(ipproto_values, "unknown", ip_proto),
410ee67461eSJoseph Mingrone                          ip_proto);
411cc391cceSBruce M Simpson 
412*0a7e5f1fSJoseph Mingrone 	    if (presumed_tso)
413*0a7e5f1fSJoseph Mingrone                 ND_PRINT(", length %u [was 0, presumed TSO]", length);
414*0a7e5f1fSJoseph Mingrone 	    else
415ee67461eSJoseph Mingrone                 ND_PRINT(", length %u", GET_BE_U_2(ip->ip_len));
416cc391cceSBruce M Simpson 
417cc391cceSBruce M Simpson             if ((hlen - sizeof(struct ip)) > 0) {
418ee67461eSJoseph Mingrone                 ND_PRINT(", options (");
419ee67461eSJoseph Mingrone                 if (ip_optprint(ndo, (const u_char *)(ip + 1),
420ee67461eSJoseph Mingrone                     hlen - sizeof(struct ip)) == -1) {
421ee67461eSJoseph Mingrone                         ND_PRINT(" [truncated-option]");
422ee67461eSJoseph Mingrone 			truncated = 1;
423ee67461eSJoseph Mingrone                 }
424ee67461eSJoseph Mingrone                 ND_PRINT(")");
425cc391cceSBruce M Simpson             }
426cc391cceSBruce M Simpson 
427ee67461eSJoseph Mingrone 	    if (!ndo->ndo_Kflag && (const u_char *)ip + hlen <= ndo->ndo_snapend) {
428ee67461eSJoseph Mingrone 	        vec[0].ptr = (const uint8_t *)(const void *)ip;
429cac3dcd5SXin LI 	        vec[0].len = hlen;
430cac3dcd5SXin LI 	        sum = in_cksum(vec, 1);
431cc391cceSBruce M Simpson 		if (sum != 0) {
432ee67461eSJoseph Mingrone 		    ip_sum = GET_BE_U_2(ip->ip_sum);
433ee67461eSJoseph Mingrone 		    ND_PRINT(", bad cksum %x (->%x)!", ip_sum,
434ee67461eSJoseph Mingrone 			     in_cksum_shouldbe(ip_sum, sum));
435cc391cceSBruce M Simpson 		}
436cc391cceSBruce M Simpson 	    }
437cc391cceSBruce M Simpson 
438ee67461eSJoseph Mingrone 	    ND_PRINT(")\n    ");
439ee67461eSJoseph Mingrone 	    if (truncated) {
440ee67461eSJoseph Mingrone 		ND_PRINT("%s > %s: ",
441ee67461eSJoseph Mingrone 			 GET_IPADDR_STRING(ip->ip_src),
442ee67461eSJoseph Mingrone 			 GET_IPADDR_STRING(ip->ip_dst));
443ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
444ee67461eSJoseph Mingrone 		nd_pop_packet_info(ndo);
445ee67461eSJoseph Mingrone 		return;
446ee67461eSJoseph Mingrone 	    }
447cc391cceSBruce M Simpson 	}
448cc391cceSBruce M Simpson 
4494edb46e9SPaul Traina 	/*
4504edb46e9SPaul Traina 	 * If this is fragment zero, hand it to the next higher
451ee67461eSJoseph Mingrone 	 * level protocol.  Let them know whether there are more
452ee67461eSJoseph Mingrone 	 * fragments.
4534edb46e9SPaul Traina 	 */
454ee67461eSJoseph Mingrone 	if ((off & IP_OFFMASK) == 0) {
455ee67461eSJoseph Mingrone 		uint8_t nh = GET_U_1(ip->ip_p);
456a88113a8SBill Fenner 
457ee67461eSJoseph Mingrone 		if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
458ee67461eSJoseph Mingrone 		    nh != IPPROTO_SCTP && nh != IPPROTO_DCCP) {
459ee67461eSJoseph Mingrone 			ND_PRINT("%s > %s: ",
460ee67461eSJoseph Mingrone 				     GET_IPADDR_STRING(ip->ip_src),
461ee67461eSJoseph Mingrone 				     GET_IPADDR_STRING(ip->ip_dst));
462a88113a8SBill Fenner 		}
463ee67461eSJoseph Mingrone 		/*
464ee67461eSJoseph Mingrone 		 * Do a bounds check before calling ip_demux_print().
465ee67461eSJoseph Mingrone 		 * At least the header data is required.
466ee67461eSJoseph Mingrone 		 */
467ee67461eSJoseph Mingrone 		if (!ND_TTEST_LEN((const u_char *)ip, hlen)) {
468ee67461eSJoseph Mingrone 			ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
469ee67461eSJoseph Mingrone 				 ND_BYTES_AVAILABLE_AFTER((const u_char *)ip),
470ee67461eSJoseph Mingrone 				 hlen);
471ee67461eSJoseph Mingrone 			nd_trunc_longjmp(ndo);
472ee67461eSJoseph Mingrone 		}
473ee67461eSJoseph Mingrone 		ip_demux_print(ndo, (const u_char *)ip + hlen, len, 4,
474ee67461eSJoseph Mingrone 			       off & IP_MF, GET_U_1(ip->ip_ttl), nh, bp);
475cc391cceSBruce M Simpson 	} else {
4763340d773SGleb Smirnoff 		/*
4773340d773SGleb Smirnoff 		 * Ultra quiet now means that all this stuff should be
4783340d773SGleb Smirnoff 		 * suppressed.
4793340d773SGleb Smirnoff 		 */
480ee67461eSJoseph Mingrone 		if (ndo->ndo_qflag > 1) {
481ee67461eSJoseph Mingrone 			nd_pop_packet_info(ndo);
4823340d773SGleb Smirnoff 			return;
483ee67461eSJoseph Mingrone 		}
484a88113a8SBill Fenner 
4854edb46e9SPaul Traina 		/*
4863340d773SGleb Smirnoff 		 * This isn't the first frag, so we're missing the
4870e0def19SBill Fenner 		 * next level protocol header.  print the ip addr
4880e0def19SBill Fenner 		 * and the protocol.
4894edb46e9SPaul Traina 		 */
490ee67461eSJoseph Mingrone 		ND_PRINT("%s > %s:", GET_IPADDR_STRING(ip->ip_src),
491ee67461eSJoseph Mingrone 		          GET_IPADDR_STRING(ip->ip_dst));
492ee67461eSJoseph Mingrone 		if (!ndo->ndo_nflag && (p_name = netdb_protoname(ip_proto)) != NULL)
493ee67461eSJoseph Mingrone 			ND_PRINT(" %s", p_name);
4940e0def19SBill Fenner 		else
495ee67461eSJoseph Mingrone 			ND_PRINT(" ip-proto-%u", ip_proto);
4960e0def19SBill Fenner 	}
497ee67461eSJoseph Mingrone 	nd_pop_packet_info(ndo);
49820869109SPedro F. Giffuni 	return;
49920869109SPedro F. Giffuni 
50020869109SPedro F. Giffuni trunc:
501ee67461eSJoseph Mingrone 	nd_print_trunc(ndo);
502*0a7e5f1fSJoseph Mingrone 	return;
503*0a7e5f1fSJoseph Mingrone 
504*0a7e5f1fSJoseph Mingrone invalid:
505*0a7e5f1fSJoseph Mingrone 	nd_print_invalid(ndo);
5064edb46e9SPaul Traina }
507943ee2b1SBill Fenner 
508943ee2b1SBill Fenner void
ipN_print(netdissect_options * ndo,const u_char * bp,u_int length)509ee67461eSJoseph Mingrone ipN_print(netdissect_options *ndo, const u_char *bp, u_int length)
510943ee2b1SBill Fenner {
511ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "ipn";
5123340d773SGleb Smirnoff 	if (length < 1) {
513ee67461eSJoseph Mingrone 		ND_PRINT("truncated-ip %u", length);
514943ee2b1SBill Fenner 		return;
515943ee2b1SBill Fenner 	}
5163340d773SGleb Smirnoff 
517ee67461eSJoseph Mingrone 	switch (GET_U_1(bp) & 0xF0) {
5183340d773SGleb Smirnoff 	case 0x40:
5193c602fabSXin LI 		ip_print(ndo, bp, length);
5203340d773SGleb Smirnoff 		break;
5213340d773SGleb Smirnoff 	case 0x60:
5223c602fabSXin LI 		ip6_print(ndo, bp, length);
5233340d773SGleb Smirnoff 		break;
524943ee2b1SBill Fenner 	default:
525ee67461eSJoseph Mingrone 		ND_PRINT("unknown ip %u", (GET_U_1(bp) & 0xF0) >> 4);
5263340d773SGleb Smirnoff 		break;
527943ee2b1SBill Fenner 	}
528943ee2b1SBill Fenner }
529