netfilter.c 5.5 KB

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