xref: /csrg-svn/sys/netinet/in_pcb.c (revision 10598)
1 /*	in_pcb.c	4.40	83/01/22	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/mbuf.h"
8 #include "../h/socket.h"
9 #include "../h/socketvar.h"
10 #include "../netinet/in.h"
11 #include "../netinet/in_systm.h"
12 #include "../net/if.h"
13 #include "../net/route.h"
14 #include "../netinet/in_pcb.h"
15 #include "../h/protosw.h"
16 
17 struct	in_addr zeroin_addr;
18 
19 in_pcballoc(so, head)
20 	struct socket *so;
21 	struct inpcb *head;
22 {
23 	struct mbuf *m;
24 	register struct inpcb *inp;
25 
26 	m = m_getclr(M_DONTWAIT, MT_PCB);
27 	if (m == NULL)
28 		return (ENOBUFS);
29 	inp = mtod(m, struct inpcb *);
30 	inp->inp_head = head;
31 	inp->inp_socket = so;
32 	insque(inp, head);
33 	so->so_pcb = (caddr_t)inp;
34 	return (0);
35 }
36 
37 in_pcbbind(inp, nam)
38 	register struct inpcb *inp;
39 	struct mbuf *nam;
40 {
41 	register struct socket *so = inp->inp_socket;
42 	register struct inpcb *head = inp->inp_head;
43 	register struct sockaddr_in *sin;
44 	u_short lport = 0;
45 
46 	if (ifnet == 0)
47 		return (EADDRNOTAVAIL);
48 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
49 		return (EINVAL);
50 	if (nam == 0)
51 		goto noname;
52 	sin = mtod(nam, struct sockaddr_in *);
53 	if (nam->m_len != sizeof (*sin))
54 		return (EINVAL);
55 	if (sin->sin_addr.s_addr != INADDR_ANY) {
56 		int tport = sin->sin_port;
57 
58 		sin->sin_port = 0;		/* yech... */
59 		if (if_ifwithaddr((struct sockaddr *)sin) == 0)
60 			return (EADDRNOTAVAIL);
61 		sin->sin_port = tport;
62 	}
63 	lport = sin->sin_port;
64 	if (lport) {
65 		u_short aport = htons(lport);
66 		int wild = 0;
67 
68 		/* GROSS */
69 		if (aport < IPPORT_RESERVED && u.u_uid != 0)
70 			return (EACCES);
71 		/* even GROSSER, but this is the Internet */
72 		if ((so->so_options & SO_REUSEADDR) == 0 &&
73 		    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
74 		     (so->so_options & SO_ACCEPTCONN) == 0))
75 			wild = INPLOOKUP_WILDCARD;
76 		if (in_pcblookup(head,
77 		    zeroin_addr, 0, sin->sin_addr, lport, wild))
78 			return (EADDRINUSE);
79 	}
80 	inp->inp_laddr = sin->sin_addr;
81 noname:
82 	if (lport == 0)
83 		do {
84 			if (head->inp_lport++ < IPPORT_RESERVED)
85 				head->inp_lport = IPPORT_RESERVED;
86 			lport = htons(head->inp_lport);
87 		} while (in_pcblookup(head,
88 			    zeroin_addr, 0, inp->inp_laddr, lport, 0));
89 	inp->inp_lport = lport;
90 	return (0);
91 }
92 
93 /*
94  * Connect from a socket to a specified address.
95  * Both address and port must be specified in argument sin.
96  * If don't have a local address for this socket yet,
97  * then pick one.
98  */
99 in_pcbconnect(inp, nam)
100 	struct inpcb *inp;
101 	struct mbuf *nam;
102 {
103 	struct ifnet *ifp;
104 	struct sockaddr_in *ifaddr;
105 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
106 
107 	if (nam->m_len != sizeof (*sin))
108 		return (EINVAL);
109 	if (sin->sin_family != AF_INET)
110 		return (EAFNOSUPPORT);
111 	if (sin->sin_addr.s_addr == INADDR_ANY || sin->sin_port == 0)
112 		return (EADDRNOTAVAIL);
113 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
114 		ifp = if_ifonnetof(in_netof(sin->sin_addr));
115 		if (ifp == 0) {
116 			/*
117 			 * We should select the interface based on
118 			 * the route to be used, but for udp this would
119 			 * result in two calls to rtalloc for each packet
120 			 * sent; hardly worthwhile...
121 			 */
122 			ifp = if_ifwithaf(AF_INET);
123 			if (ifp == 0)
124 				return (EADDRNOTAVAIL);
125 		}
126 		ifaddr = (struct sockaddr_in *)&ifp->if_addr;
127 	}
128 	if (in_pcblookup(inp->inp_head,
129 	    sin->sin_addr,
130 	    sin->sin_port,
131 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
132 	    inp->inp_lport,
133 	    0))
134 		return (EADDRINUSE);
135 	if (inp->inp_laddr.s_addr == INADDR_ANY)
136 		inp->inp_laddr = ifaddr->sin_addr;
137 	inp->inp_faddr = sin->sin_addr;
138 	inp->inp_fport = sin->sin_port;
139 	return (0);
140 }
141 
142 in_pcbdisconnect(inp)
143 	struct inpcb *inp;
144 {
145 
146 	inp->inp_faddr.s_addr = INADDR_ANY;
147 	inp->inp_fport = 0;
148 	if (inp->inp_socket->so_state & SS_NOFDREF)
149 		in_pcbdetach(inp);
150 }
151 
152 in_pcbdetach(inp)
153 	struct inpcb *inp;
154 {
155 	struct socket *so = inp->inp_socket;
156 
157 	so->so_pcb = 0;
158 	sofree(so);
159 	if (inp->inp_route.ro_rt)
160 		rtfree(inp->inp_route.ro_rt);
161 	remque(inp);
162 	(void) m_free(dtom(inp));
163 }
164 
165 in_setsockaddr(inp, nam)
166 	register struct inpcb *inp;
167 	struct mbuf *nam;
168 {
169 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
170 
171 	nam->m_len = sizeof (*sin);
172 	sin = mtod(nam, struct sockaddr_in *);
173 	bzero((caddr_t)sin, sizeof (*sin));
174 	sin->sin_family = AF_INET;
175 	sin->sin_port = inp->inp_lport;
176 	sin->sin_addr = inp->inp_laddr;
177 }
178 
179 /*
180  * Pass an error to all internet connections
181  * associated with address sin.  Call the
182  * protocol specific routine to clean up the
183  * mess afterwards.
184  */
185 in_pcbnotify(head, dst, errno, abort)
186 	struct inpcb *head;
187 	register struct in_addr *dst;
188 	int errno, (*abort)();
189 {
190 	register struct inpcb *inp, *oinp;
191 	int s = splimp();
192 
193 	for (inp = head->inp_next; inp != head;) {
194 		if (inp->inp_faddr.s_addr != dst->s_addr) {
195 	next:
196 			inp = inp->inp_next;
197 			continue;
198 		}
199 		if (inp->inp_socket == 0)
200 			goto next;
201 		inp->inp_socket->so_error = errno;
202 		oinp = inp;
203 		inp = inp->inp_next;
204 		(*abort)(oinp);
205 	}
206 	splx(s);
207 }
208 
209 struct inpcb *
210 in_pcblookup(head, faddr, fport, laddr, lport, flags)
211 	struct inpcb *head;
212 	struct in_addr faddr, laddr;
213 	u_short fport, lport;
214 	int flags;
215 {
216 	register struct inpcb *inp, *match = 0;
217 	int matchwild = 3, wildcard;
218 
219 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
220 		if (inp->inp_lport != lport)
221 			continue;
222 		wildcard = 0;
223 		if (inp->inp_laddr.s_addr != INADDR_ANY) {
224 			if (laddr.s_addr == INADDR_ANY)
225 				wildcard++;
226 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
227 				continue;
228 		} else {
229 			if (laddr.s_addr != INADDR_ANY)
230 				wildcard++;
231 		}
232 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
233 			if (faddr.s_addr == INADDR_ANY)
234 				wildcard++;
235 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
236 			    inp->inp_fport != fport)
237 				continue;
238 		} else {
239 			if (faddr.s_addr != INADDR_ANY)
240 				wildcard++;
241 		}
242 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
243 			continue;
244 		if (wildcard < matchwild) {
245 			match = inp;
246 			matchwild = wildcard;
247 			if (matchwild == 0)
248 				break;
249 		}
250 	}
251 	return (match);
252 }
253