Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/rculist.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/netlink.h> |
| 17 | #include <linux/netfilter.h> |
| 18 | #include <linux/netfilter/nfnetlink.h> |
| 19 | #include <linux/netfilter/nf_tables.h> |
| 20 | #include <net/netfilter/nf_tables_core.h> |
| 21 | #include <net/netfilter/nf_tables.h> |
| 22 | |
Patrick McHardy | cb7dbfd | 2013-10-10 23:35:40 +0200 | [diff] [blame] | 23 | static void nft_cmp_fast_eval(const struct nft_expr *expr, |
| 24 | struct nft_data data[NFT_REG_MAX + 1]) |
| 25 | { |
| 26 | const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr); |
| 27 | u32 mask; |
| 28 | |
| 29 | mask = ~0U >> (sizeof(priv->data) * BITS_PER_BYTE - priv->len); |
| 30 | if ((data[priv->sreg].data[0] & mask) == priv->data) |
| 31 | return; |
| 32 | data[NFT_REG_VERDICT].verdict = NFT_BREAK; |
| 33 | } |
| 34 | |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 35 | static bool nft_payload_fast_eval(const struct nft_expr *expr, |
| 36 | struct nft_data data[NFT_REG_MAX + 1], |
| 37 | const struct nft_pktinfo *pkt) |
| 38 | { |
| 39 | const struct nft_payload *priv = nft_expr_priv(expr); |
| 40 | const struct sk_buff *skb = pkt->skb; |
| 41 | struct nft_data *dest = &data[priv->dreg]; |
| 42 | unsigned char *ptr; |
| 43 | |
| 44 | if (priv->base == NFT_PAYLOAD_NETWORK_HEADER) |
| 45 | ptr = skb_network_header(skb); |
| 46 | else |
Pablo Neira Ayuso | c54032e | 2013-10-11 10:00:22 +0200 | [diff] [blame^] | 47 | ptr = skb_network_header(skb) + pkt->xt.thoff; |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 48 | |
| 49 | ptr += priv->offset; |
| 50 | |
| 51 | if (unlikely(ptr + priv->len >= skb_tail_pointer(skb))) |
| 52 | return false; |
| 53 | |
| 54 | if (priv->len == 2) |
| 55 | *(u16 *)dest->data = *(u16 *)ptr; |
| 56 | else if (priv->len == 4) |
| 57 | *(u32 *)dest->data = *(u32 *)ptr; |
| 58 | else |
| 59 | *(u8 *)dest->data = *(u8 *)ptr; |
| 60 | return true; |
| 61 | } |
| 62 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 63 | struct nft_jumpstack { |
| 64 | const struct nft_chain *chain; |
| 65 | const struct nft_rule *rule; |
| 66 | }; |
| 67 | |
| 68 | static inline void |
| 69 | nft_chain_stats(const struct nft_chain *this, const struct nft_pktinfo *pkt, |
| 70 | struct nft_jumpstack *jumpstack, unsigned int stackptr) |
| 71 | { |
| 72 | struct nft_stats __percpu *stats; |
| 73 | const struct nft_chain *chain = stackptr ? jumpstack[0].chain : this; |
| 74 | |
| 75 | rcu_read_lock_bh(); |
| 76 | stats = rcu_dereference(nft_base_chain(chain)->stats); |
| 77 | __this_cpu_inc(stats->pkts); |
| 78 | __this_cpu_add(stats->bytes, pkt->skb->len); |
| 79 | rcu_read_unlock_bh(); |
| 80 | } |
| 81 | |
| 82 | unsigned int |
| 83 | nft_do_chain_pktinfo(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 84 | { |
| 85 | const struct nft_chain *chain = ops->priv; |
| 86 | const struct nft_rule *rule; |
| 87 | const struct nft_expr *expr, *last; |
| 88 | struct nft_data data[NFT_REG_MAX + 1]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 89 | unsigned int stackptr = 0; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 90 | struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 91 | |
| 92 | do_chain: |
| 93 | rule = list_entry(&chain->rules, struct nft_rule, list); |
| 94 | next_rule: |
| 95 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
| 96 | list_for_each_entry_continue_rcu(rule, &chain->rules, list) { |
| 97 | nft_rule_for_each_expr(expr, last, rule) { |
Patrick McHardy | cb7dbfd | 2013-10-10 23:35:40 +0200 | [diff] [blame] | 98 | if (expr->ops == &nft_cmp_fast_ops) |
| 99 | nft_cmp_fast_eval(expr, data); |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 100 | else if (expr->ops != &nft_payload_fast_ops || |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 101 | !nft_payload_fast_eval(expr, data, pkt)) |
| 102 | expr->ops->eval(expr, data, pkt); |
Patrick McHardy | cb7dbfd | 2013-10-10 23:35:40 +0200 | [diff] [blame] | 103 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 104 | if (data[NFT_REG_VERDICT].verdict != NFT_CONTINUE) |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | switch (data[NFT_REG_VERDICT].verdict) { |
| 109 | case NFT_BREAK: |
| 110 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
| 111 | /* fall through */ |
| 112 | case NFT_CONTINUE: |
| 113 | continue; |
| 114 | } |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | switch (data[NFT_REG_VERDICT].verdict) { |
| 119 | case NF_ACCEPT: |
| 120 | case NF_DROP: |
| 121 | case NF_QUEUE: |
| 122 | return data[NFT_REG_VERDICT].verdict; |
| 123 | case NFT_JUMP: |
| 124 | BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE); |
| 125 | jumpstack[stackptr].chain = chain; |
| 126 | jumpstack[stackptr].rule = rule; |
| 127 | stackptr++; |
| 128 | /* fall through */ |
| 129 | case NFT_GOTO: |
| 130 | chain = data[NFT_REG_VERDICT].chain; |
| 131 | goto do_chain; |
| 132 | case NFT_RETURN: |
| 133 | case NFT_CONTINUE: |
| 134 | break; |
| 135 | default: |
| 136 | WARN_ON(1); |
| 137 | } |
| 138 | |
| 139 | if (stackptr > 0) { |
| 140 | stackptr--; |
| 141 | chain = jumpstack[stackptr].chain; |
| 142 | rule = jumpstack[stackptr].rule; |
| 143 | goto next_rule; |
| 144 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 145 | nft_chain_stats(chain, pkt, jumpstack, stackptr); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 146 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 147 | return nft_base_chain(chain)->policy; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 148 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 149 | EXPORT_SYMBOL_GPL(nft_do_chain_pktinfo); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 150 | |
| 151 | int __init nf_tables_core_module_init(void) |
| 152 | { |
| 153 | int err; |
| 154 | |
| 155 | err = nft_immediate_module_init(); |
| 156 | if (err < 0) |
| 157 | goto err1; |
| 158 | |
| 159 | err = nft_cmp_module_init(); |
| 160 | if (err < 0) |
| 161 | goto err2; |
| 162 | |
| 163 | err = nft_lookup_module_init(); |
| 164 | if (err < 0) |
| 165 | goto err3; |
| 166 | |
| 167 | err = nft_bitwise_module_init(); |
| 168 | if (err < 0) |
| 169 | goto err4; |
| 170 | |
| 171 | err = nft_byteorder_module_init(); |
| 172 | if (err < 0) |
| 173 | goto err5; |
| 174 | |
| 175 | err = nft_payload_module_init(); |
| 176 | if (err < 0) |
| 177 | goto err6; |
| 178 | |
| 179 | return 0; |
| 180 | |
| 181 | err6: |
| 182 | nft_byteorder_module_exit(); |
| 183 | err5: |
| 184 | nft_bitwise_module_exit(); |
| 185 | err4: |
| 186 | nft_lookup_module_exit(); |
| 187 | err3: |
| 188 | nft_cmp_module_exit(); |
| 189 | err2: |
| 190 | nft_immediate_module_exit(); |
| 191 | err1: |
| 192 | return err; |
| 193 | } |
| 194 | |
| 195 | void nf_tables_core_module_exit(void) |
| 196 | { |
| 197 | nft_payload_module_exit(); |
| 198 | nft_byteorder_module_exit(); |
| 199 | nft_bitwise_module_exit(); |
| 200 | nft_lookup_module_exit(); |
| 201 | nft_cmp_module_exit(); |
| 202 | nft_immediate_module_exit(); |
| 203 | } |