ip_input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * The Internet Protocol (IP) module.
  8. *
  9. * Authors: Ross Biro
  10. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  11. * Donald Becker, <becker@super.org>
  12. * Alan Cox, <alan@lxorguk.ukuu.org.uk>
  13. * Richard Underwood
  14. * Stefan Becker, <stefanb@yello.ping.de>
  15. * Jorge Cwik, <jorge@laser.satlink.net>
  16. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  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. * To Fix:
  100. * IP fragmentation wants rewriting cleanly. The RFC815 algorithm is much more efficient
  101. * and could be made very efficient with the addition of some virtual memory hacks to permit
  102. * the allocation of a buffer that can then be 'grown' by twiddling page tables.
  103. * Output fragmentation wants updating along with the buffer management to use a single
  104. * interleaved copy algorithm so that fragmenting has a one copy overhead. Actual packet
  105. * output should probably do its own fragmentation at the UDP/RAW layer. TCP shouldn't cause
  106. * fragmentation anyway.
  107. */
  108. #define pr_fmt(fmt) "IPv4: " fmt
  109. #include <linux/module.h>
  110. #include <linux/types.h>
  111. #include <linux/kernel.h>
  112. #include <linux/string.h>
  113. #include <linux/errno.h>
  114. #include <linux/slab.h>
  115. #include <linux/net.h>
  116. #include <linux/socket.h>
  117. #include <linux/sockios.h>
  118. #include <linux/in.h>
  119. #include <linux/inet.h>
  120. #include <linux/inetdevice.h>
  121. #include <linux/netdevice.h>
  122. #include <linux/etherdevice.h>
  123. #include <linux/indirect_call_wrapper.h>
  124. #include <net/snmp.h>
  125. #include <net/ip.h>
  126. #include <net/protocol.h>
  127. #include <net/route.h>
  128. #include <linux/skbuff.h>
  129. #include <net/sock.h>
  130. #include <net/arp.h>
  131. #include <net/icmp.h>
  132. #include <net/raw.h>
  133. #include <net/checksum.h>
  134. #include <net/inet_ecn.h>
  135. #include <linux/netfilter_ipv4.h>
  136. #include <net/xfrm.h>
  137. #include <linux/mroute.h>
  138. #include <linux/netlink.h>
  139. #include <net/dst_metadata.h>
  140. /*
  141. * Process Router Attention IP option (RFC 2113)
  142. */
  143. bool ip_call_ra_chain(struct sk_buff *skb)
  144. {
  145. struct ip_ra_chain *ra;
  146. u8 protocol = ip_hdr(skb)->protocol;
  147. struct sock *last = NULL;
  148. struct net_device *dev = skb->dev;
  149. struct net *net = dev_net(dev);
  150. for (ra = rcu_dereference(net->ipv4.ra_chain); ra; ra = rcu_dereference(ra->next)) {
  151. struct sock *sk = ra->sk;
  152. /* If socket is bound to an interface, only report
  153. * the packet if it came from that interface.
  154. */
  155. if (sk && inet_sk(sk)->inet_num == protocol &&
  156. (!sk->sk_bound_dev_if ||
  157. sk->sk_bound_dev_if == dev->ifindex)) {
  158. if (ip_is_fragment(ip_hdr(skb))) {
  159. if (ip_defrag(net, skb, IP_DEFRAG_CALL_RA_CHAIN))
  160. return true;
  161. }
  162. if (last) {
  163. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  164. if (skb2)
  165. raw_rcv(last, skb2);
  166. }
  167. last = sk;
  168. }
  169. }
  170. if (last) {
  171. raw_rcv(last, skb);
  172. return true;
  173. }
  174. return false;
  175. }
  176. INDIRECT_CALLABLE_DECLARE(int udp_rcv(struct sk_buff *));
  177. INDIRECT_CALLABLE_DECLARE(int tcp_v4_rcv(struct sk_buff *));
  178. void ip_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int protocol)
  179. {
  180. const struct net_protocol *ipprot;
  181. int raw, ret;
  182. resubmit:
  183. raw = raw_local_deliver(skb, protocol);
  184. ipprot = rcu_dereference(inet_protos[protocol]);
  185. if (ipprot) {
  186. if (!ipprot->no_policy) {
  187. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  188. kfree_skb(skb);
  189. return;
  190. }
  191. nf_reset_ct(skb);
  192. }
  193. ret = INDIRECT_CALL_2(ipprot->handler, tcp_v4_rcv, udp_rcv,
  194. skb);
  195. if (ret < 0) {
  196. protocol = -ret;
  197. goto resubmit;
  198. }
  199. __IP_INC_STATS(net, IPSTATS_MIB_INDELIVERS);
  200. } else {
  201. if (!raw) {
  202. if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  203. __IP_INC_STATS(net, IPSTATS_MIB_INUNKNOWNPROTOS);
  204. icmp_send(skb, ICMP_DEST_UNREACH,
  205. ICMP_PROT_UNREACH, 0);
  206. }
  207. kfree_skb(skb);
  208. } else {
  209. __IP_INC_STATS(net, IPSTATS_MIB_INDELIVERS);
  210. consume_skb(skb);
  211. }
  212. }
  213. }
  214. static int ip_local_deliver_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  215. {
  216. __skb_pull(skb, skb_network_header_len(skb));
  217. rcu_read_lock();
  218. ip_protocol_deliver_rcu(net, skb, ip_hdr(skb)->protocol);
  219. rcu_read_unlock();
  220. return 0;
  221. }
  222. /*
  223. * Deliver IP Packets to the higher protocol layers.
  224. */
  225. int ip_local_deliver(struct sk_buff *skb)
  226. {
  227. /*
  228. * Reassemble IP fragments.
  229. */
  230. struct net *net = dev_net(skb->dev);
  231. if (ip_is_fragment(ip_hdr(skb))) {
  232. if (ip_defrag(net, skb, IP_DEFRAG_LOCAL_DELIVER))
  233. return 0;
  234. }
  235. return NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_IN,
  236. net, NULL, skb, skb->dev, NULL,
  237. ip_local_deliver_finish);
  238. }
  239. static inline bool ip_rcv_options(struct sk_buff *skb, struct net_device *dev)
  240. {
  241. struct ip_options *opt;
  242. const struct iphdr *iph;
  243. /* It looks as overkill, because not all
  244. IP options require packet mangling.
  245. But it is the easiest for now, especially taking
  246. into account that combination of IP options
  247. and running sniffer is extremely rare condition.
  248. --ANK (980813)
  249. */
  250. if (skb_cow(skb, skb_headroom(skb))) {
  251. __IP_INC_STATS(dev_net(dev), IPSTATS_MIB_INDISCARDS);
  252. goto drop;
  253. }
  254. iph = ip_hdr(skb);
  255. opt = &(IPCB(skb)->opt);
  256. opt->optlen = iph->ihl*4 - sizeof(struct iphdr);
  257. if (ip_options_compile(dev_net(dev), opt, skb)) {
  258. __IP_INC_STATS(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
  259. goto drop;
  260. }
  261. if (unlikely(opt->srr)) {
  262. struct in_device *in_dev = __in_dev_get_rcu(dev);
  263. if (in_dev) {
  264. if (!IN_DEV_SOURCE_ROUTE(in_dev)) {
  265. if (IN_DEV_LOG_MARTIANS(in_dev))
  266. net_info_ratelimited("source route option %pI4 -> %pI4\n",
  267. &iph->saddr,
  268. &iph->daddr);
  269. goto drop;
  270. }
  271. }
  272. if (ip_options_rcv_srr(skb, dev))
  273. goto drop;
  274. }
  275. return false;
  276. drop:
  277. return true;
  278. }
  279. static bool ip_can_use_hint(const struct sk_buff *skb, const struct iphdr *iph,
  280. const struct sk_buff *hint)
  281. {
  282. return hint && !skb_dst(skb) && ip_hdr(hint)->daddr == iph->daddr &&
  283. ip_hdr(hint)->tos == iph->tos;
  284. }
  285. INDIRECT_CALLABLE_DECLARE(int udp_v4_early_demux(struct sk_buff *));
  286. INDIRECT_CALLABLE_DECLARE(int tcp_v4_early_demux(struct sk_buff *));
  287. static int ip_rcv_finish_core(struct net *net, struct sock *sk,
  288. struct sk_buff *skb, struct net_device *dev,
  289. const struct sk_buff *hint)
  290. {
  291. const struct iphdr *iph = ip_hdr(skb);
  292. int (*edemux)(struct sk_buff *skb);
  293. struct rtable *rt;
  294. int err;
  295. if (ip_can_use_hint(skb, iph, hint)) {
  296. err = ip_route_use_hint(skb, iph->daddr, iph->saddr, iph->tos,
  297. dev, hint);
  298. if (unlikely(err))
  299. goto drop_error;
  300. }
  301. if (net->ipv4.sysctl_ip_early_demux &&
  302. !skb_dst(skb) &&
  303. !skb->sk &&
  304. !ip_is_fragment(iph)) {
  305. const struct net_protocol *ipprot;
  306. int protocol = iph->protocol;
  307. ipprot = rcu_dereference(inet_protos[protocol]);
  308. if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) {
  309. err = INDIRECT_CALL_2(edemux, tcp_v4_early_demux,
  310. udp_v4_early_demux, skb);
  311. if (unlikely(err))
  312. goto drop_error;
  313. /* must reload iph, skb->head might have changed */
  314. iph = ip_hdr(skb);
  315. }
  316. }
  317. /*
  318. * Initialise the virtual path cache for the packet. It describes
  319. * how the packet travels inside Linux networking.
  320. */
  321. if (!skb_valid_dst(skb)) {
  322. err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
  323. iph->tos, dev);
  324. if (unlikely(err))
  325. goto drop_error;
  326. }
  327. #ifdef CONFIG_IP_ROUTE_CLASSID
  328. if (unlikely(skb_dst(skb)->tclassid)) {
  329. struct ip_rt_acct *st = this_cpu_ptr(ip_rt_acct);
  330. u32 idx = skb_dst(skb)->tclassid;
  331. st[idx&0xFF].o_packets++;
  332. st[idx&0xFF].o_bytes += skb->len;
  333. st[(idx>>16)&0xFF].i_packets++;
  334. st[(idx>>16)&0xFF].i_bytes += skb->len;
  335. }
  336. #endif
  337. if (iph->ihl > 5 && ip_rcv_options(skb, dev))
  338. goto drop;
  339. rt = skb_rtable(skb);
  340. if (rt->rt_type == RTN_MULTICAST) {
  341. __IP_UPD_PO_STATS(net, IPSTATS_MIB_INMCAST, skb->len);
  342. } else if (rt->rt_type == RTN_BROADCAST) {
  343. __IP_UPD_PO_STATS(net, IPSTATS_MIB_INBCAST, skb->len);
  344. } else if (skb->pkt_type == PACKET_BROADCAST ||
  345. skb->pkt_type == PACKET_MULTICAST) {
  346. struct in_device *in_dev = __in_dev_get_rcu(dev);
  347. /* RFC 1122 3.3.6:
  348. *
  349. * When a host sends a datagram to a link-layer broadcast
  350. * address, the IP destination address MUST be a legal IP
  351. * broadcast or IP multicast address.
  352. *
  353. * A host SHOULD silently discard a datagram that is received
  354. * via a link-layer broadcast (see Section 2.4) but does not
  355. * specify an IP multicast or broadcast destination address.
  356. *
  357. * This doesn't explicitly say L2 *broadcast*, but broadcast is
  358. * in a way a form of multicast and the most common use case for
  359. * this is 802.11 protecting against cross-station spoofing (the
  360. * so-called "hole-196" attack) so do it for both.
  361. */
  362. if (in_dev &&
  363. IN_DEV_ORCONF(in_dev, DROP_UNICAST_IN_L2_MULTICAST))
  364. goto drop;
  365. }
  366. return NET_RX_SUCCESS;
  367. drop:
  368. kfree_skb(skb);
  369. return NET_RX_DROP;
  370. drop_error:
  371. if (err == -EXDEV)
  372. __NET_INC_STATS(net, LINUX_MIB_IPRPFILTER);
  373. goto drop;
  374. }
  375. static int ip_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  376. {
  377. struct net_device *dev = skb->dev;
  378. int ret;
  379. /* if ingress device is enslaved to an L3 master device pass the
  380. * skb to its handler for processing
  381. */
  382. skb = l3mdev_ip_rcv(skb);
  383. if (!skb)
  384. return NET_RX_SUCCESS;
  385. ret = ip_rcv_finish_core(net, sk, skb, dev, NULL);
  386. if (ret != NET_RX_DROP)
  387. ret = dst_input(skb);
  388. return ret;
  389. }
  390. /*
  391. * Main IP Receive routine.
  392. */
  393. static struct sk_buff *ip_rcv_core(struct sk_buff *skb, struct net *net)
  394. {
  395. const struct iphdr *iph;
  396. u32 len;
  397. /* When the interface is in promisc. mode, drop all the crap
  398. * that it receives, do not try to analyse it.
  399. */
  400. if (skb->pkt_type == PACKET_OTHERHOST)
  401. goto drop;
  402. __IP_UPD_PO_STATS(net, IPSTATS_MIB_IN, skb->len);
  403. skb = skb_share_check(skb, GFP_ATOMIC);
  404. if (!skb) {
  405. __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
  406. goto out;
  407. }
  408. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  409. goto inhdr_error;
  410. iph = ip_hdr(skb);
  411. /*
  412. * RFC1122: 3.2.1.2 MUST silently discard any IP frame that fails the checksum.
  413. *
  414. * Is the datagram acceptable?
  415. *
  416. * 1. Length at least the size of an ip header
  417. * 2. Version of 4
  418. * 3. Checksums correctly. [Speed optimisation for later, skip loopback checksums]
  419. * 4. Doesn't have a bogus length
  420. */
  421. if (iph->ihl < 5 || iph->version != 4)
  422. goto inhdr_error;
  423. BUILD_BUG_ON(IPSTATS_MIB_ECT1PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_1);
  424. BUILD_BUG_ON(IPSTATS_MIB_ECT0PKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_ECT_0);
  425. BUILD_BUG_ON(IPSTATS_MIB_CEPKTS != IPSTATS_MIB_NOECTPKTS + INET_ECN_CE);
  426. __IP_ADD_STATS(net,
  427. IPSTATS_MIB_NOECTPKTS + (iph->tos & INET_ECN_MASK),
  428. max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
  429. if (!pskb_may_pull(skb, iph->ihl*4))
  430. goto inhdr_error;
  431. iph = ip_hdr(skb);
  432. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  433. goto csum_error;
  434. len = ntohs(iph->tot_len);
  435. if (skb->len < len) {
  436. __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
  437. goto drop;
  438. } else if (len < (iph->ihl*4))
  439. goto inhdr_error;
  440. /* Our transport medium may have padded the buffer out. Now we know it
  441. * is IP we can trim to the true length of the frame.
  442. * Note this now means skb->len holds ntohs(iph->tot_len).
  443. */
  444. if (pskb_trim_rcsum(skb, len)) {
  445. __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
  446. goto drop;
  447. }
  448. iph = ip_hdr(skb);
  449. skb->transport_header = skb->network_header + iph->ihl*4;
  450. /* Remove any debris in the socket control block */
  451. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  452. IPCB(skb)->iif = skb->skb_iif;
  453. /* Must drop socket now because of tproxy. */
  454. if (!skb_sk_is_prefetched(skb))
  455. skb_orphan(skb);
  456. return skb;
  457. csum_error:
  458. __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
  459. inhdr_error:
  460. __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
  461. drop:
  462. kfree_skb(skb);
  463. out:
  464. return NULL;
  465. }
  466. /*
  467. * IP receive entry point
  468. */
  469. int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
  470. struct net_device *orig_dev)
  471. {
  472. struct net *net = dev_net(dev);
  473. skb = ip_rcv_core(skb, net);
  474. if (skb == NULL)
  475. return NET_RX_DROP;
  476. return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
  477. net, NULL, skb, dev, NULL,
  478. ip_rcv_finish);
  479. }
  480. static void ip_sublist_rcv_finish(struct list_head *head)
  481. {
  482. struct sk_buff *skb, *next;
  483. list_for_each_entry_safe(skb, next, head, list) {
  484. skb_list_del_init(skb);
  485. dst_input(skb);
  486. }
  487. }
  488. static struct sk_buff *ip_extract_route_hint(const struct net *net,
  489. struct sk_buff *skb, int rt_type)
  490. {
  491. if (fib4_has_custom_rules(net) || rt_type == RTN_BROADCAST)
  492. return NULL;
  493. return skb;
  494. }
  495. static void ip_list_rcv_finish(struct net *net, struct sock *sk,
  496. struct list_head *head)
  497. {
  498. struct sk_buff *skb, *next, *hint = NULL;
  499. struct dst_entry *curr_dst = NULL;
  500. struct list_head sublist;
  501. INIT_LIST_HEAD(&sublist);
  502. list_for_each_entry_safe(skb, next, head, list) {
  503. struct net_device *dev = skb->dev;
  504. struct dst_entry *dst;
  505. skb_list_del_init(skb);
  506. /* if ingress device is enslaved to an L3 master device pass the
  507. * skb to its handler for processing
  508. */
  509. skb = l3mdev_ip_rcv(skb);
  510. if (!skb)
  511. continue;
  512. if (ip_rcv_finish_core(net, sk, skb, dev, hint) == NET_RX_DROP)
  513. continue;
  514. dst = skb_dst(skb);
  515. if (curr_dst != dst) {
  516. hint = ip_extract_route_hint(net, skb,
  517. ((struct rtable *)dst)->rt_type);
  518. /* dispatch old sublist */
  519. if (!list_empty(&sublist))
  520. ip_sublist_rcv_finish(&sublist);
  521. /* start new sublist */
  522. INIT_LIST_HEAD(&sublist);
  523. curr_dst = dst;
  524. }
  525. list_add_tail(&skb->list, &sublist);
  526. }
  527. /* dispatch final sublist */
  528. ip_sublist_rcv_finish(&sublist);
  529. }
  530. static void ip_sublist_rcv(struct list_head *head, struct net_device *dev,
  531. struct net *net)
  532. {
  533. NF_HOOK_LIST(NFPROTO_IPV4, NF_INET_PRE_ROUTING, net, NULL,
  534. head, dev, NULL, ip_rcv_finish);
  535. ip_list_rcv_finish(net, NULL, head);
  536. }
  537. /* Receive a list of IP packets */
  538. void ip_list_rcv(struct list_head *head, struct packet_type *pt,
  539. struct net_device *orig_dev)
  540. {
  541. struct net_device *curr_dev = NULL;
  542. struct net *curr_net = NULL;
  543. struct sk_buff *skb, *next;
  544. struct list_head sublist;
  545. INIT_LIST_HEAD(&sublist);
  546. list_for_each_entry_safe(skb, next, head, list) {
  547. struct net_device *dev = skb->dev;
  548. struct net *net = dev_net(dev);
  549. skb_list_del_init(skb);
  550. skb = ip_rcv_core(skb, net);
  551. if (skb == NULL)
  552. continue;
  553. if (curr_dev != dev || curr_net != net) {
  554. /* dispatch old sublist */
  555. if (!list_empty(&sublist))
  556. ip_sublist_rcv(&sublist, curr_dev, curr_net);
  557. /* start new sublist */
  558. INIT_LIST_HEAD(&sublist);
  559. curr_dev = dev;
  560. curr_net = net;
  561. }
  562. list_add_tail(&skb->list, &sublist);
  563. }
  564. /* dispatch final sublist */
  565. if (!list_empty(&sublist))
  566. ip_sublist_rcv(&sublist, curr_dev, curr_net);
  567. }