]> Pileus Git - ~andy/linux/blob - net/netfilter/nft_nat.c
core, nfqueue, openvswitch: Orphan frags in skb_zerocopy and handle errors
[~andy/linux] / net / netfilter / nft_nat.c
1 /*
2  * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3  * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4  * Copyright (c) 2012 Intel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/skbuff.h>
15 #include <linux/ip.h>
16 #include <linux/string.h>
17 #include <linux/netlink.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/netfilter/nfnetlink.h>
21 #include <linux/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_nat.h>
24 #include <net/netfilter/nf_nat_core.h>
25 #include <net/netfilter/nf_tables.h>
26 #include <net/netfilter/nf_nat_l3proto.h>
27 #include <net/ip.h>
28
29 struct nft_nat {
30         enum nft_registers      sreg_addr_min:8;
31         enum nft_registers      sreg_addr_max:8;
32         enum nft_registers      sreg_proto_min:8;
33         enum nft_registers      sreg_proto_max:8;
34         int                     family;
35         enum nf_nat_manip_type  type;
36 };
37
38 static void nft_nat_eval(const struct nft_expr *expr,
39                          struct nft_data data[NFT_REG_MAX + 1],
40                          const struct nft_pktinfo *pkt)
41 {
42         const struct nft_nat *priv = nft_expr_priv(expr);
43         enum ip_conntrack_info ctinfo;
44         struct nf_conn *ct = nf_ct_get(pkt->skb, &ctinfo);
45         struct nf_nat_range range;
46
47         memset(&range, 0, sizeof(range));
48         if (priv->sreg_addr_min) {
49                 if (priv->family == AF_INET) {
50                         range.min_addr.ip = (__force __be32)
51                                         data[priv->sreg_addr_min].data[0];
52                         range.max_addr.ip = (__force __be32)
53                                         data[priv->sreg_addr_max].data[0];
54
55                 } else {
56                         memcpy(range.min_addr.ip6,
57                                data[priv->sreg_addr_min].data,
58                                sizeof(struct nft_data));
59                         memcpy(range.max_addr.ip6,
60                                data[priv->sreg_addr_max].data,
61                                sizeof(struct nft_data));
62                 }
63                 range.flags |= NF_NAT_RANGE_MAP_IPS;
64         }
65
66         if (priv->sreg_proto_min) {
67                 range.min_proto.all = (__force __be16)
68                                         data[priv->sreg_proto_min].data[0];
69                 range.max_proto.all = (__force __be16)
70                                         data[priv->sreg_proto_max].data[0];
71                 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
72         }
73
74         data[NFT_REG_VERDICT].verdict =
75                 nf_nat_setup_info(ct, &range, priv->type);
76 }
77
78 static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = {
79         [NFTA_NAT_TYPE]          = { .type = NLA_U32 },
80         [NFTA_NAT_FAMILY]        = { .type = NLA_U32 },
81         [NFTA_NAT_REG_ADDR_MIN]  = { .type = NLA_U32 },
82         [NFTA_NAT_REG_ADDR_MAX]  = { .type = NLA_U32 },
83         [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 },
84         [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 },
85 };
86
87 static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
88                         const struct nlattr * const tb[])
89 {
90         struct nft_nat *priv = nft_expr_priv(expr);
91         int err;
92
93         if (tb[NFTA_NAT_TYPE] == NULL)
94                 return -EINVAL;
95
96         switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) {
97         case NFT_NAT_SNAT:
98                 priv->type = NF_NAT_MANIP_SRC;
99                 break;
100         case NFT_NAT_DNAT:
101                 priv->type = NF_NAT_MANIP_DST;
102                 break;
103         default:
104                 return -EINVAL;
105         }
106
107         if (tb[NFTA_NAT_FAMILY] == NULL)
108                 return -EINVAL;
109
110         priv->family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY]));
111         if (priv->family != AF_INET && priv->family != AF_INET6)
112                 return -EINVAL;
113
114         if (tb[NFTA_NAT_REG_ADDR_MIN]) {
115                 priv->sreg_addr_min = ntohl(nla_get_be32(
116                                                 tb[NFTA_NAT_REG_ADDR_MIN]));
117                 err = nft_validate_input_register(priv->sreg_addr_min);
118                 if (err < 0)
119                         return err;
120         }
121
122         if (tb[NFTA_NAT_REG_ADDR_MAX]) {
123                 priv->sreg_addr_max = ntohl(nla_get_be32(
124                                                 tb[NFTA_NAT_REG_ADDR_MAX]));
125                 err = nft_validate_input_register(priv->sreg_addr_max);
126                 if (err < 0)
127                         return err;
128         } else
129                 priv->sreg_addr_max = priv->sreg_addr_min;
130
131         if (tb[NFTA_NAT_REG_PROTO_MIN]) {
132                 priv->sreg_proto_min = ntohl(nla_get_be32(
133                                                 tb[NFTA_NAT_REG_PROTO_MIN]));
134                 err = nft_validate_input_register(priv->sreg_proto_min);
135                 if (err < 0)
136                         return err;
137         }
138
139         if (tb[NFTA_NAT_REG_PROTO_MAX]) {
140                 priv->sreg_proto_max = ntohl(nla_get_be32(
141                                                 tb[NFTA_NAT_REG_PROTO_MAX]));
142                 err = nft_validate_input_register(priv->sreg_proto_max);
143                 if (err < 0)
144                         return err;
145         } else
146                 priv->sreg_proto_max = priv->sreg_proto_min;
147
148         return 0;
149 }
150
151 static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr)
152 {
153         const struct nft_nat *priv = nft_expr_priv(expr);
154
155         switch (priv->type) {
156         case NF_NAT_MANIP_SRC:
157                 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT)))
158                         goto nla_put_failure;
159                 break;
160         case NF_NAT_MANIP_DST:
161                 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT)))
162                         goto nla_put_failure;
163                 break;
164         }
165
166         if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family)))
167                 goto nla_put_failure;
168         if (nla_put_be32(skb,
169                          NFTA_NAT_REG_ADDR_MIN, htonl(priv->sreg_addr_min)))
170                 goto nla_put_failure;
171         if (nla_put_be32(skb,
172                          NFTA_NAT_REG_ADDR_MAX, htonl(priv->sreg_addr_max)))
173                 goto nla_put_failure;
174         if (nla_put_be32(skb,
175                          NFTA_NAT_REG_PROTO_MIN, htonl(priv->sreg_proto_min)))
176                 goto nla_put_failure;
177         if (nla_put_be32(skb,
178                          NFTA_NAT_REG_PROTO_MAX, htonl(priv->sreg_proto_max)))
179                 goto nla_put_failure;
180         return 0;
181
182 nla_put_failure:
183         return -1;
184 }
185
186 static struct nft_expr_type nft_nat_type;
187 static const struct nft_expr_ops nft_nat_ops = {
188         .type           = &nft_nat_type,
189         .size           = NFT_EXPR_SIZE(sizeof(struct nft_nat)),
190         .eval           = nft_nat_eval,
191         .init           = nft_nat_init,
192         .dump           = nft_nat_dump,
193 };
194
195 static struct nft_expr_type nft_nat_type __read_mostly = {
196         .name           = "nat",
197         .ops            = &nft_nat_ops,
198         .policy         = nft_nat_policy,
199         .maxattr        = NFTA_NAT_MAX,
200         .owner          = THIS_MODULE,
201 };
202
203 static int __init nft_nat_module_init(void)
204 {
205         int err;
206
207         err = nft_register_expr(&nft_nat_type);
208         if (err < 0)
209                 return err;
210
211         return 0;
212 }
213
214 static void __exit nft_nat_module_exit(void)
215 {
216         nft_unregister_expr(&nft_nat_type);
217 }
218
219 module_init(nft_nat_module_init);
220 module_exit(nft_nat_module_exit);
221
222 MODULE_LICENSE("GPL");
223 MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
224 MODULE_ALIAS_NFT_EXPR("nat");