xref: /csrg-svn/sys/netinet/in_pcb.c (revision 55347)
1 /*
2  * Copyright (c) 1982, 1986, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)in_pcb.c	7.20 (Berkeley) 07/18/92
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "malloc.h"
13 #include "mbuf.h"
14 #include "protosw.h"
15 #include "socket.h"
16 #include "socketvar.h"
17 #include "ioctl.h"
18 #include "errno.h"
19 
20 #include "../net/if.h"
21 #include "../net/route.h"
22 
23 #include "in.h"
24 #include "in_systm.h"
25 #include "ip.h"
26 #include "in_pcb.h"
27 #include "in_var.h"
28 
29 #ifdef MULTICAST
30 #include "ip_var.h"
31 #endif
32 
33 struct	in_addr zeroin_addr;
34 
35 in_pcballoc(so, head)
36 	struct socket *so;
37 	struct inpcb *head;
38 {
39 	struct mbuf *m;
40 	register struct inpcb *inp;
41 
42 	MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK);
43 	if (inp == NULL)
44 		return (ENOBUFS);
45 	bzero((caddr_t)inp, sizeof(*inp));
46 	inp->inp_head = head;
47 	inp->inp_socket = so;
48 	insque(inp, head);
49 	so->so_pcb = (caddr_t)inp;
50 	return (0);
51 }
52 
53 in_pcbbind(inp, nam)
54 	register struct inpcb *inp;
55 	struct mbuf *nam;
56 {
57 	register struct socket *so = inp->inp_socket;
58 	register struct inpcb *head = inp->inp_head;
59 	register struct sockaddr_in *sin;
60 	u_short lport = 0;
61 
62 	if (in_ifaddr == 0)
63 		return (EADDRNOTAVAIL);
64 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
65 		return (EINVAL);
66 	if (nam == 0)
67 		goto noname;
68 	sin = mtod(nam, struct sockaddr_in *);
69 	if (nam->m_len != sizeof (*sin))
70 		return (EINVAL);
71 	if (sin->sin_addr.s_addr != INADDR_ANY) {
72 		int tport = sin->sin_port;
73 
74 		sin->sin_port = 0;		/* yech... */
75 		if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
76 			return (EADDRNOTAVAIL);
77 		sin->sin_port = tport;
78 	}
79 	lport = sin->sin_port;
80 	if (lport) {
81 		struct inpcb *t;
82 		u_short aport = ntohs(lport);
83 		int wild = 0;
84 
85 		/* GROSS */
86 		if (aport < IPPORT_RESERVED && (so->so_state & SS_PRIV) == 0)
87 			return (EACCES);
88 		/* even GROSSER, but this is the Internet */
89 		if ((so->so_options & SO_REUSEADDR) == 0 &&
90 		    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
91 		     (so->so_options & SO_ACCEPTCONN) == 0))
92 			wild = INPLOOKUP_WILDCARD;
93 		t = in_pcblookup(head, zeroin_addr, 0,
94 				sin->sin_addr, lport, wild))
95 		if (t && !((so->so_options & t->inp_socket->so_options) &
96 		    SO_REUSEPORT))
97 			return (EADDRINUSE);
98 	}
99 	inp->inp_laddr = sin->sin_addr;
100 noname:
101 	if (lport == 0)
102 		do {
103 			if (head->inp_lport++ < IPPORT_RESERVED ||
104 			    head->inp_lport > IPPORT_USERRESERVED)
105 				head->inp_lport = IPPORT_RESERVED;
106 			lport = htons(head->inp_lport);
107 		} while (in_pcblookup(head,
108 			    zeroin_addr, 0, inp->inp_laddr, lport, 0));
109 	inp->inp_lport = lport;
110 	return (0);
111 }
112 
113 /*
114  * Connect from a socket to a specified address.
115  * Both address and port must be specified in argument sin.
116  * If don't have a local address for this socket yet,
117  * then pick one.
118  */
119 in_pcbconnect(inp, nam)
120 	register struct inpcb *inp;
121 	struct mbuf *nam;
122 {
123 	struct in_ifaddr *ia;
124 	struct sockaddr_in *ifaddr;
125 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
126 
127 	if (nam->m_len != sizeof (*sin))
128 		return (EINVAL);
129 	if (sin->sin_family != AF_INET)
130 		return (EAFNOSUPPORT);
131 	if (sin->sin_port == 0)
132 		return (EADDRNOTAVAIL);
133 	if (in_ifaddr) {
134 		/*
135 		 * If the destination address is INADDR_ANY,
136 		 * use the primary local address.
137 		 * If the supplied address is INADDR_BROADCAST,
138 		 * and the primary interface supports broadcast,
139 		 * choose the broadcast address for that interface.
140 		 */
141 #define	satosin(sa)	((struct sockaddr_in *)(sa))
142 		if (sin->sin_addr.s_addr == INADDR_ANY)
143 		    sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr;
144 		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
145 		  (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST))
146 		    sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr;
147 	}
148 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
149 		register struct route *ro;
150 		struct ifnet *ifp;
151 
152 		ia = (struct in_ifaddr *)0;
153 		/*
154 		 * If route is known or can be allocated now,
155 		 * our src addr is taken from the i/f, else punt.
156 		 */
157 		ro = &inp->inp_route;
158 		if (ro->ro_rt &&
159 		    (satosin(&ro->ro_dst)->sin_addr.s_addr !=
160 			sin->sin_addr.s_addr ||
161 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
162 			RTFREE(ro->ro_rt);
163 			ro->ro_rt = (struct rtentry *)0;
164 		}
165 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
166 		    (ro->ro_rt == (struct rtentry *)0 ||
167 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
168 			/* No route yet, so try to acquire one */
169 			ro->ro_dst.sa_family = AF_INET;
170 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
171 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
172 				sin->sin_addr;
173 			rtalloc(ro);
174 		}
175 		/*
176 		 * If we found a route, use the address
177 		 * corresponding to the outgoing interface
178 		 * unless it is the loopback (in case a route
179 		 * to our address on another net goes to loopback).
180 		 */
181 		if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp) &&
182 		    (ifp->if_flags & IFF_LOOPBACK) == 0 &&
183 		    (ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa) == 0)
184 			for (ia = in_ifaddr; ia; ia = ia->ia_next)
185 				if (ia->ia_ifp == ifp)
186 					break;
187 		if (ia == 0) {
188 			int fport = sin->sin_port;
189 
190 			sin->sin_port = 0;
191 			ia = (struct in_ifaddr *)
192 			    ifa_ifwithdstaddr((struct sockaddr *)sin);
193 			sin->sin_port = fport;
194 			if (ia == 0)
195 				ia = in_iaonnetof(in_netof(sin->sin_addr));
196 			if (ia == 0)
197 				ia = in_ifaddr;
198 			if (ia == 0)
199 				return (EADDRNOTAVAIL);
200 		}
201 #ifdef MULTICAST
202 		/*
203 		 * If the destination address is multicast and an outgoing
204 		 * interface has been set as a multicast option, use the
205 		 * address of that interface as our source address.
206 		 */
207 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
208 		    inp->inp_moptions != NULL) {
209 			struct ip_moptions *imo;
210 
211 			imo = inp->inp_moptions;
212 			if (imo->imo_multicast_ifp != NULL) {
213 				ifp = imo->imo_multicast_ifp;
214 				for (ia = in_ifaddr; ia; ia = ia->ia_next)
215 					if (ia->ia_ifp == ifp)
216 						break;
217 				if (ia == 0)
218 					return (EADDRNOTAVAIL);
219 			}
220 		}
221 #endif
222 		ifaddr = (struct sockaddr_in *)&ia->ia_addr;
223 	}
224 	if (in_pcblookup(inp->inp_head,
225 	    sin->sin_addr,
226 	    sin->sin_port,
227 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
228 	    inp->inp_lport,
229 	    0))
230 		return (EADDRINUSE);
231 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
232 		if (inp->inp_lport == 0)
233 			(void)in_pcbbind(inp, (struct mbuf *)0);
234 		inp->inp_laddr = ifaddr->sin_addr;
235 	}
236 	inp->inp_faddr = sin->sin_addr;
237 	inp->inp_fport = sin->sin_port;
238 	return (0);
239 }
240 
241 in_pcbdisconnect(inp)
242 	struct inpcb *inp;
243 {
244 
245 	inp->inp_faddr.s_addr = INADDR_ANY;
246 	inp->inp_fport = 0;
247 	if (inp->inp_socket->so_state & SS_NOFDREF)
248 		in_pcbdetach(inp);
249 }
250 
251 in_pcbdetach(inp)
252 	struct inpcb *inp;
253 {
254 	struct socket *so = inp->inp_socket;
255 
256 	so->so_pcb = 0;
257 	sofree(so);
258 	if (inp->inp_options)
259 		(void)m_free(inp->inp_options);
260 	if (inp->inp_route.ro_rt)
261 		rtfree(inp->inp_route.ro_rt);
262 #ifdef MULTICAST
263 	ip_freemoptions(inp->inp_moptions);
264 #endif
265 	remque(inp);
266 	FREE(inp, M_PCB);
267 }
268 
269 in_setsockaddr(inp, nam)
270 	register struct inpcb *inp;
271 	struct mbuf *nam;
272 {
273 	register struct sockaddr_in *sin;
274 
275 	nam->m_len = sizeof (*sin);
276 	sin = mtod(nam, struct sockaddr_in *);
277 	bzero((caddr_t)sin, sizeof (*sin));
278 	sin->sin_family = AF_INET;
279 	sin->sin_len = sizeof(*sin);
280 	sin->sin_port = inp->inp_lport;
281 	sin->sin_addr = inp->inp_laddr;
282 }
283 
284 in_setpeeraddr(inp, nam)
285 	struct inpcb *inp;
286 	struct mbuf *nam;
287 {
288 	register struct sockaddr_in *sin;
289 
290 	nam->m_len = sizeof (*sin);
291 	sin = mtod(nam, struct sockaddr_in *);
292 	bzero((caddr_t)sin, sizeof (*sin));
293 	sin->sin_family = AF_INET;
294 	sin->sin_len = sizeof(*sin);
295 	sin->sin_port = inp->inp_fport;
296 	sin->sin_addr = inp->inp_faddr;
297 }
298 
299 /*
300  * Pass some notification to all connections of a protocol
301  * associated with address dst.  The local address and/or port numbers
302  * may be specified to limit the search.  The "usual action" will be
303  * taken, depending on the ctlinput cmd.  The caller must filter any
304  * cmds that are uninteresting (e.g., no error in the map).
305  * Call the protocol specific routine (if any) to report
306  * any errors for each matching socket.
307  *
308  * Must be called at splnet.
309  */
310 in_pcbnotify(head, dst, fport, laddr, lport, cmd, notify)
311 	struct inpcb *head;
312 	struct sockaddr *dst;
313 	u_short fport, lport;
314 	struct in_addr laddr;
315 	int cmd, (*notify)();
316 {
317 	register struct inpcb *inp, *oinp;
318 	struct in_addr faddr;
319 	int errno;
320 	int in_rtchange();
321 	extern u_char inetctlerrmap[];
322 
323 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET)
324 		return;
325 	faddr = ((struct sockaddr_in *)dst)->sin_addr;
326 	if (faddr.s_addr == INADDR_ANY)
327 		return;
328 
329 	/*
330 	 * Redirects go to all references to the destination,
331 	 * and use in_rtchange to invalidate the route cache.
332 	 * Dead host indications: notify all references to the destination.
333 	 * Otherwise, if we have knowledge of the local port and address,
334 	 * deliver only to that socket.
335 	 */
336 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
337 		fport = 0;
338 		lport = 0;
339 		laddr.s_addr = 0;
340 		if (cmd != PRC_HOSTDEAD)
341 			notify = in_rtchange;
342 	}
343 	errno = inetctlerrmap[cmd];
344 	for (inp = head->inp_next; inp != head;) {
345 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
346 		    inp->inp_socket == 0 ||
347 		    (lport && inp->inp_lport != lport) ||
348 		    (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) ||
349 		    (fport && inp->inp_fport != fport)) {
350 			inp = inp->inp_next;
351 			continue;
352 		}
353 		oinp = inp;
354 		inp = inp->inp_next;
355 		if (notify)
356 			(*notify)(oinp, errno);
357 	}
358 }
359 
360 /*
361  * Check for alternatives when higher level complains
362  * about service problems.  For now, invalidate cached
363  * routing information.  If the route was created dynamically
364  * (by a redirect), time to try a default gateway again.
365  */
366 in_losing(inp)
367 	struct inpcb *inp;
368 {
369 	register struct rtentry *rt;
370 	struct rt_addrinfo info;
371 
372 	if ((rt = inp->inp_route.ro_rt)) {
373 		bzero((caddr_t)&info, sizeof(info));
374 		info.rti_info[RTAX_DST] =
375 			(struct sockaddr *)&inp->inp_route.ro_dst;
376 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
377 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
378 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
379 		if (rt->rt_flags & RTF_DYNAMIC)
380 			(void) rtrequest(RTM_DELETE, rt_key(rt),
381 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
382 				(struct rtentry **)0);
383 		inp->inp_route.ro_rt = 0;
384 		rtfree(rt);
385 		/*
386 		 * A new route can be allocated
387 		 * the next time output is attempted.
388 		 */
389 	}
390 }
391 
392 /*
393  * After a routing change, flush old routing
394  * and allocate a (hopefully) better one.
395  */
396 in_rtchange(inp)
397 	register struct inpcb *inp;
398 {
399 	if (inp->inp_route.ro_rt) {
400 		rtfree(inp->inp_route.ro_rt);
401 		inp->inp_route.ro_rt = 0;
402 		/*
403 		 * A new route can be allocated the next time
404 		 * output is attempted.
405 		 */
406 	}
407 }
408 
409 struct inpcb *
410 in_pcblookup(head, faddr, fport, laddr, lport, flags)
411 	struct inpcb *head;
412 	struct in_addr faddr, laddr;
413 	u_short fport, lport;
414 	int flags;
415 {
416 	register struct inpcb *inp, *match = 0;
417 	int matchwild = 3, wildcard;
418 
419 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
420 		if (inp->inp_lport != lport)
421 			continue;
422 		wildcard = 0;
423 		if (inp->inp_laddr.s_addr != INADDR_ANY) {
424 			if (laddr.s_addr == INADDR_ANY)
425 				wildcard++;
426 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
427 				continue;
428 		} else {
429 			if (laddr.s_addr != INADDR_ANY)
430 				wildcard++;
431 		}
432 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
433 			if (faddr.s_addr == INADDR_ANY)
434 				wildcard++;
435 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
436 			    inp->inp_fport != fport)
437 				continue;
438 		} else {
439 			if (faddr.s_addr != INADDR_ANY)
440 				wildcard++;
441 		}
442 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
443 			continue;
444 		if (wildcard < matchwild) {
445 			match = inp;
446 			matchwild = wildcard;
447 			if (matchwild == 0)
448 				break;
449 		}
450 	}
451 	return (match);
452 }
453