Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 1 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 2 | #include <linux/slab.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 3 | #include <linux/init.h> |
| 4 | #include <linux/module.h> |
| 5 | #include <linux/proc_fs.h> |
| 6 | #include <linux/skbuff.h> |
| 7 | #include <linux/netfilter.h> |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 8 | #include <linux/seq_file.h> |
Patrick McHardy | 7a11b98 | 2006-02-27 13:03:24 -0800 | [diff] [blame] | 9 | #include <linux/rcupdate.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 10 | #include <net/protocol.h> |
Patrick McHardy | c01cd42 | 2007-12-05 01:24:48 -0800 | [diff] [blame] | 11 | #include <net/netfilter/nf_queue.h> |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 12 | #include <net/dst.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 13 | |
| 14 | #include "nf_internals.h" |
| 15 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 16 | /* |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 17 | * A queue handler may be registered for each protocol. Each is protected by |
| 18 | * long term mutex. The handler must provide an an outfn() to accept packets |
| 19 | * for queueing and must reinject all packets it receives, no matter what. |
| 20 | */ |
Arnd Bergmann | 0906a37 | 2010-03-09 20:59:15 +0100 | [diff] [blame] | 21 | static const struct nf_queue_handler __rcu *queue_handler[NFPROTO_NUMPROTO] __read_mostly; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 22 | |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 23 | static DEFINE_MUTEX(queue_handler_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 24 | |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 25 | /* return EBUSY when somebody else is registered, return EEXIST if the |
| 26 | * same handler is registered, return 0 in case of success. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 27 | int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 28 | { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 29 | int ret; |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 30 | const struct nf_queue_handler *old; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 31 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 32 | if (pf >= ARRAY_SIZE(queue_handler)) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 33 | return -EINVAL; |
| 34 | |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 35 | mutex_lock(&queue_handler_mutex); |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 36 | old = rcu_dereference_protected(queue_handler[pf], |
| 37 | lockdep_is_held(&queue_handler_mutex)); |
| 38 | if (old == qh) |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 39 | ret = -EEXIST; |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 40 | else if (old) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 41 | ret = -EBUSY; |
| 42 | else { |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 43 | rcu_assign_pointer(queue_handler[pf], qh); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 44 | ret = 0; |
| 45 | } |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 46 | mutex_unlock(&queue_handler_mutex); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | EXPORT_SYMBOL(nf_register_queue_handler); |
| 51 | |
| 52 | /* The caller must flush their queue before this */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 53 | int nf_unregister_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 54 | { |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 55 | const struct nf_queue_handler *old; |
| 56 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 57 | if (pf >= ARRAY_SIZE(queue_handler)) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 58 | return -EINVAL; |
| 59 | |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 60 | mutex_lock(&queue_handler_mutex); |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 61 | old = rcu_dereference_protected(queue_handler[pf], |
| 62 | lockdep_is_held(&queue_handler_mutex)); |
| 63 | if (old && old != qh) { |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 64 | mutex_unlock(&queue_handler_mutex); |
Yasuyuki Kozakai | ce7663d | 2007-07-07 22:40:08 -0700 | [diff] [blame] | 65 | return -EINVAL; |
| 66 | } |
| 67 | |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 68 | RCU_INIT_POINTER(queue_handler[pf], NULL); |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 69 | mutex_unlock(&queue_handler_mutex); |
| 70 | |
| 71 | synchronize_rcu(); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 72 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | EXPORT_SYMBOL(nf_unregister_queue_handler); |
| 76 | |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 77 | void nf_unregister_queue_handlers(const struct nf_queue_handler *qh) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 78 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 79 | u_int8_t pf; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 80 | |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 81 | mutex_lock(&queue_handler_mutex); |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 82 | for (pf = 0; pf < ARRAY_SIZE(queue_handler); pf++) { |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 83 | if (rcu_dereference_protected( |
| 84 | queue_handler[pf], |
| 85 | lockdep_is_held(&queue_handler_mutex) |
| 86 | ) == qh) |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 87 | RCU_INIT_POINTER(queue_handler[pf], NULL); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 88 | } |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 89 | mutex_unlock(&queue_handler_mutex); |
| 90 | |
| 91 | synchronize_rcu(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 92 | } |
| 93 | EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers); |
| 94 | |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 95 | static void nf_queue_entry_release_refs(struct nf_queue_entry *entry) |
| 96 | { |
| 97 | /* Release those devices we held, or Alexey will kill me. */ |
| 98 | if (entry->indev) |
| 99 | dev_put(entry->indev); |
| 100 | if (entry->outdev) |
| 101 | dev_put(entry->outdev); |
| 102 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 103 | if (entry->skb->nf_bridge) { |
| 104 | struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge; |
| 105 | |
| 106 | if (nf_bridge->physindev) |
| 107 | dev_put(nf_bridge->physindev); |
| 108 | if (nf_bridge->physoutdev) |
| 109 | dev_put(nf_bridge->physoutdev); |
| 110 | } |
| 111 | #endif |
| 112 | /* Drop reference to owner of hook which queued us. */ |
| 113 | module_put(entry->elem->owner); |
| 114 | } |
| 115 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 116 | /* |
| 117 | * Any packet that leaves via this function must come back |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 118 | * through nf_reinject(). |
| 119 | */ |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 120 | static int __nf_queue(struct sk_buff *skb, |
| 121 | struct list_head *elem, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 122 | u_int8_t pf, unsigned int hook, |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 123 | struct net_device *indev, |
| 124 | struct net_device *outdev, |
| 125 | int (*okfn)(struct sk_buff *), |
| 126 | unsigned int queuenum) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 127 | { |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 128 | int status = -ENOENT; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 129 | struct nf_queue_entry *entry = NULL; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 130 | #ifdef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 131 | struct net_device *physindev; |
| 132 | struct net_device *physoutdev; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 133 | #endif |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 134 | const struct nf_afinfo *afinfo; |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 135 | const struct nf_queue_handler *qh; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 136 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 137 | /* QUEUE == DROP if no one is waiting, to be safe. */ |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 138 | rcu_read_lock(); |
| 139 | |
| 140 | qh = rcu_dereference(queue_handler[pf]); |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 141 | if (!qh) { |
| 142 | status = -ESRCH; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 143 | goto err_unlock; |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 144 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 145 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 146 | afinfo = nf_get_afinfo(pf); |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 147 | if (!afinfo) |
| 148 | goto err_unlock; |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 149 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 150 | entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 151 | if (!entry) { |
| 152 | status = -ENOMEM; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 153 | goto err_unlock; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 154 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 155 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 156 | *entry = (struct nf_queue_entry) { |
| 157 | .skb = skb, |
| 158 | .elem = list_entry(elem, struct nf_hook_ops, list), |
| 159 | .pf = pf, |
| 160 | .hook = hook, |
| 161 | .indev = indev, |
| 162 | .outdev = outdev, |
| 163 | .okfn = okfn, |
| 164 | }; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 165 | |
| 166 | /* If it's going away, ignore hook. */ |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 167 | if (!try_module_get(entry->elem->owner)) { |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 168 | status = -ECANCELED; |
| 169 | goto err_unlock; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 170 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 171 | /* Bump dev refs so they don't vanish while packet is out */ |
Patrick McHardy | 8b1cf0d | 2007-12-05 01:23:17 -0800 | [diff] [blame] | 172 | if (indev) |
| 173 | dev_hold(indev); |
| 174 | if (outdev) |
| 175 | dev_hold(outdev); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 176 | #ifdef CONFIG_BRIDGE_NETFILTER |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 177 | if (skb->nf_bridge) { |
| 178 | physindev = skb->nf_bridge->physindev; |
Patrick McHardy | 8b1cf0d | 2007-12-05 01:23:17 -0800 | [diff] [blame] | 179 | if (physindev) |
| 180 | dev_hold(physindev); |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 181 | physoutdev = skb->nf_bridge->physoutdev; |
Patrick McHardy | 8b1cf0d | 2007-12-05 01:23:17 -0800 | [diff] [blame] | 182 | if (physoutdev) |
| 183 | dev_hold(physoutdev); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 184 | } |
| 185 | #endif |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 186 | skb_dst_force(skb); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 187 | afinfo->saveroute(skb, entry); |
| 188 | status = qh->outfn(entry, queuenum); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 189 | |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 190 | rcu_read_unlock(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 191 | |
| 192 | if (status < 0) { |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 193 | nf_queue_entry_release_refs(entry); |
| 194 | goto err; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 197 | return 0; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 198 | |
| 199 | err_unlock: |
| 200 | rcu_read_unlock(); |
| 201 | err: |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 202 | kfree(entry); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 203 | return status; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 206 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 207 | /* When called from bridge netfilter, skb->data must point to MAC header |
| 208 | * before calling skb_gso_segment(). Else, original MAC header is lost |
| 209 | * and segmented skbs will be sent to wrong destination. |
| 210 | */ |
| 211 | static void nf_bridge_adjust_skb_data(struct sk_buff *skb) |
| 212 | { |
| 213 | if (skb->nf_bridge) |
| 214 | __skb_push(skb, skb->network_header - skb->mac_header); |
| 215 | } |
| 216 | |
| 217 | static void nf_bridge_adjust_segmented_data(struct sk_buff *skb) |
| 218 | { |
| 219 | if (skb->nf_bridge) |
| 220 | __skb_pull(skb, skb->network_header - skb->mac_header); |
| 221 | } |
| 222 | #else |
| 223 | #define nf_bridge_adjust_skb_data(s) do {} while (0) |
| 224 | #define nf_bridge_adjust_segmented_data(s) do {} while (0) |
| 225 | #endif |
| 226 | |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 227 | int nf_queue(struct sk_buff *skb, |
| 228 | struct list_head *elem, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 229 | u_int8_t pf, unsigned int hook, |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 230 | struct net_device *indev, |
| 231 | struct net_device *outdev, |
| 232 | int (*okfn)(struct sk_buff *), |
| 233 | unsigned int queuenum) |
| 234 | { |
| 235 | struct sk_buff *segs; |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 236 | int err = -EINVAL; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 237 | unsigned int queued; |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 238 | |
| 239 | if (!skb_is_gso(skb)) |
| 240 | return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn, |
| 241 | queuenum); |
| 242 | |
| 243 | switch (pf) { |
Jan Engelhardt | 4b1e27e | 2009-04-14 14:26:49 +0200 | [diff] [blame] | 244 | case NFPROTO_IPV4: |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 245 | skb->protocol = htons(ETH_P_IP); |
| 246 | break; |
Jan Engelhardt | 4b1e27e | 2009-04-14 14:26:49 +0200 | [diff] [blame] | 247 | case NFPROTO_IPV6: |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 248 | skb->protocol = htons(ETH_P_IPV6); |
| 249 | break; |
| 250 | } |
| 251 | |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 252 | nf_bridge_adjust_skb_data(skb); |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 253 | segs = skb_gso_segment(skb, 0); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 254 | /* Does not use PTR_ERR to limit the number of error codes that can be |
| 255 | * returned by nf_queue. For instance, callers rely on -ECANCELED to mean |
| 256 | * 'ignore this hook'. |
| 257 | */ |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 258 | if (IS_ERR(segs)) |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 259 | goto out_err; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 260 | queued = 0; |
| 261 | err = 0; |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 262 | do { |
| 263 | struct sk_buff *nskb = segs->next; |
| 264 | |
| 265 | segs->next = NULL; |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 266 | if (err == 0) { |
| 267 | nf_bridge_adjust_segmented_data(segs); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 268 | err = __nf_queue(segs, elem, pf, hook, indev, |
| 269 | outdev, okfn, queuenum); |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 270 | } |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 271 | if (err == 0) |
| 272 | queued++; |
| 273 | else |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 274 | kfree_skb(segs); |
| 275 | segs = nskb; |
| 276 | } while (segs); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 277 | |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 278 | if (queued) { |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 279 | kfree_skb(skb); |
Florian Westphal | a8db7b2 | 2012-02-06 13:23:10 +0100 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | out_err: |
| 283 | nf_bridge_adjust_segmented_data(skb); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 284 | return err; |
Patrick McHardy | 394f545 | 2006-08-05 00:58:52 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 287 | void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 288 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 289 | struct sk_buff *skb = entry->skb; |
| 290 | struct list_head *elem = &entry->elem->list; |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 291 | const struct nf_afinfo *afinfo; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 292 | int err; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 293 | |
| 294 | rcu_read_lock(); |
| 295 | |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 296 | nf_queue_entry_release_refs(entry); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 297 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 298 | /* Continue traversal iff userspace said ok... */ |
| 299 | if (verdict == NF_REPEAT) { |
| 300 | elem = elem->prev; |
| 301 | verdict = NF_ACCEPT; |
| 302 | } |
| 303 | |
| 304 | if (verdict == NF_ACCEPT) { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 305 | afinfo = nf_get_afinfo(entry->pf); |
| 306 | if (!afinfo || afinfo->reroute(skb, entry) < 0) |
Patrick McHardy | 7a11b98 | 2006-02-27 13:03:24 -0800 | [diff] [blame] | 307 | verdict = NF_DROP; |
| 308 | } |
| 309 | |
| 310 | if (verdict == NF_ACCEPT) { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 311 | next_hook: |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 312 | verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook], |
| 313 | skb, entry->hook, |
| 314 | entry->indev, entry->outdev, &elem, |
| 315 | entry->okfn, INT_MIN); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | switch (verdict & NF_VERDICT_MASK) { |
| 319 | case NF_ACCEPT: |
Patrick McHardy | 3bc3871 | 2006-07-24 22:52:47 -0700 | [diff] [blame] | 320 | case NF_STOP: |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 321 | local_bh_disable(); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 322 | entry->okfn(skb); |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 323 | local_bh_enable(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 324 | break; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 325 | case NF_QUEUE: |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 326 | err = __nf_queue(skb, elem, entry->pf, entry->hook, |
| 327 | entry->indev, entry->outdev, entry->okfn, |
Florian Westphal | f615df7 | 2011-01-18 15:52:14 +0100 | [diff] [blame] | 328 | verdict >> NF_VERDICT_QBITS); |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 329 | if (err < 0) { |
| 330 | if (err == -ECANCELED) |
| 331 | goto next_hook; |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 332 | if (err == -ESRCH && |
| 333 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) |
| 334 | goto next_hook; |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 335 | kfree_skb(skb); |
| 336 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 337 | break; |
Eric Dumazet | 64507fd | 2010-02-19 15:28:38 +0100 | [diff] [blame] | 338 | case NF_STOLEN: |
Julian Anastasov | fad5444 | 2011-08-05 00:36:28 +0000 | [diff] [blame] | 339 | break; |
Patrick McHardy | 3bc3871 | 2006-07-24 22:52:47 -0700 | [diff] [blame] | 340 | default: |
| 341 | kfree_skb(skb); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 342 | } |
| 343 | rcu_read_unlock(); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 344 | kfree(entry); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 345 | } |
| 346 | EXPORT_SYMBOL(nf_reinject); |
| 347 | |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 348 | #ifdef CONFIG_PROC_FS |
| 349 | static void *seq_start(struct seq_file *seq, loff_t *pos) |
| 350 | { |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 351 | if (*pos >= ARRAY_SIZE(queue_handler)) |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 352 | return NULL; |
| 353 | |
| 354 | return pos; |
| 355 | } |
| 356 | |
| 357 | static void *seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 358 | { |
| 359 | (*pos)++; |
| 360 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 361 | if (*pos >= ARRAY_SIZE(queue_handler)) |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 362 | return NULL; |
| 363 | |
| 364 | return pos; |
| 365 | } |
| 366 | |
| 367 | static void seq_stop(struct seq_file *s, void *v) |
| 368 | { |
| 369 | |
| 370 | } |
| 371 | |
| 372 | static int seq_show(struct seq_file *s, void *v) |
| 373 | { |
| 374 | int ret; |
| 375 | loff_t *pos = v; |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 376 | const struct nf_queue_handler *qh; |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 377 | |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 378 | rcu_read_lock(); |
| 379 | qh = rcu_dereference(queue_handler[*pos]); |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 380 | if (!qh) |
| 381 | ret = seq_printf(s, "%2lld NONE\n", *pos); |
| 382 | else |
| 383 | ret = seq_printf(s, "%2lld %s\n", *pos, qh->name); |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 384 | rcu_read_unlock(); |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 389 | static const struct seq_operations nfqueue_seq_ops = { |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 390 | .start = seq_start, |
| 391 | .next = seq_next, |
| 392 | .stop = seq_stop, |
| 393 | .show = seq_show, |
| 394 | }; |
| 395 | |
| 396 | static int nfqueue_open(struct inode *inode, struct file *file) |
| 397 | { |
| 398 | return seq_open(file, &nfqueue_seq_ops); |
| 399 | } |
| 400 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 401 | static const struct file_operations nfqueue_file_ops = { |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 402 | .owner = THIS_MODULE, |
| 403 | .open = nfqueue_open, |
| 404 | .read = seq_read, |
| 405 | .llseek = seq_lseek, |
| 406 | .release = seq_release, |
| 407 | }; |
| 408 | #endif /* PROC_FS */ |
| 409 | |
| 410 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 411 | int __init netfilter_queue_init(void) |
| 412 | { |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 413 | #ifdef CONFIG_PROC_FS |
Denis V. Lunev | 8eeee8b | 2008-03-27 16:55:53 -0700 | [diff] [blame] | 414 | if (!proc_create("nf_queue", S_IRUGO, |
| 415 | proc_net_netfilter, &nfqueue_file_ops)) |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 416 | return -1; |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 417 | #endif |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |