ip_input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 Internet Protocol (IP) module.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Donald Becker, <becker@super.org>
  11. * Alan Cox, <alan@lxorguk.ukuu.org.uk>
  12. * Richard Underwood
  13. * Stefan Becker, <stefanb@yello.ping.de>
  14. * Jorge Cwik, <jorge@laser.satlink.net>
  15. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  16. *
  17. *
  18. * Fixes:
  19. * Alan Cox : Commented a couple of minor bits of surplus code
  20. * Alan Cox : Undefining IP_FORWARD doesn't include the code
  21. * (just stops a compiler warning).
  22. * Alan Cox : Frames with >=MAX_ROUTE record routes, strict routes or loose routes
  23. * are junked rather than corrupting things.
  24. * Alan Cox : Frames to bad broadcast subnets are dumped
  25. * We used to process them non broadcast and
  26. * boy could that cause havoc.
  27. * Alan Cox : ip_forward sets the free flag on the
  28. * new frame it queues. Still crap because
  29. * it copies the frame but at least it
  30. * doesn't eat memory too.
  31. * Alan Cox : Generic queue code and memory fixes.
  32. * Fred Van Kempen : IP fragment support (borrowed from NET2E)
  33. * Gerhard Koerting: Forward fragmented frames correctly.
  34. * Gerhard Koerting: Fixes to my fix of the above 8-).
  35. * Gerhard Koerting: IP interface addressing fix.
  36. * Linus Torvalds : More robustness checks
  37. * Alan Cox : Even more checks: Still not as robust as it ought to be
  38. * Alan Cox : Save IP header pointer for later
  39. * Alan Cox : ip option setting
  40. * Alan Cox : Use ip_tos/ip_ttl settings
  41. * Alan Cox : Fragmentation bogosity removed
  42. * (Thanks to Mark.Bush@prg.ox.ac.uk)
  43. * Dmitry Gorodchanin : Send of a raw packet crash fix.
  44. * Alan Cox : Silly ip bug when an overlength
  45. * fragment turns up. Now frees the
  46. * queue.
  47. * Linus Torvalds/ : Memory leakage on fragmentation
  48. * Alan Cox : handling.
  49. * Gerhard Koerting: Forwarding uses IP priority hints
  50. * Teemu Rantanen : Fragment problems.
  51. * Alan Cox : General cleanup, comments and reformat
  52. * Alan Cox : SNMP statistics
  53. * Alan Cox : BSD address rule semantics. Also see
  54. * UDP as there is a nasty checksum issue
  55. * if you do things the wrong way.
  56. * Alan Cox : Always defrag, moved IP_FORWARD to the config.in file
  57. * Alan Cox : IP options adjust sk->priority.
  58. * Pedro Roque : Fix mtu/length error in ip_forward.
  59. * Alan Cox : Avoid ip_chk_addr when possible.
  60. * Richard Underwood : IP multicasting.
  61. * Alan Cox : Cleaned up multicast handlers.
  62. * Alan Cox : RAW sockets demultiplex in the BSD style.
  63. * Gunther Mayer : Fix the SNMP reporting typo
  64. * Alan Cox : Always in group 224.0.0.1
  65. * Pauline Middelink : Fast ip_checksum update when forwarding
  66. * Masquerading support.
  67. * Alan Cox : Multicast loopback error for 224.0.0.1
  68. * Alan Cox : IP_MULTICAST_LOOP option.
  69. * Alan Cox : Use notifiers.
  70. * Bjorn Ekwall : Removed ip_csum (from slhc.c too)
  71. * Bjorn Ekwall : Moved ip_fast_csum to ip.h (inline!)
  72. * Stefan Becker : Send out ICMP HOST REDIRECT
  73. * Arnt Gulbrandsen : ip_build_xmit
  74. * Alan Cox : Per socket routing cache
  75. * Alan Cox : Fixed routing cache, added header cache.
  76. * Alan Cox : Loopback didn't work right in original ip_build_xmit - fixed it.
  77. * Alan Cox : Only send ICMP_REDIRECT if src/dest are the same net.
  78. * Alan Cox : Incoming IP option handling.
  79. * Alan Cox : Set saddr on raw output frames as per BSD.
  80. * Alan Cox : Stopped broadcast source route explosions.
  81. * Alan Cox : Can disable source routing
  82. * Takeshi Sone : Masquerading didn't work.
  83. * Dave Bonn,Alan Cox : Faster IP forwarding whenever possible.
  84. * Alan Cox : Memory leaks, tramples, misc debugging.
  85. * Alan Cox : Fixed multicast (by popular demand 8))
  86. * Alan Cox : Fixed forwarding (by even more popular demand 8))
  87. * Alan Cox : Fixed SNMP statistics [I think]
  88. * Gerhard Koerting : IP fragmentation forwarding fix
  89. * Alan Cox : Device lock against page fault.
  90. * Alan Cox : IP_HDRINCL facility.
  91. * Werner Almesberger : Zero fragment bug
  92. * Alan Cox : RAW IP frame length bug
  93. * Alan Cox : Outgoing firewall on build_xmit
  94. * A.N.Kuznetsov : IP_OPTIONS support throughout the kernel
  95. * Alan Cox : Multicast routing hooks
  96. * Jos Vos : Do accounting *before* call_in_firewall
  97. * Willy Konynenberg : Transparent proxying support
  98. *
  99. *
  100. *
  101. * To Fix:
  102. * IP fragmentation wants rewriting cleanly. The RFC815 algorithm is much more efficient
  103. * and could be made very efficient with the addition of some virtual memory hacks to permit
  104. * the allocation of a buffer that can then be 'grown' by twiddling page tables.
  105. * Output fragmentation wants updating along with the buffer management to use a single
  106. * interleaved copy algorithm so that fragmenting has a one copy overhead. Actual packet
  107. * output should probably do its own fragmentation at the UDP/RAW layer. TCP shouldn't cause
  108. * fragmentation anyway.
  109. *
  110. * This program is free software; you can redistribute it and/or
  111. * modify it under the terms of the GNU General Public License
  112. * as published by the Free Software Foundation; either version
  113. * 2 of the License, or (at your option) any later version.
  114. */
  115. #define pr_fmt(fmt) "IPv4: " fmt
  116. #include <linux/module.h>
  117. #include <linux/types.h>
  118. #include <linux/kernel.h>
  119. #include <linux/string.h>
  120. #include <linux/errno.h>
  121. #include <linux/slab.h>
  122. #include <linux/net.h>
  123. #include <linux/socket.h>
  124. #include <linux/sockios.h>
  125. #include <linux/in.h>
  126. #include <linux/inet.h>
  127. #include <linux/inetdevice.h>
  128. #include <linux/netdevice.h>
  129. #include <linux/etherdevice.h>
  130. #include <net/snmp.h>
  131. #include <net/ip.h>
  132. #include <net/protocol.h>
  133. #include <net/route.h>
  134. #include <linux/skbuff.h>
  135. #include <net/sock.h>
  136. #include <net/arp.h>
  137. #include <net/icmp.h>
  138. #include <net/raw.h>
  139. #include <net/checksum.h>
  140. #include <net/inet_ecn.h>
  141. #include <linux/netfilter_ipv4.h>
  142. #include <net/xfrm.h>
  143. #include <linux/mroute.h>
  144. #include <linux/netlink.h>
  145. #include <net/dst_metadata.h>
  146. /*
  147. * Process Router Attention IP option (RFC 2113)
  148. */
  149. bool ip_call_ra_chain(struct sk_buff *skb)
  150. {
  151. struct ip_ra_chain *ra;
  152. u8 protocol = ip_hdr(skb)->protocol;
  153. struct sock *last = NULL;
  154. struct net_device *dev = skb->dev;
  155. struct net *net = dev_net(dev);
  156. for (ra = rcu_dereference(ip_ra_chain); ra; ra = rcu_dereference(ra->next)) {
  157. struct sock *sk = ra->sk;
  158. /* If socket is bound to an interface, only report
  159. * the packet if it came from that interface.
  160. */
  161. if (sk && inet_sk(sk)->inet_num == protocol &&
  162. (!sk->sk_bound_dev_if ||
  163. sk->sk_bound_dev_if == dev->ifindex) &&
  164. net_eq(sock_net(sk), net)) {
  165. if (ip_is_fragment(ip_hdr(skb))) {
  166. if (ip_defrag(net, skb, IP_DEFRAG_CALL_RA_CHAIN))
  167. return true;
  168. }
  169. if (last) {
  170. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  171. if (skb2)
  172. raw_rcv(last, skb2);
  173. }
  174. last = sk;
  175. }
  176. }
  177. if (last) {
  178. raw_rcv(last, skb);
  179. return true;
  180. }
  181. return false;
  182. }
  183. static int ip_local_deliver_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  184. {
  185. __skb_pull(skb, skb_network_header_len(skb));
  186. rcu_read_lock();
  187. {
  188. int protocol = ip_hdr(skb)->protocol;
  189. const struct net_protocol *ipprot;
  190. int raw;
  191. resubmit:
  192. raw = raw_local_deliver(skb, protocol);
  193. ipprot = rcu_dereference(inet_protos[protocol]);
  194. if (ipprot) {
  195. int ret;
  196. if (!ipprot->no_policy) {
  197. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  198. kfree_skb(skb);
  199. goto out;
  200. }
  201. nf_reset(skb);
  202. }
  203. ret = ipprot->handler(skb);
  204. if (ret < 0) {
  205. protocol = -ret;
  206. goto resubmit;
  207. }
  208. IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
  209. } else {
  210. if (!raw) {
  211. if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  212. IP_INC_STATS_BH(net, IPSTATS_MIB_INUNKNOWNPROTOS);
  213. icmp_send(skb, ICMP_DEST_UNREACH,
  214. ICMP_PROT_UNREACH, 0);
  215. }
  216. kfree_skb(skb);
  217. } else {
  218. IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);
  219. consume_skb(skb);
  220. }
  221. }
  222. }
  223. out:
  224. rcu_read_unlock();
  225. return 0;
  226. }
  227. /*
  228. * Deliver IP Packets to the higher protocol layers.
  229. */
  230. int ip_local_deliver(struct sk_buff *skb)
  231. {
  232. /*
  233. * Reassemble IP fragments.
  234. */
  235. struct net *net = dev_net(skb->dev);
  236. if (ip_is_fragment(ip_hdr(skb))) {
  237. if (ip_defrag(net, skb, IP_DEFRAG_LOCAL_DELIVER))
  238. return 0;
  239. }
  240. return NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_IN,
  241. net, NULL, skb, skb->dev, NULL,
  242. ip_local_deliver_finish);
  243. }
  244. static inline bool ip_rcv_options(struct sk_buff *skb)
  245. {
  246. struct ip_options *opt;
  247. const struct iphdr *iph;
  248. struct net_device *dev = skb->dev;
  249. /* It looks as overkill, because not all
  250. IP options require packet mangling.
  251. But it is the easiest for now, especially taking
  252. into account that combination of IP options
  253. and running sniffer is extremely rare condition.
  254. --ANK (980813)
  255. */
  256. if (skb_cow(skb, skb_headroom(skb))) {
  257. IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
  258. goto drop;
  259. }
  260. iph = ip_hdr(skb);
  261. opt = &(IPCB(skb)->opt);
  262. opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
  263. if (ip_options_compile(dev_net(dev), opt, skb)) {
  264. IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
  265. goto drop;
  266. }
  267. if (unlikely(opt->srr)) {
  268. struct in_device *in_dev = __in_dev_get_rcu(dev);
  269. if (in_dev) {
  270. if (!IN_DEV_SOURCE_ROUTE(in_dev)) {
  271. if (IN_DEV_LOG_MARTIANS(in_dev))
  272. net_info_ratelimited("source route option %pI4 -> %pI4\n",
  273. &iph->saddr,
  274. &iph->daddr);
  275. goto drop;
  276. }
  277. }
  278. if (ip_options_rcv_srr(skb))
  279. goto drop;
  280. }
  281. return false;
  282. drop:
  283. return true;
  284. }
  285. int sysctl_ip_early_demux __read_mostly = 1;
  286. EXPORT_SYMBOL(sysctl_ip_early_demux);
  287. static int ip_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  288. {
  289. const struct iphdr *iph = ip_hdr(skb);
  290. struct rtable *rt;
  291. if (sysctl_ip_early_demux && !skb_dst(skb) && !skb->sk) {
  292. const struct net_protocol *ipprot;
  293. int protocol = iph->protocol;
  294. ipprot = rcu_dereference(inet_protos[protocol]);
  295. if (ipprot && ipprot->early_demux) {
  296. ipprot->early_demux(skb);
  297. /* must reload iph, skb->head might have changed */
  298. iph = ip_hdr(skb);
  299. }
  300. }
  301. /*
  302. * Initialise the virtual path cache for the packet. It describes
  303. * how the packet travels inside Linux networking.
  304. */
  305. if (!skb_valid_dst(skb)) {
  306. int err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
  307. iph->tos, skb->dev);
  308. if (unlikely(err)) {
  309. if (err == -EXDEV)
  310. NET_INC_STATS_BH(net, LINUX_MIB_IPRPFILTER);
  311. goto drop;
  312. }
  313. }
  314. #ifdef CONFIG_IP_ROUTE_CLASSID
  315. if (unlikely(skb_dst(skb)->tclassid)) {
  316. struct ip_rt_acct *st = this_cpu_ptr(ip_rt_acct);
  317. u32 idx = skb_dst(skb)->tclassid;
  318. st[idx&0xFF].o_packets++;
  319. st[idx&0xFF].o_bytes += skb->len;
  320. st[(idx>>16)&0xFF].i_packets++;
  321. st[(idx>>16)&0xFF].i_bytes += skb->len;
  322. }
  323. #endif
  324. if (iph->ihl > 5 && ip_rcv_options(skb))
  325. goto drop;
  326. rt = skb_rtable(skb);
  327. if (rt->rt_type == RTN_MULTICAST) {
  328. IP_UPD_PO_STATS_BH(net, IPSTATS_MIB_INMCAST, skb->len);
  329. } else if (rt->rt_type == RTN_BROADCAST)
  330. IP_UPD_PO_STATS_BH(net, IPSTATS_MIB_INBCAST, skb->len);
  331. return dst_input(skb);
  332. drop:
  333. kfree_skb(skb);
  334. return NET_RX_DROP;
  335. }
  336. /*
  337. * Main IP Receive routine.
  338. */
  339. int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  340. {
  341. const struct iphdr *iph;
  342. struct net *net;
  343. u32 len;
  344. /* When the interface is in promisc. mode, drop all the crap
  345. * that it receives, do not try to analyse it.
  346. */
  347. if (skb->pkt_type == PACKET_OTHERHOST)
  348. goto drop;
  349. net = dev_net(dev);
  350. IP_UPD_PO_STATS_BH(net, IPSTATS_MIB_IN, skb->len);
  351. skb = skb_share_check(skb, GFP_ATOMIC);
  352. if (!skb) {
  353. IP_INC_STATS_BH(net, IPSTATS_MIB_INDISCARDS);
  354. goto out;
  355. }
  356. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  357. goto inhdr_error;
  358. iph = ip_hdr(skb);
  359. /*
  360. * RFC1122: 3.2.1.2 MUST silently discard any IP frame that fails the checksum.
  361. *
  362. * Is the datagram acceptable?
  363. *
  364. * 1. Length at least the size of an ip header
  365. * 2. Version of 4
  366. * 3. Checksums correctly. [Speed optimisation for later, skip loopback checksums]
  367. * 4. Doesn't have a bogus length
  368. */
  369. if (iph->ihl < 5 || iph->version != 4)
  370. goto inhdr_error;
  371. BUILD_BUG_ON(IPSTATS_MIB_ECT1PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_1);
  372. BUILD_BUG_ON(IPSTATS_MIB_ECT0PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_0);
  373. BUILD_BUG_ON(IPSTATS_MIB_CEPKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_CE);
  374. IP_ADD_STATS_BH(net,
  375. IPSTATS_MIB_NOECTPKTS + (iph->tos & INET_ECN_MASK),
  376. max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
  377. if (!pskb_may_pull(skb, iph->ihl*4))
  378. goto inhdr_error;
  379. iph = ip_hdr(skb);
  380. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  381. goto csum_error;
  382. len = ntohs(iph->tot_len);
  383. if (skb->len < len) {
  384. IP_INC_STATS_BH(net, IPSTATS_MIB_INTRUNCATEDPKTS);
  385. goto drop;
  386. } else if (len < (iph->ihl*4))
  387. goto inhdr_error;
  388. /* Our transport medium may have padded the buffer out. Now we know it
  389. * is IP we can trim to the true length of the frame.
  390. * Note this now means skb->len holds ntohs(iph->tot_len).
  391. */
  392. if (pskb_trim_rcsum(skb, len)) {
  393. IP_INC_STATS_BH(net, IPSTATS_MIB_INDISCARDS);
  394. goto drop;
  395. }
  396. iph = ip_hdr(skb);
  397. skb->transport_header = skb->network_header + iph->ihl*4;
  398. /* Remove any debris in the socket control block */
  399. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  400. /* Must drop socket now because of tproxy. */
  401. skb_orphan(skb);
  402. return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
  403. net, NULL, skb, dev, NULL,
  404. ip_rcv_finish);
  405. csum_error:
  406. IP_INC_STATS_BH(net, IPSTATS_MIB_CSUMERRORS);
  407. inhdr_error:
  408. IP_INC_STATS_BH(net, IPSTATS_MIB_INHDRERRORS);
  409. drop:
  410. kfree_skb(skb);
  411. out:
  412. return NET_RX_DROP;
  413. }