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