act_nat.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Stateless NAT actions
  3. *
  4. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/string.h>
  21. #include <linux/tc_act/tc_nat.h>
  22. #include <net/act_api.h>
  23. #include <net/icmp.h>
  24. #include <net/ip.h>
  25. #include <net/netlink.h>
  26. #include <net/tc_act/tc_nat.h>
  27. #include <net/tcp.h>
  28. #include <net/udp.h>
  29. static unsigned int nat_net_id;
  30. static struct tc_action_ops act_nat_ops;
  31. static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
  32. [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
  33. };
  34. static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
  35. struct tc_action **a, int ovr, int bind,
  36. bool rtnl_held, struct netlink_ext_ack *extack)
  37. {
  38. struct tc_action_net *tn = net_generic(net, nat_net_id);
  39. struct nlattr *tb[TCA_NAT_MAX + 1];
  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(tb, TCA_NAT_MAX, nla, nat_policy, 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);
  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. p = to_tcf_nat(*a);
  73. spin_lock_bh(&p->tcf_lock);
  74. p->old_addr = parm->old_addr;
  75. p->new_addr = parm->new_addr;
  76. p->mask = parm->mask;
  77. p->flags = parm->flags;
  78. p->tcf_action = parm->action;
  79. spin_unlock_bh(&p->tcf_lock);
  80. if (ret == ACT_P_CREATED)
  81. tcf_idr_insert(tn, *a);
  82. return ret;
  83. }
  84. static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
  85. struct tcf_result *res)
  86. {
  87. struct tcf_nat *p = to_tcf_nat(a);
  88. struct iphdr *iph;
  89. __be32 old_addr;
  90. __be32 new_addr;
  91. __be32 mask;
  92. __be32 addr;
  93. int egress;
  94. int action;
  95. int ihl;
  96. int noff;
  97. spin_lock(&p->tcf_lock);
  98. tcf_lastuse_update(&p->tcf_tm);
  99. old_addr = p->old_addr;
  100. new_addr = p->new_addr;
  101. mask = p->mask;
  102. egress = p->flags & TCA_NAT_FLAG_EGRESS;
  103. action = p->tcf_action;
  104. bstats_update(&p->tcf_bstats, skb);
  105. spin_unlock(&p->tcf_lock);
  106. if (unlikely(action == TC_ACT_SHOT))
  107. goto drop;
  108. noff = skb_network_offset(skb);
  109. if (!pskb_may_pull(skb, sizeof(*iph) + noff))
  110. goto drop;
  111. iph = ip_hdr(skb);
  112. if (egress)
  113. addr = iph->saddr;
  114. else
  115. addr = iph->daddr;
  116. if (!((old_addr ^ addr) & mask)) {
  117. if (skb_try_make_writable(skb, sizeof(*iph) + noff))
  118. goto drop;
  119. new_addr &= mask;
  120. new_addr |= addr & ~mask;
  121. /* Rewrite IP header */
  122. iph = ip_hdr(skb);
  123. if (egress)
  124. iph->saddr = new_addr;
  125. else
  126. iph->daddr = new_addr;
  127. csum_replace4(&iph->check, addr, new_addr);
  128. } else if ((iph->frag_off & htons(IP_OFFSET)) ||
  129. iph->protocol != IPPROTO_ICMP) {
  130. goto out;
  131. }
  132. ihl = iph->ihl * 4;
  133. /* It would be nice to share code with stateful NAT. */
  134. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  135. case IPPROTO_TCP:
  136. {
  137. struct tcphdr *tcph;
  138. if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
  139. skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
  140. goto drop;
  141. tcph = (void *)(skb_network_header(skb) + ihl);
  142. inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
  143. true);
  144. break;
  145. }
  146. case IPPROTO_UDP:
  147. {
  148. struct udphdr *udph;
  149. if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
  150. skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
  151. goto drop;
  152. udph = (void *)(skb_network_header(skb) + ihl);
  153. if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  154. inet_proto_csum_replace4(&udph->check, skb, addr,
  155. new_addr, true);
  156. if (!udph->check)
  157. udph->check = CSUM_MANGLED_0;
  158. }
  159. break;
  160. }
  161. case IPPROTO_ICMP:
  162. {
  163. struct icmphdr *icmph;
  164. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
  165. goto drop;
  166. icmph = (void *)(skb_network_header(skb) + ihl);
  167. if ((icmph->type != ICMP_DEST_UNREACH) &&
  168. (icmph->type != ICMP_TIME_EXCEEDED) &&
  169. (icmph->type != ICMP_PARAMETERPROB))
  170. break;
  171. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
  172. noff))
  173. goto drop;
  174. icmph = (void *)(skb_network_header(skb) + ihl);
  175. iph = (void *)(icmph + 1);
  176. if (egress)
  177. addr = iph->daddr;
  178. else
  179. addr = iph->saddr;
  180. if ((old_addr ^ addr) & mask)
  181. break;
  182. if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
  183. sizeof(*iph) + noff))
  184. goto drop;
  185. icmph = (void *)(skb_network_header(skb) + ihl);
  186. iph = (void *)(icmph + 1);
  187. new_addr &= mask;
  188. new_addr |= addr & ~mask;
  189. /* XXX Fix up the inner checksums. */
  190. if (egress)
  191. iph->daddr = new_addr;
  192. else
  193. iph->saddr = new_addr;
  194. inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
  195. false);
  196. break;
  197. }
  198. default:
  199. break;
  200. }
  201. out:
  202. return action;
  203. drop:
  204. spin_lock(&p->tcf_lock);
  205. p->tcf_qstats.drops++;
  206. spin_unlock(&p->tcf_lock);
  207. return TC_ACT_SHOT;
  208. }
  209. static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
  210. int bind, int ref)
  211. {
  212. unsigned char *b = skb_tail_pointer(skb);
  213. struct tcf_nat *p = to_tcf_nat(a);
  214. struct tc_nat opt = {
  215. .old_addr = p->old_addr,
  216. .new_addr = p->new_addr,
  217. .mask = p->mask,
  218. .flags = p->flags,
  219. .index = p->tcf_index,
  220. .action = p->tcf_action,
  221. .refcnt = refcount_read(&p->tcf_refcnt) - ref,
  222. .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
  223. };
  224. struct tcf_t t;
  225. if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
  226. goto nla_put_failure;
  227. tcf_tm_dump(&t, &p->tcf_tm);
  228. if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
  229. goto nla_put_failure;
  230. return skb->len;
  231. nla_put_failure:
  232. nlmsg_trim(skb, b);
  233. return -1;
  234. }
  235. static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
  236. struct netlink_callback *cb, int type,
  237. const struct tc_action_ops *ops,
  238. struct netlink_ext_ack *extack)
  239. {
  240. struct tc_action_net *tn = net_generic(net, nat_net_id);
  241. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  242. }
  243. static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index,
  244. struct netlink_ext_ack *extack)
  245. {
  246. struct tc_action_net *tn = net_generic(net, nat_net_id);
  247. return tcf_idr_search(tn, a, index);
  248. }
  249. static struct tc_action_ops act_nat_ops = {
  250. .kind = "nat",
  251. .type = TCA_ACT_NAT,
  252. .owner = THIS_MODULE,
  253. .act = tcf_nat_act,
  254. .dump = tcf_nat_dump,
  255. .init = tcf_nat_init,
  256. .walk = tcf_nat_walker,
  257. .lookup = tcf_nat_search,
  258. .size = sizeof(struct tcf_nat),
  259. };
  260. static __net_init int nat_init_net(struct net *net)
  261. {
  262. struct tc_action_net *tn = net_generic(net, nat_net_id);
  263. return tc_action_net_init(net, tn, &act_nat_ops);
  264. }
  265. static void __net_exit nat_exit_net(struct list_head *net_list)
  266. {
  267. tc_action_net_exit(net_list, nat_net_id);
  268. }
  269. static struct pernet_operations nat_net_ops = {
  270. .init = nat_init_net,
  271. .exit_batch = nat_exit_net,
  272. .id = &nat_net_id,
  273. .size = sizeof(struct tc_action_net),
  274. };
  275. MODULE_DESCRIPTION("Stateless NAT actions");
  276. MODULE_LICENSE("GPL");
  277. static int __init nat_init_module(void)
  278. {
  279. return tcf_register_action(&act_nat_ops, &nat_net_ops);
  280. }
  281. static void __exit nat_cleanup_module(void)
  282. {
  283. tcf_unregister_action(&act_nat_ops, &nat_net_ops);
  284. }
  285. module_init(nat_init_module);
  286. module_exit(nat_cleanup_module);