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