10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53448Sdh155122 * Common Development and Distribution License (the "License").
63448Sdh155122 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12016SGirish.Moodalbail@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <inet/common.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/stropts.h>
320Sstevel@tonic-gate #include <inet/mi.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <sys/cmn_err.h>
350Sstevel@tonic-gate #include <sys/policy.h>
360Sstevel@tonic-gate #include <inet/nd.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate /* Free the table pointed to by 'ndp' */
390Sstevel@tonic-gate void
nd_free(caddr_t * nd_pparam)400Sstevel@tonic-gate nd_free(caddr_t *nd_pparam)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate ND *nd;
430Sstevel@tonic-gate
440Sstevel@tonic-gate if ((nd = (ND *)(*nd_pparam)) != NULL) {
450Sstevel@tonic-gate if (nd->nd_tbl)
460Sstevel@tonic-gate mi_free((char *)nd->nd_tbl);
470Sstevel@tonic-gate mi_free((char *)nd);
480Sstevel@tonic-gate *nd_pparam = NULL;
490Sstevel@tonic-gate }
500Sstevel@tonic-gate }
510Sstevel@tonic-gate
520Sstevel@tonic-gate int
nd_getset(queue_t * q,caddr_t nd_param,MBLKP mp)530Sstevel@tonic-gate nd_getset(queue_t *q, caddr_t nd_param, MBLKP mp)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate int err;
560Sstevel@tonic-gate IOCP iocp;
570Sstevel@tonic-gate MBLKP mp1;
580Sstevel@tonic-gate ND *nd;
590Sstevel@tonic-gate NDE *nde;
600Sstevel@tonic-gate char *valp;
610Sstevel@tonic-gate long avail;
620Sstevel@tonic-gate
630Sstevel@tonic-gate if (!nd_param)
640Sstevel@tonic-gate return (B_FALSE);
650Sstevel@tonic-gate nd = (ND *)nd_param;
660Sstevel@tonic-gate iocp = (IOCP)mp->b_rptr;
670Sstevel@tonic-gate if (iocp->ioc_count == 0 || !(mp1 = mp->b_cont)) {
680Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK;
690Sstevel@tonic-gate iocp->ioc_count = 0;
700Sstevel@tonic-gate iocp->ioc_error = EINVAL;
710Sstevel@tonic-gate return (B_TRUE);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * NOTE - logic throughout nd_xxx assumes single data block for ioctl.
750Sstevel@tonic-gate * However, existing code sends in some big buffers.
760Sstevel@tonic-gate */
770Sstevel@tonic-gate avail = iocp->ioc_count;
780Sstevel@tonic-gate if (mp1->b_cont) {
790Sstevel@tonic-gate freemsg(mp1->b_cont);
800Sstevel@tonic-gate mp1->b_cont = NULL;
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate mp1->b_datap->db_lim[-1] = '\0'; /* Force null termination */
840Sstevel@tonic-gate valp = (char *)mp1->b_rptr;
850Sstevel@tonic-gate for (nde = nd->nd_tbl; ; nde++) {
860Sstevel@tonic-gate if (!nde->nde_name)
870Sstevel@tonic-gate return (B_FALSE);
880Sstevel@tonic-gate if (mi_strcmp(nde->nde_name, valp) == 0)
890Sstevel@tonic-gate break;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate err = EINVAL;
920Sstevel@tonic-gate while (*valp++)
930Sstevel@tonic-gate noop;
940Sstevel@tonic-gate if (!*valp || valp >= (char *)mp1->b_wptr)
950Sstevel@tonic-gate valp = NULL;
960Sstevel@tonic-gate switch (iocp->ioc_cmd) {
970Sstevel@tonic-gate case ND_GET:
980Sstevel@tonic-gate /* We overwrite the name/value with the reply data */
99*12016SGirish.Moodalbail@Sun.COM mp1->b_wptr = mp1->b_rptr;
1000Sstevel@tonic-gate err = (*nde->nde_get_pfi)(q, mp1, nde->nde_data, iocp->ioc_cr);
1010Sstevel@tonic-gate if (!err) {
1020Sstevel@tonic-gate int size_out;
1030Sstevel@tonic-gate int excess;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate iocp->ioc_rval = 0;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /* Tack on the null */
1080Sstevel@tonic-gate (void) mi_mpprintf_putc((char *)mp1, '\0');
1090Sstevel@tonic-gate size_out = msgdsize(mp1);
1100Sstevel@tonic-gate excess = size_out - avail;
1110Sstevel@tonic-gate if (excess > 0) {
1120Sstevel@tonic-gate iocp->ioc_rval = size_out;
1130Sstevel@tonic-gate size_out -= excess;
1140Sstevel@tonic-gate (void) adjmsg(mp1, -(excess + 1));
1150Sstevel@tonic-gate (void) mi_mpprintf_putc((char *)mp1, '\0');
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate iocp->ioc_count = size_out;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate break;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate case ND_SET:
1220Sstevel@tonic-gate if (valp) {
1230Sstevel@tonic-gate if ((iocp->ioc_cr != NULL) &&
1243448Sdh155122 ((err = secpolicy_ip_config(iocp->ioc_cr, B_FALSE))
125*12016SGirish.Moodalbail@Sun.COM == 0)) {
1260Sstevel@tonic-gate err = (*nde->nde_set_pfi)(q, mp1, valp,
1270Sstevel@tonic-gate nde->nde_data, iocp->ioc_cr);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate iocp->ioc_count = 0;
1300Sstevel@tonic-gate freemsg(mp1);
1310Sstevel@tonic-gate mp->b_cont = NULL;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate break;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate default:
1360Sstevel@tonic-gate break;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate iocp->ioc_error = err;
1390Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK;
1400Sstevel@tonic-gate return (B_TRUE);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate /* ARGSUSED */
1440Sstevel@tonic-gate int
nd_get_default(queue_t * q,MBLKP mp,caddr_t data,cred_t * ioc_cr)1450Sstevel@tonic-gate nd_get_default(queue_t *q, MBLKP mp, caddr_t data, cred_t *ioc_cr)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate return (EACCES);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate * This routine may be used as the get dispatch routine in nd tables
1520Sstevel@tonic-gate * for long variables. To use this routine instead of a module
1530Sstevel@tonic-gate * specific routine, call nd_load as
1540Sstevel@tonic-gate * nd_load(&nd_ptr, "name", nd_get_long, set_pfi, &long_variable)
1550Sstevel@tonic-gate * The name of the variable followed by a space and the value of the
1560Sstevel@tonic-gate * variable will be printed in response to a get_status call.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate /* ARGSUSED */
1590Sstevel@tonic-gate int
nd_get_long(queue_t * q,MBLKP mp,caddr_t data,cred_t * ioc_cr)1600Sstevel@tonic-gate nd_get_long(queue_t *q, MBLKP mp, caddr_t data, cred_t *ioc_cr)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate ulong_t *lp;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate lp = (ulong_t *)data;
1650Sstevel@tonic-gate (void) mi_mpprintf(mp, "%ld", *lp);
1660Sstevel@tonic-gate return (0);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /* ARGSUSED */
1700Sstevel@tonic-gate int
nd_get_names(queue_t * q,MBLKP mp,caddr_t nd_param,cred_t * ioc_cr)1710Sstevel@tonic-gate nd_get_names(queue_t *q, MBLKP mp, caddr_t nd_param, cred_t *ioc_cr)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate ND *nd;
1740Sstevel@tonic-gate NDE *nde;
1750Sstevel@tonic-gate char *rwtag;
1760Sstevel@tonic-gate boolean_t get_ok, set_ok;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate nd = (ND *)nd_param;
1790Sstevel@tonic-gate if (!nd)
1800Sstevel@tonic-gate return (ENOENT);
1810Sstevel@tonic-gate for (nde = nd->nd_tbl; nde->nde_name; nde++) {
1820Sstevel@tonic-gate get_ok = nde->nde_get_pfi != nd_get_default;
1830Sstevel@tonic-gate set_ok = nde->nde_set_pfi != nd_set_default;
1840Sstevel@tonic-gate if (get_ok) {
1850Sstevel@tonic-gate if (set_ok)
1860Sstevel@tonic-gate rwtag = "read and write";
1870Sstevel@tonic-gate else
1880Sstevel@tonic-gate rwtag = "read only";
1890Sstevel@tonic-gate } else if (set_ok)
1900Sstevel@tonic-gate rwtag = "write only";
1910Sstevel@tonic-gate else
1920Sstevel@tonic-gate rwtag = "no read or write";
1930Sstevel@tonic-gate (void) mi_mpprintf(mp, "%s (%s)", nde->nde_name, rwtag);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate return (0);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * Load 'name' into the named dispatch table pointed to by 'ndp'.
2000Sstevel@tonic-gate * 'ndp' should be the address of a char pointer cell. If the table
2010Sstevel@tonic-gate * does not exist (*ndp == 0), a new table is allocated and 'ndp'
2020Sstevel@tonic-gate * is stuffed. If there is not enough space in the table for a new
2030Sstevel@tonic-gate * entry, more space is allocated.
2043448Sdh155122 * Never fails due to memory allocation failures.
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate boolean_t
nd_load(caddr_t * nd_pparam,char * name,ndgetf_t get_pfi,ndsetf_t set_pfi,caddr_t data)2070Sstevel@tonic-gate nd_load(caddr_t *nd_pparam, char *name, ndgetf_t get_pfi, ndsetf_t set_pfi,
2080Sstevel@tonic-gate caddr_t data)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate ND *nd;
2110Sstevel@tonic-gate NDE *nde;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (!nd_pparam)
2140Sstevel@tonic-gate return (B_FALSE);
2150Sstevel@tonic-gate if ((nd = (ND *)(*nd_pparam)) == NULL) {
2163448Sdh155122 nd = (ND *)mi_alloc_sleep(sizeof (ND), BPRI_MED);
2170Sstevel@tonic-gate bzero((caddr_t)nd, sizeof (ND));
2180Sstevel@tonic-gate *nd_pparam = (caddr_t)nd;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate if (nd->nd_tbl) {
2210Sstevel@tonic-gate for (nde = nd->nd_tbl; nde->nde_name; nde++) {
2220Sstevel@tonic-gate if (mi_strcmp(name, nde->nde_name) == 0)
2230Sstevel@tonic-gate goto fill_it;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate if (nd->nd_free_count <= 1) {
2273448Sdh155122 nde = (NDE *)mi_alloc_sleep(nd->nd_size +
2283448Sdh155122 NDE_ALLOC_SIZE, BPRI_MED);
2290Sstevel@tonic-gate bzero((char *)nde, nd->nd_size + NDE_ALLOC_SIZE);
2300Sstevel@tonic-gate nd->nd_free_count += NDE_ALLOC_COUNT;
2310Sstevel@tonic-gate if (nd->nd_tbl) {
2320Sstevel@tonic-gate bcopy((char *)nd->nd_tbl, (char *)nde, nd->nd_size);
2330Sstevel@tonic-gate mi_free((char *)nd->nd_tbl);
2340Sstevel@tonic-gate } else {
2350Sstevel@tonic-gate nd->nd_free_count--;
2360Sstevel@tonic-gate nde->nde_name = "?";
2370Sstevel@tonic-gate nde->nde_get_pfi = nd_get_names;
2380Sstevel@tonic-gate nde->nde_set_pfi = nd_set_default;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate nde->nde_data = (caddr_t)nd;
2410Sstevel@tonic-gate nd->nd_tbl = nde;
2420Sstevel@tonic-gate nd->nd_size += NDE_ALLOC_SIZE;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate for (nde = nd->nd_tbl; nde->nde_name; nde++)
2450Sstevel@tonic-gate noop;
2460Sstevel@tonic-gate nd->nd_free_count--;
2470Sstevel@tonic-gate fill_it:
2480Sstevel@tonic-gate nde->nde_name = name;
2490Sstevel@tonic-gate nde->nde_get_pfi = get_pfi ? get_pfi : nd_get_default;
2500Sstevel@tonic-gate nde->nde_set_pfi = set_pfi ? set_pfi : nd_set_default;
2510Sstevel@tonic-gate nde->nde_data = data;
2520Sstevel@tonic-gate return (B_TRUE);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * Unload 'name' from the named dispatch table. If the table does not
2570Sstevel@tonic-gate * exist, I return. I do not free up space, but I do raise the
2580Sstevel@tonic-gate * free count so future nd_load()s don't take as much memory.
2590Sstevel@tonic-gate */
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate void
nd_unload(caddr_t * nd_pparam,char * name)2620Sstevel@tonic-gate nd_unload(caddr_t *nd_pparam, char *name)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate ND *nd;
2650Sstevel@tonic-gate NDE *nde;
2660Sstevel@tonic-gate boolean_t foundit = B_FALSE;
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /* My apologies for the in-boolean assignment. */
2690Sstevel@tonic-gate if (nd_pparam == NULL || (nd = (ND *)(*nd_pparam)) == NULL ||
2700Sstevel@tonic-gate nd->nd_tbl == NULL)
2710Sstevel@tonic-gate return;
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate for (nde = nd->nd_tbl; nde->nde_name != NULL; nde++) {
2740Sstevel@tonic-gate if (foundit)
2750Sstevel@tonic-gate *(nde - 1) = *nde;
2760Sstevel@tonic-gate if (mi_strcmp(name, nde->nde_name) == 0)
2770Sstevel@tonic-gate foundit = B_TRUE;
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate if (foundit)
2800Sstevel@tonic-gate bzero(nde - 1, sizeof (NDE));
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate /* ARGSUSED */
2840Sstevel@tonic-gate int
nd_set_default(queue_t * q,MBLKP mp,char * value,caddr_t data,cred_t * ioc_cr)2850Sstevel@tonic-gate nd_set_default(queue_t *q, MBLKP mp, char *value, caddr_t data, cred_t *ioc_cr)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate return (EACCES);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /* ARGSUSED */
2910Sstevel@tonic-gate int
nd_set_long(queue_t * q,MBLKP mp,char * value,caddr_t data,cred_t * ioc_cr)2920Sstevel@tonic-gate nd_set_long(queue_t *q, MBLKP mp, char *value, caddr_t data, cred_t *ioc_cr)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate ulong_t *lp;
2950Sstevel@tonic-gate long new_value;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (ddi_strtol(value, NULL, 10, &new_value) != 0)
2980Sstevel@tonic-gate return (EINVAL);
2990Sstevel@tonic-gate lp = (ulong_t *)data;
3000Sstevel@tonic-gate *lp = new_value;
3010Sstevel@tonic-gate return (0);
3020Sstevel@tonic-gate }
303