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