1 /*
2 * Simple netbios-dgm transparent proxy for in-kernel use.
3 * For use with the NAT code.
4 * $Id: ip_netbios_pxy.c,v 2.8.2.1 2005/07/15 21:56:51 darrenr Exp $
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: ip_netbios_pxy.c,v 2.8.2.1 2005/07/15 21:56:51 darrenr Exp $
33 *
34 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
35 * Use is subject to license terms.
36 */
37
38 #pragma ident "%Z%%M% %I% %E% SMI"
39
40 #define IPF_NETBIOS_PROXY
41
42 typedef struct ifs_netbiospxy {
43 frentry_t netbiosfr;
44 int netbios_proxy_init;
45 } ifs_netbiospxy_t;
46
47 int ippr_netbios_init __P((void **, ipf_stack_t *));
48 void ippr_netbios_fini __P((void **, ipf_stack_t *));
49 int ippr_netbios_out __P((fr_info_t *, ap_session_t *, nat_t *, void *));
50
51 /*
52 * Initialize local structures.
53 */
54 /*ARGSUSED*/
ippr_netbios_init(private,ifs)55 int ippr_netbios_init(private, ifs)
56 void **private;
57 ipf_stack_t *ifs;
58 {
59 ifs_netbiospxy_t *ifsnetbios;
60
61 KMALLOC(ifsnetbios, ifs_netbiospxy_t *);
62 if (ifsnetbios == NULL)
63 return -1;
64
65 bzero((char *)&ifsnetbios->netbiosfr, sizeof(ifsnetbios->netbiosfr));
66 ifsnetbios->netbiosfr.fr_ref = 1;
67 ifsnetbios->netbiosfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
68 MUTEX_INIT(&ifsnetbios->netbiosfr.fr_lock, "NETBIOS proxy rule lock");
69 ifsnetbios->netbios_proxy_init = 1;
70
71 *private = (void *)ifsnetbios;
72
73 return 0;
74 }
75
76
77 /*ARGSUSED*/
ippr_netbios_fini(private,ifs)78 void ippr_netbios_fini(private, ifs)
79 void **private;
80 ipf_stack_t *ifs;
81 {
82 ifs_netbiospxy_t *ifsnetbios = *((ifs_netbiospxy_t **)private);
83
84 if (ifsnetbios->netbios_proxy_init == 1) {
85 MUTEX_DESTROY(&ifsnetbios->netbiosfr.fr_lock);
86 ifsnetbios->netbios_proxy_init = 0;
87 }
88
89 KFREE(ifsnetbios);
90 *private = NULL;
91 }
92
93
94 /*ARGSUSED*/
ippr_netbios_out(fin,aps,nat,private)95 int ippr_netbios_out(fin, aps, nat, private)
96 fr_info_t *fin;
97 ap_session_t *aps;
98 nat_t *nat;
99 void *private;
100 {
101 char dgmbuf[6];
102 int off, dlen;
103 udphdr_t *udp;
104 ip_t *ip;
105 mb_t *m;
106
107 aps = aps; /* LINT */
108 nat = nat; /* LINT */
109
110 m = fin->fin_m;
111 dlen = fin->fin_dlen - sizeof(*udp);
112 /*
113 * no net bios datagram could possibly be shorter than this
114 */
115 if (dlen < 11)
116 return 0;
117
118 ip = fin->fin_ip;
119 udp = (udphdr_t *)fin->fin_dp;
120 off = (char *)udp - (char *)ip + sizeof(*udp) + fin->fin_ipoff;
121
122 /*
123 * move past the
124 * ip header;
125 * udp header;
126 * 4 bytes into the net bios dgm header.
127 * According to rfc1002, this should be the exact location of
128 * the source address/port
129 */
130 off += 4;
131
132 /* Copy NATed source Address/port*/
133 dgmbuf[0] = (char)((ip->ip_src.s_addr ) &0xFF);
134 dgmbuf[1] = (char)((ip->ip_src.s_addr >> 8) &0xFF);
135 dgmbuf[2] = (char)((ip->ip_src.s_addr >> 16)&0xFF);
136 dgmbuf[3] = (char)((ip->ip_src.s_addr >> 24)&0xFF);
137
138 dgmbuf[4] = (char)((udp->uh_sport )&0xFF);
139 dgmbuf[5] = (char)((udp->uh_sport >> 8)&0xFF);
140
141 /* replace data in packet */
142 COPYBACK(m, off, sizeof(dgmbuf), dgmbuf);
143
144 return 0;
145 }
146