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 case NFT_META_CGROUP
:
170 if (skb
->sk
== NULL
|| !sk_fullsock(skb
->sk
))
172 *dest
= skb
->sk
->sk_classid
;
181 regs
->verdict
.code
= NFT_BREAK
;
183 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
185 void nft_meta_set_eval(const struct nft_expr
*expr
,
186 struct nft_regs
*regs
,
187 const struct nft_pktinfo
*pkt
)
189 const struct nft_meta
*meta
= nft_expr_priv(expr
);
190 struct sk_buff
*skb
= pkt
->skb
;
191 u32 value
= regs
->data
[meta
->sreg
];
197 case NFT_META_PRIORITY
:
198 skb
->priority
= value
;
200 case NFT_META_NFTRACE
:
207 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
209 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
210 [NFTA_META_DREG
] = { .type
= NLA_U32
},
211 [NFTA_META_KEY
] = { .type
= NLA_U32
},
212 [NFTA_META_SREG
] = { .type
= NLA_U32
},
214 EXPORT_SYMBOL_GPL(nft_meta_policy
);
216 int nft_meta_get_init(const struct nft_ctx
*ctx
,
217 const struct nft_expr
*expr
,
218 const struct nlattr
* const tb
[])
220 struct nft_meta
*priv
= nft_expr_priv(expr
);
223 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
225 case NFT_META_PROTOCOL
:
226 case NFT_META_IIFTYPE
:
227 case NFT_META_OIFTYPE
:
230 case NFT_META_NFPROTO
:
231 case NFT_META_L4PROTO
:
233 case NFT_META_PRIORITY
:
239 #ifdef CONFIG_IP_ROUTE_CLASSID
240 case NFT_META_RTCLASSID
:
242 #ifdef CONFIG_NETWORK_SECMARK
243 case NFT_META_SECMARK
:
245 case NFT_META_PKTTYPE
:
247 case NFT_META_IIFGROUP
:
248 case NFT_META_OIFGROUP
:
249 case NFT_META_CGROUP
:
252 case NFT_META_IIFNAME
:
253 case NFT_META_OIFNAME
:
260 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
261 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
262 NFT_DATA_VALUE
, len
);
264 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
266 int nft_meta_set_init(const struct nft_ctx
*ctx
,
267 const struct nft_expr
*expr
,
268 const struct nlattr
* const tb
[])
270 struct nft_meta
*priv
= nft_expr_priv(expr
);
274 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
277 case NFT_META_PRIORITY
:
280 case NFT_META_NFTRACE
:
287 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
288 err
= nft_validate_register_load(priv
->sreg
, len
);
294 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
296 int nft_meta_get_dump(struct sk_buff
*skb
,
297 const struct nft_expr
*expr
)
299 const struct nft_meta
*priv
= nft_expr_priv(expr
);
301 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
302 goto nla_put_failure
;
303 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
304 goto nla_put_failure
;
310 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
312 int nft_meta_set_dump(struct sk_buff
*skb
,
313 const struct nft_expr
*expr
)
315 const struct nft_meta
*priv
= nft_expr_priv(expr
);
317 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
318 goto nla_put_failure
;
319 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
320 goto nla_put_failure
;
327 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
329 static struct nft_expr_type nft_meta_type
;
330 static const struct nft_expr_ops nft_meta_get_ops
= {
331 .type
= &nft_meta_type
,
332 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
333 .eval
= nft_meta_get_eval
,
334 .init
= nft_meta_get_init
,
335 .dump
= nft_meta_get_dump
,
338 static const struct nft_expr_ops nft_meta_set_ops
= {
339 .type
= &nft_meta_type
,
340 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
341 .eval
= nft_meta_set_eval
,
342 .init
= nft_meta_set_init
,
343 .dump
= nft_meta_set_dump
,
346 static const struct nft_expr_ops
*
347 nft_meta_select_ops(const struct nft_ctx
*ctx
,
348 const struct nlattr
* const tb
[])
350 if (tb
[NFTA_META_KEY
] == NULL
)
351 return ERR_PTR(-EINVAL
);
353 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
354 return ERR_PTR(-EINVAL
);
356 if (tb
[NFTA_META_DREG
])
357 return &nft_meta_get_ops
;
359 if (tb
[NFTA_META_SREG
])
360 return &nft_meta_set_ops
;
362 return ERR_PTR(-EINVAL
);
365 static struct nft_expr_type nft_meta_type __read_mostly
= {
367 .select_ops
= &nft_meta_select_ops
,
368 .policy
= nft_meta_policy
,
369 .maxattr
= NFTA_META_MAX
,
370 .owner
= THIS_MODULE
,
373 static int __init
nft_meta_module_init(void)
375 return nft_register_expr(&nft_meta_type
);
378 static void __exit
nft_meta_module_exit(void)
380 nft_unregister_expr(&nft_meta_type
);
383 module_init(nft_meta_module_init
);
384 module_exit(nft_meta_module_exit
);
386 MODULE_LICENSE("GPL");
387 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
388 MODULE_ALIAS_NFT_EXPR("meta");