Home | History | Annotate | Line # | Download | only in netinet
ip_netbios_pxy.c revision 1.1
      1 /*	$NetBSD: ip_netbios_pxy.c,v 1.1 2012/03/23 20:37:01 christos Exp $	*/
      2 
      3 /*
      4  * Simple netbios-dgm transparent proxy for in-kernel use.
      5  * For use with the NAT code.
      6  * Id
      7  */
      8 
      9 /*-
     10  * Copyright (c) 2002-2003 Paul J. Ledbetter III
     11  * All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  * Id
     35  */
     36 
     37 #define	IPF_NETBIOS_PROXY
     38 
     39 void ipf_p_netbios_main_load __P((void));
     40 void ipf_p_netbios_main_unload __P((void));
     41 int ipf_p_netbios_out __P((void *, fr_info_t *, ap_session_t *, nat_t *));
     42 
     43 static	frentry_t	netbiosfr;
     44 
     45 int	netbios_proxy_init = 0;
     46 
     47 /*
     48  * Initialize local structures.
     49  */
     50 void
     51 ipf_p_netbios_main_load()
     52 {
     53 	bzero((char *)&netbiosfr, sizeof(netbiosfr));
     54 	netbiosfr.fr_ref = 1;
     55 	netbiosfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
     56 	MUTEX_INIT(&netbiosfr.fr_lock, "NETBIOS proxy rule lock");
     57 	netbios_proxy_init = 1;
     58 }
     59 
     60 
     61 void
     62 ipf_p_netbios_main_unload()
     63 {
     64 	if (netbios_proxy_init == 1) {
     65 		MUTEX_DESTROY(&netbiosfr.fr_lock);
     66 		netbios_proxy_init = 0;
     67 	}
     68 }
     69 
     70 
     71 int
     72 ipf_p_netbios_out(arg, fin, aps, nat)
     73 	void *arg;
     74 	fr_info_t *fin;
     75 	ap_session_t *aps;
     76 	nat_t *nat;
     77 {
     78 	char dgmbuf[6];
     79 	int off, dlen;
     80 	udphdr_t *udp;
     81 	ip_t *ip;
     82 	mb_t *m;
     83 
     84 	aps = aps;	/* LINT */
     85 	nat = nat;	/* LINT */
     86 
     87 	m = fin->fin_m;
     88 	dlen = fin->fin_dlen - sizeof(*udp);
     89 	/*
     90 	 * no net bios datagram could possibly be shorter than this
     91 	 */
     92 	if (dlen < 11)
     93 		return 0;
     94 
     95 	ip = fin->fin_ip;
     96 	udp = (udphdr_t *)fin->fin_dp;
     97 	off = (char *)udp - (char *)ip + sizeof(*udp) + fin->fin_ipoff;
     98 
     99 	/*
    100 	 * move past the
    101 	 *	ip header;
    102 	 *	udp header;
    103 	 *	4 bytes into the net bios dgm header.
    104 	 *  According to rfc1002, this should be the exact location of
    105 	 *  the source address/port
    106 	 */
    107 	off += 4;
    108 
    109 	/* Copy NATed source Address/port*/
    110 	dgmbuf[0] = (char)((ip->ip_src.s_addr     ) &0xFF);
    111 	dgmbuf[1] = (char)((ip->ip_src.s_addr >> 8) &0xFF);
    112 	dgmbuf[2] = (char)((ip->ip_src.s_addr >> 16)&0xFF);
    113 	dgmbuf[3] = (char)((ip->ip_src.s_addr >> 24)&0xFF);
    114 
    115 	dgmbuf[4] = (char)((udp->uh_sport     )&0xFF);
    116 	dgmbuf[5] = (char)((udp->uh_sport >> 8)&0xFF);
    117 
    118 	/* replace data in packet */
    119 	COPYBACK(m, off, sizeof(dgmbuf), dgmbuf);
    120 
    121 	return 0;
    122 }
    123