xref: /netbsd-src/external/bsd/tcpdump/dist/print-frag6.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
10f74e101Schristos /*
20f74e101Schristos  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994
30f74e101Schristos  *	The Regents of the University of California.  All rights reserved.
40f74e101Schristos  *
50f74e101Schristos  * Redistribution and use in source and binary forms, with or without
60f74e101Schristos  * modification, are permitted provided that: (1) source code distributions
70f74e101Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
80f74e101Schristos  * distributions including binary code include the above copyright notice and
90f74e101Schristos  * this paragraph in its entirety in the documentation or other materials
100f74e101Schristos  * provided with the distribution, and (3) all advertising materials mentioning
110f74e101Schristos  * features or use of this software display the following acknowledgement:
120f74e101Schristos  * ``This product includes software developed by the University of California,
130f74e101Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
140f74e101Schristos  * the University nor the names of its contributors may be used to endorse
150f74e101Schristos  * or promote products derived from this software without specific prior
160f74e101Schristos  * written permission.
170f74e101Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
180f74e101Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
190f74e101Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
200f74e101Schristos  */
210f74e101Schristos 
2211b3aaa1Schristos #include <sys/cdefs.h>
230f74e101Schristos #ifndef lint
24*26ba0b50Schristos __RCSID("$NetBSD: print-frag6.c,v 1.9 2024/09/02 16:15:31 christos Exp $");
250f74e101Schristos #endif
260f74e101Schristos 
27dc860a36Sspz /* \summary: IPv6 fragmentation header printer */
28dc860a36Sspz 
29c74ad251Schristos #include <config.h>
300f74e101Schristos 
31c74ad251Schristos #include "netdissect-stdinc.h"
320f74e101Schristos 
33fdccd7e4Schristos #include "netdissect.h"
340f74e101Schristos #include "extract.h"
350f74e101Schristos 
3672c96ff3Schristos #include "ip6.h"
3772c96ff3Schristos 
380f74e101Schristos int
39c74ad251Schristos frag6_print(netdissect_options *ndo, const u_char *bp, const u_char *bp2)
400f74e101Schristos {
41c74ad251Schristos 	const struct ip6_frag *dp;
42c74ad251Schristos 	const struct ip6_hdr *ip6;
430f74e101Schristos 
44c74ad251Schristos 	ndo->ndo_protocol = "frag6";
450f74e101Schristos 	dp = (const struct ip6_frag *)bp;
460f74e101Schristos 	ip6 = (const struct ip6_hdr *)bp2;
470f74e101Schristos 
48*26ba0b50Schristos 	ND_PRINT("frag (");
49*26ba0b50Schristos 	if (ndo->ndo_vflag)
50*26ba0b50Schristos 		ND_PRINT("0x%08x:", GET_BE_U_4(dp->ip6f_ident));
51*26ba0b50Schristos 	ND_PRINT("%u|", GET_BE_U_2(dp->ip6f_offlg) & IP6F_OFF_MASK);
52*26ba0b50Schristos 	if ((bp - bp2) + sizeof(struct ip6_frag) >
53*26ba0b50Schristos 	    sizeof(struct ip6_hdr) + GET_BE_U_2(ip6->ip6_plen))
54*26ba0b50Schristos 		ND_PRINT("[length < 0] (invalid))");
55*26ba0b50Schristos 	else
56*26ba0b50Schristos 		ND_PRINT("%zu)",
57c74ad251Schristos 			 sizeof(struct ip6_hdr) + GET_BE_U_2(ip6->ip6_plen) -
58c74ad251Schristos 			 (bp - bp2) - sizeof(struct ip6_frag));
590f74e101Schristos 
600f74e101Schristos 	/* it is meaningless to decode non-first fragment */
61c74ad251Schristos 	if ((GET_BE_U_2(dp->ip6f_offlg) & IP6F_OFF_MASK) != 0)
620f74e101Schristos 		return -1;
63*26ba0b50Schristos 	else {
64c74ad251Schristos 		ND_PRINT(" ");
650f74e101Schristos 		return sizeof(struct ip6_frag);
660f74e101Schristos 	}
670f74e101Schristos }
68