udp6_usrreq.c

来自「eCos操作系统源码」· C语言 代码 · 共 722 行 · 第 1/2 页

C
722
字号
	}#endif /* IPSEC */	/*	 * Construct sockaddr format source address.	 * Stuff source address and datagram in user buffer.	 */	init_sin6(&udp_in6, m); /* general init */	udp_in6.sin6_port = uh->uh_sport;	if (in6p->in6p_flags & IN6P_CONTROLOPTS	    || in6p->in6p_socket->so_options & SO_TIMESTAMP)		ip6_savecontrol(in6p, ip6, m, &opts, NULL);	m_adj(m, off + sizeof(struct udphdr));	if (sbappendaddr(&in6p->in6p_socket->so_rcv,			(struct sockaddr *)&udp_in6,			m, opts.head) == 0) {		udpstat.udps_fullsock++;		goto bad;	}	sorwakeup(in6p->in6p_socket);	return IPPROTO_DONE;bad:	if (m)		m_freem(m);	if (opts.head)		m_freem(opts.head);	return IPPROTO_DONE;}voidudp6_ctlinput(cmd, sa, d)	int cmd;	struct sockaddr *sa;	void *d;{	struct udphdr uh;	struct ip6_hdr *ip6;	struct mbuf *m;	int off = 0;	struct ip6ctlparam *ip6cp = NULL;	const struct sockaddr_in6 *sa6_src = NULL;	void *cmdarg;	void (*notify) __P((struct inpcb *, int)) = udp_notify;	struct udp_portonly {		u_int16_t uh_sport;		u_int16_t uh_dport;	} *uhp;	if (sa->sa_family != AF_INET6 ||	    sa->sa_len != sizeof(struct sockaddr_in6))		return;	if ((unsigned)cmd >= PRC_NCMDS)		return;	if (PRC_IS_REDIRECT(cmd))		notify = in6_rtchange, d = NULL;	else if (cmd == PRC_HOSTDEAD)		d = NULL;	else if (inet6ctlerrmap[cmd] == 0)		return;	/* if the parameter is from icmp6, decode it. */	if (d != NULL) {		ip6cp = (struct ip6ctlparam *)d;		m = ip6cp->ip6c_m;		ip6 = ip6cp->ip6c_ip6;		off = ip6cp->ip6c_off;		cmdarg = ip6cp->ip6c_cmdarg;		sa6_src = ip6cp->ip6c_src;	} else {		m = NULL;		ip6 = NULL;		cmdarg = NULL;		sa6_src = &sa6_any;	}	if (ip6) {		/*		 * XXX: We assume that when IPV6 is non NULL,		 * M and OFF are valid.		 */		/* check if we can safely examine src and dst ports */		if (m->m_pkthdr.len < off + sizeof(*uhp))			return;		bzero(&uh, sizeof(uh));		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);		(void) in6_pcbnotify(&udb, sa, uh.uh_dport, 				     (struct sockaddr *)ip6cp->ip6c_src,				     uh.uh_sport, cmd, cmdarg, notify);	} else		(void) in6_pcbnotify(&udb, sa, 0, (struct sockaddr *)&sa6_src,				     0, cmd, cmdarg, notify);}static intudp6_abort(struct socket *so){	struct inpcb *inp;	int s;	inp = sotoinpcb(so);	if (inp == 0)		return EINVAL;	/* ??? possible? panic instead? */	soisdisconnected(so);	s = splnet();	in6_pcbdetach(inp);	splx(s);	return 0;}static intudp6_attach(struct socket *so, int proto, struct proc *p){	struct inpcb *inp;	int s, error;	inp = sotoinpcb(so);	if (inp != 0)		return EINVAL;	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {		error = soreserve(so, udp_sendspace, udp_recvspace);		if (error)			return error;	}	s = splnet();	error = in_pcballoc(so, &udbinfo, p);	splx(s);	if (error)		return error;	inp = (struct inpcb *)so->so_pcb;	inp->inp_vflag |= INP_IPV6;	inp->in6p_hops = -1;	/* use kernel default */	inp->in6p_cksum = -1;	/* just to be sure */	/*	 * XXX: ugly!!	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,	 * because the socket may be bound to an IPv6 wildcard address,	 * which may match an IPv4-mapped IPv6 address.	 */	inp->inp_ip_ttl = ip_defttl;	return 0;}static intudp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p){	struct inpcb *inp;	int s, error;	inp = sotoinpcb(so);	if (inp == 0)		return EINVAL;	inp->inp_vflag &= ~INP_IPV4;	inp->inp_vflag |= INP_IPV6;	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {		struct sockaddr_in6 *sin6_p;		sin6_p = (struct sockaddr_in6 *)nam;		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))			inp->inp_vflag |= INP_IPV4;		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {			struct sockaddr_in sin;			in6_sin6_2_sin(&sin, sin6_p);			inp->inp_vflag |= INP_IPV4;			inp->inp_vflag &= ~INP_IPV6;			s = splnet();			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);			splx(s);			return error;		}	}	s = splnet();	error = in6_pcbbind(inp, nam, p);	splx(s);	return error;}static intudp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p){	struct inpcb *inp;	int s, error;	inp = sotoinpcb(so);	if (inp == 0)		return EINVAL;	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {		struct sockaddr_in6 *sin6_p;		sin6_p = (struct sockaddr_in6 *)nam;		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {			struct sockaddr_in sin;			if (inp->inp_faddr.s_addr != INADDR_ANY)				return EISCONN;			in6_sin6_2_sin(&sin, sin6_p);			s = splnet();			error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);			splx(s);			if (error == 0) {				inp->inp_vflag |= INP_IPV4;				inp->inp_vflag &= ~INP_IPV6;				soisconnected(so);			}			return error;		}	}	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))		return EISCONN;	s = splnet();	error = in6_pcbconnect(inp, nam, p);	splx(s);	if (error == 0) {		if (ip6_mapped_addr_on) { /* should be non mapped addr */			inp->inp_vflag &= ~INP_IPV4;			inp->inp_vflag |= INP_IPV6;		}		soisconnected(so);	}	return error;}static intudp6_detach(struct socket *so){	struct inpcb *inp;	int s;	inp = sotoinpcb(so);	if (inp == 0)		return EINVAL;	s = splnet();	in6_pcbdetach(inp);	splx(s);	return 0;}static intudp6_disconnect(struct socket *so){	struct inpcb *inp;	int s;	inp = sotoinpcb(so);	if (inp == 0)		return EINVAL;#ifdef INET	if (inp->inp_vflag & INP_IPV4) {		struct pr_usrreqs *pru;		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;		return ((*pru->pru_disconnect)(so));	}#endif	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))		return ENOTCONN;	s = splnet();	in6_pcbdisconnect(inp);	inp->in6p_laddr = in6addr_any;	splx(s);	so->so_state &= ~SS_ISCONNECTED;		/* XXX */	return 0;}static intudp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,	  struct mbuf *control, struct proc *p){	struct inpcb *inp;	int error = 0;	inp = sotoinpcb(so);	if (inp == 0) {		error = EINVAL;		goto bad;	}	if (addr) {		if (addr->sa_len != sizeof(struct sockaddr_in6)) { 			error = EINVAL;			goto bad;		}		if (addr->sa_family != AF_INET6) {			error = EAFNOSUPPORT;			goto bad;		}	}#ifdef INET	if (ip6_mapped_addr_on) {		int hasv4addr;		struct sockaddr_in6 *sin6 = 0;		if (addr == 0)			hasv4addr = (inp->inp_vflag & INP_IPV4);		else {			sin6 = (struct sockaddr_in6 *)addr;			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)				? 1 : 0;		}		if (hasv4addr) {			struct pr_usrreqs *pru;			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {				/* 				 * since a user of this socket set the				 * IPV6_V6ONLY flag, we discard this				 * datagram destined to a v4 addr.				 */				return EINVAL;			}			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)			    && !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {				/*				 * when remote addr is IPv4-mapped				 * address, local addr should not be				 * an IPv6 address; since you cannot				 * determine how to map IPv6 source				 * address to IPv4.				 */				return EINVAL;			}			if (sin6)				in6_sin6_2_sin_in_sock(addr);			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;			error = ((*pru->pru_send)(so, flags, m, addr, control,						  p));			/* addr will just be freed in sendit(). */			return error;		}	}#endif	return udp6_output(inp, m, addr, control, p);  bad:	m_freem(m);	return(error);}struct pr_usrreqs udp6_usrreqs = {	udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,	pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,	pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,	pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,	in6_mapped_sockaddr, sosend, soreceive, sopoll};

⌨️ 快捷键说明

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