home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / AX25.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  30KB  |  878 lines

  1. /*  Low level AX.25 code:
  2.  *  incoming frame processing (including digipeating)
  3.  *  IP encapsulation
  4.  *  digipeater routing
  5.  *
  6.  *  Copyright 1991 Phil Karn, KA9Q / 1991 Kevin Hil, G1EMM
  7.  */
  8. /* Mods by PA0GRI */
  9.   
  10. #include "global.h"
  11. #ifdef AX25
  12. #include "mbuf.h"
  13. #include "iface.h"
  14. #include "arp.h"
  15. #include "slip.h"
  16. #include "ax25.h"
  17. #include "lapb.h"
  18. #include "netrom.h"
  19. #include "ip.h"
  20. #include "devparam.h"
  21. #include "trace.h"
  22. #include "pktdrvr.h"
  23. #include "netuser.h"
  24. #include "commands.h"
  25. #include <ctype.h>
  26.   
  27. static int axsend __ARGS((struct iface *iface,char *dest,char *source,
  28. int cmdrsp,int ctl,struct mbuf *data));
  29. static int axip_stop __ARGS((struct iface *iface));
  30. static int axip_raw __ARGS((struct iface *iface,struct mbuf *bp));
  31.   
  32. /* List of AX.25 multicast addresses in network format (shifted ascii).
  33.  * Only the first entry is used for transmissions, but any incoming
  34.  * packet with any one of these destination addresses is recognized
  35.  * as a multicast
  36.  */
  37. /* NOTE: IF you CHANGE the order of these, also change the codes in ax25.h !!!
  38.  * mailfor.c, nr3, and ax25cmd.c depend on this to get broadcast addresses !!!
  39.  * 920306 - WG7J
  40.  */
  41. char Ax25multi[][AXALEN] = {
  42.     'Q'<<1, 'S'<<1, 'T'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* QST */
  43.     'N'<<1, 'O'<<1, 'D'<<1, 'E'<<1, 'S'<<1, ' '<<1, '0'<<1, /* NODES */
  44.     'M'<<1, 'A'<<1, 'I'<<1, 'L'<<1, ' '<<1, ' '<<1, '0'<<1, /* MAIL */
  45.     'I'<<1, 'D'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* ID */
  46. #ifdef notdef
  47.     'O'<<1, 'P'<<1, 'E'<<1, 'N'<<1, ' '<<1, ' '<<1, '0'<<1, /* OPEN */
  48.     'C'<<1, 'Q'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* CQ */
  49.     'B'<<1, 'E'<<1, 'A'<<1, 'C'<<1, 'O'<<1, 'N'<<1, '0'<<1, /* BEACON */
  50.     'R'<<1, 'M'<<1, 'N'<<1, 'C'<<1, ' '<<1, ' '<<1, '0'<<1, /* RMNC */
  51.     'A'<<1, 'L'<<1, 'L'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* ALL */
  52. #endif
  53.     '\0',
  54. };
  55. char Mycall[AXALEN];
  56. char Myalias[AXALEN];    /* the NETROM alias in 'call' form */
  57. #ifdef MAILBOX
  58. char Bbscall[AXALEN];
  59. #endif
  60. #ifdef NETROM
  61. char Nralias[ALEN+1];      /* the NETROM alias in 'alias' form */
  62. #endif
  63. #ifdef CONVERS
  64. char Ccall[AXALEN];
  65. #endif
  66. #ifdef TTYCALL
  67. char Ttycall[AXALEN];
  68. #endif
  69.   
  70. struct ax_route *Ax_routes; /* Routing table header */
  71. #ifdef AXIP
  72. static int32 axipaddr[NAX25];   /* table of IP addresses of AX.25 interfaces */
  73. #ifdef PPP
  74. extern int16 DFAR fcstab[];      /* table used when calculating FCS */
  75. #else
  76. int16 fcstab[256] = {
  77.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  78.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  79.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  80.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  81.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  82.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  83.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  84.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  85.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  86.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  87.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  88.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  89.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  90.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  91.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  92.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  93.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  94.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  95.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  96.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  97.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  98.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  99.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  100.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  101.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  102.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  103.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  104.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  105.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  106.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  107.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  108.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  109. };
  110. #endif
  111. #endif
  112.   
  113. /* Send IP datagrams across an AX.25 link */
  114. int
  115. ax_send(bp,iface,gateway,prec,del,tput,rel)
  116. struct mbuf *bp;
  117. struct iface *iface;
  118. int32 gateway;
  119. int prec;
  120. int del;
  121. int tput;
  122. int rel;
  123. {
  124.     char *hw_addr;
  125.     struct ax25_cb *axp;
  126.     struct mbuf *tbp;
  127.   
  128.     struct ax_route *axr;
  129.     char mode = AX_DEFMODE;     /* default to interface mode */
  130.   
  131.     if(gateway == iface->broadcast) /* This is a broadcast IP datagram */
  132.         return (*iface->output)(iface,Ax25multi[0],iface->hwaddr,PID_IP,bp);
  133.   
  134.     if((hw_addr = res_arp(iface,ARP_AX25,gateway,bp)) == NULLCHAR)
  135.         return 0;   /* Wait for address resolution */
  136.   
  137.     /* If there's a defined route, get it */
  138.     axr = ax_lookup(hw_addr,iface);
  139.   
  140.     if(axr == NULLAXR) {
  141.         if(iface->flags & CONNECT_MODE){
  142.             mode = AX_VC_MODE;
  143.         } else {
  144.             mode = AX_DATMODE;
  145.         }
  146.     } else {
  147.         mode = axr->mode;
  148.         if(mode == AX_DEFMODE) {
  149.             if(iface->flags & CONNECT_MODE){
  150.                 mode = AX_VC_MODE;
  151.             } else {
  152.                 mode = AX_DATMODE;
  153.             }
  154.         }
  155.     }
  156.   
  157.     /* UI frames are used for any one of the following three conditions:
  158.      * 1. The "low delay" bit is set in the type-of-service field.
  159.      * 2. The "reliability" TOS bit is NOT set and the interface is in
  160.      *    datagram mode.
  161.      * 3. The destination is the broadcast address (this is helpful
  162.      *    when broadcasting on an interface that's in connected mode).
  163.      */
  164.     if(del || (!rel && (mode == AX_DATMODE))
  165.     || addreq(hw_addr,Ax25multi[0])){
  166.         /* Use UI frame */
  167.         return (*iface->output)(iface,hw_addr,iface->hwaddr,PID_IP,bp);
  168.     }
  169.   
  170.   
  171.     /* Reliability is needed; use I-frames in AX.25 connection */
  172.     if((axp = find_ax25(iface->hwaddr,hw_addr,iface)) == NULLAX25){
  173.         /* Open a new connection */
  174.         axp = open_ax25(iface,iface->hwaddr,hw_addr,
  175.         AX_ACTIVE,0,s_arcall,s_atcall,s_ascall,-1);
  176.         if(axp == NULLAX25){
  177.             free_p(bp);
  178.             return -1;
  179.         }
  180.     }
  181.     if(axp->state == LAPB_DISCONNECTED){
  182.         est_link(axp);
  183.         lapbstate(axp,LAPB_SETUP);
  184.     }
  185.     /* Insert the PID */
  186.     if((tbp = pushdown(bp,1)) == NULLBUF){
  187.         free_p(bp);
  188.         return -1;
  189.     }
  190.     bp = tbp;
  191.     bp->data[0] = PID_IP;
  192.     if((tbp = segmenter(bp,axp->paclen)) == NULLBUF){
  193.         free_p(bp);
  194.         return -1;
  195.     }
  196.     return send_ax25(axp,tbp,-1);
  197. }
  198. /* Add header and send connectionless (UI) AX.25 packet.
  199.  * Note that the calling order here must match enet_output
  200.  * since ARP also uses it.
  201.  */
  202. int
  203. ax_output(iface,dest,source,pid,data)
  204. struct iface *iface;    /* Interface to use; overrides routing table */
  205. char *dest;     /* Destination AX.25 address (7 bytes, shifted) */
  206. char *source;       /* Source AX.25 address (7 bytes, shifted) */
  207. int16 pid;      /* Protocol ID */
  208. struct mbuf *data;  /* Data field (follows PID) */
  209. {
  210.     struct mbuf *bp;
  211.   
  212.     /* Prepend pid to data */
  213.     bp = pushdown(data,1);
  214.     if(bp == NULLBUF){
  215.         free_p(data);
  216.         return -1;
  217.     }
  218.     bp->data[0] = (char)pid;
  219.     return axsend(iface,dest,source,LAPB_COMMAND,UI,bp);
  220. }
  221. /* Common subroutine for sendframe() and ax_output() */
  222. static int
  223. axsend(iface,dest,source,cmdrsp,ctl,data)
  224. struct iface *iface;    /* Interface to use; overrides routing table */
  225. char *dest;     /* Destination AX.25 address (7 bytes, shifted) */
  226. char *source;       /* Source AX.25 address (7 bytes, shifted) */
  227. int cmdrsp;     /* Command/response indication */
  228. int ctl;        /* Control field */
  229. struct mbuf *data;  /* Data field (includes PID) */
  230. {
  231.     struct mbuf *cbp;
  232.     struct ax25 addr;
  233.     struct ax_route *axr;
  234.     char *idest;
  235.     int rval;
  236.   
  237.     /* If the source addr is unspecified, use the interface address */
  238.     if(source[0] == '\0')
  239.         source = iface->hwaddr;
  240.   
  241.     /* If there's a digipeater route, get it */
  242.     axr = ax_lookup(dest,iface);
  243.   
  244.     memcpy(addr.dest,dest,AXALEN);
  245.     memcpy(addr.source,source,AXALEN);
  246.     addr.cmdrsp = cmdrsp;
  247.   
  248.     if(axr != NULLAXR){
  249.         memcpy(addr.digis,axr->digis,axr->ndigis*AXALEN);
  250.         addr.ndigis = axr->ndigis;
  251.         idest = addr.digis[0];
  252.     } else {
  253.         addr.ndigis = 0;
  254.         idest = dest;
  255.     }
  256.   
  257.     addr.nextdigi = 0;
  258.   
  259.     /* Allocate mbuf for control field, and fill in */
  260.     if((cbp = pushdown(data,1)) == NULLBUF){
  261.         free_p(data);
  262.         return -1;
  263.     }
  264.     cbp->data[0] = ctl;
  265.   
  266.     if((data = htonax25(&addr,cbp)) == NULLBUF){
  267.         free_p(cbp);    /* Also frees data */
  268.         return -1;
  269.     }
  270.     /* This shouldn't be necessary because redirection has already been
  271.      * done at the IP router layer, but just to be safe...
  272.      */
  273.     if(iface->forw != NULLIF){
  274. #ifdef notdef
  275.         logsrc(iface->forw,iface->forw->hwaddr);
  276. #endif
  277.         logsrc(iface->forw,source);
  278.         logdest(iface->forw,idest);
  279.         rval = (*iface->forw->raw)(iface->forw,data);
  280.     } else {
  281. #ifdef notdef
  282.         logsrc(iface,iface->hwaddr);
  283. #endif
  284.         logsrc(iface,source);
  285.         logdest(iface,idest);
  286.         rval = (*iface->raw)(iface,data);
  287.     }
  288.     return rval;
  289. }
  290. #ifdef  AXIP
  291. /* Handle AX.25 frames received inside IP according to RFC-1226 */
  292. void
  293. axip_input(iface,ip,bp,rxbroadcast)
  294. struct iface *iface;    /* Input interface */
  295. struct ip *ip;          /* IP header */
  296. struct mbuf *bp;        /* AX.25 frame with FCS */
  297. int rxbroadcast;    /* Accepted for now */
  298. {
  299.     int i;
  300.     struct mbuf *tbp;
  301.     int16 len, f, fcs = 0xffff;
  302.   
  303.     /* Since the AX.25 frame arrived on an interface that does
  304.        not necessarily support AX.25, we have to find a suitable
  305.        AX.25 interface, or drop the packet.
  306.      */
  307.     /* Try to find a matching AX.25 pseudo interface */
  308.     for(i=0; i < NAX25; ++i)
  309.         if(axipaddr[i] == ip->source)
  310.             break;
  311.     if(i == NAX25) {
  312.          /* Here we could still try to pick a real AX.25 interface,
  313.         but that would mean that we are accepting AX.25 frames
  314.         from unknown IP hosts, so we'd rather drop it.
  315.           */
  316.         free_p(bp);
  317.         return;
  318.     }
  319.     iface = Ifaces;
  320.     while (iface != NULLIF) {
  321.         if(iface->raw == axip_raw && iface->dev == i)
  322.           /* found the right AX.25 pseudo interface */
  323.             break;
  324.         iface = iface->next;
  325.     }
  326.     if(iface == NULLIF) {
  327.         free_p(bp);
  328.         return;
  329.     }
  330.     len = len_p(bp) - sizeof(fcs);
  331.     if(dup_p(&tbp,bp,0,len) != len) {
  332.         free_p(bp);
  333.         return;
  334.     }
  335.     while(len--)
  336.         fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp)) & 0x00ff];
  337.     fcs ^= 0xffff;
  338.     f = PULLCHAR(&bp);
  339.     f |= (PULLCHAR(&bp) << 8);
  340.     if(fcs == f){
  341.          /* add some tracing - WG7J */
  342. #ifdef TRACE
  343.         dump(iface,IF_TRACE_IN,CL_AX25,tbp);
  344. #endif
  345.         ax_recv(iface,tbp);
  346.     } else
  347.         free_p(tbp);
  348. }
  349. #endif
  350. /* Process incoming AX.25 packets.
  351.  * After optional tracing, the address field is examined. If it is
  352.  * directed to us as a digipeater, repeat it.  If it is addressed to
  353.  * us or to QST-0, kick it upstairs depending on the protocol ID.
  354.  */
  355. extern int32 CT4init;
  356.   
  357. void
  358. ax_recv(iface,bp)
  359. struct iface *iface;
  360. struct mbuf *bp;
  361. {
  362.     struct mbuf *hbp;
  363.     char control;
  364.     struct ax25 hdr;
  365.     struct ax25_cb *axp;
  366.     struct ax_route *axr;
  367.     struct iface *cross;
  368.     char (*mpp)[AXALEN];
  369.     int mcast,hwaddr,digiaddr = 0;
  370.     char *isrc,*idest;  /* "immediate" source and destination */
  371.     int To_us = 0;      /* Is this a link to us ? */
  372.   
  373.     /* Pull header off packet and convert to host structure */
  374.     if(ntohax25(&hdr,&bp) < 0){
  375.         /* Something wrong with the header */
  376.         free_p(bp);
  377.         return;
  378.     }
  379.   
  380.     if(iface->flags & LOG_IPHEARD) {
  381.         struct mbuf *nbp;
  382.         int pid;
  383.         struct ip ip;
  384.         int len;
  385.   
  386.         len = len_p(bp);
  387.         if(dup_p(&nbp, bp, 0, len) == len) {
  388.             /* find higher proto, if any */
  389.             (void) PULLCHAR(&nbp);  /* skip control byte */
  390.             if(PULLCHAR(&nbp) == PID_IP) {
  391.                 ntohip(&ip,&nbp);
  392.                 if(ip.version == IPVERSION)
  393.                     log_ipheard(ip.source,iface);
  394.             }
  395.             free_p(nbp);
  396.         }
  397.     }
  398.   
  399.     /* If there were digis in this packet and at least one has
  400.      * been passed, then the last passed digi is the immediate source.
  401.      * Otherwise it is the original source.
  402.      */
  403.     if(hdr.ndigis != 0 && hdr.nextdigi != 0)
  404.         isrc = hdr.digis[hdr.nextdigi-1];
  405.     else
  406.         isrc = hdr.source;
  407.   
  408.     /* If there are digis in this packet and not all have been passed,
  409.      * then the immediate destination is the next digi. Otherwise it
  410.      * is the final destination.
  411.      */
  412.     cross = NULLIF;
  413.     if(hdr.ndigis != 0 && hdr.nextdigi != hdr.ndigis){
  414.         idest = hdr.digis[hdr.nextdigi];
  415.         if(!addreq(idest,iface->hwaddr)){
  416.             /* Check if digi matches callsign of any other
  417.              * interface for crossband digipeating.
  418.              */
  419.             /* Modified to allow a separate crossband digi call
  420.              * for each interface. This works ONLY cross band,
  421.              * and works independent from the 'ax25 digiport' setting - WG7J
  422.              */
  423.             for(cross = Ifaces; cross != NULLIF; cross = cross->next){
  424.                 if(cross->type == CL_AX25) {
  425.                     if((hwaddr = addreq(idest,cross->hwaddr)) == 0)
  426.                         /* Only allow cdigi digipeating if BOTH interfaces
  427.                          * have a cdigi call set ! - WG7J
  428.                          */
  429.                         if(cross != iface && iface->ax25->cdigi[0] != '\0')
  430.                             digiaddr = addreq(idest,cross->ax25->cdigi);
  431.                     if(hwaddr || digiaddr) {
  432.                         /* Swap callsigns so that the reply
  433.                          * can be crossband digipeated in
  434.                          * the other direction.
  435.                          */
  436.                         if(hwaddr)
  437.                             memcpy(idest,iface->hwaddr,AXALEN);
  438.                         else
  439.                             memcpy(idest,iface->ax25->cdigi,AXALEN);
  440.                         break;
  441.                     }
  442.                 }
  443.             }
  444.         }
  445.     }
  446.     else
  447.         idest = hdr.dest;
  448.   
  449.     /* Don't log our own packets if we overhear them, as they're
  450.      * already logged by axsend() and by the digipeater code.
  451.      */
  452. #ifdef NETROM
  453.     if(!addreq(isrc,iface->hwaddr) &&
  454. #ifdef MAILBOX
  455.         !addreq(isrc,iface->ax25->bbscall) &&
  456. #endif
  457.     !addreq(isrc,Nr_iface->hwaddr)) {
  458. #else
  459.         if(!addreq(isrc,iface->hwaddr)
  460. #ifdef MAILBOX
  461.             && !addreq(isrc,iface->ax25->bbscall)
  462. #endif
  463.         ) {
  464. #endif
  465.             logsrc(iface,isrc);
  466.             logdest(iface,idest);
  467.         }
  468.     /* Examine immediate destination for a multicast address */
  469.         mcast = 0;
  470.         for(mpp = Ax25multi;(*mpp)[0] != '\0';mpp++){
  471.             if(addreq(idest,*mpp)){
  472.                 mcast = 1;
  473.                 break;
  474.             }
  475.         }
  476.   
  477.     /* Now check for any connection already in the AX.25 cb list
  478.      * This allows netrom user connects (with inverted ssid's) ,
  479.      * connections already established etc.. to pass.
  480.      * There should be no more digis needed!
  481.      * Added 11/15/91 WG7J
  482.      */
  483.         if(hdr.nextdigi == hdr.ndigis)
  484.         /* See if the source and destination address are in hash table */
  485.             if((axp = find_ax25(hdr.dest,hdr.source,iface)) != NULLAX25)
  486.                 To_us = KNOWN_LINK;
  487.   
  488.     /* Check to see if this is the Netrom interface callsign
  489.      * or the alias ! Accept any SSID on the alias.
  490.      * NOTE: This also allows digipeating via those calls!
  491.      * Added 11/15/91 WG7J
  492.      */
  493.         if(!To_us) {
  494.             if(!memcmp(idest,Myalias,ALEN))
  495.                 To_us = ALIAS_LINK;     /* this is for the alias */
  496.             else
  497. #ifdef NETROM
  498.                 if((iface->flags & IS_NR_IFACE) && addreq(idest,Nr_iface->hwaddr))
  499.                     To_us = NETROM_LINK;    /* this is for the netrom callsign! */
  500.                 else
  501. #endif
  502.                     if(addreq(idest,iface->hwaddr))
  503.                         To_us = IFACE_LINK;     /* this is to the interface call */
  504. #ifdef MAILBOX
  505.                     else if(addreq(idest,iface->ax25->bbscall))
  506.                         To_us = ALIAS_LINK;       /* This will jumpstart */
  507. #endif
  508. #ifdef CONVERS
  509.                     else if((iface->flags & IS_CONV_IFACE) && addreq(idest,Ccall))
  510.                         To_us = CONF_LINK;      /* this is for the conference call */
  511. #endif
  512. #ifdef TTYCALL
  513.                     else if (addreq(idest, Ttycall))
  514.                         To_us = TTY_LINK;
  515. #endif
  516.   
  517.         }
  518.   
  519.         if(!To_us && !mcast && !cross){
  520.         /* Not a broadcast, and not addressed to us. Inhibit
  521.          * transmitter to avoid colliding with addressed station's
  522.          * response, and discard packet.
  523.          */
  524. #ifdef notdef
  525.             if(iface->ioctl != NULL){
  526.                 (*iface->ioctl)(iface,PARAM_MUTE,1,-1L);
  527.             }
  528. #endif
  529.             free_p(bp);
  530.             return;
  531.         }
  532.   
  533. #ifdef notdef
  534.         if(!mcast && !cross && iface->ioctl != NULL){
  535.         /* Packet was sent to us; abort transmit inhibit */
  536.             (*iface->ioctl)(iface,PARAM_MUTE,1,0L);
  537.         }
  538. #endif
  539.     /* At this point, packet is either addressed to us, or is
  540.      * a multicast.
  541.      */
  542.         if(hdr.nextdigi < hdr.ndigis){
  543.         /* Packet requests digipeating. See if we can repeat it. */
  544.             if(digiaddr || ((iface->flags & AX25_DIGI) && !mcast)){
  545.             /* Yes, kick it back out. htonax25 will set the
  546.              * repeated bit.
  547.              */
  548.                 hdr.nextdigi++;
  549.                 if(cross)   /* Crossband digipeat */
  550.                     iface = cross;
  551.                 if(digiaddr || iface->flags & AX25_DIGI) {
  552.                     if((hbp = htonax25(&hdr,bp)) != NULLBUF){
  553.                         if(digiaddr)
  554.                             logsrc(iface,iface->ax25->cdigi);
  555.                         else
  556.                             logsrc(iface,iface->hwaddr);
  557.                         if(iface->forw != NULLIF){
  558.                             logdest(iface->forw,hdr.digis[hdr.nextdigi]);
  559.                             (*iface->forw->raw)(iface->forw,hbp);
  560.                         } else {
  561.                             logdest(iface,hdr.digis[hdr.nextdigi]);
  562.                             (*iface->raw)(iface,hbp);
  563.                         }
  564.                         bp = NULLBUF;
  565.                     }
  566.                 }
  567.             }
  568.             free_p(bp); /* Dispose if not forwarded */
  569.             return;
  570.         }
  571.     /* If we reach this point, then the packet has passed all digis,
  572.      * and is either addressed to us or is a multicast
  573.      */
  574.         if(bp == NULLBUF){
  575.             return;      /* Nothing left */
  576.         }
  577.   
  578.     /* If there's no locally-set entry in the routing table and
  579.      * this packet has digipeaters, create or update it. Leave
  580.      * local routes alone.
  581.      */
  582.         if(((axr = ax_lookup(hdr.source,iface)) == NULLAXR || axr->type == AX_AUTO)
  583.         && hdr.ndigis > 0){
  584.             char digis[MAXDIGIS][AXALEN];
  585.             int i,j;
  586.   
  587.         /* Construct reverse digipeater path */
  588.             for(i=hdr.ndigis-1,j=0;i >= 0;i--,j++){
  589.                 memcpy(digis[j],hdr.digis[i],AXALEN);
  590.                 digis[j][ALEN] &= ~(E|REPEATED);
  591.             }
  592.             ax_add(hdr.source,AX_AUTO,digis,hdr.ndigis,iface);
  593.         }
  594.     /* Sneak a peek at the control field. This kludge is necessary because
  595.      * AX.25 lacks a proper protocol ID field between the address and LAPB
  596.      * sublayers; a control value of UI indicates that LAPB is to be
  597.      * bypassed.
  598.      */
  599.         control = *bp->data & ~PF;
  600.   
  601.         if(uchar(control) == UI){
  602.             int pid;
  603.             struct axlink *ipp;
  604.   
  605.             (void) PULLCHAR(&bp);
  606.             if((pid = PULLCHAR(&bp)) == -1)
  607.                 return;     /* No PID */
  608.         /* Find network level protocol and hand it off */
  609.             for(ipp = Axlink;ipp->funct != NULL;ipp++){
  610.                 if(ipp->pid == pid)
  611.                     break;
  612.             }
  613.             if(ipp->funct != NULL)
  614.                 (*ipp->funct)(iface,NULLAX25,hdr.source,hdr.dest,bp,mcast);
  615.             else
  616.                 free_p(bp);
  617.             return;
  618.         }
  619.     /* Everything from here down is connected-mode LAPB, so ignore
  620.      * multicasts
  621.      */
  622.         if(mcast){
  623.             free_p(bp);
  624.             return;
  625.         }
  626.     /* At this point, if we already have a connection on its way,
  627.      * we already have found the control block !
  628.      * 11/15/91 WG7J/PA3DIS
  629.      */
  630.         if(To_us !=KNOWN_LINK) {
  631.         /* This is a new connection to either the interface call,
  632.          * or to the netrom-interface call or system alias.
  633.          * Create a new ax25 entry for this guy,
  634.          * insert into hash table keyed on his address,
  635.          * and initialize table entries
  636.          */
  637.             if((axp = cr_ax25(hdr.dest,hdr.source,iface)) == NULLAX25){
  638.                 free_p(bp);
  639.                 return;
  640.             }
  641. #ifdef CONVERS
  642.         /* set a different T4 if conference link - WG7J */
  643.             if(To_us == CONF_LINK)
  644.                 set_timer(&axp->t4,CT4init * 1000L);
  645. #endif
  646.             axp->jumpstarted = To_us;
  647.             if(hdr.cmdrsp == LAPB_UNKNOWN)
  648.                 axp->proto = V1;    /* Old protocol in use */
  649.         }
  650.         lapb_input(axp,hdr.cmdrsp,bp);
  651.     }
  652.   
  653. /* General purpose AX.25 frame output */
  654.     int
  655.     sendframe(axp,cmdrsp,ctl,data)
  656.     struct ax25_cb *axp;
  657.     int cmdrsp;
  658.     int ctl;
  659.     struct mbuf *data;
  660.     {
  661.         return axsend(axp->iface,axp->remote,axp->local,cmdrsp,ctl,data);
  662.     }
  663. /* Find a route for an AX.25 address.
  664.  * Code to remove SSID field C/R- and E-bits in ax_lookup(), ax_add() and
  665.  * ax_drop() added by vk6zjm 4/5/92. This eliminates duplicate AX25 routes.
  666.  * 1992-05-28 - Added interface -- sm6rpz
  667.  */
  668.   
  669.     struct ax_route *
  670.     ax_lookup(target,ifp)
  671.     char *target;
  672.     struct iface *ifp;
  673.     {
  674.         register struct ax_route *axr;
  675.         struct ax_route *axlast = NULLAXR;
  676.         char xtarget[AXALEN];
  677.   
  678.     /* Remove C/R and E bits in local copy only */
  679.         memcpy(xtarget,target,AXALEN);
  680.         xtarget[AXALEN-1] &= SSID;
  681.   
  682.         for(axr = Ax_routes; axr != NULLAXR; axlast = axr,axr = axr->next){
  683.             if(memcmp(axr->target,xtarget,AXALEN) == 0 && axr->iface == ifp){
  684.                 if(axr != Ax_routes){
  685.                 /* Move entry to top of list to speed
  686.                  * future searches
  687.                  */
  688.                     axlast->next = axr->next;
  689.                     axr->next = Ax_routes;
  690.                     Ax_routes = axr;
  691.   
  692.                 }
  693.                 return axr;
  694.             }
  695.         }
  696.         return axr;
  697.     }
  698. /* Add an entry to the AX.25 routing table */
  699.     struct ax_route *
  700.     ax_add(target,type,digis,ndigis,ifp)
  701.     char *target;
  702.     int type;
  703.     char digis[][AXALEN];
  704.     int ndigis;
  705.     struct iface *ifp;
  706.     {
  707.         register struct ax_route *axr;
  708.         char xtarget[AXALEN];
  709.   
  710.         if(ndigis < 0 || ndigis > MAXDIGIS || addreq(target,ifp->hwaddr))
  711.             return NULLAXR;
  712.   
  713.     /* Remove C/R and E bits in local copy only */
  714.         memcpy(xtarget,target,AXALEN);
  715.         xtarget[AXALEN-1] &= SSID;
  716.   
  717.         if((axr = ax_lookup(xtarget,ifp)) == NULLAXR){
  718.             axr = (struct ax_route *)callocw(1,sizeof(struct ax_route));
  719.             axr->next = Ax_routes;
  720.             Ax_routes = axr;
  721.             memcpy(axr->target,xtarget,AXALEN);
  722.             axr->ndigis = ndigis;
  723.             axr->iface = ifp;
  724.         }
  725.         axr->type = type;
  726.         axr->mode = AX_DEFMODE;     /* set mode to default */
  727.         if(axr->ndigis != ndigis)
  728.             axr->ndigis = ndigis;
  729.   
  730.         memcpy(axr->digis,digis[0],ndigis*AXALEN);
  731.         return axr;
  732.     }
  733.     int
  734.     ax_drop(target,ifp)
  735.     char *target;
  736.     struct iface *ifp;
  737.     {
  738.         register struct ax_route *axr;
  739.         struct ax_route *axlast = NULLAXR;
  740.         char xtarget[AXALEN];
  741.   
  742.     /* Remove C/R and E bits in local copy only */
  743.         memcpy(xtarget,target,AXALEN);
  744.         xtarget[AXALEN-1] &= SSID;
  745.   
  746.         for(axr = Ax_routes;axr != NULLAXR;axlast=axr,axr = axr->next)
  747.             if(memcmp(axr->target,xtarget,AXALEN) == 0 && axr->iface == ifp)
  748.                 break;
  749.         if(axr == NULLAXR)
  750.             return -1;  /* Not in table! */
  751.         if(axlast != NULLAXR)
  752.             axlast->next = axr->next;
  753.         else
  754.             Ax_routes = axr->next;
  755.   
  756.         free((char *)axr);
  757.         return 0;
  758.     }
  759. /* Handle ordinary incoming data (no network protocol) */
  760.     void
  761.     axnl3(iface,axp,src,dest,bp,mcast)
  762.     struct iface *iface;
  763.     struct ax25_cb *axp;
  764.     char *src;
  765.     char *dest;
  766.     struct mbuf *bp;
  767.     int mcast;
  768.     {
  769.         if(axp == NULLAX25){
  770.             free_p(bp);
  771.         /* beac_input(iface,src,bp); */
  772.         } else {
  773.             append(&axp->rxq,bp);
  774.             if(axp->r_upcall != NULLVFP)
  775.                 (*axp->r_upcall)(axp,len_p(axp->rxq));
  776.         }
  777.     }
  778.   
  779. #ifdef  AXIP
  780. /* attach a fake AX.25 interface for AX.25 on top of IP */
  781. /* argv[0] == "axip"
  782.  * argv[1] == name of new interface
  783.  * argv[2] == MTU
  784.  * argv[3] == hostname of remote end of wormhole
  785.  * argv[4] == optional AX.25 callsign for this interface, must be specified
  786.  *        and be different from any other interface callsign if crossband
  787.  *            digipeating is going to work properly
  788.  */
  789.     int
  790.     axip_attach(argc,argv,p)
  791.     int argc;
  792.     char *argv[];
  793.     void *p;
  794.     {
  795.         int i;
  796.         struct iface *ifp;
  797.   
  798.      /* Check for too long iface names - WG7J */
  799.         if(strlen(argv[1]) >= ILEN) {
  800.             tprintf("interface max %d chars\n",ILEN-1);
  801.             return -1;
  802.         }
  803.         if(if_lookup(argv[1]) != NULLIF) {
  804.             tprintf("interface %s already attached\n",argv[1]);
  805.             return -1;
  806.         }
  807.         for(i=0; i < NAX25; ++i)
  808.             if(axipaddr[i] == 0)
  809.                 break;
  810.         if(i == NAX25) {
  811.             tputs("too many AX25 interfaces attached\n");
  812.             return -1;
  813.         }
  814.         if((axipaddr[i] = resolve(argv[3])) == 0) {
  815.             tputs("invalid address\n");
  816.             return -1;
  817.         }
  818.         ifp = (struct iface *)callocw(1,sizeof(struct iface));
  819.         ifp->dev = i;
  820.         ifp->addr = Ip_addr;
  821.         ifp->name = strdup(argv[1]);
  822.         ifp->hwaddr = mallocw(AXALEN);
  823.         memcpy(ifp->hwaddr,Mycall,AXALEN);
  824.         ifp->mtu = atoi(argv[2]);
  825.         setencap(ifp,"AX25");
  826.         if(argc > 4) {
  827.             ifp->hwaddr = mallocw(ifp->iftype->hwalen);
  828.             (*ifp->iftype->scan)(ifp->hwaddr,argv[4]);
  829.         }
  830.         ifp->raw = axip_raw;
  831.         ifp->stop = axip_stop;
  832.         ifp->next = Ifaces;
  833.         Ifaces = ifp;
  834.         return 0;
  835.     }
  836.     static int
  837.     axip_stop(iface)
  838.     struct iface *iface;
  839.     {
  840.         axipaddr[iface->dev] = 0;
  841.         return 0;
  842.     }
  843.   
  844. /* raw routine for sending AX.25 on top of IP */
  845.     static int
  846.     axip_raw(iface,bp)
  847.     struct iface *iface;    /* Pointer to interface control block */
  848.     struct mbuf *bp;        /* Data field */
  849.     {
  850.         int16 len, fcs = 0xffff;
  851.         struct mbuf *bp1;
  852.   
  853.         dump(iface,IF_TRACE_OUT,iface->type,bp);
  854.         iface->rawsndcnt++;
  855.         iface->lastsent = secclock();
  856.         len = len_p(bp);
  857.         if(dup_p(&bp1,bp,0,len) != len) {
  858.             free_p(bp);
  859.             return -1;
  860.         }
  861.         while (len--)      /* calculate FCS */
  862.             fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp1)) & 0x00ff];
  863.   
  864.         fcs ^= 0xffff; /* final FCS (is this right?) */
  865.         if((bp1 = alloc_mbuf(sizeof (fcs))) == NULLBUF) {
  866.             free_p(bp);
  867.             return -1;
  868.         }
  869.         *bp1->data = fcs & 0xff;
  870.         *(bp1->data+1) = (fcs >> 8) & 0xff;
  871.         bp1->cnt += sizeof(fcs);
  872.         append(&bp,bp1);
  873.         return ip_send(INADDR_ANY,axipaddr[iface->dev],AX25_PTCL,0,0,bp,0,0,0);
  874.     }
  875. #endif
  876.   
  877. #endif /* AX25 */
  878.