act_nat.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_hashinfo nat_hash_info;
  31. static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
  32. [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
  33. };
  34. static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
  35. struct tc_action *a, int ovr, int bind)
  36. {
  37. struct nlattr *tb[TCA_NAT_MAX + 1];
  38. struct tc_nat *parm;
  39. int ret = 0, err;
  40. struct tcf_nat *p;
  41. struct tcf_common *pc;
  42. if (nla == NULL)
  43. return -EINVAL;
  44. err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy);
  45. if (err < 0)
  46. return err;
  47. if (tb[TCA_NAT_PARMS] == NULL)
  48. return -EINVAL;
  49. parm = nla_data(tb[TCA_NAT_PARMS]);
  50. pc = tcf_hash_check(parm->index, a, bind);
  51. if (!pc) {
  52. pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind);
  53. if (IS_ERR(pc))
  54. return PTR_ERR(pc);
  55. ret = ACT_P_CREATED;
  56. } else {
  57. if (bind)
  58. return 0;
  59. tcf_hash_release(pc, bind, a->ops->hinfo);
  60. if (!ovr)
  61. return -EEXIST;
  62. }
  63. p = to_tcf_nat(pc);
  64. spin_lock_bh(&p->tcf_lock);
  65. p->old_addr = parm->old_addr;
  66. p->new_addr = parm->new_addr;
  67. p->mask = parm->mask;
  68. p->flags = parm->flags;
  69. p->tcf_action = parm->action;
  70. spin_unlock_bh(&p->tcf_lock);
  71. if (ret == ACT_P_CREATED)
  72. tcf_hash_insert(pc, a->ops->hinfo);
  73. return ret;
  74. }
  75. static int tcf_nat_cleanup(struct tc_action *a, int bind)
  76. {
  77. struct tcf_nat *p = a->priv;
  78. return tcf_hash_release(&p->common, bind, &nat_hash_info);
  79. }
  80. static int tcf_nat(struct sk_buff *skb, const struct tc_action *a,
  81. struct tcf_result *res)
  82. {
  83. struct tcf_nat *p = a->priv;
  84. struct iphdr *iph;
  85. __be32 old_addr;
  86. __be32 new_addr;
  87. __be32 mask;
  88. __be32 addr;
  89. int egress;
  90. int action;
  91. int ihl;
  92. int noff;
  93. spin_lock(&p->tcf_lock);
  94. p->tcf_tm.lastuse = jiffies;
  95. old_addr = p->old_addr;
  96. new_addr = p->new_addr;
  97. mask = p->mask;
  98. egress = p->flags & TCA_NAT_FLAG_EGRESS;
  99. action = p->tcf_action;
  100. bstats_update(&p->tcf_bstats, skb);
  101. spin_unlock(&p->tcf_lock);
  102. if (unlikely(action == TC_ACT_SHOT))
  103. goto drop;
  104. noff = skb_network_offset(skb);
  105. if (!pskb_may_pull(skb, sizeof(*iph) + noff))
  106. goto drop;
  107. iph = ip_hdr(skb);
  108. if (egress)
  109. addr = iph->saddr;
  110. else
  111. addr = iph->daddr;
  112. if (!((old_addr ^ addr) & mask)) {
  113. if (skb_cloned(skb) &&
  114. !skb_clone_writable(skb, sizeof(*iph) + noff) &&
  115. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  116. goto drop;
  117. new_addr &= mask;
  118. new_addr |= addr & ~mask;
  119. /* Rewrite IP header */
  120. iph = ip_hdr(skb);
  121. if (egress)
  122. iph->saddr = new_addr;
  123. else
  124. iph->daddr = new_addr;
  125. csum_replace4(&iph->check, addr, new_addr);
  126. } else if ((iph->frag_off & htons(IP_OFFSET)) ||
  127. iph->protocol != IPPROTO_ICMP) {
  128. goto out;
  129. }
  130. ihl = iph->ihl * 4;
  131. /* It would be nice to share code with stateful NAT. */
  132. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  133. case IPPROTO_TCP:
  134. {
  135. struct tcphdr *tcph;
  136. if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
  137. (skb_cloned(skb) &&
  138. !skb_clone_writable(skb, ihl + sizeof(*tcph) + noff) &&
  139. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  140. goto drop;
  141. tcph = (void *)(skb_network_header(skb) + ihl);
  142. inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, 1);
  143. break;
  144. }
  145. case IPPROTO_UDP:
  146. {
  147. struct udphdr *udph;
  148. if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
  149. (skb_cloned(skb) &&
  150. !skb_clone_writable(skb, ihl + sizeof(*udph) + noff) &&
  151. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  152. goto drop;
  153. udph = (void *)(skb_network_header(skb) + ihl);
  154. if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  155. inet_proto_csum_replace4(&udph->check, skb, addr,
  156. new_addr, 1);
  157. if (!udph->check)
  158. udph->check = CSUM_MANGLED_0;
  159. }
  160. break;
  161. }
  162. case IPPROTO_ICMP:
  163. {
  164. struct icmphdr *icmph;
  165. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
  166. goto drop;
  167. icmph = (void *)(skb_network_header(skb) + ihl);
  168. if ((icmph->type != ICMP_DEST_UNREACH) &&
  169. (icmph->type != ICMP_TIME_EXCEEDED) &&
  170. (icmph->type != ICMP_PARAMETERPROB))
  171. break;
  172. if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
  173. noff))
  174. goto drop;
  175. icmph = (void *)(skb_network_header(skb) + ihl);
  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, ihl + sizeof(*icmph) +
  185. sizeof(*iph) + noff) &&
  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. 0);
  199. break;
  200. }
  201. default:
  202. break;
  203. }
  204. out:
  205. return action;
  206. drop:
  207. spin_lock(&p->tcf_lock);
  208. p->tcf_qstats.drops++;
  209. spin_unlock(&p->tcf_lock);
  210. return TC_ACT_SHOT;
  211. }
  212. static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
  213. int bind, int ref)
  214. {
  215. unsigned char *b = skb_tail_pointer(skb);
  216. struct tcf_nat *p = a->priv;
  217. struct tc_nat opt = {
  218. .old_addr = p->old_addr,
  219. .new_addr = p->new_addr,
  220. .mask = p->mask,
  221. .flags = p->flags,
  222. .index = p->tcf_index,
  223. .action = p->tcf_action,
  224. .refcnt = p->tcf_refcnt - ref,
  225. .bindcnt = p->tcf_bindcnt - bind,
  226. };
  227. struct tcf_t t;
  228. if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
  229. goto nla_put_failure;
  230. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  231. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  232. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  233. if (nla_put(skb, TCA_NAT_TM, sizeof(t), &t))
  234. goto nla_put_failure;
  235. return skb->len;
  236. nla_put_failure:
  237. nlmsg_trim(skb, b);
  238. return -1;
  239. }
  240. static struct tc_action_ops act_nat_ops = {
  241. .kind = "nat",
  242. .hinfo = &nat_hash_info,
  243. .type = TCA_ACT_NAT,
  244. .owner = THIS_MODULE,
  245. .act = tcf_nat,
  246. .dump = tcf_nat_dump,
  247. .cleanup = tcf_nat_cleanup,
  248. .init = tcf_nat_init,
  249. };
  250. MODULE_DESCRIPTION("Stateless NAT actions");
  251. MODULE_LICENSE("GPL");
  252. static int __init nat_init_module(void)
  253. {
  254. int err = tcf_hashinfo_init(&nat_hash_info, NAT_TAB_MASK);
  255. if (err)
  256. return err;
  257. return tcf_register_action(&act_nat_ops);
  258. }
  259. static void __exit nat_cleanup_module(void)
  260. {
  261. tcf_unregister_action(&act_nat_ops);
  262. tcf_hashinfo_destroy(&nat_hash_info);
  263. }
  264. module_init(nat_init_module);
  265. module_exit(nat_cleanup_module);