home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / pcb121.zip / BRIDGE.INC < prev    next >
Text File  |  1992-01-23  |  11KB  |  329 lines

  1. ;;******************************************************************************
  2. ;;                         bridge.inc      bridge.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. ;; Bridge.inc provides ethernet bridge cababilities.  Bridge will do bridging
  19. ;; between the interfaces provided to it.  It will also act like a IF object
  20. ;; and return packets that are directed to any of its interfaces.
  21. ;; 
  22. ;; The functions provided by this file are
  23. ;;
  24. ;;   BDG_DECLARE name, start_ifs, end_ifs, forward_ip
  25. ;;   BDG_DEFINE name
  26. ;;   BDG_IF_R_ACCESS_out_BX_CX_ES name, no_packet
  27. ;;   BDG_IF_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES name, ok
  28. ;;   BDG_IF_R_FREE_const_BX_CX_BP_SI_DI_ES name
  29. ;;   BDG_IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP name, no_buffer
  30. ;;   BDG_IF_W_WRITE_in_CX_const_BX_BP_ES name
  31. ;;   BDG_IF_SET_ADDRESS_in_SI_const_BX_CX_BP_DI_ES name
  32. ;;   BDG_IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  33. ;;
  34. ;; Variables set by this module
  35. ;;
  36. ;;   bdg_&name&_declared                       ;; one if this interface exists
  37. ;;   if_&name&_address                         ;; the hardware address
  38. ;;   if_&name&_mtu                             ;; the maximum trans unit
  39. ;;
  40. ;;******************************************************************************
  41.  
  42.  
  43. ;;******************************************************************************
  44. ;;   BDG_DECLARE name, start_ifs, end_ifs, forward_ip  
  45. ;;      BDG_DECLARE declares a bridge made up of the ifs [start_ifs..end_ifs].
  46. ;;  All of these ifs are assumed to be declared to be PROMISCUOUS and
  47. ;;  if 'forward_ip' is not blank, then IP broadcasts and ARPS will be 
  48. ;;      forwarded, otherwize they will not
  49. ;;
  50. BDG_DECLARE MACRO name, start_ifs, end_ifs, forward_ip
  51.     .errb <name>
  52.     .errb <start_ifs>
  53.     .errb <end_ifs>
  54.  
  55.     if name ne 1
  56.         .err                ;; you are only alowed 1 bridge and it must be
  57.                             ;; named '1'
  58.     endif
  59.  
  60.     .DATA
  61.     bdg_&name&_declared  = 1
  62.     bdg_&name&_start_ifs = start_ifs
  63.     bdg_&name&_end_ifs = end_ifs
  64.     global bdg_&name&_table_ptr:word        ;; holds segement for table
  65.  
  66.     bdg_&name&_forw_ip   = 0&forward_ip
  67.     .CODE
  68. ENDM
  69.  
  70.  
  71. ;;******************************************************************************
  72. ;;   BDG_DEFINE name
  73. ;;      sets asside memory for the bridge and initializes it.
  74. ;;      routine is a no-op if 'name' was not declared
  75. ;;
  76. BDG_DEFINE MACRO name
  77.     LOCAL loop1, loop2, loop3, table
  78.     .errb <name>
  79.  
  80. ifdef bdg_&name&_declared
  81.     .data
  82.     bdg_&name&_table_ptr dw ?                   ;; holds segment address of tab
  83.     .FarData?
  84.     table db 65535 dup (?)                      ;; set aside a whole segment
  85.                                                 ;; for the bridge table
  86.  
  87.     .CODE
  88.     mov AX, SEG table
  89.     mov bdg_&name&_table_ptr, AX                ;; remember where the table is
  90.  
  91.     mov ES, AX  
  92.     xor DI, DI
  93.     mov CX, 32768
  94.     xor AX, AX
  95.     rep
  96.     stosw                                       ;; null the bridge table
  97.  
  98. endif
  99. ENDM
  100.  
  101.  
  102. ;;******************************************************************************
  103. ;;   IF_R_ACCESS_out_BX_ES name, no_packet
  104. ;;       IF_R_ACCESS waits for the next packet to come from the the board
  105. ;;       associated with 'name' and returns a pointer to the begining of 
  106. ;;       an ethernet packet in BX:ES.  CX holds the length of the packet
  107. ;;       R_ACCESS jumps to 'no_packet' if there are no packets waiting to 
  108. ;;       be read in
  109. ;;       
  110. BDG_IF_R_ACCESS_out_BX_CX_ES MACRO name, no_packet
  111.     local got_packet, forward_all, forward, not_me, drop
  112.     local broadcast, broadcast_drop, around
  113.     .errb <no_packet>
  114.  
  115.     IF_R_ACCESS_out_BX_CX_ES name, no_packet
  116.  
  117.     mov AX, ES:[BX+4]       ;; is this my ethernet address?
  118.     cmp AX, word ptr if_&name&_address+4
  119.     jnz not_me
  120.     mov AX, ES:[BX+2]
  121.     cmp AX, word ptr if_&name&_address+2
  122.     jnz not_me
  123.     mov AX, ES:[BX]
  124.     cmp AX, word ptr if_&name&_address
  125.     jz got_packet
  126.     not_me:
  127.  
  128.     if bdg_1_forw_ip eq 0       ;; drop because it is an IP or ARP packet?
  129.         mov AX, word ptr ES:[BX+12]
  130.         cmp AX, 0008H           ;; is it an IP packet
  131.         jz drop
  132.         cmp AX, 0608H           ;; is it an ARP packet
  133.         jz drop
  134.     endif
  135.  
  136.     mov BP, DS                  ;; save DS
  137.     mov SI, BX
  138.  
  139.     mov AX, ES
  140.     mov ES, bdg_1_table_ptr     ;; ES:0 points to the table
  141.     mov DS, AX                  ;; DS:SI points to the packet
  142.  
  143.     add SI, 6                   ;; skip to source address of the packet
  144.  
  145.     mov DI, [SI+4]              ;; compute hash function ((LSB+MSB) * 8 mod 65k)
  146.     xor DI, [SI+2]              
  147.     shl DI, 1
  148.     shl DI, 1
  149.     shl DI, 1
  150.     movsw                       ;; save the ethernet address
  151.     movsw
  152.     movsw
  153.     mov AL, name            ;; and the fact that it is on this interface
  154.     stosb
  155.  
  156.     sub SI, 12                  ;; restore SI to the begining of the packet
  157.  
  158.     test byte ptr [SI], 1H      ;; is it a broadcast
  159.  
  160.     jnz broadcast
  161.  
  162.     mov DI, [SI+4]              ;; compute hash function ((LSB+MSB) * 8 mod 65k)
  163.     xor DI, [SI+2]              
  164.     shl DI, 1
  165.     shl DI, 1
  166.     shl DI, 1
  167.  
  168.     cmpsw
  169.     jnz forward_all         ;; didn't find in table
  170.     cmpsw
  171.     jnz forward_all         ;; didn't find in table
  172.     cmpsw
  173.     jnz forward_all         ;; didn't find in table
  174.     mov DS, BP              ;; restore DS
  175.     mov AL, ES:[DI]
  176.     cmp AL, name
  177.     jnz forward
  178.         drop:
  179.         IF_R_FREE_const_BX_CX_BP_SI_DI_ES name 
  180.         jmp no_packet
  181.  
  182.     forward:
  183.         mov BP, CX
  184.         IRP idx, <1,2,3,4,5,6,7,8>
  185.         local next
  186.         if (idx le bdg_1_end_ifs) and (idx ge bdg_1_start_ifs)
  187.         if idx ne name
  188.             cmp AL, idx
  189.             jnz next
  190.                 IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP idx, drop
  191.                 mov SI, BX
  192.                 IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name 
  193.                 mov CX, BP
  194.                 IF_W_WRITE_in_CX_const_BX_BP_ES idx
  195.             next:
  196.         endif
  197.         endif
  198.         endm
  199.        jmp drop
  200.  
  201.     forward_all:
  202.         mov DS, BP              ;; restore DS
  203.         mov BP, CX
  204.         IRP idx, <1,2,3,4,5,6,7,8>
  205.         if (idx le bdg_1_end_ifs) and (idx ge bdg_1_start_ifs)
  206.         if idx ne name
  207.             mov CX, BP
  208.             IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP idx, drop
  209.             mov SI, BX
  210.             IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  211.             mov CX, BP
  212.             IF_W_WRITE_in_CX_const_BX_BP_ES idx
  213.         endif
  214.         endif
  215.         endm
  216.        jmp drop
  217.     
  218.     broadcast:
  219.     mov AX, DS                  ;; restore ES
  220.     mov ES, AX
  221.     mov DS, BP                  ;; restore DS
  222.  
  223.     ;; if we don't drop it, then we must send it to all the interfaces
  224.     push ES
  225.     mov BP, CX          
  226.     IRP idx, <1,2,3,4,5,6,7,8>
  227.     if (idx le bdg_1_end_ifs) and (idx ge bdg_1_start_ifs)
  228.     if idx ne name
  229.         mov CX, BP
  230.         IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP idx, drop
  231.         mov SI, BX
  232.         IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  233.         mov CX, BP
  234.         IF_W_WRITE_in_CX_const_BX_BP_ES idx
  235.     endif
  236.     endif
  237.     endm
  238.     pop ES
  239.  
  240.     broadcast_drop:
  241.     ;; in either case, we also send the packet up to the higher level protocols
  242.     mov CX, BP          ;; restore the length
  243.     got_packet:
  244. ENDM
  245.  
  246.  
  247. ;;******************************************************************************
  248. ;;   IF_R_FREE_const_BX_CX_BP_SI_DI_ES  name
  249. ;;       After the client is through processing the packet returned by 
  250. ;;       IF_R_ACCESS, IF_R_FREE must be called to inform 'name' that the 
  251. ;;       memory that the packet was in can be reused for future packets.
  252. ;;
  253. BDG_IF_R_FREE_const_BX_CX_BP_SI_DI_ES MACRO name
  254.     local inside
  255.     .errb <name>
  256.  
  257.     IF_R_FREE_const_BX_CX_BP_SI_DI_ES name
  258. ENDM
  259.  
  260.  
  261. ;;******************************************************************************
  262. ;;   BDG_IF_R_CONT_in_BX_CX_ES name, ok
  263. ;;       IF_R_CONT determines if the packet returned by R_ACCESS in BX:ES
  264. ;;       of length CX is continuous.  If it is it jumps to 'ok' otherwise
  265. ;;       it just returns
  266. ;;
  267. BDG_IF_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES MACRO name, ok
  268.     .errb <ok>
  269.  
  270.     IF_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES name, ok
  271. ENDM
  272.  
  273.  
  274. ;;******************************************************************************
  275. ;;   IF_W_ACCESS_in_CX_out_DI_ES name, no_buffer
  276. ;;       IF_W_ACCESS returns a pointer to an output buffer for a packet.  The 
  277. ;;       pointer is returned in DI:ES.  If the ouptut buffer is busy, this 
  278. ;;       routine will jump to 'no_buffer'.  The output buffer  min(CX, 1536) 
  279. ;;       bytes long
  280. ;;
  281. BDG_IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP MACRO name, no_buffer
  282.     .errb <no_buffer>
  283.  
  284.     IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP name,no_buffer
  285. ENDM
  286.  
  287.  
  288. ;;******************************************************************************
  289. ;;   IF_W_WRITE_in_CX name
  290. ;;       IF_W_WRITE actually signals the ethernet board to write a packet to 
  291. ;;       the ethernet.  The packet is assumed to be in the buffer returned by 
  292. ;;       IF_W_ACCESS. CX is the length of the packet to send.  
  293. ;;
  294. BDG_IF_W_WRITE_in_CX_const_BX_BP_ES MACRO name
  295.     .errb <name>
  296.  
  297.     IF_W_WRITE_in_CX_const_BX_BP_ES name 
  298. ENDM
  299.  
  300.  
  301. ;;******************************************************************************
  302. ;;   IF_SET_ADDRESS_in_SI name
  303. ;;       IF_SET_ADDRESS_in_SI sets the hardware address to be the value
  304. ;;       pointed to by SI.  Note this function may be a no-op if the
  305. ;;       hardware address cannot be set (ETHERNET for example)
  306. ;;
  307. BDG_IF_SET_ADDRESS_in_SI_const_BX_CX_BP_DI_ES MACRO name
  308.     .errb <name>
  309.  
  310.     IF_SET_ADDRESS_in_SI_const_BX_CX_BP_DI_ES name 
  311. ENDM
  312.  
  313.  
  314. ;;******************************************************************************
  315. ;;   IF_COPY_in_CX_SI_DI_ES name
  316. ;;      IF_COPY_in_CX_SI_DI_ES copys a packet from the input buffer (pointed 
  317. ;;      to by SI and the segement register given in IF_DECLARE) to an output 
  318. ;;      buffer (pointed to by DI and dest_reg) of length CX.   It assumes the
  319. ;;      output buffer is contiguous.  (and the caller shouln't care if the 
  320. ;;      input buffer is contiguous)
  321. ;;
  322. BDG_IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES MACRO name
  323.     local wrap, done
  324.     .errb <name>
  325.  
  326.     IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  327. ENDM
  328.  
  329.