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