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