in_pcb.c revision 1.25 1 /* $NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos 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)
102 register void *v;
103 struct mbuf *nam;
104 {
105 register struct inpcb *inp = v;
106 register struct socket *so = inp->inp_socket;
107 register struct inpcbtable *table = inp->inp_table;
108 register struct sockaddr_in *sin;
109 struct proc *p = curproc; /* XXX */
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) {
123 sin = mtod(nam, struct sockaddr_in *);
124 if (nam->m_len != sizeof (*sin))
125 return (EINVAL);
126 #ifdef notdef
127 /*
128 * We should check the family, but old programs
129 * incorrectly fail to initialize it.
130 */
131 if (sin->sin_family != AF_INET)
132 return (EAFNOSUPPORT);
133 #endif
134 lport = sin->sin_port;
135 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
136 /*
137 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
138 * allow complete duplication of binding if
139 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
140 * and a multicast address is bound on both
141 * new and duplicated sockets.
142 */
143 if (so->so_options & SO_REUSEADDR)
144 reuseport = SO_REUSEADDR|SO_REUSEPORT;
145 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
146 sin->sin_port = 0; /* yech... */
147 if (ifa_ifwithaddr(sintosa(sin)) == 0)
148 return (EADDRNOTAVAIL);
149 }
150 if (lport) {
151 struct inpcb *t;
152
153 /* GROSS */
154 if (ntohs(lport) < IPPORT_RESERVED &&
155 (error = suser(p->p_ucred, &p->p_acflag)))
156 return (EACCES);
157 t = in_pcblookup(table, zeroin_addr, 0,
158 sin->sin_addr, lport, wild);
159 if (t && (reuseport & t->inp_socket->so_options) == 0)
160 return (EADDRINUSE);
161 }
162 inp->inp_laddr = sin->sin_addr;
163 }
164 if (lport == 0)
165 do {
166 if (table->inpt_lastport++ < IPPORT_RESERVED ||
167 table->inpt_lastport > IPPORT_USERRESERVED)
168 table->inpt_lastport = IPPORT_RESERVED;
169 lport = htons(table->inpt_lastport);
170 } while (in_pcblookup(table,
171 zeroin_addr, 0, inp->inp_laddr, lport, wild));
172 inp->inp_lport = lport;
173 in_pcbrehash(inp);
174 return (0);
175 }
176
177 /*
178 * Connect from a socket to a specified address.
179 * Both address and port must be specified in argument sin.
180 * If don't have a local address for this socket yet,
181 * then pick one.
182 */
183 int
184 in_pcbconnect(v, nam)
185 register void *v;
186 struct mbuf *nam;
187 {
188 register struct inpcb *inp = v;
189 struct in_ifaddr *ia;
190 struct sockaddr_in *ifaddr = NULL;
191 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
192
193 if (nam->m_len != sizeof (*sin))
194 return (EINVAL);
195 if (sin->sin_family != AF_INET)
196 return (EAFNOSUPPORT);
197 if (sin->sin_port == 0)
198 return (EADDRNOTAVAIL);
199 if (in_ifaddr.tqh_first != 0) {
200 /*
201 * If the destination address is INADDR_ANY,
202 * use the primary local address.
203 * If the supplied address is INADDR_BROADCAST,
204 * and the primary interface supports broadcast,
205 * choose the broadcast address for that interface.
206 */
207 if (sin->sin_addr.s_addr == INADDR_ANY)
208 sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr;
209 else if (sin->sin_addr.s_addr == INADDR_BROADCAST &&
210 (in_ifaddr.tqh_first->ia_ifp->if_flags & IFF_BROADCAST))
211 sin->sin_addr = in_ifaddr.tqh_first->ia_broadaddr.sin_addr;
212 }
213 if (inp->inp_laddr.s_addr == INADDR_ANY) {
214 register struct route *ro;
215
216 ia = (struct in_ifaddr *)0;
217 /*
218 * If route is known or can be allocated now,
219 * our src addr is taken from the i/f, else punt.
220 */
221 ro = &inp->inp_route;
222 if (ro->ro_rt &&
223 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
224 sin->sin_addr.s_addr ||
225 inp->inp_socket->so_options & SO_DONTROUTE)) {
226 RTFREE(ro->ro_rt);
227 ro->ro_rt = (struct rtentry *)0;
228 }
229 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
230 (ro->ro_rt == (struct rtentry *)0 ||
231 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
232 /* No route yet, so try to acquire one */
233 ro->ro_dst.sa_family = AF_INET;
234 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
235 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
236 rtalloc(ro);
237 }
238 /*
239 * If we found a route, use the address
240 * corresponding to the outgoing interface
241 * unless it is the loopback (in case a route
242 * to our address on another net goes to loopback).
243 */
244 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
245 ia = ifatoia(ro->ro_rt->rt_ifa);
246 if (ia == 0) {
247 u_int16_t fport = sin->sin_port;
248
249 sin->sin_port = 0;
250 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
251 if (ia == 0)
252 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
253 sin->sin_port = fport;
254 if (ia == 0)
255 ia = in_ifaddr.tqh_first;
256 if (ia == 0)
257 return (EADDRNOTAVAIL);
258 }
259 /*
260 * If the destination address is multicast and an outgoing
261 * interface has been set as a multicast option, use the
262 * address of that interface as our source address.
263 */
264 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
265 inp->inp_moptions != NULL) {
266 struct ip_moptions *imo;
267 struct ifnet *ifp;
268
269 imo = inp->inp_moptions;
270 if (imo->imo_multicast_ifp != NULL) {
271 ifp = imo->imo_multicast_ifp;
272 for (ia = in_ifaddr.tqh_first; ia != 0;
273 ia = ia->ia_list.tqe_next)
274 if (ia->ia_ifp == ifp)
275 break;
276 if (ia == 0)
277 return (EADDRNOTAVAIL);
278 }
279 }
280 ifaddr = satosin(&ia->ia_addr);
281 }
282 if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port,
283 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
284 inp->inp_lport) != 0)
285 return (EADDRINUSE);
286 if (inp->inp_laddr.s_addr == INADDR_ANY) {
287 if (inp->inp_lport == 0)
288 (void)in_pcbbind(inp, (struct mbuf *)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