xref: /openbsd/usr.sbin/tcpdump/print-mobile.c (revision acf2e20b)
1*acf2e20bSdlg /*	$OpenBSD: print-mobile.c,v 1.7 2022/01/05 05:46:18 dlg Exp $ */
242416b7cSjakob /*	$NetBSD: print-mobile.c,v 1.3 1999/07/26 06:11:57 itojun Exp $ */
342416b7cSjakob 
442416b7cSjakob /*
542416b7cSjakob  * (c) 1998 The NetBSD Foundation, Inc.
642416b7cSjakob  * All rights reserved.
742416b7cSjakob  *
842416b7cSjakob  * This code is derived from software contributed to The NetBSD Foundation
942416b7cSjakob  * by Heiko W.Rupp <hwr@pilhuhn.de>
1042416b7cSjakob  *
1142416b7cSjakob  * Redistribution and use in source and binary forms, with or without
1242416b7cSjakob  * modification, are permitted provided that the following conditions
1342416b7cSjakob  * are met:
1442416b7cSjakob  * 1. Redistributions of source code must retain the above copyright
1542416b7cSjakob  *    notice, this list of conditions and the following disclaimer.
1642416b7cSjakob  * 2. Redistributions in binary form must reproduce the above copyright
1742416b7cSjakob  *    notice, this list of conditions and the following disclaimer in the
1842416b7cSjakob  *    documentation and/or other materials provided with the distribution.
1942416b7cSjakob  *
2042416b7cSjakob  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2142416b7cSjakob  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2242416b7cSjakob  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2342416b7cSjakob  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2442416b7cSjakob  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2542416b7cSjakob  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2642416b7cSjakob  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2742416b7cSjakob  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2842416b7cSjakob  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2942416b7cSjakob  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3042416b7cSjakob  * POSSIBILITY OF SUCH DAMAGE.
3142416b7cSjakob  */
3242416b7cSjakob 
3342416b7cSjakob #include <sys/time.h>
3442416b7cSjakob #include <sys/uio.h>
3542416b7cSjakob #include <sys/socket.h>
3642416b7cSjakob 
3742416b7cSjakob #include <netinet/in.h>
3842416b7cSjakob #include <netinet/ip.h>
3942416b7cSjakob 
4042416b7cSjakob #include <netdb.h>
4142416b7cSjakob #include <stdio.h>
4242416b7cSjakob 
4342416b7cSjakob #include "interface.h"
4442416b7cSjakob #include "addrtoname.h"
4542416b7cSjakob #include "extract.h"		/* must come after interface.h */
4642416b7cSjakob 
4742416b7cSjakob #define MOBILE_SIZE (8)
4842416b7cSjakob 
4942416b7cSjakob struct mobile_ip {
5042416b7cSjakob 	u_int16_t proto;
5142416b7cSjakob 	u_int16_t hcheck;
5242416b7cSjakob 	u_int32_t odst;
5342416b7cSjakob 	u_int32_t osrc;
5442416b7cSjakob };
5542416b7cSjakob 
5642416b7cSjakob #define OSRC_PRES	0x0080	/* old source is present */
5742416b7cSjakob 
5842416b7cSjakob static u_int16_t mob_in_cksum(u_short *p, int len);
5942416b7cSjakob 
6042416b7cSjakob /*
6142416b7cSjakob  * Deencapsulate and print a mobile-tunneled IP datagram
6242416b7cSjakob  */
6342416b7cSjakob void
mobile_print(const u_char * bp,u_int length)6442416b7cSjakob mobile_print(const u_char *bp, u_int length)
6542416b7cSjakob {
6642416b7cSjakob 	const struct mobile_ip *mob;
6742416b7cSjakob 	u_short proto,crc;
6842416b7cSjakob 	u_char osp =0;			/* old source address present */
6942416b7cSjakob 
7042416b7cSjakob 	mob = (const struct mobile_ip *)bp;
7142416b7cSjakob 
7242416b7cSjakob 	if (length < MOBILE_SIZE) {
73f96bb33fSprocter 		printf("[|mobile]");
7442416b7cSjakob 		return;
7542416b7cSjakob 	}
7642416b7cSjakob 
7742416b7cSjakob 	proto = EXTRACT_16BITS(&mob->proto);
7842416b7cSjakob 	crc =  EXTRACT_16BITS(&mob->hcheck);
7942416b7cSjakob 	if (proto & OSRC_PRES) {
8042416b7cSjakob 		osp=1;
8142416b7cSjakob 	}
8242416b7cSjakob 
8342416b7cSjakob 	if (osp)  {
84f96bb33fSprocter 		printf("[S] ");
8542416b7cSjakob 		if (vflag)
86f96bb33fSprocter 			printf("%s ",ipaddr_string(&mob->osrc));
8742416b7cSjakob 	} else {
88f96bb33fSprocter 		printf("[] ");
8942416b7cSjakob 	}
9042416b7cSjakob 	if (vflag) {
91f96bb33fSprocter 		printf("> %s ",ipaddr_string(&mob->odst));
92f96bb33fSprocter 		printf("(oproto=%d)",proto>>8);
9342416b7cSjakob 	}
9442416b7cSjakob 	if (mob_in_cksum((u_short *)mob, osp ? 12 : 8)!=0) {
95f96bb33fSprocter 		printf(" (bad checksum %d)",crc);
9642416b7cSjakob 	}
9742416b7cSjakob 
9842416b7cSjakob 	return;
9942416b7cSjakob }
10042416b7cSjakob 
mob_in_cksum(u_short * p,int len)10142416b7cSjakob static u_int16_t mob_in_cksum(u_short *p, int len)
10242416b7cSjakob {
10342416b7cSjakob 	u_int32_t sum = 0;
10442416b7cSjakob 	int nwords = len >> 1;
10542416b7cSjakob 
10642416b7cSjakob 	while (nwords-- != 0)
10742416b7cSjakob 		sum += *p++;
10842416b7cSjakob 
10942416b7cSjakob 	if (len & 1) {
11042416b7cSjakob 		union {
11142416b7cSjakob 			u_int16_t w;
11242416b7cSjakob 			u_int32_t c[2];
11342416b7cSjakob 		} u;
11442416b7cSjakob 		u.c[0] = *(u_char *)p;
11542416b7cSjakob 		u.c[1] = 0;
11642416b7cSjakob 		sum += u.w;
11742416b7cSjakob 	}
11842416b7cSjakob 
11942416b7cSjakob 	/* end-around-carry */
12042416b7cSjakob 	sum = (sum >> 16) + (sum & 0xffff);
12142416b7cSjakob 	sum += (sum >> 16);
12242416b7cSjakob 	return (~sum);
12342416b7cSjakob }
12442416b7cSjakob 
125