home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / p / pcrte224.zip / SOURCE.ZIP / ARP.INC < prev    next >
Text File  |  1992-06-09  |  16KB  |  437 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_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_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 (IP addr bitwize AND ip_mask)
  40. ;;      dl_ip_&name&_broad          the network broadcast address
  41. ;;      dl_ip_&name&_flags          A word exclusively for IP use
  42. ;;      dl_ip_&name&_mtu            The maximum transmition unit
  43. ;;      dl_ip_&name&_metric         The interface metric
  44. ;;      dl_ip_&name&_haddr          The hardware address
  45. ;;
  46. ;;*****************************************************************************
  47.  
  48.  
  49. ;;*****************************************************************************
  50. ;; data storage needed by this module
  51.  
  52. arp_data STRUC
  53.     arp_broadcast        DW 3 dup (0)
  54.     arp_write_off        DW 0               ;; offset and seg for 
  55.     arp_write_seg        DW 0               ;; the write buffer
  56. arp_data ENDS
  57.  
  58.  
  59. ;;*****************************************************************************
  60. ;;   ARP_DECLARE name, ether
  61. ;;       creates a new data link  object.  'name' is the name of this new
  62. ;;       object.  'ether' is the name of the ethernet to that will provide
  63. ;;       the low level services this module will need.  
  64. ;;   
  65. ARP_DECLARE MACRO name, ether
  66.     .errb <name>
  67.     .errb <ether>
  68.  
  69.     .DATA
  70.     arp_&name&_declared =  1
  71.     arp_&name&_ether =  ether
  72.     arp_&name&_arptab =  (100*name+1)
  73.     dl_ip_&name&_mtu = 1500
  74.     dl_ip_&name&_haddr =  eth_ðer&_address
  75.  
  76.     global dl_ip_&name&_ip:dword
  77.     global dl_ip_&name&_mask:dword
  78.     global dl_ip_&name&_net:dword
  79.     global dl_ip_&name&_broad:dword
  80.     global dl_ip_&name&_flags:word
  81.     global dl_ip_&name&_metric:word
  82.     global arp_&name&_data:arp_data 
  83.     .CODE
  84.  
  85.     ARP_TAB_DECLARE %arp_&name&_arptab
  86. ENDM
  87.  
  88.  
  89. ;;*****************************************************************************
  90. ;;   ARP_DEFINE name, ip_address, ip_mask
  91. ;;      ARP_DEFINE declare all the things that have to be defined in
  92. ;;      every independantly assembled module.  DL_IP declares those
  93. ;;      things that need be be done only once (in particular memory allocation
  94. ;;      and initialzation code).  Every module including the one ARP_DEFINE
  95. ;;      is in) must have a DL_IP_DELCARE.   'ip_address' holds the address of 
  96. ;;      the IP address for this ethernet and 'ip_mask' is the subnet mask.
  97. ;;
  98. ARP_DEFINE MACRO name, ip_address, ip_mask
  99.     local around, arp_read_packet, zero_broad
  100.     .errb <ip_mask>
  101.  
  102. ifdef arp_&name&_declared 
  103.     .DATA
  104.     dl_ip_&name&_ip    DD ?
  105.     dl_ip_&name&_mask  DD ?
  106.     dl_ip_&name&_net   DD ?
  107.     dl_ip_&name&_broad DD ?
  108.     dl_ip_&name&_flags DW ?
  109.     dl_ip_&name&_metric DW ?
  110.  
  111.     arp_&name&_data  arp_data <>             ;; create storage needed
  112.  
  113.     .CODE
  114.     jmp around
  115.         arp_read_packet:
  116.             ARP_PROCESS_PACKET_in_AX_BX_ES name
  117.             ETH_R_RETURN %arp_&name&_ether
  118.             ;; this does NOT fall through
  119.     around:
  120.  
  121.     mov AX, word ptr ip_address                 ;; copy over params
  122.     mov BX, 0FFFFH                              ;; Force our assumtion 
  123.     mov word ptr dl_ip_&name&_ip, AX
  124.     mov word ptr dl_ip_&name&_net, AX
  125.     mov word ptr dl_ip_&name&_broad, AX
  126.     mov word ptr dl_ip_&name&_mask, BX
  127.  
  128.     mov AX, word ptr ip_address+2
  129.     mov BX, word ptr ip_mask+2
  130.     mov word ptr dl_ip_&name&_ip+2, AX
  131.     mov word ptr dl_ip_&name&_mask+2, BX
  132.     and AX, BX
  133.     mov word ptr dl_ip_&name&_net+2, AX
  134.  
  135.     test word ptr dl_ip_&name&_flags, ZERO_BROADCAST
  136.     jnz zero_broad
  137.         not BX
  138.         or AX, BX
  139.     zero_broad:
  140.     mov word ptr dl_ip_&name&_broad+2, AX
  141.  
  142.     ARP_TAB_DEFINE %arp_&name&_arptab
  143.  
  144.     mov AX, DS                          ;; initialize the broadcast addr
  145.     mov ES, AX
  146.     mov DI, offset arp_&name&_data.arp_broadcast
  147.     mov AX, 0FFFFh
  148.     stosw
  149.     stosw
  150.     stosw
  151.  
  152.     mov AX, word ptr dl_ip_&name&_broad+2
  153.     mov BX, offset arp_&name&_data.arp_broadcast
  154.     ARP_TAB_ADD_in_AX_BX_ES_out_SI_const_BX_DX_BP_DI_ES %arp_&name&_arptab
  155.  
  156.         ;; start up the arp task
  157.     ETH_R_READ %arp_&name&_ether, ARP_TYPE, arp_read_packet
  158. endif
  159. ENDM
  160.  
  161.  
  162. ;;******************************************************************************
  163. ;;   DL_IP_R_READ name, code_label
  164. ;;       DL_IP_R_READ declares that the code starting at 'code_label'
  165. ;;       should be called whenever a IP packet is read.  BX:ES is initilized 
  166. ;;       to the begining of the IP packet before 'macro_code' is called
  167. ;;       The code at 'code_label' should call ARP_DL_IP_R_RETURN when it
  168. ;;       is done processing the packet.
  169. ;;       This procedure can only be called once per 'name'
  170. ;;
  171. ARP_DL_IP_R_READ MACRO name, code_label
  172.     .errb <code_label>
  173.  
  174.     ETH_R_READ %arp_&name&_ether, IP_TYPE, code_label
  175. ENDM
  176.  
  177. ;;******************************************************************************
  178. ;;   DL_IP_R_CONT_in_BX_CX_ES name, ok
  179. ;;       DL_IP_R_CONT determines if the packet returned by R_READ in BX:ES
  180. ;;       of length CX is continuous.  If it is it jumps to 'ok' otherwise
  181. ;;       it just returns
  182. ;;
  183. ARP_DL_IP_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES MACRO name, ok
  184.     .errb <ok>
  185.  
  186.     ETH_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES %arp_&name&_ether, ok
  187. ENDM
  188.  
  189. ;;******************************************************************************
  190. ;;   DL_IP_R_RETURN name
  191. ;;       DL_IP_R_RETURN should be executed by the READ routine to signal
  192. ;;       that it is done processing the packet.
  193. ;;
  194. ARP_DL_IP_R_RETURN MACRO name
  195.     .errb <name>
  196.  
  197.     ETH_R_RETURN %arp_&name&_ether
  198. ENDM
  199.  
  200.  
  201. ;;******************************************************************************
  202. ;;   DL_IP_W_ACCESS_in_CX_out_DI_ES name, fail
  203. ;;       DL_IP_W_ACCESS returns a pointer to an output buffer for a IP 
  204. ;;       packet.  The pointer is returned in DI:ES.  This routine may
  205. ;;       busy wait for a time, and after a reasonable attempt a buffer
  206. ;;       could not be had, it will jump to 'fail' 
  207. ;;
  208. ARP_DL_IP_W_ACCESS_in_CX_out_DI_ES_const_CX_BP MACRO name, fail
  209.     .errb <name>
  210.     .errb <fail>
  211.     
  212.     ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP %arp_&name&_ether, fail
  213.     mov arp_&name&_data.arp_write_off, DI
  214.     mov arp_&name&_data.arp_write_seg, ES
  215. ENDM
  216.  
  217.  
  218. ;;******************************************************************************
  219. ;;   DL_IP_W_WRITE_in_AX_CX name, broadcast
  220. ;;       DL_IP_W_WRITE actually signals the link layer to write a packet to the 
  221. ;;       network.  The packet is assumed to be in the buffer returned by 
  222. ;;       DL_IP_W_ACCESS.  CX is the length of the packet to send.  AX holds the 
  223. ;;       last two bytes of the IP address to send the packet to.  (notice we 
  224. ;;       are assuming a host portion of less that 16 bits)
  225. ;;       if 'broadcast' is not blank, then the packet is written to the
  226. ;;       broadcast address.  AX is ignored in this case
  227. ;;
  228. ARP_DL_IP_W_WRITE_in_AX_CX_const_BP MACRO name, broadcast
  229.     local send_packet
  230.     .errb <name>
  231.     
  232.     les DI, dword ptr arp_&name&_data.arp_write_off    
  233.     mov BX, SWAPPED_IP_TYPE
  234.     ifb <broadcast>
  235.         ARP_TAB_GET_in_AX_out_SI_const_AX_BX_CX_BP_DI_ES %arp_&name&_arptab
  236.         jz send_packet
  237.             ARP_BUILD_REQUEST_in_AX_DI_ES_const_CX_DX_BP_ES name
  238.             mov CX, size arp
  239.             mov DI, word ptr arp_&name&_data.arp_write_off    
  240.             mov SI, offset arp_&name&_data.arp_broadcast
  241.             mov BX, SWAPPED_ARP_TYPE
  242.         send_packet:
  243.     else
  244.         mov SI, offset arp_&name&_data.arp_broadcast
  245.     endif
  246.     mov AX, BX
  247.     ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES %arp_&name&_ether
  248. ENDM
  249.  
  250.  
  251. ;;******************************************************************************
  252. ;;   DL_IP_IS_BROADCAST_in_BX_ES name
  253. ;;      DL_IP_IS_BROADCAST_in_BX_ES determines if the packet pointed to
  254. ;;      by BX:ES is a broadcast and sets the zero flag if it is NOT a 
  255. ;;      broadcast
  256. ;;
  257. ARP_DL_IP_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES MACRO name
  258.     .errb <name>
  259.     ETH_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES %arp_&name&_ether
  260. ENDM
  261.  
  262.  
  263. ;;******************************************************************************
  264. ;;   DL_IP_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  265. ;;      DL_IP_COPY_in_CX_SI_DI_ES copys a packet from the input buffer (pointed 
  266. ;;      to by SI and the segement register given in IF_DECLARE) to an output 
  267. ;;      buffer (pointed to by DI and dest_reg) of length CX.   It assumes the
  268. ;;      output buffer is contiguous.  (and the caller shouldn't care if the 
  269. ;;      input buffer is contiguous)  COPY updates the pointers SI and DI
  270. ;;      to the end of the packet, and COPY could be called again if CX is not
  271. ;;      the total packet length (Note that CX MUST be even if you care about
  272. ;;      SI, and DI being updated properly)
  273. ;;
  274. ;;
  275. ARP_DL_IP_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES MACRO name
  276.     .errb <name>
  277.     ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES %arp_&name&_ether
  278. ENDM
  279.  
  280.  
  281. ;;******************************************************************************
  282. ;; ARP_PROCESS_PACKET_in_AX_BX_ES MACRO name
  283. ;;      ARP_PROCESS_PACKET is called when an ARP packet is received.  AX holds
  284. ;;      the ethernet type (SWAPPED_ARP_TYPE) and BX:ES points to the begining
  285. ;;      of the ARP packet
  286. ;;
  287. ARP_PROCESS_PACKET_in_AX_BX_ES MACRO name
  288.     local notarp, forme, notme
  289.     .errb <name>
  290.  
  291.     cmp word ptr AX, SWAPPED_ARP_TYPE  ;; is it an arp packet   
  292.     jne notarp
  293.     mov AX, word ptr ES:[BX+arp_proto] ;; is it my IP address
  294.     cmp word ptr AX, SWAPPED_IP_TYPE   ;; is it an arp packet   
  295.     jne notarp
  296.         mov AX, word ptr ES:[BX+arp_spa+2]
  297.         add BX, arp_sha
  298.         ARP_TAB_ADD_in_AX_BX_ES_out_SI_const_BX_DX_BP_DI_ES %arp_&name&_arptab
  299.         sub BX, arp_sha
  300.  
  301.         cmp byte ptr ES:[BX+arp_op+1], ARP_REQUEST
  302.         jnz notme
  303.         mov AX, word ptr ES:[BX+arp_tpa+2]   ;; is it my IP address
  304.         cmp AX, word ptr dl_ip_&name&_ip+2
  305.         jz  forme 
  306.  
  307.                 ;; should I PROXY
  308.         test word ptr dl_ip_&name&_flags, ARP_PROXY_ARP
  309.         jz notme
  310.         and AX, word ptr dl_ip_&name&_mask+2
  311.         cmp AX, word ptr dl_ip_&name&_net+2
  312.         jz notme
  313.  
  314.         forme:
  315.             mov BP, SI                      ;; save SI
  316.             mov SI, BX
  317.             ARP_BUILD_REPLY_in_SI_ES_const_DX_BP_SI_ES name
  318.  
  319.             mov CX, size arp
  320.             mov BX, SI                      ;; save SI
  321.             ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP %arp_&name&_ether, notme
  322.             mov SI, BX                      ;; restore SI
  323.  
  324.             mov BX, DI                      ;; save DI
  325.             mov CX, size arp
  326.             ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES %arp_&name&_ether
  327.  
  328.             mov CX, size arp
  329.             mov DI, BX                      ;; restore DI
  330.             mov SI, BP                      ;; restore SI
  331.             mov AX, SWAPPED_ARP_TYPE
  332.             ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES %arp_&name&_ether
  333.         notme:
  334.    notarp:
  335. ENDM
  336.  
  337.  
  338. ;;******************************************************************************
  339. ;; arp related stuff.  these are for internal use only
  340.  
  341. ARP_REQUEST = 1
  342. ARP_REPLY   = 2
  343. SWAPPED_ARP_REQUEST = 100h
  344. SWAPPED_ARP_REPLY   = 200h
  345.  
  346. ARP_ETHER_TYPE  = 1
  347. SWAPPED_ARP_ETHER_TYPE = 100h
  348.  
  349. arp STRUC
  350.     arp_hdr DW 1            ;; header type 1 = ETHERNET
  351.     arp_proto DW 0
  352.     arp_hln DB 6            ;; length of ethernet address
  353.     arp_pln DB 4            ;; length of IP address
  354.     arp_op  DW ARP_REPLY    ;; operation
  355.     arp_sha DB 6 DUP (0)    ;; source hardware address
  356.     arp_spa DB 4 DUP (0)    ;; source protocol address
  357.     arp_tha DB 6 DUP (0)    ;; target hardware address
  358.     arp_tpa DB 4 DUP (0)    ;; target protocol address
  359. arp ENDS
  360.  
  361.  
  362. ;;******************************************************************************
  363. ;; ARP_BUILD_REQUEST_in_AX_DI_ES builds an arp request in the buffer
  364. ;; pointed to by DI:ES  for the ip address that ends in AX
  365.  
  366. ARP_BUILD_REQUEST_in_AX_DI_ES_const_CX_DX_BP_ES MACRO name
  367.     .errb <name>
  368.  
  369.     mov BX, AX      ;; save AX
  370.     mov AX, SWAPPED_ARP_ETHER_TYPE
  371.     stosw
  372.     mov AX, SWAPPED_IP_TYPE
  373.     stosw
  374.     mov AX, 0406h   ;; byte swapped hardware address len, ip address len
  375.     stosw
  376.     mov AX, SWAPPED_ARP_REQUEST 
  377.     stosw
  378.     mov SI, offset dl_ip_&name&_haddr   ;; copy me as source hardware address
  379.     movsw
  380.     movsw
  381.     movsw
  382.     mov SI, OFFSET dl_ip_&name&_ip    ;; and protocol address
  383.     movsw
  384.     movsw
  385.     mov AX, 0
  386.     stosw                             ;; target harware address
  387.     stosw
  388.     stosw
  389.     mov SI, OFFSET dl_ip_&name&_ip    ;; target ip address
  390.     movsw
  391.     mov AX, BX
  392.     stosw
  393. ENDM
  394.  
  395.  
  396. ;;******************************************************************************
  397. ;; ARP_BUILD_REPLY_in_SI_ES converts the ARP request in SI:ES to a reply
  398. ;;   that has indicates that this node is the target of the request.  Note
  399. ;;   that this routine just copies the target IP address from the request,
  400. ;;   so that proxy arp is easy.
  401.  
  402. ARP_BUILD_REPLY_in_SI_ES_const_DX_BP_SI_ES MACRO name
  403.     .errb <name>
  404.     
  405.     mov CX, SI                                  ;; save the pointer 
  406.  
  407.     mov word ptr ES:[SI+arp_op], SWAPPED_ARP_REPLY
  408.     mov AX, word ptr ES:[SI+arp_tpa]
  409.     mov BX, word ptr ES:[SI+arp_tpa+2]
  410.     add SI, offset arp_sha
  411.     mov DI, CX
  412.     add DI, offset arp_tha
  413.     seges                       ;; copy source hardware and proto addr
  414.     movsw
  415.     seges
  416.     movsw
  417.     seges
  418.     movsw
  419.     seges
  420.     movsw
  421.     seges
  422.     movsw
  423.  
  424.     mov DI, CX
  425.     add DI, offset arp_sha
  426.     mov SI, offset dl_ip_&name&_haddr
  427.     movsw                       ;; put in me as source hardware addr
  428.     movsw
  429.     movsw
  430.  
  431.     stosw                       ;; put in TPA as source hardware addr
  432.     mov AX, BX
  433.     stosw
  434.     mov SI, CX                  ;; restore SI
  435. ENDM
  436.  
  437.