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 5*f4b3ec61Sdh155122 * Common Development and Distribution License (the "License"). 6*f4b3ec61Sdh155122 * 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 /* 22*f4b3ec61Sdh155122 * 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 * INET_STRTAB The name of the `streamtab' structure. 467c478bd9Sstevel@tonic-gate * 477c478bd9Sstevel@tonic-gate * The symbols that all modules must define are: 487c478bd9Sstevel@tonic-gate * 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 * 547c478bd9Sstevel@tonic-gate * INET_DEVDESC The one-line description for this driver. 557c478bd9Sstevel@tonic-gate * INET_DEVMTFLAGS The mt-streams(9F) flags for the driver. 567c478bd9Sstevel@tonic-gate * INET_DEVMINOR The minor number of the driver (usually 0). 577c478bd9Sstevel@tonic-gate * 587c478bd9Sstevel@tonic-gate * Drivers that need to masquerade as IP should set INET_DEVMTFLAGS to 597c478bd9Sstevel@tonic-gate * IP_DEVMTFLAGS and then call INET_BECOME_IP() in their _init(9E) routine. 607c478bd9Sstevel@tonic-gate */ 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #if !defined(INET_STRTAB) 637c478bd9Sstevel@tonic-gate #error inetddi.c: INET_STRTAB is not defined! 647c478bd9Sstevel@tonic-gate #elif !defined(INET_NAME) 657c478bd9Sstevel@tonic-gate #error inetddi.c: INET_NAME is not defined! 667c478bd9Sstevel@tonic-gate #elif !defined(INET_DEVDESC) && !defined(INET_MODDESC) 677c478bd9Sstevel@tonic-gate #error inetddi.c: at least one of INET_DEVDESC or INET_MODDESC must be defined! 687c478bd9Sstevel@tonic-gate #elif defined(INET_DEVDESC) && !defined(INET_DEVMTFLAGS) 697c478bd9Sstevel@tonic-gate #error inetddi.c: INET_DEVDESC is defined but INET_DEVMTFLAGS is not! 707c478bd9Sstevel@tonic-gate #elif defined(INET_DEVDESC) && !defined(INET_DEVMINOR) 717c478bd9Sstevel@tonic-gate #error inetddi.c: INET_DEVDESC is defined but INET_DEVMINOR is not! 727c478bd9Sstevel@tonic-gate #elif defined(INET_MODDESC) && !defined(INET_MODMTFLAGS) 737c478bd9Sstevel@tonic-gate #error inetddi.c: INET_MODDESC is defined but INET_MODMTFLAGS is not! 747c478bd9Sstevel@tonic-gate #endif 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate extern struct streamtab INET_STRTAB, ipinfo; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #ifdef INET_DEVDESC 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * This macro is intended to be called from the _init() routine of drivers 827c478bd9Sstevel@tonic-gate * that actually want to be IP. Yes, this is a disgusting, vile hack. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate #define INET_BECOME_IP() (cb_inet_devops.cb_str = &ipinfo) 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate static dev_info_t *inet_dev_info; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate #define INET_DEFAULT_PRIV_MODE 0666 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate static struct dev_priv { 917c478bd9Sstevel@tonic-gate char *driver; 927c478bd9Sstevel@tonic-gate int privonly; 937c478bd9Sstevel@tonic-gate const char *read_priv; 947c478bd9Sstevel@tonic-gate const char *write_priv; 957c478bd9Sstevel@tonic-gate } netdev_privs[] = { 967c478bd9Sstevel@tonic-gate {"icmp", PRIVONLY_DEV, PRIV_NET_ICMPACCESS, PRIV_NET_ICMPACCESS}, 977c478bd9Sstevel@tonic-gate {"icmp6", PRIVONLY_DEV, PRIV_NET_ICMPACCESS, PRIV_NET_ICMPACCESS}, 987c478bd9Sstevel@tonic-gate {"ip", PRIVONLY_DEV, PRIV_NET_RAWACCESS, PRIV_NET_RAWACCESS}, 997c478bd9Sstevel@tonic-gate {"ip6", PRIVONLY_DEV, PRIV_NET_RAWACCESS, PRIV_NET_RAWACCESS}, 100*f4b3ec61Sdh155122 {"keysock", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 101*f4b3ec61Sdh155122 {"ipsecah", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 102*f4b3ec61Sdh155122 {"ipsecesp", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 103*f4b3ec61Sdh155122 {"spdsock", PRIVONLY_DEV, PRIV_SYS_IP_CONFIG, PRIV_SYS_IP_CONFIG}, 1047c478bd9Sstevel@tonic-gate {NULL, 0, NULL, NULL} 1057c478bd9Sstevel@tonic-gate }; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate static int 1087c478bd9Sstevel@tonic-gate inet_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1097c478bd9Sstevel@tonic-gate { 1107c478bd9Sstevel@tonic-gate int i, ndevs; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH) 1137c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate inet_dev_info = devi; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate ndevs = sizeof (netdev_privs) / sizeof (struct dev_priv); 1187c478bd9Sstevel@tonic-gate for (i = 0; i < ndevs; i++) { 1197c478bd9Sstevel@tonic-gate char *drv = netdev_privs[i].driver; 1207c478bd9Sstevel@tonic-gate if (drv == NULL || strcmp(drv, ddi_driver_name(devi)) == 0) 1217c478bd9Sstevel@tonic-gate break; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate return (ddi_create_priv_minor_node(devi, INET_NAME, S_IFCHR, 1257c478bd9Sstevel@tonic-gate INET_DEVMINOR, DDI_PSEUDO, netdev_privs[i].privonly, 1267c478bd9Sstevel@tonic-gate netdev_privs[i].read_priv, netdev_privs[i].write_priv, 1277c478bd9Sstevel@tonic-gate INET_DEFAULT_PRIV_MODE)); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate static int 1317c478bd9Sstevel@tonic-gate inet_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) 1347c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate ASSERT(devi == inet_dev_info); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 1397c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1447c478bd9Sstevel@tonic-gate static int 1457c478bd9Sstevel@tonic-gate inet_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 1467c478bd9Sstevel@tonic-gate { 1477c478bd9Sstevel@tonic-gate int error = DDI_FAILURE; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate switch (cmd) { 1507c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1517c478bd9Sstevel@tonic-gate if (inet_dev_info != NULL) { 1527c478bd9Sstevel@tonic-gate *result = (void *)inet_dev_info; 1537c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate break; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1587c478bd9Sstevel@tonic-gate *result = NULL; 1597c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1607c478bd9Sstevel@tonic-gate break; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate default: 1637c478bd9Sstevel@tonic-gate break; 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate return (error); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(inet_devops, nulldev, nulldev, inet_attach, inet_detach, 1707c478bd9Sstevel@tonic-gate nulldev, inet_info, INET_DEVMTFLAGS, &INET_STRTAB); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1737c478bd9Sstevel@tonic-gate &mod_driverops, 1747c478bd9Sstevel@tonic-gate INET_DEVDESC, 1757c478bd9Sstevel@tonic-gate &inet_devops 1767c478bd9Sstevel@tonic-gate }; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate #endif /* INET_DEVDESC */ 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate #ifdef INET_MODDESC 1817c478bd9Sstevel@tonic-gate static struct fmodsw fsw = { 1827c478bd9Sstevel@tonic-gate INET_NAME, 1837c478bd9Sstevel@tonic-gate &INET_STRTAB, 1847c478bd9Sstevel@tonic-gate INET_MODMTFLAGS 1857c478bd9Sstevel@tonic-gate }; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = { 1887c478bd9Sstevel@tonic-gate &mod_strmodops, 1897c478bd9Sstevel@tonic-gate INET_MODDESC, 1907c478bd9Sstevel@tonic-gate &fsw 1917c478bd9Sstevel@tonic-gate }; 1927c478bd9Sstevel@tonic-gate #endif /* INET_MODDESC */ 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1957c478bd9Sstevel@tonic-gate MODREV_1, 1967c478bd9Sstevel@tonic-gate #ifdef INET_DEVDESC 1977c478bd9Sstevel@tonic-gate &modldrv, 1987c478bd9Sstevel@tonic-gate #endif 1997c478bd9Sstevel@tonic-gate #ifdef INET_MODDESC 2007c478bd9Sstevel@tonic-gate &modlstrmod, 2017c478bd9Sstevel@tonic-gate #endif 2027c478bd9Sstevel@tonic-gate NULL 2037c478bd9Sstevel@tonic-gate }; 204