xref: /freebsd/sys/netlink/netlink_domain.c (revision 4b9d6057)
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_shutdown(struct socket *so)
545 {
546 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
547 	MPASS(sotonlpcb(so) != NULL);
548 	socantsendmore(so);
549 	return (0);
550 }
551 
552 static int
553 nl_sockaddr(struct socket *so, struct sockaddr *sa)
554 {
555 
556 	*(struct sockaddr_nl *)sa = (struct sockaddr_nl ){
557 		/* TODO: set other fields */
558 		.nl_len = sizeof(struct sockaddr_nl),
559 		.nl_family = AF_NETLINK,
560 		.nl_pid = sotonlpcb(so)->nl_port,
561 	};
562 
563 	return (0);
564 }
565 
566 static void
567 nl_pru_close(struct socket *so)
568 {
569 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
570 	MPASS(sotonlpcb(so) != NULL);
571 	soisdisconnected(so);
572 }
573 
574 static int
575 nl_pru_output(struct mbuf *m, struct socket *so, ...)
576 {
577 
578 	if (__predict_false(m == NULL ||
579 	    ((m->m_len < sizeof(struct nlmsghdr)) &&
580 		(m = m_pullup(m, sizeof(struct nlmsghdr))) == NULL)))
581 		return (ENOBUFS);
582 	MPASS((m->m_flags & M_PKTHDR) != 0);
583 
584 	NL_LOG(LOG_DEBUG3, "sending message to kernel async processing");
585 	nl_receive_async(m, so);
586 	return (0);
587 }
588 
589 
590 static int
591 nl_pru_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *sa,
592     struct mbuf *control, struct thread *td)
593 {
594         NL_LOG(LOG_DEBUG2, "sending message to kernel");
595 
596 	if (__predict_false(control != NULL)) {
597 		if (control->m_len) {
598 			m_freem(control);
599 			return (EINVAL);
600 		}
601 		m_freem(control);
602 	}
603 
604 	return (nl_pru_output(m, so));
605 }
606 
607 static int
608 nl_pru_rcvd(struct socket *so, int flags)
609 {
610 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
611 	MPASS(sotonlpcb(so) != NULL);
612 
613 	nl_on_transmit(sotonlpcb(so));
614 
615 	return (0);
616 }
617 
618 static int
619 nl_getoptflag(int sopt_name)
620 {
621 	switch (sopt_name) {
622 	case NETLINK_CAP_ACK:
623 		return (NLF_CAP_ACK);
624 	case NETLINK_EXT_ACK:
625 		return (NLF_EXT_ACK);
626 	case NETLINK_GET_STRICT_CHK:
627 		return (NLF_STRICT);
628 	case NETLINK_MSG_INFO:
629 		return (NLF_MSG_INFO);
630 	}
631 
632 	return (0);
633 }
634 
635 static int
636 nl_ctloutput(struct socket *so, struct sockopt *sopt)
637 {
638 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
639 	struct nlpcb *nlp = sotonlpcb(so);
640 	uint32_t flag;
641 	int optval, error = 0;
642 	NLCTL_TRACKER;
643 
644 	NL_LOG(LOG_DEBUG2, "%ssockopt(%p, %d)", (sopt->sopt_dir) ? "set" : "get",
645 	    so, sopt->sopt_name);
646 
647 	switch (sopt->sopt_dir) {
648 	case SOPT_SET:
649 		switch (sopt->sopt_name) {
650 		case NETLINK_ADD_MEMBERSHIP:
651 		case NETLINK_DROP_MEMBERSHIP:
652 			error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
653 			if (error != 0)
654 				break;
655 			if (optval <= 0 || optval >= NLP_MAX_GROUPS) {
656 				error = ERANGE;
657 				break;
658 			}
659 			NL_LOG(LOG_DEBUG2, "ADD/DEL group %d", (uint32_t)optval);
660 
661 			NLCTL_WLOCK(ctl);
662 			if (sopt->sopt_name == NETLINK_ADD_MEMBERSHIP)
663 				nl_add_group_locked(nlp, optval);
664 			else
665 				nl_del_group_locked(nlp, optval);
666 			NLCTL_WUNLOCK(ctl);
667 			break;
668 		case NETLINK_CAP_ACK:
669 		case NETLINK_EXT_ACK:
670 		case NETLINK_GET_STRICT_CHK:
671 		case NETLINK_MSG_INFO:
672 			error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
673 			if (error != 0)
674 				break;
675 
676 			flag = nl_getoptflag(sopt->sopt_name);
677 
678 			if ((flag == NLF_MSG_INFO) && nlp->nl_linux) {
679 				error = EINVAL;
680 				break;
681 			}
682 
683 			NLCTL_WLOCK(ctl);
684 			if (optval != 0)
685 				nlp->nl_flags |= flag;
686 			else
687 				nlp->nl_flags &= ~flag;
688 			NLCTL_WUNLOCK(ctl);
689 			break;
690 		default:
691 			error = ENOPROTOOPT;
692 		}
693 		break;
694 	case SOPT_GET:
695 		switch (sopt->sopt_name) {
696 		case NETLINK_LIST_MEMBERSHIPS:
697 			NLCTL_RLOCK(ctl);
698 			optval = nl_get_groups_compat(nlp);
699 			NLCTL_RUNLOCK(ctl);
700 			error = sooptcopyout(sopt, &optval, sizeof(optval));
701 			break;
702 		case NETLINK_CAP_ACK:
703 		case NETLINK_EXT_ACK:
704 		case NETLINK_GET_STRICT_CHK:
705 		case NETLINK_MSG_INFO:
706 			NLCTL_RLOCK(ctl);
707 			optval = (nlp->nl_flags & nl_getoptflag(sopt->sopt_name)) != 0;
708 			NLCTL_RUNLOCK(ctl);
709 			error = sooptcopyout(sopt, &optval, sizeof(optval));
710 			break;
711 		default:
712 			error = ENOPROTOOPT;
713 		}
714 		break;
715 	default:
716 		error = ENOPROTOOPT;
717 	}
718 
719 	return (error);
720 }
721 
722 static int
723 sysctl_handle_nl_maxsockbuf(SYSCTL_HANDLER_ARGS)
724 {
725 	int error = 0;
726 	u_long tmp_maxsockbuf = nl_maxsockbuf;
727 
728 	error = sysctl_handle_long(oidp, &tmp_maxsockbuf, arg2, req);
729 	if (error || !req->newptr)
730 		return (error);
731 	if (tmp_maxsockbuf < MSIZE + MCLBYTES)
732 		return (EINVAL);
733 	nl_maxsockbuf = tmp_maxsockbuf;
734 
735 	return (0);
736 }
737 
738 static int
739 nl_setsbopt(struct socket *so, struct sockopt *sopt)
740 {
741 	int error, optval;
742 	bool result;
743 
744 	if (sopt->sopt_name != SO_RCVBUF)
745 		return (sbsetopt(so, sopt));
746 
747 	/* Allow to override max buffer size in certain conditions */
748 
749 	error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
750 	if (error != 0)
751 		return (error);
752 	NL_LOG(LOG_DEBUG2, "socket %p, PID %d, SO_RCVBUF=%d", so, curproc->p_pid, optval);
753 	if (optval > sb_max_adj) {
754 		if (priv_check(curthread, PRIV_NET_ROUTE) != 0)
755 			return (EPERM);
756 	}
757 
758 	SOCK_RECVBUF_LOCK(so);
759 	result = sbreserve_locked_limit(so, SO_RCV, optval, nl_maxsockbuf, curthread);
760 	SOCK_RECVBUF_UNLOCK(so);
761 
762 	return (result ? 0 : ENOBUFS);
763 }
764 
765 #define	NETLINK_PROTOSW						\
766 	.pr_flags = PR_ATOMIC | PR_ADDR | PR_WANTRCVD,		\
767 	.pr_ctloutput = nl_ctloutput,				\
768 	.pr_setsbopt = nl_setsbopt,				\
769 	.pr_abort = nl_pru_abort,				\
770 	.pr_attach = nl_pru_attach,				\
771 	.pr_bind = nl_pru_bind,					\
772 	.pr_connect = nl_pru_connect,				\
773 	.pr_detach = nl_pru_detach,				\
774 	.pr_disconnect = nl_pru_disconnect,			\
775 	.pr_send = nl_pru_send,					\
776 	.pr_rcvd = nl_pru_rcvd,					\
777 	.pr_shutdown = nl_pru_shutdown,				\
778 	.pr_sockaddr = nl_sockaddr,				\
779 	.pr_close = nl_pru_close
780 
781 static struct protosw netlink_raw_sw = {
782 	.pr_type = SOCK_RAW,
783 	NETLINK_PROTOSW
784 };
785 
786 static struct protosw netlink_dgram_sw = {
787 	.pr_type = SOCK_DGRAM,
788 	NETLINK_PROTOSW
789 };
790 
791 static struct domain netlinkdomain = {
792 	.dom_family = PF_NETLINK,
793 	.dom_name = "netlink",
794 	.dom_flags = DOMF_UNLOADABLE,
795 	.dom_nprotosw =		2,
796 	.dom_protosw =		{ &netlink_raw_sw, &netlink_dgram_sw },
797 };
798 
799 DOMAIN_SET(netlink);
800