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