1 /* $NetBSD: iso_pcb.c,v 1.48 2009/04/16 21:37:17 elad Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)iso_pcb.c 8.3 (Berkeley) 7/19/94
34 /***********************************************************
35 Copyright IBM Corporation 1987
39 Permission to use, copy, modify, and distribute this software and its
40 documentation for any purpose and without fee is hereby granted,
41 provided that the above copyright notice appear in all copies and that
42 both that copyright notice and this permission notice appear in
43 supporting documentation, and that the name of IBM not be
44 used in advertising or publicity pertaining to distribution of the
45 software without specific, written prior permission.
47 IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
48 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
49 IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
50 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
51 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
52 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 ******************************************************************/
58 * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
61 * Iso address family net-layer(s) pcb stuff. NEH 1/29/87
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: iso_pcb.c,v 1.48 2009/04/16 21:37:17 elad Exp $");
71 #include <sys/param.h>
72 #include <sys/systm.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/errno.h>
78 #include <sys/kauth.h>
80 #include <netiso/argo_debug.h>
81 #include <netiso/iso.h>
82 #include <netiso/clnp.h>
83 #include <netinet/in_systm.h>
85 #include <net/route.h>
86 #include <netiso/iso_pcb.h>
87 #include <netiso/iso_var.h>
88 #include <sys/protosw.h>
90 const struct iso_addr zeroiso_addr
;
93 unsigned char argo_debug
[128];
97 * FUNCTION: iso_pcballoc
99 * PURPOSE: creates an isopcb structure in an mbuf,
100 * with socket (so), and
101 * puts it in the queue with head (head)
103 * RETURNS: 0 if OK, ENOBUFS if can't alloc the necessary mbuf
106 iso_pcballoc(struct socket
*so
, void *v
)
108 struct isopcb
*head
= v
;
112 if (argo_debug
[D_ISO
]) {
113 printf("iso_pcballoc(so %p)\n", so
);
116 isop
= malloc(sizeof(*isop
), M_PCB
, M_NOWAIT
|M_ZERO
);
119 isop
->isop_head
= head
;
120 isop
->isop_socket
= so
;
121 iso_insque(isop
, head
);
128 * FUNCTION: iso_pcbbind
130 * PURPOSE: binds the address given in *(nam) to the socket
131 * specified by the isopcb in *(isop)
132 * If the given address is zero, it makes sure the
133 * address isn't already in use and if it's got a network
134 * portion, we look for an interface with that network
135 * address. If the address given is zero, we allocate
136 * a port and stuff it in the (nam) structure.
138 * RETURNS: errno E* or 0 if ok.
140 * SIDE EFFECTS: increments head->isop_lport if it allocates a port #
145 iso_pcbbind(void *v
, struct mbuf
*nam
, struct lwp
*l
)
147 struct isopcb
*isop
= v
;
148 struct isopcb
*head
= isop
->isop_head
;
149 struct sockaddr_iso
*siso
;
150 struct iso_ifaddr
*ia
;
157 if (argo_debug
[D_ISO
]) {
158 printf("iso_pcbbind(isop %p, nam %p)\n", isop
, nam
);
162 if (iso_ifaddr
.tqh_first
== 0) /* any interfaces attached? */
163 return EADDRNOTAVAIL
;
164 if (isop
->isop_laddr
) /* already bound */
166 if (nam
== (struct mbuf
*) 0) {
167 isop
->isop_laddr
= &isop
->isop_sladdr
;
168 isop
->isop_sladdr
.siso_len
= sizeof(struct sockaddr_iso
);
169 isop
->isop_sladdr
.siso_family
= AF_ISO
;
170 isop
->isop_sladdr
.siso_tlen
= 2;
171 isop
->isop_sladdr
.siso_nlen
= 0;
172 isop
->isop_sladdr
.siso_slen
= 0;
173 isop
->isop_sladdr
.siso_plen
= 0;
176 siso
= mtod(nam
, struct sockaddr_iso
*);
178 if (argo_debug
[D_ISO
]) {
179 printf("iso_pcbbind(name len 0x%x)\n", nam
->m_len
);
180 printf("The address is %s\n", clnp_iso_addrp(&siso
->siso_addr
));
184 * We would like sort of length check but since some OSI addrs
185 * do not have fixed length, we can't really do much.
186 * The ONLY thing we can say is that an osi addr has to have
187 * at LEAST an afi and one more byte and had better fit into
189 * However, in fact the size of the whole thing is a struct
190 * sockaddr_iso, so probably this is what we should check for.
192 if ((nam
->m_len
< 2) || (nam
->m_len
< siso
->siso_len
)) {
195 if (siso
->siso_nlen
) {
196 /* non-zero net addr- better match one of our interfaces */
198 if (argo_debug
[D_ISO
]) {
199 printf("iso_pcbbind: bind to NOT zeroisoaddr\n");
202 for (ia
= iso_ifaddr
.tqh_first
; ia
!= 0; ia
= ia
->ia_list
.tqe_next
)
203 if (SAME_ISOIFADDR(siso
, &ia
->ia_addr
))
206 return EADDRNOTAVAIL
;
208 if (siso
->siso_len
<= sizeof(isop
->isop_sladdr
)) {
209 isop
->isop_laddr
= &isop
->isop_sladdr
;
211 if ((nam
= m_copy(nam
, 0, (int) M_COPYALL
)) == 0)
213 isop
->isop_mladdr
= nam
;
214 isop
->isop_laddr
= mtod(nam
, struct sockaddr_iso
*);
216 memcpy((void *) isop
->isop_laddr
, (void *) siso
, siso
->siso_len
);
217 if (siso
->siso_tlen
== 0)
219 if ((isop
->isop_socket
->so_options
& SO_REUSEADDR
) == 0 &&
220 iso_pcblookup(head
, 0, (void *) 0, isop
->isop_laddr
))
222 if (siso
->siso_tlen
<= 2) {
223 memcpy(suf
.data
, TSEL(siso
), sizeof(suf
.data
));
224 suf
.s
= ntohs(suf
.s
);
225 if (suf
.s
< ISO_PORT_RESERVED
) {
231 error
= kauth_authorize_network(l
->l_cred
,
233 KAUTH_REQ_NETWORK_BIND_PRIVPORT
,
234 isop
->isop_socket
, siso
, NULL
);
242 cp
= WRITABLE_TSEL(isop
->isop_laddr
);
243 isop
->isop_laddr
->siso_tlen
= 2;
245 if (argo_debug
[D_ISO
]) {
246 printf("iso_pcbbind noname\n");
250 if (head
->isop_lport
++ < ISO_PORT_RESERVED
||
251 head
->isop_lport
> ISO_PORT_USERRESERVED
)
252 head
->isop_lport
= ISO_PORT_RESERVED
;
253 suf
.s
= htons(head
->isop_lport
);
256 } while (iso_pcblookup(head
, 0, (void *) 0, isop
->isop_laddr
));
259 if (argo_debug
[D_ISO
]) {
260 printf("iso_pcbbind returns 0, suf 0x%x\n", suf
.s
);
266 * FUNCTION: iso_pcbconnect
268 * PURPOSE: Make the isopcb (isop) look like it's connected.
269 * In other words, give it the peer address given in
270 * the mbuf * (nam). Make sure such a combination
271 * of local, peer addresses doesn't already exist
272 * for this protocol. Internet mentality prevails here,
273 * wherein a src,dst pair uniquely identifies a connection.
274 * Both net address and port must be specified in argument
276 * If we don't have a local address for this socket yet,
277 * we pick one by calling iso_pcbbind().
279 * RETURNS: errno E* or 0 if ok.
281 * SIDE EFFECTS: Looks up a route, which may cause one to be left
287 iso_pcbconnect(void *v
, struct mbuf
*nam
, struct lwp
*l
)
289 struct isopcb
*isop
= v
;
290 struct sockaddr_iso
*siso
= mtod(nam
, struct sockaddr_iso
*);
291 int local_zero
, error
= 0;
292 struct iso_ifaddr
*ia
;
295 if (argo_debug
[D_ISO
]) {
296 printf("iso_pcbconnect(isop %p sock %p nam %p",
297 isop
, isop
->isop_socket
, nam
);
298 printf("nam->m_len 0x%x), addr:\n", nam
->m_len
);
302 if (nam
->m_len
< siso
->siso_len
)
304 if (siso
->siso_family
!= AF_ISO
)
306 if (siso
->siso_nlen
== 0) {
307 if ((ia
= iso_ifaddr
.tqh_first
) != NULL
) {
308 int nlen
= ia
->ia_addr
.siso_nlen
;
310 tmp
= WRITABLE_TSEL(siso
);
311 memmove(tmp
+ nlen
, TSEL(siso
),
312 siso
->siso_plen
+ siso
->siso_tlen
+
314 bcopy((void *) & ia
->ia_addr
.siso_addr
,
315 (void *) & siso
->siso_addr
, nlen
+ 1);
316 /* includes siso->siso_nlen = nlen; */
318 return EADDRNOTAVAIL
;
321 * Local zero means either not bound, or bound to a TSEL, but no
322 * particular local interface. So, if we want to send somebody
323 * we need to choose a return address.
326 ((isop
->isop_laddr
== 0) || (isop
->isop_laddr
->siso_nlen
== 0));
331 if (argo_debug
[D_ISO
]) {
332 printf("iso_pcbconnect localzero 1\n");
336 * If route is known or can be allocated now, our src addr is
337 * taken from the i/f, else punt.
339 flags
= isop
->isop_socket
->so_options
& SO_DONTROUTE
;
340 error
= clnp_route(&siso
->siso_addr
, &isop
->isop_route
, flags
,
345 if (argo_debug
[D_ISO
]) {
346 printf("iso_pcbconnect localzero 2, rt %p",
347 rtcache_validate(&isop
->isop_route
));
348 printf(" ia %p\n", ia
);
353 if (argo_debug
[D_ISO
]) {
354 printf("in iso_pcbconnect before lookup isop %p isop->sock %p\n",
355 isop
, isop
->isop_socket
);
359 int nlen
, tlen
, totlen
;
362 siso
= isop
->isop_laddr
;
363 if (siso
== 0 || siso
->siso_tlen
== 0)
364 (void) iso_pcbbind(isop
, (struct mbuf
*)0,
367 * Here we have problem of squezeing in a definite network address
368 * into an existing sockaddr_iso, which in fact may not have room
369 * for it. This gets messy.
371 siso
= isop
->isop_laddr
;
372 oldtsel
= TSEL(siso
);
373 tlen
= siso
->siso_tlen
;
374 nlen
= ia
->ia_addr
.siso_nlen
;
375 totlen
= tlen
+ nlen
+ offsetof(struct sockaddr_iso
, siso_data
[0]);
376 if ((siso
== &isop
->isop_sladdr
) &&
377 (totlen
> sizeof(isop
->isop_sladdr
))) {
378 struct mbuf
*m
= m_get(M_DONTWAIT
, MT_SONAME
);
382 isop
->isop_mladdr
= m
;
383 isop
->isop_laddr
= siso
= mtod(m
, struct sockaddr_iso
*);
385 siso
->siso_nlen
= ia
->ia_addr
.siso_nlen
;
386 newtsel
= WRITABLE_TSEL(siso
);
387 memmove(newtsel
, oldtsel
, tlen
);
388 memcpy(siso
->siso_data
, ia
->ia_addr
.siso_data
, nlen
);
389 siso
->siso_tlen
= tlen
;
390 siso
->siso_family
= AF_ISO
;
391 siso
->siso_len
= totlen
;
392 siso
= mtod(nam
, struct sockaddr_iso
*);
395 if (argo_debug
[D_ISO
]) {
396 printf("in iso_pcbconnect before bcopy isop %p isop->sock %p\n",
397 isop
, isop
->isop_socket
);
401 * If we had to allocate space to a previous big foreign address,
402 * and for some reason we didn't free it, we reuse it knowing
403 * that is going to be big enough, as sockaddrs are delivered in
405 * If the foreign address is small enough, we use default space;
406 * otherwise, we grab an mbuf to copy into.
408 if (isop
->isop_faddr
== 0 || isop
->isop_faddr
== &isop
->isop_sfaddr
) {
409 if (siso
->siso_len
<= sizeof(isop
->isop_sfaddr
))
410 isop
->isop_faddr
= &isop
->isop_sfaddr
;
412 struct mbuf
*m
= m_get(M_DONTWAIT
, MT_SONAME
);
415 isop
->isop_mfaddr
= m
;
416 isop
->isop_faddr
= mtod(m
, struct sockaddr_iso
*);
419 memcpy((void *) isop
->isop_faddr
, (void *) siso
, siso
->siso_len
);
421 if (argo_debug
[D_ISO
]) {
422 printf("in iso_pcbconnect after bcopy isop %p isop->sock %p\n",
423 isop
, isop
->isop_socket
);
424 printf("iso_pcbconnect connected to addr:\n");
425 dump_isoaddr(isop
->isop_faddr
);
426 printf("iso_pcbconnect end: src addr:\n");
427 dump_isoaddr(isop
->isop_laddr
);
434 * FUNCTION: iso_pcbdisconnect()
436 * PURPOSE: washes away the peer address info so the socket
437 * appears to be disconnected.
438 * If there's no file descriptor associated with the socket
439 * it detaches the pcb.
443 * SIDE EFFECTS: May detach the pcb.
448 iso_pcbdisconnect(void *v
)
450 struct isopcb
*isop
= v
;
451 struct sockaddr_iso
*siso
;
454 if (argo_debug
[D_ISO
]) {
455 printf("iso_pcbdisconnect(isop %p)\n", isop
);
459 * Preserver binding infnormation if already bound.
461 if ((siso
= isop
->isop_laddr
) && siso
->siso_nlen
&& siso
->siso_tlen
) {
462 const void *otsel
= TSEL(siso
);
464 memmove(WRITABLE_TSEL(siso
), otsel
, siso
->siso_tlen
);
466 if (isop
->isop_faddr
&& isop
->isop_faddr
!= &isop
->isop_sfaddr
)
467 m_freem(isop
->isop_mfaddr
);
468 isop
->isop_faddr
= 0;
469 if (isop
->isop_socket
->so_state
& SS_NOFDREF
)
474 * FUNCTION: iso_pcbdetach
476 * PURPOSE: detach the pcb at *(isop) from it's socket and free
477 * the mbufs associated with the pcb..
478 * Dequeues (isop) from its head.
487 iso_pcbdetach(void *v
)
489 struct isopcb
*isop
= v
;
490 struct socket
*so
= isop
->isop_socket
;
493 if (argo_debug
[D_ISO
]) {
494 printf("iso_pcbdetach(isop %p socket %p so %p)\n",
495 isop
, isop
->isop_socket
, so
);
498 if (so
) { /* in the x.25 domain, we sometimes have no
501 /* sofree drops the lock */
503 mutex_enter(softnet_lock
);
506 if (argo_debug
[D_ISO
]) {
507 printf("iso_pcbdetach 2 \n");
510 if (isop
->isop_options
)
511 (void) m_free(isop
->isop_options
);
513 if (argo_debug
[D_ISO
]) {
514 printf("iso_pcbdetach 3 \n");
517 rtcache_free(&isop
->isop_route
);
519 if (argo_debug
[D_ISO
]) {
520 printf("iso_pcbdetach 3.1\n");
523 if (isop
->isop_clnpcache
!= NULL
) {
524 struct clnp_cache
*clcp
=
525 mtod(isop
->isop_clnpcache
, struct clnp_cache
*);
527 if (argo_debug
[D_ISO
]) {
528 printf("iso_pcbdetach 3.2: clcp %p freeing clc_hdr %p\n",
529 clcp
, clcp
->clc_hdr
);
532 if (clcp
->clc_hdr
!= NULL
)
533 m_free(clcp
->clc_hdr
);
535 if (argo_debug
[D_ISO
]) {
536 printf("iso_pcbdetach 3.3: freeing cache %p\n",
537 isop
->isop_clnpcache
);
540 m_free(isop
->isop_clnpcache
);
543 if (argo_debug
[D_ISO
]) {
544 printf("iso_pcbdetach 4 \n");
549 if (argo_debug
[D_ISO
]) {
550 printf("iso_pcbdetach 5 \n");
553 if (isop
->isop_laddr
&& (isop
->isop_laddr
!= &isop
->isop_sladdr
))
554 m_freem(isop
->isop_mladdr
);
555 free((void *) isop
, M_PCB
);
560 * FUNCTION: iso_pcbnotify
562 * PURPOSE: notify all connections in this protocol's queue (head)
563 * that have peer address (dst) of the problem (errno)
564 * by calling (notify) on the connections' isopcbs.
570 * NOTES: (notify) is called at splnet!
573 iso_pcbnotify(struct isopcb
*head
, const struct sockaddr_iso
*siso
, int errno
,
574 void (*notify
) (struct isopcb
*))
580 if (argo_debug
[D_ISO
]) {
581 printf("iso_pcbnotify(head %p, notify %p) dst:\n",
585 for (isop
= head
->isop_next
; isop
!= head
; isop
= isop
->isop_next
) {
586 if (isop
->isop_socket
== 0 || isop
->isop_faddr
== 0 ||
587 !SAME_ISOADDR(siso
, isop
->isop_faddr
)) {
589 if (argo_debug
[D_ISO
]) {
590 printf("iso_pcbnotify: CONTINUE isop %p, sock %p\n",
591 isop
, isop
->isop_socket
);
592 printf("addrmatch cmp'd with (%p):\n",
594 dump_isoaddr(isop
->isop_faddr
);
600 isop
->isop_socket
->so_error
= errno
;
606 if (argo_debug
[D_ISO
]) {
607 printf("END OF iso_pcbnotify\n");
614 * FUNCTION: iso_pcblookup
616 * PURPOSE: looks for a given combination of (faddr), (fport),
617 * (lport), (laddr) in the queue named by (head).
618 * Argument (flags) is ignored.
620 * RETURNS: ptr to the isopcb if it finds a connection matching
621 * these arguments, o.w. returns zero.
632 const struct sockaddr_iso
*laddr
)
635 const void *lp
= TSEL(laddr
);
636 unsigned int llen
= laddr
->siso_tlen
;
639 if (argo_debug
[D_ISO
]) {
640 printf("iso_pcblookup(head %p laddr %p fport %p)\n",
644 for (isop
= head
->isop_next
; isop
!= head
; isop
= isop
->isop_next
) {
645 if (isop
->isop_laddr
== 0 || isop
->isop_laddr
== laddr
)
647 if (isop
->isop_laddr
->siso_tlen
!= llen
)
649 if (memcmp(lp
, TSEL(isop
->isop_laddr
), llen
))
651 if (fportlen
&& isop
->isop_faddr
&&
652 memcmp(fport
, TSEL(isop
->isop_faddr
), (unsigned) fportlen
))
655 * PHASE2 addrmatch1 should be iso_addrmatch(a, b, mask)
656 * where mask is taken from isop->isop_laddrmask (new field)
657 * isop_lnetmask will also be available in isop if (laddr !=
658 * &zeroiso_addr && !iso_addrmatch1(laddr,
659 * &(isop->isop_laddr.siso_addr))) continue;
661 if (laddr
->siso_nlen
&& (!SAME_ISOADDR(laddr
, isop
->isop_laddr
)))
665 return (struct isopcb
*) 0;