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
;
155 switch (skb
->protocol
) {
156 case htons(ETH_P_IP
): {
157 int noff
= skb_network_offset(skb
);
158 struct iphdr
*iph
, _iph
;
160 iph
= skb_header_pointer(skb
, noff
,
161 sizeof(_iph
), &_iph
);
165 if (ipv4_is_multicast(iph
->daddr
))
166 *dest
= PACKET_MULTICAST
;
168 *dest
= PACKET_BROADCAST
;
172 case htons(ETH_P_IPV6
):
173 *dest
= PACKET_MULTICAST
;
186 *dest
= raw_smp_processor_id();
188 case NFT_META_IIFGROUP
:
193 case NFT_META_OIFGROUP
:
198 #ifdef CONFIG_CGROUP_NET_CLASSID
199 case NFT_META_CGROUP
:
200 sk
= skb_to_full_sk(skb
);
201 if (!sk
|| !sk_fullsock(sk
))
203 *dest
= sk
->sk_classid
;
213 regs
->verdict
.code
= NFT_BREAK
;
215 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
217 void nft_meta_set_eval(const struct nft_expr
*expr
,
218 struct nft_regs
*regs
,
219 const struct nft_pktinfo
*pkt
)
221 const struct nft_meta
*meta
= nft_expr_priv(expr
);
222 struct sk_buff
*skb
= pkt
->skb
;
223 u32 value
= regs
->data
[meta
->sreg
];
229 case NFT_META_PRIORITY
:
230 skb
->priority
= value
;
232 case NFT_META_NFTRACE
:
239 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
241 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
242 [NFTA_META_DREG
] = { .type
= NLA_U32
},
243 [NFTA_META_KEY
] = { .type
= NLA_U32
},
244 [NFTA_META_SREG
] = { .type
= NLA_U32
},
246 EXPORT_SYMBOL_GPL(nft_meta_policy
);
248 int nft_meta_get_init(const struct nft_ctx
*ctx
,
249 const struct nft_expr
*expr
,
250 const struct nlattr
* const tb
[])
252 struct nft_meta
*priv
= nft_expr_priv(expr
);
255 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
257 case NFT_META_PROTOCOL
:
258 case NFT_META_IIFTYPE
:
259 case NFT_META_OIFTYPE
:
262 case NFT_META_NFPROTO
:
263 case NFT_META_L4PROTO
:
265 case NFT_META_PRIORITY
:
271 #ifdef CONFIG_IP_ROUTE_CLASSID
272 case NFT_META_RTCLASSID
:
274 #ifdef CONFIG_NETWORK_SECMARK
275 case NFT_META_SECMARK
:
277 case NFT_META_PKTTYPE
:
279 case NFT_META_IIFGROUP
:
280 case NFT_META_OIFGROUP
:
281 #ifdef CONFIG_CGROUP_NET_CLASSID
282 case NFT_META_CGROUP
:
286 case NFT_META_IIFNAME
:
287 case NFT_META_OIFNAME
:
294 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
295 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
296 NFT_DATA_VALUE
, len
);
298 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
300 int nft_meta_set_init(const struct nft_ctx
*ctx
,
301 const struct nft_expr
*expr
,
302 const struct nlattr
* const tb
[])
304 struct nft_meta
*priv
= nft_expr_priv(expr
);
308 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
311 case NFT_META_PRIORITY
:
314 case NFT_META_NFTRACE
:
321 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
322 err
= nft_validate_register_load(priv
->sreg
, len
);
328 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
330 int nft_meta_get_dump(struct sk_buff
*skb
,
331 const struct nft_expr
*expr
)
333 const struct nft_meta
*priv
= nft_expr_priv(expr
);
335 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
336 goto nla_put_failure
;
337 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
338 goto nla_put_failure
;
344 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
346 int nft_meta_set_dump(struct sk_buff
*skb
,
347 const struct nft_expr
*expr
)
349 const struct nft_meta
*priv
= nft_expr_priv(expr
);
351 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
352 goto nla_put_failure
;
353 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
354 goto nla_put_failure
;
361 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
363 static struct nft_expr_type nft_meta_type
;
364 static const struct nft_expr_ops nft_meta_get_ops
= {
365 .type
= &nft_meta_type
,
366 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
367 .eval
= nft_meta_get_eval
,
368 .init
= nft_meta_get_init
,
369 .dump
= nft_meta_get_dump
,
372 static const struct nft_expr_ops nft_meta_set_ops
= {
373 .type
= &nft_meta_type
,
374 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
375 .eval
= nft_meta_set_eval
,
376 .init
= nft_meta_set_init
,
377 .dump
= nft_meta_set_dump
,
380 static const struct nft_expr_ops
*
381 nft_meta_select_ops(const struct nft_ctx
*ctx
,
382 const struct nlattr
* const tb
[])
384 if (tb
[NFTA_META_KEY
] == NULL
)
385 return ERR_PTR(-EINVAL
);
387 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
388 return ERR_PTR(-EINVAL
);
390 if (tb
[NFTA_META_DREG
])
391 return &nft_meta_get_ops
;
393 if (tb
[NFTA_META_SREG
])
394 return &nft_meta_set_ops
;
396 return ERR_PTR(-EINVAL
);
399 static struct nft_expr_type nft_meta_type __read_mostly
= {
401 .select_ops
= &nft_meta_select_ops
,
402 .policy
= nft_meta_policy
,
403 .maxattr
= NFTA_META_MAX
,
404 .owner
= THIS_MODULE
,
407 static int __init
nft_meta_module_init(void)
409 return nft_register_expr(&nft_meta_type
);
412 static void __exit
nft_meta_module_exit(void)
414 nft_unregister_expr(&nft_meta_type
);
417 module_init(nft_meta_module_init
);
418 module_exit(nft_meta_module_exit
);
420 MODULE_LICENSE("GPL");
421 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
422 MODULE_ALIAS_NFT_EXPR("meta");