act_nat.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 nlattr *nla, struct nlattr *est,
  39. struct tc_action *a, int ovr, int bind)
  40. {
  41. struct nlattr *tb[TCA_NAT_MAX + 1];
  42. struct tc_nat *parm;
  43. int ret = 0, err;
  44. struct tcf_nat *p;
  45. struct tcf_common *pc;
  46. if (nla == NULL)
  47. return -EINVAL;
  48. err = nla_parse_nested(tb, TCA_NAT_MAX, nla, NULL);
  49. if (err < 0)
  50. return err;
  51. if (tb[TCA_NAT_PARMS] == NULL ||
  52. nla_len(tb[TCA_NAT_PARMS]) < sizeof(*parm))
  53. return -EINVAL;
  54. parm = nla_data(tb[TCA_NAT_PARMS]);
  55. pc = tcf_hash_check(parm->index, a, bind, &nat_hash_info);
  56. if (!pc) {
  57. pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind,
  58. &nat_idx_gen, &nat_hash_info);
  59. if (unlikely(!pc))
  60. return -ENOMEM;
  61. p = to_tcf_nat(pc);
  62. ret = ACT_P_CREATED;
  63. } else {
  64. p = to_tcf_nat(pc);
  65. if (!ovr) {
  66. tcf_hash_release(pc, bind, &nat_hash_info);
  67. return -EEXIST;
  68. }
  69. }
  70. spin_lock_bh(&p->tcf_lock);
  71. p->old_addr = parm->old_addr;
  72. p->new_addr = parm->new_addr;
  73. p->mask = parm->mask;
  74. p->flags = parm->flags;
  75. p->tcf_action = parm->action;
  76. spin_unlock_bh(&p->tcf_lock);
  77. if (ret == ACT_P_CREATED)
  78. tcf_hash_insert(pc, &nat_hash_info);
  79. return ret;
  80. }
  81. static int tcf_nat_cleanup(struct tc_action *a, int bind)
  82. {
  83. struct tcf_nat *p = a->priv;
  84. return tcf_hash_release(&p->common, bind, &nat_hash_info);
  85. }
  86. static int tcf_nat(struct sk_buff *skb, struct tc_action *a,
  87. struct tcf_result *res)
  88. {
  89. struct tcf_nat *p = a->priv;
  90. struct iphdr *iph;
  91. __be32 old_addr;
  92. __be32 new_addr;
  93. __be32 mask;
  94. __be32 addr;
  95. int egress;
  96. int action;
  97. int ihl;
  98. spin_lock(&p->tcf_lock);
  99. p->tcf_tm.lastuse = jiffies;
  100. old_addr = p->old_addr;
  101. new_addr = p->new_addr;
  102. mask = p->mask;
  103. egress = p->flags & TCA_NAT_FLAG_EGRESS;
  104. action = p->tcf_action;
  105. p->tcf_bstats.bytes += skb->len;
  106. p->tcf_bstats.packets++;
  107. spin_unlock(&p->tcf_lock);
  108. if (unlikely(action == TC_ACT_SHOT))
  109. goto drop;
  110. if (!pskb_may_pull(skb, sizeof(*iph)))
  111. goto drop;
  112. iph = ip_hdr(skb);
  113. if (egress)
  114. addr = iph->saddr;
  115. else
  116. addr = iph->daddr;
  117. if (!((old_addr ^ addr) & mask)) {
  118. if (skb_cloned(skb) &&
  119. !skb_clone_writable(skb, sizeof(*iph)) &&
  120. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  121. goto drop;
  122. new_addr &= mask;
  123. new_addr |= addr & ~mask;
  124. /* Rewrite IP header */
  125. iph = ip_hdr(skb);
  126. if (egress)
  127. iph->saddr = new_addr;
  128. else
  129. iph->daddr = new_addr;
  130. csum_replace4(&iph->check, addr, new_addr);
  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)) ||
  139. (skb_cloned(skb) &&
  140. !skb_clone_writable(skb, ihl + sizeof(*tcph)) &&
  141. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  142. goto drop;
  143. tcph = (void *)(skb_network_header(skb) + ihl);
  144. inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, 1);
  145. break;
  146. }
  147. case IPPROTO_UDP:
  148. {
  149. struct udphdr *udph;
  150. if (!pskb_may_pull(skb, ihl + sizeof(*udph)) ||
  151. (skb_cloned(skb) &&
  152. !skb_clone_writable(skb, ihl + sizeof(*udph)) &&
  153. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  154. goto drop;
  155. udph = (void *)(skb_network_header(skb) + ihl);
  156. if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  157. inet_proto_csum_replace4(&udph->check, skb, addr,
  158. new_addr, 1);
  159. if (!udph->check)
  160. udph->check = CSUM_MANGLED_0;
  161. }
  162. break;
  163. }
  164. case IPPROTO_ICMP:
  165. {
  166. struct icmphdr *icmph;
  167. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph)))
  168. goto drop;
  169. icmph = (void *)(skb_network_header(skb) + ihl);
  170. if ((icmph->type != ICMP_DEST_UNREACH) &&
  171. (icmph->type != ICMP_TIME_EXCEEDED) &&
  172. (icmph->type != ICMP_PARAMETERPROB))
  173. break;
  174. iph = (void *)(icmph + 1);
  175. if (egress)
  176. addr = iph->daddr;
  177. else
  178. addr = iph->saddr;
  179. if ((old_addr ^ addr) & mask)
  180. break;
  181. if (skb_cloned(skb) &&
  182. !skb_clone_writable(skb,
  183. ihl + sizeof(*icmph) + sizeof(*iph)) &&
  184. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  185. goto drop;
  186. icmph = (void *)(skb_network_header(skb) + ihl);
  187. iph = (void *)(icmph + 1);
  188. new_addr &= mask;
  189. new_addr |= addr & ~mask;
  190. /* XXX Fix up the inner checksums. */
  191. if (egress)
  192. iph->daddr = new_addr;
  193. else
  194. iph->saddr = new_addr;
  195. inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
  196. 1);
  197. break;
  198. }
  199. default:
  200. break;
  201. }
  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 = a->priv;
  214. struct tc_nat *opt;
  215. struct tcf_t t;
  216. int s;
  217. s = sizeof(*opt);
  218. /* netlink spinlocks held above us - must use ATOMIC */
  219. opt = kzalloc(s, GFP_ATOMIC);
  220. if (unlikely(!opt))
  221. return -ENOBUFS;
  222. opt->old_addr = p->old_addr;
  223. opt->new_addr = p->new_addr;
  224. opt->mask = p->mask;
  225. opt->flags = p->flags;
  226. opt->index = p->tcf_index;
  227. opt->action = p->tcf_action;
  228. opt->refcnt = p->tcf_refcnt - ref;
  229. opt->bindcnt = p->tcf_bindcnt - bind;
  230. NLA_PUT(skb, TCA_NAT_PARMS, s, opt);
  231. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  232. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  233. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  234. NLA_PUT(skb, TCA_NAT_TM, sizeof(t), &t);
  235. kfree(opt);
  236. return skb->len;
  237. nla_put_failure:
  238. nlmsg_trim(skb, b);
  239. kfree(opt);
  240. return -1;
  241. }
  242. static struct tc_action_ops act_nat_ops = {
  243. .kind = "nat",
  244. .hinfo = &nat_hash_info,
  245. .type = TCA_ACT_NAT,
  246. .capab = TCA_CAP_NONE,
  247. .owner = THIS_MODULE,
  248. .act = tcf_nat,
  249. .dump = tcf_nat_dump,
  250. .cleanup = tcf_nat_cleanup,
  251. .lookup = tcf_hash_search,
  252. .init = tcf_nat_init,
  253. .walk = tcf_generic_walker
  254. };
  255. MODULE_DESCRIPTION("Stateless NAT actions");
  256. MODULE_LICENSE("GPL");
  257. static int __init nat_init_module(void)
  258. {
  259. return tcf_register_action(&act_nat_ops);
  260. }
  261. static void __exit nat_cleanup_module(void)
  262. {
  263. tcf_unregister_action(&act_nat_ops);
  264. }
  265. module_init(nat_init_module);
  266. module_exit(nat_cleanup_module);