netfilter.c 5.3 KB

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