xref: /csrg-svn/sys/netccitt/if_x25subr.c (revision 49727)
1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)if_x25subr.c	7.10 (Berkeley) 05/15/91
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "malloc.h"
13 #include "mbuf.h"
14 #include "protosw.h"
15 #include "socket.h"
16 #include "socketvar.h"
17 #include "ioctl.h"
18 #include "errno.h"
19 #include "syslog.h"
20 
21 #include "../net/if.h"
22 #include "../net/if_types.h"
23 #include "../net/netisr.h"
24 #include "../net/route.h"
25 
26 #include "x25.h"
27 #include "x25err.h"
28 #include "pk.h"
29 #include "pk_var.h"
30 
31 #include "machine/mtpr.h"
32 
33 #ifdef INET
34 #include "../netinet/in.h"
35 #include "../netinet/in_var.h"
36 #endif
37 
38 #ifdef NS
39 #include "../netns/ns.h"
40 #include "../netns/ns_if.h"
41 #endif
42 
43 #ifdef ISO
44 #include "../netiso/argo_debug.h"
45 #include "../netiso/iso.h"
46 #include "../netiso/iso_var.h"
47 #endif
48 
49 extern	struct ifnet loif;
50 struct llinfo_x25 llinfo_x25 = {&llinfo_x25, &llinfo_x25};
51 int x25_autoconnect = 0;
52 
53 #define senderr(x) {error = x; goto bad;}
54 /*
55  * Ancillary routines
56  */
57 static struct llinfo_x25 *
58 x25_lxalloc(rt)
59 register struct rtentry *rt;
60 {
61 	register struct llinfo_x25 *lx;
62 	register struct sockaddr *dst = rt_key(rt);
63 	register struct ifaddr *ifa;
64 
65 	MALLOC(lx, struct llinfo_x25 *, sizeof (*lx), M_PCB, M_NOWAIT);
66 	if (lx == 0)
67 		return lx;
68 	Bzero(lx, sizeof(*lx));
69 	lx->lx_rt = rt;
70 	lx->lx_family = dst->sa_family;
71 	rt->rt_refcnt++;
72 	if (rt->rt_llinfo)
73 		insque(lx, (struct llinfo_x25 *)rt->rt_llinfo);
74 	else {
75 		rt->rt_llinfo = (caddr_t)lx;
76 		insque(lx, &llinfo_x25);
77 	}
78 	for (ifa = rt->rt_ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
79 		if (ifa->ifa_addr->sa_family == AF_CCITT)
80 			lx->lx_ia = (struct x25_ifaddr *)ifa;
81 	}
82 	return lx;
83 }
84 x25_lxfree(lx)
85 register struct llinfo_x25 *lx;
86 {
87 	register struct rtentry *rt = lx->lx_rt;
88 	register struct pklcd *lcp = lx->lx_lcd;
89 
90 	if (lcp) {
91 		lcp->lcd_upper = 0;
92 		pk_disconnect(lcp);
93 	}
94 	if ((rt->rt_llinfo == (caddr_t)lx) && (lx->lx_next->lx_rt == rt))
95 		rt->rt_llinfo = (caddr_t)lx->lx_next;
96 	else
97 		rt->rt_llinfo = 0;
98 	RTFREE(rt);
99 	remque(lx);
100 	FREE(lx, M_PCB);
101 }
102 /*
103  * Process a x25 packet as datagram;
104  */
105 x25_ifinput(lcp, m)
106 struct pklcd *lcp;
107 register struct mbuf *m;
108 {
109 	struct llinfo_x25 *lx = (struct llinfo_x25 *)lcp->lcd_upnext;
110 	register struct ifnet *ifp;
111 	struct ifqueue *inq;
112 	extern struct timeval time;
113 	int s, len, isr;
114 
115 	if (m == 0 || lcp->lcd_state != DATA_TRANSFER) {
116 		x25_connect_callback(lcp, 0);
117 		return;
118 	}
119 	ifp = m->m_pkthdr.rcvif;
120 	ifp->if_lastchange = time;
121 	switch (m->m_type) {
122 	case MT_OOBDATA:
123 		if (m)
124 			m_freem(m);
125 	default:
126 		return;
127 
128 	case MT_DATA:
129 		/* FALLTHROUGH */;
130 	}
131 	switch (lx->lx_family) {
132 #ifdef INET
133 	case AF_INET:
134 		isr = NETISR_IP;
135 		inq = &ipintrq;
136 		break;
137 
138 #endif
139 #ifdef NS
140 	case AF_NS:
141 		isr = NETISR_NS;
142 		inq = &nsintrq;
143 		break;
144 
145 #endif
146 #ifdef	ISO
147 	case AF_ISO:
148 		isr = NETISR_ISO;
149 		inq = &clnlintrq;
150 		break;
151 #endif
152 	default:
153 		m_freem(m);
154 		ifp->if_noproto++;
155 		return;
156 	}
157 	s = splimp();
158 	schednetisr(isr);
159 	if (IF_QFULL(inq)) {
160 		IF_DROP(inq);
161 		m_freem(m);
162 	} else {
163 		IF_ENQUEUE(inq, m);
164 		ifp->if_ibytes += m->m_pkthdr.len;
165 	}
166 	splx(s);
167 }
168 x25_connect_callback(lcp, m)
169 register struct pklcd *lcp;
170 register struct mbuf *m;
171 {
172 	register struct llinfo_x25 *lx = (struct llinfo_x25 *)lcp->lcd_upnext;
173 	if (m == 0)
174 		goto refused;
175 	if (m->m_type != MT_CONTROL) {
176 		printf("x25_connect_callback: should panic\n");
177 		goto refused;
178 	}
179 	switch (pk_decode(mtod(m, struct x25_packet *))) {
180 	case CALL_ACCEPTED:
181 		lcp->lcd_upper = x25_ifinput;
182 		if (lcp->lcd_sb.sb_mb)
183 			lcp->lcd_send(lcp); /* XXX start queued packets */
184 		return;
185 	default:
186 	refused:
187 		lcp->lcd_upper = 0;
188 		lx->lx_lcd = 0;
189 		pk_disconnect(lcp);
190 		return;
191 	}
192 }
193 /*
194  * X.25 output routine.
195  */
196 x25_ifoutput(ifp, m0, dst, rt)
197 struct	ifnet *ifp;
198 struct	mbuf *m0;
199 struct	sockaddr *dst;
200 register struct	rtentry *rt;
201 {
202 	register struct	mbuf *m;
203 	register struct	llinfo_x25 *lx;
204 	struct pklcd *lcp;
205 	int             s, error = 0;
206 
207 	if ((ifp->if_flags & IFF_UP) == 0)
208 		senderr(ENETDOWN);
209 	if (rt == 0 ||
210 	    ((rt->rt_flags & RTF_GATEWAY) && (dst = rt->rt_gateway))) {
211 		if ((rt = rtalloc1(dst, 1)) == 0)
212 			senderr(EHOSTUNREACH);
213 		rt->rt_refcnt--;
214 	}
215 	/*
216 	 * Sanity checks.
217 	 */
218 	if ((rt->rt_ifp != ifp) ||
219 	    (rt->rt_flags & (RTF_CLONING | RTF_GATEWAY)) ||
220 	    ((lx = (struct llinfo_x25 *)rt->rt_llinfo) == 0)) {
221 		senderr(ENETUNREACH);
222 	}
223 	if (dst->sa_family == AF_INET &&
224 	    ifp->if_type == IFT_X25DDN &&
225 	    rt->rt_gateway->sa_family != AF_CCITT)
226 		x25_ddnip_to_ccitt(dst, rt);
227 next_circuit:
228 	lcp = lx->lx_lcd;
229 	if (lcp == 0) {
230 		lx->lx_lcd = lcp = pk_attach((struct socket *)0);
231 		if (lcp == 0)
232 			senderr(ENOBUFS);
233 		lcp->lcd_upper = x25_connect_callback;
234 		lcp->lcd_upnext = (caddr_t)lx;
235 		lcp->lcd_packetsize = lx->lx_ia->ia_xc.xc_psize;
236 	}
237 	switch (lcp->lcd_state) {
238 	case READY:
239 		if (rt->rt_gateway->sa_family != AF_CCITT) {
240 			if ((rt->rt_flags & RTF_XRESOLVE) == 0)
241 				senderr(EHOSTUNREACH);
242 		} else if (x25_autoconnect)
243 			error = pk_connect(lcp,
244 					(struct sockaddr_x25 *)rt->rt_gateway);
245 		if (error)
246 			senderr(error);
247 		/* FALLTHROUGH */
248 	case SENT_CALL:
249 	case DATA_TRANSFER:
250 		if (sbspace(&lcp->lcd_sb) < 0) {
251 			lx = lx->lx_next;
252 			if (lx->lx_rt != rt)
253 				senderr(ENOSPC);
254 			goto next_circuit;
255 		}
256 		if (lx->lx_ia)
257 			lcp->lcd_dg_timer =
258 				       lx->lx_ia->ia_xc.xc_dg_idletimo;
259 		pk_send(lcp, m);
260 		break;
261 	default:
262 		/*
263 		 * We count on the timer routine to close idle
264 		 * connections, if there are not enough circuits to go
265 		 * around.
266 		 *
267 		 * So throw away data for now.
268 		 * After we get it all working, we'll rewrite to handle
269 		 * actively closing connections (other than by timers),
270 		 * when circuits get tight.
271 		 *
272 		 * In the DDN case, the imp itself closes connections
273 		 * under heavy load.
274 		 */
275 		error = ENOBUFS;
276 	bad:
277 		if (m)
278 			m_freem(m);
279 	}
280 	return (error);
281 }
282 
283 /*
284  * Simpleminded timer routine.
285  */
286 x25_iftimeout(ifp)
287 struct ifnet *ifp;
288 {
289 	register struct pkcb *pkcb = 0;
290 	register struct ifaddr *ifa;
291 	register struct pklcd **lcpp, *lcp;
292 	int s = splimp();
293 
294 	for (ifa = ifp->if_addrlist; ifa && !pkcb; ifa = ifa->ifa_next) {
295 		if (ifa->ifa_addr->sa_family == AF_CCITT)
296 			pkcb = &((struct x25_ifaddr *)ifa)->ia_pkcb;
297 	}
298 	if (pkcb)
299 		for (lcpp = pkcb->pk_chan + pkcb->pk_maxlcn;
300 		     --lcpp > pkcb->pk_chan;)
301 			if ((lcp = *lcpp) &&
302 			    lcp->lcd_state == DATA_TRANSFER &&
303 			    (lcp->lcd_flags & X25_DG_CIRCUIT) &&
304 			    (lcp->lcd_dg_timer && --lcp->lcd_dg_timer == 0)) {
305 				lcp->lcd_upper(lcp, 0);
306 			}
307 	splx(s);
308 }
309 /*
310  * This routine gets called when validating additions of new routes
311  * or deletions of old *ones.
312  */
313 x25_ifrtchange(cmd, rt, dst)
314 register struct rtentry *rt;
315 struct sockaddr *dst;
316 {
317 	register struct llinfo_x25 *lx = (struct llinfo_x25 *)rt->rt_llinfo;
318 	register struct sockaddr_x25 *sa =(struct sockaddr_x25 *)rt->rt_gateway;
319 	register struct pklcd *lcp;
320 #define SA(p) ((struct sockaddr *)(p))
321 
322 	if (rt->rt_flags & RTF_GATEWAY) {
323 		if (rt->rt_llinfo)
324 			RTFREE((struct rtentry *)rt->rt_llinfo);
325 		rt->rt_llinfo = (cmd == RTM_ADD) ?
326 			(caddr_t)rtalloc1(rt->rt_gateway, 1) : 0;
327 		return;
328 	}
329 	if (cmd == RTM_DELETE) {
330 		while (rt->rt_llinfo)
331 			x25_lxfree((struct llinfo *)rt->rt_llinfo);
332 		x25_rtinvert(RTM_DELETE, rt->rt_gateway, rt);
333 		return;
334 	}
335 	if (lx == 0)
336 		lx = x25_lxalloc(rt);
337 	if ((lcp = lx->lx_lcd) && lcp->lcd_state != READY) {
338 		/*
339 		 * This can only happen on a RTM_CHANGE operation
340 		 * though cmd will be RTM_ADD.
341 		 */
342 		if (lcp->lcd_ceaddr &&
343 		    Bcmp(rt->rt_gateway, lcp->lcd_ceaddr,
344 					 lcp->lcd_ceaddr->x25_len) != 0) {
345 			x25_rtinvert(RTM_DELETE, lcp->lcd_ceaddr, rt);
346 			lcp->lcd_upper = 0;
347 			pk_disconnect(lcp);
348 		}
349 		lcp = 0;
350 	}
351 	x25_rtinvert(RTM_ADD, rt->rt_gateway, rt);
352 }
353 
354 x25_rtinvert(cmd, sa, rt)
355 register struct sockaddr *sa;
356 register struct rtentry *rt;
357 {
358 	struct rtentry *rt2 = 0;
359 	/*
360 	 * rt_gateway contains PID indicating which proto
361 	 * family on the other end, so will be different
362 	 * from general host route via X.25.
363 	 */
364 	if (rt->rt_ifp->if_type == IFT_X25DDN)
365 		return;
366 	if (sa->sa_family != AF_CCITT)
367 		return;
368 	if (cmd == RTM_ADD) {
369 		rtrequest(RTM_ADD, sa, rt_key(rt), SA(0),
370 				RTF_HOST|RTF_PROTO1, &rt2);
371 		if (rt2) {
372 			rt2->rt_llinfo = (caddr_t) rt;
373 			rt->rt_refcnt++;
374 		}
375 		return;
376 	}
377 	rt2 = rt;
378 	if ((rt = rtalloc1(sa, 0)) == 0 ||
379 	    (rt->rt_flags & RTF_PROTO1) == 0 ||
380 	    rt->rt_llinfo != (caddr_t)rt) {
381 		printf("x25_rtchange: inverse route screwup\n");
382 		return;
383 	} else
384 		rt2->rt_refcnt--;
385 	rtrequest(RTM_DELETE, rt->rt_gateway, rt_key(rt),
386 		SA(0), 0, (struct rtentry **) 0);
387 }
388 
389 static struct sockaddr_x25 blank_x25 = {sizeof blank_x25, AF_CCITT};
390 /*
391  * IP to X25 address routine copyright ACC, used by permission.
392  */
393 union imp_addr {
394 	struct in_addr  ip;
395 	struct imp {
396 		u_char		s_net;
397 		u_char		s_host;
398 		u_char		s_lh;
399 		u_char		s_impno;
400 	}		    imp;
401 };
402 
403 /*
404  * The following is totally bogus and here only to preserve
405  * the IP to X.25 translation.
406  */
407 x25_ddnip_to_ccitt(src, rt)
408 struct sockaddr_in *src;
409 register struct rtentry *rt;
410 {
411 	register struct sockaddr_x25 *dst = (struct sockaddr_x25 *)rt->rt_gateway;
412 	union imp_addr imp_addr;
413 	int             imp_no, imp_port, temp;
414 	char *x25addr = dst->x25_addr;
415 
416 
417 	imp_addr.ip = src->sin_addr;
418 	*dst = blank_x25;
419 	if ((imp_addr.imp.s_net & 0x80) == 0x00) {	/* class A */
420 	    imp_no = imp_addr.imp.s_impno;
421 	    imp_port = imp_addr.imp.s_host;
422 	} else if ((imp_addr.imp.s_net & 0xc0) == 0x80) {	/* class B */
423 	    imp_no = imp_addr.imp.s_impno;
424 	    imp_port = imp_addr.imp.s_lh;
425 	} else {		/* class C */
426 	    imp_no = imp_addr.imp.s_impno / 32;
427 	    imp_port = imp_addr.imp.s_impno % 32;
428 	}
429 
430 	x25addr[0] = 12; /* length */
431 	/* DNIC is cleared by struct copy above */
432 
433 	if (imp_port < 64) {	/* Physical:  0000 0 IIIHH00 [SS] *//* s_impno
434 				 *  -> III, s_host -> HH */
435 	    x25addr[5] = 0;	/* set flag bit */
436 	    x25addr[6] = imp_no / 100;
437 	    x25addr[7] = (imp_no % 100) / 10;
438 	    x25addr[8] = imp_no % 10;
439 	    x25addr[9] = imp_port / 10;
440 	    x25addr[10] = imp_port % 10;
441 	} else {		/* Logical:   0000 1 RRRRR00 [SS]	 *//* s
442 				 * _host * 256 + s_impno -> RRRRR */
443 	    temp = (imp_port << 8) + imp_no;
444 	    x25addr[5] = 1;
445 	    x25addr[6] = temp / 10000;
446 	    x25addr[7] = (temp % 10000) / 1000;
447 	    x25addr[8] = (temp % 1000) / 100;
448 	    x25addr[9] = (temp % 100) / 10;
449 	    x25addr[10] = temp % 10;
450 	}
451 }
452 
453 /*
454  * This routine is a sketch and is not to be believed!!!!!
455  *
456  * This is a utility routine to be called by x25 devices when a
457  * call request is honored with the intent of starting datagram forwarding.
458  */
459 x25_dg_rtinit(dst, ia, af)
460 struct sockaddr_x25 *dst;
461 register struct x25_ifaddr *ia;
462 {
463 	struct sockaddr *sa = 0;
464 	struct rtentry *rt;
465 	struct in_addr my_addr;
466 	static struct sockaddr_in sin = {sizeof(sin), AF_INET};
467 
468 	if (ia->ia_ifp->if_type == IFT_X25DDN && af == AF_INET) {
469 	/*
470 	 * Inverse X25 to IP mapping copyright and courtesy ACC.
471 	 */
472 		int             imp_no, imp_port, temp;
473 		union imp_addr imp_addr;
474 	    {
475 		/*
476 		 * First determine our IP addr for network
477 		 */
478 		register struct in_ifaddr *ina;
479 		extern struct in_ifaddr *in_ifaddr;
480 
481 		for (ina = in_ifaddr; ina; ina = ina->ia_next)
482 			if (ina->ia_ifp == ia->ia_ifp) {
483 				my_addr = ina->ia_addr.sin_addr;
484 				break;
485 			}
486 	    }
487 	    {
488 
489 		register char *x25addr = dst->x25_addr;
490 
491 		switch (x25addr[5] & 0x0f) {
492 		  case 0:	/* Physical:  0000 0 IIIHH00 [SS]	 */
493 		    imp_no =
494 			((int) (x25addr[6] & 0x0f) * 100) +
495 			((int) (x25addr[7] & 0x0f) * 10) +
496 			((int) (x25addr[8] & 0x0f));
497 
498 
499 		    imp_port =
500 			((int) (x25addr[9] & 0x0f) * 10) +
501 			((int) (x25addr[10] & 0x0f));
502 		    break;
503 		  case 1:	/* Logical:   0000 1 RRRRR00 [SS]	 */
504 		    temp = ((int) (x25addr[6] & 0x0f) * 10000)
505 			+ ((int) (x25addr[7] & 0x0f) * 1000)
506 			+ ((int) (x25addr[8] & 0x0f) * 100)
507 			+ ((int) (x25addr[9] & 0x0f) * 10)
508 			+ ((int) (x25addr[10] & 0x0f));
509 
510 		    imp_port = temp >> 8;
511 		    imp_no = temp & 0xff;
512 		    break;
513 		  default:
514 		    return (0L);
515 		}
516 		imp_addr.ip = my_addr;
517 		if ((imp_addr.imp.s_net & 0x80) == 0x00) {
518 		/* class A */
519 		    imp_addr.imp.s_host = imp_port;
520 		    imp_addr.imp.s_impno = imp_no;
521 		    imp_addr.imp.s_lh = 0;
522 		} else if ((imp_addr.imp.s_net & 0xc0) == 0x80) {
523 		/* class B */
524 		    imp_addr.imp.s_lh = imp_port;
525 		    imp_addr.imp.s_impno = imp_no;
526 		} else {
527 		/* class C */
528 		    imp_addr.imp.s_impno = (imp_no << 5) + imp_port;
529 		}
530 	    }
531 		sin.sin_addr = imp_addr.ip;
532 		sa = (struct sockaddr *)&sin;
533 	} else {
534 		/*
535 		 * This uses the X25 routing table to do inverse
536 		 * lookup of x25 address to sockaddr.
537 		 */
538 		if (rt = rtalloc1(dst, 0)) {
539 			sa = rt->rt_gateway;
540 			rt->rt_refcnt--;
541 		}
542 	}
543 	/*
544 	 * Call to rtalloc1 will create rtentry for reverse path
545 	 * to callee by virtue of cloning magic and will allocate
546 	 * space for local control block.
547 	 */
548 	if (sa && (rt = rtalloc1(sa, 1)))
549 		rt->rt_refcnt--;
550 }
551 
552 struct radix_tree_head *x25_rnhead;
553 
554 pk_init()
555 {
556 	/*
557 	 * warning, sizeof (struct sockaddr_x25) > 32,
558 	 * but contains no data of interest beyond 32
559 	 */
560 	rn_inithead(&x25_rnhead, 32, AF_CCITT);
561 }
562 /*
563  * This routine steals a virtual circuit from a socket,
564  * and glues it to a routing entry.  It wouldn't be hard
565  * to extend this to a routine that stole back the vc from
566  * rtentry.
567  */
568 pk_rtattach(so, m0)
569 register struct socket *so;
570 struct mbuf *m0;
571 {
572 	register struct pklcd *lcp = (struct pklcd *)so->so_pcb;
573 	register struct mbuf *m = m0;
574 	struct sockaddr *dst = mtod(m, struct sockaddr *);
575 	register struct rtentry *rt = rtalloc1(dst, 0);
576 	register struct llinfo_x25 *lx;
577 	caddr_t cp;
578 #define ROUNDUP(a) \
579 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
580 #define transfer_sockbuf(s, f, l) \
581 	while (m = (s)->sb_mb) {(s)->sb_mb = m->m_act; sbfree((s), m); f(l, m);}
582 
583 	if (rt)
584 		rt->rt_refcnt--;
585 	cp = (dst->sa_len < m->m_len) ? ROUNDUP(dst->sa_len) + (caddr_t)dst : 0;
586 	while (rt &&
587 	       ((cp == 0 && rt_mask(rt) != 0) ||
588 		(cp != 0 && (rt_mask(rt) == 0 ||
589 			     Bcmp(cp, rt_mask(rt), rt_mask(rt)->sa_len)) != 0)))
590 			rt = (struct rtentry *)rt->rt_nodes->rn_dupedkey;
591 	if (rt == 0 || (rt->rt_flags & RTF_GATEWAY) ||
592 	    (lx = (struct llinfo_x25 *)rt->rt_llinfo) == 0)
593 		return ESRCH;
594 	if (lcp == 0 || lcp->lcd_state != DATA_TRANSFER)
595 		return ENOTCONN;
596 	if (lcp = lx->lx_lcd) { /* adding an additional VC */
597 		if (lcp->lcd_state == READY) {
598 			transfer_sockbuf(&lcp->lcd_sb, pk_output,
599 					 (struct pklcd *)so->so_pcb);
600 			lcp->lcd_upper = 0;
601 			pk_close(lcp);
602 		} else {
603 			lx = x25_lxalloc(rt);
604 			if (lx == 0)
605 				return ENOBUFS;
606 		}
607 	}
608 	lx->lx_lcd = lcp = (struct pklcd *)so->so_pcb;
609 	lcp->lcd_so = 0;
610 	lcp->lcd_sb = so->so_snd; /* structure copy */
611 	lcp->lcd_upper = x25_ifinput;
612 	lcp->lcd_upnext = (caddr_t)lx;
613 	transfer_sockbuf(&so->so_rcv, x25_ifinput, lcp);
614 	so->so_pcb = 0;
615 	bzero((caddr_t)&so->so_snd, sizeof(so->so_snd)); /* XXXXXX */
616 	soisdisconnected(so);
617 	return (0);
618 }
619