xref: /openbsd-src/usr.sbin/tcpdump/print-ip6opts.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: print-ip6opts.c,v 1.5 2015/11/16 00:16:39 mmcc Exp $	*/
2 
3 /*
4  * Copyright (C) 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifdef INET6
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/ip6.h>
39 
40 #include <stdio.h>
41 
42 #include "interface.h"
43 #include "addrtoname.h"
44 
45 void
46 ip6_opt_print(const u_char *bp, int len)
47 {
48     int i;
49     int optlen;
50 
51     for (i = 0; i < len; i += optlen) {
52 	switch (bp[i]) {
53 	case IP6OPT_PAD1:
54 	    optlen = 1;
55 	    break;
56 	case IP6OPT_PADN:
57 	    if (len - i < IP6OPT_MINLEN) {
58 		printf("(padn: trunc)");
59 		goto trunc;
60 	    }
61 	    optlen = bp[i + 1] + 2;
62 	    break;
63 	case IP6OPT_ROUTER_ALERT:
64 	    if (len - i < IP6OPT_RTALERT_LEN) {
65 		printf("(rtalert: trunc)");
66 		goto trunc;
67 	    }
68 	    if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) {
69 		printf("(rtalert: invalid len %d)", bp[i + 1]);
70 		goto trunc;
71 	    }
72 	    printf("(rtalert: 0x%04x) ", ntohs(*(u_short *)&bp[i + 2]));
73 	    optlen = IP6OPT_RTALERT_LEN;
74 	    break;
75 	case IP6OPT_JUMBO:
76 	    if (len - i < IP6OPT_JUMBO_LEN) {
77 		printf("(jumbo: trunc)");
78 		goto trunc;
79 	    }
80 	    if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) {
81 		printf("(jumbo: invalid len %d)", bp[i + 1]);
82 		goto trunc;
83 	    }
84 	    printf("(jumbo: %u) ", (u_int32_t)ntohl(*(u_int *)&bp[i + 2]));
85 	    optlen = IP6OPT_JUMBO_LEN;
86 	    break;
87 	default:
88 	    if (len - i < IP6OPT_MINLEN) {
89 		printf("(type %d: trunc)", bp[i]);
90 		goto trunc;
91 	    }
92 	    printf("(type 0x%02x: len=%d) ", bp[i], bp[i + 1]);
93 	    optlen = bp[i + 1] + 2;
94 	    break;
95 	}
96     }
97 
98 #if 0
99 end:
100 #endif
101     return;
102 
103 trunc:
104     printf("[trunc] ");
105 }
106 
107 int
108 hbhopt_print(const u_char *bp)
109 {
110     const struct ip6_hbh *dp = (struct ip6_hbh *)bp;
111     const u_char *ep;
112     int hbhlen = 0;
113 
114     /* 'ep' points to the end of avaible data. */
115     ep = snapend;
116     TCHECK(dp->ip6h_len);
117     hbhlen = (int)((dp->ip6h_len + 1) << 3);
118     TCHECK2(*dp, hbhlen);
119     printf("HBH ");
120     if (vflag)
121 	ip6_opt_print((const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp));
122 
123     return(hbhlen);
124 
125   trunc:
126     fputs("[|HBH]", stdout);
127     return(hbhlen);
128 }
129 
130 int
131 dstopt_print(const u_char *bp)
132 {
133     const struct ip6_dest *dp = (struct ip6_dest *)bp;
134     const u_char *ep;
135     int dstoptlen = 0;
136 
137     /* 'ep' points to the end of avaible data. */
138     ep = snapend;
139     TCHECK(dp->ip6d_len);
140     dstoptlen = (int)((dp->ip6d_len + 1) << 3);
141     TCHECK2(*dp, dstoptlen);
142     printf("DSTOPT ");
143     if (vflag) {
144 	ip6_opt_print((const u_char *)dp + sizeof(*dp),
145 	    dstoptlen - sizeof(*dp));
146     }
147 
148     return(dstoptlen);
149 
150   trunc:
151     fputs("[|DSTOPT]", stdout);
152     return(dstoptlen);
153 }
154 #endif /* INET6 */
155