netfilter.c 5.1 KB

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