1 /* $NetBSD: pk_input.c,v 1.20 2003/08/07 16:33:04 agc Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by the
8 * Laboratory for Computation Vision and the Computer Science Department
9 * of the University of British Columbia and the Computer Science
10 * Department (IV) of the University of Erlangen-Nuremberg, Germany.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)pk_input.c 8.1 (Berkeley) 6/10/93
37 */
38
39 /*
40 * Copyright (c) 1984 University of British Columbia.
41 * Copyright (c) 1992 Computer Science Department IV,
42 * University of Erlangen-Nuremberg, Germany.
43 *
44 * This code is derived from software contributed to Berkeley by the
45 * Laboratory for Computation Vision and the Computer Science Department
46 * of the University of British Columbia and the Computer Science
47 * Department (IV) of the University of Erlangen-Nuremberg, Germany.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 * must display the following acknowledgement:
59 * This product includes software developed by the University of
60 * California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * SUCH DAMAGE.
76 *
77 * @(#)pk_input.c 8.1 (Berkeley) 6/10/93
78 */
79
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: pk_input.c,v 1.20 2003/08/07 16:33:04 agc Exp $");
82
83 #include "opt_hdlc.h"
84 #include "opt_llc.h"
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/mbuf.h>
89 #include <sys/socket.h>
90 #include <sys/protosw.h>
91 #include <sys/socketvar.h>
92 #include <sys/proc.h>
93 #include <sys/errno.h>
94
95 #include <net/if.h>
96 #include <net/if_dl.h>
97 #include <net/if_llc.h>
98 #include <net/route.h>
99
100 #include <netccitt/dll.h>
101 #include <netccitt/x25.h>
102 #include <netccitt/pk.h>
103 #include <netccitt/pk_var.h>
104 #include <netccitt/pk_extern.h>
105 #include <netccitt/llc_var.h>
106 #ifdef HDLC
107 #include <netccitt/hdlc.h>
108 #include <netccitt/hd_var.h>
109 #endif
110
111 #include <machine/stdarg.h>
112
113 struct pkcb_q pkcb_q = {&pkcb_q, &pkcb_q};
114
115 static void prune_dnic __P((char *, char *, char *, struct x25config *));
116 static void save_extra __P((struct mbuf *, octet *, struct socket *));
117
118 /*
119 * ccittintr() is the generic interrupt handler for HDLC, LLC2, and X.25. This
120 * allows to have kernel running X.25 but no HDLC or LLC2 or both (in case we
121 * employ boards that do all the stuff themselves, e.g. ADAX X.25 or TPS ISDN.)
122 */
123 void
124 ccittintr()
125 {
126 extern struct ifqueue pkintrq;
127 extern struct ifqueue hdintrq;
128 extern struct ifqueue llcintrq;
129
130 #ifdef HDLC
131 if (hdintrq.ifq_len)
132 hdintr();
133 #endif
134 #ifdef LLC
135 if (llcintrq.ifq_len)
136 llcintr();
137 #endif
138 if (pkintrq.ifq_len)
139 pkintr();
140 }
141
142 struct pkcb *
143 pk_newlink(ia, llnext)
144 struct x25_ifaddr *ia;
145 caddr_t llnext;
146 {
147 struct x25config *xcp = &ia->ia_xc;
148 struct pkcb *pkp;
149 struct protosw *pp;
150 unsigned size;
151
152 pp = pffindproto(AF_CCITT, (int) xcp->xc_lproto, 0);
153 if (pp == 0 || pp->pr_output == 0) {
154 pk_message(0, xcp, "link level protosw error");
155 return ((struct pkcb *) 0);
156 }
157 /*
158 * Allocate a network control block structure
159 */
160 size = sizeof(struct pkcb);
161 pkp = (struct pkcb *) malloc(size, M_PCB, M_WAITOK);
162 if (pkp == 0)
163 return ((struct pkcb *) 0);
164 bzero((caddr_t) pkp, size);
165 pkp->pk_lloutput = pp->pr_output;
166 pkp->pk_llctlinput = pp->pr_ctlinput;
167 pkp->pk_xcp = xcp;
168 IFAREF(&ia->ia_ifa);
169 pkp->pk_ia = ia;
170 pkp->pk_state = DTE_WAITING;
171 pkp->pk_llnext = llnext;
172 insque(pkp, &pkcb_q);
173
174 /*
175 * set defaults
176 */
177
178 if (xcp->xc_pwsize == 0)
179 xcp->xc_pwsize = DEFAULT_WINDOW_SIZE;
180 if (xcp->xc_psize == 0)
181 xcp->xc_psize = X25_PS128;
182 /*
183 * Allocate logical channel descriptor vector
184 */
185
186 (void) pk_resize(pkp);
187 return (pkp);
188 }
189
190
191 int
192 pk_dellink(pkp)
193 struct pkcb *pkp;
194 {
195 int i;
196 struct protosw *pp;
197
198 /*
199 * Essentially we have the choice to
200 * (a) go ahead and let the route be deleted and
201 * leave the pkcb associated with that route
202 * as it is, i.e. the connections stay open
203 * (b) do a pk_disconnect() on all channels associated
204 * with the route via the pkcb and then proceed.
205 *
206 * For the time being we stick with (b)
207 */
208
209 for (i = 1; i < pkp->pk_maxlcn; ++i)
210 if (pkp->pk_chan[i])
211 pk_disconnect(pkp->pk_chan[i]);
212
213 /*
214 * Free the pkcb
215 */
216
217 /*
218 * First find the protoswitch to get hold of the link level
219 * protocol to be notified that the packet level entity is
220 * dissolving ...
221 */
222 pp = pffindproto(AF_CCITT, (int) pkp->pk_xcp->xc_lproto, 0);
223 if (pp == 0 || pp->pr_output == 0) {
224 pk_message(0, pkp->pk_xcp, "link level protosw error");
225 return (EPROTONOSUPPORT);
226 }
227 pkp->pk_refcount--;
228 if (!pkp->pk_refcount) {
229 struct dll_ctlinfo ctlinfo;
230
231 remque(pkp);
232 if (pkp->pk_rt->rt_llinfo == (caddr_t) pkp)
233 pkp->pk_rt->rt_llinfo = (caddr_t) NULL;
234
235 /*
236 * Tell the link level that the pkcb is dissolving
237 */
238 if (pp->pr_ctlinput && pkp->pk_llnext) {
239 ctlinfo.dlcti_pcb = pkp->pk_llnext;
240 ctlinfo.dlcti_rt = pkp->pk_rt;
241 (*pp->pr_ctlinput) (PRC_DISCONNECT_REQUEST,
242 (struct sockaddr *)pkp->pk_xcp,
243 &ctlinfo);
244 }
245 if (pkp->pk_ia != NULL)
246 IFAFREE(&pkp->pk_ia->ia_ifa);
247
248 free((caddr_t) pkp->pk_chan, M_IFADDR);
249 free((caddr_t) pkp, M_PCB);
250 }
251 return (0);
252 }
253
254
255 int
256 pk_resize(pkp)
257 struct pkcb *pkp;
258 {
259 struct pklcd *dev_lcp = 0;
260 struct x25config *xcp = pkp->pk_xcp;
261 if (pkp->pk_chan &&
262 (pkp->pk_maxlcn != xcp->xc_maxlcn)) {
263 pk_restart(pkp, X25_RESTART_NETWORK_CONGESTION);
264 dev_lcp = pkp->pk_chan[0];
265 free((caddr_t) pkp->pk_chan, M_IFADDR);
266 pkp->pk_chan = 0;
267 }
268 if (pkp->pk_chan == 0) {
269 unsigned size;
270 pkp->pk_maxlcn = xcp->xc_maxlcn;
271 size = (pkp->pk_maxlcn + 1) * sizeof(struct pklcd *);
272 pkp->pk_chan = malloc(size, M_IFADDR, M_WAITOK);
273 if (pkp->pk_chan) {
274 bzero((caddr_t) pkp->pk_chan, size);
275 /*
276 * Allocate a logical channel descriptor for lcn 0
277 */
278 if (dev_lcp == 0 &&
279 (dev_lcp = pk_attach((struct socket *) 0)) == 0)
280 return (ENOBUFS);
281 dev_lcp->lcd_state = READY;
282 dev_lcp->lcd_pkp = pkp;
283 pkp->pk_chan[0] = dev_lcp;
284 } else {
285 if (dev_lcp)
286 pk_close(dev_lcp);
287 return (ENOBUFS);
288 }
289 }
290 return 0;
291 }
292
293 /*
294 * This procedure is called by the link level whenever the link becomes
295 * operational, is reset, or when the link goes down.
296 */
297 /* VARARGS */
298 void *
299 pk_ctlinput(code, src, addr)
300 int code;
301 struct sockaddr *src;
302 void *addr;
303 {
304 struct pkcb *pkp = (struct pkcb *) addr;
305 struct rtentry *llrt;
306
307 /*XXX correct?*/
308 if (src->sa_family != AF_CCITT)
309 return 0;
310
311 switch (code) {
312 case PRC_LINKUP:
313 if (pkp->pk_state == DTE_WAITING)
314 pk_restart(pkp, X25_RESTART_NETWORK_CONGESTION);
315 break;
316
317 case PRC_LINKDOWN:
318 pk_restart(pkp, -1); /* Clear all active circuits */
319 pkp->pk_state = DTE_WAITING;
320 break;
321
322 case PRC_LINKRESET:
323 pk_restart(pkp, X25_RESTART_NETWORK_CONGESTION);
324 break;
325
326 case PRC_CONNECT_INDICATION:
327
328 if ((llrt = rtalloc1(src, 0)) == 0)
329 return 0;
330 else
331 llrt->rt_refcnt--;
332
333 pkp = (((struct npaidbentry *) llrt->rt_llinfo)->np_rt) ?
334 (struct pkcb *) (((struct npaidbentry *) llrt->rt_llinfo)->np_rt->rt_llinfo) : (struct pkcb *) 0;
335 if (pkp == (struct pkcb *) 0)
336 return 0;
337 pkp->pk_llnext = addr;
338
339 return ((caddr_t) pkp);
340
341 case PRC_DISCONNECT_INDICATION:
342 pk_restart(pkp, -1); /* Clear all active circuits */
343 pkp->pk_state = DTE_WAITING;
344 pkp->pk_llnext = (caddr_t) 0;
345 break;
346 }
347 return (0);
348 }
349 struct ifqueue pkintrq;
350 /*
351 * This routine is called if there are semi-smart devices that do HDLC
352 * in hardware and want to queue the packet and call level 3 directly
353 */
354 void
355 pkintr()
356 {
357 struct mbuf *m;
358 int s;
359
360 for (;;) {
361 s = splnet();
362 IF_DEQUEUE(&pkintrq, m);
363 splx(s);
364 if (m == 0)
365 break;
366 if (m->m_len < PKHEADERLN) {
367 printf("pkintr: packet too short (len=%d)\n",
368 m->m_len);
369 m_freem(m);
370 continue;
371 }
372 pk_input(m);
373 }
374 }
375
376 struct mbuf *pk_bad_packet;
377 struct mbuf_cache pk_input_cache = {0};
378 /*
379 * X.25 PACKET INPUT
380 *
381 * This procedure is called by a link level procedure whenever an information
382 * frame is received. It decodes the packet and demultiplexes based on the
383 * logical channel number.
384 *
385 * We change the original conventions of the UBC code here -- since there may be
386 * multiple pkcb's for a given interface of type 802.2 class 2, we retrieve
387 * which one it is from m_pkthdr.rcvif (which has been overwritten by lower
388 * layers); That field is then restored for the benefit of upper layers which
389 * may make use of it, such as CLNP.
390 *
391 */
392
393 #define RESTART_DTE_ORIGINATED(xp) \
394 (((xp)->packet_cause == X25_RESTART_DTE_ORIGINATED) || \
395 ((xp)->packet_cause >= X25_RESTART_DTE_ORIGINATED2))
396
397 void
398 #if __STDC__
399 pk_input(struct mbuf *m, ...)
400 #else
401 pk_input(m, va_alist)
402 struct mbuf *m;
403 va_dcl
404 #endif
405 {
406 struct x25_packet *xp;
407 struct pklcd *lcp;
408 struct socket *so = 0;
409 struct pkcb *pkp;
410 int ptype, lcn, lcdstate = LISTEN;
411
412 if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize)
413 mbuf_cache(&pk_input_cache, m);
414 if ((m->m_flags & M_PKTHDR) == 0)
415 panic("pkintr");
416
417 if ((pkp = (struct pkcb *) m->m_pkthdr.rcvif) == 0)
418 return;
419 xp = mtod(m, struct x25_packet *);
420 ptype = pk_decode(xp);
421 lcn = LCN(xp);
422 lcp = pkp->pk_chan[lcn];
423
424 /*
425 * If the DTE is in Restart state, then it will ignore data,
426 * interrupt, call setup and clearing, flow control and reset
427 * packets.
428 */
429 if (lcn < 0 || lcn > pkp->pk_maxlcn) {
430 pk_message(lcn, pkp->pk_xcp, "illegal lcn");
431 m_freem(m);
432 return;
433 }
434 pk_trace(pkp->pk_xcp, m, "P-In");
435
436 if (pkp->pk_state != DTE_READY && ptype != PK_RESTART &&
437 ptype != PK_RESTART_CONF) {
438 m_freem(m);
439 return;
440 }
441 if (lcp) {
442 so = lcp->lcd_so;
443 lcdstate = lcp->lcd_state;
444 } else {
445 if (ptype == PK_CLEAR) {/* idle line probe (Datapac specific) */
446 /* send response on lcd 0's output queue */
447 lcp = pkp->pk_chan[0];
448 lcp->lcd_template = pk_template(lcn, X25_CLEAR_CONFIRM);
449 pk_output(lcp);
450 m_freem(m);
451 return;
452 }
453 if (ptype != PK_CALL)
454 ptype = PK_INVALID_PACKET;
455 }
456
457 if (lcn == 0 && ptype != PK_RESTART && ptype != PK_RESTART_CONF) {
458 pk_message(0, pkp->pk_xcp, "illegal ptype (%d, %s) on lcn 0",
459 ptype, pk_name[ptype / MAXSTATES]);
460 if (pk_bad_packet)
461 m_freem(pk_bad_packet);
462 pk_bad_packet = m;
463 return;
464 }
465 m->m_pkthdr.rcvif = pkp->pk_ia->ia_ifp;
466
467 switch (ptype + lcdstate) {
468 /*
469 * Incoming Call packet received.
470 */
471 case PK_CALL + LISTEN:
472 pk_incoming_call(pkp, m);
473 break;
474
475 /*
476 * Call collision: Just throw this "incoming call" away since
477 * the DCE will ignore it anyway.
478 */
479 case PK_CALL + SENT_CALL:
480 pk_message((int) lcn, pkp->pk_xcp,
481 "incoming call collision");
482 break;
483
484 /*
485 * Call confirmation packet received. This usually means our
486 * previous connect request is now complete.
487 */
488 case PK_CALL_ACCEPTED + SENT_CALL:
489 MCHTYPE(m, MT_CONTROL);
490 pk_call_accepted(lcp, m);
491 break;
492
493 /*
494 * This condition can only happen if the previous state was
495 * SENT_CALL. Just ignore the packet, eventually a clear
496 * confirmation should arrive.
497 */
498 case PK_CALL_ACCEPTED + SENT_CLEAR:
499 break;
500
501 /*
502 * Clear packet received. This requires a complete tear down
503 * of the virtual circuit. Free buffers and control blocks.
504 * and send a clear confirmation.
505 */
506 case PK_CLEAR + READY:
507 case PK_CLEAR + RECEIVED_CALL:
508 case PK_CLEAR + SENT_CALL:
509 case PK_CLEAR + DATA_TRANSFER:
510 lcp->lcd_state = RECEIVED_CLEAR;
511 lcp->lcd_template = pk_template(lcp->lcd_lcn, X25_CLEAR_CONFIRM);
512 pk_output(lcp);
513 pk_clearcause(pkp, xp);
514 if (lcp->lcd_upper) {
515 MCHTYPE(m, MT_CONTROL);
516 (*lcp->lcd_upper)(m, lcp);
517 }
518 pk_close(lcp);
519 lcp = 0;
520 break;
521
522 /*
523 * Clear collision: Treat this clear packet as a
524 * confirmation.
525 */
526 case PK_CLEAR + SENT_CLEAR:
527 pk_close(lcp);
528 break;
529
530 /*
531 * Clear confirmation received. This usually means the
532 * virtual circuit is now completely removed.
533 */
534 case PK_CLEAR_CONF + SENT_CLEAR:
535 pk_close(lcp);
536 break;
537
538 /*
539 * A clear confirmation on an unassigned logical channel -
540 * just ignore it. Note: All other packets on an unassigned
541 * channel results in a clear.
542 */
543 case PK_CLEAR_CONF + READY:
544 case PK_CLEAR_CONF + LISTEN:
545 break;
546
547 /*
548 * Data packet received. Pass on to next level. Move the Q
549 * and M bits into the data portion for the next level.
550 */
551 case PK_DATA + DATA_TRANSFER:
552 if (lcp->lcd_reset_condition) {
553 ptype = PK_DELETE_PACKET;
554 break;
555 }
556 /*
557 * Process the P(S) flow control information in this Data
558 * packet. Check that the packets arrive in the correct
559 * sequence and that they are within the "lcd_input_window".
560 * Input window rotation is initiated by the receive
561 * interface.
562 */
563
564 if (PS(xp) != ((lcp->lcd_rsn + 1) % MODULUS) ||
565 PS(xp) == ((lcp->lcd_input_window + lcp->lcd_windowsize) % MODULUS)) {
566 m_freem(m);
567 pk_procerror(PK_RESET, lcp,
568 "p(s) flow control error", 1);
569 break;
570 }
571 lcp->lcd_rsn = PS(xp);
572
573 if (pk_ack(lcp, PR(xp)) != PACKET_OK) {
574 m_freem(m);
575 break;
576 }
577 m->m_data += PKHEADERLN;
578 m->m_len -= PKHEADERLN;
579 m->m_pkthdr.len -= PKHEADERLN;
580
581 lcp->lcd_rxcnt++;
582 if (lcp->lcd_flags & X25_MBS_HOLD) {
583 struct mbuf *n = lcp->lcd_cps;
584 int mbit = MBIT(xp);
585 octet q_and_d_bits;
586
587 if (n) {
588 n->m_pkthdr.len += m->m_pkthdr.len;
589 while (n->m_next)
590 n = n->m_next;
591 n->m_next = m;
592 m = lcp->lcd_cps;
593
594 if (lcp->lcd_cpsmax &&
595 n->m_pkthdr.len > lcp->lcd_cpsmax) {
596 pk_procerror(PK_RESET, lcp,
597 "C.P.S. overflow", 128);
598 return;
599 }
600 q_and_d_bits = 0xc0 & *(octet *) xp;
601 xp = (struct x25_packet *)
602 (mtod(m, octet *) - PKHEADERLN);
603 *(octet *) xp |= q_and_d_bits;
604 }
605 if (mbit) {
606 lcp->lcd_cps = m;
607 pk_flowcontrol(lcp, 0, 1);
608 return;
609 }
610 lcp->lcd_cps = 0;
611 }
612 if (so == 0)
613 break;
614 if (lcp->lcd_flags & X25_MQBIT) {
615 octet t = (X25GBITS(xp->bits, q_bit)) ? t = 0x80 : 0;
616
617 if (MBIT(xp))
618 t |= 0x40;
619 m->m_data -= 1;
620 m->m_len += 1;
621 m->m_pkthdr.len += 1;
622 *mtod(m, octet *) = t;
623 }
624 /*
625 * Discard Q-BIT packets if the application
626 * doesn't want to be informed of M and Q bit status
627 */
628 if (X25GBITS(xp->bits, q_bit)
629 && (lcp->lcd_flags & X25_MQBIT) == 0) {
630 m_freem(m);
631 /*
632 * NB. This is dangerous: sending a RR here can
633 * cause sequence number errors if a previous data
634 * packet has not yet been passed up to the application
635 * (RR's are normally generated via PRU_RCVD).
636 */
637 pk_flowcontrol(lcp, 0, 1);
638 } else {
639 sbappendrecord(&so->so_rcv, m);
640 sorwakeup(so);
641 }
642 break;
643
644 /*
645 * Interrupt packet received.
646 */
647 case PK_INTERRUPT + DATA_TRANSFER:
648 if (lcp->lcd_reset_condition)
649 break;
650 lcp->lcd_intrdata = xp->packet_data;
651 lcp->lcd_template = pk_template(lcp->lcd_lcn,
652 X25_INTERRUPT_CONFIRM);
653 pk_output(lcp);
654 m->m_data += PKHEADERLN;
655 m->m_len -= PKHEADERLN;
656 m->m_pkthdr.len -= PKHEADERLN;
657 MCHTYPE(m, MT_OOBDATA);
658 if (so) {
659 if (so->so_options & SO_OOBINLINE)
660 sbinsertoob(&so->so_rcv, m);
661 else
662 m_freem(m);
663 sohasoutofband(so);
664 }
665 break;
666
667 /*
668 * Interrupt confirmation packet received.
669 */
670 case PK_INTERRUPT_CONF + DATA_TRANSFER:
671 if (lcp->lcd_reset_condition)
672 break;
673 if (lcp->lcd_intrconf_pending == TRUE)
674 lcp->lcd_intrconf_pending = FALSE;
675 else
676 pk_procerror(PK_RESET, lcp, "unexpected packet", 43);
677 break;
678
679 /*
680 * Receiver ready received. Rotate the output window and
681 * output any data packets waiting transmission.
682 */
683 case PK_RR + DATA_TRANSFER:
684 if (lcp->lcd_reset_condition ||
685 pk_ack(lcp, PR(xp)) != PACKET_OK) {
686 ptype = PK_DELETE_PACKET;
687 break;
688 }
689 if (lcp->lcd_rnr_condition == TRUE)
690 lcp->lcd_rnr_condition = FALSE;
691 pk_output(lcp);
692 break;
693
694 /*
695 * Receiver Not Ready received. Packets up to the P(R) can be
696 * be sent. Condition is cleared with a RR.
697 */
698 case PK_RNR + DATA_TRANSFER:
699 if (lcp->lcd_reset_condition ||
700 pk_ack(lcp, PR(xp)) != PACKET_OK) {
701 ptype = PK_DELETE_PACKET;
702 break;
703 }
704 lcp->lcd_rnr_condition = TRUE;
705 break;
706
707 /*
708 * Reset packet received. Set state to FLOW_OPEN. The Input
709 * and Output window edges ar set to zero. Both the send and
710 * receive numbers are reset. A confirmation is returned.
711 */
712 case PK_RESET + DATA_TRANSFER:
713 if (lcp->lcd_reset_condition)
714 /* Reset collision. Just ignore packet. */
715 break;
716
717 pk_resetcause(pkp, xp);
718 lcp->lcd_window_condition = lcp->lcd_rnr_condition =
719 lcp->lcd_intrconf_pending = FALSE;
720 lcp->lcd_output_window = lcp->lcd_input_window =
721 lcp->lcd_last_transmitted_pr = 0;
722 lcp->lcd_ssn = 0;
723 lcp->lcd_rsn = MODULUS - 1;
724
725 lcp->lcd_template = pk_template(lcp->lcd_lcn, X25_RESET_CONFIRM);
726 pk_output(lcp);
727
728 pk_flush(lcp);
729 if (so == 0)
730 break;
731 wakeup((caddr_t) & so->so_timeo);
732 sorwakeup(so);
733 sowwakeup(so);
734 break;
735
736 /*
737 * Reset confirmation received.
738 */
739 case PK_RESET_CONF + DATA_TRANSFER:
740 if (lcp->lcd_reset_condition) {
741 lcp->lcd_reset_condition = FALSE;
742 pk_output(lcp);
743 } else
744 pk_procerror(PK_RESET, lcp, "unexpected packet", 32);
745 break;
746
747 case PK_DATA + SENT_CLEAR:
748 ptype = PK_DELETE_PACKET;
749 case PK_RR + SENT_CLEAR:
750 case PK_RNR + SENT_CLEAR:
751 case PK_INTERRUPT + SENT_CLEAR:
752 case PK_INTERRUPT_CONF + SENT_CLEAR:
753 case PK_RESET + SENT_CLEAR:
754 case PK_RESET_CONF + SENT_CLEAR:
755 /*
756 * Just ignore p if we have sent a CLEAR already.
757 */
758 break;
759
760 /*
761 * Restart sets all the permanent virtual circuits to the
762 * "Data Transfer" stae and all the switched virtual
763 * circuits to the "Ready" state.
764 */
765 case PK_RESTART + READY:
766 switch (pkp->pk_state) {
767 case DTE_SENT_RESTART:
768 /*
769 * Restart collision. If case the restart cause is
770 * "DTE originated" we have a DTE-DTE situation and
771 * are trying to resolve who is going to play DTE/DCE
772 * [ISO 8208:4.2-4.5]
773 */
774 if (RESTART_DTE_ORIGINATED(xp)) {
775 pk_restart(pkp, X25_RESTART_DTE_ORIGINATED);
776 pk_message(0, pkp->pk_xcp,
777 "RESTART collision");
778 if ((pkp->pk_restartcolls++) > MAXRESTARTCOLLISIONS) {
779 pk_message(0, pkp->pk_xcp,
780 "excessive RESTART collisions");
781 pkp->pk_restartcolls = 0;
782 }
783 break;
784 }
785 pkp->pk_state = DTE_READY;
786 pkp->pk_dxerole |= DTE_PLAYDTE;
787 pkp->pk_dxerole &= ~DTE_PLAYDCE;
788 pk_message(0, pkp->pk_xcp,
789 "Packet level operational");
790 pk_message(0, pkp->pk_xcp,
791 "Assuming DTE role");
792 if (pkp->pk_dxerole & DTE_CONNECTPENDING)
793 pk_callcomplete(pkp);
794 break;
795
796 default:
797 pk_restart(pkp, -1);
798 pk_restartcause(pkp, xp);
799 pkp->pk_chan[0]->lcd_template = pk_template(0,
800 X25_RESTART_CONFIRM);
801 pk_output(pkp->pk_chan[0]);
802 pkp->pk_state = DTE_READY;
803 pkp->pk_dxerole |= RESTART_DTE_ORIGINATED(xp) ? DTE_PLAYDCE :
804 DTE_PLAYDTE;
805 if (pkp->pk_dxerole & DTE_PLAYDTE) {
806 pkp->pk_dxerole &= ~DTE_PLAYDCE;
807 pk_message(0, pkp->pk_xcp,
808 "Assuming DTE role");
809 } else {
810 pkp->pk_dxerole &= ~DTE_PLAYDTE;
811 pk_message(0, pkp->pk_xcp,
812 "Assuming DCE role");
813 }
814 if (pkp->pk_dxerole & DTE_CONNECTPENDING)
815 pk_callcomplete(pkp);
816 }
817 break;
818
819 /*
820 * Restart confirmation received. All logical channels are
821 * set to READY.
822 */
823 case PK_RESTART_CONF + READY:
824 switch (pkp->pk_state) {
825 case DTE_SENT_RESTART:
826 pkp->pk_state = DTE_READY;
827 pkp->pk_dxerole |= DTE_PLAYDTE;
828 pkp->pk_dxerole &= ~DTE_PLAYDCE;
829 pk_message(0, pkp->pk_xcp,
830 "Packet level operational");
831 pk_message(0, pkp->pk_xcp,
832 "Assuming DTE role");
833 if (pkp->pk_dxerole & DTE_CONNECTPENDING)
834 pk_callcomplete(pkp);
835 break;
836
837 default:
838 /* Restart local procedure error. */
839 pk_restart(pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR);
840 pkp->pk_state = DTE_SENT_RESTART;
841 pkp->pk_dxerole &= ~(DTE_PLAYDTE | DTE_PLAYDCE);
842 }
843 break;
844
845 default:
846 if (lcp) {
847 pk_procerror(PK_CLEAR, lcp, "unknown packet error", 33);
848 pk_message(lcn, pkp->pk_xcp,
849 "\"%s\" unexpected in \"%s\" state",
850 pk_name[ptype / MAXSTATES], pk_state[lcdstate]);
851 } else
852 pk_message(lcn, pkp->pk_xcp,
853 "packet arrived on unassigned lcn");
854 break;
855 }
856 if (so == 0 && lcp && lcp->lcd_upper && lcdstate == DATA_TRANSFER) {
857 if (ptype != PK_DATA && ptype != PK_INTERRUPT)
858 MCHTYPE(m, MT_CONTROL);
859 lcp->lcd_upper(m, lcp);
860 } else if (ptype != PK_DATA && ptype != PK_INTERRUPT)
861 m_freem(m);
862 }
863
864 static void
865 prune_dnic(from, to, dnicname, xcp)
866 char *from, *to, *dnicname;
867 struct x25config *xcp;
868 {
869 char *cp1 = from, *cp2 = from;
870 if (xcp->xc_prepnd0 && *cp1 == '') {
871 from = ++cp1;
872 goto copyrest;
873 }
874 if (xcp->xc_nodnic) {
875 for (cp1 = dnicname; (*cp2 = *cp1++) != '\0';)
876 cp2++;
877 cp1 = from;
878 }
879 copyrest:
880 for (cp1 = dnicname; (*cp2 = *cp1++) != '\0';)
881 cp2++;
882 }
883
884 void
885 pk_simple_bsd(from, to, lower, len)
886 octet *from, *to;
887 int len, lower;
888 {
889 int c;
890 while (--len >= 0) {
891 c = *from;
892 if (lower & 0x01)
893 from++;
894 else
895 c >>= 4;
896 c &= 0x0f;
897 c |= 0x30;
898 *to++ = c;
899 lower++;
900 }
901 *to = 0;
902 }
903
904 void
905 pk_from_bcd(a, iscalling, sa, xcp)
906 struct x25_calladdr *a;
907 int iscalling;
908 struct sockaddr_x25 *sa;
909 struct x25config *xcp;
910 {
911 octet buf[MAXADDRLN + 1];
912 octet *cp;
913 unsigned count;
914
915 bzero((caddr_t) sa, sizeof(*sa));
916 sa->x25_len = sizeof(*sa);
917 sa->x25_family = AF_CCITT;
918 if (iscalling) {
919 cp = a->address_field + (X25GBITS(a->addrlens, called_addrlen) / 2);
920 count = X25GBITS(a->addrlens, calling_addrlen);
921 pk_simple_bsd(cp, buf, X25GBITS(a->addrlens, called_addrlen), count);
922 } else {
923 count = X25GBITS(a->addrlens, called_addrlen);
924 pk_simple_bsd(a->address_field, buf, 0, count);
925 }
926 if (xcp->xc_addr.x25_net && (xcp->xc_nodnic || xcp->xc_prepnd0)) {
927 octet dnicname[sizeof(long) * NBBY / 3 + 2];
928
929 sprintf((char *) dnicname, "%d", xcp->xc_addr.x25_net);
930 prune_dnic((char *) buf, sa->x25_addr, dnicname, xcp);
931 } else
932 bcopy((caddr_t) buf, (caddr_t) sa->x25_addr, count + 1);
933 }
934
935 static void
936 save_extra(m0, fp, so)
937 struct mbuf *m0;
938 octet *fp;
939 struct socket *so;
940 {
941 struct mbuf *m;
942 struct cmsghdr cmsghdr;
943 /* XXX: christos:
944 * used to be m_copy(m, 0, ...)
945 * I think it is supposed to be m_copy(m0,
946 */
947 if ((m = m_copy(m0, 0, (int) M_COPYALL)) != NULL) {
948 int off = fp - mtod(m0, octet *);
949 int len = m->m_pkthdr.len - off + sizeof(cmsghdr);
950 cmsghdr.cmsg_len = len;
951 cmsghdr.cmsg_level = AF_CCITT;
952 cmsghdr.cmsg_type = PK_FACILITIES;
953 m_adj(m, off);
954 M_PREPEND(m, sizeof(cmsghdr), M_DONTWAIT);
955 if (m == 0)
956 return;
957 bcopy((caddr_t) & cmsghdr, mtod(m, caddr_t), sizeof(cmsghdr));
958 MCHTYPE(m, MT_CONTROL);
959 sbappendrecord(&so->so_rcv, m);
960 }
961 }
962
963 /*
964 * This routine handles incoming call packets. It matches the protocol field
965 * on the Call User Data field (usually the first four bytes) with sockets
966 * awaiting connections.
967 */
968
969 void
970 pk_incoming_call(pkp, m0)
971 struct pkcb *pkp;
972 struct mbuf *m0;
973 {
974 struct pklcd *lcp = 0, *l;
975 struct sockaddr_x25 *sa;
976 struct x25_calladdr *a;
977 struct socket *so = 0;
978 struct x25_packet *xp = mtod(m0, struct x25_packet *);
979 struct mbuf *m;
980 struct x25config *xcp = pkp->pk_xcp;
981 int len = m0->m_pkthdr.len;
982 unsigned udlen;
983 char *errstr = "server unavailable";
984 octet *u, *facp;
985 int lcn = LCN(xp);
986
987 /*
988 * First, copy the data from the incoming call packet to a X25
989 * address descriptor. It is to be regretted that you have to parse
990 * the facilities into a sockaddr to determine if reverse charging is
991 * being requested
992 */
993 if ((m = m_get(M_DONTWAIT, MT_SONAME)) == 0)
994 return;
995 sa = mtod(m, struct sockaddr_x25 *);
996 a = (struct x25_calladdr *) & xp->packet_data;
997 facp = u = (octet *) (a->address_field +
998 ((X25GBITS(a->addrlens, called_addrlen) + X25GBITS(a->addrlens, calling_addrlen) + 1) / 2));
999 u += *u + 1;
1000 udlen = min(16, ((octet *) xp) + len - u);
1001 #if 0
1002 /* Cannot happen; udlen is unsigned */
1003 if (udlen < 0)
1004 udlen = 0;
1005 #endif
1006 pk_from_bcd(a, 1, sa, pkp->pk_xcp); /* get calling address */
1007 pk_parse_facilities(facp, sa);
1008 bcopy((caddr_t) u, sa->x25_udata, udlen);
1009 sa->x25_udlen = udlen;
1010
1011 /*
1012 * Now, loop through the listen sockets looking for a match on the
1013 * PID. That is the first few octets of the user data field.
1014 * This is the closest thing to a port number for X.25 packets.
1015 * It does provide a way of multiplexing services at the user level.
1016 */
1017
1018 for (l = pk_listenhead.tqh_first; l; l = l->lcd_listen.tqe_next) {
1019 struct sockaddr_x25 *sxp = l->lcd_ceaddr;
1020
1021 if (bcmp(sxp->x25_udata, u, sxp->x25_udlen))
1022 continue;
1023 if (sxp->x25_net &&
1024 sxp->x25_net != xcp->xc_addr.x25_net)
1025 continue;
1026 /*
1027 * don't accept incoming calls with the D-Bit on
1028 * unless the server agrees
1029 */
1030 if (X25GBITS(xp->bits, d_bit) && !(sxp->x25_opts.op_flags & X25_DBIT)) {
1031 errstr = "incoming D-Bit mismatch";
1032 break;
1033 }
1034 /*
1035 * don't accept incoming collect calls unless
1036 * the server sets the reverse charging option.
1037 */
1038 if ((sxp->x25_opts.op_flags & (X25_OLDSOCKADDR | X25_REVERSE_CHARGE)) == 0 &&
1039 sa->x25_opts.op_flags & X25_REVERSE_CHARGE) {
1040 errstr = "incoming collect call refused";
1041 break;
1042 }
1043 if (l->lcd_so) {
1044 so = sonewconn(l->lcd_so, SS_ISCONNECTED);
1045 if (so)
1046 lcp = (struct pklcd *) so->so_pcb;
1047 } else
1048 lcp = pk_attach((struct socket *) 0);
1049 if (lcp == 0) {
1050 /*
1051 * Insufficient space or too many unaccepted
1052 * connections. Just throw the call away.
1053 */
1054 errstr = "server malfunction";
1055 break;
1056 }
1057 lcp->lcd_upper = l->lcd_upper;
1058 lcp->lcd_upnext = l->lcd_upnext;
1059 lcp->lcd_lcn = lcn;
1060 lcp->lcd_state = RECEIVED_CALL;
1061 sa->x25_opts.op_flags |= (sxp->x25_opts.op_flags &
1062 ~X25_REVERSE_CHARGE) | l->lcd_flags;
1063 pk_assoc(pkp, lcp, sa);
1064 lcp->lcd_faddr = *sa;
1065 lcp->lcd_laddr.x25_udlen = sxp->x25_udlen;
1066 lcp->lcd_craddr = &lcp->lcd_faddr;
1067 lcp->lcd_template = pk_template(lcp->lcd_lcn, X25_CALL_ACCEPTED);
1068 if (lcp->lcd_flags & X25_DBIT) {
1069 if (X25GBITS(xp->bits, d_bit))
1070 X25SBITS(mtod(lcp->lcd_template,
1071 struct x25_packet *)->bits, d_bit, 1);
1072 else
1073 lcp->lcd_flags &= ~X25_DBIT;
1074 }
1075 if (so) {
1076 pk_output(lcp);
1077 soisconnected(so);
1078 if (so->so_options & SO_OOBINLINE)
1079 save_extra(m0, facp, so);
1080 } else if (lcp->lcd_upper) {
1081 (*lcp->lcd_upper) (m0, lcp);
1082 }
1083 (void) m_free(m);
1084 return;
1085 }
1086
1087 /*
1088 * If the call fails for whatever reason, we still need to build a
1089 * skeleton LCD in order to be able to properly receive the CLEAR
1090 * CONFIRMATION.
1091 */
1092 #ifdef WATERLOO /* be explicit */
1093 if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0)
1094 pk_message(lcn, pkp->pk_xcp, "host=%s ean%c: %s",
1095 sa->x25_addr, sa->x25_udata[3] & 0xff, errstr);
1096 else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0)
1097 pk_message(lcn, pkp->pk_xcp, "host=%s x29d: %s",
1098 sa->x25_addr, errstr);
1099 else
1100 #endif
1101 pk_message(lcn, pkp->pk_xcp, "host=%s pid=%x %x %x %x: %s",
1102 sa->x25_addr, sa->x25_udata[0] & 0xff,
1103 sa->x25_udata[1] & 0xff, sa->x25_udata[2] & 0xff,
1104 sa->x25_udata[3] & 0xff, errstr);
1105 if ((lcp = pk_attach((struct socket *) 0)) == 0) {
1106 (void) m_free(m);
1107 return;
1108 }
1109 lcp->lcd_lcn = lcn;
1110 lcp->lcd_state = RECEIVED_CALL;
1111 pk_assoc(pkp, lcp, sa);
1112 (void) m_free(m);
1113 pk_clear(lcp, 0, 1);
1114 }
1115
1116 void
1117 pk_call_accepted(lcp, m)
1118 struct pklcd *lcp;
1119 struct mbuf *m;
1120 {
1121 struct x25_calladdr *ap;
1122 octet *fcp;
1123 struct x25_packet *xp = mtod(m, struct x25_packet *);
1124 int len = m->m_len;
1125
1126 lcp->lcd_state = DATA_TRANSFER;
1127 if (lcp->lcd_so)
1128 soisconnected(lcp->lcd_so);
1129 if ((lcp->lcd_flags & X25_DBIT) && (X25GBITS(xp->bits, d_bit) == 0))
1130 lcp->lcd_flags &= ~X25_DBIT;
1131 if (len > 3) {
1132 ap = (struct x25_calladdr *) & xp->packet_data;
1133 fcp = (octet *) ap->address_field + (X25GBITS(ap->addrlens, calling_addrlen) +
1134 X25GBITS(ap->addrlens, called_addrlen) + 1) / 2;
1135 if (fcp + *fcp <= ((octet *) xp) + len)
1136 pk_parse_facilities(fcp, lcp->lcd_ceaddr);
1137 }
1138 pk_assoc(lcp->lcd_pkp, lcp, lcp->lcd_ceaddr);
1139 if (lcp->lcd_so == 0 && lcp->lcd_upper)
1140 (*lcp->lcd_upper)(m, lcp);
1141 }
1142
1143 void
1144 pk_parse_facilities(fcp, sa)
1145 octet *fcp;
1146 struct sockaddr_x25 *sa;
1147 {
1148 octet *maxfcp;
1149
1150 maxfcp = fcp + *fcp;
1151 fcp++;
1152 while (fcp < maxfcp) {
1153 /*
1154 * Ignore national DCE or DTE facilities
1155 */
1156 if (*fcp == 0 || *fcp == 0xff)
1157 break;
1158 switch (*fcp) {
1159 case FACILITIES_WINDOWSIZE:
1160 sa->x25_opts.op_wsize = fcp[1];
1161 fcp += 3;
1162 break;
1163
1164 case FACILITIES_PACKETSIZE:
1165 sa->x25_opts.op_psize = fcp[1];
1166 fcp += 3;
1167 break;
1168
1169 case FACILITIES_THROUGHPUT:
1170 sa->x25_opts.op_speed = fcp[1];
1171 fcp += 2;
1172 break;
1173
1174 case FACILITIES_REVERSE_CHARGE:
1175 if (fcp[1] & 01)
1176 sa->x25_opts.op_flags |= X25_REVERSE_CHARGE;
1177 /*
1178 * Datapac specific: for a X.25(1976) DTE, bit 2
1179 * indicates a "hi priority" (eg. international) call.
1180 */
1181 if (fcp[1] & 02 && sa->x25_opts.op_psize == 0)
1182 sa->x25_opts.op_psize = X25_PS128;
1183 fcp += 2;
1184 break;
1185
1186 default:
1187 #if 0
1188 printf("unknown facility %x, class=%d\n", *fcp,
1189 (*fcp & 0xc0) >> 6);
1190 #endif
1191 switch ((*fcp & 0xc0) >> 6) {
1192 case 0:/* class A */
1193 fcp += 2;
1194 break;
1195
1196 case 1:
1197 fcp += 3;
1198 break;
1199
1200 case 2:
1201 fcp += 4;
1202 break;
1203
1204 case 3:
1205 fcp++;
1206 fcp += *fcp;
1207 }
1208 }
1209 }
1210 }
Cache object: 87f9961bae497d4078f0583bc11a642a
|