act_nat.c 7.3 KB

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