xref: /freebsd/sys/netlink/netlink_domain.c (revision 608da65de9552d5678c1000776ed69da04a45983)
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  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * This file contains socket and protocol bindings for netlink.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/lock.h>
37 #include <sys/rmlock.h>
38 #include <sys/domain.h>
39 #include <sys/jail.h>
40 #include <sys/mbuf.h>
41 #include <sys/osd.h>
42 #include <sys/protosw.h>
43 #include <sys/proc.h>
44 #include <sys/ck.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysent.h>
48 #include <sys/syslog.h>
49 #include <sys/priv.h> /* priv_check */
50 
51 #include <netlink/netlink.h>
52 #include <netlink/netlink_ctl.h>
53 #include <netlink/netlink_var.h>
54 
55 #define	DEBUG_MOD_NAME	nl_domain
56 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
57 #include <netlink/netlink_debug.h>
58 _DECLARE_DEBUG(LOG_INFO);
59 
60 _Static_assert((NLP_MAX_GROUPS % 64) == 0,
61     "NLP_MAX_GROUPS has to be multiple of 64");
62 _Static_assert(NLP_MAX_GROUPS >= 64,
63     "NLP_MAX_GROUPS has to be at least 64");
64 
65 #define	NLCTL_TRACKER		struct rm_priotracker nl_tracker
66 #define	NLCTL_RLOCK(_ctl)	rm_rlock(&((_ctl)->ctl_lock), &nl_tracker)
67 #define	NLCTL_RUNLOCK(_ctl)	rm_runlock(&((_ctl)->ctl_lock), &nl_tracker)
68 
69 #define	NLCTL_WLOCK(_ctl)	rm_wlock(&((_ctl)->ctl_lock))
70 #define	NLCTL_WUNLOCK(_ctl)	rm_wunlock(&((_ctl)->ctl_lock))
71 
72 static u_long nl_sendspace = NLSNDQ;
73 SYSCTL_ULONG(_net_netlink, OID_AUTO, sendspace, CTLFLAG_RW, &nl_sendspace, 0,
74     "Default netlink socket send space");
75 
76 static u_long nl_recvspace = NLSNDQ;
77 SYSCTL_ULONG(_net_netlink, OID_AUTO, recvspace, CTLFLAG_RW, &nl_recvspace, 0,
78     "Default netlink socket receive space");
79 
80 extern u_long sb_max_adj;
81 static u_long nl_maxsockbuf = 512 * 1024 * 1024; /* 512M, XXX: init based on physmem */
82 static int sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS);
83 SYSCTL_OID(_net_netlink, OID_AUTO, nl_maxsockbuf,
84     CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, &nl_maxsockbuf, 0,
85     sysctl_handle_nl_maxsockbuf, "LU",
86     "Maximum Netlink socket buffer size");
87 
88 
89 static unsigned int osd_slot_id = 0;
90 
91 void
92 nl_osd_register(void)
93 {
94 	osd_slot_id = osd_register(OSD_THREAD, NULL, NULL);
95 }
96 
97 void
98 nl_osd_unregister(void)
99 {
100 	osd_deregister(OSD_THREAD, osd_slot_id);
101 }
102 
103 struct nlpcb *
104 _nl_get_thread_nlp(struct thread *td)
105 {
106 	return (osd_get(OSD_THREAD, &td->td_osd, osd_slot_id));
107 }
108 
109 void
110 nl_set_thread_nlp(struct thread *td, struct nlpcb *nlp)
111 {
112 	NLP_LOG(LOG_DEBUG2, nlp, "Set thread %p nlp to %p (slot %u)", td, nlp, osd_slot_id);
113 	if (osd_set(OSD_THREAD, &td->td_osd, osd_slot_id, nlp) == 0)
114 		return;
115 	/* Failed, need to realloc */
116 	void **rsv = osd_reserve(osd_slot_id);
117 	osd_set_reserved(OSD_THREAD, &td->td_osd, osd_slot_id, rsv, nlp);
118 }
119 
120 /*
121  * Looks up a nlpcb struct based on the @portid. Need to claim nlsock_mtx.
122  * Returns nlpcb pointer if present else NULL
123  */
124 static struct nlpcb *
125 nl_port_lookup(uint32_t port_id)
126 {
127 	struct nlpcb *nlp;
128 
129 	CK_LIST_FOREACH(nlp, &V_nl_ctl->ctl_port_head, nl_port_next) {
130 		if (nlp->nl_port == port_id)
131 			return (nlp);
132 	}
133 	return (NULL);
134 }
135 
136 static void
137 nl_add_group_locked(struct nlpcb *nlp, unsigned int group_id)
138 {
139 	MPASS(group_id <= NLP_MAX_GROUPS);
140 	--group_id;
141 
142 	/* TODO: add family handler callback */
143 	if (!nlp_unconstrained_vnet(nlp))
144 		return;
145 
146 	nlp->nl_groups[group_id / 64] |= (uint64_t)1 << (group_id % 64);
147 }
148 
149 static void
150 nl_del_group_locked(struct nlpcb *nlp, unsigned int group_id)
151 {
152 	MPASS(group_id <= NLP_MAX_GROUPS);
153 	--group_id;
154 
155 	nlp->nl_groups[group_id / 64] &= ~((uint64_t)1 << (group_id % 64));
156 }
157 
158 static bool
159 nl_isset_group_locked(struct nlpcb *nlp, unsigned int group_id)
160 {
161 	MPASS(group_id <= NLP_MAX_GROUPS);
162 	--group_id;
163 
164 	return (nlp->nl_groups[group_id / 64] & ((uint64_t)1 << (group_id % 64)));
165 }
166 
167 static uint32_t
168 nl_get_groups_compat(struct nlpcb *nlp)
169 {
170 	uint32_t groups_mask = 0;
171 
172 	for (int i = 0; i < 32; i++) {
173 		if (nl_isset_group_locked(nlp, i + 1))
174 			groups_mask |= (1 << i);
175 	}
176 
177 	return (groups_mask);
178 }
179 
180 static void
181 nl_send_one_group(struct mbuf *m, struct nlpcb *nlp, int num_messages,
182     int io_flags)
183 {
184 	if (__predict_false(nlp->nl_flags & NLF_MSG_INFO))
185 		nl_add_msg_info(m);
186 	nl_send_one(m, nlp, num_messages, io_flags);
187 }
188 
189 /*
190  * Broadcasts message @m to the protocol @proto group specified by @group_id
191  */
192 void
193 nl_send_group(struct mbuf *m, int num_messages, int proto, int group_id)
194 {
195 	struct nlpcb *nlp_last = NULL;
196 	struct nlpcb *nlp;
197 	NLCTL_TRACKER;
198 
199 	IF_DEBUG_LEVEL(LOG_DEBUG2) {
200 		struct nlmsghdr *hdr = mtod(m, struct nlmsghdr *);
201 		NL_LOG(LOG_DEBUG2, "MCAST mbuf len %u msg type %d len %u to group %d/%d",
202 		    m->m_len, hdr->nlmsg_type, hdr->nlmsg_len, proto, group_id);
203 	}
204 
205 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
206 	if (__predict_false(ctl == NULL)) {
207 		/*
208 		 * Can be the case when notification is sent within VNET
209 		 * which doesn't have any netlink sockets.
210 		 */
211 		m_freem(m);
212 		return;
213 	}
214 
215 	NLCTL_RLOCK(ctl);
216 
217 	int io_flags = NL_IOF_UNTRANSLATED;
218 
219 	CK_LIST_FOREACH(nlp, &ctl->ctl_pcb_head, nl_next) {
220 		if (nl_isset_group_locked(nlp, group_id) && nlp->nl_proto == proto) {
221 			if (nlp_last != NULL) {
222 				struct mbuf *m_copy;
223 				m_copy = m_copym(m, 0, M_COPYALL, M_NOWAIT);
224 				if (m_copy != NULL)
225 					nl_send_one_group(m_copy, nlp_last,
226 					    num_messages, io_flags);
227 				else {
228 					NLP_LOCK(nlp_last);
229 					if (nlp_last->nl_socket != NULL)
230 						sorwakeup(nlp_last->nl_socket);
231 					NLP_UNLOCK(nlp_last);
232 				}
233 			}
234 			nlp_last = nlp;
235 		}
236 	}
237 	if (nlp_last != NULL)
238 		nl_send_one_group(m, nlp_last, num_messages, io_flags);
239 	else
240 		m_freem(m);
241 
242 	NLCTL_RUNLOCK(ctl);
243 }
244 
245 bool
246 nl_has_listeners(int netlink_family, uint32_t groups_mask)
247 {
248 	return (V_nl_ctl != NULL);
249 }
250 
251 static uint32_t
252 nl_find_port(void)
253 {
254 	/*
255 	 * app can open multiple netlink sockets.
256 	 * Start with current pid, if already taken,
257 	 * try random numbers in 65k..256k+65k space,
258 	 * avoiding clash with pids.
259 	 */
260 	if (nl_port_lookup(curproc->p_pid) == NULL)
261 		return (curproc->p_pid);
262 	for (int i = 0; i < 16; i++) {
263 		uint32_t nl_port = (arc4random() % 65536) + 65536 * 4;
264 		if (nl_port_lookup(nl_port) == 0)
265 			return (nl_port);
266 		NL_LOG(LOG_DEBUG3, "tried %u\n", nl_port);
267 	}
268 	return (curproc->p_pid);
269 }
270 
271 static int
272 nl_bind_locked(struct nlpcb *nlp, struct sockaddr_nl *snl)
273 {
274 	if (nlp->nl_bound) {
275 		if (nlp->nl_port != snl->nl_pid) {
276 			NL_LOG(LOG_DEBUG,
277 			    "bind() failed: program pid %d "
278 			    "is different from provided pid %d",
279 			    nlp->nl_port, snl->nl_pid);
280 			return (EINVAL); // XXX: better error
281 		}
282 	} else {
283 		if (snl->nl_pid == 0)
284 			snl->nl_pid = nl_find_port();
285 		if (nl_port_lookup(snl->nl_pid) != NULL)
286 			return (EADDRINUSE);
287 		nlp->nl_port = snl->nl_pid;
288 		nlp->nl_bound = true;
289 		CK_LIST_INSERT_HEAD(&V_nl_ctl->ctl_port_head, nlp, nl_port_next);
290 	}
291 	for (int i = 0; i < 32; i++) {
292 		if (snl->nl_groups & ((uint32_t)1 << i))
293 			nl_add_group_locked(nlp, i + 1);
294 		else
295 			nl_del_group_locked(nlp, i + 1);
296 	}
297 
298 	return (0);
299 }
300 
301 static int
302 nl_pru_attach(struct socket *so, int proto, struct thread *td)
303 {
304 	struct nlpcb *nlp;
305 	int error;
306 
307 	if (__predict_false(netlink_unloading != 0))
308 		return (EAFNOSUPPORT);
309 
310 	error = nl_verify_proto(proto);
311 	if (error != 0)
312 		return (error);
313 
314 	bool is_linux = SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX;
315 	NL_LOG(LOG_DEBUG2, "socket %p, %sPID %d: attaching socket to %s",
316 	    so, is_linux ? "(linux) " : "", curproc->p_pid,
317 	    nl_get_proto_name(proto));
318 
319 	/* Create per-VNET state on first socket init */
320 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
321 	if (ctl == NULL)
322 		ctl = vnet_nl_ctl_init();
323 	KASSERT(V_nl_ctl != NULL, ("nl_attach: vnet_sock_init() failed"));
324 
325 	MPASS(sotonlpcb(so) == NULL);
326 
327 	nlp = malloc(sizeof(struct nlpcb), M_PCB, M_WAITOK | M_ZERO);
328 	error = soreserve(so, nl_sendspace, nl_recvspace);
329 	if (error != 0) {
330 		free(nlp, M_PCB);
331 		return (error);
332 	}
333 	so->so_pcb = nlp;
334 	nlp->nl_socket = so;
335 	/* Copy so_cred to avoid having socket_var.h in every header */
336 	nlp->nl_cred = so->so_cred;
337 	nlp->nl_proto = proto;
338 	nlp->nl_process_id = curproc->p_pid;
339 	nlp->nl_linux = is_linux;
340 	nlp->nl_active = true;
341 	nlp->nl_unconstrained_vnet = !jailed_without_vnet(so->so_cred);
342 	nlp->nl_need_thread_setup = true;
343 	NLP_LOCK_INIT(nlp);
344 	refcount_init(&nlp->nl_refcount, 1);
345 	nl_init_io(nlp);
346 
347 	nlp->nl_taskqueue = taskqueue_create("netlink_socket", M_WAITOK,
348 	    taskqueue_thread_enqueue, &nlp->nl_taskqueue);
349 	TASK_INIT(&nlp->nl_task, 0, nl_taskqueue_handler, nlp);
350 	taskqueue_start_threads(&nlp->nl_taskqueue, 1, PWAIT,
351 	    "netlink_socket (PID %u)", nlp->nl_process_id);
352 
353 	NLCTL_WLOCK(ctl);
354 	/* XXX: check ctl is still alive */
355 	CK_LIST_INSERT_HEAD(&ctl->ctl_pcb_head, nlp, nl_next);
356 	NLCTL_WUNLOCK(ctl);
357 
358 	soisconnected(so);
359 
360 	return (0);
361 }
362 
363 static void
364 nl_pru_abort(struct socket *so)
365 {
366 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
367 	MPASS(sotonlpcb(so) != NULL);
368 	soisdisconnected(so);
369 }
370 
371 static int
372 nl_pru_bind(struct socket *so, struct sockaddr *sa, struct thread *td)
373 {
374 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
375 	struct nlpcb *nlp = sotonlpcb(so);
376 	struct sockaddr_nl *snl = (struct sockaddr_nl *)sa;
377 	int error;
378 
379 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
380 	if (snl->nl_len != sizeof(*snl)) {
381 		NL_LOG(LOG_DEBUG, "socket %p, wrong sizeof(), ignoring bind()", so);
382 		return (EINVAL);
383 	}
384 
385 
386 	NLCTL_WLOCK(ctl);
387 	NLP_LOCK(nlp);
388 	error = nl_bind_locked(nlp, snl);
389 	NLP_UNLOCK(nlp);
390 	NLCTL_WUNLOCK(ctl);
391 	NL_LOG(LOG_DEBUG2, "socket %p, bind() to %u, groups %u, error %d", so,
392 	    snl->nl_pid, snl->nl_groups, error);
393 
394 	return (error);
395 }
396 
397 
398 static int
399 nl_assign_port(struct nlpcb *nlp, uint32_t port_id)
400 {
401 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
402 	struct sockaddr_nl snl = {
403 		.nl_pid = port_id,
404 	};
405 	int error;
406 
407 	NLCTL_WLOCK(ctl);
408 	NLP_LOCK(nlp);
409 	snl.nl_groups = nl_get_groups_compat(nlp);
410 	error = nl_bind_locked(nlp, &snl);
411 	NLP_UNLOCK(nlp);
412 	NLCTL_WUNLOCK(ctl);
413 
414 	NL_LOG(LOG_DEBUG3, "socket %p, port assign: %d, error: %d", nlp->nl_socket, port_id, error);
415 	return (error);
416 }
417 
418 /*
419  * nl_autobind_port binds a unused portid to @nlp
420  * @nlp: pcb data for the netlink socket
421  * @candidate_id: first id to consider
422  */
423 static int
424 nl_autobind_port(struct nlpcb *nlp, uint32_t candidate_id)
425 {
426 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
427 	uint32_t port_id = candidate_id;
428 	NLCTL_TRACKER;
429 	bool exist;
430 	int error = EADDRINUSE;
431 
432 	for (int i = 0; i < 10; i++) {
433 		NL_LOG(LOG_DEBUG3, "socket %p, trying to assign port %d", nlp->nl_socket, port_id);
434 		NLCTL_RLOCK(ctl);
435 		exist = nl_port_lookup(port_id) != 0;
436 		NLCTL_RUNLOCK(ctl);
437 		if (!exist) {
438 			error = nl_assign_port(nlp, port_id);
439 			if (error != EADDRINUSE)
440 				break;
441 		}
442 		port_id++;
443 	}
444 	NL_LOG(LOG_DEBUG3, "socket %p, autobind to %d, error: %d", nlp->nl_socket, port_id, error);
445 	return (error);
446 }
447 
448 static int
449 nl_pru_connect(struct socket *so, struct sockaddr *sa, struct thread *td)
450 {
451 	struct sockaddr_nl *snl = (struct sockaddr_nl *)sa;
452 	struct nlpcb *nlp;
453 
454 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
455 	if (snl->nl_len != sizeof(*snl)) {
456 		NL_LOG(LOG_DEBUG, "socket %p, wrong sizeof(), ignoring bind()", so);
457 		return (EINVAL);
458 	}
459 
460 	nlp = sotonlpcb(so);
461 	if (!nlp->nl_bound) {
462 		int error = nl_autobind_port(nlp, td->td_proc->p_pid);
463 		if (error != 0) {
464 			NL_LOG(LOG_DEBUG, "socket %p, nl_autobind() failed: %d", so, error);
465 			return (error);
466 		}
467 	}
468 	/* XXX: Handle socket flags & multicast */
469 	soisconnected(so);
470 
471 	NL_LOG(LOG_DEBUG2, "socket %p, connect to %u", so, snl->nl_pid);
472 
473 	return (0);
474 }
475 
476 static void
477 destroy_nlpcb(struct nlpcb *nlp)
478 {
479 	NLP_LOCK(nlp);
480 	nl_free_io(nlp);
481 	NLP_LOCK_DESTROY(nlp);
482 	free(nlp, M_PCB);
483 }
484 
485 static void
486 destroy_nlpcb_epoch(epoch_context_t ctx)
487 {
488 	struct nlpcb *nlp;
489 
490 	nlp = __containerof(ctx, struct nlpcb, nl_epoch_ctx);
491 
492 	destroy_nlpcb(nlp);
493 }
494 
495 
496 static void
497 nl_pru_detach(struct socket *so)
498 {
499 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
500 	MPASS(sotonlpcb(so) != NULL);
501 	struct nlpcb *nlp;
502 
503 	NL_LOG(LOG_DEBUG2, "detaching socket %p, PID %d", so, curproc->p_pid);
504 	nlp = sotonlpcb(so);
505 
506 	/* Mark as inactive so no new work can be enqueued */
507 	NLP_LOCK(nlp);
508 	bool was_bound = nlp->nl_bound;
509 	nlp->nl_active = false;
510 	NLP_UNLOCK(nlp);
511 
512 	/* Wait till all scheduled work has been completed  */
513 	taskqueue_drain_all(nlp->nl_taskqueue);
514 	taskqueue_free(nlp->nl_taskqueue);
515 
516 	NLCTL_WLOCK(ctl);
517 	NLP_LOCK(nlp);
518 	if (was_bound) {
519 		CK_LIST_REMOVE(nlp, nl_port_next);
520 		NL_LOG(LOG_DEBUG3, "socket %p, unlinking bound pid %u", so, nlp->nl_port);
521 	}
522 	CK_LIST_REMOVE(nlp, nl_next);
523 	nlp->nl_socket = NULL;
524 	NLP_UNLOCK(nlp);
525 	NLCTL_WUNLOCK(ctl);
526 
527 	so->so_pcb = NULL;
528 
529 	NL_LOG(LOG_DEBUG3, "socket %p, detached", so);
530 
531 	/* XXX: is delayed free needed? */
532 	NET_EPOCH_CALL(destroy_nlpcb_epoch, &nlp->nl_epoch_ctx);
533 }
534 
535 static int
536 nl_pru_disconnect(struct socket *so)
537 {
538 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
539 	MPASS(sotonlpcb(so) != NULL);
540 	return (ENOTCONN);
541 }
542 
543 static int
544 nl_pru_peeraddr(struct socket *so, struct sockaddr **sa)
545 {
546 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
547 	MPASS(sotonlpcb(so) != NULL);
548 	return (ENOTCONN);
549 }
550 
551 static int
552 nl_pru_shutdown(struct socket *so)
553 {
554 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
555 	MPASS(sotonlpcb(so) != NULL);
556 	socantsendmore(so);
557 	return (0);
558 }
559 
560 static int
561 nl_pru_sockaddr(struct socket *so, struct sockaddr **sa)
562 {
563 	struct sockaddr_nl *snl;
564 
565 	snl = malloc(sizeof(struct sockaddr_nl), M_SONAME, M_WAITOK | M_ZERO);
566 	/* TODO: set other fields */
567 	snl->nl_len = sizeof(struct sockaddr_nl);
568 	snl->nl_family = AF_NETLINK;
569 	snl->nl_pid = sotonlpcb(so)->nl_port;
570 	*sa = (struct sockaddr *)snl;
571 	return (0);
572 }
573 
574 static void
575 nl_pru_close(struct socket *so)
576 {
577 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
578 	MPASS(sotonlpcb(so) != NULL);
579 	soisdisconnected(so);
580 }
581 
582 static int
583 nl_pru_output(struct mbuf *m, struct socket *so, ...)
584 {
585 
586 	if (__predict_false(m == NULL ||
587 	    ((m->m_len < sizeof(struct nlmsghdr)) &&
588 		(m = m_pullup(m, sizeof(struct nlmsghdr))) == NULL)))
589 		return (ENOBUFS);
590 	MPASS((m->m_flags & M_PKTHDR) != 0);
591 
592 	NL_LOG(LOG_DEBUG3, "sending message to kernel async processing");
593 	nl_receive_async(m, so);
594 	return (0);
595 }
596 
597 
598 static int
599 nl_pru_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *sa,
600     struct mbuf *control, struct thread *td)
601 {
602         NL_LOG(LOG_DEBUG2, "sending message to kernel");
603 
604 	if (__predict_false(control != NULL)) {
605 		if (control->m_len) {
606 			m_freem(control);
607 			return (EINVAL);
608 		}
609 		m_freem(control);
610 	}
611 
612 	return (nl_pru_output(m, so));
613 }
614 
615 static int
616 nl_pru_rcvd(struct socket *so, int flags)
617 {
618 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
619 	MPASS(sotonlpcb(so) != NULL);
620 
621 	nl_on_transmit(sotonlpcb(so));
622 
623 	return (0);
624 }
625 
626 static int
627 nl_getoptflag(int sopt_name)
628 {
629 	switch (sopt_name) {
630 	case NETLINK_CAP_ACK:
631 		return (NLF_CAP_ACK);
632 	case NETLINK_EXT_ACK:
633 		return (NLF_EXT_ACK);
634 	case NETLINK_GET_STRICT_CHK:
635 		return (NLF_STRICT);
636 	case NETLINK_MSG_INFO:
637 		return (NLF_MSG_INFO);
638 	}
639 
640 	return (0);
641 }
642 
643 static int
644 nl_ctloutput(struct socket *so, struct sockopt *sopt)
645 {
646 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
647 	struct nlpcb *nlp = sotonlpcb(so);
648 	uint32_t flag;
649 	int optval, error = 0;
650 	NLCTL_TRACKER;
651 
652 	NL_LOG(LOG_DEBUG2, "%ssockopt(%p, %d)", (sopt->sopt_dir) ? "set" : "get",
653 	    so, sopt->sopt_name);
654 
655 	switch (sopt->sopt_dir) {
656 	case SOPT_SET:
657 		switch (sopt->sopt_name) {
658 		case NETLINK_ADD_MEMBERSHIP:
659 		case NETLINK_DROP_MEMBERSHIP:
660 			error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
661 			if (error != 0)
662 				break;
663 			if (optval <= 0 || optval >= NLP_MAX_GROUPS) {
664 				error = ERANGE;
665 				break;
666 			}
667 			NL_LOG(LOG_DEBUG2, "ADD/DEL group %d", (uint32_t)optval);
668 
669 			NLCTL_WLOCK(ctl);
670 			if (sopt->sopt_name == NETLINK_ADD_MEMBERSHIP)
671 				nl_add_group_locked(nlp, optval);
672 			else
673 				nl_del_group_locked(nlp, optval);
674 			NLCTL_WUNLOCK(ctl);
675 			break;
676 		case NETLINK_CAP_ACK:
677 		case NETLINK_EXT_ACK:
678 		case NETLINK_GET_STRICT_CHK:
679 		case NETLINK_MSG_INFO:
680 			error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
681 			if (error != 0)
682 				break;
683 
684 			flag = nl_getoptflag(sopt->sopt_name);
685 
686 			if ((flag == NLF_MSG_INFO) && nlp->nl_linux) {
687 				error = EINVAL;
688 				break;
689 			}
690 
691 			NLCTL_WLOCK(ctl);
692 			if (optval != 0)
693 				nlp->nl_flags |= flag;
694 			else
695 				nlp->nl_flags &= ~flag;
696 			NLCTL_WUNLOCK(ctl);
697 			break;
698 		default:
699 			error = ENOPROTOOPT;
700 		}
701 		break;
702 	case SOPT_GET:
703 		switch (sopt->sopt_name) {
704 		case NETLINK_LIST_MEMBERSHIPS:
705 			NLCTL_RLOCK(ctl);
706 			optval = nl_get_groups_compat(nlp);
707 			NLCTL_RUNLOCK(ctl);
708 			error = sooptcopyout(sopt, &optval, sizeof(optval));
709 			break;
710 		case NETLINK_CAP_ACK:
711 		case NETLINK_EXT_ACK:
712 		case NETLINK_GET_STRICT_CHK:
713 		case NETLINK_MSG_INFO:
714 			NLCTL_RLOCK(ctl);
715 			optval = (nlp->nl_flags & nl_getoptflag(sopt->sopt_name)) != 0;
716 			NLCTL_RUNLOCK(ctl);
717 			error = sooptcopyout(sopt, &optval, sizeof(optval));
718 			break;
719 		default:
720 			error = ENOPROTOOPT;
721 		}
722 		break;
723 	default:
724 		error = ENOPROTOOPT;
725 	}
726 
727 	return (error);
728 }
729 
730 static int
731 sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS)
732 {
733 	int error = 0;
734 	u_long tmp_maxsockbuf = nl_maxsockbuf;
735 
736 	error = sysctl_handle_long(oidp, &tmp_maxsockbuf, arg2, req);
737 	if (error || !req->newptr)
738 		return (error);
739 	if (tmp_maxsockbuf < MSIZE + MCLBYTES)
740 		return (EINVAL);
741 	nl_maxsockbuf = tmp_maxsockbuf;
742 
743 	return (0);
744 }
745 
746 static int
747 nl_setsbopt(struct socket *so, struct sockopt *sopt)
748 {
749 	int error, optval;
750 	bool result;
751 
752 	if (sopt->sopt_name != SO_RCVBUF)
753 		return (sbsetopt(so, sopt));
754 
755 	/* Allow to override max buffer size in certain conditions */
756 
757 	error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
758 	if (error != 0)
759 		return (error);
760 	NL_LOG(LOG_DEBUG2, "socket %p, PID %d, SO_RCVBUF=%d", so, curproc->p_pid, optval);
761 	if (optval > sb_max_adj) {
762 		if (priv_check(curthread, PRIV_NET_ROUTE) != 0)
763 			return (EPERM);
764 	}
765 
766 	SOCK_RECVBUF_LOCK(so);
767 	result = sbreserve_locked_limit(so, SO_RCV, optval, nl_maxsockbuf, curthread);
768 	SOCK_RECVBUF_UNLOCK(so);
769 
770 	return (result ? 0 : ENOBUFS);
771 }
772 
773 #define	NETLINK_PROTOSW						\
774 	.pr_flags = PR_ATOMIC | PR_ADDR | PR_WANTRCVD,		\
775 	.pr_ctloutput = nl_ctloutput,				\
776 	.pr_setsbopt = nl_setsbopt,				\
777 	.pr_abort = nl_pru_abort,				\
778 	.pr_attach = nl_pru_attach,				\
779 	.pr_bind = nl_pru_bind,					\
780 	.pr_connect = nl_pru_connect,				\
781 	.pr_detach = nl_pru_detach,				\
782 	.pr_disconnect = nl_pru_disconnect,			\
783 	.pr_peeraddr = nl_pru_peeraddr,				\
784 	.pr_send = nl_pru_send,					\
785 	.pr_rcvd = nl_pru_rcvd,					\
786 	.pr_shutdown = nl_pru_shutdown,				\
787 	.pr_sockaddr = nl_pru_sockaddr,				\
788 	.pr_close = nl_pru_close
789 
790 static struct protosw netlink_raw_sw = {
791 	.pr_type = SOCK_RAW,
792 	NETLINK_PROTOSW
793 };
794 
795 static struct protosw netlink_dgram_sw = {
796 	.pr_type = SOCK_DGRAM,
797 	NETLINK_PROTOSW
798 };
799 
800 static struct domain netlinkdomain = {
801 	.dom_family = PF_NETLINK,
802 	.dom_name = "netlink",
803 	.dom_flags = DOMF_UNLOADABLE,
804 	.dom_nprotosw =		2,
805 	.dom_protosw =		{ &netlink_raw_sw, &netlink_dgram_sw },
806 };
807 
808 DOMAIN_SET(netlink);
809