act_nat.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_obj(*nparm);
  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. nparm->action = parm->action;
  86. p = to_tcf_nat(*a);
  87. spin_lock_bh(&p->tcf_lock);
  88. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  89. oparm = rcu_replace_pointer(p->parms, nparm, lockdep_is_held(&p->tcf_lock));
  90. spin_unlock_bh(&p->tcf_lock);
  91. if (goto_ch)
  92. tcf_chain_put_by_act(goto_ch);
  93. if (oparm)
  94. kfree_rcu(oparm, rcu);
  95. return ret;
  96. release_idr:
  97. tcf_idr_release(*a, bind);
  98. return err;
  99. }
  100. TC_INDIRECT_SCOPE int tcf_nat_act(struct sk_buff *skb,
  101. const struct tc_action *a,
  102. struct tcf_result *res)
  103. {
  104. struct tcf_nat *p = to_tcf_nat(a);
  105. struct tcf_nat_parms *parms;
  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;
  114. int noff;
  115. tcf_lastuse_update(&p->tcf_tm);
  116. tcf_action_update_bstats(&p->common, skb);
  117. parms = rcu_dereference_bh(p->parms);
  118. action = parms->action;
  119. if (unlikely(action == TC_ACT_SHOT))
  120. goto drop;
  121. old_addr = parms->old_addr;
  122. new_addr = parms->new_addr;
  123. mask = parms->mask;
  124. egress = parms->flags & TCA_NAT_FLAG_EGRESS;
  125. noff = skb_network_offset(skb);
  126. if (!pskb_may_pull(skb, sizeof(*iph) + noff))
  127. goto drop;
  128. iph = ip_hdr(skb);
  129. if (egress)
  130. addr = iph->saddr;
  131. else
  132. addr = iph->daddr;
  133. if (!((old_addr ^ addr) & mask)) {
  134. if (skb_try_make_writable(skb, sizeof(*iph) + noff))
  135. goto drop;
  136. new_addr &= mask;
  137. new_addr |= addr & ~mask;
  138. /* Rewrite IP header */
  139. iph = ip_hdr(skb);
  140. if (egress)
  141. iph->saddr = new_addr;
  142. else
  143. iph->daddr = new_addr;
  144. csum_replace4(&iph->check, addr, new_addr);
  145. } else if ((iph->frag_off & htons(IP_OFFSET)) ||
  146. iph->protocol != IPPROTO_ICMP) {
  147. goto out;
  148. }
  149. ihl = iph->ihl * 4;
  150. /* It would be nice to share code with stateful NAT. */
  151. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  152. case IPPROTO_TCP:
  153. {
  154. struct tcphdr *tcph;
  155. if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
  156. skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
  157. goto drop;
  158. tcph = (void *)(skb_network_header(skb) + ihl);
  159. inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
  160. true);
  161. break;
  162. }
  163. case IPPROTO_UDP:
  164. {
  165. struct udphdr *udph;
  166. if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
  167. skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
  168. goto drop;
  169. udph = (void *)(skb_network_header(skb) + ihl);
  170. if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  171. inet_proto_csum_replace4(&udph->check, skb, addr,
  172. new_addr, true);
  173. if (!udph->check)
  174. udph->check = CSUM_MANGLED_0;
  175. }
  176. break;
  177. }
  178. case IPPROTO_ICMP:
  179. {
  180. struct icmphdr *icmph;
  181. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
  182. goto drop;
  183. icmph = (void *)(skb_network_header(skb) + ihl);
  184. if (!icmp_is_err(icmph->type))
  185. break;
  186. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
  187. noff))
  188. goto drop;
  189. icmph = (void *)(skb_network_header(skb) + ihl);
  190. iph = (void *)(icmph + 1);
  191. if (egress)
  192. addr = iph->daddr;
  193. else
  194. addr = iph->saddr;
  195. if ((old_addr ^ addr) & mask)
  196. break;
  197. if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
  198. sizeof(*iph) + noff))
  199. goto drop;
  200. icmph = (void *)(skb_network_header(skb) + ihl);
  201. iph = (void *)(icmph + 1);
  202. new_addr &= mask;
  203. new_addr |= addr & ~mask;
  204. /* XXX Fix up the inner checksums. */
  205. if (egress)
  206. iph->daddr = new_addr;
  207. else
  208. iph->saddr = new_addr;
  209. inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
  210. false);
  211. break;
  212. }
  213. default:
  214. break;
  215. }
  216. out:
  217. return action;
  218. drop:
  219. tcf_action_inc_drop_qstats(&p->common);
  220. return TC_ACT_SHOT;
  221. }
  222. static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
  223. int bind, int ref)
  224. {
  225. unsigned char *b = skb_tail_pointer(skb);
  226. const struct tcf_nat *p = to_tcf_nat(a);
  227. const struct tcf_nat_parms *parms;
  228. struct tc_nat opt = {
  229. .index = p->tcf_index,
  230. .refcnt = refcount_read(&p->tcf_refcnt) - ref,
  231. .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
  232. };
  233. struct tcf_t t;
  234. rcu_read_lock();
  235. parms = rcu_dereference(p->parms);
  236. opt.action = parms->action;
  237. opt.old_addr = parms->old_addr;
  238. opt.new_addr = parms->new_addr;
  239. opt.mask = parms->mask;
  240. opt.flags = parms->flags;
  241. if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
  242. goto nla_put_failure;
  243. tcf_tm_dump(&t, &p->tcf_tm);
  244. if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
  245. goto nla_put_failure;
  246. rcu_read_unlock();
  247. return skb->len;
  248. nla_put_failure:
  249. rcu_read_unlock();
  250. nlmsg_trim(skb, b);
  251. return -1;
  252. }
  253. static void tcf_nat_cleanup(struct tc_action *a)
  254. {
  255. struct tcf_nat *p = to_tcf_nat(a);
  256. struct tcf_nat_parms *parms;
  257. parms = rcu_dereference_protected(p->parms, 1);
  258. if (parms)
  259. kfree_rcu(parms, rcu);
  260. }
  261. static struct tc_action_ops act_nat_ops = {
  262. .kind = "nat",
  263. .id = TCA_ID_NAT,
  264. .owner = THIS_MODULE,
  265. .act = tcf_nat_act,
  266. .dump = tcf_nat_dump,
  267. .init = tcf_nat_init,
  268. .cleanup = tcf_nat_cleanup,
  269. .size = sizeof(struct tcf_nat),
  270. };
  271. MODULE_ALIAS_NET_ACT("nat");
  272. static __net_init int nat_init_net(struct net *net)
  273. {
  274. struct tc_action_net *tn = net_generic(net, act_nat_ops.net_id);
  275. return tc_action_net_init(net, tn, &act_nat_ops);
  276. }
  277. static void __net_exit nat_exit_net(struct list_head *net_list)
  278. {
  279. tc_action_net_exit(net_list, act_nat_ops.net_id);
  280. }
  281. static struct pernet_operations nat_net_ops = {
  282. .init = nat_init_net,
  283. .exit_batch = nat_exit_net,
  284. .id = &act_nat_ops.net_id,
  285. .size = sizeof(struct tc_action_net),
  286. };
  287. MODULE_DESCRIPTION("Stateless NAT actions");
  288. MODULE_LICENSE("GPL");
  289. static int __init nat_init_module(void)
  290. {
  291. return tcf_register_action(&act_nat_ops, &nat_net_ops);
  292. }
  293. static void __exit nat_cleanup_module(void)
  294. {
  295. tcf_unregister_action(&act_nat_ops, &nat_net_ops);
  296. }
  297. module_init(nat_init_module);
  298. module_exit(nat_cleanup_module);