Projet

Général

Profil

Télécharger (8,33 ko) Statistiques
| Branche: | Révision:

univnautes-tools / pfPorts / filterlog / files / print-ip6.c @ 8135339c

1
/*
2
 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
3
 *	The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that: (1) source code distributions
7
 * retain the above copyright notice and this paragraph in its entirety, (2)
8
 * distributions including binary code include the above copyright notice and
9
 * this paragraph in its entirety in the documentation or other materials
10
 * provided with the distribution, and (3) all advertising materials mentioning
11
 * features or use of this software display the following acknowledgement:
12
 * ``This product includes software developed by the University of California,
13
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14
 * the University nor the names of its contributors may be used to endorse
15
 * or promote products derived from this software without specific prior
16
 * written permission.
17
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
 *
21
 * $FreeBSD$
22
 */
23

    
24
#include <sys/types.h>
25
#include <sys/socket.h>
26

    
27
#include <netinet/in.h>
28
#include <netinet/ip6.h>
29
#include <arpa/inet.h>
30

    
31
#include <netinet/udp.h>
32

    
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36

    
37
#include "common.h"
38

    
39
static int
40
rt6_print(struct sbuf *sbuf, register const u_char *bp, int datalen)
41
{
42
	register const struct ip6_rthdr *dp;
43
	register const struct ip6_rthdr0 *dp0;
44
	register const u_char *ep;
45
	int i, len;
46
	register const struct in6_addr *addr;
47
	char ip6addr[INET6_ADDRSTRLEN];
48

    
49
	dp = (const struct ip6_rthdr *)bp;
50
	len = dp->ip6r_len;
51

    
52
	/* 'ep' points to the end of available data. */
53
	ep = bp + datalen;
54

    
55
	sbuf_printf(sbuf, "%d,%d,%d,", dp->ip6r_len, dp->ip6r_type, dp->ip6r_segleft);	/*)*/
56

    
57
	switch (dp->ip6r_type) {
58
#ifndef IPV6_RTHDR_TYPE_0
59
#define IPV6_RTHDR_TYPE_0 0
60
#endif
61
#ifndef IPV6_RTHDR_TYPE_2
62
#define IPV6_RTHDR_TYPE_2 2
63
#endif
64
	case IPV6_RTHDR_TYPE_0:
65
	case IPV6_RTHDR_TYPE_2:			/* Mobile IPv6 ID-20 */
66
		dp0 = (const struct ip6_rthdr0 *)dp;
67

    
68
		sbuf_printf(sbuf, "0x%0x,",
69
			    EXTRACT_32BITS(&dp0->ip6r0_reserved));
70

    
71
		if (len % 2 == 1)
72
			goto trunc;
73
		len >>= 1;
74
		addr = (const struct in6_addr *)(dp0+1);
75
		for (i = 0; i < len; i++) {
76
			if ((const u_char *)(addr + 1) > ep)
77
				goto trunc;
78

    
79
			memset(ip6addr, 0, INET6_ADDRSTRLEN);
80
			sbuf_printf(sbuf, "%d, %s", i, inet_ntop(AF_INET6, addr, ip6addr, INET6_ADDRSTRLEN));
81
			addr++;
82
		}
83
		/*(*/
84
		return((dp0->ip6r0_len + 1) << 3);
85
		break;
86
	default:
87
		goto trunc;
88
		break;
89
	}
90

    
91
 trunc:
92
	sbuf_printf(sbuf, "TRUNC,[|srcrt],");
93
	return -1;
94
}
95

    
96
static int
97
frag6_print(struct sbuf *sbuf, register const u_char *bp, register const u_char *bp2)
98
{
99
	register const struct ip6_frag *dp;
100
	register const struct ip6_hdr *ip6;
101

    
102
	dp = (const struct ip6_frag *)bp;
103
	ip6 = (const struct ip6_hdr *)bp2;
104

    
105

    
106
	sbuf_printf(sbuf, "FRAG6, 0x%08x:%d|%ld,",
107
		       EXTRACT_32BITS(&dp->ip6f_ident),
108
		       EXTRACT_16BITS(&dp->ip6f_offlg) & IP6F_OFF_MASK,
109
		       sizeof(struct ip6_hdr) + EXTRACT_16BITS(&ip6->ip6_plen) -
110
			       (long)(bp - bp2) - sizeof(struct ip6_frag));
111

    
112
#if 1
113
	/* it is meaningless to decode non-first fragment */
114
	if ((EXTRACT_16BITS(&dp->ip6f_offlg) & IP6F_OFF_MASK) != 0)
115
		return -1;
116
	else
117
#endif
118
	{
119
		sbuf_printf(sbuf, ",");
120
		return sizeof(struct ip6_frag);
121
	}
122
}
123

    
124
/*
125
 * Compute a V6-style checksum by building a pseudoheader.
126
 */
127
#if 0
128
int
129
nextproto6_cksum(const struct ip6_hdr *ip6, const u_int8_t *data,
130
		 u_int len, u_int next_proto)
131
{
132
        struct {
133
                struct in6_addr ph_src;
134
                struct in6_addr ph_dst;
135
                u_int32_t       ph_len;
136
                u_int8_t        ph_zero[3];
137
                u_int8_t        ph_nxt;
138
        } ph;
139
        struct cksum_vec vec[2];
140

    
141
        /* pseudo-header */
142
        memset(&ph, 0, sizeof(ph));
143
        ph.ph_src = ip6->ip6_src;
144
        ph.ph_dst = ip6->ip6_dst;
145
        ph.ph_len = htonl(len);
146
        ph.ph_nxt = next_proto;
147

    
148
        vec[0].ptr = (const u_int8_t *)(void *)&ph;
149
        vec[0].len = sizeof(ph);
150
        vec[1].ptr = data;
151
        vec[1].len = len;
152

    
153
        return in_cksum(vec, 2);
154
}
155
#endif
156

    
157
/*
158
 * print an IP6 datagram.
159
 */
160
void
161
ip6_print(struct sbuf *sbuf, const u_char *bp, u_int length)
162
{
163
	register const struct ip6_hdr *ip6;
164
	register int advance;
165
	u_int len;
166
	const u_char *ipend;
167
	register const u_char *cp;
168
	register u_int payload_len;
169
	int nh;
170
	int fragmented = 0;
171
	u_int flow;
172

    
173
	ip6 = (const struct ip6_hdr *)bp;
174
	sbuf_printf(sbuf, "6,");
175

    
176
	if (length < sizeof (struct ip6_hdr)) {
177
		sbuf_printf(sbuf, "truncated-ip6=%u,", length);
178
		return;
179
	}
180

    
181
	payload_len = EXTRACT_16BITS(&ip6->ip6_plen);
182
	len = payload_len + sizeof(struct ip6_hdr);
183
	if (length < len)
184
		sbuf_printf(sbuf, "error='truncated-ip6 - %u bytes missing!',",
185
			len - length);
186

    
187
	flow = EXTRACT_32BITS(&ip6->ip6_flow);
188
#if 0
189
	/* rfc1883 */
190
	if (flow & 0x0f000000)
191
	(void)ND_PRINT((ndo, "pri 0x%02x, ", (flow & 0x0f000000) >> 24));
192
	if (flow & 0x00ffffff)
193
	(void)ND_PRINT((ndo, "flowlabel 0x%06x, ", flow & 0x00ffffff));
194
#else
195
	/* RFC 2460 */
196
	sbuf_printf(sbuf, "0x%02x,", (flow & 0x0ff00000) >> 20);
197
	sbuf_printf(sbuf, "0x%05x,", flow & 0x000fffff);
198
#endif
199

    
200
	sbuf_printf(sbuf, "%u,%s,%u,%u,",
201
		ip6->ip6_hlim,
202
		code2str(ipproto_values, "unknown", ip6->ip6_nxt),
203
		ip6->ip6_nxt, payload_len);
204
	
205

    
206
	/*
207
	 * Cut off the snapshot length to the end of the IP payload.
208
	 */
209
	ipend = bp + len;
210

    
211
	cp = (const u_char *)ip6;
212
	advance = sizeof(struct ip6_hdr);
213
	nh = ip6->ip6_nxt;
214
	while (cp < ipend && advance > 0) {
215
		char ip6addr[INET6_ADDRSTRLEN];
216

    
217
		cp += advance;
218
		len -= advance;
219

    
220
		if (cp == (const u_char *)(ip6 + 1)) {
221
			sbuf_printf(sbuf, "%s,", inet_ntop(AF_INET6, &ip6->ip6_src, ip6addr, INET6_ADDRSTRLEN));
222
			sbuf_printf(sbuf, "%s,", inet_ntop(AF_INET6, &ip6->ip6_dst, ip6addr, INET6_ADDRSTRLEN));
223
		}
224

    
225
		switch (nh) {
226
		case IPPROTO_HOPOPTS:
227
			advance = hbhopt_print(sbuf, cp);
228
			nh = *cp;
229
			break;
230
		case IPPROTO_DSTOPTS:
231
			advance = dstopt_print(sbuf, cp);
232
			nh = *cp;
233
			break;
234
		case IPPROTO_FRAGMENT:
235
			advance = frag6_print(sbuf, cp, (const u_char *)ip6);
236
			nh = *cp;
237
			fragmented = 1;
238
			break;
239

    
240
		case IPPROTO_MOBILITY_OLD:
241
		case IPPROTO_MOBILITY:
242
			/*
243
			 * XXX - we don't use "advance"; the current
244
			 * "Mobility Support in IPv6" draft
245
			 * (draft-ietf-mobileip-ipv6-24) says that
246
			 * the next header field in a mobility header
247
			 * should be IPPROTO_NONE, but speaks of
248
			 * the possiblity of a future extension in
249
			 * which payload can be piggybacked atop a
250
			 * mobility header.
251
			 */
252
			sbuf_printf(sbuf, "MOBILITY,");
253
			advance = mobility_print(sbuf, cp, len);
254
			nh = *cp;
255
			return;
256
		case IPPROTO_ROUTING:
257
			advance = rt6_print(sbuf, cp, len);
258
			nh = *cp;
259
			break;
260
#if 0
261
		case IPPROTO_SCTP:
262
			sctp_print(cp, (const u_char *)ip6, len);
263
			return;
264
		case IPPROTO_DCCP:
265
			dccp_print(cp, (const u_char *)ip6, len);
266
			return;
267
#endif
268
		case IPPROTO_TCP:
269
			tcp_print(sbuf, cp, len, (const u_char *)ip6);
270
			return;
271
		case IPPROTO_UDP:
272
		{
273
			const struct udphdr *up;
274

    
275
			up = (const struct udphdr *)cp;
276
			sbuf_printf(sbuf, "%d,%d,%d", EXTRACT_16BITS(&up->uh_sport), EXTRACT_16BITS(&up->uh_dport),
277
				EXTRACT_16BITS(&up->uh_ulen));
278

    
279
		}
280
			return;
281
#if 0
282
		case IPPROTO_ICMPV6:
283
			icmp6_print(ndo, cp, len, (const u_char *)ip6, fragmented);
284
			return;
285
		case IPPROTO_AH:
286
			advance = ah_print(cp);
287
			nh = *cp;
288
			break;
289
		case IPPROTO_ESP:
290
		    {
291
			int enh, padlen;
292
			advance = esp_print(ndo, cp, len, (const u_char *)ip6, &enh, &padlen);
293
			nh = enh & 0xff;
294
			len -= padlen;
295
			break;
296
		    }
297
		case IPPROTO_IPCOMP:
298
		    {
299
			int enh;
300
			advance = ipcomp_print(cp, &enh);
301
			nh = enh & 0xff;
302
			break;
303
		    }
304

    
305
		case IPPROTO_PIM:
306
			pim_print(cp, len, nextproto6_cksum(ip6, cp, len,
307
							    IPPROTO_PIM));
308
			return;
309

    
310
		case IPPROTO_OSPF:
311
			ospf6_print(cp, len);
312
			return;
313
#endif
314
		case IPPROTO_IPV6:
315
			sbuf_printf(sbuf, "IPV6-IN-IPV6,");
316
			//ip6_print(sbuf, cp, len);
317
			return;
318

    
319
		case IPPROTO_IPV4:
320
			sbuf_printf(sbuf, "IPV4-IN-IPV6,");
321
		        //ip_print(sbuf, cp, len);
322
			return;
323

    
324
#if 0
325
                case IPPROTO_PGM:
326
                        pgm_print(cp, len, (const u_char *)ip6);
327
                        return;
328

    
329
		case IPPROTO_GRE:
330
			gre_print(cp, len);
331
			return;
332

    
333
		case IPPROTO_RSVP:
334
			rsvp_print(cp, len);
335
			return;
336
#endif
337
		default:
338
			return;
339
		}
340
	}
341

    
342
	return;
343
}
(8-8/11)