hooks.xml 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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="hooks-libraries">
  7. <title>Hooks Libraries</title>
  8. <section id="hooks-libraries-introduction">
  9. <title>Introduction</title>
  10. <para>
  11. Although Kea offers a lot of flexibility, there may be cases where
  12. its behavior needs customisation. To accommodate this possibility,
  13. Kea includes the idea of "Hooks". This feature lets Kea load one
  14. or more dynamically-linked libraries (known as "hooks libraries")
  15. and, at various points in its processing ("hook points"), call
  16. functions in them. Those functions perform whatever custom
  17. processing is required.
  18. </para>
  19. <para>
  20. Hooks libraries are attached to individual Kea processes, not to
  21. Kea as a whole. This means (for example) that it is possible
  22. to associate one set of libraries with the DHCP4 server and a
  23. different set to the DHCP6 server.
  24. </para>
  25. <para>
  26. Another point to note is that it is possible for a process to
  27. load multiple libraries. When processing reaches a hook point,
  28. Kea calls the hooks library functions attached to it. If multiple
  29. libraries have attached a function to a given hook point, Kea calls
  30. all of them, in the order in which the libraries are specified in
  31. the configuration file. The order may be important: consult the
  32. documentation of the libraries to see if this is the case.
  33. </para>
  34. <para>
  35. The next section describes how to configure hooks libraries. If you
  36. are interested in writing your own hooks library, information can be
  37. found in the <ulink url="https://jenkins.isc.org/job/Fedora20_32_doxygen_doc/doxygen/">Kea
  38. Developer's Guide</ulink>.
  39. </para>
  40. </section> <!-- end Introduction -->
  41. <section>
  42. <title>Configuring Hooks Libraries</title>
  43. <para>
  44. The hooks libraries for a given process are configured using the
  45. <command>hooks-libraries</command> keyword in the
  46. configuration for that process. (Note that
  47. the word "hooks" is plural). The value of the keyword
  48. is an array of map structures, each structure corresponding to a hooks
  49. library. For example, to set up two hooks libraries for the DHCPv4
  50. server, the configuration would be:
  51. <screen>
  52. <userinput>"Dhcp4": {
  53. :
  54. "hooks-libraries": [
  55. {
  56. "library": "/opt/charging.so"
  57. },
  58. {
  59. "library": "/opt/local/notification.so",
  60. "parameters": {
  61. "mail": "spam@example.com",
  62. "floor": 13,
  63. "debug": false,
  64. "users": [ "alice", "bob", "charlie" ],
  65. "languages": {
  66. "french": "bonjour",
  67. "klingon": "yl'el"
  68. }
  69. }
  70. }
  71. ]
  72. :
  73. }</userinput>
  74. </screen>
  75. </para>
  76. <note><para>
  77. This is a change to the syntax used in Kea 0.9.2 and earlier, where
  78. hooks-libraries was a list of strings, each string being the name of
  79. a library. The change was made in Kea 1.0 to facilitate the
  80. specification of library-specific parameters, a capability
  81. available in Kea 1.1.0 onwards.
  82. </para></note>
  83. <note>
  84. <para>
  85. The library reloading behavior has changed in Kea 1.1. Libraries are
  86. reloaded, even if their list hasn't changed. Kea does that, because
  87. the parameters specified for the library (or the files those
  88. parameters point to) may have changed.
  89. </para>
  90. </note>
  91. <para>
  92. Libraries may have additional parameters. Those are not mandatory in the
  93. sense that there may be libraries that don't require them. However, for
  94. specific library there is often specific requirement for specify certain
  95. set of parameters. Please consult the documentation for your library
  96. for details. In the example above, the first library has no parameters.
  97. The second library has five parameters, specifying mail (string
  98. parameter), floor (integer parameter), debug (boolean parameter) and
  99. even lists (list of strings) and maps (containing strings). Nested
  100. parameters could be used if the library supports it. This topic is
  101. explained in detail in the Hooks Developer's Guide in the "Configuring
  102. Hooks Libraries" section.
  103. </para>
  104. <para>
  105. Notes:
  106. <itemizedlist mark='bullet'>
  107. <listitem><para>
  108. The full path to each library should be given.
  109. </para></listitem>
  110. <listitem><para>
  111. As noted above, order may be important - consult the documentation for
  112. each library.
  113. </para></listitem>
  114. <listitem><para>
  115. An empty list has the same effect as omitting the
  116. <command>hooks-libraries</command> configuration element all together.
  117. </para>
  118. <note><para>
  119. There is one case where this is not true: if Kea
  120. is running with a configuration that contains a
  121. <command>hooks-libraries</command> item, and that item is
  122. removed and the configuration reloaded, the removal will be
  123. ignored and the libraries remain loaded. As a workaround,
  124. instead of removing the <command>hooks-libraries</command>
  125. item, change it to an empty list. This will be fixed in a
  126. future version of Kea.
  127. </para></note>
  128. </listitem>
  129. </itemizedlist>
  130. </para>
  131. <para>
  132. At the present time, only the kea-dhcp4 and kea-dhcp6 processes support
  133. hooks libraries.
  134. </para>
  135. </section>
  136. <section>
  137. <title>Available Hooks Libraries</title>
  138. <para>
  139. As described above, the hooks functionality provides a way to customize
  140. a Kea server without modifying the core code. ISC has chosen to take
  141. advantage of this feature to provide functions that may only be useful
  142. to a subset of Kea users. To this end ISC has created some hooks
  143. libraries; these discussed in the following sections.
  144. </para>
  145. <note><para>
  146. Some of these libraries will be available with the base code while others
  147. will be shared with organizations supporting development of Kea
  148. , possibly as a 'benefit' or 'thank you' for helping to sustain
  149. the larger Kea project. If you would like to get access to those
  150. libraries, please consider taking out a support contract: this includes
  151. professional support, advance security notifications, input into our
  152. roadmap planning, and many other benefits, while helping
  153. making Kea sustainable in the long term.
  154. </para></note>
  155. <para>Currently the following libraries are available or planned from ISC:
  156. <table frame="all" id="hook-libs">
  157. <title>List of available hooks libraries</title>
  158. <tgroup cols='3'>
  159. <colspec colname='name' />
  160. <colspec colname='avail' />
  161. <colspec colname='description' />
  162. <thead>
  163. <row>
  164. <entry>Name</entry>
  165. <entry>Availability</entry>
  166. <entry>Since</entry>
  167. <entry>Description</entry>
  168. </row>
  169. </thead>
  170. <tbody>
  171. <row>
  172. <entry>user_chk</entry>
  173. <entry>Kea sources</entry>
  174. <entry>Kea 0.8</entry>
  175. <entry>Reads known users list from a file. Unknown users
  176. will be assigned a
  177. lease from the last subnet defined in the configuration file,
  178. e.g. to redirect them a captive portal. This demonstrates how an
  179. external source of information can be used to influence the Kea
  180. allocation engine. This hook is part of the Kea source code and is
  181. available in the src/hooks/dhcp/user_chk directory.</entry>
  182. </row>
  183. <row>
  184. <entry>Forensic Logging</entry>
  185. <entry>Support customers</entry>
  186. <entry>Kea 1.1.0</entry>
  187. <entry>This library provides hooks that record a detailed log of
  188. lease assignments and renewals into a set of log files. In many
  189. legal jurisdictions companies, especially ISPs, must record
  190. information about the addresses they have leased to DHCP
  191. clients. This library is designed to help with that
  192. requirement. If the information that it records is sufficient it
  193. may be used directly. If your jurisdiction requires that you save
  194. a different set of information, you may use it as a template or
  195. example and create your own custom logging hooks.</entry>
  196. </row>
  197. <row>
  198. <entry>Lightweight 4over6</entry>
  199. <entry>Support customers</entry>
  200. <entry>Autumn 2016</entry>
  201. <entry>Lightweight 4over6
  202. (<ulink url="http://tools.ietf.org/html/rfc7596">RFC 7596</ulink>)
  203. is a new IPv6 transition
  204. technology that provides IPv4 as a service in IPv6-only
  205. network. It assumes that dual-stack clients will get a regular IPv6
  206. address and IPv6 prefix, but only a fraction of an IPv4 address. The
  207. fraction is specified as port-set, which is essentially a range of
  208. TCP and UDP ports a client can use. By doing the transition on the
  209. client side, this technology eliminates the need to deploy
  210. expensive Carrier Grade NATs within the operator's network. The
  211. problem on the DHCP side is the non-trivial logic behind it: each
  212. client needs to receive an unique set of lightweight 4over6 options
  213. (<ulink url="http://tools.ietf.org/html/rfc7598">RFC 7598</ulink>),
  214. that include the IPv4 address (shared among several
  215. clients), port-set (which is unique among clients sharing the same
  216. IPv4 address) and a number of additional parameters. This hooks
  217. library will generate values of those options dynamically, thus
  218. eliminating the need to manually configure values for each client
  219. separately.
  220. </entry>
  221. </row>
  222. </tbody>
  223. </tgroup>
  224. </table>
  225. </para>
  226. <para>
  227. ISC hopes to see more hooks libraries become available as time
  228. progresses, both developed internally and externally. Since
  229. this list may evolve dynamically, we decided to keep it on a
  230. wiki page, available at this link: <ulink
  231. url="http://kea.isc.org/wiki/Hooks">http://kea.isc.org/wiki/Hooks</ulink>.
  232. If you are a developer or are aware of any hooks libraries not
  233. listed there, please send a note to the kea-users or kea-dev
  234. mailing lists and someone will update it.
  235. </para>
  236. <section>
  237. <title>user_chk: Checking User Access</title>
  238. <para>
  239. The user_chk library is the first hooks library published by ISC. It
  240. attempts to serve several purposes:
  241. <itemizedlist>
  242. <listitem>
  243. <para>To assign "new" or "unregistered" users to a
  244. restricted subnet, while "known" or "registered" users are assigned
  245. to unrestricted subnets.</para>
  246. </listitem>
  247. <listitem>
  248. <para>To allow DHCP response options or vendor option
  249. values to be customized based upon user identity. </para>
  250. </listitem>
  251. <listitem>
  252. <para>To provide a real time record of the user registration
  253. activity which can be sampled by an external consumer.</para>
  254. </listitem>
  255. <listitem>
  256. <para> To serve as a demonstration of various capabilities
  257. possible using the hooks interface.</para>
  258. </listitem>
  259. </itemizedlist>
  260. </para>
  261. <para>
  262. Once loaded, the library allows segregating incomings requests into
  263. known and unknown clients. For known clients, the packets are
  264. processed mostly as usual, except it is possible to override certain
  265. options being sent. That can be done on a per host basis. Clients
  266. that are not on the known hosts list will be treated as unknown and
  267. will be assigned to the last subnet defined in the configuration file.
  268. </para>
  269. <para>
  270. As an example of use, this behavior may be used to put unknown users into a
  271. separate subnet that leads to a walled garden, where they can only
  272. access a registration portal. Once they fill in necessary data, their
  273. details are added to the known clients file and they get a proper
  274. address after their device is restarted.
  275. </para>
  276. <note><para>This library was developed several years before the host
  277. reservation mechanism has become available. Currently host reservation is
  278. much more
  279. powerful and flexible, but nevertheless the user_chk capability to consult
  280. and external source of information about clients and alter Kea's
  281. behavior is useful and remains of educational value.
  282. </para></note>
  283. <para>
  284. The library reads the /tmp/user_chk_registry.txt file while being
  285. loaded and each time an incoming packet is processed. The file is expected
  286. to have each line contain a self-contained JSON snippet which must
  287. have the following two entries:
  288. <itemizedlist>
  289. <listitem><para><command>type</command>, whose value
  290. is "HW_ADDR" for IPv4 users or "DUID" for IPv6
  291. users</para></listitem>
  292. <listitem><para><command>id</command>, whose value is
  293. either the hardware address or the DUID from the equest
  294. formatted as a string of hex digits, with or without
  295. ":" delimiters.</para></listitem>
  296. </itemizedlist>
  297. and may have the zero or more of the following entries:
  298. <itemizedlist>
  299. <listitem><para><command>bootfile</command> whose value
  300. is the pathname of the desired file</para></listitem>
  301. <listitem><para><command>tftp_server</command> whose
  302. value is the hostname or IP address of the desired
  303. server</para></listitem>
  304. </itemizedlist>
  305. A sample user registry file is shown below:
  306. <screen>{ "type" : "HW_ADDR", "id" : "0c:0e:0a:01:ff:04", "bootfile" : "/tmp/v4bootfile" }
  307. { "type" : "HW_ADDR", "id" : "0c:0e:0a:01:ff:06", "tftp_server" : "tftp.v4.example.com" }
  308. { "type" : "DUID", "id" : "00:01:00:01:19:ef:e6:3b:00:0c:01:02:03:04", "bootfile" : "/tmp/v6bootfile" }
  309. { "type" : "DUID", "id" : "00:01:00:01:19:ef:e6:3b:00:0c:01:02:03:06", "tftp_server" : "tftp.v6.example.com" }</screen>
  310. </para>
  311. <para>As with any other hooks libraries provided by ISC, internals of the
  312. user_chk code are well documented. You can take a look at the <ulink
  313. url="https://jenkins.isc.org/job/Fedora20_32_doxygen_doc/doxygen/d8/db2/libdhcp_user_chk.html">Kea Developer's Guide section dedicated to the user_chk library</ulink>
  314. that discusses how the code works internally. That, together with
  315. our general entries in <ulink
  316. url="https://jenkins.isc.org/job/Fedora20_32_doxygen_doc/doxygen/">Hooks
  317. Framework section</ulink> should give you some pointers how to extend
  318. this library and perhaps even write your own from scratch.</para>
  319. </section>
  320. <section>
  321. <title>Forensic Logging Hooks</title>
  322. <para>
  323. This section describes the forensic log hooks library. This library
  324. povides hooks that record a detailed log of lease assignments
  325. and renewals into a set of log files. Currently this library
  326. is only available to ISC customers with a support contract.
  327. </para>
  328. <para>
  329. In many legal jurisdictions companies, especially ISPs, must record
  330. information about the addresses they have leased to DHCP clients.
  331. This library is designed to help with that requirement. If the
  332. information that it records is sufficient it may be used directly.
  333. If your jurisdiction requires that you save a different set of
  334. information you may use it as a template or example and create your
  335. own custom logging hooks.
  336. </para>
  337. <para>
  338. This logging is done as a set of hooks to allow it to be customized
  339. to any particular need. Modifying a hooks library is easier and
  340. safer than updating the core code. In addition by using the hooks
  341. features those users who don't need to log this information can
  342. leave it out and avoid any performance penalties.
  343. </para>
  344. <section>
  345. <title>Log File Naming</title>
  346. <para>
  347. The names for the log files have the following form:
  348. </para>
  349. <screen>
  350. path/base-name.CCYYMMDD.txt
  351. </screen>
  352. <para>
  353. The &quot;path&quot; and &quot;base-name&quot; are supplied in the
  354. configuration as described below see
  355. <xref linkend="forensic-log-configuration"/>. The next part of the name is
  356. the date the log file was started, with four digits for year, two digits
  357. for month and two digits for day. The file is rotated on a daily basis.
  358. </para>
  359. <note><para>
  360. When running Kea servers for both DHCPv4 and DHCPv6 the log names must
  361. be distinct. See the examples in <xref linkend="forensic-log-configuration"/>.
  362. </para></note>
  363. </section>
  364. <section>
  365. <title>DHCPv4 Log Entries</title>
  366. <para>
  367. For DHCPv4 the library creates entries based on DHCPREQUEST messages
  368. and corresponding DHCPv4 leases intercepted by lease4_select
  369. (for new leases) and lease4_renew (for renewed leases) hooks.
  370. </para>
  371. <para>
  372. An entry is a single string with no embedded end-of-line markers
  373. and has the following sections:
  374. <screen>
  375. address duration device-id {client-info} {relay-info}
  376. </screen>
  377. </para>
  378. <para>
  379. Where:
  380. <itemizedlist>
  381. <listitem><para>
  382. address - the leased IPv4 address given out and whether it was
  383. assigned or renewed.
  384. </para></listitem>
  385. <listitem><para>
  386. duration - the lease lifetime expressed in days (if present),
  387. hours, minutes and seconds. A lease lifetime of 0xFFFFFFFF will be
  388. denoted with the text &quot;infinite duration&quot;.
  389. </para></listitem>
  390. <listitem><para>
  391. device-id - the client's hardware address shown as numerical type
  392. and hex digit string.
  393. </para></listitem>
  394. <listitem><para>
  395. client-info - the DHCP client id option (61) if present, shown as
  396. a hex string.
  397. </para></listitem>
  398. <listitem><para>
  399. relay-info - for relayed packets the giaddr and the RAI circuit id
  400. and remote id options (option 82 sub options 1 and 2) if present.
  401. The circuit id and remote id are presented as hex strings
  402. </para></listitem>
  403. </itemizedlist>
  404. </para>
  405. <para>
  406. For instance (line breaks added for readability, they would not
  407. be present in the log file).
  408. <screen>
  409. Address: 192.2.1.100 has been renewed for 1 hrs 52 min 15 secs to a device with
  410. hardware address: hwtype=1 08:00:2b:02:3f:4e, client-id: 17:34:e2:ff:09:92:54
  411. connected via relay at address: 192.2.16.33, identified by circuit-id:
  412. 68:6f:77:64:79 and remote-id: 87:f6:79:77:ef
  413. </screen>
  414. </para>
  415. </section>
  416. <section>
  417. <title>DHCPv6 Log Entries</title>
  418. <para>
  419. For DHCPv6 the library creates entries based on lease management
  420. actions intercepted by the lease6_select (for new leases), lease6_renew
  421. (for renewed leases) and lease6_rebind (for rebound leases).
  422. </para>
  423. <para>
  424. An entry is a single string with no embedded end-of-line markers
  425. and has the following sections:
  426. <screen>
  427. address duration device-id {relay-info}*
  428. </screen>
  429. </para>
  430. <para>
  431. Where:
  432. <itemizedlist>
  433. <listitem><para>
  434. address - the leased IPv6 address or prefix given out and whether
  435. it was assigned or renewed.
  436. </para></listitem>
  437. <listitem><para>
  438. duration - the lease lifetime expressed in days (if present),
  439. hours, minutes and seconds. A lease lifetime of 0xFFFFFFFF will be
  440. denoted with the text "infinite duration".
  441. </para></listitem>
  442. <listitem><para>
  443. device-id - the client's DUID and hardware address (if present).
  444. </para></listitem>
  445. <listitem><para>
  446. relay-info - for relayed packets the content of relay agent
  447. messages, remote id and subscriber id options (x and xx) if present.
  448. </para></listitem>
  449. </itemizedlist>
  450. </para>
  451. <para>
  452. For instance (line breaks added for readability, they would not
  453. be present in the log file).
  454. <screen>
  455. Address:2001:db8:1:: has been assigned for 0 hrs 11 mins 53 secs to a device with
  456. DUID: 17:34:e2:ff:09:92:54 and hardware address: hwtype=1 08:00:2b:02:3f:4e
  457. (from Raw Socket) connected via relay at address: fe80::abcd for client on
  458. link address: 3001::1, hop count: 1, identified by remote-id:
  459. 01:02:03:04:0a:0b:0c:0d:0e:0f and subscriber-id: 1a:2b:3c:4d:5e:6f
  460. </screen>
  461. </para>
  462. </section>
  463. <section id="forensic-log-configuration">
  464. <title>Configuring the Forensic Log Hooks</title>
  465. <para>
  466. To use this functionality the hook library must be included in the
  467. configuration of the desired DHCP server modules. The legal_log
  468. library is installed alongside the Kea libraries in
  469. <filename>[kea-install-dir]/lib</filename> where
  470. <filename>kea-install-dir</filename> is determined by the
  471. &quot;--prefix&quot; option of the configure script. It defaults to
  472. <filename>/usr/local</filename>. Assuming the
  473. default value then, configuring kea-dhcp4 to load the legal_log
  474. library could be done with the following Kea4 configuration:
  475. <screen>
  476. "Dhcp4": { <userinput>
  477. "hooks-libraries": [
  478. {
  479. "library": "/usr/local/lib/libdhcp_legal_log.so",
  480. "parameters": {
  481. "path": "/var/kea/var",
  482. "base-name": "kea-forensic4"
  483. }
  484. },
  485. ...
  486. ] </userinput>
  487. }
  488. </screen>
  489. </para>
  490. <para>
  491. To configure it for kea-dhcp6, the commands are simply as shown below:
  492. <screen>
  493. "Dhcp6": { <userinput>
  494. "hooks-libraries": [
  495. {
  496. "library": "/usr/local/lib/libdhcp_legal_log.so",
  497. "parameters": {
  498. "path": "/var/kea/var",
  499. "base-name": "kea-forensic6"
  500. }
  501. },
  502. ...
  503. ] </userinput>
  504. }
  505. </screen>
  506. </para>
  507. <para>
  508. Two Hook Library parameters are supported:
  509. <itemizedlist>
  510. <listitem><para>
  511. path - the directory in which the forensic file(s) will be written. The
  512. default value is
  513. <filename>[prefix]/kea/var</filename>. The directory must exist.
  514. </para></listitem>
  515. <listitem><para>
  516. base-name - an arbitrary value which is used in conjunction with
  517. the current system date to form the current foresnic file name. It defaults
  518. to <filename>kea-legal</filename>.
  519. </para></listitem>
  520. </itemizedlist>
  521. </para>
  522. </section>
  523. </section>
  524. </section>
  525. <section id="user-context">
  526. <title>User contexts</title>
  527. <para>Hook libraries can have their own configuration parameters. That is
  528. convenient if the parameter applies to the whole library. However,
  529. sometimes it is very useful if certain configuration entities are extended
  530. with additional configuration data. This is where the concept of user
  531. contexts comes in. A sysadmin can define an arbitrary set of data and
  532. attach it to Kea structures, as long as the data is specified as JSON map.
  533. In particular, it is possible to define fields that are integers, strings,
  534. boolean, lists and maps. It is possible to define nested structures of
  535. arbitrary complexity. Kea does not use that data on its own, simply stores
  536. and makes it available for the hook libraries.
  537. </para>
  538. <para>
  539. As of Kea 1.2, the only structures that allow user contexts are address
  540. and prefix pools, but it is expected to extend other structures with the
  541. user context capability.
  542. </para>
  543. </section>
  544. </chapter> <!-- hooks-libraries -->