in_pcb.c revision 1.30 1 /* $NetBSD: in_pcb.c,v 1.30 1996/08/14 03:46:48 thorpej 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 = 0;
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 do {
167 if (table->inpt_lastport++ < IPPORT_RESERVED ||
168 table->inpt_lastport > IPPORT_USERRESERVED)
169 table->inpt_lastport = IPPORT_RESERVED;
170 lport = htons(table->inpt_lastport);
171 } while (in_pcblookup(table,
172 zeroin_addr, 0, inp->inp_laddr, lport, wild));
173 inp->inp_lport = lport;
174 in_pcbrehash(inp);
175 return (0);
176 }
177
178 /*
179 * Connect from a socket to a specified address.
180 * Both address and port must be specified in argument sin.
181 * If don't have a local address for this socket yet,
182 * then pick one.
183 */
184 int
185 in_pcbconnect(v, nam)
186 register void *v;
187 struct mbuf *nam;
188 {
189 register struct inpcb *inp = v;
190 struct in_ifaddr *ia;
191 struct sockaddr_in *ifaddr = NULL;
192 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
193
194 if (nam->m_len != sizeof (*sin))
195 return (EINVAL);
196 if (sin->sin_family != AF_INET)
197 return (EAFNOSUPPORT);
198 if (sin->sin_port == 0)
199 return (EADDRNOTAVAIL);
200 if (in_ifaddr.tqh_first != 0) {
201 /*
202 * If the destination address is INADDR_ANY,
203 * use the primary local address.
204 * If the supplied address is INADDR_BROADCAST,
205 * and the primary interface supports broadcast,
206 * choose the broadcast address for that interface.
207 */
208 if (sin->sin_addr.s_addr == INADDR_ANY)
209 sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr;
210 else if (sin->sin_addr.s_addr == INADDR_BROADCAST &&
211 (in_ifaddr.tqh_first->ia_ifp->if_flags & IFF_BROADCAST))
212 sin->sin_addr = in_ifaddr.tqh_first->ia_broadaddr.sin_addr;
213 }
214 if (inp->inp_laddr.s_addr == INADDR_ANY) {
215 register struct route *ro;
216
217 ia = (struct in_ifaddr *)0;
218 /*
219 * If route is known or can be allocated now,
220 * our src addr is taken from the i/f, else punt.
221 */
222 ro = &inp->inp_route;
223 if (ro->ro_rt &&
224 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
225 sin->sin_addr.s_addr ||
226 inp->inp_socket->so_options & SO_DONTROUTE)) {
227 RTFREE(ro->ro_rt);
228 ro->ro_rt = (struct rtentry *)0;
229 }
230 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
231 (ro->ro_rt == (struct rtentry *)0 ||
232 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
233 /* No route yet, so try to acquire one */
234 ro->ro_dst.sa_family = AF_INET;
235 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
236 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
237 rtalloc(ro);
238 }
239 /*
240 * If we found a route, use the address
241 * corresponding to the outgoing interface
242 * unless it is the loopback (in case a route
243 * to our address on another net goes to loopback).
244 */
245 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
246 ia = ifatoia(ro->ro_rt->rt_ifa);
247 if (ia == 0) {
248 u_int16_t fport = sin->sin_port;
249
250 sin->sin_port = 0;
251 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
252 sin->sin_port = fport;
253 if (ia == 0)
254 ia = in_ifaddr.tqh_first;
255 if (ia == 0)
256 return (EADDRNOTAVAIL);
257 }
258 /*
259 * If the destination address is multicast and an outgoing
260 * interface has been set as a multicast option, use the
261 * address of that interface as our source address.
262 */
263 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
264 inp->inp_moptions != NULL) {
265 struct ip_moptions *imo;
266 struct ifnet *ifp;
267
268 imo = inp->inp_moptions;
269 if (imo->imo_multicast_ifp != NULL) {
270 ifp = imo->imo_multicast_ifp;
271 for (ia = in_ifaddr.tqh_first; ia != 0;
272 ia = ia->ia_list.tqe_next)
273 if (ia->ia_ifp == ifp)
274 break;
275 if (ia == 0)
276 return (EADDRNOTAVAIL);
277 }
278 }
279 ifaddr = satosin(&ia->ia_addr);
280 }
281 if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port,
282 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
283 inp->inp_lport) != 0)
284 return (EADDRINUSE);
285 if (inp->inp_laddr.s_addr == INADDR_ANY) {
286 if (inp->inp_lport == 0)
287 (void)in_pcbbind(inp, (struct mbuf *)0,
288 (struct proc *)0);
289 inp->inp_laddr = ifaddr->sin_addr;
290 }
291 inp->inp_faddr = sin->sin_addr;
292 inp->inp_fport = sin->sin_port;
293 in_pcbrehash(inp);
294 return (0);
295 }
296
297 void
298 in_pcbdisconnect(v)
299 void *v;
300 {
301 struct inpcb *inp = v;
302
303 inp->inp_faddr.s_addr = INADDR_ANY;
304 inp->inp_fport = 0;
305 in_pcbrehash(inp);
306 if (inp->inp_socket->so_state & SS_NOFDREF)
307 in_pcbdetach(inp);
308 }
309
310 void
311 in_pcbdetach(v)
312 void *v;
313 {
314 struct inpcb *inp = v;
315 struct socket *so = inp->inp_socket;
316 int s;
317
318 so->so_pcb = 0;
319 sofree(so);
320 if (inp->inp_options)
321 (void)m_free(inp->inp_options);
322 if (inp->inp_route.ro_rt)
323 rtfree(inp->inp_route.ro_rt);
324 ip_freemoptions(inp->inp_moptions);
325 s = splnet();
326 LIST_REMOVE(inp, inp_hash);
327 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
328 splx(s);
329 FREE(inp, M_PCB);
330 }
331
332 void
333 in_setsockaddr(inp, nam)
334 register struct inpcb *inp;
335 struct mbuf *nam;
336 {
337 register struct sockaddr_in *sin;
338
339 nam->m_len = sizeof (*sin);
340 sin = mtod(nam, struct sockaddr_in *);
341 bzero((caddr_t)sin, sizeof (*sin));
342 sin->sin_family = AF_INET;
343 sin->sin_len = sizeof(*sin);
344 sin->sin_port = inp->inp_lport;
345 sin->sin_addr = inp->inp_laddr;
346 }
347
348 void
349 in_setpeeraddr(inp, nam)
350 struct inpcb *inp;
351 struct mbuf *nam;
352 {
353 register struct sockaddr_in *sin;
354
355 nam->m_len = sizeof (*sin);
356 sin = mtod(nam, struct sockaddr_in *);
357 bzero((caddr_t)sin, sizeof (*sin));
358 sin->sin_family = AF_INET;
359 sin->sin_len = sizeof(*sin);
360 sin->sin_port = inp->inp_fport;
361 sin->sin_addr = inp->inp_faddr;
362 }
363
364 /*
365 * Pass some notification to all connections of a protocol
366 * associated with address dst. The local address and/or port numbers
367 * may be specified to limit the search. The "usual action" will be
368 * taken, depending on the ctlinput cmd. The caller must filter any
369 * cmds that are uninteresting (e.g., no error in the map).
370 * Call the protocol specific routine (if any) to report
371 * any errors for each matching socket.
372 *
373 * Must be called at splsoftnet.
374 */
375 void
376 in_pcbnotify(table, dst, fport_arg, laddr, lport_arg, errno, notify)
377 struct inpcbtable *table;
378 struct sockaddr *dst;
379 u_int fport_arg, lport_arg;
380 struct in_addr laddr;
381 int errno;
382 void (*notify) __P((struct inpcb *, int));
383 {
384 register struct inpcb *inp, *oinp;
385 struct in_addr faddr;
386 u_int16_t fport = fport_arg, lport = lport_arg;
387
388 if (dst->sa_family != AF_INET)
389 return;
390 faddr = satosin(dst)->sin_addr;
391 if (faddr.s_addr == INADDR_ANY)
392 return;
393
394 for (inp = table->inpt_queue.cqh_first;
395 inp != (struct inpcb *)&table->inpt_queue;) {
396 if (inp->inp_faddr.s_addr != faddr.s_addr ||
397 inp->inp_socket == 0 ||
398 inp->inp_fport != fport ||
399 inp->inp_lport != lport ||
400 inp->inp_laddr.s_addr != laddr.s_addr) {
401 inp = inp->inp_queue.cqe_next;
402 continue;
403 }
404 oinp = inp;
405 inp = inp->inp_queue.cqe_next;
406 if (notify)
407 (*notify)(oinp, errno);
408 }
409 }
410
411 void
412 in_pcbnotifyall(table, dst, errno, notify)
413 struct inpcbtable *table;
414 struct sockaddr *dst;
415 int errno;
416 void (*notify) __P((struct inpcb *, int));
417 {
418 register struct inpcb *inp, *oinp;
419 struct in_addr faddr;
420
421 if (dst->sa_family != AF_INET)
422 return;
423 faddr = satosin(dst)->sin_addr;
424 if (faddr.s_addr == INADDR_ANY)
425 return;
426
427 for (inp = table->inpt_queue.cqh_first;
428 inp != (struct inpcb *)&table->inpt_queue;) {
429 if (inp->inp_faddr.s_addr != faddr.s_addr ||
430 inp->inp_socket == 0) {
431 inp = inp->inp_queue.cqe_next;
432 continue;
433 }
434 oinp = inp;
435 inp = inp->inp_queue.cqe_next;
436 if (notify)
437 (*notify)(oinp, errno);
438 }
439 }
440
441 /*
442 * Check for alternatives when higher level complains
443 * about service problems. For now, invalidate cached
444 * routing information. If the route was created dynamically
445 * (by a redirect), time to try a default gateway again.
446 */
447 void
448 in_losing(inp)
449 struct inpcb *inp;
450 {
451 register struct rtentry *rt;
452 struct rt_addrinfo info;
453
454 if ((rt = inp->inp_route.ro_rt)) {
455 inp->inp_route.ro_rt = 0;
456 bzero((caddr_t)&info, sizeof(info));
457 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
458 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
459 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
460 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
461 if (rt->rt_flags & RTF_DYNAMIC)
462 (void) rtrequest(RTM_DELETE, rt_key(rt),
463 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
464 (struct rtentry **)0);
465 else
466 /*
467 * A new route can be allocated
468 * the next time output is attempted.
469 */
470 rtfree(rt);
471 }
472 }
473
474 /*
475 * After a routing change, flush old routing
476 * and allocate a (hopefully) better one.
477 */
478 void
479 in_rtchange(inp, errno)
480 register struct inpcb *inp;
481 int errno;
482 {
483 if (inp->inp_route.ro_rt) {
484 rtfree(inp->inp_route.ro_rt);
485 inp->inp_route.ro_rt = 0;
486 /*
487 * A new route can be allocated the next time
488 * output is attempted.
489 */
490 }
491 }
492
493 struct inpcb *
494 in_pcblookup(table, faddr, fport_arg, laddr, lport_arg, flags)
495 struct inpcbtable *table;
496 struct in_addr faddr, laddr;
497 u_int fport_arg, lport_arg;
498 int flags;
499 {
500 register struct inpcb *inp, *match = 0;
501 int matchwild = 3, wildcard;
502 u_int16_t fport = fport_arg, lport = lport_arg;
503
504 for (inp = table->inpt_queue.cqh_first;
505 inp != (struct inpcb *)&table->inpt_queue;
506 inp = inp->inp_queue.cqe_next) {
507 if (inp->inp_lport != lport)
508 continue;
509 wildcard = 0;
510 if (inp->inp_faddr.s_addr != INADDR_ANY) {
511 if (faddr.s_addr == INADDR_ANY)
512 wildcard++;
513 else if (inp->inp_faddr.s_addr != faddr.s_addr ||
514 inp->inp_fport != fport)
515 continue;
516 } else {
517 if (faddr.s_addr != INADDR_ANY)
518 wildcard++;
519 }
520 if (inp->inp_laddr.s_addr != INADDR_ANY) {
521 if (laddr.s_addr == INADDR_ANY)
522 wildcard++;
523 else if (inp->inp_laddr.s_addr != laddr.s_addr)
524 continue;
525 } else {
526 if (laddr.s_addr != INADDR_ANY)
527 wildcard++;
528 }
529 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
530 continue;
531 if (wildcard < matchwild) {
532 match = inp;
533 matchwild = wildcard;
534 if (matchwild == 0)
535 break;
536 }
537 }
538 return (match);
539 }
540
541 void
542 in_pcbrehash(inp)
543 struct inpcb *inp;
544 {
545 struct inpcbtable *table = inp->inp_table;
546 int s;
547
548 s = splnet();
549 LIST_REMOVE(inp, inp_hash);
550 LIST_INSERT_HEAD(INPCBHASH(table, &inp->inp_faddr, inp->inp_fport,
551 &inp->inp_laddr, inp->inp_lport), inp, inp_hash);
552 splx(s);
553 }
554
555 #ifdef DIAGNOSTIC
556 int in_pcbnotifymiss = 0;
557 #endif
558
559 struct inpcb *
560 in_pcbhashlookup(table, faddr, fport_arg, laddr, lport_arg)
561 struct inpcbtable *table;
562 struct in_addr faddr, laddr;
563 u_int fport_arg, lport_arg;
564 {
565 struct inpcbhead *head;
566 register struct inpcb *inp;
567 u_int16_t fport = fport_arg, lport = lport_arg;
568
569 head = INPCBHASH(table, &faddr, fport, &laddr, lport);
570 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
571 if (inp->inp_faddr.s_addr == faddr.s_addr &&
572 inp->inp_fport == fport &&
573 inp->inp_lport == lport &&
574 inp->inp_laddr.s_addr == laddr.s_addr) {
575 /*
576 * Move this PCB to the head of hash chain so that
577 * repeated accesses are quicker. This is analogous to
578 * the historic single-entry PCB cache.
579 */
580 if (inp != head->lh_first) {
581 LIST_REMOVE(inp, inp_hash);
582 LIST_INSERT_HEAD(head, inp, inp_hash);
583 }
584 break;
585 }
586 }
587 #ifdef DIAGNOSTIC
588 if (inp == NULL && in_pcbnotifymiss) {
589 printf("in_pcbhashlookup: faddr=%08x fport=%d laddr=%08x lport=%d\n",
590 ntohl(faddr.s_addr), ntohs(fport),
591 ntohl(laddr.s_addr), ntohs(lport));
592 }
593 #endif
594 return (inp);
595 }
596