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