netfilter.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * IPv4 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/netfilter.h>
  9. #include <linux/netfilter_ipv4.h>
  10. #include <linux/ip.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/gfp.h>
  13. #include <linux/export.h>
  14. #include <net/route.h>
  15. #include <net/xfrm.h>
  16. #include <net/ip.h>
  17. #include <net/netfilter/nf_queue.h>
  18. /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
  19. int ip_route_me_harder(struct net *net, struct sk_buff *skb, unsigned int addr_type)
  20. {
  21. const struct iphdr *iph = ip_hdr(skb);
  22. struct rtable *rt;
  23. struct flowi4 fl4 = {};
  24. __be32 saddr = iph->saddr;
  25. const struct sock *sk = skb_to_full_sk(skb);
  26. __u8 flags = sk ? inet_sk_flowi_flags(sk) : 0;
  27. unsigned int hh_len;
  28. if (addr_type == RTN_UNSPEC)
  29. addr_type = inet_addr_type(net, saddr);
  30. if (addr_type == RTN_LOCAL || addr_type == RTN_UNICAST)
  31. flags |= FLOWI_FLAG_ANYSRC;
  32. else
  33. saddr = 0;
  34. /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
  35. * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
  36. */
  37. fl4.daddr = iph->daddr;
  38. fl4.saddr = saddr;
  39. fl4.flowi4_tos = RT_TOS(iph->tos);
  40. fl4.flowi4_oif = sk ? sk->sk_bound_dev_if : 0;
  41. fl4.flowi4_mark = skb->mark;
  42. fl4.flowi4_flags = flags;
  43. rt = ip_route_output_key(net, &fl4);
  44. if (IS_ERR(rt))
  45. return PTR_ERR(rt);
  46. /* Drop old route. */
  47. skb_dst_drop(skb);
  48. skb_dst_set(skb, &rt->dst);
  49. if (skb_dst(skb)->error)
  50. return skb_dst(skb)->error;
  51. #ifdef CONFIG_XFRM
  52. if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
  53. xfrm_decode_session(skb, flowi4_to_flowi(&fl4), AF_INET) == 0) {
  54. struct dst_entry *dst = skb_dst(skb);
  55. skb_dst_set(skb, NULL);
  56. dst = xfrm_lookup(net, dst, flowi4_to_flowi(&fl4), sk, 0);
  57. if (IS_ERR(dst))
  58. return PTR_ERR(dst);
  59. skb_dst_set(skb, dst);
  60. }
  61. #endif
  62. /* Change in oif may mean change in hh_len. */
  63. hh_len = skb_dst(skb)->dev->hard_header_len;
  64. if (skb_headroom(skb) < hh_len &&
  65. pskb_expand_head(skb, HH_DATA_ALIGN(hh_len - skb_headroom(skb)),
  66. 0, GFP_ATOMIC))
  67. return -ENOMEM;
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(ip_route_me_harder);
  71. /*
  72. * Extra routing may needed on local out, as the QUEUE target never
  73. * returns control to the table.
  74. */
  75. struct ip_rt_info {
  76. __be32 daddr;
  77. __be32 saddr;
  78. u_int8_t tos;
  79. u_int32_t mark;
  80. };
  81. static void nf_ip_saveroute(const struct sk_buff *skb,
  82. struct nf_queue_entry *entry)
  83. {
  84. struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
  85. if (entry->state.hook == NF_INET_LOCAL_OUT) {
  86. const struct iphdr *iph = ip_hdr(skb);
  87. rt_info->tos = iph->tos;
  88. rt_info->daddr = iph->daddr;
  89. rt_info->saddr = iph->saddr;
  90. rt_info->mark = skb->mark;
  91. }
  92. }
  93. static int nf_ip_reroute(struct net *net, struct sk_buff *skb,
  94. const struct nf_queue_entry *entry)
  95. {
  96. const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
  97. if (entry->state.hook == NF_INET_LOCAL_OUT) {
  98. const struct iphdr *iph = ip_hdr(skb);
  99. if (!(iph->tos == rt_info->tos &&
  100. skb->mark == rt_info->mark &&
  101. iph->daddr == rt_info->daddr &&
  102. iph->saddr == rt_info->saddr))
  103. return ip_route_me_harder(net, skb, RTN_UNSPEC);
  104. }
  105. return 0;
  106. }
  107. __sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
  108. unsigned int dataoff, u_int8_t protocol)
  109. {
  110. const struct iphdr *iph = ip_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 ((protocol == 0 && !csum_fold(skb->csum)) ||
  117. !csum_tcpudp_magic(iph->saddr, iph->daddr,
  118. skb->len - dataoff, protocol,
  119. skb->csum)) {
  120. skb->ip_summed = CHECKSUM_UNNECESSARY;
  121. break;
  122. }
  123. /* fall through */
  124. case CHECKSUM_NONE:
  125. if (protocol == 0)
  126. skb->csum = 0;
  127. else
  128. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  129. skb->len - dataoff,
  130. protocol, 0);
  131. csum = __skb_checksum_complete(skb);
  132. }
  133. return csum;
  134. }
  135. EXPORT_SYMBOL(nf_ip_checksum);
  136. static __sum16 nf_ip_checksum_partial(struct sk_buff *skb, unsigned int hook,
  137. unsigned int dataoff, unsigned int len,
  138. u_int8_t protocol)
  139. {
  140. const struct iphdr *iph = ip_hdr(skb);
  141. __sum16 csum = 0;
  142. switch (skb->ip_summed) {
  143. case CHECKSUM_COMPLETE:
  144. if (len == skb->len - dataoff)
  145. return nf_ip_checksum(skb, hook, dataoff, protocol);
  146. /* fall through */
  147. case CHECKSUM_NONE:
  148. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, protocol,
  149. skb->len - dataoff, 0);
  150. skb->ip_summed = CHECKSUM_NONE;
  151. return __skb_checksum_complete_head(skb, dataoff + len);
  152. }
  153. return csum;
  154. }
  155. static int nf_ip_route(struct net *net, struct dst_entry **dst,
  156. struct flowi *fl, bool strict __always_unused)
  157. {
  158. struct rtable *rt = ip_route_output_key(net, &fl->u.ip4);
  159. if (IS_ERR(rt))
  160. return PTR_ERR(rt);
  161. *dst = &rt->dst;
  162. return 0;
  163. }
  164. static const struct nf_afinfo nf_ip_afinfo = {
  165. .family = AF_INET,
  166. .checksum = nf_ip_checksum,
  167. .checksum_partial = nf_ip_checksum_partial,
  168. .route = nf_ip_route,
  169. .saveroute = nf_ip_saveroute,
  170. .reroute = nf_ip_reroute,
  171. .route_key_size = sizeof(struct ip_rt_info),
  172. };
  173. static int __init ipv4_netfilter_init(void)
  174. {
  175. return nf_register_afinfo(&nf_ip_afinfo);
  176. }
  177. subsys_initcall(ipv4_netfilter_init);