netfilter.c 5.3 KB

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