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

📄 udp.c

📁 嵌入式系统设计与实验教材二源码linux内核移植与编译
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * INET		An implementation of the TCP/IP protocol suite for the LINUX *		operating system.  INET is implemented using the  BSD Socket *		interface as the means of communication with the user level. * *		The User Datagram Protocol (UDP). * * Version:	$Id: udp.c,v 1.100.2.1 2002/01/12 07:39:23 davem Exp $ * * Authors:	Ross Biro, <bir7@leland.Stanford.Edu> *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> *		Arnt Gulbrandsen, <agulbra@nvg.unit.no> *		Alan Cox, <Alan.Cox@linux.org> * * Fixes: *		Alan Cox	:	verify_area() calls *		Alan Cox	: 	stopped close while in use off icmp *					messages. Not a fix but a botch that *					for udp at least is 'valid'. *		Alan Cox	:	Fixed icmp handling properly *		Alan Cox	: 	Correct error for oversized datagrams *		Alan Cox	:	Tidied select() semantics.  *		Alan Cox	:	udp_err() fixed properly, also now  *					select and read wake correctly on errors *		Alan Cox	:	udp_send verify_area moved to avoid mem leak *		Alan Cox	:	UDP can count its memory *		Alan Cox	:	send to an unknown connection causes *					an ECONNREFUSED off the icmp, but *					does NOT close. *		Alan Cox	:	Switched to new sk_buff handlers. No more backlog! *		Alan Cox	:	Using generic datagram code. Even smaller and the PEEK *					bug no longer crashes it. *		Fred Van Kempen	: 	Net2e support for sk->broadcast. *		Alan Cox	:	Uses skb_free_datagram *		Alan Cox	:	Added get/set sockopt support. *		Alan Cox	:	Broadcasting without option set returns EACCES. *		Alan Cox	:	No wakeup calls. Instead we now use the callbacks. *		Alan Cox	:	Use ip_tos and ip_ttl *		Alan Cox	:	SNMP Mibs *		Alan Cox	:	MSG_DONTROUTE, and 0.0.0.0 support. *		Matt Dillon	:	UDP length checks. *		Alan Cox	:	Smarter af_inet used properly. *		Alan Cox	:	Use new kernel side addressing. *		Alan Cox	:	Incorrect return on truncated datagram receive. *	Arnt Gulbrandsen 	:	New udp_send and stuff *		Alan Cox	:	Cache last socket *		Alan Cox	:	Route cache *		Jon Peatfield	:	Minor efficiency fix to sendto(). *		Mike Shaver	:	RFC1122 checks. *		Alan Cox	:	Nonblocking error fix. *	Willy Konynenberg	:	Transparent proxying support. *		Mike McLagan	:	Routing by source *		David S. Miller	:	New socket lookup architecture. *					Last socket cache retained as it *					does have a high hit rate. *		Olaf Kirch	:	Don't linearise iovec on sendmsg. *		Andi Kleen	:	Some cleanups, cache destination entry *					for connect.  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma. *		Melvin Smith	:	Check msg_name not msg_namelen in sendto(), *					return ENOTCONN for unconnected sockets (POSIX) *		Janos Farkas	:	don't deliver multi/broadcasts to a different *					bound-to-device socket * * *		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. */ #include <asm/system.h>#include <asm/uaccess.h>#include <asm/ioctls.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/socket.h>#include <linux/sockios.h>#include <linux/in.h>#include <linux/errno.h>#include <linux/timer.h>#include <linux/mm.h>#include <linux/config.h>#include <linux/inet.h>#include <linux/netdevice.h>#include <net/snmp.h>#include <net/ip.h>#include <net/protocol.h>#include <linux/skbuff.h>#include <net/sock.h>#include <net/udp.h>#include <net/icmp.h>#include <net/route.h>#include <net/inet_common.h>#include <net/checksum.h>/* *	Snmp MIB for the UDP layer */struct udp_mib		udp_statistics[NR_CPUS*2];struct sock *udp_hash[UDP_HTABLE_SIZE];rwlock_t udp_hash_lock = RW_LOCK_UNLOCKED;/* Shared by v4/v6 udp. */int udp_port_rover;static int udp_v4_get_port(struct sock *sk, unsigned short snum){	write_lock_bh(&udp_hash_lock);	if (snum == 0) {		int best_size_so_far, best, result, i;		if (udp_port_rover > sysctl_local_port_range[1] ||		    udp_port_rover < sysctl_local_port_range[0])			udp_port_rover = sysctl_local_port_range[0];		best_size_so_far = 32767;		best = result = udp_port_rover;		for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {			struct sock *sk;			int size;			sk = udp_hash[result & (UDP_HTABLE_SIZE - 1)];			if (!sk) {				if (result > sysctl_local_port_range[1])					result = sysctl_local_port_range[0] +						((result - sysctl_local_port_range[0]) &						 (UDP_HTABLE_SIZE - 1));				goto gotit;			}			size = 0;			do {				if (++size >= best_size_so_far)					goto next;			} while ((sk = sk->next) != NULL);			best_size_so_far = size;			best = result;		next:;		}		result = best;		for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {			if (result > sysctl_local_port_range[1])				result = sysctl_local_port_range[0]					+ ((result - sysctl_local_port_range[0]) &					   (UDP_HTABLE_SIZE - 1));			if (!udp_lport_inuse(result))				break;		}		if (i >= (1 << 16) / UDP_HTABLE_SIZE)			goto fail;gotit:		udp_port_rover = snum = result;	} else {		struct sock *sk2;		for (sk2 = udp_hash[snum & (UDP_HTABLE_SIZE - 1)];		     sk2 != NULL;		     sk2 = sk2->next) {			if (sk2->num == snum &&			    sk2 != sk &&			    sk2->bound_dev_if == sk->bound_dev_if &&			    (!sk2->rcv_saddr ||			     !sk->rcv_saddr ||			     sk2->rcv_saddr == sk->rcv_saddr) &&			    (!sk2->reuse || !sk->reuse))				goto fail;		}	}	sk->num = snum;	if (sk->pprev == NULL) {		struct sock **skp = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];		if ((sk->next = *skp) != NULL)			(*skp)->pprev = &sk->next;		*skp = sk;		sk->pprev = skp;		sock_prot_inc_use(sk->prot);		sock_hold(sk);	}	write_unlock_bh(&udp_hash_lock);	return 0;fail:	write_unlock_bh(&udp_hash_lock);	return 1;}static void udp_v4_hash(struct sock *sk){	BUG();}static void udp_v4_unhash(struct sock *sk){	write_lock_bh(&udp_hash_lock);	if (sk->pprev) {		if (sk->next)			sk->next->pprev = sk->pprev;		*sk->pprev = sk->next;		sk->pprev = NULL;		sk->num = 0;		sock_prot_dec_use(sk->prot);		__sock_put(sk);	}	write_unlock_bh(&udp_hash_lock);}/* UDP is nearly always wildcards out the wazoo, it makes no sense to try * harder than this. -DaveM */struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif){	struct sock *sk, *result = NULL;	unsigned short hnum = ntohs(dport);	int badness = -1;	for(sk = udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]; sk != NULL; sk = sk->next) {		if(sk->num == hnum) {			int score = 0;			if(sk->rcv_saddr) {				if(sk->rcv_saddr != daddr)					continue;				score++;			}			if(sk->daddr) {				if(sk->daddr != saddr)					continue;				score++;			}			if(sk->dport) {				if(sk->dport != sport)					continue;				score++;			}			if(sk->bound_dev_if) {				if(sk->bound_dev_if != dif)					continue;				score++;			}			if(score == 4) {				result = sk;				break;			} else if(score > badness) {				result = sk;				badness = score;			}		}	}	return result;}__inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif){	struct sock *sk;	read_lock(&udp_hash_lock);	sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);	if (sk)		sock_hold(sk);	read_unlock(&udp_hash_lock);	return sk;}static inline struct sock *udp_v4_mcast_next(struct sock *sk,					     u16 loc_port, u32 loc_addr,					     u16 rmt_port, u32 rmt_addr,					     int dif){	struct sock *s = sk;	unsigned short hnum = ntohs(loc_port);	for(; s; s = s->next) {		if ((s->num != hnum)					||		    (s->daddr && s->daddr!=rmt_addr)			||		    (s->dport != rmt_port && s->dport != 0)			||		    (s->rcv_saddr  && s->rcv_saddr != loc_addr)		||		    (s->bound_dev_if && s->bound_dev_if != dif))			continue;		break;  	}  	return s;}/* * This routine is called by the ICMP module when it gets some * sort of error condition.  If err < 0 then the socket should * be closed and the error returned to the user.  If err > 0 * it's just the icmp type << 8 | icmp code.   * Header points to the ip header of the error packet. We move * on past this. Then (as it used to claim before adjustment) * header points to the first 8 bytes of the udp header.  We need * to find the appropriate port. */void udp_err(struct sk_buff *skb, u32 info){	struct iphdr *iph = (struct iphdr*)skb->data;	struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));	int type = skb->h.icmph->type;	int code = skb->h.icmph->code;	struct sock *sk;	int harderr;	int err;	sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);	if (sk == NULL) {		ICMP_INC_STATS_BH(IcmpInErrors);    	  	return;	/* No socket for error */	}	err = 0;	harderr = 0;	switch (type) {	default:	case ICMP_TIME_EXCEEDED:		err = EHOSTUNREACH;		break;	case ICMP_SOURCE_QUENCH:		goto out;	case ICMP_PARAMETERPROB:		err = EPROTO;		harderr = 1;		break;	case ICMP_DEST_UNREACH:		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */			if (sk->protinfo.af_inet.pmtudisc != IP_PMTUDISC_DONT) {				err = EMSGSIZE;				harderr = 1;				break;			}			goto out;		}		err = EHOSTUNREACH;		if (code <= NR_ICMP_UNREACH) {			harderr = icmp_err_convert[code].fatal;			err = icmp_err_convert[code].errno;		}		break;	}	/*	 *      RFC1122: OK.  Passes ICMP errors back to application, as per 	 *	4.1.3.3.	 */	if (!sk->protinfo.af_inet.recverr) {		if (!harderr || sk->state != TCP_ESTABLISHED)			goto out;	} else {		ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));	}	sk->err = err;	sk->error_report(sk);out:	sock_put(sk);}static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base){	return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));}struct udpfakehdr {	struct udphdr uh;	u32 saddr;	u32 daddr;	struct iovec *iov;	u32 wcheck;};/* *	Copy and checksum a UDP packet from user space into a buffer. */ static int udp_getfrag(const void *p, char * to, unsigned int offset, unsigned int fraglen) {	struct udpfakehdr *ufh = (struct udpfakehdr *)p;	if (offset==0) {		if (csum_partial_copy_fromiovecend(to+sizeof(struct udphdr), ufh->iov, offset,						   fraglen-sizeof(struct udphdr), &ufh->wcheck))			return -EFAULT; 		ufh->wcheck = csum_partial((char *)ufh, sizeof(struct udphdr),					   ufh->wcheck);		ufh->uh.check = csum_tcpudp_magic(ufh->saddr, ufh->daddr, 					  ntohs(ufh->uh.len),					  IPPROTO_UDP, ufh->wcheck);		if (ufh->uh.check == 0)			ufh->uh.check = -1;		memcpy(to, ufh, sizeof(struct udphdr));		return 0;	}	if (csum_partial_copy_fromiovecend(to, ufh->iov, offset-sizeof(struct udphdr),					   fraglen, &ufh->wcheck))		return -EFAULT;	return 0;}/* *	Copy a UDP packet from user space into a buffer without checksumming. */ static int udp_getfrag_nosum(const void *p, char * to, unsigned int offset, unsigned int fraglen) {	struct udpfakehdr *ufh = (struct udpfakehdr *)p;	if (offset==0) {		memcpy(to, ufh, sizeof(struct udphdr));		return memcpy_fromiovecend(to+sizeof(struct udphdr), ufh->iov, offset,					   fraglen-sizeof(struct udphdr));	}	return memcpy_fromiovecend(to, ufh->iov, offset-sizeof(struct udphdr),				   fraglen);}int udp_sendmsg(struct sock *sk, struct msghdr *msg, int len){	int ulen = len + sizeof(struct udphdr);	struct ipcm_cookie ipc;	struct udpfakehdr ufh;	struct rtable *rt = NULL;	int free = 0;	int connected = 0;	u32 daddr;	u8  tos;	int err;	/* This check is ONLY to check for arithmetic overflow	   on integer(!) len. Not more! Real check will be made	   in ip_build_xmit --ANK	   BTW socket.c -> af_*.c -> ... make multiple	   invalid conversions size_t -> int. We MUST repair it f.e.	   by replacing all of them with size_t and revise all	   the places sort of len += sizeof(struct iphdr)	   If len was ULONG_MAX-10 it would be cathastrophe  --ANK	 */	if (len < 0 || len > 0xFFFF)		return -EMSGSIZE;	/* 	 *	Check the flags.	 */	if (msg->msg_flags&MSG_OOB)	/* Mirror BSD error message compatibility */		return -EOPNOTSUPP;	/*	 *	Get and verify the address. 	 */	 	if (msg->msg_name) {		struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;		if (msg->msg_namelen < sizeof(*usin))			return -EINVAL;		if (usin->sin_family != AF_INET) {			if (usin->sin_family != AF_UNSPEC)				return -EINVAL;		}		ufh.daddr = usin->sin_addr.s_addr;		ufh.uh.dest = usin->sin_port;		if (ufh.uh.dest == 0)			return -EINVAL;	} else {		if (sk->state != TCP_ESTABLISHED)			return -ENOTCONN;		ufh.daddr = sk->daddr;		ufh.uh.dest = sk->dport;		/* Open fast path for connected socket.		   Route will not be used, if at least one option is set.		 */		connected = 1;  	}	ipc.addr = sk->saddr;	ufh.uh.source = sk->sport;	ipc.opt = NULL;	ipc.oif = sk->bound_dev_if;	if (msg->msg_controllen) {		err = ip_cmsg_send(msg, &ipc);		if (err)			return err;		if (ipc.opt)			free = 1;		connected = 0;	}	if (!ipc.opt)		ipc.opt = sk->protinfo.af_inet.opt;	ufh.saddr = ipc.addr;	ipc.addr = daddr = ufh.daddr;	if (ipc.opt && ipc.opt->srr) {		if (!daddr)			return -EINVAL;		daddr = ipc.opt->faddr;		connected = 0;	}	tos = RT_TOS(sk->protinfo.af_inet.tos);	if (sk->localroute || (msg->msg_flags&MSG_DONTROUTE) || 	    (ipc.opt && ipc.opt->is_strictroute)) {		tos |= RTO_ONLINK;		connected = 0;	}	if (MULTICAST(daddr)) {		if (!ipc.oif)			ipc.oif = sk->protinfo.af_inet.mc_index;		if (!ufh.saddr)			ufh.saddr = sk->protinfo.af_inet.mc_addr;		connected = 0;	}

⌨️ 快捷键说明

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