xref: /freebsd/sys/netlink/netlink_domain.c (revision d30a1689f5b37e78ea189232a8b94a7011dc0dc8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/mbuf.h>
40 #include <sys/protosw.h>
41 #include <sys/proc.h>
42 #include <sys/ck.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysent.h>
46 #include <sys/syslog.h>
47 #include <sys/priv.h> /* priv_check */
48 
49 #include <netlink/netlink.h>
50 #include <netlink/netlink_ctl.h>
51 #include <netlink/netlink_var.h>
52 
53 #define	DEBUG_MOD_NAME	nl_domain
54 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
55 #include <netlink/netlink_debug.h>
56 _DECLARE_DEBUG(LOG_DEBUG);
57 
58 
59 #define	NLCTL_TRACKER		struct rm_priotracker nl_tracker
60 #define	NLCTL_RLOCK(_ctl)	rm_rlock(&((_ctl)->ctl_lock), &nl_tracker)
61 #define	NLCTL_RUNLOCK(_ctl)	rm_runlock(&((_ctl)->ctl_lock), &nl_tracker)
62 
63 #define	NLCTL_WLOCK(_ctl)	rm_wlock(&((_ctl)->ctl_lock))
64 #define	NLCTL_WUNLOCK(_ctl)	rm_wunlock(&((_ctl)->ctl_lock))
65 
66 static u_long nl_sendspace = NLSNDQ;
67 SYSCTL_ULONG(_net_netlink, OID_AUTO, sendspace, CTLFLAG_RW, &nl_sendspace, 0,
68     "Default netlink socket send space");
69 
70 static u_long nl_recvspace = NLSNDQ;
71 SYSCTL_ULONG(_net_netlink, OID_AUTO, recvspace, CTLFLAG_RW, &nl_recvspace, 0,
72     "Default netlink socket receive space");
73 
74 extern u_long sb_max_adj;
75 static u_long nl_maxsockbuf = 512 * 1024 * 1024; /* 512M, XXX: init based on physmem */
76 
77 uint32_t
78 nlp_get_pid(const struct nlpcb *nlp)
79 {
80 	return (nlp->nl_process_id);
81 }
82 
83 /*
84  * Looks up a nlpcb struct based on the @portid. Need to claim nlsock_mtx.
85  * Returns nlpcb pointer if present else NULL
86  */
87 static struct nlpcb *
88 nl_port_lookup(uint32_t port_id)
89 {
90 	struct nlpcb *nlp;
91 
92 	CK_LIST_FOREACH(nlp, &V_nl_ctl->ctl_port_head, nl_port_next) {
93 		if (nlp->nl_port == port_id)
94 			return (nlp);
95 	}
96 	return (NULL);
97 }
98 
99 static void
100 nl_update_groups_locked(struct nlpcb *nlp, uint64_t nl_groups)
101 {
102 	/* Update group mask */
103 	NL_LOG(LOG_DEBUG2, "socket %p, groups 0x%X -> 0x%X",
104 	    nlp->nl_socket, (uint32_t)nlp->nl_groups, (uint32_t)nl_groups);
105 	nlp->nl_groups = nl_groups;
106 }
107 
108 /*
109  * Broadcasts message @m to the protocol @proto group specified by @group_id
110  */
111 void
112 nl_send_group(struct mbuf *m, int num_messages, int proto, int group_id)
113 {
114 	struct nlpcb *nlp_last = NULL;
115 	struct nlpcb *nlp;
116 	NLCTL_TRACKER;
117 
118 	IF_DEBUG_LEVEL(LOG_DEBUG2) {
119 		struct nlmsghdr *hdr = mtod(m, struct nlmsghdr *);
120 		NL_LOG(LOG_DEBUG2, "MCAST mbuf len %u msg type %d len %u to group %d/%d",
121 		    m->m_len, hdr->nlmsg_type, hdr->nlmsg_len, proto, group_id);
122 	}
123 
124 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
125 	if (__predict_false(ctl == NULL)) {
126 		/*
127 		 * Can be the case when notification is sent within VNET
128 		 * which doesn't have any netlink sockets.
129 		 */
130 		m_freem(m);
131 		return;
132 	}
133 
134 	NLCTL_RLOCK(ctl);
135 
136 	int io_flags = NL_IOF_UNTRANSLATED;
137 	uint64_t groups_mask = 1 << ((uint64_t)group_id - 1);
138 
139 	CK_LIST_FOREACH(nlp, &ctl->ctl_pcb_head, nl_next) {
140 		if (nlp->nl_groups & groups_mask && nlp->nl_proto == proto) {
141 			if (nlp_last != NULL) {
142 				struct mbuf *m_copy;
143 				m_copy = m_copym(m, 0, M_COPYALL, M_NOWAIT);
144 				if (m_copy != NULL)
145 					nl_send_one(m_copy, nlp_last, num_messages, io_flags);
146 				else {
147 					NLP_LOCK(nlp_last);
148 					if (nlp_last->nl_socket != NULL)
149 						sorwakeup(nlp_last->nl_socket);
150 					NLP_UNLOCK(nlp_last);
151 				}
152 			}
153 			nlp_last = nlp;
154 		}
155 	}
156 	if (nlp_last != NULL)
157 		nl_send_one(m, nlp_last, num_messages, io_flags);
158 	else
159 		m_freem(m);
160 
161 	NLCTL_RUNLOCK(ctl);
162 }
163 
164 bool
165 nl_has_listeners(int netlink_family, uint32_t groups_mask)
166 {
167 	return (V_nl_ctl != NULL);
168 }
169 
170 bool
171 nlp_has_priv(struct nlpcb *nlp, int priv)
172 {
173 	return (priv_check_cred(nlp->nl_cred, priv) == 0);
174 }
175 
176 static uint32_t
177 nl_find_port() {
178 	/*
179 	 * app can open multiple netlink sockets.
180 	 * Start with current pid, if already taken,
181 	 * try random numbers in 65k..256k+65k space,
182 	 * avoiding clash with pids.
183 	 */
184 	if (nl_port_lookup(curproc->p_pid) == NULL)
185 		return (curproc->p_pid);
186 	for (int i = 0; i < 16; i++) {
187 		uint32_t nl_port = (arc4random() % 65536) + 65536 * 4;
188 		if (nl_port_lookup(nl_port) == 0)
189 			return (nl_port);
190 		NL_LOG(LOG_DEBUG3, "tried %u\n", nl_port);
191 	}
192 	return (curproc->p_pid);
193 }
194 
195 static int
196 nl_bind_locked(struct nlpcb *nlp, struct sockaddr_nl *snl)
197 {
198 	if (nlp->nl_bound) {
199 		if (nlp->nl_port != snl->nl_pid) {
200 			NL_LOG(LOG_DEBUG,
201 			    "bind() failed: program pid %d "
202 			    "is different from provided pid %d",
203 			    nlp->nl_port, snl->nl_pid);
204 			return (EINVAL); // XXX: better error
205 		}
206 	} else {
207 		if (snl->nl_pid == 0)
208 			snl->nl_pid = nl_find_port();
209 		if (nl_port_lookup(snl->nl_pid) != NULL)
210 			return (EADDRINUSE);
211 		nlp->nl_port = snl->nl_pid;
212 		nlp->nl_bound = true;
213 		CK_LIST_INSERT_HEAD(&V_nl_ctl->ctl_port_head, nlp, nl_port_next);
214 	}
215 	nl_update_groups_locked(nlp, snl->nl_groups);
216 
217 	return (0);
218 }
219 
220 static int
221 nl_pru_attach(struct socket *so, int proto, struct thread *td)
222 {
223 	struct nlpcb *nlp;
224 	int error;
225 
226 	if (__predict_false(netlink_unloading != 0))
227 		return (EAFNOSUPPORT);
228 
229 	error = nl_verify_proto(proto);
230 	if (error != 0)
231 		return (error);
232 
233 	bool is_linux = SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX;
234 	NL_LOG(LOG_DEBUG2, "socket %p, %sPID %d: attaching socket to %s",
235 	    so, is_linux ? "(linux) " : "", curproc->p_pid,
236 	    nl_get_proto_name(proto));
237 
238 	/* Create per-VNET state on first socket init */
239 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
240 	if (ctl == NULL)
241 		ctl = vnet_nl_ctl_init();
242 	KASSERT(V_nl_ctl != NULL, ("nl_attach: vnet_sock_init() failed"));
243 
244 	MPASS(sotonlpcb(so) == NULL);
245 
246 	nlp = malloc(sizeof(struct nlpcb), M_PCB, M_WAITOK | M_ZERO);
247 	error = soreserve(so, nl_sendspace, nl_recvspace);
248 	if (error != 0) {
249 		free(nlp, M_PCB);
250 		return (error);
251 	}
252 	so->so_pcb = nlp;
253 	nlp->nl_socket = so;
254 	/* Copy so_cred to avoid having socket_var.h in every header */
255 	nlp->nl_cred = so->so_cred;
256 	nlp->nl_proto = proto;
257 	nlp->nl_process_id = curproc->p_pid;
258 	nlp->nl_linux = is_linux;
259 	nlp->nl_active = true;
260 	NLP_LOCK_INIT(nlp);
261 	refcount_init(&nlp->nl_refcount, 1);
262 	nl_init_io(nlp);
263 
264 	nlp->nl_taskqueue = taskqueue_create("netlink_socket", M_WAITOK,
265 	    taskqueue_thread_enqueue, &nlp->nl_taskqueue);
266 	TASK_INIT(&nlp->nl_task, 0, nl_taskqueue_handler, nlp);
267 	taskqueue_start_threads(&nlp->nl_taskqueue, 1, PWAIT,
268 	    "netlink_socket (PID %u)", nlp->nl_process_id);
269 
270 	NLCTL_WLOCK(ctl);
271 	/* XXX: check ctl is still alive */
272 	CK_LIST_INSERT_HEAD(&ctl->ctl_pcb_head, nlp, nl_next);
273 	NLCTL_WUNLOCK(ctl);
274 
275 	soisconnected(so);
276 
277 	return (0);
278 }
279 
280 static void
281 nl_pru_abort(struct socket *so)
282 {
283 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
284 	MPASS(sotonlpcb(so) != NULL);
285 	soisdisconnected(so);
286 }
287 
288 static int
289 nl_pru_bind(struct socket *so, struct sockaddr *sa, struct thread *td)
290 {
291 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
292 	struct nlpcb *nlp = sotonlpcb(so);
293 	struct sockaddr_nl *snl = (struct sockaddr_nl *)sa;
294 	int error;
295 
296 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
297 	if (snl->nl_len != sizeof(*snl)) {
298 		NL_LOG(LOG_DEBUG, "socket %p, wrong sizeof(), ignoring bind()", so);
299 		return (EINVAL);
300 	}
301 
302 
303 	NLCTL_WLOCK(ctl);
304 	NLP_LOCK(nlp);
305 	error = nl_bind_locked(nlp, snl);
306 	NLP_UNLOCK(nlp);
307 	NLCTL_WUNLOCK(ctl);
308 	NL_LOG(LOG_DEBUG2, "socket %p, bind() to %u, groups %u, error %d", so,
309 	    snl->nl_pid, snl->nl_groups, error);
310 
311 	return (error);
312 }
313 
314 
315 static int
316 nl_assign_port(struct nlpcb *nlp, uint32_t port_id)
317 {
318 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
319 	struct sockaddr_nl snl = {
320 		.nl_pid = port_id,
321 	};
322 	int error;
323 
324 	NLCTL_WLOCK(ctl);
325 	NLP_LOCK(nlp);
326 	snl.nl_groups = nlp->nl_groups;
327 	error = nl_bind_locked(nlp, &snl);
328 	NLP_UNLOCK(nlp);
329 	NLCTL_WUNLOCK(ctl);
330 
331 	NL_LOG(LOG_DEBUG3, "socket %p, port assign: %d, error: %d", nlp->nl_socket, port_id, error);
332 	return (error);
333 }
334 
335 /*
336  * nl_autobind_port binds a unused portid to @nlp
337  * @nlp: pcb data for the netlink socket
338  * @candidate_id: first id to consider
339  */
340 static int
341 nl_autobind_port(struct nlpcb *nlp, uint32_t candidate_id)
342 {
343 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
344 	uint32_t port_id = candidate_id;
345 	NLCTL_TRACKER;
346 	bool exist;
347 	int error;
348 
349 	for (int i = 0; i < 10; i++) {
350 		NL_LOG(LOG_DEBUG3, "socket %p, trying to assign port %d", nlp->nl_socket, port_id);
351 		NLCTL_RLOCK(ctl);
352 		exist = nl_port_lookup(port_id) != 0;
353 		NLCTL_RUNLOCK(ctl);
354 		if (!exist) {
355 			error = nl_assign_port(nlp, port_id);
356 			if (error != EADDRINUSE)
357 				break;
358 		}
359 		port_id++;
360 	}
361 	NL_LOG(LOG_DEBUG3, "socket %p, autobind to %d, error: %d", nlp->nl_socket, port_id, error);
362 	return (error);
363 }
364 
365 static int
366 nl_pru_connect(struct socket *so, struct sockaddr *sa, struct thread *td)
367 {
368 	struct sockaddr_nl *snl = (struct sockaddr_nl *)sa;
369 	struct nlpcb *nlp;
370 
371 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
372 	if (snl->nl_len != sizeof(*snl)) {
373 		NL_LOG(LOG_DEBUG, "socket %p, wrong sizeof(), ignoring bind()", so);
374 		return (EINVAL);
375 	}
376 
377 	nlp = sotonlpcb(so);
378 	if (!nlp->nl_bound) {
379 		int error = nl_autobind_port(nlp, td->td_proc->p_pid);
380 		if (error != 0) {
381 			NL_LOG(LOG_DEBUG, "socket %p, nl_autobind() failed: %d", so, error);
382 			return (error);
383 		}
384 	}
385 	/* XXX: Handle socket flags & multicast */
386 	soisconnected(so);
387 
388 	NL_LOG(LOG_DEBUG2, "socket %p, connect to %u", so, snl->nl_pid);
389 
390 	return (0);
391 }
392 
393 static void
394 destroy_nlpcb(struct nlpcb *nlp)
395 {
396 	NLP_LOCK(nlp);
397 	nl_free_io(nlp);
398 	NLP_LOCK_DESTROY(nlp);
399 	free(nlp, M_PCB);
400 }
401 
402 static void
403 destroy_nlpcb_epoch(epoch_context_t ctx)
404 {
405 	struct nlpcb *nlp;
406 
407 	nlp = __containerof(ctx, struct nlpcb, nl_epoch_ctx);
408 
409 	destroy_nlpcb(nlp);
410 }
411 
412 
413 static void
414 nl_pru_detach(struct socket *so)
415 {
416 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
417 	MPASS(sotonlpcb(so) != NULL);
418 	struct nlpcb *nlp;
419 
420 	NL_LOG(LOG_DEBUG2, "detaching socket %p, PID %d", so, curproc->p_pid);
421 	nlp = sotonlpcb(so);
422 
423 	/* Mark as inactive so no new work can be enqueued */
424 	NLP_LOCK(nlp);
425 	bool was_bound = nlp->nl_bound;
426 	nlp->nl_active = false;
427 	NLP_UNLOCK(nlp);
428 
429 	/* Wait till all scheduled work has been completed  */
430 	taskqueue_drain_all(nlp->nl_taskqueue);
431 	taskqueue_free(nlp->nl_taskqueue);
432 
433 	NLCTL_WLOCK(ctl);
434 	NLP_LOCK(nlp);
435 	if (was_bound) {
436 		CK_LIST_REMOVE(nlp, nl_port_next);
437 		NL_LOG(LOG_DEBUG3, "socket %p, unlinking bound pid %u", so, nlp->nl_port);
438 	}
439 	CK_LIST_REMOVE(nlp, nl_next);
440 	nlp->nl_socket = NULL;
441 	NLP_UNLOCK(nlp);
442 	NLCTL_WUNLOCK(ctl);
443 
444 	so->so_pcb = NULL;
445 
446 	NL_LOG(LOG_DEBUG3, "socket %p, detached", so);
447 
448 	/* XXX: is delayed free needed? */
449 	epoch_call(net_epoch_preempt, destroy_nlpcb_epoch, &nlp->nl_epoch_ctx);
450 }
451 
452 static int
453 nl_pru_disconnect(struct socket *so)
454 {
455 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
456 	MPASS(sotonlpcb(so) != NULL);
457 	return (ENOTCONN);
458 }
459 
460 static int
461 nl_pru_peeraddr(struct socket *so, struct sockaddr **sa)
462 {
463 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
464 	MPASS(sotonlpcb(so) != NULL);
465 	return (ENOTCONN);
466 }
467 
468 static int
469 nl_pru_shutdown(struct socket *so)
470 {
471 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
472 	MPASS(sotonlpcb(so) != NULL);
473 	socantsendmore(so);
474 	return (0);
475 }
476 
477 static int
478 nl_pru_sockaddr(struct socket *so, struct sockaddr **sa)
479 {
480 	struct sockaddr_nl *snl;
481 
482 	snl = malloc(sizeof(struct sockaddr_nl), M_SONAME, M_WAITOK | M_ZERO);
483 	/* TODO: set other fields */
484 	snl->nl_len = sizeof(struct sockaddr_nl);
485 	snl->nl_family = AF_NETLINK;
486 	snl->nl_pid = sotonlpcb(so)->nl_port;
487 	*sa = (struct sockaddr *)snl;
488 	return (0);
489 }
490 
491 static void
492 nl_pru_close(struct socket *so)
493 {
494 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
495 	MPASS(sotonlpcb(so) != NULL);
496 	soisdisconnected(so);
497 }
498 
499 static int
500 nl_pru_output(struct mbuf *m, struct socket *so, ...)
501 {
502 
503 	if (__predict_false(m == NULL ||
504 	    ((m->m_len < sizeof(struct nlmsghdr)) &&
505 		(m = m_pullup(m, sizeof(struct nlmsghdr))) == NULL)))
506 		return (ENOBUFS);
507 	MPASS((m->m_flags & M_PKTHDR) != 0);
508 
509 	NL_LOG(LOG_DEBUG3, "sending message to kernel async processing");
510 	nl_receive_async(m, so);
511 	return (0);
512 }
513 
514 
515 static int
516 nl_pru_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *sa,
517     struct mbuf *control, struct thread *td)
518 {
519         NL_LOG(LOG_DEBUG2, "sending message to kernel");
520 
521 	if (__predict_false(control != NULL)) {
522 		if (control->m_len) {
523 			m_freem(control);
524 			return (EINVAL);
525 		}
526 		m_freem(control);
527 	}
528 
529 	return (nl_pru_output(m, so));
530 }
531 
532 static int
533 nl_pru_rcvd(struct socket *so, int flags)
534 {
535 	NL_LOG(LOG_DEBUG3, "socket %p, PID %d", so, curproc->p_pid);
536 	MPASS(sotonlpcb(so) != NULL);
537 
538 	nl_on_transmit(sotonlpcb(so));
539 
540 	return (0);
541 }
542 
543 static int
544 nl_getoptflag(int sopt_name)
545 {
546 	switch (sopt_name) {
547 	case NETLINK_CAP_ACK:
548 		return (NLF_CAP_ACK);
549 	case NETLINK_EXT_ACK:
550 		return (NLF_EXT_ACK);
551 	case NETLINK_GET_STRICT_CHK:
552 		return (NLF_STRICT);
553 	}
554 
555 	return (0);
556 }
557 
558 static int
559 nl_ctloutput(struct socket *so, struct sockopt *sopt)
560 {
561 	struct nl_control *ctl = atomic_load_ptr(&V_nl_ctl);
562 	struct nlpcb *nlp = sotonlpcb(so);
563 	uint32_t flag;
564 	uint64_t groups, group_mask;
565 	int optval, error = 0;
566 	NLCTL_TRACKER;
567 
568 	NL_LOG(LOG_DEBUG2, "%ssockopt(%p, %d)", (sopt->sopt_dir) ? "set" : "get",
569 	    so, sopt->sopt_name);
570 
571 	switch (sopt->sopt_dir) {
572 	case SOPT_SET:
573 		switch (sopt->sopt_name) {
574 		case NETLINK_ADD_MEMBERSHIP:
575 		case NETLINK_DROP_MEMBERSHIP:
576 			sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
577 			if (optval <= 0 || optval >= 64) {
578 				error = ERANGE;
579 				break;
580 			}
581 			group_mask = (uint64_t)1 << (optval - 1);
582 			NL_LOG(LOG_DEBUG2, "ADD/DEL group %d mask (%X)",
583 			    (uint32_t)optval, (uint32_t)group_mask);
584 
585 			NLCTL_WLOCK(ctl);
586 			if (sopt->sopt_name == NETLINK_ADD_MEMBERSHIP)
587 				groups = nlp->nl_groups | group_mask;
588 			else
589 				groups = nlp->nl_groups & ~group_mask;
590 			nl_update_groups_locked(nlp, groups);
591 			NLCTL_WUNLOCK(ctl);
592 			break;
593 		case NETLINK_CAP_ACK:
594 		case NETLINK_EXT_ACK:
595 		case NETLINK_GET_STRICT_CHK:
596 			sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
597 
598 			flag = nl_getoptflag(sopt->sopt_name);
599 
600 			NLCTL_WLOCK(ctl);
601 			if (optval != 0)
602 				nlp->nl_flags |= flag;
603 			else
604 				nlp->nl_flags &= ~flag;
605 			NLCTL_WUNLOCK(ctl);
606 			break;
607 		default:
608 			error = ENOPROTOOPT;
609 		}
610 		break;
611 	case SOPT_GET:
612 		switch (sopt->sopt_name) {
613 		case NETLINK_LIST_MEMBERSHIPS:
614 			NLCTL_RLOCK(ctl);
615 			optval = nlp->nl_groups;
616 			NLCTL_RUNLOCK(ctl);
617 			error = sooptcopyout(sopt, &optval, sizeof(optval));
618 			break;
619 		case NETLINK_CAP_ACK:
620 		case NETLINK_EXT_ACK:
621 		case NETLINK_GET_STRICT_CHK:
622 			NLCTL_RLOCK(ctl);
623 			optval = (nlp->nl_flags & nl_getoptflag(sopt->sopt_name)) != 0;
624 			NLCTL_RUNLOCK(ctl);
625 			error = sooptcopyout(sopt, &optval, sizeof(optval));
626 			break;
627 		default:
628 			error = ENOPROTOOPT;
629 		}
630 		break;
631 	default:
632 		error = ENOPROTOOPT;
633 	}
634 
635 	return (error);
636 }
637 
638 static int
639 nl_setsbopt(struct socket *so, struct sockopt *sopt)
640 {
641 	int error, optval;
642 	bool result;
643 
644 	if (sopt->sopt_name != SO_RCVBUF)
645 		return (sbsetopt(so, sopt));
646 
647 	/* Allow to override max buffer size in certain conditions */
648 
649 	error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
650 	if (error != 0)
651 		return (error);
652 	NL_LOG(LOG_DEBUG2, "socket %p, PID %d, SO_RCVBUF=%d", so, curproc->p_pid, optval);
653 	if (optval > sb_max_adj) {
654 		if (priv_check(curthread, PRIV_NET_ROUTE) != 0)
655 			return (EPERM);
656 	}
657 
658 	SOCK_RECVBUF_LOCK(so);
659 	result = sbreserve_locked_limit(so, SO_RCV, optval, nl_maxsockbuf, curthread);
660 	SOCK_RECVBUF_UNLOCK(so);
661 
662 	return (result ? 0 : ENOBUFS);
663 }
664 
665 static struct protosw netlinksw = {
666 	.pr_type = SOCK_RAW,
667 	.pr_flags = PR_ATOMIC | PR_ADDR | PR_WANTRCVD,
668 	.pr_ctloutput = nl_ctloutput,
669 	.pr_setsbopt = nl_setsbopt,
670 	.pr_abort = nl_pru_abort,
671 	.pr_attach = nl_pru_attach,
672 	.pr_bind = nl_pru_bind,
673 	.pr_connect = nl_pru_connect,
674 	.pr_detach = nl_pru_detach,
675 	.pr_disconnect = nl_pru_disconnect,
676 	.pr_peeraddr = nl_pru_peeraddr,
677 	.pr_send = nl_pru_send,
678 	.pr_rcvd = nl_pru_rcvd,
679 	.pr_shutdown = nl_pru_shutdown,
680 	.pr_sockaddr = nl_pru_sockaddr,
681 	.pr_close = nl_pru_close
682 };
683 
684 static struct domain netlinkdomain = {
685 	.dom_family = PF_NETLINK,
686 	.dom_name = "netlink",
687 	.dom_flags = DOMF_UNLOADABLE,
688 	.dom_nprotosw =		1,
689 	.dom_protosw =		{ &netlinksw },
690 };
691 
692 DOMAIN_SET(netlink);
693