xref: /dragonfly/sys/kern/uipc_msg.c (revision 8a0bcd56)
1 /*
2  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $DragonFly: src/sys/kern/uipc_msg.c,v 1.26 2008/10/27 02:56:30 sephe Exp $
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/msgport.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/socketops.h>
44 #include <sys/thread.h>
45 #include <sys/thread2.h>
46 #include <sys/msgport2.h>
47 #include <vm/pmap.h>
48 #include <net/netmsg2.h>
49 
50 #include <net/netisr.h>
51 #include <net/netmsg.h>
52 
53 /*
54  * Abort a socket and free it.  Called from soabort() only.
55  */
56 void
57 so_pru_abort(struct socket *so)
58 {
59 	struct netmsg_pru_abort msg;
60 
61 	netmsg_init(&msg.base, so, &curthread->td_msgport,
62 		    0, so->so_proto->pr_usrreqs->pru_abort);
63 	(void)lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
64 }
65 
66 /*
67  * Abort a socket and free it, asynchronously.  Called from
68  * soaborta() only.
69  */
70 void
71 so_pru_aborta(struct socket *so)
72 {
73 	struct netmsg_pru_abort *msg;
74 
75 	msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_ZERO);
76 	netmsg_init(&msg->base, so, &netisr_afree_rport,
77 		    0, so->so_proto->pr_usrreqs->pru_abort);
78 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
79 }
80 
81 /*
82  * Abort a socket and free it.  Called from soabort_oncpu() only.
83  * Caller must make sure that the current CPU is inpcb's owner CPU.
84  */
85 void
86 so_pru_abort_oncpu(struct socket *so)
87 {
88 	struct netmsg_pru_abort msg;
89 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_abort;
90 
91 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
92 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
93 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
94 	func((netmsg_t)&msg);
95 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
96 }
97 
98 /*
99  * WARNING!  Synchronous call from user context
100  */
101 int
102 so_pru_accept_direct(struct socket *so, struct sockaddr **nam)
103 {
104 	struct netmsg_pru_accept msg;
105 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_accept;
106 
107 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
108 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
109 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
110 	msg.nm_nam = nam;
111 	func((netmsg_t)&msg);
112 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
113 	return(msg.base.lmsg.ms_error);
114 }
115 
116 int
117 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
118 {
119 	struct netmsg_pru_attach msg;
120 	int error;
121 
122 	netmsg_init(&msg.base, so, &curthread->td_msgport,
123 		    0, so->so_proto->pr_usrreqs->pru_attach);
124 	msg.nm_proto = proto;
125 	msg.nm_ai = ai;
126 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
127 	return (error);
128 }
129 
130 int
131 so_pru_attach_direct(struct socket *so, int proto, struct pru_attach_info *ai)
132 {
133 	struct netmsg_pru_attach msg;
134 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_attach;
135 
136 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
137 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
138 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
139 	msg.nm_proto = proto;
140 	msg.nm_ai = ai;
141 	func((netmsg_t)&msg);
142 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
143 	return(msg.base.lmsg.ms_error);
144 }
145 
146 /*
147  * NOTE: If the target port changes the bind operation will deal with it.
148  */
149 int
150 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
151 {
152 	struct netmsg_pru_bind msg;
153 	int error;
154 
155 	netmsg_init(&msg.base, so, &curthread->td_msgport,
156 		    0, so->so_proto->pr_usrreqs->pru_bind);
157 	msg.nm_nam = nam;
158 	msg.nm_td = td;		/* used only for prison_ip() */
159 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
160 	return (error);
161 }
162 
163 int
164 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
165 {
166 	struct netmsg_pru_connect msg;
167 	int error;
168 
169 	netmsg_init(&msg.base, so, &curthread->td_msgport,
170 		    0, so->so_proto->pr_usrreqs->pru_connect);
171 	msg.nm_nam = nam;
172 	msg.nm_td = td;
173 	msg.nm_m = NULL;
174 	msg.nm_flags = 0;
175 	msg.nm_reconnect = 0;
176 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
177 	return (error);
178 }
179 
180 int
181 so_pru_connect2(struct socket *so1, struct socket *so2)
182 {
183 	struct netmsg_pru_connect2 msg;
184 	int error;
185 
186 	netmsg_init(&msg.base, so1, &curthread->td_msgport,
187 		    0, so1->so_proto->pr_usrreqs->pru_connect2);
188 	msg.nm_so1 = so1;
189 	msg.nm_so2 = so2;
190 	error = lwkt_domsg(so1->so_port, &msg.base.lmsg, 0);
191 	return (error);
192 }
193 
194 /*
195  * WARNING!  Synchronous call from user context.  Control function may do
196  *	     copyin/copyout.
197  */
198 int
199 so_pru_control_direct(struct socket *so, u_long cmd, caddr_t data,
200 		      struct ifnet *ifp)
201 {
202 	struct netmsg_pru_control msg;
203 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_control;
204 
205 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
206 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
207 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
208 	msg.nm_cmd = cmd;
209 	msg.nm_data = data;
210 	msg.nm_ifp = ifp;
211 	msg.nm_td = curthread;
212 	func((netmsg_t)&msg);
213 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
214 	return(msg.base.lmsg.ms_error);
215 }
216 
217 int
218 so_pru_detach(struct socket *so)
219 {
220 	struct netmsg_pru_detach msg;
221 	int error;
222 
223 	netmsg_init(&msg.base, so, &curthread->td_msgport,
224 		    0, so->so_proto->pr_usrreqs->pru_detach);
225 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
226 	return (error);
227 }
228 
229 int
230 so_pru_disconnect(struct socket *so)
231 {
232 	struct netmsg_pru_disconnect msg;
233 	int error;
234 
235 	netmsg_init(&msg.base, so, &curthread->td_msgport,
236 		    0, so->so_proto->pr_usrreqs->pru_disconnect);
237 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
238 	return (error);
239 }
240 
241 int
242 so_pru_listen(struct socket *so, struct thread *td)
243 {
244 	struct netmsg_pru_listen msg;
245 	int error;
246 
247 	netmsg_init(&msg.base, so, &curthread->td_msgport,
248 		    0, so->so_proto->pr_usrreqs->pru_listen);
249 	msg.nm_td = td;		/* used only for prison_ip() XXX JH */
250 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
251 	return (error);
252 }
253 
254 int
255 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
256 {
257 	struct netmsg_pru_peeraddr msg;
258 	int error;
259 
260 	netmsg_init(&msg.base, so, &curthread->td_msgport,
261 		    0, so->so_proto->pr_usrreqs->pru_peeraddr);
262 	msg.nm_nam = nam;
263 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
264 	return (error);
265 }
266 
267 int
268 so_pru_rcvd(struct socket *so, int flags)
269 {
270 	struct netmsg_pru_rcvd msg;
271 	int error;
272 
273 	netmsg_init(&msg.base, so, &curthread->td_msgport,
274 		    0, so->so_proto->pr_usrreqs->pru_rcvd);
275 	msg.nm_flags = flags;
276 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
277 	return (error);
278 }
279 
280 int
281 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
282 {
283 	struct netmsg_pru_rcvoob msg;
284 	int error;
285 
286 	netmsg_init(&msg.base, so, &curthread->td_msgport,
287 		    0, so->so_proto->pr_usrreqs->pru_rcvoob);
288 	msg.nm_m = m;
289 	msg.nm_flags = flags;
290 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
291 	return (error);
292 }
293 
294 /*
295  * NOTE: If the target port changes the implied connect will deal with it.
296  */
297 int
298 so_pru_send(struct socket *so, int flags, struct mbuf *m,
299 	    struct sockaddr *addr, struct mbuf *control, struct thread *td)
300 {
301 	struct netmsg_pru_send msg;
302 	int error;
303 
304 	netmsg_init(&msg.base, so, &curthread->td_msgport,
305 		    0, so->so_proto->pr_usrreqs->pru_send);
306 	msg.nm_flags = flags;
307 	msg.nm_m = m;
308 	msg.nm_addr = addr;
309 	msg.nm_control = control;
310 	msg.nm_td = td;
311 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
312 	return (error);
313 }
314 
315 int
316 so_pru_sense(struct socket *so, struct stat *sb)
317 {
318 	struct netmsg_pru_sense msg;
319 	int error;
320 
321 	netmsg_init(&msg.base, so, &curthread->td_msgport,
322 		    0, so->so_proto->pr_usrreqs->pru_sense);
323 	msg.nm_stat = sb;
324 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
325 	return (error);
326 }
327 
328 int
329 so_pru_shutdown(struct socket *so)
330 {
331 	struct netmsg_pru_shutdown msg;
332 	int error;
333 
334 	netmsg_init(&msg.base, so, &curthread->td_msgport,
335 		    0, so->so_proto->pr_usrreqs->pru_shutdown);
336 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
337 	return (error);
338 }
339 
340 int
341 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
342 {
343 	struct netmsg_pru_sockaddr msg;
344 	int error;
345 
346 	netmsg_init(&msg.base, so, &curthread->td_msgport,
347 		    0, so->so_proto->pr_usrreqs->pru_sockaddr);
348 	msg.nm_nam = nam;
349 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
350 	return (error);
351 }
352 
353 int
354 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
355 {
356 	struct netmsg_pr_ctloutput msg;
357 	int error;
358 
359 	KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
360 	netmsg_init(&msg.base, so, &curthread->td_msgport,
361 		    0, so->so_proto->pr_ctloutput);
362 	msg.nm_sopt = sopt;
363 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
364 	return (error);
365 }
366 
367 /*
368  * Protocol control input, typically via icmp.
369  *
370  * If the protocol pr_ctlport is not NULL we call it to figure out the
371  * protocol port.  If NULL is returned we can just return, otherwise
372  * we issue a netmsg to call pr_ctlinput in the proper thread.
373  *
374  * This must be done synchronously as arg and/or extra may point to
375  * temporary data.
376  */
377 void
378 so_pru_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra)
379 {
380 	struct netmsg_pru_ctlinput msg;
381 	lwkt_port_t port;
382 
383 	if (pr->pr_ctlport == NULL)
384 		return;
385 	KKASSERT(pr->pr_ctlinput != NULL);
386 	port = pr->pr_ctlport(cmd, arg, extra);
387 	if (port == NULL)
388 		return;
389 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
390 		    0, pr->pr_ctlinput);
391 	msg.nm_cmd = cmd;
392 	msg.nm_arg = arg;
393 	msg.nm_extra = extra;
394 	lwkt_domsg(port, &msg.base.lmsg, 0);
395 }
396 
397 /*
398  * If we convert all the protosw pr_ functions for all the protocols
399  * to take a message directly, this layer can go away.  For the moment
400  * our dispatcher ignores the return value, but since we are handling
401  * the replymsg ourselves we return EASYNC by convention.
402  */
403 
404 /*
405  * Handle a predicate event request.  This function is only called once
406  * when the predicate message queueing request is received.
407  */
408 void
409 netmsg_so_notify(netmsg_t msg)
410 {
411 	struct signalsockbuf *ssb;
412 
413 	ssb = (msg->notify.nm_etype & NM_REVENT) ?
414 			&msg->base.nm_so->so_rcv :
415 			&msg->base.nm_so->so_snd;
416 
417 	/*
418 	 * Reply immediately if the event has occured, otherwise queue the
419 	 * request.
420 	 */
421 	if (msg->notify.nm_predicate(&msg->notify)) {
422 		lwkt_replymsg(&msg->base.lmsg,
423 			      msg->base.lmsg.ms_error);
424 	} else {
425 		lwkt_gettoken(&kq_token);
426 		TAILQ_INSERT_TAIL(&ssb->ssb_kq.ki_mlist, &msg->notify, nm_list);
427 		atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
428 		lwkt_reltoken(&kq_token);
429 	}
430 }
431 
432 /*
433  * Called by doio when trying to abort a netmsg_so_notify message.
434  * Unlike the other functions this one is dispatched directly by
435  * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
436  *
437  * The original message, lmsg, is under the control of the caller and
438  * will not be destroyed until we return so we can safely reference it
439  * in our synchronous abort request.
440  *
441  * This part of the abort request occurs on the originating cpu which
442  * means we may race the message flags and the original message may
443  * not even have been processed by the target cpu yet.
444  */
445 void
446 netmsg_so_notify_doabort(lwkt_msg_t lmsg)
447 {
448 	struct netmsg_so_notify_abort msg;
449 
450 	if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
451 		netmsg_init(&msg.base, NULL, &curthread->td_msgport,
452 			    0, netmsg_so_notify_abort);
453 		msg.nm_notifymsg = (void *)lmsg;
454 		lwkt_domsg(lmsg->ms_target_port, &msg.base.lmsg, 0);
455 	}
456 }
457 
458 /*
459  * Predicate requests can be aborted.  This function is only called once
460  * and will interlock against processing/reply races (since such races
461  * occur on the same thread that controls the port where the abort is
462  * requeued).
463  *
464  * This part of the abort request occurs on the target cpu.  The message
465  * flags must be tested again in case the test that we did on the
466  * originating cpu raced.  Since messages are handled in sequence, the
467  * original message will have already been handled by the loop and either
468  * replied to or queued.
469  *
470  * We really only need to interlock with MSGF_REPLY (a bit that is set on
471  * our cpu when we reply).  Note that MSGF_DONE is not set until the
472  * reply reaches the originating cpu.  Test both bits anyway.
473  */
474 void
475 netmsg_so_notify_abort(netmsg_t msg)
476 {
477 	struct netmsg_so_notify_abort *abrtmsg = &msg->notify_abort;
478 	struct netmsg_so_notify *nmsg = abrtmsg->nm_notifymsg;
479 	struct signalsockbuf *ssb;
480 
481 	/*
482 	 * The original notify message is not destroyed until after the
483 	 * abort request is returned, so we can check its state.
484 	 */
485 	if ((nmsg->base.lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
486 		ssb = (nmsg->nm_etype & NM_REVENT) ?
487 				&nmsg->base.nm_so->so_rcv :
488 				&nmsg->base.nm_so->so_snd;
489 		lwkt_gettoken(&kq_token);
490 		TAILQ_REMOVE(&ssb->ssb_kq.ki_mlist, nmsg, nm_list);
491 		lwkt_reltoken(&kq_token);
492 		lwkt_replymsg(&nmsg->base.lmsg, EINTR);
493 	}
494 
495 	/*
496 	 * Reply to the abort message
497 	 */
498 	lwkt_replymsg(&abrtmsg->base.lmsg, 0);
499 }
500