^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * INET An implementation of the TCP/IP protocol suite for the LINUX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * operating system. INET is implemented using the BSD Socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * interface as the means of communication with the user level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * ROUTE - implementation of the IP router.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors: Ross Biro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Alan Cox, <gw4pts@gw4pts.ampr.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Linus Torvalds, <Linus.Torvalds@helsinki.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Fixes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Alan Cox : Verify area fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Alan Cox : cli() protects routing changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Rui Oliveira : ICMP routing table updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * (rco@di.uminho.pt) Routing table insertion and update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Linus Torvalds : Rewrote bits to be sensible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Alan Cox : Added BSD route gw semantics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Alan Cox : Super /proc >4K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Alan Cox : MTU in route table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Alan Cox : MSS actually. Also added the window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * clamper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Sam Lantinga : Fixed route matching in rt_del()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Alan Cox : Routing cache support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Alan Cox : Removed compatibility cruft.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Alan Cox : RTF_REJECT support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Alan Cox : TCP irtt support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Jonathan Naylor : Added Metric support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Miquel van Smoorenburg : BSD API fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Miquel van Smoorenburg : Metrics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Alan Cox : Use __u32 properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Alan Cox : Aligned routing errors more closely with BSD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * our system is still very different.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Alan Cox : Faster /proc handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Alexey Kuznetsov : Massive rework to support tree based routing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * routing caches and better behaviour.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Olaf Erb : irtt wasn't being copied right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Bjorn Ekwall : Kerneld route support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Alan Cox : Multicast fixed (I hope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Pavel Krauz : Limited broadcast fixed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Mike McLagan : Routing by source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Alexey Kuznetsov : End of old history. Split to fib.c and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * route.c and rewritten from scratch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Andi Kleen : Load-limit warning messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * Vitaly E. Lavrov : Transparent proxy revived after year coma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Vitaly E. Lavrov : Race condition in ip_route_input_slow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Vladimir V. Ivanov : IP rule info (flowid) is really useful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Marc Boucher : routing by fwmark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Robert Olsson : Added rt_cache statistics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Arnaldo C. Melo : Convert proc stuff to seq_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * Ilia Sotnikov : Removed TOS from hash calculations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define pr_fmt(fmt) "IPv4: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #include <linux/igmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #include <linux/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #include <linux/mroute.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #include <linux/times.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #include <net/dst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #include <net/inetpeer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #include <net/ip_fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #include <net/nexthop.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #include <net/lwtunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #include <net/netevent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #include <net/secure_seq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #include <net/l3mdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #include "fib_lookup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #define RT_FL_TOS(oldflp4) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #define RT_GC_TIMEOUT (300*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int ip_rt_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int ip_rt_redirect_number __read_mostly = 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int ip_rt_redirect_load __read_mostly = HZ / 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int ip_rt_error_cost __read_mostly = HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int ip_rt_error_burst __read_mostly = 5 * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int ip_rt_mtu_expires __read_mostly = 10 * 60 * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static u32 ip_rt_min_pmtu __read_mostly = 512 + 20 + 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int ip_rt_min_advmss __read_mostly = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * Interface to generic destination cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static unsigned int ipv4_default_advmss(const struct dst_entry *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static unsigned int ipv4_mtu(const struct dst_entry *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void ipv4_link_failure(struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct sk_buff *skb, u32 mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) bool confirm_neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void ip_do_redirect(struct dst_entry *dst, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void ipv4_dst_destroy(struct dst_entry *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) const void *daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static struct dst_ops ipv4_dst_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .family = AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .check = ipv4_dst_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .default_advmss = ipv4_default_advmss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .mtu = ipv4_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .cow_metrics = ipv4_cow_metrics,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .destroy = ipv4_dst_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .negative_advice = ipv4_negative_advice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .link_failure = ipv4_link_failure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .update_pmtu = ip_rt_update_pmtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .redirect = ip_do_redirect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .local_out = __ip_local_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .neigh_lookup = ipv4_neigh_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .confirm_neigh = ipv4_confirm_neigh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #define ECN_OR_COST(class) TC_PRIO_##class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) const __u8 ip_tos2prio[16] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) TC_PRIO_BESTEFFORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ECN_OR_COST(BESTEFFORT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) TC_PRIO_BESTEFFORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ECN_OR_COST(BESTEFFORT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) TC_PRIO_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ECN_OR_COST(BULK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) TC_PRIO_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) ECN_OR_COST(BULK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) TC_PRIO_INTERACTIVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ECN_OR_COST(INTERACTIVE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) TC_PRIO_INTERACTIVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ECN_OR_COST(INTERACTIVE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) TC_PRIO_INTERACTIVE_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ECN_OR_COST(INTERACTIVE_BULK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) TC_PRIO_INTERACTIVE_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ECN_OR_COST(INTERACTIVE_BULK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) EXPORT_SYMBOL(ip_tos2prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #define RT_CACHE_STAT_INC(field) raw_cpu_inc(rt_cache_stat.field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (*pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static void rt_cache_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int rt_cache_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (v == SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) seq_printf(seq, "%-127s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) "HHUptod\tSpecDst");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static const struct seq_operations rt_cache_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) .start = rt_cache_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) .next = rt_cache_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .stop = rt_cache_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .show = rt_cache_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int rt_cache_seq_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return seq_open(file, &rt_cache_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static const struct proc_ops rt_cache_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) .proc_open = rt_cache_seq_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) .proc_read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) .proc_lseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) .proc_release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (*pos == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!cpu_possible(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *pos = cpu+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return &per_cpu(rt_cache_stat, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!cpu_possible(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *pos = cpu+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return &per_cpu(rt_cache_stat, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) (*pos)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int rt_cpu_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct rt_cache_stat *st = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (v == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) seq_printf(seq, "entries in_hit in_slow_tot in_slow_mc in_no_route in_brd in_martian_dst in_martian_src out_hit out_slow_tot out_slow_mc gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dst_entries_get_slow(&ipv4_dst_ops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 0, /* st->in_hit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) st->in_slow_tot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) st->in_slow_mc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) st->in_no_route,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) st->in_brd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) st->in_martian_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) st->in_martian_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 0, /* st->out_hit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) st->out_slow_tot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) st->out_slow_mc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 0, /* st->gc_total */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 0, /* st->gc_ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 0, /* st->gc_goal_miss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 0, /* st->gc_dst_overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 0, /* st->in_hlist_search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 0 /* st->out_hlist_search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static const struct seq_operations rt_cpu_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) .start = rt_cpu_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .next = rt_cpu_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) .stop = rt_cpu_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) .show = rt_cpu_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int rt_cpu_seq_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return seq_open(file, &rt_cpu_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static const struct proc_ops rt_cpu_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) .proc_open = rt_cpu_seq_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) .proc_read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) .proc_lseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) .proc_release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static int rt_acct_proc_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct ip_rt_acct *dst, *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (!dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) for (j = 0; j < 256; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) dst[j].o_bytes += src[j].o_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) dst[j].o_packets += src[j].o_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) dst[j].i_bytes += src[j].i_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dst[j].i_packets += src[j].i_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) kfree(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static int __net_init ip_rt_do_proc_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct proc_dir_entry *pde;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) pde = proc_create("rt_cache", 0444, net->proc_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) &rt_cache_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) pde = proc_create("rt_cache", 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) net->proc_net_stat, &rt_cpu_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) pde = proc_create_single("rt_acct", 0, net->proc_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) rt_acct_proc_show);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (!pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto err3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) err3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) remove_proc_entry("rt_cache", net->proc_net_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) err2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) remove_proc_entry("rt_cache", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static void __net_exit ip_rt_do_proc_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) remove_proc_entry("rt_cache", net->proc_net_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) remove_proc_entry("rt_cache", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) remove_proc_entry("rt_acct", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static struct pernet_operations ip_rt_proc_ops __net_initdata = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) .init = ip_rt_do_proc_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) .exit = ip_rt_do_proc_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int __init ip_rt_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return register_pernet_subsys(&ip_rt_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static inline int ip_rt_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) #endif /* CONFIG_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static inline bool rt_is_expired(const struct rtable *rth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return rth->rt_genid != rt_genid_ipv4(dev_net(rth->dst.dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) void rt_cache_flush(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) rt_genid_bump_ipv4(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) const void *daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) const struct rtable *rt = container_of(dst, struct rtable, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) struct net_device *dev = dst->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct neighbour *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) rcu_read_lock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (likely(rt->rt_gw_family == AF_INET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) n = ip_neigh_gw4(dev, rt->rt_gw4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) } else if (rt->rt_gw_family == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) n = ip_neigh_gw6(dev, &rt->rt_gw6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) __be32 pkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) n = ip_neigh_gw4(dev, pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) n = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) rcu_read_unlock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) const struct rtable *rt = container_of(dst, struct rtable, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct net_device *dev = dst->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) const __be32 *pkey = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (rt->rt_gw_family == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) pkey = (const __be32 *)&rt->rt_gw4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) } else if (rt->rt_gw_family == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return __ipv6_confirm_neigh_stub(dev, &rt->rt_gw6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) } else if (!daddr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) (rt->rt_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) (RTCF_MULTICAST | RTCF_BROADCAST | RTCF_LOCAL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) __ipv4_confirm_neigh(dev, *(__force u32 *)pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* Hash tables of size 2048..262144 depending on RAM size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * Each bucket uses 8 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static u32 ip_idents_mask __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static atomic_t *ip_idents __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static u32 *ip_tstamps __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* In order to protect privacy, we add a perturbation to identifiers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * if one generator is seldom used. This makes hard for an attacker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * to infer how many packets were sent between two points in time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) u32 ip_idents_reserve(u32 hash, int segs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) u32 bucket, old, now = (u32)jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) atomic_t *p_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) u32 *p_tstamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) u32 delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) bucket = hash & ip_idents_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) p_tstamp = ip_tstamps + bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) p_id = ip_idents + bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) old = READ_ONCE(*p_tstamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (old != now && cmpxchg(p_tstamp, old, now) == old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) delta = prandom_u32_max(now - old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /* If UBSAN reports an error there, please make sure your compiler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * supports -fno-strict-overflow before reporting it that was a bug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * in UBSAN, and it has been fixed in GCC-8.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return atomic_add_return(segs + delta, p_id) - segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) EXPORT_SYMBOL(ip_idents_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) void __ip_select_ident(struct net *net, struct iphdr *iph, int segs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) u32 hash, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /* Note the following code is not safe, but this is okay. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (unlikely(siphash_key_is_zero(&net->ipv4.ip_id_key)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) get_random_bytes(&net->ipv4.ip_id_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) sizeof(net->ipv4.ip_id_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) hash = siphash_3u32((__force u32)iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) (__force u32)iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) iph->protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) &net->ipv4.ip_id_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) id = ip_idents_reserve(hash, segs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) iph->id = htons(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) EXPORT_SYMBOL(__ip_select_ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static void ip_rt_fix_tos(struct flowi4 *fl4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) __u8 tos = RT_FL_TOS(fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) fl4->flowi4_tos = tos & IPTOS_RT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) fl4->flowi4_scope = tos & RTO_ONLINK ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static void __build_flow_key(const struct net *net, struct flowi4 *fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) const struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) const struct iphdr *iph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) int oif, u8 tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) u8 prot, u32 mark, int flow_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) const struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) oif = sk->sk_bound_dev_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) mark = sk->sk_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) tos = RT_CONN_FLAGS(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) prot = inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) flowi4_init_output(fl4, oif, mark, tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) RT_SCOPE_UNIVERSE, prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) flow_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) iph->daddr, iph->saddr, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) sock_net_uid(net, sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) const struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) const struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int oif = skb->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) u8 tos = RT_TOS(iph->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) u8 prot = iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) u32 mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) __build_flow_key(net, fl4, sk, iph, oif, tos, prot, mark, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) const struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) const struct ip_options_rcu *inet_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) __be32 daddr = inet->inet_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) inet_opt = rcu_dereference(inet->inet_opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (inet_opt && inet_opt->opt.srr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) daddr = inet_opt->opt.faddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) inet_sk_flowi_flags(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) daddr, inet->inet_saddr, 0, 0, sk->sk_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) build_skb_flow_key(fl4, skb, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) build_sk_flow_key(fl4, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static DEFINE_SPINLOCK(fnhe_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) rt = rcu_dereference(fnhe->fnhe_rth_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) dst_dev_put(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) rt = rcu_dereference(fnhe->fnhe_rth_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) dst_dev_put(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) struct fib_nh_exception *fnhe, *oldest = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) fnhe = rcu_dereference_protected(*fnhe_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) lockdep_is_held(&fnhe_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (!fnhe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (!oldest ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) oldest = fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) oldest_p = fnhe_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) fnhe_flush_routes(oldest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) *oldest_p = oldest->fnhe_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) kfree_rcu(oldest, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static u32 fnhe_hashfun(__be32 daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) static siphash_key_t fnhe_hash_key __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) u64 hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) net_get_random_once(&fnhe_hash_key, sizeof(fnhe_hash_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) hval = siphash_1u32((__force u32)daddr, &fnhe_hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return hash_64(hval, FNHE_HASH_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) rt->rt_pmtu = fnhe->fnhe_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) rt->rt_mtu_locked = fnhe->fnhe_mtu_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) rt->dst.expires = fnhe->fnhe_expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (fnhe->fnhe_gw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) rt->rt_flags |= RTCF_REDIRECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) rt->rt_uses_gateway = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) rt->rt_gw_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) rt->rt_gw4 = fnhe->fnhe_gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) __be32 gw, u32 pmtu, bool lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) unsigned long expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct fnhe_hash_bucket *hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct fib_nh_exception *fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) u32 genid, hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) genid = fnhe_genid(dev_net(nhc->nhc_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) hval = fnhe_hashfun(daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) spin_lock_bh(&fnhe_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) hash = rcu_dereference(nhc->nhc_exceptions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (!hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) hash = kcalloc(FNHE_HASH_SIZE, sizeof(*hash), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) rcu_assign_pointer(nhc->nhc_exceptions, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) hash += hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) for (fnhe = rcu_dereference(hash->chain); fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) fnhe = rcu_dereference(fnhe->fnhe_next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (fnhe->fnhe_daddr == daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (fnhe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (fnhe->fnhe_genid != genid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) fnhe->fnhe_genid = genid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) fnhe->fnhe_gw = gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (pmtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) fnhe->fnhe_pmtu = pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) fnhe->fnhe_mtu_locked = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) fnhe->fnhe_expires = max(1UL, expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /* Update all cached dsts too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) rt = rcu_dereference(fnhe->fnhe_rth_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) fill_route_from_fnhe(rt, fnhe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) rt = rcu_dereference(fnhe->fnhe_rth_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) fill_route_from_fnhe(rt, fnhe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /* Randomize max depth to avoid some side channels attacks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) int max_depth = FNHE_RECLAIM_DEPTH +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) prandom_u32_max(FNHE_RECLAIM_DEPTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) while (depth > max_depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) fnhe_remove_oldest(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (!fnhe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) fnhe->fnhe_next = hash->chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) fnhe->fnhe_genid = genid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) fnhe->fnhe_daddr = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) fnhe->fnhe_gw = gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) fnhe->fnhe_pmtu = pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) fnhe->fnhe_mtu_locked = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) fnhe->fnhe_expires = max(1UL, expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) rcu_assign_pointer(hash->chain, fnhe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) /* Exception created; mark the cached routes for the nexthop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * stale, so anyone caching it rechecks if this exception
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) * applies to them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) rt = rcu_dereference(nhc->nhc_rth_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) rt->dst.obsolete = DST_OBSOLETE_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct rtable __rcu **prt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) prt = per_cpu_ptr(nhc->nhc_pcpu_rth_output, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) rt = rcu_dereference(*prt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) rt->dst.obsolete = DST_OBSOLETE_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) fnhe->fnhe_stamp = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) spin_unlock_bh(&fnhe_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) bool kill_route)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) __be32 new_gw = icmp_hdr(skb)->un.gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) __be32 old_gw = ip_hdr(skb)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct neighbour *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) switch (icmp_hdr(skb)->code & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) case ICMP_REDIR_NET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) case ICMP_REDIR_NETTOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) case ICMP_REDIR_HOST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) case ICMP_REDIR_HOSTTOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (rt->rt_gw_family != AF_INET || rt->rt_gw4 != old_gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) ipv4_is_zeronet(new_gw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) goto reject_redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (!IN_DEV_SHARED_MEDIA(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (!inet_addr_onlink(in_dev, new_gw, old_gw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) goto reject_redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) goto reject_redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (inet_addr_type(net, new_gw) != RTN_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) goto reject_redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) n = __ipv4_neigh_lookup(rt->dst.dev, new_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) n = neigh_create(&arp_tbl, &new_gw, rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (!IS_ERR(n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (!(n->nud_state & NUD_VALID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) neigh_event_send(n, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (fib_lookup(net, fl4, &res, 0) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) struct fib_nh_common *nhc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) fib_select_path(net, &res, fl4, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) nhc = FIB_RES_NHC(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) update_or_create_fnhe(nhc, fl4->daddr, new_gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 0, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) jiffies + ip_rt_gc_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (kill_route)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) rt->dst.obsolete = DST_OBSOLETE_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) neigh_release(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) reject_redirect:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) #ifdef CONFIG_IP_ROUTE_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (IN_DEV_LOG_MARTIANS(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) const struct iphdr *iph = (const struct iphdr *) skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) __be32 daddr = iph->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) __be32 saddr = iph->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) " Advised path = %pI4 -> %pI4\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) &old_gw, dev->name, &new_gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) &saddr, &daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) const struct iphdr *iph = (const struct iphdr *) skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) int oif = skb->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) u8 tos = RT_TOS(iph->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) u8 prot = iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) u32 mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) rt = (struct rtable *) dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) __build_flow_key(net, &fl4, sk, iph, oif, tos, prot, mark, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) ip_rt_fix_tos(&fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) __ip_do_redirect(rt, skb, &fl4, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) struct rtable *rt = (struct rtable *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) struct dst_entry *ret = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (dst->obsolete > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) } else if ((rt->rt_flags & RTCF_REDIRECTED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) rt->dst.expires) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * Algorithm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * 1. The first ip_rt_redirect_number redirects are sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * with exponential backoff, then we stop sending them at all,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) * assuming that the host ignores our redirects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) * 2. If we did not see packets requiring redirects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) * during ip_rt_redirect_silence, we assume that the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) * forgot redirected route and start to send redirects again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * This algorithm is much cheaper and more intelligent than dumb load limiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * in icmp.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * and "frag. need" (breaks PMTU discovery) in icmp.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) void ip_rt_send_redirect(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct rtable *rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) struct inet_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) int log_martians;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) int vif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) in_dev = __in_dev_get_rcu(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) log_martians = IN_DEV_LOG_MARTIANS(in_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) vif = l3mdev_master_ifindex_rcu(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) net = dev_net(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, vif, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (!peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) rt_nexthop(rt, ip_hdr(skb)->daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) /* No redirected packets during ip_rt_redirect_silence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * reset the algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) peer->rate_tokens = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) peer->n_redirects = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) /* Too many ignored redirects; do not send anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * set dst.rate_last to the last seen redirected packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (peer->n_redirects >= ip_rt_redirect_number) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) peer->rate_last = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) goto out_put_peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) /* Check for load limit; set rate_last to the latest sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * redirect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) if (peer->n_redirects == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) time_after(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) (peer->rate_last +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) (ip_rt_redirect_load << peer->n_redirects)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) __be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) peer->rate_last = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) ++peer->n_redirects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) #ifdef CONFIG_IP_ROUTE_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (log_martians &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) peer->n_redirects == ip_rt_redirect_number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) &ip_hdr(skb)->saddr, inet_iif(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) &ip_hdr(skb)->daddr, &gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) out_put_peer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) inet_putpeer(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) static int ip_error(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct rtable *rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) struct inet_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) unsigned long now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) bool send;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (netif_is_l3_master(skb->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) /* IP on this device is disabled. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) net = dev_net(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (!IN_DEV_FORWARD(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) switch (rt->dst.error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) case EHOSTUNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) __IP_INC_STATS(net, IPSTATS_MIB_INADDRERRORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) case ENETUNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) switch (rt->dst.error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) case EINVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) case EHOSTUNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) code = ICMP_HOST_UNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) case ENETUNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) code = ICMP_NET_UNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) case EACCES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) code = ICMP_PKT_FILTERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) l3mdev_master_ifindex(skb->dev), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) send = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) peer->rate_tokens += now - peer->rate_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (peer->rate_tokens > ip_rt_error_burst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) peer->rate_tokens = ip_rt_error_burst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) peer->rate_last = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (peer->rate_tokens >= ip_rt_error_cost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) peer->rate_tokens -= ip_rt_error_cost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) send = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) inet_putpeer(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) if (send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) out: kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) struct dst_entry *dst = &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) struct net *net = dev_net(dst->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) bool lock = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) u32 old_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if (ip_mtu_locked(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) old_mtu = ipv4_mtu(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) if (old_mtu < mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (mtu < ip_rt_min_pmtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) lock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) mtu = min(old_mtu, ip_rt_min_pmtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (rt->rt_pmtu == mtu && !lock &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) time_before(jiffies, dst->expires - ip_rt_mtu_expires / 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) if (fib_lookup(net, fl4, &res, 0) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) struct fib_nh_common *nhc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) fib_select_path(net, &res, fl4, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) nhc = FIB_RES_NHC(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) jiffies + ip_rt_mtu_expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) struct sk_buff *skb, u32 mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) bool confirm_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) struct rtable *rt = (struct rtable *) dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) ip_rt_build_flow_key(&fl4, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) ip_rt_fix_tos(&fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /* Don't make lookup fail for bridged encapsulations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (skb && netif_is_any_bridge_port(skb->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) fl4.flowi4_oif = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) __ip_rt_update_pmtu(rt, &fl4, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) int oif, u8 protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) u32 mark = IP4_REPLY_MARK(net, skb->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) __build_flow_key(net, &fl4, NULL, iph, oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) RT_TOS(iph->tos), protocol, mark, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) rt = __ip_route_output_key(net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (!IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) __ip_rt_update_pmtu(rt, &fl4, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) EXPORT_SYMBOL_GPL(ipv4_update_pmtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) __build_flow_key(sock_net(sk), &fl4, sk, iph, 0, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) if (!fl4.flowi4_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) rt = __ip_route_output_key(sock_net(sk), &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (!IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) __ip_rt_update_pmtu(rt, &fl4, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) struct dst_entry *odst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) bool new = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (!ip_sk_accept_pmtu(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) odst = sk_dst_get(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (sock_owned_by_user(sk) || !odst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) __ipv4_sk_update_pmtu(skb, sk, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) rt = (struct rtable *)odst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) if (odst->obsolete && !odst->ops->check(odst, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) new = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) ip_rt_fix_tos(&fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) __ip_rt_update_pmtu((struct rtable *)xfrm_dst_path(&rt->dst), &fl4, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) if (!dst_check(&rt->dst, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) new = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) sk_dst_set(sk, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) dst_release(odst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) void ipv4_redirect(struct sk_buff *skb, struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) int oif, u8 protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) __build_flow_key(net, &fl4, NULL, iph, oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) RT_TOS(iph->tos), protocol, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) rt = __ip_route_output_key(net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (!IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) __ip_do_redirect(rt, skb, &fl4, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) EXPORT_SYMBOL_GPL(ipv4_redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) const struct iphdr *iph = (const struct iphdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) rt = __ip_route_output_key(net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) if (!IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) __ip_do_redirect(rt, skb, &fl4, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) EXPORT_SYMBOL_GPL(ipv4_sk_redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) struct rtable *rt = (struct rtable *) dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) /* All IPV4 dsts are created with ->obsolete set to the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) * DST_OBSOLETE_FORCE_CHK which forces validation calls down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) * into this function always.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) * When a PMTU/redirect information update invalidates a route,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) * this is indicated by setting obsolete to DST_OBSOLETE_KILL or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) * DST_OBSOLETE_DEAD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) if (dst->obsolete != DST_OBSOLETE_FORCE_CHK || rt_is_expired(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) static void ipv4_send_dest_unreach(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) struct ip_options opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) /* Recompile ip options since IPCB may not be valid anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) * Also check we have a reasonable ipv4 header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) if (!pskb_network_may_pull(skb, sizeof(struct iphdr)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) ip_hdr(skb)->version != 4 || ip_hdr(skb)->ihl < 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) memset(&opt, 0, sizeof(opt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (ip_hdr(skb)->ihl > 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (!pskb_network_may_pull(skb, ip_hdr(skb)->ihl * 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) opt.optlen = ip_hdr(skb)->ihl * 4 - sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) res = __ip_options_compile(dev_net(skb->dev), &opt, skb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, &opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) static void ipv4_link_failure(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) ipv4_send_dest_unreach(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) dst_set_expires(&rt->dst, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) static int ip_rt_bug(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) pr_debug("%s: %pI4 -> %pI4, %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) skb->dev ? skb->dev->name : "?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) We do not cache source address of outgoing interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) because it is used only by IP RR, TS and SRR options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) so that it out of fast path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) BTW remember: "addr" is allowed to be not aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) in IP options!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) __be32 src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) if (rt_is_output_route(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) src = ip_hdr(skb)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) struct flowi4 fl4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) .daddr = iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) .saddr = iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) .flowi4_tos = RT_TOS(iph->tos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) .flowi4_oif = rt->dst.dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) .flowi4_iif = skb->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) .flowi4_mark = skb->mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res, 0) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) src = fib_result_prefsrc(dev_net(rt->dst.dev), &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) src = inet_select_addr(rt->dst.dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) rt_nexthop(rt, iph->daddr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) RT_SCOPE_UNIVERSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) memcpy(addr, &src, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) static void set_class_tag(struct rtable *rt, u32 tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) if (!(rt->dst.tclassid & 0xFFFF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) rt->dst.tclassid |= tag & 0xFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) if (!(rt->dst.tclassid & 0xFFFF0000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) rt->dst.tclassid |= tag & 0xFFFF0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) unsigned int header_size = sizeof(struct tcphdr) + sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) unsigned int advmss = max_t(unsigned int, ipv4_mtu(dst) - header_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) ip_rt_min_advmss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) return min(advmss, IPV4_MAX_PMTU - header_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) static unsigned int ipv4_mtu(const struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) const struct rtable *rt = (const struct rtable *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) unsigned int mtu = rt->rt_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) if (!mtu || time_after_eq(jiffies, rt->dst.expires))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) mtu = dst_metric_raw(dst, RTAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) if (mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) mtu = READ_ONCE(dst->dev->mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) if (unlikely(ip_mtu_locked(dst))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) if (rt->rt_uses_gateway && mtu > 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) mtu = 576;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) mtu = min_t(unsigned int, mtu, IP_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) static void ip_del_fnhe(struct fib_nh_common *nhc, __be32 daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) struct fnhe_hash_bucket *hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) struct fib_nh_exception *fnhe, __rcu **fnhe_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) u32 hval = fnhe_hashfun(daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) spin_lock_bh(&fnhe_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) hash = rcu_dereference_protected(nhc->nhc_exceptions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) lockdep_is_held(&fnhe_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) hash += hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) fnhe_p = &hash->chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) fnhe = rcu_dereference_protected(*fnhe_p, lockdep_is_held(&fnhe_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) while (fnhe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) if (fnhe->fnhe_daddr == daddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) rcu_assign_pointer(*fnhe_p, rcu_dereference_protected(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) fnhe->fnhe_next, lockdep_is_held(&fnhe_lock)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) /* set fnhe_daddr to 0 to ensure it won't bind with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) * new dsts in rt_bind_exception().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) fnhe->fnhe_daddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) fnhe_flush_routes(fnhe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) kfree_rcu(fnhe, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) fnhe_p = &fnhe->fnhe_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) fnhe = rcu_dereference_protected(fnhe->fnhe_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) lockdep_is_held(&fnhe_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) spin_unlock_bh(&fnhe_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) static struct fib_nh_exception *find_exception(struct fib_nh_common *nhc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) __be32 daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) struct fnhe_hash_bucket *hash = rcu_dereference(nhc->nhc_exceptions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) struct fib_nh_exception *fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) u32 hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) hval = fnhe_hashfun(daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) fnhe = rcu_dereference(fnhe->fnhe_next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) if (fnhe->fnhe_daddr == daddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) if (fnhe->fnhe_expires &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) time_after(jiffies, fnhe->fnhe_expires)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) ip_del_fnhe(nhc, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) return fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) /* MTU selection:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) * 1. mtu on route is locked - use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) * 2. mtu from nexthop exception
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) * 3. mtu from egress device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) struct fib_nh_common *nhc = res->nhc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) struct net_device *dev = nhc->nhc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) struct fib_info *fi = res->fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) u32 mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) if (dev_net(dev)->ipv4.sysctl_ip_fwd_use_pmtu ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) fi->fib_metrics->metrics[RTAX_LOCK - 1] & (1 << RTAX_MTU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) mtu = fi->fib_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) if (likely(!mtu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) struct fib_nh_exception *fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) fnhe = find_exception(nhc, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) if (fnhe && !time_after_eq(jiffies, fnhe->fnhe_expires))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) mtu = fnhe->fnhe_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) if (likely(!mtu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) mtu = min(READ_ONCE(dev->mtu), IP_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) return mtu - lwtunnel_headroom(nhc->nhc_lwtstate, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) __be32 daddr, const bool do_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) spin_lock_bh(&fnhe_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) if (daddr == fnhe->fnhe_daddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) struct rtable __rcu **porig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) struct rtable *orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) int genid = fnhe_genid(dev_net(rt->dst.dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if (rt_is_input_route(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) porig = &fnhe->fnhe_rth_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) porig = &fnhe->fnhe_rth_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) orig = rcu_dereference(*porig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) if (fnhe->fnhe_genid != genid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) fnhe->fnhe_genid = genid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) fnhe->fnhe_gw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) fnhe->fnhe_pmtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) fnhe->fnhe_expires = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) fnhe->fnhe_mtu_locked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) fnhe_flush_routes(fnhe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) orig = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) fill_route_from_fnhe(rt, fnhe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) if (!rt->rt_gw4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) rt->rt_gw4 = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) rt->rt_gw_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) if (do_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) dst_hold(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) rcu_assign_pointer(*porig, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) if (orig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) dst_dev_put(&orig->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) dst_release(&orig->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) fnhe->fnhe_stamp = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) spin_unlock_bh(&fnhe_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) static bool rt_cache_route(struct fib_nh_common *nhc, struct rtable *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) struct rtable *orig, *prev, **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (rt_is_input_route(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) p = (struct rtable **)&nhc->nhc_rth_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) p = (struct rtable **)raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) orig = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) /* hold dst before doing cmpxchg() to avoid race condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) * on this dst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) dst_hold(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) prev = cmpxchg(p, orig, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) if (prev == orig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) if (orig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) rt_add_uncached_list(orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) dst_release(&orig->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) dst_release(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) struct uncached_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) struct list_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) void rt_add_uncached_list(struct rtable *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) rt->rt_uncached_list = ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) spin_lock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) list_add_tail(&rt->rt_uncached, &ul->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) spin_unlock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) void rt_del_uncached_list(struct rtable *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) if (!list_empty(&rt->rt_uncached)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) struct uncached_list *ul = rt->rt_uncached_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) spin_lock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) list_del(&rt->rt_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) spin_unlock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) static void ipv4_dst_destroy(struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) struct rtable *rt = (struct rtable *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) ip_dst_metrics_put(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) rt_del_uncached_list(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) void rt_flush_dev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) spin_lock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) list_for_each_entry(rt, &ul->head, rt_uncached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) if (rt->dst.dev != dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) rt->dst.dev = blackhole_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) dev_hold(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) spin_unlock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) static bool rt_cache_valid(const struct rtable *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) return rt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) !rt_is_expired(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) const struct fib_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) struct fib_nh_exception *fnhe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) struct fib_info *fi, u16 type, u32 itag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) const bool do_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) bool cached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) if (fi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) struct fib_nh_common *nhc = FIB_RES_NHC(*res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) if (nhc->nhc_gw_family && nhc->nhc_scope == RT_SCOPE_LINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) rt->rt_uses_gateway = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) rt->rt_gw_family = nhc->nhc_gw_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) /* only INET and INET6 are supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) if (likely(nhc->nhc_gw_family == AF_INET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) rt->rt_gw4 = nhc->nhc_gw.ipv4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) rt->rt_gw6 = nhc->nhc_gw.ipv6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) ip_dst_init_metrics(&rt->dst, fi->fib_metrics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (nhc->nhc_family == AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) struct fib_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) nh = container_of(nhc, struct fib_nh, nh_common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) rt->dst.tclassid = nh->nh_tclassid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) rt->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) if (unlikely(fnhe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) cached = rt_bind_exception(rt, fnhe, daddr, do_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) else if (do_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) cached = rt_cache_route(nhc, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) if (unlikely(!cached)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) /* Routes we intend to cache in nexthop exception or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) * FIB nexthop have the DST_NOCACHE bit clear.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) * However, if we are unsuccessful at storing this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) * route into the cache we really need to set it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) if (!rt->rt_gw4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) rt->rt_gw_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) rt->rt_gw4 = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) rt_add_uncached_list(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) rt_add_uncached_list(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) #ifdef CONFIG_IP_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) set_class_tag(rt, res->tclassid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) set_class_tag(rt, itag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) struct rtable *rt_dst_alloc(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) unsigned int flags, u16 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) bool nopolicy, bool noxfrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) (nopolicy ? DST_NOPOLICY : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) (noxfrm ? DST_NOXFRM : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) rt->rt_genid = rt_genid_ipv4(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) rt->rt_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) rt->rt_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) rt->rt_is_input = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) rt->rt_iif = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) rt->rt_pmtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) rt->rt_mtu_locked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) rt->rt_uses_gateway = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) rt->rt_gw_family = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) rt->rt_gw4 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) INIT_LIST_HEAD(&rt->rt_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) rt->dst.output = ip_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) if (flags & RTCF_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) rt->dst.input = ip_local_deliver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) EXPORT_SYMBOL(rt_dst_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) struct rtable *new_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) new_rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) rt->dst.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) if (new_rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) new_rt->rt_genid = rt_genid_ipv4(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) new_rt->rt_flags = rt->rt_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) new_rt->rt_type = rt->rt_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) new_rt->rt_is_input = rt->rt_is_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) new_rt->rt_iif = rt->rt_iif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) new_rt->rt_pmtu = rt->rt_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) new_rt->rt_mtu_locked = rt->rt_mtu_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) new_rt->rt_gw_family = rt->rt_gw_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) if (rt->rt_gw_family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) new_rt->rt_gw4 = rt->rt_gw4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) else if (rt->rt_gw_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) new_rt->rt_gw6 = rt->rt_gw6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) INIT_LIST_HEAD(&new_rt->rt_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) new_rt->dst.input = rt->dst.input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) new_rt->dst.output = rt->dst.output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) new_rt->dst.error = rt->dst.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) new_rt->dst.lastuse = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) new_rt->dst.lwtstate = lwtstate_get(rt->dst.lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) return new_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) EXPORT_SYMBOL(rt_dst_clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) /* called in rcu_read_lock() section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) u8 tos, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) struct in_device *in_dev, u32 *itag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) /* Primary sanity checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) skb->protocol != htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) if (ipv4_is_loopback(saddr) && !IN_DEV_ROUTE_LOCALNET(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) if (ipv4_is_zeronet(saddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) if (!ipv4_is_local_multicast(daddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) ip_hdr(skb)->protocol != IPPROTO_IGMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) in_dev, itag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) /* called in rcu_read_lock() section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) u8 tos, struct net_device *dev, int our)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) struct in_device *in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) unsigned int flags = RTCF_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) struct rtable *rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) u32 itag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) err = ip_mc_validate_source(skb, daddr, saddr, tos, dev, in_dev, &itag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) if (our)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) flags |= RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) rth = rt_dst_alloc(dev_net(dev)->loopback_dev, flags, RTN_MULTICAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) IN_DEV_CONF_GET(in_dev, NOPOLICY), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) if (!rth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) rth->dst.tclassid = itag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) rth->dst.output = ip_rt_bug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) rth->rt_is_input= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) #ifdef CONFIG_IP_MROUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) rth->dst.input = ip_mr_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) RT_CACHE_STAT_INC(in_slow_mc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) skb_dst_set(skb, &rth->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) static void ip_handle_martian_source(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) struct in_device *in_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) __be32 daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) __be32 saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) RT_CACHE_STAT_INC(in_martian_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) #ifdef CONFIG_IP_ROUTE_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) * RFC1812 recommendation, if source is martian,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) * the only hint is MAC header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) pr_warn("martian source %pI4 from %pI4, on dev %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) &daddr, &saddr, dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) print_hex_dump(KERN_WARNING, "ll header: ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) DUMP_PREFIX_OFFSET, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) skb_mac_header(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) dev->hard_header_len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) /* called in rcu_read_lock() section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) static int __mkroute_input(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) const struct fib_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) struct in_device *in_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) __be32 daddr, __be32 saddr, u32 tos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) struct fib_nh_common *nhc = FIB_RES_NHC(*res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) struct net_device *dev = nhc->nhc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) struct fib_nh_exception *fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) struct rtable *rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) struct in_device *out_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) bool do_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) u32 itag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) /* get a working reference to the output device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) out_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) if (!out_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) in_dev->dev, in_dev, &itag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) do_cache = res->fi && !itag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) skb->protocol == htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) __be32 gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) gw = nhc->nhc_gw_family == AF_INET ? nhc->nhc_gw.ipv4 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) if (IN_DEV_SHARED_MEDIA(out_dev) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) inet_addr_onlink(out_dev, saddr, gw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) IPCB(skb)->flags |= IPSKB_DOREDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) if (skb->protocol != htons(ETH_P_IP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) /* Not IP (i.e. ARP). Do not create route, if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) * invalid for proxy arp. DNAT routes are always valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) * Proxy arp feature have been extended to allow, ARP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) * replies back to the same interface, to support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) * Private VLAN switch technologies. See arp.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) if (out_dev == in_dev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) fnhe = find_exception(nhc, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) if (do_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) if (fnhe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) rth = rcu_dereference(fnhe->fnhe_rth_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) rth = rcu_dereference(nhc->nhc_rth_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) if (rt_cache_valid(rth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) skb_dst_set_noref(skb, &rth->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) rth = rt_dst_alloc(out_dev->dev, 0, res->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) IN_DEV_CONF_GET(in_dev, NOPOLICY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) IN_DEV_CONF_GET(out_dev, NOXFRM));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) if (!rth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) rth->rt_is_input = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) RT_CACHE_STAT_INC(in_slow_tot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) rth->dst.input = ip_forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) do_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) lwtunnel_set_redirect(&rth->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) skb_dst_set(skb, &rth->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) #ifdef CONFIG_IP_ROUTE_MULTIPATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) /* To make ICMP packets follow the right flow, the multipath hash is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) * calculated from the inner IP addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) static void ip_multipath_l3_keys(const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) struct flow_keys *hash_keys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) const struct iphdr *outer_iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) const struct iphdr *key_iph = outer_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) const struct iphdr *inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) const struct icmphdr *icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) struct iphdr _inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) struct icmphdr _icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) if (likely(outer_iph->protocol != IPPROTO_ICMP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) if (unlikely((outer_iph->frag_off & htons(IP_OFFSET)) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) icmph = skb_header_pointer(skb, outer_iph->ihl * 4, sizeof(_icmph),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) &_icmph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) if (!icmph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) if (!icmp_is_err(icmph->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) inner_iph = skb_header_pointer(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) outer_iph->ihl * 4 + sizeof(_icmph),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) sizeof(_inner_iph), &_inner_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) if (!inner_iph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) key_iph = inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) hash_keys->addrs.v4addrs.src = key_iph->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) hash_keys->addrs.v4addrs.dst = key_iph->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) /* if skb is set it will be used and fl4 can be NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) const struct sk_buff *skb, struct flow_keys *flkeys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) struct flow_keys hash_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) u32 mhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) switch (net->ipv4.sysctl_fib_multipath_hash_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) ip_multipath_l3_keys(skb, &hash_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) hash_keys.addrs.v4addrs.src = fl4->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) hash_keys.addrs.v4addrs.dst = fl4->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) /* skb is currently provided only when forwarding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) struct flow_keys keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) /* short-circuit if we already have L4 hash present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) if (skb->l4_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) return skb_get_hash_raw(skb) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) if (!flkeys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) skb_flow_dissect_flow_keys(skb, &keys, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) flkeys = &keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) hash_keys.ports.src = flkeys->ports.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) hash_keys.ports.dst = flkeys->ports.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) hash_keys.addrs.v4addrs.src = fl4->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) hash_keys.addrs.v4addrs.dst = fl4->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) hash_keys.ports.src = fl4->fl4_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) hash_keys.ports.dst = fl4->fl4_dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) hash_keys.basic.ip_proto = fl4->flowi4_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) /* skb is currently provided only when forwarding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) struct flow_keys keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) skb_flow_dissect_flow_keys(skb, &keys, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) /* Inner can be v4 or v6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) hash_keys.tags.flow_label = keys.tags.flow_label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) hash_keys.basic.ip_proto = keys.basic.ip_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) /* Same as case 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) ip_multipath_l3_keys(skb, &hash_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) /* Same as case 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) hash_keys.addrs.v4addrs.src = fl4->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) hash_keys.addrs.v4addrs.dst = fl4->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) mhash = flow_hash_from_keys(&hash_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) if (multipath_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) mhash = jhash_2words(mhash, multipath_hash, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) return mhash >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) #endif /* CONFIG_IP_ROUTE_MULTIPATH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) static int ip_mkroute_input(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) struct fib_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) struct in_device *in_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) __be32 daddr, __be32 saddr, u32 tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) struct flow_keys *hkeys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) #ifdef CONFIG_IP_ROUTE_MULTIPATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) if (res->fi && fib_info_num_path(res->fi) > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) int h = fib_multipath_hash(res->fi->fib_net, NULL, skb, hkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) fib_select_multipath(res, h);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) /* create a routing cache entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) /* Implements all the saddr-related checks as ip_route_input_slow(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) * assuming daddr is valid and the destination is not a local broadcast one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) * Uses the provided hint instead of performing a route lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) int ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) u8 tos, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) const struct sk_buff *hint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) struct in_device *in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) struct rtable *rt = skb_rtable(hint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) u32 tag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) if (ipv4_is_zeronet(saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) if (ipv4_is_loopback(saddr) && !IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) if (rt->rt_type != RTN_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) goto skip_validate_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) tos &= IPTOS_RT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) err = fib_validate_source(skb, saddr, daddr, tos, 0, dev, in_dev, &tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) skip_validate_source:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) skb_dst_copy(skb, hint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) martian_source:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) /* get device for dst_alloc with local routes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) static struct net_device *ip_rt_get_dev(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) const struct fib_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) struct fib_nh_common *nhc = res->fi ? res->nhc : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) if (nhc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) dev = l3mdev_master_dev_rcu(nhc->nhc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) return dev ? : net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) * NOTE. We drop all the packets that has local source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) * addresses, because every properly looped back packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) * must have correct destination already attached by output routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) * Changes in the enforced policies must be applied also to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) * ip_route_use_hint().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) * Such approach solves two big problems:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) * 1. Not simplex devices are handled properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) * 2. IP spoofing attempts are filtered with 100% of guarantee.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) * called with rcu_read_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) u8 tos, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) struct fib_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) struct in_device *in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) struct flow_keys *flkeys = NULL, _flkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) struct ip_tunnel_info *tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) unsigned int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) u32 itag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) struct rtable *rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) bool do_cache = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) /* IP on this device is disabled. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) /* Check for the most weird martians, which can be not detected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) by fib_lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) tun_info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) fl4.flowi4_tun_key.tun_id = tun_info->key.tun_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) fl4.flowi4_tun_key.tun_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) res->fi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) res->table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) goto brd_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) /* Accept zero addresses only to limited broadcast;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) * I even do not know to fix it or not. Waiting for complains :-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) if (ipv4_is_zeronet(saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) if (ipv4_is_zeronet(daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) goto martian_destination;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) * and call it once if daddr or/and saddr are loopback addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) if (ipv4_is_loopback(daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) goto martian_destination;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) } else if (ipv4_is_loopback(saddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) * Now we are ready to route packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) fl4.flowi4_oif = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) fl4.flowi4_iif = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) fl4.flowi4_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) fl4.flowi4_tos = tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) fl4.flowi4_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) fl4.daddr = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) fl4.saddr = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) fl4.flowi4_uid = sock_net_uid(net, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) fl4.flowi4_multipath_hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) if (fib4_rules_early_flow_dissect(net, skb, &fl4, &_flkeys)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) flkeys = &_flkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) fl4.flowi4_proto = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) fl4.fl4_sport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) fl4.fl4_dport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) err = fib_lookup(net, &fl4, res, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) if (err != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) if (!IN_DEV_FORWARD(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) goto no_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) if (res->type == RTN_BROADCAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) if (IN_DEV_BFORWARD(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) goto make_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) /* not do cache if bc_forwarding is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) if (IPV4_DEVCONF_ALL(net, BC_FORWARDING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) do_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) goto brd_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) if (res->type == RTN_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) err = fib_validate_source(skb, saddr, daddr, tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 0, dev, in_dev, &itag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) goto local_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) if (!IN_DEV_FORWARD(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) goto no_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) if (res->type != RTN_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) goto martian_destination;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) make_route:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, tos, flkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) out: return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) brd_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) if (skb->protocol != htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) goto e_inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) if (!ipv4_is_zeronet(saddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) in_dev, &itag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) goto martian_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) flags |= RTCF_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) res->type = RTN_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) RT_CACHE_STAT_INC(in_brd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) local_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) do_cache &= res->fi && !itag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) if (do_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) struct fib_nh_common *nhc = FIB_RES_NHC(*res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) rth = rcu_dereference(nhc->nhc_rth_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) if (rt_cache_valid(rth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) skb_dst_set_noref(skb, &rth->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) rth = rt_dst_alloc(ip_rt_get_dev(net, res),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) flags | RTCF_LOCAL, res->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) IN_DEV_CONF_GET(in_dev, NOPOLICY), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) if (!rth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) goto e_nobufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) rth->dst.output= ip_rt_bug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) rth->dst.tclassid = itag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) rth->rt_is_input = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) RT_CACHE_STAT_INC(in_slow_tot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) if (res->type == RTN_UNREACHABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) rth->dst.input= ip_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) rth->dst.error= -err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) rth->rt_flags &= ~RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) if (do_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) struct fib_nh_common *nhc = FIB_RES_NHC(*res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) rth->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) if (lwtunnel_input_redirect(rth->dst.lwtstate)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) WARN_ON(rth->dst.input == lwtunnel_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) rth->dst.lwtstate->orig_input = rth->dst.input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) rth->dst.input = lwtunnel_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) if (unlikely(!rt_cache_route(nhc, rth)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) rt_add_uncached_list(rth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) skb_dst_set(skb, &rth->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) no_route:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) RT_CACHE_STAT_INC(in_no_route);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) res->type = RTN_UNREACHABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) res->fi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) res->table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) goto local_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) * Do not cache martian addresses: they should be logged (RFC1812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) martian_destination:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) RT_CACHE_STAT_INC(in_martian_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) #ifdef CONFIG_IP_ROUTE_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) if (IN_DEV_LOG_MARTIANS(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) &daddr, &saddr, dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) e_inval:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) e_nobufs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) martian_source:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) u8 tos, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) tos &= IPTOS_RT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) err = ip_route_input_rcu(skb, daddr, saddr, tos, dev, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) EXPORT_SYMBOL(ip_route_input_noref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) /* called with rcu_read_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) int ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) u8 tos, struct net_device *dev, struct fib_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) /* Multicast recognition logic is moved from route cache to here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) The problem was that too many Ethernet cards have broken/missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) hardware multicast filters :-( As result the host on multicasting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) network acquires a lot of useless route cache entries, sort of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) SDR messages from all the world. Now we try to get rid of them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) Really, provided software IP multicast filter is organized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) reasonably (at least, hashed), it does not result in a slowdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) comparing with route cache reject entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) Note, that multicast routers are not affected, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) route cache entry is created eventually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) if (ipv4_is_multicast(daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) struct in_device *in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) int our = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) our = ip_check_mc_rcu(in_dev, daddr, saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) ip_hdr(skb)->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) /* check l3 master if no match yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) if (!our && netif_is_l3_slave(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) struct in_device *l3_in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) l3_in_dev = __in_dev_get_rcu(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) if (l3_in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) our = ip_check_mc_rcu(l3_in_dev, daddr, saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) ip_hdr(skb)->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) if (our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) #ifdef CONFIG_IP_MROUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) (!ipv4_is_local_multicast(daddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) IN_DEV_MFORWARD(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) err = ip_route_input_mc(skb, daddr, saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) tos, dev, our);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) return ip_route_input_slow(skb, daddr, saddr, tos, dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) /* called with rcu_read_lock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) static struct rtable *__mkroute_output(const struct fib_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) const struct flowi4 *fl4, int orig_oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) struct net_device *dev_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) struct fib_info *fi = res->fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) struct fib_nh_exception *fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) u16 type = res->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) struct rtable *rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) bool do_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) in_dev = __in_dev_get_rcu(dev_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) if (ipv4_is_loopback(fl4->saddr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) !(dev_out->flags & IFF_LOOPBACK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) !netif_is_l3_master(dev_out))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) if (ipv4_is_lbcast(fl4->daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) type = RTN_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) else if (ipv4_is_multicast(fl4->daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) type = RTN_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) else if (ipv4_is_zeronet(fl4->daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) if (dev_out->flags & IFF_LOOPBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) flags |= RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) do_cache = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) if (type == RTN_BROADCAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) flags |= RTCF_BROADCAST | RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) fi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) } else if (type == RTN_MULTICAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) flags |= RTCF_MULTICAST | RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) fl4->flowi4_proto))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) flags &= ~RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) do_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) /* If multicast route do not exist use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) * default one, but do not gateway in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) * Yes, it is hack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) if (fi && res->prefixlen < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) fi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) } else if ((type == RTN_LOCAL) && (orig_oif != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) (orig_oif != dev_out->ifindex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) /* For local routes that require a particular output interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) * we do not want to cache the result. Caching the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) * causes incorrect behaviour when there are multiple source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) * addresses on the interface, the end result being that if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) * intended recipient is waiting on that interface for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) * packet he won't receive it because it will be delivered on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) * the loopback interface and the IP_PKTINFO ipi_ifindex will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) * be set to the loopback interface as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) do_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) fnhe = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) do_cache &= fi != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) if (fi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) struct fib_nh_common *nhc = FIB_RES_NHC(*res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) struct rtable __rcu **prth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) fnhe = find_exception(nhc, fl4->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) if (!do_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) goto add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) if (fnhe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) prth = &fnhe->fnhe_rth_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) if (unlikely(fl4->flowi4_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) FLOWI_FLAG_KNOWN_NH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) !(nhc->nhc_gw_family &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) nhc->nhc_scope == RT_SCOPE_LINK))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) do_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) goto add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) prth = raw_cpu_ptr(nhc->nhc_pcpu_rth_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) rth = rcu_dereference(*prth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) if (rt_cache_valid(rth) && dst_hold_safe(&rth->dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) return rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) add:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) rth = rt_dst_alloc(dev_out, flags, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) IN_DEV_CONF_GET(in_dev, NOPOLICY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) IN_DEV_CONF_GET(in_dev, NOXFRM));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) if (!rth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) return ERR_PTR(-ENOBUFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) rth->rt_iif = orig_oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) RT_CACHE_STAT_INC(out_slow_tot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) if (flags & RTCF_LOCAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) !(dev_out->flags & IFF_LOOPBACK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) rth->dst.output = ip_mc_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) RT_CACHE_STAT_INC(out_slow_mc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) #ifdef CONFIG_IP_MROUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) if (type == RTN_MULTICAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) if (IN_DEV_MFORWARD(in_dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) !ipv4_is_local_multicast(fl4->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) rth->dst.input = ip_mr_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) rth->dst.output = ip_mc_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0, do_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) lwtunnel_set_redirect(&rth->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) return rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) * Major route resolver routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) struct fib_result res = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) .type = RTN_UNSPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) .fi = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) .table = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) .tclassid = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) struct rtable *rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) fl4->flowi4_iif = LOOPBACK_IFINDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) ip_rt_fix_tos(fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) rth = ip_route_output_key_hash_rcu(net, fl4, &res, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) return rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) EXPORT_SYMBOL_GPL(ip_route_output_key_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) struct fib_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) const struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) struct net_device *dev_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) int orig_oif = fl4->flowi4_oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) unsigned int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) struct rtable *rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) if (fl4->saddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) if (ipv4_is_multicast(fl4->saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) ipv4_is_lbcast(fl4->saddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) ipv4_is_zeronet(fl4->saddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) rth = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) rth = ERR_PTR(-ENETUNREACH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) /* I removed check for oif == dev_out->oif here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) It was wrong for two reasons:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 1. ip_dev_find(net, saddr) can return wrong iface, if saddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) is assigned to multiple interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 2. Moreover, we are allowed to send packets with saddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) of another iface. --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) if (fl4->flowi4_oif == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) (ipv4_is_multicast(fl4->daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) ipv4_is_lbcast(fl4->daddr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) dev_out = __ip_dev_find(net, fl4->saddr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) if (!dev_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) /* Special hack: user can direct multicasts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) and limited broadcast via necessary interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) This hack is not just for fun, it allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) vic,vat and friends to work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) They bind socket to loopback, set ttl to zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) and expect that it will work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) From the viewpoint of routing cache they are broken,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) because we are not allowed to build multicast path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) with loopback source addr (look, routing cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) cannot know, that ttl is zero, so that packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) will not leave this host and route is valid).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) Luckily, this hack is good workaround.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) fl4->flowi4_oif = dev_out->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) goto make_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) if (!__ip_dev_find(net, fl4->saddr, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) if (fl4->flowi4_oif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) rth = ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) if (!dev_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) /* RACE: Check return value of inet_select_addr instead. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) rth = ERR_PTR(-ENETUNREACH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) if (ipv4_is_local_multicast(fl4->daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) ipv4_is_lbcast(fl4->daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) fl4->flowi4_proto == IPPROTO_IGMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) if (!fl4->saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) fl4->saddr = inet_select_addr(dev_out, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) RT_SCOPE_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) goto make_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) if (!fl4->saddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) if (ipv4_is_multicast(fl4->daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) fl4->saddr = inet_select_addr(dev_out, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) fl4->flowi4_scope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) else if (!fl4->daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) fl4->saddr = inet_select_addr(dev_out, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) RT_SCOPE_HOST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) if (!fl4->daddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) fl4->daddr = fl4->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) if (!fl4->daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) dev_out = net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) fl4->flowi4_oif = LOOPBACK_IFINDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) res->type = RTN_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) flags |= RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) goto make_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) err = fib_lookup(net, fl4, res, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) res->fi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) res->table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) if (fl4->flowi4_oif &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) (ipv4_is_multicast(fl4->daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) !netif_index_is_l3_master(net, fl4->flowi4_oif))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) /* Apparently, routing tables are wrong. Assume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) that the destination is on link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) WHY? DW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) Because we are allowed to send to iface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) even if it has NO routes and NO assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) addresses. When oif is specified, routing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) tables are looked up with only one purpose:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) to catch if destination is gatewayed, rather than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) direct. Moreover, if MSG_DONTROUTE is set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) we send packet, ignoring both routing tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) and ifaddr state. --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) We could make it even if oif is unknown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) likely IPv6, but we do not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) if (fl4->saddr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) fl4->saddr = inet_select_addr(dev_out, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) RT_SCOPE_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) res->type = RTN_UNICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) goto make_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) rth = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) if (res->type == RTN_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) if (!fl4->saddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) if (res->fi->fib_prefsrc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) fl4->saddr = res->fi->fib_prefsrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) fl4->saddr = fl4->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) /* L3 master device is the loopback for that domain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) dev_out = l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) ? :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) /* make sure orig_oif points to fib result device even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) * though packet rx/tx happens over loopback or l3mdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) orig_oif = FIB_RES_OIF(*res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) fl4->flowi4_oif = dev_out->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) flags |= RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) goto make_route;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) fib_select_path(net, res, fl4, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) dev_out = FIB_RES_DEV(*res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) make_route:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) rth = __mkroute_output(res, fl4, orig_oif, dev_out, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) return rth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) static struct dst_ops ipv4_dst_blackhole_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) .family = AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) .default_advmss = ipv4_default_advmss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) .neigh_lookup = ipv4_neigh_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) .check = dst_blackhole_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) .cow_metrics = dst_blackhole_cow_metrics,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) .update_pmtu = dst_blackhole_update_pmtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) .redirect = dst_blackhole_redirect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) .mtu = dst_blackhole_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) struct rtable *ort = (struct rtable *) dst_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_DEAD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) struct dst_entry *new = &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) new->__use = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) new->input = dst_discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) new->output = dst_discard_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) new->dev = net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) if (new->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) dev_hold(new->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) rt->rt_is_input = ort->rt_is_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) rt->rt_iif = ort->rt_iif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) rt->rt_pmtu = ort->rt_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) rt->rt_mtu_locked = ort->rt_mtu_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) rt->rt_genid = rt_genid_ipv4(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) rt->rt_flags = ort->rt_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) rt->rt_type = ort->rt_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) rt->rt_uses_gateway = ort->rt_uses_gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) rt->rt_gw_family = ort->rt_gw_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) if (rt->rt_gw_family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) rt->rt_gw4 = ort->rt_gw4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) else if (rt->rt_gw_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) rt->rt_gw6 = ort->rt_gw6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) INIT_LIST_HEAD(&rt->rt_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) dst_release(dst_orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) return rt ? &rt->dst : ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) const struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) struct rtable *rt = __ip_route_output_key(net, flp4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) if (flp4->flowi4_proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) flp4->flowi4_oif = rt->dst.dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) rt = (struct rtable *)xfrm_lookup_route(net, &rt->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) flowi4_to_flowi(flp4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) sk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) EXPORT_SYMBOL_GPL(ip_route_output_flow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) struct rtable *ip_route_output_tunnel(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) struct net *net, __be32 *saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) const struct ip_tunnel_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) u8 protocol, bool use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) #ifdef CONFIG_DST_CACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) struct dst_cache *dst_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) struct rtable *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) __u8 tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) #ifdef CONFIG_DST_CACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) dst_cache = (struct dst_cache *)&info->dst_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) if (use_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) rt = dst_cache_get_ip4(dst_cache, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) memset(&fl4, 0, sizeof(fl4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) fl4.flowi4_mark = skb->mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) fl4.flowi4_proto = protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) fl4.daddr = info->key.u.ipv4.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) fl4.saddr = info->key.u.ipv4.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) tos = info->key.tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) fl4.flowi4_tos = RT_TOS(tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) rt = ip_route_output_key(net, &fl4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) netdev_dbg(dev, "no route to %pI4\n", &fl4.daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) return ERR_PTR(-ENETUNREACH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) if (rt->dst.dev == dev) { /* is this necessary? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) netdev_dbg(dev, "circular route to %pI4\n", &fl4.daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) return ERR_PTR(-ELOOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) #ifdef CONFIG_DST_CACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) if (use_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) *saddr = fl4.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) EXPORT_SYMBOL_GPL(ip_route_output_tunnel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) /* called with rcu_read_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) struct rtable *rt, u32 table_id, struct flowi4 *fl4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) struct sk_buff *skb, u32 portid, u32 seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) struct rtmsg *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) unsigned long expires = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) u32 error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) u32 metrics[RTAX_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*r), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) r = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) r->rtm_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) r->rtm_dst_len = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) r->rtm_src_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) r->rtm_tos = fl4 ? fl4->flowi4_tos : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) r->rtm_table = table_id < 256 ? table_id : RT_TABLE_COMPAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) if (nla_put_u32(skb, RTA_TABLE, table_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) r->rtm_type = rt->rt_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) r->rtm_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) r->rtm_protocol = RTPROT_UNSPEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) if (rt->rt_flags & RTCF_NOTIFY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) r->rtm_flags |= RTM_F_NOTIFY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) if (IPCB(skb)->flags & IPSKB_DOREDIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) r->rtm_flags |= RTCF_DOREDIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) if (nla_put_in_addr(skb, RTA_DST, dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) if (src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) r->rtm_src_len = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) if (nla_put_in_addr(skb, RTA_SRC, src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) if (rt->dst.dev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) if (rt->dst.tclassid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) if (fl4 && !rt_is_input_route(rt) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) fl4->saddr != src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) if (rt->rt_uses_gateway) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) if (rt->rt_gw_family == AF_INET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gw4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) } else if (rt->rt_gw_family == AF_INET6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) int alen = sizeof(struct in6_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) struct rtvia *via;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) nla = nla_reserve(skb, RTA_VIA, alen + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) via = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) via->rtvia_family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) memcpy(via->rtvia_addr, &rt->rt_gw6, alen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) expires = rt->dst.expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) if (expires) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) unsigned long now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) if (time_before(now, expires))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) expires -= now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) expires = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) if (rt->rt_pmtu && expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) metrics[RTAX_MTU - 1] = rt->rt_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) if (rt->rt_mtu_locked && expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) metrics[RTAX_LOCK - 1] |= BIT(RTAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) if (rtnetlink_put_metrics(skb, metrics) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) if (fl4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) if (fl4->flowi4_mark &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) if (!uid_eq(fl4->flowi4_uid, INVALID_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) nla_put_u32(skb, RTA_UID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) from_kuid_munged(current_user_ns(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) fl4->flowi4_uid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) if (rt_is_input_route(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) #ifdef CONFIG_IP_MROUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) if (ipv4_is_multicast(dst) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) !ipv4_is_local_multicast(dst) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) IPV4_DEVCONF_ALL(net, MC_FORWARDING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) int err = ipmr_get_route(net, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) fl4->saddr, fl4->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) r, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) if (err <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) if (nla_put_u32(skb, RTA_IIF, fl4->flowi4_iif))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) error = rt->dst.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) static int fnhe_dump_bucket(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) struct netlink_callback *cb, u32 table_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) struct fnhe_hash_bucket *bucket, int genid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) int *fa_index, int fa_start, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) for (i = 0; i < FNHE_HASH_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) struct fib_nh_exception *fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) for (fnhe = rcu_dereference(bucket[i].chain); fnhe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) fnhe = rcu_dereference(fnhe->fnhe_next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) if (*fa_index < fa_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) if (fnhe->fnhe_genid != genid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) if (fnhe->fnhe_expires &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) time_after(jiffies, fnhe->fnhe_expires))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) rt = rcu_dereference(fnhe->fnhe_rth_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) rt = rcu_dereference(fnhe->fnhe_rth_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) err = rt_fill_info(net, fnhe->fnhe_daddr, 0, rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) table_id, NULL, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) cb->nlh->nlmsg_seq, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) (*fa_index)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) u32 table_id, struct fib_info *fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) int *fa_index, int fa_start, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) struct net *net = sock_net(cb->skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) int nhsel, genid = fnhe_genid(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) struct fib_nh_common *nhc = fib_info_nhc(fi, nhsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) struct fnhe_hash_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) if (nhc->nhc_flags & RTNH_F_DEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) bucket = rcu_dereference(nhc->nhc_exceptions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) if (bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) err = fnhe_dump_bucket(net, skb, cb, table_id, bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) genid, fa_index, fa_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) static struct sk_buff *inet_rtm_getroute_build_skb(__be32 src, __be32 dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) u8 ip_proto, __be16 sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) __be16 dport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) /* Reserve room for dummy headers, this skb can pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) * through good chunk of routing engine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) skb->protocol = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) iph = skb_put(skb, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) iph->protocol = ip_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) iph->saddr = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) iph->daddr = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) iph->version = 0x4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) iph->frag_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) iph->ihl = 0x5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) skb_set_transport_header(skb, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) switch (iph->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) case IPPROTO_UDP: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) struct udphdr *udph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) udph = skb_put_zero(skb, sizeof(struct udphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) udph->source = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) udph->dest = dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) udph->len = htons(sizeof(struct udphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) udph->check = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) case IPPROTO_TCP: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) struct tcphdr *tcph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) tcph = skb_put_zero(skb, sizeof(struct tcphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) tcph->source = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) tcph->dest = dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) tcph->doff = sizeof(struct tcphdr) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) tcph->rst = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) tcph->check = ~tcp_v4_check(sizeof(struct tcphdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) src, dst, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) case IPPROTO_ICMP: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) struct icmphdr *icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) icmph = skb_put_zero(skb, sizeof(struct icmphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) icmph->type = ICMP_ECHO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) icmph->code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) static int inet_rtm_valid_getroute_req(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) const struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) "ipv4: Invalid header for route get request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) if (!netlink_strict_get_check(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) rtm_ipv4_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) rtm->rtm_table || rtm->rtm_protocol ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) rtm->rtm_scope || rtm->rtm_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for route get request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) if (rtm->rtm_flags & ~(RTM_F_NOTIFY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) RTM_F_LOOKUP_TABLE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) RTM_F_FIB_MATCH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) NL_SET_ERR_MSG(extack, "ipv4: Unsupported rtm_flags for route get request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) rtm_ipv4_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) (tb[RTA_DST] && !rtm->rtm_dst_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) for (i = 0; i <= RTA_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) if (!tb[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) switch (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) case RTA_IIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) case RTA_OIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) case RTA_SRC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) case RTA_DST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) case RTA_IP_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) case RTA_SPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) case RTA_DPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) case RTA_MARK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) case RTA_UID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in route get request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) struct net *net = sock_net(in_skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) struct nlattr *tb[RTA_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) u32 table_id = RT_TABLE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) __be16 sport = 0, dport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) struct fib_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) u8 ip_proto = IPPROTO_UDP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) struct rtable *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) struct flowi4 fl4 = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) __be32 dst = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) __be32 src = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) kuid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) u32 iif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) int mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) err = inet_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) if (tb[RTA_UID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) uid = make_kuid(current_user_ns(), nla_get_u32(tb[RTA_UID]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) uid = (iif ? INVALID_UID : current_uid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) if (tb[RTA_IP_PROTO]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) &ip_proto, AF_INET, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) if (tb[RTA_SPORT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) sport = nla_get_be16(tb[RTA_SPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) if (tb[RTA_DPORT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) dport = nla_get_be16(tb[RTA_DPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) skb = inet_rtm_getroute_build_skb(src, dst, ip_proto, sport, dport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) fl4.daddr = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) fl4.saddr = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) fl4.flowi4_tos = rtm->rtm_tos & IPTOS_RT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) fl4.flowi4_mark = mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) fl4.flowi4_uid = uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) if (sport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) fl4.fl4_sport = sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) if (dport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) fl4.fl4_dport = dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) fl4.flowi4_proto = ip_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) if (iif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) dev = dev_get_by_index_rcu(net, iif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) goto errout_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) fl4.flowi4_iif = iif; /* for rt_fill_info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) skb->mark = mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) err = ip_route_input_rcu(skb, dst, src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) rtm->rtm_tos & IPTOS_RT_MASK, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) if (err == 0 && rt->dst.error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) err = -rt->dst.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) fl4.flowi4_iif = LOOPBACK_IFINDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) skb->dev = net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) rt = ip_route_output_key_hash_rcu(net, &fl4, &res, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) err = PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) skb_dst_set(skb, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) goto errout_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) if (rtm->rtm_flags & RTM_F_NOTIFY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) rt->rt_flags |= RTCF_NOTIFY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) if (rtm->rtm_flags & RTM_F_LOOKUP_TABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) table_id = res.table ? res.table->tb_id : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) /* reset skb for netlink reply msg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) skb_trim(skb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) skb_reset_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) struct fib_rt_info fri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) if (!res.fi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) err = fib_props[res.type].error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) goto errout_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) fri.fi = res.fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) fri.tb_id = table_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) fri.dst = res.prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) fri.dst_len = res.prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) fri.tos = fl4.flowi4_tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) fri.type = rt->rt_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) fri.offload = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) fri.trap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) if (res.fa_head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) struct fib_alias *fa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) hlist_for_each_entry_rcu(fa, res.fa_head, fa_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) u8 slen = 32 - fri.dst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) if (fa->fa_slen == slen &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) fa->tb_id == fri.tb_id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) fa->fa_tos == fri.tos &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) fa->fa_info == res.fi &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) fa->fa_type == fri.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) fri.offload = fa->offload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) fri.trap = fa->trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) err = fib_dump_info(skb, NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) nlh->nlmsg_seq, RTM_NEWROUTE, &fri, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) err = rt_fill_info(net, dst, src, rt, table_id, &fl4, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) nlh->nlmsg_seq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) goto errout_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) errout_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) errout_rcu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) goto errout_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) void ip_rt_multicast_event(struct in_device *in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) rt_cache_flush(dev_net(in_dev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) static int ip_rt_gc_interval __read_mostly = 60 * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) static int ip_rt_gc_min_interval __read_mostly = HZ / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) static int ip_rt_gc_elasticity __read_mostly = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) static int ip_min_valid_pmtu __read_mostly = IPV4_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) static int ipv4_sysctl_rtcache_flush(struct ctl_table *__ctl, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) struct net *net = (struct net *)__ctl->extra1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) if (write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) rt_cache_flush(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) fnhe_genid_bump(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) static struct ctl_table ipv4_route_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) .procname = "gc_thresh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) .data = &ipv4_dst_ops.gc_thresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) .procname = "max_size",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) .data = &ip_rt_max_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) /* Deprecated. Use gc_min_interval_ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) .procname = "gc_min_interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) .data = &ip_rt_gc_min_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) .procname = "gc_min_interval_ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) .data = &ip_rt_gc_min_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) .proc_handler = proc_dointvec_ms_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) .procname = "gc_timeout",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) .data = &ip_rt_gc_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) .procname = "gc_interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) .data = &ip_rt_gc_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) .procname = "redirect_load",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) .data = &ip_rt_redirect_load,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) .procname = "redirect_number",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) .data = &ip_rt_redirect_number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) .procname = "redirect_silence",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) .data = &ip_rt_redirect_silence,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) .procname = "error_cost",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) .data = &ip_rt_error_cost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) .procname = "error_burst",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) .data = &ip_rt_error_burst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) .procname = "gc_elasticity",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) .data = &ip_rt_gc_elasticity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) .procname = "mtu_expires",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) .data = &ip_rt_mtu_expires,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) .procname = "min_pmtu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) .data = &ip_rt_min_pmtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) .proc_handler = proc_dointvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) .extra1 = &ip_min_valid_pmtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) .procname = "min_adv_mss",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) .data = &ip_rt_min_advmss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) static const char ipv4_route_flush_procname[] = "flush";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) static struct ctl_table ipv4_route_flush_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) .procname = ipv4_route_flush_procname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) .mode = 0200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) .proc_handler = ipv4_sysctl_rtcache_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) static __net_init int sysctl_route_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) struct ctl_table *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) tbl = ipv4_route_flush_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) if (!net_eq(net, &init_net)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) if (!tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) goto err_dup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) /* Don't export non-whitelisted sysctls to unprivileged users */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) if (net->user_ns != &init_user_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) if (tbl[0].procname != ipv4_route_flush_procname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) tbl[0].procname = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) tbl[0].extra1 = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) if (!net->ipv4.route_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) goto err_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) err_reg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) if (tbl != ipv4_route_flush_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) kfree(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) err_dup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) static __net_exit void sysctl_route_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) struct ctl_table *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) tbl = net->ipv4.route_hdr->ctl_table_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) unregister_net_sysctl_table(net->ipv4.route_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) BUG_ON(tbl == ipv4_route_flush_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) kfree(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) static __net_initdata struct pernet_operations sysctl_route_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) .init = sysctl_route_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) .exit = sysctl_route_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) static __net_init int rt_genid_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) atomic_set(&net->ipv4.rt_genid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) atomic_set(&net->fnhe_genid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) atomic_set(&net->ipv4.dev_addr_genid, get_random_int());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) static __net_initdata struct pernet_operations rt_genid_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) .init = rt_genid_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) static int __net_init ipv4_inetpeer_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) if (!bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) inet_peer_base_init(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) net->ipv4.peers = bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) static void __net_exit ipv4_inetpeer_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) struct inet_peer_base *bp = net->ipv4.peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) net->ipv4.peers = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) inetpeer_invalidate_tree(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) kfree(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) static __net_initdata struct pernet_operations ipv4_inetpeer_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) .init = ipv4_inetpeer_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) .exit = ipv4_inetpeer_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) #endif /* CONFIG_IP_ROUTE_CLASSID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) int __init ip_rt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) void *idents_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) /* For modern hosts, this will use 2 MB of memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) idents_hash = alloc_large_system_hash("IP idents",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) sizeof(*ip_idents) + sizeof(*ip_tstamps),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 16, /* one bucket per 64 KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) HASH_ZERO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) &ip_idents_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 2048,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 256*1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) ip_idents = idents_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) prandom_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) INIT_LIST_HEAD(&ul->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) spin_lock_init(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) if (!ip_rt_acct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) panic("IP: failed to allocate ip_rt_acct\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) ipv4_dst_ops.kmem_cachep =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) if (dst_entries_init(&ipv4_dst_ops) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) panic("IP: failed to allocate ipv4_dst_ops counter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) ipv4_dst_ops.gc_thresh = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) ip_rt_max_size = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) devinet_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) ip_fib_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) if (ip_rt_proc_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) pr_err("Unable to create route proc files\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) #ifdef CONFIG_XFRM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) xfrm_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) xfrm4_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) RTNL_FLAG_DOIT_UNLOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) register_pernet_subsys(&sysctl_route_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) register_pernet_subsys(&rt_genid_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) register_pernet_subsys(&ipv4_inetpeer_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) * We really need to sanitize the damn ipv4 init order, then all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) * this nonsense will go away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) void __init ip_static_sysctl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) #endif