* Copyright (c) 1991 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* from: @(#)if_cons.c 7.10 (Berkeley) 5/29/91
* $Id: if_cons.c,v 1.3 1993/11/07 17:49:36 wollman Exp $
/***********************************************************
Copyright IBM Corporation 1987
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of IBM not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
******************************************************************/
* ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
* cons.c - Connection Oriented Network Service:
* including support for a) user transport-level service,
* b) COSNS below CLNP, and c) CONS below TP.
#include "../net/netisr.h"
#include "../net/route.h"
#include "../netccitt/x25.h"
#include "../netccitt/pk.h"
#include "../netccitt/pk_var.h"
#define MT_XCLOSE MT_DATA
#define MT_XCONFIRM MT_DATA
#define MT_XHEADER MT_HEADER
static int parse_facil(struct pklcd
*, struct isopcb
*, caddr_t
,
/*********************************************************************
* cons.c - CONS interface to the x.25 layer
* TODO: figure out what resources we might run out of besides mbufs.
* If we run out of any of them (including mbufs) close and recycle
* lru x% of the connections, for some parameter x.
* There are 2 interfaces from above:
* cons CO network service
* TP associates a transport connection with a network connection.
* cons_output( isop, m, len, isdgm==0 )
* It's a datagram service, like clnp is. - even though it calls
* cons_output( isop, m, len, isdgm==1 )
* it eventually goes through
* cosns_output(ifp, m, dst).
* TP4 permits multiplexing (reuse, possibly simultaneously) of the
* This means that many sockets (many tpcbs) may be associated with
* this pklcd, hence cannot have a back ptr from pklcd to a tpcb.
* co_socket is null since there may be many sockets that use this pklcd.
streams would really be nice. sigh.
PVCs could be handled by config-ing a cons with an address and with the
IFF_POINTTOPOINT flag on. This code would then have to skip the
connection setup stuff for pt-to-pt links.
*********************************************************************/
/* struct cons_stat cons_stat; */
u_char x25_error_stats
[CONL_ERROR_MAX
+ 1];
struct ifqueue consintrq
;
/* protosw pointers for getting to higher layer */
Static
struct protosw
*CLNP_proto
;
Static
struct protosw
*TP_proto
;
Static
struct protosw
*X25_proto
;
Static
int issue_clear_req();
extern struct ifaddr
*ifa_ifwithnet();
extern struct ifaddr
*ifa_ifwithaddr();
Static
struct socket dummysocket
; /* for use by cosns */
extern struct isopcb tp_isopcb
; /* chain of all TP pcbs */
struct isopcb tp_incoming_pending
; /* incoming connections
struct isopcb
*Xpcblist
[] = {
Static
int parse_facil(), NSAPtoDTE(), make_partial_x25_packet();
Static
int FACILtoNSAP(), DTEtoNSAP();
Static
struct pklcd
*cons_chan_to_pcb();
* FUNCTION and ARGUMENTS:
* copies (len) nibbles from (src_octet), high or low nibble
* to (dst_octet), high or low nibble,
* src_nibble & dst_nibble should be:
* HIGH_NIBBLE (1) if leftmost 4 bits/ most significant nibble
* LOW_NIBBLE (0) if rightmost 4 bits/ least significant nibble
nibble_copy(src_octet
, src_nibble
, dst_octet
, dst_nibble
, len
)
register char *src_octet
;
register char *dst_octet
;
register unsigned src_nibble
;
register unsigned dst_nibble
;
register unsigned dshift
, sshift
;
printf("nibble_copy ( 0x%x, 0x%x, 0x%x, 0x%x 0x%x)\n",
src_octet
, src_nibble
, dst_octet
, dst_nibble
, len
);
dshift
= dst_nibble
<< 2;
sshift
= src_nibble
<< 2;
*dst_octet
&= ~(0xf<< dshift
);
*dst_octet
|= ( 0xf & (*src_octet
>> sshift
))<< dshift
;
src_nibble
= 1-src_nibble
;
dst_nibble
= 1-dst_nibble
;
printf("nibble_copy DONE\n");
* FUNCTION and ARGUMENTS:
* compares src_octet/src_nibble and dst_octet/dst_nibble for len nibbles.
* RETURNS: 0 if they differ, 1 if they are the same.
nibble_match( src_octet
, src_nibble
, dst_octet
, dst_nibble
, len
)
register char *src_octet
;
register char *dst_octet
;
register unsigned src_nibble
;
register unsigned dst_nibble
;
register unsigned dshift
, sshift
;
u_char nibble_a
, nibble_b
;
printf("nibble_match ( 0x%x, 0x%x, 0x%x, 0x%x 0x%x)\n",
src_octet
, src_nibble
, dst_octet
, dst_nibble
, len
);
dshift
= dst_nibble
<< 2;
sshift
= src_nibble
<< 2;
nibble_b
= ((*dst_octet
)>>dshift
) & 0xf;
nibble_a
= ( 0xf & (*src_octet
>> sshift
));
if (nibble_b
!= nibble_a
)
src_nibble
= 1-src_nibble
;
dst_nibble
= 1-dst_nibble
;
printf("nibble_match DONE\n");
**************************** NET PROTOCOL cons ***************************
* initialize the protocol
void tp_incoming(), clnp_incoming();
CLNP_proto
= pffindproto(AF_ISO
, ISOPROTO_CLNP
, SOCK_DGRAM
);
X25_proto
= pffindproto(AF_ISO
, ISOPROTO_X25
, SOCK_STREAM
);
TP_proto
= pffindproto(AF_ISO
, ISOPROTO_TP0
, SOCK_SEQPACKET
);
printf("cons_init end : cnlp_proto 0x%x cons proto 0x%x tp proto 0x%x\n",
CLNP_proto
, X25_proto
, TP_proto
);
pk_protolisten(0x81, 0, clnp_incoming
);
pk_protolisten(0x82, 0, esis_incoming
);
pk_protolisten(0x84, 0, tp8878_A_incoming
);
pk_protolisten(0, 0, tp_incoming
);
register struct isopcb
*isop
;
extern struct isopcb tp_isopcb
;
if (iso_pcballoc((struct socket
*)0, &tp_incoming_pending
)) {
isop
= tp_incoming_pending
.isop_next
;
lcp
->lcd_upper
= cons_tpinput
;
lcp
->lcd_upnext
= (caddr_t
)isop
;
lcp
->lcd_send(lcp
); /* Confirms call */
isop
->isop_chan
= (caddr_t
)lcp
;
isop
->isop_laddr
= &isop
->isop_sladdr
;
isop
->isop_faddr
= &isop
->isop_sfaddr
;
DTEtoNSAP(isop
->isop_laddr
, &lcp
->lcd_laddr
);
DTEtoNSAP(isop
->isop_faddr
, &lcp
->lcd_faddr
);
parse_facil(lcp
, isop
, &(mtod(m
, struct x25_packet
*)->packet_data
),
m
->m_pkthdr
.len
- PKHEADERLN
);
register struct isopcb
*isop
= (struct isopcb
*)lcp
->lcd_upnext
;
register struct x25_packet
*xp
;
tpcons_input(m0
, isop
->isop_faddr
, isop
->isop_laddr
,
(struct socket
*)0, (caddr_t
)lcp
);
switch (pk_decode(mtod(m0
, struct x25_packet
*))) {
cmd
= PRC_CONS_SEND_DONE
;
lcp
->lcd_send(lcp
); /* XXX - fix this */
tpcons_ctlinput(cmd
, isop
->isop_faddr
, isop
);
* tpcons_pcbconnect() when opening a new connection.
* FUNCTION anD ARGUMENTS:
* Figures out which device to use, finding a route if one doesn't
register struct isopcb
*isop
;
register struct pklcd
*lcp
= (struct pklcd
*)isop
->isop_chan
;
register struct mbuf
*m
= 0;
printf("cons_connect(0x%x): ", isop
);
dump_isoaddr(isop
->isop_faddr
);
dump_isoaddr(isop
->isop_laddr
);
NSAPtoDTE(isop
->isop_faddr
, &lcp
->lcd_faddr
);
lcp
->lcd_upper
= cons_tpinput
;
lcp
->lcd_upnext
= (caddr_t
)isop
;
"calling make_partial_x25_packet( 0x%x, 0x%x, 0x%x)\n",
&lcp
->lcd_faddr
, &lcp
->lcd_laddr
,
isop
->isop_socket
->so_proto
->pr_protocol
);
if ((error
= make_partial_x25_packet(isop
, lcp
, m
)) == 0)
error
= pk_connect(lcp
, &lcp
->lcd_faddr
);
**************************** DEVICE cons ***************************
* lower layer when ECN_CLEAR occurs : this routine is here
* for consistency - cons subnet service calls its higher layer
* through the protosw entry.
* cmd is a PRC_* command, list found in ../sys/protosw.h
* This serves the higher-layer cons service.
* NOTE: this takes 3rd arg. because cons uses it to inform itself
* of things (timeouts, etc) but has a pcb instead of an address.
cons_ctlinput(cmd
, sa
, copcb
)
register struct pklcd
*copcb
;
register struct x25_packet
*xp
;
extern u_char x25_error_stats
[];
/* DTE originated; look at the diagnostic */
error
= (CONL_ERROR_MASK
| cause
);
case 0x01: /* number busy */
case 0x09: /* Out of order */
case 0x11: /* Remot Procedure Error */
case 0x19: /* reverse charging accept not subscribed */
case 0x21: /* Incampat destination */
case 0x29: /* fast select accept not subscribed */
case 0x39: /* ship absent */
case 0x03: /* invalid facil request */
case 0x0b: /* access barred */
case 0x13: /* local procedure error */
case 0x05: /* network congestion */
case 0x8d: /* not obtainable */
case 0x95: /* RPOA out of order */
* so we don't have to have so many perror entries
error
= (CONL_ERROR_MASK
| 0x100 | (cause
& ~0x80));
case 0xc1: /* gateway-detected proc error */
case 0xc3: /* gateway congestion */
error
= (CONL_ERROR_MASK
| 0x100 | cause
);
/* otherwise, a *hopefully* valid perror exists in the e_reason field */
printf("Incoming PKT TYPE 0x%x with reason 0x%x\n",
* NAME: make_partial_x25_packet()
* FUNCTION and ARGUMENTS:
* Makes part of an X.25 call packet, for use by x25.
* (src) and (dst) are the NSAP-addresses of source and destination.
* (buf) is a ptr to a buffer into which to write this partial header.
* 0 Facility length (in octets)
* 1 Facility field, which is a set of:
* m+1 facil param len (for >2-byte facilities) in octets
* m+2..p facil param field
* q user data (protocol identification octet)
* Stores facilites mbuf in X.25 control block, where the connect
* routine knows where to look for it.
int cons_use_udata
= 1; /* KLUDGE FOR DEBUGGING */
make_partial_x25_packet(isop
, lcp
)
printf("make_partial_x25_packet(0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\n",
isop
->isop_laddr
, isop
->isop_faddr
, proto
, m
, flag
);
if (isop
->isop_x25crud_len
> 0) {
* The user specified something. Stick it in
bcopy(isop
->isop_x25crud
, lcp
->lcd_faddr
.x25_udata
,
lcp
->lcd_faddr
.x25_udlen
= isop
->isop_x25crud_len
;
if (cons_use_facils
== 0) {
MGETHDR(m
, MT_DATA
, M_WAITOK
);
/* ptr now points to facil length (len of whole facil field in OCTETS */
pk_build_facilities(m
, &lcp
->lcd_faddr
, 0);
printf("make_partial calling: ptr 0x%x, len 0x%x\n", ptr
,
isop
->isop_laddr
->siso_addr
.isoa_len
);
*ptr
++ = 0; /* Marker to separate X.25 facitilies from CCITT ones */
*ptr
= 0xcb; /* calling facility code */
ptr
++; /* leave room for facil param len (in OCTETS + 1) */
ptr
++; /* leave room for the facil param len (in nibbles),
* high two bits of which indicate full/partial NSAP
len
= isop
->isop_laddr
->siso_addr
.isoa_len
;
bcopy( isop
->isop_laddr
->siso_data
, ptr
, len
);
*(ptr
-2) = len
+1; /* facil param len in octets */
*(ptr
-1) = len
<<1; /* facil param len in nibbles */
printf("make_partial called: ptr 0x%x, len 0x%x\n", ptr
,
isop
->isop_faddr
->siso_addr
.isoa_len
);
*ptr
= 0xc9; /* called facility code */
ptr
++; /* leave room for facil param len (in OCTETS + 1) */
ptr
++; /* leave room for the facil param len (in nibbles),
* high two bits of which indicate full/partial NSAP
len
= isop
->isop_faddr
->siso_nlen
;
bcopy(isop
->isop_faddr
->siso_data
, ptr
, len
);
*(ptr
-2) = len
+1; /* facil param len = addr len + 1 for each of these
* two length fields, in octets */
*(ptr
-1) = len
<<1; /* facil param len in nibbles */
*facil_len
= ptr
- facil_len
- 1;
if (*facil_len
> MAX_FACILITIES
)
buflen
= (int)(ptr
- buf
);
printf("ECN_CONNECT DATA buf 0x%x len %d (0x%x)\n",
for( i
=0; i
< buflen
; ) {
printf("+%d: %x %x %x %x %x %x %x %x\n",
*(buf
+i
), *(buf
+i
+1), *(buf
+i
+2), *(buf
+i
+3),
*(buf
+i
+4), *(buf
+i
+5), *(buf
+i
+6), *(buf
+i
+7));
printf("make_partial returns buf 0x%x size 0x%x bytes\n",
mtod(m
, caddr_t
), buflen
);
m
->m_pkthdr
.len
= m
->m_len
= buflen
;
* make_partial_x25_packet()
* FUNCTION and ARGUMENTS:
* get a DTE address from an NSAP-address (struct sockaddr_iso)
* (dst_octet) is the octet into which to begin stashing the DTE addr
* (dst_nibble) takes 0 or 1. 1 means begin filling in the DTE addr
* in the high-order nibble of dst_octet. 0 means low-order nibble.
* (addr) is the NSAP-address
* (flag) is true if the transport suffix is to become the
* last two digits of the DTE address
* A DTE address is a series of ASCII digits
* A DTE address may have leading zeros. The are significant.
* 1 digit per nibble, may be an odd number of nibbles.
* An NSAP-address has the DTE address in the IDI. Leading zeros are
* significant. Trailing hex f indicates the end of the DTE address.
* The IDI is a series of BCD digits, one per nibble.
* # significant digits in the DTE address, -1 if error.
register struct sockaddr_iso
*siso
;
register struct sockaddr_x25
*sx25
;
printf("NSAPtoDTE: nsap: %s\n", clnp_iso_addrp(&siso
->siso_addr
));
if (siso
->siso_data
[0] == AFI_37
) {
register char *out
= sx25
->x25_addr
;
register char *in
= siso
->siso_data
+ 1;
char *lim
= siso
->siso_data
+ siso
->siso_nlen
;
nibble
= ((lowNibble
? *in
++ : (*in
>> 4)) & 0xf) | 0x30;
if (nibble
!= 0x3f && out
< olim
)
dtelen
= out
- sx25
->x25_addr
;
register struct rtentry
*rt
= rtalloc1((struct sockaddr
*)siso
,
/* error = iso_8208snparesolve(addr, x121string, &x121strlen);*/
register struct sockaddr_x25
*sxx
=
(struct sockaddr_x25
*)rt
->rt_gateway
;
register char *in
= sxx
->x25_addr
;
if (sxx
&& sxx
->x25_family
== AF_CCITT
) {
bcopy(sx25
->x25_addr
, sxx
->x25_addr
, sizeof(sx25
->x25_addr
));
dtelen
= in
- sxx
->x25_addr
;
* FUNCTION and ARGUMENTS:
* Creates and NSAP in the sockaddr_iso (addr) from the
* x.25 facility found at buf - 1.
* length of parameter if ok, -1 if error.
register struct sockaddr_iso
*addr
;
int len_in_nibbles
, param_len
= *buf
++;
u_char buf_len
= 0; /* in bytes */
printf("FACILtoNSAP( 0x%x, 0x%x, 0x%x )\n",
len_in_nibbles
= *buf
& 0x3f;
buf_len
= (len_in_nibbles
+ 1) >> 1;
/* despite the fact that X.25 makes us put a length in nibbles
* here, the NSAP-addrs are always in full octets
/* Entire OSI NSAP address */
bcopy((caddr_t
)buf
, addr
->siso_data
, addr
->siso_nlen
= buf_len
);
/* Partial OSI NSAP address, assume trailing */
if (buf_len
+ addr
->siso_nlen
> sizeof(addr
->siso_addr
))
bcopy((caddr_t
)buf
, TSEL(addr
), buf_len
);
addr
->siso_nlen
+= buf_len
;
/* Rather than blow away the connection, just ignore and use
register struct sockaddr_iso
*siso
;
siso
->siso_len
= sizeof (*siso
);
siso
->siso_family
= AF_ISO
;
siso
->siso_data
[0] = AFI_37
;
* FUNCTION and ARGUMENTS:
* Creates a type 37 NSAP in the sockaddr_iso (addr)
* from a DTE address found in a sockaddr_x25.
struct sockaddr_iso
*addr
;
src_len
= strlen(sx
->x25_addr
);
out
= addr
->siso_data
+ 1;
if (*in
== '0' && (src_len
& 1 == 0)) {
for (first
= 0; src_len
> 0; src_len
--) {
* FUNCTION and ARGUMENTS:
* parses (buf_len) bytes beginning at (buf) and finds
* a called nsap, a calling nsap, and protocol identifier.
parse_facil(lcp
, isop
, buf
, buf_len
)
u_char buf_len
; /* in bytes */
register struct sockaddr_iso
*called
= isop
->isop_laddr
;
register struct sockaddr_iso
*calling
= isop
->isop_faddr
;
register u_char
*ptr
= (u_char
*)buf
;
u_char
*ptr_lim
, *facil_lim
;
int facil_param_len
, facil_len
;
printf("parse_facil(0x%x, 0x%x, 0x%x, 0x%x)\n",
buf
, buf_len
, called
, calling
);
/* find the beginnings of the facility fields in buf
* by skipping over the called & calling DTE addresses
* i <- # nibbles in called + # nibbles in calling
* i += 1 so that an odd nibble gets rounded up to even
* before dividing by 2, then divide by two to get # octets
i
= (int)(*ptr
>> 4) + (int)(*ptr
&0xf);
ptr
++; /* plus one for the DTE lengths byte */
/* ptr now is at facil_length field */
facil_lim
= ptr
+ facil_len
;
printf("parse_facils: facil length is 0x%x\n", (int) facil_len
);
while (ptr
<= facil_lim
) {
/* get NSAP addresses from facilities */
facil_param_len
= FACILtoNSAP(isop
->isop_faddr
, ptr
);
facil_param_len
= FACILtoNSAP(isop
->isop_laddr
, ptr
);
/* from here to default are legit cases that I ignore */
case 0xca: /* end-to-end transit delay negot */
case 0xc6: /* network user id */
case 0xc5: /* charging info : indicating monetary unit */
case 0xc2: /* charging info : indicating segment count */
case 0xc1: /* charging info : indicating call duration */
case 0xc4: /* RPOA extended format */
case 0xc3: /* call redirection notification */
case 0x0a: /* min. throughput class negot */
case 0x02: /* throughput class */
case 0x03: case 0x47: /* CUG shit */
case 0x0b: /* expedited data negot */
case 0x01: /* Fast select or reverse charging
(example of intelligent protocol design) */
case 0x04: /* charging info : requesting service */
case 0x08: /* called line addr modified notification */
case 0x42: /* pkt size */
case 0x43: /* win size */
case 0x44: /* RPOA basic format */
case 0x41: /* bilateral CUG shit */
case 0x49: /* transit delay selection and indication */
/* don't have any 3 octets */
"BOGUS FACILITY CODE facil_len 0x%x *facil_len 0x%x, ptr 0x%x *ptr 0x%x\n",
ptr
, facil_len
, ptr
- 1, ptr
[-1]);
/* facil that we don't handle */
if (facil_param_len
== -1)
if (facil_param_len
== 0) /* variable length */
facil_param_len
= (int)*ptr
; /* 1 + the real facil param */