netfilter.c 4.5 KB

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