netfilter.c 5.1 KB

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