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