1  | 
     | 
     | 
    /*	$OpenBSD: print-mobile.c,v 1.5 2015/01/16 06:40:21 deraadt 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  | 
    
    
    64  | 
     | 
     | 
    mobile_print(const u_char *bp, u_int length)  | 
    
    
    65  | 
     | 
     | 
    { | 
    
    
    66  | 
     | 
     | 
    	const u_char *cp = bp +8 ;  | 
    
    
    67  | 
     | 
     | 
    	const struct mobile_ip *mob;  | 
    
    
    68  | 
     | 
     | 
    	u_short proto,crc;  | 
    
    
    69  | 
     | 
     | 
    	u_char osp =0;			/* old source address present */  | 
    
    
    70  | 
     | 
     | 
     | 
    
    
    71  | 
     | 
     | 
    	mob = (const struct mobile_ip *)bp;  | 
    
    
    72  | 
     | 
     | 
     | 
    
    
    73  | 
     | 
     | 
    	if (length < MOBILE_SIZE) { | 
    
    
    74  | 
     | 
     | 
    		fputs("[|mobile]", stdout); | 
    
    
    75  | 
     | 
     | 
    		return;  | 
    
    
    76  | 
     | 
     | 
    	}  | 
    
    
    77  | 
     | 
     | 
     | 
    
    
    78  | 
     | 
     | 
    	proto = EXTRACT_16BITS(&mob->proto);  | 
    
    
    79  | 
     | 
     | 
    	crc =  EXTRACT_16BITS(&mob->hcheck);  | 
    
    
    80  | 
     | 
     | 
    	if (proto & OSRC_PRES) { | 
    
    
    81  | 
     | 
     | 
    		osp=1;  | 
    
    
    82  | 
     | 
     | 
    		cp +=4 ;  | 
    
    
    83  | 
     | 
     | 
    	}  | 
    
    
    84  | 
     | 
     | 
     | 
    
    
    85  | 
     | 
     | 
    	if (osp)  { | 
    
    
    86  | 
     | 
     | 
    		fputs("[S] ",stdout); | 
    
    
    87  | 
     | 
     | 
    		if (vflag)  | 
    
    
    88  | 
     | 
     | 
    			(void)printf("%s ",ipaddr_string(&mob->osrc)); | 
    
    
    89  | 
     | 
     | 
    	} else { | 
    
    
    90  | 
     | 
     | 
    		fputs("[] ",stdout); | 
    
    
    91  | 
     | 
     | 
    	}  | 
    
    
    92  | 
     | 
     | 
    	if (vflag) { | 
    
    
    93  | 
     | 
     | 
    		(void)printf("> %s ",ipaddr_string(&mob->odst)); | 
    
    
    94  | 
     | 
     | 
    		(void)printf("(oproto=%d)",proto>>8); | 
    
    
    95  | 
     | 
     | 
    	}  | 
    
    
    96  | 
     | 
     | 
    	if (mob_in_cksum((u_short *)mob, osp ? 12 : 8)!=0) { | 
    
    
    97  | 
     | 
     | 
    		(void)printf(" (bad checksum %d)",crc); | 
    
    
    98  | 
     | 
     | 
    	}  | 
    
    
    99  | 
     | 
     | 
     | 
    
    
    100  | 
     | 
     | 
    	return;  | 
    
    
    101  | 
     | 
     | 
    }  | 
    
    
    102  | 
     | 
     | 
     | 
    
    
    103  | 
     | 
     | 
    static u_int16_t mob_in_cksum(u_short *p, int len)  | 
    
    
    104  | 
     | 
     | 
    { | 
    
    
    105  | 
     | 
     | 
    	u_int32_t sum = 0;  | 
    
    
    106  | 
     | 
     | 
    	int nwords = len >> 1;  | 
    
    
    107  | 
     | 
     | 
     | 
    
    
    108  | 
     | 
     | 
    	while (nwords-- != 0)  | 
    
    
    109  | 
     | 
     | 
    		sum += *p++;  | 
    
    
    110  | 
     | 
     | 
     | 
    
    
    111  | 
     | 
     | 
    	if (len & 1) { | 
    
    
    112  | 
     | 
     | 
    		union { | 
    
    
    113  | 
     | 
     | 
    			u_int16_t w;  | 
    
    
    114  | 
     | 
     | 
    			u_int32_t c[2];  | 
    
    
    115  | 
     | 
     | 
    		} u;  | 
    
    
    116  | 
     | 
     | 
    		u.c[0] = *(u_char *)p;  | 
    
    
    117  | 
     | 
     | 
    		u.c[1] = 0;  | 
    
    
    118  | 
     | 
     | 
    		sum += u.w;  | 
    
    
    119  | 
     | 
     | 
    	}  | 
    
    
    120  | 
     | 
     | 
     | 
    
    
    121  | 
     | 
     | 
    	/* end-around-carry */  | 
    
    
    122  | 
     | 
     | 
    	sum = (sum >> 16) + (sum & 0xffff);  | 
    
    
    123  | 
     | 
     | 
    	sum += (sum >> 16);  | 
    
    
    124  | 
     | 
     | 
    	return (~sum);  | 
    
    
    125  | 
     | 
     | 
    }  | 
    
    
    126  | 
     | 
     | 
     |