netfilter.c 5.5 KB

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