2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netlink.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/nf_tables.h>
19 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
20 #include <net/netfilter/nf_tables.h>
23 enum nft_meta_keys key
:8;
25 enum nft_registers dreg
:8;
26 enum nft_registers sreg
:8;
30 static void nft_meta_get_eval(const struct nft_expr
*expr
,
31 struct nft_data data
[NFT_REG_MAX
+ 1],
32 const struct nft_pktinfo
*pkt
)
34 const struct nft_meta
*priv
= nft_expr_priv(expr
);
35 const struct sk_buff
*skb
= pkt
->skb
;
36 const struct net_device
*in
= pkt
->in
, *out
= pkt
->out
;
37 struct nft_data
*dest
= &data
[priv
->dreg
];
41 dest
->data
[0] = skb
->len
;
43 case NFT_META_PROTOCOL
:
44 *(__be16
*)dest
->data
= skb
->protocol
;
46 case NFT_META_NFPROTO
:
47 dest
->data
[0] = pkt
->ops
->pf
;
49 case NFT_META_L4PROTO
:
50 dest
->data
[0] = pkt
->tprot
;
52 case NFT_META_PRIORITY
:
53 dest
->data
[0] = skb
->priority
;
56 dest
->data
[0] = skb
->mark
;
61 dest
->data
[0] = in
->ifindex
;
66 dest
->data
[0] = out
->ifindex
;
68 case NFT_META_IIFNAME
:
71 strncpy((char *)dest
->data
, in
->name
, sizeof(dest
->data
));
73 case NFT_META_OIFNAME
:
76 strncpy((char *)dest
->data
, out
->name
, sizeof(dest
->data
));
78 case NFT_META_IIFTYPE
:
81 *(u16
*)dest
->data
= in
->type
;
83 case NFT_META_OIFTYPE
:
86 *(u16
*)dest
->data
= out
->type
;
89 if (skb
->sk
== NULL
|| skb
->sk
->sk_state
== TCP_TIME_WAIT
)
92 read_lock_bh(&skb
->sk
->sk_callback_lock
);
93 if (skb
->sk
->sk_socket
== NULL
||
94 skb
->sk
->sk_socket
->file
== NULL
) {
95 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
100 from_kuid_munged(&init_user_ns
,
101 skb
->sk
->sk_socket
->file
->f_cred
->fsuid
);
102 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
105 if (skb
->sk
== NULL
|| skb
->sk
->sk_state
== TCP_TIME_WAIT
)
108 read_lock_bh(&skb
->sk
->sk_callback_lock
);
109 if (skb
->sk
->sk_socket
== NULL
||
110 skb
->sk
->sk_socket
->file
== NULL
) {
111 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
115 from_kgid_munged(&init_user_ns
,
116 skb
->sk
->sk_socket
->file
->f_cred
->fsgid
);
117 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
119 #ifdef CONFIG_NET_CLS_ROUTE
120 case NFT_META_RTCLASSID
: {
121 const struct dst_entry
*dst
= skb_dst(skb
);
125 dest
->data
[0] = dst
->tclassid
;
129 #ifdef CONFIG_NETWORK_SECMARK
130 case NFT_META_SECMARK
:
131 dest
->data
[0] = skb
->secmark
;
141 data
[NFT_REG_VERDICT
].verdict
= NFT_BREAK
;
144 static void nft_meta_set_eval(const struct nft_expr
*expr
,
145 struct nft_data data
[NFT_REG_MAX
+ 1],
146 const struct nft_pktinfo
*pkt
)
148 const struct nft_meta
*meta
= nft_expr_priv(expr
);
149 struct sk_buff
*skb
= pkt
->skb
;
150 u32 value
= data
[meta
->sreg
].data
[0];
156 case NFT_META_PRIORITY
:
157 skb
->priority
= value
;
159 case NFT_META_NFTRACE
:
167 static const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
168 [NFTA_META_DREG
] = { .type
= NLA_U32
},
169 [NFTA_META_KEY
] = { .type
= NLA_U32
},
170 [NFTA_META_SREG
] = { .type
= NLA_U32
},
173 static int nft_meta_init_validate_set(uint32_t key
)
177 case NFT_META_PRIORITY
:
178 case NFT_META_NFTRACE
:
185 static int nft_meta_init_validate_get(uint32_t key
)
189 case NFT_META_PROTOCOL
:
190 case NFT_META_NFPROTO
:
191 case NFT_META_L4PROTO
:
192 case NFT_META_PRIORITY
:
196 case NFT_META_IIFNAME
:
197 case NFT_META_OIFNAME
:
198 case NFT_META_IIFTYPE
:
199 case NFT_META_OIFTYPE
:
202 #ifdef CONFIG_NET_CLS_ROUTE
203 case NFT_META_RTCLASSID
:
205 #ifdef CONFIG_NETWORK_SECMARK
206 case NFT_META_SECMARK
:
215 static int nft_meta_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
216 const struct nlattr
* const tb
[])
218 struct nft_meta
*priv
= nft_expr_priv(expr
);
221 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
223 if (tb
[NFTA_META_DREG
]) {
224 err
= nft_meta_init_validate_get(priv
->key
);
228 priv
->dreg
= ntohl(nla_get_be32(tb
[NFTA_META_DREG
]));
229 err
= nft_validate_output_register(priv
->dreg
);
233 return nft_validate_data_load(ctx
, priv
->dreg
, NULL
,
237 err
= nft_meta_init_validate_set(priv
->key
);
241 priv
->sreg
= ntohl(nla_get_be32(tb
[NFTA_META_SREG
]));
242 err
= nft_validate_input_register(priv
->sreg
);
249 static int nft_meta_get_dump(struct sk_buff
*skb
,
250 const struct nft_expr
*expr
)
252 const struct nft_meta
*priv
= nft_expr_priv(expr
);
254 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
255 goto nla_put_failure
;
256 if (nla_put_be32(skb
, NFTA_META_DREG
, htonl(priv
->dreg
)))
257 goto nla_put_failure
;
264 static int nft_meta_set_dump(struct sk_buff
*skb
,
265 const struct nft_expr
*expr
)
267 const struct nft_meta
*priv
= nft_expr_priv(expr
);
269 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
270 goto nla_put_failure
;
271 if (nla_put_be32(skb
, NFTA_META_SREG
, htonl(priv
->sreg
)))
272 goto nla_put_failure
;
280 static struct nft_expr_type nft_meta_type
;
281 static const struct nft_expr_ops nft_meta_get_ops
= {
282 .type
= &nft_meta_type
,
283 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
284 .eval
= nft_meta_get_eval
,
285 .init
= nft_meta_init
,
286 .dump
= nft_meta_get_dump
,
289 static const struct nft_expr_ops nft_meta_set_ops
= {
290 .type
= &nft_meta_type
,
291 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
292 .eval
= nft_meta_set_eval
,
293 .init
= nft_meta_init
,
294 .dump
= nft_meta_set_dump
,
297 static const struct nft_expr_ops
*
298 nft_meta_select_ops(const struct nft_ctx
*ctx
,
299 const struct nlattr
* const tb
[])
301 if (tb
[NFTA_META_KEY
] == NULL
)
302 return ERR_PTR(-EINVAL
);
304 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
305 return ERR_PTR(-EINVAL
);
307 if (tb
[NFTA_META_DREG
])
308 return &nft_meta_get_ops
;
310 if (tb
[NFTA_META_SREG
])
311 return &nft_meta_set_ops
;
313 return ERR_PTR(-EINVAL
);
316 static struct nft_expr_type nft_meta_type __read_mostly
= {
318 .select_ops
= &nft_meta_select_ops
,
319 .policy
= nft_meta_policy
,
320 .maxattr
= NFTA_META_MAX
,
321 .owner
= THIS_MODULE
,
324 static int __init
nft_meta_module_init(void)
326 return nft_register_expr(&nft_meta_type
);
329 static void __exit
nft_meta_module_exit(void)
331 nft_unregister_expr(&nft_meta_type
);
334 module_init(nft_meta_module_init
);
335 module_exit(nft_meta_module_exit
);
337 MODULE_LICENSE("GPL");
338 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
339 MODULE_ALIAS_NFT_EXPR("meta");