~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nft_meta.c

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  3  *
  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.
  7  *
  8  * Development of this code funded by Astaro AG (http://www.astaro.com/)
  9  */
 10 
 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>
 17 #include <linux/in.h>
 18 #include <linux/ip.h>
 19 #include <linux/ipv6.h>
 20 #include <linux/smp.h>
 21 #include <net/dst.h>
 22 #include <net/sock.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>
 26 
 27 void nft_meta_get_eval(const struct nft_expr *expr,
 28                        struct nft_regs *regs,
 29                        const struct nft_pktinfo *pkt)
 30 {
 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 = &regs->data[priv->dreg];
 35 
 36         switch (priv->key) {
 37         case NFT_META_LEN:
 38                 *dest = skb->len;
 39                 break;
 40         case NFT_META_PROTOCOL:
 41                 *dest = 0;
 42                 *(__be16 *)dest = skb->protocol;
 43                 break;
 44         case NFT_META_NFPROTO:
 45                 *dest = pkt->ops->pf;
 46                 break;
 47         case NFT_META_L4PROTO:
 48                 *dest = pkt->tprot;
 49                 break;
 50         case NFT_META_PRIORITY:
 51                 *dest = skb->priority;
 52                 break;
 53         case NFT_META_MARK:
 54                 *dest = skb->mark;
 55                 break;
 56         case NFT_META_IIF:
 57                 if (in == NULL)
 58                         goto err;
 59                 *dest = in->ifindex;
 60                 break;
 61         case NFT_META_OIF:
 62                 if (out == NULL)
 63                         goto err;
 64                 *dest = out->ifindex;
 65                 break;
 66         case NFT_META_IIFNAME:
 67                 if (in == NULL)
 68                         goto err;
 69                 strncpy((char *)dest, in->name, IFNAMSIZ);
 70                 break;
 71         case NFT_META_OIFNAME:
 72                 if (out == NULL)
 73                         goto err;
 74                 strncpy((char *)dest, out->name, IFNAMSIZ);
 75                 break;
 76         case NFT_META_IIFTYPE:
 77                 if (in == NULL)
 78                         goto err;
 79                 *dest = 0;
 80                 *(u16 *)dest = in->type;
 81                 break;
 82         case NFT_META_OIFTYPE:
 83                 if (out == NULL)
 84                         goto err;
 85                 *dest = 0;
 86                 *(u16 *)dest = out->type;
 87                 break;
 88         case NFT_META_SKUID:
 89                 if (skb->sk == NULL || !sk_fullsock(skb->sk))
 90                         goto err;
 91 
 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);
 96                         goto err;
 97                 }
 98 
 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);
102                 break;
103         case NFT_META_SKGID:
104                 if (skb->sk == NULL || !sk_fullsock(skb->sk))
105                         goto err;
106 
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);
111                         goto err;
112                 }
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);
116                 break;
117 #ifdef CONFIG_IP_ROUTE_CLASSID
118         case NFT_META_RTCLASSID: {
119                 const struct dst_entry *dst = skb_dst(skb);
120 
121                 if (dst == NULL)
122                         goto err;
123                 *dest = dst->tclassid;
124                 break;
125         }
126 #endif
127 #ifdef CONFIG_NETWORK_SECMARK
128         case NFT_META_SECMARK:
129                 *dest = skb->secmark;
130                 break;
131 #endif
132         case NFT_META_PKTTYPE:
133                 if (skb->pkt_type != PACKET_LOOPBACK) {
134                         *dest = skb->pkt_type;
135                         break;
136                 }
137 
138                 switch (pkt->ops->pf) {
139                 case NFPROTO_IPV4:
140                         if (ipv4_is_multicast(ip_hdr(skb)->daddr))
141                                 *dest = PACKET_MULTICAST;
142                         else
143                                 *dest = PACKET_BROADCAST;
144                         break;
145                 case NFPROTO_IPV6:
146                         if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
147                                 *dest = PACKET_MULTICAST;
148                         else
149                                 *dest = PACKET_BROADCAST;
150                         break;
151                 default:
152                         WARN_ON(1);
153                         goto err;
154                 }
155                 break;
156         case NFT_META_CPU:
157                 *dest = raw_smp_processor_id();
158                 break;
159         case NFT_META_IIFGROUP:
160                 if (in == NULL)
161                         goto err;
162                 *dest = in->group;
163                 break;
164         case NFT_META_OIFGROUP:
165                 if (out == NULL)
166                         goto err;
167                 *dest = out->group;
168                 break;
169 #ifdef CONFIG_CGROUP_NET_CLASSID
170         case NFT_META_CGROUP:
171                 if (skb->sk == NULL || !sk_fullsock(skb->sk))
172                         goto err;
173                 *dest = skb->sk->sk_classid;
174                 break;
175 #endif
176         default:
177                 WARN_ON(1);
178                 goto err;
179         }
180         return;
181 
182 err:
183         regs->verdict.code = NFT_BREAK;
184 }
185 EXPORT_SYMBOL_GPL(nft_meta_get_eval);
186 
187 void nft_meta_set_eval(const struct nft_expr *expr,
188                        struct nft_regs *regs,
189                        const struct nft_pktinfo *pkt)
190 {
191         const struct nft_meta *meta = nft_expr_priv(expr);
192         struct sk_buff *skb = pkt->skb;
193         u32 value = regs->data[meta->sreg];
194 
195         switch (meta->key) {
196         case NFT_META_MARK:
197                 skb->mark = value;
198                 break;
199         case NFT_META_PRIORITY:
200                 skb->priority = value;
201                 break;
202         case NFT_META_NFTRACE:
203                 skb->nf_trace = 1;
204                 break;
205         default:
206                 WARN_ON(1);
207         }
208 }
209 EXPORT_SYMBOL_GPL(nft_meta_set_eval);
210 
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 },
215 };
216 EXPORT_SYMBOL_GPL(nft_meta_policy);
217 
218 int nft_meta_get_init(const struct nft_ctx *ctx,
219                       const struct nft_expr *expr,
220                       const struct nlattr * const tb[])
221 {
222         struct nft_meta *priv = nft_expr_priv(expr);
223         unsigned int len;
224 
225         priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
226         switch (priv->key) {
227         case NFT_META_PROTOCOL:
228         case NFT_META_IIFTYPE:
229         case NFT_META_OIFTYPE:
230                 len = sizeof(u16);
231                 break;
232         case NFT_META_NFPROTO:
233         case NFT_META_L4PROTO:
234         case NFT_META_LEN:
235         case NFT_META_PRIORITY:
236         case NFT_META_MARK:
237         case NFT_META_IIF:
238         case NFT_META_OIF:
239         case NFT_META_SKUID:
240         case NFT_META_SKGID:
241 #ifdef CONFIG_IP_ROUTE_CLASSID
242         case NFT_META_RTCLASSID:
243 #endif
244 #ifdef CONFIG_NETWORK_SECMARK
245         case NFT_META_SECMARK:
246 #endif
247         case NFT_META_PKTTYPE:
248         case NFT_META_CPU:
249         case NFT_META_IIFGROUP:
250         case NFT_META_OIFGROUP:
251 #ifdef CONFIG_CGROUP_NET_CLASSID
252         case NFT_META_CGROUP:
253 #endif
254                 len = sizeof(u32);
255                 break;
256         case NFT_META_IIFNAME:
257         case NFT_META_OIFNAME:
258                 len = IFNAMSIZ;
259                 break;
260         default:
261                 return -EOPNOTSUPP;
262         }
263 
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);
267 }
268 EXPORT_SYMBOL_GPL(nft_meta_get_init);
269 
270 int nft_meta_set_init(const struct nft_ctx *ctx,
271                       const struct nft_expr *expr,
272                       const struct nlattr * const tb[])
273 {
274         struct nft_meta *priv = nft_expr_priv(expr);
275         unsigned int len;
276         int err;
277 
278         priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
279         switch (priv->key) {
280         case NFT_META_MARK:
281         case NFT_META_PRIORITY:
282                 len = sizeof(u32);
283                 break;
284         case NFT_META_NFTRACE:
285                 len = sizeof(u8);
286                 break;
287         default:
288                 return -EOPNOTSUPP;
289         }
290 
291         priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
292         err = nft_validate_register_load(priv->sreg, len);
293         if (err < 0)
294                 return err;
295 
296         return 0;
297 }
298 EXPORT_SYMBOL_GPL(nft_meta_set_init);
299 
300 int nft_meta_get_dump(struct sk_buff *skb,
301                       const struct nft_expr *expr)
302 {
303         const struct nft_meta *priv = nft_expr_priv(expr);
304 
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;
309         return 0;
310 
311 nla_put_failure:
312         return -1;
313 }
314 EXPORT_SYMBOL_GPL(nft_meta_get_dump);
315 
316 int nft_meta_set_dump(struct sk_buff *skb,
317                       const struct nft_expr *expr)
318 {
319         const struct nft_meta *priv = nft_expr_priv(expr);
320 
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;
325 
326         return 0;
327 
328 nla_put_failure:
329         return -1;
330 }
331 EXPORT_SYMBOL_GPL(nft_meta_set_dump);
332 
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,
340 };
341 
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,
348 };
349 
350 static const struct nft_expr_ops *
351 nft_meta_select_ops(const struct nft_ctx *ctx,
352                     const struct nlattr * const tb[])
353 {
354         if (tb[NFTA_META_KEY] == NULL)
355                 return ERR_PTR(-EINVAL);
356 
357         if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
358                 return ERR_PTR(-EINVAL);
359 
360         if (tb[NFTA_META_DREG])
361                 return &nft_meta_get_ops;
362 
363         if (tb[NFTA_META_SREG])
364                 return &nft_meta_set_ops;
365 
366         return ERR_PTR(-EINVAL);
367 }
368 
369 static struct nft_expr_type nft_meta_type __read_mostly = {
370         .name           = "meta",
371         .select_ops     = &nft_meta_select_ops,
372         .policy         = nft_meta_policy,
373         .maxattr        = NFTA_META_MAX,
374         .owner          = THIS_MODULE,
375 };
376 
377 static int __init nft_meta_module_init(void)
378 {
379         return nft_register_expr(&nft_meta_type);
380 }
381 
382 static void __exit nft_meta_module_exit(void)
383 {
384         nft_unregister_expr(&nft_meta_type);
385 }
386 
387 module_init(nft_meta_module_init);
388 module_exit(nft_meta_module_exit);
389 
390 MODULE_LICENSE("GPL");
391 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
392 MODULE_ALIAS_NFT_EXPR("meta");
393 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp