xref: /openbsd-src/usr.sbin/tcpdump/print-mobile.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: print-mobile.c,v 1.4 2014/08/14 12:44:44 mpi 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 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/uio.h>
36 #include <sys/socket.h>
37 
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 
41 #include <netdb.h>
42 #include <stdio.h>
43 
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "extract.h"		/* must come after interface.h */
47 
48 #define MOBILE_SIZE (8)
49 
50 struct mobile_ip {
51 	u_int16_t proto;
52 	u_int16_t hcheck;
53 	u_int32_t odst;
54 	u_int32_t osrc;
55 };
56 
57 #define OSRC_PRES	0x0080	/* old source is present */
58 
59 static u_int16_t mob_in_cksum(u_short *p, int len);
60 
61 /*
62  * Deencapsulate and print a mobile-tunneled IP datagram
63  */
64 void
65 mobile_print(const u_char *bp, u_int length)
66 {
67 	const u_char *cp = bp +8 ;
68 	const struct mobile_ip *mob;
69 	u_short proto,crc;
70 	u_char osp =0;			/* old source address present */
71 
72 	mob = (const struct mobile_ip *)bp;
73 
74 	if (length < MOBILE_SIZE) {
75 		fputs("[|mobile]", stdout);
76 		return;
77 	}
78 
79 	proto = EXTRACT_16BITS(&mob->proto);
80 	crc =  EXTRACT_16BITS(&mob->hcheck);
81 	if (proto & OSRC_PRES) {
82 		osp=1;
83 		cp +=4 ;
84 	}
85 
86 	if (osp)  {
87 		fputs("[S] ",stdout);
88 		if (vflag)
89 			(void)printf("%s ",ipaddr_string(&mob->osrc));
90 	} else {
91 		fputs("[] ",stdout);
92 	}
93 	if (vflag) {
94 		(void)printf("> %s ",ipaddr_string(&mob->odst));
95 		(void)printf("(oproto=%d)",proto>>8);
96 	}
97 	if (mob_in_cksum((u_short *)mob, osp ? 12 : 8)!=0) {
98 		(void)printf(" (bad checksum %d)",crc);
99 	}
100 
101 	return;
102 }
103 
104 static u_int16_t mob_in_cksum(u_short *p, int len)
105 {
106 	u_int32_t sum = 0;
107 	int nwords = len >> 1;
108 
109 	while (nwords-- != 0)
110 		sum += *p++;
111 
112 	if (len & 1) {
113 		union {
114 			u_int16_t w;
115 			u_int32_t c[2];
116 		} u;
117 		u.c[0] = *(u_char *)p;
118 		u.c[1] = 0;
119 		sum += u.w;
120 	}
121 
122 	/* end-around-carry */
123 	sum = (sum >> 16) + (sum & 0xffff);
124 	sum += (sum >> 16);
125 	return (~sum);
126 }
127 
128