classify.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
  4. <!ENTITY mdash "&#x2014;" >
  5. ]>
  6. <chapter id="classify">
  7. <title>Client Classification</title>
  8. <section>
  9. <title>Client Classification Overview</title>
  10. <para>
  11. In certain cases it is useful to differentiate between different
  12. types of clients and treat them accordingly. Common reasons include:
  13. <itemizedlist>
  14. <listitem><para>
  15. The clients represent different pieces of topology, e.g. a cable
  16. modem is different to the clients behind that modem.
  17. </para></listitem>
  18. <listitem><para>
  19. The clients have different behavior, e.g.a smart phone behaves
  20. differently to a lapttop.
  21. </para></listitem>
  22. <listitem><para>
  23. The clients require different values for some options, e.g. a docsis3.0
  24. cable modem requires different settings to docsis2.0 cable modem.
  25. </para></listitem>
  26. </itemizedlist>
  27. </para>
  28. <para>
  29. It is envisaged that client classification will be used for changing the
  30. behavior of almost any part of the DHCP message processing, including the assignment of
  31. leases from different pools, the assignment of different options (or different values of
  32. the same options) etc. In the current release of the software however, there are only three
  33. mechanisms that take
  34. advantage of client classification: subnet selection, assignment of different
  35. options and, for DHCPv4 cable modems, the setting of specific options for use with
  36. the TFTP server address and the boot file field.
  37. </para>
  38. <para>
  39. The process of doing classification is conducted in three steps:
  40. <orderedlist>
  41. <listitem><para>
  42. Assess an incoming packet and assign it to zero or more classes.
  43. </para></listitem>
  44. <listitem><para>
  45. Choose a subnet, possibly based on the class information.
  46. </para></listitem>
  47. <listitem><para>
  48. Assign options, again possibly based on the class information.
  49. </para></listitem>
  50. </orderedlist>
  51. </para>
  52. <para>
  53. When determining which options to include in the response the server will examine
  54. the union of options from all of the assigned classes. In the case two or more
  55. classes include the same option, the value from the first class examined will
  56. be used. Similarly if two or more classes are associated with a subnet, the
  57. first class examined will be used. In the future the processing order of the
  58. various classes may be specified but for now it is being left unspecified and
  59. may change in future releases.
  60. </para>
  61. <para>
  62. As an example, imagine two classes. Class "foo" defines values for an NTP server
  63. (option 42 in DHCPv4) and an SMTP server (option 69 in DHCPv4) while class
  64. "bar" defines values for an NTP server and a POP3 server (option 70 in DHCPv4).
  65. The server will examine the three options NTP, SMTP and POP3 and return any
  66. of them that the client requested. As the NTP server was defined twice the
  67. server will choose only one of the values for the reply: the class from which the
  68. value is obtained is unspecified.
  69. </para>
  70. <para>
  71. There are two methods of doing classification. The first is automatic and relies
  72. on examining the values in the vendor class options. Information from these
  73. options is extracted and a class name is constructed from it and added to
  74. the class list for the packet. The second allows you to specify an expression
  75. that is evaluated for each packet. If the result is true, the packet is
  76. a member of the class.
  77. </para>
  78. <note>
  79. <para>
  80. Care should be taken with client classification as it is easy for
  81. clients that do not meet class criteria to be denied any service altogether.
  82. </para>
  83. </note>
  84. </section>
  85. <section id="classification-using-vendor">
  86. <title>Using Vendor Class Information In Classification</title>
  87. <para>
  88. The server checks whether an incoming DHCPv4 packet includes
  89. the vendor class identifier option (60) or an incoming DHCPv6 packet
  90. includes the vendor class option (16). If it does, the content of that
  91. option is prepended with &quot;VENDOR_CLASS_&quot; and the result is interpreted
  92. as a class. For example, modern cable modems will send this option with
  93. value &quot;docsis3.0&quot; and so the packet will belong to
  94. class &quot;VENDOR_CLASS_docsis3.0&quot;.
  95. </para>
  96. </section>
  97. <section id="classification-using-expressions">
  98. <title>Using Expressions In Classification</title>
  99. <para>
  100. The expression portion of classification contains operators and values.
  101. All values are currently strings and operators take a string or strings and
  102. return another string. When all the operations have completed
  103. the result should be a value of &quot;true&quot; or &quot;false&quot;.
  104. The packet belongs to
  105. the class (and the class name is added to the list of classes) if the result
  106. is &quot;true&quot;. Expressions are written in standard format and can be nested.
  107. </para>
  108. <para>
  109. Expressions are pre-processed during the parsing of the configuration file
  110. and converted to an internal representation. This allows certain types of
  111. errors to be caught and logged during parsing. Examples of these errors
  112. include incorrect number or types of arguments to an operator. The
  113. evaluation code will also check for this class of error and generally
  114. throw an exception, though they should not occur in a normally functioning
  115. system.
  116. </para>
  117. <para>
  118. Other issues, for example the starting position of a substring being
  119. outside of the substring or an option not existing in the packet, result
  120. in the operator returning an empty string.
  121. </para>
  122. <para>
  123. Expressions are a work in progress and the supported operators and
  124. values are limited. The expectation is that additional operators and values
  125. will be added over time, however it is expected the basic mechanisms will
  126. remain the same.
  127. </para>
  128. <para>
  129. <table frame="all" id="classification-values-list">
  130. <title>List of Classification Values</title>
  131. <tgroup cols='3'>
  132. <colspec colname='name' />
  133. <colspec colname='example' />
  134. <colspec colname='description' />
  135. <thead>
  136. <row>
  137. <entry>Name</entry>
  138. <entry>Example</entry>
  139. <entry>Description</entry>
  140. </row>
  141. </thead>
  142. <tbody>
  143. <row><entry>String</entry><entry>'example'</entry><entry>A string</entry></row>
  144. <row><entry>Hex String</entry><entry>0XABCD</entry><entry>A hexadecimal string</entry></row>
  145. <row><entry>Integer</entry><entry>123</entry><entry>An integer value</entry></row>
  146. <row><entry>Option Text</entry><entry>option[code].text</entry><entry>The value of the option with code "code" from the packet as text</entry></row>
  147. <row><entry>Option Hex</entry><entry>option[code].hex</entry><entry>The value of the option with code "code" from the packet as hex</entry></row>
  148. </tbody>
  149. </tgroup>
  150. </table>
  151. Hex Strings are converted into a string as expected. The starting &quot;0X&quot; or
  152. &quot;0x&quot; is removed and if the string is an odd number of characters a
  153. &quot;0&quot; is prepended to it.
  154. </para>
  155. <para>
  156. Integers in the expression are converted to strings
  157. when the expression is read into Kea.
  158. </para>
  159. <para>
  160. "option[code]" extracts the value of the option with the given code
  161. from the incoming packet. If the packet doesn't contain the option, it
  162. returns the empty string. The string can be presented as text or hex
  163. with the ".text" or ".hex" modifiers. In both cases only the payload
  164. is presented; the type code and length fields are not included.
  165. </para>
  166. <para>
  167. <table frame="all" id="classification-expressions-list">
  168. <title>List of Classification Expressions</title>
  169. <tgroup cols='3'>
  170. <colspec colname='name' />
  171. <colspec colname='example' />
  172. <colspec colname='description' />
  173. <thead>
  174. <row>
  175. <entry>Name</entry>
  176. <entry>Example</entry>
  177. <entry>Description</entry>
  178. </row>
  179. </thead>
  180. <tbody>
  181. <row><entry>Equal</entry> <entry>'foo' == 'bar'</entry><entry>Compare the two values and return "true" or "false"</entry></row>
  182. <row><entry>Substring</entry><entry>substring('foobar',0,3)</entry><entry>Return the requested substring</entry></row>
  183. </tbody>
  184. </tgroup>
  185. </table>
  186. The substring operator "substring(value, start, length)" accepts both positive and
  187. negative values for the starting position and the length. For "start", a value of
  188. 0 is the first byte in the string while -1 is the last byte. If the starting
  189. point is outside of the original string an empty string is returned. "length"
  190. is the number of bytes to extract. A negative number means to count towards
  191. the beginning of the string but doesn't include the byte pointed to by "start".
  192. The special value "all" means to return all bytes from start to the end of the
  193. string. If length is longer than the remaining portion of the string then
  194. the entire remaining portion is returned.
  195. </para>
  196. </section>
  197. <note>
  198. <para>
  199. The expression for each class is executed on each packet received.
  200. If the expressions are overly complex, the time taken to execute
  201. them may impact the performance of the server. If you need
  202. complex or time consuming expressions you should write a <link
  203. linkend='hooks-libraries'>hook</link> to perform the necessary work.
  204. </para> </note>
  205. <section id="classification-configuring">
  206. <title>Configuring Classes</title>
  207. <para>
  208. A class contains three items: a name, a test expression and option data.
  209. The name must exist and must be unique amongst all classes. The test
  210. expression and option data are optional.
  211. </para>
  212. <para>
  213. The test expression is a string containing the logical expression used to
  214. determine membership in the class. The entire expression is in double
  215. quotes.
  216. </para>
  217. <para>
  218. The option data is a list which defines any options that should be assigned
  219. to members of this class.
  220. </para>
  221. <para>
  222. <screen>
  223. "Dhcp4": {
  224. "client-class": [<userinput>
  225. {
  226. "name": "Client_foo",
  227. "test": "substring(option[61].text,0,3) == 'foo'",
  228. "option-data": [
  229. {
  230. "name": "domain-name-servers",
  231. "code": 6,
  232. "space": "dhcp4",
  233. "csv-format": true,
  234. "data": "192.0.2.1, 192.0.2.2"
  235. }
  236. ]
  237. },
  238. ...
  239. ],</userinput>
  240. ...
  241. }</screen>
  242. </para>
  243. <para>
  244. In this example the class named &quot;Client_foo&quot; is defined. It is comprised
  245. of all clients who's client ids (option 61) start with the string &quot;foo&quot;.
  246. Members of this class will be given 192.0.2.1 and 192.0.2.2 as their domain name
  247. servers.
  248. </para>
  249. </section>
  250. <section id="classification-subnets">
  251. <title>Configuring Subnets With Class Information</title>
  252. <para>
  253. In certain cases it beneficial to restrict access to certain subnets
  254. only to clients that belong to a given class.
  255. Let's assume that the server is connected to a network segment that uses
  256. the 192.0.2.0/24 prefix. The Administrator of that network has decided
  257. that addresses from range 192.0.2.10 to 192.0.2.20 are going to be
  258. managed by the DHCP4 server. Only clients belonging to client class
  259. Client_foo are allowed to use this subnet. Such a
  260. configuration can be achieved in the following way:
  261. <screen>
  262. "Dhcp4": {
  263. "client-class": [
  264. {
  265. "name": "Client_foo",
  266. "test": "substring(option[61].text,0,3) == 'foo'",
  267. "option-data": [
  268. {
  269. "name": "domain-name-servers",
  270. "code": 6,
  271. "space": "dhcp4",
  272. "csv-format": true,
  273. "data": "192.0.2.1, 192.0.2.2"
  274. }
  275. ]
  276. },
  277. ...
  278. ],<userinput>
  279. "subnet4": [
  280. {
  281. "subnet": "192.0.2.0/24",
  282. "pools": [ { "pool": "192.0.2.10 - 192.0.2.20" } ],
  283. "client-class": "Client_foo"
  284. }
  285. ]</userinput>,
  286. ...
  287. }</screen>
  288. </para>
  289. </section>
  290. <section>
  291. <title>Using Classes</title>
  292. <para>
  293. Currently classes can be used for two functions. They can supply options
  294. to the members of the class and they can be used to choose a subnet from which an
  295. address will be assigned to the class member.
  296. </para>
  297. <para>
  298. When supplying options, options defined as part of the class definition
  299. are considered &quot;class globals&quot;. They will override any global options that
  300. may be defined and in turn will be overridden by any options defined for an
  301. individual subnet.
  302. </para>
  303. </section>
  304. <section>
  305. <title>Classes and Hooks</title>
  306. <para>
  307. You may use a hook to classify your packets. This may be useful if the
  308. expression would either be complex or time consuming and be easier or
  309. better to write as code. Once the hook has added the proper class name
  310. to the packet the rest of the classification system will work as normal
  311. in choosing a subnet and selecting options. For a description of the
  312. hooks see <xref linkend="hooks-libraries"/>, for a description on
  313. configuring he classes see <xref linkend="classification-configuring"/>
  314. and <xref linkend="classification-subnets"/>.
  315. </para>
  316. </section>
  317. </chapter>