xref: /csrg-svn/sys/netinet/in_pcb.c (revision 37471)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)in_pcb.c	7.9 (Berkeley) 04/22/89
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "dir.h"
23 #include "user.h"
24 #include "malloc.h"
25 #include "mbuf.h"
26 #include "socket.h"
27 #include "socketvar.h"
28 #include "ioctl.h"
29 #include "in.h"
30 #include "in_systm.h"
31 #include "../net/if.h"
32 #include "../net/route.h"
33 #include "in_pcb.h"
34 #include "in_var.h"
35 #include "protosw.h"
36 
37 struct	in_addr zeroin_addr;
38 
39 in_pcballoc(so, head)
40 	struct socket *so;
41 	struct inpcb *head;
42 {
43 	struct mbuf *m;
44 	register struct inpcb *inp;
45 
46 	m = m_getclr(M_DONTWAIT, MT_PCB);
47 	if (m == NULL)
48 		return (ENOBUFS);
49 	inp = mtod(m, struct inpcb *);
50 	inp->inp_head = head;
51 	inp->inp_socket = so;
52 	insque(inp, head);
53 	so->so_pcb = (caddr_t)inp;
54 	return (0);
55 }
56 
57 in_pcbbind(inp, nam)
58 	register struct inpcb *inp;
59 	struct mbuf *nam;
60 {
61 	register struct socket *so = inp->inp_socket;
62 	register struct inpcb *head = inp->inp_head;
63 	register struct sockaddr_in *sin;
64 	u_short lport = 0;
65 
66 	if (in_ifaddr == 0)
67 		return (EADDRNOTAVAIL);
68 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
69 		return (EINVAL);
70 	if (nam == 0)
71 		goto noname;
72 	sin = mtod(nam, struct sockaddr_in *);
73 	if (nam->m_len != sizeof (*sin))
74 		return (EINVAL);
75 	if (sin->sin_addr.s_addr != INADDR_ANY) {
76 		int tport = sin->sin_port;
77 
78 		sin->sin_port = 0;		/* yech... */
79 		if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
80 			return (EADDRNOTAVAIL);
81 		sin->sin_port = tport;
82 	}
83 	lport = sin->sin_port;
84 	if (lport) {
85 		u_short aport = ntohs(lport);
86 		int wild = 0;
87 
88 		/* GROSS */
89 		if (aport < IPPORT_RESERVED && u.u_uid != 0)
90 			return (EACCES);
91 		/* even GROSSER, but this is the Internet */
92 		if ((so->so_options & SO_REUSEADDR) == 0 &&
93 		    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
94 		     (so->so_options & SO_ACCEPTCONN) == 0))
95 			wild = INPLOOKUP_WILDCARD;
96 		if (in_pcblookup(head,
97 		    zeroin_addr, 0, sin->sin_addr, lport, wild))
98 			return (EADDRINUSE);
99 	}
100 	inp->inp_laddr = sin->sin_addr;
101 noname:
102 	if (lport == 0)
103 		do {
104 			if (head->inp_lport++ < IPPORT_RESERVED ||
105 			    head->inp_lport > IPPORT_USERRESERVED)
106 				head->inp_lport = IPPORT_RESERVED;
107 			lport = htons(head->inp_lport);
108 		} while (in_pcblookup(head,
109 			    zeroin_addr, 0, inp->inp_laddr, lport, 0));
110 	inp->inp_lport = lport;
111 	return (0);
112 }
113 
114 /*
115  * Connect from a socket to a specified address.
116  * Both address and port must be specified in argument sin.
117  * If don't have a local address for this socket yet,
118  * then pick one.
119  */
120 in_pcbconnect(inp, nam)
121 	register struct inpcb *inp;
122 	struct mbuf *nam;
123 {
124 	struct in_ifaddr *ia;
125 	struct sockaddr_in *ifaddr;
126 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
127 
128 	if (nam->m_len != sizeof (*sin))
129 		return (EINVAL);
130 	if (sin->sin_family != AF_INET)
131 		return (EAFNOSUPPORT);
132 	if (sin->sin_port == 0)
133 		return (EADDRNOTAVAIL);
134 	if (in_ifaddr) {
135 		/*
136 		 * If the destination address is INADDR_ANY,
137 		 * use the primary local address.
138 		 * If the supplied address is INADDR_BROADCAST,
139 		 * and the primary interface supports broadcast,
140 		 * choose the broadcast address for that interface.
141 		 */
142 #define	satosin(sa)	((struct sockaddr_in *)(sa))
143 		if (sin->sin_addr.s_addr == INADDR_ANY)
144 		    sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr;
145 		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
146 		  (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST))
147 		    sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr;
148 	}
149 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
150 		register struct route *ro;
151 		struct ifnet *ifp;
152 
153 		ia = (struct in_ifaddr *)0;
154 		/*
155 		 * If route is known or can be allocated now,
156 		 * our src addr is taken from the i/f, else punt.
157 		 */
158 		ro = &inp->inp_route;
159 		if (ro->ro_rt &&
160 		    (satosin(&ro->ro_dst)->sin_addr.s_addr !=
161 			sin->sin_addr.s_addr ||
162 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
163 			RTFREE(ro->ro_rt);
164 			ro->ro_rt = (struct rtentry *)0;
165 		}
166 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
167 		    (ro->ro_rt == (struct rtentry *)0 ||
168 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
169 			/* No route yet, so try to acquire one */
170 			ro->ro_dst.sa_family = AF_INET;
171 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
172 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
173 				sin->sin_addr;
174 			rtalloc(ro);
175 		}
176 		/*
177 		 * If we found a route, use the address
178 		 * corresponding to the outgoing interface
179 		 * unless it is the loopback (in case a route
180 		 * to our address on another net goes to loopback).
181 		 */
182 		if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp) &&
183 		    (ifp->if_flags & IFF_LOOPBACK) == 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 		ifaddr = (struct sockaddr_in *)&ia->ia_addr;
202 	}
203 	if (in_pcblookup(inp->inp_head,
204 	    sin->sin_addr,
205 	    sin->sin_port,
206 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
207 	    inp->inp_lport,
208 	    0))
209 		return (EADDRINUSE);
210 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
211 		if (inp->inp_lport == 0)
212 			(void)in_pcbbind(inp, (struct mbuf *)0);
213 		inp->inp_laddr = ifaddr->sin_addr;
214 	}
215 	inp->inp_faddr = sin->sin_addr;
216 	inp->inp_fport = sin->sin_port;
217 	return (0);
218 }
219 
220 in_pcbdisconnect(inp)
221 	struct inpcb *inp;
222 {
223 
224 	inp->inp_faddr.s_addr = INADDR_ANY;
225 	inp->inp_fport = 0;
226 	if (inp->inp_socket->so_state & SS_NOFDREF)
227 		in_pcbdetach(inp);
228 }
229 
230 in_pcbdetach(inp)
231 	struct inpcb *inp;
232 {
233 	struct socket *so = inp->inp_socket;
234 
235 	so->so_pcb = 0;
236 	sofree(so);
237 	if (inp->inp_options)
238 		(void)m_free(inp->inp_options);
239 	if (inp->inp_route.ro_rt)
240 		rtfree(inp->inp_route.ro_rt);
241 	remque(inp);
242 	(void) m_free(dtom(inp));
243 }
244 
245 in_setsockaddr(inp, nam)
246 	register struct inpcb *inp;
247 	struct mbuf *nam;
248 {
249 	register struct sockaddr_in *sin;
250 
251 	nam->m_len = sizeof (*sin);
252 	sin = mtod(nam, struct sockaddr_in *);
253 	bzero((caddr_t)sin, sizeof (*sin));
254 	sin->sin_family = AF_INET;
255 	sin->sin_len = sizeof(*sin);
256 	sin->sin_port = inp->inp_lport;
257 	sin->sin_addr = inp->inp_laddr;
258 }
259 
260 in_setpeeraddr(inp, nam)
261 	struct inpcb *inp;
262 	struct mbuf *nam;
263 {
264 	register struct sockaddr_in *sin;
265 
266 	nam->m_len = sizeof (*sin);
267 	sin = mtod(nam, struct sockaddr_in *);
268 	bzero((caddr_t)sin, sizeof (*sin));
269 	sin->sin_family = AF_INET;
270 	sin->sin_len = sizeof(*sin);
271 	sin->sin_port = inp->inp_fport;
272 	sin->sin_addr = inp->inp_faddr;
273 }
274 
275 /*
276  * Pass some notification to all connections of a protocol
277  * associated with address dst.  Call the protocol specific
278  * routine (if any) to handle each connection.
279  */
280 in_pcbnotify(head, dst, errno, notify)
281 	struct inpcb *head;
282 	register struct in_addr *dst;
283 	int errno, (*notify)();
284 {
285 	register struct inpcb *inp, *oinp;
286 	int s = splimp();
287 
288 	for (inp = head->inp_next; inp != head;) {
289 		if (inp->inp_faddr.s_addr != dst->s_addr ||
290 		    inp->inp_socket == 0) {
291 			inp = inp->inp_next;
292 			continue;
293 		}
294 		if (errno)
295 			inp->inp_socket->so_error = errno;
296 		oinp = inp;
297 		inp = inp->inp_next;
298 		if (notify)
299 			(*notify)(oinp);
300 	}
301 	splx(s);
302 }
303 
304 /*
305  * Check for alternatives when higher level complains
306  * about service problems.  For now, invalidate cached
307  * routing information.  If the route was created dynamically
308  * (by a redirect), time to try a default gateway again.
309  */
310 in_losing(inp)
311 	struct inpcb *inp;
312 {
313 	register struct rtentry *rt;
314 
315 	if ((rt = inp->inp_route.ro_rt)) {
316 		rt_missmsg(RTM_LOSING, &inp->inp_route.ro_dst,
317 			    rt->rt_gateway, (struct sockaddr *)rt_mask(rt),
318 			    (struct sockaddr *)0, rt->rt_flags, 0);
319 		if (rt->rt_flags & RTF_DYNAMIC)
320 			(void) rtrequest(RTM_DELETE, rt_key(rt),
321 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
322 				(struct rtentry **)0);
323 		inp->inp_route.ro_rt = 0;
324 		rtfree(rt);
325 		/*
326 		 * A new route can be allocated
327 		 * the next time output is attempted.
328 		 */
329 	}
330 }
331 
332 /*
333  * After a routing change, flush old routing
334  * and allocate a (hopefully) better one.
335  */
336 in_rtchange(inp)
337 	register struct inpcb *inp;
338 {
339 	if (inp->inp_route.ro_rt) {
340 		rtfree(inp->inp_route.ro_rt);
341 		inp->inp_route.ro_rt = 0;
342 		/*
343 		 * A new route can be allocated the next time
344 		 * output is attempted.
345 		 */
346 	}
347 }
348 
349 struct inpcb *
350 in_pcblookup(head, faddr, fport, laddr, lport, flags)
351 	struct inpcb *head;
352 	struct in_addr faddr, laddr;
353 	u_short fport, lport;
354 	int flags;
355 {
356 	register struct inpcb *inp, *match = 0;
357 	int matchwild = 3, wildcard;
358 
359 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
360 		if (inp->inp_lport != lport)
361 			continue;
362 		wildcard = 0;
363 		if (inp->inp_laddr.s_addr != INADDR_ANY) {
364 			if (laddr.s_addr == INADDR_ANY)
365 				wildcard++;
366 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
367 				continue;
368 		} else {
369 			if (laddr.s_addr != INADDR_ANY)
370 				wildcard++;
371 		}
372 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
373 			if (faddr.s_addr == INADDR_ANY)
374 				wildcard++;
375 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
376 			    inp->inp_fport != fport)
377 				continue;
378 		} else {
379 			if (faddr.s_addr != INADDR_ANY)
380 				wildcard++;
381 		}
382 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
383 			continue;
384 		if (wildcard < matchwild) {
385 			match = inp;
386 			matchwild = wildcard;
387 			if (matchwild == 0)
388 				break;
389 		}
390 	}
391 	return (match);
392 }
393