Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 1 | /* netfilter.c: look after the filters for various protocols. |
| 2 | * Heavily influenced by the old firewall.c by David Bonn and Alan Cox. |
| 3 | * |
| 4 | * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any |
| 5 | * way. |
| 6 | * |
| 7 | * Rusty Russell (C)2000 -- This code is GPL. |
| 8 | * |
| 9 | * February 2000: Modified by James Morris to have 1 queue per protocol. |
| 10 | * 15-Mar-2000: Added NF_REPEAT --RR. |
| 11 | * 08-May-2003: Internal logging interface added by Jozsef Kadlecsik. |
| 12 | */ |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/netfilter.h> |
| 15 | #include <net/protocol.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/wait.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/if.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/inetdevice.h> |
| 24 | #include <linux/proc_fs.h> |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 26 | #include <net/sock.h> |
| 27 | |
| 28 | #include "nf_internals.h" |
| 29 | |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 30 | static DEFINE_MUTEX(afinfo_mutex); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 31 | |
Martin Josefsson | e2b7606 | 2006-11-29 02:35:04 +0100 | [diff] [blame] | 32 | struct nf_afinfo *nf_afinfo[NPROTO] __read_mostly; |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 33 | EXPORT_SYMBOL(nf_afinfo); |
| 34 | |
| 35 | int nf_register_afinfo(struct nf_afinfo *afinfo) |
| 36 | { |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 37 | int err; |
| 38 | |
| 39 | err = mutex_lock_interruptible(&afinfo_mutex); |
| 40 | if (err < 0) |
| 41 | return err; |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 42 | rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo); |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 43 | mutex_unlock(&afinfo_mutex); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(nf_register_afinfo); |
| 47 | |
| 48 | void nf_unregister_afinfo(struct nf_afinfo *afinfo) |
| 49 | { |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 50 | mutex_lock(&afinfo_mutex); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 51 | rcu_assign_pointer(nf_afinfo[afinfo->family], NULL); |
Patrick McHardy | d486dd1 | 2007-02-12 11:09:55 -0800 | [diff] [blame] | 52 | mutex_unlock(&afinfo_mutex); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 53 | synchronize_rcu(); |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(nf_unregister_afinfo); |
| 56 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 57 | /* In this code, we can be waiting indefinitely for userspace to |
| 58 | * service a packet if a hook returns NF_QUEUE. We could keep a count |
| 59 | * of skbuffs queued for userspace, and not deregister a hook unless |
| 60 | * this is zero, but that sucks. Now, we simply check when the |
| 61 | * packets come back: if the hook is gone, the packet is discarded. */ |
Martin Josefsson | e2b7606 | 2006-11-29 02:35:04 +0100 | [diff] [blame] | 62 | struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS] __read_mostly; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 63 | EXPORT_SYMBOL(nf_hooks); |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame^] | 64 | static DEFINE_MUTEX(nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 65 | |
| 66 | int nf_register_hook(struct nf_hook_ops *reg) |
| 67 | { |
| 68 | struct list_head *i; |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame^] | 69 | int err; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 70 | |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame^] | 71 | err = mutex_lock_interruptible(&nf_hook_mutex); |
| 72 | if (err < 0) |
| 73 | return err; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 74 | list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) { |
| 75 | if (reg->priority < ((struct nf_hook_ops *)i)->priority) |
| 76 | break; |
| 77 | } |
| 78 | list_add_rcu(®->list, i->prev); |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame^] | 79 | mutex_unlock(&nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | EXPORT_SYMBOL(nf_register_hook); |
| 83 | |
| 84 | void nf_unregister_hook(struct nf_hook_ops *reg) |
| 85 | { |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame^] | 86 | mutex_lock(&nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 87 | list_del_rcu(®->list); |
Patrick McHardy | fd706d6 | 2007-02-12 11:10:14 -0800 | [diff] [blame^] | 88 | mutex_unlock(&nf_hook_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 89 | |
| 90 | synchronize_net(); |
| 91 | } |
| 92 | EXPORT_SYMBOL(nf_unregister_hook); |
| 93 | |
Patrick McHardy | 972d1cb | 2006-04-06 14:09:12 -0700 | [diff] [blame] | 94 | int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n) |
| 95 | { |
| 96 | unsigned int i; |
| 97 | int err = 0; |
| 98 | |
| 99 | for (i = 0; i < n; i++) { |
| 100 | err = nf_register_hook(®[i]); |
| 101 | if (err) |
| 102 | goto err; |
| 103 | } |
| 104 | return err; |
| 105 | |
| 106 | err: |
| 107 | if (i > 0) |
| 108 | nf_unregister_hooks(reg, i); |
| 109 | return err; |
| 110 | } |
| 111 | EXPORT_SYMBOL(nf_register_hooks); |
| 112 | |
| 113 | void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n) |
| 114 | { |
| 115 | unsigned int i; |
| 116 | |
| 117 | for (i = 0; i < n; i++) |
| 118 | nf_unregister_hook(®[i]); |
| 119 | } |
| 120 | EXPORT_SYMBOL(nf_unregister_hooks); |
| 121 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 122 | unsigned int nf_iterate(struct list_head *head, |
| 123 | struct sk_buff **skb, |
| 124 | int hook, |
| 125 | const struct net_device *indev, |
| 126 | const struct net_device *outdev, |
| 127 | struct list_head **i, |
| 128 | int (*okfn)(struct sk_buff *), |
| 129 | int hook_thresh) |
| 130 | { |
| 131 | unsigned int verdict; |
| 132 | |
| 133 | /* |
| 134 | * The caller must not block between calls to this |
| 135 | * function because of risk of continuing from deleted element. |
| 136 | */ |
| 137 | list_for_each_continue_rcu(*i, head) { |
| 138 | struct nf_hook_ops *elem = (struct nf_hook_ops *)*i; |
| 139 | |
| 140 | if (hook_thresh > elem->priority) |
| 141 | continue; |
| 142 | |
| 143 | /* Optimization: we don't need to hold module |
| 144 | reference here, since function can't sleep. --RR */ |
| 145 | verdict = elem->hook(hook, skb, indev, outdev, okfn); |
| 146 | if (verdict != NF_ACCEPT) { |
| 147 | #ifdef CONFIG_NETFILTER_DEBUG |
| 148 | if (unlikely((verdict & NF_VERDICT_MASK) |
| 149 | > NF_MAX_VERDICT)) { |
| 150 | NFDEBUG("Evil return from %p(%u).\n", |
| 151 | elem->hook, hook); |
| 152 | continue; |
| 153 | } |
| 154 | #endif |
| 155 | if (verdict != NF_REPEAT) |
| 156 | return verdict; |
| 157 | *i = (*i)->prev; |
| 158 | } |
| 159 | } |
| 160 | return NF_ACCEPT; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /* Returns 1 if okfn() needs to be executed by the caller, |
| 165 | * -EPERM for NF_DROP, 0 otherwise. */ |
| 166 | int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb, |
| 167 | struct net_device *indev, |
| 168 | struct net_device *outdev, |
| 169 | int (*okfn)(struct sk_buff *), |
| 170 | int hook_thresh) |
| 171 | { |
| 172 | struct list_head *elem; |
| 173 | unsigned int verdict; |
| 174 | int ret = 0; |
| 175 | |
| 176 | /* We may already have this, but read-locks nest anyway */ |
| 177 | rcu_read_lock(); |
| 178 | |
| 179 | elem = &nf_hooks[pf][hook]; |
| 180 | next_hook: |
| 181 | verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev, |
| 182 | outdev, &elem, okfn, hook_thresh); |
| 183 | if (verdict == NF_ACCEPT || verdict == NF_STOP) { |
| 184 | ret = 1; |
| 185 | goto unlock; |
| 186 | } else if (verdict == NF_DROP) { |
| 187 | kfree_skb(*pskb); |
| 188 | ret = -EPERM; |
| 189 | } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { |
| 190 | NFDEBUG("nf_hook: Verdict = QUEUE.\n"); |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 191 | if (!nf_queue(*pskb, elem, pf, hook, indev, outdev, okfn, |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 192 | verdict >> NF_VERDICT_BITS)) |
| 193 | goto next_hook; |
| 194 | } |
| 195 | unlock: |
| 196 | rcu_read_unlock(); |
| 197 | return ret; |
| 198 | } |
| 199 | EXPORT_SYMBOL(nf_hook_slow); |
| 200 | |
| 201 | |
| 202 | int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len) |
| 203 | { |
| 204 | struct sk_buff *nskb; |
| 205 | |
| 206 | if (writable_len > (*pskb)->len) |
| 207 | return 0; |
| 208 | |
| 209 | /* Not exclusive use of packet? Must copy. */ |
| 210 | if (skb_shared(*pskb) || skb_cloned(*pskb)) |
| 211 | goto copy_skb; |
| 212 | |
| 213 | return pskb_may_pull(*pskb, writable_len); |
| 214 | |
| 215 | copy_skb: |
| 216 | nskb = skb_copy(*pskb, GFP_ATOMIC); |
| 217 | if (!nskb) |
| 218 | return 0; |
| 219 | BUG_ON(skb_is_nonlinear(nskb)); |
| 220 | |
| 221 | /* Rest of kernel will get very unhappy if we pass it a |
| 222 | suddenly-orphaned skbuff */ |
| 223 | if ((*pskb)->sk) |
| 224 | skb_set_owner_w(nskb, (*pskb)->sk); |
| 225 | kfree_skb(*pskb); |
| 226 | *pskb = nskb; |
| 227 | return 1; |
| 228 | } |
| 229 | EXPORT_SYMBOL(skb_make_writable); |
| 230 | |
Al Viro | 43bc0ca | 2006-11-14 21:43:23 -0800 | [diff] [blame] | 231 | void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb, |
| 232 | __be32 from, __be32 to, int pseudohdr) |
Patrick McHardy | 4cf411d | 2006-08-05 00:58:33 -0700 | [diff] [blame] | 233 | { |
Al Viro | 43bc0ca | 2006-11-14 21:43:23 -0800 | [diff] [blame] | 234 | __be32 diff[] = { ~from, to }; |
Patrick McHardy | 4cf411d | 2006-08-05 00:58:33 -0700 | [diff] [blame] | 235 | if (skb->ip_summed != CHECKSUM_PARTIAL) { |
Al Viro | 43bc0ca | 2006-11-14 21:43:23 -0800 | [diff] [blame] | 236 | *sum = csum_fold(csum_partial((char *)diff, sizeof(diff), |
| 237 | ~csum_unfold(*sum))); |
Patrick McHardy | 4cf411d | 2006-08-05 00:58:33 -0700 | [diff] [blame] | 238 | if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr) |
Al Viro | 43bc0ca | 2006-11-14 21:43:23 -0800 | [diff] [blame] | 239 | skb->csum = ~csum_partial((char *)diff, sizeof(diff), |
| 240 | ~skb->csum); |
Patrick McHardy | 4cf411d | 2006-08-05 00:58:33 -0700 | [diff] [blame] | 241 | } else if (pseudohdr) |
Al Viro | 43bc0ca | 2006-11-14 21:43:23 -0800 | [diff] [blame] | 242 | *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff), |
| 243 | csum_unfold(*sum))); |
Patrick McHardy | 4cf411d | 2006-08-05 00:58:33 -0700 | [diff] [blame] | 244 | } |
Al Viro | 43bc0ca | 2006-11-14 21:43:23 -0800 | [diff] [blame] | 245 | EXPORT_SYMBOL(nf_proto_csum_replace4); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 246 | |
| 247 | /* This does not belong here, but locally generated errors need it if connection |
| 248 | tracking in use: without this, connection may not be in hash table, and hence |
| 249 | manufactured ICMP or RST packets will not be associated with it. */ |
| 250 | void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); |
| 251 | EXPORT_SYMBOL(ip_ct_attach); |
| 252 | |
| 253 | void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) |
| 254 | { |
| 255 | void (*attach)(struct sk_buff *, struct sk_buff *); |
| 256 | |
Patrick McHardy | c3a47ab | 2007-02-12 11:09:19 -0800 | [diff] [blame] | 257 | if (skb->nfct) { |
| 258 | rcu_read_lock(); |
| 259 | attach = rcu_dereference(ip_ct_attach); |
| 260 | if (attach) |
| 261 | attach(new, skb); |
| 262 | rcu_read_unlock(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | EXPORT_SYMBOL(nf_ct_attach); |
| 266 | |
| 267 | #ifdef CONFIG_PROC_FS |
| 268 | struct proc_dir_entry *proc_net_netfilter; |
| 269 | EXPORT_SYMBOL(proc_net_netfilter); |
| 270 | #endif |
| 271 | |
| 272 | void __init netfilter_init(void) |
| 273 | { |
| 274 | int i, h; |
| 275 | for (i = 0; i < NPROTO; i++) { |
| 276 | for (h = 0; h < NF_MAX_HOOKS; h++) |
| 277 | INIT_LIST_HEAD(&nf_hooks[i][h]); |
| 278 | } |
| 279 | |
| 280 | #ifdef CONFIG_PROC_FS |
| 281 | proc_net_netfilter = proc_mkdir("netfilter", proc_net); |
| 282 | if (!proc_net_netfilter) |
| 283 | panic("cannot create netfilter proc entry"); |
| 284 | #endif |
| 285 | |
| 286 | if (netfilter_queue_init() < 0) |
| 287 | panic("cannot initialize nf_queue"); |
| 288 | if (netfilter_log_init() < 0) |
| 289 | panic("cannot initialize nf_log"); |
| 290 | } |