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 /* 22f4b3ec61Sdh155122 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/stat.h> 307c478bd9Sstevel@tonic-gate #include <sys/stream.h> 317c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 337c478bd9Sstevel@tonic-gate #include <sys/priv_names.h> 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate /* 367c478bd9Sstevel@tonic-gate * This file contains generic goo needed to hook the STREAMS modules and 377c478bd9Sstevel@tonic-gate * drivers that live under uts/common/inet into the DDI. In order to use it, 387c478bd9Sstevel@tonic-gate * each module/driver should #define the symbols below (as appropriate) and 397c478bd9Sstevel@tonic-gate * then #include this source file; see the other uts/common/inet/<star>ddi.c 407c478bd9Sstevel@tonic-gate * files for examples of this in action. 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * The symbols that all modules and drivers must define are: 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * INET_NAME The name of the module/driver. 457c478bd9Sstevel@tonic-gate * 467c478bd9Sstevel@tonic-gate * The symbols that all modules must define are: 477c478bd9Sstevel@tonic-gate * 48*fc80c0dfSnordmark * INET_MODSTRTAB The name of the `streamtab' structure for this module. 497c478bd9Sstevel@tonic-gate * INET_MODDESC The one-line description for this module. 507c478bd9Sstevel@tonic-gate * INET_MODMTFLAGS The mt-streams(9F) flags for the module. 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * The symbols that all drivers must define are: 537c478bd9Sstevel@tonic-gate * 54*fc80c0dfSnordmark * INET_DEVSTRTAB The name of the `streamtab' structure for this driver. 557c478bd9Sstevel@tonic-gate * INET_DEVDESC The one-line description for this driver. 567c478bd9Sstevel@tonic-gate * INET_DEVMTFLAGS The mt-streams(9F) flags for the driver. 577c478bd9Sstevel@tonic-gate * INET_DEVMINOR The minor number of the driver (usually 0). 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * Drivers that need to masquerade as IP should set INET_DEVMTFLAGS to 60*fc80c0dfSnordmark * IP_DEVMTFLAGS and set INET_DEVSTRTAB to ipinfo. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate 63*fc80c0dfSnordmark #if !defined(INET_NAME) 647c478bd9Sstevel@tonic-gate #error inetddi.c: INET_NAME is not defined! 657c478bd9Sstevel@tonic-gate #elif !defined(INET_DEVDESC) && !defined(INET_MODDESC) 667c478bd9Sstevel@tonic-gate #error inetddi.c: at least one of INET_DEVDESC or INET_MODDESC must be defined! 67*fc80c0dfSnordmark #elif defined(INET_DEVDESC) && !defined(INET_DEVSTRTAB) 68*fc80c0dfSnordmark #error inetddi.c: INET_DEVDESC is defined but INET_DEVSTRTAB is not! 697c478bd9Sstevel@tonic-gate #elif defined(INET_DEVDESC) && !defined(INET_DEVMTFLAGS) 707c478bd9Sstevel@tonic-gate #error inetddi.c: INET_DEVDESC is defined but INET_DEVMTFLAGS is not! 717c478bd9Sstevel@tonic-gate #elif defined(INET_DEVDESC) && !defined(INET_DEVMINOR) 727c478bd9Sstevel@tonic-gate #error inetddi.c: INET_DEVDESC is defined but INET_DEVMINOR is not! 73*fc80c0dfSnordmark #elif defined(INET_MODDESC) && !defined(INET_MODSTRTAB) 74*fc80c0dfSnordmark #error inetddi.c: INET_MODDESC is defined but INET_MODSTRTAB is not! 757c478bd9Sstevel@tonic-gate #elif defined(INET_MODDESC) && !defined(INET_MODMTFLAGS) 767c478bd9Sstevel@tonic-gate #error inetddi.c: INET_MODDESC is defined but INET_MODMTFLAGS is not! 777c478bd9Sstevel@tonic-gate #endif 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate #ifdef INET_DEVDESC 807c478bd9Sstevel@tonic-gate 81*fc80c0dfSnordmark extern struct streamtab INET_DEVSTRTAB; 82*fc80c0dfSnordmark 837c478bd9Sstevel@tonic-gate /* 84*fc80c0dfSnordmark * Drivers that actually want to be IP would set INET_DEVSTRTAB to ipinfo. 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate static dev_info_t *inet_dev_info; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate #define INET_DEFAULT_PRIV_MODE 0666 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate static struct dev_priv { 927c478bd9Sstevel@tonic-gate char *driver; 937c478bd9Sstevel@tonic-gate int privonly; 947c478bd9Sstevel@tonic-gate const char *read_priv; 957c478bd9Sstevel@tonic-gate const char *write_priv; 967c478bd9Sstevel@tonic-gate } netdev_privs[] = { 977c478bd9Sstevel@tonic-gate {"icmp", PRIVONLY_DEV, PRIV_NET_ICMPACCESS, PRIV_NET_ICMPACCESS}, 987c478bd9Sstevel@tonic-gate {"icmp6", PRIVONLY_DEV, PRIV_NET_ICMPACCESS, PRIV_NET_ICMPACCESS}, 997c478bd9Sstevel@tonic-gate {"ip", PRIVONLY_DEV, PRIV_NET_RAWACCESS, PRIV_NET_RAWACCESS}, 1007c478bd9Sstevel@tonic-gate {"ip6", PRIVONLY_DEV, PRIV_NET_RAWACCESS, PRIV_NET_RAWACCESS}, 101f4b3ec61Sdh155122 {"keysock", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 102f4b3ec61Sdh155122 {"ipsecah", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 103f4b3ec61Sdh155122 {"ipsecesp", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 104f4b3ec61Sdh155122 {"spdsock", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 1057c478bd9Sstevel@tonic-gate {NULL, 0, NULL, NULL} 1067c478bd9Sstevel@tonic-gate }; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate static int 1097c478bd9Sstevel@tonic-gate inet_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate int i, ndevs; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH) 1147c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate inet_dev_info = devi; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate ndevs = sizeof (netdev_privs) / sizeof (struct dev_priv); 1197c478bd9Sstevel@tonic-gate for (i = 0; i < ndevs; i++) { 1207c478bd9Sstevel@tonic-gate char *drv = netdev_privs[i].driver; 1217c478bd9Sstevel@tonic-gate if (drv == NULL || strcmp(drv, ddi_driver_name(devi)) == 0) 1227c478bd9Sstevel@tonic-gate break; 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate return (ddi_create_priv_minor_node(devi, INET_NAME, S_IFCHR, 1267c478bd9Sstevel@tonic-gate INET_DEVMINOR, DDI_PSEUDO, netdev_privs[i].privonly, 1277c478bd9Sstevel@tonic-gate netdev_privs[i].read_priv, netdev_privs[i].write_priv, 1287c478bd9Sstevel@tonic-gate INET_DEFAULT_PRIV_MODE)); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate static int 1327c478bd9Sstevel@tonic-gate inet_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) 1357c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate ASSERT(devi == inet_dev_info); 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 1407c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1457c478bd9Sstevel@tonic-gate static int 1467c478bd9Sstevel@tonic-gate inet_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate int error = DDI_FAILURE; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate switch (cmd) { 1517c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1527c478bd9Sstevel@tonic-gate if (inet_dev_info != NULL) { 1537c478bd9Sstevel@tonic-gate *result = (void *)inet_dev_info; 1547c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate break; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1597c478bd9Sstevel@tonic-gate *result = NULL; 1607c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1617c478bd9Sstevel@tonic-gate break; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate default: 1647c478bd9Sstevel@tonic-gate break; 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate return (error); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(inet_devops, nulldev, nulldev, inet_attach, inet_detach, 171*fc80c0dfSnordmark nulldev, inet_info, INET_DEVMTFLAGS, &INET_DEVSTRTAB); 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1747c478bd9Sstevel@tonic-gate &mod_driverops, 1757c478bd9Sstevel@tonic-gate INET_DEVDESC, 1767c478bd9Sstevel@tonic-gate &inet_devops 1777c478bd9Sstevel@tonic-gate }; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate #endif /* INET_DEVDESC */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate #ifdef INET_MODDESC 182*fc80c0dfSnordmark extern struct streamtab INET_MODSTRTAB; 183*fc80c0dfSnordmark 1847c478bd9Sstevel@tonic-gate static struct fmodsw fsw = { 1857c478bd9Sstevel@tonic-gate INET_NAME, 186*fc80c0dfSnordmark &INET_MODSTRTAB, 1877c478bd9Sstevel@tonic-gate INET_MODMTFLAGS 1887c478bd9Sstevel@tonic-gate }; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = { 1917c478bd9Sstevel@tonic-gate &mod_strmodops, 1927c478bd9Sstevel@tonic-gate INET_MODDESC, 1937c478bd9Sstevel@tonic-gate &fsw 1947c478bd9Sstevel@tonic-gate }; 1957c478bd9Sstevel@tonic-gate #endif /* INET_MODDESC */ 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1987c478bd9Sstevel@tonic-gate MODREV_1, 1997c478bd9Sstevel@tonic-gate #ifdef INET_DEVDESC 2007c478bd9Sstevel@tonic-gate &modldrv, 2017c478bd9Sstevel@tonic-gate #endif 2027c478bd9Sstevel@tonic-gate #ifdef INET_MODDESC 2037c478bd9Sstevel@tonic-gate &modlstrmod, 2047c478bd9Sstevel@tonic-gate #endif 2057c478bd9Sstevel@tonic-gate NULL 2067c478bd9Sstevel@tonic-gate }; 207