xref: /dragonfly/sys/kern/uipc_msg.c (revision 678e8cc6)
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 <sys/mbuf.h>
48 #include <vm/pmap.h>
49 #include <net/netmsg2.h>
50 
51 #include <net/netisr.h>
52 #include <net/netmsg.h>
53 
54 /*
55  * Abort a socket and free it.  Called from soabort() only.  soabort()
56  * got a ref on the socket which we must free on reply.
57  */
58 void
59 so_pru_abort(struct socket *so)
60 {
61 	struct netmsg_pru_abort msg;
62 
63 	netmsg_init(&msg.base, so, &curthread->td_msgport,
64 		    0, so->so_proto->pr_usrreqs->pru_abort);
65 	(void)lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
66 	sofree(msg.base.nm_so);
67 }
68 
69 /*
70  * Abort a socket and free it, asynchronously.  Called from
71  * soaborta() only.  soaborta() got a ref on the socket which we must
72  * free on reply.
73  */
74 void
75 so_pru_aborta(struct socket *so)
76 {
77 	struct netmsg_pru_abort *msg;
78 
79 	msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_ZERO);
80 	netmsg_init(&msg->base, so, &netisr_afree_free_so_rport,
81 		    0, so->so_proto->pr_usrreqs->pru_abort);
82 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
83 }
84 
85 /*
86  * Abort a socket and free it.  Called from soabort_oncpu() only.
87  * Caller must make sure that the current CPU is inpcb's owner CPU.
88  */
89 void
90 so_pru_abort_oncpu(struct socket *so)
91 {
92 	struct netmsg_pru_abort msg;
93 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_abort;
94 
95 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
96 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
97 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
98 	func((netmsg_t)&msg);
99 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
100 	sofree(msg.base.nm_so);
101 }
102 
103 int
104 so_pru_accept(struct socket *so, struct sockaddr **nam)
105 {
106 	struct netmsg_pru_accept msg;
107 
108 	netmsg_init(&msg.base, so, &curthread->td_msgport,
109 	    0, so->so_proto->pr_usrreqs->pru_accept);
110 	msg.nm_nam = nam;
111 
112 	return lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
113 }
114 
115 int
116 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
117 {
118 	struct netmsg_pru_attach msg;
119 	int error;
120 
121 	netmsg_init(&msg.base, so, &curthread->td_msgport,
122 		    0, so->so_proto->pr_usrreqs->pru_attach);
123 	msg.nm_proto = proto;
124 	msg.nm_ai = ai;
125 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
126 	return (error);
127 }
128 
129 int
130 so_pru_attach_direct(struct socket *so, int proto, struct pru_attach_info *ai)
131 {
132 	struct netmsg_pru_attach msg;
133 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_attach;
134 
135 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
136 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
137 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
138 	msg.nm_proto = proto;
139 	msg.nm_ai = ai;
140 	func((netmsg_t)&msg);
141 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
142 	return(msg.base.lmsg.ms_error);
143 }
144 
145 /*
146  * NOTE: If the target port changes the bind operation will deal with it.
147  */
148 int
149 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
150 {
151 	struct netmsg_pru_bind msg;
152 	int error;
153 
154 	netmsg_init(&msg.base, so, &curthread->td_msgport,
155 		    0, so->so_proto->pr_usrreqs->pru_bind);
156 	msg.nm_nam = nam;
157 	msg.nm_td = td;		/* used only for prison_ip() */
158 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
159 	return (error);
160 }
161 
162 int
163 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
164 {
165 	struct netmsg_pru_connect msg;
166 	int error;
167 
168 	netmsg_init(&msg.base, so, &curthread->td_msgport,
169 		    0, so->so_proto->pr_usrreqs->pru_connect);
170 	msg.nm_nam = nam;
171 	msg.nm_td = td;
172 	msg.nm_m = NULL;
173 	msg.nm_flags = 0;
174 	msg.nm_reconnect = 0;
175 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
176 	return (error);
177 }
178 
179 int
180 so_pru_connect2(struct socket *so1, struct socket *so2)
181 {
182 	struct netmsg_pru_connect2 msg;
183 	int error;
184 
185 	netmsg_init(&msg.base, so1, &curthread->td_msgport,
186 		    0, so1->so_proto->pr_usrreqs->pru_connect2);
187 	msg.nm_so1 = so1;
188 	msg.nm_so2 = so2;
189 	error = lwkt_domsg(so1->so_port, &msg.base.lmsg, 0);
190 	return (error);
191 }
192 
193 /*
194  * WARNING!  Synchronous call from user context.  Control function may do
195  *	     copyin/copyout.
196  */
197 int
198 so_pru_control_direct(struct socket *so, u_long cmd, caddr_t data,
199 		      struct ifnet *ifp)
200 {
201 	struct netmsg_pru_control msg;
202 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_control;
203 
204 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
205 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
206 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
207 	msg.nm_cmd = cmd;
208 	msg.nm_data = data;
209 	msg.nm_ifp = ifp;
210 	msg.nm_td = curthread;
211 	func((netmsg_t)&msg);
212 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
213 	return(msg.base.lmsg.ms_error);
214 }
215 
216 int
217 so_pru_detach(struct socket *so)
218 {
219 	struct netmsg_pru_detach msg;
220 	int error;
221 
222 	netmsg_init(&msg.base, so, &curthread->td_msgport,
223 		    0, so->so_proto->pr_usrreqs->pru_detach);
224 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
225 	return (error);
226 }
227 
228 void
229 so_pru_detach_direct(struct socket *so)
230 {
231 	struct netmsg_pru_detach msg;
232 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_detach;
233 
234 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
235 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
236 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
237 	func((netmsg_t)&msg);
238 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
239 }
240 
241 int
242 so_pru_disconnect(struct socket *so)
243 {
244 	struct netmsg_pru_disconnect msg;
245 	int error;
246 
247 	netmsg_init(&msg.base, so, &curthread->td_msgport,
248 		    0, so->so_proto->pr_usrreqs->pru_disconnect);
249 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
250 	return (error);
251 }
252 
253 void
254 so_pru_disconnect_direct(struct socket *so)
255 {
256 	struct netmsg_pru_disconnect msg;
257 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_disconnect;
258 
259 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
260 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
261 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
262 	func((netmsg_t)&msg);
263 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
264 }
265 
266 int
267 so_pru_listen(struct socket *so, struct thread *td)
268 {
269 	struct netmsg_pru_listen msg;
270 	int error;
271 
272 	netmsg_init(&msg.base, so, &curthread->td_msgport,
273 		    0, so->so_proto->pr_usrreqs->pru_listen);
274 	msg.nm_td = td;		/* used only for prison_ip() XXX JH */
275 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
276 	return (error);
277 }
278 
279 int
280 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
281 {
282 	struct netmsg_pru_peeraddr msg;
283 	int error;
284 
285 	netmsg_init(&msg.base, so, &curthread->td_msgport,
286 		    0, so->so_proto->pr_usrreqs->pru_peeraddr);
287 	msg.nm_nam = nam;
288 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
289 	return (error);
290 }
291 
292 int
293 so_pru_rcvd(struct socket *so, int flags)
294 {
295 	struct netmsg_pru_rcvd msg;
296 	int error;
297 
298 	netmsg_init(&msg.base, so, &curthread->td_msgport,
299 		    0, so->so_proto->pr_usrreqs->pru_rcvd);
300 	msg.nm_flags = flags;
301 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
302 	return (error);
303 }
304 
305 int
306 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
307 {
308 	struct netmsg_pru_rcvoob msg;
309 	int error;
310 
311 	netmsg_init(&msg.base, so, &curthread->td_msgport,
312 		    0, so->so_proto->pr_usrreqs->pru_rcvoob);
313 	msg.nm_m = m;
314 	msg.nm_flags = flags;
315 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
316 	return (error);
317 }
318 
319 /*
320  * NOTE: If the target port changes the implied connect will deal with it.
321  */
322 int
323 so_pru_send(struct socket *so, int flags, struct mbuf *m,
324 	    struct sockaddr *addr, struct mbuf *control, struct thread *td)
325 {
326 	struct netmsg_pru_send msg;
327 	int error;
328 
329 	netmsg_init(&msg.base, so, &curthread->td_msgport,
330 		    0, so->so_proto->pr_usrreqs->pru_send);
331 	msg.nm_flags = flags;
332 	msg.nm_m = m;
333 	msg.nm_addr = addr;
334 	msg.nm_control = control;
335 	msg.nm_td = td;
336 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
337 	return (error);
338 }
339 
340 void
341 so_pru_sync(struct socket *so)
342 {
343 	struct netmsg_base msg;
344 
345 	netmsg_init(&msg, so, &curthread->td_msgport, 0,
346 	    netmsg_sync_handler);
347 	lwkt_domsg(so->so_port, &msg.lmsg, 0);
348 }
349 
350 void
351 so_pru_send_async(struct socket *so, int flags, struct mbuf *m,
352 	    struct sockaddr *addr0, struct mbuf *control, struct thread *td)
353 {
354 	struct netmsg_pru_send *msg;
355 	struct sockaddr *addr = NULL;
356 
357 	KASSERT(so->so_proto->pr_flags & PR_ASYNC_SEND,
358 	    ("async pru_send is not supported\n"));
359 
360 	flags |= PRUS_NOREPLY;
361 	if (addr0 != NULL) {
362 		addr = kmalloc(addr0->sa_len, M_SONAME, M_WAITOK);
363 		memcpy(addr, addr0, addr0->sa_len);
364 		flags |= PRUS_FREEADDR;
365 	}
366 
367 	msg = &m->m_hdr.mh_sndmsg;
368 	netmsg_init(&msg->base, so, &netisr_apanic_rport,
369 		    0, so->so_proto->pr_usrreqs->pru_send);
370 	msg->nm_flags = flags;
371 	msg->nm_m = m;
372 	msg->nm_addr = addr;
373 	msg->nm_control = control;
374 	msg->nm_td = td;
375 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
376 }
377 
378 int
379 so_pru_sense(struct socket *so, struct stat *sb)
380 {
381 	struct netmsg_pru_sense msg;
382 	int error;
383 
384 	netmsg_init(&msg.base, so, &curthread->td_msgport,
385 		    0, so->so_proto->pr_usrreqs->pru_sense);
386 	msg.nm_stat = sb;
387 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
388 	return (error);
389 }
390 
391 int
392 so_pru_shutdown(struct socket *so)
393 {
394 	struct netmsg_pru_shutdown msg;
395 	int error;
396 
397 	netmsg_init(&msg.base, so, &curthread->td_msgport,
398 		    0, so->so_proto->pr_usrreqs->pru_shutdown);
399 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
400 	return (error);
401 }
402 
403 int
404 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
405 {
406 	struct netmsg_pru_sockaddr msg;
407 	int error;
408 
409 	netmsg_init(&msg.base, so, &curthread->td_msgport,
410 		    0, so->so_proto->pr_usrreqs->pru_sockaddr);
411 	msg.nm_nam = nam;
412 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
413 	return (error);
414 }
415 
416 int
417 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
418 {
419 	struct netmsg_pr_ctloutput msg;
420 	int error;
421 
422 	KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
423 	netmsg_init(&msg.base, so, &curthread->td_msgport,
424 		    0, so->so_proto->pr_ctloutput);
425 	msg.nm_sopt = sopt;
426 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
427 	return (error);
428 }
429 
430 /*
431  * Protocol control input, typically via icmp.
432  *
433  * If the protocol pr_ctlport is not NULL we call it to figure out the
434  * protocol port.  If NULL is returned we can just return, otherwise
435  * we issue a netmsg to call pr_ctlinput in the proper thread.
436  *
437  * This must be done synchronously as arg and/or extra may point to
438  * temporary data.
439  */
440 void
441 so_pru_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra)
442 {
443 	struct netmsg_pru_ctlinput msg;
444 	lwkt_port_t port;
445 
446 	if (pr->pr_ctlport == NULL)
447 		return;
448 	KKASSERT(pr->pr_ctlinput != NULL);
449 	port = pr->pr_ctlport(cmd, arg, extra);
450 	if (port == NULL)
451 		return;
452 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
453 		    0, pr->pr_ctlinput);
454 	msg.nm_cmd = cmd;
455 	msg.nm_arg = arg;
456 	msg.nm_extra = extra;
457 	lwkt_domsg(port, &msg.base.lmsg, 0);
458 }
459 
460 /*
461  * If we convert all the protosw pr_ functions for all the protocols
462  * to take a message directly, this layer can go away.  For the moment
463  * our dispatcher ignores the return value, but since we are handling
464  * the replymsg ourselves we return EASYNC by convention.
465  */
466 
467 /*
468  * Handle a predicate event request.  This function is only called once
469  * when the predicate message queueing request is received.
470  */
471 void
472 netmsg_so_notify(netmsg_t msg)
473 {
474 	struct lwkt_token *tok;
475 	struct signalsockbuf *ssb;
476 
477 	ssb = (msg->notify.nm_etype & NM_REVENT) ?
478 			&msg->base.nm_so->so_rcv :
479 			&msg->base.nm_so->so_snd;
480 
481 	/*
482 	 * Reply immediately if the event has occured, otherwise queue the
483 	 * request.
484 	 *
485 	 * NOTE: Socket can change if this is an accept predicate so cache
486 	 *	 the token.
487 	 */
488 	tok = lwkt_token_pool_lookup(msg->base.nm_so);
489 	lwkt_gettoken(tok);
490 	if (msg->notify.nm_predicate(&msg->notify)) {
491 		lwkt_reltoken(tok);
492 		lwkt_replymsg(&msg->base.lmsg,
493 			      msg->base.lmsg.ms_error);
494 	} else {
495 		TAILQ_INSERT_TAIL(&ssb->ssb_kq.ki_mlist, &msg->notify, nm_list);
496 		atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
497 		lwkt_reltoken(tok);
498 	}
499 }
500 
501 /*
502  * Called by doio when trying to abort a netmsg_so_notify message.
503  * Unlike the other functions this one is dispatched directly by
504  * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
505  *
506  * The original message, lmsg, is under the control of the caller and
507  * will not be destroyed until we return so we can safely reference it
508  * in our synchronous abort request.
509  *
510  * This part of the abort request occurs on the originating cpu which
511  * means we may race the message flags and the original message may
512  * not even have been processed by the target cpu yet.
513  */
514 void
515 netmsg_so_notify_doabort(lwkt_msg_t lmsg)
516 {
517 	struct netmsg_so_notify_abort msg;
518 
519 	if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
520 		netmsg_init(&msg.base, NULL, &curthread->td_msgport,
521 			    0, netmsg_so_notify_abort);
522 		msg.nm_notifymsg = (void *)lmsg;
523 		lwkt_domsg(lmsg->ms_target_port, &msg.base.lmsg, 0);
524 	}
525 }
526 
527 /*
528  * Predicate requests can be aborted.  This function is only called once
529  * and will interlock against processing/reply races (since such races
530  * occur on the same thread that controls the port where the abort is
531  * requeued).
532  *
533  * This part of the abort request occurs on the target cpu.  The message
534  * flags must be tested again in case the test that we did on the
535  * originating cpu raced.  Since messages are handled in sequence, the
536  * original message will have already been handled by the loop and either
537  * replied to or queued.
538  *
539  * We really only need to interlock with MSGF_REPLY (a bit that is set on
540  * our cpu when we reply).  Note that MSGF_DONE is not set until the
541  * reply reaches the originating cpu.  Test both bits anyway.
542  */
543 void
544 netmsg_so_notify_abort(netmsg_t msg)
545 {
546 	struct netmsg_so_notify_abort *abrtmsg = &msg->notify_abort;
547 	struct netmsg_so_notify *nmsg = abrtmsg->nm_notifymsg;
548 	struct signalsockbuf *ssb;
549 
550 	/*
551 	 * The original notify message is not destroyed until after the
552 	 * abort request is returned, so we can check its state.
553 	 */
554 	lwkt_getpooltoken(nmsg->base.nm_so);
555 	if ((nmsg->base.lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
556 		ssb = (nmsg->nm_etype & NM_REVENT) ?
557 				&nmsg->base.nm_so->so_rcv :
558 				&nmsg->base.nm_so->so_snd;
559 		TAILQ_REMOVE(&ssb->ssb_kq.ki_mlist, nmsg, nm_list);
560 		lwkt_relpooltoken(nmsg->base.nm_so);
561 		lwkt_replymsg(&nmsg->base.lmsg, EINTR);
562 	} else {
563 		lwkt_relpooltoken(nmsg->base.nm_so);
564 	}
565 
566 	/*
567 	 * Reply to the abort message
568 	 */
569 	lwkt_replymsg(&abrtmsg->base.lmsg, 0);
570 }
571