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 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 /* Copyright (c) 1990 Mentat Inc. */
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <inet/common.h>
30 #include <sys/stream.h>
31 #include <sys/stropts.h>
33 #include <sys/sunddi.h>
34 #include <sys/cmn_err.h>
35 #include <sys/policy.h>
38 /* Free the table pointed to by 'ndp' */
40 nd_free(caddr_t
*nd_pparam
)
44 if ((nd
= (ND
*)(*nd_pparam
)) != NULL
) {
46 mi_free((char *)nd
->nd_tbl
);
53 nd_getset(queue_t
*q
, caddr_t nd_param
, MBLKP mp
)
66 iocp
= (IOCP
)mp
->b_rptr
;
67 if (iocp
->ioc_count
== 0 || !(mp1
= mp
->b_cont
)) {
68 mp
->b_datap
->db_type
= M_IOCACK
;
70 iocp
->ioc_error
= EINVAL
;
74 * NOTE - logic throughout nd_xxx assumes single data block for ioctl.
75 * However, existing code sends in some big buffers.
77 avail
= iocp
->ioc_count
;
83 mp1
->b_datap
->db_lim
[-1] = '\0'; /* Force null termination */
84 valp
= (char *)mp1
->b_rptr
;
85 for (nde
= nd
->nd_tbl
; ; nde
++) {
88 if (mi_strcmp(nde
->nde_name
, valp
) == 0)
94 if (!*valp
|| valp
>= (char *)mp1
->b_wptr
)
96 switch (iocp
->ioc_cmd
) {
98 /* We overwrite the name/value with the reply data */
99 mp1
->b_wptr
= mp1
->b_rptr
;
100 err
= (*nde
->nde_get_pfi
)(q
, mp1
, nde
->nde_data
, iocp
->ioc_cr
);
107 /* Tack on the null */
108 (void) mi_mpprintf_putc((char *)mp1
, '\0');
109 size_out
= msgdsize(mp1
);
110 excess
= size_out
- avail
;
112 iocp
->ioc_rval
= size_out
;
114 (void) adjmsg(mp1
, -(excess
+ 1));
115 (void) mi_mpprintf_putc((char *)mp1
, '\0');
117 iocp
->ioc_count
= size_out
;
123 if ((iocp
->ioc_cr
!= NULL
) &&
124 ((err
= secpolicy_ip_config(iocp
->ioc_cr
, B_FALSE
))
126 err
= (*nde
->nde_set_pfi
)(q
, mp1
, valp
,
127 nde
->nde_data
, iocp
->ioc_cr
);
138 iocp
->ioc_error
= err
;
139 mp
->b_datap
->db_type
= M_IOCACK
;
145 nd_get_default(queue_t
*q
, MBLKP mp
, caddr_t data
, cred_t
*ioc_cr
)
151 * This routine may be used as the get dispatch routine in nd tables
152 * for long variables. To use this routine instead of a module
153 * specific routine, call nd_load as
154 * nd_load(&nd_ptr, "name", nd_get_long, set_pfi, &long_variable)
155 * The name of the variable followed by a space and the value of the
156 * variable will be printed in response to a get_status call.
160 nd_get_long(queue_t
*q
, MBLKP mp
, caddr_t data
, cred_t
*ioc_cr
)
164 lp
= (ulong_t
*)data
;
165 (void) mi_mpprintf(mp
, "%ld", *lp
);
171 nd_get_names(queue_t
*q
, MBLKP mp
, caddr_t nd_param
, cred_t
*ioc_cr
)
176 boolean_t get_ok
, set_ok
;
181 for (nde
= nd
->nd_tbl
; nde
->nde_name
; nde
++) {
182 get_ok
= nde
->nde_get_pfi
!= nd_get_default
;
183 set_ok
= nde
->nde_set_pfi
!= nd_set_default
;
186 rwtag
= "read and write";
190 rwtag
= "write only";
192 rwtag
= "no read or write";
193 (void) mi_mpprintf(mp
, "%s (%s)", nde
->nde_name
, rwtag
);
199 * Load 'name' into the named dispatch table pointed to by 'ndp'.
200 * 'ndp' should be the address of a char pointer cell. If the table
201 * does not exist (*ndp == 0), a new table is allocated and 'ndp'
202 * is stuffed. If there is not enough space in the table for a new
203 * entry, more space is allocated.
204 * Never fails due to memory allocation failures.
207 nd_load(caddr_t
*nd_pparam
, char *name
, ndgetf_t get_pfi
, ndsetf_t set_pfi
,
215 if ((nd
= (ND
*)(*nd_pparam
)) == NULL
) {
216 nd
= (ND
*)mi_alloc_sleep(sizeof (ND
), BPRI_MED
);
217 bzero((caddr_t
)nd
, sizeof (ND
));
218 *nd_pparam
= (caddr_t
)nd
;
221 for (nde
= nd
->nd_tbl
; nde
->nde_name
; nde
++) {
222 if (mi_strcmp(name
, nde
->nde_name
) == 0)
226 if (nd
->nd_free_count
<= 1) {
227 nde
= (NDE
*)mi_alloc_sleep(nd
->nd_size
+
228 NDE_ALLOC_SIZE
, BPRI_MED
);
229 bzero((char *)nde
, nd
->nd_size
+ NDE_ALLOC_SIZE
);
230 nd
->nd_free_count
+= NDE_ALLOC_COUNT
;
232 bcopy((char *)nd
->nd_tbl
, (char *)nde
, nd
->nd_size
);
233 mi_free((char *)nd
->nd_tbl
);
237 nde
->nde_get_pfi
= nd_get_names
;
238 nde
->nde_set_pfi
= nd_set_default
;
240 nde
->nde_data
= (caddr_t
)nd
;
242 nd
->nd_size
+= NDE_ALLOC_SIZE
;
244 for (nde
= nd
->nd_tbl
; nde
->nde_name
; nde
++)
248 nde
->nde_name
= name
;
249 nde
->nde_get_pfi
= get_pfi
? get_pfi
: nd_get_default
;
250 nde
->nde_set_pfi
= set_pfi
? set_pfi
: nd_set_default
;
251 nde
->nde_data
= data
;
256 * Unload 'name' from the named dispatch table. If the table does not
257 * exist, I return. I do not free up space, but I do raise the
258 * free count so future nd_load()s don't take as much memory.
262 nd_unload(caddr_t
*nd_pparam
, char *name
)
266 boolean_t foundit
= B_FALSE
;
268 /* My apologies for the in-boolean assignment. */
269 if (nd_pparam
== NULL
|| (nd
= (ND
*)(*nd_pparam
)) == NULL
||
273 for (nde
= nd
->nd_tbl
; nde
->nde_name
!= NULL
; nde
++) {
276 if (mi_strcmp(name
, nde
->nde_name
) == 0)
280 bzero(nde
- 1, sizeof (NDE
));
285 nd_set_default(queue_t
*q
, MBLKP mp
, char *value
, caddr_t data
, cred_t
*ioc_cr
)
292 nd_set_long(queue_t
*q
, MBLKP mp
, char *value
, caddr_t data
, cred_t
*ioc_cr
)
297 if (ddi_strtol(value
, NULL
, 10, &new_value
) != 0)
299 lp
= (ulong_t
*)data
;