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