nf_nat_l3proto_ipv6.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Development of IPv6 NAT funded by Astaro.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter_ipv6.h>
  16. #include <net/secure_seq.h>
  17. #include <net/checksum.h>
  18. #include <net/ip6_checksum.h>
  19. #include <net/ip6_route.h>
  20. #include <net/ipv6.h>
  21. #include <net/netfilter/nf_conntrack_core.h>
  22. #include <net/netfilter/nf_conntrack.h>
  23. #include <net/netfilter/nf_nat_core.h>
  24. #include <net/netfilter/nf_nat_l3proto.h>
  25. #include <net/netfilter/nf_nat_l4proto.h>
  26. static const struct nf_nat_l3proto nf_nat_l3proto_ipv6;
  27. #ifdef CONFIG_XFRM
  28. static void nf_nat_ipv6_decode_session(struct sk_buff *skb,
  29. const struct nf_conn *ct,
  30. enum ip_conntrack_dir dir,
  31. unsigned long statusbit,
  32. struct flowi *fl)
  33. {
  34. const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
  35. struct flowi6 *fl6 = &fl->u.ip6;
  36. if (ct->status & statusbit) {
  37. fl6->daddr = t->dst.u3.in6;
  38. if (t->dst.protonum == IPPROTO_TCP ||
  39. t->dst.protonum == IPPROTO_UDP ||
  40. t->dst.protonum == IPPROTO_UDPLITE ||
  41. t->dst.protonum == IPPROTO_DCCP ||
  42. t->dst.protonum == IPPROTO_SCTP)
  43. fl6->fl6_dport = t->dst.u.all;
  44. }
  45. statusbit ^= IPS_NAT_MASK;
  46. if (ct->status & statusbit) {
  47. fl6->saddr = t->src.u3.in6;
  48. if (t->dst.protonum == IPPROTO_TCP ||
  49. t->dst.protonum == IPPROTO_UDP ||
  50. t->dst.protonum == IPPROTO_UDPLITE ||
  51. t->dst.protonum == IPPROTO_DCCP ||
  52. t->dst.protonum == IPPROTO_SCTP)
  53. fl6->fl6_sport = t->src.u.all;
  54. }
  55. }
  56. #endif
  57. static bool nf_nat_ipv6_in_range(const struct nf_conntrack_tuple *t,
  58. const struct nf_nat_range *range)
  59. {
  60. return ipv6_addr_cmp(&t->src.u3.in6, &range->min_addr.in6) >= 0 &&
  61. ipv6_addr_cmp(&t->src.u3.in6, &range->max_addr.in6) <= 0;
  62. }
  63. static u32 nf_nat_ipv6_secure_port(const struct nf_conntrack_tuple *t,
  64. __be16 dport)
  65. {
  66. return secure_ipv6_port_ephemeral(t->src.u3.ip6, t->dst.u3.ip6, dport);
  67. }
  68. static bool nf_nat_ipv6_manip_pkt(struct sk_buff *skb,
  69. unsigned int iphdroff,
  70. const struct nf_nat_l4proto *l4proto,
  71. const struct nf_conntrack_tuple *target,
  72. enum nf_nat_manip_type maniptype)
  73. {
  74. struct ipv6hdr *ipv6h;
  75. __be16 frag_off;
  76. int hdroff;
  77. u8 nexthdr;
  78. if (!skb_make_writable(skb, iphdroff + sizeof(*ipv6h)))
  79. return false;
  80. ipv6h = (void *)skb->data + iphdroff;
  81. nexthdr = ipv6h->nexthdr;
  82. hdroff = ipv6_skip_exthdr(skb, iphdroff + sizeof(*ipv6h),
  83. &nexthdr, &frag_off);
  84. if (hdroff < 0)
  85. goto manip_addr;
  86. if ((frag_off & htons(~0x7)) == 0 &&
  87. !l4proto->manip_pkt(skb, &nf_nat_l3proto_ipv6, iphdroff, hdroff,
  88. target, maniptype))
  89. return false;
  90. /* must reload, offset might have changed */
  91. ipv6h = (void *)skb->data + iphdroff;
  92. manip_addr:
  93. if (maniptype == NF_NAT_MANIP_SRC)
  94. ipv6h->saddr = target->src.u3.in6;
  95. else
  96. ipv6h->daddr = target->dst.u3.in6;
  97. return true;
  98. }
  99. static void nf_nat_ipv6_csum_update(struct sk_buff *skb,
  100. unsigned int iphdroff, __sum16 *check,
  101. const struct nf_conntrack_tuple *t,
  102. enum nf_nat_manip_type maniptype)
  103. {
  104. const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff);
  105. const struct in6_addr *oldip, *newip;
  106. if (maniptype == NF_NAT_MANIP_SRC) {
  107. oldip = &ipv6h->saddr;
  108. newip = &t->src.u3.in6;
  109. } else {
  110. oldip = &ipv6h->daddr;
  111. newip = &t->dst.u3.in6;
  112. }
  113. inet_proto_csum_replace16(check, skb, oldip->s6_addr32,
  114. newip->s6_addr32, true);
  115. }
  116. static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
  117. u8 proto, void *data, __sum16 *check,
  118. int datalen, int oldlen)
  119. {
  120. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  121. struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
  122. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  123. if (!(rt->rt6i_flags & RTF_LOCAL) &&
  124. (!skb->dev || skb->dev->features & NETIF_F_V6_CSUM)) {
  125. skb->ip_summed = CHECKSUM_PARTIAL;
  126. skb->csum_start = skb_headroom(skb) +
  127. skb_network_offset(skb) +
  128. (data - (void *)skb->data);
  129. skb->csum_offset = (void *)check - data;
  130. *check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  131. datalen, proto, 0);
  132. } else {
  133. *check = 0;
  134. *check = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  135. datalen, proto,
  136. csum_partial(data, datalen,
  137. 0));
  138. if (proto == IPPROTO_UDP && !*check)
  139. *check = CSUM_MANGLED_0;
  140. }
  141. } else
  142. inet_proto_csum_replace2(check, skb,
  143. htons(oldlen), htons(datalen), true);
  144. }
  145. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  146. static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
  147. struct nf_nat_range *range)
  148. {
  149. if (tb[CTA_NAT_V6_MINIP]) {
  150. nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP],
  151. sizeof(struct in6_addr));
  152. range->flags |= NF_NAT_RANGE_MAP_IPS;
  153. }
  154. if (tb[CTA_NAT_V6_MAXIP])
  155. nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
  156. sizeof(struct in6_addr));
  157. else
  158. range->max_addr = range->min_addr;
  159. return 0;
  160. }
  161. #endif
  162. static const struct nf_nat_l3proto nf_nat_l3proto_ipv6 = {
  163. .l3proto = NFPROTO_IPV6,
  164. .secure_port = nf_nat_ipv6_secure_port,
  165. .in_range = nf_nat_ipv6_in_range,
  166. .manip_pkt = nf_nat_ipv6_manip_pkt,
  167. .csum_update = nf_nat_ipv6_csum_update,
  168. .csum_recalc = nf_nat_ipv6_csum_recalc,
  169. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  170. .nlattr_to_range = nf_nat_ipv6_nlattr_to_range,
  171. #endif
  172. #ifdef CONFIG_XFRM
  173. .decode_session = nf_nat_ipv6_decode_session,
  174. #endif
  175. };
  176. int nf_nat_icmpv6_reply_translation(struct sk_buff *skb,
  177. struct nf_conn *ct,
  178. enum ip_conntrack_info ctinfo,
  179. unsigned int hooknum,
  180. unsigned int hdrlen)
  181. {
  182. struct {
  183. struct icmp6hdr icmp6;
  184. struct ipv6hdr ip6;
  185. } *inside;
  186. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  187. enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
  188. const struct nf_nat_l4proto *l4proto;
  189. struct nf_conntrack_tuple target;
  190. unsigned long statusbit;
  191. NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
  192. if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
  193. return 0;
  194. if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6))
  195. return 0;
  196. inside = (void *)skb->data + hdrlen;
  197. if (inside->icmp6.icmp6_type == NDISC_REDIRECT) {
  198. if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
  199. return 0;
  200. if (ct->status & IPS_NAT_MASK)
  201. return 0;
  202. }
  203. if (manip == NF_NAT_MANIP_SRC)
  204. statusbit = IPS_SRC_NAT;
  205. else
  206. statusbit = IPS_DST_NAT;
  207. /* Invert if this is reply direction */
  208. if (dir == IP_CT_DIR_REPLY)
  209. statusbit ^= IPS_NAT_MASK;
  210. if (!(ct->status & statusbit))
  211. return 1;
  212. l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, inside->ip6.nexthdr);
  213. if (!nf_nat_ipv6_manip_pkt(skb, hdrlen + sizeof(inside->icmp6),
  214. l4proto, &ct->tuplehash[!dir].tuple, !manip))
  215. return 0;
  216. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  217. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  218. inside = (void *)skb->data + hdrlen;
  219. inside->icmp6.icmp6_cksum = 0;
  220. inside->icmp6.icmp6_cksum =
  221. csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  222. skb->len - hdrlen, IPPROTO_ICMPV6,
  223. csum_partial(&inside->icmp6,
  224. skb->len - hdrlen, 0));
  225. }
  226. nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
  227. l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, IPPROTO_ICMPV6);
  228. if (!nf_nat_ipv6_manip_pkt(skb, 0, l4proto, &target, manip))
  229. return 0;
  230. return 1;
  231. }
  232. EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation);
  233. unsigned int
  234. nf_nat_ipv6_fn(void *priv, struct sk_buff *skb,
  235. const struct nf_hook_state *state,
  236. unsigned int (*do_chain)(void *priv,
  237. struct sk_buff *skb,
  238. const struct nf_hook_state *state,
  239. struct nf_conn *ct))
  240. {
  241. struct nf_conn *ct;
  242. enum ip_conntrack_info ctinfo;
  243. struct nf_conn_nat *nat;
  244. enum nf_nat_manip_type maniptype = HOOK2MANIP(state->hook);
  245. __be16 frag_off;
  246. int hdrlen;
  247. u8 nexthdr;
  248. ct = nf_ct_get(skb, &ctinfo);
  249. /* Can't track? It's not due to stress, or conntrack would
  250. * have dropped it. Hence it's the user's responsibilty to
  251. * packet filter it out, or implement conntrack/NAT for that
  252. * protocol. 8) --RR
  253. */
  254. if (!ct)
  255. return NF_ACCEPT;
  256. /* Don't try to NAT if this packet is not conntracked */
  257. if (nf_ct_is_untracked(ct))
  258. return NF_ACCEPT;
  259. nat = nf_ct_nat_ext_add(ct);
  260. if (nat == NULL)
  261. return NF_ACCEPT;
  262. switch (ctinfo) {
  263. case IP_CT_RELATED:
  264. case IP_CT_RELATED_REPLY:
  265. nexthdr = ipv6_hdr(skb)->nexthdr;
  266. hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
  267. &nexthdr, &frag_off);
  268. if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
  269. if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
  270. state->hook,
  271. hdrlen))
  272. return NF_DROP;
  273. else
  274. return NF_ACCEPT;
  275. }
  276. /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
  277. case IP_CT_NEW:
  278. /* Seen it before? This can happen for loopback, retrans,
  279. * or local packets.
  280. */
  281. if (!nf_nat_initialized(ct, maniptype)) {
  282. unsigned int ret;
  283. ret = do_chain(priv, skb, state, ct);
  284. if (ret != NF_ACCEPT)
  285. return ret;
  286. if (nf_nat_initialized(ct, HOOK2MANIP(state->hook)))
  287. break;
  288. ret = nf_nat_alloc_null_binding(ct, state->hook);
  289. if (ret != NF_ACCEPT)
  290. return ret;
  291. } else {
  292. pr_debug("Already setup manip %s for ct %p\n",
  293. maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
  294. ct);
  295. if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
  296. goto oif_changed;
  297. }
  298. break;
  299. default:
  300. /* ESTABLISHED */
  301. NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
  302. ctinfo == IP_CT_ESTABLISHED_REPLY);
  303. if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
  304. goto oif_changed;
  305. }
  306. return nf_nat_packet(ct, ctinfo, state->hook, skb);
  307. oif_changed:
  308. nf_ct_kill_acct(ct, ctinfo, skb);
  309. return NF_DROP;
  310. }
  311. EXPORT_SYMBOL_GPL(nf_nat_ipv6_fn);
  312. unsigned int
  313. nf_nat_ipv6_in(void *priv, struct sk_buff *skb,
  314. const struct nf_hook_state *state,
  315. unsigned int (*do_chain)(void *priv,
  316. struct sk_buff *skb,
  317. const struct nf_hook_state *state,
  318. struct nf_conn *ct))
  319. {
  320. unsigned int ret;
  321. struct in6_addr daddr = ipv6_hdr(skb)->daddr;
  322. ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
  323. if (ret != NF_DROP && ret != NF_STOLEN &&
  324. ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
  325. skb_dst_drop(skb);
  326. return ret;
  327. }
  328. EXPORT_SYMBOL_GPL(nf_nat_ipv6_in);
  329. unsigned int
  330. nf_nat_ipv6_out(void *priv, struct sk_buff *skb,
  331. const struct nf_hook_state *state,
  332. unsigned int (*do_chain)(void *priv,
  333. struct sk_buff *skb,
  334. const struct nf_hook_state *state,
  335. struct nf_conn *ct))
  336. {
  337. #ifdef CONFIG_XFRM
  338. const struct nf_conn *ct;
  339. enum ip_conntrack_info ctinfo;
  340. int err;
  341. #endif
  342. unsigned int ret;
  343. /* root is playing with raw sockets. */
  344. if (skb->len < sizeof(struct ipv6hdr))
  345. return NF_ACCEPT;
  346. ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
  347. #ifdef CONFIG_XFRM
  348. if (ret != NF_DROP && ret != NF_STOLEN &&
  349. !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
  350. (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
  351. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  352. if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
  353. &ct->tuplehash[!dir].tuple.dst.u3) ||
  354. (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
  355. ct->tuplehash[dir].tuple.src.u.all !=
  356. ct->tuplehash[!dir].tuple.dst.u.all)) {
  357. err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
  358. if (err < 0)
  359. ret = NF_DROP_ERR(err);
  360. }
  361. }
  362. #endif
  363. return ret;
  364. }
  365. EXPORT_SYMBOL_GPL(nf_nat_ipv6_out);
  366. unsigned int
  367. nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb,
  368. const struct nf_hook_state *state,
  369. unsigned int (*do_chain)(void *priv,
  370. struct sk_buff *skb,
  371. const struct nf_hook_state *state,
  372. struct nf_conn *ct))
  373. {
  374. const struct nf_conn *ct;
  375. enum ip_conntrack_info ctinfo;
  376. unsigned int ret;
  377. int err;
  378. /* root is playing with raw sockets. */
  379. if (skb->len < sizeof(struct ipv6hdr))
  380. return NF_ACCEPT;
  381. ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
  382. if (ret != NF_DROP && ret != NF_STOLEN &&
  383. (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
  384. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  385. if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
  386. &ct->tuplehash[!dir].tuple.src.u3)) {
  387. err = ip6_route_me_harder(state->net, skb);
  388. if (err < 0)
  389. ret = NF_DROP_ERR(err);
  390. }
  391. #ifdef CONFIG_XFRM
  392. else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
  393. ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
  394. ct->tuplehash[dir].tuple.dst.u.all !=
  395. ct->tuplehash[!dir].tuple.src.u.all) {
  396. err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
  397. if (err < 0)
  398. ret = NF_DROP_ERR(err);
  399. }
  400. #endif
  401. }
  402. return ret;
  403. }
  404. EXPORT_SYMBOL_GPL(nf_nat_ipv6_local_fn);
  405. static int __init nf_nat_l3proto_ipv6_init(void)
  406. {
  407. int err;
  408. err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
  409. if (err < 0)
  410. goto err1;
  411. err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv6);
  412. if (err < 0)
  413. goto err2;
  414. return err;
  415. err2:
  416. nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
  417. err1:
  418. return err;
  419. }
  420. static void __exit nf_nat_l3proto_ipv6_exit(void)
  421. {
  422. nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6);
  423. nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
  424. }
  425. MODULE_LICENSE("GPL");
  426. MODULE_ALIAS("nf-nat-" __stringify(AF_INET6));
  427. module_init(nf_nat_l3proto_ipv6_init);
  428. module_exit(nf_nat_l3proto_ipv6_exit);