classify.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 laptop.
  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
  33. only three 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. When choosing a subnet the server will iterate over all of the
  57. subnets that are feasible given the information found in the packet (client address,
  58. relay address etc). It will use the first subnet it finds that either doesn't
  59. have a class associated with it or that has a class which matches one of
  60. the packet's classes. In the future the processing order of the
  61. various classes may be specified but for now it is being left unspecified and
  62. may change in future releases.
  63. </para>
  64. <para>
  65. As an example, imagine two classes. Class "foo" defines values for an NTP server
  66. (option 42 in DHCPv4) and an SMTP server (option 69 in DHCPv4) while class
  67. "bar" defines values for an NTP server and a POP3 server (option 70 in DHCPv4).
  68. The server will examine the three options NTP, SMTP and POP3 and return any
  69. of them that the client requested. As the NTP server was defined twice the
  70. server will choose only one of the values for the reply: the class from which the
  71. value is obtained is unspecified.
  72. </para>
  73. <para>
  74. There are two methods of doing classification. The first is automatic and relies
  75. on examining the values in the vendor class options. Information from these
  76. options is extracted and a class name is constructed from it and added to
  77. the class list for the packet. The second allows you to specify an expression
  78. that is evaluated for each packet. If the result is true, the packet is
  79. a member of the class.
  80. </para>
  81. <note>
  82. <para>
  83. Care should be taken with client classification as it is easy for
  84. clients that do not meet class criteria to be denied any service altogether.
  85. </para>
  86. </note>
  87. </section>
  88. <section id="classification-using-vendor">
  89. <title>Using Vendor Class Information In Classification</title>
  90. <para>
  91. The server checks whether an incoming DHCPv4 packet includes
  92. the vendor class identifier option (60) or an incoming DHCPv6 packet
  93. includes the vendor class option (16). If it does, the content of that
  94. option is prepended with &quot;VENDOR_CLASS_&quot; and the result is interpreted
  95. as a class. For example, modern cable modems will send this option with
  96. value &quot;docsis3.0&quot; and so the packet will belong to
  97. class &quot;VENDOR_CLASS_docsis3.0&quot;.
  98. </para>
  99. </section>
  100. <section id="classification-using-expressions">
  101. <title>Using Expressions In Classification</title>
  102. <para>
  103. The expression portion of classification contains operators and values.
  104. All values are currently strings and operators take a string or strings and
  105. return another string. When all the operations have completed
  106. the result should be a value of &quot;true&quot; or &quot;false&quot;.
  107. The packet belongs to
  108. the class (and the class name is added to the list of classes) if the result
  109. is &quot;true&quot;. Expressions are written in standard format and can be nested.
  110. </para>
  111. <para>
  112. Expressions are pre-processed during the parsing of the configuration file
  113. and converted to an internal representation. This allows certain types of
  114. errors to be caught and logged during parsing. Examples of these errors
  115. include incorrect number or types of arguments to an operator. The
  116. evaluation code will also check for this class of error and generally
  117. throw an exception, though they should not occur in a normally functioning
  118. system.
  119. </para>
  120. <para>
  121. Other issues, for example the starting position of a substring being
  122. outside of the substring or an option not existing in the packet, result
  123. in the operator returning an empty string.
  124. </para>
  125. <para>
  126. Expressions are a work in progress and the supported operators and
  127. values are limited. The expectation is that additional operators and values
  128. will be added over time, however it is expected the basic mechanisms will
  129. remain the same.
  130. </para>
  131. <para>
  132. <table frame="all" id="classification-values-list">
  133. <title>List of Classification Values</title>
  134. <tgroup cols='3'>
  135. <colspec colname='name' />
  136. <colspec colname='example' />
  137. <colspec colname='description' />
  138. <thead>
  139. <row>
  140. <entry>Name</entry>
  141. <entry>Example</entry>
  142. <entry>Description</entry>
  143. </row>
  144. </thead>
  145. <tbody>
  146. <row><entry>String</entry><entry>'example'</entry><entry>A string</entry></row>
  147. <row><entry>Hex String</entry><entry>0XABCD</entry><entry>A hexadecimal string</entry></row>
  148. <row><entry>Integer</entry><entry>123</entry><entry>An integer value</entry></row>
  149. <!-- Text option not fully defined yet, leave it out
  150. <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>
  151. -->
  152. <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>
  153. <row><entry>Option Exist</entry><entry>option[code].exist</entry><entry>If the option with code "code" is present in the packet "true" else "false"</entry></row>
  154. <row><entry>DHCPv4 Relay Agent
  155. sub-option</entry><entry>relay4[code].hex</entry><entry>The value of
  156. sub-option with code "code" from the DHCPv4 Relay Agent Information option
  157. (option 82)</entry></row>
  158. <row>
  159. <entry>Message Type in DHCPv6 packet</entry>
  160. <entry>pkt6.msgtype</entry>
  161. <!-- <entry>'1'</entry>
  162. -->
  163. <entry>The value of the message type field in the DHCPv6 packet.</entry>
  164. </row>
  165. <row>
  166. <entry>Transaction ID in DHCPv6 packet</entry>
  167. <entry>pkt6.transid</entry>
  168. <!-- <entry>'12345'</entry>
  169. -->
  170. <entry>The value of the transaction id in the DHCPv6 packet.</entry>
  171. </row>
  172. </tbody>
  173. </tgroup>
  174. </table>
  175. Hex Strings are converted into a string as expected. The starting &quot;0X&quot; or
  176. &quot;0x&quot; is removed and if the string is an odd number of characters a
  177. &quot;0&quot; is prepended to it.
  178. </para>
  179. <para>
  180. Integers in the expression are converted to strings
  181. when the expression is read into Kea.
  182. </para>
  183. <para>
  184. "option[code].hex" extracts the value of the option with the given code
  185. from the incoming packet. If the packet doesn't contain the option, it
  186. returns the empty string. The string is presented as a byte string of
  187. the option payload without the type code or length fields.
  188. </para>
  189. <para>
  190. "option[code].exist" checks if an option with the given code is present
  191. in the incoming packet. It can be used with empty options.
  192. </para>
  193. <para>
  194. "relay4[code].hex" attempts to extract the value of the sub-option
  195. "code" from the option inserted as the DHCPv4 Relay Agent Information
  196. (82) option. If the packet doesn't contain a RAI option, or the RAI
  197. option doesn't contain the requested sub-option, the expression returns
  198. an empty string. The string is presented as a byte string of the
  199. option payload without the type code or length fields. This
  200. expression is allowed in DHCPv4 only.
  201. </para>
  202. <para>
  203. "relay4" shares the same representation types than "option", for
  204. instance "relay4[code].exists" is supported.
  205. </para>
  206. <para>
  207. "pkt6" refers to information from the client request. To access any
  208. information from an intermediate relay use "relay6". "pkt6.msgtype"
  209. outputs the string representation of the integer value for the message
  210. type, for example SOLICIT will be '1'. "pkt6.transid" outputs the string
  211. representation of the value of the transaction ID.
  212. </para>
  213. <para>
  214. <table frame="all" id="classification-expressions-list">
  215. <title>List of Classification Expressions</title>
  216. <tgroup cols='3'>
  217. <colspec colname='name' />
  218. <colspec colname='example' />
  219. <colspec colname='description' />
  220. <thead>
  221. <row>
  222. <entry>Name</entry>
  223. <entry>Example</entry>
  224. <entry>Description</entry>
  225. </row>
  226. </thead>
  227. <tbody>
  228. <row><entry>Equal</entry> <entry>'foo' == 'bar'</entry><entry>Compare the two values and return "true" or "false"</entry></row>
  229. <row><entry>Not</entry> <entry>not ('foo' == 'bar')</entry><entry>Logical negation</entry></row>
  230. <row><entry>And</entry> <entry>('foo' == 'bar') and ('bar' == 'foo')</entry><entry>Logical and</entry></row>
  231. <row><entry>Or</entry> <entry>('foo' == 'bar') or ('bar' == 'foo')</entry><entry>Logical or</entry></row>
  232. <row><entry>Substring</entry><entry>substring('foobar',0,3)</entry><entry>Return the requested substring</entry></row>
  233. <row><entry>Concat</entry><entry>concat('foo','bar')</entry><entry>Return the concatenation of the strings</entry></row>
  234. </tbody>
  235. </tgroup>
  236. </table>
  237. </para>
  238. <section>
  239. <title>Logical operators</title>
  240. The Not, And and Or logical operators are the common operators. Not
  241. has the highest precedence, Or the lowest. And and Or are (left)
  242. associative, parentheses around a logical expression can be used
  243. to enforce a specific grouping, for instance in "A and (B or C)"
  244. (without parentheses "A and B or C" means "(A and B) or C").
  245. </section>
  246. <section>
  247. <title>Substring</title>
  248. The substring operator "substring(value, start, length)" accepts both positive and
  249. negative values for the starting position and the length. For "start", a value of
  250. 0 is the first byte in the string while -1 is the last byte. If the starting
  251. point is outside of the original string an empty string is returned. "length"
  252. is the number of bytes to extract. A negative number means to count towards
  253. the beginning of the string but doesn't include the byte pointed to by "start".
  254. The special value "all" means to return all bytes from start to the end of the
  255. string. If length is longer than the remaining portion of the string then
  256. the entire remaining portion is returned. Some examples may be helpful:
  257. <screen>
  258. substring('foobar', 0, 6) == 'foobar'
  259. substring('foobar', 3, 3) == 'bar'
  260. substring('foobar', 3, all) == 'bar'
  261. substring('foobar', 1, 4) == 'ooba'
  262. substring('foobar', -5, 4) == 'ooba'
  263. substring('foobar', -1, -3) == 'oba'
  264. substring('foobar', 4, -2) == 'ob'
  265. substring('foobar', 10, 2) == ''
  266. </screen>
  267. </section>
  268. <section>
  269. <title>Concat</title>
  270. The concat function "concat(string1, string2)" returns the
  271. concatenation of its two arguments. For instance:
  272. <screen>
  273. concat('foo', 'bar') == 'foobar'
  274. </screen>
  275. </section>
  276. </section>
  277. <note>
  278. <para>
  279. The expression for each class is executed on each packet received.
  280. If the expressions are overly complex, the time taken to execute
  281. them may impact the performance of the server. If you need
  282. complex or time consuming expressions you should write a <link
  283. linkend='hooks-libraries'>hook</link> to perform the necessary work.
  284. </para> </note>
  285. <section id="classification-configuring">
  286. <title>Configuring Classes</title>
  287. <para>
  288. A class contains three items: a name, a test expression and option data.
  289. The name must exist and must be unique amongst all classes. The test
  290. expression and option data are optional.
  291. </para>
  292. <para>
  293. The test expression is a string containing the logical expression used to
  294. determine membership in the class. The entire expression is in double
  295. quotes.
  296. </para>
  297. <para>
  298. The option data is a list which defines any options that should be assigned
  299. to members of this class.
  300. </para>
  301. <para>
  302. In the following example the class named &quot;Client_foo&quot; is defined.
  303. It is comprised of all clients who's client ids (option 61) start with the
  304. string &quot;foo&quot;. Members of this class will be given 192.0.2.1 and
  305. 192.0.2.2 as their domain name servers.
  306. <screen>
  307. "Dhcp4": {
  308. "client-classes": [<userinput>
  309. {
  310. "name": "Client_foo",
  311. "test": "substring(option[61].hex,0,3) == 'foo'",
  312. "option-data": [
  313. {
  314. "name": "domain-name-servers",
  315. "code": 6,
  316. "space": "dhcp4",
  317. "csv-format": true,
  318. "data": "192.0.2.1, 192.0.2.2"
  319. }
  320. ]
  321. },
  322. ...
  323. ],</userinput>
  324. ...
  325. }</screen>
  326. </para>
  327. <para>
  328. This example shows a client class being defined for use by the DHCPv6 server.
  329. In it the class named &quot;Client_enterprise&quot; is defined. It is comprised
  330. of all clients who's client identifiers start with the given hex string (which
  331. would indicate a DUID based on an enterprise id of 0xAABBCCDD). Members of this
  332. class will be given an 2001:db8:0::1 and 2001:db8:2::1 as their domain name servers.
  333. <screen>
  334. "Dhcp6": {
  335. "client-classes": [<userinput>
  336. {
  337. "name": "Client_enterprise",
  338. "test": "substring(option[1].hex,0,6) == 0x0002AABBCCDD'",
  339. "option-data": [
  340. {
  341. "name": "dns-servers",
  342. "code": 23,
  343. "space": "dhcp6",
  344. "csv-format": true,
  345. "data": "2001:db8:0::1, 2001:db8:2::1"
  346. }
  347. ]
  348. },
  349. ...
  350. ],</userinput>
  351. ...
  352. }</screen>
  353. </para>
  354. </section>
  355. <section id="classification-subnets">
  356. <title>Configuring Subnets With Class Information</title>
  357. <para>
  358. In certain cases it beneficial to restrict access to certain subnets
  359. only to clients that belong to a given class using the "client-class"
  360. keyword when defining the subnet.
  361. </para>
  362. <para>
  363. Let's assume that the server is connected to a network segment that uses
  364. the 192.0.2.0/24 prefix. The Administrator of that network has decided
  365. that addresses from range 192.0.2.10 to 192.0.2.20 are going to be
  366. managed by the DHCP4 server. Only clients belonging to client class
  367. Client_foo are allowed to use this subnet. Such a
  368. configuration can be achieved in the following way:
  369. <screen>
  370. "Dhcp4": {
  371. "client-classes": [
  372. {
  373. "name": "Client_foo",
  374. "test": "substring(option[61].hex,0,3) == 'foo'",
  375. "option-data": [
  376. {
  377. "name": "domain-name-servers",
  378. "code": 6,
  379. "space": "dhcp4",
  380. "csv-format": true,
  381. "data": "192.0.2.1, 192.0.2.2"
  382. }
  383. ]
  384. },
  385. ...
  386. ],<userinput>
  387. "subnet4": [
  388. {
  389. "subnet": "192.0.2.0/24",
  390. "pools": [ { "pool": "192.0.2.10 - 192.0.2.20" } ],
  391. "client-class": "Client_foo"
  392. },
  393. ...
  394. ],</userinput>,
  395. ...
  396. }</screen>
  397. </para>
  398. <para>
  399. The following example shows restricting access to a DHCPv6 subnet. This
  400. configuration will restrict use of the addresses 2001:db8:1::1 to
  401. 2001:db8:1::FFFF to members of the "Client_enterprise" class.
  402. <screen>
  403. "Dhcp6": {
  404. "client-classes": [
  405. {
  406. "name": "Client_enterprise",
  407. "test": "substring(option[1].hex,0,6) == 0x0002AABBCCDD'",
  408. "option-data": [
  409. {
  410. "name": "dns-servers",
  411. "code": 23,
  412. "space": "dhcp6",
  413. "csv-format": true,
  414. "data": "2001:db8:0::1, 2001:db8:2::1"
  415. }
  416. ]
  417. },
  418. ...
  419. ], <userinput>
  420. "subnet6": [
  421. {
  422. "subnet": "2001:db8:1::/64",
  423. "pools": [ { "pool": "2001:db8:1::-2001:db8:1::ffff" } ],
  424. "client-class": "Client_enterprise"
  425. }
  426. ],</userinput>
  427. ...
  428. }</screen>
  429. </para>
  430. </section>
  431. <section>
  432. <title>Using Classes</title>
  433. <para>
  434. Currently classes can be used for two functions. They can supply options
  435. to the members of the class and they can be used to choose a subnet from which an
  436. address will be assigned to the class member.
  437. </para>
  438. <para>
  439. When supplying options, options defined as part of the class definition
  440. are considered &quot;class globals&quot;. They will override any global options that
  441. may be defined and in turn will be overridden by any options defined for an
  442. individual subnet.
  443. </para>
  444. </section>
  445. <section>
  446. <title>Classes and Hooks</title>
  447. <para>
  448. You may use a hook to classify your packets. This may be useful if the
  449. expression would either be complex or time consuming and be easier or
  450. better to write as code. Once the hook has added the proper class name
  451. to the packet the rest of the classification system will work as normal
  452. in choosing a subnet and selecting options. For a description of the
  453. hooks see <xref linkend="hooks-libraries"/>, for a description on
  454. configuring he classes see <xref linkend="classification-configuring"/>
  455. and <xref linkend="classification-subnets"/>.
  456. </para>
  457. </section>
  458. </chapter>