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