1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/types.h> 28 #include <inet/ip.h> 29 #include <inet/ip_impl.h> 30 #include <inet/ipclassifier.h> 31 #include <inet/proto_set.h> 32 #include <sys/stream.h> 33 #include <sys/strsubr.h> 34 #include <sys/strsun.h> 35 #include <sys/cmn_err.h> 36 #include <sys/t_kuser.h> 37 #include <sys/tihdr.h> 38 #include <sys/pathname.h> 39 #include <sys/sockio.h> 40 #include <sys/vmem.h> 41 #include <sys/disp.h> 42 43 void ip_helper_wput(queue_t *q, mblk_t *mp); 44 45 static int ip_helper_stream_close(queue_t *, int); 46 47 static struct module_info ip_helper_stream_info = { 48 0, "iphelper", IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT, IP_MOD_LOWAT 49 }; 50 51 static struct qinit ip_helper_stream_rinit = { 52 NULL, NULL, NULL, ip_helper_stream_close, NULL, 53 &ip_helper_stream_info, NULL 54 }; 55 56 static struct qinit ip_helper_stream_winit = { 57 (pfi_t)ip_helper_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL, 58 &ip_helper_stream_info, NULL, NULL, NULL, STRUIOT_NONE 59 }; 60 61 #define IP_USE_HELPER_CACHE (ip_helper_stream_cache != NULL) 62 63 /* 64 * set the q_ptr of the 'q' to the conn_t pointer passed in 65 */ 66 static void 67 ip_helper_share_conn(queue_t *q, mblk_t *mp) 68 { 69 if (IP_USE_HELPER_CACHE) { 70 ip_helper_stream_info_t *ip_helper_info; 71 72 ip_helper_info = *((ip_helper_stream_info_t **) 73 mp->b_cont->b_rptr); 74 ip_helper_info->iphs_minfo = q->q_ptr; 75 ip_helper_info->iphs_rq = RD(q); 76 ip_helper_info->iphs_wq = WR(q); 77 } else { 78 conn_t *connp = *((conn_t **)mp->b_cont->b_rptr); 79 80 connp->conn_helper_info->iphs_minfo = q->q_ptr; 81 connp->conn_helper_info->iphs_rq = RD(q); 82 connp->conn_helper_info->iphs_wq = WR(q); 83 WR(q)->q_ptr = RD(q)->q_ptr = (void *)connp; 84 connp->conn_rq = RD(q); 85 connp->conn_wq = WR(q); 86 } 87 miocack(q, mp, 0, 0); 88 } 89 90 void 91 ip_helper_wput(queue_t *q, mblk_t *mp) 92 { 93 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 94 if (DB_TYPE(mp) == M_IOCTL && 95 iocp->ioc_cmd == SIOCSQPTR) { 96 ip_helper_share_conn(q, mp); 97 } else { 98 conn_t *connp = (conn_t *)q->q_ptr; 99 100 if (connp->conn_af_isv6) { 101 ip_wput_v6(q, mp); 102 } else { 103 ip_wput(q, mp); 104 } 105 } 106 } 107 108 /* ARGSUSED */ 109 int 110 ip_helper_stream_setup(queue_t *q, dev_t *devp, int flag, int sflag, 111 cred_t *credp, boolean_t isv6) 112 { 113 major_t maj; 114 ip_helper_minfo_t *ip_minfop; 115 116 ASSERT((flag & ~(FKLYR)) == IP_HELPER_STR); 117 118 ASSERT(RD(q) == q); 119 120 ip_minfop = kmem_alloc(sizeof (ip_helper_minfo_t), KM_NOSLEEP); 121 if (ip_minfop == NULL) { 122 return (ENOMEM); 123 } 124 125 ip_minfop->ip_minfo_dev = 0; 126 ip_minfop->ip_minfo_arena = NULL; 127 128 /* 129 * Clone the device, allocate minor device number 130 */ 131 if (ip_minor_arena_la != NULL) 132 ip_minfop->ip_minfo_dev = inet_minor_alloc(ip_minor_arena_la); 133 134 if (ip_minfop->ip_minfo_dev == 0) { 135 /* 136 * numbers in the large arena are exhausted 137 * Try small arena. 138 * Or this is a 32 bit system, 32 bit systems do not have 139 * ip_minor_arena_la 140 */ 141 ip_minfop->ip_minfo_dev = inet_minor_alloc(ip_minor_arena_sa); 142 if (ip_minfop->ip_minfo_dev == 0) { 143 return (EBUSY); 144 } 145 ip_minfop->ip_minfo_arena = ip_minor_arena_sa; 146 } else { 147 ip_minfop->ip_minfo_arena = ip_minor_arena_la; 148 } 149 150 151 ASSERT(ip_minfop->ip_minfo_dev != 0); 152 ASSERT(ip_minfop->ip_minfo_arena != NULL); 153 154 RD(q)->q_ptr = WR(q)->q_ptr = ip_minfop; 155 156 maj = getemajor(*devp); 157 *devp = makedevice(maj, (ulong_t)(ip_minfop->ip_minfo_dev)); 158 159 q->q_qinfo = &ip_helper_stream_rinit; 160 WR(q)->q_qinfo = &ip_helper_stream_winit; 161 qprocson(q); 162 return (0); 163 } 164 165 /* ARGSUSED */ 166 static int 167 ip_helper_stream_close(queue_t *q, int flag) 168 { 169 ip_helper_minfo_t *ip_minfop; 170 171 qprocsoff(q); 172 ip_minfop = (q)->q_ptr; 173 inet_minor_free(ip_minfop->ip_minfo_arena, 174 ip_minfop->ip_minfo_dev); 175 kmem_free(ip_minfop, sizeof (ip_helper_minfo_t)); 176 RD(q)->q_ptr = NULL; 177 WR(q)->q_ptr = NULL; 178 return (0); 179 } 180 181 /* 182 * Public interface for creating an IP stream with shared conn_t 183 */ 184 /* ARGSUSED */ 185 int 186 ip_create_helper_stream(conn_t *connp, ldi_ident_t li) 187 { 188 int error; 189 int ret; 190 191 ASSERT(!servicing_interrupt()); 192 193 error = 0; 194 if (IP_USE_HELPER_CACHE) { 195 connp->conn_helper_info = kmem_cache_alloc( 196 ip_helper_stream_cache, KM_NOSLEEP); 197 if (connp->conn_helper_info == NULL) 198 return (EAGAIN); 199 connp->conn_rq = connp->conn_helper_info->iphs_rq; 200 connp->conn_wq = connp->conn_helper_info->iphs_wq; 201 connp->conn_helper_info->iphs_rq->q_ptr = connp; 202 connp->conn_helper_info->iphs_wq->q_ptr = connp; 203 } else { 204 ASSERT(connp->conn_helper_info == NULL); 205 connp->conn_helper_info = kmem_alloc( 206 sizeof (ip_helper_stream_info_t), KM_SLEEP); 207 /* 208 * open ip device via the layered interface. 209 * pass in kcred as some threads do not have the 210 * priviledge to open /dev/ip and the check in 211 * secpolicy_spec_open() will fail the open 212 */ 213 error = ldi_open_by_name(connp->conn_af_isv6 ? 214 DEV_IP6 : DEV_IP, IP_HELPER_STR, 215 kcred, &connp->conn_helper_info->iphs_handle, li); 216 217 if (error != 0) { 218 kmem_free(connp->conn_helper_info, 219 (sizeof (ip_helper_stream_info_t))); 220 connp->conn_helper_info = NULL; 221 return (error); 222 } 223 /* 224 * Share connp with the helper stream 225 */ 226 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 227 SIOCSQPTR, (intptr_t)connp, FKIOCTL, kcred, &ret); 228 229 if (error != 0) { 230 /* 231 * Passing in a zero flag indicates that an error 232 * occured and stream was not shared 233 */ 234 (void) ldi_close(connp->conn_helper_info->iphs_handle, 235 0, kcred); 236 kmem_free(connp->conn_helper_info, 237 (sizeof (ip_helper_stream_info_t))); 238 connp->conn_helper_info = NULL; 239 } 240 } 241 return (error); 242 } 243 244 /* 245 * Public interface for closing the shared IP stream 246 */ 247 /* ARGSUSED */ 248 void 249 ip_close_helper_stream(conn_t *connp) 250 { 251 ASSERT(!servicing_interrupt()); 252 if (IP_USE_HELPER_CACHE) { 253 254 if (connp->conn_helper_info == NULL) 255 return; 256 ASSERT(connp->conn_helper_info->iphs_rq != NULL); 257 ASSERT(connp->conn_helper_info->iphs_wq != NULL); 258 259 /* Prevent service procedures from being called */ 260 disable_svc(connp->conn_helper_info->iphs_rq); 261 262 /* Wait until service procedure of each queue is run */ 263 wait_svc(connp->conn_helper_info->iphs_rq); 264 265 /* Cleanup any pending ioctls */ 266 conn_ioctl_cleanup(connp); 267 268 /* Allow service procedures to be called again */ 269 enable_svc(connp->conn_helper_info->iphs_rq); 270 271 /* Flush the queues */ 272 flushq(connp->conn_helper_info->iphs_rq, FLUSHALL); 273 flushq(connp->conn_helper_info->iphs_wq, FLUSHALL); 274 275 connp->conn_helper_info->iphs_rq->q_ptr = NULL; 276 connp->conn_helper_info->iphs_wq->q_ptr = NULL; 277 278 kmem_cache_free(ip_helper_stream_cache, 279 connp->conn_helper_info); 280 } else { 281 ASSERT( 282 connp->conn_helper_info->iphs_handle != NULL); 283 284 connp->conn_helper_info->iphs_rq->q_ptr = 285 connp->conn_helper_info->iphs_wq->q_ptr = 286 connp->conn_helper_info->iphs_minfo; 287 (void) ldi_close(connp->conn_helper_info->iphs_handle, 288 IP_HELPER_STR, kcred); 289 kmem_free(connp->conn_helper_info, 290 sizeof (ip_helper_stream_info_t)); 291 } 292 connp->conn_helper_info = NULL; 293 } 294 295 /* 296 * create a T_SVR4_OPTMGMT_REQ TPI message and send down the IP stream 297 */ 298 static int 299 ip_send_option_request(conn_t *connp, uint_t optset_context, int level, 300 int option_name, const void *optval, t_uscalar_t optlen, cred_t *cr) 301 { 302 struct T_optmgmt_req *optmgmt_reqp; 303 struct opthdr *ohp; 304 ssize_t size; 305 mblk_t *mp; 306 307 size = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr) + optlen; 308 mp = allocb_cred(size, cr); 309 if (mp == NULL) 310 return (ENOMEM); 311 312 mp->b_datap->db_type = M_PROTO; 313 optmgmt_reqp = (struct T_optmgmt_req *)mp->b_wptr; 314 315 optmgmt_reqp->PRIM_type = T_SVR4_OPTMGMT_REQ; 316 optmgmt_reqp->MGMT_flags = optset_context; 317 optmgmt_reqp->OPT_length = (t_scalar_t)sizeof (struct opthdr) + optlen; 318 optmgmt_reqp->OPT_offset = (t_scalar_t)sizeof (struct T_optmgmt_req); 319 320 mp->b_wptr += sizeof (struct T_optmgmt_req); 321 322 ohp = (struct opthdr *)mp->b_wptr; 323 324 ohp->level = level; 325 ohp->name = option_name; 326 ohp->len = optlen; 327 328 mp->b_wptr += sizeof (struct opthdr); 329 330 if (optval != NULL) { 331 bcopy(optval, mp->b_wptr, optlen); 332 } else { 333 bzero(mp->b_wptr, optlen); 334 } 335 mp->b_wptr += optlen; 336 337 /* 338 * Send down the primitive 339 */ 340 return (ldi_putmsg(connp->conn_helper_info->iphs_handle, mp)); 341 } 342 343 /* 344 * wait/process the response to T_SVR4_OPTMGMT_REQ TPI message 345 */ 346 static int 347 ip_get_option_response(conn_t *connp, uint_t optset_context, void *optval, 348 t_uscalar_t *optlenp) 349 { 350 union T_primitives *tpr; 351 int error; 352 mblk_t *mp; 353 354 mp = NULL; 355 356 ASSERT(optset_context == T_CHECK || optset_context == T_NEGOTIATE); 357 error = ldi_getmsg(connp->conn_helper_info->iphs_handle, &mp, NULL); 358 if (error != 0) { 359 return (error); 360 } 361 362 if (DB_TYPE(mp) != M_PCPROTO || MBLKL(mp) < sizeof (tpr->type)) { 363 error = EPROTO; 364 goto done; 365 } 366 367 tpr = (union T_primitives *)mp->b_rptr; 368 369 switch (tpr->type) { 370 case T_OPTMGMT_ACK: 371 if (MBLKL(mp) < TOPTMGMTACKSZ) 372 error = EPROTO; 373 break; 374 case T_ERROR_ACK: 375 if (MBLKL(mp) < TERRORACKSZ) { 376 error = EPROTO; 377 break; 378 } 379 380 if (tpr->error_ack.TLI_error == TSYSERR) 381 error = tpr->error_ack.UNIX_error; 382 else 383 error = proto_tlitosyserr(tpr->error_ack.TLI_error); 384 break; 385 default: 386 error = EPROTO; 387 break; 388 } 389 390 if ((optset_context == T_CHECK) && (error == 0)) { 391 struct opthdr *opt_res; 392 t_uscalar_t len; 393 t_uscalar_t size; 394 t_uscalar_t maxlen = *optlenp; 395 void *option; 396 struct T_optmgmt_ack *optmgmt_ack; 397 398 optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr; 399 opt_res = (struct opthdr *) 400 ((uintptr_t)mp->b_rptr + optmgmt_ack->OPT_offset); 401 /* 402 * Check mblk boundary 403 */ 404 if (!MBLKIN(mp, optmgmt_ack->OPT_offset, 405 optmgmt_ack->OPT_length)) { 406 error = EPROTO; 407 goto done; 408 } 409 410 /* 411 * Check alignment 412 */ 413 if ((((uintptr_t)opt_res) & (__TPI_ALIGN_SIZE - 1)) != 0) { 414 error = EPROTO; 415 goto done; 416 } 417 418 option = &opt_res[1]; 419 420 /* check to ensure that the option is within bounds */ 421 if ((((uintptr_t)option + opt_res->len) < (uintptr_t)option) || 422 !MBLKIN(mp, sizeof (struct opthdr), opt_res->len)) { 423 error = EPROTO; 424 goto done; 425 } 426 427 len = opt_res->len; 428 size = MIN(len, maxlen); 429 430 /* 431 * Copy data 432 */ 433 bcopy(option, optval, size); 434 bcopy(&size, optlenp, sizeof (size)); 435 } 436 437 done: 438 freemsg(mp); 439 return (error); 440 } 441 442 /* 443 * Public interface to get socketoptions via the ip helper stream. 444 */ 445 int 446 ip_get_options(conn_t *connp, int level, int option_name, void *optval, 447 t_uscalar_t *optlenp, cred_t *cr) 448 { 449 int error; 450 451 error = ip_send_option_request(connp, T_CHECK, level, option_name, NULL, 452 *optlenp, cr); 453 if (error) 454 return (error); 455 456 return (ip_get_option_response(connp, T_CHECK, optval, optlenp)); 457 } 458 459 /* 460 * Public interface to set socket options via the ip helper stream. 461 */ 462 int 463 ip_set_options(conn_t *connp, int level, int option_name, const void *optval, 464 t_uscalar_t optlen, cred_t *cr) 465 { 466 467 int error; 468 469 error = ip_send_option_request(connp, T_NEGOTIATE, level, option_name, 470 optval, optlen, cr); 471 if (error) 472 return (error); 473 474 return (ip_get_option_response(connp, T_NEGOTIATE, (void *)optval, 475 &optlen)); 476 } 477