act_nat.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_try_make_writable(skb, sizeof(*iph) + noff))
  107. goto drop;
  108. new_addr &= mask;
  109. new_addr |= addr & ~mask;
  110. /* Rewrite IP header */
  111. iph = ip_hdr(skb);
  112. if (egress)
  113. iph->saddr = new_addr;
  114. else
  115. iph->daddr = new_addr;
  116. csum_replace4(&iph->check, addr, new_addr);
  117. } else if ((iph->frag_off & htons(IP_OFFSET)) ||
  118. iph->protocol != IPPROTO_ICMP) {
  119. goto out;
  120. }
  121. ihl = iph->ihl * 4;
  122. /* It would be nice to share code with stateful NAT. */
  123. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  124. case IPPROTO_TCP:
  125. {
  126. struct tcphdr *tcph;
  127. if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
  128. skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
  129. goto drop;
  130. tcph = (void *)(skb_network_header(skb) + ihl);
  131. inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
  132. true);
  133. break;
  134. }
  135. case IPPROTO_UDP:
  136. {
  137. struct udphdr *udph;
  138. if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
  139. skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
  140. goto drop;
  141. udph = (void *)(skb_network_header(skb) + ihl);
  142. if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  143. inet_proto_csum_replace4(&udph->check, skb, addr,
  144. new_addr, true);
  145. if (!udph->check)
  146. udph->check = CSUM_MANGLED_0;
  147. }
  148. break;
  149. }
  150. case IPPROTO_ICMP:
  151. {
  152. struct icmphdr *icmph;
  153. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
  154. goto drop;
  155. icmph = (void *)(skb_network_header(skb) + ihl);
  156. if ((icmph->type != ICMP_DEST_UNREACH) &&
  157. (icmph->type != ICMP_TIME_EXCEEDED) &&
  158. (icmph->type != ICMP_PARAMETERPROB))
  159. break;
  160. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
  161. noff))
  162. goto drop;
  163. icmph = (void *)(skb_network_header(skb) + ihl);
  164. iph = (void *)(icmph + 1);
  165. if (egress)
  166. addr = iph->daddr;
  167. else
  168. addr = iph->saddr;
  169. if ((old_addr ^ addr) & mask)
  170. break;
  171. if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
  172. sizeof(*iph) + noff))
  173. goto drop;
  174. icmph = (void *)(skb_network_header(skb) + ihl);
  175. iph = (void *)(icmph + 1);
  176. new_addr &= mask;
  177. new_addr |= addr & ~mask;
  178. /* XXX Fix up the inner checksums. */
  179. if (egress)
  180. iph->daddr = new_addr;
  181. else
  182. iph->saddr = new_addr;
  183. inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
  184. false);
  185. break;
  186. }
  187. default:
  188. break;
  189. }
  190. out:
  191. return action;
  192. drop:
  193. spin_lock(&p->tcf_lock);
  194. p->tcf_qstats.drops++;
  195. spin_unlock(&p->tcf_lock);
  196. return TC_ACT_SHOT;
  197. }
  198. static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
  199. int bind, int ref)
  200. {
  201. unsigned char *b = skb_tail_pointer(skb);
  202. struct tcf_nat *p = a->priv;
  203. struct tc_nat opt = {
  204. .old_addr = p->old_addr,
  205. .new_addr = p->new_addr,
  206. .mask = p->mask,
  207. .flags = p->flags,
  208. .index = p->tcf_index,
  209. .action = p->tcf_action,
  210. .refcnt = p->tcf_refcnt - ref,
  211. .bindcnt = p->tcf_bindcnt - bind,
  212. };
  213. struct tcf_t t;
  214. if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
  215. goto nla_put_failure;
  216. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  217. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  218. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  219. if (nla_put(skb, TCA_NAT_TM, sizeof(t), &t))
  220. goto nla_put_failure;
  221. return skb->len;
  222. nla_put_failure:
  223. nlmsg_trim(skb, b);
  224. return -1;
  225. }
  226. static struct tc_action_ops act_nat_ops = {
  227. .kind = "nat",
  228. .type = TCA_ACT_NAT,
  229. .owner = THIS_MODULE,
  230. .act = tcf_nat,
  231. .dump = tcf_nat_dump,
  232. .init = tcf_nat_init,
  233. };
  234. MODULE_DESCRIPTION("Stateless NAT actions");
  235. MODULE_LICENSE("GPL");
  236. static int __init nat_init_module(void)
  237. {
  238. return tcf_register_action(&act_nat_ops, NAT_TAB_MASK);
  239. }
  240. static void __exit nat_cleanup_module(void)
  241. {
  242. tcf_unregister_action(&act_nat_ops);
  243. }
  244. module_init(nat_init_module);
  245. module_exit(nat_cleanup_module);