| /* |
| * CDDL HEADER START |
| * |
| * The contents of this file are subject to the terms of the |
| * Common Development and Distribution License, Version 1.0 only |
| * (the "License"). You may not use this file except in compliance |
| * with the License. |
| * |
| * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
| * or http://www.opensolaris.org/os/licensing. |
| * See the License for the specific language governing permissions |
| * and limitations under the License. |
| * |
| * When distributing Covered Code, include this CDDL HEADER in each |
| * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
| * If applicable, add the following below this CDDL HEADER, with the |
| * fields enclosed by brackets "[]" replaced with your own identifying |
| * information: Portions Copyright [yyyy] [name of copyright owner] |
| * |
| * CDDL HEADER END |
| */ |
| /* |
| * Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
| * Use is subject to license terms. |
| */ |
| /* Copyright (c) 1990 Mentat Inc. */ |
| |
| |
| #pragma ident "%Z%%M% %I% %E% SMI" |
| |
| #include <sys/types.h> |
| #include <sys/stream.h> |
| #include <sys/stropts.h> |
| #include <sys/strlog.h> |
| #include <sys/strsun.h> |
| #define _SUN_TPI_VERSION 2 |
| #include <sys/tihdr.h> |
| #include <sys/timod.h> |
| #include <sys/ddi.h> |
| #include <sys/sunddi.h> |
| #include <sys/cmn_err.h> |
| #include <sys/debug.h> |
| #include <sys/kmem.h> |
| #include <sys/policy.h> |
| #include <sys/zone.h> |
| |
| #include <sys/socket.h> |
| #include <sys/isa_defs.h> |
| #include <sys/suntpi.h> |
| #include <sys/xti_inet.h> |
| |
| #include <net/route.h> |
| #include <net/if.h> |
| |
| #include <netinet/in.h> |
| #include <netinet/ip6.h> |
| #include <netinet/icmp6.h> |
| #include <inet/common.h> |
| #include <inet/ip.h> |
| #include <inet/ip6.h> |
| #include <inet/ip_ire.h> |
| #include <inet/mi.h> |
| #include <inet/nd.h> |
| #include <inet/optcom.h> |
| #include <inet/snmpcom.h> |
| #include <inet/kstatcom.h> |
| #include <inet/rawip_impl.h> |
| |
| #include <netinet/ip_mroute.h> |
| #include <inet/tcp.h> |
| #include <net/pfkeyv2.h> |
| #include <inet/ipsec_info.h> |
| #include <inet/ipclassifier.h> |
| |
| #define ICMP6 "icmp6" |
| major_t ICMP6_MAJ; |
| |
| /* |
| * Object to represent database of options to search passed to |
| * {sock,tpi}optcom_req() interface routine to take care of option |
| * management and associated methods. |
| * XXX These and other extern's should really move to a icmp header. |
| */ |
| extern optdb_obj_t icmp_opt_obj; |
| extern uint_t icmp_max_optsize; |
| |
| /* |
| * Synchronization notes: |
| * |
| * At all points in this code where exclusive access is required, we |
| * pass a message to a subroutine by invoking qwriter(..., PERIM_OUTER) |
| * which will arrange to call the routine only after all threads have |
| * exited the shared resource. |
| */ |
| |
| /* Named Dispatch Parameter Management Structure */ |
| typedef struct icmpparam_s { |
| uint_t icmp_param_min; |
| uint_t icmp_param_max; |
| uint_t icmp_param_value; |
| char *icmp_param_name; |
| } icmpparam_t; |
| |
| static void icmp_addr_req(queue_t *q, mblk_t *mp); |
| static void icmp_bind(queue_t *q, mblk_t *mp); |
| static void icmp_bind_proto(queue_t *q); |
| static int icmp_build_hdrs(queue_t *q, icmp_t *icmp); |
| static void icmp_capability_req(queue_t *q, mblk_t *mp); |
| static int icmp_close(queue_t *q); |
| static void icmp_connect(queue_t *q, mblk_t *mp); |
| static void icmp_disconnect(queue_t *q, mblk_t *mp); |
| static void icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, |
| int sys_error); |
| static void icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive, |
| t_scalar_t t_error, int sys_error); |
| static void icmp_icmp_error(queue_t *q, mblk_t *mp); |
| static void icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp); |
| static void icmp_info_req(queue_t *q, mblk_t *mp); |
| static mblk_t *icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, |
| t_scalar_t addr_length, in_port_t); |
| static int icmp_open(queue_t *q, dev_t *devp, int flag, |
| int sflag, cred_t *credp); |
| static int icmp_unitdata_opt_process(queue_t *q, mblk_t *mp, |
| int *errorp, void *thisdg_attrs); |
| static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name); |
| int icmp_opt_set(queue_t *q, uint_t optset_context, |
| int level, int name, uint_t inlen, |
| uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, |
| void *thisdg_attrs, cred_t *cr, mblk_t *mblk); |
| int icmp_opt_get(queue_t *q, int level, int name, |
| uchar_t *ptr); |
| static int icmp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); |
| static boolean_t icmp_param_register(icmpparam_t *icmppa, int cnt); |
| static int icmp_param_set(queue_t *q, mblk_t *mp, char *value, |
| caddr_t cp, cred_t *cr); |
| static int icmp_pkt_set(uchar_t *invalp, uint_t inlen, boolean_t sticky, |
| uchar_t **optbufp, uint_t *optlenp); |
| static void icmp_rput(queue_t *q, mblk_t *mp); |
| static void icmp_rput_bind_ack(queue_t *q, mblk_t *mp); |
| static int icmp_snmp_get(queue_t *q, mblk_t *mpctl); |
| static int icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name, |
| uchar_t *ptr, int len); |
| static int icmp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, |
| cred_t *cr); |
| static void icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err); |
| static void icmp_unbind(queue_t *q, mblk_t *mp); |
| static void icmp_wput(queue_t *q, mblk_t *mp); |
| static void icmp_wput_ipv6(queue_t *q, mblk_t *mp, sin6_t *sin6, |
| t_scalar_t tudr_optlen); |
| static void icmp_wput_other(queue_t *q, mblk_t *mp); |
| static void icmp_wput_iocdata(queue_t *q, mblk_t *mp); |
| static void icmp_wput_restricted(queue_t *q, mblk_t *mp); |
| |
| static void rawip_kstat_init(void); |
| static void rawip_kstat_fini(void); |
| static int rawip_kstat_update(kstat_t *kp, int rw); |
| |
| |
| static struct module_info info = { |
| 5707, "icmp", 1, INFPSZ, 512, 128 |
| }; |
| |
| static struct qinit rinit = { |
| (pfi_t)icmp_rput, NULL, icmp_open, icmp_close, NULL, &info |
| }; |
| |
| static struct qinit winit = { |
| (pfi_t)icmp_wput, NULL, NULL, NULL, NULL, &info |
| }; |
| |
| struct streamtab icmpinfo = { |
| &rinit, &winit |
| }; |
| |
| static sin_t sin_null; /* Zero address for quick clears */ |
| static sin6_t sin6_null; /* Zero address for quick clears */ |
| static void *icmp_g_head; /* Head for list of open icmp streams. */ |
| static IDP icmp_g_nd; /* Points to table of ICMP ND variables. */ |
| |
| /* MIB-2 stuff for SNMP */ |
| static mib2_rawip_t rawip_mib; /* SNMP fixed size info */ |
| static kstat_t *rawip_mibkp; /* kstat exporting rawip_mib data */ |
| |
| /* Default structure copied into T_INFO_ACK messages */ |
| static struct T_info_ack icmp_g_t_info_ack = { |
| T_INFO_ACK, |
| IP_MAXPACKET, /* TSDU_size. icmp allows maximum size messages. */ |
| T_INVALID, /* ETSDU_size. icmp does not support expedited data. */ |
| T_INVALID, /* CDATA_size. icmp does not support connect data. */ |
| T_INVALID, /* DDATA_size. icmp does not support disconnect data. */ |
| 0, /* ADDR_size - filled in later. */ |
| 0, /* OPT_size - not initialized here */ |
| IP_MAXPACKET, /* TIDU_size. icmp allows maximum size messages. */ |
| T_CLTS, /* SERV_type. icmp supports connection-less. */ |
| TS_UNBND, /* CURRENT_state. This is set from icmp_state. */ |
| (XPG4_1|SENDZERO) /* PROVIDER_flag */ |
| }; |
| |
| /* |
| * Table of ND variables supported by icmp. These are loaded into icmp_g_nd |
| * in icmp_open. |
| * All of these are alterable, within the min/max values given, at run time. |
| */ |
| static icmpparam_t icmp_param_arr[] = { |
| /* min max value name */ |
| { 0, 128, 32, "icmp_wroff_extra" }, |
| { 1, 255, 255, "icmp_ipv4_ttl" }, |
| { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "icmp_ipv6_hoplimit"}, |
| { 0, 1, 1, "icmp_bsd_compat" }, |
| { 4096, 65536, 8192, "icmp_xmit_hiwat"}, |
| { 0, 65536, 1024, "icmp_xmit_lowat"}, |
| { 4096, 65536, 8192, "icmp_recv_hiwat"}, |
| { 65536, 1024*1024*1024, 256*1024, "icmp_max_buf"}, |
| }; |
| #define icmp_wroff_extra icmp_param_arr[0].icmp_param_value |
| #define icmp_ipv4_ttl icmp_param_arr[1].icmp_param_value |
| #define icmp_ipv6_hoplimit icmp_param_arr[2].icmp_param_value |
| #define icmp_bsd_compat icmp_param_arr[3].icmp_param_value |
| #define icmp_xmit_hiwat icmp_param_arr[4].icmp_param_value |
| #define icmp_xmit_lowat icmp_param_arr[5].icmp_param_value |
| #define icmp_recv_hiwat icmp_param_arr[6].icmp_param_value |
| #define icmp_max_buf icmp_param_arr[7].icmp_param_value |
| |
| /* |
| * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message |
| * passed to icmp_wput. |
| * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the ICMP |
| * protocol type placed in the message following the address. A T_BIND_ACK |
| * message is passed upstream when ip acknowledges the request. |
| * (Called as writer.) |
| */ |
| static void |
| icmp_bind(queue_t *q, mblk_t *mp) |
| { |
| sin_t *sin; |
| sin6_t *sin6; |
| mblk_t *mp1; |
| struct T_bind_req *tbr; |
| icmp_t *icmp; |
| |
| icmp = (icmp_t *)q->q_ptr; |
| if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { |
| (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, |
| "icmp_bind: bad req, len %u", |
| (uint_t)(mp->b_wptr - mp->b_rptr)); |
| icmp_err_ack(q, mp, TPROTO, 0); |
| return; |
| } |
| if (icmp->icmp_state != TS_UNBND) { |
| (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, |
| "icmp_bind: bad state, %d", icmp->icmp_state); |
| icmp_err_ack(q, mp, TOUTSTATE, 0); |
| return; |
| } |
| /* |
| * Reallocate the message to make sure we have enough room for an |
| * address and the protocol type. |
| */ |
| mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); |
| if (!mp1) { |
| icmp_err_ack(q, mp, TSYSERR, ENOMEM); |
| return; |
| } |
| mp = mp1; |
| tbr = (struct T_bind_req *)mp->b_rptr; |
| switch (tbr->ADDR_length) { |
| case 0: /* Generic request */ |
| tbr->ADDR_offset = sizeof (struct T_bind_req); |
| if (icmp->icmp_family == AF_INET) { |
| tbr->ADDR_length = sizeof (sin_t); |
| sin = (sin_t *)&tbr[1]; |
| *sin = sin_null; |
| sin->sin_family = AF_INET; |
| mp->b_wptr = (uchar_t *)&sin[1]; |
| } else { |
| ASSERT(icmp->icmp_family == AF_INET6); |
| tbr->ADDR_length = sizeof (sin6_t); |
| sin6 = (sin6_t *)&tbr[1]; |
| *sin6 = sin6_null; |
| sin6->sin6_family = AF_INET6; |
| mp->b_wptr = (uchar_t *)&sin6[1]; |
| } |
| break; |
| case sizeof (sin_t): /* Complete IP address */ |
| sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, |
| sizeof (sin_t)); |
| if (sin == NULL || !OK_32PTR((char *)sin)) { |
| icmp_err_ack(q, mp, TSYSERR, EINVAL); |
| return; |
| } |
| if (icmp->icmp_family != AF_INET || |
| sin->sin_family != AF_INET) { |
| icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); |
| return; |
| } |
| break; |
| case sizeof (sin6_t): /* Complete IP address */ |
| sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset, |
| sizeof (sin6_t)); |
| if (sin6 == NULL || !OK_32PTR((char *)sin6)) { |
| icmp_err_ack(q, mp, TSYSERR, EINVAL); |
| return; |
| } |
| if (icmp->icmp_family != AF_INET6 || |
| sin6->sin6_family != AF_INET6) { |
| icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); |
| return; |
| } |
| /* No support for mapped addresses on raw sockets */ |
| if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL); |
| return; |
| } |
| break; |
| default: |
| (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, |
| "icmp_bind: bad ADDR_length %d", tbr->ADDR_length); |
| icmp_err_ack(q, mp, TBADADDR, 0); |
| return; |
| } |
| /* |
| * Copy the source address into our icmp structure. This address |
| * may still be zero; if so, ip will fill in the correct address |
| * each time an outbound packet is passed to it. |
| * If we are binding to a broadcast or multicast address icmp_rput |
| * will clear the source address when it receives the T_BIND_ACK. |
| */ |
| icmp->icmp_state = TS_IDLE; |
| |
| if (icmp->icmp_family == AF_INET) { |
| ASSERT(sin != NULL); |
| ASSERT(icmp->icmp_ipversion == IPV4_VERSION); |
| IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, |
| &icmp->icmp_v6src); |
| icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + |
| icmp->icmp_ip_snd_options_len; |
| icmp->icmp_bound_v6src = icmp->icmp_v6src; |
| } else { |
| int error; |
| |
| ASSERT(sin6 != NULL); |
| ASSERT(icmp->icmp_ipversion == IPV6_VERSION); |
| icmp->icmp_v6src = sin6->sin6_addr; |
| icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len; |
| icmp->icmp_bound_v6src = icmp->icmp_v6src; |
| |
| /* Rebuild the header template */ |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) { |
| icmp_err_ack(q, mp, TSYSERR, error); |
| return; |
| } |
| } |
| /* |
| * Place protocol type in the O_T_BIND_REQ/T_BIND_REQ following |
| * the address. |
| */ |
| *mp->b_wptr++ = icmp->icmp_proto; |
| if (!(V6_OR_V4_INADDR_ANY(icmp->icmp_v6src))) { |
| /* |
| * Append a request for an IRE if src not 0 (INADDR_ANY) |
| */ |
| mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); |
| if (!mp->b_cont) { |
| icmp_err_ack(q, mp, TSYSERR, ENOMEM); |
| return; |
| } |
| mp->b_cont->b_wptr += sizeof (ire_t); |
| mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; |
| } |
| |
| /* Pass the O_T_BIND_REQ/T_BIND_REQ to ip. */ |
| putnext(q, mp); |
| } |
| |
| /* |
| * Send message to IP to just bind to the protocol. |
| */ |
| static void |
| icmp_bind_proto(queue_t *q) |
| { |
| mblk_t *mp; |
| struct T_bind_req *tbr; |
| icmp_t *icmp; |
| |
| icmp = (icmp_t *)q->q_ptr; |
| mp = allocb(sizeof (struct T_bind_req) + sizeof (sin6_t) + 1, |
| BPRI_MED); |
| if (!mp) { |
| return; |
| } |
| mp->b_datap->db_type = M_PROTO; |
| tbr = (struct T_bind_req *)mp->b_rptr; |
| tbr->PRIM_type = O_T_BIND_REQ; /* change to T_BIND_REQ ? */ |
| tbr->ADDR_offset = sizeof (struct T_bind_req); |
| if (icmp->icmp_ipversion == IPV4_VERSION) { |
| sin_t *sin; |
| |
| tbr->ADDR_length = sizeof (sin_t); |
| sin = (sin_t *)&tbr[1]; |
| *sin = sin_null; |
| sin->sin_family = AF_INET; |
| mp->b_wptr = (uchar_t *)&sin[1]; |
| } else { |
| sin6_t *sin6; |
| |
| ASSERT(icmp->icmp_ipversion == IPV6_VERSION); |
| tbr->ADDR_length = sizeof (sin6_t); |
| sin6 = (sin6_t *)&tbr[1]; |
| *sin6 = sin6_null; |
| sin6->sin6_family = AF_INET6; |
| mp->b_wptr = (uchar_t *)&sin6[1]; |
| } |
| |
| /* Place protocol type in the O_T_BIND_REQ following the address. */ |
| *mp->b_wptr++ = icmp->icmp_proto; |
| |
| /* Pass the O_T_BIND_REQ to ip. */ |
| putnext(q, mp); |
| } |
| |
| /* |
| * This routine handles each T_CONN_REQ message passed to icmp. It |
| * associates a default destination address with the stream. |
| * |
| * This routine sends down a T_BIND_REQ to IP with the following mblks: |
| * T_BIND_REQ - specifying local and remote address. |
| * IRE_DB_REQ_TYPE - to get an IRE back containing ire_type and src |
| * T_OK_ACK - for the T_CONN_REQ |
| * T_CONN_CON - to keep the TPI user happy |
| * |
| * The connect completes in icmp_rput. |
| * When a T_BIND_ACK is received information is extracted from the IRE |
| * and the two appended messages are sent to the TPI user. |
| * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert |
| * it to an error ack for the appropriate primitive. |
| */ |
| static void |
| icmp_connect(queue_t *q, mblk_t *mp) |
| { |
| sin_t *sin; |
| sin6_t *sin6; |
| mblk_t *mp1, *mp2; |
| struct T_conn_req *tcr; |
| icmp_t *icmp; |
| ipaddr_t v4dst; |
| in6_addr_t v6dst; |
| uint32_t flowinfo; |
| |
| icmp = (icmp_t *)q->q_ptr; |
| tcr = (struct T_conn_req *)mp->b_rptr; |
| /* Sanity checks */ |
| if ((mp->b_wptr - mp->b_rptr < sizeof (struct T_conn_req))) { |
| icmp_err_ack(q, mp, TPROTO, 0); |
| return; |
| } |
| |
| if (icmp->icmp_state == TS_DATA_XFER) { |
| /* Already connected - clear out state */ |
| icmp->icmp_v6src = icmp->icmp_bound_v6src; |
| icmp->icmp_state = TS_IDLE; |
| } |
| |
| |
| if (tcr->OPT_length != 0) { |
| icmp_err_ack(q, mp, TBADOPT, 0); |
| return; |
| } |
| switch (tcr->DEST_length) { |
| default: |
| icmp_err_ack(q, mp, TBADADDR, 0); |
| return; |
| |
| case sizeof (sin_t): |
| sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, |
| sizeof (sin_t)); |
| if (sin == NULL || !OK_32PTR((char *)sin)) { |
| icmp_err_ack(q, mp, TSYSERR, EINVAL); |
| return; |
| } |
| if (icmp->icmp_family != AF_INET || |
| sin->sin_family != AF_INET) { |
| icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); |
| return; |
| } |
| v4dst = sin->sin_addr.s_addr; |
| IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); |
| ASSERT(icmp->icmp_ipversion == IPV4_VERSION); |
| icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + |
| icmp->icmp_ip_snd_options_len; |
| break; |
| |
| case sizeof (sin6_t): |
| sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, |
| sizeof (sin6_t)); |
| if (sin6 == NULL || !OK_32PTR((char *)sin6)) { |
| icmp_err_ack(q, mp, TSYSERR, EINVAL); |
| return; |
| } |
| if (icmp->icmp_family != AF_INET6 || |
| sin6->sin6_family != AF_INET6) { |
| icmp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); |
| return; |
| } |
| /* No support for mapped addresses on raw sockets */ |
| if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| icmp_err_ack(q, mp, TSYSERR, EADDRNOTAVAIL); |
| return; |
| } |
| v6dst = sin6->sin6_addr; |
| ASSERT(icmp->icmp_ipversion == IPV6_VERSION); |
| icmp->icmp_max_hdr_len = icmp->icmp_sticky_hdrs_len; |
| flowinfo = sin6->sin6_flowinfo; |
| break; |
| } |
| if (icmp->icmp_ipversion == IPV4_VERSION) { |
| /* |
| * Interpret a zero destination to mean loopback. |
| * Update the T_CONN_REQ (sin/sin6) since it is used to |
| * generate the T_CONN_CON. |
| */ |
| if (v4dst == INADDR_ANY) { |
| v4dst = htonl(INADDR_LOOPBACK); |
| IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); |
| if (icmp->icmp_family == AF_INET) { |
| sin->sin_addr.s_addr = v4dst; |
| } else { |
| sin6->sin6_addr = v6dst; |
| } |
| } |
| icmp->icmp_v6dst = v6dst; |
| icmp->icmp_flowinfo = 0; |
| |
| /* |
| * If the destination address is multicast and |
| * an outgoing multicast interface has been set, |
| * use the address of that interface as our |
| * source address if no source address has been set. |
| */ |
| if (V4_PART_OF_V6(icmp->icmp_v6src) == INADDR_ANY && |
| CLASSD(v4dst) && |
| icmp->icmp_multicast_if_addr != INADDR_ANY) { |
| IN6_IPADDR_TO_V4MAPPED(icmp->icmp_multicast_if_addr, |
| &icmp->icmp_v6src); |
| } |
| } else { |
| ASSERT(icmp->icmp_ipversion == IPV6_VERSION); |
| /* |
| * Interpret a zero destination to mean loopback. |
| * Update the T_CONN_REQ (sin/sin6) since it is used to |
| * generate the T_CONN_CON. |
| */ |
| if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) { |
| v6dst = ipv6_loopback; |
| sin6->sin6_addr = v6dst; |
| } |
| icmp->icmp_v6dst = v6dst; |
| icmp->icmp_flowinfo = flowinfo; |
| /* |
| * If the destination address is multicast and |
| * an outgoing multicast interface has been set, |
| * then the ip bind logic will pick the correct source |
| * address (i.e. matching the outgoing multicast interface). |
| */ |
| } |
| |
| /* |
| * Send down bind to IP to verify that there is a route |
| * and to determine the source address. |
| * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput. |
| */ |
| if (icmp->icmp_family == AF_INET) { |
| mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa_conn_t), |
| sin->sin_port); |
| } else { |
| ASSERT(icmp->icmp_family == AF_INET6); |
| mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (ipa6_conn_t), |
| sin6->sin6_port); |
| } |
| if (mp1 == NULL) { |
| icmp_err_ack(q, mp, TSYSERR, ENOMEM); |
| return; |
| } |
| |
| /* |
| * We also have to send a connection confirmation to |
| * keep TLI happy. Prepare it for icmp_rput. |
| */ |
| if (icmp->icmp_family == AF_INET) { |
| mp2 = mi_tpi_conn_con(NULL, (char *)sin, sizeof (*sin), NULL, |
| 0); |
| } else { |
| ASSERT(icmp->icmp_family == AF_INET6); |
| mp2 = mi_tpi_conn_con(NULL, (char *)sin6, sizeof (*sin6), NULL, |
| 0); |
| } |
| if (mp2 == NULL) { |
| freemsg(mp1); |
| icmp_err_ack(q, mp, TSYSERR, ENOMEM); |
| return; |
| } |
| |
| mp = mi_tpi_ok_ack_alloc(mp); |
| if (mp == NULL) { |
| /* Unable to reuse the T_CONN_REQ for the ack. */ |
| freemsg(mp2); |
| icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM); |
| return; |
| } |
| |
| icmp->icmp_state = TS_DATA_XFER; |
| |
| /* Hang onto the T_OK_ACK and T_CONN_CON for later. */ |
| linkb(mp1, mp); |
| linkb(mp1, mp2); |
| |
| putnext(q, mp1); |
| } |
| |
| static int |
| icmp_close(queue_t *q) |
| { |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| int i1; |
| |
| qprocsoff(q); |
| |
| /* If there are any options associated with the stream, free them. */ |
| if (icmp->icmp_ip_snd_options) |
| mi_free((char *)icmp->icmp_ip_snd_options); |
| |
| if (icmp->icmp_filter != NULL) |
| kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t)); |
| |
| /* Free memory associated with sticky options */ |
| if (icmp->icmp_sticky_hdrs_len != 0) { |
| kmem_free(icmp->icmp_sticky_hdrs, |
| icmp->icmp_sticky_hdrs_len); |
| icmp->icmp_sticky_hdrs = NULL; |
| icmp->icmp_sticky_hdrs_len = 0; |
| } |
| if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) { |
| kmem_free(icmp->icmp_sticky_ipp.ipp_hopopts, |
| icmp->icmp_sticky_ipp.ipp_hopoptslen); |
| } |
| if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) { |
| kmem_free(icmp->icmp_sticky_ipp.ipp_rtdstopts, |
| icmp->icmp_sticky_ipp.ipp_rtdstoptslen); |
| } |
| if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_RTHDR) { |
| kmem_free(icmp->icmp_sticky_ipp.ipp_rthdr, |
| icmp->icmp_sticky_ipp.ipp_rthdrlen); |
| } |
| if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) { |
| kmem_free(icmp->icmp_sticky_ipp.ipp_dstopts, |
| icmp->icmp_sticky_ipp.ipp_dstoptslen); |
| } |
| if (icmp->icmp_sticky_ipp.ipp_fields & IPPF_PATHMTU) { |
| kmem_free(icmp->icmp_sticky_ipp.ipp_pathmtu, |
| icmp->icmp_sticky_ipp.ipp_pathmtulen); |
| } |
| icmp->icmp_sticky_ipp.ipp_fields &= |
| ~(IPPF_HOPOPTS|IPPF_RTDSTOPTS|IPPF_RTHDR|IPPF_DSTOPTS); |
| |
| crfree(icmp->icmp_credp); |
| |
| /* Free the icmp structure and release the minor device number. */ |
| i1 = mi_close_comm(&icmp_g_head, q); |
| |
| return (i1); |
| } |
| |
| /* |
| * This routine handles each T_DISCON_REQ message passed to icmp |
| * as an indicating that ICMP is no longer connected. This results |
| * in sending a T_BIND_REQ to IP to restore the binding to just |
| * the local address. |
| * |
| * This routine sends down a T_BIND_REQ to IP with the following mblks: |
| * T_BIND_REQ - specifying just the local address. |
| * T_OK_ACK - for the T_DISCON_REQ |
| * |
| * The disconnect completes in icmp_rput. |
| * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user. |
| * Should icmp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert |
| * it to an error ack for the appropriate primitive. |
| */ |
| static void |
| icmp_disconnect(queue_t *q, mblk_t *mp) |
| { |
| icmp_t *icmp; |
| mblk_t *mp1; |
| |
| icmp = (icmp_t *)q->q_ptr; |
| |
| if (icmp->icmp_state != TS_DATA_XFER) { |
| (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, |
| "icmp_disconnect: bad state, %d", icmp->icmp_state); |
| icmp_err_ack(q, mp, TOUTSTATE, 0); |
| return; |
| } |
| icmp->icmp_v6src = icmp->icmp_bound_v6src; |
| icmp->icmp_state = TS_IDLE; |
| |
| /* |
| * Send down bind to IP to remove the full binding and revert |
| * to the local address binding. |
| */ |
| if (icmp->icmp_family == AF_INET) { |
| mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin_t), 0); |
| } else { |
| ASSERT(icmp->icmp_family == AF_INET6); |
| mp1 = icmp_ip_bind_mp(icmp, O_T_BIND_REQ, sizeof (sin6_t), 0); |
| } |
| if (mp1 == NULL) { |
| icmp_err_ack(q, mp, TSYSERR, ENOMEM); |
| return; |
| } |
| mp = mi_tpi_ok_ack_alloc(mp); |
| if (mp == NULL) { |
| /* Unable to reuse the T_DISCON_REQ for the ack. */ |
| icmp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM); |
| return; |
| } |
| |
| if (icmp->icmp_family == AF_INET6) { |
| int error; |
| |
| /* Rebuild the header template */ |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) { |
| icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error); |
| freemsg(mp1); |
| return; |
| } |
| } |
| icmp->icmp_discon_pending = 1; |
| |
| /* Append the T_OK_ACK to the T_BIND_REQ for icmp_rput */ |
| linkb(mp1, mp); |
| putnext(q, mp1); |
| } |
| |
| /* This routine creates a T_ERROR_ACK message and passes it upstream. */ |
| static void |
| icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error) |
| { |
| if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) |
| qreply(q, mp); |
| } |
| |
| /* Shorthand to generate and send TPI error acks to our client */ |
| static void |
| icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive, |
| t_scalar_t t_error, int sys_error) |
| { |
| struct T_error_ack *teackp; |
| |
| if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), |
| M_PCPROTO, T_ERROR_ACK)) != NULL) { |
| teackp = (struct T_error_ack *)mp->b_rptr; |
| teackp->ERROR_prim = primitive; |
| teackp->TLI_error = t_error; |
| teackp->UNIX_error = sys_error; |
| qreply(q, mp); |
| } |
| } |
| |
| /* |
| * icmp_icmp_error is called by icmp_rput to process ICMP |
| * messages passed up by IP. |
| * Generates the appropriate T_UDERROR_IND for permanent |
| * (non-transient) errors. |
| * Assumes that IP has pulled up everything up to and including |
| * the ICMP header. |
| */ |
| static void |
| icmp_icmp_error(queue_t *q, mblk_t *mp) |
| { |
| icmph_t *icmph; |
| ipha_t *ipha; |
| int iph_hdr_length; |
| sin_t sin; |
| sin6_t sin6; |
| mblk_t *mp1; |
| int error = 0; |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| |
| /* |
| * Deliver T_UDERROR_IND when the application has asked for it. |
| * The socket layer enables this automatically when connected. |
| */ |
| if (!icmp->icmp_dgram_errind) { |
| freemsg(mp); |
| return; |
| } |
| |
| ipha = (ipha_t *)mp->b_rptr; |
| |
| if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) { |
| ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION); |
| icmp_icmp_error_ipv6(q, mp); |
| return; |
| } |
| ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); |
| |
| iph_hdr_length = IPH_HDR_LENGTH(ipha); |
| icmph = (icmph_t *)(&mp->b_rptr[iph_hdr_length]); |
| ipha = (ipha_t *)&icmph[1]; |
| iph_hdr_length = IPH_HDR_LENGTH(ipha); |
| |
| switch (icmph->icmph_type) { |
| case ICMP_DEST_UNREACHABLE: |
| switch (icmph->icmph_code) { |
| case ICMP_FRAGMENTATION_NEEDED: |
| /* |
| * IP has already adjusted the path MTU. |
| * XXX Somehow pass MTU indication to application? |
| */ |
| break; |
| case ICMP_PORT_UNREACHABLE: |
| case ICMP_PROTOCOL_UNREACHABLE: |
| error = ECONNREFUSED; |
| break; |
| default: |
| /* Transient errors */ |
| break; |
| } |
| break; |
| default: |
| /* Transient errors */ |
| break; |
| } |
| if (error == 0) { |
| freemsg(mp); |
| return; |
| } |
| |
| switch (icmp->icmp_family) { |
| case AF_INET: |
| sin = sin_null; |
| sin.sin_family = AF_INET; |
| sin.sin_addr.s_addr = ipha->ipha_dst; |
| mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0, |
| error); |
| break; |
| case AF_INET6: |
| sin6 = sin6_null; |
| sin6.sin6_family = AF_INET6; |
| IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr); |
| |
| mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), |
| NULL, 0, error); |
| break; |
| } |
| if (mp1) |
| putnext(q, mp1); |
| freemsg(mp); |
| } |
| |
| /* |
| * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMPv6 |
| * for IPv6 packets. |
| * Send permanent (non-transient) errors upstream. |
| * Assumes that IP has pulled up all the extension headers as well |
| * as the ICMPv6 header. |
| */ |
| static void |
| icmp_icmp_error_ipv6(queue_t *q, mblk_t *mp) |
| { |
| icmp6_t *icmp6; |
| ip6_t *ip6h, *outer_ip6h; |
| uint16_t iph_hdr_length; |
| uint8_t *nexthdrp; |
| sin6_t sin6; |
| mblk_t *mp1; |
| int error = 0; |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| |
| outer_ip6h = (ip6_t *)mp->b_rptr; |
| if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6) |
| iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h); |
| else |
| iph_hdr_length = IPV6_HDR_LEN; |
| |
| icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; |
| ip6h = (ip6_t *)&icmp6[1]; |
| if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) { |
| freemsg(mp); |
| return; |
| } |
| if (*nexthdrp != icmp->icmp_proto) { |
| /* |
| * Could have switched icmp_proto after while ip did fanout of |
| * this message |
| */ |
| freemsg(mp); |
| return; |
| } |
| switch (icmp6->icmp6_type) { |
| case ICMP6_DST_UNREACH: |
| switch (icmp6->icmp6_code) { |
| case ICMP6_DST_UNREACH_NOPORT: |
| error = ECONNREFUSED; |
| break; |
| case ICMP6_DST_UNREACH_ADMIN: |
| case ICMP6_DST_UNREACH_NOROUTE: |
| case ICMP6_DST_UNREACH_BEYONDSCOPE: |
| case ICMP6_DST_UNREACH_ADDR: |
| /* Transient errors */ |
| break; |
| default: |
| break; |
| } |
| break; |
| case ICMP6_PACKET_TOO_BIG: { |
| struct T_unitdata_ind *tudi; |
| struct T_opthdr *toh; |
| size_t udi_size; |
| mblk_t *newmp; |
| t_scalar_t opt_length = sizeof (struct T_opthdr) + |
| sizeof (struct ip6_mtuinfo); |
| sin6_t *sin6; |
| struct ip6_mtuinfo *mtuinfo; |
| |
| /* |
| * If the application has requested to receive path mtu |
| * information, send up an empty message containing an |
| * IPV6_PATHMTU ancillary data item. |
| */ |
| if (!icmp->icmp_ipv6_recvpathmtu) |
| break; |
| |
| udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) + |
| opt_length; |
| if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) { |
| BUMP_MIB(&rawip_mib, rawipInErrors); |
| break; |
| } |
| |
| /* |
| * newmp->b_cont is left to NULL on purpose. This is an |
| * empty message containing only ancillary data. |
| */ |
| newmp->b_datap->db_type = M_PROTO; |
| tudi = (struct T_unitdata_ind *)newmp->b_rptr; |
| newmp->b_wptr = (uchar_t *)tudi + udi_size; |
| tudi->PRIM_type = T_UNITDATA_IND; |
| tudi->SRC_length = sizeof (sin6_t); |
| tudi->SRC_offset = sizeof (struct T_unitdata_ind); |
| tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t); |
| tudi->OPT_length = opt_length; |
| |
| sin6 = (sin6_t *)&tudi[1]; |
| bzero(sin6, sizeof (sin6_t)); |
| sin6->sin6_family = AF_INET6; |
| sin6->sin6_addr = icmp->icmp_v6dst; |
| |
| toh = (struct T_opthdr *)&sin6[1]; |
| toh->level = IPPROTO_IPV6; |
| toh->name = IPV6_PATHMTU; |
| toh->len = opt_length; |
| toh->status = 0; |
| |
| mtuinfo = (struct ip6_mtuinfo *)&toh[1]; |
| bzero(mtuinfo, sizeof (struct ip6_mtuinfo)); |
| mtuinfo->ip6m_addr.sin6_family = AF_INET6; |
| mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst; |
| mtuinfo->ip6m_mtu = icmp6->icmp6_mtu; |
| /* |
| * We've consumed everything we need from the original |
| * message. Free it, then send our empty message. |
| */ |
| freemsg(mp); |
| putnext(q, newmp); |
| return; |
| } |
| case ICMP6_TIME_EXCEEDED: |
| /* Transient errors */ |
| break; |
| case ICMP6_PARAM_PROB: |
| /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ |
| if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && |
| (uchar_t *)ip6h + icmp6->icmp6_pptr == |
| (uchar_t *)nexthdrp) { |
| error = ECONNREFUSED; |
| break; |
| } |
| break; |
| } |
| if (error == 0) { |
| freemsg(mp); |
| return; |
| } |
| |
| sin6 = sin6_null; |
| sin6.sin6_family = AF_INET6; |
| sin6.sin6_addr = ip6h->ip6_dst; |
| sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; |
| |
| mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0, |
| error); |
| if (mp1) |
| putnext(q, mp1); |
| freemsg(mp); |
| } |
| |
| /* |
| * This routine responds to T_ADDR_REQ messages. It is called by icmp_wput. |
| * The local address is filled in if endpoint is bound. The remote address |
| * is filled in if remote address has been precified ("connected endpoint") |
| * (The concept of connected CLTS sockets is alien to published TPI |
| * but we support it anyway). |
| */ |
| static void |
| icmp_addr_req(queue_t *q, mblk_t *mp) |
| { |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| mblk_t *ackmp; |
| struct T_addr_ack *taa; |
| |
| /* Make it large enough for worst case */ |
| ackmp = reallocb(mp, sizeof (struct T_addr_ack) + |
| 2 * sizeof (sin6_t), 1); |
| if (ackmp == NULL) { |
| icmp_err_ack(q, mp, TSYSERR, ENOMEM); |
| return; |
| } |
| taa = (struct T_addr_ack *)ackmp->b_rptr; |
| |
| bzero(taa, sizeof (struct T_addr_ack)); |
| ackmp->b_wptr = (uchar_t *)&taa[1]; |
| |
| taa->PRIM_type = T_ADDR_ACK; |
| ackmp->b_datap->db_type = M_PCPROTO; |
| |
| /* |
| * Note: Following code assumes 32 bit alignment of basic |
| * data structures like sin_t and struct T_addr_ack. |
| */ |
| if (icmp->icmp_state != TS_UNBND) { |
| /* |
| * Fill in local address |
| */ |
| taa->LOCADDR_offset = sizeof (*taa); |
| if (icmp->icmp_family == AF_INET) { |
| sin_t *sin; |
| |
| taa->LOCADDR_length = sizeof (sin_t); |
| sin = (sin_t *)&taa[1]; |
| /* Fill zeroes and then intialize non-zero fields */ |
| *sin = sin_null; |
| sin->sin_family = AF_INET; |
| if (!IN6_IS_ADDR_V4MAPPED_ANY(&icmp->icmp_v6src) && |
| !IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) { |
| IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_v6src, |
| sin->sin_addr.s_addr); |
| } else { |
| /* |
| * INADDR_ANY |
| * icmp_v6src is not set, we might be bound to |
| * broadcast/multicast. Use icmp_bound_v6src as |
| * local address instead (that could |
| * also still be INADDR_ANY) |
| */ |
| IN6_V4MAPPED_TO_IPADDR(&icmp->icmp_bound_v6src, |
| sin->sin_addr.s_addr); |
| } |
| ackmp->b_wptr = (uchar_t *)&sin[1]; |
| } else { |
| sin6_t *sin6; |
| |
| ASSERT(icmp->icmp_family == AF_INET6); |
| taa->LOCADDR_length = sizeof (sin6_t); |
| sin6 = (sin6_t *)&taa[1]; |
| /* Fill zeroes and then intialize non-zero fields */ |
| *sin6 = sin6_null; |
| sin6->sin6_family = AF_INET6; |
| if (!IN6_IS_ADDR_UNSPECIFIED(&icmp->icmp_v6src)) { |
| sin6->sin6_addr = icmp->icmp_v6src; |
| } else { |
| /* |
| * UNSPECIFIED |
| * icmp_v6src is not set, we might be bound to |
| * broadcast/multicast. Use icmp_bound_v6src as |
| * local address instead (that could |
| * also still be UNSPECIFIED) |
| */ |
| sin6->sin6_addr = icmp->icmp_bound_v6src; |
| } |
| ackmp->b_wptr = (uchar_t *)&sin6[1]; |
| } |
| } |
| ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim); |
| qreply(q, ackmp); |
| } |
| |
| static void |
| icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp) |
| { |
| *tap = icmp_g_t_info_ack; |
| |
| if (icmp->icmp_family == AF_INET6) |
| tap->ADDR_size = sizeof (sin6_t); |
| else |
| tap->ADDR_size = sizeof (sin_t); |
| tap->CURRENT_state = icmp->icmp_state; |
| tap->OPT_size = icmp_max_optsize; |
| } |
| |
| /* |
| * This routine responds to T_CAPABILITY_REQ messages. It is called by |
| * icmp_wput. Much of the T_CAPABILITY_ACK information is copied from |
| * icmp_g_t_info_ack. The current state of the stream is copied from |
| * icmp_state. |
| */ |
| static void |
| icmp_capability_req(queue_t *q, mblk_t *mp) |
| { |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| t_uscalar_t cap_bits1; |
| struct T_capability_ack *tcap; |
| |
| cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; |
| |
| mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), |
| mp->b_datap->db_type, T_CAPABILITY_ACK); |
| if (!mp) |
| return; |
| |
| tcap = (struct T_capability_ack *)mp->b_rptr; |
| tcap->CAP_bits1 = 0; |
| |
| if (cap_bits1 & TC1_INFO) { |
| icmp_copy_info(&tcap->INFO_ack, icmp); |
| tcap->CAP_bits1 |= TC1_INFO; |
| } |
| |
| qreply(q, mp); |
| } |
| |
| /* |
| * This routine responds to T_INFO_REQ messages. It is called by icmp_wput. |
| * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack. |
| * The current state of the stream is copied from icmp_state. |
| */ |
| static void |
| icmp_info_req(queue_t *q, mblk_t *mp) |
| { |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| |
| mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, |
| T_INFO_ACK); |
| if (!mp) |
| return; |
| icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp); |
| qreply(q, mp); |
| } |
| |
| /* |
| * IP recognizes seven kinds of bind requests: |
| * |
| * - A zero-length address binds only to the protocol number. |
| * |
| * - A 4-byte address is treated as a request to |
| * validate that the address is a valid local IPv4 |
| * address, appropriate for an application to bind to. |
| * IP does the verification, but does not make any note |
| * of the address at this time. |
| * |
| * - A 16-byte address contains is treated as a request |
| * to validate a local IPv6 address, as the 4-byte |
| * address case above. |
| * |
| * - A 16-byte sockaddr_in to validate the local IPv4 address and also |
| * use it for the inbound fanout of packets. |
| * |
| * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also |
| * use it for the inbound fanout of packets. |
| * |
| * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout |
| * information consisting of local and remote addresses |
| * and ports (unused for raw sockets). In this case, the addresses are both |
| * validated as appropriate for this operation, and, if |
| * so, the information is retained for use in the |
| * inbound fanout. |
| * |
| * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 |
| * fanout information, like the 12-byte case above. |
| * |
| * IP will also fill in the IRE request mblk with information |
| * regarding our peer. In all cases, we notify IP of our protocol |
| * type by appending a single protocol byte to the bind request. |
| */ |
| static mblk_t * |
| icmp_ip_bind_mp(icmp_t *icmp, t_scalar_t bind_prim, t_scalar_t addr_length, |
| in_port_t fport) |
| { |
| char *cp; |
| mblk_t *mp; |
| struct T_bind_req *tbr; |
| ipa_conn_t *ac; |
| ipa6_conn_t *ac6; |
| sin_t *sin; |
| sin6_t *sin6; |
| |
| ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); |
| |
| mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); |
| if (mp == NULL) |
| return (NULL); |
| mp->b_datap->db_type = M_PROTO; |
| tbr = (struct T_bind_req *)mp->b_rptr; |
| tbr->PRIM_type = bind_prim; |
| tbr->ADDR_offset = sizeof (*tbr); |
| tbr->CONIND_number = 0; |
| tbr->ADDR_length = addr_length; |
| cp = (char *)&tbr[1]; |
| switch (addr_length) { |
| case sizeof (ipa_conn_t): |
| ASSERT(icmp->icmp_family == AF_INET); |
| /* Append a request for an IRE */ |
| mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); |
| if (mp->b_cont == NULL) { |
| freemsg(mp); |
| return (NULL); |
| } |
| mp->b_cont->b_wptr += sizeof (ire_t); |
| mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; |
| |
| /* cp known to be 32 bit aligned */ |
| ac = (ipa_conn_t *)cp; |
| ac->ac_laddr = V4_PART_OF_V6(icmp->icmp_v6src); |
| ac->ac_faddr = V4_PART_OF_V6(icmp->icmp_v6dst); |
| ac->ac_fport = fport; |
| ac->ac_lport = 0; |
| break; |
| |
| case sizeof (ipa6_conn_t): |
| ASSERT(icmp->icmp_family == AF_INET6); |
| /* Append a request for an IRE */ |
| mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); |
| if (mp->b_cont == NULL) { |
| freemsg(mp); |
| return (NULL); |
| } |
| mp->b_cont->b_wptr += sizeof (ire_t); |
| mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; |
| |
| /* cp known to be 32 bit aligned */ |
| ac6 = (ipa6_conn_t *)cp; |
| ac6->ac6_laddr = icmp->icmp_v6src; |
| ac6->ac6_faddr = icmp->icmp_v6dst; |
| ac6->ac6_fport = fport; |
| ac6->ac6_lport = 0; |
| break; |
| |
| case sizeof (sin_t): |
| ASSERT(icmp->icmp_family == AF_INET); |
| /* Append a request for an IRE */ |
| mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); |
| if (!mp->b_cont) { |
| freemsg(mp); |
| return (NULL); |
| } |
| mp->b_cont->b_wptr += sizeof (ire_t); |
| mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; |
| |
| sin = (sin_t *)cp; |
| *sin = sin_null; |
| sin->sin_family = AF_INET; |
| sin->sin_addr.s_addr = V4_PART_OF_V6(icmp->icmp_bound_v6src); |
| break; |
| |
| case sizeof (sin6_t): |
| ASSERT(icmp->icmp_family == AF_INET6); |
| /* Append a request for an IRE */ |
| mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); |
| if (!mp->b_cont) { |
| freemsg(mp); |
| return (NULL); |
| } |
| mp->b_cont->b_wptr += sizeof (ire_t); |
| mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; |
| |
| sin6 = (sin6_t *)cp; |
| *sin6 = sin6_null; |
| sin6->sin6_family = AF_INET6; |
| sin6->sin6_addr = icmp->icmp_bound_v6src; |
| break; |
| } |
| /* Add protocol number to end */ |
| cp[addr_length] = icmp->icmp_proto; |
| mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; |
| return (mp); |
| } |
| |
| /* |
| * This is the open routine for icmp. It allocates a icmp_t structure for |
| * the stream and, on the first open of the module, creates an ND table. |
| */ |
| static int |
| icmp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) |
| { |
| int err; |
| icmp_t *icmp; |
| |
| /* If the stream is already open, return immediately. */ |
| if (q->q_ptr != NULL) |
| return (0); |
| |
| /* If this is not a push of icmp as a module, fail. */ |
| if (sflag != MODOPEN) |
| return (EINVAL); |
| |
| /* |
| * Defer the qprocson until everything is initialized since |
| * we are D_MTPERQ and after qprocson the rput routine can |
| * run. (Could do qprocson earlier since icmp currently |
| * has an outer perimeter.) |
| */ |
| |
| /* |
| * Create a icmp_t structure for this stream and link into the |
| * list of open streams. |
| */ |
| err = mi_open_comm(&icmp_g_head, sizeof (icmp_t), q, devp, |
| flag, sflag, credp); |
| if (err) |
| return (err); |
| |
| /* |
| * The receive hiwat is only looked at on the stream head queue. |
| * Store in q_hiwat in order to return on SO_RCVBUF getsockopts. |
| */ |
| q->q_hiwat = icmp_recv_hiwat; |
| |
| /* Set the initial state of the stream and the privilege status. */ |
| icmp = (icmp_t *)q->q_ptr; |
| icmp->icmp_state = TS_UNBND; |
| icmp->icmp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; |
| icmp->icmp_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; |
| icmp->icmp_filter = NULL; |
| |
| icmp->icmp_credp = credp; |
| crhold(credp); |
| |
| icmp->icmp_zoneid = getzoneid(); |
| |
| if (getmajor(*devp) == (major_t)ICMP6_MAJ) { |
| icmp->icmp_ipversion = IPV6_VERSION; |
| icmp->icmp_family = AF_INET6; |
| /* May be changed by a SO_PROTOTYPE socket option. */ |
| icmp->icmp_proto = IPPROTO_ICMPV6; |
| icmp->icmp_checksum_off = 2; /* Offset for icmp6_cksum */ |
| icmp->icmp_max_hdr_len = IPV6_HDR_LEN; |
| icmp->icmp_ttl = (uint8_t)icmp_ipv6_hoplimit; |
| } else { |
| icmp->icmp_ipversion = IPV4_VERSION; |
| icmp->icmp_family = AF_INET; |
| /* May be changed by a SO_PROTOTYPE socket option. */ |
| icmp->icmp_proto = IPPROTO_ICMP; |
| icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH; |
| icmp->icmp_ttl = (uint8_t)icmp_ipv4_ttl; |
| } |
| qprocson(q); |
| |
| /* |
| * Check if icmp is being I_PUSHed by a non-privileged user. |
| * If so, we set icmp_restricted to indicate that only MIB |
| * traffic may pass. |
| */ |
| if (secpolicy_net_icmpaccess(credp) != 0) { |
| icmp->icmp_restricted = 1; |
| } |
| |
| /* |
| * The transmit hiwat is only looked at on IP's queue. |
| * Store in q_hiwat in order to return on SO_SNDBUF |
| * getsockopts. |
| */ |
| WR(q)->q_hiwat = icmp_xmit_hiwat; |
| WR(q)->q_next->q_hiwat = WR(q)->q_hiwat; |
| WR(q)->q_lowat = icmp_xmit_lowat; |
| WR(q)->q_next->q_lowat = WR(q)->q_lowat; |
| |
| if (icmp->icmp_family == AF_INET6) { |
| /* Build initial header template for transmit */ |
| int error; |
| |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) { |
| (void) icmp_close(q); |
| return (error); |
| } |
| } |
| /* Set the Stream head write offset. */ |
| (void) mi_set_sth_wroff(q, icmp->icmp_max_hdr_len + icmp_wroff_extra); |
| (void) mi_set_sth_hiwat(q, q->q_hiwat); |
| |
| return (0); |
| } |
| |
| /* |
| * Which ICMP options OK to set through T_UNITDATA_REQ... |
| */ |
| /* ARGSUSED */ |
| static boolean_t |
| icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name) |
| { |
| return (B_TRUE); |
| } |
| |
| /* |
| * This routine gets default values of certain options whose default |
| * values are maintained by protcol specific code |
| */ |
| /* ARGSUSED */ |
| int |
| icmp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) |
| { |
| int *i1 = (int *)ptr; |
| |
| switch (level) { |
| case IPPROTO_IP: |
| switch (name) { |
| case IP_MULTICAST_TTL: |
| *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; |
| return (sizeof (uchar_t)); |
| case IP_MULTICAST_LOOP: |
| *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; |
| return (sizeof (uchar_t)); |
| } |
| break; |
| case IPPROTO_IPV6: |
| switch (name) { |
| case IPV6_MULTICAST_HOPS: |
| *i1 = IP_DEFAULT_MULTICAST_TTL; |
| return (sizeof (int)); |
| case IPV6_MULTICAST_LOOP: |
| *i1 = IP_DEFAULT_MULTICAST_LOOP; |
| return (sizeof (int)); |
| case IPV6_UNICAST_HOPS: |
| *i1 = icmp_ipv6_hoplimit; |
| return (sizeof (int)); |
| } |
| break; |
| case IPPROTO_ICMPV6: |
| switch (name) { |
| case ICMP6_FILTER: |
| /* Make it look like "pass all" */ |
| ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr); |
| return (sizeof (icmp6_filter_t)); |
| } |
| break; |
| } |
| return (-1); |
| } |
| |
| /* |
| * This routine retrieves the current status of socket options. |
| * It returns the size of the option retrieved. |
| */ |
| int |
| icmp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) |
| { |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| int *i1 = (int *)ptr; |
| ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp; |
| |
| switch (level) { |
| case SOL_SOCKET: |
| switch (name) { |
| case SO_DEBUG: |
| *i1 = icmp->icmp_debug; |
| break; |
| case SO_TYPE: |
| *i1 = SOCK_RAW; |
| break; |
| case SO_PROTOTYPE: |
| *i1 = icmp->icmp_proto; |
| break; |
| case SO_REUSEADDR: |
| *i1 = icmp->icmp_reuseaddr; |
| break; |
| |
| /* |
| * The following three items are available here, |
| * but are only meaningful to IP. |
| */ |
| case SO_DONTROUTE: |
| *i1 = icmp->icmp_dontroute; |
| break; |
| case SO_USELOOPBACK: |
| *i1 = icmp->icmp_useloopback; |
| break; |
| case SO_BROADCAST: |
| *i1 = icmp->icmp_broadcast; |
| break; |
| |
| case SO_SNDBUF: |
| ASSERT(q->q_hiwat <= INT_MAX); |
| *i1 = (int)q->q_hiwat; |
| break; |
| case SO_RCVBUF: |
| ASSERT(RD(q)->q_hiwat <= INT_MAX); |
| *i1 = (int)RD(q)->q_hiwat; |
| break; |
| case SO_DGRAM_ERRIND: |
| *i1 = icmp->icmp_dgram_errind; |
| break; |
| /* |
| * Following three not meaningful for icmp |
| * Action is same as "default" to which we fallthrough |
| * so we keep them in comments. |
| * case SO_LINGER: |
| * case SO_KEEPALIVE: |
| * case SO_OOBINLINE: |
| */ |
| default: |
| return (-1); |
| } |
| break; |
| case IPPROTO_IP: |
| /* |
| * Only allow IPv4 option processing on IPv4 sockets. |
| */ |
| if (icmp->icmp_family != AF_INET) |
| return (-1); |
| |
| switch (name) { |
| case IP_OPTIONS: |
| case T_IP_OPTIONS: |
| /* Options are passed up with each packet */ |
| return (0); |
| case IP_HDRINCL: |
| *i1 = (int)icmp->icmp_hdrincl; |
| break; |
| case IP_TOS: |
| case T_IP_TOS: |
| *i1 = (int)icmp->icmp_type_of_service; |
| break; |
| case IP_TTL: |
| *i1 = (int)icmp->icmp_ttl; |
| break; |
| case IP_MULTICAST_IF: |
| /* 0 address if not set */ |
| *(ipaddr_t *)ptr = icmp->icmp_multicast_if_addr; |
| return (sizeof (ipaddr_t)); |
| case IP_MULTICAST_TTL: |
| *(uchar_t *)ptr = icmp->icmp_multicast_ttl; |
| return (sizeof (uchar_t)); |
| case IP_MULTICAST_LOOP: |
| *ptr = icmp->icmp_multicast_loop; |
| return (sizeof (uint8_t)); |
| case IP_BOUND_IF: |
| /* Zero if not set */ |
| *i1 = icmp->icmp_bound_if; |
| break; /* goto sizeof (int) option return */ |
| case IP_UNSPEC_SRC: |
| *ptr = icmp->icmp_unspec_source; |
| break; /* goto sizeof (int) option return */ |
| case IP_XMIT_IF: |
| *i1 = icmp->icmp_xmit_if; |
| break; /* goto sizeof (int) option return */ |
| case IP_RECVIF: |
| *ptr = icmp->icmp_recvif; |
| break; /* goto sizeof (int) option return */ |
| /* |
| * Cannot "get" the value of following options |
| * at this level. Action is same as "default" to |
| * which we fallthrough so we keep them in comments. |
| * |
| * case IP_ADD_MEMBERSHIP: |
| * case IP_DROP_MEMBERSHIP: |
| * case IP_BLOCK_SOURCE: |
| * case IP_UNBLOCK_SOURCE: |
| * case IP_ADD_SOURCE_MEMBERSHIP: |
| * case IP_DROP_SOURCE_MEMBERSHIP: |
| * case MCAST_JOIN_GROUP: |
| * case MCAST_LEAVE_GROUP: |
| * case MCAST_BLOCK_SOURCE: |
| * case MCAST_UNBLOCK_SOURCE: |
| * case MCAST_JOIN_SOURCE_GROUP: |
| * case MCAST_LEAVE_SOURCE_GROUP: |
| * case MRT_INIT: |
| * case MRT_DONE: |
| * case MRT_ADD_VIF: |
| * case MRT_DEL_VIF: |
| * case MRT_ADD_MFC: |
| * case MRT_DEL_MFC: |
| * case MRT_VERSION: |
| * case MRT_ASSERT: |
| * case IP_SEC_OPT: |
| * case IP_DONTFAILOVER_IF: |
| */ |
| default: |
| return (-1); |
| } |
| break; |
| case IPPROTO_IPV6: |
| /* |
| * Only allow IPv6 option processing on native IPv6 sockets. |
| */ |
| if (icmp->icmp_family != AF_INET6) |
| return (-1); |
| switch (name) { |
| case IPV6_UNICAST_HOPS: |
| *i1 = (unsigned int)icmp->icmp_ttl; |
| break; |
| case IPV6_MULTICAST_IF: |
| /* 0 index if not set */ |
| *i1 = icmp->icmp_multicast_if_index; |
| break; |
| case IPV6_MULTICAST_HOPS: |
| *i1 = icmp->icmp_multicast_ttl; |
| break; |
| case IPV6_MULTICAST_LOOP: |
| *i1 = icmp->icmp_multicast_loop; |
| break; |
| case IPV6_BOUND_IF: |
| /* Zero if not set */ |
| *i1 = icmp->icmp_bound_if; |
| break; |
| case IPV6_UNSPEC_SRC: |
| *i1 = icmp->icmp_unspec_source; |
| break; |
| case IPV6_CHECKSUM: |
| /* |
| * Return offset or -1 if no checksum offset. |
| * Does not apply to IPPROTO_ICMPV6 |
| */ |
| if (icmp->icmp_proto == IPPROTO_ICMPV6) |
| return (-1); |
| |
| if (icmp->icmp_raw_checksum) { |
| *i1 = icmp->icmp_checksum_off; |
| } else { |
| *i1 = -1; |
| } |
| break; |
| case IPV6_JOIN_GROUP: |
| case IPV6_LEAVE_GROUP: |
| case MCAST_JOIN_GROUP: |
| case MCAST_LEAVE_GROUP: |
| case MCAST_BLOCK_SOURCE: |
| case MCAST_UNBLOCK_SOURCE: |
| case MCAST_JOIN_SOURCE_GROUP: |
| case MCAST_LEAVE_SOURCE_GROUP: |
| /* cannot "get" the value for these */ |
| return (-1); |
| case IPV6_RECVPKTINFO: |
| *i1 = icmp->icmp_ipv6_recvpktinfo; |
| break; |
| case IPV6_RECVTCLASS: |
| *i1 = icmp->icmp_ipv6_recvtclass; |
| break; |
| case IPV6_RECVPATHMTU: |
| *i1 = icmp->icmp_ipv6_recvpathmtu; |
| break; |
| case IPV6_V6ONLY: |
| *i1 = 1; |
| break; |
| case IPV6_RECVHOPLIMIT: |
| *i1 = icmp->icmp_ipv6_recvhoplimit; |
| break; |
| case IPV6_RECVHOPOPTS: |
| *i1 = icmp->icmp_ipv6_recvhopopts; |
| break; |
| case IPV6_RECVDSTOPTS: |
| *i1 = icmp->icmp_ipv6_recvdstopts; |
| break; |
| case _OLD_IPV6_RECVDSTOPTS: |
| *i1 = icmp->icmp_old_ipv6_recvdstopts; |
| break; |
| case IPV6_RECVRTHDRDSTOPTS: |
| *i1 = icmp->icmp_ipv6_recvrtdstopts; |
| break; |
| case IPV6_RECVRTHDR: |
| *i1 = icmp->icmp_ipv6_recvrthdr; |
| break; |
| case IPV6_PKTINFO: { |
| /* XXX assumes that caller has room for max size! */ |
| struct in6_pktinfo *pkti; |
| |
| pkti = (struct in6_pktinfo *)ptr; |
| if (ipp->ipp_fields & IPPF_IFINDEX) |
| pkti->ipi6_ifindex = ipp->ipp_ifindex; |
| else |
| pkti->ipi6_ifindex = 0; |
| if (ipp->ipp_fields & IPPF_ADDR) |
| pkti->ipi6_addr = ipp->ipp_addr; |
| else |
| pkti->ipi6_addr = ipv6_all_zeros; |
| return (sizeof (struct in6_pktinfo)); |
| } |
| case IPV6_NEXTHOP: { |
| sin6_t *sin6 = (sin6_t *)ptr; |
| |
| if (!(ipp->ipp_fields & IPPF_NEXTHOP)) |
| return (0); |
| *sin6 = sin6_null; |
| sin6->sin6_family = AF_INET6; |
| sin6->sin6_addr = ipp->ipp_nexthop; |
| return (sizeof (sin6_t)); |
| } |
| case IPV6_HOPOPTS: |
| if (!(ipp->ipp_fields & IPPF_HOPOPTS)) |
| return (0); |
| bcopy(ipp->ipp_hopopts, ptr, ipp->ipp_hopoptslen); |
| return (ipp->ipp_hopoptslen); |
| case IPV6_RTHDRDSTOPTS: |
| if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) |
| return (0); |
| bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); |
| return (ipp->ipp_rtdstoptslen); |
| case IPV6_RTHDR: |
| if (!(ipp->ipp_fields & IPPF_RTHDR)) |
| return (0); |
| bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); |
| return (ipp->ipp_rthdrlen); |
| case IPV6_DSTOPTS: |
| if (!(ipp->ipp_fields & IPPF_DSTOPTS)) |
| return (0); |
| bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); |
| return (ipp->ipp_dstoptslen); |
| case IPV6_PATHMTU: |
| if (!(ipp->ipp_fields & IPPF_PATHMTU)) |
| return (0); |
| |
| return (ip_fill_mtuinfo(&icmp->icmp_v6dst, 0, |
| (struct ip6_mtuinfo *)ptr)); |
| case IPV6_TCLASS: |
| if (ipp->ipp_fields & IPPF_TCLASS) |
| *i1 = ipp->ipp_tclass; |
| else |
| *i1 = IPV6_FLOW_TCLASS( |
| IPV6_DEFAULT_VERS_AND_FLOW); |
| break; |
| default: |
| return (-1); |
| } |
| break; |
| case IPPROTO_ICMPV6: |
| /* |
| * Only allow IPv6 option processing on native IPv6 sockets. |
| */ |
| if (icmp->icmp_family != AF_INET6) |
| return (-1); |
| |
| if (icmp->icmp_proto != IPPROTO_ICMPV6) |
| return (-1); |
| |
| switch (name) { |
| case ICMP6_FILTER: |
| if (icmp->icmp_filter == NULL) { |
| /* Make it look like "pass all" */ |
| ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr); |
| } else { |
| (void) bcopy(icmp->icmp_filter, ptr, |
| sizeof (icmp6_filter_t)); |
| } |
| return (sizeof (icmp6_filter_t)); |
| default: |
| return (-1); |
| } |
| default: |
| return (-1); |
| } |
| return (sizeof (int)); |
| } |
| |
| /* This routine sets socket options. */ |
| /* ARGSUSED */ |
| int |
| icmp_opt_set(queue_t *q, uint_t optset_context, int level, int name, |
| uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, |
| void *thisdg_attrs, cred_t *cr, mblk_t *mblk) |
| { |
| icmp_t *icmp = (icmp_t *)q->q_ptr; |
| int *i1 = (int *)invalp; |
| boolean_t onoff = (*i1 == 0) ? 0 : 1; |
| boolean_t checkonly; |
| int error; |
| |
| switch (optset_context) { |
| case SETFN_OPTCOM_CHECKONLY: |
| checkonly = B_TRUE; |
| /* |
| * Note: Implies T_CHECK semantics for T_OPTCOM_REQ |
| * inlen != 0 implies value supplied and |
| * we have to "pretend" to set it. |
| * inlen == 0 implies that there is no |
| * value part in T_CHECK request and just validation |
| * done elsewhere should be enough, we just return here. |
| */ |
| if (inlen == 0) { |
| *outlenp = 0; |
| return (0); |
| } |
| break; |
| case SETFN_OPTCOM_NEGOTIATE: |
| checkonly = B_FALSE; |
| break; |
| case SETFN_UD_NEGOTIATE: |
| case SETFN_CONN_NEGOTIATE: |
| checkonly = B_FALSE; |
| /* |
| * Negotiating local and "association-related" options |
| * through T_UNITDATA_REQ. |
| * |
| * Following routine can filter out ones we do not |
| * want to be "set" this way. |
| */ |
| if (!icmp_opt_allow_udr_set(level, name)) { |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| break; |
| default: |
| /* |
| * We should never get here |
| */ |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| |
| ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || |
| (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); |
| |
| /* |
| * For fixed length options, no sanity check |
| * of passed in length is done. It is assumed *_optcom_req() |
| * routines do the right thing. |
| */ |
| |
| switch (level) { |
| case SOL_SOCKET: |
| switch (name) { |
| case SO_DEBUG: |
| if (!checkonly) |
| icmp->icmp_debug = onoff; |
| break; |
| case SO_PROTOTYPE: |
| if ((*i1 & 0xFF) != IPPROTO_ICMP && |
| (*i1 & 0xFF) != IPPROTO_ICMPV6 && |
| secpolicy_net_rawaccess(cr) != 0) { |
| *outlenp = 0; |
| return (EACCES); |
| } |
| /* Can't use IPPROTO_RAW with IPv6 */ |
| if ((*i1 & 0xFF) == IPPROTO_RAW && |
| icmp->icmp_family == AF_INET6) { |
| *outlenp = 0; |
| return (EPROTONOSUPPORT); |
| } |
| if (checkonly) { |
| /* T_CHECK case */ |
| *(int *)outvalp = (*i1 & 0xFF); |
| break; |
| } |
| icmp->icmp_proto = *i1 & 0xFF; |
| if ((icmp->icmp_proto == IPPROTO_RAW || |
| icmp->icmp_proto == IPPROTO_IGMP) && |
| icmp->icmp_family == AF_INET) |
| icmp->icmp_hdrincl = 1; |
| else |
| icmp->icmp_hdrincl = 0; |
| |
| if (icmp->icmp_family == AF_INET6 && |
| icmp->icmp_proto == IPPROTO_ICMPV6) { |
| /* Set offset for icmp6_cksum */ |
| icmp->icmp_raw_checksum = 0; |
| icmp->icmp_checksum_off = 2; |
| } |
| if (icmp->icmp_proto == IPPROTO_UDP || |
| icmp->icmp_proto == IPPROTO_TCP || |
| icmp->icmp_proto == IPPROTO_SCTP) { |
| icmp->icmp_no_tp_cksum = 1; |
| icmp->icmp_sticky_ipp.ipp_fields |= |
| IPPF_NO_CKSUM; |
| } else { |
| icmp->icmp_no_tp_cksum = 0; |
| icmp->icmp_sticky_ipp.ipp_fields &= |
| ~IPPF_NO_CKSUM; |
| } |
| |
| if (icmp->icmp_filter != NULL && |
| icmp->icmp_proto != IPPROTO_ICMPV6) { |
| kmem_free(icmp->icmp_filter, |
| sizeof (icmp6_filter_t)); |
| icmp->icmp_filter = NULL; |
| } |
| |
| /* Rebuild the header template */ |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) { |
| *outlenp = 0; |
| return (error); |
| } |
| |
| /* |
| * For SCTP, we don't use icmp_bind_proto() for |
| * raw socket binding. Note that we do not need |
| * to set *outlenp. |
| */ |
| if (icmp->icmp_proto == IPPROTO_SCTP) |
| return (0); |
| |
| icmp_bind_proto(q); |
| *outlenp = sizeof (int); |
| *(int *)outvalp = *i1 & 0xFF; |
| return (0); |
| case SO_REUSEADDR: |
| if (!checkonly) |
| icmp->icmp_reuseaddr = onoff; |
| break; |
| |
| /* |
| * The following three items are available here, |
| * but are only meaningful to IP. |
| */ |
| case SO_DONTROUTE: |
| if (!checkonly) |
| icmp->icmp_dontroute = onoff; |
| break; |
| case SO_USELOOPBACK: |
| if (!checkonly) |
| icmp->icmp_useloopback = onoff; |
| break; |
| case SO_BROADCAST: |
| if (!checkonly) |
| icmp->icmp_broadcast = onoff; |
| break; |
| |
| case SO_SNDBUF: |
| if (*i1 > icmp_max_buf) { |
| *outlenp = 0; |
| return (ENOBUFS); |
| } |
| if (!checkonly) { |
| q->q_hiwat = *i1; |
| q->q_next->q_hiwat = *i1; |
| } |
| break; |
| case SO_RCVBUF: |
| if (*i1 > icmp_max_buf) { |
| *outlenp = 0; |
| return (ENOBUFS); |
| } |
| if (!checkonly) { |
| RD(q)->q_hiwat = *i1; |
| (void) mi_set_sth_hiwat(RD(q), *i1); |
| } |
| break; |
| case SO_DGRAM_ERRIND: |
| if (!checkonly) |
| icmp->icmp_dgram_errind = onoff; |
| break; |
| /* |
| * Following three not meaningful for icmp |
| * Action is same as "default" so we keep them |
| * in comments. |
| * case SO_LINGER: |
| * case SO_KEEPALIVE: |
| * case SO_OOBINLINE: |
| */ |
| default: |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| break; |
| case IPPROTO_IP: |
| /* |
| * Only allow IPv4 option processing on IPv4 sockets. |
| */ |
| if (icmp->icmp_family != AF_INET) { |
| *outlenp = 0; |
| return (ENOPROTOOPT); |
| } |
| switch (name) { |
| case IP_OPTIONS: |
| case T_IP_OPTIONS: |
| /* Save options for use by IP. */ |
| if (inlen & 0x3) { |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| if (checkonly) |
| break; |
| |
| if (icmp->icmp_ip_snd_options) { |
| mi_free((char *)icmp->icmp_ip_snd_options); |
| icmp->icmp_ip_snd_options_len = 0; |
| icmp->icmp_ip_snd_options = NULL; |
| } |
| if (inlen) { |
| icmp->icmp_ip_snd_options = |
| (uchar_t *)mi_alloc(inlen, BPRI_HI); |
| if (icmp->icmp_ip_snd_options) { |
| bcopy(invalp, |
| icmp->icmp_ip_snd_options, inlen); |
| icmp->icmp_ip_snd_options_len = inlen; |
| } |
| } |
| icmp->icmp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + |
| icmp->icmp_ip_snd_options_len; |
| (void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len + |
| icmp_wroff_extra); |
| break; |
| case IP_HDRINCL: |
| if (!checkonly) |
| icmp->icmp_hdrincl = onoff; |
| break; |
| case IP_TOS: |
| case T_IP_TOS: |
| if (!checkonly) { |
| icmp->icmp_type_of_service = (uint8_t)*i1; |
| } |
| break; |
| case IP_TTL: |
| if (!checkonly) { |
| icmp->icmp_ttl = (uint8_t)*i1; |
| } |
| break; |
| case IP_MULTICAST_IF: |
| /* |
| * TODO should check OPTMGMT reply and undo this if |
| * there is an error. |
| */ |
| if (!checkonly) |
| icmp->icmp_multicast_if_addr = *i1; |
| break; |
| case IP_MULTICAST_TTL: |
| if (!checkonly) |
| icmp->icmp_multicast_ttl = *invalp; |
| break; |
| case IP_MULTICAST_LOOP: |
| if (!checkonly) { |
| icmp->icmp_multicast_loop = |
| (*invalp == 0) ? 0 : 1; |
| } |
| break; |
| case IP_BOUND_IF: |
| if (!checkonly) |
| icmp->icmp_bound_if = *i1; |
| break; |
| case IP_UNSPEC_SRC: |
| if (!checkonly) |
| icmp->icmp_unspec_source = onoff; |
| break; |
| case IP_XMIT_IF: |
| if (!checkonly) |
| icmp->icmp_xmit_if = *i1; |
| break; |
| case IP_RECVIF: |
| if (!checkonly) |
| icmp->icmp_recvif = onoff; |
| break; |
| case IP_ADD_MEMBERSHIP: |
| case IP_DROP_MEMBERSHIP: |
| case IP_BLOCK_SOURCE: |
| case IP_UNBLOCK_SOURCE: |
| case IP_ADD_SOURCE_MEMBERSHIP: |
| case IP_DROP_SOURCE_MEMBERSHIP: |
| case MCAST_JOIN_GROUP: |
| case MCAST_LEAVE_GROUP: |
| case MCAST_BLOCK_SOURCE: |
| case MCAST_UNBLOCK_SOURCE: |
| case MCAST_JOIN_SOURCE_GROUP: |
| case MCAST_LEAVE_SOURCE_GROUP: |
| case MRT_INIT: |
| case MRT_DONE: |
| case MRT_ADD_VIF: |
| case MRT_DEL_VIF: |
| case MRT_ADD_MFC: |
| case MRT_DEL_MFC: |
| case MRT_VERSION: |
| case MRT_ASSERT: |
| case IP_SEC_OPT: |
| case IP_DONTFAILOVER_IF: |
| /* |
| * "soft" error (negative) |
| * option not handled at this level |
| * Note: Do not modify *outlenp |
| */ |
| return (-EINVAL); |
| default: |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| break; |
| case IPPROTO_IPV6: { |
| ip6_pkt_t *ipp; |
| boolean_t sticky; |
| |
| if (icmp->icmp_family != AF_INET6) { |
| *outlenp = 0; |
| return (ENOPROTOOPT); |
| } |
| /* |
| * Deal with both sticky options and ancillary data |
| */ |
| if (thisdg_attrs == NULL) { |
| /* sticky options, or none */ |
| ipp = &icmp->icmp_sticky_ipp; |
| sticky = B_TRUE; |
| } else { |
| /* ancillary data */ |
| ipp = (ip6_pkt_t *)thisdg_attrs; |
| sticky = B_FALSE; |
| } |
| |
| switch (name) { |
| case IPV6_MULTICAST_IF: |
| if (!checkonly) |
| icmp->icmp_multicast_if_index = *i1; |
| break; |
| case IPV6_UNICAST_HOPS: |
| /* -1 means use default */ |
| if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| if (!checkonly) { |
| if (*i1 == -1) { |
| icmp->icmp_ttl = ipp->ipp_unicast_hops = |
| icmp_ipv6_hoplimit; |
| ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; |
| /* Pass modified value to IP. */ |
| *i1 = ipp->ipp_hoplimit; |
| } else { |
| icmp->icmp_ttl = ipp->ipp_unicast_hops = |
| (uint8_t)*i1; |
| ipp->ipp_fields |= IPPF_UNICAST_HOPS; |
| } |
| /* Rebuild the header template */ |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) { |
| *outlenp = 0; |
| return (error); |
| } |
| } |
| break; |
| case IPV6_MULTICAST_HOPS: |
| /* -1 means use default */ |
| if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| if (!checkonly) { |
| if (*i1 == -1) { |
| icmp->icmp_multicast_ttl = |
| ipp->ipp_multicast_hops = |
| IP_DEFAULT_MULTICAST_TTL; |
| ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS; |
| /* Pass modified value to IP. */ |
| *i1 = icmp->icmp_multicast_ttl; |
| } else { |
| icmp->icmp_multicast_ttl = |
| ipp->ipp_multicast_hops = |
| (uint8_t)*i1; |
| ipp->ipp_fields |= IPPF_MULTICAST_HOPS; |
| } |
| } |
| break; |
| case IPV6_MULTICAST_LOOP: |
| if (*i1 != 0 && *i1 != 1) { |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| if (!checkonly) |
| icmp->icmp_multicast_loop = *i1; |
| break; |
| case IPV6_CHECKSUM: |
| /* |
| * Integer offset into the user data of where the |
| * checksum is located. |
| * Offset of -1 disables option. |
| * Does not apply to IPPROTO_ICMPV6. |
| */ |
| if (icmp->icmp_proto == IPPROTO_ICMPV6 || !sticky) { |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) { |
| /* Negative or not 16 bit aligned offset */ |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| if (checkonly) |
| break; |
| |
| if (*i1 == -1) { |
| icmp->icmp_raw_checksum = 0; |
| ipp->ipp_fields &= ~IPPF_RAW_CKSUM; |
| } else { |
| icmp->icmp_raw_checksum = 1; |
| icmp->icmp_checksum_off = *i1; |
| ipp->ipp_fields |= IPPF_RAW_CKSUM; |
| } |
| /* Rebuild the header template */ |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) { |
| *outlenp = 0; |
| return (error); |
| } |
| break; |
| case IPV6_JOIN_GROUP: |
| case IPV6_LEAVE_GROUP: |
| case MCAST_JOIN_GROUP: |
| case MCAST_LEAVE_GROUP: |
| case MCAST_BLOCK_SOURCE: |
| case MCAST_UNBLOCK_SOURCE: |
| case MCAST_JOIN_SOURCE_GROUP: |
| case MCAST_LEAVE_SOURCE_GROUP: |
| /* |
| * "soft" error (negative) |
| * option not handled at this level |
| * Note: Do not modify *outlenp |
| */ |
| return (-EINVAL); |
| case IPV6_BOUND_IF: |
| if (!checkonly) |
| icmp->icmp_bound_if = *i1; |
| break; |
| case IPV6_UNSPEC_SRC: |
| if (!checkonly) |
| icmp->icmp_unspec_source = onoff; |
| break; |
| case IPV6_RECVTCLASS: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvtclass = onoff; |
| break; |
| /* |
| * Set boolean switches for ancillary data delivery |
| */ |
| case IPV6_RECVPKTINFO: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvpktinfo = onoff; |
| break; |
| case IPV6_RECVPATHMTU: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvpathmtu = onoff; |
| break; |
| case IPV6_RECVHOPLIMIT: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvhoplimit = onoff; |
| break; |
| case IPV6_RECVHOPOPTS: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvhopopts = onoff; |
| break; |
| case IPV6_RECVDSTOPTS: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvdstopts = onoff; |
| break; |
| case _OLD_IPV6_RECVDSTOPTS: |
| if (!checkonly) |
| icmp->icmp_old_ipv6_recvdstopts = onoff; |
| break; |
| case IPV6_RECVRTHDRDSTOPTS: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvrtdstopts = onoff; |
| break; |
| case IPV6_RECVRTHDR: |
| if (!checkonly) |
| icmp->icmp_ipv6_recvrthdr = onoff; |
| break; |
| /* |
| * Set sticky options or ancillary data. |
| * If sticky options, (re)build any extension headers |
| * that might be needed as a result. |
| */ |
| case IPV6_PKTINFO: |
| /* |
| * The source address and ifindex are verified |
| * in ip_opt_set(). For ancillary data the |
| * source address is checked in ip_wput_v6. |
| */ |
| if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) |
| return (EINVAL); |
| if (checkonly) |
| break; |
| |
| if (inlen == 0) { |
| ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); |
| ipp->ipp_sticky_ignored |= |
| (IPPF_IFINDEX|IPPF_ADDR); |
| } else { |
| struct in6_pktinfo *pkti; |
| |
| pkti = (struct in6_pktinfo *)invalp; |
| ipp->ipp_ifindex = pkti->ipi6_ifindex; |
| ipp->ipp_addr = pkti->ipi6_addr; |
| if (ipp->ipp_ifindex != 0) |
| ipp->ipp_fields |= IPPF_IFINDEX; |
| else |
| ipp->ipp_fields &= ~IPPF_IFINDEX; |
| if (!IN6_IS_ADDR_UNSPECIFIED( |
| &ipp->ipp_addr)) |
| ipp->ipp_fields |= IPPF_ADDR; |
| else |
| ipp->ipp_fields &= ~IPPF_ADDR; |
| } |
| if (sticky) { |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) |
| return (error); |
| } |
| break; |
| case IPV6_HOPLIMIT: |
| /* This option can only be used as ancillary data. */ |
| if (sticky) |
| return (EINVAL); |
| if (inlen != 0 && inlen != sizeof (int)) |
| return (EINVAL); |
| if (checkonly) |
| break; |
| |
| if (inlen == 0) { |
| ipp->ipp_fields &= ~IPPF_HOPLIMIT; |
| ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT; |
| } else { |
| if (*i1 > 255 || *i1 < -1) |
| return (EINVAL); |
| if (*i1 == -1) |
| ipp->ipp_hoplimit = icmp_ipv6_hoplimit; |
| else |
| ipp->ipp_hoplimit = *i1; |
| ipp->ipp_fields |= IPPF_HOPLIMIT; |
| } |
| break; |
| case IPV6_TCLASS: |
| /* |
| * IPV6_RECVTCLASS accepts -1 as use kernel default |
| * and [0, 255] as the actualy traffic class. |
| */ |
| if (inlen != 0 && inlen != sizeof (int)) |
| return (EINVAL); |
| if (checkonly) |
| break; |
| |
| if (inlen == 0) { |
| ipp->ipp_fields &= ~IPPF_TCLASS; |
| ipp->ipp_sticky_ignored |= IPPF_TCLASS; |
| } else { |
| if (*i1 >= 256 || *i1 < -1) |
| return (EINVAL); |
| if (*i1 == -1) { |
| ipp->ipp_tclass = |
| IPV6_FLOW_TCLASS( |
| IPV6_DEFAULT_VERS_AND_FLOW); |
| } else { |
| ipp->ipp_tclass = *i1; |
| } |
| ipp->ipp_fields |= IPPF_TCLASS; |
| } |
| if (sticky) { |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) |
| return (error); |
| } |
| break; |
| case IPV6_NEXTHOP: |
| /* |
| * IP will verify that the nexthop is reachable |
| * and fail for sticky options. |
| */ |
| if (inlen != 0 && inlen != sizeof (sin6_t)) |
| return (EINVAL); |
| if (checkonly) |
| break; |
| |
| if (inlen == 0) { |
| ipp->ipp_fields &= ~IPPF_NEXTHOP; |
| ipp->ipp_sticky_ignored |= IPPF_NEXTHOP; |
| } else { |
| sin6_t *sin6 = (sin6_t *)invalp; |
| |
| if (sin6->sin6_family != AF_INET6) |
| return (EAFNOSUPPORT); |
| if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) |
| return (EADDRNOTAVAIL); |
| ipp->ipp_nexthop = sin6->sin6_addr; |
| if (!IN6_IS_ADDR_UNSPECIFIED( |
| &ipp->ipp_nexthop)) |
| ipp->ipp_fields |= IPPF_NEXTHOP; |
| else |
| ipp->ipp_fields &= ~IPPF_NEXTHOP; |
| } |
| if (sticky) { |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) |
| return (error); |
| } |
| break; |
| case IPV6_HOPOPTS: { |
| ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; |
| /* |
| * Sanity checks - minimum size, size a multiple of |
| * eight bytes, and matching size passed in. |
| */ |
| if (inlen != 0 && |
| inlen != (8 * (hopts->ip6h_len + 1))) |
| return (EINVAL); |
| |
| if (checkonly) |
| break; |
| if (inlen == 0) { |
| if (sticky && |
| (ipp->ipp_fields & IPPF_HOPOPTS) != 0) { |
| kmem_free(ipp->ipp_hopopts, |
| ipp->ipp_hopoptslen); |
| ipp->ipp_hopopts = NULL; |
| ipp->ipp_hopoptslen = 0; |
| } |
| ipp->ipp_fields &= ~IPPF_HOPOPTS; |
| ipp->ipp_sticky_ignored |= IPPF_HOPOPTS; |
| } else { |
| error = icmp_pkt_set(invalp, inlen, sticky, |
| (uchar_t **)&ipp->ipp_hopopts, |
| &ipp->ipp_hopoptslen); |
| if (error != 0) |
| return (error); |
| ipp->ipp_fields |= IPPF_HOPOPTS; |
| } |
| if (sticky) { |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) |
| return (error); |
| } |
| break; |
| } |
| case IPV6_RTHDRDSTOPTS: { |
| ip6_dest_t *dopts = (ip6_dest_t *)invalp; |
| |
| /* |
| * Sanity checks - minimum size, size a multiple of |
| * eight bytes, and matching size passed in. |
| */ |
| if (inlen != 0 && |
| inlen != (8 * (dopts->ip6d_len + 1))) |
| return (EINVAL); |
| |
| if (checkonly) |
| break; |
| |
| if (inlen == 0) { |
| if (sticky && |
| (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) { |
| kmem_free(ipp->ipp_rtdstopts, |
| ipp->ipp_rtdstoptslen); |
| ipp->ipp_rtdstopts = NULL; |
| ipp->ipp_rtdstoptslen = 0; |
| } |
| ipp->ipp_fields &= ~IPPF_RTDSTOPTS; |
| ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS; |
| } else { |
| error = icmp_pkt_set(invalp, inlen, sticky, |
| (uchar_t **)&ipp->ipp_rtdstopts, |
| &ipp->ipp_rtdstoptslen); |
| if (error != 0) |
| return (error); |
| ipp->ipp_fields |= IPPF_RTDSTOPTS; |
| } |
| if (sticky) { |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) |
| return (error); |
| } |
| break; |
| } |
| case IPV6_DSTOPTS: { |
| ip6_dest_t *dopts = (ip6_dest_t *)invalp; |
| |
| /* |
| * Sanity checks - minimum size, size a multiple of |
| * eight bytes, and matching size passed in. |
| */ |
| if (inlen != 0 && |
| inlen != (8 * (dopts->ip6d_len + 1))) |
| return (EINVAL); |
| |
| if (checkonly) |
| break; |
| |
| if (inlen == 0) { |
| if (sticky && |
| (ipp->ipp_fields & IPPF_DSTOPTS) != 0) { |
| kmem_free(ipp->ipp_dstopts, |
| ipp->ipp_dstoptslen); |
| ipp->ipp_dstopts = NULL; |
| ipp->ipp_dstoptslen = 0; |
| } |
| ipp->ipp_fields &= ~IPPF_DSTOPTS; |
| ipp->ipp_sticky_ignored |= IPPF_DSTOPTS; |
| } else { |
| error = icmp_pkt_set(invalp, inlen, sticky, |
| (uchar_t **)&ipp->ipp_dstopts, |
| &ipp->ipp_dstoptslen); |
| if (error != 0) |
| return (error); |
| ipp->ipp_fields |= IPPF_DSTOPTS; |
| } |
| if (sticky) { |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) |
| return (error); |
| } |
| break; |
| } |
| case IPV6_RTHDR: { |
| ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; |
| |
| /* |
| * Sanity checks - minimum size, size a multiple of |
| * eight bytes, and matching size passed in. |
| */ |
| if (inlen != 0 && |
| inlen != (8 * (rt->ip6r_len + 1))) |
| return (EINVAL); |
| |
| if (checkonly) |
| break; |
| |
| if (inlen == 0) { |
| if (sticky && |
| (ipp->ipp_fields & IPPF_RTHDR) != 0) { |
| kmem_free(ipp->ipp_rthdr, |
| ipp->ipp_rthdrlen); |
| ipp->ipp_rthdr = NULL; |
| ipp->ipp_rthdrlen = 0; |
| } |
| ipp->ipp_fields &= ~IPPF_RTHDR; |
| ipp->ipp_sticky_ignored |= IPPF_RTHDR; |
| } else { |
| error = icmp_pkt_set(invalp, inlen, sticky, |
| (uchar_t **)&ipp->ipp_rthdr, |
| &ipp->ipp_rthdrlen); |
| if (error != 0) |
| return (error); |
| ipp->ipp_fields |= IPPF_RTHDR; |
| } |
| if (sticky) { |
| error = icmp_build_hdrs(q, icmp); |
| if (error != 0) |
| return (error); |
| } |
| break; |
| } |
| |
| case IPV6_DONTFRAG: |
| if (checkonly) |
| break; |
| |
| if (onoff) { |
| ipp->ipp_fields |= IPPF_DONTFRAG; |
| } else { |
| ipp->ipp_fields &= ~IPPF_DONTFRAG; |
| } |
| break; |
| |
| case IPV6_USE_MIN_MTU: |
| if (inlen != sizeof (int)) |
| return (EINVAL); |
| |
| if (*i1 < -1 || *i1 > 1) |
| return (EINVAL); |
| |
| if (checkonly) |
| break; |
| |
| ipp->ipp_fields |= IPPF_USE_MIN_MTU; |
| ipp->ipp_use_min_mtu = *i1; |
| break; |
| |
| /* |
| * This option can't be set. Its only returned via |
| * getsockopt() or ancillary data. |
| */ |
| case IPV6_PATHMTU: |
| return (EINVAL); |
| |
| case IPV6_BOUND_PIF: |
| case IPV6_SEC_OPT: |
| case IPV6_DONTFAILOVER_IF: |
| case IPV6_SRC_PREFERENCES: |
| case IPV6_V6ONLY: |
| /* Handled at IP level */ |
| return (-EINVAL); |
| default: |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| break; |
| } /* end IPPROTO_IPV6 */ |
| |
| case IPPROTO_ICMPV6: |
| /* |
| * Only allow IPv6 option processing on IPv6 sockets. |
| */ |
| if (icmp->icmp_family != AF_INET6) { |
| *outlenp = 0; |
| return (ENOPROTOOPT); |
| } |
| if (icmp->icmp_proto != IPPROTO_ICMPV6) { |
| *outlenp = 0; |
| return (ENOPROTOOPT); |
| } |
| switch (name) { |
| case ICMP6_FILTER: |
| if (!checkonly) { |
| if ((inlen != 0) && |
| (inlen != sizeof (icmp6_filter_t))) |
| return (EINVAL); |
| |
| if (inlen == 0) { |
| if (icmp->icmp_filter != NULL) { |
| kmem_free(icmp->icmp_filter, |
| sizeof (icmp6_filter_t)); |
| icmp->icmp_filter = NULL; |
| } |
| } else { |
| if (icmp->icmp_filter == NULL) { |
| icmp->icmp_filter = kmem_alloc( |
| sizeof (icmp6_filter_t), |
| KM_NOSLEEP); |
| if (icmp->icmp_filter == NULL) { |
| *outlenp = 0; |
| return (ENOBUFS); |
| } |
| } |
| (void) bcopy(invalp, icmp->icmp_filter, |
| inlen); |
| } |
| } |
| break; |
| |
| default: |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| break; |
| default: |
| *outlenp = 0; |
| return (EINVAL); |
| } |
| /* |
| * Common case of OK return with outval same as inval. |
| */ |
| if (invalp != outvalp) { |
| /* don't trust bcopy for identical src/dst */ |
| (void) bcopy(invalp, outvalp, inlen); |
| } |
| *outlenp = inlen; |
| return (0); |
| } |
| |
| /* |
| * Update icmp_sticky_hdrs based on icmp_sticky_ipp, icmp_v6src, icmp_ttl, |
| * icmp_proto, icmp_raw_checksum and icmp_no_tp_cksum. |
| * The headers include ip6i_t (if needed), ip6_t, and any sticky extension |
| * headers. |
| * Returns failure if can't allocate memory. |
| */ |
| static int |
| icmp_build_hdrs(queue_t *q, icmp_t *icmp) |
| { |
| uchar_t *hdrs; |
| uint_t hdrs_len; |
| ip6_t *ip6h; |
| ip6i_t *ip6i; |
| ip6_pkt_t *ipp = &icmp->icmp_sticky_ipp; |
| |
| hdrs_len = ip_total_hdrs_len_v6(ipp); |
| ASSERT(hdrs_len != 0); |
| if (hdrs_len != icmp->icmp_sticky_hdrs_len) { |
| /* Need to reallocate */ |
| if (hdrs_len != 0) { |
| hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP); |
| if (hdrs == NULL) |
| return (ENOMEM); |
| } else { |
| hdrs = NULL; |
| } |
| if (icmp->icmp_sticky_hdrs_len != 0) { |
| kmem_free(icmp->icmp_sticky_hdrs, |
| icmp->icmp_sticky_hdrs_len); |
| } |
| icmp->icmp_sticky_hdrs = hdrs; |
| icmp->icmp_sticky_hdrs_len = hdrs_len; |
| } |
| ip_build_hdrs_v6(icmp->icmp_sticky_hdrs, |
| icmp->icmp_sticky_hdrs_len, ipp, icmp->icmp_proto); |
| |
| /* Set header fields not in ipp */ |
| if (ipp->ipp_fields & IPPF_HAS_IP6I) { |
| ip6i = (ip6i_t *)icmp->icmp_sticky_hdrs; |
| ip6h = (ip6_t *)&ip6i[1]; |
| |
| if (ipp->ipp_fields & IPPF_RAW_CKSUM) { |
| ip6i->ip6i_flags |= IP6I_RAW_CHECKSUM; |
| ip6i->ip6i_checksum_off = icmp->icmp_checksum_off; |
| } |
| if (ipp->ipp_fields & IPPF_NO_CKSUM) { |
| ip6i->ip6i_flags |= IP6I_NO_ULP_CKSUM; |
| } |
| } else { |
| ip6h = (ip6_t *)icmp->icmp_sticky_hdrs; |
| } |
| |
| if (!(ipp->ipp_fields & IPPF_ADDR)) |
| ip6h->ip6_src = icmp->icmp_v6src; |
| |
| /* Try to get everything in a single mblk */ |
| if (hdrs_len > icmp->icmp_max_hdr_len) { |
| icmp->icmp_max_hdr_len = hdrs_len; |
| (void) mi_set_sth_wroff(RD(q), icmp->icmp_max_hdr_len + |
| icmp_wroff_extra); |
| } |
| return (0); |
| } |
| |
| /* |
| * Set optbuf and optlen for the option. |
| * If sticky is set allocate memory (if not already present). |
| * Otherwise just point optbuf and optlen at invalp and inlen. |
| * Returns failure if memory can not be allocated. |
| */ |
| static int |
| icmp_pkt_set(uchar_t *invalp, uint_t inlen, boolean_t sticky, |
| uchar_t **optbufp |