netfilter.c 5.5 KB

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