Source for file docsis_firewall.php

Documentation is available at docsis_firewall.php

  1. <?php
  2. /**
  3. * DOCSIS Port Filter (Firewall)
  4. *
  5. * @author David Eder <david@eder.us>
  6. * @copyright 2004 David Eder
  7. * @package docsis_firewall
  8. * @version .3
  9. */
  10.  
  11. /**
  12. */
  13. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'docsis_config.php');
  14.  
  15. if(!defined('SOL_ICMP')) define('SOL_ICMP', 1);
  16. if(!defined('SOL_TCP')) define('SOL_TCP', 6);
  17. if(!defined('SOL_UDP')) define('SOL_UDP', 17);
  18. if(!defined('SOL_ALL')) define('SOL_ALL', 256);
  19. if(!defined('DEFAULT_ACCEPT')) define('DEFAULT_ACCEPT', 2);
  20. if(!defined('DEFAULT_DROP')) define('DEFAULT_DROP', 1);
  21. if(!defined('INTERFACE_DEFAULT')) define('INTERFACE_DEFAULT', 0);
  22. if(!defined('DIRECTION_INBOUND')) define('DIRECTION_INBOUND', 1);
  23. if(!defined('DIRECTION_OUTBOUND')) define('DIRECTION_OUTBOUND', 2);
  24. if(!defined('DIRECTION_BOTH')) define('DIRECTION_BOTH', 3);
  25. if(!defined('BROADCAST_TRUE')) define('BROADCAST_TRUE', 1);
  26. if(!defined('BROADCAST_FALSE')) define('BROADCAST_FALSE', 2);
  27. /**
  28. * DOCSIS Port Filter (Firewall)
  29. *
  30. * @package docsis_firewall
  31. */
  32. class docsis_firewall
  33. {
  34. // see http://www.cisco.com/univercd/cc/td/doc/product/cable/cab_rout/cmtsfg/ufgcfile.htm#wp1025297
  35.  
  36.  
  37. var $filters = array();
  38.  
  39. /**
  40. * Constructor
  41. */
  42. function docsis_firewall()
  43. {}
  44.  
  45. /**
  46. * Add a filter to the firewall
  47. *
  48. * @param int $protocol either SOL_ICMP, SOL_TCP, SOL_UDP or SOL_ALL
  49. * @param int $control either DEFAULT_ACCEPT or DEFAULT_DROP
  50. * @param ipaddress $source_ip_address Source IP Address
  51. * @param ipaddress $source_mask Source Mask
  52. * @param int $source_port_low port to start filtering
  53. * @param int $source_port_high port to stop filtering
  54. * @param ipaddress $dest_ip_address Dest IP Address
  55. * @param ipaddress $dest_mask Destination Mask
  56. * @param int $dest_port_low Destination port low
  57. * @param int $dest_port_high Destination port high
  58. * @param int $interface_index Interface index defaults to INTERFACE_DEFAULT
  59. * @param int $direction Direction to match against valid values DIRECTION_INBOUND, DIRECTION_OUTBOUND or DIRECTION_BOTH default Value DIRECTION_BOTH
  60. * @param int $broadcast_only Only match multicast or broadcast traffic Valid Values BROADCAST_TRUE or BROADCAST_FALSE default is BROADCAST_FALSE
  61. */
  62. function add_filter($protocol=SOL_ALL, $control=DEFAULT_DROP,
  63. $source_ip_address='0.0.0.0', $source_mask='0.0.0.0', $source_port_low=0, $source_port_high=65535,
  64. $dest_ip_address='0.0.0.0', $dest_mask='0.0.0.0', $dest_port_low=0, $dest_port_high=65535,
  65. $interface_index=INTERFACE_DEFAULT, $direction=DIRECTION_BOTH, $broadcast_only=BROADCAST_FALSE)
  66. {
  67. $this->filters[] = array('protocol'=>$protocol, 'control'=>$control,
  68. 'source_ip_address'=>$source_ip_address, 'source_mask'=>$source_mask,
  69. 'source_port_low'=>$source_port_low, 'source_port_high'=>$source_port_high,
  70. 'dest_ip_address'=>$dest_ip_address, 'dest_mask'=>$dest_mask,
  71. 'dest_port_low'=>$dest_port_low, 'dest_port_high'=>$dest_port_high,
  72. 'interface_index'=>$interface_index, 'direction'=>$direction, 'broadcast_only'=>$broadcast_only);
  73. }
  74.  
  75. /**
  76. * Write filter to bootfile
  77. *
  78. * @param docsis_config $bootfile to write to
  79. * @param int $default_action either DEFAULT_ACCEPT or DEFAULT_DROP
  80. */
  81. function write(&$bootfile, $default_action=DEFAULT_ACCEPT)
  82. {
  83. $number = 0;
  84. foreach($this->filters as $index=>$filter)
  85. {
  86. $number ++;
  87. // Create the filter and activate it
  88. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.2.' . $number, new rfc1155_Integer(4));
  89.  
  90. // Set Control Accept or Drop for this Rule
  91. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.3.' . $number, new rfc1155_Integer($filter['control']));
  92.  
  93. // Apply filter to all interfaces.
  94. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.4.' . $number, new rfc1155_Integer($filter['interface_index']));
  95.  
  96. // Apply filter to both inbound and outbound traffic.
  97. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.5.' . $number, new rfc1155_Integer($filter['direction']));
  98.  
  99. // Apply Filter to all traffic not just multi cast traffic.
  100. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.6.' . $number, new rfc1155_Integer($filter['broadcast_only']));
  101.  
  102. // Apply filter to traffic with this Source Address
  103. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.7.' . $number, new rfc1155_IPAddress($filter['source_ip_address']));
  104.  
  105. // Apply filter to traffice with this source mask
  106. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.8.' . $number, new rfc1155_IPAddress($filter['source_mask']));
  107. // Apply filter to traffic with this Destination Address
  108. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.9.' . $number, new rfc1155_IPAddress($filter['dest_ip_address']));
  109.  
  110. // Apply filter to traffice with this Destination mask
  111. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.10.' . $number, new rfc1155_IPAddress($filter['dest_mask']));
  112. // Match $this->protocol packets.
  113. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.11.' . $number, new rfc1155_Integer($filter['protocol']));
  114.  
  115. // Apply filter to traffic for port starting at source port_low.
  116. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.12.' . $number, new rfc1155_Integer($filter['source_port_low']));
  117.  
  118. // Apply filter to traffic for port ending at source port_high.
  119. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.13.' . $number, new rfc1155_Integer($filter['source_port_high']));
  120.  
  121. // Apply filter to traffic for port starting at dest port_low.
  122. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.14.' . $number, new rfc1155_Integer($filter['dest_port_low']));
  123.  
  124. // Apply filter to traffic for port ending at dest port_high.
  125. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.4.1.15.' . $number, new rfc1155_Integer($filter['dest_port_high']));
  126.  
  127. }
  128. // Sets Default Action when a packet does not match any rule
  129. // Important note setting this to DEFAULT_DROP will drop all
  130. // traffic reguardless of whether a rule is matched equivalent to Network Access = 0
  131. $bootfile->add_snmp_object('.1.3.6.1.2.1.69.1.6.3.0', new rfc1155_Integer($default_action));
  132. }
  133. }
  134. ?>

Documentation generated on Mon, 14 Nov 2005 18:00:15 -0700 by phpDocumentor 1.3.0RC3