10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51289Sja97890 * Common Development and Distribution License (the "License"). 61289Sja97890 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 225815Sgt145670 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/stream.h> 320Sstevel@tonic-gate #include <sys/stropts.h> 330Sstevel@tonic-gate #include <sys/strlog.h> 340Sstevel@tonic-gate #include <sys/strsun.h> 350Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 360Sstevel@tonic-gate #include <sys/tihdr.h> 370Sstevel@tonic-gate #include <sys/timod.h> 380Sstevel@tonic-gate #include <sys/ddi.h> 390Sstevel@tonic-gate #include <sys/sunddi.h> 401676Sjpk #include <sys/strsubr.h> 410Sstevel@tonic-gate #include <sys/cmn_err.h> 420Sstevel@tonic-gate #include <sys/debug.h> 430Sstevel@tonic-gate #include <sys/kmem.h> 440Sstevel@tonic-gate #include <sys/policy.h> 451676Sjpk #include <sys/priv.h> 460Sstevel@tonic-gate #include <sys/zone.h> 471673Sgt145670 #include <sys/time.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <sys/socket.h> 500Sstevel@tonic-gate #include <sys/isa_defs.h> 510Sstevel@tonic-gate #include <sys/suntpi.h> 520Sstevel@tonic-gate #include <sys/xti_inet.h> 533448Sdh155122 #include <sys/netstack.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate #include <net/route.h> 560Sstevel@tonic-gate #include <net/if.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate #include <netinet/in.h> 590Sstevel@tonic-gate #include <netinet/ip6.h> 600Sstevel@tonic-gate #include <netinet/icmp6.h> 610Sstevel@tonic-gate #include <inet/common.h> 620Sstevel@tonic-gate #include <inet/ip.h> 630Sstevel@tonic-gate #include <inet/ip6.h> 640Sstevel@tonic-gate #include <inet/mi.h> 650Sstevel@tonic-gate #include <inet/nd.h> 660Sstevel@tonic-gate #include <inet/optcom.h> 670Sstevel@tonic-gate #include <inet/snmpcom.h> 680Sstevel@tonic-gate #include <inet/kstatcom.h> 690Sstevel@tonic-gate #include <inet/rawip_impl.h> 700Sstevel@tonic-gate 710Sstevel@tonic-gate #include <netinet/ip_mroute.h> 720Sstevel@tonic-gate #include <inet/tcp.h> 730Sstevel@tonic-gate #include <net/pfkeyv2.h> 740Sstevel@tonic-gate #include <inet/ipsec_info.h> 750Sstevel@tonic-gate #include <inet/ipclassifier.h> 760Sstevel@tonic-gate 771676Sjpk #include <sys/tsol/label.h> 781676Sjpk #include <sys/tsol/tnet.h> 791676Sjpk 803318Srshoaib #include <inet/ip_ire.h> 813318Srshoaib #include <inet/ip_if.h> 823318Srshoaib 833318Srshoaib #include <inet/ip_impl.h> 843318Srshoaib 850Sstevel@tonic-gate /* 860Sstevel@tonic-gate * Synchronization notes: 870Sstevel@tonic-gate * 885240Snordmark * RAWIP is MT and uses the usual kernel synchronization primitives. There is 895240Snordmark * locks, which is icmp_rwlock. We also use conn_lock when updating things 905240Snordmark * which affect the IP classifier lookup. 915240Snordmark * The lock order is icmp_rwlock -> conn_lock. 925240Snordmark * 935240Snordmark * The icmp_rwlock: 945240Snordmark * This protects most of the other fields in the icmp_t. The exact list of 955240Snordmark * fields which are protected by each of the above locks is documented in 965240Snordmark * the icmp_t structure definition. 975240Snordmark * 985240Snordmark * Plumbing notes: 995240Snordmark * ICMP is always a device driver. For compatibility with mibopen() code 1005240Snordmark * it is possible to I_PUSH "icmp", but that results in pushing a passthrough 1015240Snordmark * dummy module. 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate static void icmp_addr_req(queue_t *q, mblk_t *mp); 1050Sstevel@tonic-gate static void icmp_bind(queue_t *q, mblk_t *mp); 1060Sstevel@tonic-gate static void icmp_bind_proto(queue_t *q); 1075240Snordmark static void icmp_bind_result(conn_t *, mblk_t *); 1085240Snordmark static void icmp_bind_ack(conn_t *, mblk_t *mp); 1095240Snordmark static void icmp_bind_error(conn_t *, mblk_t *mp); 1105240Snordmark static int icmp_build_hdrs(icmp_t *icmp); 1110Sstevel@tonic-gate static void icmp_capability_req(queue_t *q, mblk_t *mp); 1120Sstevel@tonic-gate static int icmp_close(queue_t *q); 1130Sstevel@tonic-gate static void icmp_connect(queue_t *q, mblk_t *mp); 1140Sstevel@tonic-gate static void icmp_disconnect(queue_t *q, mblk_t *mp); 1150Sstevel@tonic-gate static void icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, 1160Sstevel@tonic-gate int sys_error); 1170Sstevel@tonic-gate static void icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive, 1180Sstevel@tonic-gate t_scalar_t t_error, int sys_error); 1190Sstevel@tonic-gate static void icmp_icmp_error(queue_t *q, mblk_t *mp); 1200Sstevel@tonic-gate static void icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp); 1210Sstevel@tonic-gate static void icmp_info_req(queue_t *q, mblk_t *mp); 1225240Snordmark static void icmp_input(void *, mblk_t *, void *); 1230Sstevel@tonic-gate static mblk_t *icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, 1240Sstevel@tonic-gate t_scalar_t addr_length, in_port_t); 1255240Snordmark static int icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, 1265240Snordmark cred_t *credp, boolean_t isv6); 1275240Snordmark static int icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, 1285240Snordmark cred_t *credp); 1295240Snordmark static int icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, 1305240Snordmark cred_t *credp); 1315240Snordmark static void icmp_output(queue_t *q, mblk_t *mp); 1320Sstevel@tonic-gate static int icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, 1330Sstevel@tonic-gate int *errorp, void *thisdg_attrs); 1340Sstevel@tonic-gate static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name); 1350Sstevel@tonic-gate int icmp_opt_set(queue_t *q, uint_t optset_context, 1360Sstevel@tonic-gate int level, int name, uint_t inlen, 1370Sstevel@tonic-gate uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 1380Sstevel@tonic-gate void *thisdg_attrs, cred_t *cr, mblk_t *mblk); 1390Sstevel@tonic-gate int icmp_opt_get(queue_t *q, int level, int name, 1400Sstevel@tonic-gate uchar_t *ptr); 1410Sstevel@tonic-gate static int icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 1423448Sdh155122 static boolean_t icmp_param_register(IDP *ndp, icmpparam_t *icmppa, int cnt); 1430Sstevel@tonic-gate static int icmp_param_set(queue_t *q, mblk_t *mp, char *value, 1440Sstevel@tonic-gate caddr_t cp, cred_t *cr); 1450Sstevel@tonic-gate static int icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name, 1460Sstevel@tonic-gate uchar_t *ptr, int len); 1470Sstevel@tonic-gate static int icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 1480Sstevel@tonic-gate cred_t *cr); 1490Sstevel@tonic-gate static void icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err); 1500Sstevel@tonic-gate static void icmp_unbind(queue_t *q, mblk_t *mp); 1510Sstevel@tonic-gate static void icmp_wput(queue_t *q, mblk_t *mp); 1520Sstevel@tonic-gate static void icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, 1530Sstevel@tonic-gate t_scalar_t tudr_optlen); 1540Sstevel@tonic-gate static void icmp_wput_other(queue_t *q, mblk_t *mp); 1550Sstevel@tonic-gate static void icmp_wput_iocdata(queue_t *q, mblk_t *mp); 1560Sstevel@tonic-gate static void icmp_wput_restricted(queue_t *q, mblk_t *mp); 1570Sstevel@tonic-gate 1583448Sdh155122 static void *rawip_stack_init(netstackid_t stackid, netstack_t *ns); 1593448Sdh155122 static void rawip_stack_fini(netstackid_t stackid, void *arg); 1603448Sdh155122 1613448Sdh155122 static void *rawip_kstat_init(netstackid_t stackid); 1623448Sdh155122 static void rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp); 1630Sstevel@tonic-gate static int rawip_kstat_update(kstat_t *kp, int rw); 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate 1665240Snordmark static struct module_info icmp_mod_info = { 1670Sstevel@tonic-gate 5707, "icmp", 1, INFPSZ, 512, 128 1680Sstevel@tonic-gate }; 1690Sstevel@tonic-gate 1705240Snordmark /* 1715240Snordmark * Entry points for ICMP as a device. 1725240Snordmark * We have separate open functions for the /dev/icmp and /dev/icmp6 devices. 1735240Snordmark */ 1745240Snordmark static struct qinit icmprinitv4 = { 1755240Snordmark NULL, NULL, icmp_openv4, icmp_close, NULL, &icmp_mod_info 1765240Snordmark }; 1775240Snordmark 1785240Snordmark static struct qinit icmprinitv6 = { 1795240Snordmark NULL, NULL, icmp_openv6, icmp_close, NULL, &icmp_mod_info 1800Sstevel@tonic-gate }; 1810Sstevel@tonic-gate 1825240Snordmark static struct qinit icmpwinit = { 1835277Snordmark (pfi_t)icmp_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL, &icmp_mod_info 1840Sstevel@tonic-gate }; 1850Sstevel@tonic-gate 1865240Snordmark /* For AF_INET aka /dev/icmp */ 1875240Snordmark struct streamtab icmpinfov4 = { 1885240Snordmark &icmprinitv4, &icmpwinit 1895240Snordmark }; 1905240Snordmark 1915240Snordmark /* For AF_INET6 aka /dev/icmp6 */ 1925240Snordmark struct streamtab icmpinfov6 = { 1935240Snordmark &icmprinitv6, &icmpwinit 1940Sstevel@tonic-gate }; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate static sin_t sin_null; /* Zero address for quick clears */ 1970Sstevel@tonic-gate static sin6_t sin6_null; /* Zero address for quick clears */ 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages */ 2000Sstevel@tonic-gate static struct T_info_ack icmp_g_t_info_ack = { 2010Sstevel@tonic-gate T_INFO_ACK, 2020Sstevel@tonic-gate IP_MAXPACKET, /* TSDU_size. icmp allows maximum size messages. */ 2030Sstevel@tonic-gate T_INVALID, /* ETSDU_size. icmp does not support expedited data. */ 2040Sstevel@tonic-gate T_INVALID, /* CDATA_size. icmp does not support connect data. */ 2050Sstevel@tonic-gate T_INVALID, /* DDATA_size. icmp does not support disconnect data. */ 2060Sstevel@tonic-gate 0, /* ADDR_size - filled in later. */ 2070Sstevel@tonic-gate 0, /* OPT_size - not initialized here */ 2080Sstevel@tonic-gate IP_MAXPACKET, /* TIDU_size. icmp allows maximum size messages. */ 2090Sstevel@tonic-gate T_CLTS, /* SERV_type. icmp supports connection-less. */ 2100Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from icmp_state. */ 2110Sstevel@tonic-gate (XPG4_1|SENDZERO) /* PROVIDER_flag */ 2120Sstevel@tonic-gate }; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2153448Sdh155122 * Table of ND variables supported by icmp. These are loaded into is_nd 2163448Sdh155122 * when the stack instance is created. 2170Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 2180Sstevel@tonic-gate */ 2190Sstevel@tonic-gate static icmpparam_t icmp_param_arr[] = { 2200Sstevel@tonic-gate /* min max value name */ 2210Sstevel@tonic-gate { 0, 128, 32, "icmp_wroff_extra" }, 2220Sstevel@tonic-gate { 1, 255, 255, "icmp_ipv4_ttl" }, 2230Sstevel@tonic-gate { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "icmp_ipv6_hoplimit"}, 2240Sstevel@tonic-gate { 0, 1, 1, "icmp_bsd_compat" }, 2250Sstevel@tonic-gate { 4096, 65536, 8192, "icmp_xmit_hiwat"}, 2260Sstevel@tonic-gate { 0, 65536, 1024, "icmp_xmit_lowat"}, 2270Sstevel@tonic-gate { 4096, 65536, 8192, "icmp_recv_hiwat"}, 2280Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "icmp_max_buf"}, 2290Sstevel@tonic-gate }; 2303448Sdh155122 #define is_wroff_extra is_param_arr[0].icmp_param_value 2313448Sdh155122 #define is_ipv4_ttl is_param_arr[1].icmp_param_value 2323448Sdh155122 #define is_ipv6_hoplimit is_param_arr[2].icmp_param_value 2333448Sdh155122 #define is_bsd_compat is_param_arr[3].icmp_param_value 2343448Sdh155122 #define is_xmit_hiwat is_param_arr[4].icmp_param_value 2353448Sdh155122 #define is_xmit_lowat is_param_arr[5].icmp_param_value 2363448Sdh155122 #define is_recv_hiwat is_param_arr[6].icmp_param_value 2373448Sdh155122 #define is_max_buf is_param_arr[7].icmp_param_value 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* 2400Sstevel@tonic-gate * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message 2410Sstevel@tonic-gate * passed to icmp_wput. 2420Sstevel@tonic-gate * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the ICMP 2430Sstevel@tonic-gate * protocol type placed in the message following the address. A T_BIND_ACK 2445240Snordmark * message is returned by ip_bind_v4/v6. 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate static void 2470Sstevel@tonic-gate icmp_bind(queue_t *q, mblk_t *mp) 2480Sstevel@tonic-gate { 2490Sstevel@tonic-gate sin_t *sin; 2500Sstevel@tonic-gate sin6_t *sin6; 2510Sstevel@tonic-gate mblk_t *mp1; 2520Sstevel@tonic-gate struct T_bind_req *tbr; 2530Sstevel@tonic-gate icmp_t *icmp; 2545240Snordmark conn_t *connp = Q_TO_CONN(q); 2555240Snordmark 2565240Snordmark icmp = connp->conn_icmp; 2570Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2580Sstevel@tonic-gate (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 2590Sstevel@tonic-gate "icmp_bind: bad req, len %u", 2600Sstevel@tonic-gate (uint_t)(mp->b_wptr - mp->b_rptr)); 2610Sstevel@tonic-gate icmp_err_ack(q, mp, TPROTO, 0); 2620Sstevel@tonic-gate return; 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate if (icmp->icmp_state != TS_UNBND) { 2650Sstevel@tonic-gate (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 2660Sstevel@tonic-gate "icmp_bind: bad state, %d", icmp->icmp_state); 2670Sstevel@tonic-gate icmp_err_ack(q, mp, TOUTSTATE, 0); 2680Sstevel@tonic-gate return; 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate /* 2710Sstevel@tonic-gate * Reallocate the message to make sure we have enough room for an 2720Sstevel@tonic-gate * address and the protocol type. 2730Sstevel@tonic-gate */ 2740Sstevel@tonic-gate mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 2750Sstevel@tonic-gate if (!mp1) { 2760Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, ENOMEM); 2770Sstevel@tonic-gate return; 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate mp = mp1; 2800Sstevel@tonic-gate tbr = (struct T_bind_req *)mp->b_rptr; 2810Sstevel@tonic-gate switch (tbr->ADDR_length) { 2820Sstevel@tonic-gate case 0: /* Generic request */ 2830Sstevel@tonic-gate tbr->ADDR_offset = sizeof (struct T_bind_req); 2840Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 2850Sstevel@tonic-gate tbr->ADDR_length = sizeof (sin_t); 2860Sstevel@tonic-gate sin = (sin_t *)&tbr[1]; 2870Sstevel@tonic-gate *sin = sin_null; 2880Sstevel@tonic-gate sin->sin_family = AF_INET; 2890Sstevel@tonic-gate mp->b_wptr = (uchar_t *)&sin[1]; 2900Sstevel@tonic-gate } else { 2910Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 2920Sstevel@tonic-gate tbr->ADDR_length = sizeof (sin6_t); 2930Sstevel@tonic-gate sin6 = (sin6_t *)&tbr[1]; 2940Sstevel@tonic-gate *sin6 = sin6_null; 2950Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 2960Sstevel@tonic-gate mp->b_wptr = (uchar_t *)&sin6[1]; 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate break; 2990Sstevel@tonic-gate case sizeof (sin_t): /* Complete IP address */ 3000Sstevel@tonic-gate sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3010Sstevel@tonic-gate sizeof (sin_t)); 3020Sstevel@tonic-gate if (sin == NULL || !OK_32PTR((char *)sin)) { 3030Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EINVAL); 3040Sstevel@tonic-gate return; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate if (icmp->icmp_family != AF_INET || 3070Sstevel@tonic-gate sin->sin_family != AF_INET) { 3080Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 3090Sstevel@tonic-gate return; 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate break; 3120Sstevel@tonic-gate case sizeof (sin6_t): /* Complete IP address */ 3130Sstevel@tonic-gate sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset, 3140Sstevel@tonic-gate sizeof (sin6_t)); 3150Sstevel@tonic-gate if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3160Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EINVAL); 3170Sstevel@tonic-gate return; 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate if (icmp->icmp_family != AF_INET6 || 3200Sstevel@tonic-gate sin6->sin6_family != AF_INET6) { 3210Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 3220Sstevel@tonic-gate return; 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate /* No support for mapped addresses on raw sockets */ 3250Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 3260Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL); 3270Sstevel@tonic-gate return; 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate break; 3300Sstevel@tonic-gate default: 3310Sstevel@tonic-gate (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 3320Sstevel@tonic-gate "icmp_bind: bad ADDR_length %d", tbr->ADDR_length); 3330Sstevel@tonic-gate icmp_err_ack(q, mp, TBADADDR, 0); 3340Sstevel@tonic-gate return; 3350Sstevel@tonic-gate } 3365240Snordmark 3375240Snordmark /* 3385240Snordmark * The state must be TS_UNBND. TPI mandates that users must send 3395240Snordmark * TPI primitives only 1 at a time and wait for the response before 3405240Snordmark * sending the next primitive. 3415240Snordmark */ 3425240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 3435240Snordmark if (icmp->icmp_state != TS_UNBND || icmp->icmp_pending_op != -1) { 3445240Snordmark rw_exit(&icmp->icmp_rwlock); 3455240Snordmark (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 3465240Snordmark "icmp_bind: bad state, %d", icmp->icmp_state); 3475240Snordmark icmp_err_ack(q, mp, TOUTSTATE, 0); 3485240Snordmark return; 3495240Snordmark } 3505240Snordmark 3515240Snordmark icmp->icmp_pending_op = tbr->PRIM_type; 3525240Snordmark 3530Sstevel@tonic-gate /* 3540Sstevel@tonic-gate * Copy the source address into our icmp structure. This address 3550Sstevel@tonic-gate * may still be zero; if so, ip will fill in the correct address 3560Sstevel@tonic-gate * each time an outbound packet is passed to it. 3575240Snordmark * If we are binding to a broadcast or multicast address then 3585240Snordmark * icmp_bind_ack will clear the source address when it receives 3595240Snordmark * the T_BIND_ACK. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate icmp->icmp_state = TS_IDLE; 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 3640Sstevel@tonic-gate ASSERT(sin != NULL); 3650Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV4_VERSION); 3660Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, 3670Sstevel@tonic-gate &icmp->icmp_v6src); 3680Sstevel@tonic-gate icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + 3690Sstevel@tonic-gate icmp->icmp_ip_snd_options_len; 3700Sstevel@tonic-gate icmp->icmp_bound_v6src = icmp->icmp_v6src; 3710Sstevel@tonic-gate } else { 3720Sstevel@tonic-gate int error; 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate ASSERT(sin6 != NULL); 3750Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV6_VERSION); 3760Sstevel@tonic-gate icmp->icmp_v6src = sin6->sin6_addr; 3770Sstevel@tonic-gate icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len; 3780Sstevel@tonic-gate icmp->icmp_bound_v6src = icmp->icmp_v6src; 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* Rebuild the header template */ 3815240Snordmark error = icmp_build_hdrs(icmp); 3820Sstevel@tonic-gate if (error != 0) { 3835240Snordmark icmp->icmp_pending_op = -1; 3845240Snordmark rw_exit(&icmp->icmp_rwlock); 3850Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, error); 3860Sstevel@tonic-gate return; 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate /* 3900Sstevel@tonic-gate * Place protocol type in the O_T_BIND_REQ/T_BIND_REQ following 3910Sstevel@tonic-gate * the address. 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate *mp->b_wptr++ = icmp->icmp_proto; 3940Sstevel@tonic-gate if (!(V6_OR_V4_INADDR_ANY(icmp->icmp_v6src))) { 3950Sstevel@tonic-gate /* 3960Sstevel@tonic-gate * Append a request for an IRE if src not 0 (INADDR_ANY) 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 3990Sstevel@tonic-gate if (!mp->b_cont) { 4005240Snordmark icmp->icmp_pending_op = -1; 4015240Snordmark rw_exit(&icmp->icmp_rwlock); 4020Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, ENOMEM); 4030Sstevel@tonic-gate return; 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (ire_t); 4060Sstevel@tonic-gate mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 4070Sstevel@tonic-gate } 4085240Snordmark rw_exit(&icmp->icmp_rwlock); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* Pass the O_T_BIND_REQ/T_BIND_REQ to ip. */ 4115240Snordmark if (icmp->icmp_family == AF_INET6) 4125240Snordmark mp = ip_bind_v6(q, mp, connp, NULL); 4135240Snordmark else 4145240Snordmark mp = ip_bind_v4(q, mp, connp); 4155240Snordmark 4165240Snordmark /* The above return NULL if the bind needs to be deferred */ 4175240Snordmark if (mp != NULL) 4185240Snordmark icmp_bind_result(connp, mp); 4195240Snordmark else 4205240Snordmark CONN_INC_REF(connp); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * Send message to IP to just bind to the protocol. 4250Sstevel@tonic-gate */ 4260Sstevel@tonic-gate static void 4270Sstevel@tonic-gate icmp_bind_proto(queue_t *q) 4280Sstevel@tonic-gate { 4290Sstevel@tonic-gate mblk_t *mp; 4300Sstevel@tonic-gate struct T_bind_req *tbr; 4310Sstevel@tonic-gate icmp_t *icmp; 4325240Snordmark conn_t *connp = Q_TO_CONN(q); 4335240Snordmark 4345240Snordmark icmp = connp->conn_icmp; 4355240Snordmark 4360Sstevel@tonic-gate mp = allocb(sizeof (struct T_bind_req) + sizeof (sin6_t) + 1, 4370Sstevel@tonic-gate BPRI_MED); 4380Sstevel@tonic-gate if (!mp) { 4390Sstevel@tonic-gate return; 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 4420Sstevel@tonic-gate tbr = (struct T_bind_req *)mp->b_rptr; 4430Sstevel@tonic-gate tbr->PRIM_type = O_T_BIND_REQ; /* change to T_BIND_REQ ? */ 4440Sstevel@tonic-gate tbr->ADDR_offset = sizeof (struct T_bind_req); 4455240Snordmark 4465240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 4470Sstevel@tonic-gate if (icmp->icmp_ipversion == IPV4_VERSION) { 4480Sstevel@tonic-gate sin_t *sin; 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate tbr->ADDR_length = sizeof (sin_t); 4510Sstevel@tonic-gate sin = (sin_t *)&tbr[1]; 4520Sstevel@tonic-gate *sin = sin_null; 4530Sstevel@tonic-gate sin->sin_family = AF_INET; 4540Sstevel@tonic-gate mp->b_wptr = (uchar_t *)&sin[1]; 4550Sstevel@tonic-gate } else { 4560Sstevel@tonic-gate sin6_t *sin6; 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV6_VERSION); 4590Sstevel@tonic-gate tbr->ADDR_length = sizeof (sin6_t); 4600Sstevel@tonic-gate sin6 = (sin6_t *)&tbr[1]; 4610Sstevel@tonic-gate *sin6 = sin6_null; 4620Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 4630Sstevel@tonic-gate mp->b_wptr = (uchar_t *)&sin6[1]; 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* Place protocol type in the O_T_BIND_REQ following the address. */ 4670Sstevel@tonic-gate *mp->b_wptr++ = icmp->icmp_proto; 4685240Snordmark rw_exit(&icmp->icmp_rwlock); 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* Pass the O_T_BIND_REQ to ip. */ 4715240Snordmark if (icmp->icmp_family == AF_INET6) 4725240Snordmark mp = ip_bind_v6(q, mp, connp, NULL); 4735240Snordmark else 4745240Snordmark mp = ip_bind_v4(q, mp, connp); 4755240Snordmark 4765240Snordmark /* The above return NULL if the bind needs to be deferred */ 4775240Snordmark if (mp != NULL) 4785240Snordmark icmp_bind_result(connp, mp); 4795240Snordmark else 4805240Snordmark CONN_INC_REF(connp); 4815240Snordmark } 4825240Snordmark 4835240Snordmark /* 4845240Snordmark * This is called from ip_wput_nondata to handle the results of a 4855240Snordmark * deferred RAWIP bind. It is called once the bind has been completed. 4865240Snordmark */ 4875240Snordmark void 4885240Snordmark rawip_resume_bind(conn_t *connp, mblk_t *mp) 4895240Snordmark { 4905240Snordmark ASSERT(connp != NULL && IPCL_IS_RAWIP(connp)); 4915240Snordmark 4925240Snordmark icmp_bind_result(connp, mp); 4935240Snordmark 4945240Snordmark CONN_OPER_PENDING_DONE(connp); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * This routine handles each T_CONN_REQ message passed to icmp. It 4990Sstevel@tonic-gate * associates a default destination address with the stream. 5000Sstevel@tonic-gate * 5010Sstevel@tonic-gate * This routine sends down a T_BIND_REQ to IP with the following mblks: 5020Sstevel@tonic-gate * T_BIND_REQ - specifying local and remote address. 5030Sstevel@tonic-gate * IRE_DB_REQ_TYPE - to get an IRE back containing ire_type and src 5040Sstevel@tonic-gate * T_OK_ACK - for the T_CONN_REQ 5050Sstevel@tonic-gate * T_CONN_CON - to keep the TPI user happy 5060Sstevel@tonic-gate * 5075240Snordmark * The connect completes in icmp_bind_result. 5080Sstevel@tonic-gate * When a T_BIND_ACK is received information is extracted from the IRE 5090Sstevel@tonic-gate * and the two appended messages are sent to the TPI user. 5105240Snordmark * Should icmp_bind_result receive T_ERROR_ACK for the T_BIND_REQ it will 5115240Snordmark * convert it to an error ack for the appropriate primitive. 5120Sstevel@tonic-gate */ 5130Sstevel@tonic-gate static void 5140Sstevel@tonic-gate icmp_connect(queue_t *q, mblk_t *mp) 5150Sstevel@tonic-gate { 5160Sstevel@tonic-gate sin_t *sin; 5170Sstevel@tonic-gate sin6_t *sin6; 5180Sstevel@tonic-gate mblk_t *mp1, *mp2; 5190Sstevel@tonic-gate struct T_conn_req *tcr; 5200Sstevel@tonic-gate icmp_t *icmp; 5210Sstevel@tonic-gate ipaddr_t v4dst; 5220Sstevel@tonic-gate in6_addr_t v6dst; 5230Sstevel@tonic-gate uint32_t flowinfo; 5245240Snordmark conn_t *connp = Q_TO_CONN(q); 5255240Snordmark 5265240Snordmark icmp = connp->conn_icmp; 5270Sstevel@tonic-gate tcr = (struct T_conn_req *)mp->b_rptr; 5280Sstevel@tonic-gate /* Sanity checks */ 5295240Snordmark if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_req)) { 5300Sstevel@tonic-gate icmp_err_ack(q, mp, TPROTO, 0); 5310Sstevel@tonic-gate return; 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate if (tcr->OPT_length != 0) { 5350Sstevel@tonic-gate icmp_err_ack(q, mp, TBADOPT, 0); 5360Sstevel@tonic-gate return; 5370Sstevel@tonic-gate } 5385240Snordmark 5390Sstevel@tonic-gate switch (tcr->DEST_length) { 5400Sstevel@tonic-gate default: 5410Sstevel@tonic-gate icmp_err_ack(q, mp, TBADADDR, 0); 5420Sstevel@tonic-gate return; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate case sizeof (sin_t): 5450Sstevel@tonic-gate sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5460Sstevel@tonic-gate sizeof (sin_t)); 5470Sstevel@tonic-gate if (sin == NULL || !OK_32PTR((char *)sin)) { 5480Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EINVAL); 5490Sstevel@tonic-gate return; 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate if (icmp->icmp_family != AF_INET || 5520Sstevel@tonic-gate sin->sin_family != AF_INET) { 5530Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 5540Sstevel@tonic-gate return; 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate v4dst = sin->sin_addr.s_addr; 5570Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); 5580Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV4_VERSION); 5590Sstevel@tonic-gate icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + 5600Sstevel@tonic-gate icmp->icmp_ip_snd_options_len; 5610Sstevel@tonic-gate break; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate case sizeof (sin6_t): 5640Sstevel@tonic-gate sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 5650Sstevel@tonic-gate sizeof (sin6_t)); 5660Sstevel@tonic-gate if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 5670Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EINVAL); 5680Sstevel@tonic-gate return; 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate if (icmp->icmp_family != AF_INET6 || 5710Sstevel@tonic-gate sin6->sin6_family != AF_INET6) { 5720Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 5730Sstevel@tonic-gate return; 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate /* No support for mapped addresses on raw sockets */ 5760Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 5770Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL); 5780Sstevel@tonic-gate return; 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate v6dst = sin6->sin6_addr; 5810Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV6_VERSION); 5820Sstevel@tonic-gate icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len; 5830Sstevel@tonic-gate flowinfo = sin6->sin6_flowinfo; 5840Sstevel@tonic-gate break; 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate if (icmp->icmp_ipversion == IPV4_VERSION) { 5870Sstevel@tonic-gate /* 5880Sstevel@tonic-gate * Interpret a zero destination to mean loopback. 5890Sstevel@tonic-gate * Update the T_CONN_REQ (sin/sin6) since it is used to 5900Sstevel@tonic-gate * generate the T_CONN_CON. 5910Sstevel@tonic-gate */ 5920Sstevel@tonic-gate if (v4dst == INADDR_ANY) { 5930Sstevel@tonic-gate v4dst = htonl(INADDR_LOOPBACK); 5940Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); 5950Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 5960Sstevel@tonic-gate sin->sin_addr.s_addr = v4dst; 5970Sstevel@tonic-gate } else { 5980Sstevel@tonic-gate sin6->sin6_addr = v6dst; 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate icmp->icmp_v6dst = v6dst; 6020Sstevel@tonic-gate icmp->icmp_flowinfo = 0; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate /* 6050Sstevel@tonic-gate * If the destination address is multicast and 6060Sstevel@tonic-gate * an outgoing multicast interface has been set, 6070Sstevel@tonic-gate * use the address of that interface as our 6080Sstevel@tonic-gate * source address if no source address has been set. 6090Sstevel@tonic-gate */ 6100Sstevel@tonic-gate if (V4_PART_OF_V6(icmp->icmp_v6src) == INADDR_ANY && 6110Sstevel@tonic-gate CLASSD(v4dst) && 6120Sstevel@tonic-gate icmp->icmp_multicast_if_addr != INADDR_ANY) { 6130Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(icmp->icmp_multicast_if_addr, 6140Sstevel@tonic-gate &icmp->icmp_v6src); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate } else { 6170Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV6_VERSION); 6180Sstevel@tonic-gate /* 6190Sstevel@tonic-gate * Interpret a zero destination to mean loopback. 6200Sstevel@tonic-gate * Update the T_CONN_REQ (sin/sin6) since it is used to 6210Sstevel@tonic-gate * generate the T_CONN_CON. 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) { 6240Sstevel@tonic-gate v6dst = ipv6_loopback; 6250Sstevel@tonic-gate sin6->sin6_addr = v6dst; 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate icmp->icmp_v6dst = v6dst; 6280Sstevel@tonic-gate icmp->icmp_flowinfo = flowinfo; 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * If the destination address is multicast and 6310Sstevel@tonic-gate * an outgoing multicast interface has been set, 6320Sstevel@tonic-gate * then the ip bind logic will pick the correct source 6330Sstevel@tonic-gate * address (i.e. matching the outgoing multicast interface). 6340Sstevel@tonic-gate */ 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6375240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 6385240Snordmark if (icmp->icmp_state == TS_UNBND || icmp->icmp_pending_op != -1) { 6395240Snordmark rw_exit(&icmp->icmp_rwlock); 6405240Snordmark (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 6415240Snordmark "icmp_connect: bad state, %d", icmp->icmp_state); 6425240Snordmark icmp_err_ack(q, mp, TOUTSTATE, 0); 6435240Snordmark return; 6445240Snordmark } 6455240Snordmark icmp->icmp_pending_op = T_CONN_REQ; 6465240Snordmark 6475240Snordmark if (icmp->icmp_state == TS_DATA_XFER) { 6485240Snordmark /* Already connected - clear out state */ 6495240Snordmark icmp->icmp_v6src = icmp->icmp_bound_v6src; 6505240Snordmark icmp->icmp_state = TS_IDLE; 6515240Snordmark } 6525240Snordmark 6530Sstevel@tonic-gate /* 6540Sstevel@tonic-gate * Send down bind to IP to verify that there is a route 6550Sstevel@tonic-gate * and to determine the source address. 6560Sstevel@tonic-gate * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput. 6570Sstevel@tonic-gate */ 6580Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 6590Sstevel@tonic-gate mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa_conn_t), 6600Sstevel@tonic-gate sin->sin_port); 6610Sstevel@tonic-gate } else { 6620Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 6630Sstevel@tonic-gate mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa6_conn_t), 6640Sstevel@tonic-gate sin6->sin6_port); 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate if (mp1 == NULL) { 6675240Snordmark icmp->icmp_pending_op = -1; 6685240Snordmark rw_exit(&icmp->icmp_rwlock); 6690Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, ENOMEM); 6700Sstevel@tonic-gate return; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * We also have to send a connection confirmation to 6755240Snordmark * keep TLI happy. Prepare it for icmp_bind_result. 6760Sstevel@tonic-gate */ 6770Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 6780Sstevel@tonic-gate mp2 = mi_tpi_conn_con(NULL, (char *)sin, sizeof (*sin), NULL, 6790Sstevel@tonic-gate 0); 6800Sstevel@tonic-gate } else { 6810Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 6820Sstevel@tonic-gate mp2 = mi_tpi_conn_con(NULL, (char *)sin6, sizeof (*sin6), NULL, 6830Sstevel@tonic-gate 0); 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate if (mp2 == NULL) { 6860Sstevel@tonic-gate freemsg(mp1); 6875240Snordmark icmp->icmp_pending_op = -1; 6885240Snordmark rw_exit(&icmp->icmp_rwlock); 6890Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, ENOMEM); 6900Sstevel@tonic-gate return; 6910Sstevel@tonic-gate } 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate mp = mi_tpi_ok_ack_alloc(mp); 6940Sstevel@tonic-gate if (mp == NULL) { 6950Sstevel@tonic-gate /* Unable to reuse the T_CONN_REQ for the ack. */ 6960Sstevel@tonic-gate freemsg(mp2); 6975240Snordmark icmp->icmp_pending_op = -1; 6985240Snordmark rw_exit(&icmp->icmp_rwlock); 6990Sstevel@tonic-gate icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM); 7000Sstevel@tonic-gate return; 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate icmp->icmp_state = TS_DATA_XFER; 7045240Snordmark rw_exit(&icmp->icmp_rwlock); 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate /* Hang onto the T_OK_ACK and T_CONN_CON for later. */ 7070Sstevel@tonic-gate linkb(mp1, mp); 7080Sstevel@tonic-gate linkb(mp1, mp2); 7090Sstevel@tonic-gate 7105240Snordmark mblk_setcred(mp1, connp->conn_cred); 7115240Snordmark if (icmp->icmp_family == AF_INET) 7125240Snordmark mp1 = ip_bind_v4(q, mp1, connp); 7135240Snordmark else 7145240Snordmark mp1 = ip_bind_v6(q, mp1, connp, NULL); 7155240Snordmark 7165240Snordmark /* The above return NULL if the bind needs to be deferred */ 7175240Snordmark if (mp1 != NULL) 7185240Snordmark icmp_bind_result(connp, mp1); 7195240Snordmark else 7205240Snordmark CONN_INC_REF(connp); 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7235240Snordmark static void 7245240Snordmark icmp_close_free(conn_t *connp) 7250Sstevel@tonic-gate { 7265240Snordmark icmp_t *icmp = connp->conn_icmp; 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate /* If there are any options associated with the stream, free them. */ 7295315Snordmark if (icmp->icmp_ip_snd_options != NULL) { 7300Sstevel@tonic-gate mi_free((char *)icmp->icmp_ip_snd_options); 7315315Snordmark icmp->icmp_ip_snd_options = NULL; 7325330Snordmark icmp->icmp_ip_snd_options_len = 0; 7335315Snordmark } 7345315Snordmark 7355315Snordmark if (icmp->icmp_filter != NULL) { 7360Sstevel@tonic-gate kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t)); 7375315Snordmark icmp->icmp_filter = NULL; 7385315Snordmark } 7390Sstevel@tonic-gate /* Free memory associated with sticky options */ 7400Sstevel@tonic-gate if (icmp->icmp_sticky_hdrs_len != 0) { 7410Sstevel@tonic-gate kmem_free(icmp->icmp_sticky_hdrs, 7420Sstevel@tonic-gate icmp->icmp_sticky_hdrs_len); 7430Sstevel@tonic-gate icmp->icmp_sticky_hdrs = NULL; 7440Sstevel@tonic-gate icmp->icmp_sticky_hdrs_len = 0; 7450Sstevel@tonic-gate } 7461676Sjpk ip6_pkt_free(&icmp->icmp_sticky_ipp); 7475330Snordmark 7485330Snordmark /* 7495330Snordmark * Clear any fields which the kmem_cache constructor clears. 7505330Snordmark * Only icmp_connp needs to be preserved. 7515330Snordmark * TBD: We should make this more efficient to avoid clearing 7525330Snordmark * everything. 7535330Snordmark */ 7545330Snordmark ASSERT(icmp->icmp_connp == connp); 7555330Snordmark bzero(icmp, sizeof (icmp_t)); 7565330Snordmark icmp->icmp_connp = connp; 7575240Snordmark } 7585240Snordmark 7595240Snordmark static int 7605240Snordmark icmp_close(queue_t *q) 7615240Snordmark { 7625240Snordmark conn_t *connp = (conn_t *)q->q_ptr; 7635240Snordmark 7645240Snordmark ASSERT(connp != NULL && IPCL_IS_RAWIP(connp)); 7655240Snordmark 7665240Snordmark ip_quiesce_conn(connp); 7675240Snordmark 7685240Snordmark qprocsoff(connp->conn_rq); 7695240Snordmark 7705240Snordmark icmp_close_free(connp); 7715240Snordmark 7725240Snordmark /* 7735240Snordmark * Now we are truly single threaded on this stream, and can 7745240Snordmark * delete the things hanging off the connp, and finally the connp. 7755240Snordmark * We removed this connp from the fanout list, it cannot be 7765240Snordmark * accessed thru the fanouts, and we already waited for the 7775240Snordmark * conn_ref to drop to 0. We are already in close, so 7785240Snordmark * there cannot be any other thread from the top. qprocsoff 7795240Snordmark * has completed, and service has completed or won't run in 7805240Snordmark * future. 7815240Snordmark */ 7825240Snordmark ASSERT(connp->conn_ref == 1); 7835240Snordmark 7845815Sgt145670 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 7855240Snordmark 7865240Snordmark connp->conn_ref--; 7875240Snordmark ipcl_conn_destroy(connp); 7885240Snordmark 7895240Snordmark q->q_ptr = WR(q)->q_ptr = NULL; 7905240Snordmark return (0); 7910Sstevel@tonic-gate } 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate /* 7940Sstevel@tonic-gate * This routine handles each T_DISCON_REQ message passed to icmp 7950Sstevel@tonic-gate * as an indicating that ICMP is no longer connected. This results 7960Sstevel@tonic-gate * in sending a T_BIND_REQ to IP to restore the binding to just 7970Sstevel@tonic-gate * the local address. 7980Sstevel@tonic-gate * 7990Sstevel@tonic-gate * This routine sends down a T_BIND_REQ to IP with the following mblks: 8000Sstevel@tonic-gate * T_BIND_REQ - specifying just the local address. 8010Sstevel@tonic-gate * T_OK_ACK - for the T_DISCON_REQ 8020Sstevel@tonic-gate * 8035240Snordmark * The disconnect completes in icmp_bind_result. 8040Sstevel@tonic-gate * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user. 8055240Snordmark * Should icmp_bind_result receive T_ERROR_ACK for the T_BIND_REQ it will 8065240Snordmark * convert it to an error ack for the appropriate primitive. 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate static void 8090Sstevel@tonic-gate icmp_disconnect(queue_t *q, mblk_t *mp) 8100Sstevel@tonic-gate { 8110Sstevel@tonic-gate icmp_t *icmp; 8120Sstevel@tonic-gate mblk_t *mp1; 8135240Snordmark conn_t *connp = Q_TO_CONN(q); 8145240Snordmark 8155240Snordmark icmp = connp->conn_icmp; 8165240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 8175240Snordmark if (icmp->icmp_state != TS_DATA_XFER || icmp->icmp_pending_op != -1) { 8185240Snordmark rw_exit(&icmp->icmp_rwlock); 8190Sstevel@tonic-gate (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 8200Sstevel@tonic-gate "icmp_disconnect: bad state, %d", icmp->icmp_state); 8210Sstevel@tonic-gate icmp_err_ack(q, mp, TOUTSTATE, 0); 8220Sstevel@tonic-gate return; 8230Sstevel@tonic-gate } 8245240Snordmark icmp->icmp_pending_op = T_DISCON_REQ; 8250Sstevel@tonic-gate icmp->icmp_v6src = icmp->icmp_bound_v6src; 8260Sstevel@tonic-gate icmp->icmp_state = TS_IDLE; 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate /* 8290Sstevel@tonic-gate * Send down bind to IP to remove the full binding and revert 8300Sstevel@tonic-gate * to the local address binding. 8310Sstevel@tonic-gate */ 8320Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 8330Sstevel@tonic-gate mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0); 8340Sstevel@tonic-gate } else { 8350Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 8360Sstevel@tonic-gate mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0); 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate if (mp1 == NULL) { 8395240Snordmark icmp->icmp_pending_op = -1; 8405240Snordmark rw_exit(&icmp->icmp_rwlock); 8410Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, ENOMEM); 8420Sstevel@tonic-gate return; 8430Sstevel@tonic-gate } 8440Sstevel@tonic-gate mp = mi_tpi_ok_ack_alloc(mp); 8450Sstevel@tonic-gate if (mp == NULL) { 8460Sstevel@tonic-gate /* Unable to reuse the T_DISCON_REQ for the ack. */ 8475240Snordmark icmp->icmp_pending_op = -1; 8485240Snordmark rw_exit(&icmp->icmp_rwlock); 8490Sstevel@tonic-gate icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM); 8500Sstevel@tonic-gate return; 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate if (icmp->icmp_family == AF_INET6) { 8540Sstevel@tonic-gate int error; 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate /* Rebuild the header template */ 8575240Snordmark error = icmp_build_hdrs(icmp); 8580Sstevel@tonic-gate if (error != 0) { 8595240Snordmark icmp->icmp_pending_op = -1; 8605240Snordmark rw_exit(&icmp->icmp_rwlock); 8610Sstevel@tonic-gate icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error); 8620Sstevel@tonic-gate freemsg(mp1); 8630Sstevel@tonic-gate return; 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate } 8665240Snordmark 8675240Snordmark rw_exit(&icmp->icmp_rwlock); 8685240Snordmark /* Append the T_OK_ACK to the T_BIND_REQ for icmp_bind_result */ 8690Sstevel@tonic-gate linkb(mp1, mp); 8705240Snordmark 8715240Snordmark if (icmp->icmp_family == AF_INET6) 8725240Snordmark mp1 = ip_bind_v6(q, mp1, connp, NULL); 8735240Snordmark else 8745240Snordmark mp1 = ip_bind_v4(q, mp1, connp); 8755240Snordmark 8765240Snordmark /* The above return NULL if the bind needs to be deferred */ 8775240Snordmark if (mp1 != NULL) 8785240Snordmark icmp_bind_result(connp, mp1); 8795240Snordmark else 8805240Snordmark CONN_INC_REF(connp); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate /* This routine creates a T_ERROR_ACK message and passes it upstream. */ 8840Sstevel@tonic-gate static void 8850Sstevel@tonic-gate icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error) 8860Sstevel@tonic-gate { 8870Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 8880Sstevel@tonic-gate qreply(q, mp); 8890Sstevel@tonic-gate } 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate /* Shorthand to generate and send TPI error acks to our client */ 8920Sstevel@tonic-gate static void 8930Sstevel@tonic-gate icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive, 8940Sstevel@tonic-gate t_scalar_t t_error, int sys_error) 8950Sstevel@tonic-gate { 8960Sstevel@tonic-gate struct T_error_ack *teackp; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 8990Sstevel@tonic-gate M_PCPROTO, T_ERROR_ACK)) != NULL) { 9000Sstevel@tonic-gate teackp = (struct T_error_ack *)mp->b_rptr; 9010Sstevel@tonic-gate teackp->ERROR_prim = primitive; 9020Sstevel@tonic-gate teackp->TLI_error = t_error; 9030Sstevel@tonic-gate teackp->UNIX_error = sys_error; 9040Sstevel@tonic-gate qreply(q, mp); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate /* 9095240Snordmark * icmp_icmp_error is called by icmp_input to process ICMP 9100Sstevel@tonic-gate * messages passed up by IP. 9110Sstevel@tonic-gate * Generates the appropriate T_UDERROR_IND for permanent 9120Sstevel@tonic-gate * (non-transient) errors. 9130Sstevel@tonic-gate * Assumes that IP has pulled up everything up to and including 9140Sstevel@tonic-gate * the ICMP header. 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate static void 9170Sstevel@tonic-gate icmp_icmp_error(queue_t *q, mblk_t *mp) 9180Sstevel@tonic-gate { 9190Sstevel@tonic-gate icmph_t *icmph; 9200Sstevel@tonic-gate ipha_t *ipha; 9210Sstevel@tonic-gate int iph_hdr_length; 9220Sstevel@tonic-gate sin_t sin; 9230Sstevel@tonic-gate sin6_t sin6; 9240Sstevel@tonic-gate mblk_t *mp1; 9250Sstevel@tonic-gate int error = 0; 9265240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate ipha = (ipha_t *)mp->b_rptr; 9290Sstevel@tonic-gate 9305240Snordmark ASSERT(OK_32PTR(mp->b_rptr)); 9315240Snordmark 9320Sstevel@tonic-gate if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) { 9330Sstevel@tonic-gate ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION); 9340Sstevel@tonic-gate icmp_icmp_error_ipv6(q, mp); 9350Sstevel@tonic-gate return; 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 9380Sstevel@tonic-gate 9395240Snordmark /* Skip past the outer IP and ICMP headers */ 9400Sstevel@tonic-gate iph_hdr_length = IPH_HDR_LENGTH(ipha); 9410Sstevel@tonic-gate icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]); 9420Sstevel@tonic-gate ipha = (ipha_t *)&icmph[1]; 9430Sstevel@tonic-gate iph_hdr_length = IPH_HDR_LENGTH(ipha); 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate switch (icmph->icmph_type) { 9460Sstevel@tonic-gate case ICMP_DEST_UNREACHABLE: 9470Sstevel@tonic-gate switch (icmph->icmph_code) { 9480Sstevel@tonic-gate case ICMP_FRAGMENTATION_NEEDED: 9490Sstevel@tonic-gate /* 9500Sstevel@tonic-gate * IP has already adjusted the path MTU. 9510Sstevel@tonic-gate */ 9520Sstevel@tonic-gate break; 9530Sstevel@tonic-gate case ICMP_PORT_UNREACHABLE: 9540Sstevel@tonic-gate case ICMP_PROTOCOL_UNREACHABLE: 9550Sstevel@tonic-gate error = ECONNREFUSED; 9560Sstevel@tonic-gate break; 9570Sstevel@tonic-gate default: 9580Sstevel@tonic-gate /* Transient errors */ 9590Sstevel@tonic-gate break; 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate break; 9620Sstevel@tonic-gate default: 9630Sstevel@tonic-gate /* Transient errors */ 9640Sstevel@tonic-gate break; 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate if (error == 0) { 9670Sstevel@tonic-gate freemsg(mp); 9680Sstevel@tonic-gate return; 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate 9715240Snordmark /* 9725240Snordmark * Deliver T_UDERROR_IND when the application has asked for it. 9735240Snordmark * The socket layer enables this automatically when connected. 9745240Snordmark */ 9755240Snordmark if (!icmp->icmp_dgram_errind) { 9765240Snordmark freemsg(mp); 9775240Snordmark return; 9785240Snordmark } 9795240Snordmark 9800Sstevel@tonic-gate switch (icmp->icmp_family) { 9810Sstevel@tonic-gate case AF_INET: 9820Sstevel@tonic-gate sin = sin_null; 9830Sstevel@tonic-gate sin.sin_family = AF_INET; 9840Sstevel@tonic-gate sin.sin_addr.s_addr = ipha->ipha_dst; 9850Sstevel@tonic-gate mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0, 9860Sstevel@tonic-gate error); 9870Sstevel@tonic-gate break; 9880Sstevel@tonic-gate case AF_INET6: 9890Sstevel@tonic-gate sin6 = sin6_null; 9900Sstevel@tonic-gate sin6.sin6_family = AF_INET6; 9910Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr); 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), 9940Sstevel@tonic-gate NULL, 0, error); 9950Sstevel@tonic-gate break; 9960Sstevel@tonic-gate } 9970Sstevel@tonic-gate if (mp1) 9980Sstevel@tonic-gate putnext(q, mp1); 9990Sstevel@tonic-gate freemsg(mp); 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate 10020Sstevel@tonic-gate /* 10030Sstevel@tonic-gate * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6 10040Sstevel@tonic-gate * for IPv6 packets. 10050Sstevel@tonic-gate * Send permanent (non-transient) errors upstream. 10060Sstevel@tonic-gate * Assumes that IP has pulled up all the extension headers as well 10070Sstevel@tonic-gate * as the ICMPv6 header. 10080Sstevel@tonic-gate */ 10090Sstevel@tonic-gate static void 10100Sstevel@tonic-gate icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp) 10110Sstevel@tonic-gate { 10120Sstevel@tonic-gate icmp6_t *icmp6; 10130Sstevel@tonic-gate ip6_t *ip6h, *outer_ip6h; 10140Sstevel@tonic-gate uint16_t iph_hdr_length; 10150Sstevel@tonic-gate uint8_t *nexthdrp; 10160Sstevel@tonic-gate sin6_t sin6; 10170Sstevel@tonic-gate mblk_t *mp1; 10180Sstevel@tonic-gate int error = 0; 10195240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate outer_ip6h = (ip6_t *)mp->b_rptr; 10220Sstevel@tonic-gate if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6) 10230Sstevel@tonic-gate iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h); 10240Sstevel@tonic-gate else 10250Sstevel@tonic-gate iph_hdr_length = IPV6_HDR_LEN; 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 10280Sstevel@tonic-gate ip6h = (ip6_t *)&icmp6[1]; 10290Sstevel@tonic-gate if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) { 10300Sstevel@tonic-gate freemsg(mp); 10310Sstevel@tonic-gate return; 10320Sstevel@tonic-gate } 10335240Snordmark 10340Sstevel@tonic-gate switch (icmp6->icmp6_type) { 10350Sstevel@tonic-gate case ICMP6_DST_UNREACH: 10360Sstevel@tonic-gate switch (icmp6->icmp6_code) { 10370Sstevel@tonic-gate case ICMP6_DST_UNREACH_NOPORT: 10380Sstevel@tonic-gate error = ECONNREFUSED; 10390Sstevel@tonic-gate break; 10400Sstevel@tonic-gate case ICMP6_DST_UNREACH_ADMIN: 10410Sstevel@tonic-gate case ICMP6_DST_UNREACH_NOROUTE: 10420Sstevel@tonic-gate case ICMP6_DST_UNREACH_BEYONDSCOPE: 10430Sstevel@tonic-gate case ICMP6_DST_UNREACH_ADDR: 10440Sstevel@tonic-gate /* Transient errors */ 10450Sstevel@tonic-gate break; 10460Sstevel@tonic-gate default: 10470Sstevel@tonic-gate break; 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate break; 10500Sstevel@tonic-gate case ICMP6_PACKET_TOO_BIG: { 10510Sstevel@tonic-gate struct T_unitdata_ind *tudi; 10520Sstevel@tonic-gate struct T_opthdr *toh; 10530Sstevel@tonic-gate size_t udi_size; 10540Sstevel@tonic-gate mblk_t *newmp; 10550Sstevel@tonic-gate t_scalar_t opt_length = sizeof (struct T_opthdr) + 10560Sstevel@tonic-gate sizeof (struct ip6_mtuinfo); 10570Sstevel@tonic-gate sin6_t *sin6; 10580Sstevel@tonic-gate struct ip6_mtuinfo *mtuinfo; 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate /* 10610Sstevel@tonic-gate * If the application has requested to receive path mtu 10620Sstevel@tonic-gate * information, send up an empty message containing an 10630Sstevel@tonic-gate * IPV6_PATHMTU ancillary data item. 10640Sstevel@tonic-gate */ 10650Sstevel@tonic-gate if (!icmp->icmp_ipv6_recvpathmtu) 10660Sstevel@tonic-gate break; 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) + 10690Sstevel@tonic-gate opt_length; 10700Sstevel@tonic-gate if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) { 10715240Snordmark BUMP_MIB(&icmp->icmp_is->is_rawip_mib, rawipInErrors); 10720Sstevel@tonic-gate break; 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate /* 10760Sstevel@tonic-gate * newmp->b_cont is left to NULL on purpose. This is an 10770Sstevel@tonic-gate * empty message containing only ancillary data. 10780Sstevel@tonic-gate */ 10790Sstevel@tonic-gate newmp->b_datap->db_type = M_PROTO; 10800Sstevel@tonic-gate tudi = (struct T_unitdata_ind *)newmp->b_rptr; 10810Sstevel@tonic-gate newmp->b_wptr = (uchar_t *)tudi + udi_size; 10820Sstevel@tonic-gate tudi->PRIM_type = T_UNITDATA_IND; 10830Sstevel@tonic-gate tudi->SRC_length = sizeof (sin6_t); 10840Sstevel@tonic-gate tudi->SRC_offset = sizeof (struct T_unitdata_ind); 10850Sstevel@tonic-gate tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t); 10860Sstevel@tonic-gate tudi->OPT_length = opt_length; 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate sin6 = (sin6_t *)&tudi[1]; 10890Sstevel@tonic-gate bzero(sin6, sizeof (sin6_t)); 10900Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 10910Sstevel@tonic-gate sin6->sin6_addr = icmp->icmp_v6dst; 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate toh = (struct T_opthdr *)&sin6[1]; 10940Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 10950Sstevel@tonic-gate toh->name = IPV6_PATHMTU; 10960Sstevel@tonic-gate toh->len = opt_length; 10970Sstevel@tonic-gate toh->status = 0; 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate mtuinfo = (struct ip6_mtuinfo *)&toh[1]; 11000Sstevel@tonic-gate bzero(mtuinfo, sizeof (struct ip6_mtuinfo)); 11010Sstevel@tonic-gate mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11020Sstevel@tonic-gate mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst; 11030Sstevel@tonic-gate mtuinfo->ip6m_mtu = icmp6->icmp6_mtu; 11040Sstevel@tonic-gate /* 11050Sstevel@tonic-gate * We've consumed everything we need from the original 11060Sstevel@tonic-gate * message. Free it, then send our empty message. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate freemsg(mp); 11090Sstevel@tonic-gate putnext(q, newmp); 11100Sstevel@tonic-gate return; 11110Sstevel@tonic-gate } 11120Sstevel@tonic-gate case ICMP6_TIME_EXCEEDED: 11130Sstevel@tonic-gate /* Transient errors */ 11140Sstevel@tonic-gate break; 11150Sstevel@tonic-gate case ICMP6_PARAM_PROB: 11160Sstevel@tonic-gate /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 11170Sstevel@tonic-gate if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 11180Sstevel@tonic-gate (uchar_t *)ip6h + icmp6->icmp6_pptr == 11190Sstevel@tonic-gate (uchar_t *)nexthdrp) { 11200Sstevel@tonic-gate error = ECONNREFUSED; 11210Sstevel@tonic-gate break; 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate break; 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate if (error == 0) { 11260Sstevel@tonic-gate freemsg(mp); 11270Sstevel@tonic-gate return; 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate 11305240Snordmark /* 11315240Snordmark * Deliver T_UDERROR_IND when the application has asked for it. 11325240Snordmark * The socket layer enables this automatically when connected. 11335240Snordmark */ 11345240Snordmark if (!icmp->icmp_dgram_errind) { 11355240Snordmark freemsg(mp); 11365240Snordmark return; 11375240Snordmark } 11385240Snordmark 11390Sstevel@tonic-gate sin6 = sin6_null; 11400Sstevel@tonic-gate sin6.sin6_family = AF_INET6; 11410Sstevel@tonic-gate sin6.sin6_addr = ip6h->ip6_dst; 11420Sstevel@tonic-gate sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0, 11450Sstevel@tonic-gate error); 11460Sstevel@tonic-gate if (mp1) 11470Sstevel@tonic-gate putnext(q, mp1); 11480Sstevel@tonic-gate freemsg(mp); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate /* 11520Sstevel@tonic-gate * This routine responds to T_ADDR_REQ messages. It is called by icmp_wput. 11530Sstevel@tonic-gate * The local address is filled in if endpoint is bound. The remote address 11540Sstevel@tonic-gate * is filled in if remote address has been precified ("connected endpoint") 11550Sstevel@tonic-gate * (The concept of connected CLTS sockets is alien to published TPI 11560Sstevel@tonic-gate * but we support it anyway). 11570Sstevel@tonic-gate */ 11580Sstevel@tonic-gate static void 11590Sstevel@tonic-gate icmp_addr_req(queue_t *q, mblk_t *mp) 11600Sstevel@tonic-gate { 11615240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 11620Sstevel@tonic-gate mblk_t *ackmp; 11630Sstevel@tonic-gate struct T_addr_ack *taa; 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate /* Make it large enough for worst case */ 11660Sstevel@tonic-gate ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 11670Sstevel@tonic-gate 2 * sizeof (sin6_t), 1); 11680Sstevel@tonic-gate if (ackmp == NULL) { 11690Sstevel@tonic-gate icmp_err_ack(q, mp, TSYSERR, ENOMEM); 11700Sstevel@tonic-gate return; 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate taa = (struct T_addr_ack *)ackmp->b_rptr; 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate bzero(taa, sizeof (struct T_addr_ack)); 11750Sstevel@tonic-gate ackmp->b_wptr = (uchar_t *)&taa[1]; 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate taa->PRIM_type = T_ADDR_ACK; 11780Sstevel@tonic-gate ackmp->b_datap->db_type = M_PCPROTO; 11795240Snordmark rw_enter(&icmp->icmp_rwlock, RW_READER); 11800Sstevel@tonic-gate /* 11810Sstevel@tonic-gate * Note: Following code assumes 32 bit alignment of basic 11820Sstevel@tonic-gate * data structures like sin_t and struct T_addr_ack. 11830Sstevel@tonic-gate */ 11840Sstevel@tonic-gate if (icmp->icmp_state != TS_UNBND) { 11850Sstevel@tonic-gate /* 11860Sstevel@tonic-gate * Fill in local address 11870Sstevel@tonic-gate */ 11880Sstevel@tonic-gate taa->LOCADDR_offset = sizeof (*taa); 11890Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 11900Sstevel@tonic-gate sin_t *sin; 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate taa->LOCADDR_length = sizeof (sin_t); 11930Sstevel@tonic-gate sin = (sin_t *)&taa[1]; 11940Sstevel@tonic-gate /* Fill zeroes and then intialize non-zero fields */ 11950Sstevel@tonic-gate *sin = sin_null; 11960Sstevel@tonic-gate sin->sin_family = AF_INET; 11970Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) && 11980Sstevel@tonic-gate !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) { 11990Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, 12000Sstevel@tonic-gate sin->sin_addr.s_addr); 12010Sstevel@tonic-gate } else { 12020Sstevel@tonic-gate /* 12030Sstevel@tonic-gate * INADDR_ANY 12040Sstevel@tonic-gate * icmp_v6src is not set, we might be bound to 12050Sstevel@tonic-gate * broadcast/multicast. Use icmp_bound_v6src as 12060Sstevel@tonic-gate * local address instead (that could 12070Sstevel@tonic-gate * also still be INADDR_ANY) 12080Sstevel@tonic-gate */ 12090Sstevel@tonic-gate IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src, 12100Sstevel@tonic-gate sin->sin_addr.s_addr); 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate ackmp->b_wptr = (uchar_t *)&sin[1]; 12130Sstevel@tonic-gate } else { 12140Sstevel@tonic-gate sin6_t *sin6; 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 12170Sstevel@tonic-gate taa->LOCADDR_length = sizeof (sin6_t); 12180Sstevel@tonic-gate sin6 = (sin6_t *)&taa[1]; 12190Sstevel@tonic-gate /* Fill zeroes and then intialize non-zero fields */ 12200Sstevel@tonic-gate *sin6 = sin6_null; 12210Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 12220Sstevel@tonic-gate if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) { 12230Sstevel@tonic-gate sin6->sin6_addr = icmp->icmp_v6src; 12240Sstevel@tonic-gate } else { 12250Sstevel@tonic-gate /* 12260Sstevel@tonic-gate * UNSPECIFIED 12270Sstevel@tonic-gate * icmp_v6src is not set, we might be bound to 12280Sstevel@tonic-gate * broadcast/multicast. Use icmp_bound_v6src as 12290Sstevel@tonic-gate * local address instead (that could 12300Sstevel@tonic-gate * also still be UNSPECIFIED) 12310Sstevel@tonic-gate */ 12320Sstevel@tonic-gate sin6->sin6_addr = icmp->icmp_bound_v6src; 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate ackmp->b_wptr = (uchar_t *)&sin6[1]; 12350Sstevel@tonic-gate } 12360Sstevel@tonic-gate } 12375240Snordmark rw_exit(&icmp->icmp_rwlock); 12380Sstevel@tonic-gate ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim); 12390Sstevel@tonic-gate qreply(q, ackmp); 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate 12420Sstevel@tonic-gate static void 12430Sstevel@tonic-gate icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp) 12440Sstevel@tonic-gate { 12450Sstevel@tonic-gate *tap = icmp_g_t_info_ack; 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate if (icmp->icmp_family == AF_INET6) 12480Sstevel@tonic-gate tap->ADDR_size = sizeof (sin6_t); 12490Sstevel@tonic-gate else 12500Sstevel@tonic-gate tap->ADDR_size = sizeof (sin_t); 12510Sstevel@tonic-gate tap->CURRENT_state = icmp->icmp_state; 12520Sstevel@tonic-gate tap->OPT_size = icmp_max_optsize; 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by 12570Sstevel@tonic-gate * icmp_wput. Much of the T_CAPABILITY_ACK information is copied from 12580Sstevel@tonic-gate * icmp_g_t_info_ack. The current state of the stream is copied from 12590Sstevel@tonic-gate * icmp_state. 12600Sstevel@tonic-gate */ 12610Sstevel@tonic-gate static void 12620Sstevel@tonic-gate icmp_capability_req(queue_t *q, mblk_t *mp) 12630Sstevel@tonic-gate { 12645240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 12650Sstevel@tonic-gate t_uscalar_t cap_bits1; 12660Sstevel@tonic-gate struct T_capability_ack *tcap; 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 12715240Snordmark mp->b_datap->db_type, T_CAPABILITY_ACK); 12720Sstevel@tonic-gate if (!mp) 12730Sstevel@tonic-gate return; 12740Sstevel@tonic-gate 12750Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr; 12760Sstevel@tonic-gate tcap->CAP_bits1 = 0; 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) { 12790Sstevel@tonic-gate icmp_copy_info(&tcap->INFO_ack, icmp); 12800Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO; 12810Sstevel@tonic-gate } 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate qreply(q, mp); 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate /* 12870Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by icmp_wput. 12880Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack. 12890Sstevel@tonic-gate * The current state of the stream is copied from icmp_state. 12900Sstevel@tonic-gate */ 12910Sstevel@tonic-gate static void 12920Sstevel@tonic-gate icmp_info_req(queue_t *q, mblk_t *mp) 12930Sstevel@tonic-gate { 12945240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 12970Sstevel@tonic-gate T_INFO_ACK); 12980Sstevel@tonic-gate if (!mp) 12990Sstevel@tonic-gate return; 13000Sstevel@tonic-gate icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp); 13010Sstevel@tonic-gate qreply(q, mp); 13020Sstevel@tonic-gate } 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate /* 13050Sstevel@tonic-gate * IP recognizes seven kinds of bind requests: 13060Sstevel@tonic-gate * 13070Sstevel@tonic-gate * - A zero-length address binds only to the protocol number. 13080Sstevel@tonic-gate * 13090Sstevel@tonic-gate * - A 4-byte address is treated as a request to 13100Sstevel@tonic-gate * validate that the address is a valid local IPv4 13110Sstevel@tonic-gate * address, appropriate for an application to bind to. 13120Sstevel@tonic-gate * IP does the verification, but does not make any note 13130Sstevel@tonic-gate * of the address at this time. 13140Sstevel@tonic-gate * 13150Sstevel@tonic-gate * - A 16-byte address contains is treated as a request 13160Sstevel@tonic-gate * to validate a local IPv6 address, as the 4-byte 13170Sstevel@tonic-gate * address case above. 13180Sstevel@tonic-gate * 13190Sstevel@tonic-gate * - A 16-byte sockaddr_in to validate the local IPv4 address and also 13200Sstevel@tonic-gate * use it for the inbound fanout of packets. 13210Sstevel@tonic-gate * 13220Sstevel@tonic-gate * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 13230Sstevel@tonic-gate * use it for the inbound fanout of packets. 13240Sstevel@tonic-gate * 13250Sstevel@tonic-gate * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 13260Sstevel@tonic-gate * information consisting of local and remote addresses 13270Sstevel@tonic-gate * and ports (unused for raw sockets). In this case, the addresses are both 13280Sstevel@tonic-gate * validated as appropriate for this operation, and, if 13290Sstevel@tonic-gate * so, the information is retained for use in the 13300Sstevel@tonic-gate * inbound fanout. 13310Sstevel@tonic-gate * 13320Sstevel@tonic-gate * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 13330Sstevel@tonic-gate * fanout information, like the 12-byte case above. 13340Sstevel@tonic-gate * 13350Sstevel@tonic-gate * IP will also fill in the IRE request mblk with information 13360Sstevel@tonic-gate * regarding our peer. In all cases, we notify IP of our protocol 13370Sstevel@tonic-gate * type by appending a single protocol byte to the bind request. 13380Sstevel@tonic-gate */ 13390Sstevel@tonic-gate static mblk_t * 13400Sstevel@tonic-gate icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length, 13410Sstevel@tonic-gate in_port_t fport) 13420Sstevel@tonic-gate { 13430Sstevel@tonic-gate char *cp; 13440Sstevel@tonic-gate mblk_t *mp; 13450Sstevel@tonic-gate struct T_bind_req *tbr; 13460Sstevel@tonic-gate ipa_conn_t *ac; 13470Sstevel@tonic-gate ipa6_conn_t *ac6; 13480Sstevel@tonic-gate sin_t *sin; 13490Sstevel@tonic-gate sin6_t *sin6; 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 13525240Snordmark ASSERT(RW_LOCK_HELD(&icmp->icmp_rwlock)); 13530Sstevel@tonic-gate mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 13540Sstevel@tonic-gate if (mp == NULL) 13550Sstevel@tonic-gate return (NULL); 13560Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 13570Sstevel@tonic-gate tbr = (struct T_bind_req *)mp->b_rptr; 13580Sstevel@tonic-gate tbr->PRIM_type = bind_prim; 13590Sstevel@tonic-gate tbr->ADDR_offset = sizeof (*tbr); 13600Sstevel@tonic-gate tbr->CONIND_number = 0; 13610Sstevel@tonic-gate tbr->ADDR_length = addr_length; 13620Sstevel@tonic-gate cp = (char *)&tbr[1]; 13630Sstevel@tonic-gate switch (addr_length) { 13640Sstevel@tonic-gate case sizeof (ipa_conn_t): 13650Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET); 13660Sstevel@tonic-gate /* Append a request for an IRE */ 13670Sstevel@tonic-gate mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 13680Sstevel@tonic-gate if (mp->b_cont == NULL) { 13690Sstevel@tonic-gate freemsg(mp); 13700Sstevel@tonic-gate return (NULL); 13710Sstevel@tonic-gate } 13720Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (ire_t); 13730Sstevel@tonic-gate mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate /* cp known to be 32 bit aligned */ 13760Sstevel@tonic-gate ac = (ipa_conn_t *)cp; 13770Sstevel@tonic-gate ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src); 13780Sstevel@tonic-gate ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst); 13790Sstevel@tonic-gate ac->ac_fport = fport; 13800Sstevel@tonic-gate ac->ac_lport = 0; 13810Sstevel@tonic-gate break; 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate case sizeof (ipa6_conn_t): 13840Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 13850Sstevel@tonic-gate /* Append a request for an IRE */ 13860Sstevel@tonic-gate mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 13870Sstevel@tonic-gate if (mp->b_cont == NULL) { 13880Sstevel@tonic-gate freemsg(mp); 13890Sstevel@tonic-gate return (NULL); 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (ire_t); 13920Sstevel@tonic-gate mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate /* cp known to be 32 bit aligned */ 13950Sstevel@tonic-gate ac6 = (ipa6_conn_t *)cp; 13960Sstevel@tonic-gate ac6->ac6_laddr = icmp->icmp_v6src; 13970Sstevel@tonic-gate ac6->ac6_faddr = icmp->icmp_v6dst; 13980Sstevel@tonic-gate ac6->ac6_fport = fport; 13990Sstevel@tonic-gate ac6->ac6_lport = 0; 14000Sstevel@tonic-gate break; 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate case sizeof (sin_t): 14030Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET); 14040Sstevel@tonic-gate /* Append a request for an IRE */ 14050Sstevel@tonic-gate mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 14060Sstevel@tonic-gate if (!mp->b_cont) { 14070Sstevel@tonic-gate freemsg(mp); 14080Sstevel@tonic-gate return (NULL); 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (ire_t); 14110Sstevel@tonic-gate mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 14120Sstevel@tonic-gate 14130Sstevel@tonic-gate sin = (sin_t *)cp; 14140Sstevel@tonic-gate *sin = sin_null; 14150Sstevel@tonic-gate sin->sin_family = AF_INET; 14160Sstevel@tonic-gate sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src); 14170Sstevel@tonic-gate break; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate case sizeof (sin6_t): 14200Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 14210Sstevel@tonic-gate /* Append a request for an IRE */ 14220Sstevel@tonic-gate mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 14230Sstevel@tonic-gate if (!mp->b_cont) { 14240Sstevel@tonic-gate freemsg(mp); 14250Sstevel@tonic-gate return (NULL); 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (ire_t); 14280Sstevel@tonic-gate mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 14290Sstevel@tonic-gate 14300Sstevel@tonic-gate sin6 = (sin6_t *)cp; 14310Sstevel@tonic-gate *sin6 = sin6_null; 14320Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 14330Sstevel@tonic-gate sin6->sin6_addr = icmp->icmp_bound_v6src; 14340Sstevel@tonic-gate break; 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate /* Add protocol number to end */ 14370Sstevel@tonic-gate cp[addr_length] = icmp->icmp_proto; 14380Sstevel@tonic-gate mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 14390Sstevel@tonic-gate return (mp); 14400Sstevel@tonic-gate } 14410Sstevel@tonic-gate 14425240Snordmark /* For /dev/icmp aka AF_INET open */ 14435240Snordmark static int 14445240Snordmark icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 14451676Sjpk { 14465240Snordmark return (icmp_open(q, devp, flag, sflag, credp, B_FALSE)); 14475240Snordmark } 14485240Snordmark 14495240Snordmark /* For /dev/icmp6 aka AF_INET6 open */ 14505240Snordmark static int 14515240Snordmark icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 14525240Snordmark { 14535240Snordmark return (icmp_open(q, devp, flag, sflag, credp, B_TRUE)); 14541676Sjpk } 14551676Sjpk 14560Sstevel@tonic-gate /* 14570Sstevel@tonic-gate * This is the open routine for icmp. It allocates a icmp_t structure for 14580Sstevel@tonic-gate * the stream and, on the first open of the module, creates an ND table. 14590Sstevel@tonic-gate */ 14605240Snordmark /*ARGSUSED2*/ 14610Sstevel@tonic-gate static int 14625240Snordmark icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 14635240Snordmark boolean_t isv6) 14640Sstevel@tonic-gate { 14650Sstevel@tonic-gate int err; 14660Sstevel@tonic-gate icmp_t *icmp; 14675240Snordmark conn_t *connp; 14685240Snordmark dev_t conn_dev; 14695240Snordmark zoneid_t zoneid; 14703448Sdh155122 netstack_t *ns; 14713448Sdh155122 icmp_stack_t *is; 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate /* If the stream is already open, return immediately. */ 14740Sstevel@tonic-gate if (q->q_ptr != NULL) 14750Sstevel@tonic-gate return (0); 14760Sstevel@tonic-gate 14775240Snordmark if (sflag == MODOPEN) 14780Sstevel@tonic-gate return (EINVAL); 14790Sstevel@tonic-gate 14803448Sdh155122 ns = netstack_find_by_cred(credp); 14813448Sdh155122 ASSERT(ns != NULL); 14823448Sdh155122 is = ns->netstack_icmp; 14833448Sdh155122 ASSERT(is != NULL); 14843448Sdh155122 14853448Sdh155122 /* 14863448Sdh155122 * For exclusive stacks we set the zoneid to zero 14873448Sdh155122 * to make ICMP operate as if in the global zone. 14883448Sdh155122 */ 14895240Snordmark if (ns->netstack_stackid != GLOBAL_NETSTACKID) 14903448Sdh155122 zoneid = GLOBAL_ZONEID; 14913448Sdh155122 else 14923448Sdh155122 zoneid = crgetzoneid(credp); 14933448Sdh155122 14945815Sgt145670 /* 14955815Sgt145670 * Since ICMP is not used so heavily, allocating from the small 14965815Sgt145670 * arena should be sufficient. 14975815Sgt145670 */ 14985815Sgt145670 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 14995240Snordmark netstack_rele(ns); 15005240Snordmark return (EBUSY); 15013448Sdh155122 } 15025240Snordmark *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 15035240Snordmark 15045240Snordmark connp = ipcl_conn_create(IPCL_RAWIPCONN, KM_SLEEP, ns); 15055240Snordmark connp->conn_dev = conn_dev; 15065815Sgt145670 connp->conn_minor_arena = ip_minor_arena_sa; 15075240Snordmark icmp = connp->conn_icmp; 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate /* 15105240Snordmark * ipcl_conn_create did a netstack_hold. Undo the hold that was 15115240Snordmark * done by netstack_find_by_cred() 15125240Snordmark */ 15135240Snordmark netstack_rele(ns); 15145240Snordmark 15155240Snordmark /* 15165240Snordmark * Initialize the icmp_t structure for this stream. 15170Sstevel@tonic-gate */ 15185240Snordmark q->q_ptr = connp; 15195240Snordmark WR(q)->q_ptr = connp; 15205240Snordmark connp->conn_rq = q; 15215240Snordmark connp->conn_wq = WR(q); 15225240Snordmark 15235240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 15245240Snordmark ASSERT(connp->conn_ulp == IPPROTO_ICMP); 15255240Snordmark ASSERT(connp->conn_icmp == icmp); 15265240Snordmark ASSERT(icmp->icmp_connp == connp); 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate /* Set the initial state of the stream and the privilege status. */ 15290Sstevel@tonic-gate icmp->icmp_state = TS_UNBND; 15305240Snordmark if (isv6) { 15310Sstevel@tonic-gate icmp->icmp_ipversion = IPV6_VERSION; 15320Sstevel@tonic-gate icmp->icmp_family = AF_INET6; 15335240Snordmark connp->conn_ulp = IPPROTO_ICMPV6; 15340Sstevel@tonic-gate /* May be changed by a SO_PROTOTYPE socket option. */ 15350Sstevel@tonic-gate icmp->icmp_proto = IPPROTO_ICMPV6; 15360Sstevel@tonic-gate icmp->icmp_checksum_off = 2; /* Offset for icmp6_cksum */ 15370Sstevel@tonic-gate icmp->icmp_max_hdr_len = IPV6_HDR_LEN; 15383448Sdh155122 icmp->icmp_ttl = (uint8_t)is->is_ipv6_hoplimit; 15395240Snordmark connp->conn_af_isv6 = B_TRUE; 15405240Snordmark connp->conn_flags |= IPCL_ISV6; 15410Sstevel@tonic-gate } else { 15420Sstevel@tonic-gate icmp->icmp_ipversion = IPV4_VERSION; 15430Sstevel@tonic-gate icmp->icmp_family = AF_INET; 15440Sstevel@tonic-gate /* May be changed by a SO_PROTOTYPE socket option. */ 15450Sstevel@tonic-gate icmp->icmp_proto = IPPROTO_ICMP; 15460Sstevel@tonic-gate icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH; 15473448Sdh155122 icmp->icmp_ttl = (uint8_t)is->is_ipv4_ttl; 15485240Snordmark connp->conn_af_isv6 = B_FALSE; 15495240Snordmark connp->conn_flags &= ~IPCL_ISV6; 15500Sstevel@tonic-gate } 15515240Snordmark icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 15525240Snordmark icmp->icmp_pending_op = -1; 15535240Snordmark connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 15545240Snordmark connp->conn_zoneid = zoneid; 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate /* 15575240Snordmark * If the caller has the process-wide flag set, then default to MAC 15585240Snordmark * exempt mode. This allows read-down to unlabeled hosts. 15590Sstevel@tonic-gate */ 15605240Snordmark if (getpflags(NET_MAC_AWARE, credp) != 0) 1561*6596Skp158701 connp->conn_mac_exempt = B_TRUE; 15625240Snordmark 15635240Snordmark connp->conn_ulp_labeled = is_system_labeled(); 15645240Snordmark 15655240Snordmark icmp->icmp_is = is; 15665240Snordmark 15675240Snordmark q->q_hiwat = is->is_recv_hiwat; 15683448Sdh155122 WR(q)->q_hiwat = is->is_xmit_hiwat; 15693448Sdh155122 WR(q)->q_lowat = is->is_xmit_lowat; 15705240Snordmark 15715240Snordmark connp->conn_recv = icmp_input; 15725240Snordmark crhold(credp); 15735240Snordmark connp->conn_cred = credp; 15745240Snordmark 15755240Snordmark mutex_enter(&connp->conn_lock); 15765240Snordmark connp->conn_state_flags &= ~CONN_INCIPIENT; 15775240Snordmark mutex_exit(&connp->conn_lock); 15785240Snordmark 15795240Snordmark qprocson(q); 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate if (icmp->icmp_family == AF_INET6) { 15820Sstevel@tonic-gate /* Build initial header template for transmit */ 15835240Snordmark if ((err = icmp_build_hdrs(icmp)) != 0) { 15845240Snordmark rw_exit(&icmp->icmp_rwlock); 15855240Snordmark qprocsoff(q); 15865240Snordmark ipcl_conn_destroy(connp); 15875240Snordmark return (err); 15885240Snordmark } 15890Sstevel@tonic-gate } 15905240Snordmark rw_exit(&icmp->icmp_rwlock); 15915240Snordmark 15920Sstevel@tonic-gate /* Set the Stream head write offset. */ 15933448Sdh155122 (void) mi_set_sth_wroff(q, 15943448Sdh155122 icmp->icmp_max_hdr_len + is->is_wroff_extra); 15950Sstevel@tonic-gate (void) mi_set_sth_hiwat(q, q->q_hiwat); 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate return (0); 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate /* 16010Sstevel@tonic-gate * Which ICMP options OK to set through T_UNITDATA_REQ... 16020Sstevel@tonic-gate */ 16030Sstevel@tonic-gate /* ARGSUSED */ 16040Sstevel@tonic-gate static boolean_t 16050Sstevel@tonic-gate icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name) 16060Sstevel@tonic-gate { 16070Sstevel@tonic-gate return (B_TRUE); 16080Sstevel@tonic-gate } 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate /* 16110Sstevel@tonic-gate * This routine gets default values of certain options whose default 16120Sstevel@tonic-gate * values are maintained by protcol specific code 16130Sstevel@tonic-gate */ 16140Sstevel@tonic-gate /* ARGSUSED */ 16150Sstevel@tonic-gate int 16160Sstevel@tonic-gate icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 16170Sstevel@tonic-gate { 16185240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 16193448Sdh155122 icmp_stack_t *is = icmp->icmp_is; 16200Sstevel@tonic-gate int *i1 = (int *)ptr; 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate switch (level) { 16230Sstevel@tonic-gate case IPPROTO_IP: 16240Sstevel@tonic-gate switch (name) { 16250Sstevel@tonic-gate case IP_MULTICAST_TTL: 16260Sstevel@tonic-gate *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 16270Sstevel@tonic-gate return (sizeof (uchar_t)); 16280Sstevel@tonic-gate case IP_MULTICAST_LOOP: 16290Sstevel@tonic-gate *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 16300Sstevel@tonic-gate return (sizeof (uchar_t)); 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate break; 16330Sstevel@tonic-gate case IPPROTO_IPV6: 16340Sstevel@tonic-gate switch (name) { 16350Sstevel@tonic-gate case IPV6_MULTICAST_HOPS: 16360Sstevel@tonic-gate *i1 = IP_DEFAULT_MULTICAST_TTL; 16370Sstevel@tonic-gate return (sizeof (int)); 16380Sstevel@tonic-gate case IPV6_MULTICAST_LOOP: 16390Sstevel@tonic-gate *i1 = IP_DEFAULT_MULTICAST_LOOP; 16400Sstevel@tonic-gate return (sizeof (int)); 16410Sstevel@tonic-gate case IPV6_UNICAST_HOPS: 16423448Sdh155122 *i1 = is->is_ipv6_hoplimit; 16430Sstevel@tonic-gate return (sizeof (int)); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate break; 16460Sstevel@tonic-gate case IPPROTO_ICMPV6: 16470Sstevel@tonic-gate switch (name) { 16480Sstevel@tonic-gate case ICMP6_FILTER: 16490Sstevel@tonic-gate /* Make it look like "pass all" */ 16500Sstevel@tonic-gate ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr); 16510Sstevel@tonic-gate return (sizeof (icmp6_filter_t)); 16520Sstevel@tonic-gate } 16530Sstevel@tonic-gate break; 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate return (-1); 16560Sstevel@tonic-gate } 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * This routine retrieves the current status of socket options. 16600Sstevel@tonic-gate * It returns the size of the option retrieved. 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate int 16635240Snordmark icmp_opt_get_locked(queue_t *q, int level, int name, uchar_t *ptr) 16640Sstevel@tonic-gate { 16655240Snordmark conn_t *connp = Q_TO_CONN(q); 16665240Snordmark icmp_t *icmp = connp->conn_icmp; 16675240Snordmark icmp_stack_t *is = icmp->icmp_is; 16680Sstevel@tonic-gate int *i1 = (int *)ptr; 16690Sstevel@tonic-gate ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp; 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate switch (level) { 16720Sstevel@tonic-gate case SOL_SOCKET: 16730Sstevel@tonic-gate switch (name) { 16740Sstevel@tonic-gate case SO_DEBUG: 16750Sstevel@tonic-gate *i1 = icmp->icmp_debug; 16760Sstevel@tonic-gate break; 16770Sstevel@tonic-gate case SO_TYPE: 16780Sstevel@tonic-gate *i1 = SOCK_RAW; 16790Sstevel@tonic-gate break; 16800Sstevel@tonic-gate case SO_PROTOTYPE: 16810Sstevel@tonic-gate *i1 = icmp->icmp_proto; 16820Sstevel@tonic-gate break; 16830Sstevel@tonic-gate case SO_REUSEADDR: 16840Sstevel@tonic-gate *i1 = icmp->icmp_reuseaddr; 16850Sstevel@tonic-gate break; 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate /* 16880Sstevel@tonic-gate * The following three items are available here, 16890Sstevel@tonic-gate * but are only meaningful to IP. 16900Sstevel@tonic-gate */ 16910Sstevel@tonic-gate case SO_DONTROUTE: 16920Sstevel@tonic-gate *i1 = icmp->icmp_dontroute; 16930Sstevel@tonic-gate break; 16940Sstevel@tonic-gate case SO_USELOOPBACK: 16950Sstevel@tonic-gate *i1 = icmp->icmp_useloopback; 16960Sstevel@tonic-gate break; 16970Sstevel@tonic-gate case SO_BROADCAST: 16980Sstevel@tonic-gate *i1 = icmp->icmp_broadcast; 16990Sstevel@tonic-gate break; 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate case SO_SNDBUF: 17020Sstevel@tonic-gate ASSERT(q->q_hiwat <= INT_MAX); 17030Sstevel@tonic-gate *i1 = (int)q->q_hiwat; 17040Sstevel@tonic-gate break; 17050Sstevel@tonic-gate case SO_RCVBUF: 17060Sstevel@tonic-gate ASSERT(RD(q)->q_hiwat <= INT_MAX); 17070Sstevel@tonic-gate *i1 = (int)RD(q)->q_hiwat; 17080Sstevel@tonic-gate break; 17090Sstevel@tonic-gate case SO_DGRAM_ERRIND: 17100Sstevel@tonic-gate *i1 = icmp->icmp_dgram_errind; 17110Sstevel@tonic-gate break; 17121673Sgt145670 case SO_TIMESTAMP: 17131673Sgt145670 *i1 = icmp->icmp_timestamp; 17141673Sgt145670 break; 17151676Sjpk case SO_MAC_EXEMPT: 1716*6596Skp158701 *i1 = connp->conn_mac_exempt; 17171676Sjpk break; 17183388Skcpoon case SO_DOMAIN: 17193388Skcpoon *i1 = icmp->icmp_family; 17203388Skcpoon break; 17213388Skcpoon 17220Sstevel@tonic-gate /* 17232263Ssommerfe * Following four not meaningful for icmp 17240Sstevel@tonic-gate * Action is same as "default" to which we fallthrough 17250Sstevel@tonic-gate * so we keep them in comments. 17260Sstevel@tonic-gate * case SO_LINGER: 17270Sstevel@tonic-gate * case SO_KEEPALIVE: 17280Sstevel@tonic-gate * case SO_OOBINLINE: 17292263Ssommerfe * case SO_ALLZONES: 17300Sstevel@tonic-gate */ 17310Sstevel@tonic-gate default: 17320Sstevel@tonic-gate return (-1); 17330Sstevel@tonic-gate } 17340Sstevel@tonic-gate break; 17350Sstevel@tonic-gate case IPPROTO_IP: 17360Sstevel@tonic-gate /* 17370Sstevel@tonic-gate * Only allow IPv4 option processing on IPv4 sockets. 17380Sstevel@tonic-gate */ 17390Sstevel@tonic-gate if (icmp->icmp_family != AF_INET) 17400Sstevel@tonic-gate return (-1); 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate switch (name) { 17430Sstevel@tonic-gate case IP_OPTIONS: 17440Sstevel@tonic-gate case T_IP_OPTIONS: 17450Sstevel@tonic-gate /* Options are passed up with each packet */ 17460Sstevel@tonic-gate return (0); 17470Sstevel@tonic-gate case IP_HDRINCL: 17480Sstevel@tonic-gate *i1 = (int)icmp->icmp_hdrincl; 17490Sstevel@tonic-gate break; 17500Sstevel@tonic-gate case IP_TOS: 17510Sstevel@tonic-gate case T_IP_TOS: 17520Sstevel@tonic-gate *i1 = (int)icmp->icmp_type_of_service; 17530Sstevel@tonic-gate break; 17540Sstevel@tonic-gate case IP_TTL: 17550Sstevel@tonic-gate *i1 = (int)icmp->icmp_ttl; 17560Sstevel@tonic-gate break; 17570Sstevel@tonic-gate case IP_MULTICAST_IF: 17580Sstevel@tonic-gate /* 0 address if not set */ 17590Sstevel@tonic-gate *(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr; 17600Sstevel@tonic-gate return (sizeof (ipaddr_t)); 17610Sstevel@tonic-gate case IP_MULTICAST_TTL: 17620Sstevel@tonic-gate *(uchar_t *)ptr = icmp->icmp_multicast_ttl; 17630Sstevel@tonic-gate return (sizeof (uchar_t)); 17640Sstevel@tonic-gate case IP_MULTICAST_LOOP: 17655240Snordmark *ptr = connp->conn_multicast_loop; 17660Sstevel@tonic-gate return (sizeof (uint8_t)); 17670Sstevel@tonic-gate case IP_BOUND_IF: 17680Sstevel@tonic-gate /* Zero if not set */ 17690Sstevel@tonic-gate *i1 = icmp->icmp_bound_if; 17700Sstevel@tonic-gate break; /* goto sizeof (int) option return */ 17710Sstevel@tonic-gate case IP_UNSPEC_SRC: 17720Sstevel@tonic-gate *ptr = icmp->icmp_unspec_source; 17730Sstevel@tonic-gate break; /* goto sizeof (int) option return */ 17745455Smeem case IP_BROADCAST_TTL: 17755455Smeem *(uchar_t *)ptr = connp->conn_broadcast_ttl; 17765455Smeem return (sizeof (uchar_t)); 17770Sstevel@tonic-gate case IP_RECVIF: 17780Sstevel@tonic-gate *ptr = icmp->icmp_recvif; 17790Sstevel@tonic-gate break; /* goto sizeof (int) option return */ 17803318Srshoaib case IP_RECVPKTINFO: 17813318Srshoaib /* 17823318Srshoaib * This also handles IP_PKTINFO. 17833318Srshoaib * IP_PKTINFO and IP_RECVPKTINFO have the same value. 17843318Srshoaib * Differentiation is based on the size of the argument 17853318Srshoaib * passed in. 17863318Srshoaib * This option is handled in IP which will return an 17873318Srshoaib * error for IP_PKTINFO as it's not supported as a 17883318Srshoaib * sticky option. 17893318Srshoaib */ 17903318Srshoaib return (-EINVAL); 17910Sstevel@tonic-gate /* 17920Sstevel@tonic-gate * Cannot "get" the value of following options 17930Sstevel@tonic-gate * at this level. Action is same as "default" to 17940Sstevel@tonic-gate * which we fallthrough so we keep them in comments. 17950Sstevel@tonic-gate * 17960Sstevel@tonic-gate * case IP_ADD_MEMBERSHIP: 17970Sstevel@tonic-gate * case IP_DROP_MEMBERSHIP: 17980Sstevel@tonic-gate * case IP_BLOCK_SOURCE: 17990Sstevel@tonic-gate * case IP_UNBLOCK_SOURCE: 18000Sstevel@tonic-gate * case IP_ADD_SOURCE_MEMBERSHIP: 18010Sstevel@tonic-gate * case IP_DROP_SOURCE_MEMBERSHIP: 18020Sstevel@tonic-gate * case MCAST_JOIN_GROUP: 18030Sstevel@tonic-gate * case MCAST_LEAVE_GROUP: 18040Sstevel@tonic-gate * case MCAST_BLOCK_SOURCE: 18050Sstevel@tonic-gate * case MCAST_UNBLOCK_SOURCE: 18060Sstevel@tonic-gate * case MCAST_JOIN_SOURCE_GROUP: 18070Sstevel@tonic-gate * case MCAST_LEAVE_SOURCE_GROUP: 18080Sstevel@tonic-gate * case MRT_INIT: 18090Sstevel@tonic-gate * case MRT_DONE: 18100Sstevel@tonic-gate * case MRT_ADD_VIF: 18110Sstevel@tonic-gate * case MRT_DEL_VIF: 18120Sstevel@tonic-gate * case MRT_ADD_MFC: 18130Sstevel@tonic-gate * case MRT_DEL_MFC: 18140Sstevel@tonic-gate * case MRT_VERSION: 18150Sstevel@tonic-gate * case MRT_ASSERT: 18160Sstevel@tonic-gate * case IP_SEC_OPT: 18170Sstevel@tonic-gate * case IP_DONTFAILOVER_IF: 18181663Spriyanka * case IP_NEXTHOP: 18190Sstevel@tonic-gate */ 18200Sstevel@tonic-gate default: 18210Sstevel@tonic-gate return (-1); 18220Sstevel@tonic-gate } 18230Sstevel@tonic-gate break; 18240Sstevel@tonic-gate case IPPROTO_IPV6: 18250Sstevel@tonic-gate /* 18260Sstevel@tonic-gate * Only allow IPv6 option processing on native IPv6 sockets. 18270Sstevel@tonic-gate */ 18280Sstevel@tonic-gate if (icmp->icmp_family != AF_INET6) 18290Sstevel@tonic-gate return (-1); 18300Sstevel@tonic-gate switch (name) { 18310Sstevel@tonic-gate case IPV6_UNICAST_HOPS: 18320Sstevel@tonic-gate *i1 = (unsigned int)icmp->icmp_ttl; 18330Sstevel@tonic-gate break; 18340Sstevel@tonic-gate case IPV6_MULTICAST_IF: 18350Sstevel@tonic-gate /* 0 index if not set */ 18360Sstevel@tonic-gate *i1 = icmp->icmp_multicast_if_index; 18370Sstevel@tonic-gate break; 18380Sstevel@tonic-gate case IPV6_MULTICAST_HOPS: 18390Sstevel@tonic-gate *i1 = icmp->icmp_multicast_ttl; 18400Sstevel@tonic-gate break; 18410Sstevel@tonic-gate case IPV6_MULTICAST_LOOP: 18425240Snordmark *i1 = connp->conn_multicast_loop; 18430Sstevel@tonic-gate break; 18440Sstevel@tonic-gate case IPV6_BOUND_IF: 18450Sstevel@tonic-gate /* Zero if not set */ 18460Sstevel@tonic-gate *i1 = icmp->icmp_bound_if; 18470Sstevel@tonic-gate break; 18480Sstevel@tonic-gate case IPV6_UNSPEC_SRC: 18490Sstevel@tonic-gate *i1 = icmp->icmp_unspec_source; 18500Sstevel@tonic-gate break; 18510Sstevel@tonic-gate case IPV6_CHECKSUM: 18520Sstevel@tonic-gate /* 18530Sstevel@tonic-gate * Return offset or -1 if no checksum offset. 18540Sstevel@tonic-gate * Does not apply to IPPROTO_ICMPV6 18550Sstevel@tonic-gate */ 18560Sstevel@tonic-gate if (icmp->icmp_proto == IPPROTO_ICMPV6) 18570Sstevel@tonic-gate return (-1); 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate if (icmp->icmp_raw_checksum) { 18600Sstevel@tonic-gate *i1 = icmp->icmp_checksum_off; 18610Sstevel@tonic-gate } else { 18620Sstevel@tonic-gate *i1 = -1; 18630Sstevel@tonic-gate } 18640Sstevel@tonic-gate break; 18650Sstevel@tonic-gate case IPV6_JOIN_GROUP: 18660Sstevel@tonic-gate case IPV6_LEAVE_GROUP: 18670Sstevel@tonic-gate case MCAST_JOIN_GROUP: 18680Sstevel@tonic-gate case MCAST_LEAVE_GROUP: 18690Sstevel@tonic-gate case MCAST_BLOCK_SOURCE: 18700Sstevel@tonic-gate case MCAST_UNBLOCK_SOURCE: 18710Sstevel@tonic-gate case MCAST_JOIN_SOURCE_GROUP: 18720Sstevel@tonic-gate case MCAST_LEAVE_SOURCE_GROUP: 18730Sstevel@tonic-gate /* cannot "get" the value for these */ 18740Sstevel@tonic-gate return (-1); 18750Sstevel@tonic-gate case IPV6_RECVPKTINFO: 18763318Srshoaib *i1 = icmp->icmp_ip_recvpktinfo; 18770Sstevel@tonic-gate break; 18780Sstevel@tonic-gate case IPV6_RECVTCLASS: 18790Sstevel@tonic-gate *i1 = icmp->icmp_ipv6_recvtclass; 18800Sstevel@tonic-gate break; 18810Sstevel@tonic-gate case IPV6_RECVPATHMTU: 18820Sstevel@tonic-gate *i1 = icmp->icmp_ipv6_recvpathmtu; 18830Sstevel@tonic-gate break; 18840Sstevel@tonic-gate case IPV6_V6ONLY: 18850Sstevel@tonic-gate *i1 = 1; 18860Sstevel@tonic-gate break; 18870Sstevel@tonic-gate case IPV6_RECVHOPLIMIT: 18880Sstevel@tonic-gate *i1 = icmp->icmp_ipv6_recvhoplimit; 18890Sstevel@tonic-gate break; 18900Sstevel@tonic-gate case IPV6_RECVHOPOPTS: 18910Sstevel@tonic-gate *i1 = icmp->icmp_ipv6_recvhopopts; 18920Sstevel@tonic-gate break; 18930Sstevel@tonic-gate case IPV6_RECVDSTOPTS: 18940Sstevel@tonic-gate *i1 = icmp->icmp_ipv6_recvdstopts; 18950Sstevel@tonic-gate break; 18960Sstevel@tonic-gate case _OLD_IPV6_RECVDSTOPTS: 18970Sstevel@tonic-gate *i1 = icmp->icmp_old_ipv6_recvdstopts; 18980Sstevel@tonic-gate break; 18990Sstevel@tonic-gate case IPV6_RECVRTHDRDSTOPTS: 19000Sstevel@tonic-gate *i1 = icmp->icmp_ipv6_recvrtdstopts; 19010Sstevel@tonic-gate break; 19020Sstevel@tonic-gate case IPV6_RECVRTHDR: 19030Sstevel@tonic-gate *i1 = icmp->icmp_ipv6_recvrthdr; 19040Sstevel@tonic-gate break; 19050Sstevel@tonic-gate case IPV6_PKTINFO: { 19060Sstevel@tonic-gate /* XXX assumes that caller has room for max size! */ 19070Sstevel@tonic-gate struct in6_pktinfo *pkti; 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate pkti = (struct in6_pktinfo *)ptr; 19100Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_IFINDEX) 19110Sstevel@tonic-gate pkti->ipi6_ifindex = ipp->ipp_ifindex; 19120Sstevel@tonic-gate else 19130Sstevel@tonic-gate pkti->ipi6_ifindex = 0; 19140Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_ADDR) 19150Sstevel@tonic-gate pkti->ipi6_addr = ipp->ipp_addr; 19160Sstevel@tonic-gate else 19170Sstevel@tonic-gate pkti->ipi6_addr = ipv6_all_zeros; 19180Sstevel@tonic-gate return (sizeof (struct in6_pktinfo)); 19190Sstevel@tonic-gate } 19200Sstevel@tonic-gate case IPV6_NEXTHOP: { 19210Sstevel@tonic-gate sin6_t *sin6 = (sin6_t *)ptr; 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 19240Sstevel@tonic-gate return (0); 19250Sstevel@tonic-gate *sin6 = sin6_null; 19260Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 19270Sstevel@tonic-gate sin6->sin6_addr = ipp->ipp_nexthop; 19280Sstevel@tonic-gate return (sizeof (sin6_t)); 19290Sstevel@tonic-gate } 19300Sstevel@tonic-gate case IPV6_HOPOPTS: 19310Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 19320Sstevel@tonic-gate return (0); 19331676Sjpk if (ipp->ipp_hopoptslen <= icmp->icmp_label_len_v6) 19341676Sjpk return (0); 19351676Sjpk bcopy((char *)ipp->ipp_hopopts + 19361676Sjpk icmp->icmp_label_len_v6, ptr, 19371676Sjpk ipp->ipp_hopoptslen - icmp->icmp_label_len_v6); 19381676Sjpk if (icmp->icmp_label_len_v6 > 0) { 19391676Sjpk ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 19401676Sjpk ptr[1] = (ipp->ipp_hopoptslen - 19411676Sjpk icmp->icmp_label_len_v6 + 7) / 8 - 1; 19421676Sjpk } 19431676Sjpk return (ipp->ipp_hopoptslen - icmp->icmp_label_len_v6); 19440Sstevel@tonic-gate case IPV6_RTHDRDSTOPTS: 19450Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 19460Sstevel@tonic-gate return (0); 19470Sstevel@tonic-gate bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 19480Sstevel@tonic-gate return (ipp->ipp_rtdstoptslen); 19490Sstevel@tonic-gate case IPV6_RTHDR: 19500Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_RTHDR)) 19510Sstevel@tonic-gate return (0); 19520Sstevel@tonic-gate bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 19530Sstevel@tonic-gate return (ipp->ipp_rthdrlen); 19540Sstevel@tonic-gate case IPV6_DSTOPTS: 19550Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 19560Sstevel@tonic-gate return (0); 19570Sstevel@tonic-gate bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 19580Sstevel@tonic-gate return (ipp->ipp_dstoptslen); 19590Sstevel@tonic-gate case IPV6_PATHMTU: 19600Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_PATHMTU)) 19610Sstevel@tonic-gate return (0); 19620Sstevel@tonic-gate 19630Sstevel@tonic-gate return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0, 19645381Smeem (struct ip6_mtuinfo *)ptr, is->is_netstack)); 19650Sstevel@tonic-gate case IPV6_TCLASS: 19660Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_TCLASS) 19670Sstevel@tonic-gate *i1 = ipp->ipp_tclass; 19680Sstevel@tonic-gate else 19690Sstevel@tonic-gate *i1 = IPV6_FLOW_TCLASS( 19700Sstevel@tonic-gate IPV6_DEFAULT_VERS_AND_FLOW); 19710Sstevel@tonic-gate break; 19720Sstevel@tonic-gate default: 19730Sstevel@tonic-gate return (-1); 19740Sstevel@tonic-gate } 19750Sstevel@tonic-gate break; 19760Sstevel@tonic-gate case IPPROTO_ICMPV6: 19770Sstevel@tonic-gate /* 19780Sstevel@tonic-gate * Only allow IPv6 option processing on native IPv6 sockets. 19790Sstevel@tonic-gate */ 19800Sstevel@tonic-gate if (icmp->icmp_family != AF_INET6) 19810Sstevel@tonic-gate return (-1); 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate if (icmp->icmp_proto != IPPROTO_ICMPV6) 19840Sstevel@tonic-gate return (-1); 19850Sstevel@tonic-gate 19860Sstevel@tonic-gate switch (name) { 19870Sstevel@tonic-gate case ICMP6_FILTER: 19880Sstevel@tonic-gate if (icmp->icmp_filter == NULL) { 19890Sstevel@tonic-gate /* Make it look like "pass all" */ 19900Sstevel@tonic-gate ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr); 19910Sstevel@tonic-gate } else { 19920Sstevel@tonic-gate (void) bcopy(icmp->icmp_filter, ptr, 19930Sstevel@tonic-gate sizeof (icmp6_filter_t)); 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate return (sizeof (icmp6_filter_t)); 19960Sstevel@tonic-gate default: 19970Sstevel@tonic-gate return (-1); 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate default: 20000Sstevel@tonic-gate return (-1); 20010Sstevel@tonic-gate } 20020Sstevel@tonic-gate return (sizeof (int)); 20030Sstevel@tonic-gate } 20040Sstevel@tonic-gate 20055240Snordmark /* 20065240Snordmark * This routine retrieves the current status of socket options. 20075240Snordmark * It returns the size of the option retrieved. 20085240Snordmark */ 20095240Snordmark int 20105240Snordmark icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 20115240Snordmark { 20125240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 20135240Snordmark int err; 20145240Snordmark 20155240Snordmark rw_enter(&icmp->icmp_rwlock, RW_READER); 20165240Snordmark err = icmp_opt_get_locked(q, level, name, ptr); 20175240Snordmark rw_exit(&icmp->icmp_rwlock); 20185240Snordmark return (err); 20195240Snordmark } 20205240Snordmark 20215240Snordmark 20220Sstevel@tonic-gate /* This routine sets socket options. */ 20230Sstevel@tonic-gate /* ARGSUSED */ 20240Sstevel@tonic-gate int 20255240Snordmark icmp_opt_set_locked(queue_t *q, uint_t optset_context, int level, int name, 20260Sstevel@tonic-gate uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 20270Sstevel@tonic-gate void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 20280Sstevel@tonic-gate { 20295240Snordmark conn_t *connp = Q_TO_CONN(q); 20305240Snordmark icmp_t *icmp = connp->conn_icmp; 20313448Sdh155122 icmp_stack_t *is = icmp->icmp_is; 20320Sstevel@tonic-gate int *i1 = (int *)invalp; 20330Sstevel@tonic-gate boolean_t onoff = (*i1 == 0) ? 0 : 1; 20340Sstevel@tonic-gate boolean_t checkonly; 20350Sstevel@tonic-gate int error; 20360Sstevel@tonic-gate 20370Sstevel@tonic-gate switch (optset_context) { 20380Sstevel@tonic-gate case SETFN_OPTCOM_CHECKONLY: 20390Sstevel@tonic-gate checkonly = B_TRUE; 20400Sstevel@tonic-gate /* 20410Sstevel@tonic-gate * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 20420Sstevel@tonic-gate * inlen != 0 implies value supplied and 20430Sstevel@tonic-gate * we have to "pretend" to set it. 20440Sstevel@tonic-gate * inlen == 0 implies that there is no 20450Sstevel@tonic-gate * value part in T_CHECK request and just validation 20460Sstevel@tonic-gate * done elsewhere should be enough, we just return here. 20470Sstevel@tonic-gate */ 20480Sstevel@tonic-gate if (inlen == 0) { 20490Sstevel@tonic-gate *outlenp = 0; 20500Sstevel@tonic-gate return (0); 20510Sstevel@tonic-gate } 20520Sstevel@tonic-gate break; 20530Sstevel@tonic-gate case SETFN_OPTCOM_NEGOTIATE: 20540Sstevel@tonic-gate checkonly = B_FALSE; 20550Sstevel@tonic-gate break; 20560Sstevel@tonic-gate case SETFN_UD_NEGOTIATE: 20570Sstevel@tonic-gate case SETFN_CONN_NEGOTIATE: 20580Sstevel@tonic-gate checkonly = B_FALSE; 20590Sstevel@tonic-gate /* 20600Sstevel@tonic-gate * Negotiating local and "association-related" options 20610Sstevel@tonic-gate * through T_UNITDATA_REQ. 20620Sstevel@tonic-gate * 20630Sstevel@tonic-gate * Following routine can filter out ones we do not 20640Sstevel@tonic-gate * want to be "set" this way. 20650Sstevel@tonic-gate */ 20660Sstevel@tonic-gate if (!icmp_opt_allow_udr_set(level, name)) { 20670Sstevel@tonic-gate *outlenp = 0; 20680Sstevel@tonic-gate return (EINVAL); 20690Sstevel@tonic-gate } 20700Sstevel@tonic-gate break; 20710Sstevel@tonic-gate default: 20720Sstevel@tonic-gate /* 20730Sstevel@tonic-gate * We should never get here 20740Sstevel@tonic-gate */ 20750Sstevel@tonic-gate *outlenp = 0; 20760Sstevel@tonic-gate return (EINVAL); 20770Sstevel@tonic-gate } 20780Sstevel@tonic-gate 20790Sstevel@tonic-gate ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 20800Sstevel@tonic-gate (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate /* 20830Sstevel@tonic-gate * For fixed length options, no sanity check 20840Sstevel@tonic-gate * of passed in length is done. It is assumed *_optcom_req() 20850Sstevel@tonic-gate * routines do the right thing. 20860Sstevel@tonic-gate */ 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate switch (level) { 20890Sstevel@tonic-gate case SOL_SOCKET: 20900Sstevel@tonic-gate switch (name) { 20910Sstevel@tonic-gate case SO_DEBUG: 20920Sstevel@tonic-gate if (!checkonly) 20930Sstevel@tonic-gate icmp->icmp_debug = onoff; 20940Sstevel@tonic-gate break; 20950Sstevel@tonic-gate case SO_PROTOTYPE: 20960Sstevel@tonic-gate if ((*i1 & 0xFF) != IPPROTO_ICMP && 20970Sstevel@tonic-gate (*i1 & 0xFF) != IPPROTO_ICMPV6 && 20980Sstevel@tonic-gate secpolicy_net_rawaccess(cr) != 0) { 20990Sstevel@tonic-gate *outlenp = 0; 21000Sstevel@tonic-gate return (EACCES); 21010Sstevel@tonic-gate } 21020Sstevel@tonic-gate /* Can't use IPPROTO_RAW with IPv6 */ 21030Sstevel@tonic-gate if ((*i1 & 0xFF) == IPPROTO_RAW && 21040Sstevel@tonic-gate icmp->icmp_family == AF_INET6) { 21050Sstevel@tonic-gate *outlenp = 0; 21060Sstevel@tonic-gate return (EPROTONOSUPPORT); 21070Sstevel@tonic-gate } 21080Sstevel@tonic-gate if (checkonly) { 21090Sstevel@tonic-gate /* T_CHECK case */ 21100Sstevel@tonic-gate *(int *)outvalp = (*i1 & 0xFF); 21110Sstevel@tonic-gate break; 21120Sstevel@tonic-gate } 21130Sstevel@tonic-gate icmp->icmp_proto = *i1 & 0xFF; 21140Sstevel@tonic-gate if ((icmp->icmp_proto == IPPROTO_RAW || 21150Sstevel@tonic-gate icmp->icmp_proto == IPPROTO_IGMP) && 21160Sstevel@tonic-gate icmp->icmp_family == AF_INET) 21170Sstevel@tonic-gate icmp->icmp_hdrincl = 1; 21180Sstevel@tonic-gate else 21190Sstevel@tonic-gate icmp->icmp_hdrincl = 0; 21200Sstevel@tonic-gate 21210Sstevel@tonic-gate if (icmp->icmp_family == AF_INET6 && 21220Sstevel@tonic-gate icmp->icmp_proto == IPPROTO_ICMPV6) { 21230Sstevel@tonic-gate /* Set offset for icmp6_cksum */ 21240Sstevel@tonic-gate icmp->icmp_raw_checksum = 0; 21250Sstevel@tonic-gate icmp->icmp_checksum_off = 2; 21260Sstevel@tonic-gate } 21270Sstevel@tonic-gate if (icmp->icmp_proto == IPPROTO_UDP || 21280Sstevel@tonic-gate icmp->icmp_proto == IPPROTO_TCP || 21290Sstevel@tonic-gate icmp->icmp_proto == IPPROTO_SCTP) { 21300Sstevel@tonic-gate icmp->icmp_no_tp_cksum = 1; 21310Sstevel@tonic-gate icmp->icmp_sticky_ipp.ipp_fields |= 21320Sstevel@tonic-gate IPPF_NO_CKSUM; 21330Sstevel@tonic-gate } else { 21340Sstevel@tonic-gate icmp->icmp_no_tp_cksum = 0; 21350Sstevel@tonic-gate icmp->icmp_sticky_ipp.ipp_fields &= 21360Sstevel@tonic-gate ~IPPF_NO_CKSUM; 21370Sstevel@tonic-gate } 21380Sstevel@tonic-gate 21390Sstevel@tonic-gate if (icmp->icmp_filter != NULL && 21400Sstevel@tonic-gate icmp->icmp_proto != IPPROTO_ICMPV6) { 21410Sstevel@tonic-gate kmem_free(icmp->icmp_filter, 21420Sstevel@tonic-gate sizeof (icmp6_filter_t)); 21430Sstevel@tonic-gate icmp->icmp_filter = NULL; 21440Sstevel@tonic-gate } 21450Sstevel@tonic-gate 21460Sstevel@tonic-gate /* Rebuild the header template */ 21475240Snordmark error = icmp_build_hdrs(icmp); 21480Sstevel@tonic-gate if (error != 0) { 21490Sstevel@tonic-gate *outlenp = 0; 21500Sstevel@tonic-gate return (error); 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate 2153409Skcpoon /* 2154409Skcpoon * For SCTP, we don't use icmp_bind_proto() for 2155409Skcpoon * raw socket binding. Note that we do not need 2156409Skcpoon * to set *outlenp. 21575240Snordmark * FIXME: how does SCTP work? 2158409Skcpoon */ 2159409Skcpoon if (icmp->icmp_proto == IPPROTO_SCTP) 2160409Skcpoon return (0); 2161409Skcpoon 21620Sstevel@tonic-gate *outlenp = sizeof (int); 21630Sstevel@tonic-gate *(int *)outvalp = *i1 & 0xFF; 21645240Snordmark 21655240Snordmark /* Drop lock across the bind operation */ 21665240Snordmark rw_exit(&icmp->icmp_rwlock); 21675240Snordmark icmp_bind_proto(q); 21685240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 21690Sstevel@tonic-gate return (0); 21700Sstevel@tonic-gate case SO_REUSEADDR: 21710Sstevel@tonic-gate if (!checkonly) 21720Sstevel@tonic-gate icmp->icmp_reuseaddr = onoff; 21730Sstevel@tonic-gate break; 21740Sstevel@tonic-gate 21750Sstevel@tonic-gate /* 21760Sstevel@tonic-gate * The following three items are available here, 21770Sstevel@tonic-gate * but are only meaningful to IP. 21780Sstevel@tonic-gate */ 21790Sstevel@tonic-gate case SO_DONTROUTE: 21800Sstevel@tonic-gate if (!checkonly) 21810Sstevel@tonic-gate icmp->icmp_dontroute = onoff; 21820Sstevel@tonic-gate break; 21830Sstevel@tonic-gate case SO_USELOOPBACK: 21840Sstevel@tonic-gate if (!checkonly) 21850Sstevel@tonic-gate icmp->icmp_useloopback = onoff; 21860Sstevel@tonic-gate break; 21870Sstevel@tonic-gate case SO_BROADCAST: 21880Sstevel@tonic-gate if (!checkonly) 21890Sstevel@tonic-gate icmp->icmp_broadcast = onoff; 21900Sstevel@tonic-gate break; 21910Sstevel@tonic-gate 21920Sstevel@tonic-gate case SO_SNDBUF: 21933448Sdh155122 if (*i1 > is->is_max_buf) { 21940Sstevel@tonic-gate *outlenp = 0; 21950Sstevel@tonic-gate return (ENOBUFS); 21960Sstevel@tonic-gate } 21970Sstevel@tonic-gate if (!checkonly) { 21980Sstevel@tonic-gate q->q_hiwat = *i1; 21990Sstevel@tonic-gate } 22000Sstevel@tonic-gate break; 22010Sstevel@tonic-gate case SO_RCVBUF: 22023448Sdh155122 if (*i1 > is->is_max_buf) { 22030Sstevel@tonic-gate *outlenp = 0; 22040Sstevel@tonic-gate return (ENOBUFS); 22050Sstevel@tonic-gate } 22060Sstevel@tonic-gate if (!checkonly) { 22070Sstevel@tonic-gate RD(q)->q_hiwat = *i1; 22085240Snordmark rw_exit(&icmp->icmp_rwlock); 22090Sstevel@tonic-gate (void) mi_set_sth_hiwat(RD(q), *i1); 22105240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 22110Sstevel@tonic-gate } 22120Sstevel@tonic-gate break; 22130Sstevel@tonic-gate case SO_DGRAM_ERRIND: 22140Sstevel@tonic-gate if (!checkonly) 22150Sstevel@tonic-gate icmp->icmp_dgram_errind = onoff; 22160Sstevel@tonic-gate break; 22172263Ssommerfe case SO_ALLZONES: 22182263Ssommerfe /* 22192263Ssommerfe * "soft" error (negative) 22202263Ssommerfe * option not handled at this level 22212263Ssommerfe * Note: Do not modify *outlenp 22222263Ssommerfe */ 22232263Ssommerfe return (-EINVAL); 22241673Sgt145670 case SO_TIMESTAMP: 22251673Sgt145670 if (!checkonly) { 22261673Sgt145670 icmp->icmp_timestamp = onoff; 22271673Sgt145670 } 22281673Sgt145670 break; 22291676Sjpk case SO_MAC_EXEMPT: 2230*6596Skp158701 /* 2231*6596Skp158701 * "soft" error (negative) 2232*6596Skp158701 * option not handled at this level 2233*6596Skp158701 * Note: Do not modify *outlenp 2234*6596Skp158701 */ 2235*6596Skp158701 return (-EINVAL); 22360Sstevel@tonic-gate /* 22370Sstevel@tonic-gate * Following three not meaningful for icmp 22380Sstevel@tonic-gate * Action is same as "default" so we keep them 22390Sstevel@tonic-gate * in comments. 22400Sstevel@tonic-gate * case SO_LINGER: 22410Sstevel@tonic-gate * case SO_KEEPALIVE: 22420Sstevel@tonic-gate * case SO_OOBINLINE: 22430Sstevel@tonic-gate */ 22440Sstevel@tonic-gate default: 22450Sstevel@tonic-gate *outlenp = 0; 22460Sstevel@tonic-gate return (EINVAL); 22470Sstevel@tonic-gate } 22480Sstevel@tonic-gate break; 22490Sstevel@tonic-gate case IPPROTO_IP: 22500Sstevel@tonic-gate /* 22510Sstevel@tonic-gate * Only allow IPv4 option processing on IPv4 sockets. 22520Sstevel@tonic-gate */ 22530Sstevel@tonic-gate if (icmp->icmp_family != AF_INET) { 22540Sstevel@tonic-gate *outlenp = 0; 22550Sstevel@tonic-gate return (ENOPROTOOPT); 22560Sstevel@tonic-gate } 22570Sstevel@tonic-gate switch (name) { 22580Sstevel@tonic-gate case IP_OPTIONS: 22590Sstevel@tonic-gate case T_IP_OPTIONS: 22600Sstevel@tonic-gate /* Save options for use by IP. */ 22611676Sjpk if ((inlen & 0x3) || 22621676Sjpk inlen + icmp->icmp_label_len > IP_MAX_OPT_LENGTH) { 22630Sstevel@tonic-gate *outlenp = 0; 22640Sstevel@tonic-gate return (EINVAL); 22650Sstevel@tonic-gate } 22660Sstevel@tonic-gate if (checkonly) 22670Sstevel@tonic-gate break; 22680Sstevel@tonic-gate 22691676Sjpk if (!tsol_option_set(&icmp->icmp_ip_snd_options, 22701676Sjpk &icmp->icmp_ip_snd_options_len, 22711676Sjpk icmp->icmp_label_len, invalp, inlen)) { 22721676Sjpk *outlenp = 0; 22731676Sjpk return (ENOMEM); 22740Sstevel@tonic-gate } 22751676Sjpk 22760Sstevel@tonic-gate icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + 22770Sstevel@tonic-gate icmp->icmp_ip_snd_options_len; 22785240Snordmark rw_exit(&icmp->icmp_rwlock); 22790Sstevel@tonic-gate (void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len + 22805240Snordmark is->is_wroff_extra); 22815240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 22820Sstevel@tonic-gate break; 22830Sstevel@tonic-gate case IP_HDRINCL: 22840Sstevel@tonic-gate if (!checkonly) 22850Sstevel@tonic-gate icmp->icmp_hdrincl = onoff; 22860Sstevel@tonic-gate break; 22870Sstevel@tonic-gate case IP_TOS: 22880Sstevel@tonic-gate case T_IP_TOS: 22890Sstevel@tonic-gate if (!checkonly) { 22900Sstevel@tonic-gate icmp->icmp_type_of_service = (uint8_t)*i1; 22910Sstevel@tonic-gate } 22920Sstevel@tonic-gate break; 22930Sstevel@tonic-gate case IP_TTL: 22940Sstevel@tonic-gate if (!checkonly) { 22950Sstevel@tonic-gate icmp->icmp_ttl = (uint8_t)*i1; 22960Sstevel@tonic-gate } 22970Sstevel@tonic-gate break; 22980Sstevel@tonic-gate case IP_MULTICAST_IF: 22990Sstevel@tonic-gate /* 23000Sstevel@tonic-gate * TODO should check OPTMGMT reply and undo this if 23010Sstevel@tonic-gate * there is an error. 23020Sstevel@tonic-gate */ 23030Sstevel@tonic-gate if (!checkonly) 23040Sstevel@tonic-gate icmp->icmp_multicast_if_addr = *i1; 23050Sstevel@tonic-gate break; 23060Sstevel@tonic-gate case IP_MULTICAST_TTL: 23070Sstevel@tonic-gate if (!checkonly) 23080Sstevel@tonic-gate icmp->icmp_multicast_ttl = *invalp; 23090Sstevel@tonic-gate break; 23100Sstevel@tonic-gate case IP_MULTICAST_LOOP: 23110Sstevel@tonic-gate if (!checkonly) { 23125240Snordmark connp->conn_multicast_loop = 23130Sstevel@tonic-gate (*invalp == 0) ? 0 : 1; 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate break; 23160Sstevel@tonic-gate case IP_BOUND_IF: 23170Sstevel@tonic-gate if (!checkonly) 23180Sstevel@tonic-gate icmp->icmp_bound_if = *i1; 23190Sstevel@tonic-gate break; 23200Sstevel@tonic-gate case IP_UNSPEC_SRC: 23210Sstevel@tonic-gate if (!checkonly) 23220Sstevel@tonic-gate icmp->icmp_unspec_source = onoff; 23230Sstevel@tonic-gate break; 23245455Smeem case IP_BROADCAST_TTL: 23255455Smeem if (!checkonly) 23265455Smeem connp->conn_broadcast_ttl = *invalp; 23275455Smeem break; 23280Sstevel@tonic-gate case IP_RECVIF: 23290Sstevel@tonic-gate if (!checkonly) 23300Sstevel@tonic-gate icmp->icmp_recvif = onoff; 23315240Snordmark /* 23325240Snordmark * pass to ip 23335240Snordmark */ 23345240Snordmark return (-EINVAL); 23353318Srshoaib case IP_PKTINFO: { 23363318Srshoaib /* 23373318Srshoaib * This also handles IP_RECVPKTINFO. 23383318Srshoaib * IP_PKTINFO and IP_RECVPKTINFO have the same value. 23393318Srshoaib * Differentiation is based on the size of the argument 23403318Srshoaib * passed in. 23413318Srshoaib */ 23423318Srshoaib struct in_pktinfo *pktinfop; 23433318Srshoaib ip4_pkt_t *attr_pktinfop; 23443318Srshoaib 23453318Srshoaib if (checkonly) 23463318Srshoaib break; 23473318Srshoaib 23483318Srshoaib if (inlen == sizeof (int)) { 23493318Srshoaib /* 23503318Srshoaib * This is IP_RECVPKTINFO option. 23513318Srshoaib * Keep a local copy of wether this option is 23523318Srshoaib * set or not and pass it down to IP for 23533318Srshoaib * processing. 23543318Srshoaib */ 23553318Srshoaib icmp->icmp_ip_recvpktinfo = onoff; 23563318Srshoaib return (-EINVAL); 23573318Srshoaib } 23583318Srshoaib 23593318Srshoaib 23603318Srshoaib if (inlen != sizeof (struct in_pktinfo)) 23613318Srshoaib return (EINVAL); 23623318Srshoaib 23633318Srshoaib if ((attr_pktinfop = (ip4_pkt_t *)thisdg_attrs) 23643318Srshoaib == NULL) { 23653318Srshoaib /* 23663318Srshoaib * sticky option is not supported 23673318Srshoaib */ 23683318Srshoaib return (EINVAL); 23693318Srshoaib } 23703318Srshoaib 23713318Srshoaib pktinfop = (struct in_pktinfo *)invalp; 23723318Srshoaib 23733318Srshoaib /* 23743318Srshoaib * Atleast one of the values should be specified 23753318Srshoaib */ 23763318Srshoaib if (pktinfop->ipi_ifindex == 0 && 23773318Srshoaib pktinfop->ipi_spec_dst.s_addr == INADDR_ANY) { 23783318Srshoaib return (EINVAL); 23793318Srshoaib } 23803318Srshoaib 23813318Srshoaib attr_pktinfop->ip4_addr = pktinfop->ipi_spec_dst.s_addr; 23823318Srshoaib attr_pktinfop->ip4_ill_index = pktinfop->ipi_ifindex; 23833318Srshoaib } 23843318Srshoaib break; 23850Sstevel@tonic-gate case IP_ADD_MEMBERSHIP: 23860Sstevel@tonic-gate case IP_DROP_MEMBERSHIP: 23870Sstevel@tonic-gate case IP_BLOCK_SOURCE: 23880Sstevel@tonic-gate case IP_UNBLOCK_SOURCE: 23890Sstevel@tonic-gate case IP_ADD_SOURCE_MEMBERSHIP: 23900Sstevel@tonic-gate case IP_DROP_SOURCE_MEMBERSHIP: 23910Sstevel@tonic-gate case MCAST_JOIN_GROUP: 23920Sstevel@tonic-gate case MCAST_LEAVE_GROUP: 23930Sstevel@tonic-gate case MCAST_BLOCK_SOURCE: 23940Sstevel@tonic-gate case MCAST_UNBLOCK_SOURCE: 23950Sstevel@tonic-gate case MCAST_JOIN_SOURCE_GROUP: 23960Sstevel@tonic-gate case MCAST_LEAVE_SOURCE_GROUP: 23970Sstevel@tonic-gate case MRT_INIT: 23980Sstevel@tonic-gate case MRT_DONE: 23990Sstevel@tonic-gate case MRT_ADD_VIF: 24000Sstevel@tonic-gate case MRT_DEL_VIF: 24010Sstevel@tonic-gate case MRT_ADD_MFC: 24020Sstevel@tonic-gate case MRT_DEL_MFC: 24030Sstevel@tonic-gate case MRT_VERSION: 24040Sstevel@tonic-gate case MRT_ASSERT: 24050Sstevel@tonic-gate case IP_SEC_OPT: 24060Sstevel@tonic-gate case IP_DONTFAILOVER_IF: 24071663Spriyanka case IP_NEXTHOP: 24080Sstevel@tonic-gate /* 24090Sstevel@tonic-gate * "soft" error (negative) 24100Sstevel@tonic-gate * option not handled at this level 24110Sstevel@tonic-gate * Note: Do not modify *outlenp 24120Sstevel@tonic-gate */ 24130Sstevel@tonic-gate return (-EINVAL); 24140Sstevel@tonic-gate default: 24150Sstevel@tonic-gate *outlenp = 0; 24160Sstevel@tonic-gate return (EINVAL); 24170Sstevel@tonic-gate } 24180Sstevel@tonic-gate break; 24190Sstevel@tonic-gate case IPPROTO_IPV6: { 24200Sstevel@tonic-gate ip6_pkt_t *ipp; 24210Sstevel@tonic-gate boolean_t sticky; 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate if (icmp->icmp_family != AF_INET6) { 24240Sstevel@tonic-gate *outlenp = 0; 24250Sstevel@tonic-gate return (ENOPROTOOPT); 24260Sstevel@tonic-gate } 24270Sstevel@tonic-gate /* 24280Sstevel@tonic-gate * Deal with both sticky options and ancillary data 24290Sstevel@tonic-gate */ 24300Sstevel@tonic-gate if (thisdg_attrs == NULL) { 24310Sstevel@tonic-gate /* sticky options, or none */ 24320Sstevel@tonic-gate ipp = &icmp->icmp_sticky_ipp; 24330Sstevel@tonic-gate sticky = B_TRUE; 24340Sstevel@tonic-gate } else { 24350Sstevel@tonic-gate /* ancillary data */ 24360Sstevel@tonic-gate ipp = (ip6_pkt_t *)thisdg_attrs; 24370Sstevel@tonic-gate sticky = B_FALSE; 24380Sstevel@tonic-gate } 24390Sstevel@tonic-gate 24400Sstevel@tonic-gate switch (name) { 24410Sstevel@tonic-gate case IPV6_MULTICAST_IF: 24420Sstevel@tonic-gate if (!checkonly) 24430Sstevel@tonic-gate icmp->icmp_multicast_if_index = *i1; 24440Sstevel@tonic-gate break; 24450Sstevel@tonic-gate case IPV6_UNICAST_HOPS: 24460Sstevel@tonic-gate /* -1 means use default */ 24470Sstevel@tonic-gate if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 24480Sstevel@tonic-gate *outlenp = 0; 24490Sstevel@tonic-gate return (EINVAL); 24500Sstevel@tonic-gate } 24510Sstevel@tonic-gate if (!checkonly) { 24520Sstevel@tonic-gate if (*i1 == -1) { 2453679Sseb icmp->icmp_ttl = ipp->ipp_unicast_hops = 24543448Sdh155122 is->is_ipv6_hoplimit; 2455679Sseb ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 24560Sstevel@tonic-gate /* Pass modified value to IP. */ 24570Sstevel@tonic-gate *i1 = ipp->ipp_hoplimit; 24580Sstevel@tonic-gate } else { 2459679Sseb icmp->icmp_ttl = ipp->ipp_unicast_hops = 24600Sstevel@tonic-gate (uint8_t)*i1; 2461679Sseb ipp->ipp_fields |= IPPF_UNICAST_HOPS; 24620Sstevel@tonic-gate } 24630Sstevel@tonic-gate /* Rebuild the header template */ 24645240Snordmark error = icmp_build_hdrs(icmp); 24650Sstevel@tonic-gate if (error != 0) { 24660Sstevel@tonic-gate *outlenp = 0; 24670Sstevel@tonic-gate return (error); 24680Sstevel@tonic-gate } 24690Sstevel@tonic-gate } 24700Sstevel@tonic-gate break; 24710Sstevel@tonic-gate case IPV6_MULTICAST_HOPS: 24720Sstevel@tonic-gate /* -1 means use default */ 24730Sstevel@tonic-gate if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 24740Sstevel@tonic-gate *outlenp = 0; 24750Sstevel@tonic-gate return (EINVAL); 24760Sstevel@tonic-gate } 24770Sstevel@tonic-gate if (!checkonly) { 24780Sstevel@tonic-gate if (*i1 == -1) { 24790Sstevel@tonic-gate icmp->icmp_multicast_ttl = 2480679Sseb ipp->ipp_multicast_hops = 24810Sstevel@tonic-gate IP_DEFAULT_MULTICAST_TTL; 2482679Sseb ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS; 24830Sstevel@tonic-gate /* Pass modified value to IP. */ 2484679Sseb *i1 = icmp->icmp_multicast_ttl; 24850Sstevel@tonic-gate } else { 24860Sstevel@tonic-gate icmp->icmp_multicast_ttl = 2487679Sseb ipp->ipp_multicast_hops = 24880Sstevel@tonic-gate (uint8_t)*i1; 2489679Sseb ipp->ipp_fields |= IPPF_MULTICAST_HOPS; 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate } 24920Sstevel@tonic-gate break; 24930Sstevel@tonic-gate case IPV6_MULTICAST_LOOP: 24940Sstevel@tonic-gate if (*i1 != 0 && *i1 != 1) { 24950Sstevel@tonic-gate *outlenp = 0; 24960Sstevel@tonic-gate return (EINVAL); 24970Sstevel@tonic-gate } 24980Sstevel@tonic-gate if (!checkonly) 24995240Snordmark connp->conn_multicast_loop = *i1; 25000Sstevel@tonic-gate break; 25010Sstevel@tonic-gate case IPV6_CHECKSUM: 25020Sstevel@tonic-gate /* 25030Sstevel@tonic-gate * Integer offset into the user data of where the 25040Sstevel@tonic-gate * checksum is located. 25050Sstevel@tonic-gate * Offset of -1 disables option. 25060Sstevel@tonic-gate * Does not apply to IPPROTO_ICMPV6. 25070Sstevel@tonic-gate */ 25080Sstevel@tonic-gate if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) { 25090Sstevel@tonic-gate *outlenp = 0; 25100Sstevel@tonic-gate return (EINVAL); 25110Sstevel@tonic-gate } 25120Sstevel@tonic-gate if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) { 25130Sstevel@tonic-gate /* Negative or not 16 bit aligned offset */ 25140Sstevel@tonic-gate *outlenp = 0; 25150Sstevel@tonic-gate return (EINVAL); 25160Sstevel@tonic-gate } 25170Sstevel@tonic-gate if (checkonly) 25180Sstevel@tonic-gate break; 25190Sstevel@tonic-gate 25200Sstevel@tonic-gate if (*i1 == -1) { 25210Sstevel@tonic-gate icmp->icmp_raw_checksum = 0; 25220Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_RAW_CKSUM; 25230Sstevel@tonic-gate } else { 25240Sstevel@tonic-gate icmp->icmp_raw_checksum = 1; 25250Sstevel@tonic-gate icmp->icmp_checksum_off = *i1; 25260Sstevel@tonic-gate ipp->ipp_fields |= IPPF_RAW_CKSUM; 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate /* Rebuild the header template */ 25295240Snordmark error = icmp_build_hdrs(icmp); 25300Sstevel@tonic-gate if (error != 0) { 25310Sstevel@tonic-gate *outlenp = 0; 25320Sstevel@tonic-gate return (error); 25330Sstevel@tonic-gate } 25340Sstevel@tonic-gate break; 25350Sstevel@tonic-gate case IPV6_JOIN_GROUP: 25360Sstevel@tonic-gate case IPV6_LEAVE_GROUP: 25370Sstevel@tonic-gate case MCAST_JOIN_GROUP: 25380Sstevel@tonic-gate case MCAST_LEAVE_GROUP: 25390Sstevel@tonic-gate case MCAST_BLOCK_SOURCE: 25400Sstevel@tonic-gate case MCAST_UNBLOCK_SOURCE: 25410Sstevel@tonic-gate case MCAST_JOIN_SOURCE_GROUP: 25420Sstevel@tonic-gate case MCAST_LEAVE_SOURCE_GROUP: 25430Sstevel@tonic-gate /* 25440Sstevel@tonic-gate * "soft" error (negative) 25450Sstevel@tonic-gate * option not handled at this level 25460Sstevel@tonic-gate * Note: Do not modify *outlenp 25470Sstevel@tonic-gate */ 25480Sstevel@tonic-gate return (-EINVAL); 25490Sstevel@tonic-gate case IPV6_BOUND_IF: 25500Sstevel@tonic-gate if (!checkonly) 25510Sstevel@tonic-gate icmp->icmp_bound_if = *i1; 25520Sstevel@tonic-gate break; 25530Sstevel@tonic-gate case IPV6_UNSPEC_SRC: 25540Sstevel@tonic-gate if (!checkonly) 25550Sstevel@tonic-gate icmp->icmp_unspec_source = onoff; 25560Sstevel@tonic-gate break; 25570Sstevel@tonic-gate case IPV6_RECVTCLASS: 25580Sstevel@tonic-gate if (!checkonly) 25590Sstevel@tonic-gate icmp->icmp_ipv6_recvtclass = onoff; 25600Sstevel@tonic-gate break; 25610Sstevel@tonic-gate /* 25620Sstevel@tonic-gate * Set boolean switches for ancillary data delivery 25630Sstevel@tonic-gate */ 25640Sstevel@tonic-gate case IPV6_RECVPKTINFO: 25650Sstevel@tonic-gate if (!checkonly) 25663318Srshoaib icmp->icmp_ip_recvpktinfo = onoff; 25670Sstevel@tonic-gate break; 25680Sstevel@tonic-gate case IPV6_RECVPATHMTU: 25690Sstevel@tonic-gate if (!checkonly) 25700Sstevel@tonic-gate icmp->icmp_ipv6_recvpathmtu = onoff; 25710Sstevel@tonic-gate break; 25720Sstevel@tonic-gate case IPV6_RECVHOPLIMIT: 25730Sstevel@tonic-gate if (!checkonly) 25740Sstevel@tonic-gate icmp->icmp_ipv6_recvhoplimit = onoff; 25750Sstevel@tonic-gate break; 25760Sstevel@tonic-gate case IPV6_RECVHOPOPTS: 25770Sstevel@tonic-gate if (!checkonly) 25780Sstevel@tonic-gate icmp->icmp_ipv6_recvhopopts = onoff; 25790Sstevel@tonic-gate break; 25800Sstevel@tonic-gate case IPV6_RECVDSTOPTS: 25810Sstevel@tonic-gate if (!checkonly) 25820Sstevel@tonic-gate icmp->icmp_ipv6_recvdstopts = onoff; 25830Sstevel@tonic-gate break; 25840Sstevel@tonic-gate case _OLD_IPV6_RECVDSTOPTS: 25850Sstevel@tonic-gate if (!checkonly) 25860Sstevel@tonic-gate icmp->icmp_old_ipv6_recvdstopts = onoff; 25870Sstevel@tonic-gate break; 25880Sstevel@tonic-gate case IPV6_RECVRTHDRDSTOPTS: 25890Sstevel@tonic-gate if (!checkonly) 25900Sstevel@tonic-gate icmp->icmp_ipv6_recvrtdstopts = onoff; 25910Sstevel@tonic-gate break; 25920Sstevel@tonic-gate case IPV6_RECVRTHDR: 25930Sstevel@tonic-gate if (!checkonly) 25940Sstevel@tonic-gate icmp->icmp_ipv6_recvrthdr = onoff; 25950Sstevel@tonic-gate break; 25960Sstevel@tonic-gate /* 25970Sstevel@tonic-gate * Set sticky options or ancillary data. 25980Sstevel@tonic-gate * If sticky options, (re)build any extension headers 25990Sstevel@tonic-gate * that might be needed as a result. 26000Sstevel@tonic-gate */ 26010Sstevel@tonic-gate case IPV6_PKTINFO: 26020Sstevel@tonic-gate /* 26030Sstevel@tonic-gate * The source address and ifindex are verified 26040Sstevel@tonic-gate * in ip_opt_set(). For ancillary data the 26050Sstevel@tonic-gate * source address is checked in ip_wput_v6. 26060Sstevel@tonic-gate */ 26070Sstevel@tonic-gate if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 26080Sstevel@tonic-gate return (EINVAL); 26090Sstevel@tonic-gate if (checkonly) 26100Sstevel@tonic-gate break; 26110Sstevel@tonic-gate 26120Sstevel@tonic-gate if (inlen == 0) { 26130Sstevel@tonic-gate ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 26140Sstevel@tonic-gate ipp->ipp_sticky_ignored |= 26150Sstevel@tonic-gate (IPPF_IFINDEX|IPPF_ADDR); 26160Sstevel@tonic-gate } else { 26170Sstevel@tonic-gate struct in6_pktinfo *pkti; 26180Sstevel@tonic-gate 26190Sstevel@tonic-gate pkti = (struct in6_pktinfo *)invalp; 26200Sstevel@tonic-gate ipp->ipp_ifindex = pkti->ipi6_ifindex; 26210Sstevel@tonic-gate ipp->ipp_addr = pkti->ipi6_addr; 26220Sstevel@tonic-gate if (ipp->ipp_ifindex != 0) 26230Sstevel@tonic-gate ipp->ipp_fields |= IPPF_IFINDEX; 26240Sstevel@tonic-gate else 26250Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_IFINDEX; 26260Sstevel@tonic-gate if (!IN6_IS_ADDR_UNSPECIFIED( 26270Sstevel@tonic-gate &ipp->ipp_addr)) 26280Sstevel@tonic-gate ipp->ipp_fields |= IPPF_ADDR; 26290Sstevel@tonic-gate else 26300Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_ADDR; 26310Sstevel@tonic-gate } 26320Sstevel@tonic-gate if (sticky) { 26335240Snordmark error = icmp_build_hdrs(icmp); 26340Sstevel@tonic-gate if (error != 0) 26350Sstevel@tonic-gate return (error); 26360Sstevel@tonic-gate } 26370Sstevel@tonic-gate break; 26380Sstevel@tonic-gate case IPV6_HOPLIMIT: 2639679Sseb /* This option can only be used as ancillary data. */ 2640679Sseb if (sticky) 2641679Sseb return (EINVAL); 26420Sstevel@tonic-gate if (inlen != 0 && inlen != sizeof (int)) 26430Sstevel@tonic-gate return (EINVAL); 26440Sstevel@tonic-gate if (checkonly) 26450Sstevel@tonic-gate break; 26460Sstevel@tonic-gate 26470Sstevel@tonic-gate if (inlen == 0) { 26480Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_HOPLIMIT; 26490Sstevel@tonic-gate ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT; 26500Sstevel@tonic-gate } else { 26510Sstevel@tonic-gate if (*i1 > 255 || *i1 < -1) 26520Sstevel@tonic-gate return (EINVAL); 26530Sstevel@tonic-gate if (*i1 == -1) 26543448Sdh155122 ipp->ipp_hoplimit = 26553448Sdh155122 is->is_ipv6_hoplimit; 26560Sstevel@tonic-gate else 26570Sstevel@tonic-gate ipp->ipp_hoplimit = *i1; 26580Sstevel@tonic-gate ipp->ipp_fields |= IPPF_HOPLIMIT; 26590Sstevel@tonic-gate } 26600Sstevel@tonic-gate break; 26610Sstevel@tonic-gate case IPV6_TCLASS: 26620Sstevel@tonic-gate /* 26630Sstevel@tonic-gate * IPV6_RECVTCLASS accepts -1 as use kernel default 26640Sstevel@tonic-gate * and [0, 255] as the actualy traffic class. 26650Sstevel@tonic-gate */ 26660Sstevel@tonic-gate if (inlen != 0 && inlen != sizeof (int)) 26670Sstevel@tonic-gate return (EINVAL); 26680Sstevel@tonic-gate if (checkonly) 26690Sstevel@tonic-gate break; 26700Sstevel@tonic-gate 26710Sstevel@tonic-gate if (inlen == 0) { 26720Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_TCLASS; 26730Sstevel@tonic-gate ipp->ipp_sticky_ignored |= IPPF_TCLASS; 26740Sstevel@tonic-gate } else { 26750Sstevel@tonic-gate if (*i1 >= 256 || *i1 < -1) 26760Sstevel@tonic-gate return (EINVAL); 26770Sstevel@tonic-gate if (*i1 == -1) { 26780Sstevel@tonic-gate ipp->ipp_tclass = 26790Sstevel@tonic-gate IPV6_FLOW_TCLASS( 26800Sstevel@tonic-gate IPV6_DEFAULT_VERS_AND_FLOW); 26810Sstevel@tonic-gate } else { 26820Sstevel@tonic-gate ipp->ipp_tclass = *i1; 26830Sstevel@tonic-gate } 26840Sstevel@tonic-gate ipp->ipp_fields |= IPPF_TCLASS; 26850Sstevel@tonic-gate } 26860Sstevel@tonic-gate if (sticky) { 26875240Snordmark error = icmp_build_hdrs(icmp); 26880Sstevel@tonic-gate if (error != 0) 26890Sstevel@tonic-gate return (error); 26900Sstevel@tonic-gate } 26910Sstevel@tonic-gate break; 26920Sstevel@tonic-gate case IPV6_NEXTHOP: 26930Sstevel@tonic-gate /* 26940Sstevel@tonic-gate * IP will verify that the nexthop is reachable 26950Sstevel@tonic-gate * and fail for sticky options. 26960Sstevel@tonic-gate */ 26970Sstevel@tonic-gate if (inlen != 0 && inlen != sizeof (sin6_t)) 26980Sstevel@tonic-gate return (EINVAL); 26990Sstevel@tonic-gate if (checkonly) 27000Sstevel@tonic-gate break; 27010Sstevel@tonic-gate 27020Sstevel@tonic-gate if (inlen == 0) { 27030Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_NEXTHOP; 27040Sstevel@tonic-gate ipp->ipp_sticky_ignored |= IPPF_NEXTHOP; 27050Sstevel@tonic-gate } else { 27060Sstevel@tonic-gate sin6_t *sin6 = (sin6_t *)invalp; 27070Sstevel@tonic-gate 27080Sstevel@tonic-gate if (sin6->sin6_family != AF_INET6) 27090Sstevel@tonic-gate return (EAFNOSUPPORT); 27100Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 27110Sstevel@tonic-gate return (EADDRNOTAVAIL); 27120Sstevel@tonic-gate ipp->ipp_nexthop = sin6->sin6_addr; 27130Sstevel@tonic-gate if (!IN6_IS_ADDR_UNSPECIFIED( 27140Sstevel@tonic-gate &ipp->ipp_nexthop)) 27150Sstevel@tonic-gate ipp->ipp_fields |= IPPF_NEXTHOP; 27160Sstevel@tonic-gate else 27170Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_NEXTHOP; 27180Sstevel@tonic-gate } 27190Sstevel@tonic-gate if (sticky) { 27205240Snordmark error = icmp_build_hdrs(icmp); 27210Sstevel@tonic-gate if (error != 0) 27220Sstevel@tonic-gate return (error); 27230Sstevel@tonic-gate } 27240Sstevel@tonic-gate break; 27250Sstevel@tonic-gate case IPV6_HOPOPTS: { 27260Sstevel@tonic-gate ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 27270Sstevel@tonic-gate /* 27280Sstevel@tonic-gate * Sanity checks - minimum size, size a multiple of 27290Sstevel@tonic-gate * eight bytes, and matching size passed in. 27300Sstevel@tonic-gate */ 27310Sstevel@tonic-gate if (inlen != 0 && 27320Sstevel@tonic-gate inlen != (8 * (hopts->ip6h_len + 1))) 27330Sstevel@tonic-gate return (EINVAL); 27340Sstevel@tonic-gate 27350Sstevel@tonic-gate if (checkonly) 27360Sstevel@tonic-gate break; 27371676Sjpk error = optcom_pkt_set(invalp, inlen, sticky, 27381676Sjpk (uchar_t **)&ipp->ipp_hopopts, 27391676Sjpk &ipp->ipp_hopoptslen, 27401676Sjpk sticky ? icmp->icmp_label_len_v6 : 0); 27411676Sjpk if (error != 0) 27421676Sjpk return (error); 27431676Sjpk if (ipp->ipp_hopoptslen == 0) { 27440Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_HOPOPTS; 27450Sstevel@tonic-gate ipp->ipp_sticky_ignored |= IPPF_HOPOPTS; 27460Sstevel@tonic-gate } else { 27470Sstevel@tonic-gate ipp->ipp_fields |= IPPF_HOPOPTS; 27480Sstevel@tonic-gate } 27490Sstevel@tonic-gate if (sticky) { 27505240Snordmark error = icmp_build_hdrs(icmp); 27510Sstevel@tonic-gate if (error != 0) 27520Sstevel@tonic-gate return (error); 27530Sstevel@tonic-gate } 27540Sstevel@tonic-gate break; 27550Sstevel@tonic-gate } 27560Sstevel@tonic-gate case IPV6_RTHDRDSTOPTS: { 27570Sstevel@tonic-gate ip6_dest_t *dopts = (ip6_dest_t *)invalp; 27580Sstevel@tonic-gate 27590Sstevel@tonic-gate /* 27600Sstevel@tonic-gate * Sanity checks - minimum size, size a multiple of 27610Sstevel@tonic-gate * eight bytes, and matching size passed in. 27620Sstevel@tonic-gate */ 27630Sstevel@tonic-gate if (inlen != 0 && 27640Sstevel@tonic-gate inlen != (8 * (dopts->ip6d_len + 1))) 27650Sstevel@tonic-gate return (EINVAL); 27660Sstevel@tonic-gate 27670Sstevel@tonic-gate if (checkonly) 27680Sstevel@tonic-gate break; 27690Sstevel@tonic-gate 27700Sstevel@tonic-gate if (inlen == 0) { 27710Sstevel@tonic-gate if (sticky && 27720Sstevel@tonic-gate (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) { 27730Sstevel@tonic-gate kmem_free(ipp->ipp_rtdstopts, 27740Sstevel@tonic-gate ipp->ipp_rtdstoptslen); 27750Sstevel@tonic-gate ipp->ipp_rtdstopts = NULL; 27760Sstevel@tonic-gate ipp->ipp_rtdstoptslen = 0; 27770Sstevel@tonic-gate } 27780Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 27790Sstevel@tonic-gate ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS; 27800Sstevel@tonic-gate } else { 27811676Sjpk error = optcom_pkt_set(invalp, inlen, sticky, 27820Sstevel@tonic-gate (uchar_t **)&ipp->ipp_rtdstopts, 27831676Sjpk &ipp->ipp_rtdstoptslen, 0); 27840Sstevel@tonic-gate if (error != 0) 27850Sstevel@tonic-gate return (error); 27860Sstevel@tonic-gate ipp->ipp_fields |= IPPF_RTDSTOPTS; 27870Sstevel@tonic-gate } 27880Sstevel@tonic-gate if (sticky) { 27895240Snordmark error = icmp_build_hdrs(icmp); 27900Sstevel@tonic-gate if (error != 0) 27910Sstevel@tonic-gate return (error); 27920Sstevel@tonic-gate } 27930Sstevel@tonic-gate break; 27940Sstevel@tonic-gate } 27950Sstevel@tonic-gate case IPV6_DSTOPTS: { 27960Sstevel@tonic-gate ip6_dest_t *dopts = (ip6_dest_t *)invalp; 27970Sstevel@tonic-gate 27980Sstevel@tonic-gate /* 27990Sstevel@tonic-gate * Sanity checks - minimum size, size a multiple of 28000Sstevel@tonic-gate * eight bytes, and matching size passed in. 28010Sstevel@tonic-gate */ 28020Sstevel@tonic-gate if (inlen != 0 && 28030Sstevel@tonic-gate inlen != (8 * (dopts->ip6d_len + 1))) 28040Sstevel@tonic-gate return (EINVAL); 28050Sstevel@tonic-gate 28060Sstevel@tonic-gate if (checkonly) 28070Sstevel@tonic-gate break; 28080Sstevel@tonic-gate 28090Sstevel@tonic-gate if (inlen == 0) { 28100Sstevel@tonic-gate if (sticky && 28110Sstevel@tonic-gate (ipp->ipp_fields & IPPF_DSTOPTS) != 0) { 28120Sstevel@tonic-gate kmem_free(ipp->ipp_dstopts, 28130Sstevel@tonic-gate ipp->ipp_dstoptslen); 28140Sstevel@tonic-gate ipp->ipp_dstopts = NULL; 28150Sstevel@tonic-gate ipp->ipp_dstoptslen = 0; 28160Sstevel@tonic-gate } 28170Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_DSTOPTS; 28180Sstevel@tonic-gate ipp->ipp_sticky_ignored |= IPPF_DSTOPTS; 28190Sstevel@tonic-gate } else { 28201676Sjpk error = optcom_pkt_set(invalp, inlen, sticky, 28210Sstevel@tonic-gate (uchar_t **)&ipp->ipp_dstopts, 28221676Sjpk &ipp->ipp_dstoptslen, 0); 28230Sstevel@tonic-gate if (error != 0) 28240Sstevel@tonic-gate return (error); 28250Sstevel@tonic-gate ipp->ipp_fields |= IPPF_DSTOPTS; 28260Sstevel@tonic-gate } 28270Sstevel@tonic-gate if (sticky) { 28285240Snordmark error = icmp_build_hdrs(icmp); 28290Sstevel@tonic-gate if (error != 0) 28300Sstevel@tonic-gate return (error); 28310Sstevel@tonic-gate } 28320Sstevel@tonic-gate break; 28330Sstevel@tonic-gate } 28340Sstevel@tonic-gate case IPV6_RTHDR: { 28350Sstevel@tonic-gate ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 28360Sstevel@tonic-gate 28370Sstevel@tonic-gate /* 28380Sstevel@tonic-gate * Sanity checks - minimum size, size a multiple of 28390Sstevel@tonic-gate * eight bytes, and matching size passed in. 28400Sstevel@tonic-gate */ 28410Sstevel@tonic-gate if (inlen != 0 && 28420Sstevel@tonic-gate inlen != (8 * (rt->ip6r_len + 1))) 28430Sstevel@tonic-gate return (EINVAL); 28440Sstevel@tonic-gate 28450Sstevel@tonic-gate if (checkonly) 28460Sstevel@tonic-gate break; 28470Sstevel@tonic-gate 28480Sstevel@tonic-gate if (inlen == 0) { 28490Sstevel@tonic-gate if (sticky && 28500Sstevel@tonic-gate (ipp->ipp_fields & IPPF_RTHDR) != 0) { 28510Sstevel@tonic-gate kmem_free(ipp->ipp_rthdr, 28520Sstevel@tonic-gate ipp->ipp_rthdrlen); 28530Sstevel@tonic-gate ipp->ipp_rthdr = NULL; 28540Sstevel@tonic-gate ipp->ipp_rthdrlen = 0; 28550Sstevel@tonic-gate } 28560Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_RTHDR; 28570Sstevel@tonic-gate ipp->ipp_sticky_ignored |= IPPF_RTHDR; 28580Sstevel@tonic-gate } else { 28591676Sjpk error = optcom_pkt_set(invalp, inlen, sticky, 28600Sstevel@tonic-gate (uchar_t **)&ipp->ipp_rthdr, 28611676Sjpk &ipp->ipp_rthdrlen, 0); 28620Sstevel@tonic-gate if (error != 0) 28630Sstevel@tonic-gate return (error); 28640Sstevel@tonic-gate ipp->ipp_fields |= IPPF_RTHDR; 28650Sstevel@tonic-gate } 28660Sstevel@tonic-gate if (sticky) { 28675240Snordmark error = icmp_build_hdrs(icmp); 28680Sstevel@tonic-gate if (error != 0) 28690Sstevel@tonic-gate return (error); 28700Sstevel@tonic-gate } 28710Sstevel@tonic-gate break; 28720Sstevel@tonic-gate } 28730Sstevel@tonic-gate 28740Sstevel@tonic-gate case IPV6_DONTFRAG: 28750Sstevel@tonic-gate if (checkonly) 28760Sstevel@tonic-gate break; 28770Sstevel@tonic-gate 28780Sstevel@tonic-gate if (onoff) { 28790Sstevel@tonic-gate ipp->ipp_fields |= IPPF_DONTFRAG; 28800Sstevel@tonic-gate } else { 28810Sstevel@tonic-gate ipp->ipp_fields &= ~IPPF_DONTFRAG; 28820Sstevel@tonic-gate } 28830Sstevel@tonic-gate break; 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate case IPV6_USE_MIN_MTU: 28860Sstevel@tonic-gate if (inlen != sizeof (int)) 28870Sstevel@tonic-gate return (EINVAL); 28880Sstevel@tonic-gate 28890Sstevel@tonic-gate if (*i1 < -1 || *i1 > 1) 28900Sstevel@tonic-gate return (EINVAL); 28910Sstevel@tonic-gate 28920Sstevel@tonic-gate if (checkonly) 28930Sstevel@tonic-gate break; 28940Sstevel@tonic-gate 28950Sstevel@tonic-gate ipp->ipp_fields |= IPPF_USE_MIN_MTU; 28960Sstevel@tonic-gate ipp->ipp_use_min_mtu = *i1; 28970Sstevel@tonic-gate break; 28980Sstevel@tonic-gate 28990Sstevel@tonic-gate /* 29000Sstevel@tonic-gate * This option can't be set. Its only returned via 29010Sstevel@tonic-gate * getsockopt() or ancillary data. 29020Sstevel@tonic-gate */ 29030Sstevel@tonic-gate case IPV6_PATHMTU: 29040Sstevel@tonic-gate return (EINVAL); 29050Sstevel@tonic-gate 29060Sstevel@tonic-gate case IPV6_BOUND_PIF: 29070Sstevel@tonic-gate case IPV6_SEC_OPT: 29080Sstevel@tonic-gate case IPV6_DONTFAILOVER_IF: 29090Sstevel@tonic-gate case IPV6_SRC_PREFERENCES: 29100Sstevel@tonic-gate case IPV6_V6ONLY: 29110Sstevel@tonic-gate /* Handled at IP level */ 29120Sstevel@tonic-gate return (-EINVAL); 29130Sstevel@tonic-gate default: 29140Sstevel@tonic-gate *outlenp = 0; 29150Sstevel@tonic-gate return (EINVAL); 29160Sstevel@tonic-gate } 29170Sstevel@tonic-gate break; 29180Sstevel@tonic-gate } /* end IPPROTO_IPV6 */ 29190Sstevel@tonic-gate 29200Sstevel@tonic-gate case IPPROTO_ICMPV6: 29210Sstevel@tonic-gate /* 29220Sstevel@tonic-gate * Only allow IPv6 option processing on IPv6 sockets. 29230Sstevel@tonic-gate */ 29240Sstevel@tonic-gate if (icmp->icmp_family != AF_INET6) { 29250Sstevel@tonic-gate *outlenp = 0; 29260Sstevel@tonic-gate return (ENOPROTOOPT); 29270Sstevel@tonic-gate } 29280Sstevel@tonic-gate if (icmp->icmp_proto != IPPROTO_ICMPV6) { 29290Sstevel@tonic-gate *outlenp = 0; 29300Sstevel@tonic-gate return (ENOPROTOOPT); 29310Sstevel@tonic-gate } 29320Sstevel@tonic-gate switch (name) { 29330Sstevel@tonic-gate case ICMP6_FILTER: 29340Sstevel@tonic-gate if (!checkonly) { 29350Sstevel@tonic-gate if ((inlen != 0) && 29360Sstevel@tonic-gate (inlen != sizeof (icmp6_filter_t))) 29370Sstevel@tonic-gate return (EINVAL); 29380Sstevel@tonic-gate 29390Sstevel@tonic-gate if (inlen == 0) { 29400Sstevel@tonic-gate if (icmp->icmp_filter != NULL) { 29410Sstevel@tonic-gate kmem_free(icmp->icmp_filter, 29420Sstevel@tonic-gate sizeof (icmp6_filter_t)); 29430Sstevel@tonic-gate icmp->icmp_filter = NULL; 29440Sstevel@tonic-gate } 29450Sstevel@tonic-gate } else { 29460Sstevel@tonic-gate if (icmp->icmp_filter == NULL) { 29470Sstevel@tonic-gate icmp->icmp_filter = kmem_alloc( 29480Sstevel@tonic-gate sizeof (icmp6_filter_t), 29490Sstevel@tonic-gate KM_NOSLEEP); 29500Sstevel@tonic-gate if (icmp->icmp_filter == NULL) { 29510Sstevel@tonic-gate *outlenp = 0; 29520Sstevel@tonic-gate return (ENOBUFS); 29530Sstevel@tonic-gate } 29540Sstevel@tonic-gate } 29550Sstevel@tonic-gate (void) bcopy(invalp, icmp->icmp_filter, 29560Sstevel@tonic-gate inlen); 29570Sstevel@tonic-gate } 29580Sstevel@tonic-gate } 29590Sstevel@tonic-gate break; 29600Sstevel@tonic-gate 29610Sstevel@tonic-gate default: 29620Sstevel@tonic-gate *outlenp = 0; 29630Sstevel@tonic-gate return (EINVAL); 29640Sstevel@tonic-gate } 29650Sstevel@tonic-gate break; 29660Sstevel@tonic-gate default: 29670Sstevel@tonic-gate *outlenp = 0; 29680Sstevel@tonic-gate return (EINVAL); 29690Sstevel@tonic-gate } 29700Sstevel@tonic-gate /* 29710Sstevel@tonic-gate * Common case of OK return with outval same as inval. 29720Sstevel@tonic-gate */ 29730Sstevel@tonic-gate if (invalp != outvalp) { 29740Sstevel@tonic-gate /* don't trust bcopy for identical src/dst */ 29750Sstevel@tonic-gate (void) bcopy(invalp, outvalp, inlen); 29760Sstevel@tonic-gate } 29770Sstevel@tonic-gate *outlenp = inlen; 29780Sstevel@tonic-gate return (0); 29790Sstevel@tonic-gate } 29805240Snordmark /* This routine sets socket options. */ 29815240Snordmark /* ARGSUSED */ 29825240Snordmark int 29835240Snordmark icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 29845240Snordmark uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 29855240Snordmark void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 29865240Snordmark { 29875240Snordmark icmp_t *icmp; 29885240Snordmark int err; 29895240Snordmark 29905240Snordmark icmp = Q_TO_ICMP(q); 29915240Snordmark 29925240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 29935240Snordmark err = icmp_opt_set_locked(q, optset_context, level, name, inlen, invalp, 29945240Snordmark outlenp, outvalp, thisdg_attrs, cr, mblk); 29955240Snordmark rw_exit(&icmp->icmp_rwlock); 29965240Snordmark return (err); 29975240Snordmark } 29980Sstevel@tonic-gate 29990Sstevel@tonic-gate /* 30000Sstevel@tonic-gate * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl, 30010Sstevel@tonic-gate * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum. 30020Sstevel@tonic-gate * The headers include ip6i_t (if needed), ip6_t, and any sticky extension 30030Sstevel@tonic-gate * headers. 30040Sstevel@tonic-gate * Returns failure if can't allocate memory. 30050Sstevel@tonic-gate */ 30060Sstevel@tonic-gate static int 30075240Snordmark icmp_build_hdrs(icmp_t *icmp) 30080Sstevel@tonic-gate { 30093448Sdh155122 icmp_stack_t *is = icmp->icmp_is; 30100Sstevel@tonic-gate uchar_t *hdrs; 30110Sstevel@tonic-gate uint_t hdrs_len; 30120Sstevel@tonic-gate ip6_t *ip6h; 30130Sstevel@tonic-gate ip6i_t *ip6i; 30140Sstevel@tonic-gate ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp; 30150Sstevel@tonic-gate 30165240Snordmark ASSERT(RW_WRITE_HELD(&icmp->icmp_rwlock)); 30170Sstevel@tonic-gate hdrs_len = ip_total_hdrs_len_v6(ipp); 30180Sstevel@tonic-gate ASSERT(hdrs_len != 0); 30190Sstevel@tonic-gate if (hdrs_len != icmp->icmp_sticky_hdrs_len) { 30200Sstevel@tonic-gate /* Need to reallocate */ 30210Sstevel@tonic-gate if (hdrs_len != 0) { 30220Sstevel@tonic-gate hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP); 30230Sstevel@tonic-gate if (hdrs == NULL) 30240Sstevel@tonic-gate return (ENOMEM); 30250Sstevel@tonic-gate } else { 30260Sstevel@tonic-gate hdrs = NULL; 30270Sstevel@tonic-gate } 30280Sstevel@tonic-gate if (icmp->icmp_sticky_hdrs_len != 0) { 30290Sstevel@tonic-gate kmem_free(icmp->icmp_sticky_hdrs, 30300Sstevel@tonic-gate icmp->icmp_sticky_hdrs_len); 30310Sstevel@tonic-gate } 30320Sstevel@tonic-gate icmp->icmp_sticky_hdrs = hdrs; 30330Sstevel@tonic-gate icmp->icmp_sticky_hdrs_len = hdrs_len; 30340Sstevel@tonic-gate } 30350Sstevel@tonic-gate ip_build_hdrs_v6(icmp->icmp_sticky_hdrs, 30360Sstevel@tonic-gate icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto); 30370Sstevel@tonic-gate 30380Sstevel@tonic-gate /* Set header fields not in ipp */ 30390Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_HAS_IP6I) { 30400Sstevel@tonic-gate ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs; 30410Sstevel@tonic-gate ip6h = (ip6_t *)&ip6i[1]; 30420Sstevel@tonic-gate 30430Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_RAW_CKSUM) { 30440Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM; 30450Sstevel@tonic-gate ip6i->ip6i_checksum_off = icmp->icmp_checksum_off; 30460Sstevel@tonic-gate } 30470Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_NO_CKSUM) { 30480Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM; 30490Sstevel@tonic-gate } 30500Sstevel@tonic-gate } else { 30510Sstevel@tonic-gate ip6h = (ip6_t *)icmp->icmp_sticky_hdrs; 30520Sstevel@tonic-gate } 30530Sstevel@tonic-gate 30540Sstevel@tonic-gate if (!(ipp->ipp_fields & IPPF_ADDR)) 30550Sstevel@tonic-gate ip6h->ip6_src = icmp->icmp_v6src; 30560Sstevel@tonic-gate 30570Sstevel@tonic-gate /* Try to get everything in a single mblk */ 30580Sstevel@tonic-gate if (hdrs_len > icmp->icmp_max_hdr_len) { 30590Sstevel@tonic-gate icmp->icmp_max_hdr_len = hdrs_len; 30605240Snordmark rw_exit(&icmp->icmp_rwlock); 30615240Snordmark (void) mi_set_sth_wroff(icmp->icmp_connp->conn_rq, 30625240Snordmark icmp->icmp_max_hdr_len + is->is_wroff_extra); 30635240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 30640Sstevel@tonic-gate } 30650Sstevel@tonic-gate return (0); 30660Sstevel@tonic-gate } 30670Sstevel@tonic-gate 30680Sstevel@tonic-gate /* 30690Sstevel@tonic-gate * This routine retrieves the value of an ND variable in a icmpparam_t 30700Sstevel@tonic-gate * structure. It is called through nd_getset when a user reads the 30710Sstevel@tonic-gate * variable. 30720Sstevel@tonic-gate */ 30730Sstevel@tonic-gate /* ARGSUSED */ 30740Sstevel@tonic-gate static int 30750Sstevel@tonic-gate icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 30760Sstevel@tonic-gate { 30770Sstevel@tonic-gate icmpparam_t *icmppa = (icmpparam_t *)cp; 30780Sstevel@tonic-gate 30790Sstevel@tonic-gate (void) mi_mpprintf(mp, "%d", icmppa->icmp_param_value); 30800Sstevel@tonic-gate return (0); 30810Sstevel@tonic-gate } 30820Sstevel@tonic-gate 30830Sstevel@tonic-gate /* 30840Sstevel@tonic-gate * Walk through the param array specified registering each element with the 30850Sstevel@tonic-gate * named dispatch (ND) handler. 30860Sstevel@tonic-gate */ 30870Sstevel@tonic-gate static boolean_t 30883448Sdh155122 icmp_param_register(IDP *ndp, icmpparam_t *icmppa, int cnt) 30890Sstevel@tonic-gate { 30900Sstevel@tonic-gate for (; cnt-- > 0; icmppa++) { 30910Sstevel@tonic-gate if (icmppa->icmp_param_name && icmppa->icmp_param_name[0]) { 30923448Sdh155122 if (!nd_load(ndp, icmppa->icmp_param_name, 30930Sstevel@tonic-gate icmp_param_get, icmp_param_set, 30940Sstevel@tonic-gate (caddr_t)icmppa)) { 30953448Sdh155122 nd_free(ndp); 30960Sstevel@tonic-gate return (B_FALSE); 30970Sstevel@tonic-gate } 30980Sstevel@tonic-gate } 30990Sstevel@tonic-gate } 31003448Sdh155122 if (!nd_load(ndp, "icmp_status", icmp_status_report, NULL, 31010Sstevel@tonic-gate NULL)) { 31023448Sdh155122 nd_free(ndp); 31030Sstevel@tonic-gate return (B_FALSE); 31040Sstevel@tonic-gate } 31050Sstevel@tonic-gate return (B_TRUE); 31060Sstevel@tonic-gate } 31070Sstevel@tonic-gate 31080Sstevel@tonic-gate /* This routine sets an ND variable in a icmpparam_t structure. */ 31090Sstevel@tonic-gate /* ARGSUSED */ 31100Sstevel@tonic-gate static int 31110Sstevel@tonic-gate icmp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 31120Sstevel@tonic-gate { 31130Sstevel@tonic-gate long new_value; 31140Sstevel@tonic-gate icmpparam_t *icmppa = (icmpparam_t *)cp; 31150Sstevel@tonic-gate 31160Sstevel@tonic-gate /* 31170Sstevel@tonic-gate * Fail the request if the new value does not lie within the 31180Sstevel@tonic-gate * required bounds. 31190Sstevel@tonic-gate */ 31200Sstevel@tonic-gate if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 31210Sstevel@tonic-gate new_value < icmppa->icmp_param_min || 31220Sstevel@tonic-gate new_value > icmppa->icmp_param_max) { 31230Sstevel@tonic-gate return (EINVAL); 31240Sstevel@tonic-gate } 31250Sstevel@tonic-gate /* Set the new value */ 31260Sstevel@tonic-gate icmppa->icmp_param_value = new_value; 31270Sstevel@tonic-gate return (0); 31280Sstevel@tonic-gate } 31295240Snordmark /*ARGSUSED2*/ 31300Sstevel@tonic-gate static void 31315240Snordmark icmp_input(void *arg1, mblk_t *mp, void *arg2) 31320Sstevel@tonic-gate { 31335240Snordmark conn_t *connp = (conn_t *)arg1; 31340Sstevel@tonic-gate struct T_unitdata_ind *tudi; 31350Sstevel@tonic-gate uchar_t *rptr; 31365240Snordmark icmp_t *icmp; 31375240Snordmark icmp_stack_t *is; 31380Sstevel@tonic-gate sin_t *sin; 31390Sstevel@tonic-gate sin6_t *sin6; 31400Sstevel@tonic-gate ip6_t *ip6h; 31410Sstevel@tonic-gate ip6i_t *ip6i; 31420Sstevel@tonic-gate mblk_t *mp1; 31430Sstevel@tonic-gate int hdr_len; 31440Sstevel@tonic-gate ipha_t *ipha; 31450Sstevel@tonic-gate int udi_size; /* Size of T_unitdata_ind */ 31460Sstevel@tonic-gate uint_t ipvers; 31470Sstevel@tonic-gate ip6_pkt_t ipp; 31480Sstevel@tonic-gate uint8_t nexthdr; 31493318Srshoaib ip_pktinfo_t *pinfo = NULL; 31500Sstevel@tonic-gate mblk_t *options_mp = NULL; 31510Sstevel@tonic-gate uint_t icmp_opt = 0; 31520Sstevel@tonic-gate boolean_t icmp_ipv6_recvhoplimit = B_FALSE; 31531676Sjpk uint_t hopstrip; 31540Sstevel@tonic-gate 31555240Snordmark ASSERT(connp->conn_flags & IPCL_RAWIPCONN); 31565240Snordmark 31575240Snordmark icmp = connp->conn_icmp; 31585240Snordmark is = icmp->icmp_is; 31595240Snordmark rptr = mp->b_rptr; 31605240Snordmark ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_CTL); 31615240Snordmark ASSERT(OK_32PTR(rptr)); 31625240Snordmark 31635240Snordmark /* 31645240Snordmark * IP should have prepended the options data in an M_CTL 31655240Snordmark * Check M_CTL "type" to make sure are not here bcos of 31665240Snordmark * a valid ICMP message 31675240Snordmark */ 31685240Snordmark if (DB_TYPE(mp) == M_CTL) { 31690Sstevel@tonic-gate /* 31705240Snordmark * FIXME: does IP still do this? 31710Sstevel@tonic-gate * IP sends up the IPSEC_IN message for handling IPSEC 31720Sstevel@tonic-gate * policy at the TCP level. We don't need it here. 31730Sstevel@tonic-gate */ 31740Sstevel@tonic-gate if (*(uint32_t *)(mp->b_rptr) == IPSEC_IN) { 31750Sstevel@tonic-gate mp1 = mp->b_cont; 31760Sstevel@tonic-gate freeb(mp); 31770Sstevel@tonic-gate mp = mp1; 31785240Snordmark rptr = mp->b_rptr; 31795240Snordmark } else if (MBLKL(mp) == sizeof (ip_pktinfo_t) && 31805240Snordmark ((ip_pktinfo_t *)mp->b_rptr)->ip_pkt_ulp_type == 31815240Snordmark IN_PKTINFO) { 31825240Snordmark /* 31835240Snordmark * IP_RECVIF or IP_RECVSLLA or IPF_RECVADDR information 31845240Snordmark * has been prepended to the packet by IP. We need to 31855240Snordmark * extract the mblk and adjust the rptr 31865240Snordmark */ 31873318Srshoaib pinfo = (ip_pktinfo_t *)mp->b_rptr; 31885240Snordmark options_mp = mp; 31895240Snordmark mp = mp->b_cont; 31905240Snordmark rptr = mp->b_rptr; 31915240Snordmark } else { 31925240Snordmark /* 31935240Snordmark * ICMP messages. 31945240Snordmark */ 31955240Snordmark icmp_icmp_error(connp->conn_rq, mp); 31960Sstevel@tonic-gate return; 31970Sstevel@tonic-gate } 31980Sstevel@tonic-gate } 31990Sstevel@tonic-gate 32000Sstevel@tonic-gate /* 32010Sstevel@tonic-gate * Discard message if it is misaligned or smaller than the IP header. 32020Sstevel@tonic-gate */ 32030Sstevel@tonic-gate if (!OK_32PTR(rptr) || (mp->b_wptr - rptr) < sizeof (ipha_t)) { 32040Sstevel@tonic-gate freemsg(mp); 32050Sstevel@tonic-gate if (options_mp != NULL) 32060Sstevel@tonic-gate freeb(options_mp); 32075240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipInErrors); 32080Sstevel@tonic-gate return; 32090Sstevel@tonic-gate } 32100Sstevel@tonic-gate ipvers = IPH_HDR_VERSION((ipha_t *)rptr); 32110Sstevel@tonic-gate 32120Sstevel@tonic-gate /* Handle M_DATA messages containing IP packets messages */ 32130Sstevel@tonic-gate if (ipvers == IPV4_VERSION) { 32140Sstevel@tonic-gate /* 32150Sstevel@tonic-gate * Special case where IP attaches 32160Sstevel@tonic-gate * the IRE needs to be handled so that we don't send up 32170Sstevel@tonic-gate * IRE to the user land. 32180Sstevel@tonic-gate */ 32190Sstevel@tonic-gate ipha = (ipha_t *)rptr; 32200Sstevel@tonic-gate hdr_len = IPH_HDR_LENGTH(ipha); 32210Sstevel@tonic-gate 32220Sstevel@tonic-gate if (ipha->ipha_protocol == IPPROTO_TCP) { 32230Sstevel@tonic-gate tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 32240Sstevel@tonic-gate 32250Sstevel@tonic-gate if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == 32260Sstevel@tonic-gate TH_SYN) && mp->b_cont != NULL) { 32270Sstevel@tonic-gate mp1 = mp->b_cont; 32280Sstevel@tonic-gate if (mp1->b_datap->db_type == IRE_DB_TYPE) { 32290Sstevel@tonic-gate freeb(mp1); 32300Sstevel@tonic-gate mp->b_cont = NULL; 32310Sstevel@tonic-gate } 32320Sstevel@tonic-gate } 32330Sstevel@tonic-gate } 32343448Sdh155122 if (is->is_bsd_compat) { 32350Sstevel@tonic-gate ushort_t len; 32360Sstevel@tonic-gate len = ntohs(ipha->ipha_length); 32370Sstevel@tonic-gate 32380Sstevel@tonic-gate if (mp->b_datap->db_ref > 1) { 32390Sstevel@tonic-gate /* 32400Sstevel@tonic-gate * Allocate a new IP header so that we can 32410Sstevel@tonic-gate * modify ipha_length. 32420Sstevel@tonic-gate */ 32430Sstevel@tonic-gate mblk_t *mp1; 32440Sstevel@tonic-gate 32450Sstevel@tonic-gate mp1 = allocb(hdr_len, BPRI_MED); 32460Sstevel@tonic-gate if (!mp1) { 32470Sstevel@tonic-gate freemsg(mp); 32480Sstevel@tonic-gate if (options_mp != NULL) 32490Sstevel@tonic-gate freeb(options_mp); 32505240Snordmark BUMP_MIB(&is->is_rawip_mib, 32513448Sdh155122 rawipInErrors); 32520Sstevel@tonic-gate return; 32530Sstevel@tonic-gate } 32540Sstevel@tonic-gate bcopy(rptr, mp1->b_rptr, hdr_len); 32550Sstevel@tonic-gate mp->b_rptr = rptr + hdr_len; 32560Sstevel@tonic-gate rptr = mp1->b_rptr; 32570Sstevel@tonic-gate ipha = (ipha_t *)rptr; 32580Sstevel@tonic-gate mp1->b_cont = mp; 32590Sstevel@tonic-gate mp1->b_wptr = rptr + hdr_len; 32600Sstevel@tonic-gate mp = mp1; 32610Sstevel@tonic-gate } 32620Sstevel@tonic-gate len -= hdr_len; 32630Sstevel@tonic-gate ipha->ipha_length = htons(len); 32640Sstevel@tonic-gate } 32650Sstevel@tonic-gate } 32660Sstevel@tonic-gate 32670Sstevel@tonic-gate /* 32680Sstevel@tonic-gate * This is the inbound data path. Packets are passed upstream as 32690Sstevel@tonic-gate * T_UNITDATA_IND messages with full IP headers still attached. 32700Sstevel@tonic-gate */ 32710Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 32720Sstevel@tonic-gate ASSERT(ipvers == IPV4_VERSION); 32730Sstevel@tonic-gate udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin_t); 32745267Snordmark if (icmp->icmp_recvif && (pinfo != NULL) && 32753318Srshoaib (pinfo->ip_pkt_flags & IPF_RECVIF)) { 32760Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + 32770Sstevel@tonic-gate sizeof (uint_t); 32780Sstevel@tonic-gate } 32793318Srshoaib 32805267Snordmark if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) && 32813318Srshoaib (pinfo->ip_pkt_flags & IPF_RECVADDR)) { 32823318Srshoaib udi_size += sizeof (struct T_opthdr) + 32833318Srshoaib sizeof (struct in_pktinfo); 32843318Srshoaib } 32853318Srshoaib 32861673Sgt145670 /* 32871673Sgt145670 * If SO_TIMESTAMP is set allocate the appropriate sized 32881673Sgt145670 * buffer. Since gethrestime() expects a pointer aligned 32891673Sgt145670 * argument, we allocate space necessary for extra 32901673Sgt145670 * alignment (even though it might not be used). 32911673Sgt145670 */ 32921673Sgt145670 if (icmp->icmp_timestamp) { 32931673Sgt145670 udi_size += sizeof (struct T_opthdr) + 32941673Sgt145670 sizeof (timestruc_t) + _POINTER_ALIGNMENT; 32951673Sgt145670 } 32960Sstevel@tonic-gate mp1 = allocb(udi_size, BPRI_MED); 32970Sstevel@tonic-gate if (mp1 == NULL) { 32980Sstevel@tonic-gate freemsg(mp); 32990Sstevel@tonic-gate if (options_mp != NULL) 33000Sstevel@tonic-gate freeb(options_mp); 33015240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipInErrors); 33020Sstevel@tonic-gate return; 33030Sstevel@tonic-gate } 33040Sstevel@tonic-gate mp1->b_cont = mp; 33050Sstevel@tonic-gate mp = mp1; 33060Sstevel@tonic-gate tudi = (struct T_unitdata_ind *)mp->b_rptr; 33070Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 33080Sstevel@tonic-gate mp->b_wptr = (uchar_t *)tudi + udi_size; 33090Sstevel@tonic-gate tudi->PRIM_type = T_UNITDATA_IND; 33100Sstevel@tonic-gate tudi->SRC_length = sizeof (sin_t); 33110Sstevel@tonic-gate tudi->SRC_offset = sizeof (struct T_unitdata_ind); 33120Sstevel@tonic-gate sin = (sin_t *)&tudi[1]; 33130Sstevel@tonic-gate *sin = sin_null; 33140Sstevel@tonic-gate sin->sin_family = AF_INET; 33150Sstevel@tonic-gate sin->sin_addr.s_addr = ipha->ipha_src; 33160Sstevel@tonic-gate tudi->OPT_offset = sizeof (struct T_unitdata_ind) + 33170Sstevel@tonic-gate sizeof (sin_t); 33180Sstevel@tonic-gate udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t)); 33190Sstevel@tonic-gate tudi->OPT_length = udi_size; 33200Sstevel@tonic-gate 33210Sstevel@tonic-gate /* 33220Sstevel@tonic-gate * Add options if IP_RECVIF is set 33230Sstevel@tonic-gate */ 33240Sstevel@tonic-gate if (udi_size != 0) { 33250Sstevel@tonic-gate char *dstopt; 33260Sstevel@tonic-gate 33270Sstevel@tonic-gate dstopt = (char *)&sin[1]; 33285267Snordmark if (icmp->icmp_recvif && (pinfo != NULL) && 33293318Srshoaib (pinfo->ip_pkt_flags & IPF_RECVIF)) { 33300Sstevel@tonic-gate 33310Sstevel@tonic-gate struct T_opthdr *toh; 33320Sstevel@tonic-gate uint_t *dstptr; 33330Sstevel@tonic-gate 33340Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 33350Sstevel@tonic-gate toh->level = IPPROTO_IP; 33360Sstevel@tonic-gate toh->name = IP_RECVIF; 33370Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 33385240Snordmark sizeof (uint_t); 33390Sstevel@tonic-gate toh->status = 0; 33400Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 33410Sstevel@tonic-gate dstptr = (uint_t *)dstopt; 33423318Srshoaib *dstptr = pinfo->ip_pkt_ifindex; 33430Sstevel@tonic-gate dstopt += sizeof (uint_t); 33440Sstevel@tonic-gate udi_size -= toh->len; 33450Sstevel@tonic-gate } 33461673Sgt145670 if (icmp->icmp_timestamp) { 33471673Sgt145670 struct T_opthdr *toh; 33481673Sgt145670 33491673Sgt145670 toh = (struct T_opthdr *)dstopt; 33501673Sgt145670 toh->level = SOL_SOCKET; 33511673Sgt145670 toh->name = SCM_TIMESTAMP; 33521673Sgt145670 toh->len = sizeof (struct T_opthdr) + 33531673Sgt145670 sizeof (timestruc_t) + _POINTER_ALIGNMENT; 33541673Sgt145670 toh->status = 0; 33551673Sgt145670 dstopt += sizeof (struct T_opthdr); 33561673Sgt145670 /* Align for gethrestime() */ 33571673Sgt145670 dstopt = (char *)P2ROUNDUP((intptr_t)dstopt, 33581673Sgt145670 sizeof (intptr_t)); 33591673Sgt145670 gethrestime((timestruc_t *)dstopt); 33603318Srshoaib dstopt = (char *)toh + toh->len; 33613318Srshoaib udi_size -= toh->len; 33623318Srshoaib } 33635267Snordmark if (icmp->icmp_ip_recvpktinfo && (pinfo != NULL) && 33643318Srshoaib (pinfo->ip_pkt_flags & IPF_RECVADDR)) { 33653318Srshoaib struct T_opthdr *toh; 33663318Srshoaib struct in_pktinfo *pktinfop; 33673318Srshoaib 33683318Srshoaib toh = (struct T_opthdr *)dstopt; 33693318Srshoaib toh->level = IPPROTO_IP; 33703318Srshoaib toh->name = IP_PKTINFO; 33713318Srshoaib toh->len = sizeof (struct T_opthdr) + 33723318Srshoaib sizeof (in_pktinfo_t); 33733318Srshoaib toh->status = 0; 33743318Srshoaib dstopt += sizeof (struct T_opthdr); 33753318Srshoaib pktinfop = (struct in_pktinfo *)dstopt; 33763318Srshoaib pktinfop->ipi_ifindex = pinfo->ip_pkt_ifindex; 33773318Srshoaib pktinfop->ipi_spec_dst = 33783318Srshoaib pinfo->ip_pkt_match_addr; 33793318Srshoaib 33803318Srshoaib pktinfop->ipi_addr.s_addr = ipha->ipha_dst; 33813318Srshoaib 33823318Srshoaib dstopt += sizeof (struct in_pktinfo); 33831673Sgt145670 udi_size -= toh->len; 33841673Sgt145670 } 33850Sstevel@tonic-gate 33860Sstevel@tonic-gate /* Consumed all of allocated space */ 33870Sstevel@tonic-gate ASSERT(udi_size == 0); 33880Sstevel@tonic-gate } 33890Sstevel@tonic-gate 33905267Snordmark if (options_mp != NULL) 33915267Snordmark freeb(options_mp); 33925267Snordmark 33935240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams); 33945240Snordmark putnext(connp->conn_rq, mp); 33950Sstevel@tonic-gate return; 33960Sstevel@tonic-gate } 33970Sstevel@tonic-gate 33980Sstevel@tonic-gate /* 33990Sstevel@tonic-gate * We don't need options_mp in the IPv6 path. 34000Sstevel@tonic-gate */ 34010Sstevel@tonic-gate if (options_mp != NULL) { 34020Sstevel@tonic-gate freeb(options_mp); 34030Sstevel@tonic-gate options_mp = NULL; 34040Sstevel@tonic-gate } 34050Sstevel@tonic-gate 34060Sstevel@tonic-gate /* 34070Sstevel@tonic-gate * Discard message if it is smaller than the IPv6 header 34080Sstevel@tonic-gate * or if the header is malformed. 34090Sstevel@tonic-gate */ 34100Sstevel@tonic-gate if ((mp->b_wptr - rptr) < sizeof (ip6_t) || 34110Sstevel@tonic-gate IPH_HDR_VERSION((ipha_t *)rptr) != IPV6_VERSION || 34120Sstevel@tonic-gate icmp->icmp_family != AF_INET6) { 34130Sstevel@tonic-gate freemsg(mp); 34145240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipInErrors); 34150Sstevel@tonic-gate return; 34160Sstevel@tonic-gate } 34170Sstevel@tonic-gate 34180Sstevel@tonic-gate /* Initialize */ 34190Sstevel@tonic-gate ipp.ipp_fields = 0; 34201676Sjpk hopstrip = 0; 34210Sstevel@tonic-gate 34220Sstevel@tonic-gate ip6h = (ip6_t *)rptr; 34230Sstevel@tonic-gate /* 34240Sstevel@tonic-gate * Call on ip_find_hdr_v6 which gets the total hdr len 34250Sstevel@tonic-gate * as well as individual lenghts of ext hdrs (and ptrs to 34260Sstevel@tonic-gate * them). 34270Sstevel@tonic-gate */ 34280Sstevel@tonic-gate if (ip6h->ip6_nxt != icmp->icmp_proto) { 34290Sstevel@tonic-gate /* Look for ifindex information */ 34300Sstevel@tonic-gate if (ip6h->ip6_nxt == IPPROTO_RAW) { 34310Sstevel@tonic-gate ip6i = (ip6i_t *)ip6h; 34320Sstevel@tonic-gate if (ip6i->ip6i_flags & IP6I_IFINDEX) { 34330Sstevel@tonic-gate ASSERT(ip6i->ip6i_ifindex != 0); 34340Sstevel@tonic-gate ipp.ipp_fields |= IPPF_IFINDEX; 34350Sstevel@tonic-gate ipp.ipp_ifindex = ip6i->ip6i_ifindex; 34360Sstevel@tonic-gate } 34370Sstevel@tonic-gate rptr = (uchar_t *)&ip6i[1]; 34380Sstevel@tonic-gate mp->b_rptr = rptr; 34390Sstevel@tonic-gate if (rptr == mp->b_wptr) { 34400Sstevel@tonic-gate mp1 = mp->b_cont; 34410Sstevel@tonic-gate freeb(mp); 34420Sstevel@tonic-gate mp = mp1; 34430Sstevel@tonic-gate rptr = mp->b_rptr; 34440Sstevel@tonic-gate } 34450Sstevel@tonic-gate ASSERT(mp->b_wptr - rptr >= IPV6_HDR_LEN); 34460Sstevel@tonic-gate ip6h = (ip6_t *)rptr; 34470Sstevel@tonic-gate } 34480Sstevel@tonic-gate hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdr); 34491676Sjpk 34501676Sjpk /* 34511676Sjpk * We need to lie a bit to the user because users inside 34521676Sjpk * labeled compartments should not see their own labels. We 34531676Sjpk * assume that in all other respects IP has checked the label, 34541676Sjpk * and that the label is always first among the options. (If 34551676Sjpk * it's not first, then this code won't see it, and the option 34561676Sjpk * will be passed along to the user.) 34571676Sjpk * 34581676Sjpk * If we had multilevel ICMP sockets, then the following code 34591676Sjpk * should be skipped for them to allow the user to see the 34601676Sjpk * label. 34611676Sjpk * 34621676Sjpk * Alignment restrictions in the definition of IP options 34631676Sjpk * (namely, the requirement that the 4-octet DOI goes on a 34641676Sjpk * 4-octet boundary) mean that we know exactly where the option 34651676Sjpk * should start, but we're lenient for other hosts. 34661676Sjpk * 34671676Sjpk * Note that there are no multilevel ICMP or raw IP sockets 34681676Sjpk * yet, thus nobody ever sees the IP6OPT_LS option. 34691676Sjpk */ 34701676Sjpk if ((ipp.ipp_fields & IPPF_HOPOPTS) && 34711676Sjpk ipp.ipp_hopoptslen > 5 && is_system_labeled()) { 34721676Sjpk const uchar_t *ucp = 34731676Sjpk (const uchar_t *)ipp.ipp_hopopts + 2; 34741676Sjpk int remlen = ipp.ipp_hopoptslen - 2; 34751676Sjpk 34761676Sjpk while (remlen > 0) { 34771676Sjpk if (*ucp == IP6OPT_PAD1) { 34781676Sjpk remlen--; 34791676Sjpk ucp++; 34801676Sjpk } else if (*ucp == IP6OPT_PADN) { 34811676Sjpk remlen -= ucp[1] + 2; 34821676Sjpk ucp += ucp[1] + 2; 34831676Sjpk } else if (*ucp == ip6opt_ls) { 34841676Sjpk hopstrip = (ucp - 34851676Sjpk (const uchar_t *)ipp.ipp_hopopts) + 34861676Sjpk ucp[1] + 2; 34871676Sjpk hopstrip = (hopstrip + 7) & ~7; 34881676Sjpk break; 34891676Sjpk } else { 34901676Sjpk /* label option must be first */ 34911676Sjpk break; 34921676Sjpk } 34931676Sjpk } 34941676Sjpk } 34950Sstevel@tonic-gate } else { 34960Sstevel@tonic-gate hdr_len = IPV6_HDR_LEN; 34970Sstevel@tonic-gate ip6i = NULL; 34980Sstevel@tonic-gate nexthdr = ip6h->ip6_nxt; 34990Sstevel@tonic-gate } 35000Sstevel@tonic-gate /* 35010Sstevel@tonic-gate * One special case where IP attaches the IRE needs to 35020Sstevel@tonic-gate * be handled so that we don't send up IRE to the user land. 35030Sstevel@tonic-gate */ 35040Sstevel@tonic-gate if (nexthdr == IPPROTO_TCP) { 35050Sstevel@tonic-gate tcph_t *tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 35060Sstevel@tonic-gate 35070Sstevel@tonic-gate if (((tcph->th_flags[0] & (TH_SYN|TH_ACK)) == TH_SYN) && 35080Sstevel@tonic-gate mp->b_cont != NULL) { 35090Sstevel@tonic-gate mp1 = mp->b_cont; 35100Sstevel@tonic-gate if (mp1->b_datap->db_type == IRE_DB_TYPE) { 35110Sstevel@tonic-gate freeb(mp1); 35120Sstevel@tonic-gate mp->b_cont = NULL; 35130Sstevel@tonic-gate } 35140Sstevel@tonic-gate } 35150Sstevel@tonic-gate } 35160Sstevel@tonic-gate /* 35170Sstevel@tonic-gate * Check a filter for ICMPv6 types if needed. 35180Sstevel@tonic-gate * Verify raw checksums if needed. 35190Sstevel@tonic-gate */ 35200Sstevel@tonic-gate if (icmp->icmp_filter != NULL || icmp->icmp_raw_checksum) { 35210Sstevel@tonic-gate if (icmp->icmp_filter != NULL) { 35220Sstevel@tonic-gate int type; 35230Sstevel@tonic-gate 35240Sstevel@tonic-gate /* Assumes that IP has done the pullupmsg */ 35250Sstevel@tonic-gate type = mp->b_rptr[hdr_len]; 35260Sstevel@tonic-gate 35270Sstevel@tonic-gate ASSERT(mp->b_rptr + hdr_len <= mp->b_wptr); 35280Sstevel@tonic-gate if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) { 35290Sstevel@tonic-gate freemsg(mp); 35300Sstevel@tonic-gate return; 35310Sstevel@tonic-gate } 35320Sstevel@tonic-gate } else { 35330Sstevel@tonic-gate /* Checksum */ 35340Sstevel@tonic-gate uint16_t *up; 35350Sstevel@tonic-gate uint32_t sum; 35360Sstevel@tonic-gate int remlen; 35370Sstevel@tonic-gate 35380Sstevel@tonic-gate up = (uint16_t *)&ip6h->ip6_src; 35390Sstevel@tonic-gate 35400Sstevel@tonic-gate remlen = msgdsize(mp) - hdr_len; 35410Sstevel@tonic-gate sum = htons(icmp->icmp_proto + remlen) 35420Sstevel@tonic-gate + up[0] + up[1] + up[2] + up[3] 35430Sstevel@tonic-gate + up[4] + up[5] + up[6] + up[7] 35440Sstevel@tonic-gate + up[8] + up[9] + up[10] + up[11] 35450Sstevel@tonic-gate + up[12] + up[13] + up[14] + up[15]; 35460Sstevel@tonic-gate sum = (sum & 0xffff) + (sum >> 16); 35470Sstevel@tonic-gate sum = IP_CSUM(mp, hdr_len, sum); 35480Sstevel@tonic-gate if (sum != 0) { 35490Sstevel@tonic-gate /* IPv6 RAW checksum failed */ 35500Sstevel@tonic-gate ip0dbg(("icmp_rput: RAW checksum " 35510Sstevel@tonic-gate "failed %x\n", sum)); 35520Sstevel@tonic-gate freemsg(mp); 35535240Snordmark BUMP_MIB(&is->is_rawip_mib, 35543448Sdh155122 rawipInCksumErrs); 35550Sstevel@tonic-gate return; 35560Sstevel@tonic-gate } 35570Sstevel@tonic-gate } 35580Sstevel@tonic-gate } 35590Sstevel@tonic-gate /* Skip all the IPv6 headers per API */ 35600Sstevel@tonic-gate mp->b_rptr += hdr_len; 35610Sstevel@tonic-gate 35620Sstevel@tonic-gate udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t); 35630Sstevel@tonic-gate 35640Sstevel@tonic-gate /* 35650Sstevel@tonic-gate * We use local variables icmp_opt and icmp_ipv6_recvhoplimit to 35660Sstevel@tonic-gate * maintain state information, instead of relying on icmp_t 35670Sstevel@tonic-gate * structure, since there arent any locks protecting these members 35680Sstevel@tonic-gate * and there is a window where there might be a race between a 35690Sstevel@tonic-gate * thread setting options on the write side and a thread reading 35700Sstevel@tonic-gate * these options on the read size. 35710Sstevel@tonic-gate */ 35720Sstevel@tonic-gate if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS| 35730Sstevel@tonic-gate IPPF_RTHDR|IPPF_IFINDEX)) { 35740Sstevel@tonic-gate if (icmp->icmp_ipv6_recvhopopts && 35751676Sjpk (ipp.ipp_fields & IPPF_HOPOPTS) && 35761676Sjpk ipp.ipp_hopoptslen > hopstrip) { 35770Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + 35781676Sjpk ipp.ipp_hopoptslen - hopstrip; 35790Sstevel@tonic-gate icmp_opt |= IPPF_HOPOPTS; 35800Sstevel@tonic-gate } 35810Sstevel@tonic-gate if ((icmp->icmp_ipv6_recvdstopts || 35825240Snordmark icmp->icmp_old_ipv6_recvdstopts) && 35830Sstevel@tonic-gate (ipp.ipp_fields & IPPF_DSTOPTS)) { 35840Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + 35850Sstevel@tonic-gate ipp.ipp_dstoptslen; 35860Sstevel@tonic-gate icmp_opt |= IPPF_DSTOPTS; 35870Sstevel@tonic-gate } 35880Sstevel@tonic-gate if (((icmp->icmp_ipv6_recvdstopts && 35890Sstevel@tonic-gate icmp->icmp_ipv6_recvrthdr && 35900Sstevel@tonic-gate (ipp.ipp_fields & IPPF_RTHDR)) || 35910Sstevel@tonic-gate icmp->icmp_ipv6_recvrtdstopts) && 35920Sstevel@tonic-gate (ipp.ipp_fields & IPPF_RTDSTOPTS)) { 35930Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + 35940Sstevel@tonic-gate ipp.ipp_rtdstoptslen; 35950Sstevel@tonic-gate icmp_opt |= IPPF_RTDSTOPTS; 35960Sstevel@tonic-gate } 35970Sstevel@tonic-gate if (icmp->icmp_ipv6_recvrthdr && 35980Sstevel@tonic-gate (ipp.ipp_fields & IPPF_RTHDR)) { 35990Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + 36000Sstevel@tonic-gate ipp.ipp_rthdrlen; 36010Sstevel@tonic-gate icmp_opt |= IPPF_RTHDR; 36020Sstevel@tonic-gate } 36033318Srshoaib if (icmp->icmp_ip_recvpktinfo && 36040Sstevel@tonic-gate (ipp.ipp_fields & IPPF_IFINDEX)) { 36050Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + 36060Sstevel@tonic-gate sizeof (struct in6_pktinfo); 36070Sstevel@tonic-gate icmp_opt |= IPPF_IFINDEX; 36080Sstevel@tonic-gate } 36090Sstevel@tonic-gate } 36100Sstevel@tonic-gate if (icmp->icmp_ipv6_recvhoplimit) { 36110Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + sizeof (int); 36120Sstevel@tonic-gate icmp_ipv6_recvhoplimit = B_TRUE; 36130Sstevel@tonic-gate } 36140Sstevel@tonic-gate 36150Sstevel@tonic-gate if (icmp->icmp_ipv6_recvtclass) 36160Sstevel@tonic-gate udi_size += sizeof (struct T_opthdr) + sizeof (int); 36170Sstevel@tonic-gate 36185401Snordmark /* 36195401Snordmark * If SO_TIMESTAMP is set allocate the appropriate sized 36205401Snordmark * buffer. Since gethrestime() expects a pointer aligned 36215401Snordmark * argument, we allocate space necessary for extra 36225401Snordmark * alignment (even though it might not be used). 36235401Snordmark */ 36245401Snordmark if (icmp->icmp_timestamp) { 36255401Snordmark udi_size += sizeof (struct T_opthdr) + 36265401Snordmark sizeof (timestruc_t) + _POINTER_ALIGNMENT; 36275401Snordmark } 36285401Snordmark 36290Sstevel@tonic-gate mp1 = allocb(udi_size, BPRI_MED); 36300Sstevel@tonic-gate if (mp1 == NULL) { 36310Sstevel@tonic-gate freemsg(mp); 36325240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipInErrors); 36330Sstevel@tonic-gate return; 36340Sstevel@tonic-gate } 36350Sstevel@tonic-gate mp1->b_cont = mp; 36360Sstevel@tonic-gate mp = mp1; 36370Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 36380Sstevel@tonic-gate tudi = (struct T_unitdata_ind *)mp->b_rptr; 36390Sstevel@tonic-gate mp->b_wptr = (uchar_t *)tudi + udi_size; 36400Sstevel@tonic-gate tudi->PRIM_type = T_UNITDATA_IND; 36410Sstevel@tonic-gate tudi->SRC_length = sizeof (sin6_t); 36420Sstevel@tonic-gate tudi->SRC_offset = sizeof (struct T_unitdata_ind); 36430Sstevel@tonic-gate tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t); 36440Sstevel@tonic-gate udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t)); 36450Sstevel@tonic-gate tudi->OPT_length = udi_size; 36460Sstevel@tonic-gate sin6 = (sin6_t *)&tudi[1]; 36470Sstevel@tonic-gate sin6->sin6_port = 0; 36480Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 36490Sstevel@tonic-gate 36500Sstevel@tonic-gate sin6->sin6_addr = ip6h->ip6_src; 36510Sstevel@tonic-gate /* No sin6_flowinfo per API */ 36520Sstevel@tonic-gate sin6->sin6_flowinfo = 0; 36530Sstevel@tonic-gate /* For link-scope source pass up scope id */ 36540Sstevel@tonic-gate if ((ipp.ipp_fields & IPPF_IFINDEX) && 36550Sstevel@tonic-gate IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 36560Sstevel@tonic-gate sin6->sin6_scope_id = ipp.ipp_ifindex; 36570Sstevel@tonic-gate else 36580Sstevel@tonic-gate sin6->sin6_scope_id = 0; 36590Sstevel@tonic-gate 36600Sstevel@tonic-gate sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 36613448Sdh155122 icmp->icmp_zoneid, is->is_netstack); 36620Sstevel@tonic-gate 36630Sstevel@tonic-gate if (udi_size != 0) { 36640Sstevel@tonic-gate uchar_t *dstopt; 36650Sstevel@tonic-gate 36660Sstevel@tonic-gate dstopt = (uchar_t *)&sin6[1]; 36670Sstevel@tonic-gate if (icmp_opt & IPPF_IFINDEX) { 36680Sstevel@tonic-gate struct T_opthdr *toh; 36690Sstevel@tonic-gate struct in6_pktinfo *pkti; 36700Sstevel@tonic-gate 36710Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 36720Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 36730Sstevel@tonic-gate toh->name = IPV6_PKTINFO; 36740Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 36750Sstevel@tonic-gate sizeof (*pkti); 36760Sstevel@tonic-gate toh->status = 0; 36770Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 36780Sstevel@tonic-gate pkti = (struct in6_pktinfo *)dstopt; 36790Sstevel@tonic-gate pkti->ipi6_addr = ip6h->ip6_dst; 36800Sstevel@tonic-gate pkti->ipi6_ifindex = ipp.ipp_ifindex; 36810Sstevel@tonic-gate dstopt += sizeof (*pkti); 36820Sstevel@tonic-gate udi_size -= toh->len; 36830Sstevel@tonic-gate } 36840Sstevel@tonic-gate if (icmp_ipv6_recvhoplimit) { 36850Sstevel@tonic-gate struct T_opthdr *toh; 36860Sstevel@tonic-gate 36870Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 36880Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 36890Sstevel@tonic-gate toh->name = IPV6_HOPLIMIT; 36900Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 36910Sstevel@tonic-gate sizeof (uint_t); 36920Sstevel@tonic-gate toh->status = 0; 36930Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 36940Sstevel@tonic-gate *(uint_t *)dstopt = ip6h->ip6_hops; 36950Sstevel@tonic-gate dstopt += sizeof (uint_t); 36960Sstevel@tonic-gate udi_size -= toh->len; 36970Sstevel@tonic-gate } 36980Sstevel@tonic-gate if (icmp->icmp_ipv6_recvtclass) { 36990Sstevel@tonic-gate struct T_opthdr *toh; 37000Sstevel@tonic-gate 37010Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 37020Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 37030Sstevel@tonic-gate toh->name = IPV6_TCLASS; 37040Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 37050Sstevel@tonic-gate sizeof (uint_t); 37060Sstevel@tonic-gate toh->status = 0; 37070Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 37080Sstevel@tonic-gate *(uint_t *)dstopt = IPV6_FLOW_TCLASS(ip6h->ip6_flow); 37090Sstevel@tonic-gate dstopt += sizeof (uint_t); 37100Sstevel@tonic-gate udi_size -= toh->len; 37110Sstevel@tonic-gate } 37125401Snordmark if (icmp->icmp_timestamp) { 37135401Snordmark struct T_opthdr *toh; 37145401Snordmark 37155401Snordmark toh = (struct T_opthdr *)dstopt; 37165401Snordmark toh->level = SOL_SOCKET; 37175401Snordmark toh->name = SCM_TIMESTAMP; 37185401Snordmark toh->len = sizeof (struct T_opthdr) + 37195401Snordmark sizeof (timestruc_t) + _POINTER_ALIGNMENT; 37205401Snordmark toh->status = 0; 37215401Snordmark dstopt += sizeof (struct T_opthdr); 37225401Snordmark /* Align for gethrestime() */ 37235401Snordmark dstopt = (uchar_t *)P2ROUNDUP((intptr_t)dstopt, 37245401Snordmark sizeof (intptr_t)); 37255401Snordmark gethrestime((timestruc_t *)dstopt); 37265401Snordmark dstopt = (uchar_t *)toh + toh->len; 37275401Snordmark udi_size -= toh->len; 37285401Snordmark } 37290Sstevel@tonic-gate if (icmp_opt & IPPF_HOPOPTS) { 37300Sstevel@tonic-gate struct T_opthdr *toh; 37310Sstevel@tonic-gate 37320Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 37330Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 37340Sstevel@tonic-gate toh->name = IPV6_HOPOPTS; 37350Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 37361676Sjpk ipp.ipp_hopoptslen - hopstrip; 37370Sstevel@tonic-gate toh->status = 0; 37380Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 37391676Sjpk bcopy((char *)ipp.ipp_hopopts + hopstrip, dstopt, 37401676Sjpk ipp.ipp_hopoptslen - hopstrip); 37411676Sjpk if (hopstrip > 0) { 37421676Sjpk /* copy next header value and fake length */ 37431676Sjpk dstopt[0] = ((uchar_t *)ipp.ipp_hopopts)[0]; 37441676Sjpk dstopt[1] = ((uchar_t *)ipp.ipp_hopopts)[1] - 37451676Sjpk hopstrip / 8; 37461676Sjpk } 37471676Sjpk dstopt += ipp.ipp_hopoptslen - hopstrip; 37480Sstevel@tonic-gate udi_size -= toh->len; 37490Sstevel@tonic-gate } 37500Sstevel@tonic-gate if (icmp_opt & IPPF_RTDSTOPTS) { 37510Sstevel@tonic-gate struct T_opthdr *toh; 37520Sstevel@tonic-gate 37530Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 37540Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 37550Sstevel@tonic-gate toh->name = IPV6_DSTOPTS; 37560Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 37570Sstevel@tonic-gate ipp.ipp_rtdstoptslen; 37580Sstevel@tonic-gate toh->status = 0; 37590Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 37600Sstevel@tonic-gate bcopy(ipp.ipp_rtdstopts, dstopt, 37610Sstevel@tonic-gate ipp.ipp_rtdstoptslen); 37620Sstevel@tonic-gate dstopt += ipp.ipp_rtdstoptslen; 37630Sstevel@tonic-gate udi_size -= toh->len; 37640Sstevel@tonic-gate } 37650Sstevel@tonic-gate if (icmp_opt & IPPF_RTHDR) { 37660Sstevel@tonic-gate struct T_opthdr *toh; 37670Sstevel@tonic-gate 37680Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 37690Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 37700Sstevel@tonic-gate toh->name = IPV6_RTHDR; 37710Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 37720Sstevel@tonic-gate ipp.ipp_rthdrlen; 37730Sstevel@tonic-gate toh->status = 0; 37740Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 37750Sstevel@tonic-gate bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen); 37760Sstevel@tonic-gate dstopt += ipp.ipp_rthdrlen; 37770Sstevel@tonic-gate udi_size -= toh->len; 37780Sstevel@tonic-gate } 37790Sstevel@tonic-gate if (icmp_opt & IPPF_DSTOPTS) { 37800Sstevel@tonic-gate struct T_opthdr *toh; 37810Sstevel@tonic-gate 37820Sstevel@tonic-gate toh = (struct T_opthdr *)dstopt; 37830Sstevel@tonic-gate toh->level = IPPROTO_IPV6; 37840Sstevel@tonic-gate toh->name = IPV6_DSTOPTS; 37850Sstevel@tonic-gate toh->len = sizeof (struct T_opthdr) + 37860Sstevel@tonic-gate ipp.ipp_dstoptslen; 37870Sstevel@tonic-gate toh->status = 0; 37880Sstevel@tonic-gate dstopt += sizeof (struct T_opthdr); 37890Sstevel@tonic-gate bcopy(ipp.ipp_dstopts, dstopt, 37900Sstevel@tonic-gate ipp.ipp_dstoptslen); 37910Sstevel@tonic-gate dstopt += ipp.ipp_dstoptslen; 37920Sstevel@tonic-gate udi_size -= toh->len; 37930Sstevel@tonic-gate } 37940Sstevel@tonic-gate /* Consumed all of allocated space */ 37950Sstevel@tonic-gate ASSERT(udi_size == 0); 37960Sstevel@tonic-gate } 37975240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams); 37985240Snordmark putnext(connp->conn_rq, mp); 37995240Snordmark } 38005240Snordmark 38015240Snordmark /* 38025240Snordmark * Handle the results of a T_BIND_REQ whether deferred by IP or handled 38035240Snordmark * immediately. 38045240Snordmark */ 38055240Snordmark static void 38065240Snordmark icmp_bind_result(conn_t *connp, mblk_t *mp) 38075240Snordmark { 38085240Snordmark struct T_error_ack *tea; 38095240Snordmark 38105240Snordmark switch (mp->b_datap->db_type) { 38115240Snordmark case M_PROTO: 38125240Snordmark case M_PCPROTO: 38135240Snordmark /* M_PROTO messages contain some type of TPI message. */ 38145240Snordmark if ((mp->b_wptr - mp->b_rptr) < sizeof (t_scalar_t)) { 38155240Snordmark freemsg(mp); 38165240Snordmark return; 38175240Snordmark } 38185240Snordmark tea = (struct T_error_ack *)mp->b_rptr; 38195240Snordmark 38205240Snordmark switch (tea->PRIM_type) { 38215240Snordmark case T_ERROR_ACK: 38225240Snordmark switch (tea->ERROR_prim) { 38235240Snordmark case O_T_BIND_REQ: 38245240Snordmark case T_BIND_REQ: 38255240Snordmark icmp_bind_error(connp, mp); 38265240Snordmark return; 38275240Snordmark default: 38285240Snordmark break; 38295240Snordmark } 38305240Snordmark ASSERT(0); 38315240Snordmark freemsg(mp); 38325240Snordmark return; 38335240Snordmark 38345240Snordmark case T_BIND_ACK: 38355240Snordmark icmp_bind_ack(connp, mp); 38365240Snordmark return; 38375240Snordmark 38385240Snordmark default: 38395240Snordmark break; 38405240Snordmark } 38415240Snordmark freemsg(mp); 38425240Snordmark return; 38435240Snordmark default: 38445240Snordmark /* FIXME: other cases? */ 38455240Snordmark ASSERT(0); 38465240Snordmark freemsg(mp); 38475240Snordmark return; 38485240Snordmark } 38490Sstevel@tonic-gate } 38500Sstevel@tonic-gate 38510Sstevel@tonic-gate /* 38520Sstevel@tonic-gate * Process a T_BIND_ACK 38530Sstevel@tonic-gate */ 38540Sstevel@tonic-gate static void 38555240Snordmark icmp_bind_ack(conn_t *connp, mblk_t *mp) 38560Sstevel@tonic-gate { 38575240Snordmark icmp_t *icmp = connp->conn_icmp; 38580Sstevel@tonic-gate mblk_t *mp1; 38590Sstevel@tonic-gate ire_t *ire; 38600Sstevel@tonic-gate struct T_bind_ack *tba; 38610Sstevel@tonic-gate uchar_t *addrp; 38620Sstevel@tonic-gate ipa_conn_t *ac; 38630Sstevel@tonic-gate ipa6_conn_t *ac6; 38640Sstevel@tonic-gate 38655240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 38660Sstevel@tonic-gate /* 38670Sstevel@tonic-gate * We know if headers are included or not so we can 38680Sstevel@tonic-gate * safely do this. 38690Sstevel@tonic-gate */ 38700Sstevel@tonic-gate if (icmp->icmp_state == TS_UNBND) { 38710Sstevel@tonic-gate /* 38720Sstevel@tonic-gate * TPI has not yet bound - bind sent by 38730Sstevel@tonic-gate * icmp_bind_proto. 38740Sstevel@tonic-gate */ 38750Sstevel@tonic-gate freemsg(mp); 38765240Snordmark rw_exit(&icmp->icmp_rwlock); 38770Sstevel@tonic-gate return; 38780Sstevel@tonic-gate } 38795240Snordmark ASSERT(icmp->icmp_pending_op != -1); 38800Sstevel@tonic-gate 38810Sstevel@tonic-gate /* 38820Sstevel@tonic-gate * If a broadcast/multicast address was bound set 38830Sstevel@tonic-gate * the source address to 0. 38840Sstevel@tonic-gate * This ensures no datagrams with broadcast address 38850Sstevel@tonic-gate * as source address are emitted (which would violate 38860Sstevel@tonic-gate * RFC1122 - Hosts requirements) 38870Sstevel@tonic-gate * 38880Sstevel@tonic-gate * Note that when connecting the returned IRE is 38890Sstevel@tonic-gate * for the destination address and we only perform 38900Sstevel@tonic-gate * the broadcast check for the source address (it 38910Sstevel@tonic-gate * is OK to connect to a broadcast/multicast address.) 38920Sstevel@tonic-gate */ 38930Sstevel@tonic-gate mp1 = mp->b_cont; 38940Sstevel@tonic-gate if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) { 38950Sstevel@tonic-gate ire = (ire_t *)mp1->b_rptr; 38960Sstevel@tonic-gate 38970Sstevel@tonic-gate /* 38980Sstevel@tonic-gate * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast 38990Sstevel@tonic-gate * local address. 39000Sstevel@tonic-gate */ 39010Sstevel@tonic-gate if (ire->ire_type == IRE_BROADCAST && 39020Sstevel@tonic-gate icmp->icmp_state != TS_DATA_XFER) { 39035240Snordmark ASSERT(icmp->icmp_pending_op == T_BIND_REQ || 39045240Snordmark icmp->icmp_pending_op == O_T_BIND_REQ); 39050Sstevel@tonic-gate /* This was just a local bind to a MC/broadcast addr */ 39060Sstevel@tonic-gate V6_SET_ZERO(icmp->icmp_v6src); 39070Sstevel@tonic-gate if (icmp->icmp_family == AF_INET6) 39085240Snordmark (void) icmp_build_hdrs(icmp); 39090Sstevel@tonic-gate } else if (V6_OR_V4_INADDR_ANY(icmp->icmp_v6src)) { 39100Sstevel@tonic-gate /* 39110Sstevel@tonic-gate * Local address not yet set - pick it from the 39120Sstevel@tonic-gate * T_bind_ack 39130Sstevel@tonic-gate */ 39140Sstevel@tonic-gate tba = (struct T_bind_ack *)mp->b_rptr; 39150Sstevel@tonic-gate addrp = &mp->b_rptr[tba->ADDR_offset]; 39160Sstevel@tonic-gate switch (icmp->icmp_family) { 39170Sstevel@tonic-gate case AF_INET: 39180Sstevel@tonic-gate if (tba->ADDR_length == sizeof (ipa_conn_t)) { 39190Sstevel@tonic-gate ac = (ipa_conn_t *)addrp; 39200Sstevel@tonic-gate } else { 39210Sstevel@tonic-gate ASSERT(tba->ADDR_length == 39220Sstevel@tonic-gate sizeof (ipa_conn_x_t)); 39230Sstevel@tonic-gate ac = &((ipa_conn_x_t *)addrp)->acx_conn; 39240Sstevel@tonic-gate } 39250Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr, 39260Sstevel@tonic-gate &icmp->icmp_v6src); 39270Sstevel@tonic-gate break; 39280Sstevel@tonic-gate case AF_INET6: 39290Sstevel@tonic-gate if (tba->ADDR_length == sizeof (ipa6_conn_t)) { 39300Sstevel@tonic-gate ac6 = (ipa6_conn_t *)addrp; 39310Sstevel@tonic-gate } else { 39320Sstevel@tonic-gate ASSERT(tba->ADDR_length == 39330Sstevel@tonic-gate sizeof (ipa6_conn_x_t)); 39340Sstevel@tonic-gate ac6 = &((ipa6_conn_x_t *) 39350Sstevel@tonic-gate addrp)->ac6x_conn; 39360Sstevel@tonic-gate } 39370Sstevel@tonic-gate icmp->icmp_v6src = ac6->ac6_laddr; 39385240Snordmark (void) icmp_build_hdrs(icmp); 39390Sstevel@tonic-gate } 39400Sstevel@tonic-gate } 39410Sstevel@tonic-gate mp1 = mp1->b_cont; 39420Sstevel@tonic-gate } 39435240Snordmark icmp->icmp_pending_op = -1; 39445240Snordmark rw_exit(&icmp->icmp_rwlock); 39450Sstevel@tonic-gate /* 39460Sstevel@tonic-gate * Look for one or more appended ACK message added by 39470Sstevel@tonic-gate * icmp_connect or icmp_disconnect. 39480Sstevel@tonic-gate * If none found just send up the T_BIND_ACK. 39490Sstevel@tonic-gate * icmp_connect has appended a T_OK_ACK and a 39500Sstevel@tonic-gate * T_CONN_CON. 39510Sstevel@tonic-gate * icmp_disconnect has appended a T_OK_ACK. 39520Sstevel@tonic-gate */ 39530Sstevel@tonic-gate if (mp1 != NULL) { 39540Sstevel@tonic-gate if (mp->b_cont == mp1) 39550Sstevel@tonic-gate mp->b_cont = NULL; 39560Sstevel@tonic-gate else { 39570Sstevel@tonic-gate ASSERT(mp->b_cont->b_cont == mp1); 39580Sstevel@tonic-gate mp->b_cont->b_cont = NULL; 39590Sstevel@tonic-gate } 39600Sstevel@tonic-gate freemsg(mp); 39610Sstevel@tonic-gate mp = mp1; 39620Sstevel@tonic-gate while (mp != NULL) { 39630Sstevel@tonic-gate mp1 = mp->b_cont; 39640Sstevel@tonic-gate mp->b_cont = NULL; 39655240Snordmark putnext(connp->conn_rq, mp); 39660Sstevel@tonic-gate mp = mp1; 39670Sstevel@tonic-gate } 39680Sstevel@tonic-gate return; 39690Sstevel@tonic-gate } 39700Sstevel@tonic-gate freemsg(mp->b_cont); 39710Sstevel@tonic-gate mp->b_cont = NULL; 39725240Snordmark putnext(connp->conn_rq, mp); 39735240Snordmark } 39745240Snordmark 39755240Snordmark static void 39765240Snordmark icmp_bind_error(conn_t *connp, mblk_t *mp) 39775240Snordmark { 39785240Snordmark icmp_t *icmp = connp->conn_icmp; 39795240Snordmark struct T_error_ack *tea; 39805240Snordmark 39815240Snordmark tea = (struct T_error_ack *)mp->b_rptr; 39825240Snordmark /* 39835240Snordmark * If our O_T_BIND_REQ/T_BIND_REQ fails, 39845240Snordmark * clear out the source address before 39855240Snordmark * passing the message upstream. 39865240Snordmark * If this was caused by a T_CONN_REQ 39875240Snordmark * revert back to bound state. 39885240Snordmark */ 39895240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 39905240Snordmark if (icmp->icmp_state == TS_UNBND) { 39915240Snordmark /* 39925240Snordmark * TPI has not yet bound - bind sent by icmp_bind_proto. 39935240Snordmark */ 39945240Snordmark freemsg(mp); 39955240Snordmark rw_exit(&icmp->icmp_rwlock); 39965240Snordmark return; 39975240Snordmark } 39985240Snordmark ASSERT(icmp->icmp_pending_op != -1); 39995240Snordmark tea->ERROR_prim = icmp->icmp_pending_op; 40005240Snordmark icmp->icmp_pending_op = -1; 40015240Snordmark 40025240Snordmark switch (tea->ERROR_prim) { 40035240Snordmark case T_CONN_REQ: 40045240Snordmark ASSERT(icmp->icmp_state == TS_DATA_XFER); 40055240Snordmark /* Connect failed */ 40065240Snordmark /* Revert back to the bound source */ 40075240Snordmark icmp->icmp_v6src = icmp->icmp_bound_v6src; 40085240Snordmark icmp->icmp_state = TS_IDLE; 40095240Snordmark if (icmp->icmp_family == AF_INET6) 40105240Snordmark (void) icmp_build_hdrs(icmp); 40115240Snordmark break; 40125240Snordmark 40135240Snordmark case T_DISCON_REQ: 40145240Snordmark case T_BIND_REQ: 40155240Snordmark case O_T_BIND_REQ: 40165240Snordmark V6_SET_ZERO(icmp->icmp_v6src); 40175240Snordmark V6_SET_ZERO(icmp->icmp_bound_v6src); 40185240Snordmark icmp->icmp_state = TS_UNBND; 40195240Snordmark if (icmp->icmp_family == AF_INET6) 40205240Snordmark (void) icmp_build_hdrs(icmp); 40215240Snordmark break; 40225240Snordmark default: 40235240Snordmark break; 40245240Snordmark } 40255240Snordmark rw_exit(&icmp->icmp_rwlock); 40265240Snordmark putnext(connp->conn_rq, mp); 40270Sstevel@tonic-gate } 40280Sstevel@tonic-gate 40290Sstevel@tonic-gate /* 40300Sstevel@tonic-gate * return SNMP stuff in buffer in mpdata 40310Sstevel@tonic-gate */ 40325240Snordmark mblk_t * 40330Sstevel@tonic-gate icmp_snmp_get(queue_t *q, mblk_t *mpctl) 40340Sstevel@tonic-gate { 40350Sstevel@tonic-gate mblk_t *mpdata; 40360Sstevel@tonic-gate struct opthdr *optp; 40375240Snordmark conn_t *connp = Q_TO_CONN(q); 40385240Snordmark icmp_stack_t *is = connp->conn_netstack->netstack_icmp; 40395240Snordmark mblk_t *mp2ctl; 40405240Snordmark 40415240Snordmark /* 40425240Snordmark * make a copy of the original message 40435240Snordmark */ 40445240Snordmark mp2ctl = copymsg(mpctl); 40450Sstevel@tonic-gate 40460Sstevel@tonic-gate if (mpctl == NULL || 40470Sstevel@tonic-gate (mpdata = mpctl->b_cont) == NULL) { 40485240Snordmark freemsg(mpctl); 40495240Snordmark freemsg(mp2ctl); 40500Sstevel@tonic-gate return (0); 40510Sstevel@tonic-gate } 40520Sstevel@tonic-gate 40530Sstevel@tonic-gate /* fixed length structure for IPv4 and IPv6 counters */ 40540Sstevel@tonic-gate optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 40550Sstevel@tonic-gate optp->level = EXPER_RAWIP; 40560Sstevel@tonic-gate optp->name = 0; 40575240Snordmark (void) snmp_append_data(mpdata, (char *)&is->is_rawip_mib, 40585240Snordmark sizeof (is->is_rawip_mib)); 40590Sstevel@tonic-gate optp->len = msgdsize(mpdata); 40600Sstevel@tonic-gate qreply(q, mpctl); 40610Sstevel@tonic-gate 40625240Snordmark return (mp2ctl); 40630Sstevel@tonic-gate } 40640Sstevel@tonic-gate 40650Sstevel@tonic-gate /* 40660Sstevel@tonic-gate * Return 0 if invalid set request, 1 otherwise, including non-rawip requests. 40670Sstevel@tonic-gate * TODO: If this ever actually tries to set anything, it needs to be 40680Sstevel@tonic-gate * to do the appropriate locking. 40690Sstevel@tonic-gate */ 40700Sstevel@tonic-gate /* ARGSUSED */ 40715240Snordmark int 40720Sstevel@tonic-gate icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name, 40730Sstevel@tonic-gate uchar_t *ptr, int len) 40740Sstevel@tonic-gate { 40750Sstevel@tonic-gate switch (level) { 40760Sstevel@tonic-gate case EXPER_RAWIP: 40770Sstevel@tonic-gate return (0); 40780Sstevel@tonic-gate default: 40790Sstevel@tonic-gate return (1); 40800Sstevel@tonic-gate } 40810Sstevel@tonic-gate } 40820Sstevel@tonic-gate 40830Sstevel@tonic-gate /* Report for ndd "icmp_status" */ 40840Sstevel@tonic-gate /* ARGSUSED */ 40850Sstevel@tonic-gate static int 40860Sstevel@tonic-gate icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 40870Sstevel@tonic-gate { 40885240Snordmark conn_t *connp; 40895240Snordmark ip_stack_t *ipst; 40900Sstevel@tonic-gate char laddrbuf[INET6_ADDRSTRLEN]; 40910Sstevel@tonic-gate char faddrbuf[INET6_ADDRSTRLEN]; 40925240Snordmark int i; 40930Sstevel@tonic-gate 40940Sstevel@tonic-gate (void) mi_mpprintf(mp, 40950Sstevel@tonic-gate "RAWIP " MI_COL_HDRPAD_STR 40960Sstevel@tonic-gate /* 01234567[89ABCDEF] */ 40970Sstevel@tonic-gate " src addr dest addr state"); 40980Sstevel@tonic-gate /* xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx UNBOUND */ 40990Sstevel@tonic-gate 41005240Snordmark connp = Q_TO_CONN(q); 41015240Snordmark ipst = connp->conn_netstack->netstack_ip; 41025240Snordmark for (i = 0; i < CONN_G_HASH_SIZE; i++) { 41035240Snordmark connf_t *connfp; 41045240Snordmark char *state; 41055240Snordmark 41065240Snordmark connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 41075240Snordmark connp = NULL; 41085240Snordmark 41095240Snordmark while ((connp = ipcl_get_next_conn(connfp, connp, 41105240Snordmark IPCL_RAWIPCONN)) != NULL) { 41115240Snordmark icmp_t *icmp; 41125240Snordmark 41135240Snordmark mutex_enter(&(connp)->conn_lock); 41145240Snordmark icmp = connp->conn_icmp; 41155240Snordmark 41165240Snordmark if (icmp->icmp_state == TS_UNBND) 41175240Snordmark state = "UNBOUND"; 41185240Snordmark else if (icmp->icmp_state == TS_IDLE) 41195240Snordmark state = "IDLE"; 41205240Snordmark else if (icmp->icmp_state == TS_DATA_XFER) 41215240Snordmark state = "CONNECTED"; 41225240Snordmark else 41235240Snordmark state = "UnkState"; 41245240Snordmark 41255240Snordmark (void) mi_mpprintf(mp, MI_COL_PTRFMT_STR "%s %s %s", 41265240Snordmark (void *)icmp, 41275240Snordmark inet_ntop(AF_INET6, &icmp->icmp_v6dst, faddrbuf, 41285240Snordmark sizeof (faddrbuf)), 41295240Snordmark inet_ntop(AF_INET6, &icmp->icmp_v6src, laddrbuf, 41305240Snordmark sizeof (laddrbuf)), 41315240Snordmark state); 41325240Snordmark mutex_exit(&(connp)->conn_lock); 41335240Snordmark } 41340Sstevel@tonic-gate } 41350Sstevel@tonic-gate return (0); 41360Sstevel@tonic-gate } 41370Sstevel@tonic-gate 41380Sstevel@tonic-gate /* 41390Sstevel@tonic-gate * This routine creates a T_UDERROR_IND message and passes it upstream. 41400Sstevel@tonic-gate * The address and options are copied from the T_UNITDATA_REQ message 41410Sstevel@tonic-gate * passed in mp. This message is freed. 41420Sstevel@tonic-gate */ 41430Sstevel@tonic-gate static void 41440Sstevel@tonic-gate icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err) 41450Sstevel@tonic-gate { 41460Sstevel@tonic-gate mblk_t *mp1; 41470Sstevel@tonic-gate uchar_t *rptr = mp->b_rptr; 41480Sstevel@tonic-gate struct T_unitdata_req *tudr = (struct T_unitdata_req *)rptr; 41490Sstevel@tonic-gate 41500Sstevel@tonic-gate mp1 = mi_tpi_uderror_ind((char *)&rptr[tudr->DEST_offset], 41510Sstevel@tonic-gate tudr->DEST_length, (char *)&rptr[tudr->OPT_offset], 41520Sstevel@tonic-gate tudr->OPT_length, err); 41530Sstevel@tonic-gate if (mp1) 41540Sstevel@tonic-gate qreply(q, mp1); 41550Sstevel@tonic-gate freemsg(mp); 41560Sstevel@tonic-gate } 41570Sstevel@tonic-gate 41580Sstevel@tonic-gate /* 41590Sstevel@tonic-gate * This routine is called by icmp_wput to handle T_UNBIND_REQ messages. 41600Sstevel@tonic-gate * After some error checking, the message is passed downstream to ip. 41610Sstevel@tonic-gate */ 41620Sstevel@tonic-gate static void 41630Sstevel@tonic-gate icmp_unbind(queue_t *q, mblk_t *mp) 41640Sstevel@tonic-gate { 41655240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 41665240Snordmark 41675240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 41680Sstevel@tonic-gate /* If a bind has not been done, we can't unbind. */ 41695240Snordmark if (icmp->icmp_state == TS_UNBND || icmp->icmp_pending_op != -1) { 41705240Snordmark rw_exit(&icmp->icmp_rwlock); 41710Sstevel@tonic-gate icmp_err_ack(q, mp, TOUTSTATE, 0); 41720Sstevel@tonic-gate return; 41730Sstevel@tonic-gate } 41745240Snordmark icmp->icmp_pending_op = T_UNBIND_REQ; 41755240Snordmark rw_exit(&icmp->icmp_rwlock); 41765240Snordmark 41775240Snordmark /* 41785240Snordmark * Pass the unbind to IP; T_UNBIND_REQ is larger than T_OK_ACK 41795240Snordmark * and therefore ip_unbind must never return NULL. 41805240Snordmark */ 41815240Snordmark mp = ip_unbind(q, mp); 41825240Snordmark ASSERT(mp != NULL); 41835240Snordmark ASSERT(((struct T_ok_ack *)mp->b_rptr)->PRIM_type == T_OK_ACK); 41845240Snordmark 41855240Snordmark /* 41865240Snordmark * Once we're unbound from IP, the pending operation may be cleared 41875240Snordmark * here. 41885240Snordmark */ 41895240Snordmark rw_enter(&icmp->icmp_rwlock, RW_WRITER); 41900Sstevel@tonic-gate V6_SET_ZERO(icmp->icmp_v6src); 41910Sstevel@tonic-gate V6_SET_ZERO(icmp->icmp_bound_v6src); 41925240Snordmark icmp->icmp_pending_op = -1; 41930Sstevel@tonic-gate icmp->icmp_state = TS_UNBND; 41945240Snordmark if (icmp->icmp_family == AF_INET6) 41955240Snordmark (void) icmp_build_hdrs(icmp); 41965240Snordmark rw_exit(&icmp->icmp_rwlock); 41975240Snordmark 41985240Snordmark qreply(q, mp); 41990Sstevel@tonic-gate } 42000Sstevel@tonic-gate 42010Sstevel@tonic-gate /* 42020Sstevel@tonic-gate * Process IPv4 packets that already include an IP header. 42030Sstevel@tonic-gate * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and 42040Sstevel@tonic-gate * IPPROTO_IGMP). 42050Sstevel@tonic-gate */ 42060Sstevel@tonic-gate static void 42075240Snordmark icmp_wput_hdrincl(queue_t *q, mblk_t *mp, icmp_t *icmp, ip4_pkt_t *pktinfop) 42080Sstevel@tonic-gate { 42093448Sdh155122 icmp_stack_t *is = icmp->icmp_is; 42100Sstevel@tonic-gate ipha_t *ipha; 42110Sstevel@tonic-gate int ip_hdr_length; 42120Sstevel@tonic-gate int tp_hdr_len; 42130Sstevel@tonic-gate mblk_t *mp1; 42140Sstevel@tonic-gate uint_t pkt_len; 42153318Srshoaib ip_opt_info_t optinfo; 42165240Snordmark conn_t *connp = icmp->icmp_connp; 42173318Srshoaib 42183318Srshoaib optinfo.ip_opt_flags = 0; 42193318Srshoaib optinfo.ip_opt_ill_index = 0; 42200Sstevel@tonic-gate ipha = (ipha_t *)mp->b_rptr; 42210Sstevel@tonic-gate ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len; 42220Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) { 42230Sstevel@tonic-gate if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 42243448Sdh155122 ASSERT(icmp != NULL); 42255240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 42260Sstevel@tonic-gate freemsg(mp); 42270Sstevel@tonic-gate return; 42280Sstevel@tonic-gate } 42290Sstevel@tonic-gate ipha = (ipha_t *)mp->b_rptr; 42300Sstevel@tonic-gate } 42310Sstevel@tonic-gate ipha->ipha_version_and_hdr_length = 42320Sstevel@tonic-gate (IP_VERSION<<4) | (ip_hdr_length>>2); 42330Sstevel@tonic-gate 42340Sstevel@tonic-gate /* 42350Sstevel@tonic-gate * For the socket of SOCK_RAW type, the checksum is provided in the 42360Sstevel@tonic-gate * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to 42370Sstevel@tonic-gate * tell IP that the application has sent a complete IP header and not 42380Sstevel@tonic-gate * to compute the transport checksum nor change the DF flag. 42390Sstevel@tonic-gate */ 42400Sstevel@tonic-gate ipha->ipha_ident = IP_HDR_INCLUDED; 42410Sstevel@tonic-gate ipha->ipha_hdr_checksum = 0; 42420Sstevel@tonic-gate ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF); 42430Sstevel@tonic-gate /* Insert options if any */ 42440Sstevel@tonic-gate if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) { 42450Sstevel@tonic-gate /* 42460Sstevel@tonic-gate * Put the IP header plus any transport header that is 42470Sstevel@tonic-gate * checksumed by ip_wput into the first mblk. (ip_wput assumes 42480Sstevel@tonic-gate * that at least the checksum field is in the first mblk.) 42490Sstevel@tonic-gate */ 42500Sstevel@tonic-gate switch (ipha->ipha_protocol) { 42510Sstevel@tonic-gate case IPPROTO_UDP: 42520Sstevel@tonic-gate tp_hdr_len = 8; 42530Sstevel@tonic-gate break; 42540Sstevel@tonic-gate case IPPROTO_TCP: 42550Sstevel@tonic-gate tp_hdr_len = 20; 42560Sstevel@tonic-gate break; 42570Sstevel@tonic-gate default: 42580Sstevel@tonic-gate tp_hdr_len = 0; 42590Sstevel@tonic-gate break; 42600Sstevel@tonic-gate } 42610Sstevel@tonic-gate /* 42620Sstevel@tonic-gate * The code below assumes that IP_SIMPLE_HDR_LENGTH plus 42630Sstevel@tonic-gate * tp_hdr_len bytes will be in a single mblk. 42640Sstevel@tonic-gate */ 42650Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < (IP_SIMPLE_HDR_LENGTH + 42660Sstevel@tonic-gate tp_hdr_len)) { 42670Sstevel@tonic-gate if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH + 42680Sstevel@tonic-gate tp_hdr_len)) { 42695240Snordmark BUMP_MIB(&is->is_rawip_mib, 42703448Sdh155122 rawipOutErrors); 42710Sstevel@tonic-gate freemsg(mp); 42720Sstevel@tonic-gate return; 42730Sstevel@tonic-gate } 42740Sstevel@tonic-gate ipha = (ipha_t *)mp->b_rptr; 42750Sstevel@tonic-gate } 42760Sstevel@tonic-gate 42770Sstevel@tonic-gate /* 42780Sstevel@tonic-gate * if the length is larger then the max allowed IP packet, 42790Sstevel@tonic-gate * then send an error and abort the processing. 42800Sstevel@tonic-gate */ 42810Sstevel@tonic-gate pkt_len = ntohs(ipha->ipha_length) 42820Sstevel@tonic-gate + icmp->icmp_ip_snd_options_len; 42830Sstevel@tonic-gate if (pkt_len > IP_MAXPACKET) { 42840Sstevel@tonic-gate icmp_ud_err(q, mp, EMSGSIZE); 42850Sstevel@tonic-gate return; 42860Sstevel@tonic-gate } 42873448Sdh155122 if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra + 42880Sstevel@tonic-gate tp_hdr_len, BPRI_LO))) { 42890Sstevel@tonic-gate icmp_ud_err(q, mp, ENOMEM); 42900Sstevel@tonic-gate return; 42910Sstevel@tonic-gate } 42923448Sdh155122 mp1->b_rptr += is->is_wroff_extra; 42930Sstevel@tonic-gate mp1->b_wptr = mp1->b_rptr + ip_hdr_length; 42940Sstevel@tonic-gate 42950Sstevel@tonic-gate ipha->ipha_length = htons((uint16_t)pkt_len); 42960Sstevel@tonic-gate bcopy(ipha, mp1->b_rptr, IP_SIMPLE_HDR_LENGTH); 42970Sstevel@tonic-gate 42980Sstevel@tonic-gate /* Copy transport header if any */ 42990Sstevel@tonic-gate bcopy(&ipha[1], mp1->b_wptr, tp_hdr_len); 43000Sstevel@tonic-gate mp1->b_wptr += tp_hdr_len; 43010Sstevel@tonic-gate 43020Sstevel@tonic-gate /* Add options */ 43030Sstevel@tonic-gate ipha = (ipha_t *)mp1->b_rptr; 43040Sstevel@tonic-gate bcopy(icmp->icmp_ip_snd_options, &ipha[1], 43050Sstevel@tonic-gate icmp->icmp_ip_snd_options_len); 43060Sstevel@tonic-gate 43070Sstevel@tonic-gate /* Drop IP header and transport header from original */ 43080Sstevel@tonic-gate (void) adjmsg(mp, IP_SIMPLE_HDR_LENGTH + tp_hdr_len); 43090Sstevel@tonic-gate 43100Sstevel@tonic-gate mp1->b_cont = mp; 43110Sstevel@tonic-gate mp = mp1; 43120Sstevel@tonic-gate /* 43130Sstevel@tonic-gate * Massage source route putting first source 43140Sstevel@tonic-gate * route in ipha_dst. 43150Sstevel@tonic-gate */ 43165240Snordmark (void) ip_massage_options(ipha, is->is_netstack); 43170Sstevel@tonic-gate } 43183318Srshoaib 43193318Srshoaib if (pktinfop != NULL) { 43203318Srshoaib /* 43213318Srshoaib * Over write the source address provided in the header 43223318Srshoaib */ 43233318Srshoaib if (pktinfop->ip4_addr != INADDR_ANY) { 43243318Srshoaib ipha->ipha_src = pktinfop->ip4_addr; 43253318Srshoaib optinfo.ip_opt_flags = IP_VERIFY_SRC; 43263318Srshoaib } 43273318Srshoaib 43283318Srshoaib if (pktinfop->ip4_ill_index != 0) { 43293318Srshoaib optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index; 43303318Srshoaib } 43313318Srshoaib } 43323318Srshoaib 43335240Snordmark mblk_setcred(mp, connp->conn_cred); 43345240Snordmark ip_output_options(connp, mp, q, IP_WPUT, 43355240Snordmark &optinfo); 43360Sstevel@tonic-gate } 43370Sstevel@tonic-gate 43381676Sjpk static boolean_t 43391676Sjpk icmp_update_label(queue_t *q, icmp_t *icmp, mblk_t *mp, ipaddr_t dst) 43401676Sjpk { 43411676Sjpk int err; 43421676Sjpk uchar_t opt_storage[IP_MAX_OPT_LENGTH]; 43435240Snordmark icmp_stack_t *is = icmp->icmp_is; 43445240Snordmark conn_t *connp = icmp->icmp_connp; 43455240Snordmark 43465240Snordmark err = tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst, 4347*6596Skp158701 opt_storage, connp->conn_mac_exempt, 43485240Snordmark is->is_netstack->netstack_ip); 43491676Sjpk if (err == 0) { 43501676Sjpk err = tsol_update_options(&icmp->icmp_ip_snd_options, 43511676Sjpk &icmp->icmp_ip_snd_options_len, &icmp->icmp_label_len, 43521676Sjpk opt_storage); 43531676Sjpk } 43541676Sjpk if (err != 0) { 43555240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 43561676Sjpk DTRACE_PROBE4( 43571676Sjpk tx__ip__log__drop__updatelabel__icmp, 43581676Sjpk char *, "queue(1) failed to update options(2) on mp(3)", 43591676Sjpk queue_t *, q, char *, opt_storage, mblk_t *, mp); 43601676Sjpk icmp_ud_err(q, mp, err); 43611676Sjpk return (B_FALSE); 43621676Sjpk } 43631676Sjpk IN6_IPADDR_TO_V4MAPPED(dst, &icmp->icmp_v6lastdst); 43641676Sjpk return (B_TRUE); 43651676Sjpk } 43661676Sjpk 43670Sstevel@tonic-gate /* 43680Sstevel@tonic-gate * This routine handles all messages passed downstream. It either 43690Sstevel@tonic-gate * consumes the message or passes it downstream; it never queues a 43700Sstevel@tonic-gate * a message. 43710Sstevel@tonic-gate */ 43720Sstevel@tonic-gate static void 43730Sstevel@tonic-gate icmp_wput(queue_t *q, mblk_t *mp) 43740Sstevel@tonic-gate { 43750Sstevel@tonic-gate uchar_t *rptr = mp->b_rptr; 43760Sstevel@tonic-gate ipha_t *ipha; 43770Sstevel@tonic-gate mblk_t *mp1; 43780Sstevel@tonic-gate int ip_hdr_length; 43790Sstevel@tonic-gate #define tudr ((struct T_unitdata_req *)rptr) 43800Sstevel@tonic-gate size_t ip_len; 43815240Snordmark conn_t *connp = Q_TO_CONN(q); 43825240Snordmark icmp_t *icmp = connp->conn_icmp; 43833448Sdh155122 icmp_stack_t *is = icmp->icmp_is; 43840Sstevel@tonic-gate sin6_t *sin6; 43850Sstevel@tonic-gate sin_t *sin; 43860Sstevel@tonic-gate ipaddr_t v4dst; 43873318Srshoaib ip4_pkt_t pktinfo; 43883318Srshoaib ip4_pkt_t *pktinfop = &pktinfo; 43893318Srshoaib ip_opt_info_t optinfo; 43900Sstevel@tonic-gate 43910Sstevel@tonic-gate switch (mp->b_datap->db_type) { 43920Sstevel@tonic-gate case M_DATA: 43930Sstevel@tonic-gate if (icmp->icmp_hdrincl) { 43940Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV4_VERSION); 43951676Sjpk ipha = (ipha_t *)mp->b_rptr; 43961676Sjpk if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) { 43971676Sjpk if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 43985240Snordmark BUMP_MIB(&is->is_rawip_mib, 43993448Sdh155122 rawipOutErrors); 44001676Sjpk freemsg(mp); 44011676Sjpk return; 44021676Sjpk } 44031676Sjpk ipha = (ipha_t *)mp->b_rptr; 44041676Sjpk } 44051676Sjpk /* 44061676Sjpk * If this connection was used for v6 (inconceivable!) 44071676Sjpk * or if we have a new destination, then it's time to 44081676Sjpk * figure a new label. 44091676Sjpk */ 44101676Sjpk if (is_system_labeled() && 44111676Sjpk (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) || 44121676Sjpk V4_PART_OF_V6(icmp->icmp_v6lastdst) != 44131676Sjpk ipha->ipha_dst) && 44141676Sjpk !icmp_update_label(q, icmp, mp, ipha->ipha_dst)) { 44151676Sjpk return; 44161676Sjpk } 44175240Snordmark icmp_wput_hdrincl(q, mp, icmp, NULL); 44180Sstevel@tonic-gate return; 44190Sstevel@tonic-gate } 44200Sstevel@tonic-gate freemsg(mp); 44210Sstevel@tonic-gate return; 44220Sstevel@tonic-gate case M_PROTO: 44230Sstevel@tonic-gate case M_PCPROTO: 44240Sstevel@tonic-gate ip_len = mp->b_wptr - rptr; 44250Sstevel@tonic-gate if (ip_len >= sizeof (struct T_unitdata_req)) { 44260Sstevel@tonic-gate /* Expedite valid T_UNITDATA_REQ to below the switch */ 44270Sstevel@tonic-gate if (((union T_primitives *)rptr)->type 44280Sstevel@tonic-gate == T_UNITDATA_REQ) 44290Sstevel@tonic-gate break; 44300Sstevel@tonic-gate } 44310Sstevel@tonic-gate /* FALLTHRU */ 44320Sstevel@tonic-gate default: 44330Sstevel@tonic-gate icmp_wput_other(q, mp); 44340Sstevel@tonic-gate return; 44350Sstevel@tonic-gate } 44360Sstevel@tonic-gate 44370Sstevel@tonic-gate /* Handle T_UNITDATA_REQ messages here. */ 44380Sstevel@tonic-gate 44393318Srshoaib 44403318Srshoaib 44410Sstevel@tonic-gate if (icmp->icmp_state == TS_UNBND) { 44420Sstevel@tonic-gate /* If a port has not been bound to the stream, fail. */ 44435240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 44440Sstevel@tonic-gate icmp_ud_err(q, mp, EPROTO); 44450Sstevel@tonic-gate return; 44460Sstevel@tonic-gate } 44470Sstevel@tonic-gate mp1 = mp->b_cont; 44480Sstevel@tonic-gate if (mp1 == NULL) { 44495240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 44500Sstevel@tonic-gate icmp_ud_err(q, mp, EPROTO); 44510Sstevel@tonic-gate return; 44520Sstevel@tonic-gate } 44530Sstevel@tonic-gate 44540Sstevel@tonic-gate if ((rptr + tudr->DEST_offset + tudr->DEST_length) > mp->b_wptr) { 44555240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 44560Sstevel@tonic-gate icmp_ud_err(q, mp, EADDRNOTAVAIL); 44570Sstevel@tonic-gate return; 44580Sstevel@tonic-gate } 44590Sstevel@tonic-gate 44600Sstevel@tonic-gate switch (icmp->icmp_family) { 44610Sstevel@tonic-gate case AF_INET6: 44620Sstevel@tonic-gate sin6 = (sin6_t *)&rptr[tudr->DEST_offset]; 44630Sstevel@tonic-gate if (!OK_32PTR((char *)sin6) || 44640Sstevel@tonic-gate tudr->DEST_length != sizeof (sin6_t) || 44650Sstevel@tonic-gate sin6->sin6_family != AF_INET6) { 44665240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 44670Sstevel@tonic-gate icmp_ud_err(q, mp, EADDRNOTAVAIL); 44680Sstevel@tonic-gate return; 44690Sstevel@tonic-gate } 44700Sstevel@tonic-gate 44710Sstevel@tonic-gate /* No support for mapped addresses on raw sockets */ 44720Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 44735240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 44740Sstevel@tonic-gate icmp_ud_err(q, mp, EADDRNOTAVAIL); 44750Sstevel@tonic-gate return; 44760Sstevel@tonic-gate } 44770Sstevel@tonic-gate 44780Sstevel@tonic-gate /* 44790Sstevel@tonic-gate * Destination is a native IPv6 address. 44800Sstevel@tonic-gate * Send out an IPv6 format packet. 44810Sstevel@tonic-gate */ 44820Sstevel@tonic-gate icmp_wput_ipv6(q, mp, sin6, tudr->OPT_length); 44830Sstevel@tonic-gate return; 44840Sstevel@tonic-gate 44850Sstevel@tonic-gate case AF_INET: 44860Sstevel@tonic-gate sin = (sin_t *)&rptr[tudr->DEST_offset]; 44870Sstevel@tonic-gate if (!OK_32PTR((char *)sin) || 44880Sstevel@tonic-gate tudr->DEST_length != sizeof (sin_t) || 44890Sstevel@tonic-gate sin->sin_family != AF_INET) { 44905240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 44910Sstevel@tonic-gate icmp_ud_err(q, mp, EADDRNOTAVAIL); 44920Sstevel@tonic-gate return; 44930Sstevel@tonic-gate } 44940Sstevel@tonic-gate /* Extract and ipaddr */ 44950Sstevel@tonic-gate v4dst = sin->sin_addr.s_addr; 44960Sstevel@tonic-gate break; 44971676Sjpk 44981676Sjpk default: 44991676Sjpk ASSERT(0); 45000Sstevel@tonic-gate } 45010Sstevel@tonic-gate 45023318Srshoaib pktinfop->ip4_ill_index = 0; 45033318Srshoaib pktinfop->ip4_addr = INADDR_ANY; 45043318Srshoaib optinfo.ip_opt_flags = 0; 45053318Srshoaib optinfo.ip_opt_ill_index = 0; 45063318Srshoaib 45073318Srshoaib 45080Sstevel@tonic-gate /* 45090Sstevel@tonic-gate * If options passed in, feed it for verification and handling 45100Sstevel@tonic-gate */ 45110Sstevel@tonic-gate if (tudr->OPT_length != 0) { 45120Sstevel@tonic-gate int error; 45130Sstevel@tonic-gate 45143318Srshoaib error = 0; 45150Sstevel@tonic-gate if (icmp_unitdata_opt_process(q, mp, &error, 45163318Srshoaib (void *)pktinfop) < 0) { 45170Sstevel@tonic-gate /* failure */ 45185240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 45190Sstevel@tonic-gate icmp_ud_err(q, mp, error); 45200Sstevel@tonic-gate return; 45210Sstevel@tonic-gate } 45223318Srshoaib ASSERT(error == 0); 45230Sstevel@tonic-gate /* 45240Sstevel@tonic-gate * Note: Success in processing options. 45250Sstevel@tonic-gate * mp option buffer represented by 45260Sstevel@tonic-gate * OPT_length/offset now potentially modified 45270Sstevel@tonic-gate * and contain option setting results 45280Sstevel@tonic-gate */ 45293318Srshoaib 45300Sstevel@tonic-gate } 45310Sstevel@tonic-gate 45321676Sjpk if (v4dst == INADDR_ANY) 45331676Sjpk v4dst = htonl(INADDR_LOOPBACK); 45341676Sjpk 45351676Sjpk /* Check if our saved options are valid; update if not */ 45361676Sjpk if (is_system_labeled() && 45371676Sjpk (!IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6lastdst) || 45381676Sjpk V4_PART_OF_V6(icmp->icmp_v6lastdst) != v4dst) && 45391676Sjpk !icmp_update_label(q, icmp, mp, v4dst)) { 45401676Sjpk return; 45411676Sjpk } 45421676Sjpk 45430Sstevel@tonic-gate /* Protocol 255 contains full IP headers */ 45440Sstevel@tonic-gate if (icmp->icmp_hdrincl) { 45450Sstevel@tonic-gate freeb(mp); 45465240Snordmark icmp_wput_hdrincl(q, mp1, icmp, pktinfop); 45470Sstevel@tonic-gate return; 45480Sstevel@tonic-gate } 45491676Sjpk 45503318Srshoaib 45510Sstevel@tonic-gate /* Add an IP header */ 45520Sstevel@tonic-gate ip_hdr_length = IP_SIMPLE_HDR_LENGTH + icmp->icmp_ip_snd_options_len; 45530Sstevel@tonic-gate ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length]; 45540Sstevel@tonic-gate if ((uchar_t *)ipha < mp1->b_datap->db_base || 45550Sstevel@tonic-gate mp1->b_datap->db_ref != 1 || 45560Sstevel@tonic-gate !OK_32PTR(ipha)) { 45573448Sdh155122 if (!(mp1 = allocb(ip_hdr_length + is->is_wroff_extra, 45580Sstevel@tonic-gate BPRI_LO))) { 45595240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 45601289Sja97890 icmp_ud_err(q, mp, ENOMEM); 45610Sstevel@tonic-gate return; 45620Sstevel@tonic-gate } 45630Sstevel@tonic-gate mp1->b_cont = mp->b_cont; 45640Sstevel@tonic-gate ipha = (ipha_t *)mp1->b_datap->db_lim; 45650Sstevel@tonic-gate mp1->b_wptr = (uchar_t *)ipha; 45660Sstevel@tonic-gate ipha = (ipha_t *)((uchar_t *)ipha - ip_hdr_length); 45670Sstevel@tonic-gate } 45680Sstevel@tonic-gate #ifdef _BIG_ENDIAN 45690Sstevel@tonic-gate /* Set version, header length, and tos */ 45700Sstevel@tonic-gate *(uint16_t *)&ipha->ipha_version_and_hdr_length = 45710Sstevel@tonic-gate ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) | 45725240Snordmark icmp->icmp_type_of_service); 45730Sstevel@tonic-gate /* Set ttl and protocol */ 45740Sstevel@tonic-gate *(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_ttl << 8) | icmp->icmp_proto; 45750Sstevel@tonic-gate #else 45760Sstevel@tonic-gate /* Set version, header length, and tos */ 45770Sstevel@tonic-gate *(uint16_t *)&ipha->ipha_version_and_hdr_length = 45780Sstevel@tonic-gate ((icmp->icmp_type_of_service << 8) | 45795240Snordmark ((IP_VERSION << 4) | (ip_hdr_length>>2))); 45800Sstevel@tonic-gate /* Set ttl and protocol */ 45810Sstevel@tonic-gate *(uint16_t *)&ipha->ipha_ttl = (icmp->icmp_proto << 8) | icmp->icmp_ttl; 45820Sstevel@tonic-gate #endif 45833318Srshoaib if (pktinfop->ip4_addr != INADDR_ANY) { 45843318Srshoaib ipha->ipha_src = pktinfop->ip4_addr; 45853318Srshoaib optinfo.ip_opt_flags = IP_VERIFY_SRC; 45863318Srshoaib } else { 45873318Srshoaib 45883318Srshoaib /* 45893318Srshoaib * Copy our address into the packet. If this is zero, 45903318Srshoaib * ip will fill in the real source address. 45913318Srshoaib */ 45923318Srshoaib IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, ipha->ipha_src); 45933318Srshoaib } 45943318Srshoaib 45950Sstevel@tonic-gate ipha->ipha_fragment_offset_and_flags = 0; 45960Sstevel@tonic-gate 45973318Srshoaib if (pktinfop->ip4_ill_index != 0) { 45983318Srshoaib optinfo.ip_opt_ill_index = pktinfop->ip4_ill_index; 45993318Srshoaib } 46003318Srshoaib 46013318Srshoaib 46020Sstevel@tonic-gate /* 46030Sstevel@tonic-gate * For the socket of SOCK_RAW type, the checksum is provided in the 46040Sstevel@tonic-gate * pre-built packet. We set the ipha_ident field to IP_HDR_INCLUDED to 46050Sstevel@tonic-gate * tell IP that the application has sent a complete IP header and not 46060Sstevel@tonic-gate * to compute the transport checksum nor change the DF flag. 46070Sstevel@tonic-gate */ 46080Sstevel@tonic-gate ipha->ipha_ident = IP_HDR_INCLUDED; 46090Sstevel@tonic-gate 46100Sstevel@tonic-gate /* Finish common formatting of the packet. */ 46110Sstevel@tonic-gate mp1->b_rptr = (uchar_t *)ipha; 46120Sstevel@tonic-gate 46130Sstevel@tonic-gate ip_len = mp1->b_wptr - (uchar_t *)ipha; 46140Sstevel@tonic-gate if (mp1->b_cont != NULL) 46150Sstevel@tonic-gate ip_len += msgdsize(mp1->b_cont); 46160Sstevel@tonic-gate 46170Sstevel@tonic-gate /* 46180Sstevel@tonic-gate * Set the length into the IP header. 46190Sstevel@tonic-gate * If the length is greater than the maximum allowed by IP, 46200Sstevel@tonic-gate * then free the message and return. Do not try and send it 46210Sstevel@tonic-gate * as this can cause problems in layers below. 46220Sstevel@tonic-gate */ 46230Sstevel@tonic-gate if (ip_len > IP_MAXPACKET) { 46245240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 46250Sstevel@tonic-gate icmp_ud_err(q, mp, EMSGSIZE); 46260Sstevel@tonic-gate return; 46270Sstevel@tonic-gate } 46280Sstevel@tonic-gate ipha->ipha_length = htons((uint16_t)ip_len); 46290Sstevel@tonic-gate /* 46300Sstevel@tonic-gate * Copy in the destination address from the T_UNITDATA 46310Sstevel@tonic-gate * request 46320Sstevel@tonic-gate */ 46331676Sjpk ipha->ipha_dst = v4dst; 46340Sstevel@tonic-gate 46350Sstevel@tonic-gate /* 46360Sstevel@tonic-gate * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic. 46370Sstevel@tonic-gate */ 46380Sstevel@tonic-gate if (CLASSD(v4dst)) 46390Sstevel@tonic-gate ipha->ipha_ttl = icmp->icmp_multicast_ttl; 46400Sstevel@tonic-gate 46410Sstevel@tonic-gate /* Copy in options if any */ 46420Sstevel@tonic-gate if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) { 46430Sstevel@tonic-gate bcopy(icmp->icmp_ip_snd_options, 46440Sstevel@tonic-gate &ipha[1], icmp->icmp_ip_snd_options_len); 46450Sstevel@tonic-gate /* 46460Sstevel@tonic-gate * Massage source route putting first source route in ipha_dst. 46470Sstevel@tonic-gate * Ignore the destination in the T_unitdata_req. 46480Sstevel@tonic-gate */ 46495240Snordmark (void) ip_massage_options(ipha, is->is_netstack); 46500Sstevel@tonic-gate } 46513318Srshoaib 46520Sstevel@tonic-gate freeb(mp); 46535240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams); 46545240Snordmark mblk_setcred(mp1, connp->conn_cred); 46555240Snordmark ip_output_options(Q_TO_CONN(q), mp1, q, IP_WPUT, &optinfo); 46560Sstevel@tonic-gate #undef ipha 46570Sstevel@tonic-gate #undef tudr 46580Sstevel@tonic-gate } 46590Sstevel@tonic-gate 46601676Sjpk static boolean_t 46611676Sjpk icmp_update_label_v6(queue_t *wq, icmp_t *icmp, mblk_t *mp, in6_addr_t *dst) 46621676Sjpk { 46631676Sjpk int err; 46641676Sjpk uchar_t opt_storage[TSOL_MAX_IPV6_OPTION]; 46655240Snordmark icmp_stack_t *is = icmp->icmp_is; 46665240Snordmark conn_t *connp = icmp->icmp_connp; 46675240Snordmark 46685240Snordmark err = tsol_compute_label_v6(DB_CREDDEF(mp, connp->conn_cred), dst, 4669*6596Skp158701 opt_storage, connp->conn_mac_exempt, 46705240Snordmark is->is_netstack->netstack_ip); 46711676Sjpk if (err == 0) { 46721676Sjpk err = tsol_update_sticky(&icmp->icmp_sticky_ipp, 46731676Sjpk &icmp->icmp_label_len_v6, opt_storage); 46741676Sjpk } 46751676Sjpk if (err != 0) { 46765240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 46771676Sjpk DTRACE_PROBE4( 46781676Sjpk tx__ip__log__drop__updatelabel__icmp6, 46791676Sjpk char *, "queue(1) failed to update options(2) on mp(3)", 46801676Sjpk queue_t *, wq, char *, opt_storage, mblk_t *, mp); 46811676Sjpk icmp_ud_err(wq, mp, err); 46821676Sjpk return (B_FALSE); 46831676Sjpk } 46841676Sjpk 46851676Sjpk icmp->icmp_v6lastdst = *dst; 46861676Sjpk return (B_TRUE); 46871676Sjpk } 46881676Sjpk 46890Sstevel@tonic-gate /* 46900Sstevel@tonic-gate * icmp_wput_ipv6(): 46910Sstevel@tonic-gate * Assumes that icmp_wput did some sanity checking on the destination 46921676Sjpk * address, but that the label may not yet be correct. 46930Sstevel@tonic-gate */ 46940Sstevel@tonic-gate void 46950Sstevel@tonic-gate icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, t_scalar_t tudr_optlen) 46960Sstevel@tonic-gate { 46970Sstevel@tonic-gate ip6_t *ip6h; 46980Sstevel@tonic-gate ip6i_t *ip6i; /* mp1->b_rptr even if no ip6i_t */ 46990Sstevel@tonic-gate mblk_t *mp1; 47000Sstevel@tonic-gate int ip_hdr_len = IPV6_HDR_LEN; 47010Sstevel@tonic-gate size_t ip_len; 47025240Snordmark icmp_t *icmp = Q_TO_ICMP(q); 47033448Sdh155122 icmp_stack_t *is = icmp->icmp_is; 47040Sstevel@tonic-gate ip6_pkt_t ipp_s; /* For ancillary data options */ 47050Sstevel@tonic-gate ip6_pkt_t *ipp = &ipp_s; 47060Sstevel@tonic-gate ip6_pkt_t *tipp; 47070Sstevel@tonic-gate uint32_t csum = 0; 47080Sstevel@tonic-gate uint_t ignore = 0; 47090Sstevel@tonic-gate uint_t option_exists = 0, is_sticky = 0; 47100Sstevel@tonic-gate uint8_t *cp; 47110Sstevel@tonic-gate uint8_t *nxthdr_ptr; 47121676Sjpk in6_addr_t ip6_dst; 47130Sstevel@tonic-gate 47140Sstevel@tonic-gate /* 47150Sstevel@tonic-gate * If the local address is a mapped address return 47160Sstevel@tonic-gate * an error. 47170Sstevel@tonic-gate * It would be possible to send an IPv6 packet but the 47180Sstevel@tonic-gate * response would never make it back to the application 47190Sstevel@tonic-gate * since it is bound to a mapped address. 47200Sstevel@tonic-gate */ 47210Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&icmp->icmp_v6src)) { 47225240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 47230Sstevel@tonic-gate icmp_ud_err(q, mp, EADDRNOTAVAIL); 47240Sstevel@tonic-gate return; 47250Sstevel@tonic-gate } 47260Sstevel@tonic-gate 47270Sstevel@tonic-gate ipp->ipp_fields = 0; 47280Sstevel@tonic-gate ipp->ipp_sticky_ignored = 0; 47290Sstevel@tonic-gate 47300Sstevel@tonic-gate /* 47310Sstevel@tonic-gate * If TPI options passed in, feed it for verification and handling 47320Sstevel@tonic-gate */ 47330Sstevel@tonic-gate if (tudr_optlen != 0) { 47340Sstevel@tonic-gate int error; 47350Sstevel@tonic-gate 47360Sstevel@tonic-gate if (icmp_unitdata_opt_process(q, mp, &error, 47370Sstevel@tonic-gate (void *)ipp) < 0) { 47380Sstevel@tonic-gate /* failure */ 47395240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 47400Sstevel@tonic-gate icmp_ud_err(q, mp, error); 47410Sstevel@tonic-gate return; 47420Sstevel@tonic-gate } 47430Sstevel@tonic-gate ignore = ipp->ipp_sticky_ignored; 47440Sstevel@tonic-gate ASSERT(error == 0); 47450Sstevel@tonic-gate } 47460Sstevel@tonic-gate 47470Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 && 47480Sstevel@tonic-gate IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 47490Sstevel@tonic-gate /* 47500Sstevel@tonic-gate * IPPF_SCOPE_ID is special. It's neither a sticky 47510Sstevel@tonic-gate * option nor ancillary data. It needs to be 47520Sstevel@tonic-gate * explicitly set in options_exists. 47530Sstevel@tonic-gate */ 47540Sstevel@tonic-gate option_exists |= IPPF_SCOPE_ID; 47550Sstevel@tonic-gate } 47560Sstevel@tonic-gate 47571676Sjpk /* 47581676Sjpk * Compute the destination address 47591676Sjpk */ 47601676Sjpk ip6_dst = sin6->sin6_addr; 47611676Sjpk if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 47621676Sjpk ip6_dst = ipv6_loopback; 47631676Sjpk 47641676Sjpk /* 47651676Sjpk * If we're not going to the same destination as last time, then 47661676Sjpk * recompute the label required. This is done in a separate routine to 47671676Sjpk * avoid blowing up our stack here. 47681676Sjpk */ 47691676Sjpk if (is_system_labeled() && 47701676Sjpk !IN6_ARE_ADDR_EQUAL(&icmp->icmp_v6lastdst, &ip6_dst) && 47711676Sjpk !icmp_update_label_v6(q, icmp, mp, &ip6_dst)) { 47721676Sjpk return; 47731676Sjpk } 47741676Sjpk 47751676Sjpk /* 47761676Sjpk * If there's a security label here, then we ignore any options the 47771676Sjpk * user may try to set. We keep the peer's label as a hidden sticky 47781676Sjpk * option. 47791676Sjpk */ 47801676Sjpk if (icmp->icmp_label_len_v6 > 0) { 47811676Sjpk ignore &= ~IPPF_HOPOPTS; 47821676Sjpk ipp->ipp_fields &= ~IPPF_HOPOPTS; 47831676Sjpk } 47841676Sjpk 47850Sstevel@tonic-gate if ((icmp->icmp_sticky_ipp.ipp_fields == 0) && 47860Sstevel@tonic-gate (ipp->ipp_fields == 0)) { 47870Sstevel@tonic-gate /* No sticky options nor ancillary data. */ 47880Sstevel@tonic-gate goto no_options; 47890Sstevel@tonic-gate } 47900Sstevel@tonic-gate 47910Sstevel@tonic-gate /* 47920Sstevel@tonic-gate * Go through the options figuring out where each is going to 47930Sstevel@tonic-gate * come from and build two masks. The first mask indicates if 47940Sstevel@tonic-gate * the option exists at all. The second mask indicates if the 47950Sstevel@tonic-gate * option is sticky or ancillary. 47960Sstevel@tonic-gate */ 47970Sstevel@tonic-gate if (!(ignore & IPPF_HOPOPTS)) { 47980Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_HOPOPTS) { 47990Sstevel@tonic-gate option_exists |= IPPF_HOPOPTS; 48000Sstevel@tonic-gate ip_hdr_len += ipp->ipp_hopoptslen; 48010Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) { 48020Sstevel@tonic-gate option_exists |= IPPF_HOPOPTS; 48030Sstevel@tonic-gate is_sticky |= IPPF_HOPOPTS; 48040Sstevel@tonic-gate ip_hdr_len += icmp->icmp_sticky_ipp.ipp_hopoptslen; 48050Sstevel@tonic-gate } 48060Sstevel@tonic-gate } 48070Sstevel@tonic-gate 48080Sstevel@tonic-gate if (!(ignore & IPPF_RTHDR)) { 48090Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_RTHDR) { 48100Sstevel@tonic-gate option_exists |= IPPF_RTHDR; 48110Sstevel@tonic-gate ip_hdr_len += ipp->ipp_rthdrlen; 48120Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) { 48130Sstevel@tonic-gate option_exists |= IPPF_RTHDR; 48140Sstevel@tonic-gate is_sticky |= IPPF_RTHDR; 48150Sstevel@tonic-gate ip_hdr_len += icmp->icmp_sticky_ipp.ipp_rthdrlen; 48160Sstevel@tonic-gate } 48170Sstevel@tonic-gate } 48180Sstevel@tonic-gate 48190Sstevel@tonic-gate if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) { 48200Sstevel@tonic-gate /* 48210Sstevel@tonic-gate * Need to have a router header to use these. 48220Sstevel@tonic-gate */ 48230Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 48240Sstevel@tonic-gate option_exists |= IPPF_RTDSTOPTS; 48250Sstevel@tonic-gate ip_hdr_len += ipp->ipp_rtdstoptslen; 48260Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) { 48270Sstevel@tonic-gate option_exists |= IPPF_RTDSTOPTS; 48280Sstevel@tonic-gate is_sticky |= IPPF_RTDSTOPTS; 48290Sstevel@tonic-gate ip_hdr_len += 48300Sstevel@tonic-gate icmp->icmp_sticky_ipp.ipp_rtdstoptslen; 48310Sstevel@tonic-gate } 48320Sstevel@tonic-gate } 48330Sstevel@tonic-gate 48340Sstevel@tonic-gate if (!(ignore & IPPF_DSTOPTS)) { 48350Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_DSTOPTS) { 48360Sstevel@tonic-gate option_exists |= IPPF_DSTOPTS; 48370Sstevel@tonic-gate ip_hdr_len += ipp->ipp_dstoptslen; 48380Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) { 48390Sstevel@tonic-gate option_exists |= IPPF_DSTOPTS; 48400Sstevel@tonic-gate is_sticky |= IPPF_DSTOPTS; 48410Sstevel@tonic-gate ip_hdr_len += icmp->icmp_sticky_ipp.ipp_dstoptslen; 48420Sstevel@tonic-gate } 48430Sstevel@tonic-gate } 48440Sstevel@tonic-gate 48450Sstevel@tonic-gate if (!(ignore & IPPF_IFINDEX)) { 48460Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_IFINDEX) { 48470Sstevel@tonic-gate option_exists |= IPPF_IFINDEX; 48480Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_IFINDEX) { 48490Sstevel@tonic-gate option_exists |= IPPF_IFINDEX; 48500Sstevel@tonic-gate is_sticky |= IPPF_IFINDEX; 48510Sstevel@tonic-gate } 48520Sstevel@tonic-gate } 48530Sstevel@tonic-gate 48540Sstevel@tonic-gate if (!(ignore & IPPF_ADDR)) { 48550Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_ADDR) { 48560Sstevel@tonic-gate option_exists |= IPPF_ADDR; 48570Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_ADDR) { 48580Sstevel@tonic-gate option_exists |= IPPF_ADDR; 48590Sstevel@tonic-gate is_sticky |= IPPF_ADDR; 48600Sstevel@tonic-gate } 48610Sstevel@tonic-gate } 48620Sstevel@tonic-gate 48630Sstevel@tonic-gate if (!(ignore & IPPF_DONTFRAG)) { 48640Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_DONTFRAG) { 48650Sstevel@tonic-gate option_exists |= IPPF_DONTFRAG; 48660Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) { 48670Sstevel@tonic-gate option_exists |= IPPF_DONTFRAG; 48680Sstevel@tonic-gate is_sticky |= IPPF_DONTFRAG; 48690Sstevel@tonic-gate } 48700Sstevel@tonic-gate } 48710Sstevel@tonic-gate 48720Sstevel@tonic-gate if (!(ignore & IPPF_USE_MIN_MTU)) { 48730Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_USE_MIN_MTU) { 48740Sstevel@tonic-gate option_exists |= IPPF_USE_MIN_MTU; 48750Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & 48760Sstevel@tonic-gate IPPF_USE_MIN_MTU) { 48770Sstevel@tonic-gate option_exists |= IPPF_USE_MIN_MTU; 48780Sstevel@tonic-gate is_sticky |= IPPF_USE_MIN_MTU; 48790Sstevel@tonic-gate } 48800Sstevel@tonic-gate } 48810Sstevel@tonic-gate 48820Sstevel@tonic-gate if (!(ignore & IPPF_NEXTHOP)) { 48830Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_NEXTHOP) { 48840Sstevel@tonic-gate option_exists |= IPPF_NEXTHOP; 48850Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NEXTHOP) { 48860Sstevel@tonic-gate option_exists |= IPPF_NEXTHOP; 48870Sstevel@tonic-gate is_sticky |= IPPF_NEXTHOP; 48880Sstevel@tonic-gate } 48890Sstevel@tonic-gate } 48900Sstevel@tonic-gate 4891679Sseb if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT)) 4892679Sseb option_exists |= IPPF_HOPLIMIT; 4893679Sseb /* IPV6_HOPLIMIT can never be sticky */ 4894679Sseb ASSERT(!(icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT)); 4895679Sseb 4896679Sseb if (!(ignore & IPPF_UNICAST_HOPS) && 4897679Sseb (icmp->icmp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) { 4898679Sseb option_exists |= IPPF_UNICAST_HOPS; 4899679Sseb is_sticky |= IPPF_UNICAST_HOPS; 4900679Sseb } 4901679Sseb 4902679Sseb if (!(ignore & IPPF_MULTICAST_HOPS) && 4903679Sseb (icmp->icmp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) { 4904679Sseb option_exists |= IPPF_MULTICAST_HOPS; 4905679Sseb is_sticky |= IPPF_MULTICAST_HOPS; 49060Sstevel@tonic-gate } 49070Sstevel@tonic-gate 49080Sstevel@tonic-gate if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_NO_CKSUM) { 49090Sstevel@tonic-gate /* This is a sticky socket option only */ 49100Sstevel@tonic-gate option_exists |= IPPF_NO_CKSUM; 49110Sstevel@tonic-gate is_sticky |= IPPF_NO_CKSUM; 49120Sstevel@tonic-gate } 49130Sstevel@tonic-gate 49140Sstevel@tonic-gate if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RAW_CKSUM) { 49150Sstevel@tonic-gate /* This is a sticky socket option only */ 49160Sstevel@tonic-gate option_exists |= IPPF_RAW_CKSUM; 49170Sstevel@tonic-gate is_sticky |= IPPF_RAW_CKSUM; 49180Sstevel@tonic-gate } 49190Sstevel@tonic-gate 49200Sstevel@tonic-gate if (!(ignore & IPPF_TCLASS)) { 49210Sstevel@tonic-gate if (ipp->ipp_fields & IPPF_TCLASS) { 49220Sstevel@tonic-gate option_exists |= IPPF_TCLASS; 49230Sstevel@tonic-gate } else if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_TCLASS) { 49240Sstevel@tonic-gate option_exists |= IPPF_TCLASS; 49250Sstevel@tonic-gate is_sticky |= IPPF_TCLASS; 49260Sstevel@tonic-gate } 49270Sstevel@tonic-gate } 49280Sstevel@tonic-gate 49290Sstevel@tonic-gate no_options: 49300Sstevel@tonic-gate 49310Sstevel@tonic-gate /* 49320Sstevel@tonic-gate * If any options carried in the ip6i_t were specified, we 49330Sstevel@tonic-gate * need to account for the ip6i_t in the data we'll be sending 49340Sstevel@tonic-gate * down. 49350Sstevel@tonic-gate */ 49360Sstevel@tonic-gate if (option_exists & IPPF_HAS_IP6I) 49370Sstevel@tonic-gate ip_hdr_len += sizeof (ip6i_t); 49380Sstevel@tonic-gate 49390Sstevel@tonic-gate /* check/fix buffer config, setup pointers into it */ 49400Sstevel@tonic-gate mp1 = mp->b_cont; 49410Sstevel@tonic-gate ip6h = (ip6_t *)&mp1->b_rptr[-ip_hdr_len]; 49420Sstevel@tonic-gate if ((mp1->b_datap->db_ref != 1) || 49430Sstevel@tonic-gate ((unsigned char *)ip6h < mp1->b_datap->db_base) || 49440Sstevel@tonic-gate !OK_32PTR(ip6h)) { 49450Sstevel@tonic-gate /* Try to get everything in a single mblk next time */ 49460Sstevel@tonic-gate if (ip_hdr_len > icmp->icmp_max_hdr_len) { 49470Sstevel@tonic-gate icmp->icmp_max_hdr_len = ip_hdr_len; 49480Sstevel@tonic-gate (void) mi_set_sth_wroff(RD(q), 49493448Sdh155122 icmp->icmp_max_hdr_len + is->is_wroff_extra); 49500Sstevel@tonic-gate } 49513448Sdh155122 mp1 = allocb(ip_hdr_len + is->is_wroff_extra, BPRI_LO); 49520Sstevel@tonic-gate if (!mp1) { 49535240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 49540Sstevel@tonic-gate icmp_ud_err(q, mp, ENOMEM); 49550Sstevel@tonic-gate return; 49560Sstevel@tonic-gate } 49570Sstevel@tonic-gate mp1->b_cont = mp->b_cont; 49580Sstevel@tonic-gate mp1->b_wptr = mp1->b_datap->db_lim; 49590Sstevel@tonic-gate ip6h = (ip6_t *)(mp1->b_wptr - ip_hdr_len); 49600Sstevel@tonic-gate } 49610Sstevel@tonic-gate mp1->b_rptr = (unsigned char *)ip6h; 49620Sstevel@tonic-gate ip6i = (ip6i_t *)ip6h; 49630Sstevel@tonic-gate 49640Sstevel@tonic-gate #define ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &icmp->icmp_sticky_ipp : ipp) 49650Sstevel@tonic-gate if (option_exists & IPPF_HAS_IP6I) { 49660Sstevel@tonic-gate ip6h = (ip6_t *)&ip6i[1]; 49670Sstevel@tonic-gate ip6i->ip6i_flags = 0; 49680Sstevel@tonic-gate ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 49690Sstevel@tonic-gate 49700Sstevel@tonic-gate /* sin6_scope_id takes precendence over IPPF_IFINDEX */ 49710Sstevel@tonic-gate if (option_exists & IPPF_SCOPE_ID) { 49720Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_IFINDEX; 49730Sstevel@tonic-gate ip6i->ip6i_ifindex = sin6->sin6_scope_id; 49740Sstevel@tonic-gate } else if (option_exists & IPPF_IFINDEX) { 49750Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX); 49760Sstevel@tonic-gate ASSERT(tipp->ipp_ifindex != 0); 49770Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_IFINDEX; 49780Sstevel@tonic-gate ip6i->ip6i_ifindex = tipp->ipp_ifindex; 49790Sstevel@tonic-gate } 49800Sstevel@tonic-gate 49810Sstevel@tonic-gate if (option_exists & IPPF_RAW_CKSUM) { 49820Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM; 49830Sstevel@tonic-gate ip6i->ip6i_checksum_off = icmp->icmp_checksum_off; 49840Sstevel@tonic-gate } 49850Sstevel@tonic-gate 49860Sstevel@tonic-gate if (option_exists & IPPF_NO_CKSUM) { 49870Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM; 49880Sstevel@tonic-gate } 49890Sstevel@tonic-gate 49900Sstevel@tonic-gate if (option_exists & IPPF_ADDR) { 49910Sstevel@tonic-gate /* 49920Sstevel@tonic-gate * Enable per-packet source address verification if 49930Sstevel@tonic-gate * IPV6_PKTINFO specified the source address. 49940Sstevel@tonic-gate * ip6_src is set in the transport's _wput function. 49950Sstevel@tonic-gate */ 49960Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_VERIFY_SRC; 49970Sstevel@tonic-gate } 49980Sstevel@tonic-gate 49990Sstevel@tonic-gate if (option_exists & IPPF_DONTFRAG) { 50000Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_DONTFRAG; 50010Sstevel@tonic-gate } 50020Sstevel@tonic-gate 50030Sstevel@tonic-gate if (option_exists & IPPF_USE_MIN_MTU) { 50040Sstevel@tonic-gate ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU( 50050Sstevel@tonic-gate ip6i->ip6i_flags, ipp->ipp_use_min_mtu); 50060Sstevel@tonic-gate } 50070Sstevel@tonic-gate 50080Sstevel@tonic-gate if (option_exists & IPPF_NEXTHOP) { 50090Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP); 50100Sstevel@tonic-gate ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop)); 50110Sstevel@tonic-gate ip6i->ip6i_flags |= IP6I_NEXTHOP; 50120Sstevel@tonic-gate ip6i->ip6i_nexthop = tipp->ipp_nexthop; 50130Sstevel@tonic-gate } 50140Sstevel@tonic-gate 50150Sstevel@tonic-gate /* 50160Sstevel@tonic-gate * tell IP this is an ip6i_t private header 50170Sstevel@tonic-gate */ 50180Sstevel@tonic-gate ip6i->ip6i_nxt = IPPROTO_RAW; 50190Sstevel@tonic-gate } 50200Sstevel@tonic-gate 50210Sstevel@tonic-gate /* Initialize IPv6 header */ 50220Sstevel@tonic-gate ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 50230Sstevel@tonic-gate bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src)); 50240Sstevel@tonic-gate 5025679Sseb /* Set the hoplimit of the outgoing packet. */ 50260Sstevel@tonic-gate if (option_exists & IPPF_HOPLIMIT) { 5027679Sseb /* IPV6_HOPLIMIT ancillary data overrides all other settings. */ 5028679Sseb ip6h->ip6_hops = ipp->ipp_hoplimit; 5029679Sseb ip6i->ip6i_flags |= IP6I_HOPLIMIT; 5030679Sseb } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 50310Sstevel@tonic-gate ip6h->ip6_hops = icmp->icmp_multicast_ttl; 5032679Sseb if (option_exists & IPPF_MULTICAST_HOPS) 5033679Sseb ip6i->ip6i_flags |= IP6I_HOPLIMIT; 50340Sstevel@tonic-gate } else { 50350Sstevel@tonic-gate ip6h->ip6_hops = icmp->icmp_ttl; 5036679Sseb if (option_exists & IPPF_UNICAST_HOPS) 5037679Sseb ip6i->ip6i_flags |= IP6I_HOPLIMIT; 50380Sstevel@tonic-gate } 50390Sstevel@tonic-gate 50400Sstevel@tonic-gate if (option_exists & IPPF_ADDR) { 50410Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR); 50420Sstevel@tonic-gate ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr)); 50430Sstevel@tonic-gate ip6h->ip6_src = tipp->ipp_addr; 50440Sstevel@tonic-gate } else { 50450Sstevel@tonic-gate /* 50460Sstevel@tonic-gate * The source address was not set using IPV6_PKTINFO. 50470Sstevel@tonic-gate * First look at the bound source. 50480Sstevel@tonic-gate * If unspecified fallback to __sin6_src_id. 50490Sstevel@tonic-gate */ 50500Sstevel@tonic-gate ip6h->ip6_src = icmp->icmp_v6src; 50510Sstevel@tonic-gate if (sin6->__sin6_src_id != 0 && 50520Sstevel@tonic-gate IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) { 50530Sstevel@tonic-gate ip_srcid_find_id(sin6->__sin6_src_id, 50543448Sdh155122 &ip6h->ip6_src, icmp->icmp_zoneid, 50553448Sdh155122 is->is_netstack); 50560Sstevel@tonic-gate } 50570Sstevel@tonic-gate } 50580Sstevel@tonic-gate 50590Sstevel@tonic-gate nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt; 50600Sstevel@tonic-gate cp = (uint8_t *)&ip6h[1]; 50610Sstevel@tonic-gate 50620Sstevel@tonic-gate /* 50630Sstevel@tonic-gate * Here's where we have to start stringing together 50640Sstevel@tonic-gate * any extension headers in the right order: 50650Sstevel@tonic-gate * Hop-by-hop, destination, routing, and final destination opts. 50660Sstevel@tonic-gate */ 50670Sstevel@tonic-gate if (option_exists & IPPF_HOPOPTS) { 50680Sstevel@tonic-gate /* Hop-by-hop options */ 50690Sstevel@tonic-gate ip6_hbh_t *hbh = (ip6_hbh_t *)cp; 50700Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS); 50710Sstevel@tonic-gate 50720Sstevel@tonic-gate *nxthdr_ptr = IPPROTO_HOPOPTS; 50730Sstevel@tonic-gate nxthdr_ptr = &hbh->ip6h_nxt; 50740Sstevel@tonic-gate 50750Sstevel@tonic-gate bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen); 50760Sstevel@tonic-gate cp += tipp->ipp_hopoptslen; 50770Sstevel@tonic-gate } 50780Sstevel@tonic-gate /* 50790Sstevel@tonic-gate * En-route destination options 50800Sstevel@tonic-gate * Only do them if there's a routing header as well 50810Sstevel@tonic-gate */ 50820Sstevel@tonic-gate if (option_exists & IPPF_RTDSTOPTS) { 50830Sstevel@tonic-gate ip6_dest_t *dst = (ip6_dest_t *)cp; 50840Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS); 50850Sstevel@tonic-gate 50860Sstevel@tonic-gate *nxthdr_ptr = IPPROTO_DSTOPTS; 50870Sstevel@tonic-gate nxthdr_ptr = &dst->ip6d_nxt; 50880Sstevel@tonic-gate 50890Sstevel@tonic-gate bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen); 50900Sstevel@tonic-gate cp += tipp->ipp_rtdstoptslen; 50910Sstevel@tonic-gate } 50920Sstevel@tonic-gate /* 50930Sstevel@tonic-gate * Routing header next 50940Sstevel@tonic-gate */ 50950Sstevel@tonic-gate if (option_exists & IPPF_RTHDR) { 50960Sstevel@tonic-gate ip6_rthdr_t *rt = (ip6_rthdr_t *)cp; 50970Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR); 50980Sstevel@tonic-gate 50990Sstevel@tonic-gate *nxthdr_ptr = IPPROTO_ROUTING; 51000Sstevel@tonic-gate nxthdr_ptr = &rt->ip6r_nxt; 51010Sstevel@tonic-gate 51020Sstevel@tonic-gate bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen); 51030Sstevel@tonic-gate cp += tipp->ipp_rthdrlen; 51040Sstevel@tonic-gate } 51050Sstevel@tonic-gate /* 51060Sstevel@tonic-gate * Do ultimate destination options 51070Sstevel@tonic-gate */ 51080Sstevel@tonic-gate if (option_exists & IPPF_DSTOPTS) { 51090Sstevel@tonic-gate ip6_dest_t *dest = (ip6_dest_t *)cp; 51100Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS); 51110Sstevel@tonic-gate 51120Sstevel@tonic-gate *nxthdr_ptr = IPPROTO_DSTOPTS; 51130Sstevel@tonic-gate nxthdr_ptr = &dest->ip6d_nxt; 51140Sstevel@tonic-gate 51150Sstevel@tonic-gate bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen); 51160Sstevel@tonic-gate cp += tipp->ipp_dstoptslen; 51170Sstevel@tonic-gate } 51180Sstevel@tonic-gate 51190Sstevel@tonic-gate /* 51200Sstevel@tonic-gate * Now set the last header pointer to the proto passed in 51210Sstevel@tonic-gate */ 51220Sstevel@tonic-gate ASSERT((int)(cp - (uint8_t *)ip6i) == ip_hdr_len); 51230Sstevel@tonic-gate *nxthdr_ptr = icmp->icmp_proto; 51240Sstevel@tonic-gate 51250Sstevel@tonic-gate /* 51260Sstevel@tonic-gate * Copy in the destination address 51270Sstevel@tonic-gate */ 51281676Sjpk ip6h->ip6_dst = ip6_dst; 51290Sstevel@tonic-gate 51300Sstevel@tonic-gate ip6h->ip6_vcf = 51315240Snordmark (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 51325240Snordmark (sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 51330Sstevel@tonic-gate 51340Sstevel@tonic-gate if (option_exists & IPPF_TCLASS) { 51350Sstevel@tonic-gate tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS); 51360Sstevel@tonic-gate ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf, 51370Sstevel@tonic-gate tipp->ipp_tclass); 51380Sstevel@tonic-gate } 51390Sstevel@tonic-gate if (option_exists & IPPF_RTHDR) { 51400Sstevel@tonic-gate ip6_rthdr_t *rth; 51410Sstevel@tonic-gate 51420Sstevel@tonic-gate /* 51430Sstevel@tonic-gate * Perform any processing needed for source routing. 51440Sstevel@tonic-gate * We know that all extension headers will be in the same mblk 51450Sstevel@tonic-gate * as the IPv6 header. 51460Sstevel@tonic-gate */ 51470Sstevel@tonic-gate rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr); 51480Sstevel@tonic-gate if (rth != NULL && rth->ip6r_segleft != 0) { 51490Sstevel@tonic-gate if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) { 51500Sstevel@tonic-gate /* 51510Sstevel@tonic-gate * Drop packet - only support Type 0 routing. 51520Sstevel@tonic-gate * Notify the application as well. 51530Sstevel@tonic-gate */ 51540Sstevel@tonic-gate icmp_ud_err(q, mp, EPROTO); 51555240Snordmark BUMP_MIB(&is->is_rawip_mib, 51563448Sdh155122 rawipOutErrors); 51570Sstevel@tonic-gate return; 51580Sstevel@tonic-gate } 51590Sstevel@tonic-gate /* 51600Sstevel@tonic-gate * rth->ip6r_len is twice the number of 51610Sstevel@tonic-gate * addresses in the header 51620Sstevel@tonic-gate */ 51630Sstevel@tonic-gate if (rth->ip6r_len & 0x1) { 51640Sstevel@tonic-gate icmp_ud_err(q, mp, EPROTO); 51655240Snordmark BUMP_MIB(&is->is_rawip_mib, 51663448Sdh155122 rawipOutErrors); 51670Sstevel@tonic-gate return; 51680Sstevel@tonic-gate } 51690Sstevel@tonic-gate /* 51700Sstevel@tonic-gate * Shuffle the routing header and ip6_dst 51710Sstevel@tonic-gate * addresses, and get the checksum difference 51720Sstevel@tonic-gate * between the first hop (in ip6_dst) and 51730Sstevel@tonic-gate * the destination (in the last routing hdr entry). 51740Sstevel@tonic-gate */ 51753448Sdh155122 csum = ip_massage_options_v6(ip6h, rth, 51765240Snordmark is->is_netstack); 51770Sstevel@tonic-gate /* 51780Sstevel@tonic-gate * Verify that the first hop isn't a mapped address. 51790Sstevel@tonic-gate * Routers along the path need to do this verification 51800Sstevel@tonic-gate * for subsequent hops. 51810Sstevel@tonic-gate */ 51820Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) { 51830Sstevel@tonic-gate icmp_ud_err(q, mp, EADDRNOTAVAIL); 51845240Snordmark BUMP_MIB(&is->is_rawip_mib, 51853448Sdh155122 rawipOutErrors); 51860Sstevel@tonic-gate return; 51870Sstevel@tonic-gate } 51880Sstevel@tonic-gate } 51890Sstevel@tonic-gate } 51900Sstevel@tonic-gate 51910Sstevel@tonic-gate ip_len = mp1->b_wptr - (uchar_t *)ip6h - IPV6_HDR_LEN; 51920Sstevel@tonic-gate if (mp1->b_cont != NULL) 51930Sstevel@tonic-gate ip_len += msgdsize(mp1->b_cont); 51940Sstevel@tonic-gate 51950Sstevel@tonic-gate /* 51960Sstevel@tonic-gate * Set the length into the IP header. 51970Sstevel@tonic-gate * If the length is greater than the maximum allowed by IP, 51980Sstevel@tonic-gate * then free the message and return. Do not try and send it 51990Sstevel@tonic-gate * as this can cause problems in layers below. 52000Sstevel@tonic-gate */ 52010Sstevel@tonic-gate if (ip_len > IP_MAXPACKET) { 52025240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutErrors); 52031289Sja97890 icmp_ud_err(q, mp, EMSGSIZE); 52040Sstevel@tonic-gate return; 52050Sstevel@tonic-gate } 52060Sstevel@tonic-gate if (icmp->icmp_proto == IPPROTO_ICMPV6 || icmp->icmp_raw_checksum) { 52070Sstevel@tonic-gate uint_t cksum_off; /* From ip6i == mp1->b_rptr */ 52080Sstevel@tonic-gate uint16_t *cksum_ptr; 52090Sstevel@tonic-gate uint_t ext_hdrs_len; 52100Sstevel@tonic-gate 52110Sstevel@tonic-gate /* ICMPv6 must have an offset matching icmp6_cksum offset */ 52120Sstevel@tonic-gate ASSERT(icmp->icmp_proto != IPPROTO_ICMPV6 || 52130Sstevel@tonic-gate icmp->icmp_checksum_off == 2); 52140Sstevel@tonic-gate 52150Sstevel@tonic-gate /* 52160Sstevel@tonic-gate * We make it easy for IP to include our pseudo header 52170Sstevel@tonic-gate * by putting our length in uh_checksum, modified (if 52180Sstevel@tonic-gate * we have a routing header) by the checksum difference 52190Sstevel@tonic-gate * between the ultimate destination and first hop addresses. 52200Sstevel@tonic-gate * Note: ICMPv6 must always checksum the packet. 52210Sstevel@tonic-gate */ 52220Sstevel@tonic-gate cksum_off = ip_hdr_len + icmp->icmp_checksum_off; 52230Sstevel@tonic-gate if (cksum_off + sizeof (uint16_t) > mp1->b_wptr - mp1->b_rptr) { 52240Sstevel@tonic-gate if (!pullupmsg(mp1, cksum_off + sizeof (uint16_t))) { 52255240Snordmark BUMP_MIB(&is->is_rawip_mib, 52263448Sdh155122 rawipOutErrors); 52270Sstevel@tonic-gate freemsg(mp); 52280Sstevel@tonic-gate return; 52290Sstevel@tonic-gate } 52300Sstevel@tonic-gate ip6i = (ip6i_t *)mp1->b_rptr; 52310Sstevel@tonic-gate if (ip6i->ip6i_nxt == IPPROTO_RAW) 52320Sstevel@tonic-gate ip6h = (ip6_t *)&ip6i[1]; 52330Sstevel@tonic-gate else 52340Sstevel@tonic-gate ip6h = (ip6_t *)ip6i; 52350Sstevel@tonic-gate } 52360Sstevel@tonic-gate /* Add payload length to checksum */ 52370Sstevel@tonic-gate ext_hdrs_len = ip_hdr_len - IPV6_HDR_LEN - 52380Sstevel@tonic-gate (int)((uchar_t *)ip6h - (uchar_t *)ip6i); 52390Sstevel@tonic-gate csum += htons(ip_len - ext_hdrs_len); 52400Sstevel@tonic-gate 52410Sstevel@tonic-gate cksum_ptr = (uint16_t *)((uchar_t *)ip6i + cksum_off); 52420Sstevel@tonic-gate csum = (csum & 0xFFFF) + (csum >> 16); 52430Sstevel@tonic-gate *cksum_ptr = (uint16_t)csum; 52440Sstevel@tonic-gate } 52450Sstevel@tonic-gate 52460Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN 52470Sstevel@tonic-gate ip_len = htons(ip_len); 52480Sstevel@tonic-gate #endif 52490Sstevel@tonic-gate ip6h->ip6_plen = (uint16_t)ip_len; 52500Sstevel@tonic-gate 52510Sstevel@tonic-gate freeb(mp); 52520Sstevel@tonic-gate 52530Sstevel@tonic-gate /* We're done. Pass the packet to IP */ 52545240Snordmark BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams); 52555240Snordmark ip_output_v6(icmp->icmp_connp, mp1, q, IP_WPUT); 52560Sstevel@tonic-gate } 52570Sstevel@tonic-gate 52580Sstevel@tonic-gate static void 52590Sstevel@tonic-gate icmp_wput_other(queue_t *q, mblk_t *mp) 52600Sstevel@tonic-gate { 52610Sstevel@tonic-gate uchar_t *rptr = mp->b_rptr; 52620Sstevel@tonic-gate struct iocblk *iocp; 52630Sstevel@tonic-gate #define tudr ((struct T_unitdata_req *)rptr) 52645240Snordmark conn_t *connp = Q_TO_CONN(q); 52655240Snordmark icmp_t *icmp = connp->conn_icmp; 52665240Snordmark icmp_stack_t *is = icmp->icmp_is; 52670Sstevel@tonic-gate cred_t *cr; 52680Sstevel@tonic-gate 52695240Snordmark cr = DB_CREDDEF(mp, connp->conn_cred); 52700Sstevel@tonic-gate 52710Sstevel@tonic-gate switch (mp->b_datap->db_type) { 52720Sstevel@tonic-gate case M_PROTO: 52730Sstevel@tonic-gate case M_PCPROTO: 52740Sstevel@tonic-gate if (mp->b_wptr - rptr < sizeof (t_scalar_t)) { 52750Sstevel@tonic-gate /* 52760Sstevel@tonic-gate * If the message does not contain a PRIM_type, 52770Sstevel@tonic-gate * throw it away. 52780Sstevel@tonic-gate */ 52790Sstevel@tonic-gate freemsg(mp); 52800Sstevel@tonic-gate return; 52810Sstevel@tonic-gate } 52820Sstevel@tonic-gate switch (((union T_primitives *)rptr)->type) { 52830Sstevel@tonic-gate case T_ADDR_REQ: 52840Sstevel@tonic-gate icmp_addr_req(q, mp); 52850Sstevel@tonic-gate return; 52860Sstevel@tonic-gate case O_T_BIND_REQ: 52870Sstevel@tonic-gate case T_BIND_REQ: 52885240Snordmark icmp_bind(q, mp); 52890Sstevel@tonic-gate return; 52900Sstevel@tonic-gate case T_CONN_REQ: 52910Sstevel@tonic-gate icmp_connect(q, mp); 52920Sstevel@tonic-gate return; 52930Sstevel@tonic-gate case T_CAPABILITY_REQ: 52940Sstevel@tonic-gate icmp_capability_req(q, mp); 52950Sstevel@tonic-gate return; 52960Sstevel@tonic-gate case T_INFO_REQ: 52970Sstevel@tonic-gate icmp_info_req(q, mp); 52980Sstevel@tonic-gate return; 52990Sstevel@tonic-gate case T_UNITDATA_REQ: 53000Sstevel@tonic-gate /* 53010Sstevel@tonic-gate * If a T_UNITDATA_REQ gets here, the address must 53020Sstevel@tonic-gate * be bad. Valid T_UNITDATA_REQs are found above 53030Sstevel@tonic-gate * and break to below this switch. 53040Sstevel@tonic-gate */ 53050Sstevel@tonic-gate icmp_ud_err(q, mp, EADDRNOTAVAIL); 53060Sstevel@tonic-gate return; 53070Sstevel@tonic-gate case T_UNBIND_REQ: 53080Sstevel@tonic-gate icmp_unbind(q, mp); 53090Sstevel@tonic-gate return; 53100Sstevel@tonic-gate 53110Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ: 53125240Snordmark if (!snmpcom_req(q, mp, icmp_snmp_set, ip_snmp_get, 53135240Snordmark cr)) { 53140Sstevel@tonic-gate /* Only IP can return anything meaningful */ 53150Sstevel@tonic-gate (void) svr4_optcom_req(q, mp, cr, 53165240Snordmark &icmp_opt_obj, B_TRUE); 53175240Snordmark } 53180Sstevel@tonic-gate return; 53190Sstevel@tonic-gate 53200Sstevel@tonic-gate case T_OPTMGMT_REQ: 53210Sstevel@tonic-gate /* Only IP can return anything meaningful */ 53225240Snordmark (void) tpi_optcom_req(q, mp, cr, &icmp_opt_obj, B_TRUE); 53230Sstevel@tonic-gate return; 53240Sstevel@tonic-gate 53250Sstevel@tonic-gate case T_DISCON_REQ: 53260Sstevel@tonic-gate icmp_disconnect(q, mp); 53270Sstevel@tonic-gate return; 53280Sstevel@tonic-gate 53290Sstevel@tonic-gate /* The following TPI message is not supported by icmp. */ 53300Sstevel@tonic-gate case O_T_CONN_RES: 53310Sstevel@tonic-gate case T_CONN_RES: 53320Sstevel@tonic-gate icmp_err_ack(q, mp, TNOTSUPPORT, 0); 53330Sstevel@tonic-gate return; 53340Sstevel@tonic-gate 53350Sstevel@tonic-gate /* The following 3 TPI requests are illegal for icmp. */ 53360Sstevel@tonic-gate case T_DATA_REQ: 53370Sstevel@tonic-gate case T_EXDATA_REQ: 53380Sstevel@tonic-gate case T_ORDREL_REQ: 53390Sstevel@tonic-gate freemsg(mp); 53400Sstevel@tonic-gate (void) putctl1(RD(q), M_ERROR, EPROTO); 53410Sstevel@tonic-gate return; 53420Sstevel@tonic-gate default: 53430Sstevel@tonic-gate break; 53440Sstevel@tonic-gate } 53450Sstevel@tonic-gate break; 53460Sstevel@tonic-gate case M_IOCTL: 53470Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 53480Sstevel@tonic-gate switch (iocp->ioc_cmd) { 53490Sstevel@tonic-gate case TI_GETPEERNAME: 53500Sstevel@tonic-gate if (icmp->icmp_state != TS_DATA_XFER) { 53510Sstevel@tonic-gate /* 53520Sstevel@tonic-gate * If a default destination address has not 53530Sstevel@tonic-gate * been associated with the stream, then we 53540Sstevel@tonic-gate * don't know the peer's name. 53550Sstevel@tonic-gate */ 53560Sstevel@tonic-gate iocp->ioc_error = ENOTCONN; 53575240Snordmark err_ret:; 53580Sstevel@tonic-gate iocp->ioc_count = 0; 53590Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 53600Sstevel@tonic-gate qreply(q, mp); 53610Sstevel@tonic-gate return; 53620Sstevel@tonic-gate } 53630Sstevel@tonic-gate /* FALLTHRU */ 53640Sstevel@tonic-gate case TI_GETMYNAME: 53650Sstevel@tonic-gate /* 53660Sstevel@tonic-gate * For TI_GETPEERNAME and TI_GETMYNAME, we first 53670Sstevel@tonic-gate * need to copyin the user's strbuf structure. 53680Sstevel@tonic-gate * Processing will continue in the M_IOCDATA case 53690Sstevel@tonic-gate * below. 53700Sstevel@tonic-gate */ 53710Sstevel@tonic-gate mi_copyin(q, mp, NULL, 53720Sstevel@tonic-gate SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 53730Sstevel@tonic-gate return; 53740Sstevel@tonic-gate case ND_SET: 53750Sstevel@tonic-gate /* nd_getset performs the necessary error checking */ 53760Sstevel@tonic-gate case ND_GET: 53775240Snordmark if (nd_getset(q, is->is_nd, mp)) { 53780Sstevel@tonic-gate qreply(q, mp); 53790Sstevel@tonic-gate return; 53800Sstevel@tonic-gate } 53810Sstevel@tonic-gate break; 53820Sstevel@tonic-gate default: 53830Sstevel@tonic-gate break; 53840Sstevel@tonic-gate } 53850Sstevel@tonic-gate break; 53860Sstevel@tonic-gate case M_IOCDATA: 53870Sstevel@tonic-gate icmp_wput_iocdata(q, mp); 53880Sstevel@tonic-gate return; 53890Sstevel@tonic-gate default: 53900Sstevel@tonic-gate break; 53910Sstevel@tonic-gate } 53925240Snordmark ip_wput(q, mp); 53930Sstevel@tonic-gate } 53940Sstevel@tonic-gate 53950Sstevel@tonic-gate /* 53960Sstevel@tonic-gate * icmp_wput_iocdata is called by icmp_wput_slow to handle all M_IOCDATA 53970Sstevel@tonic-gate * messages. 53980Sstevel@tonic-gate */ 53990Sstevel@tonic-gate static void 54000Sstevel@tonic-gate icmp_wput_iocdata(queue_t *q, mblk_t *mp) 54010Sstevel@tonic-gate { 54020Sstevel@tonic-gate mblk_t *mp1; 54030Sstevel@tonic-gate STRUCT_HANDLE(strbuf, sb); 54040Sstevel@tonic-gate icmp_t *icmp; 54050Sstevel@tonic-gate in6_addr_t v6addr; 54060Sstevel@tonic-gate ipaddr_t v4addr; 54070Sstevel@tonic-gate uint32_t flowinfo = 0; 54080Sstevel@tonic-gate int addrlen; 54090Sstevel@tonic-gate 54100Sstevel@tonic-gate /* Make sure it is one of ours. */ 54110Sstevel@tonic-gate switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 54120Sstevel@tonic-gate case TI_GETMYNAME: 54130Sstevel@tonic-gate case TI_GETPEERNAME: 54140Sstevel@tonic-gate break; 54150Sstevel@tonic-gate default: 54165240Snordmark icmp = Q_TO_ICMP(q); 54175240Snordmark ip_output(icmp->icmp_connp, mp, q, IP_WPUT); 54180Sstevel@tonic-gate return; 54190Sstevel@tonic-gate } 54200Sstevel@tonic-gate switch (mi_copy_state(q, mp, &mp1)) { 54210Sstevel@tonic-gate case -1: 54220Sstevel@tonic-gate return; 54230Sstevel@tonic-gate case MI_COPY_CASE(MI_COPY_IN, 1): 54240Sstevel@tonic-gate break; 54250Sstevel@tonic-gate case MI_COPY_CASE(MI_COPY_OUT, 1): 54260Sstevel@tonic-gate /* 54270Sstevel@tonic-gate * The address has been copied out, so now 54280Sstevel@tonic-gate * copyout the strbuf. 54290Sstevel@tonic-gate */ 54300Sstevel@tonic-gate mi_copyout(q, mp); 54310Sstevel@tonic-gate return; 54320Sstevel@tonic-gate case MI_COPY_CASE(MI_COPY_OUT, 2): 54330Sstevel@tonic-gate /* 54340Sstevel@tonic-gate * The address and strbuf have been copied out. 54350Sstevel@tonic-gate * We're done, so just acknowledge the original 54360Sstevel@tonic-gate * M_IOCTL. 54370Sstevel@tonic-gate */ 54380Sstevel@tonic-gate mi_copy_done(q, mp, 0); 54390Sstevel@tonic-gate return; 54400Sstevel@tonic-gate default: 54410Sstevel@tonic-gate /* 54420Sstevel@tonic-gate * Something strange has happened, so acknowledge 54430Sstevel@tonic-gate * the original M_IOCTL with an EPROTO error. 54440Sstevel@tonic-gate */ 54450Sstevel@tonic-gate mi_copy_done(q, mp, EPROTO); 54460Sstevel@tonic-gate return; 54470Sstevel@tonic-gate } 54480Sstevel@tonic-gate /* 54490Sstevel@tonic-gate * Now we have the strbuf structure for TI_GETMYNAME 54500Sstevel@tonic-gate * and TI_GETPEERNAME. Next we copyout the requested 54510Sstevel@tonic-gate * address and then we'll copyout the strbuf. 54520Sstevel@tonic-gate */ 54530Sstevel@tonic-gate STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 54540Sstevel@tonic-gate (void *)mp1->b_rptr); 54555240Snordmark icmp = Q_TO_ICMP(q); 54560Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) 54570Sstevel@tonic-gate addrlen = sizeof (sin_t); 54580Sstevel@tonic-gate else 54590Sstevel@tonic-gate addrlen = sizeof (sin6_t); 54600Sstevel@tonic-gate 54610Sstevel@tonic-gate if (STRUCT_FGET(sb, maxlen) < addrlen) { 54620Sstevel@tonic-gate mi_copy_done(q, mp, EINVAL); 54630Sstevel@tonic-gate return; 54640Sstevel@tonic-gate } 54650Sstevel@tonic-gate switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 54660Sstevel@tonic-gate case TI_GETMYNAME: 54670Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 54680Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV4_VERSION); 54690Sstevel@tonic-gate if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) && 54700Sstevel@tonic-gate !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) { 54710Sstevel@tonic-gate v4addr = V4_PART_OF_V6(icmp->icmp_v6src); 54720Sstevel@tonic-gate } else { 54730Sstevel@tonic-gate /* 54740Sstevel@tonic-gate * INADDR_ANY 54750Sstevel@tonic-gate * icmp_v6src is not set, we might be bound to 54760Sstevel@tonic-gate * broadcast/multicast. Use icmp_bound_v6src as 54770Sstevel@tonic-gate * local address instead (that could 54780Sstevel@tonic-gate * also still be INADDR_ANY) 54790Sstevel@tonic-gate */ 54800Sstevel@tonic-gate v4addr = V4_PART_OF_V6(icmp->icmp_bound_v6src); 54810Sstevel@tonic-gate } 54820Sstevel@tonic-gate } else { 54830Sstevel@tonic-gate /* icmp->icmp_family == AF_INET6 */ 54840Sstevel@tonic-gate if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) { 54850Sstevel@tonic-gate v6addr = icmp->icmp_v6src; 54860Sstevel@tonic-gate } else { 54870Sstevel@tonic-gate /* 54880Sstevel@tonic-gate * UNSPECIFIED 54890Sstevel@tonic-gate * icmp_v6src is not set, we might be bound to 54900Sstevel@tonic-gate * broadcast/multicast. Use icmp_bound_v6src as 54910Sstevel@tonic-gate * local address instead (that could 54920Sstevel@tonic-gate * also still be UNSPECIFIED) 54930Sstevel@tonic-gate */ 54940Sstevel@tonic-gate v6addr = icmp->icmp_bound_v6src; 54950Sstevel@tonic-gate } 54960Sstevel@tonic-gate } 54970Sstevel@tonic-gate break; 54980Sstevel@tonic-gate case TI_GETPEERNAME: 54990Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 55000Sstevel@tonic-gate ASSERT(icmp->icmp_ipversion == IPV4_VERSION); 55010Sstevel@tonic-gate v4addr = V4_PART_OF_V6(icmp->icmp_v6dst); 55020Sstevel@tonic-gate } else { 55030Sstevel@tonic-gate /* icmp->icmp_family == AF_INET6) */ 55040Sstevel@tonic-gate v6addr = icmp->icmp_v6dst; 55050Sstevel@tonic-gate flowinfo = icmp->icmp_flowinfo; 55060Sstevel@tonic-gate } 55070Sstevel@tonic-gate break; 55080Sstevel@tonic-gate default: 55090Sstevel@tonic-gate mi_copy_done(q, mp, EPROTO); 55100Sstevel@tonic-gate return; 55110Sstevel@tonic-gate } 55120Sstevel@tonic-gate mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 55130Sstevel@tonic-gate if (!mp1) 55140Sstevel@tonic-gate return; 55150Sstevel@tonic-gate 55160Sstevel@tonic-gate if (icmp->icmp_family == AF_INET) { 55170Sstevel@tonic-gate sin_t *sin; 55180Sstevel@tonic-gate 55190Sstevel@tonic-gate STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 55200Sstevel@tonic-gate sin = (sin_t *)mp1->b_rptr; 55210Sstevel@tonic-gate mp1->b_wptr = (uchar_t *)&sin[1]; 55220Sstevel@tonic-gate *sin = sin_null; 55230Sstevel@tonic-gate sin->sin_family = AF_INET; 55240Sstevel@tonic-gate sin->sin_addr.s_addr = v4addr; 55250Sstevel@tonic-gate } else { 55260Sstevel@tonic-gate /* icmp->icmp_family == AF_INET6 */ 55270Sstevel@tonic-gate sin6_t *sin6; 55280Sstevel@tonic-gate 55290Sstevel@tonic-gate ASSERT(icmp->icmp_family == AF_INET6); 55300Sstevel@tonic-gate STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 55310Sstevel@tonic-gate sin6 = (sin6_t *)mp1->b_rptr; 55320Sstevel@tonic-gate mp1->b_wptr = (uchar_t *)&sin6[1]; 55330Sstevel@tonic-gate *sin6 = sin6_null; 55340Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 55350Sstevel@tonic-gate sin6->sin6_flowinfo = flowinfo; 55360Sstevel@tonic-gate sin6->sin6_addr = v6addr; 55370Sstevel@tonic-gate } 55380Sstevel@tonic-gate /* Copy out the address */ 55390Sstevel@tonic-gate mi_copyout(q, mp); 55400Sstevel@tonic-gate } 55410Sstevel@tonic-gate 55420Sstevel@tonic-gate static int 55430Sstevel@tonic-gate icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp, 55440Sstevel@tonic-gate void *thisdg_attrs) 55450Sstevel@tonic-gate { 55465240Snordmark conn_t *connp = Q_TO_CONN(q); 55470Sstevel@tonic-gate struct T_unitdata_req *udreqp; 55480Sstevel@tonic-gate int is_absreq_failure; 55490Sstevel@tonic-gate cred_t *cr; 55500Sstevel@tonic-gate 55510Sstevel@tonic-gate udreqp = (struct T_unitdata_req *)mp->b_rptr; 55520Sstevel@tonic-gate *errorp = 0; 55530Sstevel@tonic-gate 55545240Snordmark cr = DB_CREDDEF(mp, connp->conn_cred); 55550Sstevel@tonic-gate 55560Sstevel@tonic-gate *errorp = tpi_optcom_buf(q, mp, &udreqp->OPT_length, 55570Sstevel@tonic-gate udreqp->OPT_offset, cr, &icmp_opt_obj, 55580Sstevel@tonic-gate thisdg_attrs, &is_absreq_failure); 55590Sstevel@tonic-gate 55600Sstevel@tonic-gate if (*errorp != 0) { 55610Sstevel@tonic-gate /* 55620Sstevel@tonic-gate * Note: No special action needed in this 55630Sstevel@tonic-gate * module for "is_absreq_failure" 55640Sstevel@tonic-gate */ 55650Sstevel@tonic-gate return (-1); /* failure */ 55660Sstevel@tonic-gate } 55670Sstevel@tonic-gate ASSERT(is_absreq_failure == 0); 55680Sstevel@tonic-gate return (0); /* success */ 55690Sstevel@tonic-gate } 55700Sstevel@tonic-gate 55710Sstevel@tonic-gate void 55720Sstevel@tonic-gate icmp_ddi_init(void) 55730Sstevel@tonic-gate { 55745381Smeem icmp_max_optsize = optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr, 55755240Snordmark icmp_opt_obj.odb_opt_arr_cnt); 55760Sstevel@tonic-gate 55773448Sdh155122 /* 55783448Sdh155122 * We want to be informed each time a stack is created or 55793448Sdh155122 * destroyed in the kernel, so we can maintain the 55803448Sdh155122 * set of icmp_stack_t's. 55813448Sdh155122 */ 55823448Sdh155122 netstack_register(NS_ICMP, rawip_stack_init, NULL, rawip_stack_fini); 55830Sstevel@tonic-gate } 55840Sstevel@tonic-gate 55850Sstevel@tonic-gate void 55860Sstevel@tonic-gate icmp_ddi_destroy(void) 55870Sstevel@tonic-gate { 55883448Sdh155122 netstack_unregister(NS_ICMP); 55890Sstevel@tonic-gate } 55900Sstevel@tonic-gate 55913448Sdh155122 /* 55923448Sdh155122 * Initialize the ICMP stack instance. 55933448Sdh155122 */ 55943448Sdh155122 static void * 55953448Sdh155122 rawip_stack_init(netstackid_t stackid, netstack_t *ns) 55963448Sdh155122 { 55973448Sdh155122 icmp_stack_t *is; 55983448Sdh155122 icmpparam_t *pa; 55993448Sdh155122 56003448Sdh155122 is = (icmp_stack_t *)kmem_zalloc(sizeof (*is), KM_SLEEP); 56013448Sdh155122 is->is_netstack = ns; 56023448Sdh155122 56033448Sdh155122 pa = (icmpparam_t *)kmem_alloc(sizeof (icmp_param_arr), KM_SLEEP); 56043448Sdh155122 is->is_param_arr = pa; 56053448Sdh155122 bcopy(icmp_param_arr, is->is_param_arr, sizeof (icmp_param_arr)); 56063448Sdh155122 56073448Sdh155122 (void) icmp_param_register(&is->is_nd, 56083448Sdh155122 is->is_param_arr, A_CNT(icmp_param_arr)); 56093448Sdh155122 is->is_ksp = rawip_kstat_init(stackid); 56103448Sdh155122 return (is); 56113448Sdh155122 } 56123448Sdh155122 56133448Sdh155122 /* 56143448Sdh155122 * Free the ICMP stack instance. 56153448Sdh155122 */ 56160Sstevel@tonic-gate static void 56173448Sdh155122 rawip_stack_fini(netstackid_t stackid, void *arg) 56183448Sdh155122 { 56193448Sdh155122 icmp_stack_t *is = (icmp_stack_t *)arg; 56203448Sdh155122 56213448Sdh155122 nd_free(&is->is_nd); 56223448Sdh155122 kmem_free(is->is_param_arr, sizeof (icmp_param_arr)); 56233448Sdh155122 is->is_param_arr = NULL; 56243448Sdh155122 56253448Sdh155122 rawip_kstat_fini(stackid, is->is_ksp); 56263448Sdh155122 is->is_ksp = NULL; 56273448Sdh155122 kmem_free(is, sizeof (*is)); 56283448Sdh155122 } 56293448Sdh155122 56303448Sdh155122 static void * 56313448Sdh155122 rawip_kstat_init(netstackid_t stackid) { 56323448Sdh155122 kstat_t *ksp; 56330Sstevel@tonic-gate 56340Sstevel@tonic-gate rawip_named_kstat_t template = { 56350Sstevel@tonic-gate { "inDatagrams", KSTAT_DATA_UINT32, 0 }, 56360Sstevel@tonic-gate { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 56370Sstevel@tonic-gate { "inErrors", KSTAT_DATA_UINT32, 0 }, 56380Sstevel@tonic-gate { "outDatagrams", KSTAT_DATA_UINT32, 0 }, 56390Sstevel@tonic-gate { "outErrors", KSTAT_DATA_UINT32, 0 }, 56400Sstevel@tonic-gate }; 56410Sstevel@tonic-gate 56423448Sdh155122 ksp = kstat_create_netstack("icmp", 0, "rawip", "mib2", 56430Sstevel@tonic-gate KSTAT_TYPE_NAMED, 56440Sstevel@tonic-gate NUM_OF_FIELDS(rawip_named_kstat_t), 56453448Sdh155122 0, stackid); 56463448Sdh155122 if (ksp == NULL || ksp->ks_data == NULL) 56473448Sdh155122 return (NULL); 56483448Sdh155122 56493448Sdh155122 bcopy(&template, ksp->ks_data, sizeof (template)); 56503448Sdh155122 ksp->ks_update = rawip_kstat_update; 56513448Sdh155122 ksp->ks_private = (void *)(uintptr_t)stackid; 56523448Sdh155122 56533448Sdh155122 kstat_install(ksp); 56543448Sdh155122 return (ksp); 56550Sstevel@tonic-gate } 56560Sstevel@tonic-gate 56570Sstevel@tonic-gate static void 56583448Sdh155122 rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp) 56593448Sdh155122 { 56603448Sdh155122 if (ksp != NULL) { 56613448Sdh155122 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 56623448Sdh155122 kstat_delete_netstack(ksp, stackid); 56630Sstevel@tonic-gate } 56640Sstevel@tonic-gate } 56650Sstevel@tonic-gate 56660Sstevel@tonic-gate static int 56673448Sdh155122 rawip_kstat_update(kstat_t *ksp, int rw) 56683448Sdh155122 { 56690Sstevel@tonic-gate rawip_named_kstat_t *rawipkp; 56703448Sdh155122 netstackid_t stackid = (netstackid_t)(uintptr_t)ksp->ks_private; 56713448Sdh155122 netstack_t *ns; 56723448Sdh155122 icmp_stack_t *is; 56733448Sdh155122 56743448Sdh155122 if ((ksp == NULL) || (ksp->ks_data == NULL)) 56750Sstevel@tonic-gate return (EIO); 56760Sstevel@tonic-gate 56770Sstevel@tonic-gate if (rw == KSTAT_WRITE) 56780Sstevel@tonic-gate return (EACCES); 56790Sstevel@tonic-gate 56803448Sdh155122 rawipkp = (rawip_named_kstat_t *)ksp->ks_data; 56813448Sdh155122 56823448Sdh155122 ns = netstack_find_by_stackid(stackid); 56833448Sdh155122 if (ns == NULL) 56843448Sdh155122 return (-1); 56853448Sdh155122 is = ns->netstack_icmp; 56863448Sdh155122 if (is == NULL) { 56873448Sdh155122 netstack_rele(ns); 56883448Sdh155122 return (-1); 56893448Sdh155122 } 56903448Sdh155122 rawipkp->inDatagrams.value.ui32 = is->is_rawip_mib.rawipInDatagrams; 56913448Sdh155122 rawipkp->inCksumErrs.value.ui32 = is->is_rawip_mib.rawipInCksumErrs; 56923448Sdh155122 rawipkp->inErrors.value.ui32 = is->is_rawip_mib.rawipInErrors; 56933448Sdh155122 rawipkp->outDatagrams.value.ui32 = is->is_rawip_mib.rawipOutDatagrams; 56943448Sdh155122 rawipkp->outErrors.value.ui32 = is->is_rawip_mib.rawipOutErrors; 56953448Sdh155122 netstack_rele(ns); 56960Sstevel@tonic-gate return (0); 56970Sstevel@tonic-gate } 5698