home *** CD-ROM | disk | FTP | other *** search
/ PC Press: Internet / PC_PRESS.ISO / software / dos / misc / inar-100.exe / SRC / PCROUTE / ARP.INC next >
Encoding:
Text File  |  1995-05-21  |  16.8 KB  |  505 lines

  1. ;;*****************************************************************************
  2. ;;                        arp.inc           arp.inc
  3. ;;*****************************************************************************
  4. ;;
  5. ;;  Copyright (C) 1989 Northwestern University, Vance Morrison
  6. ;;
  7. ;;
  8. ;; Permission to view, compile, and modify for LOCAL (intra-organization) 
  9. ;; USE ONLY is hereby granted, provided that this copyright and permission 
  10. ;; notice appear on all copies.  Any other use by permission only.
  11. ;;
  12. ;; Northwestern University makes no representations about the suitability 
  13. ;; of this software for any purpose.  It is provided "as is" without expressed 
  14. ;; or implied warranty.  See the copywrite notice file for complete details.
  15. ;;
  16. ;;*****************************************************************************
  17. ;;
  18. ;; arp.inc constains the DL_IP interface for ethernet.  This is the module
  19. ;; that handles the arp lookup need to support the DL_IP interface 
  20. ;; (see dl_ip.inc for a description of the DL_IP interface)
  21. ;;
  22. ;; The functions provided by this file are
  23. ;;
  24. ;;   ARP_DECLARE name, ether
  25. ;;   ARP_DEFINE name, ip_address, ip_net, ip_mask
  26. ;;   ARP_DL_IP_R_READ name, code_label
  27. ;;   ARP_DL_IP_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES name, ok
  28. ;;   ARP_DL_IP_RETURN name
  29. ;;   ARP_DL_IP_W_ACCESS_in_CX_out_DI_ES_const_CX_BP name, fail
  30. ;;   ARP_DL_IP_W_WRITE_in_AX_BX_CX_const_BP name, broadcast
  31. ;;   ARP_DL_IP_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES name
  32. ;;   ARP_DL_IP_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  33. ;;
  34. ;;  Variables Provided by this module (READ ONLY!!!!)
  35. ;;      
  36. ;;      arp_&name&_declared         1 if this data structure is declared
  37. ;;      dl_ip_&name&_ip             the IP address
  38. ;;      dl_ip_&name&_mask           the network mask
  39. ;;      dl_ip_&name&_net            the network
  40. ;;      dl_ip_&name&_broad          the network broadcast address
  41. ;;      dl_ip_&name&_gbroad         the global network broadcast address
  42. ;;      dl_ip_&name&_flags          A word exclusively for IP use
  43. ;;      dl_ip_&name&_mtu            The maximum transmition unit
  44. ;;      dl_ip_&name&_metric         The interface metric
  45. ;;      dl_ip_&name&_id             The route id
  46. ;;      dl_ip_&name&_haddr          The hardware address
  47. ;;
  48. ;;*****************************************************************************
  49.  
  50.  
  51. ;;*****************************************************************************
  52. ;; data storage needed by this module
  53.  
  54. arp_data STRUC
  55.     arp_broadcast        DW 3 dup (0)
  56.     arp_write_off        DW 0               ;; offset and seg for 
  57.     arp_write_seg        DW 0               ;; the write buffer
  58. arp_data ENDS
  59.  
  60.  
  61. ;;*****************************************************************************
  62. ;;   ARP_DECLARE name, ether
  63. ;;       creates a new data link  object.  'name' is the name of this new
  64. ;;       object.  'ether' is the name of the ethernet to that will provide
  65. ;;       the low level services this module will need.  
  66. ;;   
  67. ARP_DECLARE MACRO name, ether
  68.     .errb <name>
  69.     .errb <ether>
  70.  
  71.     .DATA
  72.     arp_&name&_declared =  1
  73.     arp_&name&_ether =  ether
  74.     arp_&name&_arptab =  (100*name+1)
  75.     dl_ip_&name&_mtu = 1500
  76.     dl_ip_&name&_haddr =  eth_ðer&_address
  77.  
  78.     global dl_ip_&name&_ip:dword
  79.     global dl_ip_&name&_mask:dword
  80.     global dl_ip_&name&_net:dword
  81.     global dl_ip_&name&_broad:dword
  82.     global dl_ip_&name&_gbroad:dword
  83.     global dl_ip_&name&_flags:word
  84.     global dl_ip_&name&_metric:word
  85.     global dl_ip_&name&_id:word
  86.     global arp_&name&_data:arp_data 
  87.     global arp_&name&_real_dl_ip_w_write:near
  88.     .CODE
  89.  
  90.     ARP_TAB_DECLARE %arp_&name&_arptab
  91. ENDM
  92.  
  93.  
  94. ;;*****************************************************************************
  95. ;;   ARP_DEFINE name, ip_address, ip_net, ip_mask
  96. ;;      ARP_DEFINE declare all the things that have to be defined in
  97. ;;      every independantly assembled module.  DL_IP declares those
  98. ;;      things that need be be done only once (in particular memory allocation
  99. ;;      and initialzation code).  Every module including the one ARP_DEFINE
  100. ;;      is in) must have a DL_IP_DELCARE.   'ip_address' holds the IP address
  101. ;;      for this interface, 'ip_net' is the network and 'ip_mask' is the
  102. ;;      matching subnet mask.
  103. ;;
  104. ARP_DEFINE MACRO name, ip_address, ip_net, ip_mask
  105.     local around, arp_read_packet, zero_broad, zero_broad1
  106.     local class_A, class_B, class_C
  107.     .errb <ip_mask>
  108.  
  109. ifdef arp_&name&_declared 
  110.     .DATA
  111.     dl_ip_&name&_ip    DD ?
  112.     dl_ip_&name&_mask  DD ?
  113.     dl_ip_&name&_net   DD ?
  114.     dl_ip_&name&_broad DD ?
  115.     dl_ip_&name&_gbroad DD ?
  116.     dl_ip_&name&_flags DW ?
  117.     dl_ip_&name&_metric DW ?
  118.     dl_ip_&name&_id    DW ?
  119.  
  120.     arp_&name&_data  arp_data <>             ;; create storage needed
  121.  
  122.     .CODE
  123.     jmp around
  124.         arp_read_packet:
  125.             ARP_PROCESS_PACKET_in_AX_BX_ES name
  126.             ETH_R_RETURN %arp_&name&_ether
  127.             ;; this does NOT fall through
  128.  
  129.         arp_&name&_real_dl_ip_w_write:
  130.             ARP_REAL_DL_IP_W_WRITE_in_AX_BX_CX_const_BP name
  131.             RET
  132.     around:
  133.  
  134.     mov AX, word ptr ip_address                 ;; copy over params
  135.     mov word ptr dl_ip_&name&_ip, AX
  136.     mov AX, word ptr ip_address+2
  137.     mov word ptr dl_ip_&name&_ip+2, AX
  138.  
  139.     mov AX, word ptr ip_net
  140.     mov BX, word ptr ip_mask
  141.     mov word ptr dl_ip_&name&_net, AX
  142.     mov word ptr dl_ip_&name&_mask, BX
  143.  
  144.     test word ptr dl_ip_&name&_flags, ZERO_BROADCAST
  145.     jnz zero_broad
  146.         not BX
  147.         or AX, BX
  148.     zero_broad:
  149.     mov word ptr dl_ip_&name&_broad, AX
  150.  
  151.     mov AX, word ptr ip_net+2
  152.     mov BX, word ptr ip_mask+2
  153.     mov word ptr dl_ip_&name&_net+2, AX
  154.     mov word ptr dl_ip_&name&_mask+2, BX
  155.  
  156.     mov DL, AL
  157.     xor DH, DH
  158.     test word ptr dl_ip_&name&_flags, ZERO_BROADCAST
  159.     jnz zero_broad1
  160.         not BX
  161.         or AX, BX
  162.         mov DH, 0ffh
  163.     zero_broad1:
  164.     mov word ptr dl_ip_&name&_broad+2, AX
  165.  
  166.     mov AX, word ptr ip_net
  167.     test AL, 80h
  168.     jz class_A
  169.     test AL, 40h
  170.     jz class_B
  171.     jmp class_C
  172.     class_A:
  173.         mov AH, DH
  174.     class_B:
  175.         mov DL, DH
  176.     class_C:
  177.     mov word ptr dl_ip_&name&_gbroad, AX
  178.     mov word ptr dl_ip_&name&_gbroad+2, DX
  179.  
  180.     ARP_TAB_DEFINE %arp_&name&_arptab
  181.  
  182.     mov AX, DS                          ;; initialize the broadcast addr
  183.     mov ES, AX
  184.     mov DI, offset arp_&name&_data.arp_broadcast
  185.     mov AX, 0FFFFh
  186.     stosw
  187.     stosw
  188.     stosw
  189.  
  190.     mov AX, word ptr dl_ip_&name&_broad
  191.     mov DX, word ptr dl_ip_&name&_broad+2
  192.     mov BX, offset arp_&name&_data.arp_broadcast
  193.     ARP_TAB_ADD_FIXED_in_AX_DX_BX_ES_out_SI_const_BX_DX_BP_DI_ES %arp_&name&_arptab
  194.     mov AX, word ptr dl_ip_&name&_gbroad
  195.     mov DX, word ptr dl_ip_&name&_gbroad+2
  196.     ARP_TAB_ADD_FIXED_in_AX_DX_BX_ES_out_SI_const_BX_DX_BP_DI_ES %arp_&name&_arptab
  197.     ;; start up the arp task
  198.     ETH_R_READ %arp_&name&_ether, SWAPPED_ARP_TYPE, arp_read_packet
  199. endif
  200. ENDM
  201.  
  202.  
  203. ;;******************************************************************************
  204. ;;   DL_IP_R_READ name, code_label
  205. ;;       DL_IP_R_READ declares that the code starting at 'code_label'
  206. ;;       should be called whenever a IP packet is read.  BX:ES is initilized 
  207. ;;       to the begining of the IP packet before 'macro_code' is called
  208. ;;       The code at 'code_label' should call ARP_DL_IP_R_RETURN when it
  209. ;;       is done processing the packet.
  210. ;;       This procedure can only be called once per 'name'
  211. ;;
  212. ARP_DL_IP_R_READ MACRO name, code_label
  213.     .errb <code_label>
  214.  
  215.     ETH_R_READ %arp_&name&_ether, SWAPPED_IP_TYPE, code_label
  216. ENDM
  217.  
  218. ;;******************************************************************************
  219. ;;   DL_IP_R_CONT_in_BX_CX_ES name, ok
  220. ;;       DL_IP_R_CONT determines if the packet returned by R_READ in BX:ES
  221. ;;       of length CX is continuous.  If it is it jumps to 'ok' otherwise
  222. ;;       it just returns
  223. ;;
  224. ARP_DL_IP_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES MACRO name, ok
  225.     .errb <ok>
  226.  
  227.     ETH_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES %arp_&name&_ether, ok
  228. ENDM
  229.  
  230. ;;******************************************************************************
  231. ;;   DL_IP_R_RETURN name
  232. ;;       DL_IP_R_RETURN should be executed by the READ routine to signal
  233. ;;       that it is done processing the packet.
  234. ;;
  235. ARP_DL_IP_R_RETURN MACRO name
  236.     .errb <name>
  237.  
  238.     ETH_R_RETURN %arp_&name&_ether
  239. ENDM
  240.  
  241.  
  242. ;;******************************************************************************
  243. ;;   DL_IP_W_ACCESS_in_CX_out_DI_ES name, fail
  244. ;;       DL_IP_W_ACCESS returns a pointer to an output buffer for a IP 
  245. ;;       packet.  The pointer is returned in DI:ES.  This routine may
  246. ;;       busy wait for a time, and after a reasonable attempt a buffer
  247. ;;       could not be had, it will jump to 'fail' 
  248. ;;
  249. ARP_DL_IP_W_ACCESS_in_CX_out_DI_ES_const_CX_BP MACRO name, fail
  250.     .errb <name>
  251.     .errb <fail>
  252.     
  253.     ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP %arp_&name&_ether, fail
  254.     mov arp_&name&_data.arp_write_off, DI
  255.     mov arp_&name&_data.arp_write_seg, ES
  256. ENDM
  257.  
  258.  
  259. ;;******************************************************************************
  260. ;;   DL_IP_W_WRITE_in_AX_BX_CX name, broadcast
  261. ;;       DL_IP_W_WRITE actually signals the link layer to write a packet to the 
  262. ;;       network.  The packet is assumed to be in the buffer returned by 
  263. ;;       DL_IP_W_ACCESS.  CX is the length of the packet to send.
  264. ;;       AX_BX holds the IP address to send the packet to.
  265. ;;       if 'broadcast' is not blank, then the packet is written to the
  266. ;;       broadcast address.  AX:BX is ignored in this case
  267. ;;
  268. ARP_DL_IP_W_WRITE_in_AX_BX_CX_const_BP MACRO name, broadcast
  269.     ifb <broadcast>
  270.         call arp_&name&_real_dl_ip_w_write
  271.     else
  272.         ARP_REAL_DL_IP_W_WRITE_in_AX_BX_CX_const_BP name, broadcast
  273.     endif
  274. ENDM
  275.  
  276. ARP_REAL_DL_IP_W_WRITE_in_AX_BX_CX_const_BP MACRO name, broadcast
  277.     local send_packet
  278.     .errb <name>
  279.  
  280.     push BP
  281.     les DI, dword ptr arp_&name&_data.arp_write_off    
  282.     mov BP, SWAPPED_IP_TYPE
  283.     ifb <broadcast>
  284.         ARP_TAB_GET_in_AX_BX_out_SI_const_AX_BX_CX_BP_DI_ES %arp_&name&_arptab
  285.         jz send_packet
  286.             ARP_BUILD_REQUEST_in_AX_BX_DI_ES_const_AX_BX_DX_BP_ES name
  287.             mov CX, size arp
  288.             mov DI, word ptr arp_&name&_data.arp_write_off    
  289.             mov SI, offset arp_&name&_data.arp_broadcast
  290.             mov BP, SWAPPED_ARP_TYPE
  291.         send_packet:
  292.     else
  293.         mov SI, offset arp_&name&_data.arp_broadcast
  294.     endif
  295.     mov AX, BP
  296.     pop BP
  297.     ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES %arp_&name&_ether
  298. ENDM
  299.  
  300.  
  301. ;;******************************************************************************
  302. ;;   DL_IP_IS_BROADCAST_in_BX_ES name
  303. ;;      DL_IP_IS_BROADCAST_in_BX_ES determines if the packet pointed to
  304. ;;      by BX:ES is a broadcast and sets the zero flag if it is NOT a 
  305. ;;      broadcast
  306. ;;
  307. ARP_DL_IP_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES MACRO name
  308.     .errb <name>
  309.     ETH_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES %arp_&name&_ether
  310. ENDM
  311.  
  312.  
  313. ;;******************************************************************************
  314. ;;   DL_IP_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  315. ;;      DL_IP_COPY_in_CX_SI_DI_ES copys a packet from the input buffer (pointed 
  316. ;;      to by SI and the segement register given in IF_DECLARE) to an output 
  317. ;;      buffer (pointed to by DI and dest_reg) of length CX.   It assumes the
  318. ;;      output buffer is contiguous.  (and the caller shouldn't care if the 
  319. ;;      input buffer is contiguous)  COPY updates the pointers SI and DI
  320. ;;      to the end of the packet, and COPY could be called again if CX is not
  321. ;;      the total packet length (Note that CX MUST be even if you care about
  322. ;;      SI, and DI being updated properly)
  323. ;;
  324. ;;
  325. ARP_DL_IP_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES MACRO name
  326.     .errb <name>
  327.     ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES %arp_&name&_ether
  328. ENDM
  329.  
  330.  
  331. ;;******************************************************************************
  332. ;; ARP_PROCESS_PACKET_in_AX_BX_ES MACRO name
  333. ;;      ARP_PROCESS_PACKET is called when an ARP packet is received.  AX holds
  334. ;;      the ethernet type (SWAPPED_ARP_TYPE) and BX:ES points to the begining
  335. ;;      of the ARP packet
  336. ;;
  337. ARP_PROCESS_PACKET_in_AX_BX_ES MACRO name
  338.     local notarp, not_directly, forme, notme, pa_forme
  339.     local save_seg
  340.     .errb <name>
  341.  
  342.     .DATA
  343.     save_seg DW ?
  344.  
  345.     .CODE
  346.     mov AX, word ptr ES:[BX+arp_proto] ;; is it my IP address
  347.     cmp word ptr AX, SWAPPED_IP_TYPE
  348.     jne notarp
  349.     mov AX, word ptr ES:[BX+arp_spa]
  350.         mov DX, word ptr ES:[BX+arp_spa+2]
  351.         add BX, arp_sha
  352.         ARP_TAB_ADD_in_AX_DX_BX_ES_out_SI_const_BX_DX_BP_DI_ES %arp_&name&_arptab
  353.         sub BX, arp_sha
  354.         cmp byte ptr ES:[BX+arp_op+1], ARP_REQUEST
  355.         jnz notme
  356.         mov AX, word ptr ES:[BX+arp_tpa]
  357.         mov DX, word ptr ES:[BX+arp_tpa+2]
  358.         cmp AX, word ptr dl_ip_&name&_ip    ;; is it my IP address ?
  359.         jne  not_directly
  360.         cmp DX, word ptr dl_ip_&name&_ip+2
  361.         je forme
  362.  
  363.         not_directly:
  364.         ;; should I PROXY?
  365.         test word ptr dl_ip_&name&_flags, ARP_PROXY_ARP
  366.         jz notme
  367.     mov BP, BX            ;; Save BX
  368.     mov CX, SI            ;; Save SI
  369.         xor SI, SI
  370.         mov DI, SI
  371.     mov BX, DX
  372.     ROUTE_FIND_in_AX_BX_SI_DI_out_AX_BX_DX_const_CX_BP_ES %myip,pa_forme,notme
  373.     cmp DL, name            ;; Is the destination on same
  374.     je notme            ;; interface? then FORGET IT!
  375.         pa_forme:
  376.             mov BX, BP
  377.             mov SI, CX
  378.         forme:
  379.             mov BP, SI                      ;; save SI
  380.             mov SI, BX
  381.             ARP_BUILD_REPLY_in_SI_ES_const_DX_BP_SI_ES name
  382.  
  383.             mov CX, size arp
  384.             mov BX, SI                      ;; save SI
  385.             mov word ptr save_seg, ES       ;; save ES
  386.             ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP %arp_&name&_ether, notme
  387.             mov SI, BX                      ;; restore SI
  388.  
  389.             mov BX, DI                      ;; save DI
  390.             mov CX, size arp
  391.             push DS
  392.             mov DS, word ptr save_seg
  393.             ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES %arp_&name&_ether
  394.             pop DS
  395.  
  396.             mov CX, size arp
  397.             mov DI, BX                      ;; restore DI
  398.             mov SI, BP                      ;; restore SI
  399.             mov AX, SWAPPED_ARP_TYPE
  400.             ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES %arp_&name&_ether
  401.         notme:
  402.    notarp:
  403. ENDM
  404.  
  405.  
  406. ;;******************************************************************************
  407. ;; arp related stuff.  these are for internal use only
  408.  
  409. ARP_REQUEST = 1
  410. ARP_REPLY   = 2
  411. SWAPPED_ARP_REQUEST = 100h
  412. SWAPPED_ARP_REPLY   = 200h
  413.  
  414. ARP_ETHER_TYPE  = 1
  415. SWAPPED_ARP_ETHER_TYPE = 100h
  416.  
  417. arp STRUC
  418.     arp_hdr DW 1            ;; header type 1 = ETHERNET
  419.     arp_proto DW 0
  420.     arp_hln DB 6            ;; length of ethernet address
  421.     arp_pln DB 4            ;; length of IP address
  422.     arp_op  DW ARP_REPLY    ;; operation
  423.     arp_sha DB 6 DUP (0)    ;; source hardware address
  424.     arp_spa DB 4 DUP (0)    ;; source protocol address
  425.     arp_tha DB 6 DUP (0)    ;; target hardware address
  426.     arp_tpa DB 4 DUP (0)    ;; target protocol address
  427. arp ENDS
  428.  
  429.  
  430. ;;******************************************************************************
  431. ;; ARP_BUILD_REQUEST_in_AX_BX_DI_ES builds an arp request in the buffer
  432. ;; pointed to by DI:ES  for the ip address that ends in AX
  433.  
  434. ARP_BUILD_REQUEST_in_AX_BX_DI_ES_const_AX_BX_DX_BP_ES MACRO name
  435.     .errb <name>
  436.  
  437.     mov CX, AX      ;; save AX
  438.     mov AX, SWAPPED_ARP_ETHER_TYPE
  439.     stosw
  440.     mov AX, SWAPPED_IP_TYPE
  441.     stosw
  442.     mov AX, 0406h   ;; byte swapped hardware address len, ip address len
  443.     stosw
  444.     mov AX, SWAPPED_ARP_REQUEST 
  445.     stosw
  446.     mov SI, offset dl_ip_&name&_haddr   ;; copy me as source hardware address
  447.     movsw
  448.     movsw
  449.     movsw
  450.     mov SI, OFFSET dl_ip_&name&_ip    ;; and protocol address
  451.     movsw
  452.     movsw
  453.     mov AX, 0
  454.     stosw                             ;; target harware address
  455.     stosw
  456.     stosw
  457.     mov AX, CX
  458.     stosw                ;; target ip address
  459.     xchg AX, BX
  460.     stosw
  461.     xchg AX, BX
  462. ENDM
  463.  
  464.  
  465. ;;******************************************************************************
  466. ;; ARP_BUILD_REPLY_in_SI_ES converts the ARP request in SI:ES to a reply
  467. ;;   that has indicates that this node is the target of the request.  Note
  468. ;;   that this routine just copies the target IP address from the request,
  469. ;;   so that proxy arp is easy.
  470.  
  471. ARP_BUILD_REPLY_in_SI_ES_const_DX_BP_SI_ES MACRO name
  472.     .errb <name>
  473.     
  474.     mov CX, SI                                  ;; save the pointer 
  475.  
  476.     mov word ptr ES:[SI+arp_op], SWAPPED_ARP_REPLY
  477.     mov AX, word ptr ES:[SI+arp_tpa]
  478.     mov BX, word ptr ES:[SI+arp_tpa+2]
  479.     add SI, offset arp_sha
  480.     mov DI, CX
  481.     add DI, offset arp_tha
  482.     seges                       ;; copy source hardware and proto addr
  483.     movsw
  484.     seges
  485.     movsw
  486.     seges
  487.     movsw
  488.     seges
  489.     movsw
  490.     seges
  491.     movsw
  492.  
  493.     mov DI, CX
  494.     add DI, offset arp_sha
  495.     mov SI, offset dl_ip_&name&_haddr
  496.     movsw                       ;; put in me as source hardware addr
  497.     movsw
  498.     movsw
  499.  
  500.     stosw                       ;; put in TPA as source hardware addr
  501.     mov AX, BX
  502.     stosw
  503.     mov SI, CX                  ;; restore SI
  504. ENDM
  505.