]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/netfilter/nft_meta_target.c
Merge remote-tracking branch 'net-next/master'
[karo-tx-linux.git] / net / netfilter / nft_meta_target.c
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/kernel.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/rbtree.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables.h>
19
20 struct nft_meta {
21         enum nft_meta_keys      key;
22 };
23
24 static void nft_meta_eval(const struct nft_expr *expr,
25                           struct nft_data *nfres,
26                           struct nft_data *data,
27                           const struct nft_pktinfo *pkt)
28 {
29         const struct nft_meta *meta = nft_expr_priv(expr);
30         struct sk_buff *skb = pkt->skb;
31         u32 val = data->data[0];
32
33         switch (meta->key) {
34         case NFT_META_MARK:
35                 skb->mark = val;
36                 break;
37         case NFT_META_PRIORITY:
38                 skb->priority = val;
39                 break;
40         case NFT_META_NFTRACE:
41                 skb->nf_trace = val;
42                 break;
43 #ifdef CONFIG_NETWORK_SECMARK
44         case NFT_META_SECMARK:
45                 skb->secmark = val;
46                 break;
47 #endif
48         default:
49                 WARN_ON(1);
50         }
51 }
52
53 static const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
54         [NFTA_META_KEY]         = { .type = NLA_U32 },
55 };
56
57 static int nft_meta_init(const struct nft_expr *expr, struct nlattr *tb[])
58 {
59         struct nft_meta *meta = nft_expr_priv(expr);
60
61         if (tb[NFTA_META_KEY] == NULL)
62                 return -EINVAL;
63
64         meta->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
65         switch (meta->key) {
66         case NFT_META_MARK:
67         case NFT_META_PRIORITY:
68         case NFT_META_NFTRACE:
69 #ifdef CONFIG_NETWORK_SECMARK
70         case NFT_META_SECMARK:
71 #endif
72                 break;
73         default:
74                 return -EINVAL;
75         }
76
77         return 0;
78 }
79
80 static int nft_meta_dump(struct sk_buff *skb, const struct nft_expr *expr)
81 {
82         struct nft_meta *meta = nft_expr_priv(expr);
83
84         NLA_PUT_BE32(skb, NFTA_META_KEY, htonl(meta->key));
85         return 0;
86
87 nla_put_failure:
88         return -1;
89 }
90
91 static struct nft_expr_ops meta_target __read_mostly = {
92         .name           = "meta",
93         .size           = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
94         .owner          = THIS_MODULE,
95         .eval           = nft_meta_eval,
96         .init           = nft_meta_init,
97         .dump           = nft_meta_dump,
98         .policy         = nft_meta_policy,
99         .maxattr        = NFTA_META_MAX,
100 };
101
102 static int __init nft_meta_target_init(void)
103 {
104         return nft_register_expr(&meta_target);
105 }
106
107 static void __exit nft_meta_target_exit(void)
108 {
109         nft_unregister_expr(&meta_target);
110 }
111
112 module_init(nft_meta_target_init);
113 module_exit(nft_meta_target_exit);
114
115 MODULE_LICENSE("GPL");
116 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
117 MODULE_ALIAS_NFT_EXPR("meta");