in_pcb.c revision 1.27.4.2 1 /* $NetBSD: in_pcb.c,v 1.27.4.2 1996/12/11 04:01:01 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/ioctl.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/proc.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip_var.h>
59
60 struct in_addr zeroin_addr;
61
62 #define INPCBHASH(table, faddr, fport, laddr, lport) \
63 &(table)->inpt_hashtbl[(ntohl((faddr)->s_addr) + ntohs((fport)) + ntohs((lport))) & (table->inpt_hash)]
64
65 void
66 in_pcbinit(table, hashsize)
67 struct inpcbtable *table;
68 int hashsize;
69 {
70
71 CIRCLEQ_INIT(&table->inpt_queue);
72 table->inpt_hashtbl = hashinit(hashsize, M_PCB, &table->inpt_hash);
73 table->inpt_lastport = IPPORT_RESERVED;
74 }
75
76 int
77 in_pcballoc(so, v)
78 struct socket *so;
79 void *v;
80 {
81 struct inpcbtable *table = v;
82 register struct inpcb *inp;
83 int s;
84
85 MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK);
86 if (inp == NULL)
87 return (ENOBUFS);
88 bzero((caddr_t)inp, sizeof(*inp));
89 inp->inp_table = table;
90 inp->inp_socket = so;
91 s = splnet();
92 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
93 LIST_INSERT_HEAD(INPCBHASH(table, &inp->inp_faddr, inp->inp_fport,
94 &inp->inp_laddr, inp->inp_lport), inp, inp_hash);
95 splx(s);
96 so->so_pcb = inp;
97 return (0);
98 }
99
100 int
101 in_pcbbind(v, nam, p)
102 void *v;
103 struct mbuf *nam;
104 struct proc *p;
105 {
106 register struct inpcb *inp = v;
107 register struct socket *so = inp->inp_socket;
108 register struct inpcbtable *table = inp->inp_table;
109 register struct sockaddr_in *sin;
110 u_int16_t lport = 0;
111 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
112 int error;
113
114 if (in_ifaddr.tqh_first == 0)
115 return (EADDRNOTAVAIL);
116 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
117 return (EINVAL);
118 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
119 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
120 (so->so_options & SO_ACCEPTCONN) == 0))
121 wild = INPLOOKUP_WILDCARD;
122 if (nam == 0)
123 goto noname;
124 sin = mtod(nam, struct sockaddr_in *);
125 if (nam->m_len != sizeof (*sin))
126 return (EINVAL);
127 #ifdef notdef
128 /*
129 * We should check the family, but old programs
130 * incorrectly fail to initialize it.
131 */
132 if (sin->sin_family != AF_INET)
133 return (EAFNOSUPPORT);
134 #endif
135 lport = sin->sin_port;
136 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
137 /*
138 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
139 * allow complete duplication of binding if
140 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
141 * and a multicast address is bound on both
142 * new and duplicated sockets.
143 */
144 if (so->so_options & SO_REUSEADDR)
145 reuseport = SO_REUSEADDR|SO_REUSEPORT;
146 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
147 sin->sin_port = 0; /* yech... */
148 if (ifa_ifwithaddr(sintosa(sin)) == 0)
149 return (EADDRNOTAVAIL);
150 }
151 if (lport) {
152 struct inpcb *t;
153
154 /* GROSS */
155 if (ntohs(lport) < IPPORT_RESERVED &&
156 (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))))
157 return (EACCES);
158 t = in_pcblookup(table, zeroin_addr, 0,
159 sin->sin_addr, lport, wild);
160 if (t && (reuseport & t->inp_socket->so_options) == 0)
161 return (EADDRINUSE);
162 }
163 inp->inp_laddr = sin->sin_addr;
164 noname:
165 if (lport == 0) {
166 for (lport = table->inpt_lastport + 1;
167 lport < IPPORT_USERRESERVED; lport++)
168 if (!in_pcblookup(table, zeroin_addr, 0, inp->inp_laddr,
169 htons(lport), wild))
170 goto found;
171 for (lport = IPPORT_RESERVED;
172 lport <= table->inpt_lastport; lport++)
173 if (!in_pcblookup(table, zeroin_addr, 0, inp->inp_laddr,
174 htons(lport), wild))
175 goto found;
176 return (EAGAIN);
177 found:
178 table->inpt_lastport = lport;
179 lport = htons(lport);
180 }
181 inp->inp_lport = lport;
182 in_pcbrehash(inp);
183 return (0);
184 }
185
186 /*
187 * Connect from a socket to a specified address.
188 * Both address and port must be specified in argument sin.
189 * If don't have a local address for this socket yet,
190 * then pick one.
191 */
192 int
193 in_pcbconnect(v, nam)
194 register void *v;
195 struct mbuf *nam;
196 {
197 register struct inpcb *inp = v;
198 struct in_ifaddr *ia;
199 struct sockaddr_in *ifaddr = NULL;
200 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
201
202 if (nam->m_len != sizeof (*sin))
203 return (EINVAL);
204 if (sin->sin_family != AF_INET)
205 return (EAFNOSUPPORT);
206 if (sin->sin_port == 0)
207 return (EADDRNOTAVAIL);
208 if (in_ifaddr.tqh_first != 0) {
209 /*
210 * If the destination address is INADDR_ANY,
211 * use the primary local address.
212 * If the supplied address is INADDR_BROADCAST,
213 * and the primary interface supports broadcast,
214 * choose the broadcast address for that interface.
215 */
216 if (sin->sin_addr.s_addr == INADDR_ANY)
217 sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr;
218 else if (sin->sin_addr.s_addr == INADDR_BROADCAST &&
219 (in_ifaddr.tqh_first->ia_ifp->if_flags & IFF_BROADCAST))
220 sin->sin_addr = in_ifaddr.tqh_first->ia_broadaddr.sin_addr;
221 }
222 if (inp->inp_laddr.s_addr == INADDR_ANY) {
223 register struct route *ro;
224
225 ia = (struct in_ifaddr *)0;
226 /*
227 * If route is known or can be allocated now,
228 * our src addr is taken from the i/f, else punt.
229 */
230 ro = &inp->inp_route;
231 if (ro->ro_rt &&
232 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
233 sin->sin_addr.s_addr ||
234 inp->inp_socket->so_options & SO_DONTROUTE)) {
235 RTFREE(ro->ro_rt);
236 ro->ro_rt = (struct rtentry *)0;
237 }
238 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
239 (ro->ro_rt == (struct rtentry *)0 ||
240 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
241 /* No route yet, so try to acquire one */
242 ro->ro_dst.sa_family = AF_INET;
243 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
244 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
245 rtalloc(ro);
246 }
247 /*
248 * If we found a route, use the address
249 * corresponding to the outgoing interface
250 * unless it is the loopback (in case a route
251 * to our address on another net goes to loopback).
252 */
253 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
254 ia = ifatoia(ro->ro_rt->rt_ifa);
255 if (ia == 0) {
256 u_int16_t fport = sin->sin_port;
257
258 sin->sin_port = 0;
259 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
260 sin->sin_port = fport;
261 if (ia == 0)
262 ia = in_ifaddr.tqh_first;
263 if (ia == 0)
264 return (EADDRNOTAVAIL);
265 }
266 /*
267 * If the destination address is multicast and an outgoing
268 * interface has been set as a multicast option, use the
269 * address of that interface as our source address.
270 */
271 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
272 inp->inp_moptions != NULL) {
273 struct ip_moptions *imo;
274 struct ifnet *ifp;
275
276 imo = inp->inp_moptions;
277 if (imo->imo_multicast_ifp != NULL) {
278 ifp = imo->imo_multicast_ifp;
279 for (ia = in_ifaddr.tqh_first; ia != 0;
280 ia = ia->ia_list.tqe_next)
281 if (ia->ia_ifp == ifp)
282 break;
283 if (ia == 0)
284 return (EADDRNOTAVAIL);
285 }
286 }
287 ifaddr = satosin(&ia->ia_addr);
288 }
289 if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port,
290 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
291 inp->inp_lport) != 0)
292 return (EADDRINUSE);
293 if (inp->inp_laddr.s_addr == INADDR_ANY) {
294 if (inp->inp_lport == 0)
295 (void)in_pcbbind(inp, (struct mbuf *)0,
296 (struct proc *)0);
297 inp->inp_laddr = ifaddr->sin_addr;
298 }
299 inp->inp_faddr = sin->sin_addr;
300 inp->inp_fport = sin->sin_port;
301 in_pcbrehash(inp);
302 return (0);
303 }
304
305 void
306 in_pcbdisconnect(v)
307 void *v;
308 {
309 struct inpcb *inp = v;
310
311 inp->inp_faddr.s_addr = INADDR_ANY;
312 inp->inp_fport = 0;
313 in_pcbrehash(inp);
314 if (inp->inp_socket->so_state & SS_NOFDREF)
315 in_pcbdetach(inp);
316 }
317
318 void
319 in_pcbdetach(v)
320 void *v;
321 {
322 struct inpcb *inp = v;
323 struct socket *so = inp->inp_socket;
324 int s;
325
326 so->so_pcb = 0;
327 sofree(so);
328 if (inp->inp_options)
329 (void)m_free(inp->inp_options);
330 if (inp->inp_route.ro_rt)
331 rtfree(inp->inp_route.ro_rt);
332 ip_freemoptions(inp->inp_moptions);
333 s = splnet();
334 LIST_REMOVE(inp, inp_hash);
335 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
336 splx(s);
337 FREE(inp, M_PCB);
338 }
339
340 void
341 in_setsockaddr(inp, nam)
342 register struct inpcb *inp;
343 struct mbuf *nam;
344 {
345 register struct sockaddr_in *sin;
346
347 nam->m_len = sizeof (*sin);
348 sin = mtod(nam, struct sockaddr_in *);
349 bzero((caddr_t)sin, sizeof (*sin));
350 sin->sin_family = AF_INET;
351 sin->sin_len = sizeof(*sin);
352 sin->sin_port = inp->inp_lport;
353 sin->sin_addr = inp->inp_laddr;
354 }
355
356 void
357 in_setpeeraddr(inp, nam)
358 struct inpcb *inp;
359 struct mbuf *nam;
360 {
361 register struct sockaddr_in *sin;
362
363 nam->m_len = sizeof (*sin);
364 sin = mtod(nam, struct sockaddr_in *);
365 bzero((caddr_t)sin, sizeof (*sin));
366 sin->sin_family = AF_INET;
367 sin->sin_len = sizeof(*sin);
368 sin->sin_port = inp->inp_fport;
369 sin->sin_addr = inp->inp_faddr;
370 }
371
372 /*
373 * Pass some notification to all connections of a protocol
374 * associated with address dst. The local address and/or port numbers
375 * may be specified to limit the search. The "usual action" will be
376 * taken, depending on the ctlinput cmd. The caller must filter any
377 * cmds that are uninteresting (e.g., no error in the map).
378 * Call the protocol specific routine (if any) to report
379 * any errors for each matching socket.
380 *
381 * Must be called at splsoftnet.
382 */
383 void
384 in_pcbnotify(table, dst, fport_arg, laddr, lport_arg, errno, notify)
385 struct inpcbtable *table;
386 struct sockaddr *dst;
387 u_int fport_arg, lport_arg;
388 struct in_addr laddr;
389 int errno;
390 void (*notify) __P((struct inpcb *, int));
391 {
392 register struct inpcb *inp, *oinp;
393 struct in_addr faddr;
394 u_int16_t fport = fport_arg, lport = lport_arg;
395
396 if (dst->sa_family != AF_INET)
397 return;
398 faddr = satosin(dst)->sin_addr;
399 if (faddr.s_addr == INADDR_ANY)
400 return;
401
402 for (inp = table->inpt_queue.cqh_first;
403 inp != (struct inpcb *)&table->inpt_queue;) {
404 if (inp->inp_faddr.s_addr != faddr.s_addr ||
405 inp->inp_socket == 0 ||
406 inp->inp_fport != fport ||
407 inp->inp_lport != lport ||
408 inp->inp_laddr.s_addr != laddr.s_addr) {
409 inp = inp->inp_queue.cqe_next;
410 continue;
411 }
412 oinp = inp;
413 inp = inp->inp_queue.cqe_next;
414 if (notify)
415 (*notify)(oinp, errno);
416 }
417 }
418
419 void
420 in_pcbnotifyall(table, dst, errno, notify)
421 struct inpcbtable *table;
422 struct sockaddr *dst;
423 int errno;
424 void (*notify) __P((struct inpcb *, int));
425 {
426 register struct inpcb *inp, *oinp;
427 struct in_addr faddr;
428
429 if (dst->sa_family != AF_INET)
430 return;
431 faddr = satosin(dst)->sin_addr;
432 if (faddr.s_addr == INADDR_ANY)
433 return;
434
435 for (inp = table->inpt_queue.cqh_first;
436 inp != (struct inpcb *)&table->inpt_queue;) {
437 if (inp->inp_faddr.s_addr != faddr.s_addr ||
438 inp->inp_socket == 0) {
439 inp = inp->inp_queue.cqe_next;
440 continue;
441 }
442 oinp = inp;
443 inp = inp->inp_queue.cqe_next;
444 if (notify)
445 (*notify)(oinp, errno);
446 }
447 }
448
449 /*
450 * Check for alternatives when higher level complains
451 * about service problems. For now, invalidate cached
452 * routing information. If the route was created dynamically
453 * (by a redirect), time to try a default gateway again.
454 */
455 void
456 in_losing(inp)
457 struct inpcb *inp;
458 {
459 register struct rtentry *rt;
460 struct rt_addrinfo info;
461
462 if ((rt = inp->inp_route.ro_rt)) {
463 inp->inp_route.ro_rt = 0;
464 bzero((caddr_t)&info, sizeof(info));
465 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
466 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
467 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
468 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
469 if (rt->rt_flags & RTF_DYNAMIC)
470 (void) rtrequest(RTM_DELETE, rt_key(rt),
471 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
472 (struct rtentry **)0);
473 else
474 /*
475 * A new route can be allocated
476 * the next time output is attempted.
477 */
478 rtfree(rt);
479 }
480 }
481
482 /*
483 * After a routing change, flush old routing
484 * and allocate a (hopefully) better one.
485 */
486 void
487 in_rtchange(inp, errno)
488 register struct inpcb *inp;
489 int errno;
490 {
491 if (inp->inp_route.ro_rt) {
492 rtfree(inp->inp_route.ro_rt);
493 inp->inp_route.ro_rt = 0;
494 /*
495 * A new route can be allocated the next time
496 * output is attempted.
497 */
498 }
499 }
500
501 struct inpcb *
502 in_pcblookup(table, faddr, fport_arg, laddr, lport_arg, flags)
503 struct inpcbtable *table;
504 struct in_addr faddr, laddr;
505 u_int fport_arg, lport_arg;
506 int flags;
507 {
508 register struct inpcb *inp, *match = 0;
509 int matchwild = 3, wildcard;
510 u_int16_t fport = fport_arg, lport = lport_arg;
511
512 for (inp = table->inpt_queue.cqh_first;
513 inp != (struct inpcb *)&table->inpt_queue;
514 inp = inp->inp_queue.cqe_next) {
515 if (inp->inp_lport != lport)
516 continue;
517 wildcard = 0;
518 if (inp->inp_faddr.s_addr != INADDR_ANY) {
519 if (faddr.s_addr == INADDR_ANY)
520 wildcard++;
521 else if (inp->inp_faddr.s_addr != faddr.s_addr ||
522 inp->inp_fport != fport)
523 continue;
524 } else {
525 if (faddr.s_addr != INADDR_ANY)
526 wildcard++;
527 }
528 if (inp->inp_laddr.s_addr != INADDR_ANY) {
529 if (laddr.s_addr == INADDR_ANY)
530 wildcard++;
531 else if (inp->inp_laddr.s_addr != laddr.s_addr)
532 continue;
533 } else {
534 if (laddr.s_addr != INADDR_ANY)
535 wildcard++;
536 }
537 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
538 continue;
539 if (wildcard < matchwild) {
540 match = inp;
541 matchwild = wildcard;
542 if (matchwild == 0)
543 break;
544 }
545 }
546 return (match);
547 }
548
549 void
550 in_pcbrehash(inp)
551 struct inpcb *inp;
552 {
553 struct inpcbtable *table = inp->inp_table;
554 int s;
555
556 s = splnet();
557 LIST_REMOVE(inp, inp_hash);
558 LIST_INSERT_HEAD(INPCBHASH(table, &inp->inp_faddr, inp->inp_fport,
559 &inp->inp_laddr, inp->inp_lport), inp, inp_hash);
560 splx(s);
561 }
562
563 #ifdef DIAGNOSTIC
564 int in_pcbnotifymiss = 0;
565 #endif
566
567 struct inpcb *
568 in_pcbhashlookup(table, faddr, fport_arg, laddr, lport_arg)
569 struct inpcbtable *table;
570 struct in_addr faddr, laddr;
571 u_int fport_arg, lport_arg;
572 {
573 struct inpcbhead *head;
574 register struct inpcb *inp;
575 u_int16_t fport = fport_arg, lport = lport_arg;
576
577 head = INPCBHASH(table, &faddr, fport, &laddr, lport);
578 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
579 if (inp->inp_faddr.s_addr == faddr.s_addr &&
580 inp->inp_fport == fport &&
581 inp->inp_lport == lport &&
582 inp->inp_laddr.s_addr == laddr.s_addr) {
583 /*
584 * Move this PCB to the head of hash chain so that
585 * repeated accesses are quicker. This is analogous to
586 * the historic single-entry PCB cache.
587 */
588 if (inp != head->lh_first) {
589 LIST_REMOVE(inp, inp_hash);
590 LIST_INSERT_HEAD(head, inp, inp_hash);
591 }
592 break;
593 }
594 }
595 #ifdef DIAGNOSTIC
596 if (inp == NULL && in_pcbnotifymiss) {
597 printf("in_pcbhashlookup: faddr=%08x fport=%d laddr=%08x lport=%d\n",
598 ntohl(faddr.s_addr), ntohs(fport),
599 ntohl(laddr.s_addr), ntohs(lport));
600 }
601 #endif
602 return (inp);
603 }
604