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