act_nat.c 7.2 KB

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