xref: /freebsd/sys/netlink/netlink_domain.c (revision ea03febed52855247691ded13be97125fbf984f3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Ng Peng Nam Sean
5  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6  * Copyright (c) 2023 Gleb Smirnoff <glebius@FreeBSD.org>
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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * This file contains socket and protocol bindings for netlink.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/lock.h>
38 #include <sys/rmlock.h>
39 #include <sys/domain.h>
40 #include <sys/jail.h>
41 #include <sys/mbuf.h>
42 #include <sys/osd.h>
43 #include <sys/protosw.h>
44 #include <sys/proc.h>
45 #include <sys/ck.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysent.h>
49 #include <sys/syslog.h>
50 #include <sys/priv.h>
51 #include <sys/uio.h>
52 
53 #include <netlink/netlink.h>
54 #include <netlink/netlink_ctl.h>
55 #include <netlink/netlink_var.h>
56 
57 #define	DEBUG_MOD_NAME	nl_domain
58 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
59 #include <netlink/netlink_debug.h>
60 _DECLARE_DEBUG(LOG_INFO);
61 
62 _Static_assert((NLP_MAX_GROUPS % 64) == 0,
63     "NLP_MAX_GROUPS has to be multiple of 64");
64 _Static_assert(NLP_MAX_GROUPS >= 64,
65     "NLP_MAX_GROUPS has to be at least 64");
66 
67 #define	NLCTL_TRACKER		struct rm_priotracker nl_tracker
68 #define	NLCTL_RLOCK()		rm_rlock(&V_nl_ctl.ctl_lock, &nl_tracker)
69 #define	NLCTL_RUNLOCK()		rm_runlock(&V_nl_ctl.ctl_lock, &nl_tracker)
70 #define	NLCTL_LOCK_ASSERT()	rm_assert(&V_nl_ctl.ctl_lock, RA_LOCKED)
71 
72 #define	NLCTL_WLOCK()		rm_wlock(&V_nl_ctl.ctl_lock)
73 #define	NLCTL_WUNLOCK()		rm_wunlock(&V_nl_ctl.ctl_lock)
74 #define	NLCTL_WLOCK_ASSERT()	rm_assert(&V_nl_ctl.ctl_lock, RA_WLOCKED)
75 
76 static u_long nl_sendspace = NLSNDQ;
77 SYSCTL_ULONG(_net_netlink, OID_AUTO, sendspace, CTLFLAG_RW, &nl_sendspace, 0,
78     "Default netlink socket send space");
79 
80 static u_long nl_recvspace = NLSNDQ;
81 SYSCTL_ULONG(_net_netlink, OID_AUTO, recvspace, CTLFLAG_RW, &nl_recvspace, 0,
82     "Default netlink socket receive space");
83 
84 extern u_long sb_max_adj;
85 static u_long nl_maxsockbuf = 512 * 1024 * 1024; /* 512M, XXX: init based on physmem */
86 static int sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS);
87 SYSCTL_OID(_net_netlink, OID_AUTO, nl_maxsockbuf,
88     CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, &nl_maxsockbuf, 0,
89     sysctl_handle_nl_maxsockbuf, "LU",
90     "Maximum Netlink socket buffer size");
91 
92 
93 static unsigned int osd_slot_id = 0;
94 
95 void
nl_osd_register(void)96 nl_osd_register(void)
97 {
98 	osd_slot_id = osd_register(OSD_THREAD, NULL, NULL);
99 }
100 
101 void
nl_osd_unregister(void)102 nl_osd_unregister(void)
103 {
104 	osd_deregister(OSD_THREAD, osd_slot_id);
105 }
106 
107 struct nlpcb *
_nl_get_thread_nlp(struct thread * td)108 _nl_get_thread_nlp(struct thread *td)
109 {
110 	return (osd_get(OSD_THREAD, &td->td_osd, osd_slot_id));
111 }
112 
113 void
nl_set_thread_nlp(struct thread * td,struct nlpcb * nlp)114 nl_set_thread_nlp(struct thread *td, struct nlpcb *nlp)
115 {
116 	NLP_LOG(LOG_DEBUG2, nlp, "Set thread %p nlp to %p (slot %u)", td, nlp, osd_slot_id);
117 	if (osd_set(OSD_THREAD, &td->td_osd, osd_slot_id, nlp) == 0)
118 		return;
119 	/* Failed, need to realloc */
120 	void **rsv = osd_reserve(osd_slot_id);
121 	osd_set_reserved(OSD_THREAD, &td->td_osd, osd_slot_id, rsv, nlp);
122 }
123 
124 /*
125  * Looks up a nlpcb struct based on the @portid. Need to claim nlsock_mtx.
126  * Returns nlpcb pointer if present else NULL
127  */
128 static struct nlpcb *
nl_port_lookup(uint32_t port_id)129 nl_port_lookup(uint32_t port_id)
130 {
131 	struct nlpcb *nlp;
132 
133 	CK_LIST_FOREACH(nlp, &V_nl_ctl.ctl_port_head, nl_port_next) {
134 		if (nlp->nl_port == port_id)
135 			return (nlp);
136 	}
137 	return (NULL);
138 }
139 
140 static void
nlp_join_group(struct nlpcb * nlp,unsigned int group_id)141 nlp_join_group(struct nlpcb *nlp, unsigned int group_id)
142 {
143 	MPASS(group_id < NLP_MAX_GROUPS);
144 	NLCTL_WLOCK_ASSERT();
145 
146 	/* TODO: add family handler callback */
147 	if (!nlp_unconstrained_vnet(nlp))
148 		return;
149 
150 	BIT_SET(NLP_MAX_GROUPS, group_id, &nlp->nl_groups);
151 }
152 
153 static void
nlp_leave_group(struct nlpcb * nlp,unsigned int group_id)154 nlp_leave_group(struct nlpcb *nlp, unsigned int group_id)
155 {
156 	MPASS(group_id < NLP_MAX_GROUPS);
157 	NLCTL_WLOCK_ASSERT();
158 
159 	BIT_CLR(NLP_MAX_GROUPS, group_id, &nlp->nl_groups);
160 }
161 
162 static bool
nlp_memberof_group(struct nlpcb * nlp,unsigned int group_id)163 nlp_memberof_group(struct nlpcb *nlp, unsigned int group_id)
164 {
165 	MPASS(group_id < NLP_MAX_GROUPS);
166 	NLCTL_LOCK_ASSERT();
167 
168 	return (BIT_ISSET(NLP_MAX_GROUPS, group_id, &nlp->nl_groups));
169 }
170 
171 static uint32_t
nlp_get_groups_compat(struct nlpcb * nlp)172 nlp_get_groups_compat(struct nlpcb *nlp)
173 {
174 	uint32_t groups_mask = 0;
175 
176 	NLCTL_LOCK_ASSERT();
177 
178 	for (int i = 0; i < 32; i++) {
179 		if (nlp_memberof_group(nlp, i + 1))
180 			groups_mask |= (1 << i);
181 	}
182 
183 	return (groups_mask);
184 }
185 
186 static struct nl_buf *
nl_buf_copy(struct nl_buf * nb)187 nl_buf_copy(struct nl_buf *nb)
188 {
189 	struct nl_buf *copy;
190 
191 	copy = nl_buf_alloc(nb->buflen, M_NOWAIT);
192 	if (__predict_false(copy == NULL))
193 		return (NULL);
194 	memcpy(copy, nb, sizeof(*nb) + nb->buflen);
195 
196 	return (copy);
197 }
198 
199 /*
200  * Broadcasts in the writer's buffer.
201  */
202 bool
nl_send_group(struct nl_writer * nw)203 nl_send_group(struct nl_writer *nw)
204 {
205 	struct nl_buf *nb = nw->buf;
206 	struct nlpcb *nlp_last = NULL;
207 	struct nlpcb *nlp;
208 	NLCTL_TRACKER;
209 
210 	IF_DEBUG_LEVEL(LOG_DEBUG2) {
211 		struct nlmsghdr *hdr = (struct nlmsghdr *)nb->data;
212 		NL_LOG(LOG_DEBUG2, "MCAST len %u msg type %d len %u to group %d/%d",
213 		    nb->datalen, hdr->nlmsg_type, hdr->nlmsg_len,
214 		    nw->group.proto, nw->group.id);
215 	}
216 
217 	nw->buf = NULL;
218 
219 	NLCTL_RLOCK();
220 	CK_LIST_FOREACH(nlp, &V_nl_ctl.ctl_pcb_head, nl_next) {
221 		if ((nw->group.priv == 0 || priv_check_cred(
222 		    nlp->nl_socket->so_cred, nw->group.priv) == 0) &&
223 		    nlp->nl_proto == nw->group.proto &&
224 		    nlp_memberof_group(nlp, nw->group.id)) {
225 			if (nlp_last != NULL) {
226 				struct nl_buf *copy;
227 
228 				copy = nl_buf_copy(nb);
229 				if (copy != NULL) {
230 					nw->buf = copy;
231 					(void)nl_send(nw, nlp_last);
232 				} else {
233 					NLP_LOCK(nlp_last);
234 					if (nlp_last->nl_socket != NULL)
235 						sorwakeup(nlp_last->nl_socket);
236 					NLP_UNLOCK(nlp_last);
237 				}
238 			}
239 			nlp_last = nlp;
240 		}
241 	}
242 	if (nlp_last != NULL) {
243 		nw->buf = nb;
244 		(void)nl_send(nw, nlp_last);
245 	} else
246 		nl_buf_free(nb);
247 
248 	NLCTL_RUNLOCK();
249 
250 	return (true);
251 }
252 
253 void
nl_clear_group(u_int group)254 nl_clear_group(u_int group)
255 {
256 	struct nlpcb *nlp;
257 
258 	NLCTL_WLOCK();
259 	CK_LIST_FOREACH(nlp, &V_nl_ctl.ctl_pcb_head, nl_next)
260 		if (nlp_memberof_group(nlp, group))
261 			nlp_leave_group(nlp, group);
262 	NLCTL_WUNLOCK();
263 }
264 
265 static uint32_t
nl_find_port(void)266 nl_find_port(void)
267 {
268 	/*
269 	 * app can open multiple netlink sockets.
270 	 * Start with current pid, if already taken,
271 	 * try random numbers in 65k..256k+65k space,
272 	 * avoiding clash with pids.
273 	 */
274 	if (nl_port_lookup(curproc->p_pid) == NULL)
275 		return (curproc->p_pid);
276 	for (int i = 0; i < 16; i++) {
277 		uint32_t nl_port = (arc4random() % 65536) + 65536 * 4;
278 		if (nl_port_lookup(nl_port) == 0)
279 			return (nl_port);
280 		NL_LOG(LOG_DEBUG3, "tried %u\n", nl_port);
281 	}
282 	return (curproc->p_pid);
283 }
284 
285 static int
nl_bind_locked(struct nlpcb * nlp,struct sockaddr_nl * snl)286 nl_bind_locked(struct nlpcb *nlp, struct sockaddr_nl *snl)
287 {
288 	if (nlp->nl_bound) {
289 		if (nlp->nl_port != snl->nl_pid) {
290 			NL_LOG(LOG_DEBUG,
291 			    "bind() failed: program pid %d "
292 			    "is different from provided pid %d",
293 			    nlp->nl_port, snl->nl_pid);
294 			return (EINVAL); // XXX: better error
295 		}
296 	} else {
297 		if (snl->nl_pid == 0)
298 			snl->nl_pid = nl_find_port();
299 		if (nl_port_lookup(snl->nl_pid) != NULL)
300 			return (EADDRINUSE);
301 		nlp->nl_port = snl->nl_pid;
302 		nlp->nl_bound = true;
303 		CK_LIST_INSERT_HEAD(&V_nl_ctl.ctl_port_head, nlp, nl_port_next);
304 	}
305 	for (int i = 0; i < 32; i++) {
306 		if (snl->nl_groups & ((uint32_t)1 << i))
307 			nlp_join_group(nlp, i + 1);
308 		else
309 			nlp_leave_group(nlp, i + 1);
310 	}
311 
312 	return (0);
313 }
314 
315 static int
nl_attach(struct socket * so,int proto,struct thread * td)316 nl_attach(struct socket *so, int proto, struct thread *td)
317 {
318 	struct nlpcb *nlp;
319 	int error;
320 
321 	if (__predict_false(netlink_unloading != 0))
322 		return (EAFNOSUPPORT);
323 
324 	error = nl_verify_proto(proto);
325 	if (error != 0)
326 		return (error);
327 
328 	bool is_linux = SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX;
329 	NL_LOG(LOG_DEBUG2, "socket %p, %sPID %d: attaching socket to %s",
330 	    so, is_linux ? "(linux) " : "", curproc->p_pid,
331 	    nl_get_proto_name(proto));
332 
333 	nlp = malloc(sizeof(struct nlpcb), M_PCB, M_WAITOK | M_ZERO);
334 	error = soreserve(so, nl_sendspace, nl_recvspace);
335 	if (error != 0) {
336 		free(nlp, M_PCB);
337 		return (error);
338 	}
339 	TAILQ_INIT(&so->so_rcv.nl_queue);
340 	TAILQ_INIT(&so->so_snd.nl_queue);
341 	so->so_pcb = nlp;
342 	nlp->nl_socket = so;
343 	nlp->nl_proto = proto;
344 	nlp->nl_process_id = curproc->p_pid;
345 	nlp->nl_linux = is_linux;
346 	nlp->nl_unconstrained_vnet = !jailed_without_vnet(so->so_cred);
347 	nlp->nl_need_thread_setup = true;
348 	NLP_LOCK_INIT(nlp);
349 	refcount_init(&nlp->nl_refcount, 1);
350 
351 	nlp->nl_taskqueue = taskqueue_create("netlink_socket", M_WAITOK,
352 	    taskqueue_thread_enqueue, &nlp->nl_taskqueue);
353 	TASK_INIT(&nlp->nl_task, 0, nl_taskqueue_handler, nlp);
354 	taskqueue_start_threads(&nlp->nl_taskqueue, 1, PWAIT,
355 	    "netlink_socket (PID %u)", nlp->nl_process_id);
356 
357 	NLCTL_WLOCK();
358 	CK_LIST_INSERT_HEAD(&V_nl_ctl.ctl_pcb_head, nlp, nl_next);
359 	NLCTL_WUNLOCK();
360 
361 	soisconnected(so);
362 
363 	return (0);
364 }
365 
366 static int
nl_bind(struct socket * so,struct sockaddr * sa,struct thread * td)367 nl_bind(struct socket *so, struct sockaddr *sa, struct thread *td)
368 {
369 	struct nlpcb *nlp = sotonlpcb(so);
370 	struct sockaddr_nl *snl = (struct sockaddr_nl *)sa;
371 	int error;
372 
373 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
374 	if (snl->nl_len != sizeof(*snl)) {
375 		NL_LOG(LOG_DEBUG, "socket %p, wrong sizeof(), ignoring bind()", so);
376 		return (EINVAL);
377 	}
378 
379 
380 	NLCTL_WLOCK();
381 	NLP_LOCK(nlp);
382 	error = nl_bind_locked(nlp, snl);
383 	NLP_UNLOCK(nlp);
384 	NLCTL_WUNLOCK();
385 	NL_LOG(LOG_DEBUG2, "socket %p, bind() to %u, groups %u, error %d", so,
386 	    snl->nl_pid, snl->nl_groups, error);
387 
388 	return (error);
389 }
390 
391 
392 static int
nl_assign_port(struct nlpcb * nlp,uint32_t port_id)393 nl_assign_port(struct nlpcb *nlp, uint32_t port_id)
394 {
395 	struct sockaddr_nl snl = {
396 		.nl_pid = port_id,
397 	};
398 	int error;
399 
400 	NLCTL_WLOCK();
401 	NLP_LOCK(nlp);
402 	snl.nl_groups = nlp_get_groups_compat(nlp);
403 	error = nl_bind_locked(nlp, &snl);
404 	NLP_UNLOCK(nlp);
405 	NLCTL_WUNLOCK();
406 
407 	NL_LOG(LOG_DEBUG3, "socket %p, port assign: %d, error: %d", nlp->nl_socket, port_id, error);
408 	return (error);
409 }
410 
411 /*
412  * nl_autobind_port binds a unused portid to @nlp
413  * @nlp: pcb data for the netlink socket
414  * @candidate_id: first id to consider
415  */
416 static int
nl_autobind_port(struct nlpcb * nlp,uint32_t candidate_id)417 nl_autobind_port(struct nlpcb *nlp, uint32_t candidate_id)
418 {
419 	uint32_t port_id = candidate_id;
420 	NLCTL_TRACKER;
421 	bool exist;
422 	int error = EADDRINUSE;
423 
424 	for (int i = 0; i < 10; i++) {
425 		NL_LOG(LOG_DEBUG3, "socket %p, trying to assign port %d", nlp->nl_socket, port_id);
426 		NLCTL_RLOCK();
427 		exist = nl_port_lookup(port_id) != 0;
428 		NLCTL_RUNLOCK();
429 		if (!exist) {
430 			error = nl_assign_port(nlp, port_id);
431 			if (error != EADDRINUSE)
432 				break;
433 		}
434 		port_id++;
435 	}
436 	NL_LOG(LOG_DEBUG3, "socket %p, autobind to %d, error: %d", nlp->nl_socket, port_id, error);
437 	return (error);
438 }
439 
440 static int
nl_connect(struct socket * so,struct sockaddr * sa,struct thread * td)441 nl_connect(struct socket *so, struct sockaddr *sa, struct thread *td)
442 {
443 	struct sockaddr_nl *snl = (struct sockaddr_nl *)sa;
444 	struct nlpcb *nlp;
445 
446 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
447 	if (snl->nl_len != sizeof(*snl)) {
448 		NL_LOG(LOG_DEBUG, "socket %p, wrong sizeof(), ignoring bind()", so);
449 		return (EINVAL);
450 	}
451 
452 	nlp = sotonlpcb(so);
453 	if (!nlp->nl_bound) {
454 		int error = nl_autobind_port(nlp, td->td_proc->p_pid);
455 		if (error != 0) {
456 			NL_LOG(LOG_DEBUG, "socket %p, nl_autobind() failed: %d", so, error);
457 			return (error);
458 		}
459 	}
460 	/* XXX: Handle socket flags & multicast */
461 	soisconnected(so);
462 
463 	NL_LOG(LOG_DEBUG2, "socket %p, connect to %u", so, snl->nl_pid);
464 
465 	return (0);
466 }
467 
468 static void
destroy_nlpcb_epoch(epoch_context_t ctx)469 destroy_nlpcb_epoch(epoch_context_t ctx)
470 {
471 	struct nlpcb *nlp;
472 
473 	nlp = __containerof(ctx, struct nlpcb, nl_epoch_ctx);
474 
475 	NLP_LOCK_DESTROY(nlp);
476 	free(nlp, M_PCB);
477 }
478 
479 static void
nl_close(struct socket * so)480 nl_close(struct socket *so)
481 {
482 	MPASS(sotonlpcb(so) != NULL);
483 	struct nlpcb *nlp;
484 	struct nl_buf *nb;
485 
486 	NL_LOG(LOG_DEBUG2, "detaching socket %p, PID %d", so, curproc->p_pid);
487 	nlp = sotonlpcb(so);
488 
489 	/* Mark as inactive so no new work can be enqueued */
490 	NLP_LOCK(nlp);
491 	bool was_bound = nlp->nl_bound;
492 	NLP_UNLOCK(nlp);
493 
494 	/* Wait till all scheduled work has been completed  */
495 	taskqueue_drain_all(nlp->nl_taskqueue);
496 	taskqueue_free(nlp->nl_taskqueue);
497 
498 	NLCTL_WLOCK();
499 	NLP_LOCK(nlp);
500 	if (was_bound) {
501 		CK_LIST_REMOVE(nlp, nl_port_next);
502 		NL_LOG(LOG_DEBUG3, "socket %p, unlinking bound pid %u", so, nlp->nl_port);
503 	}
504 	CK_LIST_REMOVE(nlp, nl_next);
505 	nlp->nl_socket = NULL;
506 	NLP_UNLOCK(nlp);
507 	NLCTL_WUNLOCK();
508 
509 	so->so_pcb = NULL;
510 
511 	while ((nb = TAILQ_FIRST(&so->so_snd.nl_queue)) != NULL) {
512 		TAILQ_REMOVE(&so->so_snd.nl_queue, nb, tailq);
513 		nl_buf_free(nb);
514 	}
515 	while ((nb = TAILQ_FIRST(&so->so_rcv.nl_queue)) != NULL) {
516 		TAILQ_REMOVE(&so->so_rcv.nl_queue, nb, tailq);
517 		nl_buf_free(nb);
518 	}
519 
520 	NL_LOG(LOG_DEBUG3, "socket %p, detached", so);
521 
522 	/* XXX: is delayed free needed? */
523 	NET_EPOCH_CALL(destroy_nlpcb_epoch, &nlp->nl_epoch_ctx);
524 }
525 
526 static int
nl_disconnect(struct socket * so)527 nl_disconnect(struct socket *so)
528 {
529 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
530 	MPASS(sotonlpcb(so) != NULL);
531 	return (ENOTCONN);
532 }
533 
534 static int
nl_sockaddr(struct socket * so,struct sockaddr * sa)535 nl_sockaddr(struct socket *so, struct sockaddr *sa)
536 {
537 
538 	*(struct sockaddr_nl *)sa = (struct sockaddr_nl ){
539 		/* TODO: set other fields */
540 		.nl_len = sizeof(struct sockaddr_nl),
541 		.nl_family = AF_NETLINK,
542 		.nl_pid = sotonlpcb(so)->nl_port,
543 	};
544 
545 	return (0);
546 }
547 
548 static int
nl_sosend(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * m,struct mbuf * control,int flags,struct thread * td)549 nl_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
550     struct mbuf *m, struct mbuf *control, int flags, struct thread *td)
551 {
552 	struct nlpcb *nlp = sotonlpcb(so);
553 	struct sockbuf *sb = &so->so_snd;
554 	struct nl_buf *nb;
555 	size_t len;
556 	int error;
557 
558 	MPASS(m == NULL && uio != NULL);
559 
560 	if (__predict_false(control != NULL)) {
561 		m_freem(control);
562 		return (EINVAL);
563 	}
564 
565 	if (__predict_false(flags & MSG_OOB))	/* XXXGL: or just ignore? */
566 		return (EOPNOTSUPP);
567 
568 	if (__predict_false(uio->uio_resid < sizeof(struct nlmsghdr)))
569 		return (ENOBUFS);		/* XXXGL: any better error? */
570 
571 	error = SOCK_IO_SEND_LOCK(so, SBLOCKWAIT(flags));
572 	if (error)
573 		return (error);
574 
575 	len = roundup2(uio->uio_resid, 8) + SCRATCH_BUFFER_SIZE;
576 	if (nlp->nl_linux)
577 		len += roundup2(uio->uio_resid, 8);
578 	nb = nl_buf_alloc(len, M_WAITOK);
579 	nb->datalen = uio->uio_resid;
580 	error = uiomove(&nb->data[0], uio->uio_resid, uio);
581 	if (__predict_false(error))
582 		goto out;
583 
584         NL_LOG(LOG_DEBUG2, "sending message to kernel %u bytes", nb->datalen);
585 
586 	SOCK_SENDBUF_LOCK(so);
587 restart:
588 	if (sb->sb_hiwat - sb->sb_ccc >= nb->datalen) {
589 		TAILQ_INSERT_TAIL(&sb->nl_queue, nb, tailq);
590 		sb->sb_acc += nb->datalen;
591 		sb->sb_ccc += nb->datalen;
592 		nb = NULL;
593 	} else if ((so->so_state & SS_NBIO) ||
594 	    (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
595 		SOCK_SENDBUF_UNLOCK(so);
596 		error = EWOULDBLOCK;
597 		goto out;
598 	} else {
599 		if ((error = sbwait(so, SO_SND)) != 0) {
600 			SOCK_SENDBUF_UNLOCK(so);
601 			goto out;
602 		} else
603 			goto restart;
604 	}
605 	SOCK_SENDBUF_UNLOCK(so);
606 
607 	if (nb == NULL) {
608 		NL_LOG(LOG_DEBUG3, "success");
609 		NLP_LOCK(nlp);
610 		nl_schedule_taskqueue(nlp);
611 		NLP_UNLOCK(nlp);
612 	}
613 
614 out:
615 	SOCK_IO_SEND_UNLOCK(so);
616 	if (nb != NULL) {
617 		NL_LOG(LOG_DEBUG3, "failure, error %d", error);
618 		nl_buf_free(nb);
619 	}
620 	return (error);
621 }
622 
623 /* Create control data for recvmsg(2) on Netlink socket. */
624 static struct mbuf *
nl_createcontrol(struct nlpcb * nlp)625 nl_createcontrol(struct nlpcb *nlp)
626 {
627 	struct {
628 		struct nlattr nla;
629 		uint32_t val;
630 	} data[] = {
631 		{
632 			.nla.nla_len = sizeof(struct nlattr) + sizeof(uint32_t),
633 			.nla.nla_type = NLMSGINFO_ATTR_PROCESS_ID,
634 			.val = nlp->nl_process_id,
635 		},
636 		{
637 			.nla.nla_len = sizeof(struct nlattr) + sizeof(uint32_t),
638 			.nla.nla_type = NLMSGINFO_ATTR_PORT_ID,
639 			.val = nlp->nl_port,
640 		},
641 	};
642 
643 	return (sbcreatecontrol(data, sizeof(data), NETLINK_MSG_INFO,
644 	    SOL_NETLINK, M_WAITOK));
645 }
646 
647 static int
nl_soreceive(struct socket * so,struct sockaddr ** psa,struct uio * uio,struct mbuf ** mp,struct mbuf ** controlp,int * flagsp)648 nl_soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
649     struct mbuf **mp, struct mbuf **controlp, int *flagsp)
650 {
651 	static const struct sockaddr_nl nl_empty_src = {
652 		.nl_len = sizeof(struct sockaddr_nl),
653 		.nl_family = PF_NETLINK,
654 		.nl_pid = 0 /* comes from the kernel */
655 	};
656 	struct sockbuf *sb = &so->so_rcv;
657 	struct nlpcb *nlp = sotonlpcb(so);
658 	struct nl_buf *first, *last, *nb, *next;
659 	struct nlmsghdr *hdr;
660 	int flags, error;
661 	u_int len, overflow, partoff, partlen, msgrcv, datalen;
662 	bool nonblock, trunc, peek;
663 
664 	MPASS(mp == NULL && uio != NULL);
665 
666 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
667 
668 	if (psa != NULL)
669 		*psa = sodupsockaddr((const struct sockaddr *)&nl_empty_src,
670 		    M_WAITOK);
671 
672 	if (controlp != NULL && (nlp->nl_flags & NLF_MSG_INFO))
673 		*controlp = nl_createcontrol(nlp);
674 
675 	flags = flagsp != NULL ? *flagsp & ~MSG_TRUNC : 0;
676 	trunc = flagsp != NULL ? *flagsp & MSG_TRUNC : false;
677 	nonblock = (so->so_state & SS_NBIO) ||
678 	    (flags & (MSG_DONTWAIT | MSG_NBIO));
679 	peek = flags & MSG_PEEK;
680 
681 	error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags));
682 	if (__predict_false(error))
683 		return (error);
684 
685 	len = 0;
686 	overflow = 0;
687 	msgrcv = 0;
688 	datalen = 0;
689 
690 	SOCK_RECVBUF_LOCK(so);
691 	while ((first = TAILQ_FIRST(&sb->nl_queue)) == NULL) {
692 		if (nonblock) {
693 			SOCK_RECVBUF_UNLOCK(so);
694 			SOCK_IO_RECV_UNLOCK(so);
695 			return (EWOULDBLOCK);
696 		}
697 		error = sbwait(so, SO_RCV);
698 		if (error) {
699 			SOCK_RECVBUF_UNLOCK(so);
700 			SOCK_IO_RECV_UNLOCK(so);
701 			return (error);
702 		}
703 	}
704 
705 	/*
706 	 * Netlink socket buffer consists of a queue of nl_bufs, but for the
707 	 * userland there should be no boundaries.  However, there are Netlink
708 	 * messages, that shouldn't be split.  Internal invariant is that a
709 	 * message never spans two nl_bufs.
710 	 * If a large userland buffer is provided, we would traverse the queue
711 	 * until either queue end is reached or the buffer is fulfilled.  If
712 	 * an application provides a buffer that isn't able to fit a single
713 	 * message, we would truncate it and lose its tail.  This is the only
714 	 * condition where we would lose data.  If buffer is able to fit at
715 	 * least one message, we would return it and won't truncate the next.
716 	 *
717 	 * We use same code for normal and MSG_PEEK case.  At first queue pass
718 	 * we scan nl_bufs and count lenght.  In case we can read entire buffer
719 	 * at one write everything is trivial.  In case we can not, we save
720 	 * pointer to the last (or partial) nl_buf and in the !peek case we
721 	 * split the queue into two pieces.  We can safely drop the queue lock,
722 	 * as kernel would only append nl_bufs to the end of the queue, and
723 	 * we are the exclusive owner of queue beginning due to sleepable lock.
724 	 * At the second pass we copy data out and in !peek case free nl_bufs.
725 	 */
726 	TAILQ_FOREACH(nb, &sb->nl_queue, tailq) {
727 		u_int offset;
728 
729 		MPASS(nb->offset < nb->datalen);
730 		offset = nb->offset;
731 		while (offset < nb->datalen) {
732 			hdr = (struct nlmsghdr *)&nb->data[offset];
733 			MPASS(nb->offset + hdr->nlmsg_len <= nb->datalen);
734 			if (uio->uio_resid < len + hdr->nlmsg_len) {
735 				overflow = len + hdr->nlmsg_len -
736 				    uio->uio_resid;
737 				partoff = nb->offset;
738 				if (offset > partoff) {
739 					partlen = offset - partoff;
740 					if (!peek) {
741 						nb->offset = offset;
742 						datalen += partlen;
743 					}
744 				} else if (len == 0 && uio->uio_resid > 0) {
745 					flags |= MSG_TRUNC;
746 					partlen = uio->uio_resid;
747 					if (peek)
748 						goto nospace;
749 					datalen += hdr->nlmsg_len;
750 					if (nb->offset + hdr->nlmsg_len ==
751 					    nb->datalen) {
752 						/*
753 						 * Avoid leaving empty nb.
754 						 * Process last nb normally.
755 						 * Trust uiomove() to care
756 						 * about negative uio_resid.
757 						 */
758 						nb = TAILQ_NEXT(nb, tailq);
759 						overflow = 0;
760 						partlen = 0;
761 					} else
762 						nb->offset += hdr->nlmsg_len;
763 					msgrcv++;
764 				} else
765 					partlen = 0;
766 				goto nospace;
767 			}
768 			len += hdr->nlmsg_len;
769 			offset += hdr->nlmsg_len;
770 			MPASS(offset <= nb->buflen);
771 			msgrcv++;
772 		}
773 		MPASS(offset == nb->datalen);
774 		datalen += nb->datalen - nb->offset;
775 	}
776 nospace:
777 	last = nb;
778 	if (!peek) {
779 		if (last == NULL)
780 			TAILQ_INIT(&sb->nl_queue);
781 		else {
782 			/* XXXGL: create TAILQ_SPLIT */
783 			TAILQ_FIRST(&sb->nl_queue) = last;
784 			last->tailq.tqe_prev = &TAILQ_FIRST(&sb->nl_queue);
785 		}
786 		MPASS(sb->sb_acc >= datalen);
787 		sb->sb_acc -= datalen;
788 		sb->sb_ccc -= datalen;
789 	}
790 	SOCK_RECVBUF_UNLOCK(so);
791 
792 	for (nb = first; nb != last; nb = next) {
793 		next = TAILQ_NEXT(nb, tailq);
794 		if (__predict_true(error == 0))
795 			error = uiomove(&nb->data[nb->offset],
796 			    (int)(nb->datalen - nb->offset), uio);
797 		if (!peek)
798 			nl_buf_free(nb);
799 	}
800 	if (last != NULL && partlen > 0 && __predict_true(error == 0))
801 		error = uiomove(&nb->data[partoff], (int)partlen, uio);
802 
803 	if (trunc && overflow > 0) {
804 		uio->uio_resid -= overflow;
805 		MPASS(uio->uio_resid < 0);
806 	} else
807 		MPASS(uio->uio_resid >= 0);
808 
809 	if (uio->uio_td)
810 		uio->uio_td->td_ru.ru_msgrcv += msgrcv;
811 
812 	if (flagsp != NULL)
813 		*flagsp |= flags;
814 
815 	SOCK_IO_RECV_UNLOCK(so);
816 
817 	nl_on_transmit(sotonlpcb(so));
818 
819 	return (error);
820 }
821 
822 static int
nl_getoptflag(int sopt_name)823 nl_getoptflag(int sopt_name)
824 {
825 	switch (sopt_name) {
826 	case NETLINK_CAP_ACK:
827 		return (NLF_CAP_ACK);
828 	case NETLINK_EXT_ACK:
829 		return (NLF_EXT_ACK);
830 	case NETLINK_GET_STRICT_CHK:
831 		return (NLF_STRICT);
832 	case NETLINK_MSG_INFO:
833 		return (NLF_MSG_INFO);
834 	}
835 
836 	return (0);
837 }
838 
839 static int
nl_ctloutput(struct socket * so,struct sockopt * sopt)840 nl_ctloutput(struct socket *so, struct sockopt *sopt)
841 {
842 	struct nlpcb *nlp = sotonlpcb(so);
843 	uint32_t flag;
844 	int optval, error = 0;
845 	NLCTL_TRACKER;
846 
847 	NL_LOG(LOG_DEBUG2, "%ssockopt(%p, %d)", (sopt->sopt_dir) ? "set" : "get",
848 	    so, sopt->sopt_name);
849 
850 	switch (sopt->sopt_dir) {
851 	case SOPT_SET:
852 		switch (sopt->sopt_name) {
853 		case NETLINK_ADD_MEMBERSHIP:
854 		case NETLINK_DROP_MEMBERSHIP:
855 			error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
856 			if (error != 0)
857 				break;
858 			if (optval <= 0 || optval >= NLP_MAX_GROUPS) {
859 				error = ERANGE;
860 				break;
861 			}
862 			NL_LOG(LOG_DEBUG2, "ADD/DEL group %d", (uint32_t)optval);
863 
864 			NLCTL_WLOCK();
865 			if (sopt->sopt_name == NETLINK_ADD_MEMBERSHIP)
866 				nlp_join_group(nlp, optval);
867 			else
868 				nlp_leave_group(nlp, optval);
869 			NLCTL_WUNLOCK();
870 			break;
871 		case NETLINK_CAP_ACK:
872 		case NETLINK_EXT_ACK:
873 		case NETLINK_GET_STRICT_CHK:
874 		case NETLINK_MSG_INFO:
875 			error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
876 			if (error != 0)
877 				break;
878 
879 			flag = nl_getoptflag(sopt->sopt_name);
880 
881 			if ((flag == NLF_MSG_INFO) && nlp->nl_linux) {
882 				error = EINVAL;
883 				break;
884 			}
885 
886 			NLCTL_WLOCK();
887 			if (optval != 0)
888 				nlp->nl_flags |= flag;
889 			else
890 				nlp->nl_flags &= ~flag;
891 			NLCTL_WUNLOCK();
892 			break;
893 		default:
894 			error = ENOPROTOOPT;
895 		}
896 		break;
897 	case SOPT_GET:
898 		switch (sopt->sopt_name) {
899 		case NETLINK_LIST_MEMBERSHIPS:
900 			NLCTL_RLOCK();
901 			optval = nlp_get_groups_compat(nlp);
902 			NLCTL_RUNLOCK();
903 			error = sooptcopyout(sopt, &optval, sizeof(optval));
904 			break;
905 		case NETLINK_CAP_ACK:
906 		case NETLINK_EXT_ACK:
907 		case NETLINK_GET_STRICT_CHK:
908 		case NETLINK_MSG_INFO:
909 			NLCTL_RLOCK();
910 			optval = (nlp->nl_flags & nl_getoptflag(sopt->sopt_name)) != 0;
911 			NLCTL_RUNLOCK();
912 			error = sooptcopyout(sopt, &optval, sizeof(optval));
913 			break;
914 		default:
915 			error = ENOPROTOOPT;
916 		}
917 		break;
918 	default:
919 		error = ENOPROTOOPT;
920 	}
921 
922 	return (error);
923 }
924 
925 static int
sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS)926 sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS)
927 {
928 	int error = 0;
929 	u_long tmp_maxsockbuf = nl_maxsockbuf;
930 
931 	error = sysctl_handle_long(oidp, &tmp_maxsockbuf, arg2, req);
932 	if (error || !req->newptr)
933 		return (error);
934 	if (tmp_maxsockbuf < MSIZE + MCLBYTES)
935 		return (EINVAL);
936 	nl_maxsockbuf = tmp_maxsockbuf;
937 
938 	return (0);
939 }
940 
941 static int
nl_setsbopt(struct socket * so,struct sockopt * sopt)942 nl_setsbopt(struct socket *so, struct sockopt *sopt)
943 {
944 	int error, optval;
945 	bool result;
946 
947 	if (sopt->sopt_name != SO_RCVBUF)
948 		return (sbsetopt(so, sopt));
949 
950 	/* Allow to override max buffer size in certain conditions */
951 
952 	error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
953 	if (error != 0)
954 		return (error);
955 	NL_LOG(LOG_DEBUG2, "socket %p, PID %d, SO_RCVBUF=%d", so, curproc->p_pid, optval);
956 	if (optval > sb_max_adj) {
957 		if (priv_check(curthread, PRIV_NET_ROUTE) != 0)
958 			return (EPERM);
959 	}
960 
961 	SOCK_RECVBUF_LOCK(so);
962 	result = sbreserve_locked_limit(so, SO_RCV, optval, nl_maxsockbuf, curthread);
963 	SOCK_RECVBUF_UNLOCK(so);
964 
965 	return (result ? 0 : ENOBUFS);
966 }
967 
968 #define	NETLINK_PROTOSW						\
969 	.pr_flags = PR_ATOMIC | PR_ADDR | PR_SOCKBUF,		\
970 	.pr_ctloutput = nl_ctloutput,				\
971 	.pr_setsbopt = nl_setsbopt,				\
972 	.pr_attach = nl_attach,					\
973 	.pr_bind = nl_bind,					\
974 	.pr_connect = nl_connect,				\
975 	.pr_disconnect = nl_disconnect,				\
976 	.pr_sosend = nl_sosend,					\
977 	.pr_soreceive = nl_soreceive,				\
978 	.pr_sockaddr = nl_sockaddr,				\
979 	.pr_close = nl_close
980 
981 static struct protosw netlink_raw_sw = {
982 	.pr_type = SOCK_RAW,
983 	NETLINK_PROTOSW
984 };
985 
986 static struct protosw netlink_dgram_sw = {
987 	.pr_type = SOCK_DGRAM,
988 	NETLINK_PROTOSW
989 };
990 
991 static struct domain netlinkdomain = {
992 	.dom_family = PF_NETLINK,
993 	.dom_name = "netlink",
994 	.dom_flags = DOMF_UNLOADABLE,
995 	.dom_nprotosw =		2,
996 	.dom_protosw =		{ &netlink_raw_sw, &netlink_dgram_sw },
997 };
998 
999 DOMAIN_SET(netlink);
1000