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