act_nat.c 6.7 KB

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