ip_input.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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(net, IPSTATS_MIB_INDELIVERS);
  209. } else {
  210. if (!raw) {
  211. if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  212. __IP_INC_STATS(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(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(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(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. static int ip_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  286. {
  287. const struct iphdr *iph = ip_hdr(skb);
  288. struct rtable *rt;
  289. struct net_device *dev = skb->dev;
  290. /* if ingress device is enslaved to an L3 master device pass the
  291. * skb to its handler for processing
  292. */
  293. skb = l3mdev_ip_rcv(skb);
  294. if (!skb)
  295. return NET_RX_SUCCESS;
  296. if (net->ipv4.sysctl_ip_early_demux &&
  297. !skb_dst(skb) &&
  298. !skb->sk &&
  299. !ip_is_fragment(iph)) {
  300. const struct net_protocol *ipprot;
  301. int protocol = iph->protocol;
  302. ipprot = rcu_dereference(inet_protos[protocol]);
  303. if (ipprot && ipprot->early_demux) {
  304. ipprot->early_demux(skb);
  305. /* must reload iph, skb->head might have changed */
  306. iph = ip_hdr(skb);
  307. }
  308. }
  309. /*
  310. * Initialise the virtual path cache for the packet. It describes
  311. * how the packet travels inside Linux networking.
  312. */
  313. if (!skb_valid_dst(skb)) {
  314. int err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
  315. iph->tos, dev);
  316. if (unlikely(err)) {
  317. if (err == -EXDEV)
  318. __NET_INC_STATS(net, LINUX_MIB_IPRPFILTER);
  319. goto drop;
  320. }
  321. }
  322. #ifdef CONFIG_IP_ROUTE_CLASSID
  323. if (unlikely(skb_dst(skb)->tclassid)) {
  324. struct ip_rt_acct *st = this_cpu_ptr(ip_rt_acct);
  325. u32 idx = skb_dst(skb)->tclassid;
  326. st[idx&0xFF].o_packets++;
  327. st[idx&0xFF].o_bytes += skb->len;
  328. st[(idx>>16)&0xFF].i_packets++;
  329. st[(idx>>16)&0xFF].i_bytes += skb->len;
  330. }
  331. #endif
  332. if (iph->ihl > 5 && ip_rcv_options(skb))
  333. goto drop;
  334. rt = skb_rtable(skb);
  335. if (rt->rt_type == RTN_MULTICAST) {
  336. __IP_UPD_PO_STATS(net, IPSTATS_MIB_INMCAST, skb->len);
  337. } else if (rt->rt_type == RTN_BROADCAST) {
  338. __IP_UPD_PO_STATS(net, IPSTATS_MIB_INBCAST, skb->len);
  339. } else if (skb->pkt_type == PACKET_BROADCAST ||
  340. skb->pkt_type == PACKET_MULTICAST) {
  341. struct in_device *in_dev = __in_dev_get_rcu(dev);
  342. /* RFC 1122 3.3.6:
  343. *
  344. * When a host sends a datagram to a link-layer broadcast
  345. * address, the IP destination address MUST be a legal IP
  346. * broadcast or IP multicast address.
  347. *
  348. * A host SHOULD silently discard a datagram that is received
  349. * via a link-layer broadcast (see Section 2.4) but does not
  350. * specify an IP multicast or broadcast destination address.
  351. *
  352. * This doesn't explicitly say L2 *broadcast*, but broadcast is
  353. * in a way a form of multicast and the most common use case for
  354. * this is 802.11 protecting against cross-station spoofing (the
  355. * so-called "hole-196" attack) so do it for both.
  356. */
  357. if (in_dev &&
  358. IN_DEV_ORCONF(in_dev, DROP_UNICAST_IN_L2_MULTICAST))
  359. goto drop;
  360. }
  361. return dst_input(skb);
  362. drop:
  363. kfree_skb(skb);
  364. return NET_RX_DROP;
  365. }
  366. /*
  367. * Main IP Receive routine.
  368. */
  369. int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  370. {
  371. const struct iphdr *iph;
  372. struct net *net;
  373. u32 len;
  374. /* When the interface is in promisc. mode, drop all the crap
  375. * that it receives, do not try to analyse it.
  376. */
  377. if (skb->pkt_type == PACKET_OTHERHOST)
  378. goto drop;
  379. net = dev_net(dev);
  380. __IP_UPD_PO_STATS(net, IPSTATS_MIB_IN, skb->len);
  381. skb = skb_share_check(skb, GFP_ATOMIC);
  382. if (!skb) {
  383. __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
  384. goto out;
  385. }
  386. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  387. goto inhdr_error;
  388. iph = ip_hdr(skb);
  389. /*
  390. * RFC1122: 3.2.1.2 MUST silently discard any IP frame that fails the checksum.
  391. *
  392. * Is the datagram acceptable?
  393. *
  394. * 1. Length at least the size of an ip header
  395. * 2. Version of 4
  396. * 3. Checksums correctly. [Speed optimisation for later, skip loopback checksums]
  397. * 4. Doesn't have a bogus length
  398. */
  399. if (iph->ihl < 5 || iph->version != 4)
  400. goto inhdr_error;
  401. BUILD_BUG_ON(IPSTATS_MIB_ECT1PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_1);
  402. BUILD_BUG_ON(IPSTATS_MIB_ECT0PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_0);
  403. BUILD_BUG_ON(IPSTATS_MIB_CEPKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_CE);
  404. __IP_ADD_STATS(net,
  405. IPSTATS_MIB_NOECTPKTS + (iph->tos & INET_ECN_MASK),
  406. max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
  407. if (!pskb_may_pull(skb, iph->ihl*4))
  408. goto inhdr_error;
  409. iph = ip_hdr(skb);
  410. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  411. goto csum_error;
  412. len = ntohs(iph->tot_len);
  413. if (skb->len < len) {
  414. __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
  415. goto drop;
  416. } else if (len < (iph->ihl*4))
  417. goto inhdr_error;
  418. /* Our transport medium may have padded the buffer out. Now we know it
  419. * is IP we can trim to the true length of the frame.
  420. * Note this now means skb->len holds ntohs(iph->tot_len).
  421. */
  422. if (pskb_trim_rcsum(skb, len)) {
  423. __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
  424. goto drop;
  425. }
  426. skb->transport_header = skb->network_header + iph->ihl*4;
  427. /* Remove any debris in the socket control block */
  428. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  429. IPCB(skb)->iif = skb->skb_iif;
  430. /* Must drop socket now because of tproxy. */
  431. skb_orphan(skb);
  432. return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
  433. net, NULL, skb, dev, NULL,
  434. ip_rcv_finish);
  435. csum_error:
  436. __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
  437. inhdr_error:
  438. __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
  439. drop:
  440. kfree_skb(skb);
  441. out:
  442. return NET_RX_DROP;
  443. }