netfilter.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/ipv6.h>
  4. #include <linux/netfilter.h>
  5. #include <linux/netfilter_ipv6.h>
  6. #include <net/dst.h>
  7. #include <net/ipv6.h>
  8. #include <net/ip6_route.h>
  9. #include <net/xfrm.h>
  10. #include <net/ip6_checksum.h>
  11. #include <net/netfilter/nf_queue.h>
  12. int ip6_route_me_harder(struct sk_buff *skb)
  13. {
  14. struct net *net = dev_net(skb_dst(skb)->dev);
  15. const struct ipv6hdr *iph = ipv6_hdr(skb);
  16. struct dst_entry *dst;
  17. struct flowi6 fl6 = {
  18. .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
  19. .flowi6_mark = skb->mark,
  20. .daddr = iph->daddr,
  21. .saddr = iph->saddr,
  22. };
  23. dst = ip6_route_output(net, skb->sk, &fl6);
  24. if (dst->error) {
  25. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  26. LIMIT_NETDEBUG(KERN_DEBUG "ip6_route_me_harder: No more route.\n");
  27. dst_release(dst);
  28. return -EINVAL;
  29. }
  30. /* Drop old route. */
  31. skb_dst_drop(skb);
  32. skb_dst_set(skb, dst);
  33. #ifdef CONFIG_XFRM
  34. if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
  35. xfrm_decode_session(skb, flowi6_to_flowi(&fl6), AF_INET6) == 0) {
  36. skb_dst_set(skb, NULL);
  37. dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), skb->sk, 0);
  38. if (IS_ERR(dst))
  39. return -1;
  40. skb_dst_set(skb, dst);
  41. }
  42. #endif
  43. return 0;
  44. }
  45. EXPORT_SYMBOL(ip6_route_me_harder);
  46. /*
  47. * Extra routing may needed on local out, as the QUEUE target never
  48. * returns control to the table.
  49. */
  50. struct ip6_rt_info {
  51. struct in6_addr daddr;
  52. struct in6_addr saddr;
  53. u_int32_t mark;
  54. };
  55. static void nf_ip6_saveroute(const struct sk_buff *skb,
  56. struct nf_queue_entry *entry)
  57. {
  58. struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
  59. if (entry->hook == NF_INET_LOCAL_OUT) {
  60. const struct ipv6hdr *iph = ipv6_hdr(skb);
  61. rt_info->daddr = iph->daddr;
  62. rt_info->saddr = iph->saddr;
  63. rt_info->mark = skb->mark;
  64. }
  65. }
  66. static int nf_ip6_reroute(struct sk_buff *skb,
  67. const struct nf_queue_entry *entry)
  68. {
  69. struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
  70. if (entry->hook == NF_INET_LOCAL_OUT) {
  71. const struct ipv6hdr *iph = ipv6_hdr(skb);
  72. if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
  73. !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
  74. skb->mark != rt_info->mark)
  75. return ip6_route_me_harder(skb);
  76. }
  77. return 0;
  78. }
  79. static int nf_ip6_route(struct net *net, struct dst_entry **dst,
  80. struct flowi *fl, bool strict)
  81. {
  82. static const struct ipv6_pinfo fake_pinfo;
  83. static const struct inet_sock fake_sk = {
  84. /* makes ip6_route_output set RT6_LOOKUP_F_IFACE: */
  85. .sk.sk_bound_dev_if = 1,
  86. .pinet6 = (struct ipv6_pinfo *) &fake_pinfo,
  87. };
  88. const void *sk = strict ? &fake_sk : NULL;
  89. *dst = ip6_route_output(net, sk, &fl->u.ip6);
  90. return (*dst)->error;
  91. }
  92. __sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
  93. unsigned int dataoff, u_int8_t protocol)
  94. {
  95. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  96. __sum16 csum = 0;
  97. switch (skb->ip_summed) {
  98. case CHECKSUM_COMPLETE:
  99. if (hook != NF_INET_PRE_ROUTING && hook != NF_INET_LOCAL_IN)
  100. break;
  101. if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  102. skb->len - dataoff, protocol,
  103. csum_sub(skb->csum,
  104. skb_checksum(skb, 0,
  105. dataoff, 0)))) {
  106. skb->ip_summed = CHECKSUM_UNNECESSARY;
  107. break;
  108. }
  109. /* fall through */
  110. case CHECKSUM_NONE:
  111. skb->csum = ~csum_unfold(
  112. csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  113. skb->len - dataoff,
  114. protocol,
  115. csum_sub(0,
  116. skb_checksum(skb, 0,
  117. dataoff, 0))));
  118. csum = __skb_checksum_complete(skb);
  119. }
  120. return csum;
  121. }
  122. EXPORT_SYMBOL(nf_ip6_checksum);
  123. static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
  124. unsigned int dataoff, unsigned int len,
  125. u_int8_t protocol)
  126. {
  127. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  128. __wsum hsum;
  129. __sum16 csum = 0;
  130. switch (skb->ip_summed) {
  131. case CHECKSUM_COMPLETE:
  132. if (len == skb->len - dataoff)
  133. return nf_ip6_checksum(skb, hook, dataoff, protocol);
  134. /* fall through */
  135. case CHECKSUM_NONE:
  136. hsum = skb_checksum(skb, 0, dataoff, 0);
  137. skb->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
  138. &ip6h->daddr,
  139. skb->len - dataoff,
  140. protocol,
  141. csum_sub(0, hsum)));
  142. skb->ip_summed = CHECKSUM_NONE;
  143. return __skb_checksum_complete_head(skb, dataoff + len);
  144. }
  145. return csum;
  146. };
  147. static const struct nf_afinfo nf_ip6_afinfo = {
  148. .family = AF_INET6,
  149. .checksum = nf_ip6_checksum,
  150. .checksum_partial = nf_ip6_checksum_partial,
  151. .route = nf_ip6_route,
  152. .saveroute = nf_ip6_saveroute,
  153. .reroute = nf_ip6_reroute,
  154. .route_key_size = sizeof(struct ip6_rt_info),
  155. };
  156. int __init ipv6_netfilter_init(void)
  157. {
  158. return nf_register_afinfo(&nf_ip6_afinfo);
  159. }
  160. /* This can be called from inet6_init() on errors, so it cannot
  161. * be marked __exit. -DaveM
  162. */
  163. void ipv6_netfilter_fini(void)
  164. {
  165. nf_unregister_afinfo(&nf_ip6_afinfo);
  166. }