netfilter.c 5.2 KB

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