act_nat.c 6.7 KB

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