17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Simple netbios-dgm transparent proxy for in-kernel use.
37c478bd9Sstevel@tonic-gate  * For use with the NAT code.
4*ab25eeb5Syz  * $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  *
32*ab25eeb5Syz  * $Id: ip_netbios_pxy.c,v 2.8.2.1 2005/07/15 21:56:51 darrenr Exp $
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define	IPF_NETBIOS_PROXY
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate int ippr_netbios_init __P((void));
387c478bd9Sstevel@tonic-gate void ippr_netbios_fini __P((void));
397c478bd9Sstevel@tonic-gate int ippr_netbios_out __P((fr_info_t *, ap_session_t *, nat_t *));
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static	frentry_t	netbiosfr;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate int	netbios_proxy_init = 0;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Initialize local structures.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate int ippr_netbios_init()
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	bzero((char *)&netbiosfr, sizeof(netbiosfr));
517c478bd9Sstevel@tonic-gate 	netbiosfr.fr_ref = 1;
527c478bd9Sstevel@tonic-gate 	netbiosfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
537c478bd9Sstevel@tonic-gate 	MUTEX_INIT(&netbiosfr.fr_lock, "NETBIOS proxy rule lock");
547c478bd9Sstevel@tonic-gate 	netbios_proxy_init = 1;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	return 0;
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void ippr_netbios_fini()
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	if (netbios_proxy_init == 1) {
637c478bd9Sstevel@tonic-gate 		MUTEX_DESTROY(&netbiosfr.fr_lock);
647c478bd9Sstevel@tonic-gate 		netbios_proxy_init = 0;
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int ippr_netbios_out(fin, aps, nat)
707c478bd9Sstevel@tonic-gate fr_info_t *fin;
717c478bd9Sstevel@tonic-gate ap_session_t *aps;
727c478bd9Sstevel@tonic-gate nat_t *nat;
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	char dgmbuf[6];
757c478bd9Sstevel@tonic-gate 	int off, dlen;
767c478bd9Sstevel@tonic-gate 	udphdr_t *udp;
777c478bd9Sstevel@tonic-gate 	ip_t *ip;
787c478bd9Sstevel@tonic-gate 	mb_t *m;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	aps = aps;	/* LINT */
817c478bd9Sstevel@tonic-gate 	nat = nat;	/* LINT */
827c478bd9Sstevel@tonic-gate 
83*ab25eeb5Syz 	m = fin->fin_m;
84*ab25eeb5Syz 	dlen = fin->fin_dlen - sizeof(*udp);
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * no net bios datagram could possibly be shorter than this
877c478bd9Sstevel@tonic-gate 	 */
88*ab25eeb5Syz 	if (dlen < 11)
897c478bd9Sstevel@tonic-gate 		return 0;
907c478bd9Sstevel@tonic-gate 
91*ab25eeb5Syz 	ip = fin->fin_ip;
927c478bd9Sstevel@tonic-gate 	udp = (udphdr_t *)fin->fin_dp;
93*ab25eeb5Syz 	off = (char *)udp - (char *)ip + sizeof(*udp) + fin->fin_ipoff;
947c478bd9Sstevel@tonic-gate 
95*ab25eeb5Syz 	/*
967c478bd9Sstevel@tonic-gate 	 * move past the
977c478bd9Sstevel@tonic-gate 	 *	ip header;
987c478bd9Sstevel@tonic-gate 	 *	udp header;
99*ab25eeb5Syz 	 *	4 bytes into the net bios dgm header.
1007c478bd9Sstevel@tonic-gate 	 *  According to rfc1002, this should be the exact location of
1017c478bd9Sstevel@tonic-gate 	 *  the source address/port
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	off += 4;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* Copy NATed source Address/port*/
1067c478bd9Sstevel@tonic-gate 	dgmbuf[0] = (char)((ip->ip_src.s_addr     ) &0xFF);
1077c478bd9Sstevel@tonic-gate 	dgmbuf[1] = (char)((ip->ip_src.s_addr >> 8) &0xFF);
1087c478bd9Sstevel@tonic-gate 	dgmbuf[2] = (char)((ip->ip_src.s_addr >> 16)&0xFF);
1097c478bd9Sstevel@tonic-gate 	dgmbuf[3] = (char)((ip->ip_src.s_addr >> 24)&0xFF);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	dgmbuf[4] = (char)((udp->uh_sport     )&0xFF);
1127c478bd9Sstevel@tonic-gate 	dgmbuf[5] = (char)((udp->uh_sport >> 8)&0xFF);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* replace data in packet */
1157c478bd9Sstevel@tonic-gate 	COPYBACK(m, off, sizeof(dgmbuf), dgmbuf);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	return 0;
1187c478bd9Sstevel@tonic-gate }
119