act_nat.c 7.4 KB

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