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