netfilter.c 5.1 KB

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