ip_forward.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The IP forwarding functionality.
  7. *
  8. * Authors: see ip.c
  9. *
  10. * Fixes:
  11. * Many : Split from ip.c , see ip_input.c for
  12. * history.
  13. * Dave Gregorich : NULL ip_rt_put fix for multicast
  14. * routing.
  15. * Jos Vos : Add call_out_firewall before sending,
  16. * use output device for accounting.
  17. * Jos Vos : Call forward firewall after routing
  18. * (always use output device).
  19. * Mike McLagan : Routing by source
  20. */
  21. #include <linux/types.h>
  22. #include <linux/mm.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/ip.h>
  25. #include <linux/icmp.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/slab.h>
  28. #include <net/sock.h>
  29. #include <net/ip.h>
  30. #include <net/tcp.h>
  31. #include <net/udp.h>
  32. #include <net/icmp.h>
  33. #include <linux/tcp.h>
  34. #include <linux/udp.h>
  35. #include <linux/netfilter_ipv4.h>
  36. #include <net/checksum.h>
  37. #include <linux/route.h>
  38. #include <net/route.h>
  39. #include <net/xfrm.h>
  40. static bool ip_may_fragment(const struct sk_buff *skb)
  41. {
  42. return unlikely((ip_hdr(skb)->frag_off & htons(IP_DF)) == 0) ||
  43. skb->ignore_df;
  44. }
  45. static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
  46. {
  47. if (skb->len <= mtu)
  48. return false;
  49. if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
  50. return false;
  51. return true;
  52. }
  53. static int ip_forward_finish(struct sk_buff *skb)
  54. {
  55. struct ip_options *opt = &(IPCB(skb)->opt);
  56. IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
  57. IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
  58. if (unlikely(opt->optlen))
  59. ip_forward_options(skb);
  60. return dst_output(skb);
  61. }
  62. int ip_forward(struct sk_buff *skb)
  63. {
  64. u32 mtu;
  65. struct iphdr *iph; /* Our header */
  66. struct rtable *rt; /* Route we use */
  67. struct ip_options *opt = &(IPCB(skb)->opt);
  68. /* that should never happen */
  69. if (skb->pkt_type != PACKET_HOST)
  70. goto drop;
  71. if (skb_warn_if_lro(skb))
  72. goto drop;
  73. if (!xfrm4_policy_check(NULL, XFRM_POLICY_FWD, skb))
  74. goto drop;
  75. if (IPCB(skb)->opt.router_alert && ip_call_ra_chain(skb))
  76. return NET_RX_SUCCESS;
  77. skb_forward_csum(skb);
  78. /*
  79. * According to the RFC, we must first decrease the TTL field. If
  80. * that reaches zero, we must reply an ICMP control message telling
  81. * that the packet's lifetime expired.
  82. */
  83. if (ip_hdr(skb)->ttl <= 1)
  84. goto too_many_hops;
  85. if (!xfrm4_route_forward(skb))
  86. goto drop;
  87. rt = skb_rtable(skb);
  88. if (opt->is_strictroute && rt->rt_uses_gateway)
  89. goto sr_failed;
  90. IPCB(skb)->flags |= IPSKB_FORWARDED;
  91. mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
  92. if (!ip_may_fragment(skb) && ip_exceeds_mtu(skb, mtu)) {
  93. IP_INC_STATS(dev_net(rt->dst.dev), IPSTATS_MIB_FRAGFAILS);
  94. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  95. htonl(mtu));
  96. goto drop;
  97. }
  98. /* We are about to mangle packet. Copy it! */
  99. if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
  100. goto drop;
  101. iph = ip_hdr(skb);
  102. /* Decrease ttl after skb cow done */
  103. ip_decrease_ttl(iph);
  104. /*
  105. * We now generate an ICMP HOST REDIRECT giving the route
  106. * we calculated.
  107. */
  108. if (rt->rt_flags&RTCF_DOREDIRECT && !opt->srr && !skb_sec_path(skb))
  109. ip_rt_send_redirect(skb);
  110. skb->priority = rt_tos2priority(iph->tos);
  111. return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev,
  112. rt->dst.dev, ip_forward_finish);
  113. sr_failed:
  114. /*
  115. * Strict routing permits no gatewaying
  116. */
  117. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
  118. goto drop;
  119. too_many_hops:
  120. /* Tell the sender its packet died... */
  121. IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_INHDRERRORS);
  122. icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
  123. drop:
  124. kfree_skb(skb);
  125. return NET_RX_DROP;
  126. }