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