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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.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>
43 void ip_helper_wput(queue_t
*q
, mblk_t
*mp
);
45 static int ip_helper_stream_close(queue_t
*, int, cred_t
*);
47 static struct module_info ip_helper_stream_info
= {
48 0, "iphelper", IP_MOD_MINPSZ
, IP_MOD_MAXPSZ
, IP_MOD_HIWAT
, IP_MOD_LOWAT
51 static struct qinit ip_helper_stream_rinit
= {
52 NULL
, NULL
, NULL
, ip_helper_stream_close
, NULL
,
53 &ip_helper_stream_info
, NULL
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
62 * set the q_ptr of the 'q' to the conn_t pointer passed in
65 ip_helper_share_conn(queue_t
*q
, mblk_t
*mp
, cred_t
*crp
)
67 conn_t
*connp
= *((conn_t
**)mp
->b_cont
->b_rptr
);
70 * This operation is allowed only on helper streams with kcred
73 if (kcred
!= crp
|| msgdsize(mp
->b_cont
) != sizeof (void *)) {
74 miocnak(q
, mp
, 0, EINVAL
);
78 connp
->conn_helper_info
->iphs_minfo
= q
->q_ptr
;
79 connp
->conn_helper_info
->iphs_rq
= RD(q
);
80 connp
->conn_helper_info
->iphs_wq
= WR(q
);
81 WR(q
)->q_ptr
= RD(q
)->q_ptr
= (void *)connp
;
82 connp
->conn_rq
= RD(q
);
83 connp
->conn_wq
= WR(q
);
88 ip_helper_wput(queue_t
*q
, mblk_t
*mp
)
90 struct iocblk
*iocp
= (struct iocblk
*)mp
->b_rptr
;
91 if (DB_TYPE(mp
) == M_IOCTL
&&
92 iocp
->ioc_cmd
== SIOCSQPTR
) {
93 ip_helper_share_conn(q
, mp
, iocp
->ioc_cr
);
95 /* We only handle ioctl related messages here */
96 ASSERT(DB_TYPE(mp
) != M_DATA
);
97 ip_wput_nondata(q
, mp
);
103 ip_helper_stream_setup(queue_t
*q
, dev_t
*devp
, int flag
, int sflag
,
104 cred_t
*credp
, boolean_t isv6
)
107 ip_helper_minfo_t
*ip_minfop
;
109 ASSERT((flag
& ~(FKLYR
)) == IP_HELPER_STR
);
113 ip_minfop
= kmem_alloc(sizeof (ip_helper_minfo_t
), KM_SLEEP
);
114 ASSERT(ip_minfop
!= NULL
);
116 ip_minfop
->ip_minfo_dev
= 0;
117 ip_minfop
->ip_minfo_arena
= NULL
;
120 * Clone the device, allocate minor device number
122 if (ip_minor_arena_la
!= NULL
)
123 ip_minfop
->ip_minfo_dev
= inet_minor_alloc(ip_minor_arena_la
);
125 if (ip_minfop
->ip_minfo_dev
== 0) {
127 * numbers in the large arena are exhausted
129 * Or this is a 32 bit system, 32 bit systems do not have
132 ip_minfop
->ip_minfo_dev
= inet_minor_alloc(ip_minor_arena_sa
);
133 if (ip_minfop
->ip_minfo_dev
== 0) {
136 ip_minfop
->ip_minfo_arena
= ip_minor_arena_sa
;
138 ip_minfop
->ip_minfo_arena
= ip_minor_arena_la
;
142 ASSERT(ip_minfop
->ip_minfo_dev
!= 0);
143 ASSERT(ip_minfop
->ip_minfo_arena
!= NULL
);
145 RD(q
)->q_ptr
= WR(q
)->q_ptr
= ip_minfop
;
147 maj
= getemajor(*devp
);
148 *devp
= makedevice(maj
, (ulong_t
)(ip_minfop
->ip_minfo_dev
));
150 q
->q_qinfo
= &ip_helper_stream_rinit
;
151 WR(q
)->q_qinfo
= &ip_helper_stream_winit
;
158 ip_helper_stream_close(queue_t
*q
, int flag __unused
, cred_t
*credp __unused
)
160 ip_helper_minfo_t
*ip_minfop
;
163 ip_minfop
= (q
)->q_ptr
;
164 inet_minor_free(ip_minfop
->ip_minfo_arena
,
165 ip_minfop
->ip_minfo_dev
);
166 kmem_free(ip_minfop
, sizeof (ip_helper_minfo_t
));
173 * Public interface for creating an IP stream with shared conn_t
174 * Handles multiple callers in parallel by using conn_lock.
175 * Note that we allocate the helper stream without any locks, which means
176 * we might need to free it if we had two threads doing this concurrently
180 ip_create_helper_stream(conn_t
*connp
, ldi_ident_t li
)
182 ip_helper_stream_info_t
*helper
;
186 ASSERT(!servicing_interrupt());
188 if (connp
->conn_helper_info
!= NULL
) {
189 /* Already allocated */
194 helper
= kmem_alloc(sizeof (ip_helper_stream_info_t
), KM_SLEEP
);
197 * open ip device via the layered interface.
198 * pass in kcred as some threads do not have the
199 * priviledge to open /dev/ip and the check in
200 * secpolicy_spec_open() will fail the open
202 error
= ldi_open_by_name((connp
->conn_family
== AF_INET6
? DEV_IP6
:
203 DEV_IP
), IP_HELPER_STR
, kcred
, &helper
->iphs_handle
, li
);
206 kmem_free(helper
, sizeof (ip_helper_stream_info_t
));
209 /* Make sure we are the only one */
210 mutex_enter(&connp
->conn_lock
);
211 if (connp
->conn_helper_info
!= NULL
) {
212 /* Some other thread won - discard this stream */
213 mutex_exit(&connp
->conn_lock
);
214 (void) ldi_close(helper
->iphs_handle
, 0, kcred
);
215 kmem_free(helper
, sizeof (ip_helper_stream_info_t
));
218 connp
->conn_helper_info
= helper
;
220 * Share connp with the helper stream. We hold conn_lock across this
223 error
= ldi_ioctl(helper
->iphs_handle
, SIOCSQPTR
, (intptr_t)connp
,
224 FKIOCTL
, kcred
, &ret
);
228 * Passing in a zero flag indicates that an error
229 * occured and stream was not shared
231 (void) ldi_close(helper
->iphs_handle
, 0, kcred
);
232 kmem_free(helper
, sizeof (ip_helper_stream_info_t
));
233 connp
->conn_helper_info
= NULL
;
235 mutex_exit(&connp
->conn_lock
);
240 * Public interface for freeing IP helper stream
241 * Caller must ensure no concurrent use of the conn_t, which is normally
242 * done by calling this from the close routine when the conn_t is quiesced.
245 ip_free_helper_stream(conn_t
*connp
)
247 ASSERT(!servicing_interrupt());
249 if (connp
->conn_helper_info
== NULL
)
252 ASSERT(connp
->conn_helper_info
->iphs_handle
!= NULL
);
254 connp
->conn_helper_info
->iphs_rq
->q_ptr
=
255 connp
->conn_helper_info
->iphs_wq
->q_ptr
=
256 connp
->conn_helper_info
->iphs_minfo
;
257 (void) ldi_close(connp
->conn_helper_info
->iphs_handle
,
258 IP_HELPER_STR
, kcred
);
259 kmem_free(connp
->conn_helper_info
, sizeof (ip_helper_stream_info_t
));
260 connp
->conn_helper_info
= NULL
;