act_nat.c 7.9 KB

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