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