xref: /illumos-gate/usr/src/uts/common/sys/hook.h (revision 7ddc9b1a)
1381a2a9aSdr /*
2381a2a9aSdr  * CDDL HEADER START
3381a2a9aSdr  *
4381a2a9aSdr  * The contents of this file are subject to the terms of the
5381a2a9aSdr  * Common Development and Distribution License (the "License").
6381a2a9aSdr  * You may not use this file except in compliance with the License.
7381a2a9aSdr  *
8381a2a9aSdr  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9381a2a9aSdr  * or http://www.opensolaris.org/os/licensing.
10381a2a9aSdr  * See the License for the specific language governing permissions
11381a2a9aSdr  * and limitations under the License.
12381a2a9aSdr  *
13381a2a9aSdr  * When distributing Covered Code, include this CDDL HEADER in each
14381a2a9aSdr  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15381a2a9aSdr  * If applicable, add the following below this CDDL HEADER, with the
16381a2a9aSdr  * fields enclosed by brackets "[]" replaced with your own identifying
17381a2a9aSdr  * information: Portions Copyright [yyyy] [name of copyright owner]
18381a2a9aSdr  *
19381a2a9aSdr  * CDDL HEADER END
20381a2a9aSdr  */
21381a2a9aSdr /*
22*7ddc9b1aSDarren Reed  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23381a2a9aSdr  * Use is subject to license terms.
24381a2a9aSdr  */
25381a2a9aSdr 
26381a2a9aSdr /*
27381a2a9aSdr  * This file includes definitions of kernel hook framework components
28381a2a9aSdr  */
29381a2a9aSdr 
30381a2a9aSdr #ifndef _SYS_HOOK_H
31381a2a9aSdr #define	_SYS_HOOK_H
32381a2a9aSdr 
33381a2a9aSdr #include <sys/queue.h>
34f4b3ec61Sdh #include <sys/netstack.h>
35381a2a9aSdr 
36381a2a9aSdr #ifdef	__cplusplus
37381a2a9aSdr extern "C" {
38381a2a9aSdr #endif
39381a2a9aSdr 
40381a2a9aSdr /*
41381a2a9aSdr  * Definition exposed to hook provider and consumer
42381a2a9aSdr  */
43381a2a9aSdr 
44381a2a9aSdr #define	HOOK_VERSION	1
45381a2a9aSdr 
46381a2a9aSdr typedef uintptr_t hook_data_t;
47381a2a9aSdr 
48381a2a9aSdr struct hook_event_int;
49381a2a9aSdr typedef struct hook_event_int *hook_event_token_t;
50*7ddc9b1aSDarren Reed struct hook_int;
51*7ddc9b1aSDarren Reed typedef struct hook_int *hook_token_t;
52*7ddc9b1aSDarren Reed 
53*7ddc9b1aSDarren Reed typedef int (* hook_func_t)(hook_event_token_t, hook_data_t, void *);
54*7ddc9b1aSDarren Reed 
55*7ddc9b1aSDarren Reed /*
56*7ddc9b1aSDarren Reed  * A hook_notify_cmd_t is given as an argument to functions called as part of
57*7ddc9b1aSDarren Reed  * the notify callbacks that have been registered firing.
58*7ddc9b1aSDarren Reed  */
59*7ddc9b1aSDarren Reed typedef enum hook_notify_cmd_e {
60*7ddc9b1aSDarren Reed 	HN_NONE = 0,
61*7ddc9b1aSDarren Reed 	HN_REGISTER = 1,
62*7ddc9b1aSDarren Reed 	HN_UNREGISTER = 2
63*7ddc9b1aSDarren Reed } hook_notify_cmd_t;
64381a2a9aSdr 
65*7ddc9b1aSDarren Reed /*
66*7ddc9b1aSDarren Reed  *
67*7ddc9b1aSDarren Reed  */
68*7ddc9b1aSDarren Reed typedef enum hook_hint_e {
69*7ddc9b1aSDarren Reed 	HH_NONE = 0,
70*7ddc9b1aSDarren Reed 	HH_FIRST,
71*7ddc9b1aSDarren Reed 	HH_LAST,
72*7ddc9b1aSDarren Reed 	HH_BEFORE,
73*7ddc9b1aSDarren Reed 	HH_AFTER
74*7ddc9b1aSDarren Reed } hook_hint_t;
75381a2a9aSdr 
76381a2a9aSdr /*
77381a2a9aSdr  * Hook
78381a2a9aSdr  */
79*7ddc9b1aSDarren Reed typedef struct hook_s {
80*7ddc9b1aSDarren Reed 	int		h_version;
81381a2a9aSdr 	hook_func_t	h_func;		/* callback func */
82381a2a9aSdr 	char		*h_name;	/* name of this hook */
83*7ddc9b1aSDarren Reed 	uint_t		h_flags;	/* extra hook properties */
84*7ddc9b1aSDarren Reed 	hook_hint_t	h_hint;		/* What type of hint is hintvalue */
85*7ddc9b1aSDarren Reed 	uintptr_t	h_hintvalue;
86*7ddc9b1aSDarren Reed 	void		*h_arg;		/* value to pass back into the hook */
87381a2a9aSdr } hook_t;
88381a2a9aSdr 
89*7ddc9b1aSDarren Reed #define	HOOK_INIT(x, fn, r, a)		\
90381a2a9aSdr 	do {					\
91*7ddc9b1aSDarren Reed 		(x) = hook_alloc(HOOK_VERSION);	\
92381a2a9aSdr 		(x)->h_func = (fn);		\
93381a2a9aSdr 		(x)->h_name = (r);		\
94381a2a9aSdr 		(x)->h_flags = 0;		\
95*7ddc9b1aSDarren Reed 		(x)->h_hint = HH_NONE;		\
96*7ddc9b1aSDarren Reed 		(x)->h_hintvalue = 0;		\
97*7ddc9b1aSDarren Reed 		(x)->h_arg = (a);		\
98381a2a9aSdr 		_NOTE(CONSTCOND)		\
99381a2a9aSdr 	} while (0)
100381a2a9aSdr 
101381a2a9aSdr /*
102381a2a9aSdr  * Family
103381a2a9aSdr  */
104*7ddc9b1aSDarren Reed typedef struct hook_family_s {
105*7ddc9b1aSDarren Reed 	int		hf_version;	/* version number */
106381a2a9aSdr 	char		*hf_name;	/* family name */
107381a2a9aSdr } hook_family_t;
108381a2a9aSdr 
109381a2a9aSdr #define	HOOK_FAMILY_INIT(x, y)			\
110381a2a9aSdr 	do {					\
111381a2a9aSdr 		(x)->hf_version = HOOK_VERSION;	\
112381a2a9aSdr 		(x)->hf_name = (y);		\
113381a2a9aSdr 		_NOTE(CONSTCOND)		\
114381a2a9aSdr 	} while (0)
115381a2a9aSdr 
116381a2a9aSdr /*
117381a2a9aSdr  * Event
118381a2a9aSdr  */
119*7ddc9b1aSDarren Reed typedef struct hook_event_s {
120*7ddc9b1aSDarren Reed 	int		he_version;
121381a2a9aSdr 	char		*he_name;	/* name of this hook list */
122381a2a9aSdr 	int		he_flags;	/* 1 = multiple entries allowed */
123381a2a9aSdr 	boolean_t	he_interested;	/* true if callback exist */
124381a2a9aSdr } hook_event_t;
125381a2a9aSdr 
126381a2a9aSdr #define	HOOK_RDONLY	0x1		/* Callbacks must not change data */
127381a2a9aSdr 					/* Multiple callbacks are allowed */
128381a2a9aSdr 
129381a2a9aSdr #define	HOOK_EVENT_INIT(x, y)			\
130381a2a9aSdr 	do {					\
131381a2a9aSdr 		(x)->he_version = HOOK_VERSION;	\
132381a2a9aSdr 		(x)->he_name = (y);		\
133381a2a9aSdr 		(x)->he_flags = 0;		\
134381a2a9aSdr 		(x)->he_interested = B_FALSE;	\
135381a2a9aSdr 		_NOTE(CONSTCOND)		\
136381a2a9aSdr 	} while (0)
137381a2a9aSdr 
138*7ddc9b1aSDarren Reed typedef int (* hook_notify_fn_t)(hook_notify_cmd_t, void *, const char *,
139*7ddc9b1aSDarren Reed     const char *, const char *);
140*7ddc9b1aSDarren Reed 
141*7ddc9b1aSDarren Reed extern	hook_t		*hook_alloc(const int version);
142*7ddc9b1aSDarren Reed extern	void		hook_free(hook_t *);
143*7ddc9b1aSDarren Reed 
144381a2a9aSdr #ifdef	__cplusplus
145381a2a9aSdr }
146381a2a9aSdr #endif
147381a2a9aSdr 
148381a2a9aSdr #endif /* _SYS_HOOK_H */
149