ip_forward.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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->local_df;
  44. }
  45. static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
  46. {
  47. if (skb->len <= mtu || skb->local_df)
  48. return false;
  49. if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
  50. return false;
  51. return true;
  52. }
  53. static bool ip_gso_exceeds_dst_mtu(const struct sk_buff *skb)
  54. {
  55. unsigned int mtu;
  56. if (skb->local_df || !skb_is_gso(skb))
  57. return false;
  58. mtu = ip_dst_mtu_maybe_forward(skb_dst(skb), true);
  59. /* if seglen > mtu, do software segmentation for IP fragmentation on
  60. * output. DF bit cannot be set since ip_forward would have sent
  61. * icmp error.
  62. */
  63. return skb_gso_network_seglen(skb) > mtu;
  64. }
  65. /* called if GSO skb needs to be fragmented on forward */
  66. static int ip_forward_finish_gso(struct sk_buff *skb)
  67. {
  68. struct dst_entry *dst = skb_dst(skb);
  69. netdev_features_t features;
  70. struct sk_buff *segs;
  71. int ret = 0;
  72. features = netif_skb_dev_features(skb, dst->dev);
  73. segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
  74. if (IS_ERR(segs)) {
  75. kfree_skb(skb);
  76. return -ENOMEM;
  77. }
  78. consume_skb(skb);
  79. do {
  80. struct sk_buff *nskb = segs->next;
  81. int err;
  82. segs->next = NULL;
  83. err = dst_output(segs);
  84. if (err && ret == 0)
  85. ret = err;
  86. segs = nskb;
  87. } while (segs);
  88. return ret;
  89. }
  90. static int ip_forward_finish(struct sk_buff *skb)
  91. {
  92. struct ip_options *opt = &(IPCB(skb)->opt);
  93. IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
  94. IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
  95. if (unlikely(opt->optlen))
  96. ip_forward_options(skb);
  97. if (ip_gso_exceeds_dst_mtu(skb))
  98. return ip_forward_finish_gso(skb);
  99. return dst_output(skb);
  100. }
  101. int ip_forward(struct sk_buff *skb)
  102. {
  103. u32 mtu;
  104. struct iphdr *iph; /* Our header */
  105. struct rtable *rt; /* Route we use */
  106. struct ip_options *opt = &(IPCB(skb)->opt);
  107. if (skb_warn_if_lro(skb))
  108. goto drop;
  109. if (!xfrm4_policy_check(NULL, XFRM_POLICY_FWD, skb))
  110. goto drop;
  111. if (IPCB(skb)->opt.router_alert && ip_call_ra_chain(skb))
  112. return NET_RX_SUCCESS;
  113. if (skb->pkt_type != PACKET_HOST)
  114. goto drop;
  115. skb_forward_csum(skb);
  116. /*
  117. * According to the RFC, we must first decrease the TTL field. If
  118. * that reaches zero, we must reply an ICMP control message telling
  119. * that the packet's lifetime expired.
  120. */
  121. if (ip_hdr(skb)->ttl <= 1)
  122. goto too_many_hops;
  123. if (!xfrm4_route_forward(skb))
  124. goto drop;
  125. rt = skb_rtable(skb);
  126. if (opt->is_strictroute && rt->rt_uses_gateway)
  127. goto sr_failed;
  128. IPCB(skb)->flags |= IPSKB_FORWARDED;
  129. mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
  130. if (!ip_may_fragment(skb) && ip_exceeds_mtu(skb, mtu)) {
  131. IP_INC_STATS(dev_net(rt->dst.dev), IPSTATS_MIB_FRAGFAILS);
  132. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  133. htonl(mtu));
  134. goto drop;
  135. }
  136. /* We are about to mangle packet. Copy it! */
  137. if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+rt->dst.header_len))
  138. goto drop;
  139. iph = ip_hdr(skb);
  140. /* Decrease ttl after skb cow done */
  141. ip_decrease_ttl(iph);
  142. /*
  143. * We now generate an ICMP HOST REDIRECT giving the route
  144. * we calculated.
  145. */
  146. if (rt->rt_flags&RTCF_DOREDIRECT && !opt->srr && !skb_sec_path(skb))
  147. ip_rt_send_redirect(skb);
  148. skb->priority = rt_tos2priority(iph->tos);
  149. return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev,
  150. rt->dst.dev, ip_forward_finish);
  151. sr_failed:
  152. /*
  153. * Strict routing permits no gatewaying
  154. */
  155. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
  156. goto drop;
  157. too_many_hops:
  158. /* Tell the sender its packet died... */
  159. IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_INHDRERRORS);
  160. icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
  161. drop:
  162. kfree_skb(skb);
  163. return NET_RX_DROP;
  164. }