17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Simple netbios-dgm transparent proxy for in-kernel use.
37c478bd9Sstevel@tonic-gate  * For use with the NAT code.
4ab25eeb5Syz  * $Id: ip_netbios_pxy.c,v 2.8.2.1 2005/07/15 21:56:51 darrenr Exp $
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate /*-
87c478bd9Sstevel@tonic-gate  * Copyright (c) 2002-2003 Paul J. Ledbetter III
97c478bd9Sstevel@tonic-gate  * All rights reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
127c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
137c478bd9Sstevel@tonic-gate  * are met:
147c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
157c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
167c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
177c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
187c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
217c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
227c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
237c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
247c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
257c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
267c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
277c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
287c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
297c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
307c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
317c478bd9Sstevel@tonic-gate  *
32ab25eeb5Syz  * $Id: ip_netbios_pxy.c,v 2.8.2.1 2005/07/15 21:56:51 darrenr Exp $
33*f4b3ec61Sdh  *
34*f4b3ec61Sdh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
35*f4b3ec61Sdh  * Use is subject to license terms.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
38*f4b3ec61Sdh #pragma ident	"%Z%%M%	%I%	%E% SMI"
397c478bd9Sstevel@tonic-gate 
40*f4b3ec61Sdh #define	IPF_NETBIOS_PROXY
417c478bd9Sstevel@tonic-gate 
42*f4b3ec61Sdh typedef struct ifs_netbiospxy {
43*f4b3ec61Sdh 	frentry_t	netbiosfr;
44*f4b3ec61Sdh 	int		netbios_proxy_init;
45*f4b3ec61Sdh } ifs_netbiospxy_t;
467c478bd9Sstevel@tonic-gate 
47*f4b3ec61Sdh int ippr_netbios_init __P((void **, ipf_stack_t *));
48*f4b3ec61Sdh void ippr_netbios_fini __P((void **, ipf_stack_t *));
49*f4b3ec61Sdh int ippr_netbios_out __P((fr_info_t *, ap_session_t *, nat_t *, void *));
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Initialize local structures.
537c478bd9Sstevel@tonic-gate  */
54*f4b3ec61Sdh /*ARGSUSED*/
55*f4b3ec61Sdh int ippr_netbios_init(private, ifs)
56*f4b3ec61Sdh void **private;
57*f4b3ec61Sdh ipf_stack_t *ifs;
587c478bd9Sstevel@tonic-gate {
59*f4b3ec61Sdh 	ifs_netbiospxy_t *ifsnetbios;
60*f4b3ec61Sdh 
61*f4b3ec61Sdh 	KMALLOC(ifsnetbios, ifs_netbiospxy_t *);
62*f4b3ec61Sdh 	if (ifsnetbios == NULL)
63*f4b3ec61Sdh 		return -1;
64*f4b3ec61Sdh 
65*f4b3ec61Sdh 	bzero((char *)&ifsnetbios->netbiosfr, sizeof(ifsnetbios->netbiosfr));
66*f4b3ec61Sdh 	ifsnetbios->netbiosfr.fr_ref = 1;
67*f4b3ec61Sdh 	ifsnetbios->netbiosfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
68*f4b3ec61Sdh 	MUTEX_INIT(&ifsnetbios->netbiosfr.fr_lock, "NETBIOS proxy rule lock");
69*f4b3ec61Sdh 	ifsnetbios->netbios_proxy_init = 1;
70*f4b3ec61Sdh 
71*f4b3ec61Sdh 	*private = (void *)ifsnetbios;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	return 0;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
77*f4b3ec61Sdh /*ARGSUSED*/
78*f4b3ec61Sdh void ippr_netbios_fini(private, ifs)
79*f4b3ec61Sdh void **private;
80*f4b3ec61Sdh ipf_stack_t *ifs;
817c478bd9Sstevel@tonic-gate {
82*f4b3ec61Sdh 	ifs_netbiospxy_t *ifsnetbios = *((ifs_netbiospxy_t **)private);
83*f4b3ec61Sdh 
84*f4b3ec61Sdh 	if (ifsnetbios->netbios_proxy_init == 1) {
85*f4b3ec61Sdh 		MUTEX_DESTROY(&ifsnetbios->netbiosfr.fr_lock);
86*f4b3ec61Sdh 		ifsnetbios->netbios_proxy_init = 0;
877c478bd9Sstevel@tonic-gate 	}
88*f4b3ec61Sdh 
89*f4b3ec61Sdh 	KFREE(ifsnetbios);
90*f4b3ec61Sdh 	*private = NULL;
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
94*f4b3ec61Sdh /*ARGSUSED*/
95*f4b3ec61Sdh int ippr_netbios_out(fin, aps, nat, private)
967c478bd9Sstevel@tonic-gate fr_info_t *fin;
977c478bd9Sstevel@tonic-gate ap_session_t *aps;
987c478bd9Sstevel@tonic-gate nat_t *nat;
99*f4b3ec61Sdh void *private;
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	char dgmbuf[6];
1027c478bd9Sstevel@tonic-gate 	int off, dlen;
1037c478bd9Sstevel@tonic-gate 	udphdr_t *udp;
1047c478bd9Sstevel@tonic-gate 	ip_t *ip;
1057c478bd9Sstevel@tonic-gate 	mb_t *m;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	aps = aps;	/* LINT */
1087c478bd9Sstevel@tonic-gate 	nat = nat;	/* LINT */
1097c478bd9Sstevel@tonic-gate 
110ab25eeb5Syz 	m = fin->fin_m;
111ab25eeb5Syz 	dlen = fin->fin_dlen - sizeof(*udp);
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * no net bios datagram could possibly be shorter than this
1147c478bd9Sstevel@tonic-gate 	 */
115ab25eeb5Syz 	if (dlen < 11)
1167c478bd9Sstevel@tonic-gate 		return 0;
1177c478bd9Sstevel@tonic-gate 
118ab25eeb5Syz 	ip = fin->fin_ip;
1197c478bd9Sstevel@tonic-gate 	udp = (udphdr_t *)fin->fin_dp;
120ab25eeb5Syz 	off = (char *)udp - (char *)ip + sizeof(*udp) + fin->fin_ipoff;
1217c478bd9Sstevel@tonic-gate 
122ab25eeb5Syz 	/*
1237c478bd9Sstevel@tonic-gate 	 * move past the
1247c478bd9Sstevel@tonic-gate 	 *	ip header;
1257c478bd9Sstevel@tonic-gate 	 *	udp header;
126ab25eeb5Syz 	 *	4 bytes into the net bios dgm header.
1277c478bd9Sstevel@tonic-gate 	 *  According to rfc1002, this should be the exact location of
1287c478bd9Sstevel@tonic-gate 	 *  the source address/port
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	off += 4;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/* Copy NATed source Address/port*/
1337c478bd9Sstevel@tonic-gate 	dgmbuf[0] = (char)((ip->ip_src.s_addr     ) &0xFF);
1347c478bd9Sstevel@tonic-gate 	dgmbuf[1] = (char)((ip->ip_src.s_addr >> 8) &0xFF);
1357c478bd9Sstevel@tonic-gate 	dgmbuf[2] = (char)((ip->ip_src.s_addr >> 16)&0xFF);
1367c478bd9Sstevel@tonic-gate 	dgmbuf[3] = (char)((ip->ip_src.s_addr >> 24)&0xFF);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	dgmbuf[4] = (char)((udp->uh_sport     )&0xFF);
1397c478bd9Sstevel@tonic-gate 	dgmbuf[5] = (char)((udp->uh_sport >> 8)&0xFF);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/* replace data in packet */
1427c478bd9Sstevel@tonic-gate 	COPYBACK(m, off, sizeof(dgmbuf), dgmbuf);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	return 0;
1457c478bd9Sstevel@tonic-gate }
146