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 |
| 47 | ptr = skb_transport_header(skb); |
| 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 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 63 | unsigned int nft_do_chain(const struct nf_hook_ops *ops, |
| 64 | struct sk_buff *skb, |
| 65 | const struct net_device *in, |
| 66 | const struct net_device *out, |
| 67 | int (*okfn)(struct sk_buff *)) |
| 68 | { |
| 69 | const struct nft_chain *chain = ops->priv; |
| 70 | const struct nft_rule *rule; |
| 71 | const struct nft_expr *expr, *last; |
| 72 | struct nft_data data[NFT_REG_MAX + 1]; |
| 73 | const struct nft_pktinfo pkt = { |
| 74 | .skb = skb, |
| 75 | .in = in, |
| 76 | .out = out, |
| 77 | .hooknum = ops->hooknum, |
| 78 | }; |
| 79 | unsigned int stackptr = 0; |
| 80 | struct { |
| 81 | const struct nft_chain *chain; |
| 82 | const struct nft_rule *rule; |
| 83 | } jumpstack[NFT_JUMP_STACK_SIZE]; |
| 84 | |
| 85 | do_chain: |
| 86 | rule = list_entry(&chain->rules, struct nft_rule, list); |
| 87 | next_rule: |
| 88 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
| 89 | list_for_each_entry_continue_rcu(rule, &chain->rules, list) { |
| 90 | nft_rule_for_each_expr(expr, last, rule) { |
Patrick McHardy | cb7dbfd | 2013-10-10 23:35:40 +0200 | [diff] [blame] | 91 | if (expr->ops == &nft_cmp_fast_ops) |
| 92 | nft_cmp_fast_eval(expr, data); |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame^] | 93 | else if (expr->ops != &nft_payload_fast_ops || |
| 94 | !nft_payload_fast_eval(expr, data, &pkt)) |
Patrick McHardy | cb7dbfd | 2013-10-10 23:35:40 +0200 | [diff] [blame] | 95 | expr->ops->eval(expr, data, &pkt); |
| 96 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 97 | if (data[NFT_REG_VERDICT].verdict != NFT_CONTINUE) |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | switch (data[NFT_REG_VERDICT].verdict) { |
| 102 | case NFT_BREAK: |
| 103 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
| 104 | /* fall through */ |
| 105 | case NFT_CONTINUE: |
| 106 | continue; |
| 107 | } |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | switch (data[NFT_REG_VERDICT].verdict) { |
| 112 | case NF_ACCEPT: |
| 113 | case NF_DROP: |
| 114 | case NF_QUEUE: |
| 115 | return data[NFT_REG_VERDICT].verdict; |
| 116 | case NFT_JUMP: |
| 117 | BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE); |
| 118 | jumpstack[stackptr].chain = chain; |
| 119 | jumpstack[stackptr].rule = rule; |
| 120 | stackptr++; |
| 121 | /* fall through */ |
| 122 | case NFT_GOTO: |
| 123 | chain = data[NFT_REG_VERDICT].chain; |
| 124 | goto do_chain; |
| 125 | case NFT_RETURN: |
| 126 | case NFT_CONTINUE: |
| 127 | break; |
| 128 | default: |
| 129 | WARN_ON(1); |
| 130 | } |
| 131 | |
| 132 | if (stackptr > 0) { |
| 133 | stackptr--; |
| 134 | chain = jumpstack[stackptr].chain; |
| 135 | rule = jumpstack[stackptr].rule; |
| 136 | goto next_rule; |
| 137 | } |
| 138 | |
| 139 | return NF_ACCEPT; |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(nft_do_chain); |
| 142 | |
| 143 | int __init nf_tables_core_module_init(void) |
| 144 | { |
| 145 | int err; |
| 146 | |
| 147 | err = nft_immediate_module_init(); |
| 148 | if (err < 0) |
| 149 | goto err1; |
| 150 | |
| 151 | err = nft_cmp_module_init(); |
| 152 | if (err < 0) |
| 153 | goto err2; |
| 154 | |
| 155 | err = nft_lookup_module_init(); |
| 156 | if (err < 0) |
| 157 | goto err3; |
| 158 | |
| 159 | err = nft_bitwise_module_init(); |
| 160 | if (err < 0) |
| 161 | goto err4; |
| 162 | |
| 163 | err = nft_byteorder_module_init(); |
| 164 | if (err < 0) |
| 165 | goto err5; |
| 166 | |
| 167 | err = nft_payload_module_init(); |
| 168 | if (err < 0) |
| 169 | goto err6; |
| 170 | |
| 171 | return 0; |
| 172 | |
| 173 | err6: |
| 174 | nft_byteorder_module_exit(); |
| 175 | err5: |
| 176 | nft_bitwise_module_exit(); |
| 177 | err4: |
| 178 | nft_lookup_module_exit(); |
| 179 | err3: |
| 180 | nft_cmp_module_exit(); |
| 181 | err2: |
| 182 | nft_immediate_module_exit(); |
| 183 | err1: |
| 184 | return err; |
| 185 | } |
| 186 | |
| 187 | void nf_tables_core_module_exit(void) |
| 188 | { |
| 189 | nft_payload_module_exit(); |
| 190 | nft_byteorder_module_exit(); |
| 191 | nft_bitwise_module_exit(); |
| 192 | nft_lookup_module_exit(); |
| 193 | nft_cmp_module_exit(); |
| 194 | nft_immediate_module_exit(); |
| 195 | } |