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