classify.xml 39 KB

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