netfilter: nf_tables: validate len in nft_validate_data_load()
[deliverable/linux.git] / net / bridge / netfilter / nft_meta_bridge.c
1 /*
2 * Copyright (c) 2014 Intel Corporation
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 */
9
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/netfilter/nft_meta.h>
18
19 #include "../br_private.h"
20
21 static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
22 struct nft_data data[NFT_REG_MAX + 1],
23 const struct nft_pktinfo *pkt)
24 {
25 const struct nft_meta *priv = nft_expr_priv(expr);
26 const struct net_device *in = pkt->in, *out = pkt->out;
27 struct nft_data *dest = &data[priv->dreg];
28 const struct net_bridge_port *p;
29
30 switch (priv->key) {
31 case NFT_META_BRI_IIFNAME:
32 if (in == NULL || (p = br_port_get_rcu(in)) == NULL)
33 goto err;
34 break;
35 case NFT_META_BRI_OIFNAME:
36 if (out == NULL || (p = br_port_get_rcu(out)) == NULL)
37 goto err;
38 break;
39 default:
40 goto out;
41 }
42
43 strncpy((char *)dest->data, p->br->dev->name, sizeof(dest->data));
44 return;
45 out:
46 return nft_meta_get_eval(expr, data, pkt);
47 err:
48 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
49 }
50
51 static int nft_meta_bridge_get_init(const struct nft_ctx *ctx,
52 const struct nft_expr *expr,
53 const struct nlattr * const tb[])
54 {
55 struct nft_meta *priv = nft_expr_priv(expr);
56 unsigned int len;
57 int err;
58
59 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
60 switch (priv->key) {
61 case NFT_META_BRI_IIFNAME:
62 case NFT_META_BRI_OIFNAME:
63 len = IFNAMSIZ;
64 break;
65 default:
66 return nft_meta_get_init(ctx, expr, tb);
67 }
68
69 priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG]));
70 err = nft_validate_output_register(priv->dreg);
71 if (err < 0)
72 return err;
73
74 err = nft_validate_data_load(ctx, priv->dreg, NULL,
75 NFT_DATA_VALUE, len);
76 if (err < 0)
77 return err;
78
79 return 0;
80 }
81
82 static struct nft_expr_type nft_meta_bridge_type;
83 static const struct nft_expr_ops nft_meta_bridge_get_ops = {
84 .type = &nft_meta_bridge_type,
85 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
86 .eval = nft_meta_bridge_get_eval,
87 .init = nft_meta_bridge_get_init,
88 .dump = nft_meta_get_dump,
89 };
90
91 static const struct nft_expr_ops nft_meta_bridge_set_ops = {
92 .type = &nft_meta_bridge_type,
93 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
94 .eval = nft_meta_set_eval,
95 .init = nft_meta_set_init,
96 .dump = nft_meta_set_dump,
97 };
98
99 static const struct nft_expr_ops *
100 nft_meta_bridge_select_ops(const struct nft_ctx *ctx,
101 const struct nlattr * const tb[])
102 {
103 if (tb[NFTA_META_KEY] == NULL)
104 return ERR_PTR(-EINVAL);
105
106 if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
107 return ERR_PTR(-EINVAL);
108
109 if (tb[NFTA_META_DREG])
110 return &nft_meta_bridge_get_ops;
111
112 if (tb[NFTA_META_SREG])
113 return &nft_meta_bridge_set_ops;
114
115 return ERR_PTR(-EINVAL);
116 }
117
118 static struct nft_expr_type nft_meta_bridge_type __read_mostly = {
119 .family = NFPROTO_BRIDGE,
120 .name = "meta",
121 .select_ops = &nft_meta_bridge_select_ops,
122 .policy = nft_meta_policy,
123 .maxattr = NFTA_META_MAX,
124 .owner = THIS_MODULE,
125 };
126
127 static int __init nft_meta_bridge_module_init(void)
128 {
129 return nft_register_expr(&nft_meta_bridge_type);
130 }
131
132 static void __exit nft_meta_bridge_module_exit(void)
133 {
134 nft_unregister_expr(&nft_meta_bridge_type);
135 }
136
137 module_init(nft_meta_bridge_module_init);
138 module_exit(nft_meta_bridge_module_exit);
139
140 MODULE_LICENSE("GPL");
141 MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
142 MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "meta");
This page took 0.036547 seconds and 5 git commands to generate.