xref: /freebsd/sys/netinet/in_pcb.c (revision df8bae1de4b67ccf57f4afebd4e2bf258c38910d)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36df8bae1dSRodney W. Grimes #include <sys/param.h>
37df8bae1dSRodney W. Grimes #include <sys/systm.h>
38df8bae1dSRodney W. Grimes #include <sys/malloc.h>
39df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
40df8bae1dSRodney W. Grimes #include <sys/protosw.h>
41df8bae1dSRodney W. Grimes #include <sys/socket.h>
42df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
43df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
44df8bae1dSRodney W. Grimes #include <sys/errno.h>
45df8bae1dSRodney W. Grimes #include <sys/time.h>
46df8bae1dSRodney W. Grimes #include <sys/proc.h>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <net/if.h>
49df8bae1dSRodney W. Grimes #include <net/route.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes #include <netinet/in.h>
52df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
53df8bae1dSRodney W. Grimes #include <netinet/ip.h>
54df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes struct	in_addr zeroin_addr;
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes int
61df8bae1dSRodney W. Grimes in_pcballoc(so, head)
62df8bae1dSRodney W. Grimes 	struct socket *so;
63df8bae1dSRodney W. Grimes 	struct inpcb *head;
64df8bae1dSRodney W. Grimes {
65df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes 	MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK);
68df8bae1dSRodney W. Grimes 	if (inp == NULL)
69df8bae1dSRodney W. Grimes 		return (ENOBUFS);
70df8bae1dSRodney W. Grimes 	bzero((caddr_t)inp, sizeof(*inp));
71df8bae1dSRodney W. Grimes 	inp->inp_head = head;
72df8bae1dSRodney W. Grimes 	inp->inp_socket = so;
73df8bae1dSRodney W. Grimes 	insque(inp, head);
74df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)inp;
75df8bae1dSRodney W. Grimes 	return (0);
76df8bae1dSRodney W. Grimes }
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes int
79df8bae1dSRodney W. Grimes in_pcbbind(inp, nam)
80df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
81df8bae1dSRodney W. Grimes 	struct mbuf *nam;
82df8bae1dSRodney W. Grimes {
83df8bae1dSRodney W. Grimes 	register struct socket *so = inp->inp_socket;
84df8bae1dSRodney W. Grimes 	register struct inpcb *head = inp->inp_head;
85df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
86df8bae1dSRodney W. Grimes 	struct proc *p = curproc;		/* XXX */
87df8bae1dSRodney W. Grimes 	u_short lport = 0;
88df8bae1dSRodney W. Grimes 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
89df8bae1dSRodney W. Grimes 	int error;
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes 	if (in_ifaddr == 0)
92df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
93df8bae1dSRodney W. Grimes 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
94df8bae1dSRodney W. Grimes 		return (EINVAL);
95df8bae1dSRodney W. Grimes 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
96df8bae1dSRodney W. Grimes 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
97df8bae1dSRodney W. Grimes 	     (so->so_options & SO_ACCEPTCONN) == 0))
98df8bae1dSRodney W. Grimes 		wild = INPLOOKUP_WILDCARD;
99df8bae1dSRodney W. Grimes 	if (nam) {
100df8bae1dSRodney W. Grimes 		sin = mtod(nam, struct sockaddr_in *);
101df8bae1dSRodney W. Grimes 		if (nam->m_len != sizeof (*sin))
102df8bae1dSRodney W. Grimes 			return (EINVAL);
103df8bae1dSRodney W. Grimes #ifdef notdef
104df8bae1dSRodney W. Grimes 		/*
105df8bae1dSRodney W. Grimes 		 * We should check the family, but old programs
106df8bae1dSRodney W. Grimes 		 * incorrectly fail to initialize it.
107df8bae1dSRodney W. Grimes 		 */
108df8bae1dSRodney W. Grimes 		if (sin->sin_family != AF_INET)
109df8bae1dSRodney W. Grimes 			return (EAFNOSUPPORT);
110df8bae1dSRodney W. Grimes #endif
111df8bae1dSRodney W. Grimes 		lport = sin->sin_port;
112df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
113df8bae1dSRodney W. Grimes 			/*
114df8bae1dSRodney W. Grimes 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
115df8bae1dSRodney W. Grimes 			 * allow complete duplication of binding if
116df8bae1dSRodney W. Grimes 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
117df8bae1dSRodney W. Grimes 			 * and a multicast address is bound on both
118df8bae1dSRodney W. Grimes 			 * new and duplicated sockets.
119df8bae1dSRodney W. Grimes 			 */
120df8bae1dSRodney W. Grimes 			if (so->so_options & SO_REUSEADDR)
121df8bae1dSRodney W. Grimes 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
122df8bae1dSRodney W. Grimes 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
123df8bae1dSRodney W. Grimes 			sin->sin_port = 0;		/* yech... */
124df8bae1dSRodney W. Grimes 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
125df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
126df8bae1dSRodney W. Grimes 		}
127df8bae1dSRodney W. Grimes 		if (lport) {
128df8bae1dSRodney W. Grimes 			struct inpcb *t;
129df8bae1dSRodney W. Grimes 
130df8bae1dSRodney W. Grimes 			/* GROSS */
131df8bae1dSRodney W. Grimes 			if (ntohs(lport) < IPPORT_RESERVED &&
132df8bae1dSRodney W. Grimes 			    (error = suser(p->p_ucred, &p->p_acflag)))
133df8bae1dSRodney W. Grimes 				return (error);
134df8bae1dSRodney W. Grimes 			t = in_pcblookup(head, zeroin_addr, 0,
135df8bae1dSRodney W. Grimes 			    sin->sin_addr, lport, wild);
136df8bae1dSRodney W. Grimes 			if (t && (reuseport & t->inp_socket->so_options) == 0)
137df8bae1dSRodney W. Grimes 				return (EADDRINUSE);
138df8bae1dSRodney W. Grimes 		}
139df8bae1dSRodney W. Grimes 		inp->inp_laddr = sin->sin_addr;
140df8bae1dSRodney W. Grimes 	}
141df8bae1dSRodney W. Grimes 	if (lport == 0)
142df8bae1dSRodney W. Grimes 		do {
143df8bae1dSRodney W. Grimes 			if (head->inp_lport++ < IPPORT_RESERVED ||
144df8bae1dSRodney W. Grimes 			    head->inp_lport > IPPORT_USERRESERVED)
145df8bae1dSRodney W. Grimes 				head->inp_lport = IPPORT_RESERVED;
146df8bae1dSRodney W. Grimes 			lport = htons(head->inp_lport);
147df8bae1dSRodney W. Grimes 		} while (in_pcblookup(head,
148df8bae1dSRodney W. Grimes 			    zeroin_addr, 0, inp->inp_laddr, lport, wild));
149df8bae1dSRodney W. Grimes 	inp->inp_lport = lport;
150df8bae1dSRodney W. Grimes 	return (0);
151df8bae1dSRodney W. Grimes }
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes /*
154df8bae1dSRodney W. Grimes  * Connect from a socket to a specified address.
155df8bae1dSRodney W. Grimes  * Both address and port must be specified in argument sin.
156df8bae1dSRodney W. Grimes  * If don't have a local address for this socket yet,
157df8bae1dSRodney W. Grimes  * then pick one.
158df8bae1dSRodney W. Grimes  */
159df8bae1dSRodney W. Grimes int
160df8bae1dSRodney W. Grimes in_pcbconnect(inp, nam)
161df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
162df8bae1dSRodney W. Grimes 	struct mbuf *nam;
163df8bae1dSRodney W. Grimes {
164df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
165df8bae1dSRodney W. Grimes 	struct sockaddr_in *ifaddr;
166df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
167df8bae1dSRodney W. Grimes 
168df8bae1dSRodney W. Grimes 	if (nam->m_len != sizeof (*sin))
169df8bae1dSRodney W. Grimes 		return (EINVAL);
170df8bae1dSRodney W. Grimes 	if (sin->sin_family != AF_INET)
171df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
172df8bae1dSRodney W. Grimes 	if (sin->sin_port == 0)
173df8bae1dSRodney W. Grimes 		return (EADDRNOTAVAIL);
174df8bae1dSRodney W. Grimes 	if (in_ifaddr) {
175df8bae1dSRodney W. Grimes 		/*
176df8bae1dSRodney W. Grimes 		 * If the destination address is INADDR_ANY,
177df8bae1dSRodney W. Grimes 		 * use the primary local address.
178df8bae1dSRodney W. Grimes 		 * If the supplied address is INADDR_BROADCAST,
179df8bae1dSRodney W. Grimes 		 * and the primary interface supports broadcast,
180df8bae1dSRodney W. Grimes 		 * choose the broadcast address for that interface.
181df8bae1dSRodney W. Grimes 		 */
182df8bae1dSRodney W. Grimes #define	satosin(sa)	((struct sockaddr_in *)(sa))
183df8bae1dSRodney W. Grimes #define sintosa(sin)	((struct sockaddr *)(sin))
184df8bae1dSRodney W. Grimes #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
185df8bae1dSRodney W. Grimes 		if (sin->sin_addr.s_addr == INADDR_ANY)
186df8bae1dSRodney W. Grimes 		    sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr;
187df8bae1dSRodney W. Grimes 		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
188df8bae1dSRodney W. Grimes 		  (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST))
189df8bae1dSRodney W. Grimes 		    sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr;
190df8bae1dSRodney W. Grimes 	}
191df8bae1dSRodney W. Grimes 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
192df8bae1dSRodney W. Grimes 		register struct route *ro;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)0;
195df8bae1dSRodney W. Grimes 		/*
196df8bae1dSRodney W. Grimes 		 * If route is known or can be allocated now,
197df8bae1dSRodney W. Grimes 		 * our src addr is taken from the i/f, else punt.
198df8bae1dSRodney W. Grimes 		 */
199df8bae1dSRodney W. Grimes 		ro = &inp->inp_route;
200df8bae1dSRodney W. Grimes 		if (ro->ro_rt &&
201df8bae1dSRodney W. Grimes 		    (satosin(&ro->ro_dst)->sin_addr.s_addr !=
202df8bae1dSRodney W. Grimes 			sin->sin_addr.s_addr ||
203df8bae1dSRodney W. Grimes 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
204df8bae1dSRodney W. Grimes 			RTFREE(ro->ro_rt);
205df8bae1dSRodney W. Grimes 			ro->ro_rt = (struct rtentry *)0;
206df8bae1dSRodney W. Grimes 		}
207df8bae1dSRodney W. Grimes 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
208df8bae1dSRodney W. Grimes 		    (ro->ro_rt == (struct rtentry *)0 ||
209df8bae1dSRodney W. Grimes 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
210df8bae1dSRodney W. Grimes 			/* No route yet, so try to acquire one */
211df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_family = AF_INET;
212df8bae1dSRodney W. Grimes 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
213df8bae1dSRodney W. Grimes 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
214df8bae1dSRodney W. Grimes 				sin->sin_addr;
215df8bae1dSRodney W. Grimes 			rtalloc(ro);
216df8bae1dSRodney W. Grimes 		}
217df8bae1dSRodney W. Grimes 		/*
218df8bae1dSRodney W. Grimes 		 * If we found a route, use the address
219df8bae1dSRodney W. Grimes 		 * corresponding to the outgoing interface
220df8bae1dSRodney W. Grimes 		 * unless it is the loopback (in case a route
221df8bae1dSRodney W. Grimes 		 * to our address on another net goes to loopback).
222df8bae1dSRodney W. Grimes 		 */
223df8bae1dSRodney W. Grimes 		if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
224df8bae1dSRodney W. Grimes 			ia = ifatoia(ro->ro_rt->rt_ifa);
225df8bae1dSRodney W. Grimes 		if (ia == 0) {
226df8bae1dSRodney W. Grimes 			u_short fport = sin->sin_port;
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes 			sin->sin_port = 0;
229df8bae1dSRodney W. Grimes 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
230df8bae1dSRodney W. Grimes 			if (ia == 0)
231df8bae1dSRodney W. Grimes 				ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
232df8bae1dSRodney W. Grimes 			sin->sin_port = fport;
233df8bae1dSRodney W. Grimes 			if (ia == 0)
234df8bae1dSRodney W. Grimes 				ia = in_ifaddr;
235df8bae1dSRodney W. Grimes 			if (ia == 0)
236df8bae1dSRodney W. Grimes 				return (EADDRNOTAVAIL);
237df8bae1dSRodney W. Grimes 		}
238df8bae1dSRodney W. Grimes 		/*
239df8bae1dSRodney W. Grimes 		 * If the destination address is multicast and an outgoing
240df8bae1dSRodney W. Grimes 		 * interface has been set as a multicast option, use the
241df8bae1dSRodney W. Grimes 		 * address of that interface as our source address.
242df8bae1dSRodney W. Grimes 		 */
243df8bae1dSRodney W. Grimes 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
244df8bae1dSRodney W. Grimes 		    inp->inp_moptions != NULL) {
245df8bae1dSRodney W. Grimes 			struct ip_moptions *imo;
246df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes 			imo = inp->inp_moptions;
249df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL) {
250df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
251df8bae1dSRodney W. Grimes 				for (ia = in_ifaddr; ia; ia = ia->ia_next)
252df8bae1dSRodney W. Grimes 					if (ia->ia_ifp == ifp)
253df8bae1dSRodney W. Grimes 						break;
254df8bae1dSRodney W. Grimes 				if (ia == 0)
255df8bae1dSRodney W. Grimes 					return (EADDRNOTAVAIL);
256df8bae1dSRodney W. Grimes 			}
257df8bae1dSRodney W. Grimes 		}
258df8bae1dSRodney W. Grimes 		ifaddr = (struct sockaddr_in *)&ia->ia_addr;
259df8bae1dSRodney W. Grimes 	}
260df8bae1dSRodney W. Grimes 	if (in_pcblookup(inp->inp_head,
261df8bae1dSRodney W. Grimes 	    sin->sin_addr,
262df8bae1dSRodney W. Grimes 	    sin->sin_port,
263df8bae1dSRodney W. Grimes 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
264df8bae1dSRodney W. Grimes 	    inp->inp_lport,
265df8bae1dSRodney W. Grimes 	    0))
266df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
267df8bae1dSRodney W. Grimes 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
268df8bae1dSRodney W. Grimes 		if (inp->inp_lport == 0)
269df8bae1dSRodney W. Grimes 			(void)in_pcbbind(inp, (struct mbuf *)0);
270df8bae1dSRodney W. Grimes 		inp->inp_laddr = ifaddr->sin_addr;
271df8bae1dSRodney W. Grimes 	}
272df8bae1dSRodney W. Grimes 	inp->inp_faddr = sin->sin_addr;
273df8bae1dSRodney W. Grimes 	inp->inp_fport = sin->sin_port;
274df8bae1dSRodney W. Grimes 	return (0);
275df8bae1dSRodney W. Grimes }
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes int
278df8bae1dSRodney W. Grimes in_pcbdisconnect(inp)
279df8bae1dSRodney W. Grimes 	struct inpcb *inp;
280df8bae1dSRodney W. Grimes {
281df8bae1dSRodney W. Grimes 
282df8bae1dSRodney W. Grimes 	inp->inp_faddr.s_addr = INADDR_ANY;
283df8bae1dSRodney W. Grimes 	inp->inp_fport = 0;
284df8bae1dSRodney W. Grimes 	if (inp->inp_socket->so_state & SS_NOFDREF)
285df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
286df8bae1dSRodney W. Grimes }
287df8bae1dSRodney W. Grimes 
288df8bae1dSRodney W. Grimes int
289df8bae1dSRodney W. Grimes in_pcbdetach(inp)
290df8bae1dSRodney W. Grimes 	struct inpcb *inp;
291df8bae1dSRodney W. Grimes {
292df8bae1dSRodney W. Grimes 	struct socket *so = inp->inp_socket;
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes 	so->so_pcb = 0;
295df8bae1dSRodney W. Grimes 	sofree(so);
296df8bae1dSRodney W. Grimes 	if (inp->inp_options)
297df8bae1dSRodney W. Grimes 		(void)m_free(inp->inp_options);
298df8bae1dSRodney W. Grimes 	if (inp->inp_route.ro_rt)
299df8bae1dSRodney W. Grimes 		rtfree(inp->inp_route.ro_rt);
300df8bae1dSRodney W. Grimes 	ip_freemoptions(inp->inp_moptions);
301df8bae1dSRodney W. Grimes 	remque(inp);
302df8bae1dSRodney W. Grimes 	FREE(inp, M_PCB);
303df8bae1dSRodney W. Grimes }
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes int
306df8bae1dSRodney W. Grimes in_setsockaddr(inp, nam)
307df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
308df8bae1dSRodney W. Grimes 	struct mbuf *nam;
309df8bae1dSRodney W. Grimes {
310df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 	nam->m_len = sizeof (*sin);
313df8bae1dSRodney W. Grimes 	sin = mtod(nam, struct sockaddr_in *);
314df8bae1dSRodney W. Grimes 	bzero((caddr_t)sin, sizeof (*sin));
315df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
316df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
317df8bae1dSRodney W. Grimes 	sin->sin_port = inp->inp_lport;
318df8bae1dSRodney W. Grimes 	sin->sin_addr = inp->inp_laddr;
319df8bae1dSRodney W. Grimes }
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes int
322df8bae1dSRodney W. Grimes in_setpeeraddr(inp, nam)
323df8bae1dSRodney W. Grimes 	struct inpcb *inp;
324df8bae1dSRodney W. Grimes 	struct mbuf *nam;
325df8bae1dSRodney W. Grimes {
326df8bae1dSRodney W. Grimes 	register struct sockaddr_in *sin;
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 	nam->m_len = sizeof (*sin);
329df8bae1dSRodney W. Grimes 	sin = mtod(nam, struct sockaddr_in *);
330df8bae1dSRodney W. Grimes 	bzero((caddr_t)sin, sizeof (*sin));
331df8bae1dSRodney W. Grimes 	sin->sin_family = AF_INET;
332df8bae1dSRodney W. Grimes 	sin->sin_len = sizeof(*sin);
333df8bae1dSRodney W. Grimes 	sin->sin_port = inp->inp_fport;
334df8bae1dSRodney W. Grimes 	sin->sin_addr = inp->inp_faddr;
335df8bae1dSRodney W. Grimes }
336df8bae1dSRodney W. Grimes 
337df8bae1dSRodney W. Grimes /*
338df8bae1dSRodney W. Grimes  * Pass some notification to all connections of a protocol
339df8bae1dSRodney W. Grimes  * associated with address dst.  The local address and/or port numbers
340df8bae1dSRodney W. Grimes  * may be specified to limit the search.  The "usual action" will be
341df8bae1dSRodney W. Grimes  * taken, depending on the ctlinput cmd.  The caller must filter any
342df8bae1dSRodney W. Grimes  * cmds that are uninteresting (e.g., no error in the map).
343df8bae1dSRodney W. Grimes  * Call the protocol specific routine (if any) to report
344df8bae1dSRodney W. Grimes  * any errors for each matching socket.
345df8bae1dSRodney W. Grimes  *
346df8bae1dSRodney W. Grimes  * Must be called at splnet.
347df8bae1dSRodney W. Grimes  */
348df8bae1dSRodney W. Grimes int
349df8bae1dSRodney W. Grimes in_pcbnotify(head, dst, fport_arg, laddr, lport_arg, cmd, notify)
350df8bae1dSRodney W. Grimes 	struct inpcb *head;
351df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
352df8bae1dSRodney W. Grimes 	u_int fport_arg, lport_arg;
353df8bae1dSRodney W. Grimes 	struct in_addr laddr;
354df8bae1dSRodney W. Grimes 	int cmd;
355df8bae1dSRodney W. Grimes 	void (*notify) __P((struct inpcb *, int));
356df8bae1dSRodney W. Grimes {
357df8bae1dSRodney W. Grimes 	extern u_char inetctlerrmap[];
358df8bae1dSRodney W. Grimes 	register struct inpcb *inp, *oinp;
359df8bae1dSRodney W. Grimes 	struct in_addr faddr;
360df8bae1dSRodney W. Grimes 	u_short fport = fport_arg, lport = lport_arg;
361df8bae1dSRodney W. Grimes 	int errno;
362df8bae1dSRodney W. Grimes 
363df8bae1dSRodney W. Grimes 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET)
364df8bae1dSRodney W. Grimes 		return;
365df8bae1dSRodney W. Grimes 	faddr = ((struct sockaddr_in *)dst)->sin_addr;
366df8bae1dSRodney W. Grimes 	if (faddr.s_addr == INADDR_ANY)
367df8bae1dSRodney W. Grimes 		return;
368df8bae1dSRodney W. Grimes 
369df8bae1dSRodney W. Grimes 	/*
370df8bae1dSRodney W. Grimes 	 * Redirects go to all references to the destination,
371df8bae1dSRodney W. Grimes 	 * and use in_rtchange to invalidate the route cache.
372df8bae1dSRodney W. Grimes 	 * Dead host indications: notify all references to the destination.
373df8bae1dSRodney W. Grimes 	 * Otherwise, if we have knowledge of the local port and address,
374df8bae1dSRodney W. Grimes 	 * deliver only to that socket.
375df8bae1dSRodney W. Grimes 	 */
376df8bae1dSRodney W. Grimes 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
377df8bae1dSRodney W. Grimes 		fport = 0;
378df8bae1dSRodney W. Grimes 		lport = 0;
379df8bae1dSRodney W. Grimes 		laddr.s_addr = 0;
380df8bae1dSRodney W. Grimes 		if (cmd != PRC_HOSTDEAD)
381df8bae1dSRodney W. Grimes 			notify = in_rtchange;
382df8bae1dSRodney W. Grimes 	}
383df8bae1dSRodney W. Grimes 	errno = inetctlerrmap[cmd];
384df8bae1dSRodney W. Grimes 	for (inp = head->inp_next; inp != head;) {
385df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
386df8bae1dSRodney W. Grimes 		    inp->inp_socket == 0 ||
387df8bae1dSRodney W. Grimes 		    (lport && inp->inp_lport != lport) ||
388df8bae1dSRodney W. Grimes 		    (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) ||
389df8bae1dSRodney W. Grimes 		    (fport && inp->inp_fport != fport)) {
390df8bae1dSRodney W. Grimes 			inp = inp->inp_next;
391df8bae1dSRodney W. Grimes 			continue;
392df8bae1dSRodney W. Grimes 		}
393df8bae1dSRodney W. Grimes 		oinp = inp;
394df8bae1dSRodney W. Grimes 		inp = inp->inp_next;
395df8bae1dSRodney W. Grimes 		if (notify)
396df8bae1dSRodney W. Grimes 			(*notify)(oinp, errno);
397df8bae1dSRodney W. Grimes 	}
398df8bae1dSRodney W. Grimes }
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes /*
401df8bae1dSRodney W. Grimes  * Check for alternatives when higher level complains
402df8bae1dSRodney W. Grimes  * about service problems.  For now, invalidate cached
403df8bae1dSRodney W. Grimes  * routing information.  If the route was created dynamically
404df8bae1dSRodney W. Grimes  * (by a redirect), time to try a default gateway again.
405df8bae1dSRodney W. Grimes  */
406df8bae1dSRodney W. Grimes int
407df8bae1dSRodney W. Grimes in_losing(inp)
408df8bae1dSRodney W. Grimes 	struct inpcb *inp;
409df8bae1dSRodney W. Grimes {
410df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
411df8bae1dSRodney W. Grimes 	struct rt_addrinfo info;
412df8bae1dSRodney W. Grimes 
413df8bae1dSRodney W. Grimes 	if ((rt = inp->inp_route.ro_rt)) {
414df8bae1dSRodney W. Grimes 		inp->inp_route.ro_rt = 0;
415df8bae1dSRodney W. Grimes 		bzero((caddr_t)&info, sizeof(info));
416df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_DST] =
417df8bae1dSRodney W. Grimes 			(struct sockaddr *)&inp->inp_route.ro_dst;
418df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
419df8bae1dSRodney W. Grimes 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
420df8bae1dSRodney W. Grimes 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
421df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_DYNAMIC)
422df8bae1dSRodney W. Grimes 			(void) rtrequest(RTM_DELETE, rt_key(rt),
423df8bae1dSRodney W. Grimes 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
424df8bae1dSRodney W. Grimes 				(struct rtentry **)0);
425df8bae1dSRodney W. Grimes 		else
426df8bae1dSRodney W. Grimes 		/*
427df8bae1dSRodney W. Grimes 		 * A new route can be allocated
428df8bae1dSRodney W. Grimes 		 * the next time output is attempted.
429df8bae1dSRodney W. Grimes 		 */
430df8bae1dSRodney W. Grimes 			rtfree(rt);
431df8bae1dSRodney W. Grimes 	}
432df8bae1dSRodney W. Grimes }
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes /*
435df8bae1dSRodney W. Grimes  * After a routing change, flush old routing
436df8bae1dSRodney W. Grimes  * and allocate a (hopefully) better one.
437df8bae1dSRodney W. Grimes  */
438df8bae1dSRodney W. Grimes void
439df8bae1dSRodney W. Grimes in_rtchange(inp, errno)
440df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
441df8bae1dSRodney W. Grimes 	int errno;
442df8bae1dSRodney W. Grimes {
443df8bae1dSRodney W. Grimes 	if (inp->inp_route.ro_rt) {
444df8bae1dSRodney W. Grimes 		rtfree(inp->inp_route.ro_rt);
445df8bae1dSRodney W. Grimes 		inp->inp_route.ro_rt = 0;
446df8bae1dSRodney W. Grimes 		/*
447df8bae1dSRodney W. Grimes 		 * A new route can be allocated the next time
448df8bae1dSRodney W. Grimes 		 * output is attempted.
449df8bae1dSRodney W. Grimes 		 */
450df8bae1dSRodney W. Grimes 	}
451df8bae1dSRodney W. Grimes }
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes struct inpcb *
454df8bae1dSRodney W. Grimes in_pcblookup(head, faddr, fport_arg, laddr, lport_arg, flags)
455df8bae1dSRodney W. Grimes 	struct inpcb *head;
456df8bae1dSRodney W. Grimes 	struct in_addr faddr, laddr;
457df8bae1dSRodney W. Grimes 	u_int fport_arg, lport_arg;
458df8bae1dSRodney W. Grimes 	int flags;
459df8bae1dSRodney W. Grimes {
460df8bae1dSRodney W. Grimes 	register struct inpcb *inp, *match = 0;
461df8bae1dSRodney W. Grimes 	int matchwild = 3, wildcard;
462df8bae1dSRodney W. Grimes 	u_short fport = fport_arg, lport = lport_arg;
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
465df8bae1dSRodney W. Grimes 		if (inp->inp_lport != lport)
466df8bae1dSRodney W. Grimes 			continue;
467df8bae1dSRodney W. Grimes 		wildcard = 0;
468df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr != INADDR_ANY) {
469df8bae1dSRodney W. Grimes 			if (laddr.s_addr == INADDR_ANY)
470df8bae1dSRodney W. Grimes 				wildcard++;
471df8bae1dSRodney W. Grimes 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
472df8bae1dSRodney W. Grimes 				continue;
473df8bae1dSRodney W. Grimes 		} else {
474df8bae1dSRodney W. Grimes 			if (laddr.s_addr != INADDR_ANY)
475df8bae1dSRodney W. Grimes 				wildcard++;
476df8bae1dSRodney W. Grimes 		}
477df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
478df8bae1dSRodney W. Grimes 			if (faddr.s_addr == INADDR_ANY)
479df8bae1dSRodney W. Grimes 				wildcard++;
480df8bae1dSRodney W. Grimes 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
481df8bae1dSRodney W. Grimes 			    inp->inp_fport != fport)
482df8bae1dSRodney W. Grimes 				continue;
483df8bae1dSRodney W. Grimes 		} else {
484df8bae1dSRodney W. Grimes 			if (faddr.s_addr != INADDR_ANY)
485df8bae1dSRodney W. Grimes 				wildcard++;
486df8bae1dSRodney W. Grimes 		}
487df8bae1dSRodney W. Grimes 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
488df8bae1dSRodney W. Grimes 			continue;
489df8bae1dSRodney W. Grimes 		if (wildcard < matchwild) {
490df8bae1dSRodney W. Grimes 			match = inp;
491df8bae1dSRodney W. Grimes 			matchwild = wildcard;
492df8bae1dSRodney W. Grimes 			if (matchwild == 0)
493df8bae1dSRodney W. Grimes 				break;
494df8bae1dSRodney W. Grimes 		}
495df8bae1dSRodney W. Grimes 	}
496df8bae1dSRodney W. Grimes 	return (match);
497df8bae1dSRodney W. Grimes }
498