act_nat.c 7.4 KB

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