⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arp.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* linux/net/ipv4/arp.c * * Version:	$Id: arp.c,v 1.99 2001/08/30 22:55:42 davem Exp $ * * Copyright (C) 1994 by Florian  La Roche * * This module implements the Address Resolution Protocol ARP (RFC 826), * which is used to convert IP addresses (or in the future maybe other * high-level addresses) into a low-level hardware address (like an Ethernet * address). * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Fixes: *		Alan Cox	:	Removed the Ethernet assumptions in *					Florian's code *		Alan Cox	:	Fixed some small errors in the ARP *					logic *		Alan Cox	:	Allow >4K in /proc *		Alan Cox	:	Make ARP add its own protocol entry *		Ross Martin     :       Rewrote arp_rcv() and arp_get_info() *		Stephen Henson	:	Add AX25 support to arp_get_info() *		Alan Cox	:	Drop data when a device is downed. *		Alan Cox	:	Use init_timer(). *		Alan Cox	:	Double lock fixes. *		Martin Seine	:	Move the arphdr structure *					to if_arp.h for compatibility. *					with BSD based programs. *		Andrew Tridgell :       Added ARP netmask code and *					re-arranged proxy handling. *		Alan Cox	:	Changed to use notifiers. *		Niibe Yutaka	:	Reply for this device or proxies only. *		Alan Cox	:	Don't proxy across hardware types! *		Jonathan Naylor :	Added support for NET/ROM. *		Mike Shaver     :       RFC1122 checks. *		Jonathan Naylor :	Only lookup the hardware address for *					the correct hardware type. *		Germano Caronni	:	Assorted subtle races. *		Craig Schlenter :	Don't modify permanent entry *					during arp_rcv. *		Russ Nelson	:	Tidied up a few bits. *		Alexey Kuznetsov:	Major changes to caching and behaviour, *					eg intelligent arp probing and *					generation *					of host down events. *		Alan Cox	:	Missing unlock in device events. *		Eckes		:	ARP ioctl control errors. *		Alexey Kuznetsov:	Arp free fix. *		Manuel Rodriguez:	Gratuitous ARP. *              Jonathan Layes  :       Added arpd support through kerneld *                                      message queue (960314) *		Mike Shaver	:	/proc/sys/net/ipv4/arp_* support *		Mike McLagan    :	Routing by source *		Stuart Cheshire	:	Metricom and grat arp fixes *					*** FOR 2.1 clean this up *** *		Lawrence V. Stefani: (08/12/96) Added FDDI support. *		Alan Cox 	:	Took the AP1000 nasty FDDI hack and *					folded into the mainstream FDDI code. *					Ack spit, Linus how did you allow that *					one in... *		Jes Sorensen	:	Make FDDI work again in 2.1.x and *					clean up the APFDDI & gen. FDDI bits. *		Alexey Kuznetsov:	new arp state machine; *					now it is in net/core/neighbour.c. *		Krzysztof Halasa:	Added Frame Relay ARP support. *		Arnaldo C. Melo :	convert /proc/net/arp to seq_file *		Shmulik Hen:		Split arp_send to arp_create and *					arp_xmit so intermediate drivers like *					bonding can change the skb before *					sending (e.g. insert 8021q tag). *		Harald Welte	:	convert to make use of jenkins hash */#include <linux/module.h>#include <linux/types.h>#include <linux/string.h>#include <linux/kernel.h>#include <linux/capability.h>#include <linux/socket.h>#include <linux/sockios.h>#include <linux/errno.h>#include <linux/in.h>#include <linux/mm.h>#include <linux/inet.h>#include <linux/inetdevice.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/fddidevice.h>#include <linux/if_arp.h>#include <linux/trdevice.h>#include <linux/skbuff.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/stat.h>#include <linux/init.h>#include <linux/net.h>#include <linux/rcupdate.h>#include <linux/jhash.h>#ifdef CONFIG_SYSCTL#include <linux/sysctl.h>#endif#include <net/net_namespace.h>#include <net/ip.h>#include <net/icmp.h>#include <net/route.h>#include <net/protocol.h>#include <net/tcp.h>#include <net/sock.h>#include <net/arp.h>#include <net/ax25.h>#include <net/netrom.h>#if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE)#include <net/atmclip.h>struct neigh_table *clip_tbl_hook;#endif#include <asm/system.h>#include <asm/uaccess.h>#include <linux/netfilter_arp.h>/* *	Interface to generic neighbour cache. */static u32 arp_hash(const void *pkey, const struct net_device *dev);static int arp_constructor(struct neighbour *neigh);static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);static void parp_redo(struct sk_buff *skb);static struct neigh_ops arp_generic_ops = {	.family =		AF_INET,	.solicit =		arp_solicit,	.error_report =		arp_error_report,	.output =		neigh_resolve_output,	.connected_output =	neigh_connected_output,	.hh_output =		dev_queue_xmit,	.queue_xmit =		dev_queue_xmit,};static struct neigh_ops arp_hh_ops = {	.family =		AF_INET,	.solicit =		arp_solicit,	.error_report =		arp_error_report,	.output =		neigh_resolve_output,	.connected_output =	neigh_resolve_output,	.hh_output =		dev_queue_xmit,	.queue_xmit =		dev_queue_xmit,};static struct neigh_ops arp_direct_ops = {	.family =		AF_INET,	.output =		dev_queue_xmit,	.connected_output =	dev_queue_xmit,	.hh_output =		dev_queue_xmit,	.queue_xmit =		dev_queue_xmit,};struct neigh_ops arp_broken_ops = {	.family =		AF_INET,	.solicit =		arp_solicit,	.error_report =		arp_error_report,	.output =		neigh_compat_output,	.connected_output =	neigh_compat_output,	.hh_output =		dev_queue_xmit,	.queue_xmit =		dev_queue_xmit,};struct neigh_table arp_tbl = {	.family =	AF_INET,	.entry_size =	sizeof(struct neighbour) + 4,	.key_len =	4,	.hash =		arp_hash,	.constructor =	arp_constructor,	.proxy_redo =	parp_redo,	.id =		"arp_cache",	.parms = {		.tbl =			&arp_tbl,		.base_reachable_time =	30 * HZ,		.retrans_time =	1 * HZ,		.gc_staletime =	60 * HZ,		.reachable_time =		30 * HZ,		.delay_probe_time =	5 * HZ,		.queue_len =		3,		.ucast_probes =	3,		.mcast_probes =	3,		.anycast_delay =	1 * HZ,		.proxy_delay =		(8 * HZ) / 10,		.proxy_qlen =		64,		.locktime =		1 * HZ,	},	.gc_interval =	30 * HZ,	.gc_thresh1 =	128,	.gc_thresh2 =	512,	.gc_thresh3 =	1024,};int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir){	switch (dev->type) {	case ARPHRD_ETHER:	case ARPHRD_FDDI:	case ARPHRD_IEEE802:		ip_eth_mc_map(addr, haddr);		return 0;	case ARPHRD_IEEE802_TR:		ip_tr_mc_map(addr, haddr);		return 0;	case ARPHRD_INFINIBAND:		ip_ib_mc_map(addr, haddr);		return 0;	default:		if (dir) {			memcpy(haddr, dev->broadcast, dev->addr_len);			return 0;		}	}	return -EINVAL;}static u32 arp_hash(const void *pkey, const struct net_device *dev){	return jhash_2words(*(u32 *)pkey, dev->ifindex, arp_tbl.hash_rnd);}static int arp_constructor(struct neighbour *neigh){	__be32 addr = *(__be32*)neigh->primary_key;	struct net_device *dev = neigh->dev;	struct in_device *in_dev;	struct neigh_parms *parms;	neigh->type = inet_addr_type(addr);	rcu_read_lock();	in_dev = __in_dev_get_rcu(dev);	if (in_dev == NULL) {		rcu_read_unlock();		return -EINVAL;	}	parms = in_dev->arp_parms;	__neigh_parms_put(neigh->parms);	neigh->parms = neigh_parms_clone(parms);	rcu_read_unlock();	if (!dev->header_ops) {		neigh->nud_state = NUD_NOARP;		neigh->ops = &arp_direct_ops;		neigh->output = neigh->ops->queue_xmit;	} else {		/* Good devices (checked by reading texts, but only Ethernet is		   tested)		   ARPHRD_ETHER: (ethernet, apfddi)		   ARPHRD_FDDI: (fddi)		   ARPHRD_IEEE802: (tr)		   ARPHRD_METRICOM: (strip)		   ARPHRD_ARCNET:		   etc. etc. etc.		   ARPHRD_IPDDP will also work, if author repairs it.		   I did not it, because this driver does not work even		   in old paradigm.		 */#if 1		/* So... these "amateur" devices are hopeless.		   The only thing, that I can say now:		   It is very sad that we need to keep ugly obsolete		   code to make them happy.		   They should be moved to more reasonable state, now		   they use rebuild_header INSTEAD OF hard_start_xmit!!!		   Besides that, they are sort of out of date		   (a lot of redundant clones/copies, useless in 2.1),		   I wonder why people believe that they work.		 */		switch (dev->type) {		default:			break;		case ARPHRD_ROSE:#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)		case ARPHRD_AX25:#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)		case ARPHRD_NETROM:#endif			neigh->ops = &arp_broken_ops;			neigh->output = neigh->ops->output;			return 0;#endif		;}#endif		if (neigh->type == RTN_MULTICAST) {			neigh->nud_state = NUD_NOARP;			arp_mc_map(addr, neigh->ha, dev, 1);		} else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {			neigh->nud_state = NUD_NOARP;			memcpy(neigh->ha, dev->dev_addr, dev->addr_len);		} else if (neigh->type == RTN_BROADCAST || dev->flags&IFF_POINTOPOINT) {			neigh->nud_state = NUD_NOARP;			memcpy(neigh->ha, dev->broadcast, dev->addr_len);		}		if (dev->header_ops->cache)			neigh->ops = &arp_hh_ops;		else			neigh->ops = &arp_generic_ops;		if (neigh->nud_state&NUD_VALID)			neigh->output = neigh->ops->connected_output;		else			neigh->output = neigh->ops->output;	}	return 0;}static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb){	dst_link_failure(skb);	kfree_skb(skb);}static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb){	__be32 saddr = 0;	u8  *dst_ha = NULL;	struct net_device *dev = neigh->dev;	__be32 target = *(__be32*)neigh->primary_key;	int probes = atomic_read(&neigh->probes);	struct in_device *in_dev = in_dev_get(dev);	if (!in_dev)		return;	switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {	default:	case 0:		/* By default announce any local IP */		if (skb && inet_addr_type(ip_hdr(skb)->saddr) == RTN_LOCAL)			saddr = ip_hdr(skb)->saddr;		break;	case 1:		/* Restrict announcements of saddr in same subnet */		if (!skb)			break;		saddr = ip_hdr(skb)->saddr;		if (inet_addr_type(saddr) == RTN_LOCAL) {			/* saddr should be known to target */			if (inet_addr_onlink(in_dev, target, saddr))				break;		}		saddr = 0;		break;	case 2:		/* Avoid secondary IPs, get a primary/preferred one */		break;	}	if (in_dev)		in_dev_put(in_dev);	if (!saddr)		saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);	if ((probes -= neigh->parms->ucast_probes) < 0) {		if (!(neigh->nud_state&NUD_VALID))			printk(KERN_DEBUG "trying to ucast probe in NUD_INVALID\n");		dst_ha = neigh->ha;		read_lock_bh(&neigh->lock);	} else if ((probes -= neigh->parms->app_probes) < 0) {#ifdef CONFIG_ARPD		neigh_app_ns(neigh);#endif		return;	}	arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,		 dst_ha, dev->dev_addr, NULL);	if (dst_ha)		read_unlock_bh(&neigh->lock);}static int arp_ignore(struct in_device *in_dev, struct net_device *dev,		      __be32 sip, __be32 tip){	int scope;	switch (IN_DEV_ARP_IGNORE(in_dev)) {	case 0:	/* Reply, the tip is already validated */		return 0;	case 1:	/* Reply only if tip is configured on the incoming interface */		sip = 0;		scope = RT_SCOPE_HOST;		break;	case 2:	/*		 * Reply only if tip is configured on the incoming interface		 * and is in same subnet as sip		 */		scope = RT_SCOPE_HOST;		break;	case 3:	/* Do not reply for scope host addresses */		sip = 0;		scope = RT_SCOPE_LINK;		dev = NULL;		break;	case 4:	/* Reserved */	case 5:	case 6:	case 7:		return 0;	case 8:	/* Do not reply */		return 1;	default:		return 0;	}	return !inet_confirm_addr(dev, sip, tip, scope);}static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev){	struct flowi fl = { .nl_u = { .ip4_u = { .daddr = sip,						 .saddr = tip } } };	struct rtable *rt;	int flag = 0;	/*unsigned long now; */	if (ip_route_output_key(&rt, &fl) < 0)		return 1;	if (rt->u.dst.dev != dev) {		NET_INC_STATS_BH(LINUX_MIB_ARPFILTER);		flag = 1;	}	ip_rt_put(rt);	return flag;}/* OBSOLETE FUNCTIONS *//* *	Find an arp mapping in the cache. If not found, post a request. * *	It is very UGLY routine: it DOES NOT use skb->dst->neighbour, *	even if it exists. It is supposed that skb->dev was mangled *	by a virtual device (eql, shaper). Nobody but broken devices *	is allowed to use this function, it is scheduled to be removed. --ANK */static int arp_set_predefined(int addr_hint, unsigned char * haddr, __be32 paddr, struct net_device * dev){	switch (addr_hint) {	case RTN_LOCAL:		printk(KERN_DEBUG "ARP: arp called for own IP address\n");		memcpy(haddr, dev->dev_addr, dev->addr_len);		return 1;	case RTN_MULTICAST:		arp_mc_map(paddr, haddr, dev, 1);		return 1;	case RTN_BROADCAST:		memcpy(haddr, dev->broadcast, dev->addr_len);		return 1;	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -