Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * NetBIOS name service broadcast connection tracking helper |
| 3 | * |
| 4 | * (c) 2005 Patrick McHardy <kaber@trash.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | /* |
| 12 | * This helper tracks locally originating NetBIOS name service |
| 13 | * requests by issuing permanent expectations (valid until |
| 14 | * timing out) matching all reply connections from the |
| 15 | * destination network. The only NetBIOS specific thing is |
| 16 | * actually the port number. |
| 17 | */ |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/inetdevice.h> |
| 24 | #include <linux/if_addr.h> |
| 25 | #include <linux/in.h> |
| 26 | #include <linux/ip.h> |
Yasuyuki Kozakai | 1863f09 | 2006-12-02 22:12:54 -0800 | [diff] [blame] | 27 | #include <linux/netfilter.h> |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 28 | #include <net/route.h> |
| 29 | |
| 30 | #include <net/netfilter/nf_conntrack.h> |
| 31 | #include <net/netfilter/nf_conntrack_helper.h> |
| 32 | #include <net/netfilter/nf_conntrack_expect.h> |
| 33 | |
| 34 | #define NMBD_PORT 137 |
| 35 | |
| 36 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 37 | MODULE_DESCRIPTION("NetBIOS name service broadcast connection tracking helper"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | MODULE_ALIAS("ip_conntrack_netbios_ns"); |
Pablo Neira Ayuso | 4dc06f9 | 2008-11-17 16:01:42 +0100 | [diff] [blame] | 40 | MODULE_ALIAS_NFCT_HELPER("netbios_ns"); |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 41 | |
| 42 | static unsigned int timeout __read_mostly = 3; |
| 43 | module_param(timeout, uint, 0400); |
| 44 | MODULE_PARM_DESC(timeout, "timeout for master connection/replies in seconds"); |
| 45 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 46 | static int help(struct sk_buff *skb, unsigned int protoff, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 47 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 48 | { |
| 49 | struct nf_conntrack_expect *exp; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 50 | struct iphdr *iph = ip_hdr(skb); |
Eric Dumazet | ee6b967 | 2008-03-05 18:30:47 -0800 | [diff] [blame] | 51 | struct rtable *rt = skb->rtable; |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 52 | struct in_device *in_dev; |
| 53 | __be32 mask = 0; |
| 54 | |
| 55 | /* we're only interested in locally generated packets */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 56 | if (skb->sk == NULL) |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 57 | goto out; |
| 58 | if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST)) |
| 59 | goto out; |
| 60 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) |
| 61 | goto out; |
| 62 | |
| 63 | rcu_read_lock(); |
| 64 | in_dev = __in_dev_get_rcu(rt->u.dst.dev); |
| 65 | if (in_dev != NULL) { |
| 66 | for_primary_ifa(in_dev) { |
| 67 | if (ifa->ifa_broadcast == iph->daddr) { |
| 68 | mask = ifa->ifa_mask; |
| 69 | break; |
| 70 | } |
| 71 | } endfor_ifa(in_dev); |
| 72 | } |
| 73 | rcu_read_unlock(); |
| 74 | |
| 75 | if (mask == 0) |
| 76 | goto out; |
| 77 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 78 | exp = nf_ct_expect_alloc(ct); |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 79 | if (exp == NULL) |
| 80 | goto out; |
| 81 | |
| 82 | exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
| 83 | exp->tuple.src.u.udp.port = htons(NMBD_PORT); |
| 84 | |
| 85 | exp->mask.src.u3.ip = mask; |
| 86 | exp->mask.src.u.udp.port = htons(0xFFFF); |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 87 | |
| 88 | exp->expectfn = NULL; |
| 89 | exp->flags = NF_CT_EXPECT_PERMANENT; |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 90 | exp->class = NF_CT_EXPECT_CLASS_DEFAULT; |
Patrick McHardy | 88044c8 | 2007-01-09 00:03:26 -0800 | [diff] [blame] | 91 | exp->helper = NULL; |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 92 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 93 | nf_ct_expect_related(exp); |
| 94 | nf_ct_expect_put(exp); |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 95 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 96 | nf_ct_refresh(ct, skb, timeout * HZ); |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 97 | out: |
| 98 | return NF_ACCEPT; |
| 99 | } |
| 100 | |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 101 | static struct nf_conntrack_expect_policy exp_policy = { |
| 102 | .max_expected = 1, |
| 103 | }; |
| 104 | |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 105 | static struct nf_conntrack_helper helper __read_mostly = { |
| 106 | .name = "netbios-ns", |
| 107 | .tuple.src.l3num = AF_INET, |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 108 | .tuple.src.u.udp.port = cpu_to_be16(NMBD_PORT), |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 109 | .tuple.dst.protonum = IPPROTO_UDP, |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 110 | .me = THIS_MODULE, |
| 111 | .help = help, |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 112 | .expect_policy = &exp_policy, |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static int __init nf_conntrack_netbios_ns_init(void) |
| 116 | { |
Patrick McHardy | 6002f26 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 117 | exp_policy.timeout = timeout; |
Patrick McHardy | 92703ee | 2006-12-02 22:09:24 -0800 | [diff] [blame] | 118 | return nf_conntrack_helper_register(&helper); |
| 119 | } |
| 120 | |
| 121 | static void __exit nf_conntrack_netbios_ns_fini(void) |
| 122 | { |
| 123 | nf_conntrack_helper_unregister(&helper); |
| 124 | } |
| 125 | |
| 126 | module_init(nf_conntrack_netbios_ns_init); |
| 127 | module_exit(nf_conntrack_netbios_ns_fini); |