netfilter.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* IPv4 specific functions of netfilter core */
  2. #include <linux/kernel.h>
  3. #include <linux/netfilter.h>
  4. #include <linux/netfilter_ipv4.h>
  5. #include <linux/ip.h>
  6. #include <linux/skbuff.h>
  7. #include <net/route.h>
  8. #include <net/xfrm.h>
  9. #include <net/ip.h>
  10. /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
  11. int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type)
  12. {
  13. const struct iphdr *iph = ip_hdr(skb);
  14. struct rtable *rt;
  15. struct flowi fl = {};
  16. struct dst_entry *odst;
  17. unsigned int hh_len;
  18. unsigned int type;
  19. type = inet_addr_type(iph->saddr);
  20. if (addr_type == RTN_UNSPEC)
  21. addr_type = type;
  22. /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
  23. * packets with foreign saddr to appear on the NF_IP_LOCAL_OUT hook.
  24. */
  25. if (addr_type == RTN_LOCAL) {
  26. fl.nl_u.ip4_u.daddr = iph->daddr;
  27. if (type == RTN_LOCAL)
  28. fl.nl_u.ip4_u.saddr = iph->saddr;
  29. fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
  30. fl.oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
  31. fl.mark = skb->mark;
  32. if (ip_route_output_key(&rt, &fl) != 0)
  33. return -1;
  34. /* Drop old route. */
  35. dst_release(skb->dst);
  36. skb->dst = &rt->u.dst;
  37. } else {
  38. /* non-local src, find valid iif to satisfy
  39. * rp-filter when calling ip_route_input. */
  40. fl.nl_u.ip4_u.daddr = iph->saddr;
  41. if (ip_route_output_key(&rt, &fl) != 0)
  42. return -1;
  43. odst = skb->dst;
  44. if (ip_route_input(skb, iph->daddr, iph->saddr,
  45. RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
  46. dst_release(&rt->u.dst);
  47. return -1;
  48. }
  49. dst_release(&rt->u.dst);
  50. dst_release(odst);
  51. }
  52. if (skb->dst->error)
  53. return -1;
  54. #ifdef CONFIG_XFRM
  55. if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
  56. xfrm_decode_session(skb, &fl, AF_INET) == 0)
  57. if (xfrm_lookup(&skb->dst, &fl, skb->sk, 0))
  58. return -1;
  59. #endif
  60. /* Change in oif may mean change in hh_len. */
  61. hh_len = skb->dst->dev->hard_header_len;
  62. if (skb_headroom(skb) < hh_len &&
  63. pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
  64. return -1;
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(ip_route_me_harder);
  68. #ifdef CONFIG_XFRM
  69. int ip_xfrm_me_harder(struct sk_buff *skb)
  70. {
  71. struct flowi fl;
  72. unsigned int hh_len;
  73. struct dst_entry *dst;
  74. if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
  75. return 0;
  76. if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
  77. return -1;
  78. dst = skb->dst;
  79. if (dst->xfrm)
  80. dst = ((struct xfrm_dst *)dst)->route;
  81. dst_hold(dst);
  82. if (xfrm_lookup(&dst, &fl, skb->sk, 0) < 0)
  83. return -1;
  84. dst_release(skb->dst);
  85. skb->dst = dst;
  86. /* Change in oif may mean change in hh_len. */
  87. hh_len = skb->dst->dev->hard_header_len;
  88. if (skb_headroom(skb) < hh_len &&
  89. pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
  90. return -1;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(ip_xfrm_me_harder);
  94. #endif
  95. void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  96. EXPORT_SYMBOL(ip_nat_decode_session);
  97. /*
  98. * Extra routing may needed on local out, as the QUEUE target never
  99. * returns control to the table.
  100. */
  101. struct ip_rt_info {
  102. __be32 daddr;
  103. __be32 saddr;
  104. u_int8_t tos;
  105. };
  106. static void nf_ip_saveroute(const struct sk_buff *skb, struct nf_info *info)
  107. {
  108. struct ip_rt_info *rt_info = nf_info_reroute(info);
  109. if (info->hook == NF_IP_LOCAL_OUT) {
  110. const struct iphdr *iph = ip_hdr(skb);
  111. rt_info->tos = iph->tos;
  112. rt_info->daddr = iph->daddr;
  113. rt_info->saddr = iph->saddr;
  114. }
  115. }
  116. static int nf_ip_reroute(struct sk_buff *skb, const struct nf_info *info)
  117. {
  118. const struct ip_rt_info *rt_info = nf_info_reroute(info);
  119. if (info->hook == NF_IP_LOCAL_OUT) {
  120. const struct iphdr *iph = ip_hdr(skb);
  121. if (!(iph->tos == rt_info->tos
  122. && iph->daddr == rt_info->daddr
  123. && iph->saddr == rt_info->saddr))
  124. return ip_route_me_harder(skb, RTN_UNSPEC);
  125. }
  126. return 0;
  127. }
  128. __sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
  129. unsigned int dataoff, u_int8_t protocol)
  130. {
  131. const struct iphdr *iph = ip_hdr(skb);
  132. __sum16 csum = 0;
  133. switch (skb->ip_summed) {
  134. case CHECKSUM_COMPLETE:
  135. if (hook != NF_IP_PRE_ROUTING && hook != NF_IP_LOCAL_IN)
  136. break;
  137. if ((protocol == 0 && !csum_fold(skb->csum)) ||
  138. !csum_tcpudp_magic(iph->saddr, iph->daddr,
  139. skb->len - dataoff, protocol,
  140. skb->csum)) {
  141. skb->ip_summed = CHECKSUM_UNNECESSARY;
  142. break;
  143. }
  144. /* fall through */
  145. case CHECKSUM_NONE:
  146. if (protocol == 0)
  147. skb->csum = 0;
  148. else
  149. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  150. skb->len - dataoff,
  151. protocol, 0);
  152. csum = __skb_checksum_complete(skb);
  153. }
  154. return csum;
  155. }
  156. EXPORT_SYMBOL(nf_ip_checksum);
  157. static struct nf_afinfo nf_ip_afinfo = {
  158. .family = AF_INET,
  159. .checksum = nf_ip_checksum,
  160. .saveroute = nf_ip_saveroute,
  161. .reroute = nf_ip_reroute,
  162. .route_key_size = sizeof(struct ip_rt_info),
  163. };
  164. static int ipv4_netfilter_init(void)
  165. {
  166. return nf_register_afinfo(&nf_ip_afinfo);
  167. }
  168. static void ipv4_netfilter_fini(void)
  169. {
  170. nf_unregister_afinfo(&nf_ip_afinfo);
  171. }
  172. module_init(ipv4_netfilter_init);
  173. module_exit(ipv4_netfilter_fini);