act_nat.c 7.7 KB

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