act_nat.c 7.2 KB

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