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