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 <linux/ipv6.h>
20 #include <linux/smp.h>
23 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
24 #include <net/netfilter/nf_tables.h>
25 #include <net/netfilter/nft_meta.h>
27 void nft_meta_get_eval(const struct nft_expr
*expr
,
28 struct nft_regs
*regs
,
29 const struct nft_pktinfo
*pkt
)
31 const struct nft_meta
*priv
= nft_expr_priv(expr
);
32 const struct sk_buff
*skb
= pkt
->skb
;
33 const struct net_device
*in
= pkt
->in
, *out
= pkt
->out
;
35 u32
*dest
= ®s
->data
[priv
->dreg
];
41 case NFT_META_PROTOCOL
:
43 *(__be16
*)dest
= skb
->protocol
;
45 case NFT_META_NFPROTO
:
48 case NFT_META_L4PROTO
:
51 case NFT_META_PRIORITY
:
52 *dest
= skb
->priority
;
67 case NFT_META_IIFNAME
:
70 strncpy((char *)dest
, in
->name
, IFNAMSIZ
);
72 case NFT_META_OIFNAME
:
75 strncpy((char *)dest
, out
->name
, IFNAMSIZ
);
77 case NFT_META_IIFTYPE
:
81 *(u16
*)dest
= in
->type
;
83 case NFT_META_OIFTYPE
:
87 *(u16
*)dest
= out
->type
;
90 sk
= skb_to_full_sk(skb
);
91 if (!sk
|| !sk_fullsock(sk
))
94 read_lock_bh(&sk
->sk_callback_lock
);
95 if (sk
->sk_socket
== NULL
||
96 sk
->sk_socket
->file
== NULL
) {
97 read_unlock_bh(&sk
->sk_callback_lock
);
101 *dest
= from_kuid_munged(&init_user_ns
,
102 sk
->sk_socket
->file
->f_cred
->fsuid
);
103 read_unlock_bh(&sk
->sk_callback_lock
);
106 sk
= skb_to_full_sk(skb
);
107 if (!sk
|| !sk_fullsock(sk
))
110 read_lock_bh(&sk
->sk_callback_lock
);
111 if (sk
->sk_socket
== NULL
||
112 sk
->sk_socket
->file
== NULL
) {
113 read_unlock_bh(&sk
->sk_callback_lock
);
116 *dest
= from_kgid_munged(&init_user_ns
,
117 sk
->sk_socket
->file
->f_cred
->fsgid
);
118 read_unlock_bh(&sk
->sk_callback_lock
);
120 #ifdef CONFIG_IP_ROUTE_CLASSID
121 case NFT_META_RTCLASSID
: {
122 const struct dst_entry
*dst
= skb_dst(skb
);
126 *dest
= dst
->tclassid
;
130 #ifdef CONFIG_NETWORK_SECMARK
131 case NFT_META_SECMARK
:
132 *dest
= skb
->secmark
;
135 case NFT_META_PKTTYPE
:
136 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
137 *dest
= skb
->pkt_type
;
143 if (ipv4_is_multicast(ip_hdr(skb
)->daddr
))
144 *dest
= PACKET_MULTICAST
;
146 *dest
= PACKET_BROADCAST
;
149 if (ipv6_hdr(skb
)->daddr
.s6_addr
[0] == 0xFF)
150 *dest
= PACKET_MULTICAST
;
152 *dest
= PACKET_BROADCAST
;
160 *dest
= raw_smp_processor_id();
162 case NFT_META_IIFGROUP
:
167 case NFT_META_OIFGROUP
:
172 #ifdef CONFIG_CGROUP_NET_CLASSID
173 case NFT_META_CGROUP
:
174 sk
= skb_to_full_sk(skb
);
175 if (!sk
|| !sk_fullsock(sk
))
177 *dest
= sk
->sk_classid
;
187 regs
->verdict
.code
= NFT_BREAK
;
189 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
191 void nft_meta_set_eval(const struct nft_expr
*expr
,
192 struct nft_regs
*regs
,
193 const struct nft_pktinfo
*pkt
)
195 const struct nft_meta
*meta
= nft_expr_priv(expr
);
196 struct sk_buff
*skb
= pkt
->skb
;
197 u32 value
= regs
->data
[meta
->sreg
];
203 case NFT_META_PRIORITY
:
204 skb
->priority
= value
;
206 case NFT_META_NFTRACE
:
213 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
215 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
216 [NFTA_META_DREG
] = { .type
= NLA_U32
},
217 [NFTA_META_KEY
] = { .type
= NLA_U32
},
218 [NFTA_META_SREG
] = { .type
= NLA_U32
},
220 EXPORT_SYMBOL_GPL(nft_meta_policy
);
222 int nft_meta_get_init(const struct nft_ctx
*ctx
,
223 const struct nft_expr
*expr
,
224 const struct nlattr
* const tb
[])
226 struct nft_meta
*priv
= nft_expr_priv(expr
);
229 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
231 case NFT_META_PROTOCOL
:
232 case NFT_META_IIFTYPE
:
233 case NFT_META_OIFTYPE
:
236 case NFT_META_NFPROTO
:
237 case NFT_META_L4PROTO
:
239 case NFT_META_PRIORITY
:
245 #ifdef CONFIG_IP_ROUTE_CLASSID
246 case NFT_META_RTCLASSID
:
248 #ifdef CONFIG_NETWORK_SECMARK
249 case NFT_META_SECMARK
:
251 case NFT_META_PKTTYPE
:
253 case NFT_META_IIFGROUP
:
254 case NFT_META_OIFGROUP
:
255 #ifdef CONFIG_CGROUP_NET_CLASSID
256 case NFT_META_CGROUP
:
260 case NFT_META_IIFNAME
:
261 case NFT_META_OIFNAME
:
268 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
269 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
270 NFT_DATA_VALUE
, len
);
272 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
274 int nft_meta_set_init(const struct nft_ctx
*ctx
,
275 const struct nft_expr
*expr
,
276 const struct nlattr
* const tb
[])
278 struct nft_meta
*priv
= nft_expr_priv(expr
);
282 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
285 case NFT_META_PRIORITY
:
288 case NFT_META_NFTRACE
:
295 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
296 err
= nft_validate_register_load(priv
->sreg
, len
);
302 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
304 int nft_meta_get_dump(struct sk_buff
*skb
,
305 const struct nft_expr
*expr
)
307 const struct nft_meta
*priv
= nft_expr_priv(expr
);
309 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
310 goto nla_put_failure
;
311 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
312 goto nla_put_failure
;
318 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
320 int nft_meta_set_dump(struct sk_buff
*skb
,
321 const struct nft_expr
*expr
)
323 const struct nft_meta
*priv
= nft_expr_priv(expr
);
325 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
326 goto nla_put_failure
;
327 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
328 goto nla_put_failure
;
335 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
337 static struct nft_expr_type nft_meta_type
;
338 static const struct nft_expr_ops nft_meta_get_ops
= {
339 .type
= &nft_meta_type
,
340 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
341 .eval
= nft_meta_get_eval
,
342 .init
= nft_meta_get_init
,
343 .dump
= nft_meta_get_dump
,
346 static const struct nft_expr_ops nft_meta_set_ops
= {
347 .type
= &nft_meta_type
,
348 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
349 .eval
= nft_meta_set_eval
,
350 .init
= nft_meta_set_init
,
351 .dump
= nft_meta_set_dump
,
354 static const struct nft_expr_ops
*
355 nft_meta_select_ops(const struct nft_ctx
*ctx
,
356 const struct nlattr
* const tb
[])
358 if (tb
[NFTA_META_KEY
] == NULL
)
359 return ERR_PTR(-EINVAL
);
361 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
362 return ERR_PTR(-EINVAL
);
364 if (tb
[NFTA_META_DREG
])
365 return &nft_meta_get_ops
;
367 if (tb
[NFTA_META_SREG
])
368 return &nft_meta_set_ops
;
370 return ERR_PTR(-EINVAL
);
373 static struct nft_expr_type nft_meta_type __read_mostly
= {
375 .select_ops
= &nft_meta_select_ops
,
376 .policy
= nft_meta_policy
,
377 .maxattr
= NFTA_META_MAX
,
378 .owner
= THIS_MODULE
,
381 static int __init
nft_meta_module_init(void)
383 return nft_register_expr(&nft_meta_type
);
386 static void __exit
nft_meta_module_exit(void)
388 nft_unregister_expr(&nft_meta_type
);
391 module_init(nft_meta_module_init
);
392 module_exit(nft_meta_module_exit
);
394 MODULE_LICENSE("GPL");
395 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
396 MODULE_ALIAS_NFT_EXPR("meta");