Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Stateless NAT actions |
| 4 | * |
| 5 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/errno.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/netfilter.h> |
| 13 | #include <linux/rtnetlink.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/tc_act/tc_nat.h> |
| 19 | #include <net/act_api.h> |
Davide Caratti | 1e45d04 | 2019-03-20 15:00:06 +0100 | [diff] [blame] | 20 | #include <net/pkt_cls.h> |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 21 | #include <net/icmp.h> |
| 22 | #include <net/ip.h> |
| 23 | #include <net/netlink.h> |
| 24 | #include <net/tc_act/tc_nat.h> |
| 25 | #include <net/tcp.h> |
| 26 | #include <net/udp.h> |
| 27 | |
| 28 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 29 | static unsigned int nat_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 30 | static struct tc_action_ops act_nat_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 31 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 32 | static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = { |
| 33 | [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) }, |
| 34 | }; |
| 35 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 36 | static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est, |
Alexander Aring | 589dad6 | 2018-02-15 10:54:56 -0500 | [diff] [blame] | 37 | struct tc_action **a, int ovr, int bind, |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 38 | bool rtnl_held, struct tcf_proto *tp, |
Vlad Buslov | abbb0d3 | 2019-10-30 16:09:05 +0200 | [diff] [blame] | 39 | u32 flags, struct netlink_ext_ack *extack) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 40 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 41 | struct tc_action_net *tn = net_generic(net, nat_net_id); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 42 | struct nlattr *tb[TCA_NAT_MAX + 1]; |
Davide Caratti | 1e45d04 | 2019-03-20 15:00:06 +0100 | [diff] [blame] | 43 | struct tcf_chain *goto_ch = NULL; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 44 | struct tc_nat *parm; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 45 | int ret = 0, err; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 46 | struct tcf_nat *p; |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 47 | u32 index; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 48 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 49 | if (nla == NULL) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 50 | return -EINVAL; |
| 51 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 52 | err = nla_parse_nested_deprecated(tb, TCA_NAT_MAX, nla, nat_policy, |
| 53 | NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 54 | if (err < 0) |
| 55 | return err; |
| 56 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 57 | if (tb[TCA_NAT_PARMS] == NULL) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 58 | return -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 59 | parm = nla_data(tb[TCA_NAT_PARMS]); |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 60 | index = parm->index; |
| 61 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 62 | if (!err) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 63 | ret = tcf_idr_create(tn, index, est, a, |
Vlad Buslov | e382267 | 2019-10-30 16:09:06 +0200 | [diff] [blame] | 64 | &act_nat_ops, bind, false, 0); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 65 | if (ret) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 66 | tcf_idr_cleanup(tn, index); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 67 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 68 | } |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 69 | ret = ACT_P_CREATED; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 70 | } else if (err > 0) { |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 71 | if (bind) |
| 72 | return 0; |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 73 | if (!ovr) { |
| 74 | tcf_idr_release(*a, bind); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 75 | return -EEXIST; |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 76 | } |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 77 | } else { |
| 78 | return err; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 79 | } |
Davide Caratti | 1e45d04 | 2019-03-20 15:00:06 +0100 | [diff] [blame] | 80 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
| 81 | if (err < 0) |
| 82 | goto release_idr; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 83 | p = to_tcf_nat(*a); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 84 | |
| 85 | spin_lock_bh(&p->tcf_lock); |
| 86 | p->old_addr = parm->old_addr; |
| 87 | p->new_addr = parm->new_addr; |
| 88 | p->mask = parm->mask; |
| 89 | p->flags = parm->flags; |
| 90 | |
Davide Caratti | 1e45d04 | 2019-03-20 15:00:06 +0100 | [diff] [blame] | 91 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 92 | spin_unlock_bh(&p->tcf_lock); |
Davide Caratti | 1e45d04 | 2019-03-20 15:00:06 +0100 | [diff] [blame] | 93 | if (goto_ch) |
| 94 | tcf_chain_put_by_act(goto_ch); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 95 | |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 96 | return ret; |
Davide Caratti | 1e45d04 | 2019-03-20 15:00:06 +0100 | [diff] [blame] | 97 | release_idr: |
| 98 | tcf_idr_release(*a, bind); |
| 99 | return err; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Jamal Hadi Salim | 0390514 | 2018-08-12 09:34:54 -0400 | [diff] [blame] | 102 | static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a, |
| 103 | struct tcf_result *res) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 104 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 105 | struct tcf_nat *p = to_tcf_nat(a); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 106 | struct iphdr *iph; |
| 107 | __be32 old_addr; |
| 108 | __be32 new_addr; |
| 109 | __be32 mask; |
| 110 | __be32 addr; |
| 111 | int egress; |
| 112 | int action; |
| 113 | int ihl; |
Changli Gao | 36d1269 | 2010-08-03 17:39:18 +0000 | [diff] [blame] | 114 | int noff; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 115 | |
| 116 | spin_lock(&p->tcf_lock); |
| 117 | |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 118 | tcf_lastuse_update(&p->tcf_tm); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 119 | old_addr = p->old_addr; |
| 120 | new_addr = p->new_addr; |
| 121 | mask = p->mask; |
| 122 | egress = p->flags & TCA_NAT_FLAG_EGRESS; |
| 123 | action = p->tcf_action; |
| 124 | |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 125 | bstats_update(&p->tcf_bstats, skb); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 126 | |
| 127 | spin_unlock(&p->tcf_lock); |
| 128 | |
| 129 | if (unlikely(action == TC_ACT_SHOT)) |
| 130 | goto drop; |
| 131 | |
Changli Gao | 36d1269 | 2010-08-03 17:39:18 +0000 | [diff] [blame] | 132 | noff = skb_network_offset(skb); |
| 133 | if (!pskb_may_pull(skb, sizeof(*iph) + noff)) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 134 | goto drop; |
| 135 | |
| 136 | iph = ip_hdr(skb); |
| 137 | |
| 138 | if (egress) |
| 139 | addr = iph->saddr; |
| 140 | else |
| 141 | addr = iph->daddr; |
| 142 | |
| 143 | if (!((old_addr ^ addr) & mask)) { |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 144 | if (skb_try_make_writable(skb, sizeof(*iph) + noff)) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 145 | goto drop; |
| 146 | |
| 147 | new_addr &= mask; |
| 148 | new_addr |= addr & ~mask; |
| 149 | |
| 150 | /* Rewrite IP header */ |
| 151 | iph = ip_hdr(skb); |
| 152 | if (egress) |
| 153 | iph->saddr = new_addr; |
| 154 | else |
| 155 | iph->daddr = new_addr; |
| 156 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 157 | csum_replace4(&iph->check, addr, new_addr); |
Changli Gao | 33c29dd | 2010-05-29 14:26:59 +0000 | [diff] [blame] | 158 | } else if ((iph->frag_off & htons(IP_OFFSET)) || |
| 159 | iph->protocol != IPPROTO_ICMP) { |
| 160 | goto out; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | ihl = iph->ihl * 4; |
| 164 | |
| 165 | /* It would be nice to share code with stateful NAT. */ |
| 166 | switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) { |
| 167 | case IPPROTO_TCP: |
| 168 | { |
| 169 | struct tcphdr *tcph; |
| 170 | |
Changli Gao | 36d1269 | 2010-08-03 17:39:18 +0000 | [diff] [blame] | 171 | if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) || |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 172 | skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff)) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 173 | goto drop; |
| 174 | |
| 175 | tcph = (void *)(skb_network_header(skb) + ihl); |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 176 | inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, |
| 177 | true); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | case IPPROTO_UDP: |
| 181 | { |
| 182 | struct udphdr *udph; |
| 183 | |
Changli Gao | 36d1269 | 2010-08-03 17:39:18 +0000 | [diff] [blame] | 184 | if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) || |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 185 | skb_try_make_writable(skb, ihl + sizeof(*udph) + noff)) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 186 | goto drop; |
| 187 | |
| 188 | udph = (void *)(skb_network_header(skb) + ihl); |
| 189 | if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 190 | inet_proto_csum_replace4(&udph->check, skb, addr, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 191 | new_addr, true); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 192 | if (!udph->check) |
| 193 | udph->check = CSUM_MANGLED_0; |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | case IPPROTO_ICMP: |
| 198 | { |
| 199 | struct icmphdr *icmph; |
| 200 | |
Changli Gao | 36d1269 | 2010-08-03 17:39:18 +0000 | [diff] [blame] | 201 | if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff)) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 202 | goto drop; |
| 203 | |
| 204 | icmph = (void *)(skb_network_header(skb) + ihl); |
| 205 | |
Matteo Croce | 54074f1 | 2019-11-02 01:12:04 +0100 | [diff] [blame] | 206 | if (!icmp_is_err(icmph->type)) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 207 | break; |
| 208 | |
Changli Gao | 36d1269 | 2010-08-03 17:39:18 +0000 | [diff] [blame] | 209 | if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) + |
| 210 | noff)) |
Changli Gao | 70c2efa | 2010-07-09 15:33:25 +0000 | [diff] [blame] | 211 | goto drop; |
| 212 | |
Changli Gao | 072d79a | 2010-07-29 13:41:46 +0000 | [diff] [blame] | 213 | icmph = (void *)(skb_network_header(skb) + ihl); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 214 | iph = (void *)(icmph + 1); |
| 215 | if (egress) |
| 216 | addr = iph->daddr; |
| 217 | else |
| 218 | addr = iph->saddr; |
| 219 | |
| 220 | if ((old_addr ^ addr) & mask) |
| 221 | break; |
| 222 | |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 223 | if (skb_try_make_writable(skb, ihl + sizeof(*icmph) + |
| 224 | sizeof(*iph) + noff)) |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 225 | goto drop; |
| 226 | |
| 227 | icmph = (void *)(skb_network_header(skb) + ihl); |
| 228 | iph = (void *)(icmph + 1); |
| 229 | |
| 230 | new_addr &= mask; |
| 231 | new_addr |= addr & ~mask; |
| 232 | |
| 233 | /* XXX Fix up the inner checksums. */ |
| 234 | if (egress) |
| 235 | iph->daddr = new_addr; |
| 236 | else |
| 237 | iph->saddr = new_addr; |
| 238 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 239 | inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 240 | false); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | |
Changli Gao | 33c29dd | 2010-05-29 14:26:59 +0000 | [diff] [blame] | 247 | out: |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 248 | return action; |
| 249 | |
| 250 | drop: |
| 251 | spin_lock(&p->tcf_lock); |
| 252 | p->tcf_qstats.drops++; |
| 253 | spin_unlock(&p->tcf_lock); |
| 254 | return TC_ACT_SHOT; |
| 255 | } |
| 256 | |
| 257 | static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a, |
| 258 | int bind, int ref) |
| 259 | { |
| 260 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 261 | struct tcf_nat *p = to_tcf_nat(a); |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 262 | struct tc_nat opt = { |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 263 | .index = p->tcf_index, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 264 | .refcnt = refcount_read(&p->tcf_refcnt) - ref, |
| 265 | .bindcnt = atomic_read(&p->tcf_bindcnt) - bind, |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 266 | }; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 267 | struct tcf_t t; |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 268 | |
Vlad Buslov | f20a4d0 | 2018-09-03 10:09:20 +0300 | [diff] [blame] | 269 | spin_lock_bh(&p->tcf_lock); |
| 270 | opt.old_addr = p->old_addr; |
| 271 | opt.new_addr = p->new_addr; |
| 272 | opt.mask = p->mask; |
| 273 | opt.flags = p->flags; |
| 274 | opt.action = p->tcf_action; |
| 275 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 276 | if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt)) |
| 277 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 278 | |
| 279 | tcf_tm_dump(&t, &p->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 280 | if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 281 | goto nla_put_failure; |
Vlad Buslov | f20a4d0 | 2018-09-03 10:09:20 +0300 | [diff] [blame] | 282 | spin_unlock_bh(&p->tcf_lock); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 283 | |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 284 | return skb->len; |
| 285 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 286 | nla_put_failure: |
Vlad Buslov | f20a4d0 | 2018-09-03 10:09:20 +0300 | [diff] [blame] | 287 | spin_unlock_bh(&p->tcf_lock); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 288 | nlmsg_trim(skb, b); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 289 | return -1; |
| 290 | } |
| 291 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 292 | static int tcf_nat_walker(struct net *net, struct sk_buff *skb, |
| 293 | struct netlink_callback *cb, int type, |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 294 | const struct tc_action_ops *ops, |
| 295 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 296 | { |
| 297 | struct tc_action_net *tn = net_generic(net, nat_net_id); |
| 298 | |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 299 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 302 | static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 303 | { |
| 304 | struct tc_action_net *tn = net_generic(net, nat_net_id); |
| 305 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 306 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 309 | static struct tc_action_ops act_nat_ops = { |
| 310 | .kind = "nat", |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 311 | .id = TCA_ID_NAT, |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 312 | .owner = THIS_MODULE, |
Jamal Hadi Salim | 0390514 | 2018-08-12 09:34:54 -0400 | [diff] [blame] | 313 | .act = tcf_nat_act, |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 314 | .dump = tcf_nat_dump, |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 315 | .init = tcf_nat_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 316 | .walk = tcf_nat_walker, |
| 317 | .lookup = tcf_nat_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 318 | .size = sizeof(struct tcf_nat), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | static __net_init int nat_init_net(struct net *net) |
| 322 | { |
| 323 | struct tc_action_net *tn = net_generic(net, nat_net_id); |
| 324 | |
Cong Wang | 981471b | 2019-08-25 10:01:32 -0700 | [diff] [blame] | 325 | return tc_action_net_init(net, tn, &act_nat_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 328 | static void __net_exit nat_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 329 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 330 | tc_action_net_exit(net_list, nat_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static struct pernet_operations nat_net_ops = { |
| 334 | .init = nat_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 335 | .exit_batch = nat_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 336 | .id = &nat_net_id, |
| 337 | .size = sizeof(struct tc_action_net), |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | MODULE_DESCRIPTION("Stateless NAT actions"); |
| 341 | MODULE_LICENSE("GPL"); |
| 342 | |
| 343 | static int __init nat_init_module(void) |
| 344 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 345 | return tcf_register_action(&act_nat_ops, &nat_net_ops); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static void __exit nat_cleanup_module(void) |
| 349 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 350 | tcf_unregister_action(&act_nat_ops, &nat_net_ops); |
Herbert Xu | b421995 | 2007-09-27 12:48:05 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | module_init(nat_init_module); |
| 354 | module_exit(nat_cleanup_module); |