xref: /openbsd-src/usr.sbin/tcpdump/print-mobile.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: print-mobile.c,v 1.2 2008/06/27 06:03:08 ray Exp $ */
2 /*	$NetBSD: print-mobile.c,v 1.3 1999/07/26 06:11:57 itojun Exp $ */
3 
4 /*
5  * (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Heiko W.Rupp <hwr@pilhuhn.de>
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #if 0
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: print-mobile.c,v 1.3 1999/07/26 06:11:57 itojun Exp $");
37 #endif
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/uio.h>
43 #include <sys/socket.h>
44 
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 
49 #include <netdb.h>
50 #include <stdio.h>
51 
52 #include "interface.h"
53 #include "addrtoname.h"
54 #include "extract.h"		/* must come after interface.h */
55 
56 #define MOBILE_SIZE (8)
57 
58 struct mobile_ip {
59 	u_int16_t proto;
60 	u_int16_t hcheck;
61 	u_int32_t odst;
62 	u_int32_t osrc;
63 };
64 
65 #define OSRC_PRES	0x0080	/* old source is present */
66 
67 static u_int16_t mob_in_cksum(u_short *p, int len);
68 
69 /*
70  * Deencapsulate and print a mobile-tunneled IP datagram
71  */
72 void
73 mobile_print(const u_char *bp, u_int length)
74 {
75 	const u_char *cp = bp +8 ;
76 	const struct mobile_ip *mob;
77 	u_short proto,crc;
78 	u_char osp =0;			/* old source address present */
79 
80 	mob = (const struct mobile_ip *)bp;
81 
82 	if (length < MOBILE_SIZE) {
83 		fputs("[|mobile]", stdout);
84 		return;
85 	}
86 
87 	proto = EXTRACT_16BITS(&mob->proto);
88 	crc =  EXTRACT_16BITS(&mob->hcheck);
89 	if (proto & OSRC_PRES) {
90 		osp=1;
91 		cp +=4 ;
92 	}
93 
94 	if (osp)  {
95 		fputs("[S] ",stdout);
96 		if (vflag)
97 			(void)printf("%s ",ipaddr_string(&mob->osrc));
98 	} else {
99 		fputs("[] ",stdout);
100 	}
101 	if (vflag) {
102 		(void)printf("> %s ",ipaddr_string(&mob->odst));
103 		(void)printf("(oproto=%d)",proto>>8);
104 	}
105 	if (mob_in_cksum((u_short *)mob, osp ? 12 : 8)!=0) {
106 		(void)printf(" (bad checksum %d)",crc);
107 	}
108 
109 	return;
110 }
111 
112 static u_int16_t mob_in_cksum(u_short *p, int len)
113 {
114 	u_int32_t sum = 0;
115 	int nwords = len >> 1;
116 
117 	while (nwords-- != 0)
118 		sum += *p++;
119 
120 	if (len & 1) {
121 		union {
122 			u_int16_t w;
123 			u_int32_t c[2];
124 		} u;
125 		u.c[0] = *(u_char *)p;
126 		u.c[1] = 0;
127 		sum += u.w;
128 	}
129 
130 	/* end-around-carry */
131 	sum = (sum >> 16) + (sum & 0xffff);
132 	sum += (sum >> 16);
133 	return (~sum);
134 }
135 
136