Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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) /* linux/net/ipv4/arp.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 1994 by Florian  La Roche
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * This module implements the Address Resolution Protocol ARP (RFC 826),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * which is used to convert IP addresses (or in the future maybe other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * high-level addresses) into a low-level hardware address (like an Ethernet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * address).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * Fixes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *		Alan Cox	:	Removed the Ethernet assumptions in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *					Florian's code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *		Alan Cox	:	Fixed some small errors in the ARP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *					logic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *		Alan Cox	:	Allow >4K in /proc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *		Alan Cox	:	Make ARP add its own protocol entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *		Ross Martin     :       Rewrote arp_rcv() and arp_get_info()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *		Stephen Henson	:	Add AX25 support to arp_get_info()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *		Alan Cox	:	Drop data when a device is downed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *		Alan Cox	:	Use init_timer().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *		Alan Cox	:	Double lock fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *		Martin Seine	:	Move the arphdr structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *					to if_arp.h for compatibility.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *					with BSD based programs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *		Andrew Tridgell :       Added ARP netmask code and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *					re-arranged proxy handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *		Alan Cox	:	Changed to use notifiers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  *		Niibe Yutaka	:	Reply for this device or proxies only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *		Alan Cox	:	Don't proxy across hardware types!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  *		Jonathan Naylor :	Added support for NET/ROM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  *		Mike Shaver     :       RFC1122 checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *		Jonathan Naylor :	Only lookup the hardware address for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  *					the correct hardware type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *		Germano Caronni	:	Assorted subtle races.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *		Craig Schlenter :	Don't modify permanent entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *					during arp_rcv.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *		Russ Nelson	:	Tidied up a few bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  *		Alexey Kuznetsov:	Major changes to caching and behaviour,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *					eg intelligent arp probing and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  *					generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  *					of host down events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  *		Alan Cox	:	Missing unlock in device events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *		Eckes		:	ARP ioctl control errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  *		Alexey Kuznetsov:	Arp free fix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  *		Manuel Rodriguez:	Gratuitous ARP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  *              Jonathan Layes  :       Added arpd support through kerneld
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  *                                      message queue (960314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  *		Mike Shaver	:	/proc/sys/net/ipv4/arp_* support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  *		Mike McLagan    :	Routing by source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  *		Stuart Cheshire	:	Metricom and grat arp fixes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  *					*** FOR 2.1 clean this up ***
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  *		Lawrence V. Stefani: (08/12/96) Added FDDI support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  *		Alan Cox	:	Took the AP1000 nasty FDDI hack and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  *					folded into the mainstream FDDI code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  *					Ack spit, Linus how did you allow that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  *					one in...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  *		Jes Sorensen	:	Make FDDI work again in 2.1.x and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  *					clean up the APFDDI & gen. FDDI bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *		Alexey Kuznetsov:	new arp state machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  *					now it is in net/core/neighbour.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *		Krzysztof Halasa:	Added Frame Relay ARP support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  *		Arnaldo C. Melo :	convert /proc/net/arp to seq_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  *		Shmulik Hen:		Split arp_send to arp_create and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *					arp_xmit so intermediate drivers like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  *					bonding can change the skb before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  *					sending (e.g. insert 8021q tag).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  *		Harald Welte	:	convert to make use of jenkins hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  *		Jesper D. Brouer:       Proxy ARP PVLAN RFC 3069 support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #include <linux/fddidevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) #include <net/ax25.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #include <net/netrom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #include <linux/netfilter_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  *	Interface to generic neighbour cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static bool arp_key_eq(const struct neighbour *n, const void *pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) static int arp_constructor(struct neighbour *neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) static void parp_redo(struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) static int arp_is_multicast(const void *pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) static const struct neigh_ops arp_generic_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	.family =		AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	.solicit =		arp_solicit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	.error_report =		arp_error_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	.output =		neigh_resolve_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	.connected_output =	neigh_connected_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) static const struct neigh_ops arp_hh_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	.family =		AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	.solicit =		arp_solicit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	.error_report =		arp_error_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	.output =		neigh_resolve_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	.connected_output =	neigh_resolve_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) static const struct neigh_ops arp_direct_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	.family =		AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	.output =		neigh_direct_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	.connected_output =	neigh_direct_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) struct neigh_table arp_tbl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	.family		= AF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	.key_len	= 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	.protocol	= cpu_to_be16(ETH_P_IP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	.hash		= arp_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	.key_eq		= arp_key_eq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	.constructor	= arp_constructor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	.proxy_redo	= parp_redo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	.is_multicast	= arp_is_multicast,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	.id		= "arp_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	.parms		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		.tbl			= &arp_tbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		.reachable_time		= 30 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		.data	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			[NEIGH_VAR_MCAST_PROBES] = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			[NEIGH_VAR_UCAST_PROBES] = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			[NEIGH_VAR_RETRANS_TIME] = 1 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 			[NEIGH_VAR_BASE_REACHABLE_TIME] = 30 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			[NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			[NEIGH_VAR_GC_STALETIME] = 60 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			[NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 			[NEIGH_VAR_PROXY_QLEN] = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			[NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			[NEIGH_VAR_PROXY_DELAY]	= (8 * HZ) / 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			[NEIGH_VAR_LOCKTIME] = 1 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	.gc_interval	= 30 * HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	.gc_thresh1	= 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	.gc_thresh2	= 512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	.gc_thresh3	= 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) EXPORT_SYMBOL(arp_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	switch (dev->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	case ARPHRD_ETHER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	case ARPHRD_FDDI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	case ARPHRD_IEEE802:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		ip_eth_mc_map(addr, haddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	case ARPHRD_INFINIBAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		ip_ib_mc_map(addr, dev->broadcast, haddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	case ARPHRD_IPGRE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		ip_ipgre_mc_map(addr, dev->broadcast, haddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		if (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			memcpy(haddr, dev->broadcast, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	return -EINVAL;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) static u32 arp_hash(const void *pkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		    const struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		    __u32 *hash_rnd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	return arp_hashfn(pkey, dev, hash_rnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) static bool arp_key_eq(const struct neighbour *neigh, const void *pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	return neigh_key_eq32(neigh, pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) static int arp_constructor(struct neighbour *neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	__be32 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	struct net_device *dev = neigh->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct neigh_parms *parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	u32 inaddr_any = INADDR_ANY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		memcpy(neigh->primary_key, &inaddr_any, arp_tbl.key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	addr = *(__be32 *)neigh->primary_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	if (!in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		return -EINVAL;
^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) 	neigh->type = inet_addr_type_dev_table(dev_net(dev), dev, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	parms = in_dev->arp_parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	__neigh_parms_put(neigh->parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	neigh->parms = neigh_parms_clone(parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (!dev->header_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		neigh->nud_state = NUD_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		neigh->ops = &arp_direct_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		neigh->output = neigh_direct_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		/* Good devices (checked by reading texts, but only Ethernet is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		   tested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		   ARPHRD_ETHER: (ethernet, apfddi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		   ARPHRD_FDDI: (fddi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		   ARPHRD_IEEE802: (tr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		   ARPHRD_METRICOM: (strip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		   ARPHRD_ARCNET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		   etc. etc. etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		   ARPHRD_IPDDP will also work, if author repairs it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		   I did not it, because this driver does not work even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		   in old paradigm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		if (neigh->type == RTN_MULTICAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			neigh->nud_state = NUD_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 			arp_mc_map(addr, neigh->ha, dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		} else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			neigh->nud_state = NUD_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		} else if (neigh->type == RTN_BROADCAST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 			   (dev->flags & IFF_POINTOPOINT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			neigh->nud_state = NUD_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			memcpy(neigh->ha, dev->broadcast, dev->addr_len);
^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) 		if (dev->header_ops->cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			neigh->ops = &arp_hh_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			neigh->ops = &arp_generic_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		if (neigh->nud_state & NUD_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 			neigh->output = neigh->ops->connected_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			neigh->output = neigh->ops->output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	dst_link_failure(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) /* Create and send an arp packet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) static void arp_send_dst(int type, int ptype, __be32 dest_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			 struct net_device *dev, __be32 src_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			 const unsigned char *dest_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			 const unsigned char *src_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			 const unsigned char *target_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			 struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	/* arp on this interface. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	if (dev->flags & IFF_NOARP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	skb = arp_create(type, ptype, dest_ip, dev, src_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 			 dest_hw, src_hw, target_hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	skb_dst_set(skb, dst_clone(dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	arp_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) void arp_send(int type, int ptype, __be32 dest_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	      struct net_device *dev, __be32 src_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	      const unsigned char *dest_hw, const unsigned char *src_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	      const unsigned char *target_hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	arp_send_dst(type, ptype, dest_ip, dev, src_ip, dest_hw, src_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		     target_hw, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) EXPORT_SYMBOL(arp_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	__be32 saddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	u8 dst_ha[MAX_ADDR_LEN], *dst_hw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	struct net_device *dev = neigh->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	__be32 target = *(__be32 *)neigh->primary_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	int probes = atomic_read(&neigh->probes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	struct dst_entry *dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (!in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	case 0:		/* By default announce any local IP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		if (skb && inet_addr_type_dev_table(dev_net(dev), dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 					  ip_hdr(skb)->saddr) == RTN_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 			saddr = ip_hdr(skb)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	case 1:		/* Restrict announcements of saddr in same subnet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		saddr = ip_hdr(skb)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		if (inet_addr_type_dev_table(dev_net(dev), dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 					     saddr) == RTN_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			/* saddr should be known to target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			if (inet_addr_onlink(in_dev, target, saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		saddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	case 2:		/* Avoid secondary IPs, get a primary/preferred one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	if (!saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	if (probes < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		if (!(neigh->nud_state & NUD_VALID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 			pr_debug("trying to ucast probe in NUD_INVALID\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		neigh_ha_snapshot(dst_ha, neigh, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		dst_hw = dst_ha;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		probes -= NEIGH_VAR(neigh->parms, APP_PROBES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		if (probes < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 			neigh_app_ns(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (skb && !(dev->priv_flags & IFF_XMIT_DST_RELEASE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	arp_send_dst(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		     dst_hw, dev->dev_addr, NULL, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	struct net *net = dev_net(in_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	int scope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	switch (IN_DEV_ARP_IGNORE(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	case 0:	/* Reply, the tip is already validated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	case 1:	/* Reply only if tip is configured on the incoming interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		sip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		scope = RT_SCOPE_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	case 2:	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		 * Reply only if tip is configured on the incoming interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		 * and is in same subnet as sip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		scope = RT_SCOPE_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	case 3:	/* Do not reply for scope host addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		sip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		scope = RT_SCOPE_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		in_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	case 4:	/* Reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	case 6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	case 7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	case 8:	/* Do not reply */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	return !inet_confirm_addr(net, in_dev, sip, tip, scope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	int flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	/*unsigned long now; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	rt = ip_route_output(net, sip, tip, 0, l3mdev_master_ifindex_rcu(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (rt->dst.dev != dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		__NET_INC_STATS(net, LINUX_MIB_ARPFILTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		flag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	return flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * Check if we can use proxy ARP for this path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) static inline int arp_fwd_proxy(struct in_device *in_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 				struct net_device *dev,	struct rtable *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	struct in_device *out_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	int imi, omi = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	if (rt->dst.dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	if (!IN_DEV_PROXY_ARP(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	imi = IN_DEV_MEDIUM_ID(in_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	if (imi == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (imi == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	/* place to check for proxy_arp for routes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	out_dev = __in_dev_get_rcu(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	if (out_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		omi = IN_DEV_MEDIUM_ID(out_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	return omi != imi && omi != -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479)  * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481)  * RFC3069 supports proxy arp replies back to the same interface.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482)  * is done to support (ethernet) switch features, like RFC 3069, where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  * the individual ports are not allowed to communicate with each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * other, BUT they are allowed to talk to the upstream router.  As
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  * described in RFC 3069, it is possible to allow these hosts to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * communicate through the upstream router, by proxy_arp'ing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  *  This technology is known by different names:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  *    In RFC 3069 it is called VLAN Aggregation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  *    Cisco and Allied Telesyn call it Private VLAN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  *    Hewlett-Packard call it Source-Port filtering or port-isolation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  *    Ericsson call it MAC-Forced Forwarding (RFC Draft).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) static inline int arp_fwd_pvlan(struct in_device *in_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 				struct net_device *dev,	struct rtable *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 				__be32 sip, __be32 tip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	/* Private VLAN is only concerned about the same ethernet segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	if (rt->dst.dev != dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	/* Don't reply on self probes (often done by windowz boxes)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	if (sip == tip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516)  *	Interface to link layer: send routine and receive handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  *	Create an arp packet. If dest_hw is not set, we create a broadcast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  *	message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 			   struct net_device *dev, __be32 src_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			   const unsigned char *dest_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			   const unsigned char *src_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			   const unsigned char *target_hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct arphdr *arp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	unsigned char *arp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	int hlen = LL_RESERVED_SPACE(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	int tlen = dev->needed_tailroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	 *	Allocate a buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	skb_reserve(skb, hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	arp = skb_put(skb, arp_hdr_len(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	skb->protocol = htons(ETH_P_ARP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (!src_hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		src_hw = dev->dev_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (!dest_hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		dest_hw = dev->broadcast;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	 *	Fill the device header for the ARP frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	 * Fill out the arp protocol part.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	 * The arp hardware type should match the device type, except for FDDI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	 * which (according to RFC 1390) should always equal 1 (Ethernet).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	 *	Exceptions everywhere. AX.25 uses the AX.25 PID value not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	 *	DIX code for the protocol. Make these device structure fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	switch (dev->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		arp->ar_hrd = htons(dev->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		arp->ar_pro = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) #if IS_ENABLED(CONFIG_AX25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	case ARPHRD_AX25:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		arp->ar_hrd = htons(ARPHRD_AX25);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		arp->ar_pro = htons(AX25_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) #if IS_ENABLED(CONFIG_NETROM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	case ARPHRD_NETROM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		arp->ar_hrd = htons(ARPHRD_NETROM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		arp->ar_pro = htons(AX25_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) #if IS_ENABLED(CONFIG_FDDI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	case ARPHRD_FDDI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		arp->ar_hrd = htons(ARPHRD_ETHER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		arp->ar_pro = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	arp->ar_hln = dev->addr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	arp->ar_pln = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	arp->ar_op = htons(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	arp_ptr = (unsigned char *)(arp + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	memcpy(arp_ptr, src_hw, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	arp_ptr += dev->addr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	memcpy(arp_ptr, &src_ip, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	arp_ptr += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	switch (dev->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) #if IS_ENABLED(CONFIG_FIREWIRE_NET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	case ARPHRD_IEEE1394:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		if (target_hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			memcpy(arp_ptr, target_hw, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			memset(arp_ptr, 0, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		arp_ptr += dev->addr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	memcpy(arp_ptr, &dest_ip, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) EXPORT_SYMBOL(arp_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) static int arp_xmit_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	return dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  *	Send an arp packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) void arp_xmit(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	/* Send it off, maybe filter it using firewalling first.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	NF_HOOK(NFPROTO_ARP, NF_ARP_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		dev_net(skb->dev), NULL, skb, NULL, skb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		arp_xmit_finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) EXPORT_SYMBOL(arp_xmit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) static bool arp_is_garp(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			int *addr_type, __be16 ar_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			__be32 sip, __be32 tip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 			unsigned char *sha, unsigned char *tha)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	bool is_garp = tip == sip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	/* Gratuitous ARP _replies_ also require target hwaddr to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	 * the same as source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	if (is_garp && ar_op == htons(ARPOP_REPLY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		is_garp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 			/* IPv4 over IEEE 1394 doesn't provide target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 			 * hardware address field in its ARP payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			tha &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 			!memcmp(tha, sha, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	if (is_garp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		*addr_type = inet_addr_type_dev_table(net, dev, sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		if (*addr_type != RTN_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			is_garp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	return is_garp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674)  *	Process an arp request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	struct in_device *in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	struct arphdr *arp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	unsigned char *arp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	unsigned char *sha;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	unsigned char *tha = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	__be32 sip, tip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	u16 dev_type = dev->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	struct neighbour *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	struct dst_entry *reply_dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	bool is_garp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	/* arp_rcv below verifies the ARP header and verifies the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * is ARP'able.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	arp = arp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	switch (dev_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		if (arp->ar_pro != htons(ETH_P_IP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		    htons(dev_type) != arp->ar_hrd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	case ARPHRD_ETHER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	case ARPHRD_FDDI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	case ARPHRD_IEEE802:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		 * ETHERNET, and Fibre Channel (which are IEEE 802
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		 * devices, according to RFC 2625) devices will accept ARP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		 * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		 * This is the case also of FDDI, where the RFC 1390 says that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		 * FDDI devices should accept ARP hardware of (1) Ethernet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		 * however, to be more robust, we'll accept both 1 (Ethernet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		 * or 6 (IEEE 802.2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		     arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		    arp->ar_pro != htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	case ARPHRD_AX25:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		if (arp->ar_pro != htons(AX25_P_IP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		    arp->ar_hrd != htons(ARPHRD_AX25))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	case ARPHRD_NETROM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		if (arp->ar_pro != htons(AX25_P_IP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		    arp->ar_hrd != htons(ARPHRD_NETROM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	/* Understand only these message types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (arp->ar_op != htons(ARPOP_REPLY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	    arp->ar_op != htons(ARPOP_REQUEST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744)  *	Extract fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	arp_ptr = (unsigned char *)(arp + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	sha	= arp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	arp_ptr += dev->addr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	memcpy(&sip, arp_ptr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	arp_ptr += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	switch (dev_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) #if IS_ENABLED(CONFIG_FIREWIRE_NET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	case ARPHRD_IEEE1394:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		tha = arp_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		arp_ptr += dev->addr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	memcpy(&tip, arp_ptr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  *	Check for bad requests for 127.x.x.x and requests for multicast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  *	addresses.  If this is one such, delete it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	if (ipv4_is_multicast(tip) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	    (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)   *	For some 802.11 wireless deployments (and possibly other networks),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)   *	there will be an ARP proxy and gratuitous ARP frames are attacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772)   *	and thus should not be accepted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773)   */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	if (sip == tip && IN_DEV_ORCONF(in_dev, DROP_GRATUITOUS_ARP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778)  *     Special case: We must set Frame Relay source Q.922 address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (dev_type == ARPHRD_DLCI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		sha = dev->broadcast;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  *  Process entry.  The idea here is we want to send a reply if it is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  *  request for us or if it is a request for someone else that we hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786)  *  a proxy for.  We want to add an entry to our cache if it is a reply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787)  *  to us or if it is a request for our address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788)  *  (The assumption for this last is that if someone is requesting our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789)  *  address, they are probably intending to talk to us, so it saves time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  *  if we cache their address.  Their address is also probably not in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791)  *  our cache, since ours is not in their cache.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793)  *  Putting this another way, we only care about replies if they are to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794)  *  us, in which case we add them to the cache.  For requests, we care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  *  about those for us and those for our proxies.  We reply to both,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  *  and in the case of requests for us we add the requester to the arp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  *  cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (arp->ar_op == htons(ARPOP_REQUEST) && skb_metadata_dst(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		reply_dst = (struct dst_entry *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			    iptunnel_metadata_reply(skb_metadata_dst(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 						    GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	/* Special case: IPv4 duplicate address detection packet (RFC2131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (sip == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		if (arp->ar_op == htons(ARPOP_REQUEST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		    inet_addr_type_dev_table(net, dev, tip) == RTN_LOCAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		    !arp_ignore(in_dev, sip, tip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 			arp_send_dst(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 				     sha, dev->dev_addr, sha, reply_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		goto out_consume_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (arp->ar_op == htons(ARPOP_REQUEST) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	    ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		addr_type = rt->rt_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (addr_type == RTN_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			int dont_send;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			dont_send = arp_ignore(in_dev, sip, tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			if (!dont_send && IN_DEV_ARPFILTER(in_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 				dont_send = arp_filter(sip, tip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			if (!dont_send) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 				n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 				if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 					arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 						     sip, dev, tip, sha,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 						     dev->dev_addr, sha,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 						     reply_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 					neigh_release(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			goto out_consume_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		} else if (IN_DEV_FORWARD(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			if (addr_type == RTN_UNICAST  &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			    (arp_fwd_proxy(in_dev, dev, rt) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			     arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			     (rt->dst.dev != dev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			      pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 				n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 				if (n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 					neigh_release(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 				if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 				    skb->pkt_type == PACKET_HOST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 				    NEIGH_VAR(in_dev->arp_parms, PROXY_DELAY) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 					arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 						     sip, dev, tip, sha,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 						     dev->dev_addr, sha,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 						     reply_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 					pneigh_enqueue(&arp_tbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 						       in_dev->arp_parms, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 					goto out_free_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 				goto out_consume_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	/* Update our ARP tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	addr_type = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	if (n || IN_DEV_ARP_ACCEPT(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		is_garp = arp_is_garp(net, dev, &addr_type, arp->ar_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 				      sip, tip, sha, tha);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	if (IN_DEV_ARP_ACCEPT(in_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		/* Unsolicited ARP is not accepted by default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		   It is possible, that this option should be enabled for some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		   devices (strip is candidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		if (!n &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		    (is_garp ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		     (arp->ar_op == htons(ARPOP_REPLY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		      (addr_type == RTN_UNICAST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		       (addr_type < 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			/* postpone calculation to as late as possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			inet_addr_type_dev_table(net, dev, sip) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 				RTN_UNICAST)))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		int state = NUD_REACHABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		int override;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		/* If several different ARP replies follows back-to-back,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		   use the FIRST one. It is possible, if several proxy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		   agents are active. Taking the first reply prevents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		   arp trashing and chooses the fastest router.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		override = time_after(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 				      n->updated +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 				      NEIGH_VAR(n->parms, LOCKTIME)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 			   is_garp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		/* Broadcast replies and request packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		   do not assert neighbour reachability.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		if (arp->ar_op != htons(ARPOP_REPLY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		    skb->pkt_type != PACKET_HOST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			state = NUD_STALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		neigh_update(n, sha, state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			     override ? NEIGH_UPDATE_F_OVERRIDE : 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		neigh_release(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) out_consume_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) out_free_dst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	dst_release(reply_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) out_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) static void parp_redo(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	arp_process(dev_net(skb->dev), NULL, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) static int arp_is_multicast(const void *pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	return ipv4_is_multicast(*((__be32 *)pkey));
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  *	Receive an arp request from the device layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		   struct packet_type *pt, struct net_device *orig_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	const struct arphdr *arp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	/* do not tweak dropwatch on an ARP we will ignore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	if (dev->flags & IFF_NOARP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	    skb->pkt_type == PACKET_OTHERHOST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	    skb->pkt_type == PACKET_LOOPBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		goto consumeskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	skb = skb_share_check(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		goto out_of_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	/* ARP header, plus 2 device addresses, plus 2 IP addresses.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	if (!pskb_may_pull(skb, arp_hdr_len(dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		goto freeskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	arp = arp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		goto freeskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	return NF_HOOK(NFPROTO_ARP, NF_ARP_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		       dev_net(dev), NULL, skb, dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		       arp_process);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) consumeskb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) freeskb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) out_of_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981)  *	User level interface (ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985)  *	Set (create) an ARP cache entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	if (__in_dev_get_rtnl(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) static int arp_req_set_public(struct net *net, struct arpreq *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (mask && mask != htonl(0xFFFFFFFF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if (!dev && (r->arp_flags & ATF_COM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 				      r->arp_ha.sa_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		if (!pneigh_lookup(&arp_tbl, net, &ip, dev, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	return arp_req_set_proxy(net, dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) static int arp_req_set(struct net *net, struct arpreq *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		       struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	__be32 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	if (r->arp_flags & ATF_PUBL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		return arp_req_set_public(net, r, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	if (r->arp_flags & ATF_PERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		r->arp_flags |= ATF_COM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	switch (dev->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) #if IS_ENABLED(CONFIG_FDDI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	case ARPHRD_FDDI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		 * According to RFC 1390, FDDI devices should accept ARP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		 * hardware types of 1 (Ethernet).  However, to be more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		 * robust, we'll accept hardware types of either 1 (Ethernet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		 * or 6 (IEEE 802.2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		if (r->arp_ha.sa_family != ARPHRD_FDDI &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		    r->arp_ha.sa_family != ARPHRD_ETHER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		    r->arp_ha.sa_family != ARPHRD_IEEE802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		if (r->arp_ha.sa_family != dev->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	err = PTR_ERR(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	if (!IS_ERR(neigh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		unsigned int state = NUD_STALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		if (r->arp_flags & ATF_PERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			state = NUD_PERMANENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 				   r->arp_ha.sa_data : NULL, state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 				   NEIGH_UPDATE_F_OVERRIDE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 				   NEIGH_UPDATE_F_ADMIN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static unsigned int arp_state_to_flags(struct neighbour *neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (neigh->nud_state&NUD_PERMANENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		return ATF_PERM | ATF_COM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	else if (neigh->nud_state&NUD_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		return ATF_COM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)  *	Get an ARP cache entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static int arp_req_get(struct arpreq *r, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	__be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	int err = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	neigh = neigh_lookup(&arp_tbl, &ip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	if (neigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		if (!(neigh->nud_state & NUD_NOARP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			read_lock_bh(&neigh->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			r->arp_flags = arp_state_to_flags(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 			read_unlock_bh(&neigh->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 			r->arp_ha.sa_family = dev->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			strlcpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static int arp_invalidate(struct net_device *dev, __be32 ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	int err = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	struct neigh_table *tbl = &arp_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	if (neigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		if (neigh->nud_state & ~NUD_NOARP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			err = neigh_update(neigh, NULL, NUD_FAILED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 					   NEIGH_UPDATE_F_OVERRIDE|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 					   NEIGH_UPDATE_F_ADMIN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		write_lock_bh(&tbl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		neigh_remove_one(neigh, tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		write_unlock_bh(&tbl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) static int arp_req_delete_public(struct net *net, struct arpreq *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	__be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	if (mask == htonl(0xFFFFFFFF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		return pneigh_delete(&arp_tbl, net, &ip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	if (mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	return arp_req_set_proxy(net, dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) static int arp_req_delete(struct net *net, struct arpreq *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			  struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	__be32 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	if (r->arp_flags & ATF_PUBL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		return arp_req_delete_public(net, r, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	return arp_invalidate(dev, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)  *	Handle an ARP layer I/O control request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	struct arpreq r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	case SIOCDARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	case SIOCSARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	case SIOCGARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		err = copy_from_user(&r, arg, sizeof(struct arpreq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	if (r.arp_pa.sa_family != AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		return -EPFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	if (!(r.arp_flags & ATF_PUBL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	    (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	if (!(r.arp_flags & ATF_NETMASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 							   htonl(0xFFFFFFFFUL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (r.arp_dev[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		dev = __dev_get_by_name(net, r.arp_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		/* Mmmm... It is wrong... ARPHRD_NETROM==0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		if (!r.arp_ha.sa_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			r.arp_ha.sa_family = dev->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	} else if (cmd == SIOCGARP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	case SIOCDARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		err = arp_req_delete(net, &r, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	case SIOCSARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		err = arp_req_set(net, &r, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	case SIOCGARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		err = arp_req_get(&r, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	if (cmd == SIOCGARP && !err && copy_to_user(arg, &r, sizeof(r)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) static int arp_netdev_event(struct notifier_block *this, unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			    void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	struct netdev_notifier_change_info *change_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	case NETDEV_CHANGEADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		neigh_changeaddr(&arp_tbl, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		rt_cache_flush(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	case NETDEV_CHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		change_info = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		if (change_info->flags_changed & IFF_NOARP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			neigh_changeaddr(&arp_tbl, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		if (!netif_carrier_ok(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 			neigh_carrier_down(&arp_tbl, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) static struct notifier_block arp_netdev_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	.notifier_call = arp_netdev_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) /* Note, that it is not on notifier chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)    It is necessary, that this routine was called after route cache will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)    flushed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) void arp_ifdown(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	neigh_ifdown(&arp_tbl, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  *	Called once on startup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) static struct packet_type arp_packet_type __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	.type =	cpu_to_be16(ETH_P_ARP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	.func =	arp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) static int arp_proc_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) void __init arp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	neigh_table_init(NEIGH_ARP_TABLE, &arp_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	dev_add_pack(&arp_packet_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	arp_proc_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	neigh_sysctl_register(NULL, &arp_tbl.parms, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	register_netdevice_notifier(&arp_netdev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) #if IS_ENABLED(CONFIG_AX25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)  *	ax25 -> ASCII conversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) static void ax2asc2(ax25_address *a, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	char c, *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	for (n = 0, s = buf; n < 6; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		c = (a->ax25_call[n] >> 1) & 0x7F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		if (c != ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			*s++ = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	*s++ = '-';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	n = (a->ax25_call[6] >> 1) & 0x0F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	if (n > 9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		*s++ = '1';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		n -= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	*s++ = n + '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	*s++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	if (*buf == '\0' || *buf == '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		buf[0] = '*';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		buf[1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) #endif /* CONFIG_AX25 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) #define HBUFFERLEN 30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) static void arp_format_neigh_entry(struct seq_file *seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 				   struct neighbour *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	char hbuffer[HBUFFERLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	int k, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	char tbuf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	struct net_device *dev = n->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	int hatype = dev->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	read_lock(&n->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	/* Convert hardware address to XX:XX:XX:XX ... form. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) #if IS_ENABLED(CONFIG_AX25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		ax2asc2((ax25_address *)n->ha, hbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		hbuffer[k++] = hex_asc_hi(n->ha[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		hbuffer[k++] = hex_asc_lo(n->ha[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		hbuffer[k++] = ':';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	if (k != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		--k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	hbuffer[k] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) #if IS_ENABLED(CONFIG_AX25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	sprintf(tbuf, "%pI4", n->primary_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s     *        %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		   tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	read_unlock(&n->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) static void arp_format_pneigh_entry(struct seq_file *seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 				    struct pneigh_entry *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	struct net_device *dev = n->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	int hatype = dev ? dev->type : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	char tbuf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	sprintf(tbuf, "%pI4", n->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%s     *        %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		   tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		   dev ? dev->name : "*");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) static int arp_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	if (v == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		seq_puts(seq, "IP address       HW type     Flags       "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			      "HW address            Mask     Device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		struct neigh_seq_state *state = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		if (state->flags & NEIGH_SEQ_IS_PNEIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			arp_format_pneigh_entry(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			arp_format_neigh_entry(seq, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	/* Don't want to confuse "arp -a" w/ magic entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 * so we tell the generic iterator to skip NUD_NOARP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) static const struct seq_operations arp_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	.start	= arp_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	.next	= neigh_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	.stop	= neigh_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	.show	= arp_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) static int __net_init arp_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	if (!proc_create_net("arp", 0444, net->proc_net, &arp_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 			sizeof(struct neigh_seq_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) static void __net_exit arp_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	remove_proc_entry("arp", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) static struct pernet_operations arp_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	.init = arp_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	.exit = arp_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) static int __init arp_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	return register_pernet_subsys(&arp_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) #else /* CONFIG_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) static int __init arp_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	return 0;
^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) #endif /* CONFIG_PROC_FS */