classify.xml 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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 expression</entry>
  142. <entry>Example value</entry>
  143. <entry>Description</entry>
  144. </row>
  145. </thead>
  146. <tbody>
  147. <row>
  148. <entry>String literal</entry>
  149. <entry>'example'</entry>
  150. <entry>'example'</entry>
  151. <entry>A string</entry>
  152. </row>
  153. <row>
  154. <entry>Hexadecimal string literal</entry>
  155. <entry>0x5a7d</entry>
  156. <entry>'Z}'</entry>
  157. <entry>A hexadecimal string</entry>
  158. </row>
  159. <row>
  160. <entry>IP address literal</entry>
  161. <entry>10.0.0.1</entry>
  162. <entry>0x0a000001</entry>
  163. <entry>An IP address</entry>
  164. </row>
  165. <row>
  166. <entry>Integer literal</entry>
  167. <entry>123</entry>
  168. <entry>'123'</entry>
  169. <entry>A 32 bit unsigned integer value</entry>
  170. </row>
  171. <row></row>
  172. <row>
  173. <entry>Binary content of the option</entry>
  174. <entry>option[123].hex</entry>
  175. <entry>'(content of the option)'</entry>
  176. <entry>The value of the option with given code from the
  177. packet as hex</entry>
  178. </row>
  179. <!-- Text option not fully defined yet, leave it out
  180. <row>
  181. <entry>Option Text</entry>
  182. <entry>option[123].text</entry>
  183. <entry>'foobar'</entry>
  184. <entry>The value of the option with given code from the
  185. packet as text</entry>
  186. </row>
  187. -->
  188. <row>
  189. <entry>Option existence</entry>
  190. <entry>option[123].exists</entry>
  191. <entry>'true'</entry>
  192. <entry>If the option with given code is present in the
  193. packet "true" else "false"</entry>
  194. </row>
  195. <row>
  196. <entry>DHCPv4 relay agent sub-option</entry>
  197. <entry>relay4[123].hex</entry>
  198. <entry>'(content of the RAI sub-option)'</entry>
  199. <entry>The value of sub-option with given code from the
  200. DHCPv4 Relay Agent Information option (option 82)</entry>
  201. </row>
  202. <row>
  203. <entry>DHCPv6 Relay Options</entry>
  204. <entry>relay6[nest].option[code].hex</entry>
  205. <entry>(value of the option)</entry>
  206. <entry>The value of the option with code "code" from the
  207. relay encapsulation "nest"</entry>
  208. </row>
  209. <row>
  210. <entry>DHCPv6 Relay Peer Address</entry>
  211. <entry>relay6[nest].peeraddr</entry>
  212. <entry>2001:DB8::1</entry>
  213. <entry>The value of the peer address field from the
  214. relay encapsulation "nest"</entry>
  215. </row>
  216. <row>
  217. <entry>DHCPv6 Relay Link Address</entry>
  218. <entry>relay6[nest].linkaddr</entry>
  219. <entry>2001:DB8::1</entry>
  220. <entry>The value of the link address field from the
  221. relay encapsulation "nest"</entry>
  222. </row>
  223. <row>
  224. <entry>Interface name of packet</entry>
  225. <entry>pkt.iface</entry>
  226. <entry>eth0</entry>
  227. <entry>The name of the incoming interface of a DHCP packet.</entry>
  228. </row>
  229. <row>
  230. <entry>Source address of packet</entry>
  231. <entry>pkt.src</entry>
  232. <entry>10.1.2.3</entry>
  233. <entry>The IP source address of a DHCP packet.</entry>
  234. </row>
  235. <row>
  236. <entry>Destination address of packet</entry>
  237. <entry>pkt.dst</entry>
  238. <entry>10.1.2.3</entry>
  239. <entry>The IP destination address of a DHCP packet.</entry>
  240. </row>
  241. <row>
  242. <entry>Length of packet</entry>
  243. <entry>pkt.len</entry>
  244. <entry>513</entry>
  245. <entry>The length of a DHCP packet (UDP header field), expressed
  246. as a 32 bit unsigned integer.</entry>
  247. </row>
  248. <row>
  249. <entry>Hardware address in DHCPv4 packet</entry>
  250. <entry>pkt4.mac</entry>
  251. <entry>0x010203040506</entry>
  252. <entry>The value of the chaddr field of the DHCPv4 packet, hlen (0 to 16) bytes</entry>
  253. </row>
  254. <row>
  255. <entry>Hardware length in DHCPv4 packet</entry>
  256. <entry>pkt4.hlen</entry>
  257. <entry>6</entry>
  258. <entry>The value of the hlen field of the DHCPv4 packet padded to 4 bytes</entry>
  259. </row>
  260. <row>
  261. <entry>Hardware type in DHCPv4 packet</entry>
  262. <entry>pkt4.htype</entry>
  263. <entry>6</entry>
  264. <entry>The value of the htype field of the DHCPv4 packet padded to 4 bytes</entry>
  265. </row>
  266. <row>
  267. <entry>ciaddr field in DHCPv4 packet</entry>
  268. <entry>pkt4.ciaddr</entry>
  269. <entry>192.0.2.1</entry>
  270. <entry>The value of the ciaddr field of the DHCPv4 packet (IPv4 address, 4 bytes)</entry>
  271. </row>
  272. <row>
  273. <entry>giaddr field in DHCPv4 packet</entry>
  274. <entry>pkt4.giaddr</entry>
  275. <entry>192.0.2.1</entry>
  276. <entry>The value of the giaddr field of the DHCPv4 packet (IPv4 address, 4 bytes)</entry>
  277. </row>
  278. <row>
  279. <entry>yiaddr field in DHCPv4 packet</entry>
  280. <entry>pkt4.yiaddr</entry>
  281. <entry>192.0.2.1</entry>
  282. <entry>The value of the yiaddr field of the DHCPv4 packet (IPv4 address, 4 bytes)</entry>
  283. </row>
  284. <row>
  285. <entry>siaddr field in DHCPv4 packet</entry>
  286. <entry>pkt4.siaddr</entry>
  287. <entry>192.0.2.1</entry>
  288. <entry>The value of the siaddr field of the DHCPv4 packet (IPv4 address, 4 bytes)</entry>
  289. </row>
  290. <row>
  291. <entry>Message Type in DHCPv4 packet</entry>
  292. <entry>pkt4.msgtype</entry>
  293. <entry>1</entry>
  294. <entry>The value of the message type field in the DHCPv4
  295. packet (expressed as a 32 bit unsigned integer).</entry>
  296. </row>
  297. <row>
  298. <entry>Transaction ID (xid) in DHCPv4 packet</entry>
  299. <entry>pkt4.transid</entry>
  300. <entry>12345</entry>
  301. <entry>The value of the transaction id in the DHCPv4
  302. packet (expressed as a 32 bit unsigned integer).</entry>
  303. </row>
  304. <row>
  305. <entry>Message Type in DHCPv6 packet</entry>
  306. <entry>pkt6.msgtype</entry>
  307. <entry>1</entry>
  308. <entry>The value of the message type field in the DHCPv6
  309. packet (expressed as a 32 bit unsigned integer).</entry>
  310. </row>
  311. <row>
  312. <entry>Transaction ID in DHCPv6 packet</entry>
  313. <entry>pkt6.transid</entry>
  314. <entry>12345</entry>
  315. <entry>The value of the transaction id in the DHCPv6
  316. packet (expressed as a 32 bit unsigned integer).</entry>
  317. </row>
  318. <row>
  319. <entry>Vendor option existence (any vendor)</entry>
  320. <entry>vendor[*].exists</entry>
  321. <entry>true</entry>
  322. <entry>Returns whether a vendor option from any vendor
  323. is present ('true') or absent ('false').</entry>
  324. </row>
  325. <row>
  326. <entry>Vendor option existence (specific vendor)</entry>
  327. <entry>vendor[4491].exists</entry>
  328. <entry>true</entry>
  329. <entry>Returns whether a vendor option from specified
  330. vendor (determined by its enterprise-id)
  331. is present ('true') or absent ('false').</entry>
  332. </row>
  333. <row>
  334. <entry>Enterprise-id from vendor option</entry>
  335. <entry>vendor.enterprise</entry>
  336. <entry>4491</entry>
  337. <entry>If the vendor option is present, it returns the
  338. value of the enterprise-id field padded to 4
  339. bytes. Returns '' otherwise.</entry>
  340. </row>
  341. <row>
  342. <entry>Vendor sub-option existence</entry>
  343. <entry>vendor[4491].option[1].exists</entry>
  344. <entry>true</entry>
  345. <entry>Returns 'true' if there is vendor option with
  346. specified enterprise-id and given sub-option is present.
  347. Returns 'false' otherwise.</entry>
  348. </row>
  349. <row>
  350. <entry>Vendor sub-option content</entry>
  351. <entry>vendor[4491].option[1].hex</entry>
  352. <entry>docsis3.0</entry>
  353. <entry>Returns content of the specified sub-option of
  354. a vendor option with specified enterprise id. Returns
  355. '' if no such option or sub-option is present.
  356. </entry>
  357. </row>
  358. <row>
  359. <entry>Vendor class option existence (any vendor)</entry>
  360. <entry>vendor-class[*].exists</entry>
  361. <entry>true</entry>
  362. <entry>Returns whether a vendor class option from any vendor
  363. is present ('true') or absent ('false').</entry>
  364. </row>
  365. <row>
  366. <entry>Vendor class option existence (specific vendor)</entry>
  367. <entry>vendor-class[4491].exists</entry>
  368. <entry>true</entry>
  369. <entry>Returns whether a vendor class option from specified
  370. vendor (determined by its enterprise-id)
  371. is present ('true') or absent ('false').</entry>
  372. </row>
  373. <row>
  374. <entry>Enterprise-id from vendor class option</entry>
  375. <entry>vendor-class.enterprise</entry>
  376. <entry>4491</entry>
  377. <entry>If the vendor option is present, it returns the
  378. value of the enterprise-id field padded to 4
  379. bytes. Returns '' otherwise.</entry>
  380. </row>
  381. <row>
  382. <entry>First data chunk from vendor class option</entry>
  383. <entry>vendor-class[4491].data</entry>
  384. <entry>docsis3.0</entry>
  385. <entry>Returns content of the first data chunk from
  386. the vendor class option with specified enterprise-id.
  387. Returns '' if missing.</entry>
  388. </row>
  389. <row>
  390. <entry>Specific data chunk from vendor class option</entry>
  391. <entry>vendor-class[4491].data[3]</entry>
  392. <entry>docsis3.0</entry>
  393. <entry>Returns content of the specified data chunk of
  394. a vendor class option with specified enterprise id. Returns
  395. '' if no such option or data chunk is present.
  396. </entry>
  397. </row>
  398. </tbody>
  399. </tgroup>
  400. </table>
  401. Hex Strings are converted into a string as expected. The starting &quot;0X&quot; or
  402. &quot;0x&quot; is removed and if the string is an odd number of characters a
  403. &quot;0&quot; is prepended to it.
  404. </para>
  405. <para>
  406. IP addresses are converted into strings of length 4 or 16. IPv4, IPv6,
  407. and IPv4 embedded IPv6 (e.g., IPv4 mapped IPv6) addresses are supported.
  408. </para>
  409. <para>
  410. Integers in an expression are converted to 32 bit unsigned integers and
  411. are represented as four byte strings. For example 123 is represented as
  412. 0x0000007b. All expressions that return numeric values use 32 bit
  413. unsigned integers, even if the field in the packet is smaller. In general
  414. it is easier to use decimal notation to represent integers, but it is also
  415. possible to use hex notation. When using hex notation to represent an
  416. integer care should be taken to make sure the value is represented as 32
  417. bits, e.g. use 0x00000001 instead of 0x1 or 0x01. Also, make
  418. sure the value is specified in network order, e.g. 1 is
  419. represented as 0x00000001.
  420. </para>
  421. <para>
  422. "option[code].hex" extracts the value of the option with the code "code"
  423. from the incoming packet. If the packet doesn't contain the option, it
  424. returns the empty string. The string is presented as a byte string of
  425. the option payload without the type code or length fields.
  426. </para>
  427. <para>
  428. "option[code].exists" checks if an option with the code "code" is present
  429. in the incoming packet. It can be used with empty options.
  430. </para>
  431. <para>
  432. "relay4[code].hex" attempts to extract the value of the sub-option
  433. "code" from the option inserted as the DHCPv4 Relay Agent Information
  434. (82) option. If the packet doesn't contain a RAI option, or the RAI
  435. option doesn't contain the requested sub-option, the expression returns
  436. an empty string. The string is presented as a byte string of the
  437. option payload without the type code or length fields. This
  438. expression is allowed in DHCPv4 only.
  439. </para>
  440. <para>
  441. "relay4" shares the same representation types as "option", for
  442. instance "relay4[code].exists" is supported.
  443. </para>
  444. <para>
  445. "relay6[nest]" allows access to the encapsulations used by any DHCPv6
  446. relays that forwarded the packet. The "nest" level specifies the relay
  447. from which to extract the information, with a value of 0 indicating
  448. the relay closest to the DHCPv6 server. If the requested encapsulation
  449. doesn't exist an empty string "" is returned. This expression is
  450. allowed in DHCPv6 only.
  451. </para>
  452. <para>
  453. "relay6[nest].option[code]" shares the same representation types as
  454. "option", for instance "relay6[nest].option[code].exists" is supported.
  455. </para>
  456. <para>
  457. Expressions starting with "pkt4" can be used only in DHCPv4.
  458. They allows access to DHCPv4 message fields.
  459. </para>
  460. <para>
  461. "pkt6" refers to information from the client request. To access any
  462. information from an intermediate relay use "relay6". "pkt6.msgtype"
  463. and "pkt6.transid" output a 4 byte binary string for the message type
  464. or transaction id. For example the message type SOLICIT will be
  465. "0x00000001" or simply 1 as in "pkt6.msgtype == 1".
  466. </para>
  467. <para>
  468. Vendor option means Vendor-Identifying Vendor-specific Information
  469. option (code 125, see Section 4 of RFC3925) in DHCPv4 and
  470. Vendor-specific Information Option (code 17, defined in Section 22.17 of
  471. RFC3315) in DHCPv6. Vendor class option means Vendor-Identifying Vendor
  472. Class Option (code 124, see Section 3 of RFC3925) in DHCPv4 and Vendor
  473. Class Option (code 16, see Section 22.16 of RFC3315). Vendor options may
  474. have sub-options that are referenced by their codes. Vendor class
  475. options do not have sub-options, but rather data chunks, which are
  476. referenced by index value. Index 0 means the first data chunk, Index 1
  477. is for the second data chunk (if present), etc.
  478. </para>
  479. <para>In the vendor and vendor-class constructs Asterisk (*) or 0 can be
  480. used to specify a wildcard enterprise-id value, i.e. it will match any
  481. enterprise-id value.</para>
  482. <para>Vendor Class Identifier (option 60 in DHCPv4) can be
  483. accessed using option[60] expression.</para>
  484. <para>RFC3925 and RFC3315 allow for multiple instances of vendor options
  485. to appear in a single message. The client classification code currently
  486. examines the first instance if more than one appear. For vendor.enterprise
  487. and vendor-class.enterprise expressions, the value from the first instance
  488. is returned. Please submit a feature request on Kea website if you need
  489. support for multiple instances.</para>
  490. <para>
  491. <table frame="all" id="classification-expressions-list">
  492. <title>List of Classification Expressions</title>
  493. <tgroup cols='3'>
  494. <colspec colname='name' />
  495. <colspec colname='example' />
  496. <colspec colname='description' />
  497. <thead>
  498. <row>
  499. <entry>Name</entry>
  500. <entry>Example</entry>
  501. <entry>Description</entry>
  502. </row>
  503. </thead>
  504. <tbody>
  505. <row><entry>Equal</entry> <entry>'foo' == 'bar'</entry><entry>Compare the two values and return "true" or "false"</entry></row>
  506. <row><entry>Not</entry> <entry>not ('foo' == 'bar')</entry><entry>Logical negation</entry></row>
  507. <row><entry>And</entry> <entry>('foo' == 'bar') and ('bar' == 'foo')</entry><entry>Logical and</entry></row>
  508. <row><entry>Or</entry> <entry>('foo' == 'bar') or ('bar' == 'foo')</entry><entry>Logical or</entry></row>
  509. <row><entry>Substring</entry><entry>substring('foobar',0,3)</entry><entry>Return the requested substring</entry></row>
  510. <row><entry>Concat</entry><entry>concat('foo','bar')</entry><entry>Return the
  511. concatenation of the strings</entry></row>
  512. </tbody>
  513. </tgroup>
  514. </table>
  515. </para>
  516. <section>
  517. <title>Logical operators</title>
  518. The Not, And and Or logical operators are the common operators. Not
  519. has the highest precedence, Or the lowest. And and Or are (left)
  520. associative, parentheses around a logical expression can be used
  521. to enforce a specific grouping, for instance in "A and (B or C)"
  522. (without parentheses "A and B or C" means "(A and B) or C").
  523. </section>
  524. <section>
  525. <title>Substring</title>
  526. The substring operator "substring(value, start, length)" accepts both positive and
  527. negative values for the starting position and the length. For "start", a value of
  528. 0 is the first byte in the string while -1 is the last byte. If the starting
  529. point is outside of the original string an empty string is returned. "length"
  530. is the number of bytes to extract. A negative number means to count towards
  531. the beginning of the string but doesn't include the byte pointed to by "start".
  532. The special value "all" means to return all bytes from start to the end of the
  533. string. If length is longer than the remaining portion of the string then
  534. the entire remaining portion is returned. Some examples may be helpful:
  535. <screen>
  536. substring('foobar', 0, 6) == 'foobar'
  537. substring('foobar', 3, 3) == 'bar'
  538. substring('foobar', 3, all) == 'bar'
  539. substring('foobar', 1, 4) == 'ooba'
  540. substring('foobar', -5, 4) == 'ooba'
  541. substring('foobar', -1, -3) == 'oba'
  542. substring('foobar', 4, -2) == 'ob'
  543. substring('foobar', 10, 2) == ''
  544. </screen>
  545. </section>
  546. <section>
  547. <title>Concat</title>
  548. The concat function "concat(string1, string2)" returns the
  549. concatenation of its two arguments. For instance:
  550. <screen>
  551. concat('foo', 'bar') == 'foobar'
  552. </screen>
  553. </section>
  554. </section>
  555. <note>
  556. <para>
  557. The expression for each class is executed on each packet received.
  558. If the expressions are overly complex, the time taken to execute
  559. them may impact the performance of the server. If you need
  560. complex or time consuming expressions you should write a <link
  561. linkend='hooks-libraries'>hook</link> to perform the necessary work.
  562. </para> </note>
  563. <section id="classification-configuring">
  564. <title>Configuring Classes</title>
  565. <para>
  566. A class contains three items: a name, a test expression and option data.
  567. The name must exist and must be unique amongst all classes. The test
  568. expression and option data are optional.
  569. </para>
  570. <para>
  571. The test expression is a string containing the logical expression used to
  572. determine membership in the class. The entire expression is in double
  573. quotes.
  574. </para>
  575. <para>
  576. The option data is a list which defines any options that should be assigned
  577. to members of this class.
  578. </para>
  579. <para>
  580. In the following example the class named &quot;Client_foo&quot; is defined.
  581. It is comprised of all clients who's client ids (option 61) start with the
  582. string &quot;foo&quot;. Members of this class will be given 192.0.2.1 and
  583. 192.0.2.2 as their domain name servers.
  584. <screen>
  585. "Dhcp4": {
  586. "client-classes": [<userinput>
  587. {
  588. "name": "Client_foo",
  589. "test": "substring(option[61].hex,0,3) == 'foo'",
  590. "option-data": [
  591. {
  592. "name": "domain-name-servers",
  593. "code": 6,
  594. "space": "dhcp4",
  595. "csv-format": true,
  596. "data": "192.0.2.1, 192.0.2.2"
  597. }
  598. ]
  599. },
  600. ...
  601. ],</userinput>
  602. ...
  603. }</screen>
  604. </para>
  605. <para>
  606. This example shows a client class being defined for use by the DHCPv6 server.
  607. In it the class named &quot;Client_enterprise&quot; is defined. It is comprised
  608. of all clients who's client identifiers start with the given hex string (which
  609. would indicate a DUID based on an enterprise id of 0xAABBCCDD). Members of this
  610. class will be given an 2001:db8:0::1 and 2001:db8:2::1 as their domain name servers.
  611. <screen>
  612. "Dhcp6": {
  613. "client-classes": [<userinput>
  614. {
  615. "name": "Client_enterprise",
  616. "test": "substring(option[1].hex,0,6) == 0x0002AABBCCDD'",
  617. "option-data": [
  618. {
  619. "name": "dns-servers",
  620. "code": 23,
  621. "space": "dhcp6",
  622. "csv-format": true,
  623. "data": "2001:db8:0::1, 2001:db8:2::1"
  624. }
  625. ]
  626. },
  627. ...
  628. ],</userinput>
  629. ...
  630. }</screen>
  631. </para>
  632. </section>
  633. <section id="classification-subnets">
  634. <title>Configuring Subnets With Class Information</title>
  635. <para>
  636. In certain cases it beneficial to restrict access to certain subnets
  637. only to clients that belong to a given class using the "client-class"
  638. keyword when defining the subnet.
  639. </para>
  640. <para>
  641. Let's assume that the server is connected to a network segment that uses
  642. the 192.0.2.0/24 prefix. The Administrator of that network has decided
  643. that addresses from range 192.0.2.10 to 192.0.2.20 are going to be
  644. managed by the DHCP4 server. Only clients belonging to client class
  645. Client_foo are allowed to use this subnet. Such a
  646. configuration can be achieved in the following way:
  647. <screen>
  648. "Dhcp4": {
  649. "client-classes": [
  650. {
  651. "name": "Client_foo",
  652. "test": "substring(option[61].hex,0,3) == 'foo'",
  653. "option-data": [
  654. {
  655. "name": "domain-name-servers",
  656. "code": 6,
  657. "space": "dhcp4",
  658. "csv-format": true,
  659. "data": "192.0.2.1, 192.0.2.2"
  660. }
  661. ]
  662. },
  663. ...
  664. ],<userinput>
  665. "subnet4": [
  666. {
  667. "subnet": "192.0.2.0/24",
  668. "pools": [ { "pool": "192.0.2.10 - 192.0.2.20" } ],
  669. "client-class": "Client_foo"
  670. },
  671. ...
  672. ],</userinput>,
  673. ...
  674. }</screen>
  675. </para>
  676. <para>
  677. The following example shows restricting access to a DHCPv6 subnet. This
  678. configuration will restrict use of the addresses 2001:db8:1::1 to
  679. 2001:db8:1::FFFF to members of the "Client_enterprise" class.
  680. <screen>
  681. "Dhcp6": {
  682. "client-classes": [
  683. {
  684. "name": "Client_enterprise",
  685. "test": "substring(option[1].hex,0,6) == 0x0002AABBCCDD'",
  686. "option-data": [
  687. {
  688. "name": "dns-servers",
  689. "code": 23,
  690. "space": "dhcp6",
  691. "csv-format": true,
  692. "data": "2001:db8:0::1, 2001:db8:2::1"
  693. }
  694. ]
  695. },
  696. ...
  697. ], <userinput>
  698. "subnet6": [
  699. {
  700. "subnet": "2001:db8:1::/64",
  701. "pools": [ { "pool": "2001:db8:1::-2001:db8:1::ffff" } ],
  702. "client-class": "Client_enterprise"
  703. }
  704. ],</userinput>
  705. ...
  706. }</screen>
  707. </para>
  708. </section>
  709. <section>
  710. <title>Using Classes</title>
  711. <para>
  712. Currently classes can be used for two functions. They can supply options
  713. to the members of the class and they can be used to choose a subnet from which an
  714. address will be assigned to the class member.
  715. </para>
  716. <para>
  717. When supplying options, options defined as part of the class definition
  718. are considered &quot;class globals&quot;. They will override any global options that
  719. may be defined and in turn will be overridden by any options defined for an
  720. individual subnet.
  721. </para>
  722. </section>
  723. <section>
  724. <title>Classes and Hooks</title>
  725. <para>
  726. You may use a hook to classify your packets. This may be useful if the
  727. expression would either be complex or time consuming and be easier or
  728. better to write as code. Once the hook has added the proper class name
  729. to the packet the rest of the classification system will work as normal
  730. in choosing a subnet and selecting options. For a description of the
  731. hooks see <xref linkend="hooks-libraries"/>, for a description on
  732. configuring he classes see <xref linkend="classification-configuring"/>
  733. and <xref linkend="classification-subnets"/>.
  734. </para>
  735. </section>
  736. <section>
  737. <title>Debugging Expressions</title>
  738. <para>
  739. While you are constructing your classification expressions you may
  740. find it useful to enable logging see <xref linkend="logging"/> for
  741. a more complete description of the logging facility.
  742. </para>
  743. <para>
  744. To enable the debug statements in the classifciaton system you will
  745. need to set the severity to "DEBUG" and the debug level to at least 55.
  746. The specific loggers are "kea-dhcp4.eval" and "kea-dhcp6.eval".
  747. </para>
  748. <para>
  749. In order to understand the logging statements one must understand a
  750. bit about how expressions are evaluated, for a more complete description
  751. refer to the design document at <ulink url="http://kea.isc.org/wiki/KeaDesigns"/>.
  752. In brief there are two structures used during the evaluation of an expression:
  753. a list of tokens which represent the expressions and a value stack which
  754. represents the values being manipulated.
  755. </para>
  756. <para>
  757. The list of tokens is created when the configuration file is processed with
  758. most expressions and values being converted to a token. The list is organized
  759. in reverse Polish notation. During execution the list will be traversed
  760. in order. As each token is executed it will be able to pop values
  761. from the top of the stack and eventually push its result on the top of the
  762. stack. Imagine the following expression:
  763. <screen>
  764. "test": "substring(option[61].hex,0,3) == 'foo'",
  765. </screen>
  766. This will result in the following tokens:
  767. <screen>
  768. option, number (0), number (3), substring, text ('foo'), equals
  769. </screen>
  770. In this example the first three tokens will simply push values onto the
  771. stack. The substring token will then remove those three values and
  772. compute a result that it places on the stack. The text option also
  773. places a value on the stack and finally the equals token removes the
  774. two tokens on the stack and places its result on the stack.
  775. </para>
  776. <para>
  777. When debug logging is enabled each time a token is evaluated it will
  778. emit a log line indicating the values of any objects that were popped
  779. off of the value stack and any objects that were pushed onto the value
  780. stack.
  781. </para>
  782. <para>
  783. The values will be displayed as either text if the command is known
  784. to use text values or hex if the command either uses binary values or
  785. can manipulate either text or binary values. For expressions that
  786. pop multiple values off the stack the values will be displayed in
  787. the order they were popped. For most expressions this won't matter
  788. but for the concat expression the values are displayed in reverse
  789. order from how they are written in the expression.
  790. </para>
  791. <para>
  792. Let us assume that the following test has been entered into the configuration.
  793. This example skips most of the configuration to concentrate on the test.
  794. <screen>
  795. "test": "substring(option[61].hex,0,3) == 'foo'",
  796. </screen>
  797. The logging might then resemble this:
  798. <screen>
  799. 2016-05-19 13:35:04.163 DEBUG [kea.eval/44478] EVAL_DEBUG_OPTION Pushing option 61 with value 0x666F6F626172
  800. 2016-05-19 13:35:04.164 DEBUG [kea.eval/44478] EVAL_DEBUG_STRING Pushing text string '0'
  801. 2016-05-19 13:35:04.165 DEBUG [kea.eval/44478] EVAL_DEBUG_STRING Pushing text string '3'
  802. 2016-05-19 13:35:04.166 DEBUG [kea.eval/44478] EVAL_DEBUG_SUBSTRING Popping length 3, start 0,
  803. string 0x666F6F626172 pushing result 0x666F6F
  804. 2016-05-19 13:35:04.167 DEBUG [kea.eval/44478] EVAL_DEBUG_STRING Pushing text string 'foo'
  805. 2016-05-19 13:35:04.168 DEBUG [kea.eval/44478] EVAL_DEBUG_EQUAL Popping 0x666F6F and 0x666F6F pushing result 'true'
  806. </screen>
  807. </para>
  808. <note><para>
  809. The debug logging may be quite verbose if you have a number of expressions
  810. to evaluate. It is intended as an aide in helping you create and debug
  811. your expressions. You should plan to disable debug logging when you have your
  812. expressions working correctly. You also may wish to include only one set of
  813. expressions at a time in the configuration file while debugging them in order
  814. to limit the log statements. For example when adding a new set of expressions
  815. you might find it more convenient to create a configuration file that only
  816. includes the new expressions until you have them working correctly and then
  817. add the new set to the main configuration file.
  818. </para></note>
  819. </section>
  820. </chapter>