act_nat.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #define NAT_TAB_MASK 15
  30. static struct tcf_common *tcf_nat_ht[NAT_TAB_MASK + 1];
  31. static u32 nat_idx_gen;
  32. static DEFINE_RWLOCK(nat_lock);
  33. static struct tcf_hashinfo nat_hash_info = {
  34. .htab = tcf_nat_ht,
  35. .hmask = NAT_TAB_MASK,
  36. .lock = &nat_lock,
  37. };
  38. static int tcf_nat_init(struct rtattr *rta, struct rtattr *est,
  39. struct tc_action *a, int ovr, int bind)
  40. {
  41. struct rtattr *tb[TCA_NAT_MAX];
  42. struct tc_nat *parm;
  43. int ret = 0;
  44. struct tcf_nat *p;
  45. struct tcf_common *pc;
  46. if (rta == NULL || rtattr_parse_nested(tb, TCA_NAT_MAX, rta) < 0)
  47. return -EINVAL;
  48. if (tb[TCA_NAT_PARMS - 1] == NULL ||
  49. RTA_PAYLOAD(tb[TCA_NAT_PARMS - 1]) < sizeof(*parm))
  50. return -EINVAL;
  51. parm = RTA_DATA(tb[TCA_NAT_PARMS - 1]);
  52. pc = tcf_hash_check(parm->index, a, bind, &nat_hash_info);
  53. if (!pc) {
  54. pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind,
  55. &nat_idx_gen, &nat_hash_info);
  56. if (unlikely(!pc))
  57. return -ENOMEM;
  58. p = to_tcf_nat(pc);
  59. ret = ACT_P_CREATED;
  60. } else {
  61. p = to_tcf_nat(pc);
  62. if (!ovr) {
  63. tcf_hash_release(pc, bind, &nat_hash_info);
  64. return -EEXIST;
  65. }
  66. }
  67. spin_lock_bh(&p->tcf_lock);
  68. p->old_addr = parm->old_addr;
  69. p->new_addr = parm->new_addr;
  70. p->mask = parm->mask;
  71. p->flags = parm->flags;
  72. p->tcf_action = parm->action;
  73. spin_unlock_bh(&p->tcf_lock);
  74. if (ret == ACT_P_CREATED)
  75. tcf_hash_insert(pc, &nat_hash_info);
  76. return ret;
  77. }
  78. static int tcf_nat_cleanup(struct tc_action *a, int bind)
  79. {
  80. struct tcf_nat *p = a->priv;
  81. return tcf_hash_release(&p->common, bind, &nat_hash_info);
  82. }
  83. static int tcf_nat(struct sk_buff *skb, struct tc_action *a,
  84. struct tcf_result *res)
  85. {
  86. struct tcf_nat *p = a->priv;
  87. struct iphdr *iph;
  88. __be32 old_addr;
  89. __be32 new_addr;
  90. __be32 mask;
  91. __be32 addr;
  92. int egress;
  93. int action;
  94. int ihl;
  95. spin_lock(&p->tcf_lock);
  96. p->tcf_tm.lastuse = jiffies;
  97. old_addr = p->old_addr;
  98. new_addr = p->new_addr;
  99. mask = p->mask;
  100. egress = p->flags & TCA_NAT_FLAG_EGRESS;
  101. action = p->tcf_action;
  102. p->tcf_bstats.bytes += skb->len;
  103. p->tcf_bstats.packets++;
  104. spin_unlock(&p->tcf_lock);
  105. if (unlikely(action == TC_ACT_SHOT))
  106. goto drop;
  107. if (!pskb_may_pull(skb, sizeof(*iph)))
  108. goto drop;
  109. iph = ip_hdr(skb);
  110. if (egress)
  111. addr = iph->saddr;
  112. else
  113. addr = iph->daddr;
  114. if (!((old_addr ^ addr) & mask)) {
  115. if (skb_cloned(skb) &&
  116. !skb_clone_writable(skb, sizeof(*iph)) &&
  117. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  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. nf_csum_replace4(&iph->check, addr, new_addr);
  128. }
  129. ihl = iph->ihl * 4;
  130. /* It would be nice to share code with stateful NAT. */
  131. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  132. case IPPROTO_TCP:
  133. {
  134. struct tcphdr *tcph;
  135. if (!pskb_may_pull(skb, ihl + sizeof(*tcph)) ||
  136. (skb_cloned(skb) &&
  137. !skb_clone_writable(skb, ihl + sizeof(*tcph)) &&
  138. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  139. goto drop;
  140. tcph = (void *)(skb_network_header(skb) + ihl);
  141. nf_proto_csum_replace4(&tcph->check, skb, addr, new_addr, 1);
  142. break;
  143. }
  144. case IPPROTO_UDP:
  145. {
  146. struct udphdr *udph;
  147. if (!pskb_may_pull(skb, ihl + sizeof(*udph)) ||
  148. (skb_cloned(skb) &&
  149. !skb_clone_writable(skb, ihl + sizeof(*udph)) &&
  150. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  151. goto drop;
  152. udph = (void *)(skb_network_header(skb) + ihl);
  153. if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  154. nf_proto_csum_replace4(&udph->check, skb, addr,
  155. new_addr, 1);
  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) + sizeof(*iph)))
  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. iph = (void *)(icmph + 1);
  172. if (egress)
  173. addr = iph->daddr;
  174. else
  175. addr = iph->saddr;
  176. if ((old_addr ^ addr) & mask)
  177. break;
  178. if (skb_cloned(skb) &&
  179. !skb_clone_writable(skb,
  180. ihl + sizeof(*icmph) + sizeof(*iph)) &&
  181. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  182. goto drop;
  183. icmph = (void *)(skb_network_header(skb) + ihl);
  184. iph = (void *)(icmph + 1);
  185. new_addr &= mask;
  186. new_addr |= addr & ~mask;
  187. /* XXX Fix up the inner checksums. */
  188. if (egress)
  189. iph->daddr = new_addr;
  190. else
  191. iph->saddr = new_addr;
  192. nf_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
  193. 1);
  194. break;
  195. }
  196. default:
  197. break;
  198. }
  199. return action;
  200. drop:
  201. spin_lock(&p->tcf_lock);
  202. p->tcf_qstats.drops++;
  203. spin_unlock(&p->tcf_lock);
  204. return TC_ACT_SHOT;
  205. }
  206. static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
  207. int bind, int ref)
  208. {
  209. unsigned char *b = skb_tail_pointer(skb);
  210. struct tcf_nat *p = a->priv;
  211. struct tc_nat *opt;
  212. struct tcf_t t;
  213. int s;
  214. s = sizeof(*opt);
  215. /* netlink spinlocks held above us - must use ATOMIC */
  216. opt = kzalloc(s, GFP_ATOMIC);
  217. if (unlikely(!opt))
  218. return -ENOBUFS;
  219. opt->old_addr = p->old_addr;
  220. opt->new_addr = p->new_addr;
  221. opt->mask = p->mask;
  222. opt->flags = p->flags;
  223. opt->index = p->tcf_index;
  224. opt->action = p->tcf_action;
  225. opt->refcnt = p->tcf_refcnt - ref;
  226. opt->bindcnt = p->tcf_bindcnt - bind;
  227. RTA_PUT(skb, TCA_NAT_PARMS, s, opt);
  228. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  229. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  230. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  231. RTA_PUT(skb, TCA_NAT_TM, sizeof(t), &t);
  232. kfree(opt);
  233. return skb->len;
  234. rtattr_failure:
  235. nlmsg_trim(skb, b);
  236. kfree(opt);
  237. return -1;
  238. }
  239. static struct tc_action_ops act_nat_ops = {
  240. .kind = "nat",
  241. .hinfo = &nat_hash_info,
  242. .type = TCA_ACT_NAT,
  243. .capab = TCA_CAP_NONE,
  244. .owner = THIS_MODULE,
  245. .act = tcf_nat,
  246. .dump = tcf_nat_dump,
  247. .cleanup = tcf_nat_cleanup,
  248. .lookup = tcf_hash_search,
  249. .init = tcf_nat_init,
  250. .walk = tcf_generic_walker
  251. };
  252. MODULE_DESCRIPTION("Stateless NAT actions");
  253. MODULE_LICENSE("GPL");
  254. static int __init nat_init_module(void)
  255. {
  256. return tcf_register_action(&act_nat_ops);
  257. }
  258. static void __exit nat_cleanup_module(void)
  259. {
  260. tcf_unregister_action(&act_nat_ops);
  261. }
  262. module_init(nat_init_module);
  263. module_exit(nat_cleanup_module);