1 1.3 darrenr /* $NetBSD: ip_netbios_pxy.c,v 1.3 2012/07/22 14:27:51 darrenr 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.3 darrenr * Id: ip_netbios_pxy.c,v 1.1.1.2 2012/07/22 13:45:30 darrenr Exp 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.3 darrenr * Id: ip_netbios_pxy.c,v 1.1.1.2 2012/07/22 13:45:30 darrenr Exp 35 1.1 christos */ 36 1.1 christos 37 1.2 christos #include <sys/cdefs.h> 38 1.3 darrenr __KERNEL_RCSID(1, "$NetBSD: ip_netbios_pxy.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $"); 39 1.2 christos 40 1.1 christos #define IPF_NETBIOS_PROXY 41 1.1 christos 42 1.2 christos void ipf_p_netbios_main_load(void); 43 1.2 christos void ipf_p_netbios_main_unload(void); 44 1.2 christos int ipf_p_netbios_out(void *, fr_info_t *, ap_session_t *, nat_t *); 45 1.1 christos 46 1.1 christos static frentry_t netbiosfr; 47 1.1 christos 48 1.1 christos int netbios_proxy_init = 0; 49 1.1 christos 50 1.1 christos /* 51 1.1 christos * Initialize local structures. 52 1.1 christos */ 53 1.1 christos void 54 1.2 christos ipf_p_netbios_main_load(void) 55 1.1 christos { 56 1.1 christos bzero((char *)&netbiosfr, sizeof(netbiosfr)); 57 1.1 christos netbiosfr.fr_ref = 1; 58 1.1 christos netbiosfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE; 59 1.1 christos MUTEX_INIT(&netbiosfr.fr_lock, "NETBIOS proxy rule lock"); 60 1.1 christos netbios_proxy_init = 1; 61 1.1 christos } 62 1.1 christos 63 1.1 christos 64 1.1 christos void 65 1.2 christos ipf_p_netbios_main_unload(void) 66 1.1 christos { 67 1.1 christos if (netbios_proxy_init == 1) { 68 1.1 christos MUTEX_DESTROY(&netbiosfr.fr_lock); 69 1.1 christos netbios_proxy_init = 0; 70 1.1 christos } 71 1.1 christos } 72 1.1 christos 73 1.1 christos 74 1.1 christos int 75 1.2 christos ipf_p_netbios_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat) 76 1.1 christos { 77 1.1 christos char dgmbuf[6]; 78 1.1 christos int off, dlen; 79 1.1 christos udphdr_t *udp; 80 1.1 christos ip_t *ip; 81 1.1 christos mb_t *m; 82 1.1 christos 83 1.1 christos aps = aps; /* LINT */ 84 1.1 christos nat = nat; /* LINT */ 85 1.1 christos 86 1.1 christos m = fin->fin_m; 87 1.1 christos dlen = fin->fin_dlen - sizeof(*udp); 88 1.1 christos /* 89 1.1 christos * no net bios datagram could possibly be shorter than this 90 1.1 christos */ 91 1.1 christos if (dlen < 11) 92 1.1 christos return 0; 93 1.1 christos 94 1.1 christos ip = fin->fin_ip; 95 1.1 christos udp = (udphdr_t *)fin->fin_dp; 96 1.1 christos off = (char *)udp - (char *)ip + sizeof(*udp) + fin->fin_ipoff; 97 1.1 christos 98 1.1 christos /* 99 1.1 christos * move past the 100 1.1 christos * ip header; 101 1.1 christos * udp header; 102 1.1 christos * 4 bytes into the net bios dgm header. 103 1.1 christos * According to rfc1002, this should be the exact location of 104 1.1 christos * the source address/port 105 1.1 christos */ 106 1.1 christos off += 4; 107 1.1 christos 108 1.1 christos /* Copy NATed source Address/port*/ 109 1.1 christos dgmbuf[0] = (char)((ip->ip_src.s_addr ) &0xFF); 110 1.1 christos dgmbuf[1] = (char)((ip->ip_src.s_addr >> 8) &0xFF); 111 1.1 christos dgmbuf[2] = (char)((ip->ip_src.s_addr >> 16)&0xFF); 112 1.1 christos dgmbuf[3] = (char)((ip->ip_src.s_addr >> 24)&0xFF); 113 1.1 christos 114 1.1 christos dgmbuf[4] = (char)((udp->uh_sport )&0xFF); 115 1.1 christos dgmbuf[5] = (char)((udp->uh_sport >> 8)&0xFF); 116 1.1 christos 117 1.1 christos /* replace data in packet */ 118 1.1 christos COPYBACK(m, off, sizeof(dgmbuf), dgmbuf); 119 1.1 christos 120 1.1 christos return 0; 121 1.1 christos } 122