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