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

  1. ;;************************************************************************* 
  2. ;;                         bootp.inc       bootp.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. ;; AUTHOR: Vance Morrison
  19. ;;****************************************************************************
  20. ;; definition of Bootp structure
  21.  
  22. BOOTPC = 44h                          ;; udp port for server responces 
  23. BOOTPS = 43h                          ;; udp port for client requests
  24.  
  25. ;;****************************************************************************
  26. ;; definition of BOOTP structure
  27.  
  28. BOOTP_REQUEST = 1
  29. BOOTP_REPLY = 2
  30.  
  31. MYID = 3845                                     ;; a unique ID
  32.  
  33. bootp           STRUC
  34.     bootp_op            DB 0                ;; operation
  35.     bootp_htype         DB 0                ;; hardware type (ethernet = 1)
  36.     bootp_hlen          DB 0                ;; hardware address len
  37.     bootp_hops          DB 0                ;; hop count (start at 0)
  38.     bootp_xid           DD 0                ;; random transaction id
  39.     bootp_secs          DW 0
  40.     bootp_unused        DW 0
  41.     bootp_ciaddr        DD 0                ;; client IP address
  42.     bootp_yiaddr        DD 0                ;; client IP address (servers reply)
  43.     bootp_siaddr        DD 0                ;; server IP address
  44.     bootp_giaddr        DD 0                ;; gateway IP address
  45.     bootp_chaddr        DB 16 DUP (0)       ;; client harware address
  46.     bootp_sname         DB 64 DUP (0)       ;; server name 
  47.     bootp_file          DB 128 DUP (0)      ;; file to boot from 
  48.     bootp_magic         DD 0                ;; magic number 
  49.     bootp_vendor        DB 60 DUP (0)       ;; vendor specific stuff
  50. bootp           ENDS
  51.  
  52.  
  53. ;;******************************************************************************
  54. ;;  BOOTP_DECLARE name, udp, dls, udp_sock1, udp_sock2
  55. ;;       BOOTP_DECLARE delcares a BOOTP listener called 'name'.  'udp'
  56. ;;       is a udp object, 'udp_sock1', 'udp_sock2' are two unique numbers
  57. ;;       that can but used to create sockets with the udp object.  The
  58. ;;       Data link objects are assumed to be 1..<dls>.
  59. ;;
  60. BOOTP_DECLARE   MACRO  name, udp, dls
  61.     .errb <dls>
  62.  
  63.     .DATA
  64.     bootp_&name&_udp = udp
  65.     bootp_&name&_dls = dls
  66.     bootp_&name&_my_ip = dl_ip_1_ip         ;; make equal to any of my addresses
  67.     bootp_&name&_request_sock = (name*100+1)
  68.     bootp_&name&_reply_sock = (name*100+2)
  69.     global bootp_&name&_forward:dword
  70.     .CODE
  71.  
  72.     UDP_SOCK_DECLARE %bootp_&name&_request_sock, %bootp_&name&_udp, BOOTPS
  73.     UDP_SOCK_DECLARE %bootp_&name&_reply_sock, %bootp_&name&_udp, BOOTPC
  74. ENDM
  75.  
  76.  
  77. ;;******************************************************************************
  78. ;;   BOOTP_DEFINE   my_ip, forward_host
  79. ;;       BOOTP_DEFINE sets aside the memory and acutally does the 
  80. ;;       initialization for the bootp object 'name'.  'forward_host'
  81. ;;       is the address of a IP address to forward any bootp request to.  
  82. ;;       This address can be a directed broadcast.  
  83. ;;
  84. BOOTP_DEFINE   MACRO   name, forward_host
  85.     local around, request_code, reply_code, drop, tftp_around, tftp_request
  86.     local do_init
  87.     .errb <forward_host>
  88.  
  89.     .DATA
  90.     bootp_&name&_forward DD ?
  91.     bootp_&name&_last_xid DD ?
  92.  
  93.     .CODE
  94.     jmp around          ;; declare code objects
  95.         request_code:
  96.         reply_code:
  97.             BOOTP_REQUEST_PACKET_in_AX_BX_CX_ES name, drop
  98.             BOOTP_REPLY_PACKET_in_AX_BX_CX_ES name, drop
  99.             drop:
  100.             RET
  101.     around:
  102.  
  103.     ;; this code should be thrown out when CISCO do bootp properly
  104.     SWAPPED_TFTP_PORT = 4500h
  105.     jmp tftp_around
  106.         tftp_request:
  107.         TFTP_REQUEST_PACKET_in_AX_BX_CX_ES name, %myip
  108.         RET
  109.     tftp_around:
  110.     UDP_R_READ %bootp_&name&_udp, SWAPPED_TFTP_PORT, tftp_request
  111.     ;; end of KLUDGE code (dont forget to trough out TFTP_REQUEST_PACKET too)
  112.  
  113.     xor AX, AX
  114.     mov word ptr bootp_&name&_last_xid, AX
  115.     mov word ptr bootp_&name&_last_xid+2, AX
  116.  
  117.     mov AX, word ptr forward_host                  ;; save the forwarding host
  118.     mov BX, word ptr forward_host+2
  119.     mov word ptr bootp_&name&_forward, AX
  120.     mov word ptr bootp_&name&_forward+2, BX
  121.  
  122.     or AX, AX
  123.     jnz do_init
  124.     or BX, BX
  125.     jz done
  126.  
  127.         do_init:
  128.         UDP_SOCK_DEFINE %bootp_&name&_request_sock, request_code
  129.         UDP_SOCK_DEFINE %bootp_&name&_reply_sock, reply_code
  130.     done:
  131. ENDM
  132.  
  133.  
  134. ;;******************************************************************************
  135. ;;   BOOTP_REQUEST_PACKET   name
  136. ;;
  137. BOOTP_REQUEST_PACKET_in_AX_BX_CX_ES MACRO name, drop
  138.     local done, skip
  139.     .errb <drop>
  140.  
  141.         ;; since bootp requests are often sent to a broadcast address,
  142.         ;; I am a bit paranoid about the packets I receive.  Basicly
  143.         ;; I ONLY forward requests from that poor sap that doesn't know
  144.         ;; his IP address.  Anyone else I assume can boot without my help 
  145.     mov SI, BX
  146.     cmp byte ptr ES:[SI+bootp_op], BOOTP_REQUEST
  147.     jnz done
  148.         mov AL, ES:[SI+bootp_hops]
  149.         cmp AL, 2
  150.         ja drop
  151.         inc AL
  152.         mov ES:[SI+bootp_hops], AL
  153.  
  154.         mov AX, word ptr ES:[SI+bootp_giaddr]       ;; is the gateway filled in
  155.         or AX, AX
  156.         jnz drop
  157.         mov AX, word ptr ES:[SI+bootp_giaddr+2]     ;; is the gateway filled in
  158.         or AX, AX
  159.         jnz drop
  160.  
  161.         xor AX, AX                                  ;; reset 
  162.         mov word ptr bootp_&name&_last_xid, AX
  163.         mov word ptr bootp_&name&_last_xid+2, AX
  164.  
  165.         mov AX, word ptr bootp_&name&_my_ip         ;; fill in me as gateway
  166.         mov word ptr ES:[SI+bootp_giaddr], AX
  167.         mov AX, word ptr bootp_&name&_my_ip+2
  168.         mov word ptr ES:[SI+bootp_giaddr+2], AX
  169.  
  170.         mov AX, word ptr bootp_&name&_forward
  171.         mov BX, word ptr bootp_&name&_forward+2
  172.         or AX, AX
  173.         jnz skip
  174.         or BX, BX
  175.         jz drop
  176.         skip:
  177.         mov CX, size bootp
  178.         mov DX, BOOTPS
  179.  
  180.         push SI
  181.         push ES
  182.         UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES %bootp_&name&_reply_sock
  183.         pop  DX
  184.         pop  SI
  185.         or AX, AX
  186.         jnz drop
  187.  
  188.         mov BX, DS              ;; save DS
  189.         mov DS, DX
  190.         mov CX, size bootp
  191.         rep                     ;; copy the packet
  192.         movsb
  193.         mov DS, BX              ;; restore DS
  194.  
  195.         mov CX, size bootp
  196.         UDP_SOCK_W_WRITE_in_CX %bootp_&name&_reply_sock
  197.     done:
  198. ENDM
  199.  
  200.  
  201. ;;******************************************************************************
  202. ;;   BOOTP_REPLY_PACKET   name
  203. ;;
  204. BOOTP_REPLY_PACKET_in_AX_BX_CX_ES MACRO name, drop
  205.     local done, sendit, send_reply
  206.     .errb <drop>
  207.  
  208.     mov SI, BX
  209.     cmp ES:[SI+bootp_op], BOOTP_REPLY
  210.     jnz done
  211.         
  212.     mov AL, ES:[SI+bootp_hops]
  213.     cmp AL, 10
  214.     ja done
  215.     inc AL
  216.     mov ES:[SI+bootp_hops], AL
  217.  
  218.     mov AX, word ptr bootp_&name&_my_ip     ;; only reply if I am the designated
  219.     cmp word ptr ES:[SI+bootp_giaddr], AX    ;; gateway
  220.     jnz done
  221.     mov AX, word ptr bootp_&name&_my_ip+2
  222.     cmp word ptr ES:[SI+bootp_giaddr+2], AX
  223.     jnz done
  224.  
  225.     mov AX, word ptr bootp_&name&_last_xid    ;; dont send to same place twice
  226.     cmp word ptr ES:[SI+bootp_xid], AX    
  227.     jnz send_reply
  228.     mov AX, word ptr bootp_&name&_last_xid+2
  229.     cmp word ptr ES:[SI+bootp_xid+2], AX    
  230.     jz done
  231.  
  232.     send_reply:
  233.     mov AX, word ptr ES:[SI+bootp_xid]
  234.     mov word ptr bootp_&name&_last_xid, AX
  235.     mov AX, word ptr ES:[SI+bootp_xid+2]
  236.     mov word ptr bootp_&name&_last_xid+2, AX
  237.  
  238.     mov AX, word ptr ES:[SI+bootp_yiaddr]       ;; load up the destination
  239.     mov BX, word ptr ES:[SI+bootp_yiaddr+2]     
  240.  
  241.     IRP idx,<1,2,3,4,5,6,7,8>
  242.     if idx le bootp_&name&_dls
  243.         ;; send it out the right interface
  244.     BOOTP_GET_BROADCAST_DL_in_AX_BX_out_AX_BX_const_SI_ES name, idx, sendit
  245.     endif
  246.     endm
  247.     jmp drop
  248.  
  249.     sendit:
  250.     mov CX, size bootp
  251.     mov DX, BOOTPC
  252.  
  253.     push SI
  254.     push ES
  255.     UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES %bootp_&name&_request_sock
  256.     pop  DX
  257.     pop  SI
  258.     or AX, AX
  259.     jnz drop
  260.  
  261.     mov BX, DS              ;; save DS
  262.     mov DS, DX
  263.     mov CX, size bootp
  264.     rep                     ;; copy the packet
  265.     movsb
  266.     mov DS, BX              ;; restore DS
  267.  
  268.     mov CX, size bootp
  269.     UDP_SOCK_W_WRITE_in_CX %bootp_&name&_request_sock
  270.     done:
  271. ENDM
  272.  
  273.  
  274.  
  275. ;;************************************************************************* 
  276. ;; BOOTP_GET_BROADCAST checks to see if the address in AX,BX is on the
  277. ;; local dl 'mydl' and if it is loads AX,BX with the broadcast address 
  278. ;; for 'mydl' and jumps to 'success'.  If AX,BX is not on 'mydl' then
  279. ;; AX,BX is unchanged.
  280. ;;
  281. BOOTP_GET_BROADCAST_DL_in_AX_BX_out_AX_BX_const_SI_ES MACRO name, mydl, success
  282.     local done
  283.     .errb <success>
  284.  
  285.     mov CX, BX
  286.     and CX, word ptr dl_ip_&mydl&_mask+2
  287.     cmp CX, word ptr dl_ip_&mydl&_net+2
  288.     jnz done
  289.     mov CX, AX
  290.     and CX, word ptr dl_ip_&mydl&_mask
  291.     cmp CX, word ptr dl_ip_&mydl&_net
  292.     jnz done
  293.     mov AX, word ptr dl_ip_&mydl&_broad
  294.     mov BX, word ptr dl_ip_&mydl&_broad+2
  295.     jmp success
  296.     done:
  297. ENDM
  298.  
  299.  
  300. ;;******************************************************************************
  301. ;; TFTP_REQUEST_PACKET MACRO name
  302. ;; tftp_request_packet is a KLUGE that will make CISCO's boot properly though
  303. ;; a gateway.  You see Cisco's are brain dead and after they receive the 
  304. ;; bootp reply they ignore the fact that they now have the servers address
  305. ;; and try to TFTP their configuration file by broadcasting a TFTP command
  306. ;; on the local network.  Here i simply act as a repeater of these packets
  307. ;;
  308. TFTP_REQUEST_PACKET_in_AX_BX_CX_ES MACRO bootp, ip
  309.     local done, skip
  310.     .errb <ip>
  311.  
  312.         ;; much of this is TOTALLY ILLEGAL, and breaks every rule in the
  313.         ;; book.  It essentially has the effect of sending out the
  314.         ;; IP packet as if it came from a source other than here
  315.         ;; (in this case it acts as if it came from the original
  316.         ;; sender).  I only do this because I really hope that this
  317.         ;; code will only be needed for a short time.
  318.     mov SI, BX
  319.     sub SI, size udp
  320.     add CX, size udp
  321.     mov AX, word ptr bootp_&bootp&_forward
  322.     mov BX, word ptr bootp_&bootp&_forward+2
  323.     or AX, AX
  324.     jnz skip
  325.     or BX, BX
  326.     jz done
  327.     skip:
  328.     mov DL, UDP_PROTO
  329.     push SI
  330.     push ES
  331.     push CX
  332.     IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES ip
  333.     pop CX
  334.     pop DX
  335.     pop SI
  336.     or AX, AX
  337.     jnz done
  338.  
  339.     mov BX, ES      ;; save ES
  340.     mov ES, DX      ;; restore the input ES
  341.  
  342.     sub SI, 20
  343.     mov AX, word ptr ES:[SI+ip_src]
  344.     mov word ptr ip_&ip&_data.ip_write_src, AX
  345.     mov AX, word ptr ES:[SI+ip_src+2]
  346.     mov word ptr ip_&ip&_data.ip_write_src+2, AX
  347.     add SI, 20
  348.  
  349.     mov ES, BX      ;; restore ES
  350.  
  351.     mov BX, DS      ;; save DS
  352.     mov DS, DX      ;; DS = input ES
  353.  
  354.     mov DX, CX      ;; save CX
  355.     inc CX
  356.     shr CX, 1
  357.     rep
  358.     movsw
  359.     mov DS, BX      ;; restore DS
  360.     mov CX, DX      ;; restore CX
  361.  
  362.     IP_W_WRITE_in_CX ip
  363.     done:
  364. ENDM
  365.  
  366.