4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2013 Joyent, Inc. All rights reserved.
26 * Copyright (c) 2016 by Delphix. All rights reserved.
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/errno.h>
33 #include <sys/mutex.h>
34 #include <sys/condvar.h>
35 #include <sys/modctl.h>
36 #include <sys/hook_impl.h>
38 #include <sys/cmn_err.h>
41 * This file provides kernel hook framework.
44 static struct modldrv modlmisc
= {
45 &mod_miscops
, /* drv_modops */
46 "Hooks Interface v1.0", /* drv_linkinfo */
49 static struct modlinkage modlinkage
= {
50 MODREV_1
, /* ml_rev */
51 &modlmisc
, /* ml_linkage */
55 static const char *hook_hintvalue_none
= "<none>";
60 * Use of the hook framework here is tied up with zones - when a new zone
61 * is created, we create a new hook_stack_t and are open to business for
62 * allowing new hook families and their events.
64 * A consumer of these hooks is expected to operate in this fashion:
65 * 1) call hook_family_add() to create a new family of hooks. It is a
66 * current requirement that this call must be made with the value
67 * returned from hook_stack_init, by way of infrastructure elsewhere.
68 * 2) add events to the registered family with calls to hook_event_add.
70 * At this point, the structures in place should be open to others to
71 * add hooks to the event or add notifiers for when the contents of the
74 * The interesting stuff happens on teardown.
76 * It is a requirement that the provider of hook events work in the reverse
77 * order to the above, so that the first step is:
78 * 1) remove events from each hook family created earlier
79 * 2) remove hook families from the hook stack.
81 * When doing teardown of both events and families, a check is made to see
82 * if either structure is still "busy". If so then a boolean flag (FWF_DESTROY)
83 * is set to say that the structure is condemned. The presence of this flag
84 * being set must be checked for in _add()/_register()/ functions and a
85 * failure returned if it is set. It is ignored by the _find() functions
86 * because they're used by _remove()/_unregister().
87 * While setting the condemned flag when trying to delete a structure would
88 * normally be keyed from the presence of a reference count being greater
89 * than 1, in this implementation there are no reference counts required:
90 * instead the presence of objects on linked lists is taken to mean
91 * something is still "busy."
93 * ONLY the caller that adds the family and the events ever has a direct
94 * reference to the internal structures and thus ONLY it should be doing
95 * the removal of either the event or family. In practise, what this means
96 * is that in ip_netinfo.c, we have calls to net_protocol_register(), followed
97 * by net_event_register() (these interface to hook_family_add() and
98 * hook_event_add(), respectively) that are made when we create an instance
99 * of IP and when the IP instance is shutdown/destroyed, it calls
100 * net_event_unregister() and net_protocol_unregister(), which in turn call
101 * hook_event_remove() and hook_family_remove() respectively. Nobody else
102 * is entitled to call the _unregister() functions. It is imperative that
103 * there be only one _remove() call for every _add() call.
105 * It is possible that code which is interfacing with this hook framework
106 * won't do all the cleaning up that it needs to at the right time. While
107 * we can't prevent programmers from creating memory leaks, we can synchronise
108 * when we clean up data structures to prevent code accessing free'd memory.
110 * A simple diagram showing the ownership is as follows:
112 * Owned +--------------+
113 * by | hook_stack_t |
114 * the +--------------+
116 * - - - - - - - -|- - - - - - - - - - - - - - - - - -
118 * Owned +-------------------+ +-------------------+
119 * | hook_family_int_t |---->| hook_family_int_t |
120 * by +-------------------+ +-------------------+
121 * | \+---------------+ \+---------------+
122 * network | | hook_family_t | | hook_family_t |
123 * V +---------------+ +---------------+
124 * protocol +------------------+ +------------------+
125 * | hook_event_int_t |---->| hook_event_int_t |
126 * (ipv4,ipv6) +------------------+ +------------------+
127 * | \+--------------+ \+--------------+
128 * | | hook_event_t | | hook_event_t |
129 * | +--------------+ +--------------+
130 * - - - - - - - -|- - - - - - - - - - - - - - - - - -
132 * Owned +------------+
136 * the consumer | hook_t |
139 * The consumers, such as IPFilter, do not have any pointers or hold any
140 * references to hook_int_t, hook_event_t or hook_event_int_t. By placing
141 * a hook on an event through net_hook_register(), an implicit reference
142 * to the hook_event_int_t is returned with a successful call. Additionally,
143 * IPFilter does not see the hook_family_int_t or hook_family_t directly.
144 * Rather it is returned a net_handle_t (from net_protocol_lookup()) that
145 * contains a pointer to hook_family_int_t. The structure behind the
146 * net_handle_t (struct net_data) *is* reference counted and managed
149 * A more detailed picture that describes how the family/event structures
150 * are linked together can be found in <sys/hook_impl.h>
152 * Notification callbacks.
153 * =======================
154 * For each of the hook stack, hook family and hook event, it is possible
155 * to request notificatin of change to them. Why?
156 * First, lets equate the hook stack to an IP instance, a hook family to
157 * a network protocol and a hook event to IP packets on the input path.
158 * If a kernel module wants to apply security from the very start of
159 * things, it needs to know as soon as a new instance of networking
160 * is initiated. Whilst for the global zone, it is taken for granted that
161 * this instance will always exist before any interaction takes place,
162 * that is not true for zones running with an exclusive networking instance.
163 * Thus when a local zone is started and a new instance is created to support
164 * that, parties that wish to monitor it and apply a security policy from
165 * the onset need to be informed as early as possible - quite probably
166 * before any networking is started by the zone's boot scripts.
167 * Inside each instance, it is possible to have a number of network protocols
168 * (hook families) in operation. Inside the context of the global zone,
169 * it is possible to have code run before the kernel module providing the
170 * IP networking is loaded. From here, to apply the appropriate security,
171 * it is necessary to become informed of when IP is being configured into
172 * the zone and this is done by registering a notification callback with
173 * the hook stack for changes to it. The next step is to know when packets
174 * can be received through the physical_in, etc, events. This is achieved
175 * by registering a callback with the appropriate network protocol (or in
176 * this file, the correct hook family.) Thus when IP finally attaches a
177 * physical_in event to inet, the module looking to enforce a security
178 * policy can become aware of it being present. Of course there's no
179 * requirement for such a module to be present before all of the above
180 * happens and in such a case, it is reasonable for the same module to
181 * work after everything has been put in place. For this reason, when
182 * a notification callback is added, a series of fake callback events
183 * is generated to simulate the arrival of those entities. There is one
184 * final series of callbacks that can be registered - those to monitor
185 * actual hooks that are added or removed from an event. In practice,
186 * this is useful when there are multiple kernel modules participating
187 * in the processing of packets and there are behaviour dependencies
188 * involved, such that one kernel module might only register its hook
189 * if another is already present and also might want to remove its hook
190 * when the other disappears.
192 * If you know a kernel module will not be loaded before the infrastructure
193 * used in this file is present then it is not necessary to use this
194 * notification callback mechanism.
200 * The use of CVW_* macros to do locking is driven by the need to allow
201 * recursive locking with read locks when we're processing packets. This
202 * is necessary because various netinfo functions need to hold read locks,
203 * by design, as they can be called in or out of packet context.
206 * Hook internal functions
208 static hook_int_t
*hook_copy(hook_t
*src
);
209 static hook_event_int_t
*hook_event_checkdup(hook_event_t
*he
,
211 static hook_event_int_t
*hook_event_copy(hook_event_t
*src
);
212 static hook_event_int_t
*hook_event_find(hook_family_int_t
*hfi
, char *event
);
213 static void hook_event_free(hook_event_int_t
*hei
, hook_family_int_t
*hfi
);
214 static hook_family_int_t
*hook_family_copy(hook_family_t
*src
);
215 static hook_family_int_t
*hook_family_find(char *family
, hook_stack_t
*hks
);
216 static void hook_family_free(hook_family_int_t
*hfi
, hook_stack_t
*hks
);
217 static hook_int_t
*hook_find(hook_event_int_t
*hei
, hook_t
*h
);
218 static void hook_int_free(hook_int_t
*hi
, netstackid_t
);
219 static void hook_init(void);
220 static void hook_fini(void);
221 static void *hook_stack_init(netstackid_t stackid
, netstack_t
*ns
);
222 static void hook_stack_fini(netstackid_t stackid
, void *arg
);
223 static void hook_stack_shutdown(netstackid_t stackid
, void *arg
);
224 static int hook_insert(hook_int_head_t
*head
, hook_int_t
*new);
225 static void hook_insert_plain(hook_int_head_t
*head
, hook_int_t
*new);
226 static int hook_insert_afterbefore(hook_int_head_t
*head
, hook_int_t
*new);
227 static hook_int_t
*hook_find_byname(hook_int_head_t
*head
, char *name
);
228 static void hook_event_init_kstats(hook_family_int_t
*, hook_event_int_t
*);
229 static void hook_event_notify_run(hook_event_int_t
*, hook_family_int_t
*,
230 char *event
, char *name
, hook_notify_cmd_t cmd
);
231 static void hook_init_kstats(hook_family_int_t
*hfi
, hook_event_int_t
*hei
,
233 static int hook_notify_register(hook_notify_head_t
*head
,
234 hook_notify_fn_t callback
, void *arg
);
235 static int hook_notify_unregister(hook_notify_head_t
*head
,
236 hook_notify_fn_t callback
, void **);
237 static void hook_notify_run(hook_notify_head_t
*head
, char *family
,
238 char *event
, char *name
, hook_notify_cmd_t cmd
);
239 static void hook_stack_notify_run(hook_stack_t
*hks
, char *name
,
240 hook_notify_cmd_t cmd
);
241 static void hook_stack_remove(hook_stack_t
*hks
);
244 * A list of the hook stacks is kept here because we need to enable
245 * net_instance_notify_register() to be called during the creation
246 * of a new instance. Previously hook_stack_get() would just use
247 * the netstack functions for this work but they will return NULL
248 * until the zone has been fully initialised.
250 static hook_stack_head_t hook_stacks
;
251 static kmutex_t hook_stack_lock
;
254 * Module entry points.
262 error
= mod_install(&modlinkage
);
274 error
= mod_remove(&modlinkage
);
282 _info(struct modinfo
*modinfop
)
284 return (mod_info(&modlinkage
, modinfop
));
288 * Function: hook_init
297 mutex_init(&hook_stack_lock
, NULL
, MUTEX_DRIVER
, NULL
);
298 SLIST_INIT(&hook_stacks
);
301 * We want to be informed each time a stack is created or
302 * destroyed in the kernel.
304 netstack_register(NS_HOOK
, hook_stack_init
, hook_stack_shutdown
,
309 * Function: hook_fini
318 netstack_unregister(NS_HOOK
);
320 mutex_destroy(&hook_stack_lock
);
321 ASSERT(SLIST_EMPTY(&hook_stacks
));
325 * Function: hook_wait_setflag
326 * Returns: -1 = setting flag is disallowed, 0 = flag set and did
327 * not have to wait (ie no lock droped), 1 = flag set but
328 * it was necessary to drop locks to set it.
329 * Parameters: waiter(I) - control data structure
330 * busyset(I) - set of flags that we don't want set while
332 * wanted(I) - flag associated with newflag to indicate
333 * what we want to do.
334 * newflag(I) - the new ACTIVE flag we want to set that
335 * indicates what we are doing.
337 * The set of functions hook_wait_* implement an API that builds on top of
338 * the kcondvar_t to provide controlled execution through a critical region.
339 * For each flag that indicates work is being done (FWF_*_ACTIVE) there is
340 * also a flag that we set to indicate that we want to do it (FWF_*_WANTED).
341 * The combination of flags is required as when this function exits to do
342 * the task, the structure is then free for another caller to use and
343 * to indicate that it wants to do work. The flags used when a caller wants
344 * to destroy an object take precedence over those that are used for making
345 * changes to it (add/remove.) In this case, we don't try to secure the
346 * ability to run and return with an error.
348 * "wantedset" is used here to determine who has the right to clear the
349 * wanted bit from the fw_flags set: only whomever sets the flag has the
350 * right to clear it at the bottom of the loop, even if someone else
353 * wanted - the FWF_*_WANTED flag that describes the action being requested
354 * busyset- the set of FWF_* flags we don't want set when we run
355 * newflag- the FWF_*_ACTIVE flag we will set to indicate we are busy
358 hook_wait_setflag(flagwait_t
*waiter
, uint32_t busyset
, fwflag_t wanted
,
364 mutex_enter(&waiter
->fw_lock
);
365 if (waiter
->fw_flags
& FWF_DESTROY
) {
366 cv_signal(&waiter
->fw_cv
);
367 mutex_exit(&waiter
->fw_lock
);
370 while (waiter
->fw_flags
& busyset
) {
371 wantedset
= ((waiter
->fw_flags
& wanted
) == wanted
);
373 waiter
->fw_flags
|= wanted
;
374 CVW_EXIT_WRITE(waiter
->fw_owner
);
375 cv_wait(&waiter
->fw_cv
, &waiter
->fw_lock
);
377 * This lock needs to be dropped here to preserve the order
378 * of acquisition that is fw_owner followed by fw_lock, else
381 mutex_exit(&waiter
->fw_lock
);
383 CVW_ENTER_WRITE(waiter
->fw_owner
);
384 mutex_enter(&waiter
->fw_lock
);
386 waiter
->fw_flags
&= ~wanted
;
387 if (waiter
->fw_flags
& FWF_DESTROY
) {
388 cv_signal(&waiter
->fw_cv
);
389 mutex_exit(&waiter
->fw_lock
);
393 waiter
->fw_flags
&= ~wanted
;
394 ASSERT((waiter
->fw_flags
& wanted
) == 0);
395 ASSERT((waiter
->fw_flags
& newflag
) == 0);
396 waiter
->fw_flags
|= newflag
;
397 mutex_exit(&waiter
->fw_lock
);
402 * Function: hook_wait_unsetflag
404 * Parameters: waiter(I) - control data structure
405 * oldflag(I) - flag to reset
407 * Turn off the bit that we had set to run and let others know that
408 * they should now check to see if they can run.
411 hook_wait_unsetflag(flagwait_t
*waiter
, fwflag_t oldflag
)
413 mutex_enter(&waiter
->fw_lock
);
414 waiter
->fw_flags
&= ~oldflag
;
415 cv_signal(&waiter
->fw_cv
);
416 mutex_exit(&waiter
->fw_lock
);
420 * Function: hook_wait_destroy
422 * Parameters: waiter(I) - control data structure
424 * Since outer locking (on fw_owner) should ensure that only one function
425 * at a time gets to call hook_wait_destroy() on a given object, there is
426 * no need to guard against setting FWF_DESTROY_WANTED already being set.
427 * It is, however, necessary to wait for all activity on the owning
428 * structure to cease.
431 hook_wait_destroy(flagwait_t
*waiter
)
433 ASSERT((waiter
->fw_flags
& FWF_DESTROY_WANTED
) == 0);
434 mutex_enter(&waiter
->fw_lock
);
435 if (waiter
->fw_flags
& FWF_DESTROY_WANTED
) {
436 cv_signal(&waiter
->fw_cv
);
437 mutex_exit(&waiter
->fw_lock
);
438 return (EINPROGRESS
);
440 waiter
->fw_flags
|= FWF_DESTROY_WANTED
;
441 while (!FWF_DESTROY_OK(waiter
)) {
442 CVW_EXIT_WRITE(waiter
->fw_owner
);
443 cv_wait(&waiter
->fw_cv
, &waiter
->fw_lock
);
444 CVW_ENTER_WRITE(waiter
->fw_owner
);
447 * There should now be nothing else using "waiter" or its
448 * owner, so we can safely assign here without risk of wiiping
451 waiter
->fw_flags
= FWF_DESTROY_ACTIVE
;
452 cv_signal(&waiter
->fw_cv
);
453 mutex_exit(&waiter
->fw_lock
);
459 * Function: hook_wait_init
461 * Parameters: waiter(I) - control data structure
462 * ownder(I) - pointer to lock that the owner of this
465 * "owner" gets passed in here so that when we need to call cv_wait,
466 * for example in hook_wait_setflag(), we can drop the lock for the
467 * next layer out, which is likely to be held in an exclusive manner.
470 hook_wait_init(flagwait_t
*waiter
, cvwaitlock_t
*owner
)
472 cv_init(&waiter
->fw_cv
, NULL
, CV_DRIVER
, NULL
);
473 mutex_init(&waiter
->fw_lock
, NULL
, MUTEX_DRIVER
, NULL
);
474 waiter
->fw_flags
= FWF_NONE
;
475 waiter
->fw_owner
= owner
;
479 * Function: hook_stack_init
480 * Returns: void * - pointer to new hook stack structure
481 * Parameters: stackid(I) - identifier for the network instance that owns this
482 * ns(I) - pointer to the network instance data structure
484 * Allocate and initialize the hook stack instance. This function is not
485 * allowed to fail, so KM_SLEEP is used here when allocating memory. The
486 * value returned is passed back into the shutdown and destroy hooks.
490 hook_stack_init(netstackid_t stackid
, netstack_t
*ns
)
495 printf("hook_stack_init(stack %d)\n", stackid
);
498 hks
= (hook_stack_t
*)kmem_zalloc(sizeof (*hks
), KM_SLEEP
);
499 hks
->hks_netstack
= ns
;
500 hks
->hks_netstackid
= stackid
;
502 CVW_INIT(&hks
->hks_lock
);
503 TAILQ_INIT(&hks
->hks_nhead
);
504 SLIST_INIT(&hks
->hks_familylist
);
506 hook_wait_init(&hks
->hks_waiter
, &hks
->hks_lock
);
508 mutex_enter(&hook_stack_lock
);
509 SLIST_INSERT_HEAD(&hook_stacks
, hks
, hks_entry
);
510 mutex_exit(&hook_stack_lock
);
516 * Function: hook_stack_shutdown
518 * Parameters: stackid(I) - identifier for the network instance that owns this
519 * arg(I) - pointer returned by hook_stack_init
521 * Set the shutdown flag to indicate that we should stop accepting new
522 * register calls as we're now in the cleanup process. The cleanup is a
523 * two stage process and we're not required to free any memory here.
525 * The curious would wonder why isn't there any code that walks through
526 * all of the data structures and sets the flag(s) there? The answer is
527 * that it is expected that this will happen when the zone shutdown calls
528 * the shutdown callbacks for other modules that they will initiate the
529 * free'ing and shutdown of the hooks themselves.
533 hook_stack_shutdown(netstackid_t stackid
, void *arg
)
535 hook_stack_t
*hks
= (hook_stack_t
*)arg
;
537 mutex_enter(&hook_stack_lock
);
539 * Once this flag gets set to one, no more additions are allowed
540 * to any of the structures that make up this stack.
542 hks
->hks_shutdown
= 1;
543 mutex_exit(&hook_stack_lock
);
547 * Function: hook_stack_destroy
549 * Parameters: stackid(I) - identifier for the network instance that owns this
550 * arg(I) - pointer returned by hook_stack_init
552 * Free the hook stack instance.
554 * The rationale for the shutdown being lazy (see the comment above for
555 * hook_stack_shutdown) also applies to the destroy being lazy. Only if
556 * the hook_stack_t data structure is unused will it go away. Else it
557 * is left up to the last user of a data structure to actually free it.
561 hook_stack_fini(netstackid_t stackid
, void *arg
)
563 hook_stack_t
*hks
= (hook_stack_t
*)arg
;
565 mutex_enter(&hook_stack_lock
);
566 hks
->hks_shutdown
= 2;
567 hook_stack_remove(hks
);
568 mutex_exit(&hook_stack_lock
);
572 * Function: hook_stack_remove
574 * Parameters: hks(I) - pointer to an instance of a hook_stack_t
576 * This function assumes that it is called with hook_stack_lock held.
577 * It functions differently to hook_family/event_remove in that it does
578 * the checks to see if it can be removed. This difference exists
579 * because this structure has nothing higher up that depends on it.
582 hook_stack_remove(hook_stack_t
*hks
)
585 ASSERT(mutex_owned(&hook_stack_lock
));
588 * Is the structure still in use?
590 if (!SLIST_EMPTY(&hks
->hks_familylist
) ||
591 !TAILQ_EMPTY(&hks
->hks_nhead
))
594 SLIST_REMOVE(&hook_stacks
, hks
, hook_stack
, hks_entry
);
596 VERIFY(hook_wait_destroy(&hks
->hks_waiter
) == 0);
597 CVW_DESTROY(&hks
->hks_lock
);
598 kmem_free(hks
, sizeof (*hks
));
602 * Function: hook_stack_get
603 * Returns: hook_stack_t * - NULL if not found, else matching instance
604 * Parameters: stackid(I) - instance id to search for
606 * Search the list of currently active hook_stack_t structures for one that
607 * has a matching netstackid_t to the value passed in. The linked list can
608 * only ever have at most one match for this value.
610 static hook_stack_t
*
611 hook_stack_get(netstackid_t stackid
)
615 SLIST_FOREACH(hks
, &hook_stacks
, hks_entry
) {
616 if (hks
->hks_netstackid
== stackid
)
624 * Function: hook_stack_notify_register
625 * Returns: int - 0 = success, else failure
626 * Parameters: stackid(I) - netstack identifier
627 * callback(I)- function to be called
628 * arg(I) - arg to provide callback when it is called
630 * If we're not shutting down this instance, append a new function to the
631 * list of those to call when a new family of hooks is added to this stack.
632 * If the function can be successfully added to the list of callbacks
633 * activated when there is a change to the stack (addition or removal of
634 * a hook family) then generate a fake HN_REGISTER event by directly
635 * calling the callback with the relevant information for each hook
636 * family that currently exists (and isn't being shutdown.)
639 hook_stack_notify_register(netstackid_t stackid
, hook_notify_fn_t callback
,
642 hook_family_int_t
*hfi
;
648 ASSERT(callback
!= NULL
);
651 mutex_enter(&hook_stack_lock
);
652 hks
= hook_stack_get(stackid
);
654 if (hks
->hks_shutdown
!= 0) {
657 CVW_ENTER_WRITE(&hks
->hks_lock
);
658 canrun
= (hook_wait_setflag(&hks
->hks_waiter
,
659 FWF_ADD_WAIT_MASK
, FWF_ADD_WANTED
,
660 FWF_ADD_ACTIVE
) != -1);
661 error
= hook_notify_register(&hks
->hks_nhead
,
663 CVW_EXIT_WRITE(&hks
->hks_lock
);
668 mutex_exit(&hook_stack_lock
);
670 if (error
== 0 && canrun
) {
672 * Generate fake register event for callback that
673 * is being added, letting it know everything that
676 (void) snprintf(buffer
, sizeof (buffer
), "%u",
677 hks
->hks_netstackid
);
679 SLIST_FOREACH(hfi
, &hks
->hks_familylist
, hfi_entry
) {
680 if (hfi
->hfi_condemned
|| hfi
->hfi_shutdown
)
682 callback(HN_REGISTER
, arg
, buffer
, NULL
,
683 hfi
->hfi_family
.hf_name
);
688 hook_wait_unsetflag(&hks
->hks_waiter
, FWF_ADD_ACTIVE
);
694 * Function: hook_stack_notify_unregister
695 * Returns: int - 0 = success, else failure
696 * Parameters: stackid(I) - netstack identifier
697 * callback(I) - function to be called
699 * Attempt to remove a registered function from a hook stack's list of
700 * callbacks to activiate when protocols are added/deleted.
701 * As with hook_stack_notify_register, if all things are going well then
702 * a fake unregister event is delivered to the callback being removed
703 * for each hook family that presently exists.
706 hook_stack_notify_unregister(netstackid_t stackid
, hook_notify_fn_t callback
)
708 hook_family_int_t
*hfi
;
715 mutex_enter(&hook_stack_lock
);
716 hks
= hook_stack_get(stackid
);
718 CVW_ENTER_WRITE(&hks
->hks_lock
);
719 canrun
= (hook_wait_setflag(&hks
->hks_waiter
, FWF_ADD_WAIT_MASK
,
720 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) != -1);
722 error
= hook_notify_unregister(&hks
->hks_nhead
, callback
, &arg
);
723 CVW_EXIT_WRITE(&hks
->hks_lock
);
727 mutex_exit(&hook_stack_lock
);
732 * Generate fake unregister event for callback that
733 * is being removed, letting it know everything that
734 * currently exists is now "disappearing."
736 (void) snprintf(buffer
, sizeof (buffer
), "%u",
737 hks
->hks_netstackid
);
739 SLIST_FOREACH(hfi
, &hks
->hks_familylist
, hfi_entry
) {
740 callback(HN_UNREGISTER
, arg
, buffer
, NULL
,
741 hfi
->hfi_family
.hf_name
);
744 hook_wait_unsetflag(&hks
->hks_waiter
, FWF_ADD_ACTIVE
);
747 mutex_enter(&hook_stack_lock
);
748 hks
= hook_stack_get(stackid
);
749 if ((error
== 0) && (hks
->hks_shutdown
== 2))
750 hook_stack_remove(hks
);
751 mutex_exit(&hook_stack_lock
);
758 * Function: hook_stack_notify_run
760 * Parameters: hks(I) - hook stack pointer to execute callbacks for
761 * name(I) - name of a hook family
762 * cmd(I) - either HN_UNREGISTER or HN_REGISTER
764 * Run through the list of callbacks on the hook stack to be called when
765 * a new hook family is added
767 * As hook_notify_run() expects 3 names, one for the family that is associated
768 * with the cmd (HN_REGISTER or HN_UNREGISTER), one for the event and one
769 * for the object being introduced and we really only have one name (that
770 * of the new hook family), fake the hook stack's name by converting the
771 * integer to a string and for the event just pass NULL.
774 hook_stack_notify_run(hook_stack_t
*hks
, char *name
,
775 hook_notify_cmd_t cmd
)
780 ASSERT(name
!= NULL
);
782 (void) snprintf(buffer
, sizeof (buffer
), "%u", hks
->hks_netstackid
);
784 hook_notify_run(&hks
->hks_nhead
, buffer
, NULL
, name
, cmd
);
789 * Returns: int - return value according to callback func
790 * Parameters: token(I) - event pointer
793 * Run hooks for specific provider. The hooks registered are stepped through
794 * until either the end of the list is reached or a hook function returns a
795 * non-zero value. If a non-zero value is returned from a hook function, we
796 * return that value back to our caller. By design, a hook function can be
797 * called more than once, simultaneously.
800 hook_run(hook_family_int_t
*hfi
, hook_event_token_t token
, hook_data_t info
)
802 hook_event_int_t
*hei
;
806 ASSERT(token
!= NULL
);
808 hei
= (hook_event_int_t
*)token
;
809 DTRACE_PROBE2(hook__run__start
,
810 hook_event_token_t
, token
,
814 * If we consider that this function is only called from within the
815 * stack while an instance is currently active,
817 CVW_ENTER_READ(&hfi
->hfi_lock
);
819 TAILQ_FOREACH(hi
, &hei
->hei_head
, hi_entry
) {
820 ASSERT(hi
->hi_hook
.h_func
!= NULL
);
821 DTRACE_PROBE3(hook__func__start
,
822 hook_event_token_t
, token
,
825 rval
= (*hi
->hi_hook
.h_func
)(token
, info
, hi
->hi_hook
.h_arg
);
826 DTRACE_PROBE4(hook__func__end
,
827 hook_event_token_t
, token
,
831 hi
->hi_kstats
.hook_hits
.value
.ui64
++;
836 hei
->hei_kstats
.events
.value
.ui64
++;
838 CVW_EXIT_READ(&hfi
->hfi_lock
);
840 DTRACE_PROBE3(hook__run__end
,
841 hook_event_token_t
, token
,
849 * Function: hook_family_add
850 * Returns: internal family pointer - NULL = Fail
851 * Parameters: hf(I) - family pointer
852 * hks(I) - pointer to an instance of a hook_stack_t
853 * store(O) - where returned pointer will be stored
855 * Add new family to the family list. The requirements for the addition to
856 * succeed are that the family name must not already be registered and that
857 * the hook stack is not being shutdown.
858 * If store is non-NULL, it is expected to be a pointer to the same variable
859 * that is awaiting to be assigned the return value of this function.
860 * In its current use, the returned value is assigned to netd_hooks in
861 * net_family_register. The use of "store" allows the return value to be
862 * used before this function returns. How can this happen? Through the
863 * callbacks that can be activated at the bottom of this function, when
864 * hook_stack_notify_run is called.
867 hook_family_add(hook_family_t
*hf
, hook_stack_t
*hks
, void **store
)
869 hook_family_int_t
*hfi
, *new;
872 ASSERT(hf
->hf_name
!= NULL
);
874 new = hook_family_copy(hf
);
878 mutex_enter(&hook_stack_lock
);
879 CVW_ENTER_WRITE(&hks
->hks_lock
);
881 if (hks
->hks_shutdown
!= 0) {
882 CVW_EXIT_WRITE(&hks
->hks_lock
);
883 mutex_exit(&hook_stack_lock
);
884 hook_family_free(new, NULL
);
888 /* search family list */
889 hfi
= hook_family_find(hf
->hf_name
, hks
);
891 CVW_EXIT_WRITE(&hks
->hks_lock
);
892 mutex_exit(&hook_stack_lock
);
893 hook_family_free(new, NULL
);
898 * Try and set the FWF_ADD_ACTIVE flag so that we can drop all the
899 * lock further down when calling all of the functions registered
900 * for notification when a new hook family is added.
902 if (hook_wait_setflag(&hks
->hks_waiter
, FWF_ADD_WAIT_MASK
,
903 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) == -1) {
904 CVW_EXIT_WRITE(&hks
->hks_lock
);
905 mutex_exit(&hook_stack_lock
);
906 hook_family_free(new, NULL
);
910 CVW_INIT(&new->hfi_lock
);
911 SLIST_INIT(&new->hfi_head
);
912 TAILQ_INIT(&new->hfi_nhead
);
914 hook_wait_init(&new->hfi_waiter
, &new->hfi_lock
);
916 new->hfi_stack
= hks
;
920 /* Add to family list head */
921 SLIST_INSERT_HEAD(&hks
->hks_familylist
, new, hfi_entry
);
923 CVW_EXIT_WRITE(&hks
->hks_lock
);
924 mutex_exit(&hook_stack_lock
);
926 hook_stack_notify_run(hks
, hf
->hf_name
, HN_REGISTER
);
928 hook_wait_unsetflag(&hks
->hks_waiter
, FWF_ADD_ACTIVE
);
934 * Function: hook_family_remove
935 * Returns: int - 0 = success, else = failure
936 * Parameters: hfi(I) - internal family pointer
938 * Remove family from family list. This function has been designed to be
939 * called once and once only per hook_family_int_t. Thus when cleaning up
940 * this structure as an orphan, callers should only call hook_family_free.
943 hook_family_remove(hook_family_int_t
*hfi
)
946 boolean_t notifydone
;
949 hks
= hfi
->hfi_stack
;
951 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
952 notifydone
= hfi
->hfi_shutdown
;
953 hfi
->hfi_shutdown
= B_TRUE
;
954 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
956 CVW_ENTER_WRITE(&hks
->hks_lock
);
958 if (hook_wait_setflag(&hks
->hks_waiter
, FWF_DEL_WAIT_MASK
,
959 FWF_DEL_WANTED
, FWF_DEL_ACTIVE
) == -1) {
961 * If we're trying to destroy the hook_stack_t...
963 CVW_EXIT_WRITE(&hks
->hks_lock
);
968 * Check if the family is in use by the presence of either events
969 * or notify callbacks on the hook family.
971 if (!SLIST_EMPTY(&hfi
->hfi_head
) || !TAILQ_EMPTY(&hfi
->hfi_nhead
)) {
972 hfi
->hfi_condemned
= B_TRUE
;
974 VERIFY(hook_wait_destroy(&hfi
->hfi_waiter
) == 0);
976 * Although hfi_condemned = B_FALSE is implied from creation,
977 * putting a comment here inside the else upsets lint.
979 hfi
->hfi_condemned
= B_FALSE
;
981 CVW_EXIT_WRITE(&hks
->hks_lock
);
984 hook_stack_notify_run(hks
, hfi
->hfi_family
.hf_name
,
987 hook_wait_unsetflag(&hks
->hks_waiter
, FWF_DEL_ACTIVE
);
990 * If we don't have to wait for anything else to disappear from this
991 * structure then we can free it up.
993 if (!hfi
->hfi_condemned
)
994 hook_family_free(hfi
, hks
);
1001 * Function: hook_family_free
1003 * Parameters: hfi(I) - internal family pointer
1005 * Free alloc memory for family
1008 hook_family_free(hook_family_int_t
*hfi
, hook_stack_t
*hks
)
1012 * This lock gives us possession of the hks pointer after the
1013 * SLIST_REMOVE, for which it is not needed, when hks_shutdown
1014 * is checked and hook_stack_remove called.
1016 mutex_enter(&hook_stack_lock
);
1018 ASSERT(hfi
!= NULL
);
1021 CVW_ENTER_WRITE(&hks
->hks_lock
);
1022 /* Remove from family list */
1023 SLIST_REMOVE(&hks
->hks_familylist
, hfi
, hook_family_int
,
1026 CVW_EXIT_WRITE(&hks
->hks_lock
);
1029 /* Free name space */
1030 if (hfi
->hfi_family
.hf_name
!= NULL
) {
1031 kmem_free(hfi
->hfi_family
.hf_name
,
1032 strlen(hfi
->hfi_family
.hf_name
) + 1);
1035 /* Free container */
1036 kmem_free(hfi
, sizeof (*hfi
));
1038 if (hks
->hks_shutdown
== 2)
1039 hook_stack_remove(hks
);
1041 mutex_exit(&hook_stack_lock
);
1045 * Function: hook_family_shutdown
1046 * Returns: int - 0 = success, else = failure
1047 * Parameters: hfi(I) - internal family pointer
1049 * As an alternative to removing a family, we may desire to just generate
1050 * a series of callbacks to indicate that we will be going away in the
1051 * future. The hfi_condemned flag isn't set because we aren't trying to
1052 * remove the structure.
1055 hook_family_shutdown(hook_family_int_t
*hfi
)
1058 boolean_t notifydone
;
1060 ASSERT(hfi
!= NULL
);
1061 hks
= hfi
->hfi_stack
;
1063 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1064 notifydone
= hfi
->hfi_shutdown
;
1065 hfi
->hfi_shutdown
= B_TRUE
;
1066 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1068 CVW_ENTER_WRITE(&hks
->hks_lock
);
1070 if (hook_wait_setflag(&hks
->hks_waiter
, FWF_DEL_WAIT_MASK
,
1071 FWF_DEL_WANTED
, FWF_DEL_ACTIVE
) == -1) {
1073 * If we're trying to destroy the hook_stack_t...
1075 CVW_EXIT_WRITE(&hks
->hks_lock
);
1079 CVW_EXIT_WRITE(&hks
->hks_lock
);
1082 hook_stack_notify_run(hks
, hfi
->hfi_family
.hf_name
,
1085 hook_wait_unsetflag(&hks
->hks_waiter
, FWF_DEL_ACTIVE
);
1091 * Function: hook_family_copy
1092 * Returns: internal family pointer - NULL = Failed
1093 * Parameters: src(I) - family pointer
1095 * Allocate internal family block and duplicate incoming family
1096 * No locks should be held across this function as it may sleep.
1098 static hook_family_int_t
*
1099 hook_family_copy(hook_family_t
*src
)
1101 hook_family_int_t
*new;
1104 ASSERT(src
!= NULL
);
1105 ASSERT(src
->hf_name
!= NULL
);
1107 new = (hook_family_int_t
*)kmem_zalloc(sizeof (*new), KM_SLEEP
);
1110 dst
= &new->hfi_family
;
1113 SLIST_INIT(&new->hfi_head
);
1114 TAILQ_INIT(&new->hfi_nhead
);
1117 dst
->hf_name
= kmem_alloc(strlen(src
->hf_name
) + 1, KM_SLEEP
);
1118 (void) strcpy(dst
->hf_name
, src
->hf_name
);
1124 * Function: hook_family_find
1125 * Returns: internal family pointer - NULL = Not match
1126 * Parameters: family(I) - family name string
1128 * Search family list with family name
1129 * A lock on hfi_lock must be held when called.
1131 static hook_family_int_t
*
1132 hook_family_find(char *family
, hook_stack_t
*hks
)
1134 hook_family_int_t
*hfi
= NULL
;
1136 ASSERT(family
!= NULL
);
1138 SLIST_FOREACH(hfi
, &hks
->hks_familylist
, hfi_entry
) {
1139 if (strcmp(hfi
->hfi_family
.hf_name
, family
) == 0)
1146 * Function: hook_family_notify_register
1147 * Returns: int - 0 = success, else failure
1148 * Parameters: hfi(I) - hook family
1149 * callback(I) - function to be called
1150 * arg(I) - arg to provide callback when it is called
1152 * So long as this hook stack isn't being shut down, register a new
1153 * callback to be activated each time a new event is added to this
1156 * To call this function we must have an active handle in use on the family,
1157 * so if we take this into account, then neither the hook_family_int_t nor
1158 * the hook_stack_t that owns it can disappear. We have to put some trust
1159 * in the callers to be properly synchronised...
1161 * Holding hks_lock is required to provide synchronisation for hks_shutdown.
1164 hook_family_notify_register(hook_family_int_t
*hfi
,
1165 hook_notify_fn_t callback
, void *arg
)
1167 hook_event_int_t
*hei
;
1172 ASSERT(hfi
!= NULL
);
1174 hks
= hfi
->hfi_stack
;
1176 CVW_ENTER_READ(&hks
->hks_lock
);
1178 if ((hfi
->hfi_stack
->hks_shutdown
!= 0) ||
1179 hfi
->hfi_condemned
|| hfi
->hfi_shutdown
) {
1180 CVW_EXIT_READ(&hks
->hks_lock
);
1184 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1185 canrun
= (hook_wait_setflag(&hfi
->hfi_waiter
, FWF_ADD_WAIT_MASK
,
1186 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) != -1);
1187 error
= hook_notify_register(&hfi
->hfi_nhead
, callback
, arg
);
1188 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1190 CVW_EXIT_READ(&hks
->hks_lock
);
1192 if (error
== 0 && canrun
) {
1193 SLIST_FOREACH(hei
, &hfi
->hfi_head
, hei_entry
) {
1194 callback(HN_REGISTER
, arg
,
1195 hfi
->hfi_family
.hf_name
, NULL
,
1196 hei
->hei_event
->he_name
);
1201 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_ADD_ACTIVE
);
1207 * Function: hook_family_notify_unregister
1208 * Returns: int - 0 = success, else failure
1209 * Parameters: hfi(I) - hook family
1210 * callback(I) - function to be called
1212 * Remove a callback from the list of those executed when a new event is
1213 * added to a hook family. If the family is not in the process of being
1214 * destroyed then simulate an unregister callback for each event that is
1215 * on the family. This pairs up with the hook_family_notify_register
1216 * action that simulates register events.
1217 * The order of what happens here is important and goes like this.
1218 * 1) Remove the callback from the list of functions to be called as part
1219 * of the notify operation when an event is added or removed from the
1221 * 2) If the hook_family_int_t structure is on death row (free_family will
1222 * be set to true) then there's nothing else to do than let it be free'd.
1223 * 3) If the structure isn't about to die, mark it up as being busy using
1224 * hook_wait_setflag and then drop the lock so the loop can be run.
1225 * 4) if hook_wait_setflag was successful, tell all of the notify callback
1226 * functions that this family has been unregistered.
1230 hook_family_notify_unregister(hook_family_int_t
*hfi
,
1231 hook_notify_fn_t callback
)
1233 hook_event_int_t
*hei
;
1234 boolean_t free_family
;
1241 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1243 (void) hook_wait_setflag(&hfi
->hfi_waiter
, FWF_DEL_WAIT_MASK
,
1244 FWF_DEL_WANTED
, FWF_DEL_ACTIVE
);
1246 error
= hook_notify_unregister(&hfi
->hfi_nhead
, callback
, &arg
);
1248 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_DEL_ACTIVE
);
1251 * If hook_family_remove has been called but the structure was still
1252 * "busy" ... but we might have just made it "unbusy"...
1254 if ((error
== 0) && hfi
->hfi_condemned
&&
1255 SLIST_EMPTY(&hfi
->hfi_head
) && TAILQ_EMPTY(&hfi
->hfi_nhead
)) {
1256 free_family
= B_TRUE
;
1258 free_family
= B_FALSE
;
1261 if (error
== 0 && !free_family
) {
1262 canrun
= (hook_wait_setflag(&hfi
->hfi_waiter
, FWF_ADD_WAIT_MASK
,
1263 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) != -1);
1266 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1269 SLIST_FOREACH(hei
, &hfi
->hfi_head
, hei_entry
) {
1270 callback(HN_UNREGISTER
, arg
,
1271 hfi
->hfi_family
.hf_name
, NULL
,
1272 hei
->hei_event
->he_name
);
1275 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_ADD_ACTIVE
);
1276 } else if (free_family
) {
1277 hook_family_free(hfi
, hfi
->hfi_stack
);
1284 * Function: hook_event_add
1285 * Returns: internal event pointer - NULL = Fail
1286 * Parameters: hfi(I) - internal family pointer
1287 * he(I) - event pointer
1289 * Add new event to event list on specific family.
1290 * This function can fail to return successfully if (1) it cannot allocate
1291 * enough memory for its own internal data structures, (2) the event has
1292 * already been registered (for any hook family.)
1295 hook_event_add(hook_family_int_t
*hfi
, hook_event_t
*he
)
1297 hook_event_int_t
*hei
, *new;
1300 ASSERT(hfi
!= NULL
);
1302 ASSERT(he
->he_name
!= NULL
);
1304 new = hook_event_copy(he
);
1308 hks
= hfi
->hfi_stack
;
1309 CVW_ENTER_READ(&hks
->hks_lock
);
1311 hks
= hfi
->hfi_stack
;
1312 if (hks
->hks_shutdown
!= 0) {
1313 CVW_EXIT_READ(&hks
->hks_lock
);
1314 hook_event_free(new, NULL
);
1318 /* Check whether this event pointer is already registered */
1319 hei
= hook_event_checkdup(he
, hks
);
1321 CVW_EXIT_READ(&hks
->hks_lock
);
1322 hook_event_free(new, NULL
);
1326 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1328 if (hfi
->hfi_condemned
|| hfi
->hfi_shutdown
) {
1329 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1330 CVW_EXIT_READ(&hks
->hks_lock
);
1331 hook_event_free(new, NULL
);
1334 CVW_EXIT_READ(&hks
->hks_lock
);
1336 if (hook_wait_setflag(&hfi
->hfi_waiter
, FWF_ADD_WAIT_MASK
,
1337 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) == -1) {
1338 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1339 hook_event_free(new, NULL
);
1343 TAILQ_INIT(&new->hei_nhead
);
1345 hook_event_init_kstats(hfi
, new);
1346 hook_wait_init(&new->hei_waiter
, &new->hei_lock
);
1348 /* Add to event list head */
1349 SLIST_INSERT_HEAD(&hfi
->hfi_head
, new, hei_entry
);
1351 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1353 hook_notify_run(&hfi
->hfi_nhead
,
1354 hfi
->hfi_family
.hf_name
, NULL
, he
->he_name
, HN_REGISTER
);
1356 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_ADD_ACTIVE
);
1362 * Function: hook_event_init_kstats
1364 * Parameters: hfi(I) - pointer to the family that owns this event.
1365 * hei(I) - pointer to the hook event that needs some kstats.
1367 * Create a set of kstats that relate to each event registered with
1368 * the hook framework. A counter is kept for each time the event is
1369 * activated and for each time a hook is added or removed. As the
1370 * kstats just count the events as they happen, the total number of
1371 * hooks registered must be obtained by subtractived removed from added.
1374 hook_event_init_kstats(hook_family_int_t
*hfi
, hook_event_int_t
*hei
)
1376 hook_event_kstat_t
template = {
1377 { "hooksAdded", KSTAT_DATA_UINT64
},
1378 { "hooksRemoved", KSTAT_DATA_UINT64
},
1379 { "events", KSTAT_DATA_UINT64
}
1383 hks
= hfi
->hfi_stack
;
1384 hei
->hei_kstatp
= kstat_create_netstack(hfi
->hfi_family
.hf_name
, 0,
1385 hei
->hei_event
->he_name
, "hook_event", KSTAT_TYPE_NAMED
,
1386 sizeof (hei
->hei_kstats
) / sizeof (kstat_named_t
),
1387 KSTAT_FLAG_VIRTUAL
, hks
->hks_netstackid
);
1389 bcopy((char *)&template, &hei
->hei_kstats
, sizeof (template));
1391 if (hei
->hei_kstatp
!= NULL
) {
1392 hei
->hei_kstatp
->ks_data
= (void *)&hei
->hei_kstats
;
1393 hei
->hei_kstatp
->ks_private
=
1394 (void *)(uintptr_t)hks
->hks_netstackid
;
1396 kstat_install(hei
->hei_kstatp
);
1401 * Function: hook_event_remove
1402 * Returns: int - 0 = success, else = failure
1403 * Parameters: hfi(I) - internal family pointer
1404 * he(I) - event pointer
1406 * Remove event from event list on specific family
1408 * This function assumes that the caller has received a pointer to a the
1409 * hook_family_int_t via a call to net_protocol_lookup or net_protocol_unreg'.
1410 * This the hook_family_int_t is guaranteed to be around for the life of this
1411 * call, unless the caller has decided to call net_protocol_release or
1412 * net_protocol_unregister before calling net_event_unregister - an error.
1415 hook_event_remove(hook_family_int_t
*hfi
, hook_event_t
*he
)
1417 boolean_t free_family
;
1418 hook_event_int_t
*hei
;
1419 boolean_t notifydone
;
1421 ASSERT(hfi
!= NULL
);
1424 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1427 * Set the flag so that we can call hook_event_notify_run without
1428 * holding any locks but at the same time prevent other changes to
1429 * the event at the same time.
1431 if (hook_wait_setflag(&hfi
->hfi_waiter
, FWF_DEL_WAIT_MASK
,
1432 FWF_DEL_WANTED
, FWF_DEL_ACTIVE
) == -1) {
1433 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1437 hei
= hook_event_find(hfi
, he
->he_name
);
1439 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_DEL_ACTIVE
);
1440 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1444 free_family
= B_FALSE
;
1446 CVW_ENTER_WRITE(&hei
->hei_lock
);
1448 * The hei_shutdown flag is used to indicate whether or not we have
1449 * done a shutdown and thus already walked through the notify list.
1451 notifydone
= hei
->hei_shutdown
;
1452 hei
->hei_shutdown
= B_TRUE
;
1454 * If there are any hooks still registered for this event or
1455 * there are any notifiers registered, return an error indicating
1456 * that the event is still busy.
1458 if (!TAILQ_EMPTY(&hei
->hei_head
) || !TAILQ_EMPTY(&hei
->hei_nhead
)) {
1459 hei
->hei_condemned
= B_TRUE
;
1460 CVW_EXIT_WRITE(&hei
->hei_lock
);
1462 /* hei_condemned = B_FALSE is implied from creation */
1464 * Even though we know the notify list is empty, we call
1465 * hook_wait_destroy here to synchronise wait removing a
1466 * hook from an event.
1468 VERIFY(hook_wait_destroy(&hei
->hei_waiter
) == 0);
1470 CVW_EXIT_WRITE(&hei
->hei_lock
);
1472 if (hfi
->hfi_condemned
&& SLIST_EMPTY(&hfi
->hfi_head
) &&
1473 TAILQ_EMPTY(&hfi
->hfi_nhead
))
1474 free_family
= B_TRUE
;
1477 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1480 hook_notify_run(&hfi
->hfi_nhead
,
1481 hfi
->hfi_family
.hf_name
, NULL
, he
->he_name
, HN_UNREGISTER
);
1483 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_DEL_ACTIVE
);
1485 if (!hei
->hei_condemned
) {
1486 hook_event_free(hei
, hfi
);
1488 hook_family_free(hfi
, hfi
->hfi_stack
);
1495 * Function: hook_event_shutdown
1496 * Returns: int - 0 = success, else = failure
1497 * Parameters: hfi(I) - internal family pointer
1498 * he(I) - event pointer
1500 * As with hook_family_shutdown, we want to generate the notify callbacks
1501 * as if the event was being removed but not actually do the remove.
1504 hook_event_shutdown(hook_family_int_t
*hfi
, hook_event_t
*he
)
1506 hook_event_int_t
*hei
;
1507 boolean_t notifydone
;
1509 ASSERT(hfi
!= NULL
);
1512 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1515 * Set the flag so that we can call hook_event_notify_run without
1516 * holding any locks but at the same time prevent other changes to
1517 * the event at the same time.
1519 if (hook_wait_setflag(&hfi
->hfi_waiter
, FWF_DEL_WAIT_MASK
,
1520 FWF_DEL_WANTED
, FWF_DEL_ACTIVE
) == -1) {
1521 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1525 hei
= hook_event_find(hfi
, he
->he_name
);
1527 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_DEL_ACTIVE
);
1528 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1532 CVW_ENTER_WRITE(&hei
->hei_lock
);
1533 notifydone
= hei
->hei_shutdown
;
1534 hei
->hei_shutdown
= B_TRUE
;
1535 CVW_EXIT_WRITE(&hei
->hei_lock
);
1537 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1540 hook_notify_run(&hfi
->hfi_nhead
,
1541 hfi
->hfi_family
.hf_name
, NULL
, he
->he_name
, HN_UNREGISTER
);
1543 hook_wait_unsetflag(&hfi
->hfi_waiter
, FWF_DEL_ACTIVE
);
1549 * Function: hook_event_free
1551 * Parameters: hei(I) - internal event pointer
1553 * Free alloc memory for event
1556 hook_event_free(hook_event_int_t
*hei
, hook_family_int_t
*hfi
)
1558 boolean_t free_family
;
1560 ASSERT(hei
!= NULL
);
1563 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1565 * Remove the event from the hook family's list.
1567 SLIST_REMOVE(&hfi
->hfi_head
, hei
, hook_event_int
, hei_entry
);
1568 if (hfi
->hfi_condemned
&& SLIST_EMPTY(&hfi
->hfi_head
) &&
1569 TAILQ_EMPTY(&hfi
->hfi_nhead
)) {
1570 free_family
= B_TRUE
;
1572 free_family
= B_FALSE
;
1574 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1577 if (hei
->hei_kstatp
!= NULL
) {
1578 ASSERT(hfi
!= NULL
);
1580 kstat_delete_netstack(hei
->hei_kstatp
,
1581 hfi
->hfi_stack
->hks_netstackid
);
1582 hei
->hei_kstatp
= NULL
;
1585 /* Free container */
1586 kmem_free(hei
, sizeof (*hei
));
1589 hook_family_free(hfi
, hfi
->hfi_stack
);
1593 * Function: hook_event_checkdup
1594 * Returns: internal event pointer - NULL = Not match
1595 * Parameters: he(I) - event pointer
1597 * Search all of the hook families to see if the event being passed in
1598 * has already been associated with one.
1600 static hook_event_int_t
*
1601 hook_event_checkdup(hook_event_t
*he
, hook_stack_t
*hks
)
1603 hook_family_int_t
*hfi
;
1604 hook_event_int_t
*hei
;
1608 CVW_ENTER_READ(&hks
->hks_lock
);
1609 SLIST_FOREACH(hfi
, &hks
->hks_familylist
, hfi_entry
) {
1610 SLIST_FOREACH(hei
, &hfi
->hfi_head
, hei_entry
) {
1611 if (hei
->hei_event
== he
) {
1612 CVW_EXIT_READ(&hks
->hks_lock
);
1617 CVW_EXIT_READ(&hks
->hks_lock
);
1623 * Function: hook_event_copy
1624 * Returns: internal event pointer - NULL = Failed
1625 * Parameters: src(I) - event pointer
1627 * Allocate internal event block and duplicate incoming event
1628 * No locks should be held across this function as it may sleep.
1630 static hook_event_int_t
*
1631 hook_event_copy(hook_event_t
*src
)
1633 hook_event_int_t
*new;
1635 ASSERT(src
!= NULL
);
1636 ASSERT(src
->he_name
!= NULL
);
1638 new = (hook_event_int_t
*)kmem_zalloc(sizeof (*new), KM_SLEEP
);
1641 TAILQ_INIT(&new->hei_head
);
1642 new->hei_event
= src
;
1648 * Function: hook_event_find
1649 * Returns: internal event pointer - NULL = Not match
1650 * Parameters: hfi(I) - internal family pointer
1651 * event(I) - event name string
1653 * Search event list with event name
1654 * A lock on hfi->hfi_lock must be held when called.
1656 static hook_event_int_t
*
1657 hook_event_find(hook_family_int_t
*hfi
, char *event
)
1659 hook_event_int_t
*hei
= NULL
;
1661 ASSERT(hfi
!= NULL
);
1662 ASSERT(event
!= NULL
);
1664 SLIST_FOREACH(hei
, &hfi
->hfi_head
, hei_entry
) {
1665 if ((strcmp(hei
->hei_event
->he_name
, event
) == 0) &&
1666 ((hei
->hei_waiter
.fw_flags
& FWF_UNSAFE
) == 0))
1673 * Function: hook_event_notify_register
1674 * Returns: int - 0 = success, else failure
1675 * Parameters: hfi(I) - hook family
1676 * event(I) - name of the event
1677 * callback(I) - function to be called
1678 * arg(I) - arg to provide callback when it is called
1680 * Adds a new callback to the event named by "event" (we must find it)
1681 * that will be executed each time a new hook is added to the event.
1682 * Of course, if the stack is being shut down, this call should fail.
1685 hook_event_notify_register(hook_family_int_t
*hfi
, char *event
,
1686 hook_notify_fn_t callback
, void *arg
)
1688 hook_event_int_t
*hei
;
1695 hks
= hfi
->hfi_stack
;
1696 CVW_ENTER_READ(&hks
->hks_lock
);
1697 if (hks
->hks_shutdown
!= 0) {
1698 CVW_EXIT_READ(&hks
->hks_lock
);
1702 CVW_ENTER_READ(&hfi
->hfi_lock
);
1704 if (hfi
->hfi_condemned
|| hfi
->hfi_shutdown
) {
1705 CVW_EXIT_READ(&hfi
->hfi_lock
);
1706 CVW_EXIT_READ(&hks
->hks_lock
);
1710 hei
= hook_event_find(hfi
, event
);
1712 CVW_EXIT_READ(&hfi
->hfi_lock
);
1713 CVW_EXIT_READ(&hks
->hks_lock
);
1717 if (hei
->hei_condemned
|| hei
->hei_shutdown
) {
1718 CVW_EXIT_READ(&hfi
->hfi_lock
);
1719 CVW_EXIT_READ(&hks
->hks_lock
);
1723 CVW_ENTER_WRITE(&hei
->hei_lock
);
1724 canrun
= (hook_wait_setflag(&hei
->hei_waiter
, FWF_ADD_WAIT_MASK
,
1725 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) != -1);
1726 error
= hook_notify_register(&hei
->hei_nhead
, callback
, arg
);
1727 CVW_EXIT_WRITE(&hei
->hei_lock
);
1729 CVW_EXIT_READ(&hfi
->hfi_lock
);
1730 CVW_EXIT_READ(&hks
->hks_lock
);
1732 if (error
== 0 && canrun
) {
1733 TAILQ_FOREACH(h
, &hei
->hei_head
, hi_entry
) {
1734 callback(HN_REGISTER
, arg
,
1735 hfi
->hfi_family
.hf_name
, hei
->hei_event
->he_name
,
1741 hook_wait_unsetflag(&hei
->hei_waiter
, FWF_ADD_ACTIVE
);
1747 * Function: hook_event_notify_unregister
1748 * Returns: int - 0 = success, else failure
1749 * Parameters: hfi(I) - hook family
1750 * event(I) - name of the event
1751 * callback(I) - function to be called
1753 * Remove the given callback from the named event's list of functions
1754 * to call when a hook is added or removed.
1757 hook_event_notify_unregister(hook_family_int_t
*hfi
, char *event
,
1758 hook_notify_fn_t callback
)
1760 hook_event_int_t
*hei
;
1761 boolean_t free_event
;
1769 CVW_ENTER_READ(&hfi
->hfi_lock
);
1771 hei
= hook_event_find(hfi
, event
);
1773 CVW_EXIT_READ(&hfi
->hfi_lock
);
1777 CVW_ENTER_WRITE(&hei
->hei_lock
);
1779 (void) hook_wait_setflag(&hei
->hei_waiter
, FWF_DEL_WAIT_MASK
,
1780 FWF_DEL_WANTED
, FWF_DEL_ACTIVE
);
1782 error
= hook_notify_unregister(&hei
->hei_nhead
, callback
, &arg
);
1784 hook_wait_unsetflag(&hei
->hei_waiter
, FWF_DEL_ACTIVE
);
1787 * hei_condemned has been set if someone tried to remove the
1788 * event but couldn't because there were still things attached to
1789 * it. Now that we've done a successful remove, if it is now empty
1790 * then by all rights we should be free'ing it too. Note that the
1791 * expectation is that only the caller of hook_event_add will ever
1792 * call hook_event_remove.
1794 if ((error
== 0) && hei
->hei_condemned
&&
1795 TAILQ_EMPTY(&hei
->hei_head
) && TAILQ_EMPTY(&hei
->hei_nhead
)) {
1796 free_event
= B_TRUE
;
1798 free_event
= B_FALSE
;
1801 if (error
== 0 && !free_event
) {
1802 canrun
= (hook_wait_setflag(&hei
->hei_waiter
, FWF_ADD_WAIT_MASK
,
1803 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) != -1);
1806 CVW_EXIT_WRITE(&hei
->hei_lock
);
1807 CVW_EXIT_READ(&hfi
->hfi_lock
);
1810 TAILQ_FOREACH(h
, &hei
->hei_head
, hi_entry
) {
1811 callback(HN_UNREGISTER
, arg
,
1812 hfi
->hfi_family
.hf_name
, hei
->hei_event
->he_name
,
1816 hook_wait_unsetflag(&hei
->hei_waiter
, FWF_ADD_ACTIVE
);
1821 * It is safe to pass in hfi here, without a lock, because
1822 * our structure (hei) is still on one of its lists and thus
1823 * it won't be able to disappear yet...
1825 hook_event_free(hei
, hfi
);
1832 * Function: hook_event_notify_run
1834 * Parameters: nrun(I) - pointer to the list of callbacks to execute
1835 * hfi(I) - hook stack pointer to execute callbacks for
1836 * name(I) - name of a hook family
1837 * cmd(I) - either HN_UNREGISTER or HN_REGISTER
1839 * Execute all of the callbacks registered for this event.
1842 hook_event_notify_run(hook_event_int_t
*hei
, hook_family_int_t
*hfi
,
1843 char *event
, char *name
, hook_notify_cmd_t cmd
)
1846 hook_notify_run(&hei
->hei_nhead
, hfi
->hfi_family
.hf_name
,
1851 * Function: hook_register
1852 * Returns: int - 0 = success, else = failure
1853 * Parameters: hfi(I) - internal family pointer
1854 * event(I) - event name string
1855 * h(I) - hook pointer
1857 * Add new hook to hook list on the specified family and event.
1860 hook_register(hook_family_int_t
*hfi
, char *event
, hook_t
*h
)
1862 hook_event_int_t
*hei
;
1863 hook_int_t
*hi
, *new;
1866 ASSERT(hfi
!= NULL
);
1867 ASSERT(event
!= NULL
);
1870 if (hfi
->hfi_stack
->hks_shutdown
)
1873 /* Alloc hook_int_t and copy hook */
1879 * Since hook add/remove only impact event, so it is unnecessary
1880 * to hold global family write lock. Just get read lock here to
1881 * ensure event will not be removed when doing hooks operation
1883 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
1885 hei
= hook_event_find(hfi
, event
);
1887 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1888 hook_int_free(new, hfi
->hfi_stack
->hks_netstackid
);
1892 CVW_ENTER_WRITE(&hei
->hei_lock
);
1895 * If we've run either the remove() or shutdown(), do not allow any
1896 * more hooks to be added to this event.
1898 if (hei
->hei_shutdown
) {
1903 hi
= hook_find(hei
, h
);
1909 if (hook_wait_setflag(&hei
->hei_waiter
, FWF_ADD_WAIT_MASK
,
1910 FWF_ADD_WANTED
, FWF_ADD_ACTIVE
) == -1) {
1913 CVW_EXIT_WRITE(&hei
->hei_lock
);
1914 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1915 hook_int_free(new, hfi
->hfi_stack
->hks_netstackid
);
1919 /* Add to hook list head */
1920 error
= hook_insert(&hei
->hei_head
, new);
1922 hei
->hei_event
->he_interested
= B_TRUE
;
1923 hei
->hei_kstats
.hooks_added
.value
.ui64
++;
1925 hook_init_kstats(hfi
, hei
, new);
1928 CVW_EXIT_WRITE(&hei
->hei_lock
);
1929 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
1932 * Note that the name string passed through to the notify callbacks
1933 * is from the original hook being registered, not the copy being
1937 hook_event_notify_run(hei
, hfi
, event
, h
->h_name
, HN_REGISTER
);
1939 hook_wait_unsetflag(&hei
->hei_waiter
, FWF_ADD_ACTIVE
);
1945 * Function: hook_insert
1946 * Returns: int - 0 = success, else = failure
1947 * Parameters: head(I) - pointer to hook list to insert hook onto
1948 * new(I) - pointer to hook to be inserted
1950 * Try to insert the hook onto the list of hooks according to the hints
1951 * given in the hook to be inserted and those that already exist on the
1952 * list. For now, the implementation permits only a single hook to be
1953 * either first or last and names provided with before or after are only
1954 * loosely coupled with the action.
1957 hook_insert(hook_int_head_t
*head
, hook_int_t
*new)
1962 hook_t
*h
= &new->hi_hook
;
1964 switch (new->hi_hook
.h_hint
) {
1968 * If there is no hint present (or not one that can be
1969 * satisfied now) then try to at least respect the wishes
1970 * of those that want to be last. If there are none wanting
1971 * to be last then add the new hook to the tail of the
1972 * list - this means we keep any wanting to be first
1973 * happy without having to search for HH_FIRST.
1975 TAILQ_FOREACH(hi
, head
, hi_entry
) {
1977 if ((hih
->h_hint
== HH_AFTER
) &&
1979 (char *)hih
->h_hintvalue
) == 0)) {
1980 TAILQ_INSERT_BEFORE(hi
, new, hi_entry
);
1983 if ((hih
->h_hint
== HH_BEFORE
) && (before
== NULL
) &&
1985 (char *)hih
->h_hintvalue
) == 0)) {
1989 if (before
!= NULL
) {
1990 TAILQ_INSERT_AFTER(head
, before
, new, hi_entry
);
1993 hook_insert_plain(head
, new);
1997 hi
= TAILQ_FIRST(head
);
1998 if ((hi
!= NULL
) && (hi
->hi_hook
.h_hint
== HH_FIRST
))
2000 TAILQ_INSERT_HEAD(head
, new, hi_entry
);
2004 hi
= TAILQ_LAST(head
, hook_int_head
);
2005 if ((hi
!= NULL
) && (hi
->hi_hook
.h_hint
== HH_LAST
))
2007 TAILQ_INSERT_TAIL(head
, new, hi_entry
);
2011 hi
= hook_find_byname(head
, (char *)new->hi_hook
.h_hintvalue
);
2013 return (hook_insert_afterbefore(head
, new));
2015 if (hi
->hi_hook
.h_hint
== HH_FIRST
)
2018 TAILQ_INSERT_BEFORE(hi
, new, hi_entry
);
2022 hi
= hook_find_byname(head
, (char *)new->hi_hook
.h_hintvalue
);
2024 return (hook_insert_afterbefore(head
, new));
2026 if (hi
->hi_hook
.h_hint
== HH_LAST
)
2029 TAILQ_INSERT_AFTER(head
, hi
, new, hi_entry
);
2040 * Function: hook_insert_plain
2041 * Returns: int - 0 = success, else = failure
2042 * Parameters: head(I) - pointer to hook list to insert hook onto
2043 * new(I) - pointer to hook to be inserted
2045 * Insert a hook such that it respects the wishes of those that want to
2046 * be last. If there are none wanting to be last then add the new hook
2047 * to the tail of the list - this means we keep any wanting to be first
2048 * happy without having to search for HH_FIRST.
2051 hook_insert_plain(hook_int_head_t
*head
, hook_int_t
*new)
2055 hi
= TAILQ_FIRST(head
);
2057 if (hi
->hi_hook
.h_hint
== HH_LAST
) {
2058 TAILQ_INSERT_BEFORE(hi
, new, hi_entry
);
2060 TAILQ_INSERT_TAIL(head
, new, hi_entry
);
2063 TAILQ_INSERT_TAIL(head
, new, hi_entry
);
2068 * Function: hook_insert_afterbefore
2069 * Returns: int - 0 = success, else = failure
2070 * Parameters: head(I) - pointer to hook list to insert hook onto
2071 * new(I) - pointer to hook to be inserted
2073 * Simple insertion of a hook specifying a HH_BEFORE or HH_AFTER was not
2074 * possible, so now we need to be more careful. The first pass is to go
2075 * through the list and look for any other hooks that also specify the
2076 * same hint name as the new one. The object of this exercise is to make
2077 * sure that hooks with HH_BEFORE always appear on the list before those
2078 * with HH_AFTER so that when said hook arrives, it can be placed in the
2079 * middle of the BEFOREs and AFTERs. If this condition does not arise,
2080 * just use hook_insert_plain() to try and insert the hook somewhere that
2081 * is innocuous to existing efforts.
2084 hook_insert_afterbefore(hook_int_head_t
*head
, hook_int_t
*new)
2091 ASSERT(new->hi_hook
.h_hint
!= HH_NONE
);
2092 ASSERT(new->hi_hook
.h_hint
!= HH_LAST
);
2093 ASSERT(new->hi_hook
.h_hint
!= HH_FIRST
);
2096 * First, look through the list to see if there are any other
2097 * before's or after's that have a matching hint name.
2099 TAILQ_FOREACH(hi
, head
, hi_entry
) {
2101 switch (h
->h_hint
) {
2107 if ((nh
->h_hint
== HH_BEFORE
) &&
2108 (strcmp((char *)h
->h_hintvalue
,
2109 (char *)nh
->h_hintvalue
) == 0)) {
2110 TAILQ_INSERT_AFTER(head
, hi
, new, hi_entry
);
2113 if ((nh
->h_hint
== HH_AFTER
) &&
2114 (strcmp((char *)h
->h_hintvalue
,
2115 (char *)nh
->h_hintvalue
) == 0)) {
2116 TAILQ_INSERT_BEFORE(hi
, new, hi_entry
);
2121 if ((nh
->h_hint
== HH_AFTER
) &&
2122 (strcmp((char *)h
->h_hintvalue
,
2123 (char *)nh
->h_hintvalue
) == 0)) {
2124 TAILQ_INSERT_AFTER(head
, hi
, new, hi_entry
);
2127 if ((nh
->h_hint
== HH_BEFORE
) &&
2128 (strcmp((char *)h
->h_hintvalue
,
2129 (char *)nh
->h_hintvalue
) == 0)) {
2130 TAILQ_INSERT_BEFORE(hi
, new, hi_entry
);
2137 hook_insert_plain(head
, new);
2143 * Function: hook_unregister
2144 * Returns: int - 0 = success, else = failure
2145 * Parameters: hfi(I) - internal family pointer
2146 * event(I) - event name string
2147 * h(I) - hook pointer
2149 * Remove hook from hook list on specific family, event
2152 hook_unregister(hook_family_int_t
*hfi
, char *event
, hook_t
*h
)
2154 hook_event_int_t
*hei
;
2156 boolean_t free_event
;
2158 ASSERT(hfi
!= NULL
);
2161 CVW_ENTER_WRITE(&hfi
->hfi_lock
);
2163 hei
= hook_event_find(hfi
, event
);
2165 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
2169 /* Hold write lock for event */
2170 CVW_ENTER_WRITE(&hei
->hei_lock
);
2172 hi
= hook_find(hei
, h
);
2174 CVW_EXIT_WRITE(&hei
->hei_lock
);
2175 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
2179 if (hook_wait_setflag(&hei
->hei_waiter
, FWF_DEL_WAIT_MASK
,
2180 FWF_DEL_WANTED
, FWF_DEL_ACTIVE
) == -1) {
2181 CVW_EXIT_WRITE(&hei
->hei_lock
);
2182 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
2186 /* Remove from hook list */
2187 TAILQ_REMOVE(&hei
->hei_head
, hi
, hi_entry
);
2189 free_event
= B_FALSE
;
2190 if (TAILQ_EMPTY(&hei
->hei_head
)) {
2191 hei
->hei_event
->he_interested
= B_FALSE
;
2193 * If the delete pending flag has been set and there are
2194 * no notifiers on the event (and we've removed the last
2195 * hook) then we need to free this event after we're done.
2197 if (hei
->hei_condemned
&& TAILQ_EMPTY(&hei
->hei_nhead
))
2198 free_event
= B_TRUE
;
2200 hei
->hei_kstats
.hooks_removed
.value
.ui64
++;
2202 CVW_EXIT_WRITE(&hei
->hei_lock
);
2203 CVW_EXIT_WRITE(&hfi
->hfi_lock
);
2205 * While the FWF_DEL_ACTIVE flag is set, the hook_event_int_t
2206 * will not be free'd and thus the hook_family_int_t wil not
2209 hook_event_notify_run(hei
, hfi
, event
, h
->h_name
, HN_UNREGISTER
);
2210 hook_wait_unsetflag(&hei
->hei_waiter
, FWF_DEL_ACTIVE
);
2212 hook_int_free(hi
, hfi
->hfi_stack
->hks_netstackid
);
2215 hook_event_free(hei
, hfi
);
2221 * Function: hook_find_byname
2222 * Returns: internal hook pointer - NULL = Not match
2223 * Parameters: hei(I) - internal event pointer
2224 * name(I)- hook name
2226 * Search an event's list of hooks to see if there is a hook present that
2227 * has a matching name to the one being looked for.
2230 hook_find_byname(hook_int_head_t
*head
, char *name
)
2234 TAILQ_FOREACH(hi
, head
, hi_entry
) {
2235 if (strcmp(hi
->hi_hook
.h_name
, name
) == 0)
2243 * Function: hook_find
2244 * Returns: internal hook pointer - NULL = Not match
2245 * Parameters: hei(I) - internal event pointer
2246 * h(I) - hook pointer
2248 * Search an event's list of hooks to see if there is already one that
2249 * matches the hook being passed in. Currently the only criteria for a
2250 * successful search here is for the names to be the same.
2253 hook_find(hook_event_int_t
*hei
, hook_t
*h
)
2256 ASSERT(hei
!= NULL
);
2259 return (hook_find_byname(&hei
->hei_head
, h
->h_name
));
2263 * Function: hook_copy
2264 * Returns: internal hook pointer - NULL = Failed
2265 * Parameters: src(I) - hook pointer
2267 * Allocate internal hook block and duplicate incoming hook.
2268 * No locks should be held across this function as it may sleep.
2269 * Because hook_copy() is responsible for the creation of the internal
2270 * hook structure that is used here, it takes on population the structure
2271 * with the kstat information. Note that while the kstat bits are
2272 * seeded here, their installation of the kstats is handled elsewhere.
2275 hook_copy(hook_t
*src
)
2281 ASSERT(src
!= NULL
);
2282 ASSERT(src
->h_name
!= NULL
);
2284 new = (hook_int_t
*)kmem_zalloc(sizeof (*new), KM_SLEEP
);
2287 dst
= &new->hi_hook
;
2291 len
= strlen(src
->h_name
);
2292 dst
->h_name
= kmem_alloc(len
+ 1, KM_SLEEP
);
2293 (void) strcpy(dst
->h_name
, src
->h_name
);
2296 * This is initialised in this manner to make it safer to use the
2297 * same pointer in the kstats field.
2299 dst
->h_hintvalue
= (uintptr_t)"";
2301 if (dst
->h_hint
== HH_BEFORE
|| dst
->h_hint
== HH_AFTER
) {
2302 len
= strlen((char *)src
->h_hintvalue
);
2304 dst
->h_hintvalue
= (uintptr_t)kmem_alloc(len
+ 1,
2306 (void) strcpy((char *)dst
->h_hintvalue
,
2307 (char *)src
->h_hintvalue
);
2315 * Function: hook_init_kstats
2317 * Parameters: hfi(I) - pointer to the family that owns the event.
2318 * hei(I) - pointer to the event that owns this hook
2319 * hi(I) - pointer to the hook for which we create kstats for
2321 * Each hook that is registered with this framework has its own kstats
2322 * set up so that we can provide an easy way in which to observe the
2323 * look of hooks (using the kstat command.) The position is set to 0
2324 * here but is recalculated after we know the insertion has been a
2328 hook_init_kstats(hook_family_int_t
*hfi
, hook_event_int_t
*hei
, hook_int_t
*hi
)
2330 hook_hook_kstat_t
template = {
2331 { "version", KSTAT_DATA_INT32
},
2332 { "flags", KSTAT_DATA_UINT32
},
2333 { "hint", KSTAT_DATA_INT32
},
2334 { "hint_value", KSTAT_DATA_STRING
},
2335 { "position", KSTAT_DATA_INT32
},
2336 { "hook_hits", KSTAT_DATA_UINT64
}
2343 kslen
= strlen(hfi
->hfi_family
.hf_name
) +
2344 strlen(hei
->hei_event
->he_name
) + 2;
2346 hi
->hi_ksname
= kmem_zalloc(kslen
, KM_SLEEP
);
2347 (void) snprintf(hi
->hi_ksname
, kslen
, "%s/%s",
2348 hfi
->hfi_family
.hf_name
, hei
->hei_event
->he_name
);
2350 hks
= hfi
->hfi_stack
;
2351 hi
->hi_kstatp
= kstat_create_netstack(hi
->hi_ksname
, 0,
2352 hi
->hi_hook
.h_name
, "hook", KSTAT_TYPE_NAMED
,
2353 sizeof (hi
->hi_kstats
) / sizeof (kstat_named_t
),
2354 KSTAT_FLAG_VIRTUAL
, hks
->hks_netstackid
);
2356 /* Initialise the kstats for the structure */
2357 bcopy(&template, &hi
->hi_kstats
, sizeof (template));
2358 hi
->hi_kstats
.hook_version
.value
.i32
= hi
->hi_hook
.h_version
;
2359 hi
->hi_kstats
.hook_flags
.value
.ui32
= hi
->hi_hook
.h_flags
;
2360 hi
->hi_kstats
.hook_hint
.value
.i32
= hi
->hi_hook
.h_hint
;
2361 hi
->hi_kstats
.hook_position
.value
.i32
= 0;
2362 hi
->hi_kstats
.hook_hits
.value
.ui64
= 0;
2364 switch (hi
->hi_hook
.h_hint
) {
2367 kstat_named_setstr(&(hi
->hi_kstats
.hook_hintvalue
),
2368 (const char *)hi
->hi_hook
.h_hintvalue
);
2371 kstat_named_setstr(&(hi
->hi_kstats
.hook_hintvalue
),
2372 hook_hintvalue_none
);
2376 if (hi
->hi_kstatp
!= NULL
) {
2377 hi
->hi_kstatp
->ks_data
= (void *)&hi
->hi_kstats
;
2378 hi
->hi_kstatp
->ks_private
=
2379 (void *)(uintptr_t)hks
->hks_netstackid
;
2380 hi
->hi_kstatp
->ks_data_size
+=
2381 KSTAT_NAMED_STR_BUFLEN(&(hi
->hi_kstats
.hook_hintvalue
)) + 1;
2383 kstat_install(hi
->hi_kstatp
);
2387 TAILQ_FOREACH(h
, &hei
->hei_head
, hi_entry
) {
2388 h
->hi_kstats
.hook_position
.value
.ui32
= position
++;
2393 * Function: hook_int_free
2395 * Parameters: hi(I) - internal hook pointer
2397 * Free memory allocated to support a hook.
2400 hook_int_free(hook_int_t
*hi
, netstackid_t stackid
)
2406 /* Free name space */
2407 if (hi
->hi_hook
.h_name
!= NULL
) {
2408 kmem_free(hi
->hi_hook
.h_name
, strlen(hi
->hi_hook
.h_name
) + 1);
2410 if (hi
->hi_ksname
!= NULL
) {
2411 kmem_free(hi
->hi_ksname
, strlen(hi
->hi_ksname
) + 1);
2414 /* Free the name used with the before/after hints. */
2415 switch (hi
->hi_hook
.h_hint
) {
2418 len
= strlen((char *)hi
->hi_hook
.h_hintvalue
);
2420 kmem_free((void *)hi
->hi_hook
.h_hintvalue
, len
+ 1);
2426 if (hi
->hi_kstatp
!= NULL
)
2427 kstat_delete_netstack(hi
->hi_kstatp
, stackid
);
2429 /* Free container */
2430 kmem_free(hi
, sizeof (*hi
));
2434 * Function: hook_alloc
2435 * Returns: hook_t * - pointer to new hook structure
2436 * Parameters: version(I) - version number of the API when compiled
2438 * This function serves as the interface for consumers to obtain a hook_t
2439 * structure. At this point in time, there is only a single "version" of
2440 * it, leading to a straight forward function. In a perfect world the
2441 * h_vesion would be a protected data structure member, but C isn't that
2445 hook_alloc(const int h_version
)
2449 h
= kmem_zalloc(sizeof (hook_t
), KM_SLEEP
);
2450 h
->h_version
= h_version
;
2455 * Function: hook_free
2457 * Parameters: h(I) - external hook pointer
2459 * This function only free's memory allocated with hook_alloc(), so that if
2460 * (for example) kernel memory was allocated for h_name, this needs to be
2461 * free'd before calling hook_free().
2464 hook_free(hook_t
*h
)
2466 kmem_free(h
, sizeof (*h
));
2470 * Function: hook_notify_register
2471 * Returns: int - 0 = success, else failure
2472 * Parameters: head(I) - top of the list of callbacks
2473 * callback(I) - function to be called
2474 * arg(I) - arg to pass back to the function
2476 * This function implements the modification of the list of callbacks
2477 * that are registered when someone wants to be advised of a change
2478 * that has happened.
2481 hook_notify_register(hook_notify_head_t
*head
, hook_notify_fn_t callback
,
2486 TAILQ_FOREACH(hn
, head
, hn_entry
) {
2487 if (hn
->hn_func
== callback
) {
2492 hn
= (hook_notify_t
*)kmem_alloc(sizeof (*hn
), KM_SLEEP
);
2493 hn
->hn_func
= callback
;
2495 TAILQ_INSERT_TAIL(head
, hn
, hn_entry
);
2501 * Function: hook_notify_unregister
2502 * Returns: int - 0 = success, else failure
2503 * Parameters: stackid(I) - netstack identifier
2504 * callback(I) - function to be called
2505 * parg(O) - pointer to storage for pointer
2507 * When calling this function, the provision of a valid pointer in parg
2508 * allows the caller to be made aware of what argument the hook function
2509 * was expecting. This then allows the simulation of HN_UNREGISTER events
2510 * when a notify-unregister is performed.
2513 hook_notify_unregister(hook_notify_head_t
*head
,
2514 hook_notify_fn_t callback
, void **parg
)
2518 ASSERT(parg
!= NULL
);
2520 TAILQ_FOREACH(hn
, head
, hn_entry
) {
2521 if (hn
->hn_func
== callback
)
2530 TAILQ_REMOVE(head
, hn
, hn_entry
);
2532 kmem_free(hn
, sizeof (*hn
));
2538 * Function: hook_notify_run
2540 * Parameters: head(I) - top of the list of callbacks
2541 * family(I) - name of the hook family that owns the event
2542 * event(I) - name of the event being changed
2543 * name(I) - name of the object causing change
2544 * cmd(I) - either HN_UNREGISTER or HN_REGISTER
2546 * This function walks through the list of registered callbacks and
2547 * executes each one, passing back the arg supplied when registered
2548 * and the name of the family (that owns the event), event (the thing
2549 * to which we're making a change) and finally a name that describes
2550 * what is being added or removed, as indicated by cmd.
2552 * This function does not acquire or release any lock as it is required
2553 * that code calling it do so before hand. The use of hook_notify_head_t
2554 * is protected by the use of flagwait_t in the structures that own this
2555 * list and with the use of the FWF_ADD/DEL_ACTIVE flags.
2558 hook_notify_run(hook_notify_head_t
*head
, char *family
, char *event
,
2559 char *name
, hook_notify_cmd_t cmd
)
2563 TAILQ_FOREACH(hn
, head
, hn_entry
) {
2564 (*hn
->hn_func
)(cmd
, hn
->hn_arg
, family
, event
, name
);