xref: /titanic_44/usr/src/uts/common/inet/inetddi.c (revision 0f1702c5201310f0529cd5abb77652e5e9b241b6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f4b3ec61Sdh155122  * Common Development and Distribution License (the "License").
6f4b3ec61Sdh155122  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/stat.h>
287c478bd9Sstevel@tonic-gate #include <sys/stream.h>
297c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/priv_names.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * This file contains generic goo needed to hook the STREAMS modules and
357c478bd9Sstevel@tonic-gate  * drivers that live under uts/common/inet into the DDI.  In order to use it,
367c478bd9Sstevel@tonic-gate  * each module/driver should #define the symbols below (as appropriate) and
377c478bd9Sstevel@tonic-gate  * then #include this source file; see the other uts/common/inet/<star>ddi.c
387c478bd9Sstevel@tonic-gate  * files for examples of this in action.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * The symbols that all modules and drivers must define are:
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *	INET_NAME	 The name of the module/driver.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The symbols that all modules must define are:
457c478bd9Sstevel@tonic-gate  *
46fc80c0dfSnordmark  *	INET_MODSTRTAB	 The name of the `streamtab' structure for this module.
477c478bd9Sstevel@tonic-gate  *	INET_MODDESC	 The one-line description for this module.
487c478bd9Sstevel@tonic-gate  *	INET_MODMTFLAGS  The mt-streams(9F) flags for the module.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * The symbols that all drivers must define are:
517c478bd9Sstevel@tonic-gate  *
52fc80c0dfSnordmark  *	INET_DEVSTRTAB	 The name of the `streamtab' structure for this driver.
537c478bd9Sstevel@tonic-gate  *	INET_DEVDESC	 The one-line description for this driver.
547c478bd9Sstevel@tonic-gate  *	INET_DEVMTFLAGS  The mt-streams(9F) flags for the driver.
557c478bd9Sstevel@tonic-gate  *	INET_DEVMINOR	 The minor number of the driver (usually 0).
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * Drivers that need to masquerade as IP should set INET_DEVMTFLAGS to
58fc80c0dfSnordmark  * IP_DEVMTFLAGS and set INET_DEVSTRTAB to ipinfo.
59*0f1702c5SYu Xiangning  *
60*0f1702c5SYu Xiangning  * The symbols that all socket modules must define are:
61*0f1702c5SYu Xiangning  *
62*0f1702c5SYu Xiangning  *	INET_SOCKDESC	The one-line description for this socket module
63*0f1702c5SYu Xiangning  *	INET_SOCK_PROTO_CREATE_FUNC The function used to create PCBs
64*0f1702c5SYu Xiangning  *
65*0f1702c5SYu Xiangning  * In addition, socket modules that can be converted to TPI must define:
66*0f1702c5SYu Xiangning  *
67*0f1702c5SYu Xiangning  *	INET_SOCK_PROTO_FB_FUNC The function used to fallback to TPI
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
70fc80c0dfSnordmark #if	!defined(INET_NAME)
717c478bd9Sstevel@tonic-gate #error inetddi.c: INET_NAME is not defined!
72*0f1702c5SYu Xiangning #elif	!defined(INET_DEVDESC) && !defined(INET_MODDESC) && \
73*0f1702c5SYu Xiangning 	!defined(INET_SOCKDESC)
74*0f1702c5SYu Xiangning #error inetddi.c: at least one of INET_DEVDESC or INET_MODDESC or \
75*0f1702c5SYu Xiangning INET_SOCKDESC must be defined!
76fc80c0dfSnordmark #elif	defined(INET_DEVDESC) && !defined(INET_DEVSTRTAB)
77fc80c0dfSnordmark #error inetddi.c: INET_DEVDESC is defined but INET_DEVSTRTAB is not!
787c478bd9Sstevel@tonic-gate #elif	defined(INET_DEVDESC) && !defined(INET_DEVMTFLAGS)
797c478bd9Sstevel@tonic-gate #error inetddi.c: INET_DEVDESC is defined but INET_DEVMTFLAGS is not!
807c478bd9Sstevel@tonic-gate #elif	defined(INET_DEVDESC) && !defined(INET_DEVMINOR)
817c478bd9Sstevel@tonic-gate #error inetddi.c: INET_DEVDESC is defined but INET_DEVMINOR is not!
82fc80c0dfSnordmark #elif	defined(INET_MODDESC) && !defined(INET_MODSTRTAB)
83fc80c0dfSnordmark #error inetddi.c: INET_MODDESC is defined but INET_MODSTRTAB is not!
847c478bd9Sstevel@tonic-gate #elif	defined(INET_MODDESC) && !defined(INET_MODMTFLAGS)
857c478bd9Sstevel@tonic-gate #error inetddi.c: INET_MODDESC is defined but INET_MODMTFLAGS is not!
86*0f1702c5SYu Xiangning #elif	defined(INET_SOCKDESC) && !defined(SOCKMOD_VERSION)
87*0f1702c5SYu Xiangning #error  inetddi.c: INET_SOCKDESC is defined but SOCKMOD_VERSION is not!
88*0f1702c5SYu Xiangning #elif	defined(INET_SOCKDESC) && !defined(INET_SOCK_PROTO_CREATE_FUNC)
89*0f1702c5SYu Xiangning #error  inetddi.c: INET_SOCKDESC is defined but INET_SOCK_PROTO_CREATE_FUNC \
90*0f1702c5SYu Xiangning is not!
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #ifdef	INET_DEVDESC
947c478bd9Sstevel@tonic-gate 
95fc80c0dfSnordmark extern struct streamtab INET_DEVSTRTAB;
96fc80c0dfSnordmark 
977c478bd9Sstevel@tonic-gate /*
98fc80c0dfSnordmark  * Drivers that actually want to be IP would set INET_DEVSTRTAB to ipinfo.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static dev_info_t *inet_dev_info;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	INET_DEFAULT_PRIV_MODE	0666
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static struct dev_priv {
1067c478bd9Sstevel@tonic-gate 	char *driver;
1077c478bd9Sstevel@tonic-gate 	int privonly;
1087c478bd9Sstevel@tonic-gate 	const char *read_priv;
1097c478bd9Sstevel@tonic-gate 	const char *write_priv;
1107c478bd9Sstevel@tonic-gate } netdev_privs[] = {
1117c478bd9Sstevel@tonic-gate 	{"icmp", PRIVONLY_DEV,	PRIV_NET_ICMPACCESS,	PRIV_NET_ICMPACCESS},
1127c478bd9Sstevel@tonic-gate 	{"icmp6", PRIVONLY_DEV,	PRIV_NET_ICMPACCESS,	PRIV_NET_ICMPACCESS},
1137c478bd9Sstevel@tonic-gate 	{"ip", PRIVONLY_DEV,	PRIV_NET_RAWACCESS,	PRIV_NET_RAWACCESS},
1147c478bd9Sstevel@tonic-gate 	{"ip6", PRIVONLY_DEV,	PRIV_NET_RAWACCESS,	PRIV_NET_RAWACCESS},
115f4b3ec61Sdh155122 	{"keysock", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG,	PRIV_SYS_IP_CONFIG},
116f4b3ec61Sdh155122 	{"ipsecah", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG,	PRIV_SYS_IP_CONFIG},
117f4b3ec61Sdh155122 	{"ipsecesp", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG,	PRIV_SYS_IP_CONFIG},
118f4b3ec61Sdh155122 	{"spdsock", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG,	PRIV_SYS_IP_CONFIG},
1197c478bd9Sstevel@tonic-gate 	{NULL,	0,		NULL,			NULL}
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static int
1237c478bd9Sstevel@tonic-gate inet_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	int i, ndevs;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
1287c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	inet_dev_info = devi;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	ndevs = sizeof (netdev_privs) / sizeof (struct dev_priv);
1337c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndevs; i++) {
1347c478bd9Sstevel@tonic-gate 		char *drv = netdev_privs[i].driver;
1357c478bd9Sstevel@tonic-gate 		if (drv == NULL || strcmp(drv, ddi_driver_name(devi)) == 0)
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (ddi_create_priv_minor_node(devi, INET_NAME, S_IFCHR,
1407c478bd9Sstevel@tonic-gate 	    INET_DEVMINOR, DDI_PSEUDO, netdev_privs[i].privonly,
1417c478bd9Sstevel@tonic-gate 	    netdev_privs[i].read_priv, netdev_privs[i].write_priv,
1427c478bd9Sstevel@tonic-gate 	    INET_DEFAULT_PRIV_MODE));
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static int
1467c478bd9Sstevel@tonic-gate inet_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
1497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	ASSERT(devi == inet_dev_info);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
1547c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /* ARGSUSED */
1597c478bd9Sstevel@tonic-gate static int
1607c478bd9Sstevel@tonic-gate inet_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	switch (cmd) {
1657c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1667c478bd9Sstevel@tonic-gate 		if (inet_dev_info != NULL) {
1677c478bd9Sstevel@tonic-gate 			*result = (void *)inet_dev_info;
1687c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 		break;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1737c478bd9Sstevel@tonic-gate 		*result = NULL;
1747c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1757c478bd9Sstevel@tonic-gate 		break;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	default:
1787c478bd9Sstevel@tonic-gate 		break;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	return (error);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(inet_devops, nulldev, nulldev, inet_attach, inet_detach,
18519397407SSherry Moore     nulldev, inet_info, INET_DEVMTFLAGS, &INET_DEVSTRTAB,
18619397407SSherry Moore     ddi_quiesce_not_supported);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1897c478bd9Sstevel@tonic-gate 	&mod_driverops,
1907c478bd9Sstevel@tonic-gate 	INET_DEVDESC,
1917c478bd9Sstevel@tonic-gate 	&inet_devops
1927c478bd9Sstevel@tonic-gate };
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #endif /* INET_DEVDESC */
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #ifdef	INET_MODDESC
197fc80c0dfSnordmark extern struct streamtab INET_MODSTRTAB;
198fc80c0dfSnordmark 
1997c478bd9Sstevel@tonic-gate static struct fmodsw fsw = {
2007c478bd9Sstevel@tonic-gate 	INET_NAME,
201fc80c0dfSnordmark 	&INET_MODSTRTAB,
2027c478bd9Sstevel@tonic-gate 	INET_MODMTFLAGS
2037c478bd9Sstevel@tonic-gate };
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
2067c478bd9Sstevel@tonic-gate 	&mod_strmodops,
2077c478bd9Sstevel@tonic-gate 	INET_MODDESC,
2087c478bd9Sstevel@tonic-gate 	&fsw
2097c478bd9Sstevel@tonic-gate };
210*0f1702c5SYu Xiangning 
2117c478bd9Sstevel@tonic-gate #endif /* INET_MODDESC */
2127c478bd9Sstevel@tonic-gate 
213*0f1702c5SYu Xiangning #ifdef  INET_SOCKDESC
214*0f1702c5SYu Xiangning 
215*0f1702c5SYu Xiangning #ifdef	INET_SOCK_PROTO_FB_FUNC
216*0f1702c5SYu Xiangning static __smod_priv_t smodpriv = {
217*0f1702c5SYu Xiangning 	NULL,
218*0f1702c5SYu Xiangning 	NULL,
219*0f1702c5SYu Xiangning 	INET_SOCK_PROTO_FB_FUNC
220*0f1702c5SYu Xiangning };
221*0f1702c5SYu Xiangning #endif	/* INET_SOCK_PROTO_FB_FUNC */
222*0f1702c5SYu Xiangning 
223*0f1702c5SYu Xiangning static struct smod_reg_s smodreg = {
224*0f1702c5SYu Xiangning 	SOCKMOD_VERSION,
225*0f1702c5SYu Xiangning 	INET_NAME,
226*0f1702c5SYu Xiangning 	SOCK_UC_VERSION,
227*0f1702c5SYu Xiangning 	SOCK_DC_VERSION,
228*0f1702c5SYu Xiangning 	INET_SOCK_PROTO_CREATE_FUNC,
229*0f1702c5SYu Xiangning #ifdef	INET_SOCK_PROTO_FB_FUNC
230*0f1702c5SYu Xiangning 	&smodpriv
231*0f1702c5SYu Xiangning #else
232*0f1702c5SYu Xiangning 	NULL
233*0f1702c5SYu Xiangning #endif	/* INET_SOCK_PROTO_FB_FUNC */
234*0f1702c5SYu Xiangning };
235*0f1702c5SYu Xiangning 
236*0f1702c5SYu Xiangning static struct modlsockmod modlsockmod = {
237*0f1702c5SYu Xiangning 	&mod_sockmodops,
238*0f1702c5SYu Xiangning 	INET_SOCKDESC,
239*0f1702c5SYu Xiangning 	&smodreg
240*0f1702c5SYu Xiangning };
241*0f1702c5SYu Xiangning #endif	/* INET_SOCKDESC */
242*0f1702c5SYu Xiangning 
2437c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2447c478bd9Sstevel@tonic-gate 	MODREV_1,
2457c478bd9Sstevel@tonic-gate #ifdef	INET_DEVDESC
2467c478bd9Sstevel@tonic-gate 	&modldrv,
2477c478bd9Sstevel@tonic-gate #endif
2487c478bd9Sstevel@tonic-gate #ifdef	INET_MODDESC
2497c478bd9Sstevel@tonic-gate 	&modlstrmod,
2507c478bd9Sstevel@tonic-gate #endif
251*0f1702c5SYu Xiangning #ifdef	INET_SOCKDESC
252*0f1702c5SYu Xiangning 	&modlsockmod,
253*0f1702c5SYu Xiangning #endif
2547c478bd9Sstevel@tonic-gate 	NULL
2557c478bd9Sstevel@tonic-gate };
256