1dc632b73SJames Carlson /*
2dc632b73SJames Carlson * CDDL HEADER START
3dc632b73SJames Carlson *
4dc632b73SJames Carlson * The contents of this file are subject to the terms of the
5dc632b73SJames Carlson * Common Development and Distribution License (the "License").
6dc632b73SJames Carlson * You may not use this file except in compliance with the License.
7dc632b73SJames Carlson *
8dc632b73SJames Carlson * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9dc632b73SJames Carlson * or http://www.opensolaris.org/os/licensing.
10dc632b73SJames Carlson * See the License for the specific language governing permissions
11dc632b73SJames Carlson * and limitations under the License.
12dc632b73SJames Carlson *
13dc632b73SJames Carlson * When distributing Covered Code, include this CDDL HEADER in each
14dc632b73SJames Carlson * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15dc632b73SJames Carlson * If applicable, add the following below this CDDL HEADER, with the
16dc632b73SJames Carlson * fields enclosed by brackets "[]" replaced with your own identifying
17dc632b73SJames Carlson * information: Portions Copyright [yyyy] [name of copyright owner]
18dc632b73SJames Carlson *
19dc632b73SJames Carlson * CDDL HEADER END
20dc632b73SJames Carlson */
21dc632b73SJames Carlson
227c478bd9Sstevel@tonic-gate /*
23f53eecf5SJames Carlson * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/debug.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <sys/stream.h>
347c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
38002c70ffScarlsonj #include <sys/sdt.h>
397c478bd9Sstevel@tonic-gate #include <sys/conf.h>
407c478bd9Sstevel@tonic-gate #include <sys/dlpi.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
437c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
447c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
457c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
467c478bd9Sstevel@tonic-gate #include <sys/note.h>
477c478bd9Sstevel@tonic-gate #include <sys/policy.h>
487c478bd9Sstevel@tonic-gate #include <net/ppp_defs.h>
497c478bd9Sstevel@tonic-gate #include <net/pppio.h>
507c478bd9Sstevel@tonic-gate #include <net/sppptun.h>
517c478bd9Sstevel@tonic-gate #include <net/pppoe.h>
527c478bd9Sstevel@tonic-gate #include <netinet/in.h>
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #include "s_common.h"
557c478bd9Sstevel@tonic-gate #include "sppptun_mod.h"
567c478bd9Sstevel@tonic-gate #include "sppptun_impl.h"
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #define NTUN_INITIAL 16 /* Initial number of sppptun slots */
597c478bd9Sstevel@tonic-gate #define NTUN_PERCENT 5 /* Percent of memory to use */
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate * This is used to tag official Solaris sources. Please do not define
637c478bd9Sstevel@tonic-gate * "INTERNAL_BUILD" when building this software outside of Sun
647c478bd9Sstevel@tonic-gate * Microsystems.
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate #ifdef INTERNAL_BUILD
677c478bd9Sstevel@tonic-gate /* MODINFO is limited to 32 characters. */
68002c70ffScarlsonj const char sppptun_driver_description[] = "PPP 4.0 tunnel driver";
69002c70ffScarlsonj const char sppptun_module_description[] = "PPP 4.0 tunnel module";
707c478bd9Sstevel@tonic-gate #else
7119397407SSherry Moore const char sppptun_driver_description[] = "ANU PPP tundrv";
7219397407SSherry Moore const char sppptun_module_description[] = "ANU PPP tunmod";
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate /* LINTED */
757c478bd9Sstevel@tonic-gate static const char buildtime[] = "Built " __DATE__ " at " __TIME__
767c478bd9Sstevel@tonic-gate #ifdef DEBUG
777c478bd9Sstevel@tonic-gate " DEBUG"
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate "\n";
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * Tunable values; these are similar to the values used in ptms_conf.c.
847c478bd9Sstevel@tonic-gate * Override these settings via /etc/system.
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate uint_t sppptun_cnt = 0; /* Minimum number of tunnels */
877c478bd9Sstevel@tonic-gate size_t sppptun_max_pty = 0; /* Maximum number of tunnels */
887c478bd9Sstevel@tonic-gate uint_t sppptun_init_cnt = NTUN_INITIAL; /* Initial number of tunnel slots */
897c478bd9Sstevel@tonic-gate uint_t sppptun_pctofmem = NTUN_PERCENT; /* Percent of memory to use */
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate typedef struct ether_dest_s {
927c478bd9Sstevel@tonic-gate ether_addr_t addr;
937c478bd9Sstevel@tonic-gate ushort_t type;
947c478bd9Sstevel@tonic-gate } ether_dest_t;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /* Allows unaligned access. */
977c478bd9Sstevel@tonic-gate #define GETLONG(x) (((x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate static const char *tll_kstats_list[] = { TLL_KSTATS_NAMES };
1007c478bd9Sstevel@tonic-gate static const char *tcl_kstats_list[] = { TCL_KSTATS_NAMES };
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate #define KREF(p, m, vn) p->m.vn.value.ui64
1037c478bd9Sstevel@tonic-gate #define KINCR(p, m, vn) ++KREF(p, m, vn)
1047c478bd9Sstevel@tonic-gate #define KDECR(p, m, vn) --KREF(p, m, vn)
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate #define KLINCR(vn) KINCR(tll, tll_kstats, vn)
1077c478bd9Sstevel@tonic-gate #define KLDECR(vn) KDECR(tll, tll_kstats, vn)
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate #define KCINCR(vn) KINCR(tcl, tcl_kstats, vn)
1107c478bd9Sstevel@tonic-gate #define KCDECR(vn) KDECR(tcl, tcl_kstats, vn)
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate static int sppptun_open(queue_t *, dev_t *, int, int, cred_t *);
1135e1743f0SToomas Soome static int sppptun_close(queue_t *, int, cred_t *);
114868fcfe9SToomas Soome static int sppptun_urput(queue_t *, mblk_t *);
115868fcfe9SToomas Soome static int sppptun_uwput(queue_t *, mblk_t *);
1167c478bd9Sstevel@tonic-gate static int sppptun_ursrv(queue_t *);
1177c478bd9Sstevel@tonic-gate static int sppptun_uwsrv(queue_t *);
118868fcfe9SToomas Soome static int sppptun_lrput(queue_t *, mblk_t *);
119868fcfe9SToomas Soome static int sppptun_lwput(queue_t *, mblk_t *);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * This is the hash table of clients. Clients are the programs that
1237c478bd9Sstevel@tonic-gate * open /dev/sppptun as a device. There may be a large number of
1247c478bd9Sstevel@tonic-gate * these; one per tunneled PPP session.
1257c478bd9Sstevel@tonic-gate *
1267c478bd9Sstevel@tonic-gate * Note: slots are offset from minor node value by 1 because
1277c478bd9Sstevel@tonic-gate * vmem_alloc returns 0 for failure.
1287c478bd9Sstevel@tonic-gate *
1297c478bd9Sstevel@tonic-gate * The tcl_slots array entries are modified only when exclusive on
1307c478bd9Sstevel@tonic-gate * both inner and outer perimeters. This ensures that threads on
1317c478bd9Sstevel@tonic-gate * shared perimeters always view this as unchanging memory with no
1327c478bd9Sstevel@tonic-gate * need to lock around accesses. (Specifically, the tcl_slots array
1337c478bd9Sstevel@tonic-gate * is modified by entry to sppptun_open, sppptun_close, and _fini.)
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate static tuncl_t **tcl_slots = NULL; /* Slots for tuncl_t */
1367c478bd9Sstevel@tonic-gate static size_t tcl_nslots = 0; /* Size of slot array */
1377c478bd9Sstevel@tonic-gate static size_t tcl_minormax = 0; /* Maximum number of tunnels */
1387c478bd9Sstevel@tonic-gate static size_t tcl_inuse = 0; /* # of tunnels currently allocated */
1397c478bd9Sstevel@tonic-gate static krwlock_t tcl_rwlock;
1407c478bd9Sstevel@tonic-gate static struct kmem_cache *tcl_cache = NULL; /* tunnel cache */
1417c478bd9Sstevel@tonic-gate static vmem_t *tcl_minor_arena = NULL; /* Arena for device minors */
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * This is the simple list of lower layers. For PPPoE, there is one
1457c478bd9Sstevel@tonic-gate * of these per Ethernet interface. Lower layers are established by
1467c478bd9Sstevel@tonic-gate * "plumbing" -- using I_PLINK to connect the tunnel multiplexor to
1477c478bd9Sstevel@tonic-gate * the physical interface.
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate static struct qelem tunll_list;
1507c478bd9Sstevel@tonic-gate static int tunll_index;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate /* Test value; if all zeroes, then address hasn't been set yet. */
1537c478bd9Sstevel@tonic-gate static const ether_addr_t zero_mac_addr = { 0, 0, 0, 0, 0, 0 };
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate #define MIN_SET_FASTPATH_UNITDATAREQ_SIZE \
1567c478bd9Sstevel@tonic-gate (sizeof (dl_unitdata_req_t) + 4)
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate #define TUN_MI_ID 2104 /* officially allocated module ID */
1597c478bd9Sstevel@tonic-gate #define TUN_MI_MINPSZ (0)
1607c478bd9Sstevel@tonic-gate #define TUN_MI_MAXPSZ (PPP_MAXMTU)
1617c478bd9Sstevel@tonic-gate #define TUN_MI_HIWAT (PPP_MTU * 8)
1627c478bd9Sstevel@tonic-gate #define TUN_MI_LOWAT (128)
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate static struct module_info sppptun_modinfo = {
1657c478bd9Sstevel@tonic-gate TUN_MI_ID, /* mi_idnum */
1667c478bd9Sstevel@tonic-gate PPP_TUN_NAME, /* mi_idname */
1677c478bd9Sstevel@tonic-gate TUN_MI_MINPSZ, /* mi_minpsz */
1687c478bd9Sstevel@tonic-gate TUN_MI_MAXPSZ, /* mi_maxpsz */
1697c478bd9Sstevel@tonic-gate TUN_MI_HIWAT, /* mi_hiwat */
1707c478bd9Sstevel@tonic-gate TUN_MI_LOWAT /* mi_lowat */
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate static struct qinit sppptun_urinit = {
174868fcfe9SToomas Soome sppptun_urput, /* qi_putp */
1757c478bd9Sstevel@tonic-gate sppptun_ursrv, /* qi_srvp */
1767c478bd9Sstevel@tonic-gate sppptun_open, /* qi_qopen */
1777c478bd9Sstevel@tonic-gate sppptun_close, /* qi_qclose */
1787c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */
1797c478bd9Sstevel@tonic-gate &sppptun_modinfo, /* qi_minfo */
1807c478bd9Sstevel@tonic-gate NULL /* qi_mstat */
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate static struct qinit sppptun_uwinit = {
1847c478bd9Sstevel@tonic-gate (int (*)())sppptun_uwput, /* qi_putp */
1857c478bd9Sstevel@tonic-gate sppptun_uwsrv, /* qi_srvp */
1867c478bd9Sstevel@tonic-gate NULL, /* qi_qopen */
1877c478bd9Sstevel@tonic-gate NULL, /* qi_qclose */
1887c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */
1897c478bd9Sstevel@tonic-gate &sppptun_modinfo, /* qi_minfo */
1907c478bd9Sstevel@tonic-gate NULL /* qi_mstat */
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate static struct qinit sppptun_lrinit = {
1947c478bd9Sstevel@tonic-gate (int (*)())sppptun_lrput, /* qi_putp */
1957c478bd9Sstevel@tonic-gate NULL, /* qi_srvp */
1967c478bd9Sstevel@tonic-gate NULL, /* qi_qopen */
1977c478bd9Sstevel@tonic-gate NULL, /* qi_qclose */
1987c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */
1997c478bd9Sstevel@tonic-gate &sppptun_modinfo, /* qi_minfo */
2007c478bd9Sstevel@tonic-gate NULL /* qi_mstat */
2017c478bd9Sstevel@tonic-gate };
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate static struct qinit sppptun_lwinit = {
2047c478bd9Sstevel@tonic-gate (int (*)())sppptun_lwput, /* qi_putp */
2057c478bd9Sstevel@tonic-gate NULL, /* qi_srvp */
2067c478bd9Sstevel@tonic-gate NULL, /* qi_qopen */
2077c478bd9Sstevel@tonic-gate NULL, /* qi_qclose */
2087c478bd9Sstevel@tonic-gate NULL, /* qi_qadmin */
2097c478bd9Sstevel@tonic-gate &sppptun_modinfo, /* qi_minfo */
2107c478bd9Sstevel@tonic-gate NULL /* qi_mstat */
2117c478bd9Sstevel@tonic-gate };
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * This is referenced in sppptun_mod.c.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate struct streamtab sppptun_tab = {
2177c478bd9Sstevel@tonic-gate &sppptun_urinit, /* st_rdinit */
2187c478bd9Sstevel@tonic-gate &sppptun_uwinit, /* st_wrinit */
2197c478bd9Sstevel@tonic-gate &sppptun_lrinit, /* st_muxrinit */
2207c478bd9Sstevel@tonic-gate &sppptun_lwinit /* st_muxwrinit */
2217c478bd9Sstevel@tonic-gate };
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate * Allocate another slot table twice as large as the original one
2257c478bd9Sstevel@tonic-gate * (limited to global maximum). Migrate all tunnels to the new slot
2267c478bd9Sstevel@tonic-gate * table and free the original one. Assumes we're exclusive on both
2277c478bd9Sstevel@tonic-gate * inner and outer perimeters, and thus there are no other users of
2287c478bd9Sstevel@tonic-gate * the tcl_slots array.
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate static minor_t
tcl_grow(void)2317c478bd9Sstevel@tonic-gate tcl_grow(void)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate minor_t old_size = tcl_nslots;
2347c478bd9Sstevel@tonic-gate minor_t new_size = 2 * old_size;
2357c478bd9Sstevel@tonic-gate tuncl_t **tcl_old = tcl_slots;
2367c478bd9Sstevel@tonic-gate tuncl_t **tcl_new;
2377c478bd9Sstevel@tonic-gate void *vaddr; /* vmem_add return value */
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&tcl_rwlock));
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate /* Allocate new ptms array */
2427c478bd9Sstevel@tonic-gate tcl_new = kmem_zalloc(new_size * sizeof (tuncl_t *), KM_NOSLEEP);
2437c478bd9Sstevel@tonic-gate if (tcl_new == NULL)
2447c478bd9Sstevel@tonic-gate return ((minor_t)0);
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate /* Increase clone index space */
2477c478bd9Sstevel@tonic-gate vaddr = vmem_add(tcl_minor_arena, (void*)((uintptr_t)old_size + 1),
2487c478bd9Sstevel@tonic-gate new_size - old_size, VM_NOSLEEP);
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate if (vaddr == NULL) {
2517c478bd9Sstevel@tonic-gate kmem_free(tcl_new, new_size * sizeof (tuncl_t *));
2527c478bd9Sstevel@tonic-gate return ((minor_t)0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /* Migrate tuncl_t entries to a new location */
2567c478bd9Sstevel@tonic-gate tcl_nslots = new_size;
2577c478bd9Sstevel@tonic-gate bcopy(tcl_old, tcl_new, old_size * sizeof (tuncl_t *));
2587c478bd9Sstevel@tonic-gate tcl_slots = tcl_new;
2597c478bd9Sstevel@tonic-gate kmem_free(tcl_old, old_size * sizeof (tuncl_t *));
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate /* Allocate minor number and return it */
2627c478bd9Sstevel@tonic-gate return ((minor_t)(uintptr_t)vmem_alloc(tcl_minor_arena, 1, VM_NOSLEEP));
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * Allocate new minor number and tunnel client entry. Returns the new
2677c478bd9Sstevel@tonic-gate * entry or NULL if no memory or maximum number of entries reached.
2687c478bd9Sstevel@tonic-gate * Assumes we're exclusive on both inner and outer perimeters, and
2697c478bd9Sstevel@tonic-gate * thus there are no other users of the tcl_slots array.
2707c478bd9Sstevel@tonic-gate */
2717c478bd9Sstevel@tonic-gate static tuncl_t *
tuncl_alloc(int wantminor)2727c478bd9Sstevel@tonic-gate tuncl_alloc(int wantminor)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate minor_t dminor;
2757c478bd9Sstevel@tonic-gate tuncl_t *tcl = NULL;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate rw_enter(&tcl_rwlock, RW_WRITER);
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate ASSERT(tcl_slots != NULL);
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate * Always try to allocate new pty when sppptun_cnt minimum
2837c478bd9Sstevel@tonic-gate * limit is not achieved. If it is achieved, the maximum is
2847c478bd9Sstevel@tonic-gate * determined by either user-specified value (if it is
2857c478bd9Sstevel@tonic-gate * non-zero) or our memory estimations - whatever is less.
2867c478bd9Sstevel@tonic-gate */
2877c478bd9Sstevel@tonic-gate if (tcl_inuse >= sppptun_cnt) {
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate * When system achieved required minimum of tunnels,
2907c478bd9Sstevel@tonic-gate * check for the denial of service limits.
2917c478bd9Sstevel@tonic-gate *
2927c478bd9Sstevel@tonic-gate * Get user-imposed maximum, if configured, or
2937c478bd9Sstevel@tonic-gate * calculated memory constraint.
2947c478bd9Sstevel@tonic-gate */
2957c478bd9Sstevel@tonic-gate size_t user_max = (sppptun_max_pty == 0 ? tcl_minormax :
2967c478bd9Sstevel@tonic-gate min(sppptun_max_pty, tcl_minormax));
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate /* Do not try to allocate more than allowed */
2997c478bd9Sstevel@tonic-gate if (tcl_inuse >= user_max) {
3007c478bd9Sstevel@tonic-gate rw_exit(&tcl_rwlock);
3017c478bd9Sstevel@tonic-gate return (NULL);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate tcl_inuse++;
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate * Allocate new minor number. If this fails, all slots are
3087c478bd9Sstevel@tonic-gate * busy and we need to grow the hash.
3097c478bd9Sstevel@tonic-gate */
3107c478bd9Sstevel@tonic-gate if (wantminor <= 0) {
3117c478bd9Sstevel@tonic-gate dminor = (minor_t)(uintptr_t)vmem_alloc(tcl_minor_arena, 1,
3127c478bd9Sstevel@tonic-gate VM_NOSLEEP);
3137c478bd9Sstevel@tonic-gate if (dminor == 0) {
3147c478bd9Sstevel@tonic-gate /* Grow the cache and retry allocation */
3157c478bd9Sstevel@tonic-gate dminor = tcl_grow();
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate } else {
3187c478bd9Sstevel@tonic-gate dminor = (minor_t)(uintptr_t)vmem_xalloc(tcl_minor_arena, 1,
3197c478bd9Sstevel@tonic-gate 0, 0, 0, (void *)(uintptr_t)wantminor,
3207c478bd9Sstevel@tonic-gate (void *)((uintptr_t)wantminor+1), VM_NOSLEEP);
3217c478bd9Sstevel@tonic-gate if (dminor != 0 && dminor != wantminor) {
3227c478bd9Sstevel@tonic-gate vmem_free(tcl_minor_arena, (void *)(uintptr_t)dminor,
3237c478bd9Sstevel@tonic-gate 1);
3247c478bd9Sstevel@tonic-gate dminor = 0;
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate if (dminor == 0) {
3297c478bd9Sstevel@tonic-gate /* Not enough memory now */
3307c478bd9Sstevel@tonic-gate tcl_inuse--;
3317c478bd9Sstevel@tonic-gate rw_exit(&tcl_rwlock);
3327c478bd9Sstevel@tonic-gate return (NULL);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate tcl = kmem_cache_alloc(tcl_cache, KM_NOSLEEP);
3367c478bd9Sstevel@tonic-gate if (tcl == NULL) {
3377c478bd9Sstevel@tonic-gate /* Not enough memory - this entry can't be used now. */
3387c478bd9Sstevel@tonic-gate vmem_free(tcl_minor_arena, (void *)(uintptr_t)dminor, 1);
3397c478bd9Sstevel@tonic-gate tcl_inuse--;
3407c478bd9Sstevel@tonic-gate } else {
3417c478bd9Sstevel@tonic-gate bzero(tcl, sizeof (*tcl));
3427c478bd9Sstevel@tonic-gate tcl->tcl_lsessid = dminor;
3437c478bd9Sstevel@tonic-gate ASSERT(tcl_slots[dminor - 1] == NULL);
3447c478bd9Sstevel@tonic-gate tcl_slots[dminor - 1] = tcl;
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate rw_exit(&tcl_rwlock);
3487c478bd9Sstevel@tonic-gate return (tcl);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate * This routine frees an upper level (client) stream by removing it
3537c478bd9Sstevel@tonic-gate * from the minor number pool and freeing the state structure storage.
3547c478bd9Sstevel@tonic-gate * Assumes we're exclusive on both inner and outer perimeters, and
3557c478bd9Sstevel@tonic-gate * thus there are no other concurrent users of the tcl_slots array or
3567c478bd9Sstevel@tonic-gate * of any entry in that array.
3577c478bd9Sstevel@tonic-gate */
3587c478bd9Sstevel@tonic-gate static void
tuncl_free(tuncl_t * tcl)3597c478bd9Sstevel@tonic-gate tuncl_free(tuncl_t *tcl)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate rw_enter(&tcl_rwlock, RW_WRITER);
3627c478bd9Sstevel@tonic-gate ASSERT(tcl->tcl_lsessid <= tcl_nslots);
3637c478bd9Sstevel@tonic-gate ASSERT(tcl_slots[tcl->tcl_lsessid - 1] == tcl);
3647c478bd9Sstevel@tonic-gate ASSERT(tcl_inuse > 0);
3657c478bd9Sstevel@tonic-gate tcl_inuse--;
3667c478bd9Sstevel@tonic-gate tcl_slots[tcl->tcl_lsessid - 1] = NULL;
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate if (tcl->tcl_ksp != NULL) {
3697c478bd9Sstevel@tonic-gate kstat_delete(tcl->tcl_ksp);
3707c478bd9Sstevel@tonic-gate tcl->tcl_ksp = NULL;
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate /* Return minor number to the pool of minors */
3747c478bd9Sstevel@tonic-gate vmem_free(tcl_minor_arena, (void *)(uintptr_t)tcl->tcl_lsessid, 1);
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /* Return tuncl_t to the cache */
3777c478bd9Sstevel@tonic-gate kmem_cache_free(tcl_cache, tcl);
3787c478bd9Sstevel@tonic-gate rw_exit(&tcl_rwlock);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate * Get tuncl_t structure by minor number. Returns NULL when minor is
3837c478bd9Sstevel@tonic-gate * out of range. Note that lookup of tcl pointers (and use of those
3847c478bd9Sstevel@tonic-gate * pointers) is safe because modification is done only when exclusive
3857c478bd9Sstevel@tonic-gate * on both inner and outer perimeters.
3867c478bd9Sstevel@tonic-gate */
3877c478bd9Sstevel@tonic-gate static tuncl_t *
tcl_by_minor(minor_t dminor)3887c478bd9Sstevel@tonic-gate tcl_by_minor(minor_t dminor)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate tuncl_t *tcl = NULL;
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate if ((dminor >= 1) && (dminor <= tcl_nslots) && tcl_slots != NULL) {
3937c478bd9Sstevel@tonic-gate tcl = tcl_slots[dminor - 1];
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate return (tcl);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate * Set up kstats for upper or lower stream.
4017c478bd9Sstevel@tonic-gate */
4027c478bd9Sstevel@tonic-gate static kstat_t *
kstat_setup(kstat_named_t * knt,const char ** names,int nstat,const char * modname,int unitnum)4037c478bd9Sstevel@tonic-gate kstat_setup(kstat_named_t *knt, const char **names, int nstat,
4047c478bd9Sstevel@tonic-gate const char *modname, int unitnum)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate kstat_t *ksp;
4077c478bd9Sstevel@tonic-gate char unitname[KSTAT_STRLEN];
4087c478bd9Sstevel@tonic-gate int i;
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate for (i = 0; i < nstat; i++) {
411d624471bSelowe kstat_set_string(knt[i].name, names[i]);
4127c478bd9Sstevel@tonic-gate knt[i].data_type = KSTAT_DATA_UINT64;
4137c478bd9Sstevel@tonic-gate }
414002c70ffScarlsonj (void) sprintf(unitname, "%s" "%d", modname, unitnum);
415d624471bSelowe ksp = kstat_create(modname, unitnum, unitname, "net",
4167c478bd9Sstevel@tonic-gate KSTAT_TYPE_NAMED, nstat, KSTAT_FLAG_VIRTUAL);
4177c478bd9Sstevel@tonic-gate if (ksp != NULL) {
4187c478bd9Sstevel@tonic-gate ksp->ks_data = (void *)knt;
4197c478bd9Sstevel@tonic-gate kstat_install(ksp);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate return (ksp);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate * sppptun_open()
4267c478bd9Sstevel@tonic-gate *
4277c478bd9Sstevel@tonic-gate * MT-Perimeters:
4287c478bd9Sstevel@tonic-gate * exclusive inner, exclusive outer.
4297c478bd9Sstevel@tonic-gate *
4307c478bd9Sstevel@tonic-gate * Description:
4317c478bd9Sstevel@tonic-gate * Common open procedure for module and driver.
4327c478bd9Sstevel@tonic-gate */
4337c478bd9Sstevel@tonic-gate static int
sppptun_open(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * credp)4347c478bd9Sstevel@tonic-gate sppptun_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *credp)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(oflag))
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /* Allow a re-open */
4397c478bd9Sstevel@tonic-gate if (q->q_ptr != NULL)
4407c478bd9Sstevel@tonic-gate return (0);
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate /* In the off chance that we're on our way out, just return error */
443002c70ffScarlsonj if (tcl_slots == NULL)
4447c478bd9Sstevel@tonic-gate return (EINVAL);
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate if (sflag & MODOPEN) {
4477c478bd9Sstevel@tonic-gate tunll_t *tll;
4487c478bd9Sstevel@tonic-gate char *cp;
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate /* ordinary users have no need to push this module */
451f53eecf5SJames Carlson if (secpolicy_ppp_config(credp) != 0)
4527c478bd9Sstevel@tonic-gate return (EPERM);
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate tll = kmem_zalloc(sizeof (tunll_t), KM_SLEEP);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate tll->tll_index = tunll_index++;
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate tll->tll_wq = WR(q);
459f53eecf5SJames Carlson tll->tll_zoneid = crgetzoneid(credp);
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate /* Insert at end of list */
4627c478bd9Sstevel@tonic-gate insque(&tll->tll_next, tunll_list.q_back);
4637c478bd9Sstevel@tonic-gate q->q_ptr = WR(q)->q_ptr = tll;
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate tll->tll_style = PTS_PPPOE;
4667c478bd9Sstevel@tonic-gate tll->tll_alen = sizeof (tll->tll_lcladdr.pta_pppoe);
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate tll->tll_ksp = kstat_setup((kstat_named_t *)&tll->tll_kstats,
4697c478bd9Sstevel@tonic-gate tll_kstats_list, Dim(tll_kstats_list), "tll",
4707c478bd9Sstevel@tonic-gate tll->tll_index);
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate * Find the name of the driver somewhere beneath us.
4747c478bd9Sstevel@tonic-gate * Note that we have no driver under us until after
4757c478bd9Sstevel@tonic-gate * qprocson().
4767c478bd9Sstevel@tonic-gate */
4777c478bd9Sstevel@tonic-gate qprocson(q);
4787c478bd9Sstevel@tonic-gate for (q = WR(q); q->q_next != NULL; q = q->q_next)
4797c478bd9Sstevel@tonic-gate ;
4807c478bd9Sstevel@tonic-gate cp = NULL;
4817c478bd9Sstevel@tonic-gate if (q->q_qinfo != NULL && q->q_qinfo->qi_minfo != NULL)
4827c478bd9Sstevel@tonic-gate cp = q->q_qinfo->qi_minfo->mi_idname;
4837c478bd9Sstevel@tonic-gate if (cp != NULL && *cp == '\0')
4847c478bd9Sstevel@tonic-gate cp = NULL;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate /* Set initial name; user should overwrite. */
4877c478bd9Sstevel@tonic-gate if (cp == NULL)
4887c478bd9Sstevel@tonic-gate (void) snprintf(tll->tll_name, sizeof (tll->tll_name),
4897c478bd9Sstevel@tonic-gate PPP_TUN_NAME "%d", tll->tll_index);
4907c478bd9Sstevel@tonic-gate else
4917c478bd9Sstevel@tonic-gate (void) snprintf(tll->tll_name, sizeof (tll->tll_name),
4927c478bd9Sstevel@tonic-gate "%s:tun%d", cp, tll->tll_index);
4937c478bd9Sstevel@tonic-gate } else {
4947c478bd9Sstevel@tonic-gate tuncl_t *tcl;
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate ASSERT(devp != NULL);
4977c478bd9Sstevel@tonic-gate if (sflag & CLONEOPEN) {
4987c478bd9Sstevel@tonic-gate tcl = tuncl_alloc(-1);
4997c478bd9Sstevel@tonic-gate } else {
5007c478bd9Sstevel@tonic-gate minor_t mn;
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate * Support of non-clone open (ie, mknod with
5047c478bd9Sstevel@tonic-gate * defined minor number) is supported for
5057c478bd9Sstevel@tonic-gate * testing purposes so that 'arbitrary' minor
5067c478bd9Sstevel@tonic-gate * numbers can be used.
5077c478bd9Sstevel@tonic-gate */
5087c478bd9Sstevel@tonic-gate mn = getminor(*devp);
5097c478bd9Sstevel@tonic-gate if (mn == 0 || (tcl = tcl_by_minor(mn)) != NULL) {
5107c478bd9Sstevel@tonic-gate return (EPERM);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate tcl = tuncl_alloc(mn);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate if (tcl == NULL)
5157c478bd9Sstevel@tonic-gate return (ENOSR);
5167c478bd9Sstevel@tonic-gate tcl->tcl_rq = q; /* save read queue pointer */
5177c478bd9Sstevel@tonic-gate tcl->tcl_flags |= TCLF_ISCLIENT; /* sanity check */
518f53eecf5SJames Carlson tcl->tcl_zoneid = crgetzoneid(credp);
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate q->q_ptr = WR(q)->q_ptr = (caddr_t)tcl;
5217c478bd9Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), tcl->tcl_lsessid);
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate tcl->tcl_ksp = kstat_setup((kstat_named_t *)&tcl->tcl_kstats,
5247c478bd9Sstevel@tonic-gate tcl_kstats_list, Dim(tcl_kstats_list), "tcl",
5257c478bd9Sstevel@tonic-gate tcl->tcl_lsessid);
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate qprocson(q);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate return (0);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate * Create an appropriate control message for this client event.
5347c478bd9Sstevel@tonic-gate */
5357c478bd9Sstevel@tonic-gate static mblk_t *
make_control(tuncl_t * tclabout,tunll_t * tllabout,int action,tuncl_t * tclto)5367c478bd9Sstevel@tonic-gate make_control(tuncl_t *tclabout, tunll_t *tllabout, int action, tuncl_t *tclto)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate struct ppptun_control *ptc;
5397c478bd9Sstevel@tonic-gate mblk_t *mp = allocb(sizeof (*ptc), BPRI_HI);
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate if (mp != NULL) {
5427c478bd9Sstevel@tonic-gate MTYPE(mp) = M_PROTO;
5437c478bd9Sstevel@tonic-gate ptc = (struct ppptun_control *)mp->b_wptr;
544f53eecf5SJames Carlson bzero(ptc, sizeof (*ptc));
5457c478bd9Sstevel@tonic-gate mp->b_wptr += sizeof (*ptc);
5467c478bd9Sstevel@tonic-gate if (tclabout != NULL) {
5477c478bd9Sstevel@tonic-gate ptc->ptc_rsessid = tclabout->tcl_rsessid;
5487c478bd9Sstevel@tonic-gate ptc->ptc_address = tclabout->tcl_address;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate ptc->ptc_discrim = tclto->tcl_ctlval;
5517c478bd9Sstevel@tonic-gate ptc->ptc_action = action;
552f53eecf5SJames Carlson if (tllabout != NULL) {
553f53eecf5SJames Carlson (void) strncpy(ptc->ptc_name, tllabout->tll_name,
554f53eecf5SJames Carlson sizeof (ptc->ptc_name));
555f53eecf5SJames Carlson }
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate return (mp);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate * Send an appropriate control message up this client session.
5627c478bd9Sstevel@tonic-gate */
5637c478bd9Sstevel@tonic-gate static void
send_control(tuncl_t * tclabout,tunll_t * tllabout,int action,tuncl_t * tcl)5647c478bd9Sstevel@tonic-gate send_control(tuncl_t *tclabout, tunll_t *tllabout, int action, tuncl_t *tcl)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate mblk_t *mp;
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate if (tcl->tcl_rq != NULL) {
5697c478bd9Sstevel@tonic-gate mp = make_control(tclabout, tllabout, action, tcl);
5707c478bd9Sstevel@tonic-gate if (mp != NULL) {
5717c478bd9Sstevel@tonic-gate KCINCR(cks_octrl_spec);
5727c478bd9Sstevel@tonic-gate putnext(tcl->tcl_rq, mp);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate * If a lower stream is being unplumbed, then the upper streams
5797c478bd9Sstevel@tonic-gate * connected to this lower stream must be disconnected. This routine
5807c478bd9Sstevel@tonic-gate * accomplishes this by sending M_HANGUP to data streams and M_PROTO
5817c478bd9Sstevel@tonic-gate * messages to control streams. This is called by vmem_walk, and
5827c478bd9Sstevel@tonic-gate * handles a span of minor node numbers.
5837c478bd9Sstevel@tonic-gate *
5847c478bd9Sstevel@tonic-gate * No need to update lks_clients here; the lower stream is on its way
5857c478bd9Sstevel@tonic-gate * out.
5867c478bd9Sstevel@tonic-gate */
5877c478bd9Sstevel@tonic-gate static void
tclvm_remove_tll(void * arg,void * firstv,size_t numv)5887c478bd9Sstevel@tonic-gate tclvm_remove_tll(void *arg, void *firstv, size_t numv)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate tunll_t *tll = (tunll_t *)arg;
5917c478bd9Sstevel@tonic-gate int minorn = (int)(uintptr_t)firstv;
5927c478bd9Sstevel@tonic-gate int minormax = minorn + numv;
5937c478bd9Sstevel@tonic-gate tuncl_t *tcl;
5947c478bd9Sstevel@tonic-gate mblk_t *mp;
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate while (minorn < minormax) {
5977c478bd9Sstevel@tonic-gate tcl = tcl_slots[minorn - 1];
5987c478bd9Sstevel@tonic-gate ASSERT(tcl != NULL);
5997c478bd9Sstevel@tonic-gate if (tcl->tcl_data_tll == tll && tcl->tcl_rq != NULL) {
6007c478bd9Sstevel@tonic-gate tcl->tcl_data_tll = NULL;
6017c478bd9Sstevel@tonic-gate mp = allocb(0, BPRI_HI);
6027c478bd9Sstevel@tonic-gate if (mp != NULL) {
6037c478bd9Sstevel@tonic-gate MTYPE(mp) = M_HANGUP;
6047c478bd9Sstevel@tonic-gate putnext(tcl->tcl_rq, mp);
6057c478bd9Sstevel@tonic-gate if (tcl->tcl_ctrl_tll == tll)
6067c478bd9Sstevel@tonic-gate tcl->tcl_ctrl_tll = NULL;
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate if (tcl->tcl_ctrl_tll == tll) {
6107c478bd9Sstevel@tonic-gate send_control(tcl, tll, PTCA_UNPLUMB, tcl);
6117c478bd9Sstevel@tonic-gate tcl->tcl_ctrl_tll = NULL;
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate minorn++;
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate
6177c478bd9Sstevel@tonic-gate /*
6187c478bd9Sstevel@tonic-gate * sppptun_close()
6197c478bd9Sstevel@tonic-gate *
6207c478bd9Sstevel@tonic-gate * MT-Perimeters:
6217c478bd9Sstevel@tonic-gate * exclusive inner, exclusive outer.
6227c478bd9Sstevel@tonic-gate *
6237c478bd9Sstevel@tonic-gate * Description:
6247c478bd9Sstevel@tonic-gate * Common close procedure for module and driver.
6257c478bd9Sstevel@tonic-gate */
6265e1743f0SToomas Soome /* ARGSUSED */
6277c478bd9Sstevel@tonic-gate static int
sppptun_close(queue_t * q,int flags __unused,cred_t * credp __unused)6285e1743f0SToomas Soome sppptun_close(queue_t *q, int flags __unused, cred_t *credp __unused)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate int err;
6317c478bd9Sstevel@tonic-gate void *qptr;
6327c478bd9Sstevel@tonic-gate tunll_t *tll;
6337c478bd9Sstevel@tonic-gate tuncl_t *tcl;
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate qptr = q->q_ptr;
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate err = 0;
638002c70ffScarlsonj tll = qptr;
6397c478bd9Sstevel@tonic-gate if (!(tll->tll_flags & TLLF_NOTLOWER)) {
6407c478bd9Sstevel@tonic-gate /* q_next is set on modules */
6417c478bd9Sstevel@tonic-gate ASSERT(WR(q)->q_next != NULL);
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate /* unlink any clients using this lower layer. */
6447c478bd9Sstevel@tonic-gate vmem_walk(tcl_minor_arena, VMEM_ALLOC, tclvm_remove_tll, tll);
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /* tell daemon that this has been removed. */
6477c478bd9Sstevel@tonic-gate if ((tcl = tll->tll_defcl) != NULL)
6487c478bd9Sstevel@tonic-gate send_control(NULL, tll, PTCA_UNPLUMB, tcl);
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate tll->tll_flags |= TLLF_CLOSING;
6517c478bd9Sstevel@tonic-gate while (!(tll->tll_flags & TLLF_CLOSE_DONE)) {
6527c478bd9Sstevel@tonic-gate qenable(tll->tll_wq);
6537c478bd9Sstevel@tonic-gate qwait(tll->tll_wq);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate tll->tll_error = 0;
6567c478bd9Sstevel@tonic-gate while (!(tll->tll_flags & TLLF_SHUTDOWN_DONE)) {
6577c478bd9Sstevel@tonic-gate if (!qwait_sig(tll->tll_wq))
6587c478bd9Sstevel@tonic-gate break;
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate qprocsoff(q);
6627c478bd9Sstevel@tonic-gate q->q_ptr = WR(q)->q_ptr = NULL;
6637c478bd9Sstevel@tonic-gate tll->tll_wq = NULL;
6647c478bd9Sstevel@tonic-gate remque(&tll->tll_next);
6657c478bd9Sstevel@tonic-gate err = tll->tll_error;
6667c478bd9Sstevel@tonic-gate if (tll->tll_ksp != NULL)
6677c478bd9Sstevel@tonic-gate kstat_delete(tll->tll_ksp);
6687c478bd9Sstevel@tonic-gate kmem_free(tll, sizeof (*tll));
6697c478bd9Sstevel@tonic-gate } else {
670002c70ffScarlsonj tcl = qptr;
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate /* devices are end of line; no q_next. */
6737c478bd9Sstevel@tonic-gate ASSERT(WR(q)->q_next == NULL);
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate qprocsoff(q);
676002c70ffScarlsonj DTRACE_PROBE1(sppptun__client__close, tuncl_t *, tcl);
6777c478bd9Sstevel@tonic-gate tcl->tcl_rq = NULL;
6787c478bd9Sstevel@tonic-gate q->q_ptr = WR(q)->q_ptr = NULL;
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate tll = TO_TLL(tunll_list.q_forw);
6817c478bd9Sstevel@tonic-gate while (tll != TO_TLL(&tunll_list)) {
6827c478bd9Sstevel@tonic-gate if (tll->tll_defcl == tcl)
6837c478bd9Sstevel@tonic-gate tll->tll_defcl = NULL;
6847c478bd9Sstevel@tonic-gate if (tll->tll_lastcl == tcl)
6857c478bd9Sstevel@tonic-gate tll->tll_lastcl = NULL;
6867c478bd9Sstevel@tonic-gate tll = TO_TLL(tll->tll_next);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate * If this was a normal session, then tell the daemon.
6907c478bd9Sstevel@tonic-gate */
6917c478bd9Sstevel@tonic-gate if (!(tcl->tcl_flags & TCLF_DAEMON) &&
6927c478bd9Sstevel@tonic-gate (tll = tcl->tcl_ctrl_tll) != NULL &&
6937c478bd9Sstevel@tonic-gate tll->tll_defcl != NULL) {
6947c478bd9Sstevel@tonic-gate send_control(tcl, tll, PTCA_DISCONNECT,
6957c478bd9Sstevel@tonic-gate tll->tll_defcl);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate
6987c478bd9Sstevel@tonic-gate /* Update statistics for references being dropped. */
6997c478bd9Sstevel@tonic-gate if ((tll = tcl->tcl_data_tll) != NULL) {
7007c478bd9Sstevel@tonic-gate KLDECR(lks_clients);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate if ((tll = tcl->tcl_ctrl_tll) != NULL) {
7037c478bd9Sstevel@tonic-gate KLDECR(lks_clients);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate tuncl_free(tcl);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate return (err);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate * Allocate and initialize a DLPI or TPI template of the specified
7147c478bd9Sstevel@tonic-gate * length.
7157c478bd9Sstevel@tonic-gate */
7167c478bd9Sstevel@tonic-gate static mblk_t *
pi_alloc(size_t len,int prim)7177c478bd9Sstevel@tonic-gate pi_alloc(size_t len, int prim)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate mblk_t *mp;
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate mp = allocb(len, BPRI_MED);
7227c478bd9Sstevel@tonic-gate if (mp != NULL) {
7237c478bd9Sstevel@tonic-gate MTYPE(mp) = M_PROTO;
7247c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + len;
7257c478bd9Sstevel@tonic-gate bzero(mp->b_rptr, len);
7267c478bd9Sstevel@tonic-gate *(int *)mp->b_rptr = prim;
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate return (mp);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate #define dlpi_alloc(l, p) pi_alloc((l), (p))
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate * Prepend some room to an mblk. Try to reuse the existing buffer, if
7357c478bd9Sstevel@tonic-gate * at all possible, rather than allocating a new one. (Fast-path
7367c478bd9Sstevel@tonic-gate * output should be able to use this.)
7377c478bd9Sstevel@tonic-gate *
7387c478bd9Sstevel@tonic-gate * (XXX why isn't this a library function ...?)
7397c478bd9Sstevel@tonic-gate */
7407c478bd9Sstevel@tonic-gate static mblk_t *
prependb(mblk_t * mp,size_t len,size_t align)7417c478bd9Sstevel@tonic-gate prependb(mblk_t *mp, size_t len, size_t align)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate mblk_t *newmp;
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate if (align == 0)
7477c478bd9Sstevel@tonic-gate align = 8;
7487c478bd9Sstevel@tonic-gate if (DB_REF(mp) > 1 || mp->b_datap->db_base+len > mp->b_rptr ||
7497c478bd9Sstevel@tonic-gate ((uint_t)((uintptr_t)mp->b_rptr - len) % align) != 0) {
7507c478bd9Sstevel@tonic-gate if ((newmp = allocb(len, BPRI_LO)) == NULL) {
7517c478bd9Sstevel@tonic-gate freemsg(mp);
7527c478bd9Sstevel@tonic-gate return (NULL);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate newmp->b_wptr = newmp->b_rptr + len;
7557c478bd9Sstevel@tonic-gate newmp->b_cont = mp;
7567c478bd9Sstevel@tonic-gate return (newmp);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate mp->b_rptr -= len;
7597c478bd9Sstevel@tonic-gate return (mp);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate * sppptun_outpkt()
7647c478bd9Sstevel@tonic-gate *
7657c478bd9Sstevel@tonic-gate * MT-Perimeters:
7667c478bd9Sstevel@tonic-gate * shared inner, shared outer (if called from sppptun_uwput),
7677c478bd9Sstevel@tonic-gate * exclusive inner, shared outer (if called from sppptun_uwsrv).
7687c478bd9Sstevel@tonic-gate *
7697c478bd9Sstevel@tonic-gate * Description:
7707c478bd9Sstevel@tonic-gate * Called from sppptun_uwput or sppptun_uwsrv when processing a
7717c478bd9Sstevel@tonic-gate * M_DATA, M_PROTO, or M_PCPROTO message. For all cases, it tries
7727c478bd9Sstevel@tonic-gate * to prepare the data to be sent to the module below this driver
7737c478bd9Sstevel@tonic-gate * if there is a lower stream linked underneath. If no lower
7747c478bd9Sstevel@tonic-gate * stream exists, then the data will be discarded and an ENXIO
7757c478bd9Sstevel@tonic-gate * error returned.
7767c478bd9Sstevel@tonic-gate *
7777c478bd9Sstevel@tonic-gate * Returns:
7787c478bd9Sstevel@tonic-gate * pointer to queue if caller should do putnext, otherwise
7797c478bd9Sstevel@tonic-gate * *mpp != NULL if message should be enqueued, otherwise
7807c478bd9Sstevel@tonic-gate * *mpp == NULL if message is gone.
7817c478bd9Sstevel@tonic-gate */
7827c478bd9Sstevel@tonic-gate static queue_t *
sppptun_outpkt(queue_t * q,mblk_t ** mpp)7837c478bd9Sstevel@tonic-gate sppptun_outpkt(queue_t *q, mblk_t **mpp)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate mblk_t *mp;
7867c478bd9Sstevel@tonic-gate tuncl_t *tcl;
7877c478bd9Sstevel@tonic-gate tunll_t *tll;
7887c478bd9Sstevel@tonic-gate mblk_t *encmb;
7897c478bd9Sstevel@tonic-gate mblk_t *datamb;
7907c478bd9Sstevel@tonic-gate dl_unitdata_req_t *dur;
7917c478bd9Sstevel@tonic-gate queue_t *lowerq;
7927c478bd9Sstevel@tonic-gate poep_t *poep;
7937c478bd9Sstevel@tonic-gate int len;
7947c478bd9Sstevel@tonic-gate ether_dest_t *edestp;
7957c478bd9Sstevel@tonic-gate enum { luNone, luCopy, luSend } loopup;
7967c478bd9Sstevel@tonic-gate boolean_t isdata;
7977c478bd9Sstevel@tonic-gate struct ppptun_control *ptc;
7987c478bd9Sstevel@tonic-gate
7997c478bd9Sstevel@tonic-gate mp = *mpp;
800002c70ffScarlsonj tcl = q->q_ptr;
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate *mpp = NULL;
8037c478bd9Sstevel@tonic-gate if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
804f53eecf5SJames Carlson /* This should never happen on a lower layer stream */
805f53eecf5SJames Carlson freemsg(mp);
8067c478bd9Sstevel@tonic-gate return (NULL);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate isdata = (MTYPE(mp) == M_DATA);
8107c478bd9Sstevel@tonic-gate if (isdata) {
8117c478bd9Sstevel@tonic-gate tll = tcl->tcl_data_tll;
8127c478bd9Sstevel@tonic-gate ptc = NULL;
8137c478bd9Sstevel@tonic-gate } else {
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate * If data are unaligned or otherwise unsuitable, then
8167c478bd9Sstevel@tonic-gate * discard.
8177c478bd9Sstevel@tonic-gate */
8187c478bd9Sstevel@tonic-gate if (MBLKL(mp) != sizeof (*ptc) || DB_REF(mp) > 1 ||
8197c478bd9Sstevel@tonic-gate !IS_P2ALIGNED(mp->b_rptr, sizeof (ptc))) {
8207c478bd9Sstevel@tonic-gate KCINCR(cks_octrl_drop);
821002c70ffScarlsonj DTRACE_PROBE2(sppptun__bad__control, tuncl_t *, tcl,
822002c70ffScarlsonj mblk_t *, mp);
823f53eecf5SJames Carlson send_control(tcl, tcl->tcl_ctrl_tll, PTCA_BADCTRL, tcl);
824f53eecf5SJames Carlson freemsg(mp);
8257c478bd9Sstevel@tonic-gate return (NULL);
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate ptc = (struct ppptun_control *)mp->b_rptr;
8287c478bd9Sstevel@tonic-gate
8297c478bd9Sstevel@tonic-gate /* Set stream discriminator value if not yet set. */
8307c478bd9Sstevel@tonic-gate if (tcl->tcl_ctlval == 0)
8317c478bd9Sstevel@tonic-gate tcl->tcl_ctlval = ptc->ptc_discrim;
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate /* If this is a test message, then reply to caller. */
8347c478bd9Sstevel@tonic-gate if (ptc->ptc_action == PTCA_TEST) {
835002c70ffScarlsonj DTRACE_PROBE2(sppptun__test, tuncl_t *, tcl,
836002c70ffScarlsonj struct ppptun_control *, ptc);
8377c478bd9Sstevel@tonic-gate if (mp->b_cont != NULL) {
8387c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
8397c478bd9Sstevel@tonic-gate mp->b_cont = NULL;
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate ptc->ptc_discrim = tcl->tcl_ctlval;
8427c478bd9Sstevel@tonic-gate putnext(RD(q), mp);
8437c478bd9Sstevel@tonic-gate return (NULL);
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate /* If this one isn't for us, then discard it */
8477c478bd9Sstevel@tonic-gate if (tcl->tcl_ctlval != ptc->ptc_discrim) {
848002c70ffScarlsonj DTRACE_PROBE2(sppptun__bad__discrim, tuncl_t *, tcl,
849002c70ffScarlsonj struct ppptun_control *, ptc);
8507c478bd9Sstevel@tonic-gate freemsg(mp);
8517c478bd9Sstevel@tonic-gate return (NULL);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate /* Don't allow empty control packets. */
855f53eecf5SJames Carlson tll = tcl->tcl_ctrl_tll;
8567c478bd9Sstevel@tonic-gate if (mp->b_cont == NULL) {
8577c478bd9Sstevel@tonic-gate KCINCR(cks_octrl_drop);
858f53eecf5SJames Carlson DTRACE_PROBE2(sppptun__bad__control, tuncl_t *, tcl,
859f53eecf5SJames Carlson mblk_t *, mp);
860f53eecf5SJames Carlson send_control(tcl, tll, PTCA_BADCTRL, tcl);
861f53eecf5SJames Carlson freemsg(mp);
8627c478bd9Sstevel@tonic-gate return (NULL);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate if (tll == NULL || (lowerq = tll->tll_wq) == NULL) {
867002c70ffScarlsonj DTRACE_PROBE3(sppptun__cannot__send, tuncl_t *, tcl,
868002c70ffScarlsonj tunll_t *, tll, mblk_t *, mp);
869f53eecf5SJames Carlson send_control(tcl, tll, PTCA_UNPLUMB, tcl);
870f53eecf5SJames Carlson freemsg(mp);
8717c478bd9Sstevel@tonic-gate if (isdata) {
8727c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_oerrors++;
8737c478bd9Sstevel@tonic-gate } else {
8747c478bd9Sstevel@tonic-gate KCINCR(cks_octrl_drop);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate return (NULL);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate /*
8807c478bd9Sstevel@tonic-gate * If so, then try to send it down. The lower queue is only
8817c478bd9Sstevel@tonic-gate * ever detached while holding an exclusive lock on the whole
8827c478bd9Sstevel@tonic-gate * driver, so we can be confident that the lower queue is
8837c478bd9Sstevel@tonic-gate * still there.
8847c478bd9Sstevel@tonic-gate */
8857c478bd9Sstevel@tonic-gate if (!bcanputnext(lowerq, mp->b_band)) {
886002c70ffScarlsonj DTRACE_PROBE3(sppptun__flow__control, tuncl_t *, tcl,
887002c70ffScarlsonj tunll_t *, tll, mblk_t *, mp);
8887c478bd9Sstevel@tonic-gate *mpp = mp;
8897c478bd9Sstevel@tonic-gate return (NULL);
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate
8927c478bd9Sstevel@tonic-gate /*
8937c478bd9Sstevel@tonic-gate * Note: DLPI and TPI expect that the first buffer contains
8947c478bd9Sstevel@tonic-gate * the control (unitdata-req) header, destination address, and
8957c478bd9Sstevel@tonic-gate * nothing else. Any protocol headers must go in the next
8967c478bd9Sstevel@tonic-gate * buffer.
8977c478bd9Sstevel@tonic-gate */
8987c478bd9Sstevel@tonic-gate loopup = luNone;
8997c478bd9Sstevel@tonic-gate encmb = NULL;
9007c478bd9Sstevel@tonic-gate if (isdata) {
9017c478bd9Sstevel@tonic-gate if (tll->tll_alen != 0 &&
9027c478bd9Sstevel@tonic-gate bcmp(&tcl->tcl_address, &tll->tll_lcladdr,
903002c70ffScarlsonj tll->tll_alen) == 0)
9047c478bd9Sstevel@tonic-gate loopup = luSend;
9057c478bd9Sstevel@tonic-gate switch (tll->tll_style) {
9067c478bd9Sstevel@tonic-gate case PTS_PPPOE:
9077c478bd9Sstevel@tonic-gate /* Strip address and control fields if present. */
9087c478bd9Sstevel@tonic-gate if (mp->b_rptr[0] == 0xFF) {
9097c478bd9Sstevel@tonic-gate if (MBLKL(mp) < 3) {
9107c478bd9Sstevel@tonic-gate encmb = msgpullup(mp, 3);
9117c478bd9Sstevel@tonic-gate freemsg(mp);
9127c478bd9Sstevel@tonic-gate if ((mp = encmb) == NULL)
9137c478bd9Sstevel@tonic-gate break;
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate mp->b_rptr += 2;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate /* Broadcasting data is probably not a good idea. */
9187c478bd9Sstevel@tonic-gate if (tcl->tcl_address.pta_pppoe.ptma_mac[0] & 1)
9197c478bd9Sstevel@tonic-gate break;
9207c478bd9Sstevel@tonic-gate encmb = dlpi_alloc(sizeof (*dur) + sizeof (*edestp),
9217c478bd9Sstevel@tonic-gate DL_UNITDATA_REQ);
9227c478bd9Sstevel@tonic-gate if (encmb == NULL)
9237c478bd9Sstevel@tonic-gate break;
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate dur = (dl_unitdata_req_t *)encmb->b_rptr;
9267c478bd9Sstevel@tonic-gate dur->dl_dest_addr_length = sizeof (*edestp);
9277c478bd9Sstevel@tonic-gate dur->dl_dest_addr_offset = sizeof (*dur);
9287c478bd9Sstevel@tonic-gate edestp = (ether_dest_t *)(dur + 1);
9297c478bd9Sstevel@tonic-gate ether_copy(tcl->tcl_address.pta_pppoe.ptma_mac,
9307c478bd9Sstevel@tonic-gate edestp->addr);
9317c478bd9Sstevel@tonic-gate /* DLPI SAPs are in host byte order! */
932f53eecf5SJames Carlson edestp->type = tll->tll_sap;
9337c478bd9Sstevel@tonic-gate
9347c478bd9Sstevel@tonic-gate /* Make sure the protocol field isn't compressed. */
9357c478bd9Sstevel@tonic-gate len = (*mp->b_rptr & 1);
9367c478bd9Sstevel@tonic-gate mp = prependb(mp, sizeof (*poep) + len, POE_HDR_ALIGN);
9377c478bd9Sstevel@tonic-gate if (mp == NULL)
9387c478bd9Sstevel@tonic-gate break;
9397c478bd9Sstevel@tonic-gate poep = (poep_t *)mp->b_rptr;
9407c478bd9Sstevel@tonic-gate poep->poep_version_type = POE_VERSION;
9417c478bd9Sstevel@tonic-gate poep->poep_code = POECODE_DATA;
9427c478bd9Sstevel@tonic-gate poep->poep_session_id = htons(tcl->tcl_rsessid);
9437c478bd9Sstevel@tonic-gate poep->poep_length = htons(msgsize(mp) -
9447c478bd9Sstevel@tonic-gate sizeof (*poep));
9457c478bd9Sstevel@tonic-gate if (len > 0)
9467c478bd9Sstevel@tonic-gate *(char *)(poep + 1) = '\0';
9477c478bd9Sstevel@tonic-gate break;
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate default:
9507c478bd9Sstevel@tonic-gate ASSERT(0);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate } else {
9537c478bd9Sstevel@tonic-gate /*
9547c478bd9Sstevel@tonic-gate * Control side encapsulation.
9557c478bd9Sstevel@tonic-gate */
9567c478bd9Sstevel@tonic-gate if (bcmp(&ptc->ptc_address, &tll->tll_lcladdr, tll->tll_alen)
9577c478bd9Sstevel@tonic-gate == 0)
9587c478bd9Sstevel@tonic-gate loopup = luSend;
9597c478bd9Sstevel@tonic-gate datamb = mp->b_cont;
9607c478bd9Sstevel@tonic-gate switch (tll->tll_style) {
9617c478bd9Sstevel@tonic-gate case PTS_PPPOE:
9627c478bd9Sstevel@tonic-gate /*
9637c478bd9Sstevel@tonic-gate * Don't allow a loopback session to establish
9647c478bd9Sstevel@tonic-gate * itself. PPPoE is broken; it uses only one
9657c478bd9Sstevel@tonic-gate * session ID for both data directions, so the
9667c478bd9Sstevel@tonic-gate * loopback data path can simply never work.
9677c478bd9Sstevel@tonic-gate */
9687c478bd9Sstevel@tonic-gate if (loopup == luSend &&
9697c478bd9Sstevel@tonic-gate ((poep_t *)datamb->b_rptr)->poep_code ==
9707c478bd9Sstevel@tonic-gate POECODE_PADR)
9717c478bd9Sstevel@tonic-gate break;
9727c478bd9Sstevel@tonic-gate encmb = dlpi_alloc(sizeof (*dur) + sizeof (*edestp),
9737c478bd9Sstevel@tonic-gate DL_UNITDATA_REQ);
9747c478bd9Sstevel@tonic-gate if (encmb == NULL)
9757c478bd9Sstevel@tonic-gate break;
9767c478bd9Sstevel@tonic-gate dur = (dl_unitdata_req_t *)encmb->b_rptr;
9777c478bd9Sstevel@tonic-gate dur->dl_dest_addr_length = sizeof (*edestp);
9787c478bd9Sstevel@tonic-gate dur->dl_dest_addr_offset = sizeof (*dur);
9797c478bd9Sstevel@tonic-gate
9807c478bd9Sstevel@tonic-gate edestp = (ether_dest_t *)(dur + 1);
9817c478bd9Sstevel@tonic-gate /* DLPI SAPs are in host byte order! */
982f53eecf5SJames Carlson edestp->type = tll->tll_sap;
9837c478bd9Sstevel@tonic-gate
9847c478bd9Sstevel@tonic-gate /*
9857c478bd9Sstevel@tonic-gate * If destination isn't set yet, then we have to
9867c478bd9Sstevel@tonic-gate * allow anything at all. Otherwise, force use
9877c478bd9Sstevel@tonic-gate * of configured peer address.
9887c478bd9Sstevel@tonic-gate */
9897c478bd9Sstevel@tonic-gate if (bcmp(tcl->tcl_address.pta_pppoe.ptma_mac,
9907c478bd9Sstevel@tonic-gate zero_mac_addr, sizeof (zero_mac_addr)) == 0 ||
991002c70ffScarlsonj (tcl->tcl_flags & TCLF_DAEMON)) {
9927c478bd9Sstevel@tonic-gate ether_copy(ptc->ptc_address.pta_pppoe.ptma_mac,
9937c478bd9Sstevel@tonic-gate edestp->addr);
9947c478bd9Sstevel@tonic-gate } else {
9957c478bd9Sstevel@tonic-gate ether_copy(tcl->tcl_address.pta_pppoe.ptma_mac,
9967c478bd9Sstevel@tonic-gate edestp->addr);
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate /* Reflect multicast/broadcast back up. */
9997c478bd9Sstevel@tonic-gate if (edestp->addr[0] & 1)
10007c478bd9Sstevel@tonic-gate loopup = luCopy;
10017c478bd9Sstevel@tonic-gate break;
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate case PTS_PPTP:
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate * PPTP's control side is actually done over
10067c478bd9Sstevel@tonic-gate * separate TCP connections.
10077c478bd9Sstevel@tonic-gate */
10087c478bd9Sstevel@tonic-gate default:
10097c478bd9Sstevel@tonic-gate ASSERT(0);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate freeb(mp);
10127c478bd9Sstevel@tonic-gate mp = datamb;
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate if (mp == NULL || encmb == NULL) {
1015002c70ffScarlsonj DTRACE_PROBE1(sppptun__output__failure, tuncl_t *, tcl);
10167c478bd9Sstevel@tonic-gate freemsg(mp);
10177c478bd9Sstevel@tonic-gate freemsg(encmb);
10187c478bd9Sstevel@tonic-gate if (isdata) {
10197c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_oerrors++;
10207c478bd9Sstevel@tonic-gate } else {
10217c478bd9Sstevel@tonic-gate KCINCR(cks_octrl_drop);
10227c478bd9Sstevel@tonic-gate KLINCR(lks_octrl_drop);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate lowerq = NULL;
10257c478bd9Sstevel@tonic-gate } else {
10267c478bd9Sstevel@tonic-gate if (isdata) {
10277c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_obytes += msgsize(mp);
10287c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_opackets++;
10297c478bd9Sstevel@tonic-gate } else {
10307c478bd9Sstevel@tonic-gate KCINCR(cks_octrls);
10317c478bd9Sstevel@tonic-gate KLINCR(lks_octrls);
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate if (encmb != mp)
10347c478bd9Sstevel@tonic-gate encmb->b_cont = mp;
10357c478bd9Sstevel@tonic-gate switch (loopup) {
10367c478bd9Sstevel@tonic-gate case luNone:
10377c478bd9Sstevel@tonic-gate *mpp = encmb;
10387c478bd9Sstevel@tonic-gate break;
10397c478bd9Sstevel@tonic-gate case luCopy:
10407c478bd9Sstevel@tonic-gate mp = copymsg(encmb);
10417c478bd9Sstevel@tonic-gate if (mp != NULL)
1042*1ad5cbf2SToomas Soome (void) sppptun_urput(RD(lowerq), mp);
10437c478bd9Sstevel@tonic-gate *mpp = encmb;
10447c478bd9Sstevel@tonic-gate break;
10457c478bd9Sstevel@tonic-gate case luSend:
1046*1ad5cbf2SToomas Soome (void) sppptun_urput(RD(lowerq), encmb);
10477c478bd9Sstevel@tonic-gate lowerq = NULL;
10487c478bd9Sstevel@tonic-gate break;
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate return (lowerq);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate
10547c478bd9Sstevel@tonic-gate /*
10557c478bd9Sstevel@tonic-gate * Enqueue a message to be sent when the lower stream is closed. This
10567c478bd9Sstevel@tonic-gate * is done so that we're guaranteed that we always have the necessary
10577c478bd9Sstevel@tonic-gate * resources to properly detach ourselves from the system. (If we
10587c478bd9Sstevel@tonic-gate * waited until the close was done to allocate these messages, then
10597c478bd9Sstevel@tonic-gate * the message allocation could fail, and we'd be unable to properly
10607c478bd9Sstevel@tonic-gate * detach.)
10617c478bd9Sstevel@tonic-gate */
10627c478bd9Sstevel@tonic-gate static void
save_for_close(tunll_t * tll,mblk_t * mp)10637c478bd9Sstevel@tonic-gate save_for_close(tunll_t *tll, mblk_t *mp)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate mblk_t *onc;
10667c478bd9Sstevel@tonic-gate
10677c478bd9Sstevel@tonic-gate if ((onc = tll->tll_onclose) == NULL)
10687c478bd9Sstevel@tonic-gate tll->tll_onclose = mp;
10697c478bd9Sstevel@tonic-gate else {
10707c478bd9Sstevel@tonic-gate while (onc->b_next != NULL)
10717c478bd9Sstevel@tonic-gate onc = onc->b_next;
10727c478bd9Sstevel@tonic-gate onc->b_next = mp;
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate /*
10777c478bd9Sstevel@tonic-gate * Given the lower stream name, locate the state structure. Note that
10787c478bd9Sstevel@tonic-gate * lookup of tcl pointers (and use of those pointers) is safe because
10797c478bd9Sstevel@tonic-gate * modification is done only when exclusive on both inner and outer
10807c478bd9Sstevel@tonic-gate * perimeters.
10817c478bd9Sstevel@tonic-gate */
10827c478bd9Sstevel@tonic-gate static tunll_t *
tll_lookup_on_name(const char * dname,zoneid_t zoneid)1083f53eecf5SJames Carlson tll_lookup_on_name(const char *dname, zoneid_t zoneid)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate tunll_t *tll;
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate tll = TO_TLL(tunll_list.q_forw);
10887c478bd9Sstevel@tonic-gate for (; tll != TO_TLL(&tunll_list); tll = TO_TLL(tll->tll_next))
1089f53eecf5SJames Carlson if (tll->tll_zoneid == zoneid &&
1090f53eecf5SJames Carlson strcmp(dname, tll->tll_name) == 0)
10917c478bd9Sstevel@tonic-gate return (tll);
10927c478bd9Sstevel@tonic-gate return (NULL);
10937c478bd9Sstevel@tonic-gate }
10947c478bd9Sstevel@tonic-gate
10957c478bd9Sstevel@tonic-gate /*
10967c478bd9Sstevel@tonic-gate * sppptun_inner_ioctl()
10977c478bd9Sstevel@tonic-gate *
10987c478bd9Sstevel@tonic-gate * MT-Perimeters:
10997c478bd9Sstevel@tonic-gate * exclusive inner, shared outer.
11007c478bd9Sstevel@tonic-gate *
11017c478bd9Sstevel@tonic-gate * Description:
11027c478bd9Sstevel@tonic-gate * Called by qwriter from sppptun_ioctl as the result of receiving
11037c478bd9Sstevel@tonic-gate * a handled ioctl.
11047c478bd9Sstevel@tonic-gate */
11057c478bd9Sstevel@tonic-gate static void
sppptun_inner_ioctl(queue_t * q,mblk_t * mp)11067c478bd9Sstevel@tonic-gate sppptun_inner_ioctl(queue_t *q, mblk_t *mp)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate struct iocblk *iop;
11097c478bd9Sstevel@tonic-gate int rc = 0;
11107c478bd9Sstevel@tonic-gate int len = 0;
11117c478bd9Sstevel@tonic-gate int i;
11127c478bd9Sstevel@tonic-gate tuncl_t *tcl;
11137c478bd9Sstevel@tonic-gate tunll_t *tll;
11147c478bd9Sstevel@tonic-gate union ppptun_name *ptn;
11157c478bd9Sstevel@tonic-gate struct ppptun_info *pti;
11167c478bd9Sstevel@tonic-gate struct ppptun_peer *ptp;
11177c478bd9Sstevel@tonic-gate mblk_t *mptmp;
11187c478bd9Sstevel@tonic-gate ppptun_atype *pap;
11197c478bd9Sstevel@tonic-gate struct ppp_stats64 *psp;
1120f53eecf5SJames Carlson zoneid_t zoneid;
11217c478bd9Sstevel@tonic-gate
11227c478bd9Sstevel@tonic-gate iop = (struct iocblk *)mp->b_rptr;
11237c478bd9Sstevel@tonic-gate tcl = NULL;
1124002c70ffScarlsonj tll = q->q_ptr;
11257c478bd9Sstevel@tonic-gate if (tll->tll_flags & TLLF_NOTLOWER) {
11267c478bd9Sstevel@tonic-gate tcl = (tuncl_t *)tll;
11277c478bd9Sstevel@tonic-gate tll = NULL;
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate
1130002c70ffScarlsonj DTRACE_PROBE3(sppptun__ioctl, tuncl_t *, tcl, tunll_t *, tll,
1131002c70ffScarlsonj struct iocblk *, iop);
1132002c70ffScarlsonj
11337c478bd9Sstevel@tonic-gate switch (iop->ioc_cmd) {
11347c478bd9Sstevel@tonic-gate case PPPIO_DEBUG:
1135002c70ffScarlsonj /*
1136002c70ffScarlsonj * Debug requests are now ignored; use dtrace or wireshark
1137002c70ffScarlsonj * instead.
1138002c70ffScarlsonj */
11397c478bd9Sstevel@tonic-gate break;
11407c478bd9Sstevel@tonic-gate
11417c478bd9Sstevel@tonic-gate case PPPIO_GETSTAT:
11427c478bd9Sstevel@tonic-gate rc = EINVAL;
11437c478bd9Sstevel@tonic-gate break;
11447c478bd9Sstevel@tonic-gate
11457c478bd9Sstevel@tonic-gate case PPPIO_GETSTAT64:
11467c478bd9Sstevel@tonic-gate /* Client (device) side only */
11477c478bd9Sstevel@tonic-gate if (tcl == NULL) {
11487c478bd9Sstevel@tonic-gate rc = EINVAL;
11497c478bd9Sstevel@tonic-gate break;
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate mptmp = allocb(sizeof (*psp), BPRI_HI);
11527c478bd9Sstevel@tonic-gate if (mptmp == NULL) {
11537c478bd9Sstevel@tonic-gate rc = ENOSR;
11547c478bd9Sstevel@tonic-gate break;
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
11577c478bd9Sstevel@tonic-gate mp->b_cont = mptmp;
11587c478bd9Sstevel@tonic-gate
11597c478bd9Sstevel@tonic-gate psp = (struct ppp_stats64 *)mptmp->b_wptr;
11607c478bd9Sstevel@tonic-gate bzero((caddr_t)psp, sizeof (*psp));
11617c478bd9Sstevel@tonic-gate psp->p = tcl->tcl_stats;
11627c478bd9Sstevel@tonic-gate
11637c478bd9Sstevel@tonic-gate len = sizeof (*psp);
11647c478bd9Sstevel@tonic-gate break;
11657c478bd9Sstevel@tonic-gate
11667c478bd9Sstevel@tonic-gate case PPPTUN_SNAME:
11677c478bd9Sstevel@tonic-gate /* This is done on the *module* (lower level) side. */
11687c478bd9Sstevel@tonic-gate if (tll == NULL || mp->b_cont == NULL ||
11697c478bd9Sstevel@tonic-gate iop->ioc_count != sizeof (*ptn) ||
11707c478bd9Sstevel@tonic-gate *mp->b_cont->b_rptr == '\0') {
11717c478bd9Sstevel@tonic-gate rc = EINVAL;
11727c478bd9Sstevel@tonic-gate break;
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate
11757c478bd9Sstevel@tonic-gate ptn = (union ppptun_name *)mp->b_cont->b_rptr;
11767c478bd9Sstevel@tonic-gate ptn->ptn_name[sizeof (ptn->ptn_name) - 1] = '\0';
11777c478bd9Sstevel@tonic-gate
1178f53eecf5SJames Carlson tll = tll_lookup_on_name(ptn->ptn_name, tll->tll_zoneid);
1179f53eecf5SJames Carlson if (tll != NULL) {
11807c478bd9Sstevel@tonic-gate rc = EEXIST;
11817c478bd9Sstevel@tonic-gate break;
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate tll = (tunll_t *)q->q_ptr;
11847c478bd9Sstevel@tonic-gate (void) strcpy(tll->tll_name, ptn->ptn_name);
11857c478bd9Sstevel@tonic-gate break;
11867c478bd9Sstevel@tonic-gate
11877c478bd9Sstevel@tonic-gate case PPPTUN_SINFO:
11887c478bd9Sstevel@tonic-gate case PPPTUN_GINFO:
11897c478bd9Sstevel@tonic-gate /* Either side */
11907c478bd9Sstevel@tonic-gate if (mp->b_cont == NULL || iop->ioc_count != sizeof (*pti)) {
11917c478bd9Sstevel@tonic-gate rc = EINVAL;
11927c478bd9Sstevel@tonic-gate break;
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate pti = (struct ppptun_info *)mp->b_cont->b_rptr;
11957c478bd9Sstevel@tonic-gate if (pti->pti_name[0] != '\0')
1196f53eecf5SJames Carlson tll = tll_lookup_on_name(pti->pti_name,
1197f53eecf5SJames Carlson tcl == NULL ? tll->tll_zoneid : tcl->tcl_zoneid);
11987c478bd9Sstevel@tonic-gate if (tll == NULL) {
11997c478bd9Sstevel@tonic-gate /* Driver (client) side must have name */
1200002c70ffScarlsonj if (tcl != NULL && pti->pti_name[0] == '\0')
12017c478bd9Sstevel@tonic-gate rc = EINVAL;
1202002c70ffScarlsonj else
12037c478bd9Sstevel@tonic-gate rc = ESRCH;
12047c478bd9Sstevel@tonic-gate break;
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate if (iop->ioc_cmd == PPPTUN_GINFO) {
12077c478bd9Sstevel@tonic-gate pti->pti_muxid = tll->tll_muxid;
12087c478bd9Sstevel@tonic-gate pti->pti_style = tll->tll_style;
12097c478bd9Sstevel@tonic-gate len = sizeof (*pti);
12107c478bd9Sstevel@tonic-gate break;
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate tll->tll_muxid = pti->pti_muxid;
12137c478bd9Sstevel@tonic-gate tll->tll_style = pti->pti_style;
12147c478bd9Sstevel@tonic-gate switch (tll->tll_style) {
12157c478bd9Sstevel@tonic-gate case PTS_PPPOE: /* DLPI type */
12167c478bd9Sstevel@tonic-gate tll->tll_alen = sizeof (tll->tll_lcladdr.pta_pppoe);
12177c478bd9Sstevel@tonic-gate mptmp = dlpi_alloc(sizeof (dl_unbind_req_t),
12187c478bd9Sstevel@tonic-gate DL_UNBIND_REQ);
12197c478bd9Sstevel@tonic-gate if (mptmp == NULL) {
12207c478bd9Sstevel@tonic-gate rc = ENOSR;
12217c478bd9Sstevel@tonic-gate break;
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate save_for_close(tll, mptmp);
12247c478bd9Sstevel@tonic-gate mptmp = dlpi_alloc(sizeof (dl_detach_req_t),
12257c478bd9Sstevel@tonic-gate DL_DETACH_REQ);
12267c478bd9Sstevel@tonic-gate if (mptmp == NULL) {
12277c478bd9Sstevel@tonic-gate rc = ENOSR;
12287c478bd9Sstevel@tonic-gate break;
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate save_for_close(tll, mptmp);
12317c478bd9Sstevel@tonic-gate break;
12327c478bd9Sstevel@tonic-gate default:
12337c478bd9Sstevel@tonic-gate tll->tll_style = PTS_NONE;
12347c478bd9Sstevel@tonic-gate tll->tll_alen = 0;
12357c478bd9Sstevel@tonic-gate rc = EINVAL;
12367c478bd9Sstevel@tonic-gate break;
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate break;
12397c478bd9Sstevel@tonic-gate
12407c478bd9Sstevel@tonic-gate case PPPTUN_GNNAME:
12417c478bd9Sstevel@tonic-gate /* This can be done on either side. */
12427c478bd9Sstevel@tonic-gate if (mp->b_cont == NULL || iop->ioc_count < sizeof (uint32_t)) {
12437c478bd9Sstevel@tonic-gate rc = EINVAL;
12447c478bd9Sstevel@tonic-gate break;
12457c478bd9Sstevel@tonic-gate }
1246f53eecf5SJames Carlson zoneid = tcl == NULL ? tll->tll_zoneid : tcl->tcl_zoneid;
12477c478bd9Sstevel@tonic-gate ptn = (union ppptun_name *)mp->b_cont->b_rptr;
12487c478bd9Sstevel@tonic-gate i = ptn->ptn_index;
12497c478bd9Sstevel@tonic-gate tll = TO_TLL(tunll_list.q_forw);
1250f53eecf5SJames Carlson while (tll != TO_TLL(&tunll_list)) {
1251f53eecf5SJames Carlson if (tll->tll_zoneid == zoneid && --i < 0)
1252f53eecf5SJames Carlson break;
12537c478bd9Sstevel@tonic-gate tll = TO_TLL(tll->tll_next);
1254f53eecf5SJames Carlson }
12557c478bd9Sstevel@tonic-gate if (tll != TO_TLL(&tunll_list)) {
12567c478bd9Sstevel@tonic-gate bcopy(tll->tll_name, ptn->ptn_name,
12577c478bd9Sstevel@tonic-gate sizeof (ptn->ptn_name));
12587c478bd9Sstevel@tonic-gate } else {
12597c478bd9Sstevel@tonic-gate bzero(ptn, sizeof (*ptn));
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate len = sizeof (*ptn);
12627c478bd9Sstevel@tonic-gate break;
12637c478bd9Sstevel@tonic-gate
12647c478bd9Sstevel@tonic-gate case PPPTUN_LCLADDR:
12657c478bd9Sstevel@tonic-gate /* This is done on the *module* (lower level) side. */
12667c478bd9Sstevel@tonic-gate if (tll == NULL || mp->b_cont == NULL) {
12677c478bd9Sstevel@tonic-gate rc = EINVAL;
12687c478bd9Sstevel@tonic-gate break;
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate
12717c478bd9Sstevel@tonic-gate pap = &tll->tll_lcladdr;
12727c478bd9Sstevel@tonic-gate len = tll->tll_alen;
12737c478bd9Sstevel@tonic-gate if (len == 0 || len > iop->ioc_count) {
12747c478bd9Sstevel@tonic-gate rc = EINVAL;
12757c478bd9Sstevel@tonic-gate break;
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate bcopy(mp->b_cont->b_rptr, pap, len);
12787c478bd9Sstevel@tonic-gate len = 0;
12797c478bd9Sstevel@tonic-gate break;
12807c478bd9Sstevel@tonic-gate
12817c478bd9Sstevel@tonic-gate case PPPTUN_SPEER:
12827c478bd9Sstevel@tonic-gate /* Client (device) side only; before SDATA */
12837c478bd9Sstevel@tonic-gate if (tcl == NULL || mp->b_cont == NULL ||
12847c478bd9Sstevel@tonic-gate iop->ioc_count != sizeof (*ptp)) {
12857c478bd9Sstevel@tonic-gate rc = EINVAL;
12867c478bd9Sstevel@tonic-gate break;
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate if (tcl->tcl_data_tll != NULL) {
12897c478bd9Sstevel@tonic-gate rc = EINVAL;
12907c478bd9Sstevel@tonic-gate break;
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate ptp = (struct ppptun_peer *)mp->b_cont->b_rptr;
1293002c70ffScarlsonj DTRACE_PROBE2(sppptun__speer, tuncl_t *, tcl,
1294002c70ffScarlsonj struct ppptun_peer *, ptp);
12957c478bd9Sstevel@tonic-gate /* Once set, the style cannot change. */
12967c478bd9Sstevel@tonic-gate if (tcl->tcl_style != PTS_NONE &&
12977c478bd9Sstevel@tonic-gate tcl->tcl_style != ptp->ptp_style) {
12987c478bd9Sstevel@tonic-gate rc = EINVAL;
12997c478bd9Sstevel@tonic-gate break;
13007c478bd9Sstevel@tonic-gate }
13017c478bd9Sstevel@tonic-gate if (ptp->ptp_flags & PTPF_DAEMON) {
13027c478bd9Sstevel@tonic-gate /* User requests registration for tunnel 0 */
13037c478bd9Sstevel@tonic-gate if ((tcl->tcl_flags & TCLF_SPEER_DONE) ||
13047c478bd9Sstevel@tonic-gate ptp->ptp_ltunid != 0 || ptp->ptp_rtunid != 0 ||
13057c478bd9Sstevel@tonic-gate ptp->ptp_lsessid != 0 || ptp->ptp_rsessid != 0) {
13067c478bd9Sstevel@tonic-gate rc = EINVAL;
13077c478bd9Sstevel@tonic-gate break;
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate tcl->tcl_flags |= TCLF_DAEMON;
13107c478bd9Sstevel@tonic-gate } else {
13117c478bd9Sstevel@tonic-gate /* Normal client connection */
13127c478bd9Sstevel@tonic-gate if (tcl->tcl_flags & TCLF_DAEMON) {
13137c478bd9Sstevel@tonic-gate rc = EINVAL;
13147c478bd9Sstevel@tonic-gate break;
13157c478bd9Sstevel@tonic-gate }
13167c478bd9Sstevel@tonic-gate if (ptp->ptp_lsessid != 0 &&
13177c478bd9Sstevel@tonic-gate ptp->ptp_lsessid != tcl->tcl_lsessid) {
13187c478bd9Sstevel@tonic-gate rc = EINVAL;
13197c478bd9Sstevel@tonic-gate break;
13207c478bd9Sstevel@tonic-gate }
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate * If we're reassigning the peer data, then
13237c478bd9Sstevel@tonic-gate * the previous assignment must have been for
13247c478bd9Sstevel@tonic-gate * a client control connection. Check that.
13257c478bd9Sstevel@tonic-gate */
13267c478bd9Sstevel@tonic-gate if ((tcl->tcl_flags & TCLF_SPEER_DONE) &&
13277c478bd9Sstevel@tonic-gate ((tcl->tcl_ltunid != 0 &&
1328002c70ffScarlsonj tcl->tcl_ltunid != ptp->ptp_ltunid) ||
1329002c70ffScarlsonj (tcl->tcl_rtunid != 0 &&
1330002c70ffScarlsonj tcl->tcl_rtunid != ptp->ptp_rtunid) ||
1331002c70ffScarlsonj (tcl->tcl_rsessid != 0 &&
1332002c70ffScarlsonj tcl->tcl_rsessid != ptp->ptp_rsessid))) {
13337c478bd9Sstevel@tonic-gate rc = EINVAL;
13347c478bd9Sstevel@tonic-gate break;
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate if ((tcl->tcl_ltunid = ptp->ptp_ltunid) == 0 &&
13377c478bd9Sstevel@tonic-gate tcl->tcl_style == PTS_L2FTP)
13387c478bd9Sstevel@tonic-gate tcl->tcl_ltunid = ptp->ptp_lsessid;
13397c478bd9Sstevel@tonic-gate tcl->tcl_rtunid = ptp->ptp_rtunid;
13407c478bd9Sstevel@tonic-gate tcl->tcl_rsessid = ptp->ptp_rsessid;
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate tcl->tcl_flags |= TCLF_SPEER_DONE;
13437c478bd9Sstevel@tonic-gate tcl->tcl_style = ptp->ptp_style;
13447c478bd9Sstevel@tonic-gate tcl->tcl_address = ptp->ptp_address;
13457c478bd9Sstevel@tonic-gate goto fill_in_peer;
13467c478bd9Sstevel@tonic-gate
13477c478bd9Sstevel@tonic-gate case PPPTUN_GPEER:
13487c478bd9Sstevel@tonic-gate /* Client (device) side only */
13497c478bd9Sstevel@tonic-gate if (tcl == NULL) {
13507c478bd9Sstevel@tonic-gate rc = EINVAL;
13517c478bd9Sstevel@tonic-gate break;
13527c478bd9Sstevel@tonic-gate }
13537c478bd9Sstevel@tonic-gate if (mp->b_cont != NULL)
13547c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
13557c478bd9Sstevel@tonic-gate mp->b_cont = allocb(sizeof (*ptp), BPRI_HI);
13567c478bd9Sstevel@tonic-gate if (mp->b_cont == NULL) {
13577c478bd9Sstevel@tonic-gate rc = ENOSR;
13587c478bd9Sstevel@tonic-gate break;
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate ptp = (struct ppptun_peer *)mp->b_cont->b_rptr;
13617c478bd9Sstevel@tonic-gate fill_in_peer:
13627c478bd9Sstevel@tonic-gate ptp->ptp_style = tcl->tcl_style;
13637c478bd9Sstevel@tonic-gate ptp->ptp_flags = (tcl->tcl_flags & TCLF_DAEMON) ? PTPF_DAEMON :
13647c478bd9Sstevel@tonic-gate 0;
13657c478bd9Sstevel@tonic-gate ptp->ptp_ltunid = tcl->tcl_ltunid;
13667c478bd9Sstevel@tonic-gate ptp->ptp_rtunid = tcl->tcl_rtunid;
13677c478bd9Sstevel@tonic-gate ptp->ptp_lsessid = tcl->tcl_lsessid;
13687c478bd9Sstevel@tonic-gate ptp->ptp_rsessid = tcl->tcl_rsessid;
13697c478bd9Sstevel@tonic-gate ptp->ptp_address = tcl->tcl_address;
13707c478bd9Sstevel@tonic-gate len = sizeof (*ptp);
13717c478bd9Sstevel@tonic-gate break;
13727c478bd9Sstevel@tonic-gate
13737c478bd9Sstevel@tonic-gate case PPPTUN_SDATA:
13747c478bd9Sstevel@tonic-gate case PPPTUN_SCTL:
13757c478bd9Sstevel@tonic-gate /* Client (device) side only; must do SPEER first */
13767c478bd9Sstevel@tonic-gate if (tcl == NULL || mp->b_cont == NULL ||
13777c478bd9Sstevel@tonic-gate iop->ioc_count != sizeof (*ptn) ||
13787c478bd9Sstevel@tonic-gate *mp->b_cont->b_rptr == '\0') {
13797c478bd9Sstevel@tonic-gate rc = EINVAL;
13807c478bd9Sstevel@tonic-gate break;
13817c478bd9Sstevel@tonic-gate }
13827c478bd9Sstevel@tonic-gate if (!(tcl->tcl_flags & TCLF_SPEER_DONE)) {
13837c478bd9Sstevel@tonic-gate rc = EINVAL;
13847c478bd9Sstevel@tonic-gate break;
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate ptn = (union ppptun_name *)mp->b_cont->b_rptr;
13877c478bd9Sstevel@tonic-gate ptn->ptn_name[sizeof (ptn->ptn_name) - 1] = '\0';
1388f53eecf5SJames Carlson tll = tll_lookup_on_name(ptn->ptn_name, tcl->tcl_zoneid);
13897c478bd9Sstevel@tonic-gate if (tll == NULL) {
13907c478bd9Sstevel@tonic-gate rc = ESRCH;
13917c478bd9Sstevel@tonic-gate break;
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate if (tll->tll_style != tcl->tcl_style) {
13947c478bd9Sstevel@tonic-gate rc = ENXIO;
13957c478bd9Sstevel@tonic-gate break;
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate if (iop->ioc_cmd == PPPTUN_SDATA) {
13987c478bd9Sstevel@tonic-gate if (tcl->tcl_data_tll != NULL) {
13997c478bd9Sstevel@tonic-gate rc = EEXIST;
14007c478bd9Sstevel@tonic-gate break;
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate /* server daemons cannot use regular data */
14037c478bd9Sstevel@tonic-gate if (tcl->tcl_flags & TCLF_DAEMON) {
14047c478bd9Sstevel@tonic-gate rc = EINVAL;
14057c478bd9Sstevel@tonic-gate break;
14067c478bd9Sstevel@tonic-gate }
14077c478bd9Sstevel@tonic-gate tcl->tcl_data_tll = tll;
14087c478bd9Sstevel@tonic-gate } else if (tcl->tcl_flags & TCLF_DAEMON) {
14097c478bd9Sstevel@tonic-gate if (tll->tll_defcl != NULL && tll->tll_defcl != tcl) {
14107c478bd9Sstevel@tonic-gate rc = EEXIST;
14117c478bd9Sstevel@tonic-gate break;
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate tll->tll_defcl = tcl;
14147c478bd9Sstevel@tonic-gate if (tcl->tcl_ctrl_tll != NULL) {
14157c478bd9Sstevel@tonic-gate KDECR(tcl->tcl_ctrl_tll, tll_kstats,
14167c478bd9Sstevel@tonic-gate lks_clients);
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate tcl->tcl_ctrl_tll = tll;
14197c478bd9Sstevel@tonic-gate } else {
14207c478bd9Sstevel@tonic-gate if (tcl->tcl_ctrl_tll != NULL) {
14217c478bd9Sstevel@tonic-gate rc = EEXIST;
14227c478bd9Sstevel@tonic-gate break;
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate tcl->tcl_ctrl_tll = tll;
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate KLINCR(lks_clients);
14277c478bd9Sstevel@tonic-gate break;
14287c478bd9Sstevel@tonic-gate
14297c478bd9Sstevel@tonic-gate case PPPTUN_GDATA:
14307c478bd9Sstevel@tonic-gate case PPPTUN_GCTL:
14317c478bd9Sstevel@tonic-gate /* Client (device) side only */
14327c478bd9Sstevel@tonic-gate if (tcl == NULL) {
14337c478bd9Sstevel@tonic-gate rc = EINVAL;
14347c478bd9Sstevel@tonic-gate break;
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate if (mp->b_cont != NULL)
14377c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
14387c478bd9Sstevel@tonic-gate mp->b_cont = allocb(sizeof (*ptn), BPRI_HI);
14397c478bd9Sstevel@tonic-gate if (mp->b_cont == NULL) {
14407c478bd9Sstevel@tonic-gate rc = ENOSR;
14417c478bd9Sstevel@tonic-gate break;
14427c478bd9Sstevel@tonic-gate }
14437c478bd9Sstevel@tonic-gate ptn = (union ppptun_name *)mp->b_cont->b_rptr;
14447c478bd9Sstevel@tonic-gate if (iop->ioc_cmd == PPPTUN_GDATA)
14457c478bd9Sstevel@tonic-gate tll = tcl->tcl_data_tll;
14467c478bd9Sstevel@tonic-gate else
14477c478bd9Sstevel@tonic-gate tll = tcl->tcl_ctrl_tll;
14487c478bd9Sstevel@tonic-gate if (tll == NULL)
14497c478bd9Sstevel@tonic-gate bzero(ptn, sizeof (*ptn));
14507c478bd9Sstevel@tonic-gate else
14517c478bd9Sstevel@tonic-gate bcopy(tll->tll_name, ptn->ptn_name,
14527c478bd9Sstevel@tonic-gate sizeof (ptn->ptn_name));
14537c478bd9Sstevel@tonic-gate len = sizeof (*ptn);
14547c478bd9Sstevel@tonic-gate break;
14557c478bd9Sstevel@tonic-gate
14567c478bd9Sstevel@tonic-gate case PPPTUN_DCTL:
14577c478bd9Sstevel@tonic-gate /* Client (device) side daemon mode only */
14587c478bd9Sstevel@tonic-gate if (tcl == NULL || mp->b_cont == NULL ||
14597c478bd9Sstevel@tonic-gate iop->ioc_count != sizeof (*ptn) ||
14607c478bd9Sstevel@tonic-gate !(tcl->tcl_flags & TCLF_DAEMON)) {
14617c478bd9Sstevel@tonic-gate rc = EINVAL;
14627c478bd9Sstevel@tonic-gate break;
14637c478bd9Sstevel@tonic-gate }
14647c478bd9Sstevel@tonic-gate ptn = (union ppptun_name *)mp->b_cont->b_rptr;
14657c478bd9Sstevel@tonic-gate ptn->ptn_name[sizeof (ptn->ptn_name) - 1] = '\0';
1466f53eecf5SJames Carlson tll = tll_lookup_on_name(ptn->ptn_name, tcl->tcl_zoneid);
14677c478bd9Sstevel@tonic-gate if (tll == NULL || tll->tll_defcl != tcl) {
14687c478bd9Sstevel@tonic-gate rc = ESRCH;
14697c478bd9Sstevel@tonic-gate break;
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate tll->tll_defcl = NULL;
14727c478bd9Sstevel@tonic-gate break;
14737c478bd9Sstevel@tonic-gate
1474f53eecf5SJames Carlson case PPPTUN_SSAP:
1475f53eecf5SJames Carlson /* This is done on the *module* (lower level) side. */
1476f53eecf5SJames Carlson if (tll == NULL || mp->b_cont == NULL ||
1477f53eecf5SJames Carlson iop->ioc_count != sizeof (uint_t)) {
1478f53eecf5SJames Carlson rc = EINVAL;
1479f53eecf5SJames Carlson break;
1480f53eecf5SJames Carlson }
1481f53eecf5SJames Carlson
1482f53eecf5SJames Carlson tll->tll_sap = *(uint_t *)mp->b_cont->b_rptr;
1483f53eecf5SJames Carlson break;
1484f53eecf5SJames Carlson
14857c478bd9Sstevel@tonic-gate default:
14867c478bd9Sstevel@tonic-gate /* Caller should already have checked command value */
14877c478bd9Sstevel@tonic-gate ASSERT(0);
14887c478bd9Sstevel@tonic-gate }
14897c478bd9Sstevel@tonic-gate if (rc != 0) {
14907c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, rc);
14917c478bd9Sstevel@tonic-gate } else {
14927c478bd9Sstevel@tonic-gate if (len > 0)
14937c478bd9Sstevel@tonic-gate mp->b_cont->b_wptr = mp->b_cont->b_rptr + len;
14947c478bd9Sstevel@tonic-gate miocack(q, mp, len, 0);
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate
14987c478bd9Sstevel@tonic-gate /*
14997c478bd9Sstevel@tonic-gate * sppptun_ioctl()
15007c478bd9Sstevel@tonic-gate *
15017c478bd9Sstevel@tonic-gate * MT-Perimeters:
15027c478bd9Sstevel@tonic-gate * shared inner, shared outer.
15037c478bd9Sstevel@tonic-gate *
15047c478bd9Sstevel@tonic-gate * Description:
15057c478bd9Sstevel@tonic-gate * Called by sppptun_uwput as the result of receiving a M_IOCTL command.
15067c478bd9Sstevel@tonic-gate */
15077c478bd9Sstevel@tonic-gate static void
sppptun_ioctl(queue_t * q,mblk_t * mp)15087c478bd9Sstevel@tonic-gate sppptun_ioctl(queue_t *q, mblk_t *mp)
15097c478bd9Sstevel@tonic-gate {
15107c478bd9Sstevel@tonic-gate struct iocblk *iop;
15117c478bd9Sstevel@tonic-gate int rc = 0;
15127c478bd9Sstevel@tonic-gate int len = 0;
15137c478bd9Sstevel@tonic-gate uint32_t val = 0;
15147c478bd9Sstevel@tonic-gate tunll_t *tll;
15157c478bd9Sstevel@tonic-gate
15167c478bd9Sstevel@tonic-gate iop = (struct iocblk *)mp->b_rptr;
15177c478bd9Sstevel@tonic-gate
15187c478bd9Sstevel@tonic-gate switch (iop->ioc_cmd) {
15197c478bd9Sstevel@tonic-gate case PPPIO_DEBUG:
15207c478bd9Sstevel@tonic-gate case PPPIO_GETSTAT:
15217c478bd9Sstevel@tonic-gate case PPPIO_GETSTAT64:
15227c478bd9Sstevel@tonic-gate case PPPTUN_SNAME:
15237c478bd9Sstevel@tonic-gate case PPPTUN_SINFO:
15247c478bd9Sstevel@tonic-gate case PPPTUN_GINFO:
15257c478bd9Sstevel@tonic-gate case PPPTUN_GNNAME:
15267c478bd9Sstevel@tonic-gate case PPPTUN_LCLADDR:
15277c478bd9Sstevel@tonic-gate case PPPTUN_SPEER:
15287c478bd9Sstevel@tonic-gate case PPPTUN_GPEER:
15297c478bd9Sstevel@tonic-gate case PPPTUN_SDATA:
15307c478bd9Sstevel@tonic-gate case PPPTUN_GDATA:
15317c478bd9Sstevel@tonic-gate case PPPTUN_SCTL:
15327c478bd9Sstevel@tonic-gate case PPPTUN_GCTL:
15337c478bd9Sstevel@tonic-gate case PPPTUN_DCTL:
1534f53eecf5SJames Carlson case PPPTUN_SSAP:
15357c478bd9Sstevel@tonic-gate qwriter(q, mp, sppptun_inner_ioctl, PERIM_INNER);
15367c478bd9Sstevel@tonic-gate return;
15377c478bd9Sstevel@tonic-gate
15387c478bd9Sstevel@tonic-gate case PPPIO_GCLEAN: /* always clean */
15397c478bd9Sstevel@tonic-gate val = RCV_B7_1 | RCV_B7_0 | RCV_ODDP | RCV_EVNP;
15407c478bd9Sstevel@tonic-gate len = sizeof (uint32_t);
15417c478bd9Sstevel@tonic-gate break;
15427c478bd9Sstevel@tonic-gate
15437c478bd9Sstevel@tonic-gate case PPPIO_GTYPE: /* we look like an async driver. */
15447c478bd9Sstevel@tonic-gate val = PPPTYP_AHDLC;
15457c478bd9Sstevel@tonic-gate len = sizeof (uint32_t);
15467c478bd9Sstevel@tonic-gate break;
15477c478bd9Sstevel@tonic-gate
15487c478bd9Sstevel@tonic-gate case PPPIO_CFLAGS: /* never compress headers */
15497c478bd9Sstevel@tonic-gate val = 0;
15507c478bd9Sstevel@tonic-gate len = sizeof (uint32_t);
15517c478bd9Sstevel@tonic-gate break;
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate /* quietly ack PPP things we don't need to do. */
15547c478bd9Sstevel@tonic-gate case PPPIO_XFCS:
15557c478bd9Sstevel@tonic-gate case PPPIO_RFCS:
15567c478bd9Sstevel@tonic-gate case PPPIO_XACCM:
15577c478bd9Sstevel@tonic-gate case PPPIO_RACCM:
15587c478bd9Sstevel@tonic-gate case PPPIO_LASTMOD:
15597c478bd9Sstevel@tonic-gate case PPPIO_MUX:
15607c478bd9Sstevel@tonic-gate case I_PLINK:
15617c478bd9Sstevel@tonic-gate case I_PUNLINK:
15627c478bd9Sstevel@tonic-gate case I_LINK:
15637c478bd9Sstevel@tonic-gate case I_UNLINK:
15647c478bd9Sstevel@tonic-gate break;
15657c478bd9Sstevel@tonic-gate
15667c478bd9Sstevel@tonic-gate default:
15677c478bd9Sstevel@tonic-gate tll = (tunll_t *)q->q_ptr;
15687c478bd9Sstevel@tonic-gate if (!(tll->tll_flags & TLLF_NOTLOWER)) {
15697c478bd9Sstevel@tonic-gate /* module side; pass this through. */
15707c478bd9Sstevel@tonic-gate putnext(q, mp);
15717c478bd9Sstevel@tonic-gate return;
15727c478bd9Sstevel@tonic-gate }
15737c478bd9Sstevel@tonic-gate rc = EINVAL;
15747c478bd9Sstevel@tonic-gate break;
15757c478bd9Sstevel@tonic-gate }
15767c478bd9Sstevel@tonic-gate if (rc == 0 && len == sizeof (uint32_t)) {
15777c478bd9Sstevel@tonic-gate if (mp->b_cont != NULL)
15787c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
15797c478bd9Sstevel@tonic-gate mp->b_cont = allocb(sizeof (uint32_t), BPRI_HI);
15807c478bd9Sstevel@tonic-gate if (mp->b_cont == NULL) {
15817c478bd9Sstevel@tonic-gate rc = ENOSR;
15827c478bd9Sstevel@tonic-gate } else {
15837c478bd9Sstevel@tonic-gate *(uint32_t *)mp->b_cont->b_wptr = val;
15847c478bd9Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (uint32_t);
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate if (rc == 0) {
15887c478bd9Sstevel@tonic-gate miocack(q, mp, len, 0);
15897c478bd9Sstevel@tonic-gate } else {
15907c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, rc);
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate
15947c478bd9Sstevel@tonic-gate /*
15957c478bd9Sstevel@tonic-gate * sppptun_inner_mctl()
15967c478bd9Sstevel@tonic-gate *
15977c478bd9Sstevel@tonic-gate * MT-Perimeters:
15987c478bd9Sstevel@tonic-gate * exclusive inner, shared outer.
15997c478bd9Sstevel@tonic-gate *
16007c478bd9Sstevel@tonic-gate * Description:
16017c478bd9Sstevel@tonic-gate * Called by qwriter (via sppptun_uwput) as the result of receiving
16027c478bd9Sstevel@tonic-gate * an M_CTL. Called only on the client (driver) side.
16037c478bd9Sstevel@tonic-gate */
16047c478bd9Sstevel@tonic-gate static void
sppptun_inner_mctl(queue_t * q,mblk_t * mp)16057c478bd9Sstevel@tonic-gate sppptun_inner_mctl(queue_t *q, mblk_t *mp)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate int msglen;
16087c478bd9Sstevel@tonic-gate tuncl_t *tcl;
16097c478bd9Sstevel@tonic-gate
1610002c70ffScarlsonj tcl = q->q_ptr;
16117c478bd9Sstevel@tonic-gate
16127c478bd9Sstevel@tonic-gate if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
16137c478bd9Sstevel@tonic-gate freemsg(mp);
16147c478bd9Sstevel@tonic-gate return;
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate
16177c478bd9Sstevel@tonic-gate msglen = MBLKL(mp);
16187c478bd9Sstevel@tonic-gate switch (*mp->b_rptr) {
16197c478bd9Sstevel@tonic-gate case PPPCTL_UNIT:
16207c478bd9Sstevel@tonic-gate if (msglen == 2)
16217c478bd9Sstevel@tonic-gate tcl->tcl_unit = mp->b_rptr[1];
16227c478bd9Sstevel@tonic-gate else if (msglen == 8)
16237c478bd9Sstevel@tonic-gate tcl->tcl_unit = ((uint32_t *)mp->b_rptr)[1];
16247c478bd9Sstevel@tonic-gate break;
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate freemsg(mp);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate
16297c478bd9Sstevel@tonic-gate /*
16307c478bd9Sstevel@tonic-gate * sppptun_uwput()
16317c478bd9Sstevel@tonic-gate *
16327c478bd9Sstevel@tonic-gate * MT-Perimeters:
16337c478bd9Sstevel@tonic-gate * shared inner, shared outer.
16347c478bd9Sstevel@tonic-gate *
16357c478bd9Sstevel@tonic-gate * Description:
16367c478bd9Sstevel@tonic-gate * Regular output data and controls pass through here.
16377c478bd9Sstevel@tonic-gate */
1638868fcfe9SToomas Soome static int
sppptun_uwput(queue_t * q,mblk_t * mp)16397c478bd9Sstevel@tonic-gate sppptun_uwput(queue_t *q, mblk_t *mp)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate queue_t *nextq;
16427c478bd9Sstevel@tonic-gate tuncl_t *tcl;
16437c478bd9Sstevel@tonic-gate
16447c478bd9Sstevel@tonic-gate ASSERT(q->q_ptr != NULL);
16457c478bd9Sstevel@tonic-gate
16467c478bd9Sstevel@tonic-gate switch (MTYPE(mp)) {
16477c478bd9Sstevel@tonic-gate case M_DATA:
16487c478bd9Sstevel@tonic-gate case M_PROTO:
16497c478bd9Sstevel@tonic-gate case M_PCPROTO:
16507c478bd9Sstevel@tonic-gate if (q->q_first == NULL &&
16517c478bd9Sstevel@tonic-gate (nextq = sppptun_outpkt(q, &mp)) != NULL) {
16527c478bd9Sstevel@tonic-gate putnext(nextq, mp);
16537c478bd9Sstevel@tonic-gate } else if (mp != NULL && !putq(q, mp)) {
16547c478bd9Sstevel@tonic-gate freemsg(mp);
16557c478bd9Sstevel@tonic-gate }
16567c478bd9Sstevel@tonic-gate break;
16577c478bd9Sstevel@tonic-gate case M_IOCTL:
16587c478bd9Sstevel@tonic-gate sppptun_ioctl(q, mp);
16597c478bd9Sstevel@tonic-gate break;
16607c478bd9Sstevel@tonic-gate case M_CTL:
16617c478bd9Sstevel@tonic-gate qwriter(q, mp, sppptun_inner_mctl, PERIM_INNER);
16627c478bd9Sstevel@tonic-gate break;
16637c478bd9Sstevel@tonic-gate default:
16647c478bd9Sstevel@tonic-gate tcl = (tuncl_t *)q->q_ptr;
16657c478bd9Sstevel@tonic-gate /*
16667c478bd9Sstevel@tonic-gate * If we're the driver, then discard unknown junk.
16677c478bd9Sstevel@tonic-gate * Otherwise, if we're the module, then forward along.
16687c478bd9Sstevel@tonic-gate */
16697c478bd9Sstevel@tonic-gate if (tcl->tcl_flags & TCLF_ISCLIENT)
16707c478bd9Sstevel@tonic-gate freemsg(mp);
16717c478bd9Sstevel@tonic-gate else
16727c478bd9Sstevel@tonic-gate putnext(q, mp);
16737c478bd9Sstevel@tonic-gate break;
16747c478bd9Sstevel@tonic-gate }
1675868fcfe9SToomas Soome return (0);
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate
16787c478bd9Sstevel@tonic-gate /*
16797c478bd9Sstevel@tonic-gate * Send a DLPI/TPI control message to the driver but make sure there
16807c478bd9Sstevel@tonic-gate * is only one outstanding message. Uses tll_msg_pending to tell when
16817c478bd9Sstevel@tonic-gate * it must queue. sppptun_urput calls message_done() when an ACK or a
16827c478bd9Sstevel@tonic-gate * NAK is received to process the next queued message.
16837c478bd9Sstevel@tonic-gate */
16847c478bd9Sstevel@tonic-gate static void
message_send(tunll_t * tll,mblk_t * mp)16857c478bd9Sstevel@tonic-gate message_send(tunll_t *tll, mblk_t *mp)
16867c478bd9Sstevel@tonic-gate {
16877c478bd9Sstevel@tonic-gate mblk_t **mpp;
16887c478bd9Sstevel@tonic-gate
16897c478bd9Sstevel@tonic-gate if (tll->tll_msg_pending) {
16907c478bd9Sstevel@tonic-gate /* Must queue message. Tail insertion */
16917c478bd9Sstevel@tonic-gate mpp = &tll->tll_msg_deferred;
16927c478bd9Sstevel@tonic-gate while (*mpp != NULL)
16937c478bd9Sstevel@tonic-gate mpp = &((*mpp)->b_next);
16947c478bd9Sstevel@tonic-gate *mpp = mp;
16957c478bd9Sstevel@tonic-gate return;
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate tll->tll_msg_pending = 1;
16987c478bd9Sstevel@tonic-gate putnext(tll->tll_wq, mp);
16997c478bd9Sstevel@tonic-gate }
17007c478bd9Sstevel@tonic-gate
17017c478bd9Sstevel@tonic-gate /*
17027c478bd9Sstevel@tonic-gate * Called when an DLPI/TPI control message has been acked or nacked to
17037c478bd9Sstevel@tonic-gate * send down the next queued message (if any).
17047c478bd9Sstevel@tonic-gate */
17057c478bd9Sstevel@tonic-gate static void
message_done(tunll_t * tll)17067c478bd9Sstevel@tonic-gate message_done(tunll_t *tll)
17077c478bd9Sstevel@tonic-gate {
17087c478bd9Sstevel@tonic-gate mblk_t *mp;
17097c478bd9Sstevel@tonic-gate
17107c478bd9Sstevel@tonic-gate ASSERT(tll->tll_msg_pending);
17117c478bd9Sstevel@tonic-gate tll->tll_msg_pending = 0;
17127c478bd9Sstevel@tonic-gate mp = tll->tll_msg_deferred;
17137c478bd9Sstevel@tonic-gate if (mp != NULL) {
17147c478bd9Sstevel@tonic-gate tll->tll_msg_deferred = mp->b_next;
17157c478bd9Sstevel@tonic-gate mp->b_next = NULL;
17167c478bd9Sstevel@tonic-gate tll->tll_msg_pending = 1;
17177c478bd9Sstevel@tonic-gate putnext(tll->tll_wq, mp);
17187c478bd9Sstevel@tonic-gate }
17197c478bd9Sstevel@tonic-gate }
17207c478bd9Sstevel@tonic-gate
17217c478bd9Sstevel@tonic-gate /*
17227c478bd9Sstevel@tonic-gate * Send down queued "close" messages to lower stream. These were
17237c478bd9Sstevel@tonic-gate * enqueued right after the stream was originally allocated, when the
17247c478bd9Sstevel@tonic-gate * tll_style was set by PPPTUN_SINFO.
17257c478bd9Sstevel@tonic-gate */
17267c478bd9Sstevel@tonic-gate static int
tll_close_req(tunll_t * tll)17277c478bd9Sstevel@tonic-gate tll_close_req(tunll_t *tll)
17287c478bd9Sstevel@tonic-gate {
17297c478bd9Sstevel@tonic-gate mblk_t *mb, *mbnext;
17307c478bd9Sstevel@tonic-gate
17317c478bd9Sstevel@tonic-gate if ((mb = tll->tll_onclose) == NULL)
17327c478bd9Sstevel@tonic-gate tll->tll_flags |= TLLF_SHUTDOWN_DONE;
17337c478bd9Sstevel@tonic-gate else {
17347c478bd9Sstevel@tonic-gate tll->tll_onclose = NULL;
17357c478bd9Sstevel@tonic-gate while (mb != NULL) {
17367c478bd9Sstevel@tonic-gate mbnext = mb->b_next;
17377c478bd9Sstevel@tonic-gate mb->b_next = NULL;
17387c478bd9Sstevel@tonic-gate message_send(tll, mb);
17397c478bd9Sstevel@tonic-gate mb = mbnext;
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate return (0);
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate
1745002c70ffScarlsonj /*
1746002c70ffScarlsonj * This function is called when a backenable occurs on the write side of a
1747002c70ffScarlsonj * lower stream. It walks over the client streams, looking for ones that use
1748002c70ffScarlsonj * the given tunll_t lower stream. Each client is then backenabled.
1749002c70ffScarlsonj */
1750002c70ffScarlsonj static void
tclvm_backenable(void * arg,void * firstv,size_t numv)1751002c70ffScarlsonj tclvm_backenable(void *arg, void *firstv, size_t numv)
1752002c70ffScarlsonj {
1753002c70ffScarlsonj tunll_t *tll = arg;
1754002c70ffScarlsonj int minorn = (int)(uintptr_t)firstv;
1755002c70ffScarlsonj int minormax = minorn + numv;
1756002c70ffScarlsonj tuncl_t *tcl;
1757002c70ffScarlsonj queue_t *q;
1758002c70ffScarlsonj
1759002c70ffScarlsonj while (minorn < minormax) {
1760002c70ffScarlsonj tcl = tcl_slots[minorn - 1];
1761002c70ffScarlsonj if ((tcl->tcl_data_tll == tll ||
1762002c70ffScarlsonj tcl->tcl_ctrl_tll == tll) &&
1763002c70ffScarlsonj (q = tcl->tcl_rq) != NULL) {
1764002c70ffScarlsonj qenable(OTHERQ(q));
1765002c70ffScarlsonj }
1766002c70ffScarlsonj minorn++;
1767002c70ffScarlsonj }
1768002c70ffScarlsonj }
1769002c70ffScarlsonj
17707c478bd9Sstevel@tonic-gate /*
17717c478bd9Sstevel@tonic-gate * sppptun_uwsrv()
17727c478bd9Sstevel@tonic-gate *
17737c478bd9Sstevel@tonic-gate * MT-Perimeters:
17747c478bd9Sstevel@tonic-gate * exclusive inner, shared outer.
17757c478bd9Sstevel@tonic-gate *
17767c478bd9Sstevel@tonic-gate * Description:
17777c478bd9Sstevel@tonic-gate * Upper write-side service procedure. In addition to the usual
17787c478bd9Sstevel@tonic-gate * STREAMS queue service handling, this routine also handles the
17797c478bd9Sstevel@tonic-gate * transmission of the unbind/detach messages to the lower stream
17807c478bd9Sstevel@tonic-gate * driver when a lower stream is being closed. (See the use of
17817c478bd9Sstevel@tonic-gate * qenable/qwait in sppptun_close().)
17827c478bd9Sstevel@tonic-gate */
17837c478bd9Sstevel@tonic-gate static int
sppptun_uwsrv(queue_t * q)17847c478bd9Sstevel@tonic-gate sppptun_uwsrv(queue_t *q)
17857c478bd9Sstevel@tonic-gate {
17867c478bd9Sstevel@tonic-gate tuncl_t *tcl;
17877c478bd9Sstevel@tonic-gate mblk_t *mp;
17887c478bd9Sstevel@tonic-gate queue_t *nextq;
17897c478bd9Sstevel@tonic-gate
1790002c70ffScarlsonj tcl = q->q_ptr;
17917c478bd9Sstevel@tonic-gate if (!(tcl->tcl_flags & TCLF_ISCLIENT)) {
17927c478bd9Sstevel@tonic-gate tunll_t *tll = (tunll_t *)tcl;
1793002c70ffScarlsonj
17947c478bd9Sstevel@tonic-gate if ((tll->tll_flags & (TLLF_CLOSING|TLLF_CLOSE_DONE)) ==
17957c478bd9Sstevel@tonic-gate TLLF_CLOSING) {
17967c478bd9Sstevel@tonic-gate tll->tll_error = tll_close_req(tll);
17977c478bd9Sstevel@tonic-gate tll->tll_flags |= TLLF_CLOSE_DONE;
1798002c70ffScarlsonj } else {
1799002c70ffScarlsonj /*
1800002c70ffScarlsonj * We've been enabled here because of a backenable on
1801002c70ffScarlsonj * output flow control. Backenable clients using this
1802002c70ffScarlsonj * lower layer.
1803002c70ffScarlsonj */
1804002c70ffScarlsonj vmem_walk(tcl_minor_arena, VMEM_ALLOC, tclvm_backenable,
1805002c70ffScarlsonj tll);
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate return (0);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate
18107c478bd9Sstevel@tonic-gate while ((mp = getq(q)) != NULL) {
18117c478bd9Sstevel@tonic-gate if ((nextq = sppptun_outpkt(q, &mp)) != NULL) {
18127c478bd9Sstevel@tonic-gate putnext(nextq, mp);
18137c478bd9Sstevel@tonic-gate } else if (mp != NULL) {
18147c478bd9Sstevel@tonic-gate (void) putbq(q, mp);
18157c478bd9Sstevel@tonic-gate break;
18167c478bd9Sstevel@tonic-gate }
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate return (0);
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate
18217c478bd9Sstevel@tonic-gate /*
18227c478bd9Sstevel@tonic-gate * sppptun_lwput()
18237c478bd9Sstevel@tonic-gate *
18247c478bd9Sstevel@tonic-gate * MT-Perimeters:
18257c478bd9Sstevel@tonic-gate * shared inner, shared outer.
18267c478bd9Sstevel@tonic-gate *
18277c478bd9Sstevel@tonic-gate * Description:
18287c478bd9Sstevel@tonic-gate * Lower write-side put procedure. Nothing should be sending
18297c478bd9Sstevel@tonic-gate * packets down this stream.
18307c478bd9Sstevel@tonic-gate */
1831868fcfe9SToomas Soome static int
sppptun_lwput(queue_t * q,mblk_t * mp)18327c478bd9Sstevel@tonic-gate sppptun_lwput(queue_t *q, mblk_t *mp)
18337c478bd9Sstevel@tonic-gate {
18347c478bd9Sstevel@tonic-gate switch (MTYPE(mp)) {
18357c478bd9Sstevel@tonic-gate case M_PROTO:
18367c478bd9Sstevel@tonic-gate putnext(q, mp);
18377c478bd9Sstevel@tonic-gate break;
18387c478bd9Sstevel@tonic-gate default:
18397c478bd9Sstevel@tonic-gate freemsg(mp);
18407c478bd9Sstevel@tonic-gate break;
18417c478bd9Sstevel@tonic-gate }
1842868fcfe9SToomas Soome return (0);
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate
18457c478bd9Sstevel@tonic-gate /*
18467c478bd9Sstevel@tonic-gate * sppptun_lrput()
18477c478bd9Sstevel@tonic-gate *
18487c478bd9Sstevel@tonic-gate * MT-Perimeters:
18497c478bd9Sstevel@tonic-gate * shared inner, shared outer.
18507c478bd9Sstevel@tonic-gate *
18517c478bd9Sstevel@tonic-gate * Description:
18527c478bd9Sstevel@tonic-gate * Lower read-side put procedure. Nothing should arrive here.
18537c478bd9Sstevel@tonic-gate */
1854868fcfe9SToomas Soome static int
sppptun_lrput(queue_t * q,mblk_t * mp)18557c478bd9Sstevel@tonic-gate sppptun_lrput(queue_t *q, mblk_t *mp)
18567c478bd9Sstevel@tonic-gate {
18577c478bd9Sstevel@tonic-gate tuncl_t *tcl;
18587c478bd9Sstevel@tonic-gate
18597c478bd9Sstevel@tonic-gate switch (MTYPE(mp)) {
18607c478bd9Sstevel@tonic-gate case M_IOCTL:
18617c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, EINVAL);
1862868fcfe9SToomas Soome return (0);
18637c478bd9Sstevel@tonic-gate case M_FLUSH:
18647c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) {
18657c478bd9Sstevel@tonic-gate flushq(q, FLUSHDATA);
18667c478bd9Sstevel@tonic-gate }
18677c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) {
18687c478bd9Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHR;
18697c478bd9Sstevel@tonic-gate qreply(q, mp);
18707c478bd9Sstevel@tonic-gate } else {
18717c478bd9Sstevel@tonic-gate freemsg(mp);
18727c478bd9Sstevel@tonic-gate }
1873868fcfe9SToomas Soome return (0);
18747c478bd9Sstevel@tonic-gate }
18757c478bd9Sstevel@tonic-gate /*
18767c478bd9Sstevel@tonic-gate * Try to forward the message to the put procedure for the upper
18777c478bd9Sstevel@tonic-gate * control stream for this lower stream. If there are already messages
18787c478bd9Sstevel@tonic-gate * queued here, queue this one up to preserve message ordering.
18797c478bd9Sstevel@tonic-gate */
18807c478bd9Sstevel@tonic-gate if ((tcl = (tuncl_t *)q->q_ptr) == NULL || tcl->tcl_rq == NULL) {
18817c478bd9Sstevel@tonic-gate freemsg(mp);
1882868fcfe9SToomas Soome return (0);
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate if (queclass(mp) == QPCTL ||
18857c478bd9Sstevel@tonic-gate (q->q_first == NULL && canput(tcl->tcl_rq))) {
18867c478bd9Sstevel@tonic-gate put(tcl->tcl_rq, mp);
18877c478bd9Sstevel@tonic-gate } else {
18887c478bd9Sstevel@tonic-gate if (!putq(q, mp))
18897c478bd9Sstevel@tonic-gate freemsg(mp);
18907c478bd9Sstevel@tonic-gate }
1891868fcfe9SToomas Soome return (0);
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate
18947c478bd9Sstevel@tonic-gate /*
18957c478bd9Sstevel@tonic-gate * MT-Perimeters:
18967c478bd9Sstevel@tonic-gate * shared inner, shared outer.
18977c478bd9Sstevel@tonic-gate *
18987c478bd9Sstevel@tonic-gate * Handle non-data DLPI messages. Used with PPPoE, which runs over
18997c478bd9Sstevel@tonic-gate * Ethernet only.
19007c478bd9Sstevel@tonic-gate */
19017c478bd9Sstevel@tonic-gate static void
urput_dlpi(queue_t * q,mblk_t * mp)19027c478bd9Sstevel@tonic-gate urput_dlpi(queue_t *q, mblk_t *mp)
19037c478bd9Sstevel@tonic-gate {
19047c478bd9Sstevel@tonic-gate int err;
19057c478bd9Sstevel@tonic-gate union DL_primitives *dlp = (union DL_primitives *)mp->b_rptr;
1906002c70ffScarlsonj tunll_t *tll = q->q_ptr;
1907002c70ffScarlsonj size_t mlen = MBLKL(mp);
19087c478bd9Sstevel@tonic-gate
19097c478bd9Sstevel@tonic-gate switch (dlp->dl_primitive) {
19107c478bd9Sstevel@tonic-gate case DL_UDERROR_IND:
19117c478bd9Sstevel@tonic-gate break;
19127c478bd9Sstevel@tonic-gate
19137c478bd9Sstevel@tonic-gate case DL_ERROR_ACK:
1914002c70ffScarlsonj if (mlen < DL_ERROR_ACK_SIZE)
1915002c70ffScarlsonj break;
19167c478bd9Sstevel@tonic-gate err = dlp->error_ack.dl_unix_errno ?
19177c478bd9Sstevel@tonic-gate dlp->error_ack.dl_unix_errno : ENXIO;
19187c478bd9Sstevel@tonic-gate switch (dlp->error_ack.dl_error_primitive) {
19197c478bd9Sstevel@tonic-gate case DL_UNBIND_REQ:
19207c478bd9Sstevel@tonic-gate message_done(tll);
19217c478bd9Sstevel@tonic-gate break;
19227c478bd9Sstevel@tonic-gate case DL_DETACH_REQ:
19237c478bd9Sstevel@tonic-gate message_done(tll);
19247c478bd9Sstevel@tonic-gate tll->tll_error = err;
19257c478bd9Sstevel@tonic-gate tll->tll_flags |= TLLF_SHUTDOWN_DONE;
19267c478bd9Sstevel@tonic-gate break;
19277c478bd9Sstevel@tonic-gate case DL_PHYS_ADDR_REQ:
19287c478bd9Sstevel@tonic-gate message_done(tll);
19297c478bd9Sstevel@tonic-gate break;
19307c478bd9Sstevel@tonic-gate case DL_INFO_REQ:
19317c478bd9Sstevel@tonic-gate case DL_ATTACH_REQ:
19327c478bd9Sstevel@tonic-gate case DL_BIND_REQ:
19337c478bd9Sstevel@tonic-gate message_done(tll);
19347c478bd9Sstevel@tonic-gate tll->tll_error = err;
19357c478bd9Sstevel@tonic-gate break;
19367c478bd9Sstevel@tonic-gate }
19377c478bd9Sstevel@tonic-gate break;
19387c478bd9Sstevel@tonic-gate
19397c478bd9Sstevel@tonic-gate case DL_INFO_ACK:
19407c478bd9Sstevel@tonic-gate message_done(tll);
19417c478bd9Sstevel@tonic-gate break;
19427c478bd9Sstevel@tonic-gate
19437c478bd9Sstevel@tonic-gate case DL_BIND_ACK:
19447c478bd9Sstevel@tonic-gate message_done(tll);
19457c478bd9Sstevel@tonic-gate break;
19467c478bd9Sstevel@tonic-gate
19477c478bd9Sstevel@tonic-gate case DL_PHYS_ADDR_ACK:
19487c478bd9Sstevel@tonic-gate break;
19497c478bd9Sstevel@tonic-gate
19507c478bd9Sstevel@tonic-gate case DL_OK_ACK:
1951002c70ffScarlsonj if (mlen < DL_OK_ACK_SIZE)
1952002c70ffScarlsonj break;
19537c478bd9Sstevel@tonic-gate switch (dlp->ok_ack.dl_correct_primitive) {
19547c478bd9Sstevel@tonic-gate case DL_UNBIND_REQ:
19557c478bd9Sstevel@tonic-gate message_done(tll);
19567c478bd9Sstevel@tonic-gate break;
19577c478bd9Sstevel@tonic-gate case DL_DETACH_REQ:
19587c478bd9Sstevel@tonic-gate tll->tll_flags |= TLLF_SHUTDOWN_DONE;
19597c478bd9Sstevel@tonic-gate break;
19607c478bd9Sstevel@tonic-gate case DL_ATTACH_REQ:
19617c478bd9Sstevel@tonic-gate message_done(tll);
19627c478bd9Sstevel@tonic-gate break;
19637c478bd9Sstevel@tonic-gate }
19647c478bd9Sstevel@tonic-gate break;
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate freemsg(mp);
19677c478bd9Sstevel@tonic-gate }
19687c478bd9Sstevel@tonic-gate
19697c478bd9Sstevel@tonic-gate /* Search structure used with PPPoE only; see tclvm_pppoe_search(). */
19707c478bd9Sstevel@tonic-gate struct poedat {
19717c478bd9Sstevel@tonic-gate uint_t sessid;
19727c478bd9Sstevel@tonic-gate tunll_t *tll;
1973002c70ffScarlsonj const void *srcaddr;
19747c478bd9Sstevel@tonic-gate int isdata;
19757c478bd9Sstevel@tonic-gate tuncl_t *tcl;
19767c478bd9Sstevel@tonic-gate };
19777c478bd9Sstevel@tonic-gate
19787c478bd9Sstevel@tonic-gate /*
19797c478bd9Sstevel@tonic-gate * This function is called by vmem_walk from within sppptun_recv. It
19807c478bd9Sstevel@tonic-gate * iterates over a span of allocated minor node numbers to search for
19817c478bd9Sstevel@tonic-gate * the appropriate lower stream, session ID, and peer MAC address.
19827c478bd9Sstevel@tonic-gate *
19837c478bd9Sstevel@tonic-gate * (This is necessary due to a design flaw in the PPPoE protocol
19847c478bd9Sstevel@tonic-gate * itself. The protocol assigns session IDs from the server side
19857c478bd9Sstevel@tonic-gate * only. Both server and client use the same number. Thus, if there
19867c478bd9Sstevel@tonic-gate * are multiple clients on a single host, there can be session ID
19877c478bd9Sstevel@tonic-gate * conflicts between servers and there's no way to detangle them
19887c478bd9Sstevel@tonic-gate * except by looking at the remote MAC address.)
19897c478bd9Sstevel@tonic-gate *
19907c478bd9Sstevel@tonic-gate * (This could have been handled by linking together sessions that
19917c478bd9Sstevel@tonic-gate * differ only in the remote MAC address. This isn't done because it
19927c478bd9Sstevel@tonic-gate * would involve extra per-session storage and it's very unlikely that
19937c478bd9Sstevel@tonic-gate * PPPoE would be used this way.)
19947c478bd9Sstevel@tonic-gate */
19957c478bd9Sstevel@tonic-gate static void
tclvm_pppoe_search(void * arg,void * firstv,size_t numv)19967c478bd9Sstevel@tonic-gate tclvm_pppoe_search(void *arg, void *firstv, size_t numv)
19977c478bd9Sstevel@tonic-gate {
19987c478bd9Sstevel@tonic-gate struct poedat *poedat = (struct poedat *)arg;
19997c478bd9Sstevel@tonic-gate int minorn = (int)(uintptr_t)firstv;
20007c478bd9Sstevel@tonic-gate int minormax = minorn + numv;
20017c478bd9Sstevel@tonic-gate tuncl_t *tcl;
20027c478bd9Sstevel@tonic-gate
20037c478bd9Sstevel@tonic-gate if (poedat->tcl != NULL)
20047c478bd9Sstevel@tonic-gate return;
20057c478bd9Sstevel@tonic-gate while (minorn < minormax) {
20067c478bd9Sstevel@tonic-gate tcl = tcl_slots[minorn - 1];
20077c478bd9Sstevel@tonic-gate ASSERT(tcl != NULL);
20087c478bd9Sstevel@tonic-gate if (tcl->tcl_rsessid == poedat->sessid &&
20097c478bd9Sstevel@tonic-gate ((!poedat->isdata && tcl->tcl_ctrl_tll == poedat->tll) ||
2010002c70ffScarlsonj (poedat->isdata && tcl->tcl_data_tll == poedat->tll)) &&
20117c478bd9Sstevel@tonic-gate bcmp(tcl->tcl_address.pta_pppoe.ptma_mac,
2012002c70ffScarlsonj poedat->srcaddr,
2013002c70ffScarlsonj sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) == 0) {
20147c478bd9Sstevel@tonic-gate poedat->tcl = tcl;
20157c478bd9Sstevel@tonic-gate break;
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate minorn++;
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate
20217c478bd9Sstevel@tonic-gate /*
20227c478bd9Sstevel@tonic-gate * sppptun_recv()
20237c478bd9Sstevel@tonic-gate *
20247c478bd9Sstevel@tonic-gate * MT-Perimeters:
20257c478bd9Sstevel@tonic-gate * shared inner, shared outer.
20267c478bd9Sstevel@tonic-gate *
20277c478bd9Sstevel@tonic-gate * Description:
20287c478bd9Sstevel@tonic-gate * Receive function called by sppptun_urput, which is called when
20297c478bd9Sstevel@tonic-gate * the lower read-side put or service procedure sends a message
20307c478bd9Sstevel@tonic-gate * upstream to the a device user (PPP). It attempts to find an
20317c478bd9Sstevel@tonic-gate * appropriate queue on the module above us (depending on what the
20327c478bd9Sstevel@tonic-gate * associated upper stream for the protocol would be), and if not
20337c478bd9Sstevel@tonic-gate * possible, it will find an upper control stream for the protocol.
20347c478bd9Sstevel@tonic-gate * Returns a pointer to the upper queue_t, or NULL if the message
20357c478bd9Sstevel@tonic-gate * has been discarded.
20367c478bd9Sstevel@tonic-gate *
20377c478bd9Sstevel@tonic-gate * About demultiplexing:
20387c478bd9Sstevel@tonic-gate *
20397c478bd9Sstevel@tonic-gate * All four protocols (L2F, PPTP, L2TP, and PPPoE) support a
20407c478bd9Sstevel@tonic-gate * locally assigned ID for demultiplexing incoming traffic. For
20417c478bd9Sstevel@tonic-gate * L2F, this is called the Client ID, for PPTP the Call ID, for
20427c478bd9Sstevel@tonic-gate * L2TP the Session ID, and for PPPoE the SESSION_ID. This is a
20437c478bd9Sstevel@tonic-gate * 16 bit number for all four protocols, and is used to directly
20447c478bd9Sstevel@tonic-gate * index into a list of upper streams. With the upper stream in
20457c478bd9Sstevel@tonic-gate * hand, we verify that this is the right stream and deliver the
20467c478bd9Sstevel@tonic-gate * data.
20477c478bd9Sstevel@tonic-gate *
20487c478bd9Sstevel@tonic-gate * L2TP has a Tunnel ID, which represents a bundle of PPP
20497c478bd9Sstevel@tonic-gate * sessions between the peers. Because we always assign unique
20507c478bd9Sstevel@tonic-gate * session ID numbers, we merely check that the given ID matches
20517c478bd9Sstevel@tonic-gate * the assigned ID for the upper stream.
20527c478bd9Sstevel@tonic-gate *
20537c478bd9Sstevel@tonic-gate * L2F has a Multiplex ID, which is unique per connection. It
20547c478bd9Sstevel@tonic-gate * does not have L2TP's concept of multiple-connections-within-
20557c478bd9Sstevel@tonic-gate * a-tunnel. The same checking is done.
20567c478bd9Sstevel@tonic-gate *
20577c478bd9Sstevel@tonic-gate * PPPoE is a horribly broken protocol. Only one ID is assigned
20587c478bd9Sstevel@tonic-gate * per connection. The client must somehow demultiplex based on
20597c478bd9Sstevel@tonic-gate * an ID number assigned by the server. It's not necessarily
20607c478bd9Sstevel@tonic-gate * unique. The search is done based on {ID,peerEthernet} (using
20617c478bd9Sstevel@tonic-gate * tcl_rsessid) for all packet types except PADI and PADS.
20627c478bd9Sstevel@tonic-gate *
20637c478bd9Sstevel@tonic-gate * Neither PPPoE nor PPTP supports additional ID numbers.
20647c478bd9Sstevel@tonic-gate *
20657c478bd9Sstevel@tonic-gate * Both L2F and L2TP come in over UDP. They are distinguished by
20667c478bd9Sstevel@tonic-gate * looking at the GRE version field -- 001 for L2F and 010 for
20677c478bd9Sstevel@tonic-gate * L2TP.
20687c478bd9Sstevel@tonic-gate */
20697c478bd9Sstevel@tonic-gate static queue_t *
sppptun_recv(queue_t * q,mblk_t ** mpp,const void * srcaddr)2070002c70ffScarlsonj sppptun_recv(queue_t *q, mblk_t **mpp, const void *srcaddr)
20717c478bd9Sstevel@tonic-gate {
20727c478bd9Sstevel@tonic-gate mblk_t *mp;
20737c478bd9Sstevel@tonic-gate tunll_t *tll;
20747c478bd9Sstevel@tonic-gate tuncl_t *tcl;
20757c478bd9Sstevel@tonic-gate int sessid;
20767c478bd9Sstevel@tonic-gate int remlen;
20777c478bd9Sstevel@tonic-gate int msglen;
20787c478bd9Sstevel@tonic-gate int isdata;
20797c478bd9Sstevel@tonic-gate int i;
2080002c70ffScarlsonj const uchar_t *ucp;
2081002c70ffScarlsonj const poep_t *poep;
20827c478bd9Sstevel@tonic-gate mblk_t *mnew;
20837c478bd9Sstevel@tonic-gate ppptun_atype *pap;
20847c478bd9Sstevel@tonic-gate
20857c478bd9Sstevel@tonic-gate mp = *mpp;
20867c478bd9Sstevel@tonic-gate
2087002c70ffScarlsonj tll = q->q_ptr;
20887c478bd9Sstevel@tonic-gate ASSERT(!(tll->tll_flags & TLLF_NOTLOWER));
20897c478bd9Sstevel@tonic-gate
20907c478bd9Sstevel@tonic-gate tcl = NULL;
20917c478bd9Sstevel@tonic-gate switch (tll->tll_style) {
20927c478bd9Sstevel@tonic-gate case PTS_PPPOE:
2093002c70ffScarlsonj /* Note that poep_t alignment is uint16_t */
2094002c70ffScarlsonj if ((!IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)) ||
2095002c70ffScarlsonj MBLKL(mp) < sizeof (poep_t)) &&
2096002c70ffScarlsonj !pullupmsg(mp, sizeof (poep_t)))
2097002c70ffScarlsonj break;
2098002c70ffScarlsonj poep = (const poep_t *)mp->b_rptr;
20997c478bd9Sstevel@tonic-gate if (poep->poep_version_type != POE_VERSION)
21007c478bd9Sstevel@tonic-gate break;
2101002c70ffScarlsonj /*
2102002c70ffScarlsonj * First, extract a session ID number. All protocols have
2103002c70ffScarlsonj * this.
2104002c70ffScarlsonj */
21057c478bd9Sstevel@tonic-gate isdata = (poep->poep_code == POECODE_DATA);
21067c478bd9Sstevel@tonic-gate sessid = ntohs(poep->poep_session_id);
21077c478bd9Sstevel@tonic-gate remlen = sizeof (*poep);
21087c478bd9Sstevel@tonic-gate msglen = ntohs(poep->poep_length);
21097c478bd9Sstevel@tonic-gate i = poep->poep_code;
21107c478bd9Sstevel@tonic-gate if (i == POECODE_PADI || i == POECODE_PADR) {
21117c478bd9Sstevel@tonic-gate /* These go to the server daemon only. */
21127c478bd9Sstevel@tonic-gate tcl = tll->tll_defcl;
21137c478bd9Sstevel@tonic-gate } else if (i == POECODE_PADO || i == POECODE_PADS) {
21147c478bd9Sstevel@tonic-gate /*
21157c478bd9Sstevel@tonic-gate * These go to a client only, and are demuxed
21167c478bd9Sstevel@tonic-gate * by the Host-Uniq field (into which we stuff
21177c478bd9Sstevel@tonic-gate * our local ID number when generating
21187c478bd9Sstevel@tonic-gate * PADI/PADR).
21197c478bd9Sstevel@tonic-gate */
2120002c70ffScarlsonj ucp = (const uchar_t *)(poep + 1);
21217c478bd9Sstevel@tonic-gate i = msglen;
21227c478bd9Sstevel@tonic-gate while (i > POET_HDRLEN) {
21237c478bd9Sstevel@tonic-gate if (POET_GET_TYPE(ucp) == POETT_END) {
21247c478bd9Sstevel@tonic-gate i = 0;
21257c478bd9Sstevel@tonic-gate break;
21267c478bd9Sstevel@tonic-gate }
21277c478bd9Sstevel@tonic-gate if (POET_GET_TYPE(ucp) == POETT_UNIQ &&
21287c478bd9Sstevel@tonic-gate POET_GET_LENG(ucp) >= sizeof (uint32_t))
21297c478bd9Sstevel@tonic-gate break;
21307c478bd9Sstevel@tonic-gate i -= POET_GET_LENG(ucp) + POET_HDRLEN;
21317c478bd9Sstevel@tonic-gate ucp = POET_NEXT(ucp);
21327c478bd9Sstevel@tonic-gate }
21337c478bd9Sstevel@tonic-gate if (i >= POET_HDRLEN + 4)
21347c478bd9Sstevel@tonic-gate sessid = GETLONG(ucp + POET_HDRLEN);
21357c478bd9Sstevel@tonic-gate tcl = tcl_by_minor((minor_t)sessid);
21367c478bd9Sstevel@tonic-gate } else {
21377c478bd9Sstevel@tonic-gate /*
21387c478bd9Sstevel@tonic-gate * Try minor number as session ID first, since
21397c478bd9Sstevel@tonic-gate * it's used that way on server side. It's
21407c478bd9Sstevel@tonic-gate * not used that way on the client, though, so
21417c478bd9Sstevel@tonic-gate * this might not work. If this isn't the
21427c478bd9Sstevel@tonic-gate * right one, then try the tll cache. If
21437c478bd9Sstevel@tonic-gate * neither is right, then search all open
21447c478bd9Sstevel@tonic-gate * clients. Did I mention that the PPPoE
21457c478bd9Sstevel@tonic-gate * protocol is badly designed?
21467c478bd9Sstevel@tonic-gate */
21477c478bd9Sstevel@tonic-gate tcl = tcl_by_minor((minor_t)sessid);
21487c478bd9Sstevel@tonic-gate if (tcl == NULL ||
21497c478bd9Sstevel@tonic-gate (!isdata && tcl->tcl_ctrl_tll != tll) ||
21507c478bd9Sstevel@tonic-gate (isdata && tcl->tcl_data_tll != tll) ||
21517c478bd9Sstevel@tonic-gate sessid != tcl->tcl_rsessid ||
21527c478bd9Sstevel@tonic-gate bcmp(srcaddr, tcl->tcl_address.pta_pppoe.ptma_mac,
2153002c70ffScarlsonj sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) != 0)
21547c478bd9Sstevel@tonic-gate tcl = tll->tll_lastcl;
21557c478bd9Sstevel@tonic-gate if (tcl == NULL ||
21567c478bd9Sstevel@tonic-gate (!isdata && tcl->tcl_ctrl_tll != tll) ||
21577c478bd9Sstevel@tonic-gate (isdata && tcl->tcl_data_tll != tll) ||
21587c478bd9Sstevel@tonic-gate sessid != tcl->tcl_rsessid ||
21597c478bd9Sstevel@tonic-gate bcmp(srcaddr, tcl->tcl_address.pta_pppoe.ptma_mac,
2160002c70ffScarlsonj sizeof (tcl->tcl_address.pta_pppoe.ptma_mac)) != 0)
21617c478bd9Sstevel@tonic-gate tcl = NULL;
21627c478bd9Sstevel@tonic-gate if (tcl == NULL && sessid != 0) {
21637c478bd9Sstevel@tonic-gate struct poedat poedat;
21647c478bd9Sstevel@tonic-gate
21657c478bd9Sstevel@tonic-gate /*
21667c478bd9Sstevel@tonic-gate * Slow mode. Too bad. If you don't like it,
21677c478bd9Sstevel@tonic-gate * you can always choose a better protocol.
21687c478bd9Sstevel@tonic-gate */
21697c478bd9Sstevel@tonic-gate poedat.sessid = sessid;
21707c478bd9Sstevel@tonic-gate poedat.tll = tll;
21717c478bd9Sstevel@tonic-gate poedat.srcaddr = srcaddr;
21727c478bd9Sstevel@tonic-gate poedat.tcl = NULL;
21737c478bd9Sstevel@tonic-gate poedat.isdata = isdata;
21747c478bd9Sstevel@tonic-gate vmem_walk(tcl_minor_arena, VMEM_ALLOC,
21757c478bd9Sstevel@tonic-gate tclvm_pppoe_search, &poedat);
21767c478bd9Sstevel@tonic-gate KLINCR(lks_walks);
21777c478bd9Sstevel@tonic-gate if ((tcl = poedat.tcl) != NULL) {
21787c478bd9Sstevel@tonic-gate tll->tll_lastcl = tcl;
21797c478bd9Sstevel@tonic-gate KCINCR(cks_walks);
21807c478bd9Sstevel@tonic-gate }
21817c478bd9Sstevel@tonic-gate }
21827c478bd9Sstevel@tonic-gate }
21837c478bd9Sstevel@tonic-gate break;
21847c478bd9Sstevel@tonic-gate }
21857c478bd9Sstevel@tonic-gate
21867c478bd9Sstevel@tonic-gate if (tcl == NULL || tcl->tcl_rq == NULL) {
2187002c70ffScarlsonj DTRACE_PROBE3(sppptun__recv__discard, int, sessid,
2188002c70ffScarlsonj tuncl_t *, tcl, mblk_t *, mp);
21897c478bd9Sstevel@tonic-gate if (tcl == NULL) {
21907c478bd9Sstevel@tonic-gate KLINCR(lks_in_nomatch);
21917c478bd9Sstevel@tonic-gate }
21927c478bd9Sstevel@tonic-gate if (isdata) {
21937c478bd9Sstevel@tonic-gate KLINCR(lks_indata_drops);
21947c478bd9Sstevel@tonic-gate if (tcl != NULL)
21957c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_ierrors++;
21967c478bd9Sstevel@tonic-gate } else {
21977c478bd9Sstevel@tonic-gate KLINCR(lks_inctrl_drops);
21987c478bd9Sstevel@tonic-gate if (tcl != NULL) {
21997c478bd9Sstevel@tonic-gate KCINCR(cks_inctrl_drops);
22007c478bd9Sstevel@tonic-gate }
22017c478bd9Sstevel@tonic-gate }
22027c478bd9Sstevel@tonic-gate freemsg(mp);
22037c478bd9Sstevel@tonic-gate return (NULL);
22047c478bd9Sstevel@tonic-gate }
22057c478bd9Sstevel@tonic-gate
22067c478bd9Sstevel@tonic-gate if (tcl->tcl_data_tll == tll && isdata) {
22077c478bd9Sstevel@tonic-gate if (!adjmsg(mp, remlen) ||
22087c478bd9Sstevel@tonic-gate (i = msgsize(mp)) < msglen ||
22097c478bd9Sstevel@tonic-gate (i > msglen && !adjmsg(mp, msglen - i))) {
22107c478bd9Sstevel@tonic-gate KLINCR(lks_indata_drops);
22117c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_ierrors++;
22127c478bd9Sstevel@tonic-gate freemsg(mp);
22137c478bd9Sstevel@tonic-gate return (NULL);
22147c478bd9Sstevel@tonic-gate }
22157c478bd9Sstevel@tonic-gate /* XXX -- address/control handling in pppd needs help. */
22167c478bd9Sstevel@tonic-gate if (*mp->b_rptr != 0xFF) {
22177c478bd9Sstevel@tonic-gate if ((mp = prependb(mp, 2, 1)) == NULL) {
22187c478bd9Sstevel@tonic-gate KLINCR(lks_indata_drops);
22197c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_ierrors++;
22207c478bd9Sstevel@tonic-gate return (NULL);
22217c478bd9Sstevel@tonic-gate }
22227c478bd9Sstevel@tonic-gate mp->b_rptr[0] = 0xFF;
22237c478bd9Sstevel@tonic-gate mp->b_rptr[1] = 0x03;
22247c478bd9Sstevel@tonic-gate }
22257c478bd9Sstevel@tonic-gate MTYPE(mp) = M_DATA;
22267c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_ibytes += msgsize(mp);
22277c478bd9Sstevel@tonic-gate tcl->tcl_stats.ppp_ipackets++;
22287c478bd9Sstevel@tonic-gate KLINCR(lks_indata);
22297c478bd9Sstevel@tonic-gate } else {
22307c478bd9Sstevel@tonic-gate if (isdata || tcl->tcl_ctrl_tll != tll ||
22317c478bd9Sstevel@tonic-gate (mnew = make_control(tcl, tll, PTCA_CONTROL, tcl)) ==
22327c478bd9Sstevel@tonic-gate NULL) {
22337c478bd9Sstevel@tonic-gate KLINCR(lks_inctrl_drops);
22347c478bd9Sstevel@tonic-gate KCINCR(cks_inctrl_drops);
22357c478bd9Sstevel@tonic-gate freemsg(mp);
22367c478bd9Sstevel@tonic-gate return (NULL);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate /* Fix up source address; peer might not be set yet. */
22397c478bd9Sstevel@tonic-gate pap = &((struct ppptun_control *)mnew->b_rptr)->ptc_address;
22407c478bd9Sstevel@tonic-gate bcopy(srcaddr, pap->pta_pppoe.ptma_mac,
22417c478bd9Sstevel@tonic-gate sizeof (pap->pta_pppoe.ptma_mac));
22427c478bd9Sstevel@tonic-gate mnew->b_cont = mp;
22437c478bd9Sstevel@tonic-gate mp = mnew;
22447c478bd9Sstevel@tonic-gate KLINCR(lks_inctrls);
22457c478bd9Sstevel@tonic-gate KCINCR(cks_inctrls);
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate *mpp = mp;
22487c478bd9Sstevel@tonic-gate return (tcl->tcl_rq);
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate
22517c478bd9Sstevel@tonic-gate /*
22527c478bd9Sstevel@tonic-gate * sppptun_urput()
22537c478bd9Sstevel@tonic-gate *
22547c478bd9Sstevel@tonic-gate * MT-Perimeters:
22557c478bd9Sstevel@tonic-gate * shared inner, shared outer.
22567c478bd9Sstevel@tonic-gate *
22577c478bd9Sstevel@tonic-gate * Description:
22587c478bd9Sstevel@tonic-gate * Upper read-side put procedure. Messages from the underlying
22597c478bd9Sstevel@tonic-gate * lower stream driver arrive here. See sppptun_recv for the
22607c478bd9Sstevel@tonic-gate * demultiplexing logic.
22617c478bd9Sstevel@tonic-gate */
2262868fcfe9SToomas Soome static int
sppptun_urput(queue_t * q,mblk_t * mp)22637c478bd9Sstevel@tonic-gate sppptun_urput(queue_t *q, mblk_t *mp)
22647c478bd9Sstevel@tonic-gate {
22657c478bd9Sstevel@tonic-gate union DL_primitives *dlprim;
22667c478bd9Sstevel@tonic-gate mblk_t *mpnext;
22677c478bd9Sstevel@tonic-gate tunll_t *tll;
22687c478bd9Sstevel@tonic-gate queue_t *nextq;
22697c478bd9Sstevel@tonic-gate
2270002c70ffScarlsonj tll = q->q_ptr;
22717c478bd9Sstevel@tonic-gate ASSERT(!(tll->tll_flags & TLLF_NOTLOWER));
22727c478bd9Sstevel@tonic-gate
22737c478bd9Sstevel@tonic-gate switch (MTYPE(mp)) {
22747c478bd9Sstevel@tonic-gate case M_DATA:
22757c478bd9Sstevel@tonic-gate /*
22767c478bd9Sstevel@tonic-gate * When we're bound over IP, data arrives here. The
22777c478bd9Sstevel@tonic-gate * packet starts with the IP header itself.
22787c478bd9Sstevel@tonic-gate */
2279002c70ffScarlsonj if ((nextq = sppptun_recv(q, &mp, NULL)) != NULL)
22807c478bd9Sstevel@tonic-gate putnext(nextq, mp);
22817c478bd9Sstevel@tonic-gate break;
22827c478bd9Sstevel@tonic-gate
22837c478bd9Sstevel@tonic-gate case M_PROTO:
22847c478bd9Sstevel@tonic-gate case M_PCPROTO:
22857c478bd9Sstevel@tonic-gate /* Data arrives here for UDP or raw Ethernet, not IP. */
22867c478bd9Sstevel@tonic-gate switch (tll->tll_style) {
22877c478bd9Sstevel@tonic-gate /* PPTP control messages are over TCP only. */
22887c478bd9Sstevel@tonic-gate case PTS_PPTP:
22897c478bd9Sstevel@tonic-gate default:
22907c478bd9Sstevel@tonic-gate ASSERT(0); /* how'd that happen? */
22917c478bd9Sstevel@tonic-gate break;
22927c478bd9Sstevel@tonic-gate
22937c478bd9Sstevel@tonic-gate case PTS_PPPOE: /* DLPI message */
2294002c70ffScarlsonj if (MBLKL(mp) < sizeof (t_uscalar_t))
2295002c70ffScarlsonj break;
22967c478bd9Sstevel@tonic-gate dlprim = (union DL_primitives *)mp->b_rptr;
22977c478bd9Sstevel@tonic-gate switch (dlprim->dl_primitive) {
2298002c70ffScarlsonj case DL_UNITDATA_IND: {
2299002c70ffScarlsonj size_t mlen = MBLKL(mp);
23007c478bd9Sstevel@tonic-gate
2301002c70ffScarlsonj if (mlen < DL_UNITDATA_IND_SIZE)
2302002c70ffScarlsonj break;
2303002c70ffScarlsonj if (dlprim->unitdata_ind.dl_src_addr_offset <
2304002c70ffScarlsonj DL_UNITDATA_IND_SIZE ||
2305002c70ffScarlsonj dlprim->unitdata_ind.dl_src_addr_offset +
2306002c70ffScarlsonj dlprim->unitdata_ind.dl_src_addr_length >
2307002c70ffScarlsonj mlen)
2308002c70ffScarlsonj break;
2309002c70ffScarlsonj }
2310002c70ffScarlsonj /* FALLTHROUGH */
2311002c70ffScarlsonj case DL_UNITDATA_REQ: /* For loopback support. */
2312002c70ffScarlsonj if (dlprim->dl_primitive == DL_UNITDATA_REQ &&
2313002c70ffScarlsonj MBLKL(mp) < DL_UNITDATA_REQ_SIZE)
2314002c70ffScarlsonj break;
2315002c70ffScarlsonj if ((mpnext = mp->b_cont) == NULL)
2316002c70ffScarlsonj break;
23177c478bd9Sstevel@tonic-gate MTYPE(mpnext) = M_DATA;
23187c478bd9Sstevel@tonic-gate nextq = sppptun_recv(q, &mpnext,
2319002c70ffScarlsonj dlprim->dl_primitive == DL_UNITDATA_IND ?
23207c478bd9Sstevel@tonic-gate mp->b_rptr +
2321002c70ffScarlsonj dlprim->unitdata_ind.dl_src_addr_offset :
23227c478bd9Sstevel@tonic-gate tll->tll_lcladdr.pta_pppoe.ptma_mac);
23237c478bd9Sstevel@tonic-gate if (nextq != NULL)
23247c478bd9Sstevel@tonic-gate putnext(nextq, mpnext);
23257c478bd9Sstevel@tonic-gate freeb(mp);
2326868fcfe9SToomas Soome return (0);
23277c478bd9Sstevel@tonic-gate
23287c478bd9Sstevel@tonic-gate default:
23297c478bd9Sstevel@tonic-gate urput_dlpi(q, mp);
2330868fcfe9SToomas Soome return (0);
23317c478bd9Sstevel@tonic-gate }
23327c478bd9Sstevel@tonic-gate break;
23337c478bd9Sstevel@tonic-gate }
2334002c70ffScarlsonj freemsg(mp);
23357c478bd9Sstevel@tonic-gate break;
23367c478bd9Sstevel@tonic-gate
23377c478bd9Sstevel@tonic-gate default:
23387c478bd9Sstevel@tonic-gate freemsg(mp);
23397c478bd9Sstevel@tonic-gate break;
23407c478bd9Sstevel@tonic-gate }
2341868fcfe9SToomas Soome return (0);
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate
23447c478bd9Sstevel@tonic-gate /*
23457c478bd9Sstevel@tonic-gate * sppptun_ursrv()
23467c478bd9Sstevel@tonic-gate *
23477c478bd9Sstevel@tonic-gate * MT-Perimeters:
23487c478bd9Sstevel@tonic-gate * exclusive inner, shared outer.
23497c478bd9Sstevel@tonic-gate *
23507c478bd9Sstevel@tonic-gate * Description:
23517c478bd9Sstevel@tonic-gate * Upper read-side service procedure. This procedure services the
23527c478bd9Sstevel@tonic-gate * client streams. We get here because the client (PPP) asserts
23537c478bd9Sstevel@tonic-gate * flow control down to us.
23547c478bd9Sstevel@tonic-gate */
23557c478bd9Sstevel@tonic-gate static int
sppptun_ursrv(queue_t * q)23567c478bd9Sstevel@tonic-gate sppptun_ursrv(queue_t *q)
23577c478bd9Sstevel@tonic-gate {
23587c478bd9Sstevel@tonic-gate mblk_t *mp;
23597c478bd9Sstevel@tonic-gate
23607c478bd9Sstevel@tonic-gate ASSERT(q->q_ptr != NULL);
23617c478bd9Sstevel@tonic-gate
23627c478bd9Sstevel@tonic-gate while ((mp = getq(q)) != NULL) {
23637c478bd9Sstevel@tonic-gate if (canputnext(q)) {
23647c478bd9Sstevel@tonic-gate putnext(q, mp);
23657c478bd9Sstevel@tonic-gate } else {
23667c478bd9Sstevel@tonic-gate (void) putbq(q, mp);
23677c478bd9Sstevel@tonic-gate break;
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate }
23707c478bd9Sstevel@tonic-gate return (0);
23717c478bd9Sstevel@tonic-gate }
23727c478bd9Sstevel@tonic-gate
23737c478bd9Sstevel@tonic-gate /*
23747c478bd9Sstevel@tonic-gate * Dummy constructor/destructor functions for kmem_cache_create.
23757c478bd9Sstevel@tonic-gate * We're just using kmem as an allocator of integers, not real
23767c478bd9Sstevel@tonic-gate * storage.
23777c478bd9Sstevel@tonic-gate */
23787c478bd9Sstevel@tonic-gate
23797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23807c478bd9Sstevel@tonic-gate static int
tcl_constructor(void * maddr,void * arg,int kmflags)23817c478bd9Sstevel@tonic-gate tcl_constructor(void *maddr, void *arg, int kmflags)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate return (0);
23847c478bd9Sstevel@tonic-gate }
23857c478bd9Sstevel@tonic-gate
23867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23877c478bd9Sstevel@tonic-gate static void
tcl_destructor(void * maddr,void * arg)23887c478bd9Sstevel@tonic-gate tcl_destructor(void *maddr, void *arg)
23897c478bd9Sstevel@tonic-gate {
23907c478bd9Sstevel@tonic-gate }
23917c478bd9Sstevel@tonic-gate
23927c478bd9Sstevel@tonic-gate /*
23937c478bd9Sstevel@tonic-gate * Total size occupied by one tunnel client. Each tunnel client
23947c478bd9Sstevel@tonic-gate * consumes one pointer for tcl_slots array, one tuncl_t structure and
23957c478bd9Sstevel@tonic-gate * two messages preallocated for close.
23967c478bd9Sstevel@tonic-gate */
23977c478bd9Sstevel@tonic-gate #define TUNCL_SIZE (sizeof (tuncl_t) + sizeof (tuncl_t *) + \
23987c478bd9Sstevel@tonic-gate 2 * sizeof (dblk_t))
23997c478bd9Sstevel@tonic-gate
24007c478bd9Sstevel@tonic-gate /*
24017c478bd9Sstevel@tonic-gate * Clear all bits of x except the highest bit
24027c478bd9Sstevel@tonic-gate */
2403868fcfe9SToomas Soome #define truncate(x) ((x) <= 2 ? (x) : (1 << (highbit(x) - 1)))
24047c478bd9Sstevel@tonic-gate
24057c478bd9Sstevel@tonic-gate /*
24067c478bd9Sstevel@tonic-gate * This function initializes some well-known global variables inside
24077c478bd9Sstevel@tonic-gate * the module.
24087c478bd9Sstevel@tonic-gate *
24097c478bd9Sstevel@tonic-gate * Called by sppptun_mod.c:_init() before installing the module.
24107c478bd9Sstevel@tonic-gate */
24117c478bd9Sstevel@tonic-gate void
sppptun_init(void)24127c478bd9Sstevel@tonic-gate sppptun_init(void)
24137c478bd9Sstevel@tonic-gate {
24147c478bd9Sstevel@tonic-gate tunll_list.q_forw = tunll_list.q_back = &tunll_list;
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate
24177c478bd9Sstevel@tonic-gate /*
24187c478bd9Sstevel@tonic-gate * This function allocates the initial internal storage for the
24197c478bd9Sstevel@tonic-gate * sppptun driver.
24207c478bd9Sstevel@tonic-gate *
24217c478bd9Sstevel@tonic-gate * Called by sppptun_mod.c:_init() after installing module.
24227c478bd9Sstevel@tonic-gate */
24237c478bd9Sstevel@tonic-gate void
sppptun_tcl_init(void)24247c478bd9Sstevel@tonic-gate sppptun_tcl_init(void)
24257c478bd9Sstevel@tonic-gate {
24267c478bd9Sstevel@tonic-gate uint_t i, j;
24277c478bd9Sstevel@tonic-gate
24287c478bd9Sstevel@tonic-gate rw_init(&tcl_rwlock, NULL, RW_DRIVER, NULL);
24297c478bd9Sstevel@tonic-gate rw_enter(&tcl_rwlock, RW_WRITER);
24307c478bd9Sstevel@tonic-gate tcl_nslots = sppptun_init_cnt;
24317c478bd9Sstevel@tonic-gate tcl_slots = kmem_zalloc(tcl_nslots * sizeof (tuncl_t *), KM_SLEEP);
24327c478bd9Sstevel@tonic-gate
24337c478bd9Sstevel@tonic-gate tcl_cache = kmem_cache_create("sppptun_map", sizeof (tuncl_t), 0,
24347c478bd9Sstevel@tonic-gate tcl_constructor, tcl_destructor, NULL, NULL, NULL, 0);
24357c478bd9Sstevel@tonic-gate
24367c478bd9Sstevel@tonic-gate /* Allocate integer space for minor numbers */
24377c478bd9Sstevel@tonic-gate tcl_minor_arena = vmem_create("sppptun_minor", (void *)1, tcl_nslots,
24387c478bd9Sstevel@tonic-gate 1, NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
24397c478bd9Sstevel@tonic-gate
24407c478bd9Sstevel@tonic-gate /*
24417c478bd9Sstevel@tonic-gate * Calculate available number of tunnels - how many tunnels
24427c478bd9Sstevel@tonic-gate * can we allocate in sppptun_pctofmem % of available
24437c478bd9Sstevel@tonic-gate * memory. The value is rounded up to the nearest power of 2.
24447c478bd9Sstevel@tonic-gate */
24457c478bd9Sstevel@tonic-gate i = (sppptun_pctofmem * kmem_maxavail()) / (100 * TUNCL_SIZE);
24467c478bd9Sstevel@tonic-gate j = truncate(i); /* i with non-high bits stripped */
24477c478bd9Sstevel@tonic-gate if (i != j)
24487c478bd9Sstevel@tonic-gate j *= 2;
24497c478bd9Sstevel@tonic-gate tcl_minormax = j;
24507c478bd9Sstevel@tonic-gate rw_exit(&tcl_rwlock);
24517c478bd9Sstevel@tonic-gate }
24527c478bd9Sstevel@tonic-gate
24537c478bd9Sstevel@tonic-gate /*
24547c478bd9Sstevel@tonic-gate * This function checks that there are no plumbed streams or other users.
24557c478bd9Sstevel@tonic-gate *
24567c478bd9Sstevel@tonic-gate * Called by sppptun_mod.c:_fini(). Assumes that we're exclusive on
24577c478bd9Sstevel@tonic-gate * both perimeters.
24587c478bd9Sstevel@tonic-gate */
24597c478bd9Sstevel@tonic-gate int
sppptun_tcl_fintest(void)24607c478bd9Sstevel@tonic-gate sppptun_tcl_fintest(void)
24617c478bd9Sstevel@tonic-gate {
2462002c70ffScarlsonj if (tunll_list.q_forw != &tunll_list || tcl_inuse > 0)
24637c478bd9Sstevel@tonic-gate return (EBUSY);
2464002c70ffScarlsonj else
2465002c70ffScarlsonj return (0);
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate
24687c478bd9Sstevel@tonic-gate /*
24697c478bd9Sstevel@tonic-gate * If no lower streams are plumbed, then this function deallocates all
24707c478bd9Sstevel@tonic-gate * internal storage in preparation for unload.
24717c478bd9Sstevel@tonic-gate *
24727c478bd9Sstevel@tonic-gate * Called by sppptun_mod.c:_fini(). Assumes that we're exclusive on
24737c478bd9Sstevel@tonic-gate * both perimeters.
24747c478bd9Sstevel@tonic-gate */
24757c478bd9Sstevel@tonic-gate void
sppptun_tcl_fini(void)24767c478bd9Sstevel@tonic-gate sppptun_tcl_fini(void)
24777c478bd9Sstevel@tonic-gate {
24787c478bd9Sstevel@tonic-gate if (tcl_minor_arena != NULL) {
24797c478bd9Sstevel@tonic-gate vmem_destroy(tcl_minor_arena);
24807c478bd9Sstevel@tonic-gate tcl_minor_arena = NULL;
24817c478bd9Sstevel@tonic-gate }
24827c478bd9Sstevel@tonic-gate if (tcl_cache != NULL) {
24837c478bd9Sstevel@tonic-gate kmem_cache_destroy(tcl_cache);
24847c478bd9Sstevel@tonic-gate tcl_cache = NULL;
24857c478bd9Sstevel@tonic-gate }
24867c478bd9Sstevel@tonic-gate kmem_free(tcl_slots, tcl_nslots * sizeof (tuncl_t *));
24877c478bd9Sstevel@tonic-gate tcl_slots = NULL;
24887c478bd9Sstevel@tonic-gate rw_destroy(&tcl_rwlock);
24897c478bd9Sstevel@tonic-gate ASSERT(tcl_slots == NULL);
24907c478bd9Sstevel@tonic-gate ASSERT(tcl_cache == NULL);
24917c478bd9Sstevel@tonic-gate ASSERT(tcl_minor_arena == NULL);
24927c478bd9Sstevel@tonic-gate }
2493