xref: /original-bsd/sys/netccitt/pk_llcsubr.c (revision 052d6878)
1 /*
2  * Copyright (C) Dirk Husemann, Computer Science Department IV,
3  * 		 University of Erlangen-Nuremberg, Germany, 1990, 1991, 1992
4  * Copyright (c) 1992   Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Dirk Husemann and the Computer Science Department (IV) of
9  * the University of Erlangen-Nuremberg, Germany.
10  *
11  * %sccs.include.redist.c%
12  *
13  *	@(#)pk_llcsubr.c	7.1 (Berkeley) 12/08/92
14  */
15 
16 #include <sys/param.h>
17 #include <sys/systm.h>
18 #include <sys/mbuf.h>
19 #include <sys/domain.h>
20 #include <sys/socket.h>
21 #include <sys/socketvar.h>
22 #include <sys/protosw.h>
23 #include <sys/errno.h>
24 #include <sys/time.h>
25 #include <sys/kernel.h>
26 
27 #include <net/if.h>
28 #include <net/if_dl.h>
29 #include <net/if_llc.h>
30 #include <net/if_types.h>
31 #include <net/route.h>
32 
33 #include <netccitt/dll.h>
34 #include <netccitt/x25.h>
35 #include <netccitt/pk.h>
36 #include <netccitt/pk_var.h>
37 #include <netccitt/llc_var.h>
38 
39 
40 /*
41  * Routing support for X.25
42  *
43  * We distinguish between two cases:
44  * RTF_HOST:
45  * 	rt_key(rt)	X.25 address of host
46  *	rt_gateway	SNPA (MAC+DLSAP) address of host
47  *	rt_llinfo	pkcb for rt_key(rt)
48  *
49  * RTF_GATEWAY
50  *	rt_key(rt)	X.25 address of host or suitably masked network
51  *	rt_gateway	X.25 address of next X.25 gateway (switch)
52  *	rt_llinfo	rtentry for rt_gateway address
53  *			ought to be of type RTF_HOST
54  *
55  *
56  * Mapping of X.121 to pkcbs:
57  *
58  * HDLC uses the DTE-DCE model of X.25, therefore we need a many-to-one
59  * relationship, i.e.:
60  *
61  * 	{X.121_a, X.121_b, X.121_c, ..., X.121_i} -> pkcb_0
62  *
63  * LLC2 utilizes the DTE-DTE model of X.25, resulting effectively in a
64  * one-to-one relationship, i.e.:
65  *
66  *	{X.121_j} 	->	pkcb_1a
67  *	{X.121_k}	->	pkcb_1b
68  *	...
69  *	{X.121_q}	->	pkcb_1q
70  *
71  * It might make sense to allow a many-to-one relation for LLC2 also,
72  *
73  *	{X.121_r, X.121_s, X.121_t, X.121_u} -> pkcb_2a
74  *
75  * This would make addresses X.121_[r-u] essentially aliases of one
76  * address ({X.121_[r-u]} would constitute a representative set).
77  *
78  * Each one-to-one relation must obviously be entered individually with
79  * a route add command, whereas a many-to-one relationship can be
80  * either entered individually or generated by using a netmask.
81  *
82  * To facilitate dealings the many-to-one case for LLC2 can only be
83  * established via a netmask.
84  *
85  */
86 
87 #define XTRACTPKP(rt)	((rt)->rt_flags & RTF_GATEWAY ? \
88 			 ((rt)->rt_llinfo ? \
89 			  (struct pkcb *) ((struct rtentry *)((rt)->rt_llinfo))->rt_llinfo : \
90 			  (struct pkcb *) NULL) : \
91 			 (struct pkcb *)((rt)->rt_llinfo))
92 
93 #define equal(a1, a2) (bcmp((caddr_t)(a1), \
94 			       (caddr_t)(a2), \
95 			       (a1)->sa_len) == 0)
96 #define XIFA(rt) ((struct x25_ifaddr *)((rt)->rt_ifa))
97 
98 int
99 cons_rtrequest(int cmd, struct rtentry *rt, struct sockaddr *dst)
100 {
101 	register struct pkcb *pkp;
102 	register int i;
103 	register char one_to_one;
104 	struct pkcb *pk_newlink();
105 	struct rtentry *npaidb_enter();
106 
107 	pkp = XTRACTPKP(rt);
108 
109 	switch(cmd) {
110 	case RTM_RESOLVE:
111 	case RTM_ADD:
112 		if (pkp)
113 			return(EEXIST);
114 
115 		if (rt->rt_flags & RTF_GATEWAY) {
116 			if (rt->rt_llinfo)
117 				RTFREE((struct rtentry *)rt->rt_llinfo);
118 			rt->rt_llinfo = (caddr_t) rtalloc1(rt->rt_gateway, 1);
119 			return(0);
120 		}
121 		/*
122 		 * Assumptions:	(1) ifnet structure is filled in
123 		 *		(2) at least the pkcb created via
124 		 *		    x25config (ifconfig?) has been
125 		 *		    set up already.
126 		 *		(3) HDLC interfaces have an if_type of
127 		 *		    IFT_X25{,DDN}, LLC2 interfaces
128 		 *		    anything else (any better way to
129 		 *		    do this?)
130 		 *
131 		 */
132 		if (!rt->rt_ifa)
133 			return (ENETDOWN);
134 
135 		/*
136 		 * We differentiate between dealing with a many-to-one
137 		 * (HDLC: DTE-DCE) and a one-to-one (LLC2: DTE-DTE)
138 		 * relationship (by looking at the if type).
139 		 *
140 		 * Only in case of the many-to-one relationship (HDLC)
141 		 * we set the ia->ia_pkcb pointer to the pkcb allocated
142 		 * via pk_newlink() as we will use just that one pkcb for
143 		 * future route additions (the rtentry->rt_llinfo pointer
144 		 * points to the pkcb allocated for that route).
145 		 *
146 		 * In case of the one-to-one relationship (LLC2) we
147 		 * create a new pkcb (via pk_newlink()) for each new rtentry.
148 		 *
149 		 * NOTE: Only in case of HDLC does ia->ia_pkcb point
150 		 * to a pkcb, in the LLC2 case it doesn't (as we don't
151 		 * need it here)!
152 		 */
153 		one_to_one = ISISO8802(rt->rt_ifp);
154 
155 		if (!(pkp = XIFA(rt)->ia_pkcb) && !one_to_one)
156 			XIFA(rt)->ia_pkcb = pkp =
157 				pk_newlink(XIFA(rt), (caddr_t) 0);
158 		else if (one_to_one &&
159 			 !equal(rt->rt_gateway, rt->rt_ifa->ifa_addr)) {
160 			pkp = pk_newlink(XIFA(rt), (caddr_t) 0);
161 			/*
162 			 * We also need another route entry for mapping
163 			 * MAC+LSAP->X.25 address
164 			 */
165 			pkp->pk_llrt = npaidb_enter(rt->rt_gateway, rt_key(rt), rt, 0);
166 		}
167 		if (pkp) {
168 			if (!pkp->pk_rt)
169 				pkp->pk_rt = rt;
170 			pkp->pk_refcount++;
171 		}
172 		rt->rt_llinfo = (caddr_t) pkp;
173 
174 		return(0);
175 
176 	case RTM_DELETE:
177 	{
178 		/*
179 		 * The pkp might be empty if we are dealing
180 		 * with an interface route entry for LLC2, in this
181 		 * case we don't need to do anything ...
182 		 */
183 		if (pkp) {
184 			if ( rt->rt_flags & RTF_GATEWAY ) {
185 				if (rt->rt_llinfo)
186 					RTFREE((struct rtentry *)rt->rt_llinfo);
187 				return(0);
188 			}
189 
190 			if (pkp->pk_llrt)
191 				npaidb_destroy(pkp->pk_llrt);
192 
193 			pk_dellink (pkp);
194 
195 			return(0);
196 		}
197 	}
198 	}
199 }
200 
201 
202 /*
203  * Glue between X.25 and LLC2
204  */
205 int
206 x25_llcglue(int prc, struct sockaddr *addr)
207 {
208 	register struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)addr;
209 	register struct x25_ifaddr *x25ifa;
210 	struct dll_ctlinfo ctlinfo;
211 
212 	if((x25ifa = (struct x25_ifaddr *)ifa_ifwithaddr(addr)) == 0)
213 		return 0;
214 
215 	ctlinfo.dlcti_cfg  =
216 	    (struct dllconfig *)(((struct sockaddr_x25 *)(&x25ifa->ia_xc))+1);
217 	ctlinfo.dlcti_lsap = LLC_X25_LSAP;
218 
219 	return ((int)llc_ctlinput(prc, addr, (caddr_t)&ctlinfo));
220 }
221