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
;
34 u32
*dest
= ®s
->data
[priv
->dreg
];
40 case NFT_META_PROTOCOL
:
42 *(__be16
*)dest
= skb
->protocol
;
44 case NFT_META_NFPROTO
:
47 case NFT_META_L4PROTO
:
50 case NFT_META_PRIORITY
:
51 *dest
= skb
->priority
;
66 case NFT_META_IIFNAME
:
69 strncpy((char *)dest
, in
->name
, IFNAMSIZ
);
71 case NFT_META_OIFNAME
:
74 strncpy((char *)dest
, out
->name
, IFNAMSIZ
);
76 case NFT_META_IIFTYPE
:
80 *(u16
*)dest
= in
->type
;
82 case NFT_META_OIFTYPE
:
86 *(u16
*)dest
= out
->type
;
89 if (skb
->sk
== NULL
|| !sk_fullsock(skb
->sk
))
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
);
99 *dest
= from_kuid_munged(&init_user_ns
,
100 skb
->sk
->sk_socket
->file
->f_cred
->fsuid
);
101 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
104 if (skb
->sk
== NULL
|| !sk_fullsock(skb
->sk
))
107 read_lock_bh(&skb
->sk
->sk_callback_lock
);
108 if (skb
->sk
->sk_socket
== NULL
||
109 skb
->sk
->sk_socket
->file
== NULL
) {
110 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
113 *dest
= from_kgid_munged(&init_user_ns
,
114 skb
->sk
->sk_socket
->file
->f_cred
->fsgid
);
115 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
117 #ifdef CONFIG_IP_ROUTE_CLASSID
118 case NFT_META_RTCLASSID
: {
119 const struct dst_entry
*dst
= skb_dst(skb
);
123 *dest
= dst
->tclassid
;
127 #ifdef CONFIG_NETWORK_SECMARK
128 case NFT_META_SECMARK
:
129 *dest
= skb
->secmark
;
132 case NFT_META_PKTTYPE
:
133 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
134 *dest
= skb
->pkt_type
;
138 switch (pkt
->ops
->pf
) {
140 if (ipv4_is_multicast(ip_hdr(skb
)->daddr
))
141 *dest
= PACKET_MULTICAST
;
143 *dest
= PACKET_BROADCAST
;
146 if (ipv6_hdr(skb
)->daddr
.s6_addr
[0] == 0xFF)
147 *dest
= PACKET_MULTICAST
;
149 *dest
= PACKET_BROADCAST
;
157 *dest
= raw_smp_processor_id();
159 case NFT_META_IIFGROUP
:
164 case NFT_META_OIFGROUP
:
169 #ifdef CONFIG_CGROUP_NET_CLASSID
170 case NFT_META_CGROUP
:
171 if (skb
->sk
== NULL
|| !sk_fullsock(skb
->sk
))
173 *dest
= skb
->sk
->sk_classid
;
183 regs
->verdict
.code
= NFT_BREAK
;
185 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
187 void nft_meta_set_eval(const struct nft_expr
*expr
,
188 struct nft_regs
*regs
,
189 const struct nft_pktinfo
*pkt
)
191 const struct nft_meta
*meta
= nft_expr_priv(expr
);
192 struct sk_buff
*skb
= pkt
->skb
;
193 u32 value
= regs
->data
[meta
->sreg
];
199 case NFT_META_PRIORITY
:
200 skb
->priority
= value
;
202 case NFT_META_NFTRACE
:
209 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
211 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
212 [NFTA_META_DREG
] = { .type
= NLA_U32
},
213 [NFTA_META_KEY
] = { .type
= NLA_U32
},
214 [NFTA_META_SREG
] = { .type
= NLA_U32
},
216 EXPORT_SYMBOL_GPL(nft_meta_policy
);
218 int nft_meta_get_init(const struct nft_ctx
*ctx
,
219 const struct nft_expr
*expr
,
220 const struct nlattr
* const tb
[])
222 struct nft_meta
*priv
= nft_expr_priv(expr
);
225 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
227 case NFT_META_PROTOCOL
:
228 case NFT_META_IIFTYPE
:
229 case NFT_META_OIFTYPE
:
232 case NFT_META_NFPROTO
:
233 case NFT_META_L4PROTO
:
235 case NFT_META_PRIORITY
:
241 #ifdef CONFIG_IP_ROUTE_CLASSID
242 case NFT_META_RTCLASSID
:
244 #ifdef CONFIG_NETWORK_SECMARK
245 case NFT_META_SECMARK
:
247 case NFT_META_PKTTYPE
:
249 case NFT_META_IIFGROUP
:
250 case NFT_META_OIFGROUP
:
251 #ifdef CONFIG_CGROUP_NET_CLASSID
252 case NFT_META_CGROUP
:
256 case NFT_META_IIFNAME
:
257 case NFT_META_OIFNAME
:
264 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
265 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
266 NFT_DATA_VALUE
, len
);
268 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
270 int nft_meta_set_init(const struct nft_ctx
*ctx
,
271 const struct nft_expr
*expr
,
272 const struct nlattr
* const tb
[])
274 struct nft_meta
*priv
= nft_expr_priv(expr
);
278 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
281 case NFT_META_PRIORITY
:
284 case NFT_META_NFTRACE
:
291 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
292 err
= nft_validate_register_load(priv
->sreg
, len
);
298 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
300 int nft_meta_get_dump(struct sk_buff
*skb
,
301 const struct nft_expr
*expr
)
303 const struct nft_meta
*priv
= nft_expr_priv(expr
);
305 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
306 goto nla_put_failure
;
307 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
308 goto nla_put_failure
;
314 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
316 int nft_meta_set_dump(struct sk_buff
*skb
,
317 const struct nft_expr
*expr
)
319 const struct nft_meta
*priv
= nft_expr_priv(expr
);
321 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
322 goto nla_put_failure
;
323 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
324 goto nla_put_failure
;
331 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
333 static struct nft_expr_type nft_meta_type
;
334 static const struct nft_expr_ops nft_meta_get_ops
= {
335 .type
= &nft_meta_type
,
336 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
337 .eval
= nft_meta_get_eval
,
338 .init
= nft_meta_get_init
,
339 .dump
= nft_meta_get_dump
,
342 static const struct nft_expr_ops nft_meta_set_ops
= {
343 .type
= &nft_meta_type
,
344 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
345 .eval
= nft_meta_set_eval
,
346 .init
= nft_meta_set_init
,
347 .dump
= nft_meta_set_dump
,
350 static const struct nft_expr_ops
*
351 nft_meta_select_ops(const struct nft_ctx
*ctx
,
352 const struct nlattr
* const tb
[])
354 if (tb
[NFTA_META_KEY
] == NULL
)
355 return ERR_PTR(-EINVAL
);
357 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
358 return ERR_PTR(-EINVAL
);
360 if (tb
[NFTA_META_DREG
])
361 return &nft_meta_get_ops
;
363 if (tb
[NFTA_META_SREG
])
364 return &nft_meta_set_ops
;
366 return ERR_PTR(-EINVAL
);
369 static struct nft_expr_type nft_meta_type __read_mostly
= {
371 .select_ops
= &nft_meta_select_ops
,
372 .policy
= nft_meta_policy
,
373 .maxattr
= NFTA_META_MAX
,
374 .owner
= THIS_MODULE
,
377 static int __init
nft_meta_module_init(void)
379 return nft_register_expr(&nft_meta_type
);
382 static void __exit
nft_meta_module_exit(void)
384 nft_unregister_expr(&nft_meta_type
);
387 module_init(nft_meta_module_init
);
388 module_exit(nft_meta_module_exit
);
390 MODULE_LICENSE("GPL");
391 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
392 MODULE_ALIAS_NFT_EXPR("meta");